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