]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/kvm/svm.c
794d95416f7b80c2f9e777c5f16fd5c0fd2d21ed
[karo-tx-linux.git] / drivers / kvm / svm.c
1 /*
2  * Kernel-based Virtual Machine driver for Linux
3  *
4  * AMD SVM support
5  *
6  * Copyright (C) 2006 Qumranet, Inc.
7  *
8  * Authors:
9  *   Yaniv Kamay  <yaniv@qumranet.com>
10  *   Avi Kivity   <avi@qumranet.com>
11  *
12  * This work is licensed under the terms of the GNU GPL, version 2.  See
13  * the COPYING file in the top-level directory.
14  *
15  */
16
17 #include "kvm_svm.h"
18 #include "x86_emulate.h"
19 #include "irq.h"
20
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/vmalloc.h>
24 #include <linux/highmem.h>
25 #include <linux/sched.h>
26
27 #include <asm/desc.h>
28
29 MODULE_AUTHOR("Qumranet");
30 MODULE_LICENSE("GPL");
31
32 #define IOPM_ALLOC_ORDER 2
33 #define MSRPM_ALLOC_ORDER 1
34
35 #define DB_VECTOR 1
36 #define UD_VECTOR 6
37 #define GP_VECTOR 13
38
39 #define DR7_GD_MASK (1 << 13)
40 #define DR6_BD_MASK (1 << 13)
41
42 #define SEG_TYPE_LDT 2
43 #define SEG_TYPE_BUSY_TSS16 3
44
45 #define KVM_EFER_LMA (1 << 10)
46 #define KVM_EFER_LME (1 << 8)
47
48 #define SVM_FEATURE_NPT  (1 << 0)
49 #define SVM_FEATURE_LBRV (1 << 1)
50 #define SVM_DEATURE_SVML (1 << 2)
51
52 static void kvm_reput_irq(struct vcpu_svm *svm);
53
54 static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
55 {
56         return container_of(vcpu, struct vcpu_svm, vcpu);
57 }
58
59 unsigned long iopm_base;
60 unsigned long msrpm_base;
61
62 struct kvm_ldttss_desc {
63         u16 limit0;
64         u16 base0;
65         unsigned base1 : 8, type : 5, dpl : 2, p : 1;
66         unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
67         u32 base3;
68         u32 zero1;
69 } __attribute__((packed));
70
71 struct svm_cpu_data {
72         int cpu;
73
74         u64 asid_generation;
75         u32 max_asid;
76         u32 next_asid;
77         struct kvm_ldttss_desc *tss_desc;
78
79         struct page *save_area;
80 };
81
82 static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
83 static uint32_t svm_features;
84
85 struct svm_init_data {
86         int cpu;
87         int r;
88 };
89
90 static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
91
92 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
93 #define MSRS_RANGE_SIZE 2048
94 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
95
96 #define MAX_INST_SIZE 15
97
98 static inline u32 svm_has(u32 feat)
99 {
100         return svm_features & feat;
101 }
102
103 static inline u8 pop_irq(struct kvm_vcpu *vcpu)
104 {
105         int word_index = __ffs(vcpu->irq_summary);
106         int bit_index = __ffs(vcpu->irq_pending[word_index]);
107         int irq = word_index * BITS_PER_LONG + bit_index;
108
109         clear_bit(bit_index, &vcpu->irq_pending[word_index]);
110         if (!vcpu->irq_pending[word_index])
111                 clear_bit(word_index, &vcpu->irq_summary);
112         return irq;
113 }
114
115 static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq)
116 {
117         set_bit(irq, vcpu->irq_pending);
118         set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
119 }
120
121 static inline void clgi(void)
122 {
123         asm volatile (SVM_CLGI);
124 }
125
126 static inline void stgi(void)
127 {
128         asm volatile (SVM_STGI);
129 }
130
131 static inline void invlpga(unsigned long addr, u32 asid)
132 {
133         asm volatile (SVM_INVLPGA :: "a"(addr), "c"(asid));
134 }
135
136 static inline unsigned long kvm_read_cr2(void)
137 {
138         unsigned long cr2;
139
140         asm volatile ("mov %%cr2, %0" : "=r" (cr2));
141         return cr2;
142 }
143
144 static inline void kvm_write_cr2(unsigned long val)
145 {
146         asm volatile ("mov %0, %%cr2" :: "r" (val));
147 }
148
149 static inline unsigned long read_dr6(void)
150 {
151         unsigned long dr6;
152
153         asm volatile ("mov %%dr6, %0" : "=r" (dr6));
154         return dr6;
155 }
156
157 static inline void write_dr6(unsigned long val)
158 {
159         asm volatile ("mov %0, %%dr6" :: "r" (val));
160 }
161
162 static inline unsigned long read_dr7(void)
163 {
164         unsigned long dr7;
165
166         asm volatile ("mov %%dr7, %0" : "=r" (dr7));
167         return dr7;
168 }
169
170 static inline void write_dr7(unsigned long val)
171 {
172         asm volatile ("mov %0, %%dr7" :: "r" (val));
173 }
174
175 static inline void force_new_asid(struct kvm_vcpu *vcpu)
176 {
177         to_svm(vcpu)->asid_generation--;
178 }
179
180 static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
181 {
182         force_new_asid(vcpu);
183 }
184
185 static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
186 {
187         if (!(efer & KVM_EFER_LMA))
188                 efer &= ~KVM_EFER_LME;
189
190         to_svm(vcpu)->vmcb->save.efer = efer | MSR_EFER_SVME_MASK;
191         vcpu->shadow_efer = efer;
192 }
193
194 static void svm_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
195 {
196         struct vcpu_svm *svm = to_svm(vcpu);
197
198         svm->vmcb->control.event_inj =          SVM_EVTINJ_VALID |
199                                                 SVM_EVTINJ_VALID_ERR |
200                                                 SVM_EVTINJ_TYPE_EXEPT |
201                                                 GP_VECTOR;
202         svm->vmcb->control.event_inj_err = error_code;
203 }
204
205 static void inject_ud(struct kvm_vcpu *vcpu)
206 {
207         to_svm(vcpu)->vmcb->control.event_inj = SVM_EVTINJ_VALID |
208                                                 SVM_EVTINJ_TYPE_EXEPT |
209                                                 UD_VECTOR;
210 }
211
212 static int is_page_fault(uint32_t info)
213 {
214         info &= SVM_EVTINJ_VEC_MASK | SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
215         return info == (PF_VECTOR | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT);
216 }
217
218 static int is_external_interrupt(u32 info)
219 {
220         info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
221         return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
222 }
223
224 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
225 {
226         struct vcpu_svm *svm = to_svm(vcpu);
227
228         if (!svm->next_rip) {
229                 printk(KERN_DEBUG "%s: NOP\n", __FUNCTION__);
230                 return;
231         }
232         if (svm->next_rip - svm->vmcb->save.rip > MAX_INST_SIZE) {
233                 printk(KERN_ERR "%s: ip 0x%llx next 0x%llx\n",
234                        __FUNCTION__,
235                        svm->vmcb->save.rip,
236                        svm->next_rip);
237         }
238
239         vcpu->rip = svm->vmcb->save.rip = svm->next_rip;
240         svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
241
242         vcpu->interrupt_window_open = 1;
243 }
244
245 static int has_svm(void)
246 {
247         uint32_t eax, ebx, ecx, edx;
248
249         if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
250                 printk(KERN_INFO "has_svm: not amd\n");
251                 return 0;
252         }
253
254         cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
255         if (eax < SVM_CPUID_FUNC) {
256                 printk(KERN_INFO "has_svm: can't execute cpuid_8000000a\n");
257                 return 0;
258         }
259
260         cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
261         if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) {
262                 printk(KERN_DEBUG "has_svm: svm not available\n");
263                 return 0;
264         }
265         return 1;
266 }
267
268 static void svm_hardware_disable(void *garbage)
269 {
270         struct svm_cpu_data *svm_data
271                 = per_cpu(svm_data, raw_smp_processor_id());
272
273         if (svm_data) {
274                 uint64_t efer;
275
276                 wrmsrl(MSR_VM_HSAVE_PA, 0);
277                 rdmsrl(MSR_EFER, efer);
278                 wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK);
279                 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
280                 __free_page(svm_data->save_area);
281                 kfree(svm_data);
282         }
283 }
284
285 static void svm_hardware_enable(void *garbage)
286 {
287
288         struct svm_cpu_data *svm_data;
289         uint64_t efer;
290 #ifdef CONFIG_X86_64
291         struct desc_ptr gdt_descr;
292 #else
293         struct desc_ptr gdt_descr;
294 #endif
295         struct desc_struct *gdt;
296         int me = raw_smp_processor_id();
297
298         if (!has_svm()) {
299                 printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
300                 return;
301         }
302         svm_data = per_cpu(svm_data, me);
303
304         if (!svm_data) {
305                 printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
306                        me);
307                 return;
308         }
309
310         svm_data->asid_generation = 1;
311         svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
312         svm_data->next_asid = svm_data->max_asid + 1;
313         svm_features = cpuid_edx(SVM_CPUID_FUNC);
314
315         asm volatile ( "sgdt %0" : "=m"(gdt_descr) );
316         gdt = (struct desc_struct *)gdt_descr.address;
317         svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
318
319         rdmsrl(MSR_EFER, efer);
320         wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK);
321
322         wrmsrl(MSR_VM_HSAVE_PA,
323                page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
324 }
325
326 static int svm_cpu_init(int cpu)
327 {
328         struct svm_cpu_data *svm_data;
329         int r;
330
331         svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
332         if (!svm_data)
333                 return -ENOMEM;
334         svm_data->cpu = cpu;
335         svm_data->save_area = alloc_page(GFP_KERNEL);
336         r = -ENOMEM;
337         if (!svm_data->save_area)
338                 goto err_1;
339
340         per_cpu(svm_data, cpu) = svm_data;
341
342         return 0;
343
344 err_1:
345         kfree(svm_data);
346         return r;
347
348 }
349
350 static void set_msr_interception(u32 *msrpm, unsigned msr,
351                                  int read, int write)
352 {
353         int i;
354
355         for (i = 0; i < NUM_MSR_MAPS; i++) {
356                 if (msr >= msrpm_ranges[i] &&
357                     msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
358                         u32 msr_offset = (i * MSRS_IN_RANGE + msr -
359                                           msrpm_ranges[i]) * 2;
360
361                         u32 *base = msrpm + (msr_offset / 32);
362                         u32 msr_shift = msr_offset % 32;
363                         u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
364                         *base = (*base & ~(0x3 << msr_shift)) |
365                                 (mask << msr_shift);
366                         return;
367                 }
368         }
369         BUG();
370 }
371
372 static __init int svm_hardware_setup(void)
373 {
374         int cpu;
375         struct page *iopm_pages;
376         struct page *msrpm_pages;
377         void *iopm_va, *msrpm_va;
378         int r;
379
380         iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
381
382         if (!iopm_pages)
383                 return -ENOMEM;
384
385         iopm_va = page_address(iopm_pages);
386         memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
387         clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
388         iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
389
390
391         msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
392
393         r = -ENOMEM;
394         if (!msrpm_pages)
395                 goto err_1;
396
397         msrpm_va = page_address(msrpm_pages);
398         memset(msrpm_va, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
399         msrpm_base = page_to_pfn(msrpm_pages) << PAGE_SHIFT;
400
401 #ifdef CONFIG_X86_64
402         set_msr_interception(msrpm_va, MSR_GS_BASE, 1, 1);
403         set_msr_interception(msrpm_va, MSR_FS_BASE, 1, 1);
404         set_msr_interception(msrpm_va, MSR_KERNEL_GS_BASE, 1, 1);
405         set_msr_interception(msrpm_va, MSR_LSTAR, 1, 1);
406         set_msr_interception(msrpm_va, MSR_CSTAR, 1, 1);
407         set_msr_interception(msrpm_va, MSR_SYSCALL_MASK, 1, 1);
408 #endif
409         set_msr_interception(msrpm_va, MSR_K6_STAR, 1, 1);
410         set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_CS, 1, 1);
411         set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_ESP, 1, 1);
412         set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_EIP, 1, 1);
413
414         for_each_online_cpu(cpu) {
415                 r = svm_cpu_init(cpu);
416                 if (r)
417                         goto err_2;
418         }
419         return 0;
420
421 err_2:
422         __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
423         msrpm_base = 0;
424 err_1:
425         __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
426         iopm_base = 0;
427         return r;
428 }
429
430 static __exit void svm_hardware_unsetup(void)
431 {
432         __free_pages(pfn_to_page(msrpm_base >> PAGE_SHIFT), MSRPM_ALLOC_ORDER);
433         __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
434         iopm_base = msrpm_base = 0;
435 }
436
437 static void init_seg(struct vmcb_seg *seg)
438 {
439         seg->selector = 0;
440         seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
441                 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
442         seg->limit = 0xffff;
443         seg->base = 0;
444 }
445
446 static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
447 {
448         seg->selector = 0;
449         seg->attrib = SVM_SELECTOR_P_MASK | type;
450         seg->limit = 0xffff;
451         seg->base = 0;
452 }
453
454 static void init_vmcb(struct vmcb *vmcb)
455 {
456         struct vmcb_control_area *control = &vmcb->control;
457         struct vmcb_save_area *save = &vmcb->save;
458
459         control->intercept_cr_read =    INTERCEPT_CR0_MASK |
460                                         INTERCEPT_CR3_MASK |
461                                         INTERCEPT_CR4_MASK;
462
463         control->intercept_cr_write =   INTERCEPT_CR0_MASK |
464                                         INTERCEPT_CR3_MASK |
465                                         INTERCEPT_CR4_MASK;
466
467         control->intercept_dr_read =    INTERCEPT_DR0_MASK |
468                                         INTERCEPT_DR1_MASK |
469                                         INTERCEPT_DR2_MASK |
470                                         INTERCEPT_DR3_MASK;
471
472         control->intercept_dr_write =   INTERCEPT_DR0_MASK |
473                                         INTERCEPT_DR1_MASK |
474                                         INTERCEPT_DR2_MASK |
475                                         INTERCEPT_DR3_MASK |
476                                         INTERCEPT_DR5_MASK |
477                                         INTERCEPT_DR7_MASK;
478
479         control->intercept_exceptions = (1 << PF_VECTOR) |
480                                         (1 << UD_VECTOR);
481
482
483         control->intercept =    (1ULL << INTERCEPT_INTR) |
484                                 (1ULL << INTERCEPT_NMI) |
485                                 (1ULL << INTERCEPT_SMI) |
486                 /*
487                  * selective cr0 intercept bug?
488                  *      0:   0f 22 d8                mov    %eax,%cr3
489                  *      3:   0f 20 c0                mov    %cr0,%eax
490                  *      6:   0d 00 00 00 80          or     $0x80000000,%eax
491                  *      b:   0f 22 c0                mov    %eax,%cr0
492                  * set cr3 ->interception
493                  * get cr0 ->interception
494                  * set cr0 -> no interception
495                  */
496                 /*              (1ULL << INTERCEPT_SELECTIVE_CR0) | */
497                                 (1ULL << INTERCEPT_CPUID) |
498                                 (1ULL << INTERCEPT_INVD) |
499                                 (1ULL << INTERCEPT_HLT) |
500                                 (1ULL << INTERCEPT_INVLPGA) |
501                                 (1ULL << INTERCEPT_IOIO_PROT) |
502                                 (1ULL << INTERCEPT_MSR_PROT) |
503                                 (1ULL << INTERCEPT_TASK_SWITCH) |
504                                 (1ULL << INTERCEPT_SHUTDOWN) |
505                                 (1ULL << INTERCEPT_VMRUN) |
506                                 (1ULL << INTERCEPT_VMMCALL) |
507                                 (1ULL << INTERCEPT_VMLOAD) |
508                                 (1ULL << INTERCEPT_VMSAVE) |
509                                 (1ULL << INTERCEPT_STGI) |
510                                 (1ULL << INTERCEPT_CLGI) |
511                                 (1ULL << INTERCEPT_SKINIT) |
512                                 (1ULL << INTERCEPT_WBINVD) |
513                                 (1ULL << INTERCEPT_MONITOR) |
514                                 (1ULL << INTERCEPT_MWAIT);
515
516         control->iopm_base_pa = iopm_base;
517         control->msrpm_base_pa = msrpm_base;
518         control->tsc_offset = 0;
519         control->int_ctl = V_INTR_MASKING_MASK;
520
521         init_seg(&save->es);
522         init_seg(&save->ss);
523         init_seg(&save->ds);
524         init_seg(&save->fs);
525         init_seg(&save->gs);
526
527         save->cs.selector = 0xf000;
528         /* Executable/Readable Code Segment */
529         save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
530                 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
531         save->cs.limit = 0xffff;
532         /*
533          * cs.base should really be 0xffff0000, but vmx can't handle that, so
534          * be consistent with it.
535          *
536          * Replace when we have real mode working for vmx.
537          */
538         save->cs.base = 0xf0000;
539
540         save->gdtr.limit = 0xffff;
541         save->idtr.limit = 0xffff;
542
543         init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
544         init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
545
546         save->efer = MSR_EFER_SVME_MASK;
547
548         save->dr6 = 0xffff0ff0;
549         save->dr7 = 0x400;
550         save->rflags = 2;
551         save->rip = 0x0000fff0;
552
553         /*
554          * cr0 val on cpu init should be 0x60000010, we enable cpu
555          * cache by default. the orderly way is to enable cache in bios.
556          */
557         save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
558         save->cr4 = X86_CR4_PAE;
559         /* rdx = ?? */
560 }
561
562 static void svm_vcpu_reset(struct kvm_vcpu *vcpu)
563 {
564         struct vcpu_svm *svm = to_svm(vcpu);
565
566         init_vmcb(svm->vmcb);
567
568         if (vcpu->vcpu_id != 0) {
569                 svm->vmcb->save.rip = 0;
570                 svm->vmcb->save.cs.base = svm->vcpu.sipi_vector << 12;
571                 svm->vmcb->save.cs.selector = svm->vcpu.sipi_vector << 8;
572         }
573 }
574
575 static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
576 {
577         struct vcpu_svm *svm;
578         struct page *page;
579         int err;
580
581         svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
582         if (!svm) {
583                 err = -ENOMEM;
584                 goto out;
585         }
586
587         err = kvm_vcpu_init(&svm->vcpu, kvm, id);
588         if (err)
589                 goto free_svm;
590
591         if (irqchip_in_kernel(kvm)) {
592                 err = kvm_create_lapic(&svm->vcpu);
593                 if (err < 0)
594                         goto free_svm;
595         }
596
597         page = alloc_page(GFP_KERNEL);
598         if (!page) {
599                 err = -ENOMEM;
600                 goto uninit;
601         }
602
603         svm->vmcb = page_address(page);
604         clear_page(svm->vmcb);
605         svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
606         svm->asid_generation = 0;
607         memset(svm->db_regs, 0, sizeof(svm->db_regs));
608         init_vmcb(svm->vmcb);
609
610         fx_init(&svm->vcpu);
611         svm->vcpu.fpu_active = 1;
612         svm->vcpu.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
613         if (svm->vcpu.vcpu_id == 0)
614                 svm->vcpu.apic_base |= MSR_IA32_APICBASE_BSP;
615
616         return &svm->vcpu;
617
618 uninit:
619         kvm_vcpu_uninit(&svm->vcpu);
620 free_svm:
621         kmem_cache_free(kvm_vcpu_cache, svm);
622 out:
623         return ERR_PTR(err);
624 }
625
626 static void svm_free_vcpu(struct kvm_vcpu *vcpu)
627 {
628         struct vcpu_svm *svm = to_svm(vcpu);
629
630         __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
631         kvm_vcpu_uninit(vcpu);
632         kmem_cache_free(kvm_vcpu_cache, svm);
633 }
634
635 static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
636 {
637         struct vcpu_svm *svm = to_svm(vcpu);
638         int i;
639
640         if (unlikely(cpu != vcpu->cpu)) {
641                 u64 tsc_this, delta;
642
643                 /*
644                  * Make sure that the guest sees a monotonically
645                  * increasing TSC.
646                  */
647                 rdtscll(tsc_this);
648                 delta = vcpu->host_tsc - tsc_this;
649                 svm->vmcb->control.tsc_offset += delta;
650                 vcpu->cpu = cpu;
651                 kvm_migrate_apic_timer(vcpu);
652         }
653
654         for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
655                 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
656 }
657
658 static void svm_vcpu_put(struct kvm_vcpu *vcpu)
659 {
660         struct vcpu_svm *svm = to_svm(vcpu);
661         int i;
662
663         for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
664                 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
665
666         rdtscll(vcpu->host_tsc);
667         kvm_put_guest_fpu(vcpu);
668 }
669
670 static void svm_vcpu_decache(struct kvm_vcpu *vcpu)
671 {
672 }
673
674 static void svm_cache_regs(struct kvm_vcpu *vcpu)
675 {
676         struct vcpu_svm *svm = to_svm(vcpu);
677
678         vcpu->regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
679         vcpu->regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
680         vcpu->rip = svm->vmcb->save.rip;
681 }
682
683 static void svm_decache_regs(struct kvm_vcpu *vcpu)
684 {
685         struct vcpu_svm *svm = to_svm(vcpu);
686         svm->vmcb->save.rax = vcpu->regs[VCPU_REGS_RAX];
687         svm->vmcb->save.rsp = vcpu->regs[VCPU_REGS_RSP];
688         svm->vmcb->save.rip = vcpu->rip;
689 }
690
691 static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
692 {
693         return to_svm(vcpu)->vmcb->save.rflags;
694 }
695
696 static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
697 {
698         to_svm(vcpu)->vmcb->save.rflags = rflags;
699 }
700
701 static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
702 {
703         struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
704
705         switch (seg) {
706         case VCPU_SREG_CS: return &save->cs;
707         case VCPU_SREG_DS: return &save->ds;
708         case VCPU_SREG_ES: return &save->es;
709         case VCPU_SREG_FS: return &save->fs;
710         case VCPU_SREG_GS: return &save->gs;
711         case VCPU_SREG_SS: return &save->ss;
712         case VCPU_SREG_TR: return &save->tr;
713         case VCPU_SREG_LDTR: return &save->ldtr;
714         }
715         BUG();
716         return NULL;
717 }
718
719 static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
720 {
721         struct vmcb_seg *s = svm_seg(vcpu, seg);
722
723         return s->base;
724 }
725
726 static void svm_get_segment(struct kvm_vcpu *vcpu,
727                             struct kvm_segment *var, int seg)
728 {
729         struct vmcb_seg *s = svm_seg(vcpu, seg);
730
731         var->base = s->base;
732         var->limit = s->limit;
733         var->selector = s->selector;
734         var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
735         var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
736         var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
737         var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
738         var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
739         var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
740         var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
741         var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
742         var->unusable = !var->present;
743 }
744
745 static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
746 {
747         struct vcpu_svm *svm = to_svm(vcpu);
748
749         dt->limit = svm->vmcb->save.idtr.limit;
750         dt->base = svm->vmcb->save.idtr.base;
751 }
752
753 static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
754 {
755         struct vcpu_svm *svm = to_svm(vcpu);
756
757         svm->vmcb->save.idtr.limit = dt->limit;
758         svm->vmcb->save.idtr.base = dt->base ;
759 }
760
761 static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
762 {
763         struct vcpu_svm *svm = to_svm(vcpu);
764
765         dt->limit = svm->vmcb->save.gdtr.limit;
766         dt->base = svm->vmcb->save.gdtr.base;
767 }
768
769 static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
770 {
771         struct vcpu_svm *svm = to_svm(vcpu);
772
773         svm->vmcb->save.gdtr.limit = dt->limit;
774         svm->vmcb->save.gdtr.base = dt->base ;
775 }
776
777 static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
778 {
779 }
780
781 static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
782 {
783         struct vcpu_svm *svm = to_svm(vcpu);
784
785 #ifdef CONFIG_X86_64
786         if (vcpu->shadow_efer & KVM_EFER_LME) {
787                 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
788                         vcpu->shadow_efer |= KVM_EFER_LMA;
789                         svm->vmcb->save.efer |= KVM_EFER_LMA | KVM_EFER_LME;
790                 }
791
792                 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG) ) {
793                         vcpu->shadow_efer &= ~KVM_EFER_LMA;
794                         svm->vmcb->save.efer &= ~(KVM_EFER_LMA | KVM_EFER_LME);
795                 }
796         }
797 #endif
798         if ((vcpu->cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
799                 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
800                 vcpu->fpu_active = 1;
801         }
802
803         vcpu->cr0 = cr0;
804         cr0 |= X86_CR0_PG | X86_CR0_WP;
805         cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
806         svm->vmcb->save.cr0 = cr0;
807 }
808
809 static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
810 {
811        vcpu->cr4 = cr4;
812        to_svm(vcpu)->vmcb->save.cr4 = cr4 | X86_CR4_PAE;
813 }
814
815 static void svm_set_segment(struct kvm_vcpu *vcpu,
816                             struct kvm_segment *var, int seg)
817 {
818         struct vcpu_svm *svm = to_svm(vcpu);
819         struct vmcb_seg *s = svm_seg(vcpu, seg);
820
821         s->base = var->base;
822         s->limit = var->limit;
823         s->selector = var->selector;
824         if (var->unusable)
825                 s->attrib = 0;
826         else {
827                 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
828                 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
829                 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
830                 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
831                 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
832                 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
833                 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
834                 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
835         }
836         if (seg == VCPU_SREG_CS)
837                 svm->vmcb->save.cpl
838                         = (svm->vmcb->save.cs.attrib
839                            >> SVM_SELECTOR_DPL_SHIFT) & 3;
840
841 }
842
843 /* FIXME:
844
845         svm(vcpu)->vmcb->control.int_ctl &= ~V_TPR_MASK;
846         svm(vcpu)->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
847
848 */
849
850 static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
851 {
852         return -EOPNOTSUPP;
853 }
854
855 static int svm_get_irq(struct kvm_vcpu *vcpu)
856 {
857         struct vcpu_svm *svm = to_svm(vcpu);
858         u32 exit_int_info = svm->vmcb->control.exit_int_info;
859
860         if (is_external_interrupt(exit_int_info))
861                 return exit_int_info & SVM_EVTINJ_VEC_MASK;
862         return -1;
863 }
864
865 static void load_host_msrs(struct kvm_vcpu *vcpu)
866 {
867 #ifdef CONFIG_X86_64
868         wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
869 #endif
870 }
871
872 static void save_host_msrs(struct kvm_vcpu *vcpu)
873 {
874 #ifdef CONFIG_X86_64
875         rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
876 #endif
877 }
878
879 static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
880 {
881         if (svm_data->next_asid > svm_data->max_asid) {
882                 ++svm_data->asid_generation;
883                 svm_data->next_asid = 1;
884                 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
885         }
886
887         svm->vcpu.cpu = svm_data->cpu;
888         svm->asid_generation = svm_data->asid_generation;
889         svm->vmcb->control.asid = svm_data->next_asid++;
890 }
891
892 static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
893 {
894         return to_svm(vcpu)->db_regs[dr];
895 }
896
897 static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
898                        int *exception)
899 {
900         struct vcpu_svm *svm = to_svm(vcpu);
901
902         *exception = 0;
903
904         if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
905                 svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
906                 svm->vmcb->save.dr6 |= DR6_BD_MASK;
907                 *exception = DB_VECTOR;
908                 return;
909         }
910
911         switch (dr) {
912         case 0 ... 3:
913                 svm->db_regs[dr] = value;
914                 return;
915         case 4 ... 5:
916                 if (vcpu->cr4 & X86_CR4_DE) {
917                         *exception = UD_VECTOR;
918                         return;
919                 }
920         case 7: {
921                 if (value & ~((1ULL << 32) - 1)) {
922                         *exception = GP_VECTOR;
923                         return;
924                 }
925                 svm->vmcb->save.dr7 = value;
926                 return;
927         }
928         default:
929                 printk(KERN_DEBUG "%s: unexpected dr %u\n",
930                        __FUNCTION__, dr);
931                 *exception = UD_VECTOR;
932                 return;
933         }
934 }
935
936 static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
937 {
938         u32 exit_int_info = svm->vmcb->control.exit_int_info;
939         struct kvm *kvm = svm->vcpu.kvm;
940         u64 fault_address;
941         u32 error_code;
942         enum emulation_result er;
943         int r;
944
945         if (!irqchip_in_kernel(kvm) &&
946                 is_external_interrupt(exit_int_info))
947                 push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
948
949         mutex_lock(&kvm->lock);
950
951         fault_address  = svm->vmcb->control.exit_info_2;
952         error_code = svm->vmcb->control.exit_info_1;
953         r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
954         if (r < 0) {
955                 mutex_unlock(&kvm->lock);
956                 return r;
957         }
958         if (!r) {
959                 mutex_unlock(&kvm->lock);
960                 return 1;
961         }
962         er = emulate_instruction(&svm->vcpu, kvm_run, fault_address,
963                                  error_code);
964         mutex_unlock(&kvm->lock);
965
966         switch (er) {
967         case EMULATE_DONE:
968                 return 1;
969         case EMULATE_DO_MMIO:
970                 ++svm->vcpu.stat.mmio_exits;
971                 return 0;
972         case EMULATE_FAIL:
973                 kvm_report_emulation_failure(&svm->vcpu, "pagetable");
974                 break;
975         default:
976                 BUG();
977         }
978
979         kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
980         return 0;
981 }
982
983 static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
984 {
985         int er;
986
987         er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0);
988         if (er != EMULATE_DONE)
989                 inject_ud(&svm->vcpu);
990
991         return 1;
992 }
993
994 static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
995 {
996         svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
997         if (!(svm->vcpu.cr0 & X86_CR0_TS))
998                 svm->vmcb->save.cr0 &= ~X86_CR0_TS;
999         svm->vcpu.fpu_active = 1;
1000
1001         return 1;
1002 }
1003
1004 static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1005 {
1006         /*
1007          * VMCB is undefined after a SHUTDOWN intercept
1008          * so reinitialize it.
1009          */
1010         clear_page(svm->vmcb);
1011         init_vmcb(svm->vmcb);
1012
1013         kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1014         return 0;
1015 }
1016
1017 static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1018 {
1019         u32 io_info = svm->vmcb->control.exit_info_1; //address size bug?
1020         int size, down, in, string, rep;
1021         unsigned port;
1022
1023         ++svm->vcpu.stat.io_exits;
1024
1025         svm->next_rip = svm->vmcb->control.exit_info_2;
1026
1027         string = (io_info & SVM_IOIO_STR_MASK) != 0;
1028
1029         if (string) {
1030                 if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0) == EMULATE_DO_MMIO)
1031                         return 0;
1032                 return 1;
1033         }
1034
1035         in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1036         port = io_info >> 16;
1037         size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
1038         rep = (io_info & SVM_IOIO_REP_MASK) != 0;
1039         down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
1040
1041         return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
1042 }
1043
1044 static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1045 {
1046         return 1;
1047 }
1048
1049 static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1050 {
1051         svm->next_rip = svm->vmcb->save.rip + 1;
1052         skip_emulated_instruction(&svm->vcpu);
1053         return kvm_emulate_halt(&svm->vcpu);
1054 }
1055
1056 static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1057 {
1058         svm->next_rip = svm->vmcb->save.rip + 3;
1059         skip_emulated_instruction(&svm->vcpu);
1060         kvm_emulate_hypercall(&svm->vcpu);
1061         return 1;
1062 }
1063
1064 static int invalid_op_interception(struct vcpu_svm *svm,
1065                                    struct kvm_run *kvm_run)
1066 {
1067         inject_ud(&svm->vcpu);
1068         return 1;
1069 }
1070
1071 static int task_switch_interception(struct vcpu_svm *svm,
1072                                     struct kvm_run *kvm_run)
1073 {
1074         pr_unimpl(&svm->vcpu, "%s: task switch is unsupported\n", __FUNCTION__);
1075         kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
1076         return 0;
1077 }
1078
1079 static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1080 {
1081         svm->next_rip = svm->vmcb->save.rip + 2;
1082         kvm_emulate_cpuid(&svm->vcpu);
1083         return 1;
1084 }
1085
1086 static int emulate_on_interception(struct vcpu_svm *svm,
1087                                    struct kvm_run *kvm_run)
1088 {
1089         if (emulate_instruction(&svm->vcpu, NULL, 0, 0) != EMULATE_DONE)
1090                 pr_unimpl(&svm->vcpu, "%s: failed\n", __FUNCTION__);
1091         return 1;
1092 }
1093
1094 static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
1095 {
1096         struct vcpu_svm *svm = to_svm(vcpu);
1097
1098         switch (ecx) {
1099         case MSR_IA32_TIME_STAMP_COUNTER: {
1100                 u64 tsc;
1101
1102                 rdtscll(tsc);
1103                 *data = svm->vmcb->control.tsc_offset + tsc;
1104                 break;
1105         }
1106         case MSR_K6_STAR:
1107                 *data = svm->vmcb->save.star;
1108                 break;
1109 #ifdef CONFIG_X86_64
1110         case MSR_LSTAR:
1111                 *data = svm->vmcb->save.lstar;
1112                 break;
1113         case MSR_CSTAR:
1114                 *data = svm->vmcb->save.cstar;
1115                 break;
1116         case MSR_KERNEL_GS_BASE:
1117                 *data = svm->vmcb->save.kernel_gs_base;
1118                 break;
1119         case MSR_SYSCALL_MASK:
1120                 *data = svm->vmcb->save.sfmask;
1121                 break;
1122 #endif
1123         case MSR_IA32_SYSENTER_CS:
1124                 *data = svm->vmcb->save.sysenter_cs;
1125                 break;
1126         case MSR_IA32_SYSENTER_EIP:
1127                 *data = svm->vmcb->save.sysenter_eip;
1128                 break;
1129         case MSR_IA32_SYSENTER_ESP:
1130                 *data = svm->vmcb->save.sysenter_esp;
1131                 break;
1132         default:
1133                 return kvm_get_msr_common(vcpu, ecx, data);
1134         }
1135         return 0;
1136 }
1137
1138 static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1139 {
1140         u32 ecx = svm->vcpu.regs[VCPU_REGS_RCX];
1141         u64 data;
1142
1143         if (svm_get_msr(&svm->vcpu, ecx, &data))
1144                 svm_inject_gp(&svm->vcpu, 0);
1145         else {
1146                 svm->vmcb->save.rax = data & 0xffffffff;
1147                 svm->vcpu.regs[VCPU_REGS_RDX] = data >> 32;
1148                 svm->next_rip = svm->vmcb->save.rip + 2;
1149                 skip_emulated_instruction(&svm->vcpu);
1150         }
1151         return 1;
1152 }
1153
1154 static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
1155 {
1156         struct vcpu_svm *svm = to_svm(vcpu);
1157
1158         switch (ecx) {
1159         case MSR_IA32_TIME_STAMP_COUNTER: {
1160                 u64 tsc;
1161
1162                 rdtscll(tsc);
1163                 svm->vmcb->control.tsc_offset = data - tsc;
1164                 break;
1165         }
1166         case MSR_K6_STAR:
1167                 svm->vmcb->save.star = data;
1168                 break;
1169 #ifdef CONFIG_X86_64
1170         case MSR_LSTAR:
1171                 svm->vmcb->save.lstar = data;
1172                 break;
1173         case MSR_CSTAR:
1174                 svm->vmcb->save.cstar = data;
1175                 break;
1176         case MSR_KERNEL_GS_BASE:
1177                 svm->vmcb->save.kernel_gs_base = data;
1178                 break;
1179         case MSR_SYSCALL_MASK:
1180                 svm->vmcb->save.sfmask = data;
1181                 break;
1182 #endif
1183         case MSR_IA32_SYSENTER_CS:
1184                 svm->vmcb->save.sysenter_cs = data;
1185                 break;
1186         case MSR_IA32_SYSENTER_EIP:
1187                 svm->vmcb->save.sysenter_eip = data;
1188                 break;
1189         case MSR_IA32_SYSENTER_ESP:
1190                 svm->vmcb->save.sysenter_esp = data;
1191                 break;
1192         default:
1193                 return kvm_set_msr_common(vcpu, ecx, data);
1194         }
1195         return 0;
1196 }
1197
1198 static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1199 {
1200         u32 ecx = svm->vcpu.regs[VCPU_REGS_RCX];
1201         u64 data = (svm->vmcb->save.rax & -1u)
1202                 | ((u64)(svm->vcpu.regs[VCPU_REGS_RDX] & -1u) << 32);
1203         svm->next_rip = svm->vmcb->save.rip + 2;
1204         if (svm_set_msr(&svm->vcpu, ecx, data))
1205                 svm_inject_gp(&svm->vcpu, 0);
1206         else
1207                 skip_emulated_instruction(&svm->vcpu);
1208         return 1;
1209 }
1210
1211 static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1212 {
1213         if (svm->vmcb->control.exit_info_1)
1214                 return wrmsr_interception(svm, kvm_run);
1215         else
1216                 return rdmsr_interception(svm, kvm_run);
1217 }
1218
1219 static int interrupt_window_interception(struct vcpu_svm *svm,
1220                                    struct kvm_run *kvm_run)
1221 {
1222         svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1223         svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
1224         /*
1225          * If the user space waits to inject interrupts, exit as soon as
1226          * possible
1227          */
1228         if (kvm_run->request_interrupt_window &&
1229             !svm->vcpu.irq_summary) {
1230                 ++svm->vcpu.stat.irq_window_exits;
1231                 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
1232                 return 0;
1233         }
1234
1235         return 1;
1236 }
1237
1238 static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
1239                                       struct kvm_run *kvm_run) = {
1240         [SVM_EXIT_READ_CR0]                     = emulate_on_interception,
1241         [SVM_EXIT_READ_CR3]                     = emulate_on_interception,
1242         [SVM_EXIT_READ_CR4]                     = emulate_on_interception,
1243         /* for now: */
1244         [SVM_EXIT_WRITE_CR0]                    = emulate_on_interception,
1245         [SVM_EXIT_WRITE_CR3]                    = emulate_on_interception,
1246         [SVM_EXIT_WRITE_CR4]                    = emulate_on_interception,
1247         [SVM_EXIT_READ_DR0]                     = emulate_on_interception,
1248         [SVM_EXIT_READ_DR1]                     = emulate_on_interception,
1249         [SVM_EXIT_READ_DR2]                     = emulate_on_interception,
1250         [SVM_EXIT_READ_DR3]                     = emulate_on_interception,
1251         [SVM_EXIT_WRITE_DR0]                    = emulate_on_interception,
1252         [SVM_EXIT_WRITE_DR1]                    = emulate_on_interception,
1253         [SVM_EXIT_WRITE_DR2]                    = emulate_on_interception,
1254         [SVM_EXIT_WRITE_DR3]                    = emulate_on_interception,
1255         [SVM_EXIT_WRITE_DR5]                    = emulate_on_interception,
1256         [SVM_EXIT_WRITE_DR7]                    = emulate_on_interception,
1257         [SVM_EXIT_EXCP_BASE + UD_VECTOR]        = ud_interception,
1258         [SVM_EXIT_EXCP_BASE + PF_VECTOR]        = pf_interception,
1259         [SVM_EXIT_EXCP_BASE + NM_VECTOR]        = nm_interception,
1260         [SVM_EXIT_INTR]                         = nop_on_interception,
1261         [SVM_EXIT_NMI]                          = nop_on_interception,
1262         [SVM_EXIT_SMI]                          = nop_on_interception,
1263         [SVM_EXIT_INIT]                         = nop_on_interception,
1264         [SVM_EXIT_VINTR]                        = interrupt_window_interception,
1265         /* [SVM_EXIT_CR0_SEL_WRITE]             = emulate_on_interception, */
1266         [SVM_EXIT_CPUID]                        = cpuid_interception,
1267         [SVM_EXIT_INVD]                         = emulate_on_interception,
1268         [SVM_EXIT_HLT]                          = halt_interception,
1269         [SVM_EXIT_INVLPG]                       = emulate_on_interception,
1270         [SVM_EXIT_INVLPGA]                      = invalid_op_interception,
1271         [SVM_EXIT_IOIO]                         = io_interception,
1272         [SVM_EXIT_MSR]                          = msr_interception,
1273         [SVM_EXIT_TASK_SWITCH]                  = task_switch_interception,
1274         [SVM_EXIT_SHUTDOWN]                     = shutdown_interception,
1275         [SVM_EXIT_VMRUN]                        = invalid_op_interception,
1276         [SVM_EXIT_VMMCALL]                      = vmmcall_interception,
1277         [SVM_EXIT_VMLOAD]                       = invalid_op_interception,
1278         [SVM_EXIT_VMSAVE]                       = invalid_op_interception,
1279         [SVM_EXIT_STGI]                         = invalid_op_interception,
1280         [SVM_EXIT_CLGI]                         = invalid_op_interception,
1281         [SVM_EXIT_SKINIT]                       = invalid_op_interception,
1282         [SVM_EXIT_WBINVD]                       = emulate_on_interception,
1283         [SVM_EXIT_MONITOR]                      = invalid_op_interception,
1284         [SVM_EXIT_MWAIT]                        = invalid_op_interception,
1285 };
1286
1287
1288 static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1289 {
1290         struct vcpu_svm *svm = to_svm(vcpu);
1291         u32 exit_code = svm->vmcb->control.exit_code;
1292
1293         kvm_reput_irq(svm);
1294
1295         if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
1296                 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
1297                 kvm_run->fail_entry.hardware_entry_failure_reason
1298                         = svm->vmcb->control.exit_code;
1299                 return 0;
1300         }
1301
1302         if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
1303             exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR)
1304                 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
1305                        "exit_code 0x%x\n",
1306                        __FUNCTION__, svm->vmcb->control.exit_int_info,
1307                        exit_code);
1308
1309         if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
1310             || svm_exit_handlers[exit_code] == 0) {
1311                 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
1312                 kvm_run->hw.hardware_exit_reason = exit_code;
1313                 return 0;
1314         }
1315
1316         return svm_exit_handlers[exit_code](svm, kvm_run);
1317 }
1318
1319 static void reload_tss(struct kvm_vcpu *vcpu)
1320 {
1321         int cpu = raw_smp_processor_id();
1322
1323         struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
1324         svm_data->tss_desc->type = 9; //available 32/64-bit TSS
1325         load_TR_desc();
1326 }
1327
1328 static void pre_svm_run(struct vcpu_svm *svm)
1329 {
1330         int cpu = raw_smp_processor_id();
1331
1332         struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
1333
1334         svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
1335         if (svm->vcpu.cpu != cpu ||
1336             svm->asid_generation != svm_data->asid_generation)
1337                 new_asid(svm, svm_data);
1338 }
1339
1340
1341 static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
1342 {
1343         struct vmcb_control_area *control;
1344
1345         control = &svm->vmcb->control;
1346         control->int_vector = irq;
1347         control->int_ctl &= ~V_INTR_PRIO_MASK;
1348         control->int_ctl |= V_IRQ_MASK |
1349                 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1350 }
1351
1352 static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
1353 {
1354         struct vcpu_svm *svm = to_svm(vcpu);
1355
1356         svm_inject_irq(svm, irq);
1357 }
1358
1359 static void svm_intr_assist(struct kvm_vcpu *vcpu)
1360 {
1361         struct vcpu_svm *svm = to_svm(vcpu);
1362         struct vmcb *vmcb = svm->vmcb;
1363         int intr_vector = -1;
1364
1365         kvm_inject_pending_timer_irqs(vcpu);
1366         if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
1367             ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
1368                 intr_vector = vmcb->control.exit_int_info &
1369                               SVM_EVTINJ_VEC_MASK;
1370                 vmcb->control.exit_int_info = 0;
1371                 svm_inject_irq(svm, intr_vector);
1372                 return;
1373         }
1374
1375         if (vmcb->control.int_ctl & V_IRQ_MASK)
1376                 return;
1377
1378         if (!kvm_cpu_has_interrupt(vcpu))
1379                 return;
1380
1381         if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
1382             (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
1383             (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
1384                 /* unable to deliver irq, set pending irq */
1385                 vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
1386                 svm_inject_irq(svm, 0x0);
1387                 return;
1388         }
1389         /* Okay, we can deliver the interrupt: grab it and update PIC state. */
1390         intr_vector = kvm_cpu_get_interrupt(vcpu);
1391         svm_inject_irq(svm, intr_vector);
1392         kvm_timer_intr_post(vcpu, intr_vector);
1393 }
1394
1395 static void kvm_reput_irq(struct vcpu_svm *svm)
1396 {
1397         struct vmcb_control_area *control = &svm->vmcb->control;
1398
1399         if ((control->int_ctl & V_IRQ_MASK)
1400             && !irqchip_in_kernel(svm->vcpu.kvm)) {
1401                 control->int_ctl &= ~V_IRQ_MASK;
1402                 push_irq(&svm->vcpu, control->int_vector);
1403         }
1404
1405         svm->vcpu.interrupt_window_open =
1406                 !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
1407 }
1408
1409 static void svm_do_inject_vector(struct vcpu_svm *svm)
1410 {
1411         struct kvm_vcpu *vcpu = &svm->vcpu;
1412         int word_index = __ffs(vcpu->irq_summary);
1413         int bit_index = __ffs(vcpu->irq_pending[word_index]);
1414         int irq = word_index * BITS_PER_LONG + bit_index;
1415
1416         clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1417         if (!vcpu->irq_pending[word_index])
1418                 clear_bit(word_index, &vcpu->irq_summary);
1419         svm_inject_irq(svm, irq);
1420 }
1421
1422 static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1423                                        struct kvm_run *kvm_run)
1424 {
1425         struct vcpu_svm *svm = to_svm(vcpu);
1426         struct vmcb_control_area *control = &svm->vmcb->control;
1427
1428         svm->vcpu.interrupt_window_open =
1429                 (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
1430                  (svm->vmcb->save.rflags & X86_EFLAGS_IF));
1431
1432         if (svm->vcpu.interrupt_window_open && svm->vcpu.irq_summary)
1433                 /*
1434                  * If interrupts enabled, and not blocked by sti or mov ss. Good.
1435                  */
1436                 svm_do_inject_vector(svm);
1437
1438         /*
1439          * Interrupts blocked.  Wait for unblock.
1440          */
1441         if (!svm->vcpu.interrupt_window_open &&
1442             (svm->vcpu.irq_summary || kvm_run->request_interrupt_window)) {
1443                 control->intercept |= 1ULL << INTERCEPT_VINTR;
1444         } else
1445                 control->intercept &= ~(1ULL << INTERCEPT_VINTR);
1446 }
1447
1448 static void save_db_regs(unsigned long *db_regs)
1449 {
1450         asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
1451         asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
1452         asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
1453         asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
1454 }
1455
1456 static void load_db_regs(unsigned long *db_regs)
1457 {
1458         asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
1459         asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
1460         asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
1461         asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
1462 }
1463
1464 static void svm_flush_tlb(struct kvm_vcpu *vcpu)
1465 {
1466         force_new_asid(vcpu);
1467 }
1468
1469 static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
1470 {
1471 }
1472
1473 static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1474 {
1475         struct vcpu_svm *svm = to_svm(vcpu);
1476         u16 fs_selector;
1477         u16 gs_selector;
1478         u16 ldt_selector;
1479
1480         pre_svm_run(svm);
1481
1482         save_host_msrs(vcpu);
1483         fs_selector = read_fs();
1484         gs_selector = read_gs();
1485         ldt_selector = read_ldt();
1486         svm->host_cr2 = kvm_read_cr2();
1487         svm->host_dr6 = read_dr6();
1488         svm->host_dr7 = read_dr7();
1489         svm->vmcb->save.cr2 = vcpu->cr2;
1490
1491         if (svm->vmcb->save.dr7 & 0xff) {
1492                 write_dr7(0);
1493                 save_db_regs(svm->host_db_regs);
1494                 load_db_regs(svm->db_regs);
1495         }
1496
1497         clgi();
1498
1499         local_irq_enable();
1500
1501         asm volatile (
1502 #ifdef CONFIG_X86_64
1503                 "push %%rbx; push %%rcx; push %%rdx;"
1504                 "push %%rsi; push %%rdi; push %%rbp;"
1505                 "push %%r8;  push %%r9;  push %%r10; push %%r11;"
1506                 "push %%r12; push %%r13; push %%r14; push %%r15;"
1507 #else
1508                 "push %%ebx; push %%ecx; push %%edx;"
1509                 "push %%esi; push %%edi; push %%ebp;"
1510 #endif
1511
1512 #ifdef CONFIG_X86_64
1513                 "mov %c[rbx](%[svm]), %%rbx \n\t"
1514                 "mov %c[rcx](%[svm]), %%rcx \n\t"
1515                 "mov %c[rdx](%[svm]), %%rdx \n\t"
1516                 "mov %c[rsi](%[svm]), %%rsi \n\t"
1517                 "mov %c[rdi](%[svm]), %%rdi \n\t"
1518                 "mov %c[rbp](%[svm]), %%rbp \n\t"
1519                 "mov %c[r8](%[svm]),  %%r8  \n\t"
1520                 "mov %c[r9](%[svm]),  %%r9  \n\t"
1521                 "mov %c[r10](%[svm]), %%r10 \n\t"
1522                 "mov %c[r11](%[svm]), %%r11 \n\t"
1523                 "mov %c[r12](%[svm]), %%r12 \n\t"
1524                 "mov %c[r13](%[svm]), %%r13 \n\t"
1525                 "mov %c[r14](%[svm]), %%r14 \n\t"
1526                 "mov %c[r15](%[svm]), %%r15 \n\t"
1527 #else
1528                 "mov %c[rbx](%[svm]), %%ebx \n\t"
1529                 "mov %c[rcx](%[svm]), %%ecx \n\t"
1530                 "mov %c[rdx](%[svm]), %%edx \n\t"
1531                 "mov %c[rsi](%[svm]), %%esi \n\t"
1532                 "mov %c[rdi](%[svm]), %%edi \n\t"
1533                 "mov %c[rbp](%[svm]), %%ebp \n\t"
1534 #endif
1535
1536 #ifdef CONFIG_X86_64
1537                 /* Enter guest mode */
1538                 "push %%rax \n\t"
1539                 "mov %c[vmcb](%[svm]), %%rax \n\t"
1540                 SVM_VMLOAD "\n\t"
1541                 SVM_VMRUN "\n\t"
1542                 SVM_VMSAVE "\n\t"
1543                 "pop %%rax \n\t"
1544 #else
1545                 /* Enter guest mode */
1546                 "push %%eax \n\t"
1547                 "mov %c[vmcb](%[svm]), %%eax \n\t"
1548                 SVM_VMLOAD "\n\t"
1549                 SVM_VMRUN "\n\t"
1550                 SVM_VMSAVE "\n\t"
1551                 "pop %%eax \n\t"
1552 #endif
1553
1554                 /* Save guest registers, load host registers */
1555 #ifdef CONFIG_X86_64
1556                 "mov %%rbx, %c[rbx](%[svm]) \n\t"
1557                 "mov %%rcx, %c[rcx](%[svm]) \n\t"
1558                 "mov %%rdx, %c[rdx](%[svm]) \n\t"
1559                 "mov %%rsi, %c[rsi](%[svm]) \n\t"
1560                 "mov %%rdi, %c[rdi](%[svm]) \n\t"
1561                 "mov %%rbp, %c[rbp](%[svm]) \n\t"
1562                 "mov %%r8,  %c[r8](%[svm]) \n\t"
1563                 "mov %%r9,  %c[r9](%[svm]) \n\t"
1564                 "mov %%r10, %c[r10](%[svm]) \n\t"
1565                 "mov %%r11, %c[r11](%[svm]) \n\t"
1566                 "mov %%r12, %c[r12](%[svm]) \n\t"
1567                 "mov %%r13, %c[r13](%[svm]) \n\t"
1568                 "mov %%r14, %c[r14](%[svm]) \n\t"
1569                 "mov %%r15, %c[r15](%[svm]) \n\t"
1570
1571                 "pop  %%r15; pop  %%r14; pop  %%r13; pop  %%r12;"
1572                 "pop  %%r11; pop  %%r10; pop  %%r9;  pop  %%r8;"
1573                 "pop  %%rbp; pop  %%rdi; pop  %%rsi;"
1574                 "pop  %%rdx; pop  %%rcx; pop  %%rbx; \n\t"
1575 #else
1576                 "mov %%ebx, %c[rbx](%[svm]) \n\t"
1577                 "mov %%ecx, %c[rcx](%[svm]) \n\t"
1578                 "mov %%edx, %c[rdx](%[svm]) \n\t"
1579                 "mov %%esi, %c[rsi](%[svm]) \n\t"
1580                 "mov %%edi, %c[rdi](%[svm]) \n\t"
1581                 "mov %%ebp, %c[rbp](%[svm]) \n\t"
1582
1583                 "pop  %%ebp; pop  %%edi; pop  %%esi;"
1584                 "pop  %%edx; pop  %%ecx; pop  %%ebx; \n\t"
1585 #endif
1586                 :
1587                 : [svm]"a"(svm),
1588                   [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
1589                   [rbx]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RBX])),
1590                   [rcx]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RCX])),
1591                   [rdx]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RDX])),
1592                   [rsi]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RSI])),
1593                   [rdi]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RDI])),
1594                   [rbp]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RBP]))
1595 #ifdef CONFIG_X86_64
1596                   ,[r8 ]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R8])),
1597                   [r9 ]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R9 ])),
1598                   [r10]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R10])),
1599                   [r11]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R11])),
1600                   [r12]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R12])),
1601                   [r13]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R13])),
1602                   [r14]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R14])),
1603                   [r15]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R15]))
1604 #endif
1605                 : "cc", "memory" );
1606
1607         if ((svm->vmcb->save.dr7 & 0xff))
1608                 load_db_regs(svm->host_db_regs);
1609
1610         vcpu->cr2 = svm->vmcb->save.cr2;
1611
1612         write_dr6(svm->host_dr6);
1613         write_dr7(svm->host_dr7);
1614         kvm_write_cr2(svm->host_cr2);
1615
1616         load_fs(fs_selector);
1617         load_gs(gs_selector);
1618         load_ldt(ldt_selector);
1619         load_host_msrs(vcpu);
1620
1621         reload_tss(vcpu);
1622
1623         local_irq_disable();
1624
1625         stgi();
1626
1627         svm->next_rip = 0;
1628 }
1629
1630 static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
1631 {
1632         struct vcpu_svm *svm = to_svm(vcpu);
1633
1634         svm->vmcb->save.cr3 = root;
1635         force_new_asid(vcpu);
1636
1637         if (vcpu->fpu_active) {
1638                 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
1639                 svm->vmcb->save.cr0 |= X86_CR0_TS;
1640                 vcpu->fpu_active = 0;
1641         }
1642 }
1643
1644 static void svm_inject_page_fault(struct kvm_vcpu *vcpu,
1645                                   unsigned long  addr,
1646                                   uint32_t err_code)
1647 {
1648         struct vcpu_svm *svm = to_svm(vcpu);
1649         uint32_t exit_int_info = svm->vmcb->control.exit_int_info;
1650
1651         ++vcpu->stat.pf_guest;
1652
1653         if (is_page_fault(exit_int_info)) {
1654
1655                 svm->vmcb->control.event_inj_err = 0;
1656                 svm->vmcb->control.event_inj =  SVM_EVTINJ_VALID |
1657                                                 SVM_EVTINJ_VALID_ERR |
1658                                                 SVM_EVTINJ_TYPE_EXEPT |
1659                                                 DF_VECTOR;
1660                 return;
1661         }
1662         vcpu->cr2 = addr;
1663         svm->vmcb->save.cr2 = addr;
1664         svm->vmcb->control.event_inj =  SVM_EVTINJ_VALID |
1665                                         SVM_EVTINJ_VALID_ERR |
1666                                         SVM_EVTINJ_TYPE_EXEPT |
1667                                         PF_VECTOR;
1668         svm->vmcb->control.event_inj_err = err_code;
1669 }
1670
1671
1672 static int is_disabled(void)
1673 {
1674         u64 vm_cr;
1675
1676         rdmsrl(MSR_VM_CR, vm_cr);
1677         if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
1678                 return 1;
1679
1680         return 0;
1681 }
1682
1683 static void
1684 svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1685 {
1686         /*
1687          * Patch in the VMMCALL instruction:
1688          */
1689         hypercall[0] = 0x0f;
1690         hypercall[1] = 0x01;
1691         hypercall[2] = 0xd9;
1692 }
1693
1694 static void svm_check_processor_compat(void *rtn)
1695 {
1696         *(int *)rtn = 0;
1697 }
1698
1699 static struct kvm_x86_ops svm_x86_ops = {
1700         .cpu_has_kvm_support = has_svm,
1701         .disabled_by_bios = is_disabled,
1702         .hardware_setup = svm_hardware_setup,
1703         .hardware_unsetup = svm_hardware_unsetup,
1704         .check_processor_compatibility = svm_check_processor_compat,
1705         .hardware_enable = svm_hardware_enable,
1706         .hardware_disable = svm_hardware_disable,
1707
1708         .vcpu_create = svm_create_vcpu,
1709         .vcpu_free = svm_free_vcpu,
1710         .vcpu_reset = svm_vcpu_reset,
1711
1712         .prepare_guest_switch = svm_prepare_guest_switch,
1713         .vcpu_load = svm_vcpu_load,
1714         .vcpu_put = svm_vcpu_put,
1715         .vcpu_decache = svm_vcpu_decache,
1716
1717         .set_guest_debug = svm_guest_debug,
1718         .get_msr = svm_get_msr,
1719         .set_msr = svm_set_msr,
1720         .get_segment_base = svm_get_segment_base,
1721         .get_segment = svm_get_segment,
1722         .set_segment = svm_set_segment,
1723         .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
1724         .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
1725         .set_cr0 = svm_set_cr0,
1726         .set_cr3 = svm_set_cr3,
1727         .set_cr4 = svm_set_cr4,
1728         .set_efer = svm_set_efer,
1729         .get_idt = svm_get_idt,
1730         .set_idt = svm_set_idt,
1731         .get_gdt = svm_get_gdt,
1732         .set_gdt = svm_set_gdt,
1733         .get_dr = svm_get_dr,
1734         .set_dr = svm_set_dr,
1735         .cache_regs = svm_cache_regs,
1736         .decache_regs = svm_decache_regs,
1737         .get_rflags = svm_get_rflags,
1738         .set_rflags = svm_set_rflags,
1739
1740         .tlb_flush = svm_flush_tlb,
1741         .inject_page_fault = svm_inject_page_fault,
1742
1743         .inject_gp = svm_inject_gp,
1744
1745         .run = svm_vcpu_run,
1746         .handle_exit = handle_exit,
1747         .skip_emulated_instruction = skip_emulated_instruction,
1748         .patch_hypercall = svm_patch_hypercall,
1749         .get_irq = svm_get_irq,
1750         .set_irq = svm_set_irq,
1751         .inject_pending_irq = svm_intr_assist,
1752         .inject_pending_vectors = do_interrupt_requests,
1753 };
1754
1755 static int __init svm_init(void)
1756 {
1757         return kvm_init_x86(&svm_x86_ops, sizeof(struct vcpu_svm),
1758                               THIS_MODULE);
1759 }
1760
1761 static void __exit svm_exit(void)
1762 {
1763         kvm_exit_x86();
1764 }
1765
1766 module_init(svm_init)
1767 module_exit(svm_exit)