blob: a163bca38973024bc8b0a4627c0dbb2c8a0c007a [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
18#include "kvm.h"
19
20#include <linux/kvm.h>
21#include <linux/module.h>
22#include <linux/errno.h>
Andrew Mortone9cdb1e2007-03-01 11:28:13 +020023#include <linux/magic.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <asm/processor.h>
25#include <linux/percpu.h>
26#include <linux/gfp.h>
27#include <asm/msr.h>
28#include <linux/mm.h>
29#include <linux/miscdevice.h>
30#include <linux/vmalloc.h>
31#include <asm/uaccess.h>
32#include <linux/reboot.h>
33#include <asm/io.h>
34#include <linux/debugfs.h>
35#include <linux/highmem.h>
36#include <linux/file.h>
37#include <asm/desc.h>
Avi Kivity59ae6c62007-02-12 00:54:48 -080038#include <linux/sysdev.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080039#include <linux/cpu.h>
Avi Kivityf17abe92007-02-21 19:28:04 +020040#include <linux/file.h>
Avi Kivity37e29d92007-02-20 14:07:37 +020041#include <linux/fs.h>
42#include <linux/mount.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043
44#include "x86_emulate.h"
45#include "segment_descriptor.h"
46
47MODULE_AUTHOR("Qumranet");
48MODULE_LICENSE("GPL");
49
Avi Kivity133de902007-02-12 00:54:44 -080050static DEFINE_SPINLOCK(kvm_lock);
51static LIST_HEAD(vm_list);
52
Avi Kivity6aa8b732006-12-10 02:21:36 -080053struct kvm_arch_ops *kvm_arch_ops;
54struct kvm_stat kvm_stat;
55EXPORT_SYMBOL_GPL(kvm_stat);
56
57static struct kvm_stats_debugfs_item {
58 const char *name;
59 u32 *data;
60 struct dentry *dentry;
61} debugfs_entries[] = {
62 { "pf_fixed", &kvm_stat.pf_fixed },
63 { "pf_guest", &kvm_stat.pf_guest },
64 { "tlb_flush", &kvm_stat.tlb_flush },
65 { "invlpg", &kvm_stat.invlpg },
66 { "exits", &kvm_stat.exits },
67 { "io_exits", &kvm_stat.io_exits },
68 { "mmio_exits", &kvm_stat.mmio_exits },
69 { "signal_exits", &kvm_stat.signal_exits },
Dor Laorc1150d82007-01-05 16:36:24 -080070 { "irq_window", &kvm_stat.irq_window_exits },
71 { "halt_exits", &kvm_stat.halt_exits },
72 { "request_irq", &kvm_stat.request_irq_exits },
Avi Kivity6aa8b732006-12-10 02:21:36 -080073 { "irq_exits", &kvm_stat.irq_exits },
Al Viro8b6d44c2007-02-09 16:38:40 +000074 { NULL, NULL }
Avi Kivity6aa8b732006-12-10 02:21:36 -080075};
76
77static struct dentry *debugfs_dir;
78
Avi Kivity37e29d92007-02-20 14:07:37 +020079struct vfsmount *kvmfs_mnt;
80
Avi Kivity6aa8b732006-12-10 02:21:36 -080081#define MAX_IO_MSRS 256
82
83#define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
84#define LMSW_GUEST_MASK 0x0eULL
85#define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
86#define CR8_RESEVED_BITS (~0x0fULL)
87#define EFER_RESERVED_BITS 0xfffffffffffff2fe
88
Avi Kivity05b3e0c2006-12-13 00:33:45 -080089#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -080090// LDT or TSS descriptor in the GDT. 16 bytes.
91struct segment_descriptor_64 {
92 struct segment_descriptor s;
93 u32 base_higher;
94 u32 pad_zero;
95};
96
97#endif
98
Avi Kivitybccf2152007-02-21 18:04:26 +020099static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
100 unsigned long arg);
101
Avi Kivityf17abe92007-02-21 19:28:04 +0200102static struct inode *kvmfs_inode(struct file_operations *fops)
103{
104 int error = -ENOMEM;
105 struct inode *inode = new_inode(kvmfs_mnt->mnt_sb);
106
107 if (!inode)
108 goto eexit_1;
109
110 inode->i_fop = fops;
111
112 /*
113 * Mark the inode dirty from the very beginning,
114 * that way it will never be moved to the dirty
115 * list because mark_inode_dirty() will think
116 * that it already _is_ on the dirty list.
117 */
118 inode->i_state = I_DIRTY;
119 inode->i_mode = S_IRUSR | S_IWUSR;
120 inode->i_uid = current->fsuid;
121 inode->i_gid = current->fsgid;
122 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
123 return inode;
124
125eexit_1:
126 return ERR_PTR(error);
127}
128
129static struct file *kvmfs_file(struct inode *inode, void *private_data)
130{
131 struct file *file = get_empty_filp();
132
133 if (!file)
134 return ERR_PTR(-ENFILE);
135
136 file->f_path.mnt = mntget(kvmfs_mnt);
137 file->f_path.dentry = d_alloc_anon(inode);
138 if (!file->f_path.dentry)
139 return ERR_PTR(-ENOMEM);
140 file->f_mapping = inode->i_mapping;
141
142 file->f_pos = 0;
143 file->f_flags = O_RDWR;
144 file->f_op = inode->i_fop;
145 file->f_mode = FMODE_READ | FMODE_WRITE;
146 file->f_version = 0;
147 file->private_data = private_data;
148 return file;
149}
150
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151unsigned long segment_base(u16 selector)
152{
153 struct descriptor_table gdt;
154 struct segment_descriptor *d;
155 unsigned long table_base;
156 typedef unsigned long ul;
157 unsigned long v;
158
159 if (selector == 0)
160 return 0;
161
162 asm ("sgdt %0" : "=m"(gdt));
163 table_base = gdt.base;
164
165 if (selector & 4) { /* from ldt */
166 u16 ldt_selector;
167
168 asm ("sldt %0" : "=g"(ldt_selector));
169 table_base = segment_base(ldt_selector);
170 }
171 d = (struct segment_descriptor *)(table_base + (selector & ~7));
172 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800173#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174 if (d->system == 0
175 && (d->type == 2 || d->type == 9 || d->type == 11))
176 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
177#endif
178 return v;
179}
180EXPORT_SYMBOL_GPL(segment_base);
181
James Morris5aacf0c2006-12-22 01:04:55 -0800182static inline int valid_vcpu(int n)
183{
184 return likely(n >= 0 && n < KVM_MAX_VCPUS);
185}
186
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200187int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
188 void *dest)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189{
190 unsigned char *host_buf = dest;
191 unsigned long req_size = size;
192
193 while (size) {
194 hpa_t paddr;
195 unsigned now;
196 unsigned offset;
197 hva_t guest_buf;
198
199 paddr = gva_to_hpa(vcpu, addr);
200
201 if (is_error_hpa(paddr))
202 break;
203
204 guest_buf = (hva_t)kmap_atomic(
205 pfn_to_page(paddr >> PAGE_SHIFT),
206 KM_USER0);
207 offset = addr & ~PAGE_MASK;
208 guest_buf |= offset;
209 now = min(size, PAGE_SIZE - offset);
210 memcpy(host_buf, (void*)guest_buf, now);
211 host_buf += now;
212 addr += now;
213 size -= now;
214 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
215 }
216 return req_size - size;
217}
218EXPORT_SYMBOL_GPL(kvm_read_guest);
219
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200220int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
221 void *data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222{
223 unsigned char *host_buf = data;
224 unsigned long req_size = size;
225
226 while (size) {
227 hpa_t paddr;
228 unsigned now;
229 unsigned offset;
230 hva_t guest_buf;
Uri Lublinab51a432007-02-21 18:25:21 +0200231 gfn_t gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232
233 paddr = gva_to_hpa(vcpu, addr);
234
235 if (is_error_hpa(paddr))
236 break;
237
Uri Lublinab51a432007-02-21 18:25:21 +0200238 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
239 mark_page_dirty(vcpu->kvm, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 guest_buf = (hva_t)kmap_atomic(
241 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
242 offset = addr & ~PAGE_MASK;
243 guest_buf |= offset;
244 now = min(size, PAGE_SIZE - offset);
245 memcpy((void*)guest_buf, host_buf, now);
246 host_buf += now;
247 addr += now;
248 size -= now;
249 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
250 }
251 return req_size - size;
252}
253EXPORT_SYMBOL_GPL(kvm_write_guest);
254
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255/*
256 * Switches to specified vcpu, until a matching vcpu_put()
257 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200258static void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800259{
Avi Kivitybccf2152007-02-21 18:04:26 +0200260 mutex_lock(&vcpu->mutex);
261 kvm_arch_ops->vcpu_load(vcpu);
262}
263
264/*
265 * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
266 * if the slot is not populated.
267 */
268static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot)
269{
270 struct kvm_vcpu *vcpu = &kvm->vcpus[slot];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271
272 mutex_lock(&vcpu->mutex);
Avi Kivitybccf2152007-02-21 18:04:26 +0200273 if (!vcpu->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274 mutex_unlock(&vcpu->mutex);
Al Viro8b6d44c2007-02-09 16:38:40 +0000275 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276 }
Avi Kivitybccf2152007-02-21 18:04:26 +0200277 kvm_arch_ops->vcpu_load(vcpu);
278 return vcpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279}
280
281static void vcpu_put(struct kvm_vcpu *vcpu)
282{
283 kvm_arch_ops->vcpu_put(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284 mutex_unlock(&vcpu->mutex);
285}
286
Avi Kivityf17abe92007-02-21 19:28:04 +0200287static struct kvm *kvm_create_vm(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288{
289 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
290 int i;
291
292 if (!kvm)
Avi Kivityf17abe92007-02-21 19:28:04 +0200293 return ERR_PTR(-ENOMEM);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294
295 spin_lock_init(&kvm->lock);
296 INIT_LIST_HEAD(&kvm->active_mmu_pages);
297 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
298 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
299
300 mutex_init(&vcpu->mutex);
Avi Kivity133de902007-02-12 00:54:44 -0800301 vcpu->cpu = -1;
Avi Kivity86a2b422007-01-05 16:36:57 -0800302 vcpu->kvm = kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303 vcpu->mmu.root_hpa = INVALID_PAGE;
304 INIT_LIST_HEAD(&vcpu->free_pages);
Avi Kivity133de902007-02-12 00:54:44 -0800305 spin_lock(&kvm_lock);
306 list_add(&kvm->vm_list, &vm_list);
307 spin_unlock(&kvm_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308 }
Avi Kivityf17abe92007-02-21 19:28:04 +0200309 return kvm;
310}
311
312static int kvm_dev_open(struct inode *inode, struct file *filp)
313{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800314 return 0;
315}
316
317/*
318 * Free any memory in @free but not in @dont.
319 */
320static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
321 struct kvm_memory_slot *dont)
322{
323 int i;
324
325 if (!dont || free->phys_mem != dont->phys_mem)
326 if (free->phys_mem) {
327 for (i = 0; i < free->npages; ++i)
Avi Kivity55a54f72006-12-29 16:49:58 -0800328 if (free->phys_mem[i])
329 __free_page(free->phys_mem[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 vfree(free->phys_mem);
331 }
332
333 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
334 vfree(free->dirty_bitmap);
335
Al Viro8b6d44c2007-02-09 16:38:40 +0000336 free->phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800337 free->npages = 0;
Al Viro8b6d44c2007-02-09 16:38:40 +0000338 free->dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339}
340
341static void kvm_free_physmem(struct kvm *kvm)
342{
343 int i;
344
345 for (i = 0; i < kvm->nmemslots; ++i)
Al Viro8b6d44c2007-02-09 16:38:40 +0000346 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347}
348
349static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
350{
Avi Kivitybccf2152007-02-21 18:04:26 +0200351 if (!vcpu->vmcs)
Ingo Molnar1e8ba6f2007-02-12 00:54:42 -0800352 return;
353
Avi Kivitybccf2152007-02-21 18:04:26 +0200354 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355 kvm_mmu_destroy(vcpu);
Avi Kivity08438472007-01-22 20:40:38 -0800356 vcpu_put(vcpu);
Avi Kivity9ede74e2007-01-05 16:36:55 -0800357 kvm_arch_ops->vcpu_free(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358}
359
360static void kvm_free_vcpus(struct kvm *kvm)
361{
362 unsigned int i;
363
364 for (i = 0; i < KVM_MAX_VCPUS; ++i)
365 kvm_free_vcpu(&kvm->vcpus[i]);
366}
367
368static int kvm_dev_release(struct inode *inode, struct file *filp)
369{
Avi Kivityf17abe92007-02-21 19:28:04 +0200370 return 0;
371}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800372
Avi Kivityf17abe92007-02-21 19:28:04 +0200373static void kvm_destroy_vm(struct kvm *kvm)
374{
Avi Kivity133de902007-02-12 00:54:44 -0800375 spin_lock(&kvm_lock);
376 list_del(&kvm->vm_list);
377 spin_unlock(&kvm_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378 kvm_free_vcpus(kvm);
379 kvm_free_physmem(kvm);
380 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200381}
382
383static int kvm_vm_release(struct inode *inode, struct file *filp)
384{
385 struct kvm *kvm = filp->private_data;
386
387 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388 return 0;
389}
390
391static void inject_gp(struct kvm_vcpu *vcpu)
392{
393 kvm_arch_ops->inject_gp(vcpu, 0);
394}
395
Avi Kivity1342d352007-01-05 16:36:39 -0800396/*
397 * Load the pae pdptrs. Return true is they are all valid.
398 */
399static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400{
401 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800402 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403 int i;
404 u64 pdpte;
405 u64 *pdpt;
Avi Kivity1342d352007-01-05 16:36:39 -0800406 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407 struct kvm_memory_slot *memslot;
408
409 spin_lock(&vcpu->kvm->lock);
410 memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn);
411 /* FIXME: !memslot - emulate? 0xff? */
412 pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0);
413
Avi Kivity1342d352007-01-05 16:36:39 -0800414 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415 for (i = 0; i < 4; ++i) {
416 pdpte = pdpt[offset + i];
Avi Kivity1342d352007-01-05 16:36:39 -0800417 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
418 ret = 0;
419 goto out;
420 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421 }
422
Avi Kivity1342d352007-01-05 16:36:39 -0800423 for (i = 0; i < 4; ++i)
424 vcpu->pdptrs[i] = pdpt[offset + i];
425
426out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427 kunmap_atomic(pdpt, KM_USER0);
428 spin_unlock(&vcpu->kvm->lock);
429
Avi Kivity1342d352007-01-05 16:36:39 -0800430 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431}
432
433void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
434{
435 if (cr0 & CR0_RESEVED_BITS) {
436 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
437 cr0, vcpu->cr0);
438 inject_gp(vcpu);
439 return;
440 }
441
442 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
443 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
444 inject_gp(vcpu);
445 return;
446 }
447
448 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
449 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
450 "and a clear PE flag\n");
451 inject_gp(vcpu);
452 return;
453 }
454
455 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800456#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457 if ((vcpu->shadow_efer & EFER_LME)) {
458 int cs_db, cs_l;
459
460 if (!is_pae(vcpu)) {
461 printk(KERN_DEBUG "set_cr0: #GP, start paging "
462 "in long mode while PAE is disabled\n");
463 inject_gp(vcpu);
464 return;
465 }
466 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
467 if (cs_l) {
468 printk(KERN_DEBUG "set_cr0: #GP, start paging "
469 "in long mode while CS.L == 1\n");
470 inject_gp(vcpu);
471 return;
472
473 }
474 } else
475#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800476 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
478 "reserved bits\n");
479 inject_gp(vcpu);
480 return;
481 }
482
483 }
484
485 kvm_arch_ops->set_cr0(vcpu, cr0);
486 vcpu->cr0 = cr0;
487
488 spin_lock(&vcpu->kvm->lock);
489 kvm_mmu_reset_context(vcpu);
490 spin_unlock(&vcpu->kvm->lock);
491 return;
492}
493EXPORT_SYMBOL_GPL(set_cr0);
494
495void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
496{
Avi Kivity399badf2007-01-05 16:36:38 -0800497 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
499}
500EXPORT_SYMBOL_GPL(lmsw);
501
502void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
503{
504 if (cr4 & CR4_RESEVED_BITS) {
505 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
506 inject_gp(vcpu);
507 return;
508 }
509
Avi Kivitya9058ec2006-12-29 16:49:37 -0800510 if (is_long_mode(vcpu)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511 if (!(cr4 & CR4_PAE_MASK)) {
512 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
513 "in long mode\n");
514 inject_gp(vcpu);
515 return;
516 }
517 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
Avi Kivity1342d352007-01-05 16:36:39 -0800518 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
520 inject_gp(vcpu);
521 }
522
523 if (cr4 & CR4_VMXE_MASK) {
524 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
525 inject_gp(vcpu);
526 return;
527 }
528 kvm_arch_ops->set_cr4(vcpu, cr4);
529 spin_lock(&vcpu->kvm->lock);
530 kvm_mmu_reset_context(vcpu);
531 spin_unlock(&vcpu->kvm->lock);
532}
533EXPORT_SYMBOL_GPL(set_cr4);
534
535void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
536{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800537 if (is_long_mode(vcpu)) {
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200538 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
540 inject_gp(vcpu);
541 return;
542 }
543 } else {
544 if (cr3 & CR3_RESEVED_BITS) {
545 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
546 inject_gp(vcpu);
547 return;
548 }
549 if (is_paging(vcpu) && is_pae(vcpu) &&
Avi Kivity1342d352007-01-05 16:36:39 -0800550 !load_pdptrs(vcpu, cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
552 "reserved bits\n");
553 inject_gp(vcpu);
554 return;
555 }
556 }
557
558 vcpu->cr3 = cr3;
559 spin_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800560 /*
561 * Does the new cr3 value map to physical memory? (Note, we
562 * catch an invalid cr3 even in real-mode, because it would
563 * cause trouble later on when we turn on paging anyway.)
564 *
565 * A real CPU would silently accept an invalid cr3 and would
566 * attempt to use it - with largely undefined (and often hard
567 * to debug) behavior on the guest side.
568 */
569 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
570 inject_gp(vcpu);
571 else
572 vcpu->mmu.new_cr3(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 spin_unlock(&vcpu->kvm->lock);
574}
575EXPORT_SYMBOL_GPL(set_cr3);
576
577void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
578{
579 if ( cr8 & CR8_RESEVED_BITS) {
580 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
581 inject_gp(vcpu);
582 return;
583 }
584 vcpu->cr8 = cr8;
585}
586EXPORT_SYMBOL_GPL(set_cr8);
587
588void fx_init(struct kvm_vcpu *vcpu)
589{
590 struct __attribute__ ((__packed__)) fx_image_s {
591 u16 control; //fcw
592 u16 status; //fsw
593 u16 tag; // ftw
594 u16 opcode; //fop
595 u64 ip; // fpu ip
596 u64 operand;// fpu dp
597 u32 mxcsr;
598 u32 mxcsr_mask;
599
600 } *fx_image;
601
602 fx_save(vcpu->host_fx_image);
603 fpu_init();
604 fx_save(vcpu->guest_fx_image);
605 fx_restore(vcpu->host_fx_image);
606
607 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
608 fx_image->mxcsr = 0x1f80;
609 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
610 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
611}
612EXPORT_SYMBOL_GPL(fx_init);
613
Uri Lublin02b27c12007-02-22 17:15:33 +0200614static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
615{
616 spin_lock(&vcpu->kvm->lock);
617 kvm_mmu_slot_remove_write_access(vcpu, slot);
618 spin_unlock(&vcpu->kvm->lock);
619}
620
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622 * Allocate some memory and give it an address in the guest physical address
623 * space.
624 *
625 * Discontiguous memory is allowed, mostly for framebuffers.
626 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200627static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
628 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629{
630 int r;
631 gfn_t base_gfn;
632 unsigned long npages;
633 unsigned long i;
634 struct kvm_memory_slot *memslot;
635 struct kvm_memory_slot old, new;
636 int memory_config_version;
637
638 r = -EINVAL;
639 /* General sanity checks */
640 if (mem->memory_size & (PAGE_SIZE - 1))
641 goto out;
642 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
643 goto out;
644 if (mem->slot >= KVM_MEMORY_SLOTS)
645 goto out;
646 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
647 goto out;
648
649 memslot = &kvm->memslots[mem->slot];
650 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
651 npages = mem->memory_size >> PAGE_SHIFT;
652
653 if (!npages)
654 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
655
656raced:
657 spin_lock(&kvm->lock);
658
659 memory_config_version = kvm->memory_config_version;
660 new = old = *memslot;
661
662 new.base_gfn = base_gfn;
663 new.npages = npages;
664 new.flags = mem->flags;
665
666 /* Disallow changing a memory slot's size. */
667 r = -EINVAL;
668 if (npages && old.npages && npages != old.npages)
669 goto out_unlock;
670
671 /* Check for overlaps */
672 r = -EEXIST;
673 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
674 struct kvm_memory_slot *s = &kvm->memslots[i];
675
676 if (s == memslot)
677 continue;
678 if (!((base_gfn + npages <= s->base_gfn) ||
679 (base_gfn >= s->base_gfn + s->npages)))
680 goto out_unlock;
681 }
682 /*
683 * Do memory allocations outside lock. memory_config_version will
684 * detect any races.
685 */
686 spin_unlock(&kvm->lock);
687
688 /* Deallocate if slot is being removed */
689 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000690 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691
692 /* Free page dirty bitmap if unneeded */
693 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000694 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695
696 r = -ENOMEM;
697
698 /* Allocate if a slot is being created */
699 if (npages && !new.phys_mem) {
700 new.phys_mem = vmalloc(npages * sizeof(struct page *));
701
702 if (!new.phys_mem)
703 goto out_free;
704
705 memset(new.phys_mem, 0, npages * sizeof(struct page *));
706 for (i = 0; i < npages; ++i) {
707 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
708 | __GFP_ZERO);
709 if (!new.phys_mem[i])
710 goto out_free;
Markus Rechberger5972e952007-02-19 14:37:47 +0200711 set_page_private(new.phys_mem[i],0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 }
713 }
714
715 /* Allocate page dirty bitmap if needed */
716 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
717 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
718
719 new.dirty_bitmap = vmalloc(dirty_bytes);
720 if (!new.dirty_bitmap)
721 goto out_free;
722 memset(new.dirty_bitmap, 0, dirty_bytes);
723 }
724
725 spin_lock(&kvm->lock);
726
727 if (memory_config_version != kvm->memory_config_version) {
728 spin_unlock(&kvm->lock);
729 kvm_free_physmem_slot(&new, &old);
730 goto raced;
731 }
732
733 r = -EAGAIN;
734 if (kvm->busy)
735 goto out_unlock;
736
737 if (mem->slot >= kvm->nmemslots)
738 kvm->nmemslots = mem->slot + 1;
739
740 *memslot = new;
741 ++kvm->memory_config_version;
742
743 spin_unlock(&kvm->lock);
744
745 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
746 struct kvm_vcpu *vcpu;
747
Avi Kivitybccf2152007-02-21 18:04:26 +0200748 vcpu = vcpu_load_slot(kvm, i);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749 if (!vcpu)
750 continue;
Uri Lublinff990d52007-02-22 17:37:32 +0200751 if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
752 do_remove_write_access(vcpu, mem->slot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 kvm_mmu_reset_context(vcpu);
754 vcpu_put(vcpu);
755 }
756
757 kvm_free_physmem_slot(&old, &new);
758 return 0;
759
760out_unlock:
761 spin_unlock(&kvm->lock);
762out_free:
763 kvm_free_physmem_slot(&new, &old);
764out:
765 return r;
766}
767
768/*
769 * Get (and clear) the dirty memory log for a memory slot.
770 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200771static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
772 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773{
774 struct kvm_memory_slot *memslot;
775 int r, i;
776 int n;
Avi Kivity714b93d2007-01-05 16:36:53 -0800777 int cleared;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778 unsigned long any = 0;
779
780 spin_lock(&kvm->lock);
781
782 /*
783 * Prevent changes to guest memory configuration even while the lock
784 * is not taken.
785 */
786 ++kvm->busy;
787 spin_unlock(&kvm->lock);
788 r = -EINVAL;
789 if (log->slot >= KVM_MEMORY_SLOTS)
790 goto out;
791
792 memslot = &kvm->memslots[log->slot];
793 r = -ENOENT;
794 if (!memslot->dirty_bitmap)
795 goto out;
796
Uri Lublincd1a4a92007-02-22 16:43:09 +0200797 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798
Uri Lublincd1a4a92007-02-22 16:43:09 +0200799 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800 any = memslot->dirty_bitmap[i];
801
802 r = -EFAULT;
803 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
804 goto out;
805
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806 if (any) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800807 cleared = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Avi Kivitybccf2152007-02-21 18:04:26 +0200809 struct kvm_vcpu *vcpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810
Avi Kivitybccf2152007-02-21 18:04:26 +0200811 vcpu = vcpu_load_slot(kvm, i);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812 if (!vcpu)
813 continue;
Avi Kivity714b93d2007-01-05 16:36:53 -0800814 if (!cleared) {
815 do_remove_write_access(vcpu, log->slot);
816 memset(memslot->dirty_bitmap, 0, n);
817 cleared = 1;
818 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819 kvm_arch_ops->tlb_flush(vcpu);
820 vcpu_put(vcpu);
821 }
822 }
823
824 r = 0;
825
826out:
827 spin_lock(&kvm->lock);
828 --kvm->busy;
829 spin_unlock(&kvm->lock);
830 return r;
831}
832
833struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
834{
835 int i;
836
837 for (i = 0; i < kvm->nmemslots; ++i) {
838 struct kvm_memory_slot *memslot = &kvm->memslots[i];
839
840 if (gfn >= memslot->base_gfn
841 && gfn < memslot->base_gfn + memslot->npages)
842 return memslot;
843 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000844 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845}
846EXPORT_SYMBOL_GPL(gfn_to_memslot);
847
848void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
849{
850 int i;
Al Viro8b6d44c2007-02-09 16:38:40 +0000851 struct kvm_memory_slot *memslot = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 unsigned long rel_gfn;
853
854 for (i = 0; i < kvm->nmemslots; ++i) {
855 memslot = &kvm->memslots[i];
856
857 if (gfn >= memslot->base_gfn
858 && gfn < memslot->base_gfn + memslot->npages) {
859
860 if (!memslot || !memslot->dirty_bitmap)
861 return;
862
863 rel_gfn = gfn - memslot->base_gfn;
864
865 /* avoid RMW */
866 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
867 set_bit(rel_gfn, memslot->dirty_bitmap);
868 return;
869 }
870 }
871}
872
873static int emulator_read_std(unsigned long addr,
874 unsigned long *val,
875 unsigned int bytes,
876 struct x86_emulate_ctxt *ctxt)
877{
878 struct kvm_vcpu *vcpu = ctxt->vcpu;
879 void *data = val;
880
881 while (bytes) {
882 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
883 unsigned offset = addr & (PAGE_SIZE-1);
884 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
885 unsigned long pfn;
886 struct kvm_memory_slot *memslot;
887 void *page;
888
889 if (gpa == UNMAPPED_GVA)
890 return X86EMUL_PROPAGATE_FAULT;
891 pfn = gpa >> PAGE_SHIFT;
892 memslot = gfn_to_memslot(vcpu->kvm, pfn);
893 if (!memslot)
894 return X86EMUL_UNHANDLEABLE;
895 page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0);
896
897 memcpy(data, page + offset, tocopy);
898
899 kunmap_atomic(page, KM_USER0);
900
901 bytes -= tocopy;
902 data += tocopy;
903 addr += tocopy;
904 }
905
906 return X86EMUL_CONTINUE;
907}
908
909static int emulator_write_std(unsigned long addr,
910 unsigned long val,
911 unsigned int bytes,
912 struct x86_emulate_ctxt *ctxt)
913{
914 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
915 addr, bytes);
916 return X86EMUL_UNHANDLEABLE;
917}
918
919static int emulator_read_emulated(unsigned long addr,
920 unsigned long *val,
921 unsigned int bytes,
922 struct x86_emulate_ctxt *ctxt)
923{
924 struct kvm_vcpu *vcpu = ctxt->vcpu;
925
926 if (vcpu->mmio_read_completed) {
927 memcpy(val, vcpu->mmio_data, bytes);
928 vcpu->mmio_read_completed = 0;
929 return X86EMUL_CONTINUE;
930 } else if (emulator_read_std(addr, val, bytes, ctxt)
931 == X86EMUL_CONTINUE)
932 return X86EMUL_CONTINUE;
933 else {
934 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200935
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 if (gpa == UNMAPPED_GVA)
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200937 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 vcpu->mmio_needed = 1;
939 vcpu->mmio_phys_addr = gpa;
940 vcpu->mmio_size = bytes;
941 vcpu->mmio_is_write = 0;
942
943 return X86EMUL_UNHANDLEABLE;
944 }
945}
946
Avi Kivityda4a00f2007-01-05 16:36:44 -0800947static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
948 unsigned long val, int bytes)
949{
950 struct kvm_memory_slot *m;
951 struct page *page;
952 void *virt;
953
954 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
955 return 0;
956 m = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
957 if (!m)
958 return 0;
959 page = gfn_to_page(m, gpa >> PAGE_SHIFT);
960 kvm_mmu_pre_write(vcpu, gpa, bytes);
Uri Lublinab51a432007-02-21 18:25:21 +0200961 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -0800962 virt = kmap_atomic(page, KM_USER0);
963 memcpy(virt + offset_in_page(gpa), &val, bytes);
964 kunmap_atomic(virt, KM_USER0);
965 kvm_mmu_post_write(vcpu, gpa, bytes);
966 return 1;
967}
968
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969static int emulator_write_emulated(unsigned long addr,
970 unsigned long val,
971 unsigned int bytes,
972 struct x86_emulate_ctxt *ctxt)
973{
974 struct kvm_vcpu *vcpu = ctxt->vcpu;
975 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
976
977 if (gpa == UNMAPPED_GVA)
978 return X86EMUL_PROPAGATE_FAULT;
979
Avi Kivityda4a00f2007-01-05 16:36:44 -0800980 if (emulator_write_phys(vcpu, gpa, val, bytes))
981 return X86EMUL_CONTINUE;
982
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983 vcpu->mmio_needed = 1;
984 vcpu->mmio_phys_addr = gpa;
985 vcpu->mmio_size = bytes;
986 vcpu->mmio_is_write = 1;
987 memcpy(vcpu->mmio_data, &val, bytes);
988
989 return X86EMUL_CONTINUE;
990}
991
992static int emulator_cmpxchg_emulated(unsigned long addr,
993 unsigned long old,
994 unsigned long new,
995 unsigned int bytes,
996 struct x86_emulate_ctxt *ctxt)
997{
998 static int reported;
999
1000 if (!reported) {
1001 reported = 1;
1002 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1003 }
1004 return emulator_write_emulated(addr, new, bytes, ctxt);
1005}
1006
Avi Kivity32b35622007-01-05 16:36:51 -08001007#ifdef CONFIG_X86_32
1008
1009static int emulator_cmpxchg8b_emulated(unsigned long addr,
1010 unsigned long old_lo,
1011 unsigned long old_hi,
1012 unsigned long new_lo,
1013 unsigned long new_hi,
1014 struct x86_emulate_ctxt *ctxt)
1015{
1016 static int reported;
1017 int r;
1018
1019 if (!reported) {
1020 reported = 1;
1021 printk(KERN_WARNING "kvm: emulating exchange8b as write\n");
1022 }
1023 r = emulator_write_emulated(addr, new_lo, 4, ctxt);
1024 if (r != X86EMUL_CONTINUE)
1025 return r;
1026 return emulator_write_emulated(addr+4, new_hi, 4, ctxt);
1027}
1028
1029#endif
1030
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1032{
1033 return kvm_arch_ops->get_segment_base(vcpu, seg);
1034}
1035
1036int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1037{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 return X86EMUL_CONTINUE;
1039}
1040
1041int emulate_clts(struct kvm_vcpu *vcpu)
1042{
Avi Kivity399badf2007-01-05 16:36:38 -08001043 unsigned long cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044
Avi Kivity399badf2007-01-05 16:36:38 -08001045 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1046 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 kvm_arch_ops->set_cr0(vcpu, cr0);
1048 return X86EMUL_CONTINUE;
1049}
1050
1051int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1052{
1053 struct kvm_vcpu *vcpu = ctxt->vcpu;
1054
1055 switch (dr) {
1056 case 0 ... 3:
1057 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1058 return X86EMUL_CONTINUE;
1059 default:
1060 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1061 __FUNCTION__, dr);
1062 return X86EMUL_UNHANDLEABLE;
1063 }
1064}
1065
1066int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1067{
1068 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1069 int exception;
1070
1071 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1072 if (exception) {
1073 /* FIXME: better handling */
1074 return X86EMUL_UNHANDLEABLE;
1075 }
1076 return X86EMUL_CONTINUE;
1077}
1078
1079static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1080{
1081 static int reported;
1082 u8 opcodes[4];
1083 unsigned long rip = ctxt->vcpu->rip;
1084 unsigned long rip_linear;
1085
1086 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1087
1088 if (reported)
1089 return;
1090
1091 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1092
1093 printk(KERN_ERR "emulation failed but !mmio_needed?"
1094 " rip %lx %02x %02x %02x %02x\n",
1095 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1096 reported = 1;
1097}
1098
1099struct x86_emulate_ops emulate_ops = {
1100 .read_std = emulator_read_std,
1101 .write_std = emulator_write_std,
1102 .read_emulated = emulator_read_emulated,
1103 .write_emulated = emulator_write_emulated,
1104 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity32b35622007-01-05 16:36:51 -08001105#ifdef CONFIG_X86_32
1106 .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated,
1107#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108};
1109
1110int emulate_instruction(struct kvm_vcpu *vcpu,
1111 struct kvm_run *run,
1112 unsigned long cr2,
1113 u16 error_code)
1114{
1115 struct x86_emulate_ctxt emulate_ctxt;
1116 int r;
1117 int cs_db, cs_l;
1118
1119 kvm_arch_ops->cache_regs(vcpu);
1120
1121 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1122
1123 emulate_ctxt.vcpu = vcpu;
1124 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1125 emulate_ctxt.cr2 = cr2;
1126 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1127 ? X86EMUL_MODE_REAL : cs_l
1128 ? X86EMUL_MODE_PROT64 : cs_db
1129 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1130
1131 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1132 emulate_ctxt.cs_base = 0;
1133 emulate_ctxt.ds_base = 0;
1134 emulate_ctxt.es_base = 0;
1135 emulate_ctxt.ss_base = 0;
1136 } else {
1137 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1138 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1139 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1140 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1141 }
1142
1143 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1144 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1145
1146 vcpu->mmio_is_write = 0;
1147 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1148
1149 if ((r || vcpu->mmio_is_write) && run) {
1150 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1151 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1152 run->mmio.len = vcpu->mmio_size;
1153 run->mmio.is_write = vcpu->mmio_is_write;
1154 }
1155
1156 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001157 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1158 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159 if (!vcpu->mmio_needed) {
1160 report_emulation_failure(&emulate_ctxt);
1161 return EMULATE_FAIL;
1162 }
1163 return EMULATE_DO_MMIO;
1164 }
1165
1166 kvm_arch_ops->decache_regs(vcpu);
1167 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1168
1169 if (vcpu->mmio_is_write)
1170 return EMULATE_DO_MMIO;
1171
1172 return EMULATE_DONE;
1173}
1174EXPORT_SYMBOL_GPL(emulate_instruction);
1175
Avi Kivity270fd9b2007-02-19 14:37:47 +02001176int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1177{
1178 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1179
1180 kvm_arch_ops->decache_regs(vcpu);
1181 ret = -KVM_EINVAL;
1182#ifdef CONFIG_X86_64
1183 if (is_long_mode(vcpu)) {
1184 nr = vcpu->regs[VCPU_REGS_RAX];
1185 a0 = vcpu->regs[VCPU_REGS_RDI];
1186 a1 = vcpu->regs[VCPU_REGS_RSI];
1187 a2 = vcpu->regs[VCPU_REGS_RDX];
1188 a3 = vcpu->regs[VCPU_REGS_RCX];
1189 a4 = vcpu->regs[VCPU_REGS_R8];
1190 a5 = vcpu->regs[VCPU_REGS_R9];
1191 } else
1192#endif
1193 {
1194 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1195 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1196 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1197 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1198 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1199 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1200 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1201 }
1202 switch (nr) {
1203 default:
1204 ;
1205 }
1206 vcpu->regs[VCPU_REGS_RAX] = ret;
1207 kvm_arch_ops->cache_regs(vcpu);
1208 return 1;
1209}
1210EXPORT_SYMBOL_GPL(kvm_hypercall);
1211
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1213{
1214 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1215}
1216
1217void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1218{
1219 struct descriptor_table dt = { limit, base };
1220
1221 kvm_arch_ops->set_gdt(vcpu, &dt);
1222}
1223
1224void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1225{
1226 struct descriptor_table dt = { limit, base };
1227
1228 kvm_arch_ops->set_idt(vcpu, &dt);
1229}
1230
1231void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1232 unsigned long *rflags)
1233{
1234 lmsw(vcpu, msw);
1235 *rflags = kvm_arch_ops->get_rflags(vcpu);
1236}
1237
1238unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1239{
Avi Kivity399badf2007-01-05 16:36:38 -08001240 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241 switch (cr) {
1242 case 0:
1243 return vcpu->cr0;
1244 case 2:
1245 return vcpu->cr2;
1246 case 3:
1247 return vcpu->cr3;
1248 case 4:
1249 return vcpu->cr4;
1250 default:
1251 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1252 return 0;
1253 }
1254}
1255
1256void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1257 unsigned long *rflags)
1258{
1259 switch (cr) {
1260 case 0:
1261 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1262 *rflags = kvm_arch_ops->get_rflags(vcpu);
1263 break;
1264 case 2:
1265 vcpu->cr2 = val;
1266 break;
1267 case 3:
1268 set_cr3(vcpu, val);
1269 break;
1270 case 4:
1271 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1272 break;
1273 default:
1274 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1275 }
1276}
1277
Ingo Molnar102d8322007-02-19 14:37:47 +02001278/*
1279 * Register the para guest with the host:
1280 */
1281static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1282{
1283 struct kvm_vcpu_para_state *para_state;
1284 hpa_t para_state_hpa, hypercall_hpa;
1285 struct page *para_state_page;
1286 unsigned char *hypercall;
1287 gpa_t hypercall_gpa;
1288
1289 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1290 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1291
1292 /*
1293 * Needs to be page aligned:
1294 */
1295 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1296 goto err_gp;
1297
1298 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1299 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1300 if (is_error_hpa(para_state_hpa))
1301 goto err_gp;
1302
Uri Lublinab51a432007-02-21 18:25:21 +02001303 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001304 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1305 para_state = kmap_atomic(para_state_page, KM_USER0);
1306
1307 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1308 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1309
1310 para_state->host_version = KVM_PARA_API_VERSION;
1311 /*
1312 * We cannot support guests that try to register themselves
1313 * with a newer API version than the host supports:
1314 */
1315 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1316 para_state->ret = -KVM_EINVAL;
1317 goto err_kunmap_skip;
1318 }
1319
1320 hypercall_gpa = para_state->hypercall_gpa;
1321 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1322 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1323 if (is_error_hpa(hypercall_hpa)) {
1324 para_state->ret = -KVM_EINVAL;
1325 goto err_kunmap_skip;
1326 }
1327
1328 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1329 vcpu->para_state_page = para_state_page;
1330 vcpu->para_state_gpa = para_state_gpa;
1331 vcpu->hypercall_gpa = hypercall_gpa;
1332
Uri Lublinab51a432007-02-21 18:25:21 +02001333 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001334 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1335 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1336 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1337 kunmap_atomic(hypercall, KM_USER1);
1338
1339 para_state->ret = 0;
1340err_kunmap_skip:
1341 kunmap_atomic(para_state, KM_USER0);
1342 return 0;
1343err_gp:
1344 return 1;
1345}
1346
Avi Kivity3bab1f52006-12-29 16:49:48 -08001347int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1348{
1349 u64 data;
1350
1351 switch (msr) {
1352 case 0xc0010010: /* SYSCFG */
1353 case 0xc0010015: /* HWCR */
1354 case MSR_IA32_PLATFORM_ID:
1355 case MSR_IA32_P5_MC_ADDR:
1356 case MSR_IA32_P5_MC_TYPE:
1357 case MSR_IA32_MC0_CTL:
1358 case MSR_IA32_MCG_STATUS:
1359 case MSR_IA32_MCG_CAP:
1360 case MSR_IA32_MC0_MISC:
1361 case MSR_IA32_MC0_MISC+4:
1362 case MSR_IA32_MC0_MISC+8:
1363 case MSR_IA32_MC0_MISC+12:
1364 case MSR_IA32_MC0_MISC+16:
1365 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001366 case MSR_IA32_PERF_STATUS:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001367 /* MTRR registers */
1368 case 0xfe:
1369 case 0x200 ... 0x2ff:
1370 data = 0;
1371 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001372 case 0xcd: /* fsb frequency */
1373 data = 3;
1374 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001375 case MSR_IA32_APICBASE:
1376 data = vcpu->apic_base;
1377 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001378 case MSR_IA32_MISC_ENABLE:
1379 data = vcpu->ia32_misc_enable_msr;
1380 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001381#ifdef CONFIG_X86_64
1382 case MSR_EFER:
1383 data = vcpu->shadow_efer;
1384 break;
1385#endif
1386 default:
1387 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1388 return 1;
1389 }
1390 *pdata = data;
1391 return 0;
1392}
1393EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1394
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395/*
1396 * Reads an msr value (of 'msr_index') into 'pdata'.
1397 * Returns 0 on success, non-0 otherwise.
1398 * Assumes vcpu_load() was already called.
1399 */
1400static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1401{
1402 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1403}
1404
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001405#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406
Avi Kivity3bab1f52006-12-29 16:49:48 -08001407static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409 if (efer & EFER_RESERVED_BITS) {
1410 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1411 efer);
1412 inject_gp(vcpu);
1413 return;
1414 }
1415
1416 if (is_paging(vcpu)
1417 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1418 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1419 inject_gp(vcpu);
1420 return;
1421 }
1422
Avi Kivity7725f0b2006-12-13 00:34:01 -08001423 kvm_arch_ops->set_efer(vcpu, efer);
1424
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425 efer &= ~EFER_LMA;
1426 efer |= vcpu->shadow_efer & EFER_LMA;
1427
1428 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430
1431#endif
1432
Avi Kivity3bab1f52006-12-29 16:49:48 -08001433int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1434{
1435 switch (msr) {
1436#ifdef CONFIG_X86_64
1437 case MSR_EFER:
1438 set_efer(vcpu, data);
1439 break;
1440#endif
1441 case MSR_IA32_MC0_STATUS:
1442 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1443 __FUNCTION__, data);
1444 break;
1445 case MSR_IA32_UCODE_REV:
1446 case MSR_IA32_UCODE_WRITE:
1447 case 0x200 ... 0x2ff: /* MTRRs */
1448 break;
1449 case MSR_IA32_APICBASE:
1450 vcpu->apic_base = data;
1451 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001452 case MSR_IA32_MISC_ENABLE:
1453 vcpu->ia32_misc_enable_msr = data;
1454 break;
Ingo Molnar102d8322007-02-19 14:37:47 +02001455 /*
1456 * This is the 'probe whether the host is KVM' logic:
1457 */
1458 case MSR_KVM_API_MAGIC:
1459 return vcpu_register_para(vcpu, data);
1460
Avi Kivity3bab1f52006-12-29 16:49:48 -08001461 default:
1462 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1463 return 1;
1464 }
1465 return 0;
1466}
1467EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1468
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469/*
1470 * Writes msr value into into the appropriate "register".
1471 * Returns 0 on success, non-0 otherwise.
1472 * Assumes vcpu_load() was already called.
1473 */
1474static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1475{
1476 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1477}
1478
1479void kvm_resched(struct kvm_vcpu *vcpu)
1480{
1481 vcpu_put(vcpu);
1482 cond_resched();
Avi Kivitybccf2152007-02-21 18:04:26 +02001483 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484}
1485EXPORT_SYMBOL_GPL(kvm_resched);
1486
1487void load_msrs(struct vmx_msr_entry *e, int n)
1488{
1489 int i;
1490
1491 for (i = 0; i < n; ++i)
1492 wrmsrl(e[i].index, e[i].data);
1493}
1494EXPORT_SYMBOL_GPL(load_msrs);
1495
1496void save_msrs(struct vmx_msr_entry *e, int n)
1497{
1498 int i;
1499
1500 for (i = 0; i < n; ++i)
1501 rdmsrl(e[i].index, e[i].data);
1502}
1503EXPORT_SYMBOL_GPL(save_msrs);
1504
Avi Kivitybccf2152007-02-21 18:04:26 +02001505static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 int r;
1508
Avi Kivitybccf2152007-02-21 18:04:26 +02001509 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510
Dor Laor54810342007-02-12 00:54:39 -08001511 /* re-sync apic's tpr */
1512 vcpu->cr8 = kvm_run->cr8;
1513
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514 if (kvm_run->emulated) {
1515 kvm_arch_ops->skip_emulated_instruction(vcpu);
1516 kvm_run->emulated = 0;
1517 }
1518
1519 if (kvm_run->mmio_completed) {
1520 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1521 vcpu->mmio_read_completed = 1;
1522 }
1523
1524 vcpu->mmio_needed = 0;
1525
1526 r = kvm_arch_ops->run(vcpu, kvm_run);
1527
1528 vcpu_put(vcpu);
1529 return r;
1530}
1531
Avi Kivitybccf2152007-02-21 18:04:26 +02001532static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1533 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534{
Avi Kivitybccf2152007-02-21 18:04:26 +02001535 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001536
1537 kvm_arch_ops->cache_regs(vcpu);
1538
1539 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1540 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1541 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1542 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1543 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1544 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1545 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1546 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001547#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1549 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1550 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1551 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1552 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1553 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1554 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1555 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1556#endif
1557
1558 regs->rip = vcpu->rip;
1559 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1560
1561 /*
1562 * Don't leak debug flags in case they were set for guest debugging
1563 */
1564 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1565 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1566
1567 vcpu_put(vcpu);
1568
1569 return 0;
1570}
1571
Avi Kivitybccf2152007-02-21 18:04:26 +02001572static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1573 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574{
Avi Kivitybccf2152007-02-21 18:04:26 +02001575 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001576
1577 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1578 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1579 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1580 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1581 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1582 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1583 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1584 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001585#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1587 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1588 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1589 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1590 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1591 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1592 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1593 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1594#endif
1595
1596 vcpu->rip = regs->rip;
1597 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1598
1599 kvm_arch_ops->decache_regs(vcpu);
1600
1601 vcpu_put(vcpu);
1602
1603 return 0;
1604}
1605
1606static void get_segment(struct kvm_vcpu *vcpu,
1607 struct kvm_segment *var, int seg)
1608{
1609 return kvm_arch_ops->get_segment(vcpu, var, seg);
1610}
1611
Avi Kivitybccf2152007-02-21 18:04:26 +02001612static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1613 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615 struct descriptor_table dt;
1616
Avi Kivitybccf2152007-02-21 18:04:26 +02001617 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618
1619 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1620 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1621 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1622 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1623 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1624 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1625
1626 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1627 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1628
1629 kvm_arch_ops->get_idt(vcpu, &dt);
1630 sregs->idt.limit = dt.limit;
1631 sregs->idt.base = dt.base;
1632 kvm_arch_ops->get_gdt(vcpu, &dt);
1633 sregs->gdt.limit = dt.limit;
1634 sregs->gdt.base = dt.base;
1635
Avi Kivity399badf2007-01-05 16:36:38 -08001636 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637 sregs->cr0 = vcpu->cr0;
1638 sregs->cr2 = vcpu->cr2;
1639 sregs->cr3 = vcpu->cr3;
1640 sregs->cr4 = vcpu->cr4;
1641 sregs->cr8 = vcpu->cr8;
1642 sregs->efer = vcpu->shadow_efer;
1643 sregs->apic_base = vcpu->apic_base;
1644
1645 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1646 sizeof sregs->interrupt_bitmap);
1647
1648 vcpu_put(vcpu);
1649
1650 return 0;
1651}
1652
1653static void set_segment(struct kvm_vcpu *vcpu,
1654 struct kvm_segment *var, int seg)
1655{
1656 return kvm_arch_ops->set_segment(vcpu, var, seg);
1657}
1658
Avi Kivitybccf2152007-02-21 18:04:26 +02001659static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1660 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662 int mmu_reset_needed = 0;
1663 int i;
1664 struct descriptor_table dt;
1665
Avi Kivitybccf2152007-02-21 18:04:26 +02001666 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667
1668 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1669 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1670 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1671 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1672 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1673 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1674
1675 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1676 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1677
1678 dt.limit = sregs->idt.limit;
1679 dt.base = sregs->idt.base;
1680 kvm_arch_ops->set_idt(vcpu, &dt);
1681 dt.limit = sregs->gdt.limit;
1682 dt.base = sregs->gdt.base;
1683 kvm_arch_ops->set_gdt(vcpu, &dt);
1684
1685 vcpu->cr2 = sregs->cr2;
1686 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
1687 vcpu->cr3 = sregs->cr3;
1688
1689 vcpu->cr8 = sregs->cr8;
1690
1691 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001692#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 kvm_arch_ops->set_efer(vcpu, sregs->efer);
1694#endif
1695 vcpu->apic_base = sregs->apic_base;
1696
Avi Kivity399badf2007-01-05 16:36:38 -08001697 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1698
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
1700 kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0);
1701
1702 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
1703 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08001704 if (!is_long_mode(vcpu) && is_pae(vcpu))
1705 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706
1707 if (mmu_reset_needed)
1708 kvm_mmu_reset_context(vcpu);
1709
1710 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
1711 sizeof vcpu->irq_pending);
1712 vcpu->irq_summary = 0;
1713 for (i = 0; i < NR_IRQ_WORDS; ++i)
1714 if (vcpu->irq_pending[i])
1715 __set_bit(i, &vcpu->irq_summary);
1716
1717 vcpu_put(vcpu);
1718
1719 return 0;
1720}
1721
1722/*
1723 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1724 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08001725 *
1726 * This list is modified at module load time to reflect the
1727 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001728 */
1729static u32 msrs_to_save[] = {
1730 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
1731 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001732#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001733 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1734#endif
1735 MSR_IA32_TIME_STAMP_COUNTER,
1736};
1737
Michael Riepebf591b22006-12-22 01:05:36 -08001738static unsigned num_msrs_to_save;
1739
Avi Kivity6f00e682007-01-26 00:56:40 -08001740static u32 emulated_msrs[] = {
1741 MSR_IA32_MISC_ENABLE,
1742};
1743
Michael Riepebf591b22006-12-22 01:05:36 -08001744static __init void kvm_init_msr_list(void)
1745{
1746 u32 dummy[2];
1747 unsigned i, j;
1748
1749 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1750 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1751 continue;
1752 if (j < i)
1753 msrs_to_save[j] = msrs_to_save[i];
1754 j++;
1755 }
1756 num_msrs_to_save = j;
1757}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758
1759/*
1760 * Adapt set_msr() to msr_io()'s calling convention
1761 */
1762static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1763{
1764 return set_msr(vcpu, index, *data);
1765}
1766
1767/*
1768 * Read or write a bunch of msrs. All parameters are kernel addresses.
1769 *
1770 * @return number of msrs set successfully.
1771 */
Avi Kivitybccf2152007-02-21 18:04:26 +02001772static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773 struct kvm_msr_entry *entries,
1774 int (*do_msr)(struct kvm_vcpu *vcpu,
1775 unsigned index, u64 *data))
1776{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001777 int i;
1778
Avi Kivitybccf2152007-02-21 18:04:26 +02001779 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780
1781 for (i = 0; i < msrs->nmsrs; ++i)
1782 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1783 break;
1784
1785 vcpu_put(vcpu);
1786
1787 return i;
1788}
1789
1790/*
1791 * Read or write a bunch of msrs. Parameters are user addresses.
1792 *
1793 * @return number of msrs set successfully.
1794 */
Avi Kivitybccf2152007-02-21 18:04:26 +02001795static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796 int (*do_msr)(struct kvm_vcpu *vcpu,
1797 unsigned index, u64 *data),
1798 int writeback)
1799{
1800 struct kvm_msrs msrs;
1801 struct kvm_msr_entry *entries;
1802 int r, n;
1803 unsigned size;
1804
1805 r = -EFAULT;
1806 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1807 goto out;
1808
1809 r = -E2BIG;
1810 if (msrs.nmsrs >= MAX_IO_MSRS)
1811 goto out;
1812
1813 r = -ENOMEM;
1814 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1815 entries = vmalloc(size);
1816 if (!entries)
1817 goto out;
1818
1819 r = -EFAULT;
1820 if (copy_from_user(entries, user_msrs->entries, size))
1821 goto out_free;
1822
Avi Kivitybccf2152007-02-21 18:04:26 +02001823 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824 if (r < 0)
1825 goto out_free;
1826
1827 r = -EFAULT;
1828 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1829 goto out_free;
1830
1831 r = n;
1832
1833out_free:
1834 vfree(entries);
1835out:
1836 return r;
1837}
1838
1839/*
1840 * Translate a guest virtual address to a guest physical address.
1841 */
Avi Kivitybccf2152007-02-21 18:04:26 +02001842static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1843 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844{
1845 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001846 gpa_t gpa;
1847
Avi Kivitybccf2152007-02-21 18:04:26 +02001848 vcpu_load(vcpu);
1849 spin_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
1851 tr->physical_address = gpa;
1852 tr->valid = gpa != UNMAPPED_GVA;
1853 tr->writeable = 1;
1854 tr->usermode = 0;
Avi Kivitybccf2152007-02-21 18:04:26 +02001855 spin_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856 vcpu_put(vcpu);
1857
1858 return 0;
1859}
1860
Avi Kivitybccf2152007-02-21 18:04:26 +02001861static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1862 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864 if (irq->irq < 0 || irq->irq >= 256)
1865 return -EINVAL;
Avi Kivitybccf2152007-02-21 18:04:26 +02001866 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867
1868 set_bit(irq->irq, vcpu->irq_pending);
1869 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
1870
1871 vcpu_put(vcpu);
1872
1873 return 0;
1874}
1875
Avi Kivitybccf2152007-02-21 18:04:26 +02001876static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
1877 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879 int r;
1880
Avi Kivitybccf2152007-02-21 18:04:26 +02001881 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001882
1883 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
1884
1885 vcpu_put(vcpu);
1886
1887 return r;
1888}
1889
Avi Kivitybccf2152007-02-21 18:04:26 +02001890static int kvm_vcpu_release(struct inode *inode, struct file *filp)
1891{
1892 struct kvm_vcpu *vcpu = filp->private_data;
1893
1894 fput(vcpu->kvm->filp);
1895 return 0;
1896}
1897
1898static struct file_operations kvm_vcpu_fops = {
1899 .release = kvm_vcpu_release,
1900 .unlocked_ioctl = kvm_vcpu_ioctl,
1901 .compat_ioctl = kvm_vcpu_ioctl,
1902};
1903
1904/*
1905 * Allocates an inode for the vcpu.
1906 */
1907static int create_vcpu_fd(struct kvm_vcpu *vcpu)
1908{
1909 int fd, r;
1910 struct inode *inode;
1911 struct file *file;
1912
1913 atomic_inc(&vcpu->kvm->filp->f_count);
1914 inode = kvmfs_inode(&kvm_vcpu_fops);
1915 if (IS_ERR(inode)) {
1916 r = PTR_ERR(inode);
1917 goto out1;
1918 }
1919
1920 file = kvmfs_file(inode, vcpu);
1921 if (IS_ERR(file)) {
1922 r = PTR_ERR(file);
1923 goto out2;
1924 }
1925
1926 r = get_unused_fd();
1927 if (r < 0)
1928 goto out3;
1929 fd = r;
1930 fd_install(fd, file);
1931
1932 return fd;
1933
1934out3:
1935 fput(file);
1936out2:
1937 iput(inode);
1938out1:
1939 fput(vcpu->kvm->filp);
1940 return r;
1941}
1942
Avi Kivityc5ea7662007-02-20 18:41:05 +02001943/*
1944 * Creates some virtual cpus. Good luck creating more than one.
1945 */
1946static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
1947{
1948 int r;
1949 struct kvm_vcpu *vcpu;
1950
1951 r = -EINVAL;
1952 if (!valid_vcpu(n))
1953 goto out;
1954
1955 vcpu = &kvm->vcpus[n];
1956
1957 mutex_lock(&vcpu->mutex);
1958
1959 if (vcpu->vmcs) {
1960 mutex_unlock(&vcpu->mutex);
1961 return -EEXIST;
1962 }
1963
1964 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
1965 FX_IMAGE_ALIGN);
1966 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
1967
1968 r = kvm_arch_ops->vcpu_create(vcpu);
1969 if (r < 0)
1970 goto out_free_vcpus;
1971
1972 r = kvm_mmu_create(vcpu);
1973 if (r < 0)
1974 goto out_free_vcpus;
1975
1976 kvm_arch_ops->vcpu_load(vcpu);
1977 r = kvm_mmu_setup(vcpu);
1978 if (r >= 0)
1979 r = kvm_arch_ops->vcpu_setup(vcpu);
1980 vcpu_put(vcpu);
1981
1982 if (r < 0)
1983 goto out_free_vcpus;
1984
Avi Kivitybccf2152007-02-21 18:04:26 +02001985 r = create_vcpu_fd(vcpu);
1986 if (r < 0)
1987 goto out_free_vcpus;
1988
1989 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02001990
1991out_free_vcpus:
1992 kvm_free_vcpu(vcpu);
1993 mutex_unlock(&vcpu->mutex);
1994out:
1995 return r;
1996}
1997
Avi Kivitybccf2152007-02-21 18:04:26 +02001998static long kvm_vcpu_ioctl(struct file *filp,
1999 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000{
Avi Kivitybccf2152007-02-21 18:04:26 +02002001 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002002 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003 int r = -EINVAL;
2004
2005 switch (ioctl) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002006 case KVM_RUN: {
2007 struct kvm_run kvm_run;
2008
2009 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002010 if (copy_from_user(&kvm_run, argp, sizeof kvm_run))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002012 r = kvm_vcpu_ioctl_run(vcpu, &kvm_run);
Dor Laorc1150d82007-01-05 16:36:24 -08002013 if (r < 0 && r != -EINTR)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002014 goto out;
Al Viro2f3669872007-02-09 16:38:35 +00002015 if (copy_to_user(argp, &kvm_run, sizeof kvm_run)) {
Dor Laorc1150d82007-01-05 16:36:24 -08002016 r = -EFAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002017 goto out;
Dor Laorc1150d82007-01-05 16:36:24 -08002018 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019 break;
2020 }
2021 case KVM_GET_REGS: {
2022 struct kvm_regs kvm_regs;
2023
Avi Kivitybccf2152007-02-21 18:04:26 +02002024 memset(&kvm_regs, 0, sizeof kvm_regs);
2025 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026 if (r)
2027 goto out;
2028 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002029 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002030 goto out;
2031 r = 0;
2032 break;
2033 }
2034 case KVM_SET_REGS: {
2035 struct kvm_regs kvm_regs;
2036
2037 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002038 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002039 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002040 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002041 if (r)
2042 goto out;
2043 r = 0;
2044 break;
2045 }
2046 case KVM_GET_SREGS: {
2047 struct kvm_sregs kvm_sregs;
2048
Avi Kivitybccf2152007-02-21 18:04:26 +02002049 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2050 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051 if (r)
2052 goto out;
2053 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002054 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055 goto out;
2056 r = 0;
2057 break;
2058 }
2059 case KVM_SET_SREGS: {
2060 struct kvm_sregs kvm_sregs;
2061
2062 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002063 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002065 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002066 if (r)
2067 goto out;
2068 r = 0;
2069 break;
2070 }
2071 case KVM_TRANSLATE: {
2072 struct kvm_translation tr;
2073
2074 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002075 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002077 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078 if (r)
2079 goto out;
2080 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002081 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082 goto out;
2083 r = 0;
2084 break;
2085 }
2086 case KVM_INTERRUPT: {
2087 struct kvm_interrupt irq;
2088
2089 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002090 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002092 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093 if (r)
2094 goto out;
2095 r = 0;
2096 break;
2097 }
2098 case KVM_DEBUG_GUEST: {
2099 struct kvm_debug_guest dbg;
2100
2101 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002102 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002104 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105 if (r)
2106 goto out;
2107 r = 0;
2108 break;
2109 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002110 case KVM_GET_MSRS:
2111 r = msr_io(vcpu, argp, get_msr, 1);
2112 break;
2113 case KVM_SET_MSRS:
2114 r = msr_io(vcpu, argp, do_set_msr, 0);
2115 break;
2116 default:
2117 ;
2118 }
2119out:
2120 return r;
2121}
2122
2123static long kvm_vm_ioctl(struct file *filp,
2124 unsigned int ioctl, unsigned long arg)
2125{
2126 struct kvm *kvm = filp->private_data;
2127 void __user *argp = (void __user *)arg;
2128 int r = -EINVAL;
2129
2130 switch (ioctl) {
2131 case KVM_CREATE_VCPU:
2132 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2133 if (r < 0)
2134 goto out;
2135 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136 case KVM_SET_MEMORY_REGION: {
2137 struct kvm_memory_region kvm_mem;
2138
2139 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002140 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002142 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143 if (r)
2144 goto out;
2145 break;
2146 }
2147 case KVM_GET_DIRTY_LOG: {
2148 struct kvm_dirty_log log;
2149
2150 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002151 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002152 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002153 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154 if (r)
2155 goto out;
2156 break;
2157 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002158 default:
2159 ;
2160 }
2161out:
2162 return r;
2163}
2164
2165static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2166 unsigned long address,
2167 int *type)
2168{
2169 struct kvm *kvm = vma->vm_file->private_data;
2170 unsigned long pgoff;
2171 struct kvm_memory_slot *slot;
2172 struct page *page;
2173
2174 *type = VM_FAULT_MINOR;
2175 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2176 slot = gfn_to_memslot(kvm, pgoff);
2177 if (!slot)
2178 return NOPAGE_SIGBUS;
2179 page = gfn_to_page(slot, pgoff);
2180 if (!page)
2181 return NOPAGE_SIGBUS;
2182 get_page(page);
2183 return page;
2184}
2185
2186static struct vm_operations_struct kvm_vm_vm_ops = {
2187 .nopage = kvm_vm_nopage,
2188};
2189
2190static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2191{
2192 vma->vm_ops = &kvm_vm_vm_ops;
2193 return 0;
2194}
2195
2196static struct file_operations kvm_vm_fops = {
2197 .release = kvm_vm_release,
2198 .unlocked_ioctl = kvm_vm_ioctl,
2199 .compat_ioctl = kvm_vm_ioctl,
2200 .mmap = kvm_vm_mmap,
2201};
2202
2203static int kvm_dev_ioctl_create_vm(void)
2204{
2205 int fd, r;
2206 struct inode *inode;
2207 struct file *file;
2208 struct kvm *kvm;
2209
2210 inode = kvmfs_inode(&kvm_vm_fops);
2211 if (IS_ERR(inode)) {
2212 r = PTR_ERR(inode);
2213 goto out1;
2214 }
2215
2216 kvm = kvm_create_vm();
2217 if (IS_ERR(kvm)) {
2218 r = PTR_ERR(kvm);
2219 goto out2;
2220 }
2221
2222 file = kvmfs_file(inode, kvm);
2223 if (IS_ERR(file)) {
2224 r = PTR_ERR(file);
2225 goto out3;
2226 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002227 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002228
2229 r = get_unused_fd();
2230 if (r < 0)
2231 goto out4;
2232 fd = r;
2233 fd_install(fd, file);
2234
2235 return fd;
2236
2237out4:
2238 fput(file);
2239out3:
2240 kvm_destroy_vm(kvm);
2241out2:
2242 iput(inode);
2243out1:
2244 return r;
2245}
2246
2247static long kvm_dev_ioctl(struct file *filp,
2248 unsigned int ioctl, unsigned long arg)
2249{
2250 void __user *argp = (void __user *)arg;
2251 int r = -EINVAL;
2252
2253 switch (ioctl) {
2254 case KVM_GET_API_VERSION:
2255 r = KVM_API_VERSION;
2256 break;
2257 case KVM_CREATE_VM:
2258 r = kvm_dev_ioctl_create_vm();
2259 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f3669872007-02-09 16:38:35 +00002261 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002262 struct kvm_msr_list msr_list;
2263 unsigned n;
2264
2265 r = -EFAULT;
2266 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2267 goto out;
2268 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08002269 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2271 goto out;
2272 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08002273 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274 goto out;
2275 r = -EFAULT;
2276 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08002277 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08002279 if (copy_to_user(user_msr_list->indices
2280 + num_msrs_to_save * sizeof(u32),
2281 &emulated_msrs,
2282 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2283 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08002285 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286 }
2287 default:
2288 ;
2289 }
2290out:
2291 return r;
2292}
2293
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294static struct file_operations kvm_chardev_ops = {
2295 .open = kvm_dev_open,
2296 .release = kvm_dev_release,
2297 .unlocked_ioctl = kvm_dev_ioctl,
2298 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299};
2300
2301static struct miscdevice kvm_dev = {
2302 MISC_DYNAMIC_MINOR,
2303 "kvm",
2304 &kvm_chardev_ops,
2305};
2306
2307static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2308 void *v)
2309{
2310 if (val == SYS_RESTART) {
2311 /*
2312 * Some (well, at least mine) BIOSes hang on reboot if
2313 * in vmx root mode.
2314 */
2315 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Al Viro8b6d44c2007-02-09 16:38:40 +00002316 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317 }
2318 return NOTIFY_OK;
2319}
2320
2321static struct notifier_block kvm_reboot_notifier = {
2322 .notifier_call = kvm_reboot,
2323 .priority = 0,
2324};
2325
Avi Kivity774c47f2007-02-12 00:54:47 -08002326/*
2327 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2328 * cached on it.
2329 */
2330static void decache_vcpus_on_cpu(int cpu)
2331{
2332 struct kvm *vm;
2333 struct kvm_vcpu *vcpu;
2334 int i;
2335
2336 spin_lock(&kvm_lock);
2337 list_for_each_entry(vm, &vm_list, vm_list)
2338 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2339 vcpu = &vm->vcpus[i];
2340 /*
2341 * If the vcpu is locked, then it is running on some
2342 * other cpu and therefore it is not cached on the
2343 * cpu in question.
2344 *
2345 * If it's not locked, check the last cpu it executed
2346 * on.
2347 */
2348 if (mutex_trylock(&vcpu->mutex)) {
2349 if (vcpu->cpu == cpu) {
2350 kvm_arch_ops->vcpu_decache(vcpu);
2351 vcpu->cpu = -1;
2352 }
2353 mutex_unlock(&vcpu->mutex);
2354 }
2355 }
2356 spin_unlock(&kvm_lock);
2357}
2358
2359static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2360 void *v)
2361{
2362 int cpu = (long)v;
2363
2364 switch (val) {
Jeremy Katz43934a32007-02-19 14:37:46 +02002365 case CPU_DOWN_PREPARE:
Avi Kivity774c47f2007-02-12 00:54:47 -08002366 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002367 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2368 cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002369 decache_vcpus_on_cpu(cpu);
2370 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
2371 NULL, 0, 1);
2372 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002373 case CPU_ONLINE:
2374 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2375 cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002376 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
2377 NULL, 0, 1);
2378 break;
2379 }
2380 return NOTIFY_OK;
2381}
2382
2383static struct notifier_block kvm_cpu_notifier = {
2384 .notifier_call = kvm_cpu_hotplug,
2385 .priority = 20, /* must be > scheduler priority */
2386};
2387
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388static __init void kvm_init_debug(void)
2389{
2390 struct kvm_stats_debugfs_item *p;
2391
Al Viro8b6d44c2007-02-09 16:38:40 +00002392 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393 for (p = debugfs_entries; p->name; ++p)
2394 p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
2395 p->data);
2396}
2397
2398static void kvm_exit_debug(void)
2399{
2400 struct kvm_stats_debugfs_item *p;
2401
2402 for (p = debugfs_entries; p->name; ++p)
2403 debugfs_remove(p->dentry);
2404 debugfs_remove(debugfs_dir);
2405}
2406
Avi Kivity59ae6c62007-02-12 00:54:48 -08002407static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2408{
2409 decache_vcpus_on_cpu(raw_smp_processor_id());
Avi Kivity19d14082007-02-19 14:37:48 +02002410 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002411 return 0;
2412}
2413
2414static int kvm_resume(struct sys_device *dev)
2415{
Avi Kivity19d14082007-02-19 14:37:48 +02002416 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002417 return 0;
2418}
2419
2420static struct sysdev_class kvm_sysdev_class = {
2421 set_kset_name("kvm"),
2422 .suspend = kvm_suspend,
2423 .resume = kvm_resume,
2424};
2425
2426static struct sys_device kvm_sysdev = {
2427 .id = 0,
2428 .cls = &kvm_sysdev_class,
2429};
2430
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431hpa_t bad_page_address;
2432
Avi Kivity37e29d92007-02-20 14:07:37 +02002433static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
2434 const char *dev_name, void *data, struct vfsmount *mnt)
2435{
Andrew Mortone9cdb1e2007-03-01 11:28:13 +02002436 return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
Avi Kivity37e29d92007-02-20 14:07:37 +02002437}
2438
2439static struct file_system_type kvm_fs_type = {
2440 .name = "kvmfs",
2441 .get_sb = kvmfs_get_sb,
2442 .kill_sb = kill_anon_super,
2443};
2444
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
2446{
2447 int r;
2448
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08002449 if (kvm_arch_ops) {
2450 printk(KERN_ERR "kvm: already loaded the other module\n");
2451 return -EEXIST;
2452 }
2453
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002454 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455 printk(KERN_ERR "kvm: no hardware support\n");
2456 return -EOPNOTSUPP;
2457 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002458 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459 printk(KERN_ERR "kvm: disabled by bios\n");
2460 return -EOPNOTSUPP;
2461 }
2462
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002463 kvm_arch_ops = ops;
2464
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465 r = kvm_arch_ops->hardware_setup();
2466 if (r < 0)
2467 return r;
2468
Al Viro8b6d44c2007-02-09 16:38:40 +00002469 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002470 r = register_cpu_notifier(&kvm_cpu_notifier);
2471 if (r)
2472 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473 register_reboot_notifier(&kvm_reboot_notifier);
2474
Avi Kivity59ae6c62007-02-12 00:54:48 -08002475 r = sysdev_class_register(&kvm_sysdev_class);
2476 if (r)
2477 goto out_free_2;
2478
2479 r = sysdev_register(&kvm_sysdev);
2480 if (r)
2481 goto out_free_3;
2482
Avi Kivity6aa8b732006-12-10 02:21:36 -08002483 kvm_chardev_ops.owner = module;
2484
2485 r = misc_register(&kvm_dev);
2486 if (r) {
2487 printk (KERN_ERR "kvm: misc device register failed\n");
2488 goto out_free;
2489 }
2490
2491 return r;
2492
2493out_free:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002494 sysdev_unregister(&kvm_sysdev);
2495out_free_3:
2496 sysdev_class_unregister(&kvm_sysdev_class);
2497out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002499 unregister_cpu_notifier(&kvm_cpu_notifier);
2500out_free_1:
Al Viro8b6d44c2007-02-09 16:38:40 +00002501 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002502 kvm_arch_ops->hardware_unsetup();
2503 return r;
2504}
2505
2506void kvm_exit_arch(void)
2507{
2508 misc_deregister(&kvm_dev);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002509 sysdev_unregister(&kvm_sysdev);
2510 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002512 unregister_cpu_notifier(&kvm_cpu_notifier);
Al Viro8b6d44c2007-02-09 16:38:40 +00002513 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514 kvm_arch_ops->hardware_unsetup();
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08002515 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516}
2517
2518static __init int kvm_init(void)
2519{
2520 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02002521 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522
Avi Kivity37e29d92007-02-20 14:07:37 +02002523 r = register_filesystem(&kvm_fs_type);
2524 if (r)
2525 goto out3;
2526
2527 kvmfs_mnt = kern_mount(&kvm_fs_type);
2528 r = PTR_ERR(kvmfs_mnt);
2529 if (IS_ERR(kvmfs_mnt))
2530 goto out2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531 kvm_init_debug();
2532
Michael Riepebf591b22006-12-22 01:05:36 -08002533 kvm_init_msr_list();
2534
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
2536 r = -ENOMEM;
2537 goto out;
2538 }
2539
2540 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
2541 memset(__va(bad_page_address), 0, PAGE_SIZE);
2542
Avi Kivity58e690e2007-02-26 16:29:43 +02002543 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544
2545out:
2546 kvm_exit_debug();
Avi Kivity37e29d92007-02-20 14:07:37 +02002547 mntput(kvmfs_mnt);
2548out2:
2549 unregister_filesystem(&kvm_fs_type);
2550out3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551 return r;
2552}
2553
2554static __exit void kvm_exit(void)
2555{
2556 kvm_exit_debug();
2557 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivity37e29d92007-02-20 14:07:37 +02002558 mntput(kvmfs_mnt);
2559 unregister_filesystem(&kvm_fs_type);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002560}
2561
2562module_init(kvm_init)
2563module_exit(kvm_exit)
2564
2565EXPORT_SYMBOL_GPL(kvm_init_arch);
2566EXPORT_SYMBOL_GPL(kvm_exit_arch);