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