KVM: load_pdptrs() cleanups
[linux-2.6] / drivers / kvm / kvm_main.c
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 #include "x86_emulate.h"
20 #include "segment_descriptor.h"
21
22 #include <linux/kvm.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/percpu.h>
26 #include <linux/gfp.h>
27 #include <linux/mm.h>
28 #include <linux/miscdevice.h>
29 #include <linux/vmalloc.h>
30 #include <linux/reboot.h>
31 #include <linux/debugfs.h>
32 #include <linux/highmem.h>
33 #include <linux/file.h>
34 #include <linux/sysdev.h>
35 #include <linux/cpu.h>
36 #include <linux/sched.h>
37 #include <linux/cpumask.h>
38 #include <linux/smp.h>
39 #include <linux/anon_inodes.h>
40
41 #include <asm/processor.h>
42 #include <asm/msr.h>
43 #include <asm/io.h>
44 #include <asm/uaccess.h>
45 #include <asm/desc.h>
46
47 MODULE_AUTHOR("Qumranet");
48 MODULE_LICENSE("GPL");
49
50 static DEFINE_SPINLOCK(kvm_lock);
51 static LIST_HEAD(vm_list);
52
53 static cpumask_t cpus_hardware_enabled;
54
55 struct kvm_arch_ops *kvm_arch_ops;
56
57 #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
58
59 static struct kvm_stats_debugfs_item {
60         const char *name;
61         int offset;
62         struct dentry *dentry;
63 } debugfs_entries[] = {
64         { "pf_fixed", STAT_OFFSET(pf_fixed) },
65         { "pf_guest", STAT_OFFSET(pf_guest) },
66         { "tlb_flush", STAT_OFFSET(tlb_flush) },
67         { "invlpg", STAT_OFFSET(invlpg) },
68         { "exits", STAT_OFFSET(exits) },
69         { "io_exits", STAT_OFFSET(io_exits) },
70         { "mmio_exits", STAT_OFFSET(mmio_exits) },
71         { "signal_exits", STAT_OFFSET(signal_exits) },
72         { "irq_window", STAT_OFFSET(irq_window_exits) },
73         { "halt_exits", STAT_OFFSET(halt_exits) },
74         { "request_irq", STAT_OFFSET(request_irq_exits) },
75         { "irq_exits", STAT_OFFSET(irq_exits) },
76         { "light_exits", STAT_OFFSET(light_exits) },
77         { "efer_reload", STAT_OFFSET(efer_reload) },
78         { NULL }
79 };
80
81 static struct dentry *debugfs_dir;
82
83 #define MAX_IO_MSRS 256
84
85 #define CR0_RESERVED_BITS                                               \
86         (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
87                           | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
88                           | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
89 #define CR4_RESERVED_BITS                                               \
90         (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
91                           | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE     \
92                           | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR  \
93                           | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
94
95 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
96 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
97
98 #ifdef CONFIG_X86_64
99 // LDT or TSS descriptor in the GDT. 16 bytes.
100 struct segment_descriptor_64 {
101         struct segment_descriptor s;
102         u32 base_higher;
103         u32 pad_zero;
104 };
105
106 #endif
107
108 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
109                            unsigned long arg);
110
111 unsigned long segment_base(u16 selector)
112 {
113         struct descriptor_table gdt;
114         struct segment_descriptor *d;
115         unsigned long table_base;
116         typedef unsigned long ul;
117         unsigned long v;
118
119         if (selector == 0)
120                 return 0;
121
122         asm ("sgdt %0" : "=m"(gdt));
123         table_base = gdt.base;
124
125         if (selector & 4) {           /* from ldt */
126                 u16 ldt_selector;
127
128                 asm ("sldt %0" : "=g"(ldt_selector));
129                 table_base = segment_base(ldt_selector);
130         }
131         d = (struct segment_descriptor *)(table_base + (selector & ~7));
132         v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
133 #ifdef CONFIG_X86_64
134         if (d->system == 0
135             && (d->type == 2 || d->type == 9 || d->type == 11))
136                 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
137 #endif
138         return v;
139 }
140 EXPORT_SYMBOL_GPL(segment_base);
141
142 static inline int valid_vcpu(int n)
143 {
144         return likely(n >= 0 && n < KVM_MAX_VCPUS);
145 }
146
147 int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
148                    void *dest)
149 {
150         unsigned char *host_buf = dest;
151         unsigned long req_size = size;
152
153         while (size) {
154                 hpa_t paddr;
155                 unsigned now;
156                 unsigned offset;
157                 hva_t guest_buf;
158
159                 paddr = gva_to_hpa(vcpu, addr);
160
161                 if (is_error_hpa(paddr))
162                         break;
163
164                 guest_buf = (hva_t)kmap_atomic(
165                                         pfn_to_page(paddr >> PAGE_SHIFT),
166                                         KM_USER0);
167                 offset = addr & ~PAGE_MASK;
168                 guest_buf |= offset;
169                 now = min(size, PAGE_SIZE - offset);
170                 memcpy(host_buf, (void*)guest_buf, now);
171                 host_buf += now;
172                 addr += now;
173                 size -= now;
174                 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
175         }
176         return req_size - size;
177 }
178 EXPORT_SYMBOL_GPL(kvm_read_guest);
179
180 int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
181                     void *data)
182 {
183         unsigned char *host_buf = data;
184         unsigned long req_size = size;
185
186         while (size) {
187                 hpa_t paddr;
188                 unsigned now;
189                 unsigned offset;
190                 hva_t guest_buf;
191                 gfn_t gfn;
192
193                 paddr = gva_to_hpa(vcpu, addr);
194
195                 if (is_error_hpa(paddr))
196                         break;
197
198                 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
199                 mark_page_dirty(vcpu->kvm, gfn);
200                 guest_buf = (hva_t)kmap_atomic(
201                                 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
202                 offset = addr & ~PAGE_MASK;
203                 guest_buf |= offset;
204                 now = min(size, PAGE_SIZE - offset);
205                 memcpy((void*)guest_buf, host_buf, now);
206                 host_buf += now;
207                 addr += now;
208                 size -= now;
209                 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
210         }
211         return req_size - size;
212 }
213 EXPORT_SYMBOL_GPL(kvm_write_guest);
214
215 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
216 {
217         if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
218                 return;
219
220         vcpu->guest_fpu_loaded = 1;
221         fx_save(vcpu->host_fx_image);
222         fx_restore(vcpu->guest_fx_image);
223 }
224 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
225
226 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
227 {
228         if (!vcpu->guest_fpu_loaded)
229                 return;
230
231         vcpu->guest_fpu_loaded = 0;
232         fx_save(vcpu->guest_fx_image);
233         fx_restore(vcpu->host_fx_image);
234 }
235 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
236
237 /*
238  * Switches to specified vcpu, until a matching vcpu_put()
239  */
240 static void vcpu_load(struct kvm_vcpu *vcpu)
241 {
242         mutex_lock(&vcpu->mutex);
243         kvm_arch_ops->vcpu_load(vcpu);
244 }
245
246 static void vcpu_put(struct kvm_vcpu *vcpu)
247 {
248         kvm_arch_ops->vcpu_put(vcpu);
249         mutex_unlock(&vcpu->mutex);
250 }
251
252 static void ack_flush(void *_completed)
253 {
254         atomic_t *completed = _completed;
255
256         atomic_inc(completed);
257 }
258
259 void kvm_flush_remote_tlbs(struct kvm *kvm)
260 {
261         int i, cpu, needed;
262         cpumask_t cpus;
263         struct kvm_vcpu *vcpu;
264         atomic_t completed;
265
266         atomic_set(&completed, 0);
267         cpus_clear(cpus);
268         needed = 0;
269         for (i = 0; i < kvm->nvcpus; ++i) {
270                 vcpu = &kvm->vcpus[i];
271                 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
272                         continue;
273                 cpu = vcpu->cpu;
274                 if (cpu != -1 && cpu != raw_smp_processor_id())
275                         if (!cpu_isset(cpu, cpus)) {
276                                 cpu_set(cpu, cpus);
277                                 ++needed;
278                         }
279         }
280
281         /*
282          * We really want smp_call_function_mask() here.  But that's not
283          * available, so ipi all cpus in parallel and wait for them
284          * to complete.
285          */
286         for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
287                 smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
288         while (atomic_read(&completed) != needed) {
289                 cpu_relax();
290                 barrier();
291         }
292 }
293
294 static struct kvm *kvm_create_vm(void)
295 {
296         struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
297         int i;
298
299         if (!kvm)
300                 return ERR_PTR(-ENOMEM);
301
302         kvm_io_bus_init(&kvm->pio_bus);
303         spin_lock_init(&kvm->lock);
304         INIT_LIST_HEAD(&kvm->active_mmu_pages);
305         kvm_io_bus_init(&kvm->mmio_bus);
306         for (i = 0; i < KVM_MAX_VCPUS; ++i) {
307                 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
308
309                 mutex_init(&vcpu->mutex);
310                 vcpu->cpu = -1;
311                 vcpu->kvm = kvm;
312                 vcpu->mmu.root_hpa = INVALID_PAGE;
313         }
314         spin_lock(&kvm_lock);
315         list_add(&kvm->vm_list, &vm_list);
316         spin_unlock(&kvm_lock);
317         return kvm;
318 }
319
320 static int kvm_dev_open(struct inode *inode, struct file *filp)
321 {
322         return 0;
323 }
324
325 /*
326  * Free any memory in @free but not in @dont.
327  */
328 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
329                                   struct kvm_memory_slot *dont)
330 {
331         int i;
332
333         if (!dont || free->phys_mem != dont->phys_mem)
334                 if (free->phys_mem) {
335                         for (i = 0; i < free->npages; ++i)
336                                 if (free->phys_mem[i])
337                                         __free_page(free->phys_mem[i]);
338                         vfree(free->phys_mem);
339                 }
340
341         if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
342                 vfree(free->dirty_bitmap);
343
344         free->phys_mem = NULL;
345         free->npages = 0;
346         free->dirty_bitmap = NULL;
347 }
348
349 static void kvm_free_physmem(struct kvm *kvm)
350 {
351         int i;
352
353         for (i = 0; i < kvm->nmemslots; ++i)
354                 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
355 }
356
357 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
358 {
359         int i;
360
361         for (i = 0; i < 2; ++i)
362                 if (vcpu->pio.guest_pages[i]) {
363                         __free_page(vcpu->pio.guest_pages[i]);
364                         vcpu->pio.guest_pages[i] = NULL;
365                 }
366 }
367
368 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
369 {
370         if (!vcpu->vmcs)
371                 return;
372
373         vcpu_load(vcpu);
374         kvm_mmu_unload(vcpu);
375         vcpu_put(vcpu);
376 }
377
378 static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
379 {
380         if (!vcpu->vmcs)
381                 return;
382
383         vcpu_load(vcpu);
384         kvm_mmu_destroy(vcpu);
385         vcpu_put(vcpu);
386         kvm_arch_ops->vcpu_free(vcpu);
387         free_page((unsigned long)vcpu->run);
388         vcpu->run = NULL;
389         free_page((unsigned long)vcpu->pio_data);
390         vcpu->pio_data = NULL;
391         free_pio_guest_pages(vcpu);
392 }
393
394 static void kvm_free_vcpus(struct kvm *kvm)
395 {
396         unsigned int i;
397
398         /*
399          * Unpin any mmu pages first.
400          */
401         for (i = 0; i < KVM_MAX_VCPUS; ++i)
402                 kvm_unload_vcpu_mmu(&kvm->vcpus[i]);
403         for (i = 0; i < KVM_MAX_VCPUS; ++i)
404                 kvm_free_vcpu(&kvm->vcpus[i]);
405 }
406
407 static int kvm_dev_release(struct inode *inode, struct file *filp)
408 {
409         return 0;
410 }
411
412 static void kvm_destroy_vm(struct kvm *kvm)
413 {
414         spin_lock(&kvm_lock);
415         list_del(&kvm->vm_list);
416         spin_unlock(&kvm_lock);
417         kvm_io_bus_destroy(&kvm->pio_bus);
418         kvm_io_bus_destroy(&kvm->mmio_bus);
419         kvm_free_vcpus(kvm);
420         kvm_free_physmem(kvm);
421         kfree(kvm);
422 }
423
424 static int kvm_vm_release(struct inode *inode, struct file *filp)
425 {
426         struct kvm *kvm = filp->private_data;
427
428         kvm_destroy_vm(kvm);
429         return 0;
430 }
431
432 static void inject_gp(struct kvm_vcpu *vcpu)
433 {
434         kvm_arch_ops->inject_gp(vcpu, 0);
435 }
436
437 /*
438  * Load the pae pdptrs.  Return true is they are all valid.
439  */
440 static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
441 {
442         gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
443         unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
444         int i;
445         u64 *pdpt;
446         int ret;
447         struct page *page;
448         u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
449
450         spin_lock(&vcpu->kvm->lock);
451         page = gfn_to_page(vcpu->kvm, pdpt_gfn);
452         if (!page) {
453                 ret = 0;
454                 goto out;
455         }
456
457         pdpt = kmap_atomic(page, KM_USER0);
458         memcpy(pdpte, pdpt+offset, sizeof(pdpte));
459         kunmap_atomic(pdpt, KM_USER0);
460
461         for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
462                 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
463                         ret = 0;
464                         goto out;
465                 }
466         }
467         ret = 1;
468
469         memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
470 out:
471         spin_unlock(&vcpu->kvm->lock);
472
473         return ret;
474 }
475
476 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
477 {
478         if (cr0 & CR0_RESERVED_BITS) {
479                 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
480                        cr0, vcpu->cr0);
481                 inject_gp(vcpu);
482                 return;
483         }
484
485         if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
486                 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
487                 inject_gp(vcpu);
488                 return;
489         }
490
491         if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
492                 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
493                        "and a clear PE flag\n");
494                 inject_gp(vcpu);
495                 return;
496         }
497
498         if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
499 #ifdef CONFIG_X86_64
500                 if ((vcpu->shadow_efer & EFER_LME)) {
501                         int cs_db, cs_l;
502
503                         if (!is_pae(vcpu)) {
504                                 printk(KERN_DEBUG "set_cr0: #GP, start paging "
505                                        "in long mode while PAE is disabled\n");
506                                 inject_gp(vcpu);
507                                 return;
508                         }
509                         kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
510                         if (cs_l) {
511                                 printk(KERN_DEBUG "set_cr0: #GP, start paging "
512                                        "in long mode while CS.L == 1\n");
513                                 inject_gp(vcpu);
514                                 return;
515
516                         }
517                 } else
518 #endif
519                 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
520                         printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
521                                "reserved bits\n");
522                         inject_gp(vcpu);
523                         return;
524                 }
525
526         }
527
528         kvm_arch_ops->set_cr0(vcpu, cr0);
529         vcpu->cr0 = cr0;
530
531         spin_lock(&vcpu->kvm->lock);
532         kvm_mmu_reset_context(vcpu);
533         spin_unlock(&vcpu->kvm->lock);
534         return;
535 }
536 EXPORT_SYMBOL_GPL(set_cr0);
537
538 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
539 {
540         set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
541 }
542 EXPORT_SYMBOL_GPL(lmsw);
543
544 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
545 {
546         if (cr4 & CR4_RESERVED_BITS) {
547                 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
548                 inject_gp(vcpu);
549                 return;
550         }
551
552         if (is_long_mode(vcpu)) {
553                 if (!(cr4 & X86_CR4_PAE)) {
554                         printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
555                                "in long mode\n");
556                         inject_gp(vcpu);
557                         return;
558                 }
559         } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
560                    && !load_pdptrs(vcpu, vcpu->cr3)) {
561                 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
562                 inject_gp(vcpu);
563                 return;
564         }
565
566         if (cr4 & X86_CR4_VMXE) {
567                 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
568                 inject_gp(vcpu);
569                 return;
570         }
571         kvm_arch_ops->set_cr4(vcpu, cr4);
572         spin_lock(&vcpu->kvm->lock);
573         kvm_mmu_reset_context(vcpu);
574         spin_unlock(&vcpu->kvm->lock);
575 }
576 EXPORT_SYMBOL_GPL(set_cr4);
577
578 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
579 {
580         if (is_long_mode(vcpu)) {
581                 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
582                         printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
583                         inject_gp(vcpu);
584                         return;
585                 }
586         } else {
587                 if (is_pae(vcpu)) {
588                         if (cr3 & CR3_PAE_RESERVED_BITS) {
589                                 printk(KERN_DEBUG
590                                        "set_cr3: #GP, reserved bits\n");
591                                 inject_gp(vcpu);
592                                 return;
593                         }
594                         if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
595                                 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
596                                        "reserved bits\n");
597                                 inject_gp(vcpu);
598                                 return;
599                         }
600                 } else {
601                         if (cr3 & CR3_NONPAE_RESERVED_BITS) {
602                                 printk(KERN_DEBUG
603                                        "set_cr3: #GP, reserved bits\n");
604                                 inject_gp(vcpu);
605                                 return;
606                         }
607                 }
608         }
609
610         vcpu->cr3 = cr3;
611         spin_lock(&vcpu->kvm->lock);
612         /*
613          * Does the new cr3 value map to physical memory? (Note, we
614          * catch an invalid cr3 even in real-mode, because it would
615          * cause trouble later on when we turn on paging anyway.)
616          *
617          * A real CPU would silently accept an invalid cr3 and would
618          * attempt to use it - with largely undefined (and often hard
619          * to debug) behavior on the guest side.
620          */
621         if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
622                 inject_gp(vcpu);
623         else
624                 vcpu->mmu.new_cr3(vcpu);
625         spin_unlock(&vcpu->kvm->lock);
626 }
627 EXPORT_SYMBOL_GPL(set_cr3);
628
629 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
630 {
631         if (cr8 & CR8_RESERVED_BITS) {
632                 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
633                 inject_gp(vcpu);
634                 return;
635         }
636         vcpu->cr8 = cr8;
637 }
638 EXPORT_SYMBOL_GPL(set_cr8);
639
640 void fx_init(struct kvm_vcpu *vcpu)
641 {
642         struct __attribute__ ((__packed__)) fx_image_s {
643                 u16 control; //fcw
644                 u16 status; //fsw
645                 u16 tag; // ftw
646                 u16 opcode; //fop
647                 u64 ip; // fpu ip
648                 u64 operand;// fpu dp
649                 u32 mxcsr;
650                 u32 mxcsr_mask;
651
652         } *fx_image;
653
654         fx_save(vcpu->host_fx_image);
655         fpu_init();
656         fx_save(vcpu->guest_fx_image);
657         fx_restore(vcpu->host_fx_image);
658
659         fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
660         fx_image->mxcsr = 0x1f80;
661         memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
662                0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
663 }
664 EXPORT_SYMBOL_GPL(fx_init);
665
666 /*
667  * Allocate some memory and give it an address in the guest physical address
668  * space.
669  *
670  * Discontiguous memory is allowed, mostly for framebuffers.
671  */
672 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
673                                           struct kvm_memory_region *mem)
674 {
675         int r;
676         gfn_t base_gfn;
677         unsigned long npages;
678         unsigned long i;
679         struct kvm_memory_slot *memslot;
680         struct kvm_memory_slot old, new;
681         int memory_config_version;
682
683         r = -EINVAL;
684         /* General sanity checks */
685         if (mem->memory_size & (PAGE_SIZE - 1))
686                 goto out;
687         if (mem->guest_phys_addr & (PAGE_SIZE - 1))
688                 goto out;
689         if (mem->slot >= KVM_MEMORY_SLOTS)
690                 goto out;
691         if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
692                 goto out;
693
694         memslot = &kvm->memslots[mem->slot];
695         base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
696         npages = mem->memory_size >> PAGE_SHIFT;
697
698         if (!npages)
699                 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
700
701 raced:
702         spin_lock(&kvm->lock);
703
704         memory_config_version = kvm->memory_config_version;
705         new = old = *memslot;
706
707         new.base_gfn = base_gfn;
708         new.npages = npages;
709         new.flags = mem->flags;
710
711         /* Disallow changing a memory slot's size. */
712         r = -EINVAL;
713         if (npages && old.npages && npages != old.npages)
714                 goto out_unlock;
715
716         /* Check for overlaps */
717         r = -EEXIST;
718         for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
719                 struct kvm_memory_slot *s = &kvm->memslots[i];
720
721                 if (s == memslot)
722                         continue;
723                 if (!((base_gfn + npages <= s->base_gfn) ||
724                       (base_gfn >= s->base_gfn + s->npages)))
725                         goto out_unlock;
726         }
727         /*
728          * Do memory allocations outside lock.  memory_config_version will
729          * detect any races.
730          */
731         spin_unlock(&kvm->lock);
732
733         /* Deallocate if slot is being removed */
734         if (!npages)
735                 new.phys_mem = NULL;
736
737         /* Free page dirty bitmap if unneeded */
738         if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
739                 new.dirty_bitmap = NULL;
740
741         r = -ENOMEM;
742
743         /* Allocate if a slot is being created */
744         if (npages && !new.phys_mem) {
745                 new.phys_mem = vmalloc(npages * sizeof(struct page *));
746
747                 if (!new.phys_mem)
748                         goto out_free;
749
750                 memset(new.phys_mem, 0, npages * sizeof(struct page *));
751                 for (i = 0; i < npages; ++i) {
752                         new.phys_mem[i] = alloc_page(GFP_HIGHUSER
753                                                      | __GFP_ZERO);
754                         if (!new.phys_mem[i])
755                                 goto out_free;
756                         set_page_private(new.phys_mem[i],0);
757                 }
758         }
759
760         /* Allocate page dirty bitmap if needed */
761         if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
762                 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
763
764                 new.dirty_bitmap = vmalloc(dirty_bytes);
765                 if (!new.dirty_bitmap)
766                         goto out_free;
767                 memset(new.dirty_bitmap, 0, dirty_bytes);
768         }
769
770         spin_lock(&kvm->lock);
771
772         if (memory_config_version != kvm->memory_config_version) {
773                 spin_unlock(&kvm->lock);
774                 kvm_free_physmem_slot(&new, &old);
775                 goto raced;
776         }
777
778         r = -EAGAIN;
779         if (kvm->busy)
780                 goto out_unlock;
781
782         if (mem->slot >= kvm->nmemslots)
783                 kvm->nmemslots = mem->slot + 1;
784
785         *memslot = new;
786         ++kvm->memory_config_version;
787
788         kvm_mmu_slot_remove_write_access(kvm, mem->slot);
789         kvm_flush_remote_tlbs(kvm);
790
791         spin_unlock(&kvm->lock);
792
793         kvm_free_physmem_slot(&old, &new);
794         return 0;
795
796 out_unlock:
797         spin_unlock(&kvm->lock);
798 out_free:
799         kvm_free_physmem_slot(&new, &old);
800 out:
801         return r;
802 }
803
804 /*
805  * Get (and clear) the dirty memory log for a memory slot.
806  */
807 static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
808                                       struct kvm_dirty_log *log)
809 {
810         struct kvm_memory_slot *memslot;
811         int r, i;
812         int n;
813         unsigned long any = 0;
814
815         spin_lock(&kvm->lock);
816
817         /*
818          * Prevent changes to guest memory configuration even while the lock
819          * is not taken.
820          */
821         ++kvm->busy;
822         spin_unlock(&kvm->lock);
823         r = -EINVAL;
824         if (log->slot >= KVM_MEMORY_SLOTS)
825                 goto out;
826
827         memslot = &kvm->memslots[log->slot];
828         r = -ENOENT;
829         if (!memslot->dirty_bitmap)
830                 goto out;
831
832         n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
833
834         for (i = 0; !any && i < n/sizeof(long); ++i)
835                 any = memslot->dirty_bitmap[i];
836
837         r = -EFAULT;
838         if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
839                 goto out;
840
841         spin_lock(&kvm->lock);
842         kvm_mmu_slot_remove_write_access(kvm, log->slot);
843         kvm_flush_remote_tlbs(kvm);
844         memset(memslot->dirty_bitmap, 0, n);
845         spin_unlock(&kvm->lock);
846
847         r = 0;
848
849 out:
850         spin_lock(&kvm->lock);
851         --kvm->busy;
852         spin_unlock(&kvm->lock);
853         return r;
854 }
855
856 /*
857  * Set a new alias region.  Aliases map a portion of physical memory into
858  * another portion.  This is useful for memory windows, for example the PC
859  * VGA region.
860  */
861 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
862                                          struct kvm_memory_alias *alias)
863 {
864         int r, n;
865         struct kvm_mem_alias *p;
866
867         r = -EINVAL;
868         /* General sanity checks */
869         if (alias->memory_size & (PAGE_SIZE - 1))
870                 goto out;
871         if (alias->guest_phys_addr & (PAGE_SIZE - 1))
872                 goto out;
873         if (alias->slot >= KVM_ALIAS_SLOTS)
874                 goto out;
875         if (alias->guest_phys_addr + alias->memory_size
876             < alias->guest_phys_addr)
877                 goto out;
878         if (alias->target_phys_addr + alias->memory_size
879             < alias->target_phys_addr)
880                 goto out;
881
882         spin_lock(&kvm->lock);
883
884         p = &kvm->aliases[alias->slot];
885         p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
886         p->npages = alias->memory_size >> PAGE_SHIFT;
887         p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
888
889         for (n = KVM_ALIAS_SLOTS; n > 0; --n)
890                 if (kvm->aliases[n - 1].npages)
891                         break;
892         kvm->naliases = n;
893
894         kvm_mmu_zap_all(kvm);
895
896         spin_unlock(&kvm->lock);
897
898         return 0;
899
900 out:
901         return r;
902 }
903
904 static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
905 {
906         int i;
907         struct kvm_mem_alias *alias;
908
909         for (i = 0; i < kvm->naliases; ++i) {
910                 alias = &kvm->aliases[i];
911                 if (gfn >= alias->base_gfn
912                     && gfn < alias->base_gfn + alias->npages)
913                         return alias->target_gfn + gfn - alias->base_gfn;
914         }
915         return gfn;
916 }
917
918 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
919 {
920         int i;
921
922         for (i = 0; i < kvm->nmemslots; ++i) {
923                 struct kvm_memory_slot *memslot = &kvm->memslots[i];
924
925                 if (gfn >= memslot->base_gfn
926                     && gfn < memslot->base_gfn + memslot->npages)
927                         return memslot;
928         }
929         return NULL;
930 }
931
932 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
933 {
934         gfn = unalias_gfn(kvm, gfn);
935         return __gfn_to_memslot(kvm, gfn);
936 }
937
938 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
939 {
940         struct kvm_memory_slot *slot;
941
942         gfn = unalias_gfn(kvm, gfn);
943         slot = __gfn_to_memslot(kvm, gfn);
944         if (!slot)
945                 return NULL;
946         return slot->phys_mem[gfn - slot->base_gfn];
947 }
948 EXPORT_SYMBOL_GPL(gfn_to_page);
949
950 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
951 {
952         int i;
953         struct kvm_memory_slot *memslot;
954         unsigned long rel_gfn;
955
956         for (i = 0; i < kvm->nmemslots; ++i) {
957                 memslot = &kvm->memslots[i];
958
959                 if (gfn >= memslot->base_gfn
960                     && gfn < memslot->base_gfn + memslot->npages) {
961
962                         if (!memslot->dirty_bitmap)
963                                 return;
964
965                         rel_gfn = gfn - memslot->base_gfn;
966
967                         /* avoid RMW */
968                         if (!test_bit(rel_gfn, memslot->dirty_bitmap))
969                                 set_bit(rel_gfn, memslot->dirty_bitmap);
970                         return;
971                 }
972         }
973 }
974
975 static int emulator_read_std(unsigned long addr,
976                              void *val,
977                              unsigned int bytes,
978                              struct x86_emulate_ctxt *ctxt)
979 {
980         struct kvm_vcpu *vcpu = ctxt->vcpu;
981         void *data = val;
982
983         while (bytes) {
984                 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
985                 unsigned offset = addr & (PAGE_SIZE-1);
986                 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
987                 unsigned long pfn;
988                 struct page *page;
989                 void *page_virt;
990
991                 if (gpa == UNMAPPED_GVA)
992                         return X86EMUL_PROPAGATE_FAULT;
993                 pfn = gpa >> PAGE_SHIFT;
994                 page = gfn_to_page(vcpu->kvm, pfn);
995                 if (!page)
996                         return X86EMUL_UNHANDLEABLE;
997                 page_virt = kmap_atomic(page, KM_USER0);
998
999                 memcpy(data, page_virt + offset, tocopy);
1000
1001                 kunmap_atomic(page_virt, KM_USER0);
1002
1003                 bytes -= tocopy;
1004                 data += tocopy;
1005                 addr += tocopy;
1006         }
1007
1008         return X86EMUL_CONTINUE;
1009 }
1010
1011 static int emulator_write_std(unsigned long addr,
1012                               const void *val,
1013                               unsigned int bytes,
1014                               struct x86_emulate_ctxt *ctxt)
1015 {
1016         printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1017                addr, bytes);
1018         return X86EMUL_UNHANDLEABLE;
1019 }
1020
1021 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1022                                                 gpa_t addr)
1023 {
1024         /*
1025          * Note that its important to have this wrapper function because
1026          * in the very near future we will be checking for MMIOs against
1027          * the LAPIC as well as the general MMIO bus
1028          */
1029         return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1030 }
1031
1032 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
1033                                                gpa_t addr)
1034 {
1035         return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
1036 }
1037
1038 static int emulator_read_emulated(unsigned long addr,
1039                                   void *val,
1040                                   unsigned int bytes,
1041                                   struct x86_emulate_ctxt *ctxt)
1042 {
1043         struct kvm_vcpu      *vcpu = ctxt->vcpu;
1044         struct kvm_io_device *mmio_dev;
1045         gpa_t                 gpa;
1046
1047         if (vcpu->mmio_read_completed) {
1048                 memcpy(val, vcpu->mmio_data, bytes);
1049                 vcpu->mmio_read_completed = 0;
1050                 return X86EMUL_CONTINUE;
1051         } else if (emulator_read_std(addr, val, bytes, ctxt)
1052                    == X86EMUL_CONTINUE)
1053                 return X86EMUL_CONTINUE;
1054
1055         gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1056         if (gpa == UNMAPPED_GVA)
1057                 return X86EMUL_PROPAGATE_FAULT;
1058
1059         /*
1060          * Is this MMIO handled locally?
1061          */
1062         mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1063         if (mmio_dev) {
1064                 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1065                 return X86EMUL_CONTINUE;
1066         }
1067
1068         vcpu->mmio_needed = 1;
1069         vcpu->mmio_phys_addr = gpa;
1070         vcpu->mmio_size = bytes;
1071         vcpu->mmio_is_write = 0;
1072
1073         return X86EMUL_UNHANDLEABLE;
1074 }
1075
1076 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1077                                const void *val, int bytes)
1078 {
1079         struct page *page;
1080         void *virt;
1081
1082         if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1083                 return 0;
1084         page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1085         if (!page)
1086                 return 0;
1087         mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
1088         virt = kmap_atomic(page, KM_USER0);
1089         kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1090         memcpy(virt + offset_in_page(gpa), val, bytes);
1091         kunmap_atomic(virt, KM_USER0);
1092         return 1;
1093 }
1094
1095 static int emulator_write_emulated_onepage(unsigned long addr,
1096                                            const void *val,
1097                                            unsigned int bytes,
1098                                            struct x86_emulate_ctxt *ctxt)
1099 {
1100         struct kvm_vcpu      *vcpu = ctxt->vcpu;
1101         struct kvm_io_device *mmio_dev;
1102         gpa_t                 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1103
1104         if (gpa == UNMAPPED_GVA) {
1105                 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
1106                 return X86EMUL_PROPAGATE_FAULT;
1107         }
1108
1109         if (emulator_write_phys(vcpu, gpa, val, bytes))
1110                 return X86EMUL_CONTINUE;
1111
1112         /*
1113          * Is this MMIO handled locally?
1114          */
1115         mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1116         if (mmio_dev) {
1117                 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1118                 return X86EMUL_CONTINUE;
1119         }
1120
1121         vcpu->mmio_needed = 1;
1122         vcpu->mmio_phys_addr = gpa;
1123         vcpu->mmio_size = bytes;
1124         vcpu->mmio_is_write = 1;
1125         memcpy(vcpu->mmio_data, val, bytes);
1126
1127         return X86EMUL_CONTINUE;
1128 }
1129
1130 static int emulator_write_emulated(unsigned long addr,
1131                                    const void *val,
1132                                    unsigned int bytes,
1133                                    struct x86_emulate_ctxt *ctxt)
1134 {
1135         /* Crossing a page boundary? */
1136         if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1137                 int rc, now;
1138
1139                 now = -addr & ~PAGE_MASK;
1140                 rc = emulator_write_emulated_onepage(addr, val, now, ctxt);
1141                 if (rc != X86EMUL_CONTINUE)
1142                         return rc;
1143                 addr += now;
1144                 val += now;
1145                 bytes -= now;
1146         }
1147         return emulator_write_emulated_onepage(addr, val, bytes, ctxt);
1148 }
1149
1150 static int emulator_cmpxchg_emulated(unsigned long addr,
1151                                      const void *old,
1152                                      const void *new,
1153                                      unsigned int bytes,
1154                                      struct x86_emulate_ctxt *ctxt)
1155 {
1156         static int reported;
1157
1158         if (!reported) {
1159                 reported = 1;
1160                 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1161         }
1162         return emulator_write_emulated(addr, new, bytes, ctxt);
1163 }
1164
1165 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1166 {
1167         return kvm_arch_ops->get_segment_base(vcpu, seg);
1168 }
1169
1170 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1171 {
1172         return X86EMUL_CONTINUE;
1173 }
1174
1175 int emulate_clts(struct kvm_vcpu *vcpu)
1176 {
1177         unsigned long cr0;
1178
1179         cr0 = vcpu->cr0 & ~X86_CR0_TS;
1180         kvm_arch_ops->set_cr0(vcpu, cr0);
1181         return X86EMUL_CONTINUE;
1182 }
1183
1184 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1185 {
1186         struct kvm_vcpu *vcpu = ctxt->vcpu;
1187
1188         switch (dr) {
1189         case 0 ... 3:
1190                 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1191                 return X86EMUL_CONTINUE;
1192         default:
1193                 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1194                        __FUNCTION__, dr);
1195                 return X86EMUL_UNHANDLEABLE;
1196         }
1197 }
1198
1199 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1200 {
1201         unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1202         int exception;
1203
1204         kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1205         if (exception) {
1206                 /* FIXME: better handling */
1207                 return X86EMUL_UNHANDLEABLE;
1208         }
1209         return X86EMUL_CONTINUE;
1210 }
1211
1212 static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1213 {
1214         static int reported;
1215         u8 opcodes[4];
1216         unsigned long rip = ctxt->vcpu->rip;
1217         unsigned long rip_linear;
1218
1219         rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1220
1221         if (reported)
1222                 return;
1223
1224         emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1225
1226         printk(KERN_ERR "emulation failed but !mmio_needed?"
1227                " rip %lx %02x %02x %02x %02x\n",
1228                rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1229         reported = 1;
1230 }
1231
1232 struct x86_emulate_ops emulate_ops = {
1233         .read_std            = emulator_read_std,
1234         .write_std           = emulator_write_std,
1235         .read_emulated       = emulator_read_emulated,
1236         .write_emulated      = emulator_write_emulated,
1237         .cmpxchg_emulated    = emulator_cmpxchg_emulated,
1238 };
1239
1240 int emulate_instruction(struct kvm_vcpu *vcpu,
1241                         struct kvm_run *run,
1242                         unsigned long cr2,
1243                         u16 error_code)
1244 {
1245         struct x86_emulate_ctxt emulate_ctxt;
1246         int r;
1247         int cs_db, cs_l;
1248
1249         vcpu->mmio_fault_cr2 = cr2;
1250         kvm_arch_ops->cache_regs(vcpu);
1251
1252         kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1253
1254         emulate_ctxt.vcpu = vcpu;
1255         emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1256         emulate_ctxt.cr2 = cr2;
1257         emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1258                 ? X86EMUL_MODE_REAL : cs_l
1259                 ? X86EMUL_MODE_PROT64 : cs_db
1260                 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1261
1262         if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1263                 emulate_ctxt.cs_base = 0;
1264                 emulate_ctxt.ds_base = 0;
1265                 emulate_ctxt.es_base = 0;
1266                 emulate_ctxt.ss_base = 0;
1267         } else {
1268                 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1269                 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1270                 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1271                 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1272         }
1273
1274         emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1275         emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1276
1277         vcpu->mmio_is_write = 0;
1278         r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1279
1280         if ((r || vcpu->mmio_is_write) && run) {
1281                 run->exit_reason = KVM_EXIT_MMIO;
1282                 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1283                 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1284                 run->mmio.len = vcpu->mmio_size;
1285                 run->mmio.is_write = vcpu->mmio_is_write;
1286         }
1287
1288         if (r) {
1289                 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1290                         return EMULATE_DONE;
1291                 if (!vcpu->mmio_needed) {
1292                         report_emulation_failure(&emulate_ctxt);
1293                         return EMULATE_FAIL;
1294                 }
1295                 return EMULATE_DO_MMIO;
1296         }
1297
1298         kvm_arch_ops->decache_regs(vcpu);
1299         kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1300
1301         if (vcpu->mmio_is_write) {
1302                 vcpu->mmio_needed = 0;
1303                 return EMULATE_DO_MMIO;
1304         }
1305
1306         return EMULATE_DONE;
1307 }
1308 EXPORT_SYMBOL_GPL(emulate_instruction);
1309
1310 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1311 {
1312         if (vcpu->irq_summary)
1313                 return 1;
1314
1315         vcpu->run->exit_reason = KVM_EXIT_HLT;
1316         ++vcpu->stat.halt_exits;
1317         return 0;
1318 }
1319 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1320
1321 int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1322 {
1323         unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1324
1325         kvm_arch_ops->cache_regs(vcpu);
1326         ret = -KVM_EINVAL;
1327 #ifdef CONFIG_X86_64
1328         if (is_long_mode(vcpu)) {
1329                 nr = vcpu->regs[VCPU_REGS_RAX];
1330                 a0 = vcpu->regs[VCPU_REGS_RDI];
1331                 a1 = vcpu->regs[VCPU_REGS_RSI];
1332                 a2 = vcpu->regs[VCPU_REGS_RDX];
1333                 a3 = vcpu->regs[VCPU_REGS_RCX];
1334                 a4 = vcpu->regs[VCPU_REGS_R8];
1335                 a5 = vcpu->regs[VCPU_REGS_R9];
1336         } else
1337 #endif
1338         {
1339                 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1340                 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1341                 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1342                 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1343                 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1344                 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1345                 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1346         }
1347         switch (nr) {
1348         default:
1349                 run->hypercall.args[0] = a0;
1350                 run->hypercall.args[1] = a1;
1351                 run->hypercall.args[2] = a2;
1352                 run->hypercall.args[3] = a3;
1353                 run->hypercall.args[4] = a4;
1354                 run->hypercall.args[5] = a5;
1355                 run->hypercall.ret = ret;
1356                 run->hypercall.longmode = is_long_mode(vcpu);
1357                 kvm_arch_ops->decache_regs(vcpu);
1358                 return 0;
1359         }
1360         vcpu->regs[VCPU_REGS_RAX] = ret;
1361         kvm_arch_ops->decache_regs(vcpu);
1362         return 1;
1363 }
1364 EXPORT_SYMBOL_GPL(kvm_hypercall);
1365
1366 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1367 {
1368         return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1369 }
1370
1371 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1372 {
1373         struct descriptor_table dt = { limit, base };
1374
1375         kvm_arch_ops->set_gdt(vcpu, &dt);
1376 }
1377
1378 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1379 {
1380         struct descriptor_table dt = { limit, base };
1381
1382         kvm_arch_ops->set_idt(vcpu, &dt);
1383 }
1384
1385 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1386                    unsigned long *rflags)
1387 {
1388         lmsw(vcpu, msw);
1389         *rflags = kvm_arch_ops->get_rflags(vcpu);
1390 }
1391
1392 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1393 {
1394         kvm_arch_ops->decache_cr4_guest_bits(vcpu);
1395         switch (cr) {
1396         case 0:
1397                 return vcpu->cr0;
1398         case 2:
1399                 return vcpu->cr2;
1400         case 3:
1401                 return vcpu->cr3;
1402         case 4:
1403                 return vcpu->cr4;
1404         default:
1405                 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1406                 return 0;
1407         }
1408 }
1409
1410 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1411                      unsigned long *rflags)
1412 {
1413         switch (cr) {
1414         case 0:
1415                 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1416                 *rflags = kvm_arch_ops->get_rflags(vcpu);
1417                 break;
1418         case 2:
1419                 vcpu->cr2 = val;
1420                 break;
1421         case 3:
1422                 set_cr3(vcpu, val);
1423                 break;
1424         case 4:
1425                 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1426                 break;
1427         default:
1428                 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1429         }
1430 }
1431
1432 /*
1433  * Register the para guest with the host:
1434  */
1435 static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1436 {
1437         struct kvm_vcpu_para_state *para_state;
1438         hpa_t para_state_hpa, hypercall_hpa;
1439         struct page *para_state_page;
1440         unsigned char *hypercall;
1441         gpa_t hypercall_gpa;
1442
1443         printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1444         printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1445
1446         /*
1447          * Needs to be page aligned:
1448          */
1449         if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1450                 goto err_gp;
1451
1452         para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1453         printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1454         if (is_error_hpa(para_state_hpa))
1455                 goto err_gp;
1456
1457         mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
1458         para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1459         para_state = kmap(para_state_page);
1460
1461         printk(KERN_DEBUG "....  guest version: %d\n", para_state->guest_version);
1462         printk(KERN_DEBUG "....           size: %d\n", para_state->size);
1463
1464         para_state->host_version = KVM_PARA_API_VERSION;
1465         /*
1466          * We cannot support guests that try to register themselves
1467          * with a newer API version than the host supports:
1468          */
1469         if (para_state->guest_version > KVM_PARA_API_VERSION) {
1470                 para_state->ret = -KVM_EINVAL;
1471                 goto err_kunmap_skip;
1472         }
1473
1474         hypercall_gpa = para_state->hypercall_gpa;
1475         hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1476         printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1477         if (is_error_hpa(hypercall_hpa)) {
1478                 para_state->ret = -KVM_EINVAL;
1479                 goto err_kunmap_skip;
1480         }
1481
1482         printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1483         vcpu->para_state_page = para_state_page;
1484         vcpu->para_state_gpa = para_state_gpa;
1485         vcpu->hypercall_gpa = hypercall_gpa;
1486
1487         mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
1488         hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1489                                 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1490         kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1491         kunmap_atomic(hypercall, KM_USER1);
1492
1493         para_state->ret = 0;
1494 err_kunmap_skip:
1495         kunmap(para_state_page);
1496         return 0;
1497 err_gp:
1498         return 1;
1499 }
1500
1501 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1502 {
1503         u64 data;
1504
1505         switch (msr) {
1506         case 0xc0010010: /* SYSCFG */
1507         case 0xc0010015: /* HWCR */
1508         case MSR_IA32_PLATFORM_ID:
1509         case MSR_IA32_P5_MC_ADDR:
1510         case MSR_IA32_P5_MC_TYPE:
1511         case MSR_IA32_MC0_CTL:
1512         case MSR_IA32_MCG_STATUS:
1513         case MSR_IA32_MCG_CAP:
1514         case MSR_IA32_MC0_MISC:
1515         case MSR_IA32_MC0_MISC+4:
1516         case MSR_IA32_MC0_MISC+8:
1517         case MSR_IA32_MC0_MISC+12:
1518         case MSR_IA32_MC0_MISC+16:
1519         case MSR_IA32_UCODE_REV:
1520         case MSR_IA32_PERF_STATUS:
1521         case MSR_IA32_EBL_CR_POWERON:
1522                 /* MTRR registers */
1523         case 0xfe:
1524         case 0x200 ... 0x2ff:
1525                 data = 0;
1526                 break;
1527         case 0xcd: /* fsb frequency */
1528                 data = 3;
1529                 break;
1530         case MSR_IA32_APICBASE:
1531                 data = vcpu->apic_base;
1532                 break;
1533         case MSR_IA32_MISC_ENABLE:
1534                 data = vcpu->ia32_misc_enable_msr;
1535                 break;
1536 #ifdef CONFIG_X86_64
1537         case MSR_EFER:
1538                 data = vcpu->shadow_efer;
1539                 break;
1540 #endif
1541         default:
1542                 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1543                 return 1;
1544         }
1545         *pdata = data;
1546         return 0;
1547 }
1548 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1549
1550 /*
1551  * Reads an msr value (of 'msr_index') into 'pdata'.
1552  * Returns 0 on success, non-0 otherwise.
1553  * Assumes vcpu_load() was already called.
1554  */
1555 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1556 {
1557         return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1558 }
1559
1560 #ifdef CONFIG_X86_64
1561
1562 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1563 {
1564         if (efer & EFER_RESERVED_BITS) {
1565                 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1566                        efer);
1567                 inject_gp(vcpu);
1568                 return;
1569         }
1570
1571         if (is_paging(vcpu)
1572             && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1573                 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1574                 inject_gp(vcpu);
1575                 return;
1576         }
1577
1578         kvm_arch_ops->set_efer(vcpu, efer);
1579
1580         efer &= ~EFER_LMA;
1581         efer |= vcpu->shadow_efer & EFER_LMA;
1582
1583         vcpu->shadow_efer = efer;
1584 }
1585
1586 #endif
1587
1588 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1589 {
1590         switch (msr) {
1591 #ifdef CONFIG_X86_64
1592         case MSR_EFER:
1593                 set_efer(vcpu, data);
1594                 break;
1595 #endif
1596         case MSR_IA32_MC0_STATUS:
1597                 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1598                        __FUNCTION__, data);
1599                 break;
1600         case MSR_IA32_MCG_STATUS:
1601                 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1602                         __FUNCTION__, data);
1603                 break;
1604         case MSR_IA32_UCODE_REV:
1605         case MSR_IA32_UCODE_WRITE:
1606         case 0x200 ... 0x2ff: /* MTRRs */
1607                 break;
1608         case MSR_IA32_APICBASE:
1609                 vcpu->apic_base = data;
1610                 break;
1611         case MSR_IA32_MISC_ENABLE:
1612                 vcpu->ia32_misc_enable_msr = data;
1613                 break;
1614         /*
1615          * This is the 'probe whether the host is KVM' logic:
1616          */
1617         case MSR_KVM_API_MAGIC:
1618                 return vcpu_register_para(vcpu, data);
1619
1620         default:
1621                 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1622                 return 1;
1623         }
1624         return 0;
1625 }
1626 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1627
1628 /*
1629  * Writes msr value into into the appropriate "register".
1630  * Returns 0 on success, non-0 otherwise.
1631  * Assumes vcpu_load() was already called.
1632  */
1633 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1634 {
1635         return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1636 }
1637
1638 void kvm_resched(struct kvm_vcpu *vcpu)
1639 {
1640         if (!need_resched())
1641                 return;
1642         vcpu_put(vcpu);
1643         cond_resched();
1644         vcpu_load(vcpu);
1645 }
1646 EXPORT_SYMBOL_GPL(kvm_resched);
1647
1648 void load_msrs(struct vmx_msr_entry *e, int n)
1649 {
1650         int i;
1651
1652         for (i = 0; i < n; ++i)
1653                 wrmsrl(e[i].index, e[i].data);
1654 }
1655 EXPORT_SYMBOL_GPL(load_msrs);
1656
1657 void save_msrs(struct vmx_msr_entry *e, int n)
1658 {
1659         int i;
1660
1661         for (i = 0; i < n; ++i)
1662                 rdmsrl(e[i].index, e[i].data);
1663 }
1664 EXPORT_SYMBOL_GPL(save_msrs);
1665
1666 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1667 {
1668         int i;
1669         u32 function;
1670         struct kvm_cpuid_entry *e, *best;
1671
1672         kvm_arch_ops->cache_regs(vcpu);
1673         function = vcpu->regs[VCPU_REGS_RAX];
1674         vcpu->regs[VCPU_REGS_RAX] = 0;
1675         vcpu->regs[VCPU_REGS_RBX] = 0;
1676         vcpu->regs[VCPU_REGS_RCX] = 0;
1677         vcpu->regs[VCPU_REGS_RDX] = 0;
1678         best = NULL;
1679         for (i = 0; i < vcpu->cpuid_nent; ++i) {
1680                 e = &vcpu->cpuid_entries[i];
1681                 if (e->function == function) {
1682                         best = e;
1683                         break;
1684                 }
1685                 /*
1686                  * Both basic or both extended?
1687                  */
1688                 if (((e->function ^ function) & 0x80000000) == 0)
1689                         if (!best || e->function > best->function)
1690                                 best = e;
1691         }
1692         if (best) {
1693                 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1694                 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1695                 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1696                 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1697         }
1698         kvm_arch_ops->decache_regs(vcpu);
1699         kvm_arch_ops->skip_emulated_instruction(vcpu);
1700 }
1701 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1702
1703 static int pio_copy_data(struct kvm_vcpu *vcpu)
1704 {
1705         void *p = vcpu->pio_data;
1706         void *q;
1707         unsigned bytes;
1708         int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1709
1710         kvm_arch_ops->vcpu_put(vcpu);
1711         q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1712                  PAGE_KERNEL);
1713         if (!q) {
1714                 kvm_arch_ops->vcpu_load(vcpu);
1715                 free_pio_guest_pages(vcpu);
1716                 return -ENOMEM;
1717         }
1718         q += vcpu->pio.guest_page_offset;
1719         bytes = vcpu->pio.size * vcpu->pio.cur_count;
1720         if (vcpu->pio.in)
1721                 memcpy(q, p, bytes);
1722         else
1723                 memcpy(p, q, bytes);
1724         q -= vcpu->pio.guest_page_offset;
1725         vunmap(q);
1726         kvm_arch_ops->vcpu_load(vcpu);
1727         free_pio_guest_pages(vcpu);
1728         return 0;
1729 }
1730
1731 static int complete_pio(struct kvm_vcpu *vcpu)
1732 {
1733         struct kvm_pio_request *io = &vcpu->pio;
1734         long delta;
1735         int r;
1736
1737         kvm_arch_ops->cache_regs(vcpu);
1738
1739         if (!io->string) {
1740                 if (io->in)
1741                         memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
1742                                io->size);
1743         } else {
1744                 if (io->in) {
1745                         r = pio_copy_data(vcpu);
1746                         if (r) {
1747                                 kvm_arch_ops->cache_regs(vcpu);
1748                                 return r;
1749                         }
1750                 }
1751
1752                 delta = 1;
1753                 if (io->rep) {
1754                         delta *= io->cur_count;
1755                         /*
1756                          * The size of the register should really depend on
1757                          * current address size.
1758                          */
1759                         vcpu->regs[VCPU_REGS_RCX] -= delta;
1760                 }
1761                 if (io->down)
1762                         delta = -delta;
1763                 delta *= io->size;
1764                 if (io->in)
1765                         vcpu->regs[VCPU_REGS_RDI] += delta;
1766                 else
1767                         vcpu->regs[VCPU_REGS_RSI] += delta;
1768         }
1769
1770         kvm_arch_ops->decache_regs(vcpu);
1771
1772         io->count -= io->cur_count;
1773         io->cur_count = 0;
1774
1775         if (!io->count)
1776                 kvm_arch_ops->skip_emulated_instruction(vcpu);
1777         return 0;
1778 }
1779
1780 static void kernel_pio(struct kvm_io_device *pio_dev,
1781                        struct kvm_vcpu *vcpu,
1782                        void *pd)
1783 {
1784         /* TODO: String I/O for in kernel device */
1785
1786         if (vcpu->pio.in)
1787                 kvm_iodevice_read(pio_dev, vcpu->pio.port,
1788                                   vcpu->pio.size,
1789                                   pd);
1790         else
1791                 kvm_iodevice_write(pio_dev, vcpu->pio.port,
1792                                    vcpu->pio.size,
1793                                    pd);
1794 }
1795
1796 static void pio_string_write(struct kvm_io_device *pio_dev,
1797                              struct kvm_vcpu *vcpu)
1798 {
1799         struct kvm_pio_request *io = &vcpu->pio;
1800         void *pd = vcpu->pio_data;
1801         int i;
1802
1803         for (i = 0; i < io->cur_count; i++) {
1804                 kvm_iodevice_write(pio_dev, io->port,
1805                                    io->size,
1806                                    pd);
1807                 pd += io->size;
1808         }
1809 }
1810
1811 int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1812                   int size, unsigned long count, int string, int down,
1813                   gva_t address, int rep, unsigned port)
1814 {
1815         unsigned now, in_page;
1816         int i, ret = 0;
1817         int nr_pages = 1;
1818         struct page *page;
1819         struct kvm_io_device *pio_dev;
1820
1821         vcpu->run->exit_reason = KVM_EXIT_IO;
1822         vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1823         vcpu->run->io.size = size;
1824         vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1825         vcpu->run->io.count = count;
1826         vcpu->run->io.port = port;
1827         vcpu->pio.count = count;
1828         vcpu->pio.cur_count = count;
1829         vcpu->pio.size = size;
1830         vcpu->pio.in = in;
1831         vcpu->pio.port = port;
1832         vcpu->pio.string = string;
1833         vcpu->pio.down = down;
1834         vcpu->pio.guest_page_offset = offset_in_page(address);
1835         vcpu->pio.rep = rep;
1836
1837         pio_dev = vcpu_find_pio_dev(vcpu, port);
1838         if (!string) {
1839                 kvm_arch_ops->cache_regs(vcpu);
1840                 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1841                 kvm_arch_ops->decache_regs(vcpu);
1842                 if (pio_dev) {
1843                         kernel_pio(pio_dev, vcpu, vcpu->pio_data);
1844                         complete_pio(vcpu);
1845                         return 1;
1846                 }
1847                 return 0;
1848         }
1849
1850         if (!count) {
1851                 kvm_arch_ops->skip_emulated_instruction(vcpu);
1852                 return 1;
1853         }
1854
1855         now = min(count, PAGE_SIZE / size);
1856
1857         if (!down)
1858                 in_page = PAGE_SIZE - offset_in_page(address);
1859         else
1860                 in_page = offset_in_page(address) + size;
1861         now = min(count, (unsigned long)in_page / size);
1862         if (!now) {
1863                 /*
1864                  * String I/O straddles page boundary.  Pin two guest pages
1865                  * so that we satisfy atomicity constraints.  Do just one
1866                  * transaction to avoid complexity.
1867                  */
1868                 nr_pages = 2;
1869                 now = 1;
1870         }
1871         if (down) {
1872                 /*
1873                  * String I/O in reverse.  Yuck.  Kill the guest, fix later.
1874                  */
1875                 printk(KERN_ERR "kvm: guest string pio down\n");
1876                 inject_gp(vcpu);
1877                 return 1;
1878         }
1879         vcpu->run->io.count = now;
1880         vcpu->pio.cur_count = now;
1881
1882         for (i = 0; i < nr_pages; ++i) {
1883                 spin_lock(&vcpu->kvm->lock);
1884                 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1885                 if (page)
1886                         get_page(page);
1887                 vcpu->pio.guest_pages[i] = page;
1888                 spin_unlock(&vcpu->kvm->lock);
1889                 if (!page) {
1890                         inject_gp(vcpu);
1891                         free_pio_guest_pages(vcpu);
1892                         return 1;
1893                 }
1894         }
1895
1896         if (!vcpu->pio.in) {
1897                 /* string PIO write */
1898                 ret = pio_copy_data(vcpu);
1899                 if (ret >= 0 && pio_dev) {
1900                         pio_string_write(pio_dev, vcpu);
1901                         complete_pio(vcpu);
1902                         if (vcpu->pio.count == 0)
1903                                 ret = 1;
1904                 }
1905         } else if (pio_dev)
1906                 printk(KERN_ERR "no string pio read support yet, "
1907                        "port %x size %d count %ld\n",
1908                         port, size, count);
1909
1910         return ret;
1911 }
1912 EXPORT_SYMBOL_GPL(kvm_setup_pio);
1913
1914 static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1915 {
1916         int r;
1917         sigset_t sigsaved;
1918
1919         vcpu_load(vcpu);
1920
1921         if (vcpu->sigset_active)
1922                 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1923
1924         /* re-sync apic's tpr */
1925         vcpu->cr8 = kvm_run->cr8;
1926
1927         if (vcpu->pio.cur_count) {
1928                 r = complete_pio(vcpu);
1929                 if (r)
1930                         goto out;
1931         }
1932
1933         if (vcpu->mmio_needed) {
1934                 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1935                 vcpu->mmio_read_completed = 1;
1936                 vcpu->mmio_needed = 0;
1937                 r = emulate_instruction(vcpu, kvm_run,
1938                                         vcpu->mmio_fault_cr2, 0);
1939                 if (r == EMULATE_DO_MMIO) {
1940                         /*
1941                          * Read-modify-write.  Back to userspace.
1942                          */
1943                         r = 0;
1944                         goto out;
1945                 }
1946         }
1947
1948         if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
1949                 kvm_arch_ops->cache_regs(vcpu);
1950                 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1951                 kvm_arch_ops->decache_regs(vcpu);
1952         }
1953
1954         r = kvm_arch_ops->run(vcpu, kvm_run);
1955
1956 out:
1957         if (vcpu->sigset_active)
1958                 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1959
1960         vcpu_put(vcpu);
1961         return r;
1962 }
1963
1964 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1965                                    struct kvm_regs *regs)
1966 {
1967         vcpu_load(vcpu);
1968
1969         kvm_arch_ops->cache_regs(vcpu);
1970
1971         regs->rax = vcpu->regs[VCPU_REGS_RAX];
1972         regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1973         regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1974         regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1975         regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1976         regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1977         regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1978         regs->rbp = vcpu->regs[VCPU_REGS_RBP];
1979 #ifdef CONFIG_X86_64
1980         regs->r8 = vcpu->regs[VCPU_REGS_R8];
1981         regs->r9 = vcpu->regs[VCPU_REGS_R9];
1982         regs->r10 = vcpu->regs[VCPU_REGS_R10];
1983         regs->r11 = vcpu->regs[VCPU_REGS_R11];
1984         regs->r12 = vcpu->regs[VCPU_REGS_R12];
1985         regs->r13 = vcpu->regs[VCPU_REGS_R13];
1986         regs->r14 = vcpu->regs[VCPU_REGS_R14];
1987         regs->r15 = vcpu->regs[VCPU_REGS_R15];
1988 #endif
1989
1990         regs->rip = vcpu->rip;
1991         regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1992
1993         /*
1994          * Don't leak debug flags in case they were set for guest debugging
1995          */
1996         if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1997                 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1998
1999         vcpu_put(vcpu);
2000
2001         return 0;
2002 }
2003
2004 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2005                                    struct kvm_regs *regs)
2006 {
2007         vcpu_load(vcpu);
2008
2009         vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2010         vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2011         vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2012         vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2013         vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2014         vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2015         vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2016         vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
2017 #ifdef CONFIG_X86_64
2018         vcpu->regs[VCPU_REGS_R8] = regs->r8;
2019         vcpu->regs[VCPU_REGS_R9] = regs->r9;
2020         vcpu->regs[VCPU_REGS_R10] = regs->r10;
2021         vcpu->regs[VCPU_REGS_R11] = regs->r11;
2022         vcpu->regs[VCPU_REGS_R12] = regs->r12;
2023         vcpu->regs[VCPU_REGS_R13] = regs->r13;
2024         vcpu->regs[VCPU_REGS_R14] = regs->r14;
2025         vcpu->regs[VCPU_REGS_R15] = regs->r15;
2026 #endif
2027
2028         vcpu->rip = regs->rip;
2029         kvm_arch_ops->set_rflags(vcpu, regs->rflags);
2030
2031         kvm_arch_ops->decache_regs(vcpu);
2032
2033         vcpu_put(vcpu);
2034
2035         return 0;
2036 }
2037
2038 static void get_segment(struct kvm_vcpu *vcpu,
2039                         struct kvm_segment *var, int seg)
2040 {
2041         return kvm_arch_ops->get_segment(vcpu, var, seg);
2042 }
2043
2044 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2045                                     struct kvm_sregs *sregs)
2046 {
2047         struct descriptor_table dt;
2048
2049         vcpu_load(vcpu);
2050
2051         get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2052         get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2053         get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2054         get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2055         get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2056         get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2057
2058         get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2059         get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2060
2061         kvm_arch_ops->get_idt(vcpu, &dt);
2062         sregs->idt.limit = dt.limit;
2063         sregs->idt.base = dt.base;
2064         kvm_arch_ops->get_gdt(vcpu, &dt);
2065         sregs->gdt.limit = dt.limit;
2066         sregs->gdt.base = dt.base;
2067
2068         kvm_arch_ops->decache_cr4_guest_bits(vcpu);
2069         sregs->cr0 = vcpu->cr0;
2070         sregs->cr2 = vcpu->cr2;
2071         sregs->cr3 = vcpu->cr3;
2072         sregs->cr4 = vcpu->cr4;
2073         sregs->cr8 = vcpu->cr8;
2074         sregs->efer = vcpu->shadow_efer;
2075         sregs->apic_base = vcpu->apic_base;
2076
2077         memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2078                sizeof sregs->interrupt_bitmap);
2079
2080         vcpu_put(vcpu);
2081
2082         return 0;
2083 }
2084
2085 static void set_segment(struct kvm_vcpu *vcpu,
2086                         struct kvm_segment *var, int seg)
2087 {
2088         return kvm_arch_ops->set_segment(vcpu, var, seg);
2089 }
2090
2091 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2092                                     struct kvm_sregs *sregs)
2093 {
2094         int mmu_reset_needed = 0;
2095         int i;
2096         struct descriptor_table dt;
2097
2098         vcpu_load(vcpu);
2099
2100         dt.limit = sregs->idt.limit;
2101         dt.base = sregs->idt.base;
2102         kvm_arch_ops->set_idt(vcpu, &dt);
2103         dt.limit = sregs->gdt.limit;
2104         dt.base = sregs->gdt.base;
2105         kvm_arch_ops->set_gdt(vcpu, &dt);
2106
2107         vcpu->cr2 = sregs->cr2;
2108         mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2109         vcpu->cr3 = sregs->cr3;
2110
2111         vcpu->cr8 = sregs->cr8;
2112
2113         mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
2114 #ifdef CONFIG_X86_64
2115         kvm_arch_ops->set_efer(vcpu, sregs->efer);
2116 #endif
2117         vcpu->apic_base = sregs->apic_base;
2118
2119         kvm_arch_ops->decache_cr4_guest_bits(vcpu);
2120
2121         mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
2122         kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
2123
2124         mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2125         kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
2126         if (!is_long_mode(vcpu) && is_pae(vcpu))
2127                 load_pdptrs(vcpu, vcpu->cr3);
2128
2129         if (mmu_reset_needed)
2130                 kvm_mmu_reset_context(vcpu);
2131
2132         memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2133                sizeof vcpu->irq_pending);
2134         vcpu->irq_summary = 0;
2135         for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
2136                 if (vcpu->irq_pending[i])
2137                         __set_bit(i, &vcpu->irq_summary);
2138
2139         set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2140         set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2141         set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2142         set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2143         set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2144         set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2145
2146         set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2147         set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2148
2149         vcpu_put(vcpu);
2150
2151         return 0;
2152 }
2153
2154 /*
2155  * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2156  * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2157  *
2158  * This list is modified at module load time to reflect the
2159  * capabilities of the host cpu.
2160  */
2161 static u32 msrs_to_save[] = {
2162         MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2163         MSR_K6_STAR,
2164 #ifdef CONFIG_X86_64
2165         MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2166 #endif
2167         MSR_IA32_TIME_STAMP_COUNTER,
2168 };
2169
2170 static unsigned num_msrs_to_save;
2171
2172 static u32 emulated_msrs[] = {
2173         MSR_IA32_MISC_ENABLE,
2174 };
2175
2176 static __init void kvm_init_msr_list(void)
2177 {
2178         u32 dummy[2];
2179         unsigned i, j;
2180
2181         for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2182                 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2183                         continue;
2184                 if (j < i)
2185                         msrs_to_save[j] = msrs_to_save[i];
2186                 j++;
2187         }
2188         num_msrs_to_save = j;
2189 }
2190
2191 /*
2192  * Adapt set_msr() to msr_io()'s calling convention
2193  */
2194 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2195 {
2196         return kvm_set_msr(vcpu, index, *data);
2197 }
2198
2199 /*
2200  * Read or write a bunch of msrs. All parameters are kernel addresses.
2201  *
2202  * @return number of msrs set successfully.
2203  */
2204 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2205                     struct kvm_msr_entry *entries,
2206                     int (*do_msr)(struct kvm_vcpu *vcpu,
2207                                   unsigned index, u64 *data))
2208 {
2209         int i;
2210
2211         vcpu_load(vcpu);
2212
2213         for (i = 0; i < msrs->nmsrs; ++i)
2214                 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2215                         break;
2216
2217         vcpu_put(vcpu);
2218
2219         return i;
2220 }
2221
2222 /*
2223  * Read or write a bunch of msrs. Parameters are user addresses.
2224  *
2225  * @return number of msrs set successfully.
2226  */
2227 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2228                   int (*do_msr)(struct kvm_vcpu *vcpu,
2229                                 unsigned index, u64 *data),
2230                   int writeback)
2231 {
2232         struct kvm_msrs msrs;
2233         struct kvm_msr_entry *entries;
2234         int r, n;
2235         unsigned size;
2236
2237         r = -EFAULT;
2238         if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2239                 goto out;
2240
2241         r = -E2BIG;
2242         if (msrs.nmsrs >= MAX_IO_MSRS)
2243                 goto out;
2244
2245         r = -ENOMEM;
2246         size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2247         entries = vmalloc(size);
2248         if (!entries)
2249                 goto out;
2250
2251         r = -EFAULT;
2252         if (copy_from_user(entries, user_msrs->entries, size))
2253                 goto out_free;
2254
2255         r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2256         if (r < 0)
2257                 goto out_free;
2258
2259         r = -EFAULT;
2260         if (writeback && copy_to_user(user_msrs->entries, entries, size))
2261                 goto out_free;
2262
2263         r = n;
2264
2265 out_free:
2266         vfree(entries);
2267 out:
2268         return r;
2269 }
2270
2271 /*
2272  * Translate a guest virtual address to a guest physical address.
2273  */
2274 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2275                                     struct kvm_translation *tr)
2276 {
2277         unsigned long vaddr = tr->linear_address;
2278         gpa_t gpa;
2279
2280         vcpu_load(vcpu);
2281         spin_lock(&vcpu->kvm->lock);
2282         gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2283         tr->physical_address = gpa;
2284         tr->valid = gpa != UNMAPPED_GVA;
2285         tr->writeable = 1;
2286         tr->usermode = 0;
2287         spin_unlock(&vcpu->kvm->lock);
2288         vcpu_put(vcpu);
2289
2290         return 0;
2291 }
2292
2293 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2294                                     struct kvm_interrupt *irq)
2295 {
2296         if (irq->irq < 0 || irq->irq >= 256)
2297                 return -EINVAL;
2298         vcpu_load(vcpu);
2299
2300         set_bit(irq->irq, vcpu->irq_pending);
2301         set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2302
2303         vcpu_put(vcpu);
2304
2305         return 0;
2306 }
2307
2308 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2309                                       struct kvm_debug_guest *dbg)
2310 {
2311         int r;
2312
2313         vcpu_load(vcpu);
2314
2315         r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2316
2317         vcpu_put(vcpu);
2318
2319         return r;
2320 }
2321
2322 static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2323                                     unsigned long address,
2324                                     int *type)
2325 {
2326         struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2327         unsigned long pgoff;
2328         struct page *page;
2329
2330         pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2331         if (pgoff == 0)
2332                 page = virt_to_page(vcpu->run);
2333         else if (pgoff == KVM_PIO_PAGE_OFFSET)
2334                 page = virt_to_page(vcpu->pio_data);
2335         else
2336                 return NOPAGE_SIGBUS;
2337         get_page(page);
2338         if (type != NULL)
2339                 *type = VM_FAULT_MINOR;
2340
2341         return page;
2342 }
2343
2344 static struct vm_operations_struct kvm_vcpu_vm_ops = {
2345         .nopage = kvm_vcpu_nopage,
2346 };
2347
2348 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2349 {
2350         vma->vm_ops = &kvm_vcpu_vm_ops;
2351         return 0;
2352 }
2353
2354 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2355 {
2356         struct kvm_vcpu *vcpu = filp->private_data;
2357
2358         fput(vcpu->kvm->filp);
2359         return 0;
2360 }
2361
2362 static struct file_operations kvm_vcpu_fops = {
2363         .release        = kvm_vcpu_release,
2364         .unlocked_ioctl = kvm_vcpu_ioctl,
2365         .compat_ioctl   = kvm_vcpu_ioctl,
2366         .mmap           = kvm_vcpu_mmap,
2367 };
2368
2369 /*
2370  * Allocates an inode for the vcpu.
2371  */
2372 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2373 {
2374         int fd, r;
2375         struct inode *inode;
2376         struct file *file;
2377
2378         r = anon_inode_getfd(&fd, &inode, &file,
2379                              "kvm-vcpu", &kvm_vcpu_fops, vcpu);
2380         if (r)
2381                 return r;
2382         atomic_inc(&vcpu->kvm->filp->f_count);
2383         return fd;
2384 }
2385
2386 /*
2387  * Creates some virtual cpus.  Good luck creating more than one.
2388  */
2389 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2390 {
2391         int r;
2392         struct kvm_vcpu *vcpu;
2393         struct page *page;
2394
2395         r = -EINVAL;
2396         if (!valid_vcpu(n))
2397                 goto out;
2398
2399         vcpu = &kvm->vcpus[n];
2400         vcpu->vcpu_id = n;
2401
2402         mutex_lock(&vcpu->mutex);
2403
2404         if (vcpu->vmcs) {
2405                 mutex_unlock(&vcpu->mutex);
2406                 return -EEXIST;
2407         }
2408
2409         page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2410         r = -ENOMEM;
2411         if (!page)
2412                 goto out_unlock;
2413         vcpu->run = page_address(page);
2414
2415         page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2416         r = -ENOMEM;
2417         if (!page)
2418                 goto out_free_run;
2419         vcpu->pio_data = page_address(page);
2420
2421         vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2422                                            FX_IMAGE_ALIGN);
2423         vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
2424         vcpu->cr0 = 0x10;
2425
2426         r = kvm_arch_ops->vcpu_create(vcpu);
2427         if (r < 0)
2428                 goto out_free_vcpus;
2429
2430         r = kvm_mmu_create(vcpu);
2431         if (r < 0)
2432                 goto out_free_vcpus;
2433
2434         kvm_arch_ops->vcpu_load(vcpu);
2435         r = kvm_mmu_setup(vcpu);
2436         if (r >= 0)
2437                 r = kvm_arch_ops->vcpu_setup(vcpu);
2438         vcpu_put(vcpu);
2439
2440         if (r < 0)
2441                 goto out_free_vcpus;
2442
2443         r = create_vcpu_fd(vcpu);
2444         if (r < 0)
2445                 goto out_free_vcpus;
2446
2447         spin_lock(&kvm_lock);
2448         if (n >= kvm->nvcpus)
2449                 kvm->nvcpus = n + 1;
2450         spin_unlock(&kvm_lock);
2451
2452         return r;
2453
2454 out_free_vcpus:
2455         kvm_free_vcpu(vcpu);
2456 out_free_run:
2457         free_page((unsigned long)vcpu->run);
2458         vcpu->run = NULL;
2459 out_unlock:
2460         mutex_unlock(&vcpu->mutex);
2461 out:
2462         return r;
2463 }
2464
2465 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2466 {
2467         u64 efer;
2468         int i;
2469         struct kvm_cpuid_entry *e, *entry;
2470
2471         rdmsrl(MSR_EFER, efer);
2472         entry = NULL;
2473         for (i = 0; i < vcpu->cpuid_nent; ++i) {
2474                 e = &vcpu->cpuid_entries[i];
2475                 if (e->function == 0x80000001) {
2476                         entry = e;
2477                         break;
2478                 }
2479         }
2480         if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
2481                 entry->edx &= ~(1 << 20);
2482                 printk(KERN_INFO "kvm: guest NX capability removed\n");
2483         }
2484 }
2485
2486 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2487                                     struct kvm_cpuid *cpuid,
2488                                     struct kvm_cpuid_entry __user *entries)
2489 {
2490         int r;
2491
2492         r = -E2BIG;
2493         if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2494                 goto out;
2495         r = -EFAULT;
2496         if (copy_from_user(&vcpu->cpuid_entries, entries,
2497                            cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2498                 goto out;
2499         vcpu->cpuid_nent = cpuid->nent;
2500         cpuid_fix_nx_cap(vcpu);
2501         return 0;
2502
2503 out:
2504         return r;
2505 }
2506
2507 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2508 {
2509         if (sigset) {
2510                 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2511                 vcpu->sigset_active = 1;
2512                 vcpu->sigset = *sigset;
2513         } else
2514                 vcpu->sigset_active = 0;
2515         return 0;
2516 }
2517
2518 /*
2519  * fxsave fpu state.  Taken from x86_64/processor.h.  To be killed when
2520  * we have asm/x86/processor.h
2521  */
2522 struct fxsave {
2523         u16     cwd;
2524         u16     swd;
2525         u16     twd;
2526         u16     fop;
2527         u64     rip;
2528         u64     rdp;
2529         u32     mxcsr;
2530         u32     mxcsr_mask;
2531         u32     st_space[32];   /* 8*16 bytes for each FP-reg = 128 bytes */
2532 #ifdef CONFIG_X86_64
2533         u32     xmm_space[64];  /* 16*16 bytes for each XMM-reg = 256 bytes */
2534 #else
2535         u32     xmm_space[32];  /* 8*16 bytes for each XMM-reg = 128 bytes */
2536 #endif
2537 };
2538
2539 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2540 {
2541         struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2542
2543         vcpu_load(vcpu);
2544
2545         memcpy(fpu->fpr, fxsave->st_space, 128);
2546         fpu->fcw = fxsave->cwd;
2547         fpu->fsw = fxsave->swd;
2548         fpu->ftwx = fxsave->twd;
2549         fpu->last_opcode = fxsave->fop;
2550         fpu->last_ip = fxsave->rip;
2551         fpu->last_dp = fxsave->rdp;
2552         memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2553
2554         vcpu_put(vcpu);
2555
2556         return 0;
2557 }
2558
2559 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2560 {
2561         struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2562
2563         vcpu_load(vcpu);
2564
2565         memcpy(fxsave->st_space, fpu->fpr, 128);
2566         fxsave->cwd = fpu->fcw;
2567         fxsave->swd = fpu->fsw;
2568         fxsave->twd = fpu->ftwx;
2569         fxsave->fop = fpu->last_opcode;
2570         fxsave->rip = fpu->last_ip;
2571         fxsave->rdp = fpu->last_dp;
2572         memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2573
2574         vcpu_put(vcpu);
2575
2576         return 0;
2577 }
2578
2579 static long kvm_vcpu_ioctl(struct file *filp,
2580                            unsigned int ioctl, unsigned long arg)
2581 {
2582         struct kvm_vcpu *vcpu = filp->private_data;
2583         void __user *argp = (void __user *)arg;
2584         int r = -EINVAL;
2585
2586         switch (ioctl) {
2587         case KVM_RUN:
2588                 r = -EINVAL;
2589                 if (arg)
2590                         goto out;
2591                 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
2592                 break;
2593         case KVM_GET_REGS: {
2594                 struct kvm_regs kvm_regs;
2595
2596                 memset(&kvm_regs, 0, sizeof kvm_regs);
2597                 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
2598                 if (r)
2599                         goto out;
2600                 r = -EFAULT;
2601                 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
2602                         goto out;
2603                 r = 0;
2604                 break;
2605         }
2606         case KVM_SET_REGS: {
2607                 struct kvm_regs kvm_regs;
2608
2609                 r = -EFAULT;
2610                 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
2611                         goto out;
2612                 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
2613                 if (r)
2614                         goto out;
2615                 r = 0;
2616                 break;
2617         }
2618         case KVM_GET_SREGS: {
2619                 struct kvm_sregs kvm_sregs;
2620
2621                 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2622                 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
2623                 if (r)
2624                         goto out;
2625                 r = -EFAULT;
2626                 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
2627                         goto out;
2628                 r = 0;
2629                 break;
2630         }
2631         case KVM_SET_SREGS: {
2632                 struct kvm_sregs kvm_sregs;
2633
2634                 r = -EFAULT;
2635                 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
2636                         goto out;
2637                 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
2638                 if (r)
2639                         goto out;
2640                 r = 0;
2641                 break;
2642         }
2643         case KVM_TRANSLATE: {
2644                 struct kvm_translation tr;
2645
2646                 r = -EFAULT;
2647                 if (copy_from_user(&tr, argp, sizeof tr))
2648                         goto out;
2649                 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
2650                 if (r)
2651                         goto out;
2652                 r = -EFAULT;
2653                 if (copy_to_user(argp, &tr, sizeof tr))
2654                         goto out;
2655                 r = 0;
2656                 break;
2657         }
2658         case KVM_INTERRUPT: {
2659                 struct kvm_interrupt irq;
2660
2661                 r = -EFAULT;
2662                 if (copy_from_user(&irq, argp, sizeof irq))
2663                         goto out;
2664                 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2665                 if (r)
2666                         goto out;
2667                 r = 0;
2668                 break;
2669         }
2670         case KVM_DEBUG_GUEST: {
2671                 struct kvm_debug_guest dbg;
2672
2673                 r = -EFAULT;
2674                 if (copy_from_user(&dbg, argp, sizeof dbg))
2675                         goto out;
2676                 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
2677                 if (r)
2678                         goto out;
2679                 r = 0;
2680                 break;
2681         }
2682         case KVM_GET_MSRS:
2683                 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2684                 break;
2685         case KVM_SET_MSRS:
2686                 r = msr_io(vcpu, argp, do_set_msr, 0);
2687                 break;
2688         case KVM_SET_CPUID: {
2689                 struct kvm_cpuid __user *cpuid_arg = argp;
2690                 struct kvm_cpuid cpuid;
2691
2692                 r = -EFAULT;
2693                 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2694                         goto out;
2695                 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2696                 if (r)
2697                         goto out;
2698                 break;
2699         }
2700         case KVM_SET_SIGNAL_MASK: {
2701                 struct kvm_signal_mask __user *sigmask_arg = argp;
2702                 struct kvm_signal_mask kvm_sigmask;
2703                 sigset_t sigset, *p;
2704
2705                 p = NULL;
2706                 if (argp) {
2707                         r = -EFAULT;
2708                         if (copy_from_user(&kvm_sigmask, argp,
2709                                            sizeof kvm_sigmask))
2710                                 goto out;
2711                         r = -EINVAL;
2712                         if (kvm_sigmask.len != sizeof sigset)
2713                                 goto out;
2714                         r = -EFAULT;
2715                         if (copy_from_user(&sigset, sigmask_arg->sigset,
2716                                            sizeof sigset))
2717                                 goto out;
2718                         p = &sigset;
2719                 }
2720                 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2721                 break;
2722         }
2723         case KVM_GET_FPU: {
2724                 struct kvm_fpu fpu;
2725
2726                 memset(&fpu, 0, sizeof fpu);
2727                 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2728                 if (r)
2729                         goto out;
2730                 r = -EFAULT;
2731                 if (copy_to_user(argp, &fpu, sizeof fpu))
2732                         goto out;
2733                 r = 0;
2734                 break;
2735         }
2736         case KVM_SET_FPU: {
2737                 struct kvm_fpu fpu;
2738
2739                 r = -EFAULT;
2740                 if (copy_from_user(&fpu, argp, sizeof fpu))
2741                         goto out;
2742                 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2743                 if (r)
2744                         goto out;
2745                 r = 0;
2746                 break;
2747         }
2748         default:
2749                 ;
2750         }
2751 out:
2752         return r;
2753 }
2754
2755 static long kvm_vm_ioctl(struct file *filp,
2756                            unsigned int ioctl, unsigned long arg)
2757 {
2758         struct kvm *kvm = filp->private_data;
2759         void __user *argp = (void __user *)arg;
2760         int r = -EINVAL;
2761
2762         switch (ioctl) {
2763         case KVM_CREATE_VCPU:
2764                 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2765                 if (r < 0)
2766                         goto out;
2767                 break;
2768         case KVM_SET_MEMORY_REGION: {
2769                 struct kvm_memory_region kvm_mem;
2770
2771                 r = -EFAULT;
2772                 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2773                         goto out;
2774                 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
2775                 if (r)
2776                         goto out;
2777                 break;
2778         }
2779         case KVM_GET_DIRTY_LOG: {
2780                 struct kvm_dirty_log log;
2781
2782                 r = -EFAULT;
2783                 if (copy_from_user(&log, argp, sizeof log))
2784                         goto out;
2785                 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2786                 if (r)
2787                         goto out;
2788                 break;
2789         }
2790         case KVM_SET_MEMORY_ALIAS: {
2791                 struct kvm_memory_alias alias;
2792
2793                 r = -EFAULT;
2794                 if (copy_from_user(&alias, argp, sizeof alias))
2795                         goto out;
2796                 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2797                 if (r)
2798                         goto out;
2799                 break;
2800         }
2801         default:
2802                 ;
2803         }
2804 out:
2805         return r;
2806 }
2807
2808 static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2809                                   unsigned long address,
2810                                   int *type)
2811 {
2812         struct kvm *kvm = vma->vm_file->private_data;
2813         unsigned long pgoff;
2814         struct page *page;
2815
2816         pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2817         page = gfn_to_page(kvm, pgoff);
2818         if (!page)
2819                 return NOPAGE_SIGBUS;
2820         get_page(page);
2821         if (type != NULL)
2822                 *type = VM_FAULT_MINOR;
2823
2824         return page;
2825 }
2826
2827 static struct vm_operations_struct kvm_vm_vm_ops = {
2828         .nopage = kvm_vm_nopage,
2829 };
2830
2831 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2832 {
2833         vma->vm_ops = &kvm_vm_vm_ops;
2834         return 0;
2835 }
2836
2837 static struct file_operations kvm_vm_fops = {
2838         .release        = kvm_vm_release,
2839         .unlocked_ioctl = kvm_vm_ioctl,
2840         .compat_ioctl   = kvm_vm_ioctl,
2841         .mmap           = kvm_vm_mmap,
2842 };
2843
2844 static int kvm_dev_ioctl_create_vm(void)
2845 {
2846         int fd, r;
2847         struct inode *inode;
2848         struct file *file;
2849         struct kvm *kvm;
2850
2851         kvm = kvm_create_vm();
2852         if (IS_ERR(kvm))
2853                 return PTR_ERR(kvm);
2854         r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
2855         if (r) {
2856                 kvm_destroy_vm(kvm);
2857                 return r;
2858         }
2859
2860         kvm->filp = file;
2861
2862         return fd;
2863 }
2864
2865 static long kvm_dev_ioctl(struct file *filp,
2866                           unsigned int ioctl, unsigned long arg)
2867 {
2868         void __user *argp = (void __user *)arg;
2869         long r = -EINVAL;
2870
2871         switch (ioctl) {
2872         case KVM_GET_API_VERSION:
2873                 r = -EINVAL;
2874                 if (arg)
2875                         goto out;
2876                 r = KVM_API_VERSION;
2877                 break;
2878         case KVM_CREATE_VM:
2879                 r = -EINVAL;
2880                 if (arg)
2881                         goto out;
2882                 r = kvm_dev_ioctl_create_vm();
2883                 break;
2884         case KVM_GET_MSR_INDEX_LIST: {
2885                 struct kvm_msr_list __user *user_msr_list = argp;
2886                 struct kvm_msr_list msr_list;
2887                 unsigned n;
2888
2889                 r = -EFAULT;
2890                 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2891                         goto out;
2892                 n = msr_list.nmsrs;
2893                 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
2894                 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2895                         goto out;
2896                 r = -E2BIG;
2897                 if (n < num_msrs_to_save)
2898                         goto out;
2899                 r = -EFAULT;
2900                 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2901                                  num_msrs_to_save * sizeof(u32)))
2902                         goto out;
2903                 if (copy_to_user(user_msr_list->indices
2904                                  + num_msrs_to_save * sizeof(u32),
2905                                  &emulated_msrs,
2906                                  ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2907                         goto out;
2908                 r = 0;
2909                 break;
2910         }
2911         case KVM_CHECK_EXTENSION:
2912                 /*
2913                  * No extensions defined at present.
2914                  */
2915                 r = 0;
2916                 break;
2917         case KVM_GET_VCPU_MMAP_SIZE:
2918                 r = -EINVAL;
2919                 if (arg)
2920                         goto out;
2921                 r = 2 * PAGE_SIZE;
2922                 break;
2923         default:
2924                 ;
2925         }
2926 out:
2927         return r;
2928 }
2929
2930 static struct file_operations kvm_chardev_ops = {
2931         .open           = kvm_dev_open,
2932         .release        = kvm_dev_release,
2933         .unlocked_ioctl = kvm_dev_ioctl,
2934         .compat_ioctl   = kvm_dev_ioctl,
2935 };
2936
2937 static struct miscdevice kvm_dev = {
2938         KVM_MINOR,
2939         "kvm",
2940         &kvm_chardev_ops,
2941 };
2942
2943 /*
2944  * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2945  * cached on it.
2946  */
2947 static void decache_vcpus_on_cpu(int cpu)
2948 {
2949         struct kvm *vm;
2950         struct kvm_vcpu *vcpu;
2951         int i;
2952
2953         spin_lock(&kvm_lock);
2954         list_for_each_entry(vm, &vm_list, vm_list)
2955                 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2956                         vcpu = &vm->vcpus[i];
2957                         /*
2958                          * If the vcpu is locked, then it is running on some
2959                          * other cpu and therefore it is not cached on the
2960                          * cpu in question.
2961                          *
2962                          * If it's not locked, check the last cpu it executed
2963                          * on.
2964                          */
2965                         if (mutex_trylock(&vcpu->mutex)) {
2966                                 if (vcpu->cpu == cpu) {
2967                                         kvm_arch_ops->vcpu_decache(vcpu);
2968                                         vcpu->cpu = -1;
2969                                 }
2970                                 mutex_unlock(&vcpu->mutex);
2971                         }
2972                 }
2973         spin_unlock(&kvm_lock);
2974 }
2975
2976 static void hardware_enable(void *junk)
2977 {
2978         int cpu = raw_smp_processor_id();
2979
2980         if (cpu_isset(cpu, cpus_hardware_enabled))
2981                 return;
2982         cpu_set(cpu, cpus_hardware_enabled);
2983         kvm_arch_ops->hardware_enable(NULL);
2984 }
2985
2986 static void hardware_disable(void *junk)
2987 {
2988         int cpu = raw_smp_processor_id();
2989
2990         if (!cpu_isset(cpu, cpus_hardware_enabled))
2991                 return;
2992         cpu_clear(cpu, cpus_hardware_enabled);
2993         decache_vcpus_on_cpu(cpu);
2994         kvm_arch_ops->hardware_disable(NULL);
2995 }
2996
2997 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2998                            void *v)
2999 {
3000         int cpu = (long)v;
3001
3002         switch (val) {
3003         case CPU_DYING:
3004         case CPU_DYING_FROZEN:
3005                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3006                        cpu);
3007                 hardware_disable(NULL);
3008                 break;
3009         case CPU_UP_CANCELED:
3010         case CPU_UP_CANCELED_FROZEN:
3011                 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3012                        cpu);
3013                 smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
3014                 break;
3015         case CPU_ONLINE:
3016         case CPU_ONLINE_FROZEN:
3017                 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3018                        cpu);
3019                 smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
3020                 break;
3021         }
3022         return NOTIFY_OK;
3023 }
3024
3025 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
3026                        void *v)
3027 {
3028         if (val == SYS_RESTART) {
3029                 /*
3030                  * Some (well, at least mine) BIOSes hang on reboot if
3031                  * in vmx root mode.
3032                  */
3033                 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
3034                 on_each_cpu(hardware_disable, NULL, 0, 1);
3035         }
3036         return NOTIFY_OK;
3037 }
3038
3039 static struct notifier_block kvm_reboot_notifier = {
3040         .notifier_call = kvm_reboot,
3041         .priority = 0,
3042 };
3043
3044 void kvm_io_bus_init(struct kvm_io_bus *bus)
3045 {
3046         memset(bus, 0, sizeof(*bus));
3047 }
3048
3049 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3050 {
3051         int i;
3052
3053         for (i = 0; i < bus->dev_count; i++) {
3054                 struct kvm_io_device *pos = bus->devs[i];
3055
3056                 kvm_iodevice_destructor(pos);
3057         }
3058 }
3059
3060 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3061 {
3062         int i;
3063
3064         for (i = 0; i < bus->dev_count; i++) {
3065                 struct kvm_io_device *pos = bus->devs[i];
3066
3067                 if (pos->in_range(pos, addr))
3068                         return pos;
3069         }
3070
3071         return NULL;
3072 }
3073
3074 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3075 {
3076         BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3077
3078         bus->devs[bus->dev_count++] = dev;
3079 }
3080
3081 static struct notifier_block kvm_cpu_notifier = {
3082         .notifier_call = kvm_cpu_hotplug,
3083         .priority = 20, /* must be > scheduler priority */
3084 };
3085
3086 static u64 stat_get(void *_offset)
3087 {
3088         unsigned offset = (long)_offset;
3089         u64 total = 0;
3090         struct kvm *kvm;
3091         struct kvm_vcpu *vcpu;
3092         int i;
3093
3094         spin_lock(&kvm_lock);
3095         list_for_each_entry(kvm, &vm_list, vm_list)
3096                 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3097                         vcpu = &kvm->vcpus[i];
3098                         total += *(u32 *)((void *)vcpu + offset);
3099                 }
3100         spin_unlock(&kvm_lock);
3101         return total;
3102 }
3103
3104 static void stat_set(void *offset, u64 val)
3105 {
3106 }
3107
3108 DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
3109
3110 static __init void kvm_init_debug(void)
3111 {
3112         struct kvm_stats_debugfs_item *p;
3113
3114         debugfs_dir = debugfs_create_dir("kvm", NULL);
3115         for (p = debugfs_entries; p->name; ++p)
3116                 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3117                                                 (void *)(long)p->offset,
3118                                                 &stat_fops);
3119 }
3120
3121 static void kvm_exit_debug(void)
3122 {
3123         struct kvm_stats_debugfs_item *p;
3124
3125         for (p = debugfs_entries; p->name; ++p)
3126                 debugfs_remove(p->dentry);
3127         debugfs_remove(debugfs_dir);
3128 }
3129
3130 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3131 {
3132         hardware_disable(NULL);
3133         return 0;
3134 }
3135
3136 static int kvm_resume(struct sys_device *dev)
3137 {
3138         hardware_enable(NULL);
3139         return 0;
3140 }
3141
3142 static struct sysdev_class kvm_sysdev_class = {
3143         set_kset_name("kvm"),
3144         .suspend = kvm_suspend,
3145         .resume = kvm_resume,
3146 };
3147
3148 static struct sys_device kvm_sysdev = {
3149         .id = 0,
3150         .cls = &kvm_sysdev_class,
3151 };
3152
3153 hpa_t bad_page_address;
3154
3155 int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3156 {
3157         int r;
3158
3159         if (kvm_arch_ops) {
3160                 printk(KERN_ERR "kvm: already loaded the other module\n");
3161                 return -EEXIST;
3162         }
3163
3164         if (!ops->cpu_has_kvm_support()) {
3165                 printk(KERN_ERR "kvm: no hardware support\n");
3166                 return -EOPNOTSUPP;
3167         }
3168         if (ops->disabled_by_bios()) {
3169                 printk(KERN_ERR "kvm: disabled by bios\n");
3170                 return -EOPNOTSUPP;
3171         }
3172
3173         kvm_arch_ops = ops;
3174
3175         r = kvm_arch_ops->hardware_setup();
3176         if (r < 0)
3177                 goto out;
3178
3179         on_each_cpu(hardware_enable, NULL, 0, 1);
3180         r = register_cpu_notifier(&kvm_cpu_notifier);
3181         if (r)
3182                 goto out_free_1;
3183         register_reboot_notifier(&kvm_reboot_notifier);
3184
3185         r = sysdev_class_register(&kvm_sysdev_class);
3186         if (r)
3187                 goto out_free_2;
3188
3189         r = sysdev_register(&kvm_sysdev);
3190         if (r)
3191                 goto out_free_3;
3192
3193         kvm_chardev_ops.owner = module;
3194
3195         r = misc_register(&kvm_dev);
3196         if (r) {
3197                 printk (KERN_ERR "kvm: misc device register failed\n");
3198                 goto out_free;
3199         }
3200
3201         return r;
3202
3203 out_free:
3204         sysdev_unregister(&kvm_sysdev);
3205 out_free_3:
3206         sysdev_class_unregister(&kvm_sysdev_class);
3207 out_free_2:
3208         unregister_reboot_notifier(&kvm_reboot_notifier);
3209         unregister_cpu_notifier(&kvm_cpu_notifier);
3210 out_free_1:
3211         on_each_cpu(hardware_disable, NULL, 0, 1);
3212         kvm_arch_ops->hardware_unsetup();
3213 out:
3214         kvm_arch_ops = NULL;
3215         return r;
3216 }
3217
3218 void kvm_exit_arch(void)
3219 {
3220         misc_deregister(&kvm_dev);
3221         sysdev_unregister(&kvm_sysdev);
3222         sysdev_class_unregister(&kvm_sysdev_class);
3223         unregister_reboot_notifier(&kvm_reboot_notifier);
3224         unregister_cpu_notifier(&kvm_cpu_notifier);
3225         on_each_cpu(hardware_disable, NULL, 0, 1);
3226         kvm_arch_ops->hardware_unsetup();
3227         kvm_arch_ops = NULL;
3228 }
3229
3230 static __init int kvm_init(void)
3231 {
3232         static struct page *bad_page;
3233         int r;
3234
3235         r = kvm_mmu_module_init();
3236         if (r)
3237                 goto out4;
3238
3239         kvm_init_debug();
3240
3241         kvm_init_msr_list();
3242
3243         if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3244                 r = -ENOMEM;
3245                 goto out;
3246         }
3247
3248         bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3249         memset(__va(bad_page_address), 0, PAGE_SIZE);
3250
3251         return 0;
3252
3253 out:
3254         kvm_exit_debug();
3255         kvm_mmu_module_exit();
3256 out4:
3257         return r;
3258 }
3259
3260 static __exit void kvm_exit(void)
3261 {
3262         kvm_exit_debug();
3263         __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
3264         kvm_mmu_module_exit();
3265 }
3266
3267 module_init(kvm_init)
3268 module_exit(kvm_exit)
3269
3270 EXPORT_SYMBOL_GPL(kvm_init_arch);
3271 EXPORT_SYMBOL_GPL(kvm_exit_arch);