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