blob: e495855727e23985bfc6c2f8e5d8d8a63336b637 [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 Kivity954bbbc2007-03-30 14:02:32 +0300423 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424
425 spin_lock(&vcpu->kvm->lock);
Avi Kivity954bbbc2007-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
Avi Kivitye8207542007-03-30 16:54:30 +0300849/*
850 * Set a new alias region. Aliases map a portion of physical memory into
851 * another portion. This is useful for memory windows, for example the PC
852 * VGA region.
853 */
854static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
855 struct kvm_memory_alias *alias)
856{
857 int r, n;
858 struct kvm_mem_alias *p;
859
860 r = -EINVAL;
861 /* General sanity checks */
862 if (alias->memory_size & (PAGE_SIZE - 1))
863 goto out;
864 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
865 goto out;
866 if (alias->slot >= KVM_ALIAS_SLOTS)
867 goto out;
868 if (alias->guest_phys_addr + alias->memory_size
869 < alias->guest_phys_addr)
870 goto out;
871 if (alias->target_phys_addr + alias->memory_size
872 < alias->target_phys_addr)
873 goto out;
874
875 spin_lock(&kvm->lock);
876
877 p = &kvm->aliases[alias->slot];
878 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
879 p->npages = alias->memory_size >> PAGE_SHIFT;
880 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
881
882 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
883 if (kvm->aliases[n - 1].npages)
884 break;
885 kvm->naliases = n;
886
887 spin_unlock(&kvm->lock);
888
889 vcpu_load(&kvm->vcpus[0]);
890 spin_lock(&kvm->lock);
891 kvm_mmu_zap_all(&kvm->vcpus[0]);
892 spin_unlock(&kvm->lock);
893 vcpu_put(&kvm->vcpus[0]);
894
895 return 0;
896
897out:
898 return r;
899}
900
901static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
902{
903 int i;
904 struct kvm_mem_alias *alias;
905
906 for (i = 0; i < kvm->naliases; ++i) {
907 alias = &kvm->aliases[i];
908 if (gfn >= alias->base_gfn
909 && gfn < alias->base_gfn + alias->npages)
910 return alias->target_gfn + gfn - alias->base_gfn;
911 }
912 return gfn;
913}
914
915static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916{
917 int i;
918
919 for (i = 0; i < kvm->nmemslots; ++i) {
920 struct kvm_memory_slot *memslot = &kvm->memslots[i];
921
922 if (gfn >= memslot->base_gfn
923 && gfn < memslot->base_gfn + memslot->npages)
924 return memslot;
925 }
Al Viro8b6d44c2007-02-09 16:38:40 +0000926 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927}
Avi Kivitye8207542007-03-30 16:54:30 +0300928
929struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
930{
931 gfn = unalias_gfn(kvm, gfn);
932 return __gfn_to_memslot(kvm, gfn);
933}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
Avi Kivity954bbbc2007-03-30 14:02:32 +0300935struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
936{
937 struct kvm_memory_slot *slot;
938
Avi Kivitye8207542007-03-30 16:54:30 +0300939 gfn = unalias_gfn(kvm, gfn);
940 slot = __gfn_to_memslot(kvm, gfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +0300941 if (!slot)
942 return NULL;
943 return slot->phys_mem[gfn - slot->base_gfn];
944}
945EXPORT_SYMBOL_GPL(gfn_to_page);
946
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
948{
949 int i;
Al Viro8b6d44c2007-02-09 16:38:40 +0000950 struct kvm_memory_slot *memslot = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 unsigned long rel_gfn;
952
953 for (i = 0; i < kvm->nmemslots; ++i) {
954 memslot = &kvm->memslots[i];
955
956 if (gfn >= memslot->base_gfn
957 && gfn < memslot->base_gfn + memslot->npages) {
958
959 if (!memslot || !memslot->dirty_bitmap)
960 return;
961
962 rel_gfn = gfn - memslot->base_gfn;
963
964 /* avoid RMW */
965 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
966 set_bit(rel_gfn, memslot->dirty_bitmap);
967 return;
968 }
969 }
970}
971
972static int emulator_read_std(unsigned long addr,
973 unsigned long *val,
974 unsigned int bytes,
975 struct x86_emulate_ctxt *ctxt)
976{
977 struct kvm_vcpu *vcpu = ctxt->vcpu;
978 void *data = val;
979
980 while (bytes) {
981 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
982 unsigned offset = addr & (PAGE_SIZE-1);
983 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
984 unsigned long pfn;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300985 struct page *page;
986 void *page_virt;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987
988 if (gpa == UNMAPPED_GVA)
989 return X86EMUL_PROPAGATE_FAULT;
990 pfn = gpa >> PAGE_SHIFT;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300991 page = gfn_to_page(vcpu->kvm, pfn);
992 if (!page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 return X86EMUL_UNHANDLEABLE;
Avi Kivity954bbbc2007-03-30 14:02:32 +0300994 page_virt = kmap_atomic(page, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995
Avi Kivity954bbbc2007-03-30 14:02:32 +0300996 memcpy(data, page_virt + offset, tocopy);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997
Avi Kivity954bbbc2007-03-30 14:02:32 +0300998 kunmap_atomic(page_virt, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999
1000 bytes -= tocopy;
1001 data += tocopy;
1002 addr += tocopy;
1003 }
1004
1005 return X86EMUL_CONTINUE;
1006}
1007
1008static int emulator_write_std(unsigned long addr,
1009 unsigned long val,
1010 unsigned int bytes,
1011 struct x86_emulate_ctxt *ctxt)
1012{
1013 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1014 addr, bytes);
1015 return X86EMUL_UNHANDLEABLE;
1016}
1017
1018static int emulator_read_emulated(unsigned long addr,
1019 unsigned long *val,
1020 unsigned int bytes,
1021 struct x86_emulate_ctxt *ctxt)
1022{
1023 struct kvm_vcpu *vcpu = ctxt->vcpu;
1024
1025 if (vcpu->mmio_read_completed) {
1026 memcpy(val, vcpu->mmio_data, bytes);
1027 vcpu->mmio_read_completed = 0;
1028 return X86EMUL_CONTINUE;
1029 } else if (emulator_read_std(addr, val, bytes, ctxt)
1030 == X86EMUL_CONTINUE)
1031 return X86EMUL_CONTINUE;
1032 else {
1033 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001034
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 if (gpa == UNMAPPED_GVA)
Avi Kivityd27d4ac2007-02-19 14:37:46 +02001036 return X86EMUL_PROPAGATE_FAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037 vcpu->mmio_needed = 1;
1038 vcpu->mmio_phys_addr = gpa;
1039 vcpu->mmio_size = bytes;
1040 vcpu->mmio_is_write = 0;
1041
1042 return X86EMUL_UNHANDLEABLE;
1043 }
1044}
1045
Avi Kivityda4a00f2007-01-05 16:36:44 -08001046static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1047 unsigned long val, int bytes)
1048{
Avi Kivityda4a00f2007-01-05 16:36:44 -08001049 struct page *page;
1050 void *virt;
1051
1052 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1053 return 0;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001054 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1055 if (!page)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001056 return 0;
Avi Kivityda4a00f2007-01-05 16:36:44 -08001057 kvm_mmu_pre_write(vcpu, gpa, bytes);
Uri Lublinab51a432007-02-21 18:25:21 +02001058 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivityda4a00f2007-01-05 16:36:44 -08001059 virt = kmap_atomic(page, KM_USER0);
1060 memcpy(virt + offset_in_page(gpa), &val, bytes);
1061 kunmap_atomic(virt, KM_USER0);
1062 kvm_mmu_post_write(vcpu, gpa, bytes);
1063 return 1;
1064}
1065
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066static int emulator_write_emulated(unsigned long addr,
1067 unsigned long val,
1068 unsigned int bytes,
1069 struct x86_emulate_ctxt *ctxt)
1070{
1071 struct kvm_vcpu *vcpu = ctxt->vcpu;
1072 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1073
1074 if (gpa == UNMAPPED_GVA)
1075 return X86EMUL_PROPAGATE_FAULT;
1076
Avi Kivityda4a00f2007-01-05 16:36:44 -08001077 if (emulator_write_phys(vcpu, gpa, val, bytes))
1078 return X86EMUL_CONTINUE;
1079
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080 vcpu->mmio_needed = 1;
1081 vcpu->mmio_phys_addr = gpa;
1082 vcpu->mmio_size = bytes;
1083 vcpu->mmio_is_write = 1;
1084 memcpy(vcpu->mmio_data, &val, bytes);
1085
1086 return X86EMUL_CONTINUE;
1087}
1088
1089static int emulator_cmpxchg_emulated(unsigned long addr,
1090 unsigned long old,
1091 unsigned long new,
1092 unsigned int bytes,
1093 struct x86_emulate_ctxt *ctxt)
1094{
1095 static int reported;
1096
1097 if (!reported) {
1098 reported = 1;
1099 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1100 }
1101 return emulator_write_emulated(addr, new, bytes, ctxt);
1102}
1103
Avi Kivity32b35622007-01-05 16:36:51 -08001104#ifdef CONFIG_X86_32
1105
1106static int emulator_cmpxchg8b_emulated(unsigned long addr,
1107 unsigned long old_lo,
1108 unsigned long old_hi,
1109 unsigned long new_lo,
1110 unsigned long new_hi,
1111 struct x86_emulate_ctxt *ctxt)
1112{
1113 static int reported;
1114 int r;
1115
1116 if (!reported) {
1117 reported = 1;
1118 printk(KERN_WARNING "kvm: emulating exchange8b as write\n");
1119 }
1120 r = emulator_write_emulated(addr, new_lo, 4, ctxt);
1121 if (r != X86EMUL_CONTINUE)
1122 return r;
1123 return emulator_write_emulated(addr+4, new_hi, 4, ctxt);
1124}
1125
1126#endif
1127
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1129{
1130 return kvm_arch_ops->get_segment_base(vcpu, seg);
1131}
1132
1133int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1134{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 return X86EMUL_CONTINUE;
1136}
1137
1138int emulate_clts(struct kvm_vcpu *vcpu)
1139{
Avi Kivity399badf2007-01-05 16:36:38 -08001140 unsigned long cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141
Avi Kivity399badf2007-01-05 16:36:38 -08001142 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1143 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144 kvm_arch_ops->set_cr0(vcpu, cr0);
1145 return X86EMUL_CONTINUE;
1146}
1147
1148int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1149{
1150 struct kvm_vcpu *vcpu = ctxt->vcpu;
1151
1152 switch (dr) {
1153 case 0 ... 3:
1154 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1155 return X86EMUL_CONTINUE;
1156 default:
1157 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1158 __FUNCTION__, dr);
1159 return X86EMUL_UNHANDLEABLE;
1160 }
1161}
1162
1163int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1164{
1165 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1166 int exception;
1167
1168 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1169 if (exception) {
1170 /* FIXME: better handling */
1171 return X86EMUL_UNHANDLEABLE;
1172 }
1173 return X86EMUL_CONTINUE;
1174}
1175
1176static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1177{
1178 static int reported;
1179 u8 opcodes[4];
1180 unsigned long rip = ctxt->vcpu->rip;
1181 unsigned long rip_linear;
1182
1183 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1184
1185 if (reported)
1186 return;
1187
1188 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1189
1190 printk(KERN_ERR "emulation failed but !mmio_needed?"
1191 " rip %lx %02x %02x %02x %02x\n",
1192 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1193 reported = 1;
1194}
1195
1196struct x86_emulate_ops emulate_ops = {
1197 .read_std = emulator_read_std,
1198 .write_std = emulator_write_std,
1199 .read_emulated = emulator_read_emulated,
1200 .write_emulated = emulator_write_emulated,
1201 .cmpxchg_emulated = emulator_cmpxchg_emulated,
Avi Kivity32b35622007-01-05 16:36:51 -08001202#ifdef CONFIG_X86_32
1203 .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated,
1204#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205};
1206
1207int emulate_instruction(struct kvm_vcpu *vcpu,
1208 struct kvm_run *run,
1209 unsigned long cr2,
1210 u16 error_code)
1211{
1212 struct x86_emulate_ctxt emulate_ctxt;
1213 int r;
1214 int cs_db, cs_l;
1215
1216 kvm_arch_ops->cache_regs(vcpu);
1217
1218 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1219
1220 emulate_ctxt.vcpu = vcpu;
1221 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1222 emulate_ctxt.cr2 = cr2;
1223 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1224 ? X86EMUL_MODE_REAL : cs_l
1225 ? X86EMUL_MODE_PROT64 : cs_db
1226 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1227
1228 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1229 emulate_ctxt.cs_base = 0;
1230 emulate_ctxt.ds_base = 0;
1231 emulate_ctxt.es_base = 0;
1232 emulate_ctxt.ss_base = 0;
1233 } else {
1234 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1235 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1236 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1237 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1238 }
1239
1240 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1241 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1242
1243 vcpu->mmio_is_write = 0;
1244 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1245
1246 if ((r || vcpu->mmio_is_write) && run) {
1247 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1248 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1249 run->mmio.len = vcpu->mmio_size;
1250 run->mmio.is_write = vcpu->mmio_is_write;
1251 }
1252
1253 if (r) {
Avi Kivitya4360362007-01-05 16:36:45 -08001254 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1255 return EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256 if (!vcpu->mmio_needed) {
1257 report_emulation_failure(&emulate_ctxt);
1258 return EMULATE_FAIL;
1259 }
1260 return EMULATE_DO_MMIO;
1261 }
1262
1263 kvm_arch_ops->decache_regs(vcpu);
1264 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1265
1266 if (vcpu->mmio_is_write)
1267 return EMULATE_DO_MMIO;
1268
1269 return EMULATE_DONE;
1270}
1271EXPORT_SYMBOL_GPL(emulate_instruction);
1272
Avi Kivity270fd9b2007-02-19 14:37:47 +02001273int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1274{
1275 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1276
Dor Laor9b22bf52007-02-19 16:44:49 +02001277 kvm_arch_ops->cache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001278 ret = -KVM_EINVAL;
1279#ifdef CONFIG_X86_64
1280 if (is_long_mode(vcpu)) {
1281 nr = vcpu->regs[VCPU_REGS_RAX];
1282 a0 = vcpu->regs[VCPU_REGS_RDI];
1283 a1 = vcpu->regs[VCPU_REGS_RSI];
1284 a2 = vcpu->regs[VCPU_REGS_RDX];
1285 a3 = vcpu->regs[VCPU_REGS_RCX];
1286 a4 = vcpu->regs[VCPU_REGS_R8];
1287 a5 = vcpu->regs[VCPU_REGS_R9];
1288 } else
1289#endif
1290 {
1291 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1292 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1293 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1294 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1295 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1296 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1297 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1298 }
1299 switch (nr) {
1300 default:
Avi Kivityb4e63f52007-03-04 13:59:30 +02001301 run->hypercall.args[0] = a0;
1302 run->hypercall.args[1] = a1;
1303 run->hypercall.args[2] = a2;
1304 run->hypercall.args[3] = a3;
1305 run->hypercall.args[4] = a4;
1306 run->hypercall.args[5] = a5;
1307 run->hypercall.ret = ret;
1308 run->hypercall.longmode = is_long_mode(vcpu);
1309 kvm_arch_ops->decache_regs(vcpu);
1310 return 0;
Avi Kivity270fd9b2007-02-19 14:37:47 +02001311 }
1312 vcpu->regs[VCPU_REGS_RAX] = ret;
Dor Laor9b22bf52007-02-19 16:44:49 +02001313 kvm_arch_ops->decache_regs(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001314 return 1;
1315}
1316EXPORT_SYMBOL_GPL(kvm_hypercall);
1317
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1319{
1320 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1321}
1322
1323void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1324{
1325 struct descriptor_table dt = { limit, base };
1326
1327 kvm_arch_ops->set_gdt(vcpu, &dt);
1328}
1329
1330void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1331{
1332 struct descriptor_table dt = { limit, base };
1333
1334 kvm_arch_ops->set_idt(vcpu, &dt);
1335}
1336
1337void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1338 unsigned long *rflags)
1339{
1340 lmsw(vcpu, msw);
1341 *rflags = kvm_arch_ops->get_rflags(vcpu);
1342}
1343
1344unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1345{
Avi Kivity399badf2007-01-05 16:36:38 -08001346 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 switch (cr) {
1348 case 0:
1349 return vcpu->cr0;
1350 case 2:
1351 return vcpu->cr2;
1352 case 3:
1353 return vcpu->cr3;
1354 case 4:
1355 return vcpu->cr4;
1356 default:
1357 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1358 return 0;
1359 }
1360}
1361
1362void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1363 unsigned long *rflags)
1364{
1365 switch (cr) {
1366 case 0:
1367 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1368 *rflags = kvm_arch_ops->get_rflags(vcpu);
1369 break;
1370 case 2:
1371 vcpu->cr2 = val;
1372 break;
1373 case 3:
1374 set_cr3(vcpu, val);
1375 break;
1376 case 4:
1377 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1378 break;
1379 default:
1380 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1381 }
1382}
1383
Ingo Molnar102d8322007-02-19 14:37:47 +02001384/*
1385 * Register the para guest with the host:
1386 */
1387static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1388{
1389 struct kvm_vcpu_para_state *para_state;
1390 hpa_t para_state_hpa, hypercall_hpa;
1391 struct page *para_state_page;
1392 unsigned char *hypercall;
1393 gpa_t hypercall_gpa;
1394
1395 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1396 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1397
1398 /*
1399 * Needs to be page aligned:
1400 */
1401 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1402 goto err_gp;
1403
1404 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1405 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1406 if (is_error_hpa(para_state_hpa))
1407 goto err_gp;
1408
Uri Lublinab51a432007-02-21 18:25:21 +02001409 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001410 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1411 para_state = kmap_atomic(para_state_page, KM_USER0);
1412
1413 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1414 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1415
1416 para_state->host_version = KVM_PARA_API_VERSION;
1417 /*
1418 * We cannot support guests that try to register themselves
1419 * with a newer API version than the host supports:
1420 */
1421 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1422 para_state->ret = -KVM_EINVAL;
1423 goto err_kunmap_skip;
1424 }
1425
1426 hypercall_gpa = para_state->hypercall_gpa;
1427 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1428 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1429 if (is_error_hpa(hypercall_hpa)) {
1430 para_state->ret = -KVM_EINVAL;
1431 goto err_kunmap_skip;
1432 }
1433
1434 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1435 vcpu->para_state_page = para_state_page;
1436 vcpu->para_state_gpa = para_state_gpa;
1437 vcpu->hypercall_gpa = hypercall_gpa;
1438
Uri Lublinab51a432007-02-21 18:25:21 +02001439 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
Ingo Molnar102d8322007-02-19 14:37:47 +02001440 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1441 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1442 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1443 kunmap_atomic(hypercall, KM_USER1);
1444
1445 para_state->ret = 0;
1446err_kunmap_skip:
1447 kunmap_atomic(para_state, KM_USER0);
1448 return 0;
1449err_gp:
1450 return 1;
1451}
1452
Avi Kivity3bab1f52006-12-29 16:49:48 -08001453int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1454{
1455 u64 data;
1456
1457 switch (msr) {
1458 case 0xc0010010: /* SYSCFG */
1459 case 0xc0010015: /* HWCR */
1460 case MSR_IA32_PLATFORM_ID:
1461 case MSR_IA32_P5_MC_ADDR:
1462 case MSR_IA32_P5_MC_TYPE:
1463 case MSR_IA32_MC0_CTL:
1464 case MSR_IA32_MCG_STATUS:
1465 case MSR_IA32_MCG_CAP:
1466 case MSR_IA32_MC0_MISC:
1467 case MSR_IA32_MC0_MISC+4:
1468 case MSR_IA32_MC0_MISC+8:
1469 case MSR_IA32_MC0_MISC+12:
1470 case MSR_IA32_MC0_MISC+16:
1471 case MSR_IA32_UCODE_REV:
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001472 case MSR_IA32_PERF_STATUS:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001473 /* MTRR registers */
1474 case 0xfe:
1475 case 0x200 ... 0x2ff:
1476 data = 0;
1477 break;
Avi Kivitya8d13ea2006-12-29 16:49:51 -08001478 case 0xcd: /* fsb frequency */
1479 data = 3;
1480 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001481 case MSR_IA32_APICBASE:
1482 data = vcpu->apic_base;
1483 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001484 case MSR_IA32_MISC_ENABLE:
1485 data = vcpu->ia32_misc_enable_msr;
1486 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001487#ifdef CONFIG_X86_64
1488 case MSR_EFER:
1489 data = vcpu->shadow_efer;
1490 break;
1491#endif
1492 default:
1493 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1494 return 1;
1495 }
1496 *pdata = data;
1497 return 0;
1498}
1499EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1500
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501/*
1502 * Reads an msr value (of 'msr_index') into 'pdata'.
1503 * Returns 0 on success, non-0 otherwise.
1504 * Assumes vcpu_load() was already called.
1505 */
1506static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1507{
1508 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1509}
1510
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001511#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512
Avi Kivity3bab1f52006-12-29 16:49:48 -08001513static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515 if (efer & EFER_RESERVED_BITS) {
1516 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1517 efer);
1518 inject_gp(vcpu);
1519 return;
1520 }
1521
1522 if (is_paging(vcpu)
1523 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1524 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1525 inject_gp(vcpu);
1526 return;
1527 }
1528
Avi Kivity7725f0b2006-12-13 00:34:01 -08001529 kvm_arch_ops->set_efer(vcpu, efer);
1530
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531 efer &= ~EFER_LMA;
1532 efer |= vcpu->shadow_efer & EFER_LMA;
1533
1534 vcpu->shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001536
1537#endif
1538
Avi Kivity3bab1f52006-12-29 16:49:48 -08001539int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1540{
1541 switch (msr) {
1542#ifdef CONFIG_X86_64
1543 case MSR_EFER:
1544 set_efer(vcpu, data);
1545 break;
1546#endif
1547 case MSR_IA32_MC0_STATUS:
1548 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1549 __FUNCTION__, data);
1550 break;
Sergey Kiselev0e5bf0d2007-03-22 14:06:18 +02001551 case MSR_IA32_MCG_STATUS:
1552 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1553 __FUNCTION__, data);
1554 break;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001555 case MSR_IA32_UCODE_REV:
1556 case MSR_IA32_UCODE_WRITE:
1557 case 0x200 ... 0x2ff: /* MTRRs */
1558 break;
1559 case MSR_IA32_APICBASE:
1560 vcpu->apic_base = data;
1561 break;
Avi Kivity6f00e682007-01-26 00:56:40 -08001562 case MSR_IA32_MISC_ENABLE:
1563 vcpu->ia32_misc_enable_msr = data;
1564 break;
Ingo Molnar102d8322007-02-19 14:37:47 +02001565 /*
1566 * This is the 'probe whether the host is KVM' logic:
1567 */
1568 case MSR_KVM_API_MAGIC:
1569 return vcpu_register_para(vcpu, data);
1570
Avi Kivity3bab1f52006-12-29 16:49:48 -08001571 default:
1572 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1573 return 1;
1574 }
1575 return 0;
1576}
1577EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1578
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579/*
1580 * Writes msr value into into the appropriate "register".
1581 * Returns 0 on success, non-0 otherwise.
1582 * Assumes vcpu_load() was already called.
1583 */
1584static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1585{
1586 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1587}
1588
1589void kvm_resched(struct kvm_vcpu *vcpu)
1590{
1591 vcpu_put(vcpu);
1592 cond_resched();
Avi Kivitybccf2152007-02-21 18:04:26 +02001593 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594}
1595EXPORT_SYMBOL_GPL(kvm_resched);
1596
1597void load_msrs(struct vmx_msr_entry *e, int n)
1598{
1599 int i;
1600
1601 for (i = 0; i < n; ++i)
1602 wrmsrl(e[i].index, e[i].data);
1603}
1604EXPORT_SYMBOL_GPL(load_msrs);
1605
1606void save_msrs(struct vmx_msr_entry *e, int n)
1607{
1608 int i;
1609
1610 for (i = 0; i < n; ++i)
1611 rdmsrl(e[i].index, e[i].data);
1612}
1613EXPORT_SYMBOL_GPL(save_msrs);
1614
Avi Kivity06465c52007-02-28 20:46:53 +02001615void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1616{
1617 int i;
1618 u32 function;
1619 struct kvm_cpuid_entry *e, *best;
1620
1621 kvm_arch_ops->cache_regs(vcpu);
1622 function = vcpu->regs[VCPU_REGS_RAX];
1623 vcpu->regs[VCPU_REGS_RAX] = 0;
1624 vcpu->regs[VCPU_REGS_RBX] = 0;
1625 vcpu->regs[VCPU_REGS_RCX] = 0;
1626 vcpu->regs[VCPU_REGS_RDX] = 0;
1627 best = NULL;
1628 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1629 e = &vcpu->cpuid_entries[i];
1630 if (e->function == function) {
1631 best = e;
1632 break;
1633 }
1634 /*
1635 * Both basic or both extended?
1636 */
1637 if (((e->function ^ function) & 0x80000000) == 0)
1638 if (!best || e->function > best->function)
1639 best = e;
1640 }
1641 if (best) {
1642 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1643 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1644 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1645 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1646 }
1647 kvm_arch_ops->decache_regs(vcpu);
1648 kvm_arch_ops->skip_emulated_instruction(vcpu);
1649}
1650EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1651
Avi Kivity039576c2007-03-20 12:46:50 +02001652static int pio_copy_data(struct kvm_vcpu *vcpu)
Avi Kivity46fc1472007-02-22 19:39:30 +02001653{
Avi Kivity039576c2007-03-20 12:46:50 +02001654 void *p = vcpu->pio_data;
1655 void *q;
1656 unsigned bytes;
1657 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1658
1659 kvm_arch_ops->vcpu_put(vcpu);
1660 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1661 PAGE_KERNEL);
1662 if (!q) {
1663 kvm_arch_ops->vcpu_load(vcpu);
1664 free_pio_guest_pages(vcpu);
1665 return -ENOMEM;
1666 }
1667 q += vcpu->pio.guest_page_offset;
1668 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1669 if (vcpu->pio.in)
1670 memcpy(q, p, bytes);
1671 else
1672 memcpy(p, q, bytes);
1673 q -= vcpu->pio.guest_page_offset;
1674 vunmap(q);
1675 kvm_arch_ops->vcpu_load(vcpu);
1676 free_pio_guest_pages(vcpu);
1677 return 0;
1678}
1679
1680static int complete_pio(struct kvm_vcpu *vcpu)
1681{
1682 struct kvm_pio_request *io = &vcpu->pio;
Avi Kivity46fc1472007-02-22 19:39:30 +02001683 long delta;
Avi Kivity039576c2007-03-20 12:46:50 +02001684 int r;
Avi Kivity46fc1472007-02-22 19:39:30 +02001685
1686 kvm_arch_ops->cache_regs(vcpu);
1687
1688 if (!io->string) {
Avi Kivity039576c2007-03-20 12:46:50 +02001689 if (io->in)
1690 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
Avi Kivity46fc1472007-02-22 19:39:30 +02001691 io->size);
1692 } else {
Avi Kivity039576c2007-03-20 12:46:50 +02001693 if (io->in) {
1694 r = pio_copy_data(vcpu);
1695 if (r) {
1696 kvm_arch_ops->cache_regs(vcpu);
1697 return r;
1698 }
1699 }
1700
Avi Kivity46fc1472007-02-22 19:39:30 +02001701 delta = 1;
1702 if (io->rep) {
Avi Kivity039576c2007-03-20 12:46:50 +02001703 delta *= io->cur_count;
Avi Kivity46fc1472007-02-22 19:39:30 +02001704 /*
1705 * The size of the register should really depend on
1706 * current address size.
1707 */
1708 vcpu->regs[VCPU_REGS_RCX] -= delta;
1709 }
Avi Kivity039576c2007-03-20 12:46:50 +02001710 if (io->down)
Avi Kivity46fc1472007-02-22 19:39:30 +02001711 delta = -delta;
1712 delta *= io->size;
Avi Kivity039576c2007-03-20 12:46:50 +02001713 if (io->in)
Avi Kivity46fc1472007-02-22 19:39:30 +02001714 vcpu->regs[VCPU_REGS_RDI] += delta;
1715 else
1716 vcpu->regs[VCPU_REGS_RSI] += delta;
1717 }
1718
Avi Kivity46fc1472007-02-22 19:39:30 +02001719 vcpu->run->io_completed = 0;
1720
1721 kvm_arch_ops->decache_regs(vcpu);
1722
Avi Kivity039576c2007-03-20 12:46:50 +02001723 io->count -= io->cur_count;
1724 io->cur_count = 0;
1725
1726 if (!io->count)
1727 kvm_arch_ops->skip_emulated_instruction(vcpu);
1728 return 0;
Avi Kivity46fc1472007-02-22 19:39:30 +02001729}
1730
Avi Kivity039576c2007-03-20 12:46:50 +02001731int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1732 int size, unsigned long count, int string, int down,
1733 gva_t address, int rep, unsigned port)
1734{
1735 unsigned now, in_page;
1736 int i;
1737 int nr_pages = 1;
1738 struct page *page;
1739
1740 vcpu->run->exit_reason = KVM_EXIT_IO;
1741 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1742 vcpu->run->io.size = size;
1743 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1744 vcpu->run->io.count = count;
1745 vcpu->run->io.port = port;
1746 vcpu->pio.count = count;
1747 vcpu->pio.cur_count = count;
1748 vcpu->pio.size = size;
1749 vcpu->pio.in = in;
1750 vcpu->pio.string = string;
1751 vcpu->pio.down = down;
1752 vcpu->pio.guest_page_offset = offset_in_page(address);
1753 vcpu->pio.rep = rep;
1754
1755 if (!string) {
1756 kvm_arch_ops->cache_regs(vcpu);
1757 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1758 kvm_arch_ops->decache_regs(vcpu);
1759 return 0;
1760 }
1761
1762 if (!count) {
1763 kvm_arch_ops->skip_emulated_instruction(vcpu);
1764 return 1;
1765 }
1766
1767 now = min(count, PAGE_SIZE / size);
1768
1769 if (!down)
1770 in_page = PAGE_SIZE - offset_in_page(address);
1771 else
1772 in_page = offset_in_page(address) + size;
1773 now = min(count, (unsigned long)in_page / size);
1774 if (!now) {
1775 /*
1776 * String I/O straddles page boundary. Pin two guest pages
1777 * so that we satisfy atomicity constraints. Do just one
1778 * transaction to avoid complexity.
1779 */
1780 nr_pages = 2;
1781 now = 1;
1782 }
1783 if (down) {
1784 /*
1785 * String I/O in reverse. Yuck. Kill the guest, fix later.
1786 */
1787 printk(KERN_ERR "kvm: guest string pio down\n");
1788 inject_gp(vcpu);
1789 return 1;
1790 }
1791 vcpu->run->io.count = now;
1792 vcpu->pio.cur_count = now;
1793
1794 for (i = 0; i < nr_pages; ++i) {
1795 spin_lock(&vcpu->kvm->lock);
1796 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1797 if (page)
1798 get_page(page);
1799 vcpu->pio.guest_pages[i] = page;
1800 spin_unlock(&vcpu->kvm->lock);
1801 if (!page) {
1802 inject_gp(vcpu);
1803 free_pio_guest_pages(vcpu);
1804 return 1;
1805 }
1806 }
1807
1808 if (!vcpu->pio.in)
1809 return pio_copy_data(vcpu);
1810 return 0;
1811}
1812EXPORT_SYMBOL_GPL(kvm_setup_pio);
1813
Avi Kivitybccf2152007-02-21 18:04:26 +02001814static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816 int r;
Avi Kivity1961d272007-03-05 19:46:05 +02001817 sigset_t sigsaved;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001818
Avi Kivitybccf2152007-02-21 18:04:26 +02001819 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820
Avi Kivity1961d272007-03-05 19:46:05 +02001821 if (vcpu->sigset_active)
1822 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1823
Dor Laor54810342007-02-12 00:54:39 -08001824 /* re-sync apic's tpr */
1825 vcpu->cr8 = kvm_run->cr8;
1826
Avi Kivity46fc1472007-02-22 19:39:30 +02001827 if (kvm_run->io_completed) {
Avi Kivity039576c2007-03-20 12:46:50 +02001828 if (vcpu->pio.cur_count) {
1829 r = complete_pio(vcpu);
1830 if (r)
1831 goto out;
1832 } else {
Avi Kivity46fc1472007-02-22 19:39:30 +02001833 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1834 vcpu->mmio_read_completed = 1;
1835 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836 }
1837
1838 vcpu->mmio_needed = 0;
1839
Avi Kivity8eb7d332007-03-04 14:17:08 +02001840 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
Avi Kivityb4e63f52007-03-04 13:59:30 +02001841 kvm_arch_ops->cache_regs(vcpu);
1842 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1843 kvm_arch_ops->decache_regs(vcpu);
1844 }
1845
Avi Kivity6aa8b732006-12-10 02:21:36 -08001846 r = kvm_arch_ops->run(vcpu, kvm_run);
1847
Avi Kivity039576c2007-03-20 12:46:50 +02001848out:
Avi Kivity1961d272007-03-05 19:46:05 +02001849 if (vcpu->sigset_active)
1850 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1851
Avi Kivity6aa8b732006-12-10 02:21:36 -08001852 vcpu_put(vcpu);
1853 return r;
1854}
1855
Avi Kivitybccf2152007-02-21 18:04:26 +02001856static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1857 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858{
Avi Kivitybccf2152007-02-21 18:04:26 +02001859 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001860
1861 kvm_arch_ops->cache_regs(vcpu);
1862
1863 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1864 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1865 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1866 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1867 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1868 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1869 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1870 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001871#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1873 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1874 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1875 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1876 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1877 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1878 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1879 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1880#endif
1881
1882 regs->rip = vcpu->rip;
1883 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1884
1885 /*
1886 * Don't leak debug flags in case they were set for guest debugging
1887 */
1888 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1889 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1890
1891 vcpu_put(vcpu);
1892
1893 return 0;
1894}
1895
Avi Kivitybccf2152007-02-21 18:04:26 +02001896static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1897 struct kvm_regs *regs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898{
Avi Kivitybccf2152007-02-21 18:04:26 +02001899 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900
1901 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1902 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1903 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1904 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1905 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1906 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1907 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1908 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001909#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1911 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1912 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1913 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1914 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1915 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1916 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1917 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1918#endif
1919
1920 vcpu->rip = regs->rip;
1921 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1922
1923 kvm_arch_ops->decache_regs(vcpu);
1924
1925 vcpu_put(vcpu);
1926
1927 return 0;
1928}
1929
1930static void get_segment(struct kvm_vcpu *vcpu,
1931 struct kvm_segment *var, int seg)
1932{
1933 return kvm_arch_ops->get_segment(vcpu, var, seg);
1934}
1935
Avi Kivitybccf2152007-02-21 18:04:26 +02001936static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1937 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939 struct descriptor_table dt;
1940
Avi Kivitybccf2152007-02-21 18:04:26 +02001941 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942
1943 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1944 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1945 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1946 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1947 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1948 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1949
1950 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1951 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1952
1953 kvm_arch_ops->get_idt(vcpu, &dt);
1954 sregs->idt.limit = dt.limit;
1955 sregs->idt.base = dt.base;
1956 kvm_arch_ops->get_gdt(vcpu, &dt);
1957 sregs->gdt.limit = dt.limit;
1958 sregs->gdt.base = dt.base;
1959
Avi Kivity399badf2007-01-05 16:36:38 -08001960 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 sregs->cr0 = vcpu->cr0;
1962 sregs->cr2 = vcpu->cr2;
1963 sregs->cr3 = vcpu->cr3;
1964 sregs->cr4 = vcpu->cr4;
1965 sregs->cr8 = vcpu->cr8;
1966 sregs->efer = vcpu->shadow_efer;
1967 sregs->apic_base = vcpu->apic_base;
1968
1969 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1970 sizeof sregs->interrupt_bitmap);
1971
1972 vcpu_put(vcpu);
1973
1974 return 0;
1975}
1976
1977static void set_segment(struct kvm_vcpu *vcpu,
1978 struct kvm_segment *var, int seg)
1979{
1980 return kvm_arch_ops->set_segment(vcpu, var, seg);
1981}
1982
Avi Kivitybccf2152007-02-21 18:04:26 +02001983static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1984 struct kvm_sregs *sregs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001986 int mmu_reset_needed = 0;
1987 int i;
1988 struct descriptor_table dt;
1989
Avi Kivitybccf2152007-02-21 18:04:26 +02001990 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991
Avi Kivity6aa8b732006-12-10 02:21:36 -08001992 dt.limit = sregs->idt.limit;
1993 dt.base = sregs->idt.base;
1994 kvm_arch_ops->set_idt(vcpu, &dt);
1995 dt.limit = sregs->gdt.limit;
1996 dt.base = sregs->gdt.base;
1997 kvm_arch_ops->set_gdt(vcpu, &dt);
1998
1999 vcpu->cr2 = sregs->cr2;
2000 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2001 vcpu->cr3 = sregs->cr3;
2002
2003 vcpu->cr8 = sregs->cr8;
2004
2005 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002006#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002007 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2008#endif
2009 vcpu->apic_base = sregs->apic_base;
2010
Avi Kivity399badf2007-01-05 16:36:38 -08002011 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
2012
Avi Kivity6aa8b732006-12-10 02:21:36 -08002013 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
Avi Kivityf6528b02007-03-20 18:44:51 +02002014 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002015
2016 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2017 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
Avi Kivity1b0973b2007-01-05 16:36:41 -08002018 if (!is_long_mode(vcpu) && is_pae(vcpu))
2019 load_pdptrs(vcpu, vcpu->cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002020
2021 if (mmu_reset_needed)
2022 kvm_mmu_reset_context(vcpu);
2023
2024 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2025 sizeof vcpu->irq_pending);
2026 vcpu->irq_summary = 0;
2027 for (i = 0; i < NR_IRQ_WORDS; ++i)
2028 if (vcpu->irq_pending[i])
2029 __set_bit(i, &vcpu->irq_summary);
2030
Avi Kivity024aa1c2007-03-21 13:44:58 +02002031 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2032 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2033 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2034 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2035 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2036 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2037
2038 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2039 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2040
Avi Kivity6aa8b732006-12-10 02:21:36 -08002041 vcpu_put(vcpu);
2042
2043 return 0;
2044}
2045
2046/*
2047 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2048 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Michael Riepebf591b22006-12-22 01:05:36 -08002049 *
2050 * This list is modified at module load time to reflect the
2051 * capabilities of the host cpu.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002052 */
2053static u32 msrs_to_save[] = {
2054 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2055 MSR_K6_STAR,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002056#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002057 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2058#endif
2059 MSR_IA32_TIME_STAMP_COUNTER,
2060};
2061
Michael Riepebf591b22006-12-22 01:05:36 -08002062static unsigned num_msrs_to_save;
2063
Avi Kivity6f00e682007-01-26 00:56:40 -08002064static u32 emulated_msrs[] = {
2065 MSR_IA32_MISC_ENABLE,
2066};
2067
Michael Riepebf591b22006-12-22 01:05:36 -08002068static __init void kvm_init_msr_list(void)
2069{
2070 u32 dummy[2];
2071 unsigned i, j;
2072
2073 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2074 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2075 continue;
2076 if (j < i)
2077 msrs_to_save[j] = msrs_to_save[i];
2078 j++;
2079 }
2080 num_msrs_to_save = j;
2081}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082
2083/*
2084 * Adapt set_msr() to msr_io()'s calling convention
2085 */
2086static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2087{
2088 return set_msr(vcpu, index, *data);
2089}
2090
2091/*
2092 * Read or write a bunch of msrs. All parameters are kernel addresses.
2093 *
2094 * @return number of msrs set successfully.
2095 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002096static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002097 struct kvm_msr_entry *entries,
2098 int (*do_msr)(struct kvm_vcpu *vcpu,
2099 unsigned index, u64 *data))
2100{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101 int i;
2102
Avi Kivitybccf2152007-02-21 18:04:26 +02002103 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104
2105 for (i = 0; i < msrs->nmsrs; ++i)
2106 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2107 break;
2108
2109 vcpu_put(vcpu);
2110
2111 return i;
2112}
2113
2114/*
2115 * Read or write a bunch of msrs. Parameters are user addresses.
2116 *
2117 * @return number of msrs set successfully.
2118 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002119static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120 int (*do_msr)(struct kvm_vcpu *vcpu,
2121 unsigned index, u64 *data),
2122 int writeback)
2123{
2124 struct kvm_msrs msrs;
2125 struct kvm_msr_entry *entries;
2126 int r, n;
2127 unsigned size;
2128
2129 r = -EFAULT;
2130 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2131 goto out;
2132
2133 r = -E2BIG;
2134 if (msrs.nmsrs >= MAX_IO_MSRS)
2135 goto out;
2136
2137 r = -ENOMEM;
2138 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2139 entries = vmalloc(size);
2140 if (!entries)
2141 goto out;
2142
2143 r = -EFAULT;
2144 if (copy_from_user(entries, user_msrs->entries, size))
2145 goto out_free;
2146
Avi Kivitybccf2152007-02-21 18:04:26 +02002147 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148 if (r < 0)
2149 goto out_free;
2150
2151 r = -EFAULT;
2152 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2153 goto out_free;
2154
2155 r = n;
2156
2157out_free:
2158 vfree(entries);
2159out:
2160 return r;
2161}
2162
2163/*
2164 * Translate a guest virtual address to a guest physical address.
2165 */
Avi Kivitybccf2152007-02-21 18:04:26 +02002166static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2167 struct kvm_translation *tr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168{
2169 unsigned long vaddr = tr->linear_address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170 gpa_t gpa;
2171
Avi Kivitybccf2152007-02-21 18:04:26 +02002172 vcpu_load(vcpu);
2173 spin_lock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002174 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2175 tr->physical_address = gpa;
2176 tr->valid = gpa != UNMAPPED_GVA;
2177 tr->writeable = 1;
2178 tr->usermode = 0;
Avi Kivitybccf2152007-02-21 18:04:26 +02002179 spin_unlock(&vcpu->kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180 vcpu_put(vcpu);
2181
2182 return 0;
2183}
2184
Avi Kivitybccf2152007-02-21 18:04:26 +02002185static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2186 struct kvm_interrupt *irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188 if (irq->irq < 0 || irq->irq >= 256)
2189 return -EINVAL;
Avi Kivitybccf2152007-02-21 18:04:26 +02002190 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191
2192 set_bit(irq->irq, vcpu->irq_pending);
2193 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2194
2195 vcpu_put(vcpu);
2196
2197 return 0;
2198}
2199
Avi Kivitybccf2152007-02-21 18:04:26 +02002200static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2201 struct kvm_debug_guest *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002202{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 int r;
2204
Avi Kivitybccf2152007-02-21 18:04:26 +02002205 vcpu_load(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206
2207 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2208
2209 vcpu_put(vcpu);
2210
2211 return r;
2212}
2213
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002214static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2215 unsigned long address,
2216 int *type)
2217{
2218 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2219 unsigned long pgoff;
2220 struct page *page;
2221
2222 *type = VM_FAULT_MINOR;
2223 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity039576c2007-03-20 12:46:50 +02002224 if (pgoff == 0)
2225 page = virt_to_page(vcpu->run);
2226 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2227 page = virt_to_page(vcpu->pio_data);
2228 else
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002229 return NOPAGE_SIGBUS;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002230 get_page(page);
2231 return page;
2232}
2233
2234static struct vm_operations_struct kvm_vcpu_vm_ops = {
2235 .nopage = kvm_vcpu_nopage,
2236};
2237
2238static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2239{
2240 vma->vm_ops = &kvm_vcpu_vm_ops;
2241 return 0;
2242}
2243
Avi Kivitybccf2152007-02-21 18:04:26 +02002244static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2245{
2246 struct kvm_vcpu *vcpu = filp->private_data;
2247
2248 fput(vcpu->kvm->filp);
2249 return 0;
2250}
2251
2252static struct file_operations kvm_vcpu_fops = {
2253 .release = kvm_vcpu_release,
2254 .unlocked_ioctl = kvm_vcpu_ioctl,
2255 .compat_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002256 .mmap = kvm_vcpu_mmap,
Avi Kivitybccf2152007-02-21 18:04:26 +02002257};
2258
2259/*
2260 * Allocates an inode for the vcpu.
2261 */
2262static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2263{
2264 int fd, r;
2265 struct inode *inode;
2266 struct file *file;
2267
2268 atomic_inc(&vcpu->kvm->filp->f_count);
2269 inode = kvmfs_inode(&kvm_vcpu_fops);
2270 if (IS_ERR(inode)) {
2271 r = PTR_ERR(inode);
2272 goto out1;
2273 }
2274
2275 file = kvmfs_file(inode, vcpu);
2276 if (IS_ERR(file)) {
2277 r = PTR_ERR(file);
2278 goto out2;
2279 }
2280
2281 r = get_unused_fd();
2282 if (r < 0)
2283 goto out3;
2284 fd = r;
2285 fd_install(fd, file);
2286
2287 return fd;
2288
2289out3:
2290 fput(file);
2291out2:
2292 iput(inode);
2293out1:
2294 fput(vcpu->kvm->filp);
2295 return r;
2296}
2297
Avi Kivityc5ea7662007-02-20 18:41:05 +02002298/*
2299 * Creates some virtual cpus. Good luck creating more than one.
2300 */
2301static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2302{
2303 int r;
2304 struct kvm_vcpu *vcpu;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002305 struct page *page;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002306
2307 r = -EINVAL;
2308 if (!valid_vcpu(n))
2309 goto out;
2310
2311 vcpu = &kvm->vcpus[n];
2312
2313 mutex_lock(&vcpu->mutex);
2314
2315 if (vcpu->vmcs) {
2316 mutex_unlock(&vcpu->mutex);
2317 return -EEXIST;
2318 }
2319
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002320 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2321 r = -ENOMEM;
2322 if (!page)
2323 goto out_unlock;
2324 vcpu->run = page_address(page);
2325
Avi Kivity039576c2007-03-20 12:46:50 +02002326 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2327 r = -ENOMEM;
2328 if (!page)
2329 goto out_free_run;
2330 vcpu->pio_data = page_address(page);
2331
Avi Kivityc5ea7662007-02-20 18:41:05 +02002332 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2333 FX_IMAGE_ALIGN);
2334 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
2335
2336 r = kvm_arch_ops->vcpu_create(vcpu);
2337 if (r < 0)
2338 goto out_free_vcpus;
2339
2340 r = kvm_mmu_create(vcpu);
2341 if (r < 0)
2342 goto out_free_vcpus;
2343
2344 kvm_arch_ops->vcpu_load(vcpu);
2345 r = kvm_mmu_setup(vcpu);
2346 if (r >= 0)
2347 r = kvm_arch_ops->vcpu_setup(vcpu);
2348 vcpu_put(vcpu);
2349
2350 if (r < 0)
2351 goto out_free_vcpus;
2352
Avi Kivitybccf2152007-02-21 18:04:26 +02002353 r = create_vcpu_fd(vcpu);
2354 if (r < 0)
2355 goto out_free_vcpus;
2356
2357 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02002358
2359out_free_vcpus:
2360 kvm_free_vcpu(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02002361out_free_run:
2362 free_page((unsigned long)vcpu->run);
2363 vcpu->run = NULL;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002364out_unlock:
Avi Kivityc5ea7662007-02-20 18:41:05 +02002365 mutex_unlock(&vcpu->mutex);
2366out:
2367 return r;
2368}
2369
Avi Kivity06465c52007-02-28 20:46:53 +02002370static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2371 struct kvm_cpuid *cpuid,
2372 struct kvm_cpuid_entry __user *entries)
2373{
2374 int r;
2375
2376 r = -E2BIG;
2377 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2378 goto out;
2379 r = -EFAULT;
2380 if (copy_from_user(&vcpu->cpuid_entries, entries,
2381 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2382 goto out;
2383 vcpu->cpuid_nent = cpuid->nent;
2384 return 0;
2385
2386out:
2387 return r;
2388}
2389
Avi Kivity1961d272007-03-05 19:46:05 +02002390static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2391{
2392 if (sigset) {
2393 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2394 vcpu->sigset_active = 1;
2395 vcpu->sigset = *sigset;
2396 } else
2397 vcpu->sigset_active = 0;
2398 return 0;
2399}
2400
Avi Kivitybccf2152007-02-21 18:04:26 +02002401static long kvm_vcpu_ioctl(struct file *filp,
2402 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403{
Avi Kivitybccf2152007-02-21 18:04:26 +02002404 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00002405 void __user *argp = (void __user *)arg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406 int r = -EINVAL;
2407
2408 switch (ioctl) {
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002409 case KVM_RUN:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002410 r = -EINVAL;
2411 if (arg)
2412 goto out;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002413 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415 case KVM_GET_REGS: {
2416 struct kvm_regs kvm_regs;
2417
Avi Kivitybccf2152007-02-21 18:04:26 +02002418 memset(&kvm_regs, 0, sizeof kvm_regs);
2419 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002420 if (r)
2421 goto out;
2422 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002423 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424 goto out;
2425 r = 0;
2426 break;
2427 }
2428 case KVM_SET_REGS: {
2429 struct kvm_regs kvm_regs;
2430
2431 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002432 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002434 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 if (r)
2436 goto out;
2437 r = 0;
2438 break;
2439 }
2440 case KVM_GET_SREGS: {
2441 struct kvm_sregs kvm_sregs;
2442
Avi Kivitybccf2152007-02-21 18:04:26 +02002443 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2444 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445 if (r)
2446 goto out;
2447 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002448 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449 goto out;
2450 r = 0;
2451 break;
2452 }
2453 case KVM_SET_SREGS: {
2454 struct kvm_sregs kvm_sregs;
2455
2456 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002457 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002459 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460 if (r)
2461 goto out;
2462 r = 0;
2463 break;
2464 }
2465 case KVM_TRANSLATE: {
2466 struct kvm_translation tr;
2467
2468 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002469 if (copy_from_user(&tr, argp, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002471 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472 if (r)
2473 goto out;
2474 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002475 if (copy_to_user(argp, &tr, sizeof tr))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476 goto out;
2477 r = 0;
2478 break;
2479 }
2480 case KVM_INTERRUPT: {
2481 struct kvm_interrupt irq;
2482
2483 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002484 if (copy_from_user(&irq, argp, sizeof irq))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002486 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487 if (r)
2488 goto out;
2489 r = 0;
2490 break;
2491 }
2492 case KVM_DEBUG_GUEST: {
2493 struct kvm_debug_guest dbg;
2494
2495 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002496 if (copy_from_user(&dbg, argp, sizeof dbg))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002497 goto out;
Avi Kivitybccf2152007-02-21 18:04:26 +02002498 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499 if (r)
2500 goto out;
2501 r = 0;
2502 break;
2503 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002504 case KVM_GET_MSRS:
2505 r = msr_io(vcpu, argp, get_msr, 1);
2506 break;
2507 case KVM_SET_MSRS:
2508 r = msr_io(vcpu, argp, do_set_msr, 0);
2509 break;
Avi Kivity06465c52007-02-28 20:46:53 +02002510 case KVM_SET_CPUID: {
2511 struct kvm_cpuid __user *cpuid_arg = argp;
2512 struct kvm_cpuid cpuid;
2513
2514 r = -EFAULT;
2515 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2516 goto out;
2517 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2518 if (r)
2519 goto out;
2520 break;
2521 }
Avi Kivity1961d272007-03-05 19:46:05 +02002522 case KVM_SET_SIGNAL_MASK: {
2523 struct kvm_signal_mask __user *sigmask_arg = argp;
2524 struct kvm_signal_mask kvm_sigmask;
2525 sigset_t sigset, *p;
2526
2527 p = NULL;
2528 if (argp) {
2529 r = -EFAULT;
2530 if (copy_from_user(&kvm_sigmask, argp,
2531 sizeof kvm_sigmask))
2532 goto out;
2533 r = -EINVAL;
2534 if (kvm_sigmask.len != sizeof sigset)
2535 goto out;
2536 r = -EFAULT;
2537 if (copy_from_user(&sigset, sigmask_arg->sigset,
2538 sizeof sigset))
2539 goto out;
2540 p = &sigset;
2541 }
2542 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2543 break;
2544 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002545 default:
2546 ;
2547 }
2548out:
2549 return r;
2550}
2551
2552static long kvm_vm_ioctl(struct file *filp,
2553 unsigned int ioctl, unsigned long arg)
2554{
2555 struct kvm *kvm = filp->private_data;
2556 void __user *argp = (void __user *)arg;
2557 int r = -EINVAL;
2558
2559 switch (ioctl) {
2560 case KVM_CREATE_VCPU:
2561 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2562 if (r < 0)
2563 goto out;
2564 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002565 case KVM_SET_MEMORY_REGION: {
2566 struct kvm_memory_region kvm_mem;
2567
2568 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002569 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002571 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572 if (r)
2573 goto out;
2574 break;
2575 }
2576 case KVM_GET_DIRTY_LOG: {
2577 struct kvm_dirty_log log;
2578
2579 r = -EFAULT;
Al Viro2f3669872007-02-09 16:38:35 +00002580 if (copy_from_user(&log, argp, sizeof log))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002581 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02002582 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583 if (r)
2584 goto out;
2585 break;
2586 }
Avi Kivitye8207542007-03-30 16:54:30 +03002587 case KVM_SET_MEMORY_ALIAS: {
2588 struct kvm_memory_alias alias;
2589
2590 r = -EFAULT;
2591 if (copy_from_user(&alias, argp, sizeof alias))
2592 goto out;
2593 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2594 if (r)
2595 goto out;
2596 break;
2597 }
Avi Kivityf17abe92007-02-21 19:28:04 +02002598 default:
2599 ;
2600 }
2601out:
2602 return r;
2603}
2604
2605static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2606 unsigned long address,
2607 int *type)
2608{
2609 struct kvm *kvm = vma->vm_file->private_data;
2610 unsigned long pgoff;
Avi Kivityf17abe92007-02-21 19:28:04 +02002611 struct page *page;
2612
2613 *type = VM_FAULT_MINOR;
2614 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
Avi Kivity954bbbc2007-03-30 14:02:32 +03002615 page = gfn_to_page(kvm, pgoff);
Avi Kivityf17abe92007-02-21 19:28:04 +02002616 if (!page)
2617 return NOPAGE_SIGBUS;
2618 get_page(page);
2619 return page;
2620}
2621
2622static struct vm_operations_struct kvm_vm_vm_ops = {
2623 .nopage = kvm_vm_nopage,
2624};
2625
2626static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2627{
2628 vma->vm_ops = &kvm_vm_vm_ops;
2629 return 0;
2630}
2631
2632static struct file_operations kvm_vm_fops = {
2633 .release = kvm_vm_release,
2634 .unlocked_ioctl = kvm_vm_ioctl,
2635 .compat_ioctl = kvm_vm_ioctl,
2636 .mmap = kvm_vm_mmap,
2637};
2638
2639static int kvm_dev_ioctl_create_vm(void)
2640{
2641 int fd, r;
2642 struct inode *inode;
2643 struct file *file;
2644 struct kvm *kvm;
2645
2646 inode = kvmfs_inode(&kvm_vm_fops);
2647 if (IS_ERR(inode)) {
2648 r = PTR_ERR(inode);
2649 goto out1;
2650 }
2651
2652 kvm = kvm_create_vm();
2653 if (IS_ERR(kvm)) {
2654 r = PTR_ERR(kvm);
2655 goto out2;
2656 }
2657
2658 file = kvmfs_file(inode, kvm);
2659 if (IS_ERR(file)) {
2660 r = PTR_ERR(file);
2661 goto out3;
2662 }
Avi Kivitybccf2152007-02-21 18:04:26 +02002663 kvm->filp = file;
Avi Kivityf17abe92007-02-21 19:28:04 +02002664
2665 r = get_unused_fd();
2666 if (r < 0)
2667 goto out4;
2668 fd = r;
2669 fd_install(fd, file);
2670
2671 return fd;
2672
2673out4:
2674 fput(file);
2675out3:
2676 kvm_destroy_vm(kvm);
2677out2:
2678 iput(inode);
2679out1:
2680 return r;
2681}
2682
2683static long kvm_dev_ioctl(struct file *filp,
2684 unsigned int ioctl, unsigned long arg)
2685{
2686 void __user *argp = (void __user *)arg;
Avi Kivity07c45a32007-03-07 13:05:38 +02002687 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02002688
2689 switch (ioctl) {
2690 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002691 r = -EINVAL;
2692 if (arg)
2693 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002694 r = KVM_API_VERSION;
2695 break;
2696 case KVM_CREATE_VM:
Avi Kivityf0fe5102007-03-07 13:11:17 +02002697 r = -EINVAL;
2698 if (arg)
2699 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02002700 r = kvm_dev_ioctl_create_vm();
2701 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 case KVM_GET_MSR_INDEX_LIST: {
Al Viro2f3669872007-02-09 16:38:35 +00002703 struct kvm_msr_list __user *user_msr_list = argp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 struct kvm_msr_list msr_list;
2705 unsigned n;
2706
2707 r = -EFAULT;
2708 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2709 goto out;
2710 n = msr_list.nmsrs;
Avi Kivity6f00e682007-01-26 00:56:40 -08002711 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2713 goto out;
2714 r = -E2BIG;
Michael Riepebf591b22006-12-22 01:05:36 -08002715 if (n < num_msrs_to_save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716 goto out;
2717 r = -EFAULT;
2718 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
Michael Riepebf591b22006-12-22 01:05:36 -08002719 num_msrs_to_save * sizeof(u32)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720 goto out;
Avi Kivity6f00e682007-01-26 00:56:40 -08002721 if (copy_to_user(user_msr_list->indices
2722 + num_msrs_to_save * sizeof(u32),
2723 &emulated_msrs,
2724 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2725 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726 r = 0;
Avi Kivitycc1d8952007-01-05 16:36:58 -08002727 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 }
Avi Kivity5d308f42007-03-01 17:56:20 +02002729 case KVM_CHECK_EXTENSION:
2730 /*
2731 * No extensions defined at present.
2732 */
2733 r = 0;
2734 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02002735 case KVM_GET_VCPU_MMAP_SIZE:
2736 r = -EINVAL;
2737 if (arg)
2738 goto out;
Avi Kivity039576c2007-03-20 12:46:50 +02002739 r = 2 * PAGE_SIZE;
Avi Kivity07c45a32007-03-07 13:05:38 +02002740 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 default:
2742 ;
2743 }
2744out:
2745 return r;
2746}
2747
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748static struct file_operations kvm_chardev_ops = {
2749 .open = kvm_dev_open,
2750 .release = kvm_dev_release,
2751 .unlocked_ioctl = kvm_dev_ioctl,
2752 .compat_ioctl = kvm_dev_ioctl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753};
2754
2755static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02002756 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757 "kvm",
2758 &kvm_chardev_ops,
2759};
2760
2761static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2762 void *v)
2763{
2764 if (val == SYS_RESTART) {
2765 /*
2766 * Some (well, at least mine) BIOSes hang on reboot if
2767 * in vmx root mode.
2768 */
2769 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
Al Viro8b6d44c2007-02-09 16:38:40 +00002770 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 }
2772 return NOTIFY_OK;
2773}
2774
2775static struct notifier_block kvm_reboot_notifier = {
2776 .notifier_call = kvm_reboot,
2777 .priority = 0,
2778};
2779
Avi Kivity774c47f2007-02-12 00:54:47 -08002780/*
2781 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2782 * cached on it.
2783 */
2784static void decache_vcpus_on_cpu(int cpu)
2785{
2786 struct kvm *vm;
2787 struct kvm_vcpu *vcpu;
2788 int i;
2789
2790 spin_lock(&kvm_lock);
2791 list_for_each_entry(vm, &vm_list, vm_list)
2792 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2793 vcpu = &vm->vcpus[i];
2794 /*
2795 * If the vcpu is locked, then it is running on some
2796 * other cpu and therefore it is not cached on the
2797 * cpu in question.
2798 *
2799 * If it's not locked, check the last cpu it executed
2800 * on.
2801 */
2802 if (mutex_trylock(&vcpu->mutex)) {
2803 if (vcpu->cpu == cpu) {
2804 kvm_arch_ops->vcpu_decache(vcpu);
2805 vcpu->cpu = -1;
2806 }
2807 mutex_unlock(&vcpu->mutex);
2808 }
2809 }
2810 spin_unlock(&kvm_lock);
2811}
2812
2813static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2814 void *v)
2815{
2816 int cpu = (long)v;
2817
2818 switch (val) {
Jeremy Katz43934a32007-02-19 14:37:46 +02002819 case CPU_DOWN_PREPARE:
Avi Kivity774c47f2007-02-12 00:54:47 -08002820 case CPU_UP_CANCELED:
Jeremy Katz43934a32007-02-19 14:37:46 +02002821 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2822 cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002823 decache_vcpus_on_cpu(cpu);
2824 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
2825 NULL, 0, 1);
2826 break;
Jeremy Katz43934a32007-02-19 14:37:46 +02002827 case CPU_ONLINE:
2828 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2829 cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08002830 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
2831 NULL, 0, 1);
2832 break;
2833 }
2834 return NOTIFY_OK;
2835}
2836
2837static struct notifier_block kvm_cpu_notifier = {
2838 .notifier_call = kvm_cpu_hotplug,
2839 .priority = 20, /* must be > scheduler priority */
2840};
2841
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842static __init void kvm_init_debug(void)
2843{
2844 struct kvm_stats_debugfs_item *p;
2845
Al Viro8b6d44c2007-02-09 16:38:40 +00002846 debugfs_dir = debugfs_create_dir("kvm", NULL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 for (p = debugfs_entries; p->name; ++p)
2848 p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
2849 p->data);
2850}
2851
2852static void kvm_exit_debug(void)
2853{
2854 struct kvm_stats_debugfs_item *p;
2855
2856 for (p = debugfs_entries; p->name; ++p)
2857 debugfs_remove(p->dentry);
2858 debugfs_remove(debugfs_dir);
2859}
2860
Avi Kivity59ae6c62007-02-12 00:54:48 -08002861static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2862{
2863 decache_vcpus_on_cpu(raw_smp_processor_id());
Avi Kivity19d14082007-02-19 14:37:48 +02002864 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002865 return 0;
2866}
2867
2868static int kvm_resume(struct sys_device *dev)
2869{
Avi Kivity19d14082007-02-19 14:37:48 +02002870 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002871 return 0;
2872}
2873
2874static struct sysdev_class kvm_sysdev_class = {
2875 set_kset_name("kvm"),
2876 .suspend = kvm_suspend,
2877 .resume = kvm_resume,
2878};
2879
2880static struct sys_device kvm_sysdev = {
2881 .id = 0,
2882 .cls = &kvm_sysdev_class,
2883};
2884
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885hpa_t bad_page_address;
2886
Avi Kivity37e29d92007-02-20 14:07:37 +02002887static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
2888 const char *dev_name, void *data, struct vfsmount *mnt)
2889{
Andrew Mortone9cdb1e2007-03-01 11:28:13 +02002890 return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
Avi Kivity37e29d92007-02-20 14:07:37 +02002891}
2892
2893static struct file_system_type kvm_fs_type = {
2894 .name = "kvmfs",
2895 .get_sb = kvmfs_get_sb,
2896 .kill_sb = kill_anon_super,
2897};
2898
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
2900{
2901 int r;
2902
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08002903 if (kvm_arch_ops) {
2904 printk(KERN_ERR "kvm: already loaded the other module\n");
2905 return -EEXIST;
2906 }
2907
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002908 if (!ops->cpu_has_kvm_support()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909 printk(KERN_ERR "kvm: no hardware support\n");
2910 return -EOPNOTSUPP;
2911 }
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002912 if (ops->disabled_by_bios()) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913 printk(KERN_ERR "kvm: disabled by bios\n");
2914 return -EOPNOTSUPP;
2915 }
2916
Yoshimi Ichiyanagie097f352007-01-05 16:36:24 -08002917 kvm_arch_ops = ops;
2918
Avi Kivity6aa8b732006-12-10 02:21:36 -08002919 r = kvm_arch_ops->hardware_setup();
2920 if (r < 0)
Avi Kivityca45aaa2007-03-01 19:21:03 +02002921 goto out;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922
Al Viro8b6d44c2007-02-09 16:38:40 +00002923 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
Avi Kivity774c47f2007-02-12 00:54:47 -08002924 r = register_cpu_notifier(&kvm_cpu_notifier);
2925 if (r)
2926 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002927 register_reboot_notifier(&kvm_reboot_notifier);
2928
Avi Kivity59ae6c62007-02-12 00:54:48 -08002929 r = sysdev_class_register(&kvm_sysdev_class);
2930 if (r)
2931 goto out_free_2;
2932
2933 r = sysdev_register(&kvm_sysdev);
2934 if (r)
2935 goto out_free_3;
2936
Avi Kivity6aa8b732006-12-10 02:21:36 -08002937 kvm_chardev_ops.owner = module;
2938
2939 r = misc_register(&kvm_dev);
2940 if (r) {
2941 printk (KERN_ERR "kvm: misc device register failed\n");
2942 goto out_free;
2943 }
2944
2945 return r;
2946
2947out_free:
Avi Kivity59ae6c62007-02-12 00:54:48 -08002948 sysdev_unregister(&kvm_sysdev);
2949out_free_3:
2950 sysdev_class_unregister(&kvm_sysdev_class);
2951out_free_2:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity774c47f2007-02-12 00:54:47 -08002953 unregister_cpu_notifier(&kvm_cpu_notifier);
2954out_free_1:
Al Viro8b6d44c2007-02-09 16:38:40 +00002955 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956 kvm_arch_ops->hardware_unsetup();
Avi Kivityca45aaa2007-03-01 19:21:03 +02002957out:
2958 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959 return r;
2960}
2961
2962void kvm_exit_arch(void)
2963{
2964 misc_deregister(&kvm_dev);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002965 sysdev_unregister(&kvm_sysdev);
2966 sysdev_class_unregister(&kvm_sysdev_class);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 unregister_reboot_notifier(&kvm_reboot_notifier);
Avi Kivity59ae6c62007-02-12 00:54:48 -08002968 unregister_cpu_notifier(&kvm_cpu_notifier);
Al Viro8b6d44c2007-02-09 16:38:40 +00002969 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970 kvm_arch_ops->hardware_unsetup();
Yoshimi Ichiyanagi09db28b2006-12-29 16:49:41 -08002971 kvm_arch_ops = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972}
2973
2974static __init int kvm_init(void)
2975{
2976 static struct page *bad_page;
Avi Kivity37e29d92007-02-20 14:07:37 +02002977 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978
Avi Kivity37e29d92007-02-20 14:07:37 +02002979 r = register_filesystem(&kvm_fs_type);
2980 if (r)
2981 goto out3;
2982
2983 kvmfs_mnt = kern_mount(&kvm_fs_type);
2984 r = PTR_ERR(kvmfs_mnt);
2985 if (IS_ERR(kvmfs_mnt))
2986 goto out2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002987 kvm_init_debug();
2988
Michael Riepebf591b22006-12-22 01:05:36 -08002989 kvm_init_msr_list();
2990
Avi Kivity6aa8b732006-12-10 02:21:36 -08002991 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
2992 r = -ENOMEM;
2993 goto out;
2994 }
2995
2996 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
2997 memset(__va(bad_page_address), 0, PAGE_SIZE);
2998
Avi Kivity58e690e2007-02-26 16:29:43 +02002999 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003000
3001out:
3002 kvm_exit_debug();
Avi Kivity37e29d92007-02-20 14:07:37 +02003003 mntput(kvmfs_mnt);
3004out2:
3005 unregister_filesystem(&kvm_fs_type);
3006out3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007 return r;
3008}
3009
3010static __exit void kvm_exit(void)
3011{
3012 kvm_exit_debug();
3013 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
Avi Kivity37e29d92007-02-20 14:07:37 +02003014 mntput(kvmfs_mnt);
3015 unregister_filesystem(&kvm_fs_type);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003016}
3017
3018module_init(kvm_init)
3019module_exit(kvm_exit)
3020
3021EXPORT_SYMBOL_GPL(kvm_init_arch);
3022EXPORT_SYMBOL_GPL(kvm_exit_arch);