blob: 095d673b9efb8d6ee2026f81456619c821401b68 [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>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040043#include <linux/sched.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
45#include "x86_emulate.h"
46#include "segment_descriptor.h"
47
48MODULE_AUTHOR("Qumranet");
49MODULE_LICENSE("GPL");
50
Avi Kivity133de902007-02-12 00:54:44 -080051static DEFINE_SPINLOCK(kvm_lock);
52static LIST_HEAD(vm_list);
53
Avi Kivity6aa8b732006-12-10 02:21:36 -080054struct kvm_arch_ops *kvm_arch_ops;
Avi Kivity1165f5f2007-04-19 17:27:43 +030055
56#define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
58static struct kvm_stats_debugfs_item {
59 const char *name;
Avi Kivity1165f5f2007-04-19 17:27:43 +030060 int offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -080061 struct dentry *dentry;
62} debugfs_entries[] = {
Avi Kivity1165f5f2007-04-19 17:27:43 +030063 { "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) },
Avi Kivitye6adf282007-04-30 16:07:54 +030075 { "light_exits", STAT_OFFSET(light_exits) },
Avi Kivity1165f5f2007-04-19 17:27:43 +030076 { NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080077};
78
79static struct dentry *debugfs_dir;
80
Avi Kivity37e29d92007-02-20 14:07:37 +020081struct vfsmount *kvmfs_mnt;
82
Avi Kivity6aa8b732006-12-10 02:21:36 -080083#define MAX_IO_MSRS 256
84
85#define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
86#define LMSW_GUEST_MASK 0x0eULL
87#define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
88#define CR8_RESEVED_BITS (~0x0fULL)
89#define EFER_RESERVED_BITS 0xfffffffffffff2fe
90
Avi Kivity05b3e0c2006-12-13 00:33:45 -080091#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -080092// LDT or TSS descriptor in the GDT. 16 bytes.
93struct segment_descriptor_64 {
94 struct segment_descriptor s;
95 u32 base_higher;
96 u32 pad_zero;
97};
98
99#endif
100
Avi Kivitybccf2152007-02-21 18:04:26 +0200101static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
102 unsigned long arg);
103
Avi Kivityf17abe92007-02-21 19:28:04 +0200104static struct inode *kvmfs_inode(struct file_operations *fops)
105{
106 int error = -ENOMEM;
107 struct inode *inode = new_inode(kvmfs_mnt->mnt_sb);
108
109 if (!inode)
110 goto eexit_1;
111
112 inode->i_fop = fops;
113
114 /*
115 * Mark the inode dirty from the very beginning,
116 * that way it will never be moved to the dirty
117 * list because mark_inode_dirty() will think
118 * that it already _is_ on the dirty list.
119 */
120 inode->i_state = I_DIRTY;
121 inode->i_mode = S_IRUSR | S_IWUSR;
122 inode->i_uid = current->fsuid;
123 inode->i_gid = current->fsgid;
124 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
125 return inode;
126
127eexit_1:
128 return ERR_PTR(error);
129}
130
131static struct file *kvmfs_file(struct inode *inode, void *private_data)
132{
133 struct file *file = get_empty_filp();
134
135 if (!file)
136 return ERR_PTR(-ENFILE);
137
138 file->f_path.mnt = mntget(kvmfs_mnt);
139 file->f_path.dentry = d_alloc_anon(inode);
140 if (!file->f_path.dentry)
141 return ERR_PTR(-ENOMEM);
142 file->f_mapping = inode->i_mapping;
143
144 file->f_pos = 0;
145 file->f_flags = O_RDWR;
146 file->f_op = inode->i_fop;
147 file->f_mode = FMODE_READ | FMODE_WRITE;
148 file->f_version = 0;
149 file->private_data = private_data;
150 return file;
151}
152
Avi Kivity6aa8b732006-12-10 02:21:36 -0800153unsigned long segment_base(u16 selector)
154{
155 struct descriptor_table gdt;
156 struct segment_descriptor *d;
157 unsigned long table_base;
158 typedef unsigned long ul;
159 unsigned long v;
160
161 if (selector == 0)
162 return 0;
163
164 asm ("sgdt %0" : "=m"(gdt));
165 table_base = gdt.base;
166
167 if (selector & 4) { /* from ldt */
168 u16 ldt_selector;
169
170 asm ("sldt %0" : "=g"(ldt_selector));
171 table_base = segment_base(ldt_selector);
172 }
173 d = (struct segment_descriptor *)(table_base + (selector & ~7));
174 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800175#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800176 if (d->system == 0
177 && (d->type == 2 || d->type == 9 || d->type == 11))
178 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
179#endif
180 return v;
181}
182EXPORT_SYMBOL_GPL(segment_base);
183
James Morris5aacf0c2006-12-22 01:04:55 -0800184static inline int valid_vcpu(int n)
185{
186 return likely(n >= 0 && n < KVM_MAX_VCPUS);
187}
188
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200189int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
190 void *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191{
192 unsigned char *host_buf = dest;
193 unsigned long req_size = size;
194
195 while (size) {
196 hpa_t paddr;
197 unsigned now;
198 unsigned offset;
199 hva_t guest_buf;
200
201 paddr = gva_to_hpa(vcpu, addr);
202
203 if (is_error_hpa(paddr))
204 break;
205
206 guest_buf = (hva_t)kmap_atomic(
207 pfn_to_page(paddr >> PAGE_SHIFT),
208 KM_USER0);
209 offset = addr & ~PAGE_MASK;
210 guest_buf |= offset;
211 now = min(size, PAGE_SIZE - offset);
212 memcpy(host_buf, (void*)guest_buf, now);
213 host_buf += now;
214 addr += now;
215 size -= now;
216 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
217 }
218 return req_size - size;
219}
220EXPORT_SYMBOL_GPL(kvm_read_guest);
221
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200222int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
223 void *data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224{
225 unsigned char *host_buf = data;
226 unsigned long req_size = size;
227
228 while (size) {
229 hpa_t paddr;
230 unsigned now;
231 unsigned offset;
232 hva_t guest_buf;
Uri Lublinab51a432007-02-21 18:25:21 +0200233 gfn_t gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234
235 paddr = gva_to_hpa(vcpu, addr);
236
237 if (is_error_hpa(paddr))
238 break;
239
Uri Lublinab51a432007-02-21 18:25:21 +0200240 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
241 mark_page_dirty(vcpu->kvm, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242 guest_buf = (hva_t)kmap_atomic(
243 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
244 offset = addr & ~PAGE_MASK;
245 guest_buf |= offset;
246 now = min(size, PAGE_SIZE - offset);
247 memcpy((void*)guest_buf, host_buf, now);
248 host_buf += now;
249 addr += now;
250 size -= now;
251 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
252 }
253 return req_size - size;
254}
255EXPORT_SYMBOL_GPL(kvm_write_guest);
256
Avi Kivity7702fd12007-06-14 16:27:40 +0300257void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
258{
259 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
260 return;
261
262 vcpu->guest_fpu_loaded = 1;
263 fx_save(vcpu->host_fx_image);
264 fx_restore(vcpu->guest_fx_image);
265}
266EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
267
268void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
269{
270 if (!vcpu->guest_fpu_loaded)
271 return;
272
273 vcpu->guest_fpu_loaded = 0;
274 fx_save(vcpu->guest_fx_image);
275 fx_restore(vcpu->host_fx_image);
276}
277EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
278
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279/*
280 * Switches to specified vcpu, until a matching vcpu_put()
281 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200282static void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283{
Avi Kivitybccf2152007-02-21 18:04:26 +0200284 mutex_lock(&vcpu->mutex);
285 kvm_arch_ops->vcpu_load(vcpu);
286}
287
288/*
289 * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
290 * if the slot is not populated.
291 */
292static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot)
293{
294 struct kvm_vcpu *vcpu = &kvm->vcpus[slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295
296 mutex_lock(&vcpu->mutex);
Avi Kivitybccf2152007-02-21 18:04:26 +0200297 if (!vcpu->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298 mutex_unlock(&vcpu->mutex);
Al Viro8b6d44c2007-02-09 16:38:40 +0000299 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300 }
Avi Kivitybccf2152007-02-21 18:04:26 +0200301 kvm_arch_ops->vcpu_load(vcpu);
302 return vcpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303}
304
305static void vcpu_put(struct kvm_vcpu *vcpu)
306{
307 kvm_arch_ops->vcpu_put(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308 mutex_unlock(&vcpu->mutex);
309}
310
Avi Kivityf17abe92007-02-21 19:28:04 +0200311static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312{
313 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
314 int i;
315
316 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200317 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800318
319 spin_lock_init(&kvm->lock);
320 INIT_LIST_HEAD(&kvm->active_mmu_pages);
321 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
322 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
323
324 mutex_init(&vcpu->mutex);
Avi Kivity133de902007-02-12 00:54:44 -0800325 vcpu->cpu = -1;
Avi Kivity86a2b422007-01-05 16:36:57 -0800326 vcpu->kvm = kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800327 vcpu->mmu.root_hpa = INVALID_PAGE;
328 INIT_LIST_HEAD(&vcpu->free_pages);
Avi Kivity133de902007-02-12 00:54:44 -0800329 spin_lock(&kvm_lock);
330 list_add(&kvm->vm_list, &vm_list);
331 spin_unlock(&kvm_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800332 }
Avi Kivityf17abe92007-02-21 19:28:04 +0200333 return kvm;
334}
335
336static int kvm_dev_open(struct inode *inode, struct file *filp)
337{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338 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)
Avi Kivity55a54f72006-12-29 16:49:58 -0800352 if (free->phys_mem[i])
353 __free_page(free->phys_mem[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800354 vfree(free->phys_mem);
355 }
356
357 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
358 vfree(free->dirty_bitmap);
359
Al Viro8b6d44c2007-02-09 16:38:40 +0000360 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000362 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800363}
364
365static void kvm_free_physmem(struct kvm *kvm)
366{
367 int i;
368
369 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000370 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371}
372
Avi Kivity039576c2007-03-20 12:46:50 +0200373static 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
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
385{
Avi Kivitybccf2152007-02-21 18:04:26 +0200386 if (!vcpu->vmcs)
Ingo Molnar1e8ba6f2007-02-12 00:54:42 -0800387 return;
388
Avi Kivitybccf2152007-02-21 18:04:26 +0200389 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390 kvm_mmu_destroy(vcpu);
Avi Kivity08438472007-01-22 20:40:38 -0800391 vcpu_put(vcpu);
Avi Kivity9ede74e2007-01-05 16:36:55 -0800392 kvm_arch_ops->vcpu_free(vcpu);
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200393 free_page((unsigned long)vcpu->run);
394 vcpu->run = NULL;
Avi Kivity039576c2007-03-20 12:46:50 +0200395 free_page((unsigned long)vcpu->pio_data);
396 vcpu->pio_data = NULL;
397 free_pio_guest_pages(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398}
399
400static void kvm_free_vcpus(struct kvm *kvm)
401{
402 unsigned int i;
403
404 for (i = 0; i < KVM_MAX_VCPUS; ++i)
405 kvm_free_vcpu(&kvm->vcpus[i]);
406}
407
408static int kvm_dev_release(struct inode *inode, struct file *filp)
409{
Avi Kivityf17abe92007-02-21 19:28:04 +0200410 return 0;
411}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412
Avi Kivityf17abe92007-02-21 19:28:04 +0200413static void kvm_destroy_vm(struct kvm *kvm)
414{
Avi Kivity133de902007-02-12 00:54:44 -0800415 spin_lock(&kvm_lock);
416 list_del(&kvm->vm_list);
417 spin_unlock(&kvm_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418 kvm_free_vcpus(kvm);
419 kvm_free_physmem(kvm);
420 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200421}
422
423static int kvm_vm_release(struct inode *inode, struct file *filp)
424{
425 struct kvm *kvm = filp->private_data;
426
427 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428 return 0;
429}
430
431static void inject_gp(struct kvm_vcpu *vcpu)
432{
433 kvm_arch_ops->inject_gp(vcpu, 0);
434}
435
Avi Kivity1342d352007-01-05 16:36:39 -0800436/*
437 * Load the pae pdptrs. Return true is they are all valid.
438 */
439static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440{
441 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800442 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443 int i;
444 u64 pdpte;
445 u64 *pdpt;
Avi Kivity1342d352007-01-05 16:36:39 -0800446 int ret;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300447 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448
449 spin_lock(&vcpu->kvm->lock);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300450 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
451 /* FIXME: !page - emulate? 0xff? */
452 pdpt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453
Avi Kivity1342d352007-01-05 16:36:39 -0800454 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455 for (i = 0; i < 4; ++i) {
456 pdpte = pdpt[offset + i];
Avi Kivity1342d352007-01-05 16:36:39 -0800457 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
458 ret = 0;
459 goto out;
460 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461 }
462
Avi Kivity1342d352007-01-05 16:36:39 -0800463 for (i = 0; i < 4; ++i)
464 vcpu->pdptrs[i] = pdpt[offset + i];
465
466out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467 kunmap_atomic(pdpt, KM_USER0);
468 spin_unlock(&vcpu->kvm->lock);
469
Avi Kivity1342d352007-01-05 16:36:39 -0800470 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800471}
472
473void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
474{
475 if (cr0 & CR0_RESEVED_BITS) {
476 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
477 cr0, vcpu->cr0);
478 inject_gp(vcpu);
479 return;
480 }
481
482 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
483 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
484 inject_gp(vcpu);
485 return;
486 }
487
488 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
489 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
490 "and a clear PE flag\n");
491 inject_gp(vcpu);
492 return;
493 }
494
495 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800496#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 if ((vcpu->shadow_efer & EFER_LME)) {
498 int cs_db, cs_l;
499
500 if (!is_pae(vcpu)) {
501 printk(KERN_DEBUG "set_cr0: #GP, start paging "
502 "in long mode while PAE is disabled\n");
503 inject_gp(vcpu);
504 return;
505 }
506 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
507 if (cs_l) {
508 printk(KERN_DEBUG "set_cr0: #GP, start paging "
509 "in long mode while CS.L == 1\n");
510 inject_gp(vcpu);
511 return;
512
513 }
514 } else
515#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800516 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
518 "reserved bits\n");
519 inject_gp(vcpu);
520 return;
521 }
522
523 }
524
525 kvm_arch_ops->set_cr0(vcpu, cr0);
526 vcpu->cr0 = cr0;
527
528 spin_lock(&vcpu->kvm->lock);
529 kvm_mmu_reset_context(vcpu);
530 spin_unlock(&vcpu->kvm->lock);
531 return;
532}
533EXPORT_SYMBOL_GPL(set_cr0);
534
535void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
536{
537 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
538}
539EXPORT_SYMBOL_GPL(lmsw);
540
541void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
542{
543 if (cr4 & CR4_RESEVED_BITS) {
544 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
545 inject_gp(vcpu);
546 return;
547 }
548
Avi Kivitya9058ec2006-12-29 16:49:37 -0800549 if (is_long_mode(vcpu)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550 if (!(cr4 & CR4_PAE_MASK)) {
551 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
552 "in long mode\n");
553 inject_gp(vcpu);
554 return;
555 }
556 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
Avi Kivity1342d352007-01-05 16:36:39 -0800557 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
559 inject_gp(vcpu);
560 }
561
562 if (cr4 & CR4_VMXE_MASK) {
563 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
564 inject_gp(vcpu);
565 return;
566 }
567 kvm_arch_ops->set_cr4(vcpu, cr4);
568 spin_lock(&vcpu->kvm->lock);
569 kvm_mmu_reset_context(vcpu);
570 spin_unlock(&vcpu->kvm->lock);
571}
572EXPORT_SYMBOL_GPL(set_cr4);
573
574void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
575{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800576 if (is_long_mode(vcpu)) {
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200577 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
579 inject_gp(vcpu);
580 return;
581 }
582 } else {
583 if (cr3 & CR3_RESEVED_BITS) {
584 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
585 inject_gp(vcpu);
586 return;
587 }
588 if (is_paging(vcpu) && is_pae(vcpu) &&
Avi Kivity1342d352007-01-05 16:36:39 -0800589 !load_pdptrs(vcpu, cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
591 "reserved bits\n");
592 inject_gp(vcpu);
593 return;
594 }
595 }
596
597 vcpu->cr3 = cr3;
598 spin_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800599 /*
600 * Does the new cr3 value map to physical memory? (Note, we
601 * catch an invalid cr3 even in real-mode, because it would
602 * cause trouble later on when we turn on paging anyway.)
603 *
604 * A real CPU would silently accept an invalid cr3 and would
605 * attempt to use it - with largely undefined (and often hard
606 * to debug) behavior on the guest side.
607 */
608 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
609 inject_gp(vcpu);
610 else
611 vcpu->mmu.new_cr3(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612 spin_unlock(&vcpu->kvm->lock);
613}
614EXPORT_SYMBOL_GPL(set_cr3);
615
616void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
617{
618 if ( cr8 & CR8_RESEVED_BITS) {
619 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
620 inject_gp(vcpu);
621 return;
622 }
623 vcpu->cr8 = cr8;
624}
625EXPORT_SYMBOL_GPL(set_cr8);
626
627void fx_init(struct kvm_vcpu *vcpu)
628{
629 struct __attribute__ ((__packed__)) fx_image_s {
630 u16 control; //fcw
631 u16 status; //fsw
632 u16 tag; // ftw
633 u16 opcode; //fop
634 u64 ip; // fpu ip
635 u64 operand;// fpu dp
636 u32 mxcsr;
637 u32 mxcsr_mask;
638
639 } *fx_image;
640
641 fx_save(vcpu->host_fx_image);
642 fpu_init();
643 fx_save(vcpu->guest_fx_image);
644 fx_restore(vcpu->host_fx_image);
645
646 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
647 fx_image->mxcsr = 0x1f80;
648 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
649 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
650}
651EXPORT_SYMBOL_GPL(fx_init);
652
Uri Lublin02b27c12007-02-22 17:15:33 +0200653static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
654{
655 spin_lock(&vcpu->kvm->lock);
656 kvm_mmu_slot_remove_write_access(vcpu, slot);
657 spin_unlock(&vcpu->kvm->lock);
658}
659
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661 * Allocate some memory and give it an address in the guest physical address
662 * space.
663 *
664 * Discontiguous memory is allowed, mostly for framebuffers.
665 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200666static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
667 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668{
669 int r;
670 gfn_t base_gfn;
671 unsigned long npages;
672 unsigned long i;
673 struct kvm_memory_slot *memslot;
674 struct kvm_memory_slot old, new;
675 int memory_config_version;
676
677 r = -EINVAL;
678 /* General sanity checks */
679 if (mem->memory_size & (PAGE_SIZE - 1))
680 goto out;
681 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
682 goto out;
683 if (mem->slot >= KVM_MEMORY_SLOTS)
684 goto out;
685 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
686 goto out;
687
688 memslot = &kvm->memslots[mem->slot];
689 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
690 npages = mem->memory_size >> PAGE_SHIFT;
691
692 if (!npages)
693 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
694
695raced:
696 spin_lock(&kvm->lock);
697
698 memory_config_version = kvm->memory_config_version;
699 new = old = *memslot;
700
701 new.base_gfn = base_gfn;
702 new.npages = npages;
703 new.flags = mem->flags;
704
705 /* Disallow changing a memory slot's size. */
706 r = -EINVAL;
707 if (npages && old.npages && npages != old.npages)
708 goto out_unlock;
709
710 /* Check for overlaps */
711 r = -EEXIST;
712 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
713 struct kvm_memory_slot *s = &kvm->memslots[i];
714
715 if (s == memslot)
716 continue;
717 if (!((base_gfn + npages <= s->base_gfn) ||
718 (base_gfn >= s->base_gfn + s->npages)))
719 goto out_unlock;
720 }
721 /*
722 * Do memory allocations outside lock. memory_config_version will
723 * detect any races.
724 */
725 spin_unlock(&kvm->lock);
726
727 /* Deallocate if slot is being removed */
728 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000729 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730
731 /* Free page dirty bitmap if unneeded */
732 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000733 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734
735 r = -ENOMEM;
736
737 /* Allocate if a slot is being created */
738 if (npages && !new.phys_mem) {
739 new.phys_mem = vmalloc(npages * sizeof(struct page *));
740
741 if (!new.phys_mem)
742 goto out_free;
743
744 memset(new.phys_mem, 0, npages * sizeof(struct page *));
745 for (i = 0; i < npages; ++i) {
746 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
747 | __GFP_ZERO);
748 if (!new.phys_mem[i])
749 goto out_free;
Markus Rechberger5972e952007-02-19 14:37:47 +0200750 set_page_private(new.phys_mem[i],0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 }
752 }
753
754 /* Allocate page dirty bitmap if needed */
755 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
756 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
757
758 new.dirty_bitmap = vmalloc(dirty_bytes);
759 if (!new.dirty_bitmap)
760 goto out_free;
761 memset(new.dirty_bitmap, 0, dirty_bytes);
762 }
763
764 spin_lock(&kvm->lock);
765
766 if (memory_config_version != kvm->memory_config_version) {
767 spin_unlock(&kvm->lock);
768 kvm_free_physmem_slot(&new, &old);
769 goto raced;
770 }
771
772 r = -EAGAIN;
773 if (kvm->busy)
774 goto out_unlock;
775
776 if (mem->slot >= kvm->nmemslots)
777 kvm->nmemslots = mem->slot + 1;
778
779 *memslot = new;
780 ++kvm->memory_config_version;
781
782 spin_unlock(&kvm->lock);
783
784 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
785 struct kvm_vcpu *vcpu;
786
Avi Kivitybccf2152007-02-21 18:04:26 +0200787 vcpu = vcpu_load_slot(kvm, i);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 if (!vcpu)
789 continue;
Uri Lublinff990d52007-02-22 17:37:32 +0200790 if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
791 do_remove_write_access(vcpu, mem->slot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792 kvm_mmu_reset_context(vcpu);
793 vcpu_put(vcpu);
794 }
795
796 kvm_free_physmem_slot(&old, &new);
797 return 0;
798
799out_unlock:
800 spin_unlock(&kvm->lock);
801out_free:
802 kvm_free_physmem_slot(&new, &old);
803out:
804 return r;
805}
806
807/*
808 * Get (and clear) the dirty memory log for a memory slot.
809 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200810static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
811 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812{
813 struct kvm_memory_slot *memslot;
814 int r, i;
815 int n;
Avi Kivity714b93d2007-01-05 16:36:53 -0800816 int cleared;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817 unsigned long any = 0;
818
819 spin_lock(&kvm->lock);
820
821 /*
822 * Prevent changes to guest memory configuration even while the lock
823 * is not taken.
824 */
825 ++kvm->busy;
826 spin_unlock(&kvm->lock);
827 r = -EINVAL;
828 if (log->slot >= KVM_MEMORY_SLOTS)
829 goto out;
830
831 memslot = &kvm->memslots[log->slot];
832 r = -ENOENT;
833 if (!memslot->dirty_bitmap)
834 goto out;
835
Uri Lublincd1a4a92007-02-22 16:43:09 +0200836 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837
Uri Lublincd1a4a92007-02-22 16:43:09 +0200838 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 any = memslot->dirty_bitmap[i];
840
841 r = -EFAULT;
842 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
843 goto out;
844
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845 if (any) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800846 cleared = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Avi Kivitybccf2152007-02-21 18:04:26 +0200848 struct kvm_vcpu *vcpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849
Avi Kivitybccf2152007-02-21 18:04:26 +0200850 vcpu = vcpu_load_slot(kvm, i);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851 if (!vcpu)
852 continue;
Avi Kivity714b93d2007-01-05 16:36:53 -0800853 if (!cleared) {
854 do_remove_write_access(vcpu, log->slot);
855 memset(memslot->dirty_bitmap, 0, n);
856 cleared = 1;
857 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 kvm_arch_ops->tlb_flush(vcpu);
859 vcpu_put(vcpu);
860 }
861 }
862
863 r = 0;
864
865out:
866 spin_lock(&kvm->lock);
867 --kvm->busy;
868 spin_unlock(&kvm->lock);
869 return r;
870}
871
Avi Kivitye8207542007-03-30 16:54:30 +0300872/*
873 * Set a new alias region. Aliases map a portion of physical memory into
874 * another portion. This is useful for memory windows, for example the PC
875 * VGA region.
876 */
877static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
878 struct kvm_memory_alias *alias)
879{
880 int r, n;
881 struct kvm_mem_alias *p;
882
883 r = -EINVAL;
884 /* General sanity checks */
885 if (alias->memory_size & (PAGE_SIZE - 1))
886 goto out;
887 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
888 goto out;
889 if (alias->slot >= KVM_ALIAS_SLOTS)
890 goto out;
891 if (alias->guest_phys_addr + alias->memory_size
892 < alias->guest_phys_addr)
893 goto out;
894 if (alias->target_phys_addr + alias->memory_size
895 < alias->target_phys_addr)
896 goto out;
897
898 spin_lock(&kvm->lock);
899
900 p = &kvm->aliases[alias->slot];
901 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
902 p->npages = alias->memory_size >> PAGE_SHIFT;
903 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
904
905 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
906 if (kvm->aliases[n - 1].npages)
907 break;
908 kvm->naliases = n;
909
910 spin_unlock(&kvm->lock);
911
912 vcpu_load(&kvm->vcpus[0]);
913 spin_lock(&kvm->lock);
914 kvm_mmu_zap_all(&kvm->vcpus[0]);
915 spin_unlock(&kvm->lock);
916 vcpu_put(&kvm->vcpus[0]);
917
918 return 0;
919
920out:
921 return r;
922}
923
924static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
925{
926 int i;
927 struct kvm_mem_alias *alias;
928
929 for (i = 0; i < kvm->naliases; ++i) {
930 alias = &kvm->aliases[i];
931 if (gfn >= alias->base_gfn
932 && gfn < alias->base_gfn + alias->npages)
933 return alias->target_gfn + gfn - alias->base_gfn;
934 }
935 return gfn;
936}
937
938static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939{
940 int i;
941
942 for (i = 0; i < kvm->nmemslots; ++i) {
943 struct kvm_memory_slot *memslot = &kvm->memslots[i];
944
945 if (gfn >= memslot->base_gfn
946 && gfn < memslot->base_gfn + memslot->npages)
947 return memslot;
948 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000949 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950}
Avi Kivitye8207542007-03-30 16:54:30 +0300951
952struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
953{
954 gfn = unalias_gfn(kvm, gfn);
955 return __gfn_to_memslot(kvm, gfn);
956}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957
Avi Kivity954bbbc2007-03-30 14:02:32 +0300958struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
959{
960 struct kvm_memory_slot *slot;
961
Avi Kivitye8207542007-03-30 16:54:30 +0300962 gfn = unalias_gfn(kvm, gfn);
963 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300964 if (!slot)
965 return NULL;
966 return slot->phys_mem[gfn - slot->base_gfn];
967}
968EXPORT_SYMBOL_GPL(gfn_to_page);
969
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
971{
972 int i;
Al Viro8b6d44c2007-02-09 16:38:40 +0000973 struct kvm_memory_slot *memslot = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 unsigned long rel_gfn;
975
976 for (i = 0; i < kvm->nmemslots; ++i) {
977 memslot = &kvm->memslots[i];
978
979 if (gfn >= memslot->base_gfn
980 && gfn < memslot->base_gfn + memslot->npages) {
981
982 if (!memslot || !memslot->dirty_bitmap)
983 return;
984
985 rel_gfn = gfn - memslot->base_gfn;
986
987 /* avoid RMW */
988 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
989 set_bit(rel_gfn, memslot->dirty_bitmap);
990 return;
991 }
992 }
993}
994
995static int emulator_read_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +0300996 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 unsigned int bytes,
998 struct x86_emulate_ctxt *ctxt)
999{
1000 struct kvm_vcpu *vcpu = ctxt->vcpu;
1001 void *data = val;
1002
1003 while (bytes) {
1004 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1005 unsigned offset = addr & (PAGE_SIZE-1);
1006 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1007 unsigned long pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001008 struct page *page;
1009 void *page_virt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010
1011 if (gpa == UNMAPPED_GVA)
1012 return X86EMUL_PROPAGATE_FAULT;
1013 pfn = gpa >> PAGE_SHIFT;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001014 page = gfn_to_page(vcpu->kvm, pfn);
1015 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 return X86EMUL_UNHANDLEABLE;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001017 page_virt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018
Avi Kivity954bbbc2007-03-30 14:02:32 +03001019 memcpy(data, page_virt + offset, tocopy);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020
Avi Kivity954bbbc2007-03-30 14:02:32 +03001021 kunmap_atomic(page_virt, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022
1023 bytes -= tocopy;
1024 data += tocopy;
1025 addr += tocopy;
1026 }
1027
1028 return X86EMUL_CONTINUE;
1029}
1030
1031static int emulator_write_std(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001032 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 unsigned int bytes,
1034 struct x86_emulate_ctxt *ctxt)
1035{
1036 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1037 addr, bytes);
1038 return X86EMUL_UNHANDLEABLE;
1039}
1040
1041static int emulator_read_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001042 void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 unsigned int bytes,
1044 struct x86_emulate_ctxt *ctxt)
1045{
1046 struct kvm_vcpu *vcpu = ctxt->vcpu;
1047
1048 if (vcpu->mmio_read_completed) {
1049 memcpy(val, vcpu->mmio_data, bytes);
1050 vcpu->mmio_read_completed = 0;
1051 return X86EMUL_CONTINUE;
1052 } else if (emulator_read_std(addr, val, bytes, ctxt)
1053 == X86EMUL_CONTINUE)
1054 return X86EMUL_CONTINUE;
1055 else {
1056 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001057
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058 if (gpa == UNMAPPED_GVA)
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001059 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 vcpu->mmio_needed = 1;
1061 vcpu->mmio_phys_addr = gpa;
1062 vcpu->mmio_size = bytes;
1063 vcpu->mmio_is_write = 0;
1064
1065 return X86EMUL_UNHANDLEABLE;
1066 }
1067}
1068
Avi Kivityda4a00f2007-01-05 16:36:44 -08001069static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
Avi Kivity4c690a12007-04-22 15:28:19 +03001070 const void *val, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001071{
Avi Kivityda4a00f2007-01-05 16:36:44 -08001072 struct page *page;
1073 void *virt;
Avi Kivity09072da2007-05-01 14:16:52 +03001074 unsigned offset = offset_in_page(gpa);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001075
1076 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1077 return 0;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001078 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1079 if (!page)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001080 return 0;
Uri Lublinab51a432007-02-21 18:25:21 +02001081 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001082 virt = kmap_atomic(page, KM_USER0);
Avi Kivity09072da2007-05-01 14:16:52 +03001083 kvm_mmu_pte_write(vcpu, gpa, virt + offset, val, bytes);
Avi Kivity4c690a12007-04-22 15:28:19 +03001084 memcpy(virt + offset_in_page(gpa), val, bytes);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001085 kunmap_atomic(virt, KM_USER0);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001086 return 1;
1087}
1088
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089static int emulator_write_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001090 const void *val,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091 unsigned int bytes,
1092 struct x86_emulate_ctxt *ctxt)
1093{
1094 struct kvm_vcpu *vcpu = ctxt->vcpu;
1095 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1096
Avi Kivityc9047f52007-04-17 10:53:22 +03001097 if (gpa == UNMAPPED_GVA) {
1098 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099 return X86EMUL_PROPAGATE_FAULT;
Avi Kivityc9047f52007-04-17 10:53:22 +03001100 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101
Avi Kivityda4a00f2007-01-05 16:36:44 -08001102 if (emulator_write_phys(vcpu, gpa, val, bytes))
1103 return X86EMUL_CONTINUE;
1104
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 vcpu->mmio_needed = 1;
1106 vcpu->mmio_phys_addr = gpa;
1107 vcpu->mmio_size = bytes;
1108 vcpu->mmio_is_write = 1;
Avi Kivity4c690a12007-04-22 15:28:19 +03001109 memcpy(vcpu->mmio_data, val, bytes);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110
1111 return X86EMUL_CONTINUE;
1112}
1113
1114static int emulator_cmpxchg_emulated(unsigned long addr,
Avi Kivity4c690a12007-04-22 15:28:19 +03001115 const void *old,
1116 const void *new,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 unsigned int bytes,
1118 struct x86_emulate_ctxt *ctxt)
1119{
1120 static int reported;
1121
1122 if (!reported) {
1123 reported = 1;
1124 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1125 }
1126 return emulator_write_emulated(addr, new, bytes, ctxt);
1127}
1128
1129static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1130{
1131 return kvm_arch_ops->get_segment_base(vcpu, seg);
1132}
1133
1134int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1135{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 return X86EMUL_CONTINUE;
1137}
1138
1139int emulate_clts(struct kvm_vcpu *vcpu)
1140{
Avi Kivity399badf2007-01-05 16:36:38 -08001141 unsigned long cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142
Avi Kivity399badf2007-01-05 16:36:38 -08001143 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144 kvm_arch_ops->set_cr0(vcpu, cr0);
1145 return X86EMUL_CONTINUE;
1146}
1147
1148int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1149{
1150 struct kvm_vcpu *vcpu = ctxt->vcpu;
1151
1152 switch (dr) {
1153 case 0 ... 3:
1154 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1155 return X86EMUL_CONTINUE;
1156 default:
1157 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1158 __FUNCTION__, dr);
1159 return X86EMUL_UNHANDLEABLE;
1160 }
1161}
1162
1163int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1164{
1165 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1166 int exception;
1167
1168 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1169 if (exception) {
1170 /* FIXME: better handling */
1171 return X86EMUL_UNHANDLEABLE;
1172 }
1173 return X86EMUL_CONTINUE;
1174}
1175
1176static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1177{
1178 static int reported;
1179 u8 opcodes[4];
1180 unsigned long rip = ctxt->vcpu->rip;
1181 unsigned long rip_linear;
1182
1183 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1184
1185 if (reported)
1186 return;
1187
1188 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1189
1190 printk(KERN_ERR "emulation failed but !mmio_needed?"
1191 " rip %lx %02x %02x %02x %02x\n",
1192 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1193 reported = 1;
1194}
1195
1196struct x86_emulate_ops emulate_ops = {
1197 .read_std = emulator_read_std,
1198 .write_std = emulator_write_std,
1199 .read_emulated = emulator_read_emulated,
1200 .write_emulated = emulator_write_emulated,
1201 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1202};
1203
1204int emulate_instruction(struct kvm_vcpu *vcpu,
1205 struct kvm_run *run,
1206 unsigned long cr2,
1207 u16 error_code)
1208{
1209 struct x86_emulate_ctxt emulate_ctxt;
1210 int r;
1211 int cs_db, cs_l;
1212
Avi Kivitye7df56e2007-03-14 15:54:54 +02001213 vcpu->mmio_fault_cr2 = cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214 kvm_arch_ops->cache_regs(vcpu);
1215
1216 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1217
1218 emulate_ctxt.vcpu = vcpu;
1219 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1220 emulate_ctxt.cr2 = cr2;
1221 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1222 ? X86EMUL_MODE_REAL : cs_l
1223 ? X86EMUL_MODE_PROT64 : cs_db
1224 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1225
1226 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1227 emulate_ctxt.cs_base = 0;
1228 emulate_ctxt.ds_base = 0;
1229 emulate_ctxt.es_base = 0;
1230 emulate_ctxt.ss_base = 0;
1231 } else {
1232 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1233 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1234 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1235 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1236 }
1237
1238 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1239 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1240
1241 vcpu->mmio_is_write = 0;
1242 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1243
1244 if ((r || vcpu->mmio_is_write) && run) {
1245 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1246 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1247 run->mmio.len = vcpu->mmio_size;
1248 run->mmio.is_write = vcpu->mmio_is_write;
1249 }
1250
1251 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001252 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1253 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254 if (!vcpu->mmio_needed) {
1255 report_emulation_failure(&emulate_ctxt);
1256 return EMULATE_FAIL;
1257 }
1258 return EMULATE_DO_MMIO;
1259 }
1260
1261 kvm_arch_ops->decache_regs(vcpu);
1262 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1263
Avi Kivity02c83202007-04-29 15:02:17 +03001264 if (vcpu->mmio_is_write) {
1265 vcpu->mmio_needed = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 return EMULATE_DO_MMIO;
Avi Kivity02c83202007-04-29 15:02:17 +03001267 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268
1269 return EMULATE_DONE;
1270}
1271EXPORT_SYMBOL_GPL(emulate_instruction);
1272
Avi Kivity270fd9b2007-02-19 14:37:47 +02001273int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1274{
1275 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1276
Dor Laor9b22bf52007-02-19 16:44:49 +02001277 kvm_arch_ops->cache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001278 ret = -KVM_EINVAL;
1279#ifdef CONFIG_X86_64
1280 if (is_long_mode(vcpu)) {
1281 nr = vcpu->regs[VCPU_REGS_RAX];
1282 a0 = vcpu->regs[VCPU_REGS_RDI];
1283 a1 = vcpu->regs[VCPU_REGS_RSI];
1284 a2 = vcpu->regs[VCPU_REGS_RDX];
1285 a3 = vcpu->regs[VCPU_REGS_RCX];
1286 a4 = vcpu->regs[VCPU_REGS_R8];
1287 a5 = vcpu->regs[VCPU_REGS_R9];
1288 } else
1289#endif
1290 {
1291 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1292 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1293 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1294 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1295 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1296 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1297 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1298 }
1299 switch (nr) {
1300 default:
Avi Kivityb4e63f52007-03-04 13:59:30 +02001301 run->hypercall.args[0] = a0;
1302 run->hypercall.args[1] = a1;
1303 run->hypercall.args[2] = a2;
1304 run->hypercall.args[3] = a3;
1305 run->hypercall.args[4] = a4;
1306 run->hypercall.args[5] = a5;
1307 run->hypercall.ret = ret;
1308 run->hypercall.longmode = is_long_mode(vcpu);
1309 kvm_arch_ops->decache_regs(vcpu);
1310 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001311 }
1312 vcpu->regs[VCPU_REGS_RAX] = ret;
Dor Laor9b22bf52007-02-19 16:44:49 +02001313 kvm_arch_ops->decache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001314 return 1;
1315}
1316EXPORT_SYMBOL_GPL(kvm_hypercall);
1317
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1319{
1320 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1321}
1322
1323void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1324{
1325 struct descriptor_table dt = { limit, base };
1326
1327 kvm_arch_ops->set_gdt(vcpu, &dt);
1328}
1329
1330void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1331{
1332 struct descriptor_table dt = { limit, base };
1333
1334 kvm_arch_ops->set_idt(vcpu, &dt);
1335}
1336
1337void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1338 unsigned long *rflags)
1339{
1340 lmsw(vcpu, msw);
1341 *rflags = kvm_arch_ops->get_rflags(vcpu);
1342}
1343
1344unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1345{
Anthony Liguori25c4c272007-04-27 09:29:21 +03001346 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 switch (cr) {
1348 case 0:
1349 return vcpu->cr0;
1350 case 2:
1351 return vcpu->cr2;
1352 case 3:
1353 return vcpu->cr3;
1354 case 4:
1355 return vcpu->cr4;
1356 default:
1357 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1358 return 0;
1359 }
1360}
1361
1362void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1363 unsigned long *rflags)
1364{
1365 switch (cr) {
1366 case 0:
1367 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1368 *rflags = kvm_arch_ops->get_rflags(vcpu);
1369 break;
1370 case 2:
1371 vcpu->cr2 = val;
1372 break;
1373 case 3:
1374 set_cr3(vcpu, val);
1375 break;
1376 case 4:
1377 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1378 break;
1379 default:
1380 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1381 }
1382}
1383
Ingo Molnar102d8322007-02-19 14:37:47 +02001384/*
1385 * Register the para guest with the host:
1386 */
1387static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1388{
1389 struct kvm_vcpu_para_state *para_state;
1390 hpa_t para_state_hpa, hypercall_hpa;
1391 struct page *para_state_page;
1392 unsigned char *hypercall;
1393 gpa_t hypercall_gpa;
1394
1395 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1396 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1397
1398 /*
1399 * Needs to be page aligned:
1400 */
1401 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1402 goto err_gp;
1403
1404 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1405 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1406 if (is_error_hpa(para_state_hpa))
1407 goto err_gp;
1408
Uri Lublinab51a432007-02-21 18:25:21 +02001409 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001410 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1411 para_state = kmap_atomic(para_state_page, KM_USER0);
1412
1413 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1414 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1415
1416 para_state->host_version = KVM_PARA_API_VERSION;
1417 /*
1418 * We cannot support guests that try to register themselves
1419 * with a newer API version than the host supports:
1420 */
1421 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1422 para_state->ret = -KVM_EINVAL;
1423 goto err_kunmap_skip;
1424 }
1425
1426 hypercall_gpa = para_state->hypercall_gpa;
1427 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1428 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1429 if (is_error_hpa(hypercall_hpa)) {
1430 para_state->ret = -KVM_EINVAL;
1431 goto err_kunmap_skip;
1432 }
1433
1434 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1435 vcpu->para_state_page = para_state_page;
1436 vcpu->para_state_gpa = para_state_gpa;
1437 vcpu->hypercall_gpa = hypercall_gpa;
1438
Uri Lublinab51a432007-02-21 18:25:21 +02001439 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001440 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1441 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1442 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1443 kunmap_atomic(hypercall, KM_USER1);
1444
1445 para_state->ret = 0;
1446err_kunmap_skip:
1447 kunmap_atomic(para_state, KM_USER0);
1448 return 0;
1449err_gp:
1450 return 1;
1451}
1452
Avi Kivity3bab1f52006-12-29 16:49:48 -08001453int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1454{
1455 u64 data;
1456
1457 switch (msr) {
1458 case 0xc0010010: /* SYSCFG */
1459 case 0xc0010015: /* HWCR */
1460 case MSR_IA32_PLATFORM_ID:
1461 case MSR_IA32_P5_MC_ADDR:
1462 case MSR_IA32_P5_MC_TYPE:
1463 case MSR_IA32_MC0_CTL:
1464 case MSR_IA32_MCG_STATUS:
1465 case MSR_IA32_MCG_CAP:
1466 case MSR_IA32_MC0_MISC:
1467 case MSR_IA32_MC0_MISC+4:
1468 case MSR_IA32_MC0_MISC+8:
1469 case MSR_IA32_MC0_MISC+12:
1470 case MSR_IA32_MC0_MISC+16:
1471 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001472 case MSR_IA32_PERF_STATUS:
Matthew Gregan2dc70942007-05-06 10:59:46 +03001473 case MSR_IA32_EBL_CR_POWERON:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001474 /* MTRR registers */
1475 case 0xfe:
1476 case 0x200 ... 0x2ff:
1477 data = 0;
1478 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001479 case 0xcd: /* fsb frequency */
1480 data = 3;
1481 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001482 case MSR_IA32_APICBASE:
1483 data = vcpu->apic_base;
1484 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001485 case MSR_IA32_MISC_ENABLE:
1486 data = vcpu->ia32_misc_enable_msr;
1487 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001488#ifdef CONFIG_X86_64
1489 case MSR_EFER:
1490 data = vcpu->shadow_efer;
1491 break;
1492#endif
1493 default:
1494 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1495 return 1;
1496 }
1497 *pdata = data;
1498 return 0;
1499}
1500EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1501
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502/*
1503 * Reads an msr value (of 'msr_index') into 'pdata'.
1504 * Returns 0 on success, non-0 otherwise.
1505 * Assumes vcpu_load() was already called.
1506 */
1507static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1508{
1509 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1510}
1511
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001512#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513
Avi Kivity3bab1f52006-12-29 16:49:48 -08001514static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516 if (efer & EFER_RESERVED_BITS) {
1517 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1518 efer);
1519 inject_gp(vcpu);
1520 return;
1521 }
1522
1523 if (is_paging(vcpu)
1524 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1525 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1526 inject_gp(vcpu);
1527 return;
1528 }
1529
Avi Kivity7725f0b2006-12-13 00:34:01 -08001530 kvm_arch_ops->set_efer(vcpu, efer);
1531
Avi Kivity6aa8b732006-12-10 02:21:36 -08001532 efer &= ~EFER_LMA;
1533 efer |= vcpu->shadow_efer & EFER_LMA;
1534
1535 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001536}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537
1538#endif
1539
Avi Kivity3bab1f52006-12-29 16:49:48 -08001540int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1541{
1542 switch (msr) {
1543#ifdef CONFIG_X86_64
1544 case MSR_EFER:
1545 set_efer(vcpu, data);
1546 break;
1547#endif
1548 case MSR_IA32_MC0_STATUS:
1549 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1550 __FUNCTION__, data);
1551 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001552 case MSR_IA32_MCG_STATUS:
1553 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1554 __FUNCTION__, data);
1555 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001556 case MSR_IA32_UCODE_REV:
1557 case MSR_IA32_UCODE_WRITE:
1558 case 0x200 ... 0x2ff: /* MTRRs */
1559 break;
1560 case MSR_IA32_APICBASE:
1561 vcpu->apic_base = data;
1562 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001563 case MSR_IA32_MISC_ENABLE:
1564 vcpu->ia32_misc_enable_msr = data;
1565 break;
Ingo Molnar102d8322007-02-19 14:37:47 +02001566 /*
1567 * This is the 'probe whether the host is KVM' logic:
1568 */
1569 case MSR_KVM_API_MAGIC:
1570 return vcpu_register_para(vcpu, data);
1571
Avi Kivity3bab1f52006-12-29 16:49:48 -08001572 default:
1573 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1574 return 1;
1575 }
1576 return 0;
1577}
1578EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1579
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580/*
1581 * Writes msr value into into the appropriate "register".
1582 * Returns 0 on success, non-0 otherwise.
1583 * Assumes vcpu_load() was already called.
1584 */
1585static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1586{
1587 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1588}
1589
1590void kvm_resched(struct kvm_vcpu *vcpu)
1591{
Yaozu Dong3fca0362007-04-25 16:49:19 +03001592 if (!need_resched())
1593 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594 vcpu_put(vcpu);
1595 cond_resched();
Avi Kivitybccf2152007-02-21 18:04:26 +02001596 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597}
1598EXPORT_SYMBOL_GPL(kvm_resched);
1599
1600void load_msrs(struct vmx_msr_entry *e, int n)
1601{
1602 int i;
1603
1604 for (i = 0; i < n; ++i)
1605 wrmsrl(e[i].index, e[i].data);
1606}
1607EXPORT_SYMBOL_GPL(load_msrs);
1608
1609void save_msrs(struct vmx_msr_entry *e, int n)
1610{
1611 int i;
1612
1613 for (i = 0; i < n; ++i)
1614 rdmsrl(e[i].index, e[i].data);
1615}
1616EXPORT_SYMBOL_GPL(save_msrs);
1617
Avi Kivity06465c52007-02-28 20:46:53 +02001618void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1619{
1620 int i;
1621 u32 function;
1622 struct kvm_cpuid_entry *e, *best;
1623
1624 kvm_arch_ops->cache_regs(vcpu);
1625 function = vcpu->regs[VCPU_REGS_RAX];
1626 vcpu->regs[VCPU_REGS_RAX] = 0;
1627 vcpu->regs[VCPU_REGS_RBX] = 0;
1628 vcpu->regs[VCPU_REGS_RCX] = 0;
1629 vcpu->regs[VCPU_REGS_RDX] = 0;
1630 best = NULL;
1631 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1632 e = &vcpu->cpuid_entries[i];
1633 if (e->function == function) {
1634 best = e;
1635 break;
1636 }
1637 /*
1638 * Both basic or both extended?
1639 */
1640 if (((e->function ^ function) & 0x80000000) == 0)
1641 if (!best || e->function > best->function)
1642 best = e;
1643 }
1644 if (best) {
1645 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1646 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1647 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1648 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1649 }
1650 kvm_arch_ops->decache_regs(vcpu);
1651 kvm_arch_ops->skip_emulated_instruction(vcpu);
1652}
1653EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1654
Avi Kivity039576c2007-03-20 12:46:50 +02001655static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001656{
Avi Kivity039576c2007-03-20 12:46:50 +02001657 void *p = vcpu->pio_data;
1658 void *q;
1659 unsigned bytes;
1660 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1661
1662 kvm_arch_ops->vcpu_put(vcpu);
1663 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1664 PAGE_KERNEL);
1665 if (!q) {
1666 kvm_arch_ops->vcpu_load(vcpu);
1667 free_pio_guest_pages(vcpu);
1668 return -ENOMEM;
1669 }
1670 q += vcpu->pio.guest_page_offset;
1671 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1672 if (vcpu->pio.in)
1673 memcpy(q, p, bytes);
1674 else
1675 memcpy(p, q, bytes);
1676 q -= vcpu->pio.guest_page_offset;
1677 vunmap(q);
1678 kvm_arch_ops->vcpu_load(vcpu);
1679 free_pio_guest_pages(vcpu);
1680 return 0;
1681}
1682
1683static int complete_pio(struct kvm_vcpu *vcpu)
1684{
1685 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001686 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001687 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001688
1689 kvm_arch_ops->cache_regs(vcpu);
1690
1691 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001692 if (io->in)
1693 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001694 io->size);
1695 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001696 if (io->in) {
1697 r = pio_copy_data(vcpu);
1698 if (r) {
1699 kvm_arch_ops->cache_regs(vcpu);
1700 return r;
1701 }
1702 }
1703
Avi Kivity46fc1472007-02-22 19:39:30 +02001704 delta = 1;
1705 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001706 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001707 /*
1708 * The size of the register should really depend on
1709 * current address size.
1710 */
1711 vcpu->regs[VCPU_REGS_RCX] -= delta;
1712 }
Avi Kivity039576c2007-03-20 12:46:50 +02001713 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001714 delta = -delta;
1715 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001716 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001717 vcpu->regs[VCPU_REGS_RDI] += delta;
1718 else
1719 vcpu->regs[VCPU_REGS_RSI] += delta;
1720 }
1721
Avi Kivity46fc1472007-02-22 19:39:30 +02001722 kvm_arch_ops->decache_regs(vcpu);
1723
Avi Kivity039576c2007-03-20 12:46:50 +02001724 io->count -= io->cur_count;
1725 io->cur_count = 0;
1726
1727 if (!io->count)
1728 kvm_arch_ops->skip_emulated_instruction(vcpu);
1729 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001730}
1731
Avi Kivity039576c2007-03-20 12:46:50 +02001732int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1733 int size, unsigned long count, int string, int down,
1734 gva_t address, int rep, unsigned port)
1735{
1736 unsigned now, in_page;
1737 int i;
1738 int nr_pages = 1;
1739 struct page *page;
1740
1741 vcpu->run->exit_reason = KVM_EXIT_IO;
1742 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1743 vcpu->run->io.size = size;
1744 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1745 vcpu->run->io.count = count;
1746 vcpu->run->io.port = port;
1747 vcpu->pio.count = count;
1748 vcpu->pio.cur_count = count;
1749 vcpu->pio.size = size;
1750 vcpu->pio.in = in;
1751 vcpu->pio.string = string;
1752 vcpu->pio.down = down;
1753 vcpu->pio.guest_page_offset = offset_in_page(address);
1754 vcpu->pio.rep = rep;
1755
1756 if (!string) {
1757 kvm_arch_ops->cache_regs(vcpu);
1758 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1759 kvm_arch_ops->decache_regs(vcpu);
1760 return 0;
1761 }
1762
1763 if (!count) {
1764 kvm_arch_ops->skip_emulated_instruction(vcpu);
1765 return 1;
1766 }
1767
1768 now = min(count, PAGE_SIZE / size);
1769
1770 if (!down)
1771 in_page = PAGE_SIZE - offset_in_page(address);
1772 else
1773 in_page = offset_in_page(address) + size;
1774 now = min(count, (unsigned long)in_page / size);
1775 if (!now) {
1776 /*
1777 * String I/O straddles page boundary. Pin two guest pages
1778 * so that we satisfy atomicity constraints. Do just one
1779 * transaction to avoid complexity.
1780 */
1781 nr_pages = 2;
1782 now = 1;
1783 }
1784 if (down) {
1785 /*
1786 * String I/O in reverse. Yuck. Kill the guest, fix later.
1787 */
1788 printk(KERN_ERR "kvm: guest string pio down\n");
1789 inject_gp(vcpu);
1790 return 1;
1791 }
1792 vcpu->run->io.count = now;
1793 vcpu->pio.cur_count = now;
1794
1795 for (i = 0; i < nr_pages; ++i) {
1796 spin_lock(&vcpu->kvm->lock);
1797 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1798 if (page)
1799 get_page(page);
1800 vcpu->pio.guest_pages[i] = page;
1801 spin_unlock(&vcpu->kvm->lock);
1802 if (!page) {
1803 inject_gp(vcpu);
1804 free_pio_guest_pages(vcpu);
1805 return 1;
1806 }
1807 }
1808
1809 if (!vcpu->pio.in)
1810 return pio_copy_data(vcpu);
1811 return 0;
1812}
1813EXPORT_SYMBOL_GPL(kvm_setup_pio);
1814
Avi Kivitybccf2152007-02-21 18:04:26 +02001815static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02001818 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001819
Avi Kivitybccf2152007-02-21 18:04:26 +02001820 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001821
Avi Kivity1961d272007-03-05 19:46:05 +02001822 if (vcpu->sigset_active)
1823 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1824
Dor Laor54810342007-02-12 00:54:39 -08001825 /* re-sync apic's tpr */
1826 vcpu->cr8 = kvm_run->cr8;
1827
Avi Kivity02c83202007-04-29 15:02:17 +03001828 if (vcpu->pio.cur_count) {
1829 r = complete_pio(vcpu);
1830 if (r)
1831 goto out;
1832 }
1833
1834 if (vcpu->mmio_needed) {
1835 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1836 vcpu->mmio_read_completed = 1;
1837 vcpu->mmio_needed = 0;
1838 r = emulate_instruction(vcpu, kvm_run,
1839 vcpu->mmio_fault_cr2, 0);
1840 if (r == EMULATE_DO_MMIO) {
1841 /*
1842 * Read-modify-write. Back to userspace.
1843 */
1844 kvm_run->exit_reason = KVM_EXIT_MMIO;
1845 r = 0;
1846 goto out;
Avi Kivity46fc1472007-02-22 19:39:30 +02001847 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001848 }
1849
Avi Kivity8eb7d332007-03-04 14:17:08 +02001850 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Avi Kivityb4e63f52007-03-04 13:59:30 +02001851 kvm_arch_ops->cache_regs(vcpu);
1852 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1853 kvm_arch_ops->decache_regs(vcpu);
1854 }
1855
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856 r = kvm_arch_ops->run(vcpu, kvm_run);
1857
Avi Kivity039576c2007-03-20 12:46:50 +02001858out:
Avi Kivity1961d272007-03-05 19:46:05 +02001859 if (vcpu->sigset_active)
1860 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1861
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862 vcpu_put(vcpu);
1863 return r;
1864}
1865
Avi Kivitybccf2152007-02-21 18:04:26 +02001866static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1867 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868{
Avi Kivitybccf2152007-02-21 18:04:26 +02001869 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870
1871 kvm_arch_ops->cache_regs(vcpu);
1872
1873 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1874 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1875 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1876 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1877 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1878 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1879 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1880 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001881#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001882 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1883 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1884 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1885 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1886 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1887 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1888 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1889 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1890#endif
1891
1892 regs->rip = vcpu->rip;
1893 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1894
1895 /*
1896 * Don't leak debug flags in case they were set for guest debugging
1897 */
1898 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1899 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1900
1901 vcpu_put(vcpu);
1902
1903 return 0;
1904}
1905
Avi Kivitybccf2152007-02-21 18:04:26 +02001906static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1907 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908{
Avi Kivitybccf2152007-02-21 18:04:26 +02001909 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910
1911 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1912 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1913 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1914 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1915 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1916 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1917 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1918 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001919#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1921 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1922 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1923 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1924 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1925 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1926 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1927 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1928#endif
1929
1930 vcpu->rip = regs->rip;
1931 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1932
1933 kvm_arch_ops->decache_regs(vcpu);
1934
1935 vcpu_put(vcpu);
1936
1937 return 0;
1938}
1939
1940static void get_segment(struct kvm_vcpu *vcpu,
1941 struct kvm_segment *var, int seg)
1942{
1943 return kvm_arch_ops->get_segment(vcpu, var, seg);
1944}
1945
Avi Kivitybccf2152007-02-21 18:04:26 +02001946static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1947 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949 struct descriptor_table dt;
1950
Avi Kivitybccf2152007-02-21 18:04:26 +02001951 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952
1953 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1954 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1955 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1956 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1957 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1958 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1959
1960 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1961 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1962
1963 kvm_arch_ops->get_idt(vcpu, &dt);
1964 sregs->idt.limit = dt.limit;
1965 sregs->idt.base = dt.base;
1966 kvm_arch_ops->get_gdt(vcpu, &dt);
1967 sregs->gdt.limit = dt.limit;
1968 sregs->gdt.base = dt.base;
1969
Anthony Liguori25c4c272007-04-27 09:29:21 +03001970 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001971 sregs->cr0 = vcpu->cr0;
1972 sregs->cr2 = vcpu->cr2;
1973 sregs->cr3 = vcpu->cr3;
1974 sregs->cr4 = vcpu->cr4;
1975 sregs->cr8 = vcpu->cr8;
1976 sregs->efer = vcpu->shadow_efer;
1977 sregs->apic_base = vcpu->apic_base;
1978
1979 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1980 sizeof sregs->interrupt_bitmap);
1981
1982 vcpu_put(vcpu);
1983
1984 return 0;
1985}
1986
1987static void set_segment(struct kvm_vcpu *vcpu,
1988 struct kvm_segment *var, int seg)
1989{
1990 return kvm_arch_ops->set_segment(vcpu, var, seg);
1991}
1992
Avi Kivitybccf2152007-02-21 18:04:26 +02001993static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1994 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001995{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996 int mmu_reset_needed = 0;
1997 int i;
1998 struct descriptor_table dt;
1999
Avi Kivitybccf2152007-02-21 18:04:26 +02002000 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002001
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002 dt.limit = sregs->idt.limit;
2003 dt.base = sregs->idt.base;
2004 kvm_arch_ops->set_idt(vcpu, &dt);
2005 dt.limit = sregs->gdt.limit;
2006 dt.base = sregs->gdt.base;
2007 kvm_arch_ops->set_gdt(vcpu, &dt);
2008
2009 vcpu->cr2 = sregs->cr2;
2010 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2011 vcpu->cr3 = sregs->cr3;
2012
2013 vcpu->cr8 = sregs->cr8;
2014
2015 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002016#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002017 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2018#endif
2019 vcpu->apic_base = sregs->apic_base;
2020
Anthony Liguori25c4c272007-04-27 09:29:21 +03002021 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
Avi Kivity399badf2007-01-05 16:36:38 -08002022
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Avi Kivityf6528b02007-03-20 18:44:51 +02002024 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025
2026 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2027 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002028 if (!is_long_mode(vcpu) && is_pae(vcpu))
2029 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002030
2031 if (mmu_reset_needed)
2032 kvm_mmu_reset_context(vcpu);
2033
2034 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2035 sizeof vcpu->irq_pending);
2036 vcpu->irq_summary = 0;
2037 for (i = 0; i < NR_IRQ_WORDS; ++i)
2038 if (vcpu->irq_pending[i])
2039 __set_bit(i, &vcpu->irq_summary);
2040
Avi Kivity024aa1c2007-03-21 13:44:58 +02002041 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2042 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2043 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2044 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2045 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2046 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2047
2048 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2049 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2050
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051 vcpu_put(vcpu);
2052
2053 return 0;
2054}
2055
2056/*
2057 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2058 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002059 *
2060 * This list is modified at module load time to reflect the
2061 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062 */
2063static u32 msrs_to_save[] = {
2064 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2065 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002066#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002067 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2068#endif
2069 MSR_IA32_TIME_STAMP_COUNTER,
2070};
2071
Michael Riepebf591b22006-12-22 01:05:36 -08002072static unsigned num_msrs_to_save;
2073
Avi Kivity6f00e682007-01-26 00:56:40 -08002074static u32 emulated_msrs[] = {
2075 MSR_IA32_MISC_ENABLE,
2076};
2077
Michael Riepebf591b22006-12-22 01:05:36 -08002078static __init void kvm_init_msr_list(void)
2079{
2080 u32 dummy[2];
2081 unsigned i, j;
2082
2083 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2084 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2085 continue;
2086 if (j < i)
2087 msrs_to_save[j] = msrs_to_save[i];
2088 j++;
2089 }
2090 num_msrs_to_save = j;
2091}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002092
2093/*
2094 * Adapt set_msr() to msr_io()'s calling convention
2095 */
2096static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2097{
2098 return set_msr(vcpu, index, *data);
2099}
2100
2101/*
2102 * Read or write a bunch of msrs. All parameters are kernel addresses.
2103 *
2104 * @return number of msrs set successfully.
2105 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002106static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107 struct kvm_msr_entry *entries,
2108 int (*do_msr)(struct kvm_vcpu *vcpu,
2109 unsigned index, u64 *data))
2110{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111 int i;
2112
Avi Kivitybccf2152007-02-21 18:04:26 +02002113 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114
2115 for (i = 0; i < msrs->nmsrs; ++i)
2116 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2117 break;
2118
2119 vcpu_put(vcpu);
2120
2121 return i;
2122}
2123
2124/*
2125 * Read or write a bunch of msrs. Parameters are user addresses.
2126 *
2127 * @return number of msrs set successfully.
2128 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002129static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130 int (*do_msr)(struct kvm_vcpu *vcpu,
2131 unsigned index, u64 *data),
2132 int writeback)
2133{
2134 struct kvm_msrs msrs;
2135 struct kvm_msr_entry *entries;
2136 int r, n;
2137 unsigned size;
2138
2139 r = -EFAULT;
2140 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2141 goto out;
2142
2143 r = -E2BIG;
2144 if (msrs.nmsrs >= MAX_IO_MSRS)
2145 goto out;
2146
2147 r = -ENOMEM;
2148 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2149 entries = vmalloc(size);
2150 if (!entries)
2151 goto out;
2152
2153 r = -EFAULT;
2154 if (copy_from_user(entries, user_msrs->entries, size))
2155 goto out_free;
2156
Avi Kivitybccf2152007-02-21 18:04:26 +02002157 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158 if (r < 0)
2159 goto out_free;
2160
2161 r = -EFAULT;
2162 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2163 goto out_free;
2164
2165 r = n;
2166
2167out_free:
2168 vfree(entries);
2169out:
2170 return r;
2171}
2172
2173/*
2174 * Translate a guest virtual address to a guest physical address.
2175 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002176static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2177 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178{
2179 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180 gpa_t gpa;
2181
Avi Kivitybccf2152007-02-21 18:04:26 +02002182 vcpu_load(vcpu);
2183 spin_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2185 tr->physical_address = gpa;
2186 tr->valid = gpa != UNMAPPED_GVA;
2187 tr->writeable = 1;
2188 tr->usermode = 0;
Avi Kivitybccf2152007-02-21 18:04:26 +02002189 spin_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190 vcpu_put(vcpu);
2191
2192 return 0;
2193}
2194
Avi Kivitybccf2152007-02-21 18:04:26 +02002195static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2196 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198 if (irq->irq < 0 || irq->irq >= 256)
2199 return -EINVAL;
Avi Kivitybccf2152007-02-21 18:04:26 +02002200 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201
2202 set_bit(irq->irq, vcpu->irq_pending);
2203 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2204
2205 vcpu_put(vcpu);
2206
2207 return 0;
2208}
2209
Avi Kivitybccf2152007-02-21 18:04:26 +02002210static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2211 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213 int r;
2214
Avi Kivitybccf2152007-02-21 18:04:26 +02002215 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216
2217 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2218
2219 vcpu_put(vcpu);
2220
2221 return r;
2222}
2223
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002224static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2225 unsigned long address,
2226 int *type)
2227{
2228 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2229 unsigned long pgoff;
2230 struct page *page;
2231
2232 *type = VM_FAULT_MINOR;
2233 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002234 if (pgoff == 0)
2235 page = virt_to_page(vcpu->run);
2236 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2237 page = virt_to_page(vcpu->pio_data);
2238 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002239 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002240 get_page(page);
2241 return page;
2242}
2243
2244static struct vm_operations_struct kvm_vcpu_vm_ops = {
2245 .nopage = kvm_vcpu_nopage,
2246};
2247
2248static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2249{
2250 vma->vm_ops = &kvm_vcpu_vm_ops;
2251 return 0;
2252}
2253
Avi Kivitybccf2152007-02-21 18:04:26 +02002254static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2255{
2256 struct kvm_vcpu *vcpu = filp->private_data;
2257
2258 fput(vcpu->kvm->filp);
2259 return 0;
2260}
2261
2262static struct file_operations kvm_vcpu_fops = {
2263 .release = kvm_vcpu_release,
2264 .unlocked_ioctl = kvm_vcpu_ioctl,
2265 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002266 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002267};
2268
2269/*
2270 * Allocates an inode for the vcpu.
2271 */
2272static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2273{
2274 int fd, r;
2275 struct inode *inode;
2276 struct file *file;
2277
2278 atomic_inc(&vcpu->kvm->filp->f_count);
2279 inode = kvmfs_inode(&kvm_vcpu_fops);
2280 if (IS_ERR(inode)) {
2281 r = PTR_ERR(inode);
2282 goto out1;
2283 }
2284
2285 file = kvmfs_file(inode, vcpu);
2286 if (IS_ERR(file)) {
2287 r = PTR_ERR(file);
2288 goto out2;
2289 }
2290
2291 r = get_unused_fd();
2292 if (r < 0)
2293 goto out3;
2294 fd = r;
2295 fd_install(fd, file);
2296
2297 return fd;
2298
2299out3:
2300 fput(file);
2301out2:
2302 iput(inode);
2303out1:
2304 fput(vcpu->kvm->filp);
2305 return r;
2306}
2307
Avi Kivityc5ea7662007-02-20 18:41:05 +02002308/*
2309 * Creates some virtual cpus. Good luck creating more than one.
2310 */
2311static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2312{
2313 int r;
2314 struct kvm_vcpu *vcpu;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002315 struct page *page;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002316
2317 r = -EINVAL;
2318 if (!valid_vcpu(n))
2319 goto out;
2320
2321 vcpu = &kvm->vcpus[n];
2322
2323 mutex_lock(&vcpu->mutex);
2324
2325 if (vcpu->vmcs) {
2326 mutex_unlock(&vcpu->mutex);
2327 return -EEXIST;
2328 }
2329
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002330 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2331 r = -ENOMEM;
2332 if (!page)
2333 goto out_unlock;
2334 vcpu->run = page_address(page);
2335
Avi Kivity039576c2007-03-20 12:46:50 +02002336 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2337 r = -ENOMEM;
2338 if (!page)
2339 goto out_free_run;
2340 vcpu->pio_data = page_address(page);
2341
Avi Kivityc5ea7662007-02-20 18:41:05 +02002342 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2343 FX_IMAGE_ALIGN);
2344 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
Avi Kivityd917a6b2007-04-12 13:03:01 +03002345 vcpu->cr0 = 0x10;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002346
2347 r = kvm_arch_ops->vcpu_create(vcpu);
2348 if (r < 0)
2349 goto out_free_vcpus;
2350
2351 r = kvm_mmu_create(vcpu);
2352 if (r < 0)
2353 goto out_free_vcpus;
2354
2355 kvm_arch_ops->vcpu_load(vcpu);
2356 r = kvm_mmu_setup(vcpu);
2357 if (r >= 0)
2358 r = kvm_arch_ops->vcpu_setup(vcpu);
2359 vcpu_put(vcpu);
2360
2361 if (r < 0)
2362 goto out_free_vcpus;
2363
Avi Kivitybccf2152007-02-21 18:04:26 +02002364 r = create_vcpu_fd(vcpu);
2365 if (r < 0)
2366 goto out_free_vcpus;
2367
2368 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002369
2370out_free_vcpus:
2371 kvm_free_vcpu(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002372out_free_run:
2373 free_page((unsigned long)vcpu->run);
2374 vcpu->run = NULL;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002375out_unlock:
Avi Kivityc5ea7662007-02-20 18:41:05 +02002376 mutex_unlock(&vcpu->mutex);
2377out:
2378 return r;
2379}
2380
Avi Kivity06465c52007-02-28 20:46:53 +02002381static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2382 struct kvm_cpuid *cpuid,
2383 struct kvm_cpuid_entry __user *entries)
2384{
2385 int r;
2386
2387 r = -E2BIG;
2388 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2389 goto out;
2390 r = -EFAULT;
2391 if (copy_from_user(&vcpu->cpuid_entries, entries,
2392 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2393 goto out;
2394 vcpu->cpuid_nent = cpuid->nent;
2395 return 0;
2396
2397out:
2398 return r;
2399}
2400
Avi Kivity1961d272007-03-05 19:46:05 +02002401static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2402{
2403 if (sigset) {
2404 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2405 vcpu->sigset_active = 1;
2406 vcpu->sigset = *sigset;
2407 } else
2408 vcpu->sigset_active = 0;
2409 return 0;
2410}
2411
Avi Kivityb8836732007-04-01 16:34:31 +03002412/*
2413 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2414 * we have asm/x86/processor.h
2415 */
2416struct fxsave {
2417 u16 cwd;
2418 u16 swd;
2419 u16 twd;
2420 u16 fop;
2421 u64 rip;
2422 u64 rdp;
2423 u32 mxcsr;
2424 u32 mxcsr_mask;
2425 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2426#ifdef CONFIG_X86_64
2427 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2428#else
2429 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2430#endif
2431};
2432
2433static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2434{
2435 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2436
2437 vcpu_load(vcpu);
2438
2439 memcpy(fpu->fpr, fxsave->st_space, 128);
2440 fpu->fcw = fxsave->cwd;
2441 fpu->fsw = fxsave->swd;
2442 fpu->ftwx = fxsave->twd;
2443 fpu->last_opcode = fxsave->fop;
2444 fpu->last_ip = fxsave->rip;
2445 fpu->last_dp = fxsave->rdp;
2446 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2447
2448 vcpu_put(vcpu);
2449
2450 return 0;
2451}
2452
2453static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2454{
2455 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2456
2457 vcpu_load(vcpu);
2458
2459 memcpy(fxsave->st_space, fpu->fpr, 128);
2460 fxsave->cwd = fpu->fcw;
2461 fxsave->swd = fpu->fsw;
2462 fxsave->twd = fpu->ftwx;
2463 fxsave->fop = fpu->last_opcode;
2464 fxsave->rip = fpu->last_ip;
2465 fxsave->rdp = fpu->last_dp;
2466 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2467
2468 vcpu_put(vcpu);
2469
2470 return 0;
2471}
2472
Avi Kivitybccf2152007-02-21 18:04:26 +02002473static long kvm_vcpu_ioctl(struct file *filp,
2474 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475{
Avi Kivitybccf2152007-02-21 18:04:26 +02002476 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002477 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478 int r = -EINVAL;
2479
2480 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002481 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002482 r = -EINVAL;
2483 if (arg)
2484 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002485 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002486 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487 case KVM_GET_REGS: {
2488 struct kvm_regs kvm_regs;
2489
Avi Kivitybccf2152007-02-21 18:04:26 +02002490 memset(&kvm_regs, 0, sizeof kvm_regs);
2491 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492 if (r)
2493 goto out;
2494 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002495 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496 goto out;
2497 r = 0;
2498 break;
2499 }
2500 case KVM_SET_REGS: {
2501 struct kvm_regs kvm_regs;
2502
2503 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002504 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002505 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002506 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507 if (r)
2508 goto out;
2509 r = 0;
2510 break;
2511 }
2512 case KVM_GET_SREGS: {
2513 struct kvm_sregs kvm_sregs;
2514
Avi Kivitybccf2152007-02-21 18:04:26 +02002515 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2516 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517 if (r)
2518 goto out;
2519 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002520 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521 goto out;
2522 r = 0;
2523 break;
2524 }
2525 case KVM_SET_SREGS: {
2526 struct kvm_sregs kvm_sregs;
2527
2528 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002529 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002531 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 if (r)
2533 goto out;
2534 r = 0;
2535 break;
2536 }
2537 case KVM_TRANSLATE: {
2538 struct kvm_translation tr;
2539
2540 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002541 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002543 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544 if (r)
2545 goto out;
2546 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002547 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548 goto out;
2549 r = 0;
2550 break;
2551 }
2552 case KVM_INTERRUPT: {
2553 struct kvm_interrupt irq;
2554
2555 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002556 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002558 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559 if (r)
2560 goto out;
2561 r = 0;
2562 break;
2563 }
2564 case KVM_DEBUG_GUEST: {
2565 struct kvm_debug_guest dbg;
2566
2567 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002568 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002570 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002571 if (r)
2572 goto out;
2573 r = 0;
2574 break;
2575 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002576 case KVM_GET_MSRS:
2577 r = msr_io(vcpu, argp, get_msr, 1);
2578 break;
2579 case KVM_SET_MSRS:
2580 r = msr_io(vcpu, argp, do_set_msr, 0);
2581 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002582 case KVM_SET_CPUID: {
2583 struct kvm_cpuid __user *cpuid_arg = argp;
2584 struct kvm_cpuid cpuid;
2585
2586 r = -EFAULT;
2587 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2588 goto out;
2589 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2590 if (r)
2591 goto out;
2592 break;
2593 }
Avi Kivity1961d272007-03-05 19:46:05 +02002594 case KVM_SET_SIGNAL_MASK: {
2595 struct kvm_signal_mask __user *sigmask_arg = argp;
2596 struct kvm_signal_mask kvm_sigmask;
2597 sigset_t sigset, *p;
2598
2599 p = NULL;
2600 if (argp) {
2601 r = -EFAULT;
2602 if (copy_from_user(&kvm_sigmask, argp,
2603 sizeof kvm_sigmask))
2604 goto out;
2605 r = -EINVAL;
2606 if (kvm_sigmask.len != sizeof sigset)
2607 goto out;
2608 r = -EFAULT;
2609 if (copy_from_user(&sigset, sigmask_arg->sigset,
2610 sizeof sigset))
2611 goto out;
2612 p = &sigset;
2613 }
2614 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2615 break;
2616 }
Avi Kivityb8836732007-04-01 16:34:31 +03002617 case KVM_GET_FPU: {
2618 struct kvm_fpu fpu;
2619
2620 memset(&fpu, 0, sizeof fpu);
2621 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2622 if (r)
2623 goto out;
2624 r = -EFAULT;
2625 if (copy_to_user(argp, &fpu, sizeof fpu))
2626 goto out;
2627 r = 0;
2628 break;
2629 }
2630 case KVM_SET_FPU: {
2631 struct kvm_fpu fpu;
2632
2633 r = -EFAULT;
2634 if (copy_from_user(&fpu, argp, sizeof fpu))
2635 goto out;
2636 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2637 if (r)
2638 goto out;
2639 r = 0;
2640 break;
2641 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002642 default:
2643 ;
2644 }
2645out:
2646 return r;
2647}
2648
2649static long kvm_vm_ioctl(struct file *filp,
2650 unsigned int ioctl, unsigned long arg)
2651{
2652 struct kvm *kvm = filp->private_data;
2653 void __user *argp = (void __user *)arg;
2654 int r = -EINVAL;
2655
2656 switch (ioctl) {
2657 case KVM_CREATE_VCPU:
2658 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2659 if (r < 0)
2660 goto out;
2661 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 case KVM_SET_MEMORY_REGION: {
2663 struct kvm_memory_region kvm_mem;
2664
2665 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002666 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002668 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002669 if (r)
2670 goto out;
2671 break;
2672 }
2673 case KVM_GET_DIRTY_LOG: {
2674 struct kvm_dirty_log log;
2675
2676 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002677 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002679 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 if (r)
2681 goto out;
2682 break;
2683 }
Avi Kivitye8207542007-03-30 16:54:30 +03002684 case KVM_SET_MEMORY_ALIAS: {
2685 struct kvm_memory_alias alias;
2686
2687 r = -EFAULT;
2688 if (copy_from_user(&alias, argp, sizeof alias))
2689 goto out;
2690 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2691 if (r)
2692 goto out;
2693 break;
2694 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002695 default:
2696 ;
2697 }
2698out:
2699 return r;
2700}
2701
2702static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2703 unsigned long address,
2704 int *type)
2705{
2706 struct kvm *kvm = vma->vm_file->private_data;
2707 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002708 struct page *page;
2709
2710 *type = VM_FAULT_MINOR;
2711 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03002712 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02002713 if (!page)
2714 return NOPAGE_SIGBUS;
2715 get_page(page);
2716 return page;
2717}
2718
2719static struct vm_operations_struct kvm_vm_vm_ops = {
2720 .nopage = kvm_vm_nopage,
2721};
2722
2723static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2724{
2725 vma->vm_ops = &kvm_vm_vm_ops;
2726 return 0;
2727}
2728
2729static struct file_operations kvm_vm_fops = {
2730 .release = kvm_vm_release,
2731 .unlocked_ioctl = kvm_vm_ioctl,
2732 .compat_ioctl = kvm_vm_ioctl,
2733 .mmap = kvm_vm_mmap,
2734};
2735
2736static int kvm_dev_ioctl_create_vm(void)
2737{
2738 int fd, r;
2739 struct inode *inode;
2740 struct file *file;
2741 struct kvm *kvm;
2742
2743 inode = kvmfs_inode(&kvm_vm_fops);
2744 if (IS_ERR(inode)) {
2745 r = PTR_ERR(inode);
2746 goto out1;
2747 }
2748
2749 kvm = kvm_create_vm();
2750 if (IS_ERR(kvm)) {
2751 r = PTR_ERR(kvm);
2752 goto out2;
2753 }
2754
2755 file = kvmfs_file(inode, kvm);
2756 if (IS_ERR(file)) {
2757 r = PTR_ERR(file);
2758 goto out3;
2759 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002760 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002761
2762 r = get_unused_fd();
2763 if (r < 0)
2764 goto out4;
2765 fd = r;
2766 fd_install(fd, file);
2767
2768 return fd;
2769
2770out4:
2771 fput(file);
2772out3:
2773 kvm_destroy_vm(kvm);
2774out2:
2775 iput(inode);
2776out1:
2777 return r;
2778}
2779
2780static long kvm_dev_ioctl(struct file *filp,
2781 unsigned int ioctl, unsigned long arg)
2782{
2783 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02002784 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002785
2786 switch (ioctl) {
2787 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002788 r = -EINVAL;
2789 if (arg)
2790 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002791 r = KVM_API_VERSION;
2792 break;
2793 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002794 r = -EINVAL;
2795 if (arg)
2796 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002797 r = kvm_dev_ioctl_create_vm();
2798 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f3669872007-02-09 16:38:35 +00002800 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801 struct kvm_msr_list msr_list;
2802 unsigned n;
2803
2804 r = -EFAULT;
2805 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2806 goto out;
2807 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08002808 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2810 goto out;
2811 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08002812 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813 goto out;
2814 r = -EFAULT;
2815 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08002816 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08002818 if (copy_to_user(user_msr_list->indices
2819 + num_msrs_to_save * sizeof(u32),
2820 &emulated_msrs,
2821 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2822 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08002824 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 }
Avi Kivity5d308f42007-03-01 17:56:20 +02002826 case KVM_CHECK_EXTENSION:
2827 /*
2828 * No extensions defined at present.
2829 */
2830 r = 0;
2831 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002832 case KVM_GET_VCPU_MMAP_SIZE:
2833 r = -EINVAL;
2834 if (arg)
2835 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02002836 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02002837 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 default:
2839 ;
2840 }
2841out:
2842 return r;
2843}
2844
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845static struct file_operations kvm_chardev_ops = {
2846 .open = kvm_dev_open,
2847 .release = kvm_dev_release,
2848 .unlocked_ioctl = kvm_dev_ioctl,
2849 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850};
2851
2852static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002853 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854 "kvm",
2855 &kvm_chardev_ops,
2856};
2857
2858static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2859 void *v)
2860{
2861 if (val == SYS_RESTART) {
2862 /*
2863 * Some (well, at least mine) BIOSes hang on reboot if
2864 * in vmx root mode.
2865 */
2866 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Al Viro8b6d44c2007-02-09 16:38:40 +00002867 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868 }
2869 return NOTIFY_OK;
2870}
2871
2872static struct notifier_block kvm_reboot_notifier = {
2873 .notifier_call = kvm_reboot,
2874 .priority = 0,
2875};
2876
Avi Kivity774c47f2007-02-12 00:54:47 -08002877/*
2878 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2879 * cached on it.
2880 */
2881static void decache_vcpus_on_cpu(int cpu)
2882{
2883 struct kvm *vm;
2884 struct kvm_vcpu *vcpu;
2885 int i;
2886
2887 spin_lock(&kvm_lock);
2888 list_for_each_entry(vm, &vm_list, vm_list)
2889 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2890 vcpu = &vm->vcpus[i];
2891 /*
2892 * If the vcpu is locked, then it is running on some
2893 * other cpu and therefore it is not cached on the
2894 * cpu in question.
2895 *
2896 * If it's not locked, check the last cpu it executed
2897 * on.
2898 */
2899 if (mutex_trylock(&vcpu->mutex)) {
2900 if (vcpu->cpu == cpu) {
2901 kvm_arch_ops->vcpu_decache(vcpu);
2902 vcpu->cpu = -1;
2903 }
2904 mutex_unlock(&vcpu->mutex);
2905 }
2906 }
2907 spin_unlock(&kvm_lock);
2908}
2909
2910static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2911 void *v)
2912{
2913 int cpu = (long)v;
2914
2915 switch (val) {
Jeremy Katz43934a32007-02-19 14:37:46 +02002916 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002917 case CPU_DOWN_PREPARE_FROZEN:
Avi Kivity774c47f2007-02-12 00:54:47 -08002918 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002919 case CPU_UP_CANCELED_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02002920 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2921 cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002922 decache_vcpus_on_cpu(cpu);
2923 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
2924 NULL, 0, 1);
2925 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002926 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002927 case CPU_ONLINE_FROZEN:
Jeremy Katz43934a32007-02-19 14:37:46 +02002928 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2929 cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002930 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
2931 NULL, 0, 1);
2932 break;
2933 }
2934 return NOTIFY_OK;
2935}
2936
2937static struct notifier_block kvm_cpu_notifier = {
2938 .notifier_call = kvm_cpu_hotplug,
2939 .priority = 20, /* must be > scheduler priority */
2940};
2941
Avi Kivity1165f5f2007-04-19 17:27:43 +03002942static u64 stat_get(void *_offset)
2943{
2944 unsigned offset = (long)_offset;
2945 u64 total = 0;
2946 struct kvm *kvm;
2947 struct kvm_vcpu *vcpu;
2948 int i;
2949
2950 spin_lock(&kvm_lock);
2951 list_for_each_entry(kvm, &vm_list, vm_list)
2952 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2953 vcpu = &kvm->vcpus[i];
2954 total += *(u32 *)((void *)vcpu + offset);
2955 }
2956 spin_unlock(&kvm_lock);
2957 return total;
2958}
2959
2960static void stat_set(void *offset, u64 val)
2961{
2962}
2963
2964DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
2965
Avi Kivity6aa8b732006-12-10 02:21:36 -08002966static __init void kvm_init_debug(void)
2967{
2968 struct kvm_stats_debugfs_item *p;
2969
Al Viro8b6d44c2007-02-09 16:38:40 +00002970 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971 for (p = debugfs_entries; p->name; ++p)
Avi Kivity1165f5f2007-04-19 17:27:43 +03002972 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
2973 (void *)(long)p->offset,
2974 &stat_fops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975}
2976
2977static void kvm_exit_debug(void)
2978{
2979 struct kvm_stats_debugfs_item *p;
2980
2981 for (p = debugfs_entries; p->name; ++p)
2982 debugfs_remove(p->dentry);
2983 debugfs_remove(debugfs_dir);
2984}
2985
Avi Kivity59ae6c62007-02-12 00:54:48 -08002986static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2987{
2988 decache_vcpus_on_cpu(raw_smp_processor_id());
Avi Kivity19d14082007-02-19 14:37:48 +02002989 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002990 return 0;
2991}
2992
2993static int kvm_resume(struct sys_device *dev)
2994{
Avi Kivity19d14082007-02-19 14:37:48 +02002995 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002996 return 0;
2997}
2998
2999static struct sysdev_class kvm_sysdev_class = {
3000 set_kset_name("kvm"),
3001 .suspend = kvm_suspend,
3002 .resume = kvm_resume,
3003};
3004
3005static struct sys_device kvm_sysdev = {
3006 .id = 0,
3007 .cls = &kvm_sysdev_class,
3008};
3009
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010hpa_t bad_page_address;
3011
Avi Kivity37e29d92007-02-20 14:07:37 +02003012static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
3013 const char *dev_name, void *data, struct vfsmount *mnt)
3014{
Andrew Mortone9cdb1e2007-03-01 11:28:13 +02003015 return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
Avi Kivity37e29d92007-02-20 14:07:37 +02003016}
3017
3018static struct file_system_type kvm_fs_type = {
3019 .name = "kvmfs",
3020 .get_sb = kvmfs_get_sb,
3021 .kill_sb = kill_anon_super,
3022};
3023
Avi Kivity6aa8b732006-12-10 02:21:36 -08003024int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3025{
3026 int r;
3027
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003028 if (kvm_arch_ops) {
3029 printk(KERN_ERR "kvm: already loaded the other module\n");
3030 return -EEXIST;
3031 }
3032
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003033 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003034 printk(KERN_ERR "kvm: no hardware support\n");
3035 return -EOPNOTSUPP;
3036 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003037 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038 printk(KERN_ERR "kvm: disabled by bios\n");
3039 return -EOPNOTSUPP;
3040 }
3041
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08003042 kvm_arch_ops = ops;
3043
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044 r = kvm_arch_ops->hardware_setup();
3045 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02003046 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047
Al Viro8b6d44c2007-02-09 16:38:40 +00003048 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08003049 r = register_cpu_notifier(&kvm_cpu_notifier);
3050 if (r)
3051 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003052 register_reboot_notifier(&kvm_reboot_notifier);
3053
Avi Kivity59ae6c62007-02-12 00:54:48 -08003054 r = sysdev_class_register(&kvm_sysdev_class);
3055 if (r)
3056 goto out_free_2;
3057
3058 r = sysdev_register(&kvm_sysdev);
3059 if (r)
3060 goto out_free_3;
3061
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062 kvm_chardev_ops.owner = module;
3063
3064 r = misc_register(&kvm_dev);
3065 if (r) {
3066 printk (KERN_ERR "kvm: misc device register failed\n");
3067 goto out_free;
3068 }
3069
3070 return r;
3071
3072out_free:
Avi Kivity59ae6c62007-02-12 00:54:48 -08003073 sysdev_unregister(&kvm_sysdev);
3074out_free_3:
3075 sysdev_class_unregister(&kvm_sysdev_class);
3076out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08003078 unregister_cpu_notifier(&kvm_cpu_notifier);
3079out_free_1:
Al Viro8b6d44c2007-02-09 16:38:40 +00003080 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 kvm_arch_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02003082out:
3083 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084 return r;
3085}
3086
3087void kvm_exit_arch(void)
3088{
3089 misc_deregister(&kvm_dev);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003090 sysdev_unregister(&kvm_sysdev);
3091 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08003093 unregister_cpu_notifier(&kvm_cpu_notifier);
Al Viro8b6d44c2007-02-09 16:38:40 +00003094 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095 kvm_arch_ops->hardware_unsetup();
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08003096 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097}
3098
3099static __init int kvm_init(void)
3100{
3101 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02003102 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103
Avi Kivityb5a33a72007-04-15 16:31:09 +03003104 r = kvm_mmu_module_init();
3105 if (r)
3106 goto out4;
3107
Avi Kivity37e29d92007-02-20 14:07:37 +02003108 r = register_filesystem(&kvm_fs_type);
3109 if (r)
3110 goto out3;
3111
3112 kvmfs_mnt = kern_mount(&kvm_fs_type);
3113 r = PTR_ERR(kvmfs_mnt);
3114 if (IS_ERR(kvmfs_mnt))
3115 goto out2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116 kvm_init_debug();
3117
Michael Riepebf591b22006-12-22 01:05:36 -08003118 kvm_init_msr_list();
3119
Avi Kivity6aa8b732006-12-10 02:21:36 -08003120 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3121 r = -ENOMEM;
3122 goto out;
3123 }
3124
3125 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3126 memset(__va(bad_page_address), 0, PAGE_SIZE);
3127
Avi Kivity58e690e2007-02-26 16:29:43 +02003128 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129
3130out:
3131 kvm_exit_debug();
Avi Kivity37e29d92007-02-20 14:07:37 +02003132 mntput(kvmfs_mnt);
3133out2:
3134 unregister_filesystem(&kvm_fs_type);
3135out3:
Avi Kivityb5a33a72007-04-15 16:31:09 +03003136 kvm_mmu_module_exit();
3137out4:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138 return r;
3139}
3140
3141static __exit void kvm_exit(void)
3142{
3143 kvm_exit_debug();
3144 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivity37e29d92007-02-20 14:07:37 +02003145 mntput(kvmfs_mnt);
3146 unregister_filesystem(&kvm_fs_type);
Avi Kivityb5a33a72007-04-15 16:31:09 +03003147 kvm_mmu_module_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148}
3149
3150module_init(kvm_init)
3151module_exit(kvm_exit)
3152
3153EXPORT_SYMBOL_GPL(kvm_init_arch);
3154EXPORT_SYMBOL_GPL(kvm_exit_arch);