KVM: Add mmu cache clear function
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / kvm / kvm_main.c
CommitLineData
6aa8b732
AK
1/*
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>
e9cdb1e3 23#include <linux/magic.h>
6aa8b732
AK
24#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>
59ae6c6b 38#include <linux/sysdev.h>
774c47f1 39#include <linux/cpu.h>
f17abe9a 40#include <linux/file.h>
37e29d90
AK
41#include <linux/fs.h>
42#include <linux/mount.h>
6aa8b732
AK
43
44#include "x86_emulate.h"
45#include "segment_descriptor.h"
46
47MODULE_AUTHOR("Qumranet");
48MODULE_LICENSE("GPL");
49
133de902
AK
50static DEFINE_SPINLOCK(kvm_lock);
51static LIST_HEAD(vm_list);
52
6aa8b732
AK
53struct 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 },
c1150d8c
DL
70 { "irq_window", &kvm_stat.irq_window_exits },
71 { "halt_exits", &kvm_stat.halt_exits },
72 { "request_irq", &kvm_stat.request_irq_exits },
6aa8b732 73 { "irq_exits", &kvm_stat.irq_exits },
8b6d44c7 74 { NULL, NULL }
6aa8b732
AK
75};
76
77static struct dentry *debugfs_dir;
78
37e29d90
AK
79struct vfsmount *kvmfs_mnt;
80
6aa8b732
AK
81#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
05b3e0c2 89#ifdef CONFIG_X86_64
6aa8b732
AK
90// 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
bccf2150
AK
99static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
100 unsigned long arg);
101
f17abe9a
AK
102static 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
6aa8b732
AK
151unsigned 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);
05b3e0c2 173#ifdef CONFIG_X86_64
6aa8b732
AK
174 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
5aacf0ca
JM
182static inline int valid_vcpu(int n)
183{
184 return likely(n >= 0 && n < KVM_MAX_VCPUS);
185}
186
d27d4aca
AK
187int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
188 void *dest)
6aa8b732
AK
189{
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
d27d4aca
AK
220int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
221 void *data)
6aa8b732
AK
222{
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;
ab51a434 231 gfn_t gfn;
6aa8b732
AK
232
233 paddr = gva_to_hpa(vcpu, addr);
234
235 if (is_error_hpa(paddr))
236 break;
237
ab51a434
UL
238 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
239 mark_page_dirty(vcpu->kvm, gfn);
6aa8b732
AK
240 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
bccf2150
AK
255/*
256 * Switches to specified vcpu, until a matching vcpu_put()
257 */
258static void vcpu_load(struct kvm_vcpu *vcpu)
6aa8b732 259{
bccf2150
AK
260 mutex_lock(&vcpu->mutex);
261 kvm_arch_ops->vcpu_load(vcpu);
6aa8b732
AK
262}
263
264/*
bccf2150
AK
265 * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
266 * if the slot is not populated.
6aa8b732 267 */
bccf2150 268static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot)
6aa8b732 269{
bccf2150 270 struct kvm_vcpu *vcpu = &kvm->vcpus[slot];
6aa8b732
AK
271
272 mutex_lock(&vcpu->mutex);
bccf2150 273 if (!vcpu->vmcs) {
6aa8b732 274 mutex_unlock(&vcpu->mutex);
8b6d44c7 275 return NULL;
6aa8b732 276 }
bccf2150
AK
277 kvm_arch_ops->vcpu_load(vcpu);
278 return vcpu;
6aa8b732
AK
279}
280
281static void vcpu_put(struct kvm_vcpu *vcpu)
282{
283 kvm_arch_ops->vcpu_put(vcpu);
6aa8b732
AK
284 mutex_unlock(&vcpu->mutex);
285}
286
f17abe9a 287static struct kvm *kvm_create_vm(void)
6aa8b732
AK
288{
289 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
290 int i;
291
292 if (!kvm)
f17abe9a 293 return ERR_PTR(-ENOMEM);
6aa8b732
AK
294
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);
133de902 301 vcpu->cpu = -1;
86a2b42e 302 vcpu->kvm = kvm;
6aa8b732
AK
303 vcpu->mmu.root_hpa = INVALID_PAGE;
304 INIT_LIST_HEAD(&vcpu->free_pages);
133de902
AK
305 spin_lock(&kvm_lock);
306 list_add(&kvm->vm_list, &vm_list);
307 spin_unlock(&kvm_lock);
6aa8b732 308 }
f17abe9a
AK
309 return kvm;
310}
311
312static int kvm_dev_open(struct inode *inode, struct file *filp)
313{
6aa8b732
AK
314 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)
55a54f79
AK
328 if (free->phys_mem[i])
329 __free_page(free->phys_mem[i]);
6aa8b732
AK
330 vfree(free->phys_mem);
331 }
332
333 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
334 vfree(free->dirty_bitmap);
335
8b6d44c7 336 free->phys_mem = NULL;
6aa8b732 337 free->npages = 0;
8b6d44c7 338 free->dirty_bitmap = NULL;
6aa8b732
AK
339}
340
341static void kvm_free_physmem(struct kvm *kvm)
342{
343 int i;
344
345 for (i = 0; i < kvm->nmemslots; ++i)
8b6d44c7 346 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
6aa8b732
AK
347}
348
039576c0
AK
349static 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
6aa8b732
AK
360static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
361{
bccf2150 362 if (!vcpu->vmcs)
1e8ba6fb
IM
363 return;
364
bccf2150 365 vcpu_load(vcpu);
6aa8b732 366 kvm_mmu_destroy(vcpu);
08438475 367 vcpu_put(vcpu);
9ede74e0 368 kvm_arch_ops->vcpu_free(vcpu);
9a2bb7f4
AK
369 free_page((unsigned long)vcpu->run);
370 vcpu->run = NULL;
039576c0
AK
371 free_page((unsigned long)vcpu->pio_data);
372 vcpu->pio_data = NULL;
373 free_pio_guest_pages(vcpu);
6aa8b732
AK
374}
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{
f17abe9a
AK
386 return 0;
387}
6aa8b732 388
f17abe9a
AK
389static void kvm_destroy_vm(struct kvm *kvm)
390{
133de902
AK
391 spin_lock(&kvm_lock);
392 list_del(&kvm->vm_list);
393 spin_unlock(&kvm_lock);
6aa8b732
AK
394 kvm_free_vcpus(kvm);
395 kvm_free_physmem(kvm);
396 kfree(kvm);
f17abe9a
AK
397}
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);
6aa8b732
AK
404 return 0;
405}
406
407static void inject_gp(struct kvm_vcpu *vcpu)
408{
409 kvm_arch_ops->inject_gp(vcpu, 0);
410}
411
1342d353
AK
412/*
413 * Load the pae pdptrs. Return true is they are all valid.
414 */
415static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
6aa8b732
AK
416{
417 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
1342d353 418 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
6aa8b732
AK
419 int i;
420 u64 pdpte;
421 u64 *pdpt;
1342d353 422 int ret;
6aa8b732
AK
423 struct kvm_memory_slot *memslot;
424
425 spin_lock(&vcpu->kvm->lock);
426 memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn);
427 /* FIXME: !memslot - emulate? 0xff? */
428 pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0);
429
1342d353 430 ret = 1;
6aa8b732
AK
431 for (i = 0; i < 4; ++i) {
432 pdpte = pdpt[offset + i];
1342d353
AK
433 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
434 ret = 0;
435 goto out;
436 }
6aa8b732
AK
437 }
438
1342d353
AK
439 for (i = 0; i < 4; ++i)
440 vcpu->pdptrs[i] = pdpt[offset + i];
441
442out:
6aa8b732
AK
443 kunmap_atomic(pdpt, KM_USER0);
444 spin_unlock(&vcpu->kvm->lock);
445
1342d353 446 return ret;
6aa8b732
AK
447}
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)) {
05b3e0c2 472#ifdef CONFIG_X86_64
6aa8b732
AK
473 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
1342d353 492 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
6aa8b732
AK
493 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{
399badf3 513 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
6aa8b732
AK
514 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
a9058ecd 526 if (is_long_mode(vcpu)) {
6aa8b732
AK
527 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)
1342d353 534 && !load_pdptrs(vcpu, vcpu->cr3)) {
6aa8b732
AK
535 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{
a9058ecd 553 if (is_long_mode(vcpu)) {
d27d4aca 554 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
6aa8b732
AK
555 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) &&
1342d353 566 !load_pdptrs(vcpu, cr3)) {
6aa8b732
AK
567 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);
d21225ee
IM
576 /*
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);
6aa8b732
AK
589 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
02b27c1f
UL
630static 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
6aa8b732
AK
637/*
638 * 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 */
2c6f5df9
AK
643static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
644 struct kvm_memory_region *mem)
6aa8b732
AK
645{
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)
8b6d44c7 706 new.phys_mem = NULL;
6aa8b732
AK
707
708 /* Free page dirty bitmap if unneeded */
709 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
8b6d44c7 710 new.dirty_bitmap = NULL;
6aa8b732
AK
711
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;
5972e953 727 set_page_private(new.phys_mem[i],0);
6aa8b732
AK
728 }
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
bccf2150 764 vcpu = vcpu_load_slot(kvm, i);
6aa8b732
AK
765 if (!vcpu)
766 continue;
ff990d59
UL
767 if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
768 do_remove_write_access(vcpu, mem->slot);
6aa8b732
AK
769 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 */
2c6f5df9
AK
787static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
788 struct kvm_dirty_log *log)
6aa8b732
AK
789{
790 struct kvm_memory_slot *memslot;
791 int r, i;
792 int n;
714b93da 793 int cleared;
6aa8b732
AK
794 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
cd1a4a98 813 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
6aa8b732 814
cd1a4a98 815 for (i = 0; !any && i < n/sizeof(long); ++i)
6aa8b732
AK
816 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
6aa8b732 822 if (any) {
714b93da 823 cleared = 0;
6aa8b732 824 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
bccf2150 825 struct kvm_vcpu *vcpu;
6aa8b732 826
bccf2150 827 vcpu = vcpu_load_slot(kvm, i);
6aa8b732
AK
828 if (!vcpu)
829 continue;
714b93da
AK
830 if (!cleared) {
831 do_remove_write_access(vcpu, log->slot);
832 memset(memslot->dirty_bitmap, 0, n);
833 cleared = 1;
834 }
6aa8b732
AK
835 kvm_arch_ops->tlb_flush(vcpu);
836 vcpu_put(vcpu);
837 }
838 }
839
840 r = 0;
841
842out:
843 spin_lock(&kvm->lock);
844 --kvm->busy;
845 spin_unlock(&kvm->lock);
846 return r;
847}
848
849struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
850{
851 int i;
852
853 for (i = 0; i < kvm->nmemslots; ++i) {
854 struct kvm_memory_slot *memslot = &kvm->memslots[i];
855
856 if (gfn >= memslot->base_gfn
857 && gfn < memslot->base_gfn + memslot->npages)
858 return memslot;
859 }
8b6d44c7 860 return NULL;
6aa8b732
AK
861}
862EXPORT_SYMBOL_GPL(gfn_to_memslot);
863
864void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
865{
866 int i;
8b6d44c7 867 struct kvm_memory_slot *memslot = NULL;
6aa8b732
AK
868 unsigned long rel_gfn;
869
870 for (i = 0; i < kvm->nmemslots; ++i) {
871 memslot = &kvm->memslots[i];
872
873 if (gfn >= memslot->base_gfn
874 && gfn < memslot->base_gfn + memslot->npages) {
875
876 if (!memslot || !memslot->dirty_bitmap)
877 return;
878
879 rel_gfn = gfn - memslot->base_gfn;
880
881 /* avoid RMW */
882 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
883 set_bit(rel_gfn, memslot->dirty_bitmap);
884 return;
885 }
886 }
887}
888
889static int emulator_read_std(unsigned long addr,
890 unsigned long *val,
891 unsigned int bytes,
892 struct x86_emulate_ctxt *ctxt)
893{
894 struct kvm_vcpu *vcpu = ctxt->vcpu;
895 void *data = val;
896
897 while (bytes) {
898 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
899 unsigned offset = addr & (PAGE_SIZE-1);
900 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
901 unsigned long pfn;
902 struct kvm_memory_slot *memslot;
903 void *page;
904
905 if (gpa == UNMAPPED_GVA)
906 return X86EMUL_PROPAGATE_FAULT;
907 pfn = gpa >> PAGE_SHIFT;
908 memslot = gfn_to_memslot(vcpu->kvm, pfn);
909 if (!memslot)
910 return X86EMUL_UNHANDLEABLE;
911 page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0);
912
913 memcpy(data, page + offset, tocopy);
914
915 kunmap_atomic(page, KM_USER0);
916
917 bytes -= tocopy;
918 data += tocopy;
919 addr += tocopy;
920 }
921
922 return X86EMUL_CONTINUE;
923}
924
925static int emulator_write_std(unsigned long addr,
926 unsigned long val,
927 unsigned int bytes,
928 struct x86_emulate_ctxt *ctxt)
929{
930 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
931 addr, bytes);
932 return X86EMUL_UNHANDLEABLE;
933}
934
935static int emulator_read_emulated(unsigned long addr,
936 unsigned long *val,
937 unsigned int bytes,
938 struct x86_emulate_ctxt *ctxt)
939{
940 struct kvm_vcpu *vcpu = ctxt->vcpu;
941
942 if (vcpu->mmio_read_completed) {
943 memcpy(val, vcpu->mmio_data, bytes);
944 vcpu->mmio_read_completed = 0;
945 return X86EMUL_CONTINUE;
946 } else if (emulator_read_std(addr, val, bytes, ctxt)
947 == X86EMUL_CONTINUE)
948 return X86EMUL_CONTINUE;
949 else {
950 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
d27d4aca 951
6aa8b732 952 if (gpa == UNMAPPED_GVA)
d27d4aca 953 return X86EMUL_PROPAGATE_FAULT;
6aa8b732
AK
954 vcpu->mmio_needed = 1;
955 vcpu->mmio_phys_addr = gpa;
956 vcpu->mmio_size = bytes;
957 vcpu->mmio_is_write = 0;
958
959 return X86EMUL_UNHANDLEABLE;
960 }
961}
962
da4a00f0
AK
963static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
964 unsigned long val, int bytes)
965{
966 struct kvm_memory_slot *m;
967 struct page *page;
968 void *virt;
969
970 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
971 return 0;
972 m = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
973 if (!m)
974 return 0;
975 page = gfn_to_page(m, gpa >> PAGE_SHIFT);
976 kvm_mmu_pre_write(vcpu, gpa, bytes);
ab51a434 977 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
da4a00f0
AK
978 virt = kmap_atomic(page, KM_USER0);
979 memcpy(virt + offset_in_page(gpa), &val, bytes);
980 kunmap_atomic(virt, KM_USER0);
981 kvm_mmu_post_write(vcpu, gpa, bytes);
982 return 1;
983}
984
6aa8b732
AK
985static int emulator_write_emulated(unsigned long addr,
986 unsigned long val,
987 unsigned int bytes,
988 struct x86_emulate_ctxt *ctxt)
989{
990 struct kvm_vcpu *vcpu = ctxt->vcpu;
991 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
992
993 if (gpa == UNMAPPED_GVA)
994 return X86EMUL_PROPAGATE_FAULT;
995
da4a00f0
AK
996 if (emulator_write_phys(vcpu, gpa, val, bytes))
997 return X86EMUL_CONTINUE;
998
6aa8b732
AK
999 vcpu->mmio_needed = 1;
1000 vcpu->mmio_phys_addr = gpa;
1001 vcpu->mmio_size = bytes;
1002 vcpu->mmio_is_write = 1;
1003 memcpy(vcpu->mmio_data, &val, bytes);
1004
1005 return X86EMUL_CONTINUE;
1006}
1007
1008static int emulator_cmpxchg_emulated(unsigned long addr,
1009 unsigned long old,
1010 unsigned long new,
1011 unsigned int bytes,
1012 struct x86_emulate_ctxt *ctxt)
1013{
1014 static int reported;
1015
1016 if (!reported) {
1017 reported = 1;
1018 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1019 }
1020 return emulator_write_emulated(addr, new, bytes, ctxt);
1021}
1022
32b35627
AK
1023#ifdef CONFIG_X86_32
1024
1025static int emulator_cmpxchg8b_emulated(unsigned long addr,
1026 unsigned long old_lo,
1027 unsigned long old_hi,
1028 unsigned long new_lo,
1029 unsigned long new_hi,
1030 struct x86_emulate_ctxt *ctxt)
1031{
1032 static int reported;
1033 int r;
1034
1035 if (!reported) {
1036 reported = 1;
1037 printk(KERN_WARNING "kvm: emulating exchange8b as write\n");
1038 }
1039 r = emulator_write_emulated(addr, new_lo, 4, ctxt);
1040 if (r != X86EMUL_CONTINUE)
1041 return r;
1042 return emulator_write_emulated(addr+4, new_hi, 4, ctxt);
1043}
1044
1045#endif
1046
6aa8b732
AK
1047static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1048{
1049 return kvm_arch_ops->get_segment_base(vcpu, seg);
1050}
1051
1052int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1053{
6aa8b732
AK
1054 return X86EMUL_CONTINUE;
1055}
1056
1057int emulate_clts(struct kvm_vcpu *vcpu)
1058{
399badf3 1059 unsigned long cr0;
6aa8b732 1060
399badf3
AK
1061 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1062 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
6aa8b732
AK
1063 kvm_arch_ops->set_cr0(vcpu, cr0);
1064 return X86EMUL_CONTINUE;
1065}
1066
1067int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1068{
1069 struct kvm_vcpu *vcpu = ctxt->vcpu;
1070
1071 switch (dr) {
1072 case 0 ... 3:
1073 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1074 return X86EMUL_CONTINUE;
1075 default:
1076 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1077 __FUNCTION__, dr);
1078 return X86EMUL_UNHANDLEABLE;
1079 }
1080}
1081
1082int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1083{
1084 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1085 int exception;
1086
1087 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1088 if (exception) {
1089 /* FIXME: better handling */
1090 return X86EMUL_UNHANDLEABLE;
1091 }
1092 return X86EMUL_CONTINUE;
1093}
1094
1095static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1096{
1097 static int reported;
1098 u8 opcodes[4];
1099 unsigned long rip = ctxt->vcpu->rip;
1100 unsigned long rip_linear;
1101
1102 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1103
1104 if (reported)
1105 return;
1106
1107 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1108
1109 printk(KERN_ERR "emulation failed but !mmio_needed?"
1110 " rip %lx %02x %02x %02x %02x\n",
1111 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1112 reported = 1;
1113}
1114
1115struct x86_emulate_ops emulate_ops = {
1116 .read_std = emulator_read_std,
1117 .write_std = emulator_write_std,
1118 .read_emulated = emulator_read_emulated,
1119 .write_emulated = emulator_write_emulated,
1120 .cmpxchg_emulated = emulator_cmpxchg_emulated,
32b35627
AK
1121#ifdef CONFIG_X86_32
1122 .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated,
1123#endif
6aa8b732
AK
1124};
1125
1126int emulate_instruction(struct kvm_vcpu *vcpu,
1127 struct kvm_run *run,
1128 unsigned long cr2,
1129 u16 error_code)
1130{
1131 struct x86_emulate_ctxt emulate_ctxt;
1132 int r;
1133 int cs_db, cs_l;
1134
1135 kvm_arch_ops->cache_regs(vcpu);
1136
1137 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1138
1139 emulate_ctxt.vcpu = vcpu;
1140 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1141 emulate_ctxt.cr2 = cr2;
1142 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1143 ? X86EMUL_MODE_REAL : cs_l
1144 ? X86EMUL_MODE_PROT64 : cs_db
1145 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1146
1147 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1148 emulate_ctxt.cs_base = 0;
1149 emulate_ctxt.ds_base = 0;
1150 emulate_ctxt.es_base = 0;
1151 emulate_ctxt.ss_base = 0;
1152 } else {
1153 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1154 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1155 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1156 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1157 }
1158
1159 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1160 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1161
1162 vcpu->mmio_is_write = 0;
1163 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1164
1165 if ((r || vcpu->mmio_is_write) && run) {
1166 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1167 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1168 run->mmio.len = vcpu->mmio_size;
1169 run->mmio.is_write = vcpu->mmio_is_write;
1170 }
1171
1172 if (r) {
a436036b
AK
1173 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1174 return EMULATE_DONE;
6aa8b732
AK
1175 if (!vcpu->mmio_needed) {
1176 report_emulation_failure(&emulate_ctxt);
1177 return EMULATE_FAIL;
1178 }
1179 return EMULATE_DO_MMIO;
1180 }
1181
1182 kvm_arch_ops->decache_regs(vcpu);
1183 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1184
1185 if (vcpu->mmio_is_write)
1186 return EMULATE_DO_MMIO;
1187
1188 return EMULATE_DONE;
1189}
1190EXPORT_SYMBOL_GPL(emulate_instruction);
1191
270fd9b9
AK
1192int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1193{
1194 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1195
9b22bf57 1196 kvm_arch_ops->cache_regs(vcpu);
270fd9b9
AK
1197 ret = -KVM_EINVAL;
1198#ifdef CONFIG_X86_64
1199 if (is_long_mode(vcpu)) {
1200 nr = vcpu->regs[VCPU_REGS_RAX];
1201 a0 = vcpu->regs[VCPU_REGS_RDI];
1202 a1 = vcpu->regs[VCPU_REGS_RSI];
1203 a2 = vcpu->regs[VCPU_REGS_RDX];
1204 a3 = vcpu->regs[VCPU_REGS_RCX];
1205 a4 = vcpu->regs[VCPU_REGS_R8];
1206 a5 = vcpu->regs[VCPU_REGS_R9];
1207 } else
1208#endif
1209 {
1210 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1211 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1212 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1213 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1214 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1215 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1216 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1217 }
1218 switch (nr) {
1219 default:
b4e63f56
AK
1220 run->hypercall.args[0] = a0;
1221 run->hypercall.args[1] = a1;
1222 run->hypercall.args[2] = a2;
1223 run->hypercall.args[3] = a3;
1224 run->hypercall.args[4] = a4;
1225 run->hypercall.args[5] = a5;
1226 run->hypercall.ret = ret;
1227 run->hypercall.longmode = is_long_mode(vcpu);
1228 kvm_arch_ops->decache_regs(vcpu);
1229 return 0;
270fd9b9
AK
1230 }
1231 vcpu->regs[VCPU_REGS_RAX] = ret;
9b22bf57 1232 kvm_arch_ops->decache_regs(vcpu);
270fd9b9
AK
1233 return 1;
1234}
1235EXPORT_SYMBOL_GPL(kvm_hypercall);
1236
6aa8b732
AK
1237static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1238{
1239 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1240}
1241
1242void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1243{
1244 struct descriptor_table dt = { limit, base };
1245
1246 kvm_arch_ops->set_gdt(vcpu, &dt);
1247}
1248
1249void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1250{
1251 struct descriptor_table dt = { limit, base };
1252
1253 kvm_arch_ops->set_idt(vcpu, &dt);
1254}
1255
1256void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1257 unsigned long *rflags)
1258{
1259 lmsw(vcpu, msw);
1260 *rflags = kvm_arch_ops->get_rflags(vcpu);
1261}
1262
1263unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1264{
399badf3 1265 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
6aa8b732
AK
1266 switch (cr) {
1267 case 0:
1268 return vcpu->cr0;
1269 case 2:
1270 return vcpu->cr2;
1271 case 3:
1272 return vcpu->cr3;
1273 case 4:
1274 return vcpu->cr4;
1275 default:
1276 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1277 return 0;
1278 }
1279}
1280
1281void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1282 unsigned long *rflags)
1283{
1284 switch (cr) {
1285 case 0:
1286 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1287 *rflags = kvm_arch_ops->get_rflags(vcpu);
1288 break;
1289 case 2:
1290 vcpu->cr2 = val;
1291 break;
1292 case 3:
1293 set_cr3(vcpu, val);
1294 break;
1295 case 4:
1296 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1297 break;
1298 default:
1299 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1300 }
1301}
1302
102d8325
IM
1303/*
1304 * Register the para guest with the host:
1305 */
1306static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1307{
1308 struct kvm_vcpu_para_state *para_state;
1309 hpa_t para_state_hpa, hypercall_hpa;
1310 struct page *para_state_page;
1311 unsigned char *hypercall;
1312 gpa_t hypercall_gpa;
1313
1314 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1315 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1316
1317 /*
1318 * Needs to be page aligned:
1319 */
1320 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1321 goto err_gp;
1322
1323 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1324 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1325 if (is_error_hpa(para_state_hpa))
1326 goto err_gp;
1327
ab51a434 1328 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
102d8325
IM
1329 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1330 para_state = kmap_atomic(para_state_page, KM_USER0);
1331
1332 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1333 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1334
1335 para_state->host_version = KVM_PARA_API_VERSION;
1336 /*
1337 * We cannot support guests that try to register themselves
1338 * with a newer API version than the host supports:
1339 */
1340 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1341 para_state->ret = -KVM_EINVAL;
1342 goto err_kunmap_skip;
1343 }
1344
1345 hypercall_gpa = para_state->hypercall_gpa;
1346 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1347 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1348 if (is_error_hpa(hypercall_hpa)) {
1349 para_state->ret = -KVM_EINVAL;
1350 goto err_kunmap_skip;
1351 }
1352
1353 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1354 vcpu->para_state_page = para_state_page;
1355 vcpu->para_state_gpa = para_state_gpa;
1356 vcpu->hypercall_gpa = hypercall_gpa;
1357
ab51a434 1358 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
102d8325
IM
1359 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1360 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1361 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1362 kunmap_atomic(hypercall, KM_USER1);
1363
1364 para_state->ret = 0;
1365err_kunmap_skip:
1366 kunmap_atomic(para_state, KM_USER0);
1367 return 0;
1368err_gp:
1369 return 1;
1370}
1371
3bab1f5d
AK
1372int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1373{
1374 u64 data;
1375
1376 switch (msr) {
1377 case 0xc0010010: /* SYSCFG */
1378 case 0xc0010015: /* HWCR */
1379 case MSR_IA32_PLATFORM_ID:
1380 case MSR_IA32_P5_MC_ADDR:
1381 case MSR_IA32_P5_MC_TYPE:
1382 case MSR_IA32_MC0_CTL:
1383 case MSR_IA32_MCG_STATUS:
1384 case MSR_IA32_MCG_CAP:
1385 case MSR_IA32_MC0_MISC:
1386 case MSR_IA32_MC0_MISC+4:
1387 case MSR_IA32_MC0_MISC+8:
1388 case MSR_IA32_MC0_MISC+12:
1389 case MSR_IA32_MC0_MISC+16:
1390 case MSR_IA32_UCODE_REV:
a8d13ea2 1391 case MSR_IA32_PERF_STATUS:
3bab1f5d
AK
1392 /* MTRR registers */
1393 case 0xfe:
1394 case 0x200 ... 0x2ff:
1395 data = 0;
1396 break;
a8d13ea2
AK
1397 case 0xcd: /* fsb frequency */
1398 data = 3;
1399 break;
3bab1f5d
AK
1400 case MSR_IA32_APICBASE:
1401 data = vcpu->apic_base;
1402 break;
6f00e68f
AK
1403 case MSR_IA32_MISC_ENABLE:
1404 data = vcpu->ia32_misc_enable_msr;
1405 break;
3bab1f5d
AK
1406#ifdef CONFIG_X86_64
1407 case MSR_EFER:
1408 data = vcpu->shadow_efer;
1409 break;
1410#endif
1411 default:
1412 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1413 return 1;
1414 }
1415 *pdata = data;
1416 return 0;
1417}
1418EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1419
6aa8b732
AK
1420/*
1421 * Reads an msr value (of 'msr_index') into 'pdata'.
1422 * Returns 0 on success, non-0 otherwise.
1423 * Assumes vcpu_load() was already called.
1424 */
1425static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1426{
1427 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1428}
1429
05b3e0c2 1430#ifdef CONFIG_X86_64
6aa8b732 1431
3bab1f5d 1432static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
6aa8b732 1433{
6aa8b732
AK
1434 if (efer & EFER_RESERVED_BITS) {
1435 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1436 efer);
1437 inject_gp(vcpu);
1438 return;
1439 }
1440
1441 if (is_paging(vcpu)
1442 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1443 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1444 inject_gp(vcpu);
1445 return;
1446 }
1447
7725f0ba
AK
1448 kvm_arch_ops->set_efer(vcpu, efer);
1449
6aa8b732
AK
1450 efer &= ~EFER_LMA;
1451 efer |= vcpu->shadow_efer & EFER_LMA;
1452
1453 vcpu->shadow_efer = efer;
6aa8b732 1454}
6aa8b732
AK
1455
1456#endif
1457
3bab1f5d
AK
1458int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1459{
1460 switch (msr) {
1461#ifdef CONFIG_X86_64
1462 case MSR_EFER:
1463 set_efer(vcpu, data);
1464 break;
1465#endif
1466 case MSR_IA32_MC0_STATUS:
1467 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1468 __FUNCTION__, data);
1469 break;
0e5bf0d0
SK
1470 case MSR_IA32_MCG_STATUS:
1471 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1472 __FUNCTION__, data);
1473 break;
3bab1f5d
AK
1474 case MSR_IA32_UCODE_REV:
1475 case MSR_IA32_UCODE_WRITE:
1476 case 0x200 ... 0x2ff: /* MTRRs */
1477 break;
1478 case MSR_IA32_APICBASE:
1479 vcpu->apic_base = data;
1480 break;
6f00e68f
AK
1481 case MSR_IA32_MISC_ENABLE:
1482 vcpu->ia32_misc_enable_msr = data;
1483 break;
102d8325
IM
1484 /*
1485 * This is the 'probe whether the host is KVM' logic:
1486 */
1487 case MSR_KVM_API_MAGIC:
1488 return vcpu_register_para(vcpu, data);
1489
3bab1f5d
AK
1490 default:
1491 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1492 return 1;
1493 }
1494 return 0;
1495}
1496EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1497
6aa8b732
AK
1498/*
1499 * Writes msr value into into the appropriate "register".
1500 * Returns 0 on success, non-0 otherwise.
1501 * Assumes vcpu_load() was already called.
1502 */
1503static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1504{
1505 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1506}
1507
1508void kvm_resched(struct kvm_vcpu *vcpu)
1509{
1510 vcpu_put(vcpu);
1511 cond_resched();
bccf2150 1512 vcpu_load(vcpu);
6aa8b732
AK
1513}
1514EXPORT_SYMBOL_GPL(kvm_resched);
1515
1516void load_msrs(struct vmx_msr_entry *e, int n)
1517{
1518 int i;
1519
1520 for (i = 0; i < n; ++i)
1521 wrmsrl(e[i].index, e[i].data);
1522}
1523EXPORT_SYMBOL_GPL(load_msrs);
1524
1525void save_msrs(struct vmx_msr_entry *e, int n)
1526{
1527 int i;
1528
1529 for (i = 0; i < n; ++i)
1530 rdmsrl(e[i].index, e[i].data);
1531}
1532EXPORT_SYMBOL_GPL(save_msrs);
1533
06465c5a
AK
1534void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1535{
1536 int i;
1537 u32 function;
1538 struct kvm_cpuid_entry *e, *best;
1539
1540 kvm_arch_ops->cache_regs(vcpu);
1541 function = vcpu->regs[VCPU_REGS_RAX];
1542 vcpu->regs[VCPU_REGS_RAX] = 0;
1543 vcpu->regs[VCPU_REGS_RBX] = 0;
1544 vcpu->regs[VCPU_REGS_RCX] = 0;
1545 vcpu->regs[VCPU_REGS_RDX] = 0;
1546 best = NULL;
1547 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1548 e = &vcpu->cpuid_entries[i];
1549 if (e->function == function) {
1550 best = e;
1551 break;
1552 }
1553 /*
1554 * Both basic or both extended?
1555 */
1556 if (((e->function ^ function) & 0x80000000) == 0)
1557 if (!best || e->function > best->function)
1558 best = e;
1559 }
1560 if (best) {
1561 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1562 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1563 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1564 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1565 }
1566 kvm_arch_ops->decache_regs(vcpu);
1567 kvm_arch_ops->skip_emulated_instruction(vcpu);
1568}
1569EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1570
039576c0 1571static int pio_copy_data(struct kvm_vcpu *vcpu)
46fc1477 1572{
039576c0
AK
1573 void *p = vcpu->pio_data;
1574 void *q;
1575 unsigned bytes;
1576 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1577
1578 kvm_arch_ops->vcpu_put(vcpu);
1579 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1580 PAGE_KERNEL);
1581 if (!q) {
1582 kvm_arch_ops->vcpu_load(vcpu);
1583 free_pio_guest_pages(vcpu);
1584 return -ENOMEM;
1585 }
1586 q += vcpu->pio.guest_page_offset;
1587 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1588 if (vcpu->pio.in)
1589 memcpy(q, p, bytes);
1590 else
1591 memcpy(p, q, bytes);
1592 q -= vcpu->pio.guest_page_offset;
1593 vunmap(q);
1594 kvm_arch_ops->vcpu_load(vcpu);
1595 free_pio_guest_pages(vcpu);
1596 return 0;
1597}
1598
1599static int complete_pio(struct kvm_vcpu *vcpu)
1600{
1601 struct kvm_pio_request *io = &vcpu->pio;
46fc1477 1602 long delta;
039576c0 1603 int r;
46fc1477
AK
1604
1605 kvm_arch_ops->cache_regs(vcpu);
1606
1607 if (!io->string) {
039576c0
AK
1608 if (io->in)
1609 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
46fc1477
AK
1610 io->size);
1611 } else {
039576c0
AK
1612 if (io->in) {
1613 r = pio_copy_data(vcpu);
1614 if (r) {
1615 kvm_arch_ops->cache_regs(vcpu);
1616 return r;
1617 }
1618 }
1619
46fc1477
AK
1620 delta = 1;
1621 if (io->rep) {
039576c0 1622 delta *= io->cur_count;
46fc1477
AK
1623 /*
1624 * The size of the register should really depend on
1625 * current address size.
1626 */
1627 vcpu->regs[VCPU_REGS_RCX] -= delta;
1628 }
039576c0 1629 if (io->down)
46fc1477
AK
1630 delta = -delta;
1631 delta *= io->size;
039576c0 1632 if (io->in)
46fc1477
AK
1633 vcpu->regs[VCPU_REGS_RDI] += delta;
1634 else
1635 vcpu->regs[VCPU_REGS_RSI] += delta;
1636 }
1637
46fc1477
AK
1638 vcpu->run->io_completed = 0;
1639
1640 kvm_arch_ops->decache_regs(vcpu);
1641
039576c0
AK
1642 io->count -= io->cur_count;
1643 io->cur_count = 0;
1644
1645 if (!io->count)
1646 kvm_arch_ops->skip_emulated_instruction(vcpu);
1647 return 0;
46fc1477
AK
1648}
1649
039576c0
AK
1650int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1651 int size, unsigned long count, int string, int down,
1652 gva_t address, int rep, unsigned port)
1653{
1654 unsigned now, in_page;
1655 int i;
1656 int nr_pages = 1;
1657 struct page *page;
1658
1659 vcpu->run->exit_reason = KVM_EXIT_IO;
1660 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1661 vcpu->run->io.size = size;
1662 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1663 vcpu->run->io.count = count;
1664 vcpu->run->io.port = port;
1665 vcpu->pio.count = count;
1666 vcpu->pio.cur_count = count;
1667 vcpu->pio.size = size;
1668 vcpu->pio.in = in;
1669 vcpu->pio.string = string;
1670 vcpu->pio.down = down;
1671 vcpu->pio.guest_page_offset = offset_in_page(address);
1672 vcpu->pio.rep = rep;
1673
1674 if (!string) {
1675 kvm_arch_ops->cache_regs(vcpu);
1676 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1677 kvm_arch_ops->decache_regs(vcpu);
1678 return 0;
1679 }
1680
1681 if (!count) {
1682 kvm_arch_ops->skip_emulated_instruction(vcpu);
1683 return 1;
1684 }
1685
1686 now = min(count, PAGE_SIZE / size);
1687
1688 if (!down)
1689 in_page = PAGE_SIZE - offset_in_page(address);
1690 else
1691 in_page = offset_in_page(address) + size;
1692 now = min(count, (unsigned long)in_page / size);
1693 if (!now) {
1694 /*
1695 * String I/O straddles page boundary. Pin two guest pages
1696 * so that we satisfy atomicity constraints. Do just one
1697 * transaction to avoid complexity.
1698 */
1699 nr_pages = 2;
1700 now = 1;
1701 }
1702 if (down) {
1703 /*
1704 * String I/O in reverse. Yuck. Kill the guest, fix later.
1705 */
1706 printk(KERN_ERR "kvm: guest string pio down\n");
1707 inject_gp(vcpu);
1708 return 1;
1709 }
1710 vcpu->run->io.count = now;
1711 vcpu->pio.cur_count = now;
1712
1713 for (i = 0; i < nr_pages; ++i) {
1714 spin_lock(&vcpu->kvm->lock);
1715 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1716 if (page)
1717 get_page(page);
1718 vcpu->pio.guest_pages[i] = page;
1719 spin_unlock(&vcpu->kvm->lock);
1720 if (!page) {
1721 inject_gp(vcpu);
1722 free_pio_guest_pages(vcpu);
1723 return 1;
1724 }
1725 }
1726
1727 if (!vcpu->pio.in)
1728 return pio_copy_data(vcpu);
1729 return 0;
1730}
1731EXPORT_SYMBOL_GPL(kvm_setup_pio);
1732
bccf2150 1733static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
6aa8b732 1734{
6aa8b732 1735 int r;
1961d276 1736 sigset_t sigsaved;
6aa8b732 1737
bccf2150 1738 vcpu_load(vcpu);
6aa8b732 1739
1961d276
AK
1740 if (vcpu->sigset_active)
1741 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1742
54810342
DL
1743 /* re-sync apic's tpr */
1744 vcpu->cr8 = kvm_run->cr8;
1745
46fc1477 1746 if (kvm_run->io_completed) {
039576c0
AK
1747 if (vcpu->pio.cur_count) {
1748 r = complete_pio(vcpu);
1749 if (r)
1750 goto out;
1751 } else {
46fc1477
AK
1752 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1753 vcpu->mmio_read_completed = 1;
1754 }
6aa8b732
AK
1755 }
1756
1757 vcpu->mmio_needed = 0;
1758
8eb7d334 1759 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
b4e63f56
AK
1760 kvm_arch_ops->cache_regs(vcpu);
1761 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1762 kvm_arch_ops->decache_regs(vcpu);
1763 }
1764
6aa8b732
AK
1765 r = kvm_arch_ops->run(vcpu, kvm_run);
1766
039576c0 1767out:
1961d276
AK
1768 if (vcpu->sigset_active)
1769 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1770
6aa8b732
AK
1771 vcpu_put(vcpu);
1772 return r;
1773}
1774
bccf2150
AK
1775static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1776 struct kvm_regs *regs)
6aa8b732 1777{
bccf2150 1778 vcpu_load(vcpu);
6aa8b732
AK
1779
1780 kvm_arch_ops->cache_regs(vcpu);
1781
1782 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1783 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1784 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1785 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1786 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1787 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1788 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1789 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
05b3e0c2 1790#ifdef CONFIG_X86_64
6aa8b732
AK
1791 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1792 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1793 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1794 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1795 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1796 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1797 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1798 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1799#endif
1800
1801 regs->rip = vcpu->rip;
1802 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1803
1804 /*
1805 * Don't leak debug flags in case they were set for guest debugging
1806 */
1807 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1808 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1809
1810 vcpu_put(vcpu);
1811
1812 return 0;
1813}
1814
bccf2150
AK
1815static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
1816 struct kvm_regs *regs)
6aa8b732 1817{
bccf2150 1818 vcpu_load(vcpu);
6aa8b732
AK
1819
1820 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1821 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1822 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1823 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1824 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1825 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1826 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1827 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
05b3e0c2 1828#ifdef CONFIG_X86_64
6aa8b732
AK
1829 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1830 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1831 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1832 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1833 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1834 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1835 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1836 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1837#endif
1838
1839 vcpu->rip = regs->rip;
1840 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1841
1842 kvm_arch_ops->decache_regs(vcpu);
1843
1844 vcpu_put(vcpu);
1845
1846 return 0;
1847}
1848
1849static void get_segment(struct kvm_vcpu *vcpu,
1850 struct kvm_segment *var, int seg)
1851{
1852 return kvm_arch_ops->get_segment(vcpu, var, seg);
1853}
1854
bccf2150
AK
1855static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1856 struct kvm_sregs *sregs)
6aa8b732 1857{
6aa8b732
AK
1858 struct descriptor_table dt;
1859
bccf2150 1860 vcpu_load(vcpu);
6aa8b732
AK
1861
1862 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1863 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1864 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1865 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1866 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1867 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1868
1869 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1870 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1871
1872 kvm_arch_ops->get_idt(vcpu, &dt);
1873 sregs->idt.limit = dt.limit;
1874 sregs->idt.base = dt.base;
1875 kvm_arch_ops->get_gdt(vcpu, &dt);
1876 sregs->gdt.limit = dt.limit;
1877 sregs->gdt.base = dt.base;
1878
399badf3 1879 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
6aa8b732
AK
1880 sregs->cr0 = vcpu->cr0;
1881 sregs->cr2 = vcpu->cr2;
1882 sregs->cr3 = vcpu->cr3;
1883 sregs->cr4 = vcpu->cr4;
1884 sregs->cr8 = vcpu->cr8;
1885 sregs->efer = vcpu->shadow_efer;
1886 sregs->apic_base = vcpu->apic_base;
1887
1888 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1889 sizeof sregs->interrupt_bitmap);
1890
1891 vcpu_put(vcpu);
1892
1893 return 0;
1894}
1895
1896static void set_segment(struct kvm_vcpu *vcpu,
1897 struct kvm_segment *var, int seg)
1898{
1899 return kvm_arch_ops->set_segment(vcpu, var, seg);
1900}
1901
bccf2150
AK
1902static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1903 struct kvm_sregs *sregs)
6aa8b732 1904{
6aa8b732
AK
1905 int mmu_reset_needed = 0;
1906 int i;
1907 struct descriptor_table dt;
1908
bccf2150 1909 vcpu_load(vcpu);
6aa8b732 1910
6aa8b732
AK
1911 dt.limit = sregs->idt.limit;
1912 dt.base = sregs->idt.base;
1913 kvm_arch_ops->set_idt(vcpu, &dt);
1914 dt.limit = sregs->gdt.limit;
1915 dt.base = sregs->gdt.base;
1916 kvm_arch_ops->set_gdt(vcpu, &dt);
1917
1918 vcpu->cr2 = sregs->cr2;
1919 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
1920 vcpu->cr3 = sregs->cr3;
1921
1922 vcpu->cr8 = sregs->cr8;
1923
1924 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
05b3e0c2 1925#ifdef CONFIG_X86_64
6aa8b732
AK
1926 kvm_arch_ops->set_efer(vcpu, sregs->efer);
1927#endif
1928 vcpu->apic_base = sregs->apic_base;
1929
399badf3
AK
1930 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1931
6aa8b732 1932 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
f6528b03 1933 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
6aa8b732
AK
1934
1935 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
1936 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
1b0973bd
AK
1937 if (!is_long_mode(vcpu) && is_pae(vcpu))
1938 load_pdptrs(vcpu, vcpu->cr3);
6aa8b732
AK
1939
1940 if (mmu_reset_needed)
1941 kvm_mmu_reset_context(vcpu);
1942
1943 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
1944 sizeof vcpu->irq_pending);
1945 vcpu->irq_summary = 0;
1946 for (i = 0; i < NR_IRQ_WORDS; ++i)
1947 if (vcpu->irq_pending[i])
1948 __set_bit(i, &vcpu->irq_summary);
1949
024aa1c0
AK
1950 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1951 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1952 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1953 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1954 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1955 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1956
1957 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1958 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1959
6aa8b732
AK
1960 vcpu_put(vcpu);
1961
1962 return 0;
1963}
1964
1965/*
1966 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1967 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
bf591b24
MR
1968 *
1969 * This list is modified at module load time to reflect the
1970 * capabilities of the host cpu.
6aa8b732
AK
1971 */
1972static u32 msrs_to_save[] = {
1973 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
1974 MSR_K6_STAR,
05b3e0c2 1975#ifdef CONFIG_X86_64
6aa8b732
AK
1976 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1977#endif
1978 MSR_IA32_TIME_STAMP_COUNTER,
1979};
1980
bf591b24
MR
1981static unsigned num_msrs_to_save;
1982
6f00e68f
AK
1983static u32 emulated_msrs[] = {
1984 MSR_IA32_MISC_ENABLE,
1985};
1986
bf591b24
MR
1987static __init void kvm_init_msr_list(void)
1988{
1989 u32 dummy[2];
1990 unsigned i, j;
1991
1992 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1993 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1994 continue;
1995 if (j < i)
1996 msrs_to_save[j] = msrs_to_save[i];
1997 j++;
1998 }
1999 num_msrs_to_save = j;
2000}
6aa8b732
AK
2001
2002/*
2003 * Adapt set_msr() to msr_io()'s calling convention
2004 */
2005static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2006{
2007 return set_msr(vcpu, index, *data);
2008}
2009
2010/*
2011 * Read or write a bunch of msrs. All parameters are kernel addresses.
2012 *
2013 * @return number of msrs set successfully.
2014 */
bccf2150 2015static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
6aa8b732
AK
2016 struct kvm_msr_entry *entries,
2017 int (*do_msr)(struct kvm_vcpu *vcpu,
2018 unsigned index, u64 *data))
2019{
6aa8b732
AK
2020 int i;
2021
bccf2150 2022 vcpu_load(vcpu);
6aa8b732
AK
2023
2024 for (i = 0; i < msrs->nmsrs; ++i)
2025 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2026 break;
2027
2028 vcpu_put(vcpu);
2029
2030 return i;
2031}
2032
2033/*
2034 * Read or write a bunch of msrs. Parameters are user addresses.
2035 *
2036 * @return number of msrs set successfully.
2037 */
bccf2150 2038static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
6aa8b732
AK
2039 int (*do_msr)(struct kvm_vcpu *vcpu,
2040 unsigned index, u64 *data),
2041 int writeback)
2042{
2043 struct kvm_msrs msrs;
2044 struct kvm_msr_entry *entries;
2045 int r, n;
2046 unsigned size;
2047
2048 r = -EFAULT;
2049 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2050 goto out;
2051
2052 r = -E2BIG;
2053 if (msrs.nmsrs >= MAX_IO_MSRS)
2054 goto out;
2055
2056 r = -ENOMEM;
2057 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2058 entries = vmalloc(size);
2059 if (!entries)
2060 goto out;
2061
2062 r = -EFAULT;
2063 if (copy_from_user(entries, user_msrs->entries, size))
2064 goto out_free;
2065
bccf2150 2066 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
6aa8b732
AK
2067 if (r < 0)
2068 goto out_free;
2069
2070 r = -EFAULT;
2071 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2072 goto out_free;
2073
2074 r = n;
2075
2076out_free:
2077 vfree(entries);
2078out:
2079 return r;
2080}
2081
2082/*
2083 * Translate a guest virtual address to a guest physical address.
2084 */
bccf2150
AK
2085static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2086 struct kvm_translation *tr)
6aa8b732
AK
2087{
2088 unsigned long vaddr = tr->linear_address;
6aa8b732
AK
2089 gpa_t gpa;
2090
bccf2150
AK
2091 vcpu_load(vcpu);
2092 spin_lock(&vcpu->kvm->lock);
6aa8b732
AK
2093 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2094 tr->physical_address = gpa;
2095 tr->valid = gpa != UNMAPPED_GVA;
2096 tr->writeable = 1;
2097 tr->usermode = 0;
bccf2150 2098 spin_unlock(&vcpu->kvm->lock);
6aa8b732
AK
2099 vcpu_put(vcpu);
2100
2101 return 0;
2102}
2103
bccf2150
AK
2104static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2105 struct kvm_interrupt *irq)
6aa8b732 2106{
6aa8b732
AK
2107 if (irq->irq < 0 || irq->irq >= 256)
2108 return -EINVAL;
bccf2150 2109 vcpu_load(vcpu);
6aa8b732
AK
2110
2111 set_bit(irq->irq, vcpu->irq_pending);
2112 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2113
2114 vcpu_put(vcpu);
2115
2116 return 0;
2117}
2118
bccf2150
AK
2119static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2120 struct kvm_debug_guest *dbg)
6aa8b732 2121{
6aa8b732
AK
2122 int r;
2123
bccf2150 2124 vcpu_load(vcpu);
6aa8b732
AK
2125
2126 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2127
2128 vcpu_put(vcpu);
2129
2130 return r;
2131}
2132
9a2bb7f4
AK
2133static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2134 unsigned long address,
2135 int *type)
2136{
2137 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2138 unsigned long pgoff;
2139 struct page *page;
2140
2141 *type = VM_FAULT_MINOR;
2142 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
039576c0
AK
2143 if (pgoff == 0)
2144 page = virt_to_page(vcpu->run);
2145 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2146 page = virt_to_page(vcpu->pio_data);
2147 else
9a2bb7f4 2148 return NOPAGE_SIGBUS;
9a2bb7f4
AK
2149 get_page(page);
2150 return page;
2151}
2152
2153static struct vm_operations_struct kvm_vcpu_vm_ops = {
2154 .nopage = kvm_vcpu_nopage,
2155};
2156
2157static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2158{
2159 vma->vm_ops = &kvm_vcpu_vm_ops;
2160 return 0;
2161}
2162
bccf2150
AK
2163static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2164{
2165 struct kvm_vcpu *vcpu = filp->private_data;
2166
2167 fput(vcpu->kvm->filp);
2168 return 0;
2169}
2170
2171static struct file_operations kvm_vcpu_fops = {
2172 .release = kvm_vcpu_release,
2173 .unlocked_ioctl = kvm_vcpu_ioctl,
2174 .compat_ioctl = kvm_vcpu_ioctl,
9a2bb7f4 2175 .mmap = kvm_vcpu_mmap,
bccf2150
AK
2176};
2177
2178/*
2179 * Allocates an inode for the vcpu.
2180 */
2181static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2182{
2183 int fd, r;
2184 struct inode *inode;
2185 struct file *file;
2186
2187 atomic_inc(&vcpu->kvm->filp->f_count);
2188 inode = kvmfs_inode(&kvm_vcpu_fops);
2189 if (IS_ERR(inode)) {
2190 r = PTR_ERR(inode);
2191 goto out1;
2192 }
2193
2194 file = kvmfs_file(inode, vcpu);
2195 if (IS_ERR(file)) {
2196 r = PTR_ERR(file);
2197 goto out2;
2198 }
2199
2200 r = get_unused_fd();
2201 if (r < 0)
2202 goto out3;
2203 fd = r;
2204 fd_install(fd, file);
2205
2206 return fd;
2207
2208out3:
2209 fput(file);
2210out2:
2211 iput(inode);
2212out1:
2213 fput(vcpu->kvm->filp);
2214 return r;
2215}
2216
c5ea7660
AK
2217/*
2218 * Creates some virtual cpus. Good luck creating more than one.
2219 */
2220static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2221{
2222 int r;
2223 struct kvm_vcpu *vcpu;
9a2bb7f4 2224 struct page *page;
c5ea7660
AK
2225
2226 r = -EINVAL;
2227 if (!valid_vcpu(n))
2228 goto out;
2229
2230 vcpu = &kvm->vcpus[n];
2231
2232 mutex_lock(&vcpu->mutex);
2233
2234 if (vcpu->vmcs) {
2235 mutex_unlock(&vcpu->mutex);
2236 return -EEXIST;
2237 }
2238
9a2bb7f4
AK
2239 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2240 r = -ENOMEM;
2241 if (!page)
2242 goto out_unlock;
2243 vcpu->run = page_address(page);
2244
039576c0
AK
2245 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2246 r = -ENOMEM;
2247 if (!page)
2248 goto out_free_run;
2249 vcpu->pio_data = page_address(page);
2250
c5ea7660
AK
2251 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2252 FX_IMAGE_ALIGN);
2253 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
2254
2255 r = kvm_arch_ops->vcpu_create(vcpu);
2256 if (r < 0)
2257 goto out_free_vcpus;
2258
2259 r = kvm_mmu_create(vcpu);
2260 if (r < 0)
2261 goto out_free_vcpus;
2262
2263 kvm_arch_ops->vcpu_load(vcpu);
2264 r = kvm_mmu_setup(vcpu);
2265 if (r >= 0)
2266 r = kvm_arch_ops->vcpu_setup(vcpu);
2267 vcpu_put(vcpu);
2268
2269 if (r < 0)
2270 goto out_free_vcpus;
2271
bccf2150
AK
2272 r = create_vcpu_fd(vcpu);
2273 if (r < 0)
2274 goto out_free_vcpus;
2275
2276 return r;
c5ea7660
AK
2277
2278out_free_vcpus:
2279 kvm_free_vcpu(vcpu);
039576c0
AK
2280out_free_run:
2281 free_page((unsigned long)vcpu->run);
2282 vcpu->run = NULL;
9a2bb7f4 2283out_unlock:
c5ea7660
AK
2284 mutex_unlock(&vcpu->mutex);
2285out:
2286 return r;
2287}
2288
06465c5a
AK
2289static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2290 struct kvm_cpuid *cpuid,
2291 struct kvm_cpuid_entry __user *entries)
2292{
2293 int r;
2294
2295 r = -E2BIG;
2296 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2297 goto out;
2298 r = -EFAULT;
2299 if (copy_from_user(&vcpu->cpuid_entries, entries,
2300 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2301 goto out;
2302 vcpu->cpuid_nent = cpuid->nent;
2303 return 0;
2304
2305out:
2306 return r;
2307}
2308
1961d276
AK
2309static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2310{
2311 if (sigset) {
2312 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2313 vcpu->sigset_active = 1;
2314 vcpu->sigset = *sigset;
2315 } else
2316 vcpu->sigset_active = 0;
2317 return 0;
2318}
2319
bccf2150
AK
2320static long kvm_vcpu_ioctl(struct file *filp,
2321 unsigned int ioctl, unsigned long arg)
6aa8b732 2322{
bccf2150 2323 struct kvm_vcpu *vcpu = filp->private_data;
2f366987 2324 void __user *argp = (void __user *)arg;
6aa8b732
AK
2325 int r = -EINVAL;
2326
2327 switch (ioctl) {
9a2bb7f4 2328 case KVM_RUN:
f0fe5108
AK
2329 r = -EINVAL;
2330 if (arg)
2331 goto out;
9a2bb7f4 2332 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
6aa8b732 2333 break;
6aa8b732
AK
2334 case KVM_GET_REGS: {
2335 struct kvm_regs kvm_regs;
2336
bccf2150
AK
2337 memset(&kvm_regs, 0, sizeof kvm_regs);
2338 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
6aa8b732
AK
2339 if (r)
2340 goto out;
2341 r = -EFAULT;
2f366987 2342 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
6aa8b732
AK
2343 goto out;
2344 r = 0;
2345 break;
2346 }
2347 case KVM_SET_REGS: {
2348 struct kvm_regs kvm_regs;
2349
2350 r = -EFAULT;
2f366987 2351 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
6aa8b732 2352 goto out;
bccf2150 2353 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
6aa8b732
AK
2354 if (r)
2355 goto out;
2356 r = 0;
2357 break;
2358 }
2359 case KVM_GET_SREGS: {
2360 struct kvm_sregs kvm_sregs;
2361
bccf2150
AK
2362 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2363 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
6aa8b732
AK
2364 if (r)
2365 goto out;
2366 r = -EFAULT;
2f366987 2367 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
6aa8b732
AK
2368 goto out;
2369 r = 0;
2370 break;
2371 }
2372 case KVM_SET_SREGS: {
2373 struct kvm_sregs kvm_sregs;
2374
2375 r = -EFAULT;
2f366987 2376 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
6aa8b732 2377 goto out;
bccf2150 2378 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
6aa8b732
AK
2379 if (r)
2380 goto out;
2381 r = 0;
2382 break;
2383 }
2384 case KVM_TRANSLATE: {
2385 struct kvm_translation tr;
2386
2387 r = -EFAULT;
2f366987 2388 if (copy_from_user(&tr, argp, sizeof tr))
6aa8b732 2389 goto out;
bccf2150 2390 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
6aa8b732
AK
2391 if (r)
2392 goto out;
2393 r = -EFAULT;
2f366987 2394 if (copy_to_user(argp, &tr, sizeof tr))
6aa8b732
AK
2395 goto out;
2396 r = 0;
2397 break;
2398 }
2399 case KVM_INTERRUPT: {
2400 struct kvm_interrupt irq;
2401
2402 r = -EFAULT;
2f366987 2403 if (copy_from_user(&irq, argp, sizeof irq))
6aa8b732 2404 goto out;
bccf2150 2405 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
6aa8b732
AK
2406 if (r)
2407 goto out;
2408 r = 0;
2409 break;
2410 }
2411 case KVM_DEBUG_GUEST: {
2412 struct kvm_debug_guest dbg;
2413
2414 r = -EFAULT;
2f366987 2415 if (copy_from_user(&dbg, argp, sizeof dbg))
6aa8b732 2416 goto out;
bccf2150 2417 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
6aa8b732
AK
2418 if (r)
2419 goto out;
2420 r = 0;
2421 break;
2422 }
bccf2150
AK
2423 case KVM_GET_MSRS:
2424 r = msr_io(vcpu, argp, get_msr, 1);
2425 break;
2426 case KVM_SET_MSRS:
2427 r = msr_io(vcpu, argp, do_set_msr, 0);
2428 break;
06465c5a
AK
2429 case KVM_SET_CPUID: {
2430 struct kvm_cpuid __user *cpuid_arg = argp;
2431 struct kvm_cpuid cpuid;
2432
2433 r = -EFAULT;
2434 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2435 goto out;
2436 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2437 if (r)
2438 goto out;
2439 break;
2440 }
1961d276
AK
2441 case KVM_SET_SIGNAL_MASK: {
2442 struct kvm_signal_mask __user *sigmask_arg = argp;
2443 struct kvm_signal_mask kvm_sigmask;
2444 sigset_t sigset, *p;
2445
2446 p = NULL;
2447 if (argp) {
2448 r = -EFAULT;
2449 if (copy_from_user(&kvm_sigmask, argp,
2450 sizeof kvm_sigmask))
2451 goto out;
2452 r = -EINVAL;
2453 if (kvm_sigmask.len != sizeof sigset)
2454 goto out;
2455 r = -EFAULT;
2456 if (copy_from_user(&sigset, sigmask_arg->sigset,
2457 sizeof sigset))
2458 goto out;
2459 p = &sigset;
2460 }
2461 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2462 break;
2463 }
bccf2150
AK
2464 default:
2465 ;
2466 }
2467out:
2468 return r;
2469}
2470
2471static long kvm_vm_ioctl(struct file *filp,
2472 unsigned int ioctl, unsigned long arg)
2473{
2474 struct kvm *kvm = filp->private_data;
2475 void __user *argp = (void __user *)arg;
2476 int r = -EINVAL;
2477
2478 switch (ioctl) {
2479 case KVM_CREATE_VCPU:
2480 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2481 if (r < 0)
2482 goto out;
2483 break;
6aa8b732
AK
2484 case KVM_SET_MEMORY_REGION: {
2485 struct kvm_memory_region kvm_mem;
2486
2487 r = -EFAULT;
2f366987 2488 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
6aa8b732 2489 goto out;
2c6f5df9 2490 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
6aa8b732
AK
2491 if (r)
2492 goto out;
2493 break;
2494 }
2495 case KVM_GET_DIRTY_LOG: {
2496 struct kvm_dirty_log log;
2497
2498 r = -EFAULT;
2f366987 2499 if (copy_from_user(&log, argp, sizeof log))
6aa8b732 2500 goto out;
2c6f5df9 2501 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
6aa8b732
AK
2502 if (r)
2503 goto out;
2504 break;
2505 }
f17abe9a
AK
2506 default:
2507 ;
2508 }
2509out:
2510 return r;
2511}
2512
2513static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2514 unsigned long address,
2515 int *type)
2516{
2517 struct kvm *kvm = vma->vm_file->private_data;
2518 unsigned long pgoff;
2519 struct kvm_memory_slot *slot;
2520 struct page *page;
2521
2522 *type = VM_FAULT_MINOR;
2523 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2524 slot = gfn_to_memslot(kvm, pgoff);
2525 if (!slot)
2526 return NOPAGE_SIGBUS;
2527 page = gfn_to_page(slot, pgoff);
2528 if (!page)
2529 return NOPAGE_SIGBUS;
2530 get_page(page);
2531 return page;
2532}
2533
2534static struct vm_operations_struct kvm_vm_vm_ops = {
2535 .nopage = kvm_vm_nopage,
2536};
2537
2538static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2539{
2540 vma->vm_ops = &kvm_vm_vm_ops;
2541 return 0;
2542}
2543
2544static struct file_operations kvm_vm_fops = {
2545 .release = kvm_vm_release,
2546 .unlocked_ioctl = kvm_vm_ioctl,
2547 .compat_ioctl = kvm_vm_ioctl,
2548 .mmap = kvm_vm_mmap,
2549};
2550
2551static int kvm_dev_ioctl_create_vm(void)
2552{
2553 int fd, r;
2554 struct inode *inode;
2555 struct file *file;
2556 struct kvm *kvm;
2557
2558 inode = kvmfs_inode(&kvm_vm_fops);
2559 if (IS_ERR(inode)) {
2560 r = PTR_ERR(inode);
2561 goto out1;
2562 }
2563
2564 kvm = kvm_create_vm();
2565 if (IS_ERR(kvm)) {
2566 r = PTR_ERR(kvm);
2567 goto out2;
2568 }
2569
2570 file = kvmfs_file(inode, kvm);
2571 if (IS_ERR(file)) {
2572 r = PTR_ERR(file);
2573 goto out3;
2574 }
bccf2150 2575 kvm->filp = file;
f17abe9a
AK
2576
2577 r = get_unused_fd();
2578 if (r < 0)
2579 goto out4;
2580 fd = r;
2581 fd_install(fd, file);
2582
2583 return fd;
2584
2585out4:
2586 fput(file);
2587out3:
2588 kvm_destroy_vm(kvm);
2589out2:
2590 iput(inode);
2591out1:
2592 return r;
2593}
2594
2595static long kvm_dev_ioctl(struct file *filp,
2596 unsigned int ioctl, unsigned long arg)
2597{
2598 void __user *argp = (void __user *)arg;
07c45a36 2599 long r = -EINVAL;
f17abe9a
AK
2600
2601 switch (ioctl) {
2602 case KVM_GET_API_VERSION:
f0fe5108
AK
2603 r = -EINVAL;
2604 if (arg)
2605 goto out;
f17abe9a
AK
2606 r = KVM_API_VERSION;
2607 break;
2608 case KVM_CREATE_VM:
f0fe5108
AK
2609 r = -EINVAL;
2610 if (arg)
2611 goto out;
f17abe9a
AK
2612 r = kvm_dev_ioctl_create_vm();
2613 break;
6aa8b732 2614 case KVM_GET_MSR_INDEX_LIST: {
2f366987 2615 struct kvm_msr_list __user *user_msr_list = argp;
6aa8b732
AK
2616 struct kvm_msr_list msr_list;
2617 unsigned n;
2618
2619 r = -EFAULT;
2620 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2621 goto out;
2622 n = msr_list.nmsrs;
6f00e68f 2623 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
6aa8b732
AK
2624 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2625 goto out;
2626 r = -E2BIG;
bf591b24 2627 if (n < num_msrs_to_save)
6aa8b732
AK
2628 goto out;
2629 r = -EFAULT;
2630 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
bf591b24 2631 num_msrs_to_save * sizeof(u32)))
6aa8b732 2632 goto out;
6f00e68f
AK
2633 if (copy_to_user(user_msr_list->indices
2634 + num_msrs_to_save * sizeof(u32),
2635 &emulated_msrs,
2636 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2637 goto out;
6aa8b732 2638 r = 0;
cc1d8955 2639 break;
6aa8b732 2640 }
5d308f45
AK
2641 case KVM_CHECK_EXTENSION:
2642 /*
2643 * No extensions defined at present.
2644 */
2645 r = 0;
2646 break;
07c45a36
AK
2647 case KVM_GET_VCPU_MMAP_SIZE:
2648 r = -EINVAL;
2649 if (arg)
2650 goto out;
039576c0 2651 r = 2 * PAGE_SIZE;
07c45a36 2652 break;
6aa8b732
AK
2653 default:
2654 ;
2655 }
2656out:
2657 return r;
2658}
2659
6aa8b732
AK
2660static struct file_operations kvm_chardev_ops = {
2661 .open = kvm_dev_open,
2662 .release = kvm_dev_release,
2663 .unlocked_ioctl = kvm_dev_ioctl,
2664 .compat_ioctl = kvm_dev_ioctl,
6aa8b732
AK
2665};
2666
2667static struct miscdevice kvm_dev = {
bbe4432e 2668 KVM_MINOR,
6aa8b732
AK
2669 "kvm",
2670 &kvm_chardev_ops,
2671};
2672
2673static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2674 void *v)
2675{
2676 if (val == SYS_RESTART) {
2677 /*
2678 * Some (well, at least mine) BIOSes hang on reboot if
2679 * in vmx root mode.
2680 */
2681 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
8b6d44c7 2682 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
6aa8b732
AK
2683 }
2684 return NOTIFY_OK;
2685}
2686
2687static struct notifier_block kvm_reboot_notifier = {
2688 .notifier_call = kvm_reboot,
2689 .priority = 0,
2690};
2691
774c47f1
AK
2692/*
2693 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2694 * cached on it.
2695 */
2696static void decache_vcpus_on_cpu(int cpu)
2697{
2698 struct kvm *vm;
2699 struct kvm_vcpu *vcpu;
2700 int i;
2701
2702 spin_lock(&kvm_lock);
2703 list_for_each_entry(vm, &vm_list, vm_list)
2704 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2705 vcpu = &vm->vcpus[i];
2706 /*
2707 * If the vcpu is locked, then it is running on some
2708 * other cpu and therefore it is not cached on the
2709 * cpu in question.
2710 *
2711 * If it's not locked, check the last cpu it executed
2712 * on.
2713 */
2714 if (mutex_trylock(&vcpu->mutex)) {
2715 if (vcpu->cpu == cpu) {
2716 kvm_arch_ops->vcpu_decache(vcpu);
2717 vcpu->cpu = -1;
2718 }
2719 mutex_unlock(&vcpu->mutex);
2720 }
2721 }
2722 spin_unlock(&kvm_lock);
2723}
2724
2725static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2726 void *v)
2727{
2728 int cpu = (long)v;
2729
2730 switch (val) {
43934a38 2731 case CPU_DOWN_PREPARE:
774c47f1 2732 case CPU_UP_CANCELED:
43934a38
JK
2733 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2734 cpu);
774c47f1
AK
2735 decache_vcpus_on_cpu(cpu);
2736 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
2737 NULL, 0, 1);
2738 break;
43934a38
JK
2739 case CPU_ONLINE:
2740 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2741 cpu);
774c47f1
AK
2742 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
2743 NULL, 0, 1);
2744 break;
2745 }
2746 return NOTIFY_OK;
2747}
2748
2749static struct notifier_block kvm_cpu_notifier = {
2750 .notifier_call = kvm_cpu_hotplug,
2751 .priority = 20, /* must be > scheduler priority */
2752};
2753
6aa8b732
AK
2754static __init void kvm_init_debug(void)
2755{
2756 struct kvm_stats_debugfs_item *p;
2757
8b6d44c7 2758 debugfs_dir = debugfs_create_dir("kvm", NULL);
6aa8b732
AK
2759 for (p = debugfs_entries; p->name; ++p)
2760 p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
2761 p->data);
2762}
2763
2764static void kvm_exit_debug(void)
2765{
2766 struct kvm_stats_debugfs_item *p;
2767
2768 for (p = debugfs_entries; p->name; ++p)
2769 debugfs_remove(p->dentry);
2770 debugfs_remove(debugfs_dir);
2771}
2772
59ae6c6b
AK
2773static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2774{
2775 decache_vcpus_on_cpu(raw_smp_processor_id());
19d1408d 2776 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
59ae6c6b
AK
2777 return 0;
2778}
2779
2780static int kvm_resume(struct sys_device *dev)
2781{
19d1408d 2782 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
59ae6c6b
AK
2783 return 0;
2784}
2785
2786static struct sysdev_class kvm_sysdev_class = {
2787 set_kset_name("kvm"),
2788 .suspend = kvm_suspend,
2789 .resume = kvm_resume,
2790};
2791
2792static struct sys_device kvm_sysdev = {
2793 .id = 0,
2794 .cls = &kvm_sysdev_class,
2795};
2796
6aa8b732
AK
2797hpa_t bad_page_address;
2798
37e29d90
AK
2799static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
2800 const char *dev_name, void *data, struct vfsmount *mnt)
2801{
e9cdb1e3 2802 return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
37e29d90
AK
2803}
2804
2805static struct file_system_type kvm_fs_type = {
2806 .name = "kvmfs",
2807 .get_sb = kvmfs_get_sb,
2808 .kill_sb = kill_anon_super,
2809};
2810
6aa8b732
AK
2811int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
2812{
2813 int r;
2814
09db28b8
YI
2815 if (kvm_arch_ops) {
2816 printk(KERN_ERR "kvm: already loaded the other module\n");
2817 return -EEXIST;
2818 }
2819
e097f35c 2820 if (!ops->cpu_has_kvm_support()) {
6aa8b732
AK
2821 printk(KERN_ERR "kvm: no hardware support\n");
2822 return -EOPNOTSUPP;
2823 }
e097f35c 2824 if (ops->disabled_by_bios()) {
6aa8b732
AK
2825 printk(KERN_ERR "kvm: disabled by bios\n");
2826 return -EOPNOTSUPP;
2827 }
2828
e097f35c
YI
2829 kvm_arch_ops = ops;
2830
6aa8b732
AK
2831 r = kvm_arch_ops->hardware_setup();
2832 if (r < 0)
ca45aaae 2833 goto out;
6aa8b732 2834
8b6d44c7 2835 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
774c47f1
AK
2836 r = register_cpu_notifier(&kvm_cpu_notifier);
2837 if (r)
2838 goto out_free_1;
6aa8b732
AK
2839 register_reboot_notifier(&kvm_reboot_notifier);
2840
59ae6c6b
AK
2841 r = sysdev_class_register(&kvm_sysdev_class);
2842 if (r)
2843 goto out_free_2;
2844
2845 r = sysdev_register(&kvm_sysdev);
2846 if (r)
2847 goto out_free_3;
2848
6aa8b732
AK
2849 kvm_chardev_ops.owner = module;
2850
2851 r = misc_register(&kvm_dev);
2852 if (r) {
2853 printk (KERN_ERR "kvm: misc device register failed\n");
2854 goto out_free;
2855 }
2856
2857 return r;
2858
2859out_free:
59ae6c6b
AK
2860 sysdev_unregister(&kvm_sysdev);
2861out_free_3:
2862 sysdev_class_unregister(&kvm_sysdev_class);
2863out_free_2:
6aa8b732 2864 unregister_reboot_notifier(&kvm_reboot_notifier);
774c47f1
AK
2865 unregister_cpu_notifier(&kvm_cpu_notifier);
2866out_free_1:
8b6d44c7 2867 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
6aa8b732 2868 kvm_arch_ops->hardware_unsetup();
ca45aaae
AK
2869out:
2870 kvm_arch_ops = NULL;
6aa8b732
AK
2871 return r;
2872}
2873
2874void kvm_exit_arch(void)
2875{
2876 misc_deregister(&kvm_dev);
59ae6c6b
AK
2877 sysdev_unregister(&kvm_sysdev);
2878 sysdev_class_unregister(&kvm_sysdev_class);
6aa8b732 2879 unregister_reboot_notifier(&kvm_reboot_notifier);
59ae6c6b 2880 unregister_cpu_notifier(&kvm_cpu_notifier);
8b6d44c7 2881 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
6aa8b732 2882 kvm_arch_ops->hardware_unsetup();
09db28b8 2883 kvm_arch_ops = NULL;
6aa8b732
AK
2884}
2885
2886static __init int kvm_init(void)
2887{
2888 static struct page *bad_page;
37e29d90
AK
2889 int r;
2890
2891 r = register_filesystem(&kvm_fs_type);
2892 if (r)
2893 goto out3;
6aa8b732 2894
37e29d90
AK
2895 kvmfs_mnt = kern_mount(&kvm_fs_type);
2896 r = PTR_ERR(kvmfs_mnt);
2897 if (IS_ERR(kvmfs_mnt))
2898 goto out2;
6aa8b732
AK
2899 kvm_init_debug();
2900
bf591b24
MR
2901 kvm_init_msr_list();
2902
6aa8b732
AK
2903 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
2904 r = -ENOMEM;
2905 goto out;
2906 }
2907
2908 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
2909 memset(__va(bad_page_address), 0, PAGE_SIZE);
2910
58e690e6 2911 return 0;
6aa8b732
AK
2912
2913out:
2914 kvm_exit_debug();
37e29d90
AK
2915 mntput(kvmfs_mnt);
2916out2:
2917 unregister_filesystem(&kvm_fs_type);
2918out3:
6aa8b732
AK
2919 return r;
2920}
2921
2922static __exit void kvm_exit(void)
2923{
2924 kvm_exit_debug();
2925 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
37e29d90
AK
2926 mntput(kvmfs_mnt);
2927 unregister_filesystem(&kvm_fs_type);
6aa8b732
AK
2928}
2929
2930module_init(kvm_init)
2931module_exit(kvm_exit)
2932
2933EXPORT_SYMBOL_GPL(kvm_init_arch);
2934EXPORT_SYMBOL_GPL(kvm_exit_arch);