blob: 06beed7d4a0894ce8e8b0d255e0ee115bb526581 [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
Avi Kivity298101d2007-11-25 13:41:11 +0200191static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
192 bool has_error_code, u32 error_code)
193{
194 struct vcpu_svm *svm = to_svm(vcpu);
195
196 svm->vmcb->control.event_inj = nr
197 | SVM_EVTINJ_VALID
198 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
199 | SVM_EVTINJ_TYPE_EXEPT;
200 svm->vmcb->control.event_inj_err = error_code;
201}
202
203static bool svm_exception_injected(struct kvm_vcpu *vcpu)
204{
205 struct vcpu_svm *svm = to_svm(vcpu);
206
207 return !(svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID);
208}
209
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210static int is_external_interrupt(u32 info)
211{
212 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
213 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
214}
215
216static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
217{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400218 struct vcpu_svm *svm = to_svm(vcpu);
219
220 if (!svm->next_rip) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221 printk(KERN_DEBUG "%s: NOP\n", __FUNCTION__);
222 return;
223 }
Mike Dayd77c26f2007-10-08 09:02:08 -0400224 if (svm->next_rip - svm->vmcb->save.rip > MAX_INST_SIZE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225 printk(KERN_ERR "%s: ip 0x%llx next 0x%llx\n",
226 __FUNCTION__,
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400227 svm->vmcb->save.rip,
228 svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400230 vcpu->rip = svm->vmcb->save.rip = svm->next_rip;
231 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -0800232
233 vcpu->interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234}
235
236static int has_svm(void)
237{
238 uint32_t eax, ebx, ecx, edx;
239
Avi Kivity1e885462006-12-29 16:49:34 -0800240 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241 printk(KERN_INFO "has_svm: not amd\n");
242 return 0;
243 }
244
245 cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
246 if (eax < SVM_CPUID_FUNC) {
247 printk(KERN_INFO "has_svm: can't execute cpuid_8000000a\n");
248 return 0;
249 }
250
251 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
252 if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) {
253 printk(KERN_DEBUG "has_svm: svm not available\n");
254 return 0;
255 }
256 return 1;
257}
258
259static void svm_hardware_disable(void *garbage)
260{
261 struct svm_cpu_data *svm_data
262 = per_cpu(svm_data, raw_smp_processor_id());
263
264 if (svm_data) {
265 uint64_t efer;
266
267 wrmsrl(MSR_VM_HSAVE_PA, 0);
268 rdmsrl(MSR_EFER, efer);
269 wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK);
Al Viro8b6d44c2007-02-09 16:38:40 +0000270 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271 __free_page(svm_data->save_area);
272 kfree(svm_data);
273 }
274}
275
276static void svm_hardware_enable(void *garbage)
277{
278
279 struct svm_cpu_data *svm_data;
280 uint64_t efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800281#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282 struct desc_ptr gdt_descr;
283#else
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100284 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285#endif
286 struct desc_struct *gdt;
287 int me = raw_smp_processor_id();
288
289 if (!has_svm()) {
290 printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
291 return;
292 }
293 svm_data = per_cpu(svm_data, me);
294
295 if (!svm_data) {
296 printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
297 me);
298 return;
299 }
300
301 svm_data->asid_generation = 1;
302 svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
303 svm_data->next_asid = svm_data->max_asid + 1;
Joerg Roedel80b77062007-03-30 17:02:14 +0300304 svm_features = cpuid_edx(SVM_CPUID_FUNC);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305
Mike Dayd77c26f2007-10-08 09:02:08 -0400306 asm volatile ("sgdt %0" : "=m"(gdt_descr));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800307 gdt = (struct desc_struct *)gdt_descr.address;
308 svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
309
310 rdmsrl(MSR_EFER, efer);
311 wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK);
312
313 wrmsrl(MSR_VM_HSAVE_PA,
314 page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
315}
316
317static int svm_cpu_init(int cpu)
318{
319 struct svm_cpu_data *svm_data;
320 int r;
321
322 svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
323 if (!svm_data)
324 return -ENOMEM;
325 svm_data->cpu = cpu;
326 svm_data->save_area = alloc_page(GFP_KERNEL);
327 r = -ENOMEM;
328 if (!svm_data->save_area)
329 goto err_1;
330
331 per_cpu(svm_data, cpu) = svm_data;
332
333 return 0;
334
335err_1:
336 kfree(svm_data);
337 return r;
338
339}
340
Rusty Russellbfc733a2007-07-31 20:42:42 +1000341static void set_msr_interception(u32 *msrpm, unsigned msr,
342 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800343{
344 int i;
345
346 for (i = 0; i < NUM_MSR_MAPS; i++) {
347 if (msr >= msrpm_ranges[i] &&
348 msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
349 u32 msr_offset = (i * MSRS_IN_RANGE + msr -
350 msrpm_ranges[i]) * 2;
351
352 u32 *base = msrpm + (msr_offset / 32);
353 u32 msr_shift = msr_offset % 32;
354 u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
355 *base = (*base & ~(0x3 << msr_shift)) |
356 (mask << msr_shift);
Rusty Russellbfc733a2007-07-31 20:42:42 +1000357 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358 }
359 }
Rusty Russellbfc733a2007-07-31 20:42:42 +1000360 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361}
362
363static __init int svm_hardware_setup(void)
364{
365 int cpu;
366 struct page *iopm_pages;
367 struct page *msrpm_pages;
Anthony Liguoric8681332007-04-30 09:48:11 +0300368 void *iopm_va, *msrpm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800369 int r;
370
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
372
373 if (!iopm_pages)
374 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300375
376 iopm_va = page_address(iopm_pages);
377 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
378 clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
380
381
382 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
383
384 r = -ENOMEM;
385 if (!msrpm_pages)
386 goto err_1;
387
388 msrpm_va = page_address(msrpm_pages);
389 memset(msrpm_va, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
390 msrpm_base = page_to_pfn(msrpm_pages) << PAGE_SHIFT;
391
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800392#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393 set_msr_interception(msrpm_va, MSR_GS_BASE, 1, 1);
394 set_msr_interception(msrpm_va, MSR_FS_BASE, 1, 1);
395 set_msr_interception(msrpm_va, MSR_KERNEL_GS_BASE, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396 set_msr_interception(msrpm_va, MSR_LSTAR, 1, 1);
397 set_msr_interception(msrpm_va, MSR_CSTAR, 1, 1);
398 set_msr_interception(msrpm_va, MSR_SYSCALL_MASK, 1, 1);
399#endif
Avi Kivity0e859ca2006-12-22 01:05:08 -0800400 set_msr_interception(msrpm_va, MSR_K6_STAR, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_CS, 1, 1);
402 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_ESP, 1, 1);
403 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_EIP, 1, 1);
404
405 for_each_online_cpu(cpu) {
406 r = svm_cpu_init(cpu);
407 if (r)
408 goto err_2;
409 }
410 return 0;
411
412err_2:
413 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
414 msrpm_base = 0;
415err_1:
416 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
417 iopm_base = 0;
418 return r;
419}
420
421static __exit void svm_hardware_unsetup(void)
422{
423 __free_pages(pfn_to_page(msrpm_base >> PAGE_SHIFT), MSRPM_ALLOC_ORDER);
424 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
425 iopm_base = msrpm_base = 0;
426}
427
428static void init_seg(struct vmcb_seg *seg)
429{
430 seg->selector = 0;
431 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
432 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
433 seg->limit = 0xffff;
434 seg->base = 0;
435}
436
437static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
438{
439 seg->selector = 0;
440 seg->attrib = SVM_SELECTOR_P_MASK | type;
441 seg->limit = 0xffff;
442 seg->base = 0;
443}
444
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445static void init_vmcb(struct vmcb *vmcb)
446{
447 struct vmcb_control_area *control = &vmcb->control;
448 struct vmcb_save_area *save = &vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800449
450 control->intercept_cr_read = INTERCEPT_CR0_MASK |
451 INTERCEPT_CR3_MASK |
452 INTERCEPT_CR4_MASK;
453
454 control->intercept_cr_write = INTERCEPT_CR0_MASK |
455 INTERCEPT_CR3_MASK |
456 INTERCEPT_CR4_MASK;
457
458 control->intercept_dr_read = INTERCEPT_DR0_MASK |
459 INTERCEPT_DR1_MASK |
460 INTERCEPT_DR2_MASK |
461 INTERCEPT_DR3_MASK;
462
463 control->intercept_dr_write = INTERCEPT_DR0_MASK |
464 INTERCEPT_DR1_MASK |
465 INTERCEPT_DR2_MASK |
466 INTERCEPT_DR3_MASK |
467 INTERCEPT_DR5_MASK |
468 INTERCEPT_DR7_MASK;
469
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500470 control->intercept_exceptions = (1 << PF_VECTOR) |
471 (1 << UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472
473
474 control->intercept = (1ULL << INTERCEPT_INTR) |
475 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200476 (1ULL << INTERCEPT_SMI) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477 /*
478 * selective cr0 intercept bug?
479 * 0: 0f 22 d8 mov %eax,%cr3
480 * 3: 0f 20 c0 mov %cr0,%eax
481 * 6: 0d 00 00 00 80 or $0x80000000,%eax
482 * b: 0f 22 c0 mov %eax,%cr0
483 * set cr3 ->interception
484 * get cr0 ->interception
485 * set cr0 -> no interception
486 */
487 /* (1ULL << INTERCEPT_SELECTIVE_CR0) | */
488 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200489 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 (1ULL << INTERCEPT_HLT) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491 (1ULL << INTERCEPT_INVLPGA) |
492 (1ULL << INTERCEPT_IOIO_PROT) |
493 (1ULL << INTERCEPT_MSR_PROT) |
494 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800495 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 (1ULL << INTERCEPT_VMRUN) |
497 (1ULL << INTERCEPT_VMMCALL) |
498 (1ULL << INTERCEPT_VMLOAD) |
499 (1ULL << INTERCEPT_VMSAVE) |
500 (1ULL << INTERCEPT_STGI) |
501 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100502 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200503 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100504 (1ULL << INTERCEPT_MONITOR) |
505 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506
507 control->iopm_base_pa = iopm_base;
508 control->msrpm_base_pa = msrpm_base;
Avi Kivity0cc50642007-03-25 12:07:27 +0200509 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510 control->int_ctl = V_INTR_MASKING_MASK;
511
512 init_seg(&save->es);
513 init_seg(&save->ss);
514 init_seg(&save->ds);
515 init_seg(&save->fs);
516 init_seg(&save->gs);
517
518 save->cs.selector = 0xf000;
519 /* Executable/Readable Code Segment */
520 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
521 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
522 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800523 /*
524 * cs.base should really be 0xffff0000, but vmx can't handle that, so
525 * be consistent with it.
526 *
527 * Replace when we have real mode working for vmx.
528 */
529 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530
531 save->gdtr.limit = 0xffff;
532 save->idtr.limit = 0xffff;
533
534 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
535 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
536
537 save->efer = MSR_EFER_SVME_MASK;
Mike Dayd77c26f2007-10-08 09:02:08 -0400538 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539 save->dr7 = 0x400;
540 save->rflags = 2;
541 save->rip = 0x0000fff0;
542
543 /*
544 * cr0 val on cpu init should be 0x60000010, we enable cpu
545 * cache by default. the orderly way is to enable cache in bios.
546 */
Rusty Russell707d92fa2007-07-17 23:19:08 +1000547 save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
Rusty Russell66aee912007-07-17 23:34:16 +1000548 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549 /* rdx = ?? */
550}
551
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200552static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300553{
554 struct vcpu_svm *svm = to_svm(vcpu);
555
556 init_vmcb(svm->vmcb);
Avi Kivity70433382007-11-07 12:57:23 +0200557
558 if (vcpu->vcpu_id != 0) {
559 svm->vmcb->save.rip = 0;
560 svm->vmcb->save.cs.base = svm->vcpu.sipi_vector << 12;
561 svm->vmcb->save.cs.selector = svm->vcpu.sipi_vector << 8;
562 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200563
564 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300565}
566
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000567static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800568{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400569 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570 struct page *page;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000571 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572
Rusty Russellc16f8622007-07-30 21:12:19 +1000573 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000574 if (!svm) {
575 err = -ENOMEM;
576 goto out;
577 }
578
579 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
580 if (err)
581 goto free_svm;
582
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583 page = alloc_page(GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000584 if (!page) {
585 err = -ENOMEM;
586 goto uninit;
587 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400589 svm->vmcb = page_address(page);
590 clear_page(svm->vmcb);
591 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
592 svm->asid_generation = 0;
593 memset(svm->db_regs, 0, sizeof(svm->db_regs));
594 init_vmcb(svm->vmcb);
595
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000596 fx_init(&svm->vcpu);
597 svm->vcpu.fpu_active = 1;
598 svm->vcpu.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
599 if (svm->vcpu.vcpu_id == 0)
600 svm->vcpu.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800601
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000602 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800603
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000604uninit:
605 kvm_vcpu_uninit(&svm->vcpu);
606free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000607 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000608out:
609 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800610}
611
612static void svm_free_vcpu(struct kvm_vcpu *vcpu)
613{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400614 struct vcpu_svm *svm = to_svm(vcpu);
615
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000616 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
617 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000618 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619}
620
Avi Kivity15ad7142007-07-11 18:17:21 +0300621static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400623 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300624 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200625
Avi Kivity0cc50642007-03-25 12:07:27 +0200626 if (unlikely(cpu != vcpu->cpu)) {
627 u64 tsc_this, delta;
628
629 /*
630 * Make sure that the guest sees a monotonically
631 * increasing TSC.
632 */
633 rdtscll(tsc_this);
634 delta = vcpu->host_tsc - tsc_this;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400635 svm->vmcb->control.tsc_offset += delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200636 vcpu->cpu = cpu;
Eddie Donga3d7f852007-09-03 16:15:12 +0300637 kvm_migrate_apic_timer(vcpu);
Avi Kivity0cc50642007-03-25 12:07:27 +0200638 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300639
640 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400641 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642}
643
644static void svm_vcpu_put(struct kvm_vcpu *vcpu)
645{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400646 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300647 int i;
648
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200649 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300650 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400651 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300652
Avi Kivity0cc50642007-03-25 12:07:27 +0200653 rdtscll(vcpu->host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654}
655
Avi Kivity774c47f2007-02-12 00:54:47 -0800656static void svm_vcpu_decache(struct kvm_vcpu *vcpu)
657{
658}
659
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660static void svm_cache_regs(struct kvm_vcpu *vcpu)
661{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400662 struct vcpu_svm *svm = to_svm(vcpu);
663
664 vcpu->regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
665 vcpu->regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
666 vcpu->rip = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667}
668
669static void svm_decache_regs(struct kvm_vcpu *vcpu)
670{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400671 struct vcpu_svm *svm = to_svm(vcpu);
672 svm->vmcb->save.rax = vcpu->regs[VCPU_REGS_RAX];
673 svm->vmcb->save.rsp = vcpu->regs[VCPU_REGS_RSP];
674 svm->vmcb->save.rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675}
676
677static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
678{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400679 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680}
681
682static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
683{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400684 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685}
686
687static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
688{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400689 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690
691 switch (seg) {
692 case VCPU_SREG_CS: return &save->cs;
693 case VCPU_SREG_DS: return &save->ds;
694 case VCPU_SREG_ES: return &save->es;
695 case VCPU_SREG_FS: return &save->fs;
696 case VCPU_SREG_GS: return &save->gs;
697 case VCPU_SREG_SS: return &save->ss;
698 case VCPU_SREG_TR: return &save->tr;
699 case VCPU_SREG_LDTR: return &save->ldtr;
700 }
701 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000702 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703}
704
705static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
706{
707 struct vmcb_seg *s = svm_seg(vcpu, seg);
708
709 return s->base;
710}
711
712static void svm_get_segment(struct kvm_vcpu *vcpu,
713 struct kvm_segment *var, int seg)
714{
715 struct vmcb_seg *s = svm_seg(vcpu, seg);
716
717 var->base = s->base;
718 var->limit = s->limit;
719 var->selector = s->selector;
720 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
721 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
722 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
723 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
724 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
725 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
726 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
727 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
728 var->unusable = !var->present;
729}
730
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
732{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400733 struct vcpu_svm *svm = to_svm(vcpu);
734
735 dt->limit = svm->vmcb->save.idtr.limit;
736 dt->base = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737}
738
739static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
740{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400741 struct vcpu_svm *svm = to_svm(vcpu);
742
743 svm->vmcb->save.idtr.limit = dt->limit;
744 svm->vmcb->save.idtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745}
746
747static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
748{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400749 struct vcpu_svm *svm = to_svm(vcpu);
750
751 dt->limit = svm->vmcb->save.gdtr.limit;
752 dt->base = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753}
754
755static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
756{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400757 struct vcpu_svm *svm = to_svm(vcpu);
758
759 svm->vmcb->save.gdtr.limit = dt->limit;
760 svm->vmcb->save.gdtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761}
762
Anthony Liguori25c4c272007-04-27 09:29:21 +0300763static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -0800764{
765}
766
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
768{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400769 struct vcpu_svm *svm = to_svm(vcpu);
770
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800771#ifdef CONFIG_X86_64
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600772 if (vcpu->shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +1000773 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600774 vcpu->shadow_efer |= EFER_LMA;
775 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 }
777
Mike Dayd77c26f2007-10-08 09:02:08 -0400778 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 }
783#endif
Rusty Russell707d92fa2007-07-17 23:19:08 +1000784 if ((vcpu->cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400785 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Anthony Liguori7807fa62007-04-23 09:17:21 -0500786 vcpu->fpu_active = 1;
787 }
788
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 vcpu->cr0 = cr0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000790 cr0 |= X86_CR0_PG | X86_CR0_WP;
791 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400792 svm->vmcb->save.cr0 = cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793}
794
795static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
796{
797 vcpu->cr4 = cr4;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400798 to_svm(vcpu)->vmcb->save.cr4 = cr4 | X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799}
800
801static void svm_set_segment(struct kvm_vcpu *vcpu,
802 struct kvm_segment *var, int seg)
803{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400804 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805 struct vmcb_seg *s = svm_seg(vcpu, seg);
806
807 s->base = var->base;
808 s->limit = var->limit;
809 s->selector = var->selector;
810 if (var->unusable)
811 s->attrib = 0;
812 else {
813 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
814 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
815 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
816 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
817 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
818 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
819 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
820 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
821 }
822 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400823 svm->vmcb->save.cpl
824 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 >> SVM_SELECTOR_DPL_SHIFT) & 3;
826
827}
828
829/* FIXME:
830
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400831 svm(vcpu)->vmcb->control.int_ctl &= ~V_TPR_MASK;
832 svm(vcpu)->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833
834*/
835
836static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
837{
838 return -EOPNOTSUPP;
839}
840
Eddie Dong2a8067f2007-08-06 16:29:07 +0300841static int svm_get_irq(struct kvm_vcpu *vcpu)
842{
843 struct vcpu_svm *svm = to_svm(vcpu);
844 u32 exit_int_info = svm->vmcb->control.exit_int_info;
845
846 if (is_external_interrupt(exit_int_info))
847 return exit_int_info & SVM_EVTINJ_VEC_MASK;
848 return -1;
849}
850
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851static void load_host_msrs(struct kvm_vcpu *vcpu)
852{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300853#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400854 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300855#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856}
857
858static void save_host_msrs(struct kvm_vcpu *vcpu)
859{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300860#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400861 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300862#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863}
864
Rusty Russelle756fc62007-07-30 20:07:08 +1000865static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800866{
867 if (svm_data->next_asid > svm_data->max_asid) {
868 ++svm_data->asid_generation;
869 svm_data->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400870 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871 }
872
Rusty Russelle756fc62007-07-30 20:07:08 +1000873 svm->vcpu.cpu = svm_data->cpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400874 svm->asid_generation = svm_data->asid_generation;
875 svm->vmcb->control.asid = svm_data->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876}
877
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
879{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400880 return to_svm(vcpu)->db_regs[dr];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881}
882
883static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
884 int *exception)
885{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400886 struct vcpu_svm *svm = to_svm(vcpu);
887
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 *exception = 0;
889
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400890 if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
891 svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
892 svm->vmcb->save.dr6 |= DR6_BD_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 *exception = DB_VECTOR;
894 return;
895 }
896
897 switch (dr) {
898 case 0 ... 3:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400899 svm->db_regs[dr] = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 return;
901 case 4 ... 5:
Rusty Russell66aee912007-07-17 23:34:16 +1000902 if (vcpu->cr4 & X86_CR4_DE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 *exception = UD_VECTOR;
904 return;
905 }
906 case 7: {
907 if (value & ~((1ULL << 32) - 1)) {
908 *exception = GP_VECTOR;
909 return;
910 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400911 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 return;
913 }
914 default:
915 printk(KERN_DEBUG "%s: unexpected dr %u\n",
916 __FUNCTION__, dr);
917 *exception = UD_VECTOR;
918 return;
919 }
920}
921
Rusty Russelle756fc62007-07-30 20:07:08 +1000922static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400924 u32 exit_int_info = svm->vmcb->control.exit_int_info;
Rusty Russelle756fc62007-07-30 20:07:08 +1000925 struct kvm *kvm = svm->vcpu.kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 u64 fault_address;
927 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928
Eddie Dong85f455f2007-07-06 12:20:49 +0300929 if (!irqchip_in_kernel(kvm) &&
930 is_external_interrupt(exit_int_info))
Rusty Russelle756fc62007-07-30 20:07:08 +1000931 push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400933 fault_address = svm->vmcb->control.exit_info_2;
934 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity30677142007-10-28 18:48:59 +0200935 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936}
937
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500938static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
939{
940 int er;
941
Laurent Vivier34273182007-09-18 11:27:37 +0200942 er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500943 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +0200944 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500945 return 1;
946}
947
Rusty Russelle756fc62007-07-30 20:07:08 +1000948static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Anthony Liguori7807fa62007-04-23 09:17:21 -0500949{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400950 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Rusty Russelle756fc62007-07-30 20:07:08 +1000951 if (!(svm->vcpu.cr0 & X86_CR0_TS))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400952 svm->vmcb->save.cr0 &= ~X86_CR0_TS;
Rusty Russelle756fc62007-07-30 20:07:08 +1000953 svm->vcpu.fpu_active = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400954
955 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -0500956}
957
Rusty Russelle756fc62007-07-30 20:07:08 +1000958static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800959{
960 /*
961 * VMCB is undefined after a SHUTDOWN intercept
962 * so reinitialize it.
963 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400964 clear_page(svm->vmcb);
965 init_vmcb(svm->vmcb);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800966
967 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
968 return 0;
969}
970
Rusty Russelle756fc62007-07-30 20:07:08 +1000971static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972{
Mike Dayd77c26f2007-10-08 09:02:08 -0400973 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Avi Kivity039576c2007-03-20 12:46:50 +0200974 int size, down, in, string, rep;
975 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976
Rusty Russelle756fc62007-07-30 20:07:08 +1000977 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400979 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980
Laurent Viviere70669a2007-08-05 10:36:40 +0300981 string = (io_info & SVM_IOIO_STR_MASK) != 0;
982
983 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +0200984 if (emulate_instruction(&svm->vcpu,
985 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +0300986 return 0;
987 return 1;
988 }
989
Avi Kivity039576c2007-03-20 12:46:50 +0200990 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
991 port = io_info >> 16;
992 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity039576c2007-03-20 12:46:50 +0200993 rep = (io_info & SVM_IOIO_REP_MASK) != 0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400994 down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995
Laurent Vivier3090dd72007-08-05 10:43:32 +0300996 return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997}
998
Rusty Russelle756fc62007-07-30 20:07:08 +1000999static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000{
1001 return 1;
1002}
1003
Rusty Russelle756fc62007-07-30 20:07:08 +10001004static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001006 svm->next_rip = svm->vmcb->save.rip + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001007 skip_emulated_instruction(&svm->vcpu);
1008 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009}
1010
Rusty Russelle756fc62007-07-30 20:07:08 +10001011static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity02e235b2007-02-19 14:37:47 +02001012{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001013 svm->next_rip = svm->vmcb->save.rip + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001014 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001015 kvm_emulate_hypercall(&svm->vcpu);
1016 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001017}
1018
Rusty Russelle756fc62007-07-30 20:07:08 +10001019static int invalid_op_interception(struct vcpu_svm *svm,
1020 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001022 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023 return 1;
1024}
1025
Rusty Russelle756fc62007-07-30 20:07:08 +10001026static int task_switch_interception(struct vcpu_svm *svm,
1027 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028{
Rusty Russellf0242472007-08-01 10:48:02 +10001029 pr_unimpl(&svm->vcpu, "%s: task switch is unsupported\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
1031 return 0;
1032}
1033
Rusty Russelle756fc62007-07-30 20:07:08 +10001034static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001036 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001037 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001038 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039}
1040
Rusty Russelle756fc62007-07-30 20:07:08 +10001041static int emulate_on_interception(struct vcpu_svm *svm,
1042 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043{
Laurent Vivier34273182007-09-18 11:27:37 +02001044 if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
Rusty Russellf0242472007-08-01 10:48:02 +10001045 pr_unimpl(&svm->vcpu, "%s: failed\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046 return 1;
1047}
1048
1049static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
1050{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001051 struct vcpu_svm *svm = to_svm(vcpu);
1052
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054 case MSR_IA32_TIME_STAMP_COUNTER: {
1055 u64 tsc;
1056
1057 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001058 *data = svm->vmcb->control.tsc_offset + tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 break;
1060 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001061 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001062 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08001064#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001066 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 break;
1068 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001069 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070 break;
1071 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001072 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 break;
1074 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001075 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076 break;
1077#endif
1078 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001079 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080 break;
1081 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001082 *data = svm->vmcb->save.sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083 break;
1084 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001085 *data = svm->vmcb->save.sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086 break;
1087 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001088 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 }
1090 return 0;
1091}
1092
Rusty Russelle756fc62007-07-30 20:07:08 +10001093static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094{
Rusty Russelle756fc62007-07-30 20:07:08 +10001095 u32 ecx = svm->vcpu.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096 u64 data;
1097
Rusty Russelle756fc62007-07-30 20:07:08 +10001098 if (svm_get_msr(&svm->vcpu, ecx, &data))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001099 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100 else {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001101 svm->vmcb->save.rax = data & 0xffffffff;
Rusty Russelle756fc62007-07-30 20:07:08 +10001102 svm->vcpu.regs[VCPU_REGS_RDX] = data >> 32;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001103 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001104 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 }
1106 return 1;
1107}
1108
1109static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
1110{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001111 struct vcpu_svm *svm = to_svm(vcpu);
1112
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114 case MSR_IA32_TIME_STAMP_COUNTER: {
1115 u64 tsc;
1116
1117 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001118 svm->vmcb->control.tsc_offset = data - tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 break;
1120 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001121 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001122 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08001124#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001126 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127 break;
1128 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001129 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130 break;
1131 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001132 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133 break;
1134 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001135 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 break;
1137#endif
1138 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001139 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140 break;
1141 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001142 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143 break;
1144 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001145 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146 break;
1147 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001148 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149 }
1150 return 0;
1151}
1152
Rusty Russelle756fc62007-07-30 20:07:08 +10001153static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154{
Rusty Russelle756fc62007-07-30 20:07:08 +10001155 u32 ecx = svm->vcpu.regs[VCPU_REGS_RCX];
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001156 u64 data = (svm->vmcb->save.rax & -1u)
Rusty Russelle756fc62007-07-30 20:07:08 +10001157 | ((u64)(svm->vcpu.regs[VCPU_REGS_RDX] & -1u) << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001158 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001159 if (svm_set_msr(&svm->vcpu, ecx, data))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001160 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001162 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163 return 1;
1164}
1165
Rusty Russelle756fc62007-07-30 20:07:08 +10001166static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167{
Rusty Russelle756fc62007-07-30 20:07:08 +10001168 if (svm->vmcb->control.exit_info_1)
1169 return wrmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001171 return rdmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172}
1173
Rusty Russelle756fc62007-07-30 20:07:08 +10001174static int interrupt_window_interception(struct vcpu_svm *svm,
Dor Laorc1150d82007-01-05 16:36:24 -08001175 struct kvm_run *kvm_run)
1176{
Eddie Dong85f455f2007-07-06 12:20:49 +03001177 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1178 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08001179 /*
1180 * If the user space waits to inject interrupts, exit as soon as
1181 * possible
1182 */
1183 if (kvm_run->request_interrupt_window &&
Rusty Russelle756fc62007-07-30 20:07:08 +10001184 !svm->vcpu.irq_summary) {
1185 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08001186 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
1187 return 0;
1188 }
1189
1190 return 1;
1191}
1192
Rusty Russelle756fc62007-07-30 20:07:08 +10001193static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194 struct kvm_run *kvm_run) = {
1195 [SVM_EXIT_READ_CR0] = emulate_on_interception,
1196 [SVM_EXIT_READ_CR3] = emulate_on_interception,
1197 [SVM_EXIT_READ_CR4] = emulate_on_interception,
1198 /* for now: */
1199 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
1200 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
1201 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
1202 [SVM_EXIT_READ_DR0] = emulate_on_interception,
1203 [SVM_EXIT_READ_DR1] = emulate_on_interception,
1204 [SVM_EXIT_READ_DR2] = emulate_on_interception,
1205 [SVM_EXIT_READ_DR3] = emulate_on_interception,
1206 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
1207 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
1208 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
1209 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
1210 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
1211 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001212 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Anthony Liguori7807fa62007-04-23 09:17:21 -05001214 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215 [SVM_EXIT_INTR] = nop_on_interception,
1216 [SVM_EXIT_NMI] = nop_on_interception,
1217 [SVM_EXIT_SMI] = nop_on_interception,
1218 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08001219 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
1221 [SVM_EXIT_CPUID] = cpuid_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001222 [SVM_EXIT_INVD] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223 [SVM_EXIT_HLT] = halt_interception,
1224 [SVM_EXIT_INVLPG] = emulate_on_interception,
1225 [SVM_EXIT_INVLPGA] = invalid_op_interception,
1226 [SVM_EXIT_IOIO] = io_interception,
1227 [SVM_EXIT_MSR] = msr_interception,
1228 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001229 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001230 [SVM_EXIT_VMRUN] = invalid_op_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02001231 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232 [SVM_EXIT_VMLOAD] = invalid_op_interception,
1233 [SVM_EXIT_VMSAVE] = invalid_op_interception,
1234 [SVM_EXIT_STGI] = invalid_op_interception,
1235 [SVM_EXIT_CLGI] = invalid_op_interception,
1236 [SVM_EXIT_SKINIT] = invalid_op_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001237 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01001238 [SVM_EXIT_MONITOR] = invalid_op_interception,
1239 [SVM_EXIT_MWAIT] = invalid_op_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240};
1241
1242
Avi Kivity04d2cc72007-09-10 18:10:54 +03001243static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001245 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001246 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001247
Avi Kivity04d2cc72007-09-10 18:10:54 +03001248 kvm_reput_irq(svm);
1249
1250 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
1251 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
1252 kvm_run->fail_entry.hardware_entry_failure_reason
1253 = svm->vmcb->control.exit_code;
1254 return 0;
1255 }
1256
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001257 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08001258 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR)
1259 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
1260 "exit_code 0x%x\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001261 __FUNCTION__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262 exit_code);
1263
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02001264 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08001265 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03001267 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 return 0;
1269 }
1270
Rusty Russelle756fc62007-07-30 20:07:08 +10001271 return svm_exit_handlers[exit_code](svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272}
1273
1274static void reload_tss(struct kvm_vcpu *vcpu)
1275{
1276 int cpu = raw_smp_processor_id();
1277
1278 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04001279 svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280 load_TR_desc();
1281}
1282
Rusty Russelle756fc62007-07-30 20:07:08 +10001283static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284{
1285 int cpu = raw_smp_processor_id();
1286
1287 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
1288
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001289 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Rusty Russelle756fc62007-07-30 20:07:08 +10001290 if (svm->vcpu.cpu != cpu ||
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001291 svm->asid_generation != svm_data->asid_generation)
Rusty Russelle756fc62007-07-30 20:07:08 +10001292 new_asid(svm, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293}
1294
1295
Eddie Dong85f455f2007-07-06 12:20:49 +03001296static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297{
1298 struct vmcb_control_area *control;
1299
Rusty Russelle756fc62007-07-30 20:07:08 +10001300 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03001301 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302 control->int_ctl &= ~V_INTR_PRIO_MASK;
1303 control->int_ctl |= V_IRQ_MASK |
1304 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1305}
1306
Eddie Dong2a8067f2007-08-06 16:29:07 +03001307static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
1308{
1309 struct vcpu_svm *svm = to_svm(vcpu);
1310
1311 svm_inject_irq(svm, irq);
1312}
1313
Avi Kivity04d2cc72007-09-10 18:10:54 +03001314static void svm_intr_assist(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001316 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001317 struct vmcb *vmcb = svm->vmcb;
1318 int intr_vector = -1;
1319
1320 if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
1321 ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
1322 intr_vector = vmcb->control.exit_int_info &
1323 SVM_EVTINJ_VEC_MASK;
1324 vmcb->control.exit_int_info = 0;
1325 svm_inject_irq(svm, intr_vector);
1326 return;
1327 }
1328
1329 if (vmcb->control.int_ctl & V_IRQ_MASK)
1330 return;
1331
Eddie Dong1b9778d2007-09-03 16:56:58 +03001332 if (!kvm_cpu_has_interrupt(vcpu))
Eddie Dong85f455f2007-07-06 12:20:49 +03001333 return;
1334
1335 if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
1336 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
1337 (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
1338 /* unable to deliver irq, set pending irq */
1339 vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
1340 svm_inject_irq(svm, 0x0);
1341 return;
1342 }
1343 /* Okay, we can deliver the interrupt: grab it and update PIC state. */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001344 intr_vector = kvm_cpu_get_interrupt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001345 svm_inject_irq(svm, intr_vector);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001346 kvm_timer_intr_post(vcpu, intr_vector);
Eddie Dong85f455f2007-07-06 12:20:49 +03001347}
1348
1349static void kvm_reput_irq(struct vcpu_svm *svm)
1350{
Rusty Russelle756fc62007-07-30 20:07:08 +10001351 struct vmcb_control_area *control = &svm->vmcb->control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352
Eddie Dong7017fc32007-07-18 11:34:57 +03001353 if ((control->int_ctl & V_IRQ_MASK)
1354 && !irqchip_in_kernel(svm->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355 control->int_ctl &= ~V_IRQ_MASK;
Rusty Russelle756fc62007-07-30 20:07:08 +10001356 push_irq(&svm->vcpu, control->int_vector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357 }
Dor Laorc1150d82007-01-05 16:36:24 -08001358
Rusty Russelle756fc62007-07-30 20:07:08 +10001359 svm->vcpu.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001360 !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
1361}
1362
Eddie Dong85f455f2007-07-06 12:20:49 +03001363static void svm_do_inject_vector(struct vcpu_svm *svm)
1364{
1365 struct kvm_vcpu *vcpu = &svm->vcpu;
1366 int word_index = __ffs(vcpu->irq_summary);
1367 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1368 int irq = word_index * BITS_PER_LONG + bit_index;
1369
1370 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1371 if (!vcpu->irq_pending[word_index])
1372 clear_bit(word_index, &vcpu->irq_summary);
1373 svm_inject_irq(svm, irq);
1374}
1375
Avi Kivity04d2cc72007-09-10 18:10:54 +03001376static void do_interrupt_requests(struct kvm_vcpu *vcpu,
Dor Laorc1150d82007-01-05 16:36:24 -08001377 struct kvm_run *kvm_run)
1378{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001379 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001380 struct vmcb_control_area *control = &svm->vmcb->control;
Dor Laorc1150d82007-01-05 16:36:24 -08001381
Rusty Russelle756fc62007-07-30 20:07:08 +10001382 svm->vcpu.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001383 (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001384 (svm->vmcb->save.rflags & X86_EFLAGS_IF));
Dor Laorc1150d82007-01-05 16:36:24 -08001385
Rusty Russelle756fc62007-07-30 20:07:08 +10001386 if (svm->vcpu.interrupt_window_open && svm->vcpu.irq_summary)
Dor Laorc1150d82007-01-05 16:36:24 -08001387 /*
1388 * If interrupts enabled, and not blocked by sti or mov ss. Good.
1389 */
Eddie Dong85f455f2007-07-06 12:20:49 +03001390 svm_do_inject_vector(svm);
Dor Laorc1150d82007-01-05 16:36:24 -08001391
1392 /*
1393 * Interrupts blocked. Wait for unblock.
1394 */
Rusty Russelle756fc62007-07-30 20:07:08 +10001395 if (!svm->vcpu.interrupt_window_open &&
Mike Dayd77c26f2007-10-08 09:02:08 -04001396 (svm->vcpu.irq_summary || kvm_run->request_interrupt_window))
Dor Laorc1150d82007-01-05 16:36:24 -08001397 control->intercept |= 1ULL << INTERCEPT_VINTR;
Mike Dayd77c26f2007-10-08 09:02:08 -04001398 else
Dor Laorc1150d82007-01-05 16:36:24 -08001399 control->intercept &= ~(1ULL << INTERCEPT_VINTR);
1400}
1401
Izik Eiduscbc94022007-10-25 00:29:55 +02001402static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
1403{
1404 return 0;
1405}
1406
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407static void save_db_regs(unsigned long *db_regs)
1408{
Avi Kivity5aff4582006-12-13 00:33:45 -08001409 asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
1410 asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
1411 asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
1412 asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413}
1414
1415static void load_db_regs(unsigned long *db_regs)
1416{
Avi Kivity5aff4582006-12-13 00:33:45 -08001417 asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
1418 asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
1419 asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
1420 asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421}
1422
Avi Kivityd9e368d2007-06-07 19:18:30 +03001423static void svm_flush_tlb(struct kvm_vcpu *vcpu)
1424{
1425 force_new_asid(vcpu);
1426}
1427
Avi Kivity04d2cc72007-09-10 18:10:54 +03001428static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
1429{
1430}
1431
1432static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001434 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435 u16 fs_selector;
1436 u16 gs_selector;
1437 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03001438
Rusty Russelle756fc62007-07-30 20:07:08 +10001439 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440
1441 save_host_msrs(vcpu);
1442 fs_selector = read_fs();
1443 gs_selector = read_gs();
1444 ldt_selector = read_ldt();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001445 svm->host_cr2 = kvm_read_cr2();
1446 svm->host_dr6 = read_dr6();
1447 svm->host_dr7 = read_dr7();
1448 svm->vmcb->save.cr2 = vcpu->cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001450 if (svm->vmcb->save.dr7 & 0xff) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451 write_dr7(0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001452 save_db_regs(svm->host_db_regs);
1453 load_db_regs(svm->db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454 }
Avi Kivity36241b82006-12-22 01:05:20 -08001455
Avi Kivity04d2cc72007-09-10 18:10:54 +03001456 clgi();
1457
1458 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08001459
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460 asm volatile (
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001461#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02001462 "push %%rbp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463#else
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001464 "push %%ebp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465#endif
1466
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001467#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001468 "mov %c[rbx](%[svm]), %%rbx \n\t"
1469 "mov %c[rcx](%[svm]), %%rcx \n\t"
1470 "mov %c[rdx](%[svm]), %%rdx \n\t"
1471 "mov %c[rsi](%[svm]), %%rsi \n\t"
1472 "mov %c[rdi](%[svm]), %%rdi \n\t"
1473 "mov %c[rbp](%[svm]), %%rbp \n\t"
1474 "mov %c[r8](%[svm]), %%r8 \n\t"
1475 "mov %c[r9](%[svm]), %%r9 \n\t"
1476 "mov %c[r10](%[svm]), %%r10 \n\t"
1477 "mov %c[r11](%[svm]), %%r11 \n\t"
1478 "mov %c[r12](%[svm]), %%r12 \n\t"
1479 "mov %c[r13](%[svm]), %%r13 \n\t"
1480 "mov %c[r14](%[svm]), %%r14 \n\t"
1481 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001483 "mov %c[rbx](%[svm]), %%ebx \n\t"
1484 "mov %c[rcx](%[svm]), %%ecx \n\t"
1485 "mov %c[rdx](%[svm]), %%edx \n\t"
1486 "mov %c[rsi](%[svm]), %%esi \n\t"
1487 "mov %c[rdi](%[svm]), %%edi \n\t"
1488 "mov %c[rbp](%[svm]), %%ebp \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489#endif
1490
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001491#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492 /* Enter guest mode */
1493 "push %%rax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001494 "mov %c[vmcb](%[svm]), %%rax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495 SVM_VMLOAD "\n\t"
1496 SVM_VMRUN "\n\t"
1497 SVM_VMSAVE "\n\t"
1498 "pop %%rax \n\t"
1499#else
1500 /* Enter guest mode */
1501 "push %%eax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001502 "mov %c[vmcb](%[svm]), %%eax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 SVM_VMLOAD "\n\t"
1504 SVM_VMRUN "\n\t"
1505 SVM_VMSAVE "\n\t"
1506 "pop %%eax \n\t"
1507#endif
1508
1509 /* Save guest registers, load host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001510#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001511 "mov %%rbx, %c[rbx](%[svm]) \n\t"
1512 "mov %%rcx, %c[rcx](%[svm]) \n\t"
1513 "mov %%rdx, %c[rdx](%[svm]) \n\t"
1514 "mov %%rsi, %c[rsi](%[svm]) \n\t"
1515 "mov %%rdi, %c[rdi](%[svm]) \n\t"
1516 "mov %%rbp, %c[rbp](%[svm]) \n\t"
1517 "mov %%r8, %c[r8](%[svm]) \n\t"
1518 "mov %%r9, %c[r9](%[svm]) \n\t"
1519 "mov %%r10, %c[r10](%[svm]) \n\t"
1520 "mov %%r11, %c[r11](%[svm]) \n\t"
1521 "mov %%r12, %c[r12](%[svm]) \n\t"
1522 "mov %%r13, %c[r13](%[svm]) \n\t"
1523 "mov %%r14, %c[r14](%[svm]) \n\t"
1524 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525
Laurent Vivier54a08c02007-10-25 14:18:53 +02001526 "pop %%rbp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001528 "mov %%ebx, %c[rbx](%[svm]) \n\t"
1529 "mov %%ecx, %c[rcx](%[svm]) \n\t"
1530 "mov %%edx, %c[rdx](%[svm]) \n\t"
1531 "mov %%esi, %c[rsi](%[svm]) \n\t"
1532 "mov %%edi, %c[rdi](%[svm]) \n\t"
1533 "mov %%ebp, %c[rbp](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001535 "pop %%ebp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001536#endif
1537 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001538 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08001539 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Mike Dayd77c26f2007-10-08 09:02:08 -04001540 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RBX])),
1541 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RCX])),
1542 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RDX])),
1543 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RSI])),
1544 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RDI])),
1545 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001546#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -04001547 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R8])),
1548 [r9]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R9])),
1549 [r10]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R10])),
1550 [r11]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R11])),
1551 [r12]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R12])),
1552 [r13]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R13])),
1553 [r14]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R14])),
1554 [r15]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001555#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02001556 : "cc", "memory"
1557#ifdef CONFIG_X86_64
1558 , "rbx", "rcx", "rdx", "rsi", "rdi"
1559 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001560#else
1561 , "ebx", "ecx", "edx" , "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02001562#endif
1563 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08001564
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001565 if ((svm->vmcb->save.dr7 & 0xff))
1566 load_db_regs(svm->host_db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001568 vcpu->cr2 = svm->vmcb->save.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001570 write_dr6(svm->host_dr6);
1571 write_dr7(svm->host_dr7);
1572 kvm_write_cr2(svm->host_cr2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573
1574 load_fs(fs_selector);
1575 load_gs(gs_selector);
1576 load_ldt(ldt_selector);
1577 load_host_msrs(vcpu);
1578
1579 reload_tss(vcpu);
1580
Avi Kivity56ba47d2007-11-07 17:14:18 +02001581 local_irq_disable();
1582
1583 stgi();
1584
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001585 svm->next_rip = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586}
1587
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
1589{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001590 struct vcpu_svm *svm = to_svm(vcpu);
1591
1592 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593 force_new_asid(vcpu);
Anthony Liguori7807fa62007-04-23 09:17:21 -05001594
1595 if (vcpu->fpu_active) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001596 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
1597 svm->vmcb->save.cr0 |= X86_CR0_TS;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001598 vcpu->fpu_active = 0;
1599 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600}
1601
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602static int is_disabled(void)
1603{
Joerg Roedel6031a612007-06-22 12:29:50 +03001604 u64 vm_cr;
1605
1606 rdmsrl(MSR_VM_CR, vm_cr);
1607 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
1608 return 1;
1609
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610 return 0;
1611}
1612
Ingo Molnar102d8322007-02-19 14:37:47 +02001613static void
1614svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1615{
1616 /*
1617 * Patch in the VMMCALL instruction:
1618 */
1619 hypercall[0] = 0x0f;
1620 hypercall[1] = 0x01;
1621 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02001622}
1623
Yang, Sheng002c7f72007-07-31 14:23:01 +03001624static void svm_check_processor_compat(void *rtn)
1625{
1626 *(int *)rtn = 0;
1627}
1628
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001629static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630 .cpu_has_kvm_support = has_svm,
1631 .disabled_by_bios = is_disabled,
1632 .hardware_setup = svm_hardware_setup,
1633 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001634 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635 .hardware_enable = svm_hardware_enable,
1636 .hardware_disable = svm_hardware_disable,
1637
1638 .vcpu_create = svm_create_vcpu,
1639 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001640 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001641
Avi Kivity04d2cc72007-09-10 18:10:54 +03001642 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643 .vcpu_load = svm_vcpu_load,
1644 .vcpu_put = svm_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08001645 .vcpu_decache = svm_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646
1647 .set_guest_debug = svm_guest_debug,
1648 .get_msr = svm_get_msr,
1649 .set_msr = svm_set_msr,
1650 .get_segment_base = svm_get_segment_base,
1651 .get_segment = svm_get_segment,
1652 .set_segment = svm_set_segment,
Rusty Russell1747fb72007-09-06 01:21:32 +10001653 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03001654 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656 .set_cr3 = svm_set_cr3,
1657 .set_cr4 = svm_set_cr4,
1658 .set_efer = svm_set_efer,
1659 .get_idt = svm_get_idt,
1660 .set_idt = svm_set_idt,
1661 .get_gdt = svm_get_gdt,
1662 .set_gdt = svm_set_gdt,
1663 .get_dr = svm_get_dr,
1664 .set_dr = svm_set_dr,
1665 .cache_regs = svm_cache_regs,
1666 .decache_regs = svm_decache_regs,
1667 .get_rflags = svm_get_rflags,
1668 .set_rflags = svm_set_rflags,
1669
Avi Kivity6aa8b732006-12-10 02:21:36 -08001670 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001673 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02001675 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03001676 .get_irq = svm_get_irq,
1677 .set_irq = svm_set_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02001678 .queue_exception = svm_queue_exception,
1679 .exception_injected = svm_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001680 .inject_pending_irq = svm_intr_assist,
1681 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02001682
1683 .set_tss_addr = svm_set_tss_addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684};
1685
1686static int __init svm_init(void)
1687{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001688 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10001689 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690}
1691
1692static void __exit svm_exit(void)
1693{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001694 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695}
1696
1697module_init(svm_init)
1698module_exit(svm_exit)