blob: c75c6b65b6514236723117f5b5ad0b581957c197 [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 */
Zhang Xiantao34c16ee2007-10-20 15:34:38 +080016#include "x86.h"
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
Joerg Roedel80b77062007-03-30 17:02:14 +030045#define SVM_FEATURE_NPT (1 << 0)
46#define SVM_FEATURE_LBRV (1 << 1)
47#define SVM_DEATURE_SVML (1 << 2)
48
Avi Kivity04d2cc72007-09-10 18:10:54 +030049static void kvm_reput_irq(struct vcpu_svm *svm);
50
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040051static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
52{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100053 return container_of(vcpu, struct vcpu_svm, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040054}
55
Avi Kivity6aa8b732006-12-10 02:21:36 -080056unsigned long iopm_base;
57unsigned long msrpm_base;
58
59struct kvm_ldttss_desc {
60 u16 limit0;
61 u16 base0;
62 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
63 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
64 u32 base3;
65 u32 zero1;
66} __attribute__((packed));
67
68struct svm_cpu_data {
69 int cpu;
70
Avi Kivity5008fdf2007-04-02 13:05:50 +030071 u64 asid_generation;
72 u32 max_asid;
73 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -080074 struct kvm_ldttss_desc *tss_desc;
75
76 struct page *save_area;
77};
78
79static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +030080static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -080081
82struct svm_init_data {
83 int cpu;
84 int r;
85};
86
87static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
88
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020089#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -080090#define MSRS_RANGE_SIZE 2048
91#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
92
93#define MAX_INST_SIZE 15
94
Joerg Roedel80b77062007-03-30 17:02:14 +030095static inline u32 svm_has(u32 feat)
96{
97 return svm_features & feat;
98}
99
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100static inline u8 pop_irq(struct kvm_vcpu *vcpu)
101{
102 int word_index = __ffs(vcpu->irq_summary);
103 int bit_index = __ffs(vcpu->irq_pending[word_index]);
104 int irq = word_index * BITS_PER_LONG + bit_index;
105
106 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
107 if (!vcpu->irq_pending[word_index])
108 clear_bit(word_index, &vcpu->irq_summary);
109 return irq;
110}
111
112static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq)
113{
114 set_bit(irq, vcpu->irq_pending);
115 set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
116}
117
118static inline void clgi(void)
119{
120 asm volatile (SVM_CLGI);
121}
122
123static inline void stgi(void)
124{
125 asm volatile (SVM_STGI);
126}
127
128static inline void invlpga(unsigned long addr, u32 asid)
129{
130 asm volatile (SVM_INVLPGA :: "a"(addr), "c"(asid));
131}
132
133static inline unsigned long kvm_read_cr2(void)
134{
135 unsigned long cr2;
136
137 asm volatile ("mov %%cr2, %0" : "=r" (cr2));
138 return cr2;
139}
140
141static inline void kvm_write_cr2(unsigned long val)
142{
143 asm volatile ("mov %0, %%cr2" :: "r" (val));
144}
145
146static inline unsigned long read_dr6(void)
147{
148 unsigned long dr6;
149
150 asm volatile ("mov %%dr6, %0" : "=r" (dr6));
151 return dr6;
152}
153
154static inline void write_dr6(unsigned long val)
155{
156 asm volatile ("mov %0, %%dr6" :: "r" (val));
157}
158
159static inline unsigned long read_dr7(void)
160{
161 unsigned long dr7;
162
163 asm volatile ("mov %%dr7, %0" : "=r" (dr7));
164 return dr7;
165}
166
167static inline void write_dr7(unsigned long val)
168{
169 asm volatile ("mov %0, %%dr7" :: "r" (val));
170}
171
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172static inline void force_new_asid(struct kvm_vcpu *vcpu)
173{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400174 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175}
176
177static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
178{
179 force_new_asid(vcpu);
180}
181
182static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
183{
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600184 if (!(efer & EFER_LMA))
185 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400187 to_svm(vcpu)->vmcb->save.efer = efer | MSR_EFER_SVME_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188 vcpu->shadow_efer = efer;
189}
190
191static void svm_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
192{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400193 struct vcpu_svm *svm = to_svm(vcpu);
194
195 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196 SVM_EVTINJ_VALID_ERR |
197 SVM_EVTINJ_TYPE_EXEPT |
198 GP_VECTOR;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400199 svm->vmcb->control.event_inj_err = error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200}
201
202static void inject_ud(struct kvm_vcpu *vcpu)
203{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400204 to_svm(vcpu)->vmcb->control.event_inj = SVM_EVTINJ_VALID |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 SVM_EVTINJ_TYPE_EXEPT |
206 UD_VECTOR;
207}
208
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209static int is_page_fault(uint32_t info)
210{
211 info &= SVM_EVTINJ_VEC_MASK | SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
212 return info == (PF_VECTOR | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT);
213}
214
215static int is_external_interrupt(u32 info)
216{
217 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
218 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
219}
220
221static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
222{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400223 struct vcpu_svm *svm = to_svm(vcpu);
224
225 if (!svm->next_rip) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226 printk(KERN_DEBUG "%s: NOP\n", __FUNCTION__);
227 return;
228 }
Mike Dayd77c26f2007-10-08 09:02:08 -0400229 if (svm->next_rip - svm->vmcb->save.rip > MAX_INST_SIZE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230 printk(KERN_ERR "%s: ip 0x%llx next 0x%llx\n",
231 __FUNCTION__,
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400232 svm->vmcb->save.rip,
233 svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400235 vcpu->rip = svm->vmcb->save.rip = svm->next_rip;
236 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -0800237
238 vcpu->interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239}
240
241static int has_svm(void)
242{
243 uint32_t eax, ebx, ecx, edx;
244
Avi Kivity1e885462006-12-29 16:49:34 -0800245 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246 printk(KERN_INFO "has_svm: not amd\n");
247 return 0;
248 }
249
250 cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
251 if (eax < SVM_CPUID_FUNC) {
252 printk(KERN_INFO "has_svm: can't execute cpuid_8000000a\n");
253 return 0;
254 }
255
256 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
257 if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) {
258 printk(KERN_DEBUG "has_svm: svm not available\n");
259 return 0;
260 }
261 return 1;
262}
263
264static void svm_hardware_disable(void *garbage)
265{
266 struct svm_cpu_data *svm_data
267 = per_cpu(svm_data, raw_smp_processor_id());
268
269 if (svm_data) {
270 uint64_t efer;
271
272 wrmsrl(MSR_VM_HSAVE_PA, 0);
273 rdmsrl(MSR_EFER, efer);
274 wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK);
Al Viro8b6d44c2007-02-09 16:38:40 +0000275 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276 __free_page(svm_data->save_area);
277 kfree(svm_data);
278 }
279}
280
281static void svm_hardware_enable(void *garbage)
282{
283
284 struct svm_cpu_data *svm_data;
285 uint64_t efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800286#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287 struct desc_ptr gdt_descr;
288#else
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100289 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290#endif
291 struct desc_struct *gdt;
292 int me = raw_smp_processor_id();
293
294 if (!has_svm()) {
295 printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
296 return;
297 }
298 svm_data = per_cpu(svm_data, me);
299
300 if (!svm_data) {
301 printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
302 me);
303 return;
304 }
305
306 svm_data->asid_generation = 1;
307 svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
308 svm_data->next_asid = svm_data->max_asid + 1;
Joerg Roedel80b77062007-03-30 17:02:14 +0300309 svm_features = cpuid_edx(SVM_CPUID_FUNC);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310
Mike Dayd77c26f2007-10-08 09:02:08 -0400311 asm volatile ("sgdt %0" : "=m"(gdt_descr));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312 gdt = (struct desc_struct *)gdt_descr.address;
313 svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
314
315 rdmsrl(MSR_EFER, efer);
316 wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK);
317
318 wrmsrl(MSR_VM_HSAVE_PA,
319 page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
320}
321
322static int svm_cpu_init(int cpu)
323{
324 struct svm_cpu_data *svm_data;
325 int r;
326
327 svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
328 if (!svm_data)
329 return -ENOMEM;
330 svm_data->cpu = cpu;
331 svm_data->save_area = alloc_page(GFP_KERNEL);
332 r = -ENOMEM;
333 if (!svm_data->save_area)
334 goto err_1;
335
336 per_cpu(svm_data, cpu) = svm_data;
337
338 return 0;
339
340err_1:
341 kfree(svm_data);
342 return r;
343
344}
345
Rusty Russellbfc733a2007-07-31 20:42:42 +1000346static void set_msr_interception(u32 *msrpm, unsigned msr,
347 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348{
349 int i;
350
351 for (i = 0; i < NUM_MSR_MAPS; i++) {
352 if (msr >= msrpm_ranges[i] &&
353 msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
354 u32 msr_offset = (i * MSRS_IN_RANGE + msr -
355 msrpm_ranges[i]) * 2;
356
357 u32 *base = msrpm + (msr_offset / 32);
358 u32 msr_shift = msr_offset % 32;
359 u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
360 *base = (*base & ~(0x3 << msr_shift)) |
361 (mask << msr_shift);
Rusty Russellbfc733a2007-07-31 20:42:42 +1000362 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800363 }
364 }
Rusty Russellbfc733a2007-07-31 20:42:42 +1000365 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366}
367
368static __init int svm_hardware_setup(void)
369{
370 int cpu;
371 struct page *iopm_pages;
372 struct page *msrpm_pages;
Anthony Liguoric8681332007-04-30 09:48:11 +0300373 void *iopm_va, *msrpm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374 int r;
375
Avi Kivity6aa8b732006-12-10 02:21:36 -0800376 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
377
378 if (!iopm_pages)
379 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300380
381 iopm_va = page_address(iopm_pages);
382 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
383 clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
385
386
387 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
388
389 r = -ENOMEM;
390 if (!msrpm_pages)
391 goto err_1;
392
393 msrpm_va = page_address(msrpm_pages);
394 memset(msrpm_va, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
395 msrpm_base = page_to_pfn(msrpm_pages) << PAGE_SHIFT;
396
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800397#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398 set_msr_interception(msrpm_va, MSR_GS_BASE, 1, 1);
399 set_msr_interception(msrpm_va, MSR_FS_BASE, 1, 1);
400 set_msr_interception(msrpm_va, MSR_KERNEL_GS_BASE, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401 set_msr_interception(msrpm_va, MSR_LSTAR, 1, 1);
402 set_msr_interception(msrpm_va, MSR_CSTAR, 1, 1);
403 set_msr_interception(msrpm_va, MSR_SYSCALL_MASK, 1, 1);
404#endif
Avi Kivity0e859ca2006-12-22 01:05:08 -0800405 set_msr_interception(msrpm_va, MSR_K6_STAR, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_CS, 1, 1);
407 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_ESP, 1, 1);
408 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_EIP, 1, 1);
409
410 for_each_online_cpu(cpu) {
411 r = svm_cpu_init(cpu);
412 if (r)
413 goto err_2;
414 }
415 return 0;
416
417err_2:
418 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
419 msrpm_base = 0;
420err_1:
421 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
422 iopm_base = 0;
423 return r;
424}
425
426static __exit void svm_hardware_unsetup(void)
427{
428 __free_pages(pfn_to_page(msrpm_base >> PAGE_SHIFT), MSRPM_ALLOC_ORDER);
429 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
430 iopm_base = msrpm_base = 0;
431}
432
433static void init_seg(struct vmcb_seg *seg)
434{
435 seg->selector = 0;
436 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
437 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
438 seg->limit = 0xffff;
439 seg->base = 0;
440}
441
442static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
443{
444 seg->selector = 0;
445 seg->attrib = SVM_SELECTOR_P_MASK | type;
446 seg->limit = 0xffff;
447 seg->base = 0;
448}
449
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450static void init_vmcb(struct vmcb *vmcb)
451{
452 struct vmcb_control_area *control = &vmcb->control;
453 struct vmcb_save_area *save = &vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454
455 control->intercept_cr_read = INTERCEPT_CR0_MASK |
456 INTERCEPT_CR3_MASK |
457 INTERCEPT_CR4_MASK;
458
459 control->intercept_cr_write = INTERCEPT_CR0_MASK |
460 INTERCEPT_CR3_MASK |
461 INTERCEPT_CR4_MASK;
462
463 control->intercept_dr_read = INTERCEPT_DR0_MASK |
464 INTERCEPT_DR1_MASK |
465 INTERCEPT_DR2_MASK |
466 INTERCEPT_DR3_MASK;
467
468 control->intercept_dr_write = INTERCEPT_DR0_MASK |
469 INTERCEPT_DR1_MASK |
470 INTERCEPT_DR2_MASK |
471 INTERCEPT_DR3_MASK |
472 INTERCEPT_DR5_MASK |
473 INTERCEPT_DR7_MASK;
474
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500475 control->intercept_exceptions = (1 << PF_VECTOR) |
476 (1 << UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477
478
479 control->intercept = (1ULL << INTERCEPT_INTR) |
480 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200481 (1ULL << INTERCEPT_SMI) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 /*
483 * selective cr0 intercept bug?
484 * 0: 0f 22 d8 mov %eax,%cr3
485 * 3: 0f 20 c0 mov %cr0,%eax
486 * 6: 0d 00 00 00 80 or $0x80000000,%eax
487 * b: 0f 22 c0 mov %eax,%cr0
488 * set cr3 ->interception
489 * get cr0 ->interception
490 * set cr0 -> no interception
491 */
492 /* (1ULL << INTERCEPT_SELECTIVE_CR0) | */
493 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200494 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495 (1ULL << INTERCEPT_HLT) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 (1ULL << INTERCEPT_INVLPGA) |
497 (1ULL << INTERCEPT_IOIO_PROT) |
498 (1ULL << INTERCEPT_MSR_PROT) |
499 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800500 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501 (1ULL << INTERCEPT_VMRUN) |
502 (1ULL << INTERCEPT_VMMCALL) |
503 (1ULL << INTERCEPT_VMLOAD) |
504 (1ULL << INTERCEPT_VMSAVE) |
505 (1ULL << INTERCEPT_STGI) |
506 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100507 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200508 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100509 (1ULL << INTERCEPT_MONITOR) |
510 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511
512 control->iopm_base_pa = iopm_base;
513 control->msrpm_base_pa = msrpm_base;
Avi Kivity0cc50642007-03-25 12:07:27 +0200514 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515 control->int_ctl = V_INTR_MASKING_MASK;
516
517 init_seg(&save->es);
518 init_seg(&save->ss);
519 init_seg(&save->ds);
520 init_seg(&save->fs);
521 init_seg(&save->gs);
522
523 save->cs.selector = 0xf000;
524 /* Executable/Readable Code Segment */
525 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
526 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
527 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800528 /*
529 * cs.base should really be 0xffff0000, but vmx can't handle that, so
530 * be consistent with it.
531 *
532 * Replace when we have real mode working for vmx.
533 */
534 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800535
536 save->gdtr.limit = 0xffff;
537 save->idtr.limit = 0xffff;
538
539 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
540 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
541
542 save->efer = MSR_EFER_SVME_MASK;
Mike Dayd77c26f2007-10-08 09:02:08 -0400543 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544 save->dr7 = 0x400;
545 save->rflags = 2;
546 save->rip = 0x0000fff0;
547
548 /*
549 * cr0 val on cpu init should be 0x60000010, we enable cpu
550 * cache by default. the orderly way is to enable cache in bios.
551 */
Rusty Russell707d92fa2007-07-17 23:19:08 +1000552 save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
Rusty Russell66aee912007-07-17 23:34:16 +1000553 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800554 /* rdx = ?? */
555}
556
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200557static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300558{
559 struct vcpu_svm *svm = to_svm(vcpu);
560
561 init_vmcb(svm->vmcb);
Avi Kivity70433382007-11-07 12:57:23 +0200562
563 if (vcpu->vcpu_id != 0) {
564 svm->vmcb->save.rip = 0;
565 svm->vmcb->save.cs.base = svm->vcpu.sipi_vector << 12;
566 svm->vmcb->save.cs.selector = svm->vcpu.sipi_vector << 8;
567 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200568
569 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300570}
571
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000572static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400574 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 struct page *page;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000576 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577
Rusty Russellc16f8622007-07-30 21:12:19 +1000578 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000579 if (!svm) {
580 err = -ENOMEM;
581 goto out;
582 }
583
584 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
585 if (err)
586 goto free_svm;
587
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588 page = alloc_page(GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000589 if (!page) {
590 err = -ENOMEM;
591 goto uninit;
592 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400594 svm->vmcb = page_address(page);
595 clear_page(svm->vmcb);
596 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
597 svm->asid_generation = 0;
598 memset(svm->db_regs, 0, sizeof(svm->db_regs));
599 init_vmcb(svm->vmcb);
600
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000601 fx_init(&svm->vcpu);
602 svm->vcpu.fpu_active = 1;
603 svm->vcpu.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
604 if (svm->vcpu.vcpu_id == 0)
605 svm->vcpu.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800606
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000607 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800608
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000609uninit:
610 kvm_vcpu_uninit(&svm->vcpu);
611free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000612 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000613out:
614 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615}
616
617static void svm_free_vcpu(struct kvm_vcpu *vcpu)
618{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400619 struct vcpu_svm *svm = to_svm(vcpu);
620
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000621 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
622 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000623 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624}
625
Avi Kivity15ad7142007-07-11 18:17:21 +0300626static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400628 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300629 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200630
Avi Kivity0cc50642007-03-25 12:07:27 +0200631 if (unlikely(cpu != vcpu->cpu)) {
632 u64 tsc_this, delta;
633
634 /*
635 * Make sure that the guest sees a monotonically
636 * increasing TSC.
637 */
638 rdtscll(tsc_this);
639 delta = vcpu->host_tsc - tsc_this;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400640 svm->vmcb->control.tsc_offset += delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200641 vcpu->cpu = cpu;
Eddie Donga3d7f852007-09-03 16:15:12 +0300642 kvm_migrate_apic_timer(vcpu);
Avi Kivity0cc50642007-03-25 12:07:27 +0200643 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300644
645 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400646 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647}
648
649static void svm_vcpu_put(struct kvm_vcpu *vcpu)
650{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400651 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300652 int i;
653
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200654 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300655 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400656 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300657
Avi Kivity0cc50642007-03-25 12:07:27 +0200658 rdtscll(vcpu->host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659}
660
Avi Kivity774c47f2007-02-12 00:54:47 -0800661static void svm_vcpu_decache(struct kvm_vcpu *vcpu)
662{
663}
664
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665static void svm_cache_regs(struct kvm_vcpu *vcpu)
666{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400667 struct vcpu_svm *svm = to_svm(vcpu);
668
669 vcpu->regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
670 vcpu->regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
671 vcpu->rip = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672}
673
674static void svm_decache_regs(struct kvm_vcpu *vcpu)
675{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400676 struct vcpu_svm *svm = to_svm(vcpu);
677 svm->vmcb->save.rax = vcpu->regs[VCPU_REGS_RAX];
678 svm->vmcb->save.rsp = vcpu->regs[VCPU_REGS_RSP];
679 svm->vmcb->save.rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680}
681
682static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
683{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400684 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685}
686
687static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
688{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400689 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690}
691
692static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
693{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400694 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695
696 switch (seg) {
697 case VCPU_SREG_CS: return &save->cs;
698 case VCPU_SREG_DS: return &save->ds;
699 case VCPU_SREG_ES: return &save->es;
700 case VCPU_SREG_FS: return &save->fs;
701 case VCPU_SREG_GS: return &save->gs;
702 case VCPU_SREG_SS: return &save->ss;
703 case VCPU_SREG_TR: return &save->tr;
704 case VCPU_SREG_LDTR: return &save->ldtr;
705 }
706 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000707 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708}
709
710static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
711{
712 struct vmcb_seg *s = svm_seg(vcpu, seg);
713
714 return s->base;
715}
716
717static void svm_get_segment(struct kvm_vcpu *vcpu,
718 struct kvm_segment *var, int seg)
719{
720 struct vmcb_seg *s = svm_seg(vcpu, seg);
721
722 var->base = s->base;
723 var->limit = s->limit;
724 var->selector = s->selector;
725 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
726 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
727 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
728 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
729 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
730 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
731 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
732 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
733 var->unusable = !var->present;
734}
735
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
737{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400738 struct vcpu_svm *svm = to_svm(vcpu);
739
740 dt->limit = svm->vmcb->save.idtr.limit;
741 dt->base = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742}
743
744static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
745{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400746 struct vcpu_svm *svm = to_svm(vcpu);
747
748 svm->vmcb->save.idtr.limit = dt->limit;
749 svm->vmcb->save.idtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750}
751
752static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
753{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400754 struct vcpu_svm *svm = to_svm(vcpu);
755
756 dt->limit = svm->vmcb->save.gdtr.limit;
757 dt->base = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758}
759
760static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
761{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400762 struct vcpu_svm *svm = to_svm(vcpu);
763
764 svm->vmcb->save.gdtr.limit = dt->limit;
765 svm->vmcb->save.gdtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766}
767
Anthony Liguori25c4c272007-04-27 09:29:21 +0300768static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -0800769{
770}
771
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
773{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400774 struct vcpu_svm *svm = to_svm(vcpu);
775
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800776#ifdef CONFIG_X86_64
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600777 if (vcpu->shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +1000778 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600779 vcpu->shadow_efer |= EFER_LMA;
780 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 }
782
Mike Dayd77c26f2007-10-08 09:02:08 -0400783 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600784 vcpu->shadow_efer &= ~EFER_LMA;
785 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 }
787 }
788#endif
Rusty Russell707d92fa2007-07-17 23:19:08 +1000789 if ((vcpu->cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400790 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Anthony Liguori7807fa62007-04-23 09:17:21 -0500791 vcpu->fpu_active = 1;
792 }
793
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 vcpu->cr0 = cr0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000795 cr0 |= X86_CR0_PG | X86_CR0_WP;
796 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400797 svm->vmcb->save.cr0 = cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798}
799
800static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
801{
802 vcpu->cr4 = cr4;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400803 to_svm(vcpu)->vmcb->save.cr4 = cr4 | X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804}
805
806static void svm_set_segment(struct kvm_vcpu *vcpu,
807 struct kvm_segment *var, int seg)
808{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400809 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810 struct vmcb_seg *s = svm_seg(vcpu, seg);
811
812 s->base = var->base;
813 s->limit = var->limit;
814 s->selector = var->selector;
815 if (var->unusable)
816 s->attrib = 0;
817 else {
818 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
819 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
820 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
821 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
822 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
823 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
824 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
825 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
826 }
827 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400828 svm->vmcb->save.cpl
829 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 >> SVM_SELECTOR_DPL_SHIFT) & 3;
831
832}
833
834/* FIXME:
835
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400836 svm(vcpu)->vmcb->control.int_ctl &= ~V_TPR_MASK;
837 svm(vcpu)->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838
839*/
840
841static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
842{
843 return -EOPNOTSUPP;
844}
845
Eddie Dong2a8067f2007-08-06 16:29:07 +0300846static int svm_get_irq(struct kvm_vcpu *vcpu)
847{
848 struct vcpu_svm *svm = to_svm(vcpu);
849 u32 exit_int_info = svm->vmcb->control.exit_int_info;
850
851 if (is_external_interrupt(exit_int_info))
852 return exit_int_info & SVM_EVTINJ_VEC_MASK;
853 return -1;
854}
855
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856static void load_host_msrs(struct kvm_vcpu *vcpu)
857{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300858#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400859 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300860#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861}
862
863static void save_host_msrs(struct kvm_vcpu *vcpu)
864{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300865#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400866 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300867#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868}
869
Rusty Russelle756fc62007-07-30 20:07:08 +1000870static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871{
872 if (svm_data->next_asid > svm_data->max_asid) {
873 ++svm_data->asid_generation;
874 svm_data->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400875 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 }
877
Rusty Russelle756fc62007-07-30 20:07:08 +1000878 svm->vcpu.cpu = svm_data->cpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400879 svm->asid_generation = svm_data->asid_generation;
880 svm->vmcb->control.asid = svm_data->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881}
882
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
884{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400885 return to_svm(vcpu)->db_regs[dr];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886}
887
888static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
889 int *exception)
890{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400891 struct vcpu_svm *svm = to_svm(vcpu);
892
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 *exception = 0;
894
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400895 if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
896 svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
897 svm->vmcb->save.dr6 |= DR6_BD_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 *exception = DB_VECTOR;
899 return;
900 }
901
902 switch (dr) {
903 case 0 ... 3:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400904 svm->db_regs[dr] = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 return;
906 case 4 ... 5:
Rusty Russell66aee912007-07-17 23:34:16 +1000907 if (vcpu->cr4 & X86_CR4_DE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 *exception = UD_VECTOR;
909 return;
910 }
911 case 7: {
912 if (value & ~((1ULL << 32) - 1)) {
913 *exception = GP_VECTOR;
914 return;
915 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400916 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 return;
918 }
919 default:
920 printk(KERN_DEBUG "%s: unexpected dr %u\n",
921 __FUNCTION__, dr);
922 *exception = UD_VECTOR;
923 return;
924 }
925}
926
Rusty Russelle756fc62007-07-30 20:07:08 +1000927static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400929 u32 exit_int_info = svm->vmcb->control.exit_int_info;
Rusty Russelle756fc62007-07-30 20:07:08 +1000930 struct kvm *kvm = svm->vcpu.kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931 u64 fault_address;
932 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933
Eddie Dong85f455f2007-07-06 12:20:49 +0300934 if (!irqchip_in_kernel(kvm) &&
935 is_external_interrupt(exit_int_info))
Rusty Russelle756fc62007-07-30 20:07:08 +1000936 push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400938 fault_address = svm->vmcb->control.exit_info_2;
939 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity30677142007-10-28 18:48:59 +0200940 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941}
942
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500943static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
944{
945 int er;
946
Laurent Vivier34273182007-09-18 11:27:37 +0200947 er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500948 if (er != EMULATE_DONE)
949 inject_ud(&svm->vcpu);
950
951 return 1;
952}
953
Rusty Russelle756fc62007-07-30 20:07:08 +1000954static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Anthony Liguori7807fa62007-04-23 09:17:21 -0500955{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400956 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Rusty Russelle756fc62007-07-30 20:07:08 +1000957 if (!(svm->vcpu.cr0 & X86_CR0_TS))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400958 svm->vmcb->save.cr0 &= ~X86_CR0_TS;
Rusty Russelle756fc62007-07-30 20:07:08 +1000959 svm->vcpu.fpu_active = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400960
961 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -0500962}
963
Rusty Russelle756fc62007-07-30 20:07:08 +1000964static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800965{
966 /*
967 * VMCB is undefined after a SHUTDOWN intercept
968 * so reinitialize it.
969 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400970 clear_page(svm->vmcb);
971 init_vmcb(svm->vmcb);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800972
973 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
974 return 0;
975}
976
Rusty Russelle756fc62007-07-30 20:07:08 +1000977static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978{
Mike Dayd77c26f2007-10-08 09:02:08 -0400979 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Avi Kivity039576c2007-03-20 12:46:50 +0200980 int size, down, in, string, rep;
981 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982
Rusty Russelle756fc62007-07-30 20:07:08 +1000983 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400985 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986
Laurent Viviere70669a2007-08-05 10:36:40 +0300987 string = (io_info & SVM_IOIO_STR_MASK) != 0;
988
989 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +0200990 if (emulate_instruction(&svm->vcpu,
991 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +0300992 return 0;
993 return 1;
994 }
995
Avi Kivity039576c2007-03-20 12:46:50 +0200996 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
997 port = io_info >> 16;
998 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity039576c2007-03-20 12:46:50 +0200999 rep = (io_info & SVM_IOIO_REP_MASK) != 0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001000 down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001
Laurent Vivier3090dd72007-08-05 10:43:32 +03001002 return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003}
1004
Rusty Russelle756fc62007-07-30 20:07:08 +10001005static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006{
1007 return 1;
1008}
1009
Rusty Russelle756fc62007-07-30 20:07:08 +10001010static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001012 svm->next_rip = svm->vmcb->save.rip + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001013 skip_emulated_instruction(&svm->vcpu);
1014 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015}
1016
Rusty Russelle756fc62007-07-30 20:07:08 +10001017static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity02e235b2007-02-19 14:37:47 +02001018{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001019 svm->next_rip = svm->vmcb->save.rip + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001020 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001021 kvm_emulate_hypercall(&svm->vcpu);
1022 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001023}
1024
Rusty Russelle756fc62007-07-30 20:07:08 +10001025static int invalid_op_interception(struct vcpu_svm *svm,
1026 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027{
Rusty Russelle756fc62007-07-30 20:07:08 +10001028 inject_ud(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029 return 1;
1030}
1031
Rusty Russelle756fc62007-07-30 20:07:08 +10001032static int task_switch_interception(struct vcpu_svm *svm,
1033 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034{
Rusty Russellf0242472007-08-01 10:48:02 +10001035 pr_unimpl(&svm->vcpu, "%s: task switch is unsupported\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
1037 return 0;
1038}
1039
Rusty Russelle756fc62007-07-30 20:07:08 +10001040static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001042 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001043 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001044 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045}
1046
Rusty Russelle756fc62007-07-30 20:07:08 +10001047static int emulate_on_interception(struct vcpu_svm *svm,
1048 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049{
Laurent Vivier34273182007-09-18 11:27:37 +02001050 if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
Rusty Russellf0242472007-08-01 10:48:02 +10001051 pr_unimpl(&svm->vcpu, "%s: failed\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052 return 1;
1053}
1054
1055static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
1056{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001057 struct vcpu_svm *svm = to_svm(vcpu);
1058
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 case MSR_IA32_TIME_STAMP_COUNTER: {
1061 u64 tsc;
1062
1063 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001064 *data = svm->vmcb->control.tsc_offset + tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 break;
1066 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001067 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001068 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08001070#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001072 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 break;
1074 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001075 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076 break;
1077 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001078 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079 break;
1080 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001081 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082 break;
1083#endif
1084 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001085 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086 break;
1087 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001088 *data = svm->vmcb->save.sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 break;
1090 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001091 *data = svm->vmcb->save.sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 break;
1093 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001094 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095 }
1096 return 0;
1097}
1098
Rusty Russelle756fc62007-07-30 20:07:08 +10001099static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100{
Rusty Russelle756fc62007-07-30 20:07:08 +10001101 u32 ecx = svm->vcpu.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102 u64 data;
1103
Rusty Russelle756fc62007-07-30 20:07:08 +10001104 if (svm_get_msr(&svm->vcpu, ecx, &data))
1105 svm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106 else {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001107 svm->vmcb->save.rax = data & 0xffffffff;
Rusty Russelle756fc62007-07-30 20:07:08 +10001108 svm->vcpu.regs[VCPU_REGS_RDX] = data >> 32;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001109 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001110 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 }
1112 return 1;
1113}
1114
1115static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
1116{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001117 struct vcpu_svm *svm = to_svm(vcpu);
1118
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 case MSR_IA32_TIME_STAMP_COUNTER: {
1121 u64 tsc;
1122
1123 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001124 svm->vmcb->control.tsc_offset = data - tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 break;
1126 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001127 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001128 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08001130#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001132 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133 break;
1134 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001135 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 break;
1137 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001138 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 break;
1140 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001141 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 break;
1143#endif
1144 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001145 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146 break;
1147 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001148 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149 break;
1150 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001151 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152 break;
1153 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001154 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155 }
1156 return 0;
1157}
1158
Rusty Russelle756fc62007-07-30 20:07:08 +10001159static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160{
Rusty Russelle756fc62007-07-30 20:07:08 +10001161 u32 ecx = svm->vcpu.regs[VCPU_REGS_RCX];
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001162 u64 data = (svm->vmcb->save.rax & -1u)
Rusty Russelle756fc62007-07-30 20:07:08 +10001163 | ((u64)(svm->vcpu.regs[VCPU_REGS_RDX] & -1u) << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001164 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001165 if (svm_set_msr(&svm->vcpu, ecx, data))
1166 svm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001168 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001169 return 1;
1170}
1171
Rusty Russelle756fc62007-07-30 20:07:08 +10001172static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173{
Rusty Russelle756fc62007-07-30 20:07:08 +10001174 if (svm->vmcb->control.exit_info_1)
1175 return wrmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001177 return rdmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178}
1179
Rusty Russelle756fc62007-07-30 20:07:08 +10001180static int interrupt_window_interception(struct vcpu_svm *svm,
Dor Laorc1150d82007-01-05 16:36:24 -08001181 struct kvm_run *kvm_run)
1182{
Eddie Dong85f455f2007-07-06 12:20:49 +03001183 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1184 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08001185 /*
1186 * If the user space waits to inject interrupts, exit as soon as
1187 * possible
1188 */
1189 if (kvm_run->request_interrupt_window &&
Rusty Russelle756fc62007-07-30 20:07:08 +10001190 !svm->vcpu.irq_summary) {
1191 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08001192 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
1193 return 0;
1194 }
1195
1196 return 1;
1197}
1198
Rusty Russelle756fc62007-07-30 20:07:08 +10001199static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200 struct kvm_run *kvm_run) = {
1201 [SVM_EXIT_READ_CR0] = emulate_on_interception,
1202 [SVM_EXIT_READ_CR3] = emulate_on_interception,
1203 [SVM_EXIT_READ_CR4] = emulate_on_interception,
1204 /* for now: */
1205 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
1206 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
1207 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
1208 [SVM_EXIT_READ_DR0] = emulate_on_interception,
1209 [SVM_EXIT_READ_DR1] = emulate_on_interception,
1210 [SVM_EXIT_READ_DR2] = emulate_on_interception,
1211 [SVM_EXIT_READ_DR3] = emulate_on_interception,
1212 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
1213 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
1214 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
1215 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
1216 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
1217 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001218 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Anthony Liguori7807fa62007-04-23 09:17:21 -05001220 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221 [SVM_EXIT_INTR] = nop_on_interception,
1222 [SVM_EXIT_NMI] = nop_on_interception,
1223 [SVM_EXIT_SMI] = nop_on_interception,
1224 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08001225 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
1227 [SVM_EXIT_CPUID] = cpuid_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001228 [SVM_EXIT_INVD] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229 [SVM_EXIT_HLT] = halt_interception,
1230 [SVM_EXIT_INVLPG] = emulate_on_interception,
1231 [SVM_EXIT_INVLPGA] = invalid_op_interception,
1232 [SVM_EXIT_IOIO] = io_interception,
1233 [SVM_EXIT_MSR] = msr_interception,
1234 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001235 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236 [SVM_EXIT_VMRUN] = invalid_op_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02001237 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238 [SVM_EXIT_VMLOAD] = invalid_op_interception,
1239 [SVM_EXIT_VMSAVE] = invalid_op_interception,
1240 [SVM_EXIT_STGI] = invalid_op_interception,
1241 [SVM_EXIT_CLGI] = invalid_op_interception,
1242 [SVM_EXIT_SKINIT] = invalid_op_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001243 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01001244 [SVM_EXIT_MONITOR] = invalid_op_interception,
1245 [SVM_EXIT_MWAIT] = invalid_op_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001246};
1247
1248
Avi Kivity04d2cc72007-09-10 18:10:54 +03001249static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001250{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001251 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001252 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001253
Avi Kivity04d2cc72007-09-10 18:10:54 +03001254 kvm_reput_irq(svm);
1255
1256 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
1257 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
1258 kvm_run->fail_entry.hardware_entry_failure_reason
1259 = svm->vmcb->control.exit_code;
1260 return 0;
1261 }
1262
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001263 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR)
1265 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
1266 "exit_code 0x%x\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001267 __FUNCTION__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 exit_code);
1269
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02001270 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08001271 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03001273 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274 return 0;
1275 }
1276
Rusty Russelle756fc62007-07-30 20:07:08 +10001277 return svm_exit_handlers[exit_code](svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278}
1279
1280static void reload_tss(struct kvm_vcpu *vcpu)
1281{
1282 int cpu = raw_smp_processor_id();
1283
1284 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04001285 svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286 load_TR_desc();
1287}
1288
Rusty Russelle756fc62007-07-30 20:07:08 +10001289static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001290{
1291 int cpu = raw_smp_processor_id();
1292
1293 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
1294
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001295 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Rusty Russelle756fc62007-07-30 20:07:08 +10001296 if (svm->vcpu.cpu != cpu ||
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001297 svm->asid_generation != svm_data->asid_generation)
Rusty Russelle756fc62007-07-30 20:07:08 +10001298 new_asid(svm, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299}
1300
1301
Eddie Dong85f455f2007-07-06 12:20:49 +03001302static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303{
1304 struct vmcb_control_area *control;
1305
Rusty Russelle756fc62007-07-30 20:07:08 +10001306 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03001307 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308 control->int_ctl &= ~V_INTR_PRIO_MASK;
1309 control->int_ctl |= V_IRQ_MASK |
1310 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1311}
1312
Eddie Dong2a8067f2007-08-06 16:29:07 +03001313static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
1314{
1315 struct vcpu_svm *svm = to_svm(vcpu);
1316
1317 svm_inject_irq(svm, irq);
1318}
1319
Avi Kivity04d2cc72007-09-10 18:10:54 +03001320static void svm_intr_assist(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001322 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001323 struct vmcb *vmcb = svm->vmcb;
1324 int intr_vector = -1;
1325
1326 if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
1327 ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
1328 intr_vector = vmcb->control.exit_int_info &
1329 SVM_EVTINJ_VEC_MASK;
1330 vmcb->control.exit_int_info = 0;
1331 svm_inject_irq(svm, intr_vector);
1332 return;
1333 }
1334
1335 if (vmcb->control.int_ctl & V_IRQ_MASK)
1336 return;
1337
Eddie Dong1b9778d2007-09-03 16:56:58 +03001338 if (!kvm_cpu_has_interrupt(vcpu))
Eddie Dong85f455f2007-07-06 12:20:49 +03001339 return;
1340
1341 if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
1342 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
1343 (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
1344 /* unable to deliver irq, set pending irq */
1345 vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
1346 svm_inject_irq(svm, 0x0);
1347 return;
1348 }
1349 /* Okay, we can deliver the interrupt: grab it and update PIC state. */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001350 intr_vector = kvm_cpu_get_interrupt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001351 svm_inject_irq(svm, intr_vector);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001352 kvm_timer_intr_post(vcpu, intr_vector);
Eddie Dong85f455f2007-07-06 12:20:49 +03001353}
1354
1355static void kvm_reput_irq(struct vcpu_svm *svm)
1356{
Rusty Russelle756fc62007-07-30 20:07:08 +10001357 struct vmcb_control_area *control = &svm->vmcb->control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358
Eddie Dong7017fc32007-07-18 11:34:57 +03001359 if ((control->int_ctl & V_IRQ_MASK)
1360 && !irqchip_in_kernel(svm->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361 control->int_ctl &= ~V_IRQ_MASK;
Rusty Russelle756fc62007-07-30 20:07:08 +10001362 push_irq(&svm->vcpu, control->int_vector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 }
Dor Laorc1150d82007-01-05 16:36:24 -08001364
Rusty Russelle756fc62007-07-30 20:07:08 +10001365 svm->vcpu.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001366 !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
1367}
1368
Eddie Dong85f455f2007-07-06 12:20:49 +03001369static void svm_do_inject_vector(struct vcpu_svm *svm)
1370{
1371 struct kvm_vcpu *vcpu = &svm->vcpu;
1372 int word_index = __ffs(vcpu->irq_summary);
1373 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1374 int irq = word_index * BITS_PER_LONG + bit_index;
1375
1376 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1377 if (!vcpu->irq_pending[word_index])
1378 clear_bit(word_index, &vcpu->irq_summary);
1379 svm_inject_irq(svm, irq);
1380}
1381
Avi Kivity04d2cc72007-09-10 18:10:54 +03001382static void do_interrupt_requests(struct kvm_vcpu *vcpu,
Dor Laorc1150d82007-01-05 16:36:24 -08001383 struct kvm_run *kvm_run)
1384{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001385 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001386 struct vmcb_control_area *control = &svm->vmcb->control;
Dor Laorc1150d82007-01-05 16:36:24 -08001387
Rusty Russelle756fc62007-07-30 20:07:08 +10001388 svm->vcpu.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001389 (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001390 (svm->vmcb->save.rflags & X86_EFLAGS_IF));
Dor Laorc1150d82007-01-05 16:36:24 -08001391
Rusty Russelle756fc62007-07-30 20:07:08 +10001392 if (svm->vcpu.interrupt_window_open && svm->vcpu.irq_summary)
Dor Laorc1150d82007-01-05 16:36:24 -08001393 /*
1394 * If interrupts enabled, and not blocked by sti or mov ss. Good.
1395 */
Eddie Dong85f455f2007-07-06 12:20:49 +03001396 svm_do_inject_vector(svm);
Dor Laorc1150d82007-01-05 16:36:24 -08001397
1398 /*
1399 * Interrupts blocked. Wait for unblock.
1400 */
Rusty Russelle756fc62007-07-30 20:07:08 +10001401 if (!svm->vcpu.interrupt_window_open &&
Mike Dayd77c26f2007-10-08 09:02:08 -04001402 (svm->vcpu.irq_summary || kvm_run->request_interrupt_window))
Dor Laorc1150d82007-01-05 16:36:24 -08001403 control->intercept |= 1ULL << INTERCEPT_VINTR;
Mike Dayd77c26f2007-10-08 09:02:08 -04001404 else
Dor Laorc1150d82007-01-05 16:36:24 -08001405 control->intercept &= ~(1ULL << INTERCEPT_VINTR);
1406}
1407
Izik Eiduscbc94022007-10-25 00:29:55 +02001408static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
1409{
1410 return 0;
1411}
1412
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413static void save_db_regs(unsigned long *db_regs)
1414{
Avi Kivity5aff4582006-12-13 00:33:45 -08001415 asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
1416 asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
1417 asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
1418 asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419}
1420
1421static void load_db_regs(unsigned long *db_regs)
1422{
Avi Kivity5aff4582006-12-13 00:33:45 -08001423 asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
1424 asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
1425 asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
1426 asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427}
1428
Avi Kivityd9e368d2007-06-07 19:18:30 +03001429static void svm_flush_tlb(struct kvm_vcpu *vcpu)
1430{
1431 force_new_asid(vcpu);
1432}
1433
Avi Kivity04d2cc72007-09-10 18:10:54 +03001434static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
1435{
1436}
1437
1438static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001440 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441 u16 fs_selector;
1442 u16 gs_selector;
1443 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03001444
Rusty Russelle756fc62007-07-30 20:07:08 +10001445 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446
1447 save_host_msrs(vcpu);
1448 fs_selector = read_fs();
1449 gs_selector = read_gs();
1450 ldt_selector = read_ldt();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001451 svm->host_cr2 = kvm_read_cr2();
1452 svm->host_dr6 = read_dr6();
1453 svm->host_dr7 = read_dr7();
1454 svm->vmcb->save.cr2 = vcpu->cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001456 if (svm->vmcb->save.dr7 & 0xff) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457 write_dr7(0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001458 save_db_regs(svm->host_db_regs);
1459 load_db_regs(svm->db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460 }
Avi Kivity36241b82006-12-22 01:05:20 -08001461
Avi Kivity04d2cc72007-09-10 18:10:54 +03001462 clgi();
1463
1464 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08001465
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466 asm volatile (
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001467#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02001468 "push %%rbp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469#else
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001470 "push %%ebp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471#endif
1472
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001473#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001474 "mov %c[rbx](%[svm]), %%rbx \n\t"
1475 "mov %c[rcx](%[svm]), %%rcx \n\t"
1476 "mov %c[rdx](%[svm]), %%rdx \n\t"
1477 "mov %c[rsi](%[svm]), %%rsi \n\t"
1478 "mov %c[rdi](%[svm]), %%rdi \n\t"
1479 "mov %c[rbp](%[svm]), %%rbp \n\t"
1480 "mov %c[r8](%[svm]), %%r8 \n\t"
1481 "mov %c[r9](%[svm]), %%r9 \n\t"
1482 "mov %c[r10](%[svm]), %%r10 \n\t"
1483 "mov %c[r11](%[svm]), %%r11 \n\t"
1484 "mov %c[r12](%[svm]), %%r12 \n\t"
1485 "mov %c[r13](%[svm]), %%r13 \n\t"
1486 "mov %c[r14](%[svm]), %%r14 \n\t"
1487 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001489 "mov %c[rbx](%[svm]), %%ebx \n\t"
1490 "mov %c[rcx](%[svm]), %%ecx \n\t"
1491 "mov %c[rdx](%[svm]), %%edx \n\t"
1492 "mov %c[rsi](%[svm]), %%esi \n\t"
1493 "mov %c[rdi](%[svm]), %%edi \n\t"
1494 "mov %c[rbp](%[svm]), %%ebp \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495#endif
1496
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001497#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498 /* Enter guest mode */
1499 "push %%rax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001500 "mov %c[vmcb](%[svm]), %%rax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 SVM_VMLOAD "\n\t"
1502 SVM_VMRUN "\n\t"
1503 SVM_VMSAVE "\n\t"
1504 "pop %%rax \n\t"
1505#else
1506 /* Enter guest mode */
1507 "push %%eax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001508 "mov %c[vmcb](%[svm]), %%eax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509 SVM_VMLOAD "\n\t"
1510 SVM_VMRUN "\n\t"
1511 SVM_VMSAVE "\n\t"
1512 "pop %%eax \n\t"
1513#endif
1514
1515 /* Save guest registers, load host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001516#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001517 "mov %%rbx, %c[rbx](%[svm]) \n\t"
1518 "mov %%rcx, %c[rcx](%[svm]) \n\t"
1519 "mov %%rdx, %c[rdx](%[svm]) \n\t"
1520 "mov %%rsi, %c[rsi](%[svm]) \n\t"
1521 "mov %%rdi, %c[rdi](%[svm]) \n\t"
1522 "mov %%rbp, %c[rbp](%[svm]) \n\t"
1523 "mov %%r8, %c[r8](%[svm]) \n\t"
1524 "mov %%r9, %c[r9](%[svm]) \n\t"
1525 "mov %%r10, %c[r10](%[svm]) \n\t"
1526 "mov %%r11, %c[r11](%[svm]) \n\t"
1527 "mov %%r12, %c[r12](%[svm]) \n\t"
1528 "mov %%r13, %c[r13](%[svm]) \n\t"
1529 "mov %%r14, %c[r14](%[svm]) \n\t"
1530 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531
Laurent Vivier54a08c02007-10-25 14:18:53 +02001532 "pop %%rbp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001534 "mov %%ebx, %c[rbx](%[svm]) \n\t"
1535 "mov %%ecx, %c[rcx](%[svm]) \n\t"
1536 "mov %%edx, %c[rdx](%[svm]) \n\t"
1537 "mov %%esi, %c[rsi](%[svm]) \n\t"
1538 "mov %%edi, %c[rdi](%[svm]) \n\t"
1539 "mov %%ebp, %c[rbp](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001541 "pop %%ebp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001542#endif
1543 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001544 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Mike Dayd77c26f2007-10-08 09:02:08 -04001546 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RBX])),
1547 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RCX])),
1548 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RDX])),
1549 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RSI])),
1550 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RDI])),
1551 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001552#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -04001553 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R8])),
1554 [r9]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R9])),
1555 [r10]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R10])),
1556 [r11]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R11])),
1557 [r12]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R12])),
1558 [r13]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R13])),
1559 [r14]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R14])),
1560 [r15]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02001562 : "cc", "memory"
1563#ifdef CONFIG_X86_64
1564 , "rbx", "rcx", "rdx", "rsi", "rdi"
1565 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001566#else
1567 , "ebx", "ecx", "edx" , "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02001568#endif
1569 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001571 if ((svm->vmcb->save.dr7 & 0xff))
1572 load_db_regs(svm->host_db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001574 vcpu->cr2 = svm->vmcb->save.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001576 write_dr6(svm->host_dr6);
1577 write_dr7(svm->host_dr7);
1578 kvm_write_cr2(svm->host_cr2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579
1580 load_fs(fs_selector);
1581 load_gs(gs_selector);
1582 load_ldt(ldt_selector);
1583 load_host_msrs(vcpu);
1584
1585 reload_tss(vcpu);
1586
Avi Kivity56ba47d2007-11-07 17:14:18 +02001587 local_irq_disable();
1588
1589 stgi();
1590
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001591 svm->next_rip = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592}
1593
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
1595{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001596 struct vcpu_svm *svm = to_svm(vcpu);
1597
1598 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599 force_new_asid(vcpu);
Anthony Liguori7807fa62007-04-23 09:17:21 -05001600
1601 if (vcpu->fpu_active) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001602 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
1603 svm->vmcb->save.cr0 |= X86_CR0_TS;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001604 vcpu->fpu_active = 0;
1605 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606}
1607
1608static void svm_inject_page_fault(struct kvm_vcpu *vcpu,
1609 unsigned long addr,
1610 uint32_t err_code)
1611{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001612 struct vcpu_svm *svm = to_svm(vcpu);
1613 uint32_t exit_int_info = svm->vmcb->control.exit_int_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614
Avi Kivity1165f5f2007-04-19 17:27:43 +03001615 ++vcpu->stat.pf_guest;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616
1617 if (is_page_fault(exit_int_info)) {
1618
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001619 svm->vmcb->control.event_inj_err = 0;
1620 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
1621 SVM_EVTINJ_VALID_ERR |
1622 SVM_EVTINJ_TYPE_EXEPT |
1623 DF_VECTOR;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624 return;
1625 }
1626 vcpu->cr2 = addr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001627 svm->vmcb->save.cr2 = addr;
1628 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
1629 SVM_EVTINJ_VALID_ERR |
1630 SVM_EVTINJ_TYPE_EXEPT |
1631 PF_VECTOR;
1632 svm->vmcb->control.event_inj_err = err_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633}
1634
1635
1636static int is_disabled(void)
1637{
Joerg Roedel6031a612007-06-22 12:29:50 +03001638 u64 vm_cr;
1639
1640 rdmsrl(MSR_VM_CR, vm_cr);
1641 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
1642 return 1;
1643
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644 return 0;
1645}
1646
Ingo Molnar102d8322007-02-19 14:37:47 +02001647static void
1648svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1649{
1650 /*
1651 * Patch in the VMMCALL instruction:
1652 */
1653 hypercall[0] = 0x0f;
1654 hypercall[1] = 0x01;
1655 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02001656}
1657
Yang, Sheng002c7f72007-07-31 14:23:01 +03001658static void svm_check_processor_compat(void *rtn)
1659{
1660 *(int *)rtn = 0;
1661}
1662
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001663static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001664 .cpu_has_kvm_support = has_svm,
1665 .disabled_by_bios = is_disabled,
1666 .hardware_setup = svm_hardware_setup,
1667 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001668 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669 .hardware_enable = svm_hardware_enable,
1670 .hardware_disable = svm_hardware_disable,
1671
1672 .vcpu_create = svm_create_vcpu,
1673 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001674 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675
Avi Kivity04d2cc72007-09-10 18:10:54 +03001676 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677 .vcpu_load = svm_vcpu_load,
1678 .vcpu_put = svm_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08001679 .vcpu_decache = svm_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680
1681 .set_guest_debug = svm_guest_debug,
1682 .get_msr = svm_get_msr,
1683 .set_msr = svm_set_msr,
1684 .get_segment_base = svm_get_segment_base,
1685 .get_segment = svm_get_segment,
1686 .set_segment = svm_set_segment,
Rusty Russell1747fb72007-09-06 01:21:32 +10001687 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03001688 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690 .set_cr3 = svm_set_cr3,
1691 .set_cr4 = svm_set_cr4,
1692 .set_efer = svm_set_efer,
1693 .get_idt = svm_get_idt,
1694 .set_idt = svm_set_idt,
1695 .get_gdt = svm_get_gdt,
1696 .set_gdt = svm_set_gdt,
1697 .get_dr = svm_get_dr,
1698 .set_dr = svm_set_dr,
1699 .cache_regs = svm_cache_regs,
1700 .decache_regs = svm_decache_regs,
1701 .get_rflags = svm_get_rflags,
1702 .set_rflags = svm_set_rflags,
1703
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704 .tlb_flush = svm_flush_tlb,
1705 .inject_page_fault = svm_inject_page_fault,
1706
1707 .inject_gp = svm_inject_gp,
1708
1709 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001710 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02001712 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03001713 .get_irq = svm_get_irq,
1714 .set_irq = svm_set_irq,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001715 .inject_pending_irq = svm_intr_assist,
1716 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02001717
1718 .set_tss_addr = svm_set_tss_addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719};
1720
1721static int __init svm_init(void)
1722{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001723 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10001724 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725}
1726
1727static void __exit svm_exit(void)
1728{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001729 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001730}
1731
1732module_init(svm_init)
1733module_exit(svm_exit)