blob: 87072c647f286207c45e047630a06a91ec8cd3be [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 void svm_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
211{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400212 struct vcpu_svm *svm = to_svm(vcpu);
213
214 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215 SVM_EVTINJ_VALID_ERR |
216 SVM_EVTINJ_TYPE_EXEPT |
217 GP_VECTOR;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400218 svm->vmcb->control.event_inj_err = error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800219}
220
221static void inject_ud(struct kvm_vcpu *vcpu)
222{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400223 to_svm(vcpu)->vmcb->control.event_inj = SVM_EVTINJ_VALID |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224 SVM_EVTINJ_TYPE_EXEPT |
225 UD_VECTOR;
226}
227
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228static int is_page_fault(uint32_t info)
229{
230 info &= SVM_EVTINJ_VEC_MASK | SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
231 return info == (PF_VECTOR | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_EXEPT);
232}
233
234static int is_external_interrupt(u32 info)
235{
236 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
237 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
238}
239
240static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
241{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400242 struct vcpu_svm *svm = to_svm(vcpu);
243
244 if (!svm->next_rip) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245 printk(KERN_DEBUG "%s: NOP\n", __FUNCTION__);
246 return;
247 }
Mike Dayd77c26f2007-10-08 09:02:08 -0400248 if (svm->next_rip - svm->vmcb->save.rip > MAX_INST_SIZE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249 printk(KERN_ERR "%s: ip 0x%llx next 0x%llx\n",
250 __FUNCTION__,
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400251 svm->vmcb->save.rip,
252 svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800253
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400254 vcpu->rip = svm->vmcb->save.rip = svm->next_rip;
255 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -0800256
257 vcpu->interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258}
259
260static int has_svm(void)
261{
262 uint32_t eax, ebx, ecx, edx;
263
Avi Kivity1e885462006-12-29 16:49:34 -0800264 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265 printk(KERN_INFO "has_svm: not amd\n");
266 return 0;
267 }
268
269 cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
270 if (eax < SVM_CPUID_FUNC) {
271 printk(KERN_INFO "has_svm: can't execute cpuid_8000000a\n");
272 return 0;
273 }
274
275 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
276 if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) {
277 printk(KERN_DEBUG "has_svm: svm not available\n");
278 return 0;
279 }
280 return 1;
281}
282
283static void svm_hardware_disable(void *garbage)
284{
285 struct svm_cpu_data *svm_data
286 = per_cpu(svm_data, raw_smp_processor_id());
287
288 if (svm_data) {
289 uint64_t efer;
290
291 wrmsrl(MSR_VM_HSAVE_PA, 0);
292 rdmsrl(MSR_EFER, efer);
293 wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK);
Al Viro8b6d44c2007-02-09 16:38:40 +0000294 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295 __free_page(svm_data->save_area);
296 kfree(svm_data);
297 }
298}
299
300static void svm_hardware_enable(void *garbage)
301{
302
303 struct svm_cpu_data *svm_data;
304 uint64_t efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800305#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306 struct desc_ptr gdt_descr;
307#else
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100308 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800309#endif
310 struct desc_struct *gdt;
311 int me = raw_smp_processor_id();
312
313 if (!has_svm()) {
314 printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
315 return;
316 }
317 svm_data = per_cpu(svm_data, me);
318
319 if (!svm_data) {
320 printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
321 me);
322 return;
323 }
324
325 svm_data->asid_generation = 1;
326 svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
327 svm_data->next_asid = svm_data->max_asid + 1;
Joerg Roedel80b77062007-03-30 17:02:14 +0300328 svm_features = cpuid_edx(SVM_CPUID_FUNC);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800329
Mike Dayd77c26f2007-10-08 09:02:08 -0400330 asm volatile ("sgdt %0" : "=m"(gdt_descr));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331 gdt = (struct desc_struct *)gdt_descr.address;
332 svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
333
334 rdmsrl(MSR_EFER, efer);
335 wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK);
336
337 wrmsrl(MSR_VM_HSAVE_PA,
338 page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
339}
340
341static int svm_cpu_init(int cpu)
342{
343 struct svm_cpu_data *svm_data;
344 int r;
345
346 svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
347 if (!svm_data)
348 return -ENOMEM;
349 svm_data->cpu = cpu;
350 svm_data->save_area = alloc_page(GFP_KERNEL);
351 r = -ENOMEM;
352 if (!svm_data->save_area)
353 goto err_1;
354
355 per_cpu(svm_data, cpu) = svm_data;
356
357 return 0;
358
359err_1:
360 kfree(svm_data);
361 return r;
362
363}
364
Rusty Russellbfc733a2007-07-31 20:42:42 +1000365static void set_msr_interception(u32 *msrpm, unsigned msr,
366 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367{
368 int i;
369
370 for (i = 0; i < NUM_MSR_MAPS; i++) {
371 if (msr >= msrpm_ranges[i] &&
372 msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
373 u32 msr_offset = (i * MSRS_IN_RANGE + msr -
374 msrpm_ranges[i]) * 2;
375
376 u32 *base = msrpm + (msr_offset / 32);
377 u32 msr_shift = msr_offset % 32;
378 u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
379 *base = (*base & ~(0x3 << msr_shift)) |
380 (mask << msr_shift);
Rusty Russellbfc733a2007-07-31 20:42:42 +1000381 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800382 }
383 }
Rusty Russellbfc733a2007-07-31 20:42:42 +1000384 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385}
386
387static __init int svm_hardware_setup(void)
388{
389 int cpu;
390 struct page *iopm_pages;
391 struct page *msrpm_pages;
Anthony Liguoric8681332007-04-30 09:48:11 +0300392 void *iopm_va, *msrpm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393 int r;
394
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
396
397 if (!iopm_pages)
398 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300399
400 iopm_va = page_address(iopm_pages);
401 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
402 clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
404
405
406 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
407
408 r = -ENOMEM;
409 if (!msrpm_pages)
410 goto err_1;
411
412 msrpm_va = page_address(msrpm_pages);
413 memset(msrpm_va, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
414 msrpm_base = page_to_pfn(msrpm_pages) << PAGE_SHIFT;
415
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800416#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417 set_msr_interception(msrpm_va, MSR_GS_BASE, 1, 1);
418 set_msr_interception(msrpm_va, MSR_FS_BASE, 1, 1);
419 set_msr_interception(msrpm_va, MSR_KERNEL_GS_BASE, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420 set_msr_interception(msrpm_va, MSR_LSTAR, 1, 1);
421 set_msr_interception(msrpm_va, MSR_CSTAR, 1, 1);
422 set_msr_interception(msrpm_va, MSR_SYSCALL_MASK, 1, 1);
423#endif
Avi Kivity0e859ca2006-12-22 01:05:08 -0800424 set_msr_interception(msrpm_va, MSR_K6_STAR, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_CS, 1, 1);
426 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_ESP, 1, 1);
427 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_EIP, 1, 1);
428
429 for_each_online_cpu(cpu) {
430 r = svm_cpu_init(cpu);
431 if (r)
432 goto err_2;
433 }
434 return 0;
435
436err_2:
437 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
438 msrpm_base = 0;
439err_1:
440 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
441 iopm_base = 0;
442 return r;
443}
444
445static __exit void svm_hardware_unsetup(void)
446{
447 __free_pages(pfn_to_page(msrpm_base >> PAGE_SHIFT), MSRPM_ALLOC_ORDER);
448 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
449 iopm_base = msrpm_base = 0;
450}
451
452static void init_seg(struct vmcb_seg *seg)
453{
454 seg->selector = 0;
455 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
456 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
457 seg->limit = 0xffff;
458 seg->base = 0;
459}
460
461static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
462{
463 seg->selector = 0;
464 seg->attrib = SVM_SELECTOR_P_MASK | type;
465 seg->limit = 0xffff;
466 seg->base = 0;
467}
468
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469static void init_vmcb(struct vmcb *vmcb)
470{
471 struct vmcb_control_area *control = &vmcb->control;
472 struct vmcb_save_area *save = &vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473
474 control->intercept_cr_read = INTERCEPT_CR0_MASK |
475 INTERCEPT_CR3_MASK |
476 INTERCEPT_CR4_MASK;
477
478 control->intercept_cr_write = INTERCEPT_CR0_MASK |
479 INTERCEPT_CR3_MASK |
480 INTERCEPT_CR4_MASK;
481
482 control->intercept_dr_read = INTERCEPT_DR0_MASK |
483 INTERCEPT_DR1_MASK |
484 INTERCEPT_DR2_MASK |
485 INTERCEPT_DR3_MASK;
486
487 control->intercept_dr_write = INTERCEPT_DR0_MASK |
488 INTERCEPT_DR1_MASK |
489 INTERCEPT_DR2_MASK |
490 INTERCEPT_DR3_MASK |
491 INTERCEPT_DR5_MASK |
492 INTERCEPT_DR7_MASK;
493
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500494 control->intercept_exceptions = (1 << PF_VECTOR) |
495 (1 << UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496
497
498 control->intercept = (1ULL << INTERCEPT_INTR) |
499 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200500 (1ULL << INTERCEPT_SMI) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501 /*
502 * selective cr0 intercept bug?
503 * 0: 0f 22 d8 mov %eax,%cr3
504 * 3: 0f 20 c0 mov %cr0,%eax
505 * 6: 0d 00 00 00 80 or $0x80000000,%eax
506 * b: 0f 22 c0 mov %eax,%cr0
507 * set cr3 ->interception
508 * get cr0 ->interception
509 * set cr0 -> no interception
510 */
511 /* (1ULL << INTERCEPT_SELECTIVE_CR0) | */
512 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200513 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514 (1ULL << INTERCEPT_HLT) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515 (1ULL << INTERCEPT_INVLPGA) |
516 (1ULL << INTERCEPT_IOIO_PROT) |
517 (1ULL << INTERCEPT_MSR_PROT) |
518 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800519 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520 (1ULL << INTERCEPT_VMRUN) |
521 (1ULL << INTERCEPT_VMMCALL) |
522 (1ULL << INTERCEPT_VMLOAD) |
523 (1ULL << INTERCEPT_VMSAVE) |
524 (1ULL << INTERCEPT_STGI) |
525 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100526 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200527 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100528 (1ULL << INTERCEPT_MONITOR) |
529 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530
531 control->iopm_base_pa = iopm_base;
532 control->msrpm_base_pa = msrpm_base;
Avi Kivity0cc50642007-03-25 12:07:27 +0200533 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534 control->int_ctl = V_INTR_MASKING_MASK;
535
536 init_seg(&save->es);
537 init_seg(&save->ss);
538 init_seg(&save->ds);
539 init_seg(&save->fs);
540 init_seg(&save->gs);
541
542 save->cs.selector = 0xf000;
543 /* Executable/Readable Code Segment */
544 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
545 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
546 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800547 /*
548 * cs.base should really be 0xffff0000, but vmx can't handle that, so
549 * be consistent with it.
550 *
551 * Replace when we have real mode working for vmx.
552 */
553 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800554
555 save->gdtr.limit = 0xffff;
556 save->idtr.limit = 0xffff;
557
558 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
559 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
560
561 save->efer = MSR_EFER_SVME_MASK;
Mike Dayd77c26f2007-10-08 09:02:08 -0400562 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 save->dr7 = 0x400;
564 save->rflags = 2;
565 save->rip = 0x0000fff0;
566
567 /*
568 * cr0 val on cpu init should be 0x60000010, we enable cpu
569 * cache by default. the orderly way is to enable cache in bios.
570 */
Rusty Russell707d92fa2007-07-17 23:19:08 +1000571 save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
Rusty Russell66aee912007-07-17 23:34:16 +1000572 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 /* rdx = ?? */
574}
575
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200576static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300577{
578 struct vcpu_svm *svm = to_svm(vcpu);
579
580 init_vmcb(svm->vmcb);
Avi Kivity70433382007-11-07 12:57:23 +0200581
582 if (vcpu->vcpu_id != 0) {
583 svm->vmcb->save.rip = 0;
584 svm->vmcb->save.cs.base = svm->vcpu.sipi_vector << 12;
585 svm->vmcb->save.cs.selector = svm->vcpu.sipi_vector << 8;
586 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200587
588 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300589}
590
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000591static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400593 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594 struct page *page;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000595 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596
Rusty Russellc16f8622007-07-30 21:12:19 +1000597 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000598 if (!svm) {
599 err = -ENOMEM;
600 goto out;
601 }
602
603 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
604 if (err)
605 goto free_svm;
606
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607 page = alloc_page(GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000608 if (!page) {
609 err = -ENOMEM;
610 goto uninit;
611 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400613 svm->vmcb = page_address(page);
614 clear_page(svm->vmcb);
615 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
616 svm->asid_generation = 0;
617 memset(svm->db_regs, 0, sizeof(svm->db_regs));
618 init_vmcb(svm->vmcb);
619
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000620 fx_init(&svm->vcpu);
621 svm->vcpu.fpu_active = 1;
622 svm->vcpu.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
623 if (svm->vcpu.vcpu_id == 0)
624 svm->vcpu.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000626 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800627
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000628uninit:
629 kvm_vcpu_uninit(&svm->vcpu);
630free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000631 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000632out:
633 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634}
635
636static void svm_free_vcpu(struct kvm_vcpu *vcpu)
637{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400638 struct vcpu_svm *svm = to_svm(vcpu);
639
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000640 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
641 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000642 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643}
644
Avi Kivity15ad7142007-07-11 18:17:21 +0300645static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400647 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300648 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200649
Avi Kivity0cc50642007-03-25 12:07:27 +0200650 if (unlikely(cpu != vcpu->cpu)) {
651 u64 tsc_this, delta;
652
653 /*
654 * Make sure that the guest sees a monotonically
655 * increasing TSC.
656 */
657 rdtscll(tsc_this);
658 delta = vcpu->host_tsc - tsc_this;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400659 svm->vmcb->control.tsc_offset += delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200660 vcpu->cpu = cpu;
Eddie Donga3d7f852007-09-03 16:15:12 +0300661 kvm_migrate_apic_timer(vcpu);
Avi Kivity0cc50642007-03-25 12:07:27 +0200662 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300663
664 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400665 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666}
667
668static void svm_vcpu_put(struct kvm_vcpu *vcpu)
669{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400670 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300671 int i;
672
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200673 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300674 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400675 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300676
Avi Kivity0cc50642007-03-25 12:07:27 +0200677 rdtscll(vcpu->host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678}
679
Avi Kivity774c47f2007-02-12 00:54:47 -0800680static void svm_vcpu_decache(struct kvm_vcpu *vcpu)
681{
682}
683
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684static void svm_cache_regs(struct kvm_vcpu *vcpu)
685{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400686 struct vcpu_svm *svm = to_svm(vcpu);
687
688 vcpu->regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
689 vcpu->regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
690 vcpu->rip = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691}
692
693static void svm_decache_regs(struct kvm_vcpu *vcpu)
694{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400695 struct vcpu_svm *svm = to_svm(vcpu);
696 svm->vmcb->save.rax = vcpu->regs[VCPU_REGS_RAX];
697 svm->vmcb->save.rsp = vcpu->regs[VCPU_REGS_RSP];
698 svm->vmcb->save.rip = vcpu->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699}
700
701static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
702{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400703 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704}
705
706static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
707{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400708 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709}
710
711static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
712{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400713 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714
715 switch (seg) {
716 case VCPU_SREG_CS: return &save->cs;
717 case VCPU_SREG_DS: return &save->ds;
718 case VCPU_SREG_ES: return &save->es;
719 case VCPU_SREG_FS: return &save->fs;
720 case VCPU_SREG_GS: return &save->gs;
721 case VCPU_SREG_SS: return &save->ss;
722 case VCPU_SREG_TR: return &save->tr;
723 case VCPU_SREG_LDTR: return &save->ldtr;
724 }
725 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000726 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800727}
728
729static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
730{
731 struct vmcb_seg *s = svm_seg(vcpu, seg);
732
733 return s->base;
734}
735
736static void svm_get_segment(struct kvm_vcpu *vcpu,
737 struct kvm_segment *var, int seg)
738{
739 struct vmcb_seg *s = svm_seg(vcpu, seg);
740
741 var->base = s->base;
742 var->limit = s->limit;
743 var->selector = s->selector;
744 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
745 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
746 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
747 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
748 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
749 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
750 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
751 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
752 var->unusable = !var->present;
753}
754
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755static void svm_get_idt(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 dt->limit = svm->vmcb->save.idtr.limit;
760 dt->base = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761}
762
763static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
764{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400765 struct vcpu_svm *svm = to_svm(vcpu);
766
767 svm->vmcb->save.idtr.limit = dt->limit;
768 svm->vmcb->save.idtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769}
770
771static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
772{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400773 struct vcpu_svm *svm = to_svm(vcpu);
774
775 dt->limit = svm->vmcb->save.gdtr.limit;
776 dt->base = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777}
778
779static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
780{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400781 struct vcpu_svm *svm = to_svm(vcpu);
782
783 svm->vmcb->save.gdtr.limit = dt->limit;
784 svm->vmcb->save.gdtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785}
786
Anthony Liguori25c4c272007-04-27 09:29:21 +0300787static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -0800788{
789}
790
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
792{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400793 struct vcpu_svm *svm = to_svm(vcpu);
794
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800795#ifdef CONFIG_X86_64
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600796 if (vcpu->shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +1000797 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600798 vcpu->shadow_efer |= EFER_LMA;
799 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800 }
801
Mike Dayd77c26f2007-10-08 09:02:08 -0400802 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600803 vcpu->shadow_efer &= ~EFER_LMA;
804 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805 }
806 }
807#endif
Rusty Russell707d92fa2007-07-17 23:19:08 +1000808 if ((vcpu->cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400809 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Anthony Liguori7807fa62007-04-23 09:17:21 -0500810 vcpu->fpu_active = 1;
811 }
812
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813 vcpu->cr0 = cr0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000814 cr0 |= X86_CR0_PG | X86_CR0_WP;
815 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400816 svm->vmcb->save.cr0 = cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817}
818
819static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
820{
821 vcpu->cr4 = cr4;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400822 to_svm(vcpu)->vmcb->save.cr4 = cr4 | X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823}
824
825static void svm_set_segment(struct kvm_vcpu *vcpu,
826 struct kvm_segment *var, int seg)
827{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400828 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 struct vmcb_seg *s = svm_seg(vcpu, seg);
830
831 s->base = var->base;
832 s->limit = var->limit;
833 s->selector = var->selector;
834 if (var->unusable)
835 s->attrib = 0;
836 else {
837 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
838 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
839 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
840 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
841 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
842 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
843 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
844 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
845 }
846 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400847 svm->vmcb->save.cpl
848 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 >> SVM_SELECTOR_DPL_SHIFT) & 3;
850
851}
852
853/* FIXME:
854
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400855 svm(vcpu)->vmcb->control.int_ctl &= ~V_TPR_MASK;
856 svm(vcpu)->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857
858*/
859
860static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
861{
862 return -EOPNOTSUPP;
863}
864
Eddie Dong2a8067f2007-08-06 16:29:07 +0300865static int svm_get_irq(struct kvm_vcpu *vcpu)
866{
867 struct vcpu_svm *svm = to_svm(vcpu);
868 u32 exit_int_info = svm->vmcb->control.exit_int_info;
869
870 if (is_external_interrupt(exit_int_info))
871 return exit_int_info & SVM_EVTINJ_VEC_MASK;
872 return -1;
873}
874
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875static void load_host_msrs(struct kvm_vcpu *vcpu)
876{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300877#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400878 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300879#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880}
881
882static void save_host_msrs(struct kvm_vcpu *vcpu)
883{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300884#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400885 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300886#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887}
888
Rusty Russelle756fc62007-07-30 20:07:08 +1000889static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890{
891 if (svm_data->next_asid > svm_data->max_asid) {
892 ++svm_data->asid_generation;
893 svm_data->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400894 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 }
896
Rusty Russelle756fc62007-07-30 20:07:08 +1000897 svm->vcpu.cpu = svm_data->cpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400898 svm->asid_generation = svm_data->asid_generation;
899 svm->vmcb->control.asid = svm_data->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900}
901
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
903{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400904 return to_svm(vcpu)->db_regs[dr];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905}
906
907static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
908 int *exception)
909{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400910 struct vcpu_svm *svm = to_svm(vcpu);
911
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 *exception = 0;
913
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400914 if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
915 svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
916 svm->vmcb->save.dr6 |= DR6_BD_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 *exception = DB_VECTOR;
918 return;
919 }
920
921 switch (dr) {
922 case 0 ... 3:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400923 svm->db_regs[dr] = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924 return;
925 case 4 ... 5:
Rusty Russell66aee912007-07-17 23:34:16 +1000926 if (vcpu->cr4 & X86_CR4_DE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 *exception = UD_VECTOR;
928 return;
929 }
930 case 7: {
931 if (value & ~((1ULL << 32) - 1)) {
932 *exception = GP_VECTOR;
933 return;
934 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400935 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 return;
937 }
938 default:
939 printk(KERN_DEBUG "%s: unexpected dr %u\n",
940 __FUNCTION__, dr);
941 *exception = UD_VECTOR;
942 return;
943 }
944}
945
Rusty Russelle756fc62007-07-30 20:07:08 +1000946static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400948 u32 exit_int_info = svm->vmcb->control.exit_int_info;
Rusty Russelle756fc62007-07-30 20:07:08 +1000949 struct kvm *kvm = svm->vcpu.kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 u64 fault_address;
951 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952
Eddie Dong85f455f2007-07-06 12:20:49 +0300953 if (!irqchip_in_kernel(kvm) &&
954 is_external_interrupt(exit_int_info))
Rusty Russelle756fc62007-07-30 20:07:08 +1000955 push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400957 fault_address = svm->vmcb->control.exit_info_2;
958 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity30677142007-10-28 18:48:59 +0200959 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960}
961
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500962static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
963{
964 int er;
965
Laurent Vivier34273182007-09-18 11:27:37 +0200966 er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500967 if (er != EMULATE_DONE)
968 inject_ud(&svm->vcpu);
969
970 return 1;
971}
972
Rusty Russelle756fc62007-07-30 20:07:08 +1000973static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Anthony Liguori7807fa62007-04-23 09:17:21 -0500974{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400975 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Rusty Russelle756fc62007-07-30 20:07:08 +1000976 if (!(svm->vcpu.cr0 & X86_CR0_TS))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400977 svm->vmcb->save.cr0 &= ~X86_CR0_TS;
Rusty Russelle756fc62007-07-30 20:07:08 +1000978 svm->vcpu.fpu_active = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400979
980 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -0500981}
982
Rusty Russelle756fc62007-07-30 20:07:08 +1000983static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800984{
985 /*
986 * VMCB is undefined after a SHUTDOWN intercept
987 * so reinitialize it.
988 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400989 clear_page(svm->vmcb);
990 init_vmcb(svm->vmcb);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800991
992 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
993 return 0;
994}
995
Rusty Russelle756fc62007-07-30 20:07:08 +1000996static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997{
Mike Dayd77c26f2007-10-08 09:02:08 -0400998 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Avi Kivity039576c2007-03-20 12:46:50 +0200999 int size, down, in, string, rep;
1000 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001
Rusty Russelle756fc62007-07-30 20:07:08 +10001002 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001004 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005
Laurent Viviere70669a2007-08-05 10:36:40 +03001006 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1007
1008 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001009 if (emulate_instruction(&svm->vcpu,
1010 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001011 return 0;
1012 return 1;
1013 }
1014
Avi Kivity039576c2007-03-20 12:46:50 +02001015 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1016 port = io_info >> 16;
1017 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity039576c2007-03-20 12:46:50 +02001018 rep = (io_info & SVM_IOIO_REP_MASK) != 0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001019 down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020
Laurent Vivier3090dd72007-08-05 10:43:32 +03001021 return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022}
1023
Rusty Russelle756fc62007-07-30 20:07:08 +10001024static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025{
1026 return 1;
1027}
1028
Rusty Russelle756fc62007-07-30 20:07:08 +10001029static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001031 svm->next_rip = svm->vmcb->save.rip + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001032 skip_emulated_instruction(&svm->vcpu);
1033 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034}
1035
Rusty Russelle756fc62007-07-30 20:07:08 +10001036static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity02e235b2007-02-19 14:37:47 +02001037{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001038 svm->next_rip = svm->vmcb->save.rip + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001039 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001040 kvm_emulate_hypercall(&svm->vcpu);
1041 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001042}
1043
Rusty Russelle756fc62007-07-30 20:07:08 +10001044static int invalid_op_interception(struct vcpu_svm *svm,
1045 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046{
Rusty Russelle756fc62007-07-30 20:07:08 +10001047 inject_ud(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048 return 1;
1049}
1050
Rusty Russelle756fc62007-07-30 20:07:08 +10001051static int task_switch_interception(struct vcpu_svm *svm,
1052 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053{
Rusty Russellf0242472007-08-01 10:48:02 +10001054 pr_unimpl(&svm->vcpu, "%s: task switch is unsupported\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
1056 return 0;
1057}
1058
Rusty Russelle756fc62007-07-30 20:07:08 +10001059static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001061 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001062 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001063 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064}
1065
Rusty Russelle756fc62007-07-30 20:07:08 +10001066static int emulate_on_interception(struct vcpu_svm *svm,
1067 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068{
Laurent Vivier34273182007-09-18 11:27:37 +02001069 if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
Rusty Russellf0242472007-08-01 10:48:02 +10001070 pr_unimpl(&svm->vcpu, "%s: failed\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071 return 1;
1072}
1073
1074static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
1075{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001076 struct vcpu_svm *svm = to_svm(vcpu);
1077
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079 case MSR_IA32_TIME_STAMP_COUNTER: {
1080 u64 tsc;
1081
1082 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001083 *data = svm->vmcb->control.tsc_offset + tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084 break;
1085 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001086 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001087 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08001089#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001091 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 break;
1093 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001094 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095 break;
1096 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001097 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098 break;
1099 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001100 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101 break;
1102#endif
1103 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001104 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 break;
1106 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001107 *data = svm->vmcb->save.sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 break;
1109 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001110 *data = svm->vmcb->save.sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 break;
1112 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001113 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114 }
1115 return 0;
1116}
1117
Rusty Russelle756fc62007-07-30 20:07:08 +10001118static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119{
Rusty Russelle756fc62007-07-30 20:07:08 +10001120 u32 ecx = svm->vcpu.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121 u64 data;
1122
Rusty Russelle756fc62007-07-30 20:07:08 +10001123 if (svm_get_msr(&svm->vcpu, ecx, &data))
1124 svm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 else {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001126 svm->vmcb->save.rax = data & 0xffffffff;
Rusty Russelle756fc62007-07-30 20:07:08 +10001127 svm->vcpu.regs[VCPU_REGS_RDX] = data >> 32;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001128 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001129 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130 }
1131 return 1;
1132}
1133
1134static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
1135{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001136 struct vcpu_svm *svm = to_svm(vcpu);
1137
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 case MSR_IA32_TIME_STAMP_COUNTER: {
1140 u64 tsc;
1141
1142 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001143 svm->vmcb->control.tsc_offset = data - tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144 break;
1145 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001146 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001147 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08001149#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001151 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152 break;
1153 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001154 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155 break;
1156 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001157 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158 break;
1159 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001160 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161 break;
1162#endif
1163 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001164 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165 break;
1166 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001167 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168 break;
1169 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001170 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171 break;
1172 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001173 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001174 }
1175 return 0;
1176}
1177
Rusty Russelle756fc62007-07-30 20:07:08 +10001178static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179{
Rusty Russelle756fc62007-07-30 20:07:08 +10001180 u32 ecx = svm->vcpu.regs[VCPU_REGS_RCX];
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001181 u64 data = (svm->vmcb->save.rax & -1u)
Rusty Russelle756fc62007-07-30 20:07:08 +10001182 | ((u64)(svm->vcpu.regs[VCPU_REGS_RDX] & -1u) << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001183 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001184 if (svm_set_msr(&svm->vcpu, ecx, data))
1185 svm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001187 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 return 1;
1189}
1190
Rusty Russelle756fc62007-07-30 20:07:08 +10001191static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192{
Rusty Russelle756fc62007-07-30 20:07:08 +10001193 if (svm->vmcb->control.exit_info_1)
1194 return wrmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001196 return rdmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197}
1198
Rusty Russelle756fc62007-07-30 20:07:08 +10001199static int interrupt_window_interception(struct vcpu_svm *svm,
Dor Laorc1150d82007-01-05 16:36:24 -08001200 struct kvm_run *kvm_run)
1201{
Eddie Dong85f455f2007-07-06 12:20:49 +03001202 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1203 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08001204 /*
1205 * If the user space waits to inject interrupts, exit as soon as
1206 * possible
1207 */
1208 if (kvm_run->request_interrupt_window &&
Rusty Russelle756fc62007-07-30 20:07:08 +10001209 !svm->vcpu.irq_summary) {
1210 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08001211 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
1212 return 0;
1213 }
1214
1215 return 1;
1216}
1217
Rusty Russelle756fc62007-07-30 20:07:08 +10001218static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 struct kvm_run *kvm_run) = {
1220 [SVM_EXIT_READ_CR0] = emulate_on_interception,
1221 [SVM_EXIT_READ_CR3] = emulate_on_interception,
1222 [SVM_EXIT_READ_CR4] = emulate_on_interception,
1223 /* for now: */
1224 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
1225 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
1226 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
1227 [SVM_EXIT_READ_DR0] = emulate_on_interception,
1228 [SVM_EXIT_READ_DR1] = emulate_on_interception,
1229 [SVM_EXIT_READ_DR2] = emulate_on_interception,
1230 [SVM_EXIT_READ_DR3] = emulate_on_interception,
1231 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
1232 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
1233 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
1234 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
1235 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
1236 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001237 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Anthony Liguori7807fa62007-04-23 09:17:21 -05001239 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240 [SVM_EXIT_INTR] = nop_on_interception,
1241 [SVM_EXIT_NMI] = nop_on_interception,
1242 [SVM_EXIT_SMI] = nop_on_interception,
1243 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08001244 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
1246 [SVM_EXIT_CPUID] = cpuid_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001247 [SVM_EXIT_INVD] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248 [SVM_EXIT_HLT] = halt_interception,
1249 [SVM_EXIT_INVLPG] = emulate_on_interception,
1250 [SVM_EXIT_INVLPGA] = invalid_op_interception,
1251 [SVM_EXIT_IOIO] = io_interception,
1252 [SVM_EXIT_MSR] = msr_interception,
1253 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001254 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255 [SVM_EXIT_VMRUN] = invalid_op_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02001256 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257 [SVM_EXIT_VMLOAD] = invalid_op_interception,
1258 [SVM_EXIT_VMSAVE] = invalid_op_interception,
1259 [SVM_EXIT_STGI] = invalid_op_interception,
1260 [SVM_EXIT_CLGI] = invalid_op_interception,
1261 [SVM_EXIT_SKINIT] = invalid_op_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001262 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01001263 [SVM_EXIT_MONITOR] = invalid_op_interception,
1264 [SVM_EXIT_MWAIT] = invalid_op_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001265};
1266
1267
Avi Kivity04d2cc72007-09-10 18:10:54 +03001268static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001270 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001271 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272
Avi Kivity04d2cc72007-09-10 18:10:54 +03001273 kvm_reput_irq(svm);
1274
1275 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
1276 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
1277 kvm_run->fail_entry.hardware_entry_failure_reason
1278 = svm->vmcb->control.exit_code;
1279 return 0;
1280 }
1281
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001282 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR)
1284 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
1285 "exit_code 0x%x\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001286 __FUNCTION__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287 exit_code);
1288
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02001289 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08001290 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03001292 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293 return 0;
1294 }
1295
Rusty Russelle756fc62007-07-30 20:07:08 +10001296 return svm_exit_handlers[exit_code](svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297}
1298
1299static void reload_tss(struct kvm_vcpu *vcpu)
1300{
1301 int cpu = raw_smp_processor_id();
1302
1303 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04001304 svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305 load_TR_desc();
1306}
1307
Rusty Russelle756fc62007-07-30 20:07:08 +10001308static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309{
1310 int cpu = raw_smp_processor_id();
1311
1312 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
1313
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001314 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Rusty Russelle756fc62007-07-30 20:07:08 +10001315 if (svm->vcpu.cpu != cpu ||
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001316 svm->asid_generation != svm_data->asid_generation)
Rusty Russelle756fc62007-07-30 20:07:08 +10001317 new_asid(svm, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318}
1319
1320
Eddie Dong85f455f2007-07-06 12:20:49 +03001321static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322{
1323 struct vmcb_control_area *control;
1324
Rusty Russelle756fc62007-07-30 20:07:08 +10001325 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03001326 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327 control->int_ctl &= ~V_INTR_PRIO_MASK;
1328 control->int_ctl |= V_IRQ_MASK |
1329 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1330}
1331
Eddie Dong2a8067f2007-08-06 16:29:07 +03001332static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
1333{
1334 struct vcpu_svm *svm = to_svm(vcpu);
1335
1336 svm_inject_irq(svm, irq);
1337}
1338
Avi Kivity04d2cc72007-09-10 18:10:54 +03001339static void svm_intr_assist(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001341 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001342 struct vmcb *vmcb = svm->vmcb;
1343 int intr_vector = -1;
1344
1345 if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
1346 ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
1347 intr_vector = vmcb->control.exit_int_info &
1348 SVM_EVTINJ_VEC_MASK;
1349 vmcb->control.exit_int_info = 0;
1350 svm_inject_irq(svm, intr_vector);
1351 return;
1352 }
1353
1354 if (vmcb->control.int_ctl & V_IRQ_MASK)
1355 return;
1356
Eddie Dong1b9778d2007-09-03 16:56:58 +03001357 if (!kvm_cpu_has_interrupt(vcpu))
Eddie Dong85f455f2007-07-06 12:20:49 +03001358 return;
1359
1360 if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
1361 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
1362 (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
1363 /* unable to deliver irq, set pending irq */
1364 vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
1365 svm_inject_irq(svm, 0x0);
1366 return;
1367 }
1368 /* Okay, we can deliver the interrupt: grab it and update PIC state. */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001369 intr_vector = kvm_cpu_get_interrupt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001370 svm_inject_irq(svm, intr_vector);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001371 kvm_timer_intr_post(vcpu, intr_vector);
Eddie Dong85f455f2007-07-06 12:20:49 +03001372}
1373
1374static void kvm_reput_irq(struct vcpu_svm *svm)
1375{
Rusty Russelle756fc62007-07-30 20:07:08 +10001376 struct vmcb_control_area *control = &svm->vmcb->control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377
Eddie Dong7017fc32007-07-18 11:34:57 +03001378 if ((control->int_ctl & V_IRQ_MASK)
1379 && !irqchip_in_kernel(svm->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380 control->int_ctl &= ~V_IRQ_MASK;
Rusty Russelle756fc62007-07-30 20:07:08 +10001381 push_irq(&svm->vcpu, control->int_vector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382 }
Dor Laorc1150d82007-01-05 16:36:24 -08001383
Rusty Russelle756fc62007-07-30 20:07:08 +10001384 svm->vcpu.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001385 !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
1386}
1387
Eddie Dong85f455f2007-07-06 12:20:49 +03001388static void svm_do_inject_vector(struct vcpu_svm *svm)
1389{
1390 struct kvm_vcpu *vcpu = &svm->vcpu;
1391 int word_index = __ffs(vcpu->irq_summary);
1392 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1393 int irq = word_index * BITS_PER_LONG + bit_index;
1394
1395 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1396 if (!vcpu->irq_pending[word_index])
1397 clear_bit(word_index, &vcpu->irq_summary);
1398 svm_inject_irq(svm, irq);
1399}
1400
Avi Kivity04d2cc72007-09-10 18:10:54 +03001401static void do_interrupt_requests(struct kvm_vcpu *vcpu,
Dor Laorc1150d82007-01-05 16:36:24 -08001402 struct kvm_run *kvm_run)
1403{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001404 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001405 struct vmcb_control_area *control = &svm->vmcb->control;
Dor Laorc1150d82007-01-05 16:36:24 -08001406
Rusty Russelle756fc62007-07-30 20:07:08 +10001407 svm->vcpu.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001408 (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001409 (svm->vmcb->save.rflags & X86_EFLAGS_IF));
Dor Laorc1150d82007-01-05 16:36:24 -08001410
Rusty Russelle756fc62007-07-30 20:07:08 +10001411 if (svm->vcpu.interrupt_window_open && svm->vcpu.irq_summary)
Dor Laorc1150d82007-01-05 16:36:24 -08001412 /*
1413 * If interrupts enabled, and not blocked by sti or mov ss. Good.
1414 */
Eddie Dong85f455f2007-07-06 12:20:49 +03001415 svm_do_inject_vector(svm);
Dor Laorc1150d82007-01-05 16:36:24 -08001416
1417 /*
1418 * Interrupts blocked. Wait for unblock.
1419 */
Rusty Russelle756fc62007-07-30 20:07:08 +10001420 if (!svm->vcpu.interrupt_window_open &&
Mike Dayd77c26f2007-10-08 09:02:08 -04001421 (svm->vcpu.irq_summary || kvm_run->request_interrupt_window))
Dor Laorc1150d82007-01-05 16:36:24 -08001422 control->intercept |= 1ULL << INTERCEPT_VINTR;
Mike Dayd77c26f2007-10-08 09:02:08 -04001423 else
Dor Laorc1150d82007-01-05 16:36:24 -08001424 control->intercept &= ~(1ULL << INTERCEPT_VINTR);
1425}
1426
Izik Eiduscbc94022007-10-25 00:29:55 +02001427static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
1428{
1429 return 0;
1430}
1431
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432static void save_db_regs(unsigned long *db_regs)
1433{
Avi Kivity5aff4582006-12-13 00:33:45 -08001434 asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
1435 asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
1436 asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
1437 asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438}
1439
1440static void load_db_regs(unsigned long *db_regs)
1441{
Avi Kivity5aff4582006-12-13 00:33:45 -08001442 asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
1443 asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
1444 asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
1445 asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446}
1447
Avi Kivityd9e368d2007-06-07 19:18:30 +03001448static void svm_flush_tlb(struct kvm_vcpu *vcpu)
1449{
1450 force_new_asid(vcpu);
1451}
1452
Avi Kivity04d2cc72007-09-10 18:10:54 +03001453static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
1454{
1455}
1456
1457static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001459 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460 u16 fs_selector;
1461 u16 gs_selector;
1462 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03001463
Rusty Russelle756fc62007-07-30 20:07:08 +10001464 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465
1466 save_host_msrs(vcpu);
1467 fs_selector = read_fs();
1468 gs_selector = read_gs();
1469 ldt_selector = read_ldt();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001470 svm->host_cr2 = kvm_read_cr2();
1471 svm->host_dr6 = read_dr6();
1472 svm->host_dr7 = read_dr7();
1473 svm->vmcb->save.cr2 = vcpu->cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001475 if (svm->vmcb->save.dr7 & 0xff) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476 write_dr7(0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001477 save_db_regs(svm->host_db_regs);
1478 load_db_regs(svm->db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479 }
Avi Kivity36241b82006-12-22 01:05:20 -08001480
Avi Kivity04d2cc72007-09-10 18:10:54 +03001481 clgi();
1482
1483 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08001484
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485 asm volatile (
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001486#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02001487 "push %%rbp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488#else
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001489 "push %%ebp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490#endif
1491
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001492#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001493 "mov %c[rbx](%[svm]), %%rbx \n\t"
1494 "mov %c[rcx](%[svm]), %%rcx \n\t"
1495 "mov %c[rdx](%[svm]), %%rdx \n\t"
1496 "mov %c[rsi](%[svm]), %%rsi \n\t"
1497 "mov %c[rdi](%[svm]), %%rdi \n\t"
1498 "mov %c[rbp](%[svm]), %%rbp \n\t"
1499 "mov %c[r8](%[svm]), %%r8 \n\t"
1500 "mov %c[r9](%[svm]), %%r9 \n\t"
1501 "mov %c[r10](%[svm]), %%r10 \n\t"
1502 "mov %c[r11](%[svm]), %%r11 \n\t"
1503 "mov %c[r12](%[svm]), %%r12 \n\t"
1504 "mov %c[r13](%[svm]), %%r13 \n\t"
1505 "mov %c[r14](%[svm]), %%r14 \n\t"
1506 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001508 "mov %c[rbx](%[svm]), %%ebx \n\t"
1509 "mov %c[rcx](%[svm]), %%ecx \n\t"
1510 "mov %c[rdx](%[svm]), %%edx \n\t"
1511 "mov %c[rsi](%[svm]), %%esi \n\t"
1512 "mov %c[rdi](%[svm]), %%edi \n\t"
1513 "mov %c[rbp](%[svm]), %%ebp \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514#endif
1515
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001516#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517 /* Enter guest mode */
1518 "push %%rax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001519 "mov %c[vmcb](%[svm]), %%rax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520 SVM_VMLOAD "\n\t"
1521 SVM_VMRUN "\n\t"
1522 SVM_VMSAVE "\n\t"
1523 "pop %%rax \n\t"
1524#else
1525 /* Enter guest mode */
1526 "push %%eax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001527 "mov %c[vmcb](%[svm]), %%eax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528 SVM_VMLOAD "\n\t"
1529 SVM_VMRUN "\n\t"
1530 SVM_VMSAVE "\n\t"
1531 "pop %%eax \n\t"
1532#endif
1533
1534 /* Save guest registers, load host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001535#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001536 "mov %%rbx, %c[rbx](%[svm]) \n\t"
1537 "mov %%rcx, %c[rcx](%[svm]) \n\t"
1538 "mov %%rdx, %c[rdx](%[svm]) \n\t"
1539 "mov %%rsi, %c[rsi](%[svm]) \n\t"
1540 "mov %%rdi, %c[rdi](%[svm]) \n\t"
1541 "mov %%rbp, %c[rbp](%[svm]) \n\t"
1542 "mov %%r8, %c[r8](%[svm]) \n\t"
1543 "mov %%r9, %c[r9](%[svm]) \n\t"
1544 "mov %%r10, %c[r10](%[svm]) \n\t"
1545 "mov %%r11, %c[r11](%[svm]) \n\t"
1546 "mov %%r12, %c[r12](%[svm]) \n\t"
1547 "mov %%r13, %c[r13](%[svm]) \n\t"
1548 "mov %%r14, %c[r14](%[svm]) \n\t"
1549 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550
Laurent Vivier54a08c02007-10-25 14:18:53 +02001551 "pop %%rbp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001552#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001553 "mov %%ebx, %c[rbx](%[svm]) \n\t"
1554 "mov %%ecx, %c[rcx](%[svm]) \n\t"
1555 "mov %%edx, %c[rdx](%[svm]) \n\t"
1556 "mov %%esi, %c[rsi](%[svm]) \n\t"
1557 "mov %%edi, %c[rdi](%[svm]) \n\t"
1558 "mov %%ebp, %c[rbp](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001560 "pop %%ebp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561#endif
1562 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001563 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08001564 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Mike Dayd77c26f2007-10-08 09:02:08 -04001565 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RBX])),
1566 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RCX])),
1567 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RDX])),
1568 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RSI])),
1569 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RDI])),
1570 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001571#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -04001572 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R8])),
1573 [r9]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R9])),
1574 [r10]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R10])),
1575 [r11]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R11])),
1576 [r12]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R12])),
1577 [r13]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R13])),
1578 [r14]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R14])),
1579 [r15]"i"(offsetof(struct vcpu_svm, vcpu.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02001581 : "cc", "memory"
1582#ifdef CONFIG_X86_64
1583 , "rbx", "rcx", "rdx", "rsi", "rdi"
1584 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001585#else
1586 , "ebx", "ecx", "edx" , "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02001587#endif
1588 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001590 if ((svm->vmcb->save.dr7 & 0xff))
1591 load_db_regs(svm->host_db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001593 vcpu->cr2 = svm->vmcb->save.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001595 write_dr6(svm->host_dr6);
1596 write_dr7(svm->host_dr7);
1597 kvm_write_cr2(svm->host_cr2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598
1599 load_fs(fs_selector);
1600 load_gs(gs_selector);
1601 load_ldt(ldt_selector);
1602 load_host_msrs(vcpu);
1603
1604 reload_tss(vcpu);
1605
Avi Kivity56ba47d2007-11-07 17:14:18 +02001606 local_irq_disable();
1607
1608 stgi();
1609
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001610 svm->next_rip = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611}
1612
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
1614{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001615 struct vcpu_svm *svm = to_svm(vcpu);
1616
1617 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618 force_new_asid(vcpu);
Anthony Liguori7807fa62007-04-23 09:17:21 -05001619
1620 if (vcpu->fpu_active) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001621 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
1622 svm->vmcb->save.cr0 |= X86_CR0_TS;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001623 vcpu->fpu_active = 0;
1624 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625}
1626
1627static void svm_inject_page_fault(struct kvm_vcpu *vcpu,
1628 unsigned long addr,
1629 uint32_t err_code)
1630{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001631 struct vcpu_svm *svm = to_svm(vcpu);
1632 uint32_t exit_int_info = svm->vmcb->control.exit_int_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633
Avi Kivity1165f5f2007-04-19 17:27:43 +03001634 ++vcpu->stat.pf_guest;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635
1636 if (is_page_fault(exit_int_info)) {
1637
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001638 svm->vmcb->control.event_inj_err = 0;
1639 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
1640 SVM_EVTINJ_VALID_ERR |
1641 SVM_EVTINJ_TYPE_EXEPT |
1642 DF_VECTOR;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643 return;
1644 }
1645 vcpu->cr2 = addr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001646 svm->vmcb->save.cr2 = addr;
1647 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
1648 SVM_EVTINJ_VALID_ERR |
1649 SVM_EVTINJ_TYPE_EXEPT |
1650 PF_VECTOR;
1651 svm->vmcb->control.event_inj_err = err_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652}
1653
1654
1655static int is_disabled(void)
1656{
Joerg Roedel6031a612007-06-22 12:29:50 +03001657 u64 vm_cr;
1658
1659 rdmsrl(MSR_VM_CR, vm_cr);
1660 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
1661 return 1;
1662
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663 return 0;
1664}
1665
Ingo Molnar102d8322007-02-19 14:37:47 +02001666static void
1667svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1668{
1669 /*
1670 * Patch in the VMMCALL instruction:
1671 */
1672 hypercall[0] = 0x0f;
1673 hypercall[1] = 0x01;
1674 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02001675}
1676
Yang, Sheng002c7f72007-07-31 14:23:01 +03001677static void svm_check_processor_compat(void *rtn)
1678{
1679 *(int *)rtn = 0;
1680}
1681
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001682static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683 .cpu_has_kvm_support = has_svm,
1684 .disabled_by_bios = is_disabled,
1685 .hardware_setup = svm_hardware_setup,
1686 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001687 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688 .hardware_enable = svm_hardware_enable,
1689 .hardware_disable = svm_hardware_disable,
1690
1691 .vcpu_create = svm_create_vcpu,
1692 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001693 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694
Avi Kivity04d2cc72007-09-10 18:10:54 +03001695 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696 .vcpu_load = svm_vcpu_load,
1697 .vcpu_put = svm_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08001698 .vcpu_decache = svm_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699
1700 .set_guest_debug = svm_guest_debug,
1701 .get_msr = svm_get_msr,
1702 .set_msr = svm_set_msr,
1703 .get_segment_base = svm_get_segment_base,
1704 .get_segment = svm_get_segment,
1705 .set_segment = svm_set_segment,
Rusty Russell1747fb72007-09-06 01:21:32 +10001706 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03001707 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709 .set_cr3 = svm_set_cr3,
1710 .set_cr4 = svm_set_cr4,
1711 .set_efer = svm_set_efer,
1712 .get_idt = svm_get_idt,
1713 .set_idt = svm_set_idt,
1714 .get_gdt = svm_get_gdt,
1715 .set_gdt = svm_set_gdt,
1716 .get_dr = svm_get_dr,
1717 .set_dr = svm_set_dr,
1718 .cache_regs = svm_cache_regs,
1719 .decache_regs = svm_decache_regs,
1720 .get_rflags = svm_get_rflags,
1721 .set_rflags = svm_set_rflags,
1722
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723 .tlb_flush = svm_flush_tlb,
1724 .inject_page_fault = svm_inject_page_fault,
1725
1726 .inject_gp = svm_inject_gp,
1727
1728 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001729 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001730 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02001731 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03001732 .get_irq = svm_get_irq,
1733 .set_irq = svm_set_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02001734 .queue_exception = svm_queue_exception,
1735 .exception_injected = svm_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001736 .inject_pending_irq = svm_intr_assist,
1737 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02001738
1739 .set_tss_addr = svm_set_tss_addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740};
1741
1742static int __init svm_init(void)
1743{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001744 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10001745 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746}
1747
1748static void __exit svm_exit(void)
1749{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001750 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001751}
1752
1753module_init(svm_init)
1754module_exit(svm_exit)