blob: a0ec5dda330547d983cd5548b18c423713a136ed [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
Avi Kivity039576c2007-03-20 12:46:50 +0200349static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
350{
351 int i;
352
353 for (i = 0; i < 2; ++i)
354 if (vcpu->pio.guest_pages[i]) {
355 __free_page(vcpu->pio.guest_pages[i]);
356 vcpu->pio.guest_pages[i] = NULL;
357 }
358}
359
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
361{
Avi Kivitybccf2152007-02-21 18:04:26 +0200362 if (!vcpu->vmcs)
Ingo Molnar1e8ba6f2007-02-12 00:54:42 -0800363 return;
364
Avi Kivitybccf2152007-02-21 18:04:26 +0200365 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366 kvm_mmu_destroy(vcpu);
Avi Kivity08438472007-01-22 20:40:38 -0800367 vcpu_put(vcpu);
Avi Kivity9ede74e2007-01-05 16:36:55 -0800368 kvm_arch_ops->vcpu_free(vcpu);
Avi Kivity9a2bb7f2007-02-22 12:58:31 +0200369 free_page((unsigned long)vcpu->run);
370 vcpu->run = NULL;
Avi Kivity039576c2007-03-20 12:46:50 +0200371 free_page((unsigned long)vcpu->pio_data);
372 vcpu->pio_data = NULL;
373 free_pio_guest_pages(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374}
375
376static void kvm_free_vcpus(struct kvm *kvm)
377{
378 unsigned int i;
379
380 for (i = 0; i < KVM_MAX_VCPUS; ++i)
381 kvm_free_vcpu(&kvm->vcpus[i]);
382}
383
384static int kvm_dev_release(struct inode *inode, struct file *filp)
385{
Avi Kivityf17abe92007-02-21 19:28:04 +0200386 return 0;
387}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388
Avi Kivityf17abe92007-02-21 19:28:04 +0200389static void kvm_destroy_vm(struct kvm *kvm)
390{
Avi Kivity133de902007-02-12 00:54:44 -0800391 spin_lock(&kvm_lock);
392 list_del(&kvm->vm_list);
393 spin_unlock(&kvm_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394 kvm_free_vcpus(kvm);
395 kvm_free_physmem(kvm);
396 kfree(kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200397}
398
399static int kvm_vm_release(struct inode *inode, struct file *filp)
400{
401 struct kvm *kvm = filp->private_data;
402
403 kvm_destroy_vm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404 return 0;
405}
406
407static void inject_gp(struct kvm_vcpu *vcpu)
408{
409 kvm_arch_ops->inject_gp(vcpu, 0);
410}
411
Avi Kivity1342d352007-01-05 16:36:39 -0800412/*
413 * Load the pae pdptrs. Return true is they are all valid.
414 */
415static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416{
417 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
Avi Kivity1342d352007-01-05 16:36:39 -0800418 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419 int i;
420 u64 pdpte;
421 u64 *pdpt;
Avi Kivity1342d352007-01-05 16:36:39 -0800422 int ret;
Avi Kivity954bbbc22007-03-30 14:02:32 +0300423 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424
425 spin_lock(&vcpu->kvm->lock);
Avi Kivity954bbbc22007-03-30 14:02:32 +0300426 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
427 /* FIXME: !page - emulate? 0xff? */
428 pdpt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429
Avi Kivity1342d352007-01-05 16:36:39 -0800430 ret = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431 for (i = 0; i < 4; ++i) {
432 pdpte = pdpt[offset + i];
Avi Kivity1342d352007-01-05 16:36:39 -0800433 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
434 ret = 0;
435 goto out;
436 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 }
438
Avi Kivity1342d352007-01-05 16:36:39 -0800439 for (i = 0; i < 4; ++i)
440 vcpu->pdptrs[i] = pdpt[offset + i];
441
442out:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443 kunmap_atomic(pdpt, KM_USER0);
444 spin_unlock(&vcpu->kvm->lock);
445
Avi Kivity1342d352007-01-05 16:36:39 -0800446 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447}
448
449void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
450{
451 if (cr0 & CR0_RESEVED_BITS) {
452 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
453 cr0, vcpu->cr0);
454 inject_gp(vcpu);
455 return;
456 }
457
458 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
459 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
460 inject_gp(vcpu);
461 return;
462 }
463
464 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
465 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
466 "and a clear PE flag\n");
467 inject_gp(vcpu);
468 return;
469 }
470
471 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800472#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 if ((vcpu->shadow_efer & EFER_LME)) {
474 int cs_db, cs_l;
475
476 if (!is_pae(vcpu)) {
477 printk(KERN_DEBUG "set_cr0: #GP, start paging "
478 "in long mode while PAE is disabled\n");
479 inject_gp(vcpu);
480 return;
481 }
482 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
483 if (cs_l) {
484 printk(KERN_DEBUG "set_cr0: #GP, start paging "
485 "in long mode while CS.L == 1\n");
486 inject_gp(vcpu);
487 return;
488
489 }
490 } else
491#endif
Avi Kivity1342d352007-01-05 16:36:39 -0800492 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
494 "reserved bits\n");
495 inject_gp(vcpu);
496 return;
497 }
498
499 }
500
501 kvm_arch_ops->set_cr0(vcpu, cr0);
502 vcpu->cr0 = cr0;
503
504 spin_lock(&vcpu->kvm->lock);
505 kvm_mmu_reset_context(vcpu);
506 spin_unlock(&vcpu->kvm->lock);
507 return;
508}
509EXPORT_SYMBOL_GPL(set_cr0);
510
511void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
512{
Avi Kivity399badf2007-01-05 16:36:38 -0800513 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
515}
516EXPORT_SYMBOL_GPL(lmsw);
517
518void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
519{
520 if (cr4 & CR4_RESEVED_BITS) {
521 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
522 inject_gp(vcpu);
523 return;
524 }
525
Avi Kivitya9058ec2006-12-29 16:49:37 -0800526 if (is_long_mode(vcpu)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527 if (!(cr4 & CR4_PAE_MASK)) {
528 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
529 "in long mode\n");
530 inject_gp(vcpu);
531 return;
532 }
533 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
Avi Kivity1342d352007-01-05 16:36:39 -0800534 && !load_pdptrs(vcpu, vcpu->cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800535 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
536 inject_gp(vcpu);
537 }
538
539 if (cr4 & CR4_VMXE_MASK) {
540 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
541 inject_gp(vcpu);
542 return;
543 }
544 kvm_arch_ops->set_cr4(vcpu, cr4);
545 spin_lock(&vcpu->kvm->lock);
546 kvm_mmu_reset_context(vcpu);
547 spin_unlock(&vcpu->kvm->lock);
548}
549EXPORT_SYMBOL_GPL(set_cr4);
550
551void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
552{
Avi Kivitya9058ec2006-12-29 16:49:37 -0800553 if (is_long_mode(vcpu)) {
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200554 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
556 inject_gp(vcpu);
557 return;
558 }
559 } else {
560 if (cr3 & CR3_RESEVED_BITS) {
561 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
562 inject_gp(vcpu);
563 return;
564 }
565 if (is_paging(vcpu) && is_pae(vcpu) &&
Avi Kivity1342d352007-01-05 16:36:39 -0800566 !load_pdptrs(vcpu, cr3)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
568 "reserved bits\n");
569 inject_gp(vcpu);
570 return;
571 }
572 }
573
574 vcpu->cr3 = cr3;
575 spin_lock(&vcpu->kvm->lock);
Ingo Molnard21225e2007-01-05 16:36:59 -0800576 /*
577 * Does the new cr3 value map to physical memory? (Note, we
578 * catch an invalid cr3 even in real-mode, because it would
579 * cause trouble later on when we turn on paging anyway.)
580 *
581 * A real CPU would silently accept an invalid cr3 and would
582 * attempt to use it - with largely undefined (and often hard
583 * to debug) behavior on the guest side.
584 */
585 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
586 inject_gp(vcpu);
587 else
588 vcpu->mmu.new_cr3(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589 spin_unlock(&vcpu->kvm->lock);
590}
591EXPORT_SYMBOL_GPL(set_cr3);
592
593void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
594{
595 if ( cr8 & CR8_RESEVED_BITS) {
596 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
597 inject_gp(vcpu);
598 return;
599 }
600 vcpu->cr8 = cr8;
601}
602EXPORT_SYMBOL_GPL(set_cr8);
603
604void fx_init(struct kvm_vcpu *vcpu)
605{
606 struct __attribute__ ((__packed__)) fx_image_s {
607 u16 control; //fcw
608 u16 status; //fsw
609 u16 tag; // ftw
610 u16 opcode; //fop
611 u64 ip; // fpu ip
612 u64 operand;// fpu dp
613 u32 mxcsr;
614 u32 mxcsr_mask;
615
616 } *fx_image;
617
618 fx_save(vcpu->host_fx_image);
619 fpu_init();
620 fx_save(vcpu->guest_fx_image);
621 fx_restore(vcpu->host_fx_image);
622
623 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
624 fx_image->mxcsr = 0x1f80;
625 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
626 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
627}
628EXPORT_SYMBOL_GPL(fx_init);
629
Uri Lublin02b27c12007-02-22 17:15:33 +0200630static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
631{
632 spin_lock(&vcpu->kvm->lock);
633 kvm_mmu_slot_remove_write_access(vcpu, slot);
634 spin_unlock(&vcpu->kvm->lock);
635}
636
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638 * Allocate some memory and give it an address in the guest physical address
639 * space.
640 *
641 * Discontiguous memory is allowed, mostly for framebuffers.
642 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200643static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
644 struct kvm_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645{
646 int r;
647 gfn_t base_gfn;
648 unsigned long npages;
649 unsigned long i;
650 struct kvm_memory_slot *memslot;
651 struct kvm_memory_slot old, new;
652 int memory_config_version;
653
654 r = -EINVAL;
655 /* General sanity checks */
656 if (mem->memory_size & (PAGE_SIZE - 1))
657 goto out;
658 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
659 goto out;
660 if (mem->slot >= KVM_MEMORY_SLOTS)
661 goto out;
662 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
663 goto out;
664
665 memslot = &kvm->memslots[mem->slot];
666 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
667 npages = mem->memory_size >> PAGE_SHIFT;
668
669 if (!npages)
670 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
671
672raced:
673 spin_lock(&kvm->lock);
674
675 memory_config_version = kvm->memory_config_version;
676 new = old = *memslot;
677
678 new.base_gfn = base_gfn;
679 new.npages = npages;
680 new.flags = mem->flags;
681
682 /* Disallow changing a memory slot's size. */
683 r = -EINVAL;
684 if (npages && old.npages && npages != old.npages)
685 goto out_unlock;
686
687 /* Check for overlaps */
688 r = -EEXIST;
689 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
690 struct kvm_memory_slot *s = &kvm->memslots[i];
691
692 if (s == memslot)
693 continue;
694 if (!((base_gfn + npages <= s->base_gfn) ||
695 (base_gfn >= s->base_gfn + s->npages)))
696 goto out_unlock;
697 }
698 /*
699 * Do memory allocations outside lock. memory_config_version will
700 * detect any races.
701 */
702 spin_unlock(&kvm->lock);
703
704 /* Deallocate if slot is being removed */
705 if (!npages)
Al Viro8b6d44c2007-02-09 16:38:40 +0000706 new.phys_mem = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707
708 /* Free page dirty bitmap if unneeded */
709 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +0000710 new.dirty_bitmap = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711
712 r = -ENOMEM;
713
714 /* Allocate if a slot is being created */
715 if (npages && !new.phys_mem) {
716 new.phys_mem = vmalloc(npages * sizeof(struct page *));
717
718 if (!new.phys_mem)
719 goto out_free;
720
721 memset(new.phys_mem, 0, npages * sizeof(struct page *));
722 for (i = 0; i < npages; ++i) {
723 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
724 | __GFP_ZERO);
725 if (!new.phys_mem[i])
726 goto out_free;
Markus Rechberger5972e952007-02-19 14:37:47 +0200727 set_page_private(new.phys_mem[i],0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 }
729 }
730
731 /* Allocate page dirty bitmap if needed */
732 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
733 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
734
735 new.dirty_bitmap = vmalloc(dirty_bytes);
736 if (!new.dirty_bitmap)
737 goto out_free;
738 memset(new.dirty_bitmap, 0, dirty_bytes);
739 }
740
741 spin_lock(&kvm->lock);
742
743 if (memory_config_version != kvm->memory_config_version) {
744 spin_unlock(&kvm->lock);
745 kvm_free_physmem_slot(&new, &old);
746 goto raced;
747 }
748
749 r = -EAGAIN;
750 if (kvm->busy)
751 goto out_unlock;
752
753 if (mem->slot >= kvm->nmemslots)
754 kvm->nmemslots = mem->slot + 1;
755
756 *memslot = new;
757 ++kvm->memory_config_version;
758
759 spin_unlock(&kvm->lock);
760
761 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
762 struct kvm_vcpu *vcpu;
763
Avi Kivitybccf2152007-02-21 18:04:26 +0200764 vcpu = vcpu_load_slot(kvm, i);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765 if (!vcpu)
766 continue;
Uri Lublinff990d52007-02-22 17:37:32 +0200767 if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
768 do_remove_write_access(vcpu, mem->slot);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 kvm_mmu_reset_context(vcpu);
770 vcpu_put(vcpu);
771 }
772
773 kvm_free_physmem_slot(&old, &new);
774 return 0;
775
776out_unlock:
777 spin_unlock(&kvm->lock);
778out_free:
779 kvm_free_physmem_slot(&new, &old);
780out:
781 return r;
782}
783
784/*
785 * Get (and clear) the dirty memory log for a memory slot.
786 */
Avi Kivity2c6f5df2007-02-20 18:27:58 +0200787static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
788 struct kvm_dirty_log *log)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789{
790 struct kvm_memory_slot *memslot;
791 int r, i;
792 int n;
Avi Kivity714b93d2007-01-05 16:36:53 -0800793 int cleared;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 unsigned long any = 0;
795
796 spin_lock(&kvm->lock);
797
798 /*
799 * Prevent changes to guest memory configuration even while the lock
800 * is not taken.
801 */
802 ++kvm->busy;
803 spin_unlock(&kvm->lock);
804 r = -EINVAL;
805 if (log->slot >= KVM_MEMORY_SLOTS)
806 goto out;
807
808 memslot = &kvm->memslots[log->slot];
809 r = -ENOENT;
810 if (!memslot->dirty_bitmap)
811 goto out;
812
Uri Lublincd1a4a92007-02-22 16:43:09 +0200813 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814
Uri Lublincd1a4a92007-02-22 16:43:09 +0200815 for (i = 0; !any && i < n/sizeof(long); ++i)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816 any = memslot->dirty_bitmap[i];
817
818 r = -EFAULT;
819 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
820 goto out;
821
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822 if (any) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800823 cleared = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
Avi Kivitybccf2152007-02-21 18:04:26 +0200825 struct kvm_vcpu *vcpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800826
Avi Kivitybccf2152007-02-21 18:04:26 +0200827 vcpu = vcpu_load_slot(kvm, i);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828 if (!vcpu)
829 continue;
Avi Kivity714b93d2007-01-05 16:36:53 -0800830 if (!cleared) {
831 do_remove_write_access(vcpu, log->slot);
832 memset(memslot->dirty_bitmap, 0, n);
833 cleared = 1;
834 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835 kvm_arch_ops->tlb_flush(vcpu);
836 vcpu_put(vcpu);
837 }
838 }
839
840 r = 0;
841
842out:
843 spin_lock(&kvm->lock);
844 --kvm->busy;
845 spin_unlock(&kvm->lock);
846 return r;
847}
848
849struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
850{
851 int i;
852
853 for (i = 0; i < kvm->nmemslots; ++i) {
854 struct kvm_memory_slot *memslot = &kvm->memslots[i];
855
856 if (gfn >= memslot->base_gfn
857 && gfn < memslot->base_gfn + memslot->npages)
858 return memslot;
859 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000860 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861}
862EXPORT_SYMBOL_GPL(gfn_to_memslot);
863
Avi Kivity954bbbc22007-03-30 14:02:32 +0300864struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
865{
866 struct kvm_memory_slot *slot;
867
868 slot = gfn_to_memslot(kvm, gfn);
869 if (!slot)
870 return NULL;
871 return slot->phys_mem[gfn - slot->base_gfn];
872}
873EXPORT_SYMBOL_GPL(gfn_to_page);
874
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
876{
877 int i;
Al Viro8b6d44c2007-02-09 16:38:40 +0000878 struct kvm_memory_slot *memslot = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879 unsigned long rel_gfn;
880
881 for (i = 0; i < kvm->nmemslots; ++i) {
882 memslot = &kvm->memslots[i];
883
884 if (gfn >= memslot->base_gfn
885 && gfn < memslot->base_gfn + memslot->npages) {
886
887 if (!memslot || !memslot->dirty_bitmap)
888 return;
889
890 rel_gfn = gfn - memslot->base_gfn;
891
892 /* avoid RMW */
893 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
894 set_bit(rel_gfn, memslot->dirty_bitmap);
895 return;
896 }
897 }
898}
899
900static int emulator_read_std(unsigned long addr,
901 unsigned long *val,
902 unsigned int bytes,
903 struct x86_emulate_ctxt *ctxt)
904{
905 struct kvm_vcpu *vcpu = ctxt->vcpu;
906 void *data = val;
907
908 while (bytes) {
909 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
910 unsigned offset = addr & (PAGE_SIZE-1);
911 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
912 unsigned long pfn;
Avi Kivity954bbbc22007-03-30 14:02:32 +0300913 struct page *page;
914 void *page_virt;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915
916 if (gpa == UNMAPPED_GVA)
917 return X86EMUL_PROPAGATE_FAULT;
918 pfn = gpa >> PAGE_SHIFT;
Avi Kivity954bbbc22007-03-30 14:02:32 +0300919 page = gfn_to_page(vcpu->kvm, pfn);
920 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921 return X86EMUL_UNHANDLEABLE;
Avi Kivity954bbbc22007-03-30 14:02:32 +0300922 page_virt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923
Avi Kivity954bbbc22007-03-30 14:02:32 +0300924 memcpy(data, page_virt + offset, tocopy);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925
Avi Kivity954bbbc22007-03-30 14:02:32 +0300926 kunmap_atomic(page_virt, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927
928 bytes -= tocopy;
929 data += tocopy;
930 addr += tocopy;
931 }
932
933 return X86EMUL_CONTINUE;
934}
935
936static int emulator_write_std(unsigned long addr,
937 unsigned long val,
938 unsigned int bytes,
939 struct x86_emulate_ctxt *ctxt)
940{
941 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
942 addr, bytes);
943 return X86EMUL_UNHANDLEABLE;
944}
945
946static int emulator_read_emulated(unsigned long addr,
947 unsigned long *val,
948 unsigned int bytes,
949 struct x86_emulate_ctxt *ctxt)
950{
951 struct kvm_vcpu *vcpu = ctxt->vcpu;
952
953 if (vcpu->mmio_read_completed) {
954 memcpy(val, vcpu->mmio_data, bytes);
955 vcpu->mmio_read_completed = 0;
956 return X86EMUL_CONTINUE;
957 } else if (emulator_read_std(addr, val, bytes, ctxt)
958 == X86EMUL_CONTINUE)
959 return X86EMUL_CONTINUE;
960 else {
961 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200962
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 if (gpa == UNMAPPED_GVA)
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200964 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 vcpu->mmio_needed = 1;
966 vcpu->mmio_phys_addr = gpa;
967 vcpu->mmio_size = bytes;
968 vcpu->mmio_is_write = 0;
969
970 return X86EMUL_UNHANDLEABLE;
971 }
972}
973
Avi Kivityda4a00f2007-01-05 16:36:44 -0800974static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
975 unsigned long val, int bytes)
976{
Avi Kivityda4a00f2007-01-05 16:36:44 -0800977 struct page *page;
978 void *virt;
979
980 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
981 return 0;
Avi Kivity954bbbc22007-03-30 14:02:32 +0300982 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
983 if (!page)
Avi Kivityda4a00f2007-01-05 16:36:44 -0800984 return 0;
Avi Kivityda4a00f2007-01-05 16:36:44 -0800985 kvm_mmu_pre_write(vcpu, gpa, bytes);
Uri Lublinab51a432007-02-21 18:25:21 +0200986 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -0800987 virt = kmap_atomic(page, KM_USER0);
988 memcpy(virt + offset_in_page(gpa), &val, bytes);
989 kunmap_atomic(virt, KM_USER0);
990 kvm_mmu_post_write(vcpu, gpa, bytes);
991 return 1;
992}
993
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994static int emulator_write_emulated(unsigned long addr,
995 unsigned long val,
996 unsigned int bytes,
997 struct x86_emulate_ctxt *ctxt)
998{
999 struct kvm_vcpu *vcpu = ctxt->vcpu;
1000 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1001
1002 if (gpa == UNMAPPED_GVA)
1003 return X86EMUL_PROPAGATE_FAULT;
1004
Avi Kivityda4a00f2007-01-05 16:36:44 -08001005 if (emulator_write_phys(vcpu, gpa, val, bytes))
1006 return X86EMUL_CONTINUE;
1007
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 vcpu->mmio_needed = 1;
1009 vcpu->mmio_phys_addr = gpa;
1010 vcpu->mmio_size = bytes;
1011 vcpu->mmio_is_write = 1;
1012 memcpy(vcpu->mmio_data, &val, bytes);
1013
1014 return X86EMUL_CONTINUE;
1015}
1016
1017static int emulator_cmpxchg_emulated(unsigned long addr,
1018 unsigned long old,
1019 unsigned long new,
1020 unsigned int bytes,
1021 struct x86_emulate_ctxt *ctxt)
1022{
1023 static int reported;
1024
1025 if (!reported) {
1026 reported = 1;
1027 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1028 }
1029 return emulator_write_emulated(addr, new, bytes, ctxt);
1030}
1031
Avi Kivity32b35622007-01-05 16:36:51 -08001032#ifdef CONFIG_X86_32
1033
1034static int emulator_cmpxchg8b_emulated(unsigned long addr,
1035 unsigned long old_lo,
1036 unsigned long old_hi,
1037 unsigned long new_lo,
1038 unsigned long new_hi,
1039 struct x86_emulate_ctxt *ctxt)
1040{
1041 static int reported;
1042 int r;
1043
1044 if (!reported) {
1045 reported = 1;
1046 printk(KERN_WARNING "kvm: emulating exchange8b as write\n");
1047 }
1048 r = emulator_write_emulated(addr, new_lo, 4, ctxt);
1049 if (r != X86EMUL_CONTINUE)
1050 return r;
1051 return emulator_write_emulated(addr+4, new_hi, 4, ctxt);
1052}
1053
1054#endif
1055
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1057{
1058 return kvm_arch_ops->get_segment_base(vcpu, seg);
1059}
1060
1061int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1062{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 return X86EMUL_CONTINUE;
1064}
1065
1066int emulate_clts(struct kvm_vcpu *vcpu)
1067{
Avi Kivity399badf2007-01-05 16:36:38 -08001068 unsigned long cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069
Avi Kivity399badf2007-01-05 16:36:38 -08001070 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1071 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072 kvm_arch_ops->set_cr0(vcpu, cr0);
1073 return X86EMUL_CONTINUE;
1074}
1075
1076int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1077{
1078 struct kvm_vcpu *vcpu = ctxt->vcpu;
1079
1080 switch (dr) {
1081 case 0 ... 3:
1082 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1083 return X86EMUL_CONTINUE;
1084 default:
1085 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1086 __FUNCTION__, dr);
1087 return X86EMUL_UNHANDLEABLE;
1088 }
1089}
1090
1091int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1092{
1093 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1094 int exception;
1095
1096 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1097 if (exception) {
1098 /* FIXME: better handling */
1099 return X86EMUL_UNHANDLEABLE;
1100 }
1101 return X86EMUL_CONTINUE;
1102}
1103
1104static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1105{
1106 static int reported;
1107 u8 opcodes[4];
1108 unsigned long rip = ctxt->vcpu->rip;
1109 unsigned long rip_linear;
1110
1111 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1112
1113 if (reported)
1114 return;
1115
1116 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1117
1118 printk(KERN_ERR "emulation failed but !mmio_needed?"
1119 " rip %lx %02x %02x %02x %02x\n",
1120 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1121 reported = 1;
1122}
1123
1124struct x86_emulate_ops emulate_ops = {
1125 .read_std = emulator_read_std,
1126 .write_std = emulator_write_std,
1127 .read_emulated = emulator_read_emulated,
1128 .write_emulated = emulator_write_emulated,
1129 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity32b35622007-01-05 16:36:51 -08001130#ifdef CONFIG_X86_32
1131 .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated,
1132#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133};
1134
1135int emulate_instruction(struct kvm_vcpu *vcpu,
1136 struct kvm_run *run,
1137 unsigned long cr2,
1138 u16 error_code)
1139{
1140 struct x86_emulate_ctxt emulate_ctxt;
1141 int r;
1142 int cs_db, cs_l;
1143
1144 kvm_arch_ops->cache_regs(vcpu);
1145
1146 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1147
1148 emulate_ctxt.vcpu = vcpu;
1149 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1150 emulate_ctxt.cr2 = cr2;
1151 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1152 ? X86EMUL_MODE_REAL : cs_l
1153 ? X86EMUL_MODE_PROT64 : cs_db
1154 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1155
1156 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1157 emulate_ctxt.cs_base = 0;
1158 emulate_ctxt.ds_base = 0;
1159 emulate_ctxt.es_base = 0;
1160 emulate_ctxt.ss_base = 0;
1161 } else {
1162 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1163 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1164 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1165 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1166 }
1167
1168 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1169 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1170
1171 vcpu->mmio_is_write = 0;
1172 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1173
1174 if ((r || vcpu->mmio_is_write) && run) {
1175 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1176 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1177 run->mmio.len = vcpu->mmio_size;
1178 run->mmio.is_write = vcpu->mmio_is_write;
1179 }
1180
1181 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001182 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1183 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184 if (!vcpu->mmio_needed) {
1185 report_emulation_failure(&emulate_ctxt);
1186 return EMULATE_FAIL;
1187 }
1188 return EMULATE_DO_MMIO;
1189 }
1190
1191 kvm_arch_ops->decache_regs(vcpu);
1192 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1193
1194 if (vcpu->mmio_is_write)
1195 return EMULATE_DO_MMIO;
1196
1197 return EMULATE_DONE;
1198}
1199EXPORT_SYMBOL_GPL(emulate_instruction);
1200
Avi Kivity270fd9b2007-02-19 14:37:47 +02001201int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1202{
1203 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1204
Dor Laor9b22bf52007-02-19 16:44:49 +02001205 kvm_arch_ops->cache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001206 ret = -KVM_EINVAL;
1207#ifdef CONFIG_X86_64
1208 if (is_long_mode(vcpu)) {
1209 nr = vcpu->regs[VCPU_REGS_RAX];
1210 a0 = vcpu->regs[VCPU_REGS_RDI];
1211 a1 = vcpu->regs[VCPU_REGS_RSI];
1212 a2 = vcpu->regs[VCPU_REGS_RDX];
1213 a3 = vcpu->regs[VCPU_REGS_RCX];
1214 a4 = vcpu->regs[VCPU_REGS_R8];
1215 a5 = vcpu->regs[VCPU_REGS_R9];
1216 } else
1217#endif
1218 {
1219 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1220 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1221 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1222 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1223 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1224 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1225 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1226 }
1227 switch (nr) {
1228 default:
Avi Kivityb4e63f52007-03-04 13:59:30 +02001229 run->hypercall.args[0] = a0;
1230 run->hypercall.args[1] = a1;
1231 run->hypercall.args[2] = a2;
1232 run->hypercall.args[3] = a3;
1233 run->hypercall.args[4] = a4;
1234 run->hypercall.args[5] = a5;
1235 run->hypercall.ret = ret;
1236 run->hypercall.longmode = is_long_mode(vcpu);
1237 kvm_arch_ops->decache_regs(vcpu);
1238 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001239 }
1240 vcpu->regs[VCPU_REGS_RAX] = ret;
Dor Laor9b22bf52007-02-19 16:44:49 +02001241 kvm_arch_ops->decache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001242 return 1;
1243}
1244EXPORT_SYMBOL_GPL(kvm_hypercall);
1245
Avi Kivity6aa8b732006-12-10 02:21:36 -08001246static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1247{
1248 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1249}
1250
1251void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1252{
1253 struct descriptor_table dt = { limit, base };
1254
1255 kvm_arch_ops->set_gdt(vcpu, &dt);
1256}
1257
1258void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1259{
1260 struct descriptor_table dt = { limit, base };
1261
1262 kvm_arch_ops->set_idt(vcpu, &dt);
1263}
1264
1265void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1266 unsigned long *rflags)
1267{
1268 lmsw(vcpu, msw);
1269 *rflags = kvm_arch_ops->get_rflags(vcpu);
1270}
1271
1272unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1273{
Avi Kivity399badf2007-01-05 16:36:38 -08001274 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275 switch (cr) {
1276 case 0:
1277 return vcpu->cr0;
1278 case 2:
1279 return vcpu->cr2;
1280 case 3:
1281 return vcpu->cr3;
1282 case 4:
1283 return vcpu->cr4;
1284 default:
1285 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1286 return 0;
1287 }
1288}
1289
1290void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1291 unsigned long *rflags)
1292{
1293 switch (cr) {
1294 case 0:
1295 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1296 *rflags = kvm_arch_ops->get_rflags(vcpu);
1297 break;
1298 case 2:
1299 vcpu->cr2 = val;
1300 break;
1301 case 3:
1302 set_cr3(vcpu, val);
1303 break;
1304 case 4:
1305 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1306 break;
1307 default:
1308 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1309 }
1310}
1311
Ingo Molnar102d8322007-02-19 14:37:47 +02001312/*
1313 * Register the para guest with the host:
1314 */
1315static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1316{
1317 struct kvm_vcpu_para_state *para_state;
1318 hpa_t para_state_hpa, hypercall_hpa;
1319 struct page *para_state_page;
1320 unsigned char *hypercall;
1321 gpa_t hypercall_gpa;
1322
1323 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1324 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1325
1326 /*
1327 * Needs to be page aligned:
1328 */
1329 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1330 goto err_gp;
1331
1332 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1333 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1334 if (is_error_hpa(para_state_hpa))
1335 goto err_gp;
1336
Uri Lublinab51a432007-02-21 18:25:21 +02001337 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001338 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1339 para_state = kmap_atomic(para_state_page, KM_USER0);
1340
1341 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1342 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1343
1344 para_state->host_version = KVM_PARA_API_VERSION;
1345 /*
1346 * We cannot support guests that try to register themselves
1347 * with a newer API version than the host supports:
1348 */
1349 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1350 para_state->ret = -KVM_EINVAL;
1351 goto err_kunmap_skip;
1352 }
1353
1354 hypercall_gpa = para_state->hypercall_gpa;
1355 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1356 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1357 if (is_error_hpa(hypercall_hpa)) {
1358 para_state->ret = -KVM_EINVAL;
1359 goto err_kunmap_skip;
1360 }
1361
1362 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1363 vcpu->para_state_page = para_state_page;
1364 vcpu->para_state_gpa = para_state_gpa;
1365 vcpu->hypercall_gpa = hypercall_gpa;
1366
Uri Lublinab51a432007-02-21 18:25:21 +02001367 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001368 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1369 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1370 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1371 kunmap_atomic(hypercall, KM_USER1);
1372
1373 para_state->ret = 0;
1374err_kunmap_skip:
1375 kunmap_atomic(para_state, KM_USER0);
1376 return 0;
1377err_gp:
1378 return 1;
1379}
1380
Avi Kivity3bab1f52006-12-29 16:49:48 -08001381int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1382{
1383 u64 data;
1384
1385 switch (msr) {
1386 case 0xc0010010: /* SYSCFG */
1387 case 0xc0010015: /* HWCR */
1388 case MSR_IA32_PLATFORM_ID:
1389 case MSR_IA32_P5_MC_ADDR:
1390 case MSR_IA32_P5_MC_TYPE:
1391 case MSR_IA32_MC0_CTL:
1392 case MSR_IA32_MCG_STATUS:
1393 case MSR_IA32_MCG_CAP:
1394 case MSR_IA32_MC0_MISC:
1395 case MSR_IA32_MC0_MISC+4:
1396 case MSR_IA32_MC0_MISC+8:
1397 case MSR_IA32_MC0_MISC+12:
1398 case MSR_IA32_MC0_MISC+16:
1399 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001400 case MSR_IA32_PERF_STATUS:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001401 /* MTRR registers */
1402 case 0xfe:
1403 case 0x200 ... 0x2ff:
1404 data = 0;
1405 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001406 case 0xcd: /* fsb frequency */
1407 data = 3;
1408 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001409 case MSR_IA32_APICBASE:
1410 data = vcpu->apic_base;
1411 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001412 case MSR_IA32_MISC_ENABLE:
1413 data = vcpu->ia32_misc_enable_msr;
1414 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001415#ifdef CONFIG_X86_64
1416 case MSR_EFER:
1417 data = vcpu->shadow_efer;
1418 break;
1419#endif
1420 default:
1421 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1422 return 1;
1423 }
1424 *pdata = data;
1425 return 0;
1426}
1427EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1428
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429/*
1430 * Reads an msr value (of 'msr_index') into 'pdata'.
1431 * Returns 0 on success, non-0 otherwise.
1432 * Assumes vcpu_load() was already called.
1433 */
1434static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1435{
1436 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1437}
1438
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001439#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440
Avi Kivity3bab1f52006-12-29 16:49:48 -08001441static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443 if (efer & EFER_RESERVED_BITS) {
1444 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1445 efer);
1446 inject_gp(vcpu);
1447 return;
1448 }
1449
1450 if (is_paging(vcpu)
1451 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1452 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1453 inject_gp(vcpu);
1454 return;
1455 }
1456
Avi Kivity7725f0b2006-12-13 00:34:01 -08001457 kvm_arch_ops->set_efer(vcpu, efer);
1458
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459 efer &= ~EFER_LMA;
1460 efer |= vcpu->shadow_efer & EFER_LMA;
1461
1462 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464
1465#endif
1466
Avi Kivity3bab1f52006-12-29 16:49:48 -08001467int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1468{
1469 switch (msr) {
1470#ifdef CONFIG_X86_64
1471 case MSR_EFER:
1472 set_efer(vcpu, data);
1473 break;
1474#endif
1475 case MSR_IA32_MC0_STATUS:
1476 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1477 __FUNCTION__, data);
1478 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001479 case MSR_IA32_MCG_STATUS:
1480 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1481 __FUNCTION__, data);
1482 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001483 case MSR_IA32_UCODE_REV:
1484 case MSR_IA32_UCODE_WRITE:
1485 case 0x200 ... 0x2ff: /* MTRRs */
1486 break;
1487 case MSR_IA32_APICBASE:
1488 vcpu->apic_base = data;
1489 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001490 case MSR_IA32_MISC_ENABLE:
1491 vcpu->ia32_misc_enable_msr = data;
1492 break;
Ingo Molnar102d8322007-02-19 14:37:47 +02001493 /*
1494 * This is the 'probe whether the host is KVM' logic:
1495 */
1496 case MSR_KVM_API_MAGIC:
1497 return vcpu_register_para(vcpu, data);
1498
Avi Kivity3bab1f52006-12-29 16:49:48 -08001499 default:
1500 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1501 return 1;
1502 }
1503 return 0;
1504}
1505EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1506
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507/*
1508 * Writes msr value into into the appropriate "register".
1509 * Returns 0 on success, non-0 otherwise.
1510 * Assumes vcpu_load() was already called.
1511 */
1512static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1513{
1514 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1515}
1516
1517void kvm_resched(struct kvm_vcpu *vcpu)
1518{
1519 vcpu_put(vcpu);
1520 cond_resched();
Avi Kivitybccf2152007-02-21 18:04:26 +02001521 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522}
1523EXPORT_SYMBOL_GPL(kvm_resched);
1524
1525void load_msrs(struct vmx_msr_entry *e, int n)
1526{
1527 int i;
1528
1529 for (i = 0; i < n; ++i)
1530 wrmsrl(e[i].index, e[i].data);
1531}
1532EXPORT_SYMBOL_GPL(load_msrs);
1533
1534void save_msrs(struct vmx_msr_entry *e, int n)
1535{
1536 int i;
1537
1538 for (i = 0; i < n; ++i)
1539 rdmsrl(e[i].index, e[i].data);
1540}
1541EXPORT_SYMBOL_GPL(save_msrs);
1542
Avi Kivity06465c52007-02-28 20:46:53 +02001543void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1544{
1545 int i;
1546 u32 function;
1547 struct kvm_cpuid_entry *e, *best;
1548
1549 kvm_arch_ops->cache_regs(vcpu);
1550 function = vcpu->regs[VCPU_REGS_RAX];
1551 vcpu->regs[VCPU_REGS_RAX] = 0;
1552 vcpu->regs[VCPU_REGS_RBX] = 0;
1553 vcpu->regs[VCPU_REGS_RCX] = 0;
1554 vcpu->regs[VCPU_REGS_RDX] = 0;
1555 best = NULL;
1556 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1557 e = &vcpu->cpuid_entries[i];
1558 if (e->function == function) {
1559 best = e;
1560 break;
1561 }
1562 /*
1563 * Both basic or both extended?
1564 */
1565 if (((e->function ^ function) & 0x80000000) == 0)
1566 if (!best || e->function > best->function)
1567 best = e;
1568 }
1569 if (best) {
1570 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1571 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1572 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1573 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1574 }
1575 kvm_arch_ops->decache_regs(vcpu);
1576 kvm_arch_ops->skip_emulated_instruction(vcpu);
1577}
1578EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1579
Avi Kivity039576c2007-03-20 12:46:50 +02001580static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001581{
Avi Kivity039576c2007-03-20 12:46:50 +02001582 void *p = vcpu->pio_data;
1583 void *q;
1584 unsigned bytes;
1585 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1586
1587 kvm_arch_ops->vcpu_put(vcpu);
1588 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1589 PAGE_KERNEL);
1590 if (!q) {
1591 kvm_arch_ops->vcpu_load(vcpu);
1592 free_pio_guest_pages(vcpu);
1593 return -ENOMEM;
1594 }
1595 q += vcpu->pio.guest_page_offset;
1596 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1597 if (vcpu->pio.in)
1598 memcpy(q, p, bytes);
1599 else
1600 memcpy(p, q, bytes);
1601 q -= vcpu->pio.guest_page_offset;
1602 vunmap(q);
1603 kvm_arch_ops->vcpu_load(vcpu);
1604 free_pio_guest_pages(vcpu);
1605 return 0;
1606}
1607
1608static int complete_pio(struct kvm_vcpu *vcpu)
1609{
1610 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001611 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001612 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001613
1614 kvm_arch_ops->cache_regs(vcpu);
1615
1616 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001617 if (io->in)
1618 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001619 io->size);
1620 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001621 if (io->in) {
1622 r = pio_copy_data(vcpu);
1623 if (r) {
1624 kvm_arch_ops->cache_regs(vcpu);
1625 return r;
1626 }
1627 }
1628
Avi Kivity46fc1472007-02-22 19:39:30 +02001629 delta = 1;
1630 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001631 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001632 /*
1633 * The size of the register should really depend on
1634 * current address size.
1635 */
1636 vcpu->regs[VCPU_REGS_RCX] -= delta;
1637 }
Avi Kivity039576c2007-03-20 12:46:50 +02001638 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001639 delta = -delta;
1640 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001641 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001642 vcpu->regs[VCPU_REGS_RDI] += delta;
1643 else
1644 vcpu->regs[VCPU_REGS_RSI] += delta;
1645 }
1646
Avi Kivity46fc1472007-02-22 19:39:30 +02001647 vcpu->run->io_completed = 0;
1648
1649 kvm_arch_ops->decache_regs(vcpu);
1650
Avi Kivity039576c2007-03-20 12:46:50 +02001651 io->count -= io->cur_count;
1652 io->cur_count = 0;
1653
1654 if (!io->count)
1655 kvm_arch_ops->skip_emulated_instruction(vcpu);
1656 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001657}
1658
Avi Kivity039576c2007-03-20 12:46:50 +02001659int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1660 int size, unsigned long count, int string, int down,
1661 gva_t address, int rep, unsigned port)
1662{
1663 unsigned now, in_page;
1664 int i;
1665 int nr_pages = 1;
1666 struct page *page;
1667
1668 vcpu->run->exit_reason = KVM_EXIT_IO;
1669 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1670 vcpu->run->io.size = size;
1671 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1672 vcpu->run->io.count = count;
1673 vcpu->run->io.port = port;
1674 vcpu->pio.count = count;
1675 vcpu->pio.cur_count = count;
1676 vcpu->pio.size = size;
1677 vcpu->pio.in = in;
1678 vcpu->pio.string = string;
1679 vcpu->pio.down = down;
1680 vcpu->pio.guest_page_offset = offset_in_page(address);
1681 vcpu->pio.rep = rep;
1682
1683 if (!string) {
1684 kvm_arch_ops->cache_regs(vcpu);
1685 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1686 kvm_arch_ops->decache_regs(vcpu);
1687 return 0;
1688 }
1689
1690 if (!count) {
1691 kvm_arch_ops->skip_emulated_instruction(vcpu);
1692 return 1;
1693 }
1694
1695 now = min(count, PAGE_SIZE / size);
1696
1697 if (!down)
1698 in_page = PAGE_SIZE - offset_in_page(address);
1699 else
1700 in_page = offset_in_page(address) + size;
1701 now = min(count, (unsigned long)in_page / size);
1702 if (!now) {
1703 /*
1704 * String I/O straddles page boundary. Pin two guest pages
1705 * so that we satisfy atomicity constraints. Do just one
1706 * transaction to avoid complexity.
1707 */
1708 nr_pages = 2;
1709 now = 1;
1710 }
1711 if (down) {
1712 /*
1713 * String I/O in reverse. Yuck. Kill the guest, fix later.
1714 */
1715 printk(KERN_ERR "kvm: guest string pio down\n");
1716 inject_gp(vcpu);
1717 return 1;
1718 }
1719 vcpu->run->io.count = now;
1720 vcpu->pio.cur_count = now;
1721
1722 for (i = 0; i < nr_pages; ++i) {
1723 spin_lock(&vcpu->kvm->lock);
1724 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1725 if (page)
1726 get_page(page);
1727 vcpu->pio.guest_pages[i] = page;
1728 spin_unlock(&vcpu->kvm->lock);
1729 if (!page) {
1730 inject_gp(vcpu);
1731 free_pio_guest_pages(vcpu);
1732 return 1;
1733 }
1734 }
1735
1736 if (!vcpu->pio.in)
1737 return pio_copy_data(vcpu);
1738 return 0;
1739}
1740EXPORT_SYMBOL_GPL(kvm_setup_pio);
1741
Avi Kivitybccf2152007-02-21 18:04:26 +02001742static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02001745 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746
Avi Kivitybccf2152007-02-21 18:04:26 +02001747 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748
Avi Kivity1961d272007-03-05 19:46:05 +02001749 if (vcpu->sigset_active)
1750 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1751
Dor Laor54810342007-02-12 00:54:39 -08001752 /* re-sync apic's tpr */
1753 vcpu->cr8 = kvm_run->cr8;
1754
Avi Kivity46fc1472007-02-22 19:39:30 +02001755 if (kvm_run->io_completed) {
Avi Kivity039576c2007-03-20 12:46:50 +02001756 if (vcpu->pio.cur_count) {
1757 r = complete_pio(vcpu);
1758 if (r)
1759 goto out;
1760 } else {
Avi Kivity46fc1472007-02-22 19:39:30 +02001761 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1762 vcpu->mmio_read_completed = 1;
1763 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001764 }
1765
1766 vcpu->mmio_needed = 0;
1767
Avi Kivity8eb7d332007-03-04 14:17:08 +02001768 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Avi Kivityb4e63f52007-03-04 13:59:30 +02001769 kvm_arch_ops->cache_regs(vcpu);
1770 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1771 kvm_arch_ops->decache_regs(vcpu);
1772 }
1773
Avi Kivity6aa8b732006-12-10 02:21:36 -08001774 r = kvm_arch_ops->run(vcpu, kvm_run);
1775
Avi Kivity039576c2007-03-20 12:46:50 +02001776out:
Avi Kivity1961d272007-03-05 19:46:05 +02001777 if (vcpu->sigset_active)
1778 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1779
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780 vcpu_put(vcpu);
1781 return r;
1782}
1783
Avi Kivitybccf2152007-02-21 18:04:26 +02001784static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1785 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786{
Avi Kivitybccf2152007-02-21 18:04:26 +02001787 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788
1789 kvm_arch_ops->cache_regs(vcpu);
1790
1791 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1792 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1793 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1794 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1795 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1796 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1797 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1798 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001799#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1801 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1802 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1803 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1804 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1805 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1806 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1807 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1808#endif
1809
1810 regs->rip = vcpu->rip;
1811 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1812
1813 /*
1814 * Don't leak debug flags in case they were set for guest debugging
1815 */
1816 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1817 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1818
1819 vcpu_put(vcpu);
1820
1821 return 0;
1822}
1823
Avi Kivitybccf2152007-02-21 18:04:26 +02001824static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1825 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826{
Avi Kivitybccf2152007-02-21 18:04:26 +02001827 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001828
1829 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1830 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1831 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1832 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1833 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1834 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1835 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1836 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001837#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1839 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1840 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1841 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1842 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1843 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1844 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1845 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1846#endif
1847
1848 vcpu->rip = regs->rip;
1849 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1850
1851 kvm_arch_ops->decache_regs(vcpu);
1852
1853 vcpu_put(vcpu);
1854
1855 return 0;
1856}
1857
1858static void get_segment(struct kvm_vcpu *vcpu,
1859 struct kvm_segment *var, int seg)
1860{
1861 return kvm_arch_ops->get_segment(vcpu, var, seg);
1862}
1863
Avi Kivitybccf2152007-02-21 18:04:26 +02001864static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1865 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867 struct descriptor_table dt;
1868
Avi Kivitybccf2152007-02-21 18:04:26 +02001869 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870
1871 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1872 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1873 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1874 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1875 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1876 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1877
1878 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1879 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1880
1881 kvm_arch_ops->get_idt(vcpu, &dt);
1882 sregs->idt.limit = dt.limit;
1883 sregs->idt.base = dt.base;
1884 kvm_arch_ops->get_gdt(vcpu, &dt);
1885 sregs->gdt.limit = dt.limit;
1886 sregs->gdt.base = dt.base;
1887
Avi Kivity399badf2007-01-05 16:36:38 -08001888 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889 sregs->cr0 = vcpu->cr0;
1890 sregs->cr2 = vcpu->cr2;
1891 sregs->cr3 = vcpu->cr3;
1892 sregs->cr4 = vcpu->cr4;
1893 sregs->cr8 = vcpu->cr8;
1894 sregs->efer = vcpu->shadow_efer;
1895 sregs->apic_base = vcpu->apic_base;
1896
1897 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1898 sizeof sregs->interrupt_bitmap);
1899
1900 vcpu_put(vcpu);
1901
1902 return 0;
1903}
1904
1905static void set_segment(struct kvm_vcpu *vcpu,
1906 struct kvm_segment *var, int seg)
1907{
1908 return kvm_arch_ops->set_segment(vcpu, var, seg);
1909}
1910
Avi Kivitybccf2152007-02-21 18:04:26 +02001911static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1912 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001914 int mmu_reset_needed = 0;
1915 int i;
1916 struct descriptor_table dt;
1917
Avi Kivitybccf2152007-02-21 18:04:26 +02001918 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920 dt.limit = sregs->idt.limit;
1921 dt.base = sregs->idt.base;
1922 kvm_arch_ops->set_idt(vcpu, &dt);
1923 dt.limit = sregs->gdt.limit;
1924 dt.base = sregs->gdt.base;
1925 kvm_arch_ops->set_gdt(vcpu, &dt);
1926
1927 vcpu->cr2 = sregs->cr2;
1928 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
1929 vcpu->cr3 = sregs->cr3;
1930
1931 vcpu->cr8 = sregs->cr8;
1932
1933 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001934#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935 kvm_arch_ops->set_efer(vcpu, sregs->efer);
1936#endif
1937 vcpu->apic_base = sregs->apic_base;
1938
Avi Kivity399badf2007-01-05 16:36:38 -08001939 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1940
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Avi Kivityf6528b02007-03-20 18:44:51 +02001942 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943
1944 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
1945 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08001946 if (!is_long_mode(vcpu) && is_pae(vcpu))
1947 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948
1949 if (mmu_reset_needed)
1950 kvm_mmu_reset_context(vcpu);
1951
1952 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
1953 sizeof vcpu->irq_pending);
1954 vcpu->irq_summary = 0;
1955 for (i = 0; i < NR_IRQ_WORDS; ++i)
1956 if (vcpu->irq_pending[i])
1957 __set_bit(i, &vcpu->irq_summary);
1958
Avi Kivity024aa1c2007-03-21 13:44:58 +02001959 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1960 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1961 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1962 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1963 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1964 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1965
1966 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1967 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1968
Avi Kivity6aa8b732006-12-10 02:21:36 -08001969 vcpu_put(vcpu);
1970
1971 return 0;
1972}
1973
1974/*
1975 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1976 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08001977 *
1978 * This list is modified at module load time to reflect the
1979 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001980 */
1981static u32 msrs_to_save[] = {
1982 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
1983 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001984#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1986#endif
1987 MSR_IA32_TIME_STAMP_COUNTER,
1988};
1989
Michael Riepebf591b22006-12-22 01:05:36 -08001990static unsigned num_msrs_to_save;
1991
Avi Kivity6f00e682007-01-26 00:56:40 -08001992static u32 emulated_msrs[] = {
1993 MSR_IA32_MISC_ENABLE,
1994};
1995
Michael Riepebf591b22006-12-22 01:05:36 -08001996static __init void kvm_init_msr_list(void)
1997{
1998 u32 dummy[2];
1999 unsigned i, j;
2000
2001 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2002 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2003 continue;
2004 if (j < i)
2005 msrs_to_save[j] = msrs_to_save[i];
2006 j++;
2007 }
2008 num_msrs_to_save = j;
2009}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010
2011/*
2012 * Adapt set_msr() to msr_io()'s calling convention
2013 */
2014static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2015{
2016 return set_msr(vcpu, index, *data);
2017}
2018
2019/*
2020 * Read or write a bunch of msrs. All parameters are kernel addresses.
2021 *
2022 * @return number of msrs set successfully.
2023 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002024static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025 struct kvm_msr_entry *entries,
2026 int (*do_msr)(struct kvm_vcpu *vcpu,
2027 unsigned index, u64 *data))
2028{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002029 int i;
2030
Avi Kivitybccf2152007-02-21 18:04:26 +02002031 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032
2033 for (i = 0; i < msrs->nmsrs; ++i)
2034 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2035 break;
2036
2037 vcpu_put(vcpu);
2038
2039 return i;
2040}
2041
2042/*
2043 * Read or write a bunch of msrs. Parameters are user addresses.
2044 *
2045 * @return number of msrs set successfully.
2046 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002047static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048 int (*do_msr)(struct kvm_vcpu *vcpu,
2049 unsigned index, u64 *data),
2050 int writeback)
2051{
2052 struct kvm_msrs msrs;
2053 struct kvm_msr_entry *entries;
2054 int r, n;
2055 unsigned size;
2056
2057 r = -EFAULT;
2058 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2059 goto out;
2060
2061 r = -E2BIG;
2062 if (msrs.nmsrs >= MAX_IO_MSRS)
2063 goto out;
2064
2065 r = -ENOMEM;
2066 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2067 entries = vmalloc(size);
2068 if (!entries)
2069 goto out;
2070
2071 r = -EFAULT;
2072 if (copy_from_user(entries, user_msrs->entries, size))
2073 goto out_free;
2074
Avi Kivitybccf2152007-02-21 18:04:26 +02002075 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076 if (r < 0)
2077 goto out_free;
2078
2079 r = -EFAULT;
2080 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2081 goto out_free;
2082
2083 r = n;
2084
2085out_free:
2086 vfree(entries);
2087out:
2088 return r;
2089}
2090
2091/*
2092 * Translate a guest virtual address to a guest physical address.
2093 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002094static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2095 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096{
2097 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098 gpa_t gpa;
2099
Avi Kivitybccf2152007-02-21 18:04:26 +02002100 vcpu_load(vcpu);
2101 spin_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2103 tr->physical_address = gpa;
2104 tr->valid = gpa != UNMAPPED_GVA;
2105 tr->writeable = 1;
2106 tr->usermode = 0;
Avi Kivitybccf2152007-02-21 18:04:26 +02002107 spin_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108 vcpu_put(vcpu);
2109
2110 return 0;
2111}
2112
Avi Kivitybccf2152007-02-21 18:04:26 +02002113static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2114 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002116 if (irq->irq < 0 || irq->irq >= 256)
2117 return -EINVAL;
Avi Kivitybccf2152007-02-21 18:04:26 +02002118 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119
2120 set_bit(irq->irq, vcpu->irq_pending);
2121 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2122
2123 vcpu_put(vcpu);
2124
2125 return 0;
2126}
2127
Avi Kivitybccf2152007-02-21 18:04:26 +02002128static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2129 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 int r;
2132
Avi Kivitybccf2152007-02-21 18:04:26 +02002133 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002134
2135 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2136
2137 vcpu_put(vcpu);
2138
2139 return r;
2140}
2141
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002142static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2143 unsigned long address,
2144 int *type)
2145{
2146 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2147 unsigned long pgoff;
2148 struct page *page;
2149
2150 *type = VM_FAULT_MINOR;
2151 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002152 if (pgoff == 0)
2153 page = virt_to_page(vcpu->run);
2154 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2155 page = virt_to_page(vcpu->pio_data);
2156 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002157 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002158 get_page(page);
2159 return page;
2160}
2161
2162static struct vm_operations_struct kvm_vcpu_vm_ops = {
2163 .nopage = kvm_vcpu_nopage,
2164};
2165
2166static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2167{
2168 vma->vm_ops = &kvm_vcpu_vm_ops;
2169 return 0;
2170}
2171
Avi Kivitybccf2152007-02-21 18:04:26 +02002172static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2173{
2174 struct kvm_vcpu *vcpu = filp->private_data;
2175
2176 fput(vcpu->kvm->filp);
2177 return 0;
2178}
2179
2180static struct file_operations kvm_vcpu_fops = {
2181 .release = kvm_vcpu_release,
2182 .unlocked_ioctl = kvm_vcpu_ioctl,
2183 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002184 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002185};
2186
2187/*
2188 * Allocates an inode for the vcpu.
2189 */
2190static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2191{
2192 int fd, r;
2193 struct inode *inode;
2194 struct file *file;
2195
2196 atomic_inc(&vcpu->kvm->filp->f_count);
2197 inode = kvmfs_inode(&kvm_vcpu_fops);
2198 if (IS_ERR(inode)) {
2199 r = PTR_ERR(inode);
2200 goto out1;
2201 }
2202
2203 file = kvmfs_file(inode, vcpu);
2204 if (IS_ERR(file)) {
2205 r = PTR_ERR(file);
2206 goto out2;
2207 }
2208
2209 r = get_unused_fd();
2210 if (r < 0)
2211 goto out3;
2212 fd = r;
2213 fd_install(fd, file);
2214
2215 return fd;
2216
2217out3:
2218 fput(file);
2219out2:
2220 iput(inode);
2221out1:
2222 fput(vcpu->kvm->filp);
2223 return r;
2224}
2225
Avi Kivityc5ea7662007-02-20 18:41:05 +02002226/*
2227 * Creates some virtual cpus. Good luck creating more than one.
2228 */
2229static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2230{
2231 int r;
2232 struct kvm_vcpu *vcpu;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002233 struct page *page;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002234
2235 r = -EINVAL;
2236 if (!valid_vcpu(n))
2237 goto out;
2238
2239 vcpu = &kvm->vcpus[n];
2240
2241 mutex_lock(&vcpu->mutex);
2242
2243 if (vcpu->vmcs) {
2244 mutex_unlock(&vcpu->mutex);
2245 return -EEXIST;
2246 }
2247
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002248 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2249 r = -ENOMEM;
2250 if (!page)
2251 goto out_unlock;
2252 vcpu->run = page_address(page);
2253
Avi Kivity039576c2007-03-20 12:46:50 +02002254 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2255 r = -ENOMEM;
2256 if (!page)
2257 goto out_free_run;
2258 vcpu->pio_data = page_address(page);
2259
Avi Kivityc5ea7662007-02-20 18:41:05 +02002260 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2261 FX_IMAGE_ALIGN);
2262 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
2263
2264 r = kvm_arch_ops->vcpu_create(vcpu);
2265 if (r < 0)
2266 goto out_free_vcpus;
2267
2268 r = kvm_mmu_create(vcpu);
2269 if (r < 0)
2270 goto out_free_vcpus;
2271
2272 kvm_arch_ops->vcpu_load(vcpu);
2273 r = kvm_mmu_setup(vcpu);
2274 if (r >= 0)
2275 r = kvm_arch_ops->vcpu_setup(vcpu);
2276 vcpu_put(vcpu);
2277
2278 if (r < 0)
2279 goto out_free_vcpus;
2280
Avi Kivitybccf2152007-02-21 18:04:26 +02002281 r = create_vcpu_fd(vcpu);
2282 if (r < 0)
2283 goto out_free_vcpus;
2284
2285 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002286
2287out_free_vcpus:
2288 kvm_free_vcpu(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002289out_free_run:
2290 free_page((unsigned long)vcpu->run);
2291 vcpu->run = NULL;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002292out_unlock:
Avi Kivityc5ea7662007-02-20 18:41:05 +02002293 mutex_unlock(&vcpu->mutex);
2294out:
2295 return r;
2296}
2297
Avi Kivity06465c52007-02-28 20:46:53 +02002298static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2299 struct kvm_cpuid *cpuid,
2300 struct kvm_cpuid_entry __user *entries)
2301{
2302 int r;
2303
2304 r = -E2BIG;
2305 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2306 goto out;
2307 r = -EFAULT;
2308 if (copy_from_user(&vcpu->cpuid_entries, entries,
2309 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2310 goto out;
2311 vcpu->cpuid_nent = cpuid->nent;
2312 return 0;
2313
2314out:
2315 return r;
2316}
2317
Avi Kivity1961d272007-03-05 19:46:05 +02002318static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2319{
2320 if (sigset) {
2321 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2322 vcpu->sigset_active = 1;
2323 vcpu->sigset = *sigset;
2324 } else
2325 vcpu->sigset_active = 0;
2326 return 0;
2327}
2328
Avi Kivitybccf2152007-02-21 18:04:26 +02002329static long kvm_vcpu_ioctl(struct file *filp,
2330 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331{
Avi Kivitybccf2152007-02-21 18:04:26 +02002332 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f366982007-02-09 16:38:35 +00002333 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334 int r = -EINVAL;
2335
2336 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002337 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002338 r = -EINVAL;
2339 if (arg)
2340 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002341 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343 case KVM_GET_REGS: {
2344 struct kvm_regs kvm_regs;
2345
Avi Kivitybccf2152007-02-21 18:04:26 +02002346 memset(&kvm_regs, 0, sizeof kvm_regs);
2347 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002348 if (r)
2349 goto out;
2350 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002351 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352 goto out;
2353 r = 0;
2354 break;
2355 }
2356 case KVM_SET_REGS: {
2357 struct kvm_regs kvm_regs;
2358
2359 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002360 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002362 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363 if (r)
2364 goto out;
2365 r = 0;
2366 break;
2367 }
2368 case KVM_GET_SREGS: {
2369 struct kvm_sregs kvm_sregs;
2370
Avi Kivitybccf2152007-02-21 18:04:26 +02002371 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2372 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373 if (r)
2374 goto out;
2375 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002376 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377 goto out;
2378 r = 0;
2379 break;
2380 }
2381 case KVM_SET_SREGS: {
2382 struct kvm_sregs kvm_sregs;
2383
2384 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002385 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002387 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388 if (r)
2389 goto out;
2390 r = 0;
2391 break;
2392 }
2393 case KVM_TRANSLATE: {
2394 struct kvm_translation tr;
2395
2396 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002397 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002399 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002400 if (r)
2401 goto out;
2402 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002403 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404 goto out;
2405 r = 0;
2406 break;
2407 }
2408 case KVM_INTERRUPT: {
2409 struct kvm_interrupt irq;
2410
2411 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002412 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002414 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415 if (r)
2416 goto out;
2417 r = 0;
2418 break;
2419 }
2420 case KVM_DEBUG_GUEST: {
2421 struct kvm_debug_guest dbg;
2422
2423 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002424 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002426 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427 if (r)
2428 goto out;
2429 r = 0;
2430 break;
2431 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002432 case KVM_GET_MSRS:
2433 r = msr_io(vcpu, argp, get_msr, 1);
2434 break;
2435 case KVM_SET_MSRS:
2436 r = msr_io(vcpu, argp, do_set_msr, 0);
2437 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002438 case KVM_SET_CPUID: {
2439 struct kvm_cpuid __user *cpuid_arg = argp;
2440 struct kvm_cpuid cpuid;
2441
2442 r = -EFAULT;
2443 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2444 goto out;
2445 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2446 if (r)
2447 goto out;
2448 break;
2449 }
Avi Kivity1961d272007-03-05 19:46:05 +02002450 case KVM_SET_SIGNAL_MASK: {
2451 struct kvm_signal_mask __user *sigmask_arg = argp;
2452 struct kvm_signal_mask kvm_sigmask;
2453 sigset_t sigset, *p;
2454
2455 p = NULL;
2456 if (argp) {
2457 r = -EFAULT;
2458 if (copy_from_user(&kvm_sigmask, argp,
2459 sizeof kvm_sigmask))
2460 goto out;
2461 r = -EINVAL;
2462 if (kvm_sigmask.len != sizeof sigset)
2463 goto out;
2464 r = -EFAULT;
2465 if (copy_from_user(&sigset, sigmask_arg->sigset,
2466 sizeof sigset))
2467 goto out;
2468 p = &sigset;
2469 }
2470 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2471 break;
2472 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002473 default:
2474 ;
2475 }
2476out:
2477 return r;
2478}
2479
2480static long kvm_vm_ioctl(struct file *filp,
2481 unsigned int ioctl, unsigned long arg)
2482{
2483 struct kvm *kvm = filp->private_data;
2484 void __user *argp = (void __user *)arg;
2485 int r = -EINVAL;
2486
2487 switch (ioctl) {
2488 case KVM_CREATE_VCPU:
2489 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2490 if (r < 0)
2491 goto out;
2492 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002493 case KVM_SET_MEMORY_REGION: {
2494 struct kvm_memory_region kvm_mem;
2495
2496 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002497 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002499 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500 if (r)
2501 goto out;
2502 break;
2503 }
2504 case KVM_GET_DIRTY_LOG: {
2505 struct kvm_dirty_log log;
2506
2507 r = -EFAULT;
Al Viro2f366982007-02-09 16:38:35 +00002508 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002510 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002511 if (r)
2512 goto out;
2513 break;
2514 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002515 default:
2516 ;
2517 }
2518out:
2519 return r;
2520}
2521
2522static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2523 unsigned long address,
2524 int *type)
2525{
2526 struct kvm *kvm = vma->vm_file->private_data;
2527 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002528 struct page *page;
2529
2530 *type = VM_FAULT_MINOR;
2531 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc22007-03-30 14:02:32 +03002532 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02002533 if (!page)
2534 return NOPAGE_SIGBUS;
2535 get_page(page);
2536 return page;
2537}
2538
2539static struct vm_operations_struct kvm_vm_vm_ops = {
2540 .nopage = kvm_vm_nopage,
2541};
2542
2543static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2544{
2545 vma->vm_ops = &kvm_vm_vm_ops;
2546 return 0;
2547}
2548
2549static struct file_operations kvm_vm_fops = {
2550 .release = kvm_vm_release,
2551 .unlocked_ioctl = kvm_vm_ioctl,
2552 .compat_ioctl = kvm_vm_ioctl,
2553 .mmap = kvm_vm_mmap,
2554};
2555
2556static int kvm_dev_ioctl_create_vm(void)
2557{
2558 int fd, r;
2559 struct inode *inode;
2560 struct file *file;
2561 struct kvm *kvm;
2562
2563 inode = kvmfs_inode(&kvm_vm_fops);
2564 if (IS_ERR(inode)) {
2565 r = PTR_ERR(inode);
2566 goto out1;
2567 }
2568
2569 kvm = kvm_create_vm();
2570 if (IS_ERR(kvm)) {
2571 r = PTR_ERR(kvm);
2572 goto out2;
2573 }
2574
2575 file = kvmfs_file(inode, kvm);
2576 if (IS_ERR(file)) {
2577 r = PTR_ERR(file);
2578 goto out3;
2579 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002580 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002581
2582 r = get_unused_fd();
2583 if (r < 0)
2584 goto out4;
2585 fd = r;
2586 fd_install(fd, file);
2587
2588 return fd;
2589
2590out4:
2591 fput(file);
2592out3:
2593 kvm_destroy_vm(kvm);
2594out2:
2595 iput(inode);
2596out1:
2597 return r;
2598}
2599
2600static long kvm_dev_ioctl(struct file *filp,
2601 unsigned int ioctl, unsigned long arg)
2602{
2603 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02002604 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002605
2606 switch (ioctl) {
2607 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002608 r = -EINVAL;
2609 if (arg)
2610 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002611 r = KVM_API_VERSION;
2612 break;
2613 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002614 r = -EINVAL;
2615 if (arg)
2616 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002617 r = kvm_dev_ioctl_create_vm();
2618 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002619 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f366982007-02-09 16:38:35 +00002620 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621 struct kvm_msr_list msr_list;
2622 unsigned n;
2623
2624 r = -EFAULT;
2625 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2626 goto out;
2627 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08002628 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002629 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2630 goto out;
2631 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08002632 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633 goto out;
2634 r = -EFAULT;
2635 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08002636 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08002638 if (copy_to_user(user_msr_list->indices
2639 + num_msrs_to_save * sizeof(u32),
2640 &emulated_msrs,
2641 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2642 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08002644 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645 }
Avi Kivity5d308f42007-03-01 17:56:20 +02002646 case KVM_CHECK_EXTENSION:
2647 /*
2648 * No extensions defined at present.
2649 */
2650 r = 0;
2651 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002652 case KVM_GET_VCPU_MMAP_SIZE:
2653 r = -EINVAL;
2654 if (arg)
2655 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02002656 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02002657 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658 default:
2659 ;
2660 }
2661out:
2662 return r;
2663}
2664
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665static struct file_operations kvm_chardev_ops = {
2666 .open = kvm_dev_open,
2667 .release = kvm_dev_release,
2668 .unlocked_ioctl = kvm_dev_ioctl,
2669 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670};
2671
2672static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002673 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 "kvm",
2675 &kvm_chardev_ops,
2676};
2677
2678static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2679 void *v)
2680{
2681 if (val == SYS_RESTART) {
2682 /*
2683 * Some (well, at least mine) BIOSes hang on reboot if
2684 * in vmx root mode.
2685 */
2686 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Al Viro8b6d44c2007-02-09 16:38:40 +00002687 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688 }
2689 return NOTIFY_OK;
2690}
2691
2692static struct notifier_block kvm_reboot_notifier = {
2693 .notifier_call = kvm_reboot,
2694 .priority = 0,
2695};
2696
Avi Kivity774c47f2007-02-12 00:54:47 -08002697/*
2698 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2699 * cached on it.
2700 */
2701static void decache_vcpus_on_cpu(int cpu)
2702{
2703 struct kvm *vm;
2704 struct kvm_vcpu *vcpu;
2705 int i;
2706
2707 spin_lock(&kvm_lock);
2708 list_for_each_entry(vm, &vm_list, vm_list)
2709 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2710 vcpu = &vm->vcpus[i];
2711 /*
2712 * If the vcpu is locked, then it is running on some
2713 * other cpu and therefore it is not cached on the
2714 * cpu in question.
2715 *
2716 * If it's not locked, check the last cpu it executed
2717 * on.
2718 */
2719 if (mutex_trylock(&vcpu->mutex)) {
2720 if (vcpu->cpu == cpu) {
2721 kvm_arch_ops->vcpu_decache(vcpu);
2722 vcpu->cpu = -1;
2723 }
2724 mutex_unlock(&vcpu->mutex);
2725 }
2726 }
2727 spin_unlock(&kvm_lock);
2728}
2729
2730static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2731 void *v)
2732{
2733 int cpu = (long)v;
2734
2735 switch (val) {
Jeremy Katz43934a32007-02-19 14:37:46 +02002736 case CPU_DOWN_PREPARE:
Avi Kivity774c47f2007-02-12 00:54:47 -08002737 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002738 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2739 cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002740 decache_vcpus_on_cpu(cpu);
2741 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
2742 NULL, 0, 1);
2743 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002744 case CPU_ONLINE:
2745 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2746 cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002747 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
2748 NULL, 0, 1);
2749 break;
2750 }
2751 return NOTIFY_OK;
2752}
2753
2754static struct notifier_block kvm_cpu_notifier = {
2755 .notifier_call = kvm_cpu_hotplug,
2756 .priority = 20, /* must be > scheduler priority */
2757};
2758
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759static __init void kvm_init_debug(void)
2760{
2761 struct kvm_stats_debugfs_item *p;
2762
Al Viro8b6d44c2007-02-09 16:38:40 +00002763 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764 for (p = debugfs_entries; p->name; ++p)
2765 p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
2766 p->data);
2767}
2768
2769static void kvm_exit_debug(void)
2770{
2771 struct kvm_stats_debugfs_item *p;
2772
2773 for (p = debugfs_entries; p->name; ++p)
2774 debugfs_remove(p->dentry);
2775 debugfs_remove(debugfs_dir);
2776}
2777
Avi Kivity59ae6c62007-02-12 00:54:48 -08002778static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2779{
2780 decache_vcpus_on_cpu(raw_smp_processor_id());
Avi Kivity19d14082007-02-19 14:37:48 +02002781 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002782 return 0;
2783}
2784
2785static int kvm_resume(struct sys_device *dev)
2786{
Avi Kivity19d14082007-02-19 14:37:48 +02002787 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002788 return 0;
2789}
2790
2791static struct sysdev_class kvm_sysdev_class = {
2792 set_kset_name("kvm"),
2793 .suspend = kvm_suspend,
2794 .resume = kvm_resume,
2795};
2796
2797static struct sys_device kvm_sysdev = {
2798 .id = 0,
2799 .cls = &kvm_sysdev_class,
2800};
2801
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802hpa_t bad_page_address;
2803
Avi Kivity37e29d92007-02-20 14:07:37 +02002804static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
2805 const char *dev_name, void *data, struct vfsmount *mnt)
2806{
Andrew Mortone9cdb1e2007-03-01 11:28:13 +02002807 return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
Avi Kivity37e29d92007-02-20 14:07:37 +02002808}
2809
2810static struct file_system_type kvm_fs_type = {
2811 .name = "kvmfs",
2812 .get_sb = kvmfs_get_sb,
2813 .kill_sb = kill_anon_super,
2814};
2815
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
2817{
2818 int r;
2819
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08002820 if (kvm_arch_ops) {
2821 printk(KERN_ERR "kvm: already loaded the other module\n");
2822 return -EEXIST;
2823 }
2824
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002825 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826 printk(KERN_ERR "kvm: no hardware support\n");
2827 return -EOPNOTSUPP;
2828 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002829 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830 printk(KERN_ERR "kvm: disabled by bios\n");
2831 return -EOPNOTSUPP;
2832 }
2833
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002834 kvm_arch_ops = ops;
2835
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 r = kvm_arch_ops->hardware_setup();
2837 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02002838 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839
Al Viro8b6d44c2007-02-09 16:38:40 +00002840 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002841 r = register_cpu_notifier(&kvm_cpu_notifier);
2842 if (r)
2843 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844 register_reboot_notifier(&kvm_reboot_notifier);
2845
Avi Kivity59ae6c62007-02-12 00:54:48 -08002846 r = sysdev_class_register(&kvm_sysdev_class);
2847 if (r)
2848 goto out_free_2;
2849
2850 r = sysdev_register(&kvm_sysdev);
2851 if (r)
2852 goto out_free_3;
2853
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854 kvm_chardev_ops.owner = module;
2855
2856 r = misc_register(&kvm_dev);
2857 if (r) {
2858 printk (KERN_ERR "kvm: misc device register failed\n");
2859 goto out_free;
2860 }
2861
2862 return r;
2863
2864out_free:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002865 sysdev_unregister(&kvm_sysdev);
2866out_free_3:
2867 sysdev_class_unregister(&kvm_sysdev_class);
2868out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002870 unregister_cpu_notifier(&kvm_cpu_notifier);
2871out_free_1:
Al Viro8b6d44c2007-02-09 16:38:40 +00002872 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873 kvm_arch_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02002874out:
2875 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876 return r;
2877}
2878
2879void kvm_exit_arch(void)
2880{
2881 misc_deregister(&kvm_dev);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002882 sysdev_unregister(&kvm_sysdev);
2883 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002885 unregister_cpu_notifier(&kvm_cpu_notifier);
Al Viro8b6d44c2007-02-09 16:38:40 +00002886 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 kvm_arch_ops->hardware_unsetup();
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08002888 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002889}
2890
2891static __init int kvm_init(void)
2892{
2893 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02002894 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895
Avi Kivity37e29d92007-02-20 14:07:37 +02002896 r = register_filesystem(&kvm_fs_type);
2897 if (r)
2898 goto out3;
2899
2900 kvmfs_mnt = kern_mount(&kvm_fs_type);
2901 r = PTR_ERR(kvmfs_mnt);
2902 if (IS_ERR(kvmfs_mnt))
2903 goto out2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904 kvm_init_debug();
2905
Michael Riepebf591b22006-12-22 01:05:36 -08002906 kvm_init_msr_list();
2907
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
2909 r = -ENOMEM;
2910 goto out;
2911 }
2912
2913 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
2914 memset(__va(bad_page_address), 0, PAGE_SIZE);
2915
Avi Kivity58e690e2007-02-26 16:29:43 +02002916 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917
2918out:
2919 kvm_exit_debug();
Avi Kivity37e29d92007-02-20 14:07:37 +02002920 mntput(kvmfs_mnt);
2921out2:
2922 unregister_filesystem(&kvm_fs_type);
2923out3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924 return r;
2925}
2926
2927static __exit void kvm_exit(void)
2928{
2929 kvm_exit_debug();
2930 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivity37e29d92007-02-20 14:07:37 +02002931 mntput(kvmfs_mnt);
2932 unregister_filesystem(&kvm_fs_type);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933}
2934
2935module_init(kvm_init)
2936module_exit(kvm_exit)
2937
2938EXPORT_SYMBOL_GPL(kvm_init_arch);
2939EXPORT_SYMBOL_GPL(kvm_exit_arch);