blob: 794d95416f7b80c2f9e777c5f16fd5c0fd2d21ed [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
Avi Kivitye4956062007-06-28 14:15:57 -040017#include "kvm_svm.h"
18#include "x86_emulate.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020022#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/vmalloc.h>
24#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040025#include <linux/sched.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026
Avi Kivitye4956062007-06-28 14:15:57 -040027#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
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)
Avi Kivity6aa8b732006-12-10 02:21:36 -080041
42#define SEG_TYPE_LDT 2
43#define SEG_TYPE_BUSY_TSS16 3
44
45#define KVM_EFER_LMA (1 << 10)
46#define KVM_EFER_LME (1 << 8)
47
Joerg Roedel80b77062007-03-30 17:02:14 +030048#define SVM_FEATURE_NPT (1 << 0)
49#define SVM_FEATURE_LBRV (1 << 1)
50#define SVM_DEATURE_SVML (1 << 2)
51
Avi Kivity04d2cc72007-09-10 18:10:54 +030052static void kvm_reput_irq(struct vcpu_svm *svm);
53
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040054static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
55{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100056 return container_of(vcpu, struct vcpu_svm, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040057}
58
Avi Kivity6aa8b732006-12-10 02:21:36 -080059unsigned long iopm_base;
60unsigned long msrpm_base;
61
62struct kvm_ldttss_desc {
63 u16 limit0;
64 u16 base0;
65 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
66 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
67 u32 base3;
68 u32 zero1;
69} __attribute__((packed));
70
71struct svm_cpu_data {
72 int cpu;
73
Avi Kivity5008fdf2007-04-02 13:05:50 +030074 u64 asid_generation;
75 u32 max_asid;
76 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -080077 struct kvm_ldttss_desc *tss_desc;
78
79 struct page *save_area;
80};
81
82static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +030083static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -080084
85struct svm_init_data {
86 int cpu;
87 int r;
88};
89
90static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
91
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020092#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -080093#define MSRS_RANGE_SIZE 2048
94#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
95
96#define MAX_INST_SIZE 15
97
Joerg Roedel80b77062007-03-30 17:02:14 +030098static inline u32 svm_has(u32 feat)
99{
100 return svm_features & feat;
101}
102
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103static inline u8 pop_irq(struct kvm_vcpu *vcpu)
104{
105 int word_index = __ffs(vcpu->irq_summary);
106 int bit_index = __ffs(vcpu->irq_pending[word_index]);
107 int irq = word_index * BITS_PER_LONG + bit_index;
108
109 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
110 if (!vcpu->irq_pending[word_index])
111 clear_bit(word_index, &vcpu->irq_summary);
112 return irq;
113}
114
115static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq)
116{
117 set_bit(irq, vcpu->irq_pending);
118 set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
119}
120
121static inline void clgi(void)
122{
123 asm volatile (SVM_CLGI);
124}
125
126static inline void stgi(void)
127{
128 asm volatile (SVM_STGI);
129}
130
131static inline void invlpga(unsigned long addr, u32 asid)
132{
133 asm volatile (SVM_INVLPGA :: "a"(addr), "c"(asid));
134}
135
136static inline unsigned long kvm_read_cr2(void)
137{
138 unsigned long cr2;
139
140 asm volatile ("mov %%cr2, %0" : "=r" (cr2));
141 return cr2;
142}
143
144static inline void kvm_write_cr2(unsigned long val)
145{
146 asm volatile ("mov %0, %%cr2" :: "r" (val));
147}
148
149static inline unsigned long read_dr6(void)
150{
151 unsigned long dr6;
152
153 asm volatile ("mov %%dr6, %0" : "=r" (dr6));
154 return dr6;
155}
156
157static inline void write_dr6(unsigned long val)
158{
159 asm volatile ("mov %0, %%dr6" :: "r" (val));
160}
161
162static inline unsigned long read_dr7(void)
163{
164 unsigned long dr7;
165
166 asm volatile ("mov %%dr7, %0" : "=r" (dr7));
167 return dr7;
168}
169
170static inline void write_dr7(unsigned long val)
171{
172 asm volatile ("mov %0, %%dr7" :: "r" (val));
173}
174
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175static inline void force_new_asid(struct kvm_vcpu *vcpu)
176{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400177 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178}
179
180static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
181{
182 force_new_asid(vcpu);
183}
184
185static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
186{
187 if (!(efer & KVM_EFER_LMA))
188 efer &= ~KVM_EFER_LME;
189
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400190 to_svm(vcpu)->vmcb->save.efer = efer | MSR_EFER_SVME_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191 vcpu->shadow_efer = efer;
192}
193
194static void svm_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
195{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400196 struct vcpu_svm *svm = to_svm(vcpu);
197
198 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199 SVM_EVTINJ_VALID_ERR |
200 SVM_EVTINJ_TYPE_EXEPT |
201 GP_VECTOR;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400202 svm->vmcb->control.event_inj_err = error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203}
204
205static void inject_ud(struct kvm_vcpu *vcpu)
206{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400207 to_svm(vcpu)->vmcb->control.event_inj = SVM_EVTINJ_VALID |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208 SVM_EVTINJ_TYPE_EXEPT |
209 UD_VECTOR;
210}
211
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212static 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{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400226 struct vcpu_svm *svm = to_svm(vcpu);
227
228 if (!svm->next_rip) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229 printk(KERN_DEBUG "%s: NOP\n", __FUNCTION__);
230 return;
231 }
Rusty Russell3077c4512007-07-30 16:41:57 +1000232 if (svm->next_rip - svm->vmcb->save.rip > MAX_INST_SIZE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233 printk(KERN_ERR "%s: ip 0x%llx next 0x%llx\n",
234 __FUNCTION__,
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400235 svm->vmcb->save.rip,
236 svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237 }
238
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400239 vcpu->rip = svm->vmcb->save.rip = svm->next_rip;
240 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -0800241
242 vcpu->interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243}
244
245static int has_svm(void)
246{
247 uint32_t eax, ebx, ecx, edx;
248
Avi Kivity1e885462006-12-29 16:49:34 -0800249 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800250 printk(KERN_INFO "has_svm: not amd\n");
251 return 0;
252 }
253
254 cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
255 if (eax < SVM_CPUID_FUNC) {
256 printk(KERN_INFO "has_svm: can't execute cpuid_8000000a\n");
257 return 0;
258 }
259
260 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
261 if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) {
262 printk(KERN_DEBUG "has_svm: svm not available\n");
263 return 0;
264 }
265 return 1;
266}
267
268static void svm_hardware_disable(void *garbage)
269{
270 struct svm_cpu_data *svm_data
271 = per_cpu(svm_data, raw_smp_processor_id());
272
273 if (svm_data) {
274 uint64_t efer;
275
276 wrmsrl(MSR_VM_HSAVE_PA, 0);
277 rdmsrl(MSR_EFER, efer);
278 wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK);
Al Viro8b6d44c2007-02-09 16:38:40 +0000279 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280 __free_page(svm_data->save_area);
281 kfree(svm_data);
282 }
283}
284
285static void svm_hardware_enable(void *garbage)
286{
287
288 struct svm_cpu_data *svm_data;
289 uint64_t efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800290#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291 struct desc_ptr gdt_descr;
292#else
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100293 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294#endif
295 struct desc_struct *gdt;
296 int me = raw_smp_processor_id();
297
298 if (!has_svm()) {
299 printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
300 return;
301 }
302 svm_data = per_cpu(svm_data, me);
303
304 if (!svm_data) {
305 printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
306 me);
307 return;
308 }
309
310 svm_data->asid_generation = 1;
311 svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
312 svm_data->next_asid = svm_data->max_asid + 1;
Joerg Roedel80b77062007-03-30 17:02:14 +0300313 svm_features = cpuid_edx(SVM_CPUID_FUNC);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800314
315 asm volatile ( "sgdt %0" : "=m"(gdt_descr) );
316 gdt = (struct desc_struct *)gdt_descr.address;
317 svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
318
319 rdmsrl(MSR_EFER, efer);
320 wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK);
321
322 wrmsrl(MSR_VM_HSAVE_PA,
323 page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
324}
325
326static int svm_cpu_init(int cpu)
327{
328 struct svm_cpu_data *svm_data;
329 int r;
330
331 svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
332 if (!svm_data)
333 return -ENOMEM;
334 svm_data->cpu = cpu;
335 svm_data->save_area = alloc_page(GFP_KERNEL);
336 r = -ENOMEM;
337 if (!svm_data->save_area)
338 goto err_1;
339
340 per_cpu(svm_data, cpu) = svm_data;
341
342 return 0;
343
344err_1:
345 kfree(svm_data);
346 return r;
347
348}
349
Rusty Russellbfc733a2007-07-31 20:42:42 +1000350static void set_msr_interception(u32 *msrpm, unsigned msr,
351 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352{
353 int i;
354
355 for (i = 0; i < NUM_MSR_MAPS; i++) {
356 if (msr >= msrpm_ranges[i] &&
357 msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
358 u32 msr_offset = (i * MSRS_IN_RANGE + msr -
359 msrpm_ranges[i]) * 2;
360
361 u32 *base = msrpm + (msr_offset / 32);
362 u32 msr_shift = msr_offset % 32;
363 u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
364 *base = (*base & ~(0x3 << msr_shift)) |
365 (mask << msr_shift);
Rusty Russellbfc733a2007-07-31 20:42:42 +1000366 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 }
368 }
Rusty Russellbfc733a2007-07-31 20:42:42 +1000369 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370}
371
372static __init int svm_hardware_setup(void)
373{
374 int cpu;
375 struct page *iopm_pages;
376 struct page *msrpm_pages;
Anthony Liguoric8681332007-04-30 09:48:11 +0300377 void *iopm_va, *msrpm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378 int r;
379
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
381
382 if (!iopm_pages)
383 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300384
385 iopm_va = page_address(iopm_pages);
386 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
387 clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800388 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
389
390
391 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
392
393 r = -ENOMEM;
394 if (!msrpm_pages)
395 goto err_1;
396
397 msrpm_va = page_address(msrpm_pages);
398 memset(msrpm_va, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
399 msrpm_base = page_to_pfn(msrpm_pages) << PAGE_SHIFT;
400
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800401#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402 set_msr_interception(msrpm_va, MSR_GS_BASE, 1, 1);
403 set_msr_interception(msrpm_va, MSR_FS_BASE, 1, 1);
404 set_msr_interception(msrpm_va, MSR_KERNEL_GS_BASE, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405 set_msr_interception(msrpm_va, MSR_LSTAR, 1, 1);
406 set_msr_interception(msrpm_va, MSR_CSTAR, 1, 1);
407 set_msr_interception(msrpm_va, MSR_SYSCALL_MASK, 1, 1);
408#endif
Avi Kivity0e859ca2006-12-22 01:05:08 -0800409 set_msr_interception(msrpm_va, MSR_K6_STAR, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_CS, 1, 1);
411 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_ESP, 1, 1);
412 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_EIP, 1, 1);
413
414 for_each_online_cpu(cpu) {
415 r = svm_cpu_init(cpu);
416 if (r)
417 goto err_2;
418 }
419 return 0;
420
421err_2:
422 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
423 msrpm_base = 0;
424err_1:
425 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
426 iopm_base = 0;
427 return r;
428}
429
430static __exit void svm_hardware_unsetup(void)
431{
432 __free_pages(pfn_to_page(msrpm_base >> PAGE_SHIFT), MSRPM_ALLOC_ORDER);
433 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
434 iopm_base = msrpm_base = 0;
435}
436
437static void init_seg(struct vmcb_seg *seg)
438{
439 seg->selector = 0;
440 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
441 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
442 seg->limit = 0xffff;
443 seg->base = 0;
444}
445
446static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
447{
448 seg->selector = 0;
449 seg->attrib = SVM_SELECTOR_P_MASK | type;
450 seg->limit = 0xffff;
451 seg->base = 0;
452}
453
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454static void init_vmcb(struct vmcb *vmcb)
455{
456 struct vmcb_control_area *control = &vmcb->control;
457 struct vmcb_save_area *save = &vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458
459 control->intercept_cr_read = INTERCEPT_CR0_MASK |
460 INTERCEPT_CR3_MASK |
461 INTERCEPT_CR4_MASK;
462
463 control->intercept_cr_write = INTERCEPT_CR0_MASK |
464 INTERCEPT_CR3_MASK |
465 INTERCEPT_CR4_MASK;
466
467 control->intercept_dr_read = INTERCEPT_DR0_MASK |
468 INTERCEPT_DR1_MASK |
469 INTERCEPT_DR2_MASK |
470 INTERCEPT_DR3_MASK;
471
472 control->intercept_dr_write = INTERCEPT_DR0_MASK |
473 INTERCEPT_DR1_MASK |
474 INTERCEPT_DR2_MASK |
475 INTERCEPT_DR3_MASK |
476 INTERCEPT_DR5_MASK |
477 INTERCEPT_DR7_MASK;
478
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500479 control->intercept_exceptions = (1 << PF_VECTOR) |
480 (1 << UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481
482
483 control->intercept = (1ULL << INTERCEPT_INTR) |
484 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200485 (1ULL << INTERCEPT_SMI) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486 /*
487 * selective cr0 intercept bug?
488 * 0: 0f 22 d8 mov %eax,%cr3
489 * 3: 0f 20 c0 mov %cr0,%eax
490 * 6: 0d 00 00 00 80 or $0x80000000,%eax
491 * b: 0f 22 c0 mov %eax,%cr0
492 * set cr3 ->interception
493 * get cr0 ->interception
494 * set cr0 -> no interception
495 */
496 /* (1ULL << INTERCEPT_SELECTIVE_CR0) | */
497 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200498 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499 (1ULL << INTERCEPT_HLT) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500 (1ULL << INTERCEPT_INVLPGA) |
501 (1ULL << INTERCEPT_IOIO_PROT) |
502 (1ULL << INTERCEPT_MSR_PROT) |
503 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800504 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505 (1ULL << INTERCEPT_VMRUN) |
506 (1ULL << INTERCEPT_VMMCALL) |
507 (1ULL << INTERCEPT_VMLOAD) |
508 (1ULL << INTERCEPT_VMSAVE) |
509 (1ULL << INTERCEPT_STGI) |
510 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100511 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200512 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100513 (1ULL << INTERCEPT_MONITOR) |
514 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515
516 control->iopm_base_pa = iopm_base;
517 control->msrpm_base_pa = msrpm_base;
Avi Kivity0cc50642007-03-25 12:07:27 +0200518 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519 control->int_ctl = V_INTR_MASKING_MASK;
520
521 init_seg(&save->es);
522 init_seg(&save->ss);
523 init_seg(&save->ds);
524 init_seg(&save->fs);
525 init_seg(&save->gs);
526
527 save->cs.selector = 0xf000;
528 /* Executable/Readable Code Segment */
529 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
530 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
531 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800532 /*
533 * cs.base should really be 0xffff0000, but vmx can't handle that, so
534 * be consistent with it.
535 *
536 * Replace when we have real mode working for vmx.
537 */
538 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539
540 save->gdtr.limit = 0xffff;
541 save->idtr.limit = 0xffff;
542
543 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
544 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
545
546 save->efer = MSR_EFER_SVME_MASK;
547
548 save->dr6 = 0xffff0ff0;
549 save->dr7 = 0x400;
550 save->rflags = 2;
551 save->rip = 0x0000fff0;
552
553 /*
554 * cr0 val on cpu init should be 0x60000010, we enable cpu
555 * cache by default. the orderly way is to enable cache in bios.
556 */
Rusty Russell707d92fa2007-07-17 23:19:08 +1000557 save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
Rusty Russell66aee912007-07-17 23:34:16 +1000558 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559 /* rdx = ?? */
560}
561
Avi Kivity04d2cc72007-09-10 18:10:54 +0300562static void svm_vcpu_reset(struct kvm_vcpu *vcpu)
563{
564 struct vcpu_svm *svm = to_svm(vcpu);
565
566 init_vmcb(svm->vmcb);
Avi Kivity70433382007-11-07 12:57:23 +0200567
568 if (vcpu->vcpu_id != 0) {
569 svm->vmcb->save.rip = 0;
570 svm->vmcb->save.cs.base = svm->vcpu.sipi_vector << 12;
571 svm->vmcb->save.cs.selector = svm->vcpu.sipi_vector << 8;
572 }
Avi Kivity04d2cc72007-09-10 18:10:54 +0300573}
574
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000575static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800576{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400577 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578 struct page *page;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000579 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580
Rusty Russellc16f8622007-07-30 21:12:19 +1000581 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000582 if (!svm) {
583 err = -ENOMEM;
584 goto out;
585 }
586
587 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
588 if (err)
589 goto free_svm;
590
Eddie Dong97222cc2007-09-12 10:58:04 +0300591 if (irqchip_in_kernel(kvm)) {
592 err = kvm_create_lapic(&svm->vcpu);
593 if (err < 0)
594 goto free_svm;
595 }
596
Avi Kivity6aa8b732006-12-10 02:21:36 -0800597 page = alloc_page(GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000598 if (!page) {
599 err = -ENOMEM;
600 goto uninit;
601 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800602
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400603 svm->vmcb = page_address(page);
604 clear_page(svm->vmcb);
605 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
606 svm->asid_generation = 0;
607 memset(svm->db_regs, 0, sizeof(svm->db_regs));
608 init_vmcb(svm->vmcb);
609
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000610 fx_init(&svm->vcpu);
611 svm->vcpu.fpu_active = 1;
612 svm->vcpu.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
613 if (svm->vcpu.vcpu_id == 0)
614 svm->vcpu.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000616 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800617
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000618uninit:
619 kvm_vcpu_uninit(&svm->vcpu);
620free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000621 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000622out:
623 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624}
625
626static void svm_free_vcpu(struct kvm_vcpu *vcpu)
627{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400628 struct vcpu_svm *svm = to_svm(vcpu);
629
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000630 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
631 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000632 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633}
634
Avi Kivity15ad7142007-07-11 18:17:21 +0300635static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400637 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300638 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200639
Avi Kivity0cc50642007-03-25 12:07:27 +0200640 if (unlikely(cpu != vcpu->cpu)) {
641 u64 tsc_this, delta;
642
643 /*
644 * Make sure that the guest sees a monotonically
645 * increasing TSC.
646 */
647 rdtscll(tsc_this);
648 delta = vcpu->host_tsc - tsc_this;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400649 svm->vmcb->control.tsc_offset += delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200650 vcpu->cpu = cpu;
Eddie Donga3d7f852007-09-03 16:15:12 +0300651 kvm_migrate_apic_timer(vcpu);
Avi Kivity0cc50642007-03-25 12:07:27 +0200652 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300653
654 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400655 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656}
657
658static void svm_vcpu_put(struct kvm_vcpu *vcpu)
659{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400660 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300661 int i;
662
663 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400664 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300665
Avi Kivity0cc50642007-03-25 12:07:27 +0200666 rdtscll(vcpu->host_tsc);
Avi Kivity8d379a72007-11-27 15:33:10 +0200667 kvm_put_guest_fpu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668}
669
Avi Kivity774c47f2007-02-12 00:54:47 -0800670static void svm_vcpu_decache(struct kvm_vcpu *vcpu)
671{
672}
673
Avi Kivity6aa8b732006-12-10 02:21:36 -0800674static void svm_cache_regs(struct kvm_vcpu *vcpu)
675{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400676 struct vcpu_svm *svm = to_svm(vcpu);
677
678 vcpu->regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
679 vcpu->regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
680 vcpu->rip = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681}
682
683static void svm_decache_regs(struct kvm_vcpu *vcpu)
684{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400685 struct vcpu_svm *svm = to_svm(vcpu);
686 svm->vmcb->save.rax = vcpu->regs[VCPU_REGS_RAX];
687 svm->vmcb->save.rsp = vcpu->regs[VCPU_REGS_RSP];
688 svm->vmcb->save.rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689}
690
691static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
692{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400693 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694}
695
696static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
697{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400698 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699}
700
701static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
702{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400703 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704
705 switch (seg) {
706 case VCPU_SREG_CS: return &save->cs;
707 case VCPU_SREG_DS: return &save->ds;
708 case VCPU_SREG_ES: return &save->es;
709 case VCPU_SREG_FS: return &save->fs;
710 case VCPU_SREG_GS: return &save->gs;
711 case VCPU_SREG_SS: return &save->ss;
712 case VCPU_SREG_TR: return &save->tr;
713 case VCPU_SREG_LDTR: return &save->ldtr;
714 }
715 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000716 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717}
718
719static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
720{
721 struct vmcb_seg *s = svm_seg(vcpu, seg);
722
723 return s->base;
724}
725
726static void svm_get_segment(struct kvm_vcpu *vcpu,
727 struct kvm_segment *var, int seg)
728{
729 struct vmcb_seg *s = svm_seg(vcpu, seg);
730
731 var->base = s->base;
732 var->limit = s->limit;
733 var->selector = s->selector;
734 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
735 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
736 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
737 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
738 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
739 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
740 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
741 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
742 var->unusable = !var->present;
743}
744
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
746{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400747 struct vcpu_svm *svm = to_svm(vcpu);
748
749 dt->limit = svm->vmcb->save.idtr.limit;
750 dt->base = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751}
752
753static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
754{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400755 struct vcpu_svm *svm = to_svm(vcpu);
756
757 svm->vmcb->save.idtr.limit = dt->limit;
758 svm->vmcb->save.idtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800759}
760
761static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
762{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400763 struct vcpu_svm *svm = to_svm(vcpu);
764
765 dt->limit = svm->vmcb->save.gdtr.limit;
766 dt->base = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767}
768
769static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
770{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400771 struct vcpu_svm *svm = to_svm(vcpu);
772
773 svm->vmcb->save.gdtr.limit = dt->limit;
774 svm->vmcb->save.gdtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775}
776
Anthony Liguori25c4c272007-04-27 09:29:21 +0300777static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -0800778{
779}
780
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
782{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400783 struct vcpu_svm *svm = to_svm(vcpu);
784
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800785#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 if (vcpu->shadow_efer & KVM_EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +1000787 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 vcpu->shadow_efer |= KVM_EFER_LMA;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400789 svm->vmcb->save.efer |= KVM_EFER_LMA | KVM_EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 }
791
Rusty Russell707d92fa2007-07-17 23:19:08 +1000792 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG) ) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 vcpu->shadow_efer &= ~KVM_EFER_LMA;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400794 svm->vmcb->save.efer &= ~(KVM_EFER_LMA | KVM_EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 }
796 }
797#endif
Rusty Russell707d92fa2007-07-17 23:19:08 +1000798 if ((vcpu->cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400799 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Anthony Liguori7807fa62007-04-23 09:17:21 -0500800 vcpu->fpu_active = 1;
801 }
802
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803 vcpu->cr0 = cr0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000804 cr0 |= X86_CR0_PG | X86_CR0_WP;
805 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400806 svm->vmcb->save.cr0 = cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807}
808
809static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
810{
811 vcpu->cr4 = cr4;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400812 to_svm(vcpu)->vmcb->save.cr4 = cr4 | X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813}
814
815static void svm_set_segment(struct kvm_vcpu *vcpu,
816 struct kvm_segment *var, int seg)
817{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400818 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819 struct vmcb_seg *s = svm_seg(vcpu, seg);
820
821 s->base = var->base;
822 s->limit = var->limit;
823 s->selector = var->selector;
824 if (var->unusable)
825 s->attrib = 0;
826 else {
827 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
828 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
829 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
830 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
831 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
832 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
833 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
834 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
835 }
836 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400837 svm->vmcb->save.cpl
838 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 >> SVM_SELECTOR_DPL_SHIFT) & 3;
840
841}
842
843/* FIXME:
844
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400845 svm(vcpu)->vmcb->control.int_ctl &= ~V_TPR_MASK;
846 svm(vcpu)->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847
848*/
849
850static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
851{
852 return -EOPNOTSUPP;
853}
854
Eddie Dong2a8067f2007-08-06 16:29:07 +0300855static int svm_get_irq(struct kvm_vcpu *vcpu)
856{
857 struct vcpu_svm *svm = to_svm(vcpu);
858 u32 exit_int_info = svm->vmcb->control.exit_int_info;
859
860 if (is_external_interrupt(exit_int_info))
861 return exit_int_info & SVM_EVTINJ_VEC_MASK;
862 return -1;
863}
864
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865static void load_host_msrs(struct kvm_vcpu *vcpu)
866{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300867#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400868 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300869#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870}
871
872static void save_host_msrs(struct kvm_vcpu *vcpu)
873{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300874#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400875 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300876#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877}
878
Rusty Russelle756fc62007-07-30 20:07:08 +1000879static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880{
881 if (svm_data->next_asid > svm_data->max_asid) {
882 ++svm_data->asid_generation;
883 svm_data->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400884 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885 }
886
Rusty Russelle756fc62007-07-30 20:07:08 +1000887 svm->vcpu.cpu = svm_data->cpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400888 svm->asid_generation = svm_data->asid_generation;
889 svm->vmcb->control.asid = svm_data->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890}
891
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
893{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400894 return to_svm(vcpu)->db_regs[dr];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895}
896
897static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
898 int *exception)
899{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400900 struct vcpu_svm *svm = to_svm(vcpu);
901
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902 *exception = 0;
903
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400904 if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
905 svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
906 svm->vmcb->save.dr6 |= DR6_BD_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907 *exception = DB_VECTOR;
908 return;
909 }
910
911 switch (dr) {
912 case 0 ... 3:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400913 svm->db_regs[dr] = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 return;
915 case 4 ... 5:
Rusty Russell66aee912007-07-17 23:34:16 +1000916 if (vcpu->cr4 & X86_CR4_DE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 *exception = UD_VECTOR;
918 return;
919 }
920 case 7: {
921 if (value & ~((1ULL << 32) - 1)) {
922 *exception = GP_VECTOR;
923 return;
924 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400925 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 return;
927 }
928 default:
929 printk(KERN_DEBUG "%s: unexpected dr %u\n",
930 __FUNCTION__, dr);
931 *exception = UD_VECTOR;
932 return;
933 }
934}
935
Rusty Russelle756fc62007-07-30 20:07:08 +1000936static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400938 u32 exit_int_info = svm->vmcb->control.exit_int_info;
Rusty Russelle756fc62007-07-30 20:07:08 +1000939 struct kvm *kvm = svm->vcpu.kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 u64 fault_address;
941 u32 error_code;
942 enum emulation_result er;
Avi Kivitye2dec932007-01-05 16:36:54 -0800943 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944
Eddie Dong85f455f2007-07-06 12:20:49 +0300945 if (!irqchip_in_kernel(kvm) &&
946 is_external_interrupt(exit_int_info))
Rusty Russelle756fc62007-07-30 20:07:08 +1000947 push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948
Rusty Russelle756fc62007-07-30 20:07:08 +1000949 mutex_lock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400951 fault_address = svm->vmcb->control.exit_info_2;
952 error_code = svm->vmcb->control.exit_info_1;
Rusty Russelle756fc62007-07-30 20:07:08 +1000953 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -0800954 if (r < 0) {
Rusty Russelle756fc62007-07-30 20:07:08 +1000955 mutex_unlock(&kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -0800956 return r;
957 }
958 if (!r) {
Rusty Russelle756fc62007-07-30 20:07:08 +1000959 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 return 1;
961 }
Rusty Russelle756fc62007-07-30 20:07:08 +1000962 er = emulate_instruction(&svm->vcpu, kvm_run, fault_address,
963 error_code);
964 mutex_unlock(&kvm->lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965
966 switch (er) {
967 case EMULATE_DONE:
968 return 1;
969 case EMULATE_DO_MMIO:
Rusty Russelle756fc62007-07-30 20:07:08 +1000970 ++svm->vcpu.stat.mmio_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 return 0;
972 case EMULATE_FAIL:
Avi Kivity054b1362007-09-12 13:21:09 +0300973 kvm_report_emulation_failure(&svm->vcpu, "pagetable");
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 break;
975 default:
976 BUG();
977 }
978
979 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
980 return 0;
981}
982
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500983static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
984{
985 int er;
986
987 er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0);
988 if (er != EMULATE_DONE)
989 inject_ud(&svm->vcpu);
990
991 return 1;
992}
993
Rusty Russelle756fc62007-07-30 20:07:08 +1000994static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Anthony Liguori7807fa62007-04-23 09:17:21 -0500995{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400996 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Rusty Russelle756fc62007-07-30 20:07:08 +1000997 if (!(svm->vcpu.cr0 & X86_CR0_TS))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400998 svm->vmcb->save.cr0 &= ~X86_CR0_TS;
Rusty Russelle756fc62007-07-30 20:07:08 +1000999 svm->vcpu.fpu_active = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001000
1001 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001002}
1003
Rusty Russelle756fc62007-07-30 20:07:08 +10001004static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001005{
1006 /*
1007 * VMCB is undefined after a SHUTDOWN intercept
1008 * so reinitialize it.
1009 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001010 clear_page(svm->vmcb);
1011 init_vmcb(svm->vmcb);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001012
1013 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1014 return 0;
1015}
1016
Rusty Russelle756fc62007-07-30 20:07:08 +10001017static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001019 u32 io_info = svm->vmcb->control.exit_info_1; //address size bug?
Avi Kivity039576c2007-03-20 12:46:50 +02001020 int size, down, in, string, rep;
1021 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022
Rusty Russelle756fc62007-07-30 20:07:08 +10001023 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001025 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026
Laurent Viviere70669a2007-08-05 10:36:40 +03001027 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1028
1029 if (string) {
1030 if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0) == EMULATE_DO_MMIO)
1031 return 0;
1032 return 1;
1033 }
1034
Avi Kivity039576c2007-03-20 12:46:50 +02001035 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1036 port = io_info >> 16;
1037 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity039576c2007-03-20 12:46:50 +02001038 rep = (io_info & SVM_IOIO_REP_MASK) != 0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001039 down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040
Laurent Vivier3090dd72007-08-05 10:43:32 +03001041 return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042}
1043
Rusty Russelle756fc62007-07-30 20:07:08 +10001044static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045{
1046 return 1;
1047}
1048
Rusty Russelle756fc62007-07-30 20:07:08 +10001049static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001051 svm->next_rip = svm->vmcb->save.rip + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001052 skip_emulated_instruction(&svm->vcpu);
1053 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054}
1055
Rusty Russelle756fc62007-07-30 20:07:08 +10001056static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity02e235b2007-02-19 14:37:47 +02001057{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001058 svm->next_rip = svm->vmcb->save.rip + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001059 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001060 kvm_emulate_hypercall(&svm->vcpu);
1061 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001062}
1063
Rusty Russelle756fc62007-07-30 20:07:08 +10001064static int invalid_op_interception(struct vcpu_svm *svm,
1065 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066{
Rusty Russelle756fc62007-07-30 20:07:08 +10001067 inject_ud(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068 return 1;
1069}
1070
Rusty Russelle756fc62007-07-30 20:07:08 +10001071static int task_switch_interception(struct vcpu_svm *svm,
1072 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073{
Rusty Russellf0242472007-08-01 10:48:02 +10001074 pr_unimpl(&svm->vcpu, "%s: task switch is unsupported\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
1076 return 0;
1077}
1078
Rusty Russelle756fc62007-07-30 20:07:08 +10001079static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001081 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001082 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001083 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084}
1085
Rusty Russelle756fc62007-07-30 20:07:08 +10001086static int emulate_on_interception(struct vcpu_svm *svm,
1087 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088{
Rusty Russelle756fc62007-07-30 20:07:08 +10001089 if (emulate_instruction(&svm->vcpu, NULL, 0, 0) != EMULATE_DONE)
Rusty Russellf0242472007-08-01 10:48:02 +10001090 pr_unimpl(&svm->vcpu, "%s: failed\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091 return 1;
1092}
1093
1094static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
1095{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001096 struct vcpu_svm *svm = to_svm(vcpu);
1097
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099 case MSR_IA32_TIME_STAMP_COUNTER: {
1100 u64 tsc;
1101
1102 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001103 *data = svm->vmcb->control.tsc_offset + tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 break;
1105 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001106 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001107 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08001109#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001111 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 break;
1113 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001114 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115 break;
1116 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001117 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118 break;
1119 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001120 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121 break;
1122#endif
1123 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001124 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 break;
1126 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001127 *data = svm->vmcb->save.sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128 break;
1129 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001130 *data = svm->vmcb->save.sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 break;
1132 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001133 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134 }
1135 return 0;
1136}
1137
Rusty Russelle756fc62007-07-30 20:07:08 +10001138static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139{
Rusty Russelle756fc62007-07-30 20:07:08 +10001140 u32 ecx = svm->vcpu.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 u64 data;
1142
Rusty Russelle756fc62007-07-30 20:07:08 +10001143 if (svm_get_msr(&svm->vcpu, ecx, &data))
1144 svm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 else {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001146 svm->vmcb->save.rax = data & 0xffffffff;
Rusty Russelle756fc62007-07-30 20:07:08 +10001147 svm->vcpu.regs[VCPU_REGS_RDX] = data >> 32;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001148 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001149 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 }
1151 return 1;
1152}
1153
1154static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
1155{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001156 struct vcpu_svm *svm = to_svm(vcpu);
1157
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159 case MSR_IA32_TIME_STAMP_COUNTER: {
1160 u64 tsc;
1161
1162 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001163 svm->vmcb->control.tsc_offset = data - tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164 break;
1165 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001166 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001167 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08001169#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001171 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172 break;
1173 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001174 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175 break;
1176 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001177 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 break;
1179 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001180 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181 break;
1182#endif
1183 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001184 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 break;
1186 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001187 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 break;
1189 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001190 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191 break;
1192 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001193 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194 }
1195 return 0;
1196}
1197
Rusty Russelle756fc62007-07-30 20:07:08 +10001198static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199{
Rusty Russelle756fc62007-07-30 20:07:08 +10001200 u32 ecx = svm->vcpu.regs[VCPU_REGS_RCX];
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001201 u64 data = (svm->vmcb->save.rax & -1u)
Rusty Russelle756fc62007-07-30 20:07:08 +10001202 | ((u64)(svm->vcpu.regs[VCPU_REGS_RDX] & -1u) << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001203 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001204 if (svm_set_msr(&svm->vcpu, ecx, data))
1205 svm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001207 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208 return 1;
1209}
1210
Rusty Russelle756fc62007-07-30 20:07:08 +10001211static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212{
Rusty Russelle756fc62007-07-30 20:07:08 +10001213 if (svm->vmcb->control.exit_info_1)
1214 return wrmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001216 return rdmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217}
1218
Rusty Russelle756fc62007-07-30 20:07:08 +10001219static int interrupt_window_interception(struct vcpu_svm *svm,
Dor Laorc1150d82007-01-05 16:36:24 -08001220 struct kvm_run *kvm_run)
1221{
Eddie Dong85f455f2007-07-06 12:20:49 +03001222 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1223 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08001224 /*
1225 * If the user space waits to inject interrupts, exit as soon as
1226 * possible
1227 */
1228 if (kvm_run->request_interrupt_window &&
Rusty Russelle756fc62007-07-30 20:07:08 +10001229 !svm->vcpu.irq_summary) {
1230 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08001231 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
1232 return 0;
1233 }
1234
1235 return 1;
1236}
1237
Rusty Russelle756fc62007-07-30 20:07:08 +10001238static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001239 struct kvm_run *kvm_run) = {
1240 [SVM_EXIT_READ_CR0] = emulate_on_interception,
1241 [SVM_EXIT_READ_CR3] = emulate_on_interception,
1242 [SVM_EXIT_READ_CR4] = emulate_on_interception,
1243 /* for now: */
1244 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
1245 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
1246 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
1247 [SVM_EXIT_READ_DR0] = emulate_on_interception,
1248 [SVM_EXIT_READ_DR1] = emulate_on_interception,
1249 [SVM_EXIT_READ_DR2] = emulate_on_interception,
1250 [SVM_EXIT_READ_DR3] = emulate_on_interception,
1251 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
1252 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
1253 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
1254 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
1255 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
1256 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001257 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001258 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Anthony Liguori7807fa62007-04-23 09:17:21 -05001259 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260 [SVM_EXIT_INTR] = nop_on_interception,
1261 [SVM_EXIT_NMI] = nop_on_interception,
1262 [SVM_EXIT_SMI] = nop_on_interception,
1263 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08001264 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001265 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
1266 [SVM_EXIT_CPUID] = cpuid_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001267 [SVM_EXIT_INVD] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 [SVM_EXIT_HLT] = halt_interception,
1269 [SVM_EXIT_INVLPG] = emulate_on_interception,
1270 [SVM_EXIT_INVLPGA] = invalid_op_interception,
1271 [SVM_EXIT_IOIO] = io_interception,
1272 [SVM_EXIT_MSR] = msr_interception,
1273 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001274 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275 [SVM_EXIT_VMRUN] = invalid_op_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02001276 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277 [SVM_EXIT_VMLOAD] = invalid_op_interception,
1278 [SVM_EXIT_VMSAVE] = invalid_op_interception,
1279 [SVM_EXIT_STGI] = invalid_op_interception,
1280 [SVM_EXIT_CLGI] = invalid_op_interception,
1281 [SVM_EXIT_SKINIT] = invalid_op_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001282 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01001283 [SVM_EXIT_MONITOR] = invalid_op_interception,
1284 [SVM_EXIT_MWAIT] = invalid_op_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285};
1286
1287
Avi Kivity04d2cc72007-09-10 18:10:54 +03001288static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001290 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001291 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292
Avi Kivity04d2cc72007-09-10 18:10:54 +03001293 kvm_reput_irq(svm);
1294
1295 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
1296 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
1297 kvm_run->fail_entry.hardware_entry_failure_reason
1298 = svm->vmcb->control.exit_code;
1299 return 0;
1300 }
1301
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001302 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR)
1304 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
1305 "exit_code 0x%x\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001306 __FUNCTION__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307 exit_code);
1308
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02001309 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310 || svm_exit_handlers[exit_code] == 0) {
1311 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03001312 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313 return 0;
1314 }
1315
Rusty Russelle756fc62007-07-30 20:07:08 +10001316 return svm_exit_handlers[exit_code](svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317}
1318
1319static void reload_tss(struct kvm_vcpu *vcpu)
1320{
1321 int cpu = raw_smp_processor_id();
1322
1323 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
1324 svm_data->tss_desc->type = 9; //available 32/64-bit TSS
1325 load_TR_desc();
1326}
1327
Rusty Russelle756fc62007-07-30 20:07:08 +10001328static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329{
1330 int cpu = raw_smp_processor_id();
1331
1332 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
1333
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001334 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Rusty Russelle756fc62007-07-30 20:07:08 +10001335 if (svm->vcpu.cpu != cpu ||
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001336 svm->asid_generation != svm_data->asid_generation)
Rusty Russelle756fc62007-07-30 20:07:08 +10001337 new_asid(svm, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338}
1339
1340
Eddie Dong85f455f2007-07-06 12:20:49 +03001341static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001342{
1343 struct vmcb_control_area *control;
1344
Rusty Russelle756fc62007-07-30 20:07:08 +10001345 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03001346 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 control->int_ctl &= ~V_INTR_PRIO_MASK;
1348 control->int_ctl |= V_IRQ_MASK |
1349 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1350}
1351
Eddie Dong2a8067f2007-08-06 16:29:07 +03001352static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
1353{
1354 struct vcpu_svm *svm = to_svm(vcpu);
1355
1356 svm_inject_irq(svm, irq);
1357}
1358
Avi Kivity04d2cc72007-09-10 18:10:54 +03001359static void svm_intr_assist(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001361 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001362 struct vmcb *vmcb = svm->vmcb;
1363 int intr_vector = -1;
1364
Eddie Dong1b9778d2007-09-03 16:56:58 +03001365 kvm_inject_pending_timer_irqs(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001366 if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
1367 ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
1368 intr_vector = vmcb->control.exit_int_info &
1369 SVM_EVTINJ_VEC_MASK;
1370 vmcb->control.exit_int_info = 0;
1371 svm_inject_irq(svm, intr_vector);
1372 return;
1373 }
1374
1375 if (vmcb->control.int_ctl & V_IRQ_MASK)
1376 return;
1377
Eddie Dong1b9778d2007-09-03 16:56:58 +03001378 if (!kvm_cpu_has_interrupt(vcpu))
Eddie Dong85f455f2007-07-06 12:20:49 +03001379 return;
1380
1381 if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
1382 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
1383 (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
1384 /* unable to deliver irq, set pending irq */
1385 vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
1386 svm_inject_irq(svm, 0x0);
1387 return;
1388 }
1389 /* Okay, we can deliver the interrupt: grab it and update PIC state. */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001390 intr_vector = kvm_cpu_get_interrupt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001391 svm_inject_irq(svm, intr_vector);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001392 kvm_timer_intr_post(vcpu, intr_vector);
Eddie Dong85f455f2007-07-06 12:20:49 +03001393}
1394
1395static void kvm_reput_irq(struct vcpu_svm *svm)
1396{
Rusty Russelle756fc62007-07-30 20:07:08 +10001397 struct vmcb_control_area *control = &svm->vmcb->control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398
Eddie Dong7017fc32007-07-18 11:34:57 +03001399 if ((control->int_ctl & V_IRQ_MASK)
1400 && !irqchip_in_kernel(svm->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401 control->int_ctl &= ~V_IRQ_MASK;
Rusty Russelle756fc62007-07-30 20:07:08 +10001402 push_irq(&svm->vcpu, control->int_vector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403 }
Dor Laorc1150d82007-01-05 16:36:24 -08001404
Rusty Russelle756fc62007-07-30 20:07:08 +10001405 svm->vcpu.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001406 !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
1407}
1408
Eddie Dong85f455f2007-07-06 12:20:49 +03001409static void svm_do_inject_vector(struct vcpu_svm *svm)
1410{
1411 struct kvm_vcpu *vcpu = &svm->vcpu;
1412 int word_index = __ffs(vcpu->irq_summary);
1413 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1414 int irq = word_index * BITS_PER_LONG + bit_index;
1415
1416 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1417 if (!vcpu->irq_pending[word_index])
1418 clear_bit(word_index, &vcpu->irq_summary);
1419 svm_inject_irq(svm, irq);
1420}
1421
Avi Kivity04d2cc72007-09-10 18:10:54 +03001422static void do_interrupt_requests(struct kvm_vcpu *vcpu,
Dor Laorc1150d82007-01-05 16:36:24 -08001423 struct kvm_run *kvm_run)
1424{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001425 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001426 struct vmcb_control_area *control = &svm->vmcb->control;
Dor Laorc1150d82007-01-05 16:36:24 -08001427
Rusty Russelle756fc62007-07-30 20:07:08 +10001428 svm->vcpu.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001429 (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001430 (svm->vmcb->save.rflags & X86_EFLAGS_IF));
Dor Laorc1150d82007-01-05 16:36:24 -08001431
Rusty Russelle756fc62007-07-30 20:07:08 +10001432 if (svm->vcpu.interrupt_window_open && svm->vcpu.irq_summary)
Dor Laorc1150d82007-01-05 16:36:24 -08001433 /*
1434 * If interrupts enabled, and not blocked by sti or mov ss. Good.
1435 */
Eddie Dong85f455f2007-07-06 12:20:49 +03001436 svm_do_inject_vector(svm);
Dor Laorc1150d82007-01-05 16:36:24 -08001437
1438 /*
1439 * Interrupts blocked. Wait for unblock.
1440 */
Rusty Russelle756fc62007-07-30 20:07:08 +10001441 if (!svm->vcpu.interrupt_window_open &&
1442 (svm->vcpu.irq_summary || kvm_run->request_interrupt_window)) {
Dor Laorc1150d82007-01-05 16:36:24 -08001443 control->intercept |= 1ULL << INTERCEPT_VINTR;
1444 } else
1445 control->intercept &= ~(1ULL << INTERCEPT_VINTR);
1446}
1447
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448static void save_db_regs(unsigned long *db_regs)
1449{
Avi Kivity5aff4582006-12-13 00:33:45 -08001450 asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
1451 asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
1452 asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
1453 asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454}
1455
1456static void load_db_regs(unsigned long *db_regs)
1457{
Avi Kivity5aff4582006-12-13 00:33:45 -08001458 asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
1459 asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
1460 asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
1461 asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462}
1463
Avi Kivityd9e368d2007-06-07 19:18:30 +03001464static void svm_flush_tlb(struct kvm_vcpu *vcpu)
1465{
1466 force_new_asid(vcpu);
1467}
1468
Avi Kivity04d2cc72007-09-10 18:10:54 +03001469static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
1470{
1471}
1472
1473static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001475 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476 u16 fs_selector;
1477 u16 gs_selector;
1478 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03001479
Rusty Russelle756fc62007-07-30 20:07:08 +10001480 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481
1482 save_host_msrs(vcpu);
1483 fs_selector = read_fs();
1484 gs_selector = read_gs();
1485 ldt_selector = read_ldt();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001486 svm->host_cr2 = kvm_read_cr2();
1487 svm->host_dr6 = read_dr6();
1488 svm->host_dr7 = read_dr7();
1489 svm->vmcb->save.cr2 = vcpu->cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001491 if (svm->vmcb->save.dr7 & 0xff) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492 write_dr7(0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001493 save_db_regs(svm->host_db_regs);
1494 load_db_regs(svm->db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495 }
Avi Kivity36241b82006-12-22 01:05:20 -08001496
Avi Kivity04d2cc72007-09-10 18:10:54 +03001497 clgi();
1498
1499 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08001500
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 asm volatile (
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001502#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 "push %%rbx; push %%rcx; push %%rdx;"
1504 "push %%rsi; push %%rdi; push %%rbp;"
1505 "push %%r8; push %%r9; push %%r10; push %%r11;"
1506 "push %%r12; push %%r13; push %%r14; push %%r15;"
1507#else
1508 "push %%ebx; push %%ecx; push %%edx;"
1509 "push %%esi; push %%edi; push %%ebp;"
1510#endif
1511
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001512#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001513 "mov %c[rbx](%[svm]), %%rbx \n\t"
1514 "mov %c[rcx](%[svm]), %%rcx \n\t"
1515 "mov %c[rdx](%[svm]), %%rdx \n\t"
1516 "mov %c[rsi](%[svm]), %%rsi \n\t"
1517 "mov %c[rdi](%[svm]), %%rdi \n\t"
1518 "mov %c[rbp](%[svm]), %%rbp \n\t"
1519 "mov %c[r8](%[svm]), %%r8 \n\t"
1520 "mov %c[r9](%[svm]), %%r9 \n\t"
1521 "mov %c[r10](%[svm]), %%r10 \n\t"
1522 "mov %c[r11](%[svm]), %%r11 \n\t"
1523 "mov %c[r12](%[svm]), %%r12 \n\t"
1524 "mov %c[r13](%[svm]), %%r13 \n\t"
1525 "mov %c[r14](%[svm]), %%r14 \n\t"
1526 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001528 "mov %c[rbx](%[svm]), %%ebx \n\t"
1529 "mov %c[rcx](%[svm]), %%ecx \n\t"
1530 "mov %c[rdx](%[svm]), %%edx \n\t"
1531 "mov %c[rsi](%[svm]), %%esi \n\t"
1532 "mov %c[rdi](%[svm]), %%edi \n\t"
1533 "mov %c[rbp](%[svm]), %%ebp \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534#endif
1535
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001536#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 /* Enter guest mode */
1538 "push %%rax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001539 "mov %c[vmcb](%[svm]), %%rax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540 SVM_VMLOAD "\n\t"
1541 SVM_VMRUN "\n\t"
1542 SVM_VMSAVE "\n\t"
1543 "pop %%rax \n\t"
1544#else
1545 /* Enter guest mode */
1546 "push %%eax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001547 "mov %c[vmcb](%[svm]), %%eax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548 SVM_VMLOAD "\n\t"
1549 SVM_VMRUN "\n\t"
1550 SVM_VMSAVE "\n\t"
1551 "pop %%eax \n\t"
1552#endif
1553
1554 /* Save guest registers, load host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001555#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001556 "mov %%rbx, %c[rbx](%[svm]) \n\t"
1557 "mov %%rcx, %c[rcx](%[svm]) \n\t"
1558 "mov %%rdx, %c[rdx](%[svm]) \n\t"
1559 "mov %%rsi, %c[rsi](%[svm]) \n\t"
1560 "mov %%rdi, %c[rdi](%[svm]) \n\t"
1561 "mov %%rbp, %c[rbp](%[svm]) \n\t"
1562 "mov %%r8, %c[r8](%[svm]) \n\t"
1563 "mov %%r9, %c[r9](%[svm]) \n\t"
1564 "mov %%r10, %c[r10](%[svm]) \n\t"
1565 "mov %%r11, %c[r11](%[svm]) \n\t"
1566 "mov %%r12, %c[r12](%[svm]) \n\t"
1567 "mov %%r13, %c[r13](%[svm]) \n\t"
1568 "mov %%r14, %c[r14](%[svm]) \n\t"
1569 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570
1571 "pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
1572 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
1573 "pop %%rbp; pop %%rdi; pop %%rsi;"
1574 "pop %%rdx; pop %%rcx; pop %%rbx; \n\t"
1575#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001576 "mov %%ebx, %c[rbx](%[svm]) \n\t"
1577 "mov %%ecx, %c[rcx](%[svm]) \n\t"
1578 "mov %%edx, %c[rdx](%[svm]) \n\t"
1579 "mov %%esi, %c[rsi](%[svm]) \n\t"
1580 "mov %%edi, %c[rdi](%[svm]) \n\t"
1581 "mov %%ebp, %c[rbp](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001582
1583 "pop %%ebp; pop %%edi; pop %%esi;"
1584 "pop %%edx; pop %%ecx; pop %%ebx; \n\t"
1585#endif
1586 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001587 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001589 [rbx]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RBX])),
1590 [rcx]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RCX])),
1591 [rdx]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RDX])),
1592 [rsi]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RSI])),
1593 [rdi]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RDI])),
1594 [rbp]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001595#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001596 ,[r8 ]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R8])),
1597 [r9 ]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R9 ])),
1598 [r10]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R10])),
1599 [r11]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R11])),
1600 [r12]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R12])),
1601 [r13]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R13])),
1602 [r14]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R14])),
1603 [r15]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604#endif
1605 : "cc", "memory" );
1606
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001607 if ((svm->vmcb->save.dr7 & 0xff))
1608 load_db_regs(svm->host_db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001610 vcpu->cr2 = svm->vmcb->save.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001612 write_dr6(svm->host_dr6);
1613 write_dr7(svm->host_dr7);
1614 kvm_write_cr2(svm->host_cr2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615
1616 load_fs(fs_selector);
1617 load_gs(gs_selector);
1618 load_ldt(ldt_selector);
1619 load_host_msrs(vcpu);
1620
1621 reload_tss(vcpu);
1622
Avi Kivity56ba47d2007-11-07 17:14:18 +02001623 local_irq_disable();
1624
1625 stgi();
1626
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001627 svm->next_rip = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628}
1629
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
1631{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001632 struct vcpu_svm *svm = to_svm(vcpu);
1633
1634 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635 force_new_asid(vcpu);
Anthony Liguori7807fa62007-04-23 09:17:21 -05001636
1637 if (vcpu->fpu_active) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001638 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
1639 svm->vmcb->save.cr0 |= X86_CR0_TS;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001640 vcpu->fpu_active = 0;
1641 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642}
1643
1644static void svm_inject_page_fault(struct kvm_vcpu *vcpu,
1645 unsigned long addr,
1646 uint32_t err_code)
1647{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001648 struct vcpu_svm *svm = to_svm(vcpu);
1649 uint32_t exit_int_info = svm->vmcb->control.exit_int_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650
Avi Kivity1165f5f2007-04-19 17:27:43 +03001651 ++vcpu->stat.pf_guest;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652
1653 if (is_page_fault(exit_int_info)) {
1654
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001655 svm->vmcb->control.event_inj_err = 0;
1656 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
1657 SVM_EVTINJ_VALID_ERR |
1658 SVM_EVTINJ_TYPE_EXEPT |
1659 DF_VECTOR;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660 return;
1661 }
1662 vcpu->cr2 = addr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001663 svm->vmcb->save.cr2 = addr;
1664 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
1665 SVM_EVTINJ_VALID_ERR |
1666 SVM_EVTINJ_TYPE_EXEPT |
1667 PF_VECTOR;
1668 svm->vmcb->control.event_inj_err = err_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669}
1670
1671
1672static int is_disabled(void)
1673{
Joerg Roedel6031a612007-06-22 12:29:50 +03001674 u64 vm_cr;
1675
1676 rdmsrl(MSR_VM_CR, vm_cr);
1677 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
1678 return 1;
1679
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680 return 0;
1681}
1682
Ingo Molnar102d8322007-02-19 14:37:47 +02001683static void
1684svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1685{
1686 /*
1687 * Patch in the VMMCALL instruction:
1688 */
1689 hypercall[0] = 0x0f;
1690 hypercall[1] = 0x01;
1691 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02001692}
1693
Yang, Sheng002c7f72007-07-31 14:23:01 +03001694static void svm_check_processor_compat(void *rtn)
1695{
1696 *(int *)rtn = 0;
1697}
1698
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001699static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700 .cpu_has_kvm_support = has_svm,
1701 .disabled_by_bios = is_disabled,
1702 .hardware_setup = svm_hardware_setup,
1703 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001704 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705 .hardware_enable = svm_hardware_enable,
1706 .hardware_disable = svm_hardware_disable,
1707
1708 .vcpu_create = svm_create_vcpu,
1709 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001710 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711
Avi Kivity04d2cc72007-09-10 18:10:54 +03001712 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713 .vcpu_load = svm_vcpu_load,
1714 .vcpu_put = svm_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08001715 .vcpu_decache = svm_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001716
1717 .set_guest_debug = svm_guest_debug,
1718 .get_msr = svm_get_msr,
1719 .set_msr = svm_set_msr,
1720 .get_segment_base = svm_get_segment_base,
1721 .get_segment = svm_get_segment,
1722 .set_segment = svm_set_segment,
Rusty Russell1747fb72007-09-06 01:21:32 +10001723 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03001724 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726 .set_cr3 = svm_set_cr3,
1727 .set_cr4 = svm_set_cr4,
1728 .set_efer = svm_set_efer,
1729 .get_idt = svm_get_idt,
1730 .set_idt = svm_set_idt,
1731 .get_gdt = svm_get_gdt,
1732 .set_gdt = svm_set_gdt,
1733 .get_dr = svm_get_dr,
1734 .set_dr = svm_set_dr,
1735 .cache_regs = svm_cache_regs,
1736 .decache_regs = svm_decache_regs,
1737 .get_rflags = svm_get_rflags,
1738 .set_rflags = svm_set_rflags,
1739
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740 .tlb_flush = svm_flush_tlb,
1741 .inject_page_fault = svm_inject_page_fault,
1742
1743 .inject_gp = svm_inject_gp,
1744
1745 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001746 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02001748 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03001749 .get_irq = svm_get_irq,
1750 .set_irq = svm_set_irq,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001751 .inject_pending_irq = svm_intr_assist,
1752 .inject_pending_vectors = do_interrupt_requests,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001753};
1754
1755static int __init svm_init(void)
1756{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001757 return kvm_init_x86(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10001758 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759}
1760
1761static void __exit svm_exit(void)
1762{
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001763 kvm_exit_x86();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001764}
1765
1766module_init(svm_init)
1767module_exit(svm_exit)