blob: 8e9d4a5dacda35101d79c5f1a4ae570f4734ac1d [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 */
Avi Kivityedf88412007-12-16 11:02:48 +020016#include <linux/kvm_host.h>
17
Avi Kivitye4956062007-06-28 14:15:57 -040018#include "kvm_svm.h"
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040021
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020023#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/vmalloc.h>
25#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027
Avi Kivitye4956062007-06-28 14:15:57 -040028#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029
30MODULE_AUTHOR("Qumranet");
31MODULE_LICENSE("GPL");
32
33#define IOPM_ALLOC_ORDER 2
34#define MSRPM_ALLOC_ORDER 1
35
36#define DB_VECTOR 1
37#define UD_VECTOR 6
38#define GP_VECTOR 13
39
40#define DR7_GD_MASK (1 << 13)
41#define DR6_BD_MASK (1 << 13)
Avi Kivity6aa8b732006-12-10 02:21:36 -080042
43#define SEG_TYPE_LDT 2
44#define SEG_TYPE_BUSY_TSS16 3
45
Joerg Roedel80b77062007-03-30 17:02:14 +030046#define SVM_FEATURE_NPT (1 << 0)
47#define SVM_FEATURE_LBRV (1 << 1)
48#define SVM_DEATURE_SVML (1 << 2)
49
Joerg Roedel709ddeb2008-02-07 13:47:45 +010050/* enable NPT for AMD64 and X86 with PAE */
51#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
52static bool npt_enabled = true;
53#else
Joerg Roedele3da3ac2008-02-07 13:47:39 +010054static bool npt_enabled = false;
Joerg Roedel709ddeb2008-02-07 13:47:45 +010055#endif
Joerg Roedel6c7dac72008-02-07 13:47:40 +010056static int npt = 1;
57
58module_param(npt, int, S_IRUGO);
Joerg Roedele3da3ac2008-02-07 13:47:39 +010059
Avi Kivity04d2cc72007-09-10 18:10:54 +030060static void kvm_reput_irq(struct vcpu_svm *svm);
61
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040062static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
63{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100064 return container_of(vcpu, struct vcpu_svm, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040065}
66
Avi Kivity6aa8b732006-12-10 02:21:36 -080067unsigned long iopm_base;
68unsigned long msrpm_base;
69
70struct kvm_ldttss_desc {
71 u16 limit0;
72 u16 base0;
73 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
74 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
75 u32 base3;
76 u32 zero1;
77} __attribute__((packed));
78
79struct svm_cpu_data {
80 int cpu;
81
Avi Kivity5008fdf2007-04-02 13:05:50 +030082 u64 asid_generation;
83 u32 max_asid;
84 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -080085 struct kvm_ldttss_desc *tss_desc;
86
87 struct page *save_area;
88};
89
90static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +030091static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -080092
93struct svm_init_data {
94 int cpu;
95 int r;
96};
97
98static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
99
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200100#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101#define MSRS_RANGE_SIZE 2048
102#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
103
104#define MAX_INST_SIZE 15
105
Joerg Roedel80b77062007-03-30 17:02:14 +0300106static inline u32 svm_has(u32 feat)
107{
108 return svm_features & feat;
109}
110
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111static inline u8 pop_irq(struct kvm_vcpu *vcpu)
112{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800113 int word_index = __ffs(vcpu->arch.irq_summary);
114 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115 int irq = word_index * BITS_PER_LONG + bit_index;
116
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800117 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
118 if (!vcpu->arch.irq_pending[word_index])
119 clear_bit(word_index, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120 return irq;
121}
122
123static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq)
124{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800125 set_bit(irq, vcpu->arch.irq_pending);
126 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127}
128
129static inline void clgi(void)
130{
131 asm volatile (SVM_CLGI);
132}
133
134static inline void stgi(void)
135{
136 asm volatile (SVM_STGI);
137}
138
139static inline void invlpga(unsigned long addr, u32 asid)
140{
141 asm volatile (SVM_INVLPGA :: "a"(addr), "c"(asid));
142}
143
144static inline unsigned long kvm_read_cr2(void)
145{
146 unsigned long cr2;
147
148 asm volatile ("mov %%cr2, %0" : "=r" (cr2));
149 return cr2;
150}
151
152static inline void kvm_write_cr2(unsigned long val)
153{
154 asm volatile ("mov %0, %%cr2" :: "r" (val));
155}
156
157static inline unsigned long read_dr6(void)
158{
159 unsigned long dr6;
160
161 asm volatile ("mov %%dr6, %0" : "=r" (dr6));
162 return dr6;
163}
164
165static inline void write_dr6(unsigned long val)
166{
167 asm volatile ("mov %0, %%dr6" :: "r" (val));
168}
169
170static inline unsigned long read_dr7(void)
171{
172 unsigned long dr7;
173
174 asm volatile ("mov %%dr7, %0" : "=r" (dr7));
175 return dr7;
176}
177
178static inline void write_dr7(unsigned long val)
179{
180 asm volatile ("mov %0, %%dr7" :: "r" (val));
181}
182
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183static inline void force_new_asid(struct kvm_vcpu *vcpu)
184{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400185 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186}
187
188static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
189{
190 force_new_asid(vcpu);
191}
192
193static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
194{
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100195 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600196 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400198 to_svm(vcpu)->vmcb->save.efer = efer | MSR_EFER_SVME_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800199 vcpu->arch.shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200}
201
Avi Kivity298101d2007-11-25 13:41:11 +0200202static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
203 bool has_error_code, u32 error_code)
204{
205 struct vcpu_svm *svm = to_svm(vcpu);
206
207 svm->vmcb->control.event_inj = nr
208 | SVM_EVTINJ_VALID
209 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
210 | SVM_EVTINJ_TYPE_EXEPT;
211 svm->vmcb->control.event_inj_err = error_code;
212}
213
214static bool svm_exception_injected(struct kvm_vcpu *vcpu)
215{
216 struct vcpu_svm *svm = to_svm(vcpu);
217
218 return !(svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID);
219}
220
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221static int is_external_interrupt(u32 info)
222{
223 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
224 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
225}
226
227static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
228{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400229 struct vcpu_svm *svm = to_svm(vcpu);
230
231 if (!svm->next_rip) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232 printk(KERN_DEBUG "%s: NOP\n", __FUNCTION__);
233 return;
234 }
Mike Dayd77c26f2007-10-08 09:02:08 -0400235 if (svm->next_rip - svm->vmcb->save.rip > MAX_INST_SIZE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236 printk(KERN_ERR "%s: ip 0x%llx next 0x%llx\n",
237 __FUNCTION__,
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400238 svm->vmcb->save.rip,
239 svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800241 vcpu->arch.rip = svm->vmcb->save.rip = svm->next_rip;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400242 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -0800243
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800244 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245}
246
247static int has_svm(void)
248{
249 uint32_t eax, ebx, ecx, edx;
250
Avi Kivity1e885462006-12-29 16:49:34 -0800251 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800252 printk(KERN_INFO "has_svm: not amd\n");
253 return 0;
254 }
255
256 cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
257 if (eax < SVM_CPUID_FUNC) {
258 printk(KERN_INFO "has_svm: can't execute cpuid_8000000a\n");
259 return 0;
260 }
261
262 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
263 if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) {
264 printk(KERN_DEBUG "has_svm: svm not available\n");
265 return 0;
266 }
267 return 1;
268}
269
270static void svm_hardware_disable(void *garbage)
271{
272 struct svm_cpu_data *svm_data
273 = per_cpu(svm_data, raw_smp_processor_id());
274
275 if (svm_data) {
276 uint64_t efer;
277
278 wrmsrl(MSR_VM_HSAVE_PA, 0);
279 rdmsrl(MSR_EFER, efer);
280 wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK);
Al Viro8b6d44c2007-02-09 16:38:40 +0000281 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282 __free_page(svm_data->save_area);
283 kfree(svm_data);
284 }
285}
286
287static void svm_hardware_enable(void *garbage)
288{
289
290 struct svm_cpu_data *svm_data;
291 uint64_t efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800292#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293 struct desc_ptr gdt_descr;
294#else
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100295 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296#endif
297 struct desc_struct *gdt;
298 int me = raw_smp_processor_id();
299
300 if (!has_svm()) {
301 printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
302 return;
303 }
304 svm_data = per_cpu(svm_data, me);
305
306 if (!svm_data) {
307 printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
308 me);
309 return;
310 }
311
312 svm_data->asid_generation = 1;
313 svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
314 svm_data->next_asid = svm_data->max_asid + 1;
315
Mike Dayd77c26f2007-10-08 09:02:08 -0400316 asm volatile ("sgdt %0" : "=m"(gdt_descr));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800317 gdt = (struct desc_struct *)gdt_descr.address;
318 svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
319
320 rdmsrl(MSR_EFER, efer);
321 wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK);
322
323 wrmsrl(MSR_VM_HSAVE_PA,
324 page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
325}
326
327static int svm_cpu_init(int cpu)
328{
329 struct svm_cpu_data *svm_data;
330 int r;
331
332 svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
333 if (!svm_data)
334 return -ENOMEM;
335 svm_data->cpu = cpu;
336 svm_data->save_area = alloc_page(GFP_KERNEL);
337 r = -ENOMEM;
338 if (!svm_data->save_area)
339 goto err_1;
340
341 per_cpu(svm_data, cpu) = svm_data;
342
343 return 0;
344
345err_1:
346 kfree(svm_data);
347 return r;
348
349}
350
Rusty Russellbfc733a2007-07-31 20:42:42 +1000351static void set_msr_interception(u32 *msrpm, unsigned msr,
352 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353{
354 int i;
355
356 for (i = 0; i < NUM_MSR_MAPS; i++) {
357 if (msr >= msrpm_ranges[i] &&
358 msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
359 u32 msr_offset = (i * MSRS_IN_RANGE + msr -
360 msrpm_ranges[i]) * 2;
361
362 u32 *base = msrpm + (msr_offset / 32);
363 u32 msr_shift = msr_offset % 32;
364 u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
365 *base = (*base & ~(0x3 << msr_shift)) |
366 (mask << msr_shift);
Rusty Russellbfc733a2007-07-31 20:42:42 +1000367 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368 }
369 }
Rusty Russellbfc733a2007-07-31 20:42:42 +1000370 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371}
372
373static __init int svm_hardware_setup(void)
374{
375 int cpu;
376 struct page *iopm_pages;
377 struct page *msrpm_pages;
Anthony Liguoric8681332007-04-30 09:48:11 +0300378 void *iopm_va, *msrpm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379 int r;
380
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
382
383 if (!iopm_pages)
384 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300385
386 iopm_va = page_address(iopm_pages);
387 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
388 clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800389 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
390
391
392 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
393
394 r = -ENOMEM;
395 if (!msrpm_pages)
396 goto err_1;
397
398 msrpm_va = page_address(msrpm_pages);
399 memset(msrpm_va, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
400 msrpm_base = page_to_pfn(msrpm_pages) << PAGE_SHIFT;
401
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800402#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403 set_msr_interception(msrpm_va, MSR_GS_BASE, 1, 1);
404 set_msr_interception(msrpm_va, MSR_FS_BASE, 1, 1);
405 set_msr_interception(msrpm_va, MSR_KERNEL_GS_BASE, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406 set_msr_interception(msrpm_va, MSR_LSTAR, 1, 1);
407 set_msr_interception(msrpm_va, MSR_CSTAR, 1, 1);
408 set_msr_interception(msrpm_va, MSR_SYSCALL_MASK, 1, 1);
409#endif
Avi Kivity0e859ca2006-12-22 01:05:08 -0800410 set_msr_interception(msrpm_va, MSR_K6_STAR, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_CS, 1, 1);
412 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_ESP, 1, 1);
413 set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_EIP, 1, 1);
414
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100415 if (boot_cpu_has(X86_FEATURE_NX))
416 kvm_enable_efer_bits(EFER_NX);
417
Avi Kivity6aa8b732006-12-10 02:21:36 -0800418 for_each_online_cpu(cpu) {
419 r = svm_cpu_init(cpu);
420 if (r)
421 goto err_2;
422 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100423
424 svm_features = cpuid_edx(SVM_CPUID_FUNC);
425
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100426 if (!svm_has(SVM_FEATURE_NPT))
427 npt_enabled = false;
428
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100429 if (npt_enabled && !npt) {
430 printk(KERN_INFO "kvm: Nested Paging disabled\n");
431 npt_enabled = false;
432 }
433
Joerg Roedel18552672008-02-07 13:47:41 +0100434 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100435 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100436 kvm_enable_tdp();
437 }
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100438
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439 return 0;
440
441err_2:
442 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
443 msrpm_base = 0;
444err_1:
445 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
446 iopm_base = 0;
447 return r;
448}
449
450static __exit void svm_hardware_unsetup(void)
451{
452 __free_pages(pfn_to_page(msrpm_base >> PAGE_SHIFT), MSRPM_ALLOC_ORDER);
453 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
454 iopm_base = msrpm_base = 0;
455}
456
457static void init_seg(struct vmcb_seg *seg)
458{
459 seg->selector = 0;
460 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
461 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
462 seg->limit = 0xffff;
463 seg->base = 0;
464}
465
466static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
467{
468 seg->selector = 0;
469 seg->attrib = SVM_SELECTOR_P_MASK | type;
470 seg->limit = 0xffff;
471 seg->base = 0;
472}
473
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474static void init_vmcb(struct vmcb *vmcb)
475{
476 struct vmcb_control_area *control = &vmcb->control;
477 struct vmcb_save_area *save = &vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478
479 control->intercept_cr_read = INTERCEPT_CR0_MASK |
480 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200481 INTERCEPT_CR4_MASK |
482 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483
484 control->intercept_cr_write = INTERCEPT_CR0_MASK |
485 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200486 INTERCEPT_CR4_MASK |
487 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800488
489 control->intercept_dr_read = INTERCEPT_DR0_MASK |
490 INTERCEPT_DR1_MASK |
491 INTERCEPT_DR2_MASK |
492 INTERCEPT_DR3_MASK;
493
494 control->intercept_dr_write = INTERCEPT_DR0_MASK |
495 INTERCEPT_DR1_MASK |
496 INTERCEPT_DR2_MASK |
497 INTERCEPT_DR3_MASK |
498 INTERCEPT_DR5_MASK |
499 INTERCEPT_DR7_MASK;
500
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500501 control->intercept_exceptions = (1 << PF_VECTOR) |
502 (1 << UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503
504
505 control->intercept = (1ULL << INTERCEPT_INTR) |
506 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200507 (1ULL << INTERCEPT_SMI) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508 /*
509 * selective cr0 intercept bug?
510 * 0: 0f 22 d8 mov %eax,%cr3
511 * 3: 0f 20 c0 mov %cr0,%eax
512 * 6: 0d 00 00 00 80 or $0x80000000,%eax
513 * b: 0f 22 c0 mov %eax,%cr0
514 * set cr3 ->interception
515 * get cr0 ->interception
516 * set cr0 -> no interception
517 */
518 /* (1ULL << INTERCEPT_SELECTIVE_CR0) | */
519 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200520 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521 (1ULL << INTERCEPT_HLT) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522 (1ULL << INTERCEPT_INVLPGA) |
523 (1ULL << INTERCEPT_IOIO_PROT) |
524 (1ULL << INTERCEPT_MSR_PROT) |
525 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800526 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527 (1ULL << INTERCEPT_VMRUN) |
528 (1ULL << INTERCEPT_VMMCALL) |
529 (1ULL << INTERCEPT_VMLOAD) |
530 (1ULL << INTERCEPT_VMSAVE) |
531 (1ULL << INTERCEPT_STGI) |
532 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100533 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200534 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100535 (1ULL << INTERCEPT_MONITOR) |
536 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800537
538 control->iopm_base_pa = iopm_base;
539 control->msrpm_base_pa = msrpm_base;
Avi Kivity0cc50642007-03-25 12:07:27 +0200540 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541 control->int_ctl = V_INTR_MASKING_MASK;
542
543 init_seg(&save->es);
544 init_seg(&save->ss);
545 init_seg(&save->ds);
546 init_seg(&save->fs);
547 init_seg(&save->gs);
548
549 save->cs.selector = 0xf000;
550 /* Executable/Readable Code Segment */
551 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
552 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
553 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800554 /*
555 * cs.base should really be 0xffff0000, but vmx can't handle that, so
556 * be consistent with it.
557 *
558 * Replace when we have real mode working for vmx.
559 */
560 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561
562 save->gdtr.limit = 0xffff;
563 save->idtr.limit = 0xffff;
564
565 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
566 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
567
568 save->efer = MSR_EFER_SVME_MASK;
Mike Dayd77c26f2007-10-08 09:02:08 -0400569 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570 save->dr7 = 0x400;
571 save->rflags = 2;
572 save->rip = 0x0000fff0;
573
574 /*
575 * cr0 val on cpu init should be 0x60000010, we enable cpu
576 * cache by default. the orderly way is to enable cache in bios.
577 */
Rusty Russell707d92f2007-07-17 23:19:08 +1000578 save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
Rusty Russell66aee912007-07-17 23:34:16 +1000579 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100581
582 if (npt_enabled) {
583 /* Setup VMCB for Nested Paging */
584 control->nested_ctl = 1;
585 control->intercept_exceptions &= ~(1 << PF_VECTOR);
586 control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK|
587 INTERCEPT_CR3_MASK);
588 control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK|
589 INTERCEPT_CR3_MASK);
590 save->g_pat = 0x0007040600070406ULL;
591 /* enable caching because the QEMU Bios doesn't enable it */
592 save->cr0 = X86_CR0_ET;
593 save->cr3 = 0;
594 save->cr4 = 0;
595 }
596
Avi Kivity6aa8b732006-12-10 02:21:36 -0800597}
598
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200599static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300600{
601 struct vcpu_svm *svm = to_svm(vcpu);
602
603 init_vmcb(svm->vmcb);
Avi Kivity70433382007-11-07 12:57:23 +0200604
605 if (vcpu->vcpu_id != 0) {
606 svm->vmcb->save.rip = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800607 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
608 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200609 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200610
611 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300612}
613
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000614static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400616 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617 struct page *page;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000618 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619
Rusty Russellc16f8622007-07-30 21:12:19 +1000620 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000621 if (!svm) {
622 err = -ENOMEM;
623 goto out;
624 }
625
626 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
627 if (err)
628 goto free_svm;
629
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630 page = alloc_page(GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000631 if (!page) {
632 err = -ENOMEM;
633 goto uninit;
634 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400636 svm->vmcb = page_address(page);
637 clear_page(svm->vmcb);
638 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
639 svm->asid_generation = 0;
640 memset(svm->db_regs, 0, sizeof(svm->db_regs));
641 init_vmcb(svm->vmcb);
642
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000643 fx_init(&svm->vcpu);
644 svm->vcpu.fpu_active = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800645 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000646 if (svm->vcpu.vcpu_id == 0)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800647 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000649 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800650
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000651uninit:
652 kvm_vcpu_uninit(&svm->vcpu);
653free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000654 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000655out:
656 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657}
658
659static void svm_free_vcpu(struct kvm_vcpu *vcpu)
660{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400661 struct vcpu_svm *svm = to_svm(vcpu);
662
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000663 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
664 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000665 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666}
667
Avi Kivity15ad7142007-07-11 18:17:21 +0300668static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400670 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300671 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200672
Avi Kivity0cc50642007-03-25 12:07:27 +0200673 if (unlikely(cpu != vcpu->cpu)) {
674 u64 tsc_this, delta;
675
676 /*
677 * Make sure that the guest sees a monotonically
678 * increasing TSC.
679 */
680 rdtscll(tsc_this);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800681 delta = vcpu->arch.host_tsc - tsc_this;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400682 svm->vmcb->control.tsc_offset += delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200683 vcpu->cpu = cpu;
Eddie Donga3d7f852007-09-03 16:15:12 +0300684 kvm_migrate_apic_timer(vcpu);
Avi Kivity0cc50642007-03-25 12:07:27 +0200685 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300686
687 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400688 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689}
690
691static void svm_vcpu_put(struct kvm_vcpu *vcpu)
692{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400693 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300694 int i;
695
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200696 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300697 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400698 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300699
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800700 rdtscll(vcpu->arch.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701}
702
Avi Kivity774c47f2007-02-12 00:54:47 -0800703static void svm_vcpu_decache(struct kvm_vcpu *vcpu)
704{
705}
706
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707static void svm_cache_regs(struct kvm_vcpu *vcpu)
708{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400709 struct vcpu_svm *svm = to_svm(vcpu);
710
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800711 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
712 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
713 vcpu->arch.rip = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714}
715
716static void svm_decache_regs(struct kvm_vcpu *vcpu)
717{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400718 struct vcpu_svm *svm = to_svm(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800719 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
720 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
721 svm->vmcb->save.rip = vcpu->arch.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722}
723
724static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
725{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400726 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800727}
728
729static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
730{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400731 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732}
733
734static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
735{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400736 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737
738 switch (seg) {
739 case VCPU_SREG_CS: return &save->cs;
740 case VCPU_SREG_DS: return &save->ds;
741 case VCPU_SREG_ES: return &save->es;
742 case VCPU_SREG_FS: return &save->fs;
743 case VCPU_SREG_GS: return &save->gs;
744 case VCPU_SREG_SS: return &save->ss;
745 case VCPU_SREG_TR: return &save->tr;
746 case VCPU_SREG_LDTR: return &save->ldtr;
747 }
748 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000749 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750}
751
752static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
753{
754 struct vmcb_seg *s = svm_seg(vcpu, seg);
755
756 return s->base;
757}
758
759static void svm_get_segment(struct kvm_vcpu *vcpu,
760 struct kvm_segment *var, int seg)
761{
762 struct vmcb_seg *s = svm_seg(vcpu, seg);
763
764 var->base = s->base;
765 var->limit = s->limit;
766 var->selector = s->selector;
767 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
768 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
769 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
770 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
771 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
772 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
773 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
774 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
775 var->unusable = !var->present;
776}
777
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
779{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400780 struct vcpu_svm *svm = to_svm(vcpu);
781
782 dt->limit = svm->vmcb->save.idtr.limit;
783 dt->base = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784}
785
786static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
787{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400788 struct vcpu_svm *svm = to_svm(vcpu);
789
790 svm->vmcb->save.idtr.limit = dt->limit;
791 svm->vmcb->save.idtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792}
793
794static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
795{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400796 struct vcpu_svm *svm = to_svm(vcpu);
797
798 dt->limit = svm->vmcb->save.gdtr.limit;
799 dt->base = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800}
801
802static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
803{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400804 struct vcpu_svm *svm = to_svm(vcpu);
805
806 svm->vmcb->save.gdtr.limit = dt->limit;
807 svm->vmcb->save.gdtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808}
809
Anthony Liguori25c4c272007-04-27 09:29:21 +0300810static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -0800811{
812}
813
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
815{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400816 struct vcpu_svm *svm = to_svm(vcpu);
817
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800818#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800819 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +1000820 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800821 vcpu->arch.shadow_efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600822 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823 }
824
Mike Dayd77c26f2007-10-08 09:02:08 -0400825 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800826 vcpu->arch.shadow_efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600827 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828 }
829 }
830#endif
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100831 if (npt_enabled)
832 goto set;
833
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800834 if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400835 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Anthony Liguori7807fa62007-04-23 09:17:21 -0500836 vcpu->fpu_active = 1;
837 }
838
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800839 vcpu->arch.cr0 = cr0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000840 cr0 |= X86_CR0_PG | X86_CR0_WP;
Joerg Roedel6b390b62008-01-29 13:01:27 +0100841 if (!vcpu->fpu_active) {
842 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
Joerg Roedel334df502008-01-21 13:09:33 +0100843 cr0 |= X86_CR0_TS;
Joerg Roedel6b390b62008-01-29 13:01:27 +0100844 }
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100845set:
846 /*
847 * re-enable caching here because the QEMU bios
848 * does not do it - this results in some delay at
849 * reboot
850 */
851 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400852 svm->vmcb->save.cr0 = cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853}
854
855static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
856{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800857 vcpu->arch.cr4 = cr4;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100858 if (!npt_enabled)
859 cr4 |= X86_CR4_PAE;
860 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861}
862
863static void svm_set_segment(struct kvm_vcpu *vcpu,
864 struct kvm_segment *var, int seg)
865{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400866 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 struct vmcb_seg *s = svm_seg(vcpu, seg);
868
869 s->base = var->base;
870 s->limit = var->limit;
871 s->selector = var->selector;
872 if (var->unusable)
873 s->attrib = 0;
874 else {
875 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
876 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
877 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
878 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
879 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
880 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
881 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
882 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
883 }
884 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400885 svm->vmcb->save.cpl
886 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887 >> SVM_SELECTOR_DPL_SHIFT) & 3;
888
889}
890
891/* FIXME:
892
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400893 svm(vcpu)->vmcb->control.int_ctl &= ~V_TPR_MASK;
894 svm(vcpu)->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895
896*/
897
898static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
899{
900 return -EOPNOTSUPP;
901}
902
Eddie Dong2a8067f2007-08-06 16:29:07 +0300903static int svm_get_irq(struct kvm_vcpu *vcpu)
904{
905 struct vcpu_svm *svm = to_svm(vcpu);
906 u32 exit_int_info = svm->vmcb->control.exit_int_info;
907
908 if (is_external_interrupt(exit_int_info))
909 return exit_int_info & SVM_EVTINJ_VEC_MASK;
910 return -1;
911}
912
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913static void load_host_msrs(struct kvm_vcpu *vcpu)
914{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300915#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400916 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300917#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918}
919
920static void save_host_msrs(struct kvm_vcpu *vcpu)
921{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300922#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400923 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300924#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925}
926
Rusty Russelle756fc62007-07-30 20:07:08 +1000927static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928{
929 if (svm_data->next_asid > svm_data->max_asid) {
930 ++svm_data->asid_generation;
931 svm_data->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400932 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 }
934
Rusty Russelle756fc62007-07-30 20:07:08 +1000935 svm->vcpu.cpu = svm_data->cpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400936 svm->asid_generation = svm_data->asid_generation;
937 svm->vmcb->control.asid = svm_data->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938}
939
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
941{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400942 return to_svm(vcpu)->db_regs[dr];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943}
944
945static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
946 int *exception)
947{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400948 struct vcpu_svm *svm = to_svm(vcpu);
949
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950 *exception = 0;
951
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400952 if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
953 svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
954 svm->vmcb->save.dr6 |= DR6_BD_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955 *exception = DB_VECTOR;
956 return;
957 }
958
959 switch (dr) {
960 case 0 ... 3:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400961 svm->db_regs[dr] = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 return;
963 case 4 ... 5:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800964 if (vcpu->arch.cr4 & X86_CR4_DE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 *exception = UD_VECTOR;
966 return;
967 }
968 case 7: {
969 if (value & ~((1ULL << 32) - 1)) {
970 *exception = GP_VECTOR;
971 return;
972 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400973 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 return;
975 }
976 default:
977 printk(KERN_DEBUG "%s: unexpected dr %u\n",
978 __FUNCTION__, dr);
979 *exception = UD_VECTOR;
980 return;
981 }
982}
983
Rusty Russelle756fc62007-07-30 20:07:08 +1000984static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400986 u32 exit_int_info = svm->vmcb->control.exit_int_info;
Rusty Russelle756fc62007-07-30 20:07:08 +1000987 struct kvm *kvm = svm->vcpu.kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 u64 fault_address;
989 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990
Eddie Dong85f455f2007-07-06 12:20:49 +0300991 if (!irqchip_in_kernel(kvm) &&
992 is_external_interrupt(exit_int_info))
Rusty Russelle756fc62007-07-30 20:07:08 +1000993 push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400995 fault_address = svm->vmcb->control.exit_info_2;
996 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity30677142007-10-28 18:48:59 +0200997 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998}
999
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001000static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1001{
1002 int er;
1003
Sheng Yang571008d2008-01-02 14:49:22 +08001004 er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001005 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001006 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001007 return 1;
1008}
1009
Rusty Russelle756fc62007-07-30 20:07:08 +10001010static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001011{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001012 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001013 if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001014 svm->vmcb->save.cr0 &= ~X86_CR0_TS;
Rusty Russelle756fc62007-07-30 20:07:08 +10001015 svm->vcpu.fpu_active = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001016
1017 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001018}
1019
Rusty Russelle756fc62007-07-30 20:07:08 +10001020static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001021{
1022 /*
1023 * VMCB is undefined after a SHUTDOWN intercept
1024 * so reinitialize it.
1025 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001026 clear_page(svm->vmcb);
1027 init_vmcb(svm->vmcb);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001028
1029 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1030 return 0;
1031}
1032
Rusty Russelle756fc62007-07-30 20:07:08 +10001033static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034{
Mike Dayd77c26f2007-10-08 09:02:08 -04001035 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Avi Kivity039576c2007-03-20 12:46:50 +02001036 int size, down, in, string, rep;
1037 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038
Rusty Russelle756fc62007-07-30 20:07:08 +10001039 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001041 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042
Laurent Viviere70669a2007-08-05 10:36:40 +03001043 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1044
1045 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001046 if (emulate_instruction(&svm->vcpu,
1047 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001048 return 0;
1049 return 1;
1050 }
1051
Avi Kivity039576c2007-03-20 12:46:50 +02001052 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1053 port = io_info >> 16;
1054 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity039576c2007-03-20 12:46:50 +02001055 rep = (io_info & SVM_IOIO_REP_MASK) != 0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001056 down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057
Laurent Vivier3090dd72007-08-05 10:43:32 +03001058 return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059}
1060
Rusty Russelle756fc62007-07-30 20:07:08 +10001061static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062{
1063 return 1;
1064}
1065
Rusty Russelle756fc62007-07-30 20:07:08 +10001066static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001068 svm->next_rip = svm->vmcb->save.rip + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001069 skip_emulated_instruction(&svm->vcpu);
1070 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071}
1072
Rusty Russelle756fc62007-07-30 20:07:08 +10001073static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity02e235b2007-02-19 14:37:47 +02001074{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001075 svm->next_rip = svm->vmcb->save.rip + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001076 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001077 kvm_emulate_hypercall(&svm->vcpu);
1078 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001079}
1080
Rusty Russelle756fc62007-07-30 20:07:08 +10001081static int invalid_op_interception(struct vcpu_svm *svm,
1082 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001084 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085 return 1;
1086}
1087
Rusty Russelle756fc62007-07-30 20:07:08 +10001088static int task_switch_interception(struct vcpu_svm *svm,
1089 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090{
Rusty Russellf0242472007-08-01 10:48:02 +10001091 pr_unimpl(&svm->vcpu, "%s: task switch is unsupported\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
1093 return 0;
1094}
1095
Rusty Russelle756fc62007-07-30 20:07:08 +10001096static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001098 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001099 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001100 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101}
1102
Rusty Russelle756fc62007-07-30 20:07:08 +10001103static int emulate_on_interception(struct vcpu_svm *svm,
1104 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105{
Laurent Vivier34273182007-09-18 11:27:37 +02001106 if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
Rusty Russellf0242472007-08-01 10:48:02 +10001107 pr_unimpl(&svm->vcpu, "%s: failed\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 return 1;
1109}
1110
Joerg Roedel1d075432007-12-06 21:02:25 +01001111static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1112{
1113 emulate_instruction(&svm->vcpu, NULL, 0, 0, 0);
1114 if (irqchip_in_kernel(svm->vcpu.kvm))
1115 return 1;
1116 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
1117 return 0;
1118}
1119
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
1121{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001122 struct vcpu_svm *svm = to_svm(vcpu);
1123
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 case MSR_IA32_TIME_STAMP_COUNTER: {
1126 u64 tsc;
1127
1128 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001129 *data = svm->vmcb->control.tsc_offset + tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130 break;
1131 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001132 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001133 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08001135#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001137 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 break;
1139 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001140 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 break;
1142 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001143 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144 break;
1145 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001146 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 break;
1148#endif
1149 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001150 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 break;
1152 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001153 *data = svm->vmcb->save.sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 break;
1155 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001156 *data = svm->vmcb->save.sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01001158 /* Nobody will change the following 5 values in the VMCB so
1159 we can safely return them on rdmsr. They will always be 0
1160 until LBRV is implemented. */
1161 case MSR_IA32_DEBUGCTLMSR:
1162 *data = svm->vmcb->save.dbgctl;
1163 break;
1164 case MSR_IA32_LASTBRANCHFROMIP:
1165 *data = svm->vmcb->save.br_from;
1166 break;
1167 case MSR_IA32_LASTBRANCHTOIP:
1168 *data = svm->vmcb->save.br_to;
1169 break;
1170 case MSR_IA32_LASTINTFROMIP:
1171 *data = svm->vmcb->save.last_excp_from;
1172 break;
1173 case MSR_IA32_LASTINTTOIP:
1174 *data = svm->vmcb->save.last_excp_to;
1175 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001177 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 }
1179 return 0;
1180}
1181
Rusty Russelle756fc62007-07-30 20:07:08 +10001182static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001184 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 u64 data;
1186
Rusty Russelle756fc62007-07-30 20:07:08 +10001187 if (svm_get_msr(&svm->vcpu, ecx, &data))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001188 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189 else {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001190 svm->vmcb->save.rax = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001191 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001192 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001193 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194 }
1195 return 1;
1196}
1197
1198static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
1199{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001200 struct vcpu_svm *svm = to_svm(vcpu);
1201
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203 case MSR_IA32_TIME_STAMP_COUNTER: {
1204 u64 tsc;
1205
1206 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001207 svm->vmcb->control.tsc_offset = data - tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208 break;
1209 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001210 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001211 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08001213#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001215 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216 break;
1217 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001218 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 break;
1220 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001221 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 break;
1223 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001224 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225 break;
1226#endif
1227 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001228 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229 break;
1230 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001231 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232 break;
1233 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001234 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01001236 case MSR_IA32_DEBUGCTLMSR:
1237 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1238 __FUNCTION__, data);
1239 break;
Joerg Roedel62b9aba2007-12-11 15:36:57 +01001240 case MSR_K7_EVNTSEL0:
1241 case MSR_K7_EVNTSEL1:
1242 case MSR_K7_EVNTSEL2:
1243 case MSR_K7_EVNTSEL3:
1244 /*
1245 * only support writing 0 to the performance counters for now
1246 * to make Windows happy. Should be replaced by a real
1247 * performance counter emulation later.
1248 */
1249 if (data != 0)
1250 goto unhandled;
1251 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252 default:
Joerg Roedel62b9aba2007-12-11 15:36:57 +01001253 unhandled:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001254 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255 }
1256 return 0;
1257}
1258
Rusty Russelle756fc62007-07-30 20:07:08 +10001259static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001261 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001262 u64 data = (svm->vmcb->save.rax & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001263 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001264 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001265 if (svm_set_msr(&svm->vcpu, ecx, data))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001266 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001267 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001268 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269 return 1;
1270}
1271
Rusty Russelle756fc62007-07-30 20:07:08 +10001272static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273{
Rusty Russelle756fc62007-07-30 20:07:08 +10001274 if (svm->vmcb->control.exit_info_1)
1275 return wrmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001277 return rdmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278}
1279
Rusty Russelle756fc62007-07-30 20:07:08 +10001280static int interrupt_window_interception(struct vcpu_svm *svm,
Dor Laorc1150d82007-01-05 16:36:24 -08001281 struct kvm_run *kvm_run)
1282{
Eddie Dong85f455f2007-07-06 12:20:49 +03001283 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1284 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08001285 /*
1286 * If the user space waits to inject interrupts, exit as soon as
1287 * possible
1288 */
1289 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001290 !svm->vcpu.arch.irq_summary) {
Rusty Russelle756fc62007-07-30 20:07:08 +10001291 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08001292 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
1293 return 0;
1294 }
1295
1296 return 1;
1297}
1298
Rusty Russelle756fc62007-07-30 20:07:08 +10001299static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300 struct kvm_run *kvm_run) = {
1301 [SVM_EXIT_READ_CR0] = emulate_on_interception,
1302 [SVM_EXIT_READ_CR3] = emulate_on_interception,
1303 [SVM_EXIT_READ_CR4] = emulate_on_interception,
Avi Kivity80a81192007-12-06 19:50:00 +02001304 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305 /* for now: */
1306 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
1307 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
1308 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
Joerg Roedel1d075432007-12-06 21:02:25 +01001309 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310 [SVM_EXIT_READ_DR0] = emulate_on_interception,
1311 [SVM_EXIT_READ_DR1] = emulate_on_interception,
1312 [SVM_EXIT_READ_DR2] = emulate_on_interception,
1313 [SVM_EXIT_READ_DR3] = emulate_on_interception,
1314 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
1315 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
1316 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
1317 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
1318 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
1319 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001320 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Anthony Liguori7807fa62007-04-23 09:17:21 -05001322 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323 [SVM_EXIT_INTR] = nop_on_interception,
1324 [SVM_EXIT_NMI] = nop_on_interception,
1325 [SVM_EXIT_SMI] = nop_on_interception,
1326 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08001327 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
1329 [SVM_EXIT_CPUID] = cpuid_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001330 [SVM_EXIT_INVD] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331 [SVM_EXIT_HLT] = halt_interception,
1332 [SVM_EXIT_INVLPG] = emulate_on_interception,
1333 [SVM_EXIT_INVLPGA] = invalid_op_interception,
1334 [SVM_EXIT_IOIO] = io_interception,
1335 [SVM_EXIT_MSR] = msr_interception,
1336 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001337 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338 [SVM_EXIT_VMRUN] = invalid_op_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02001339 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340 [SVM_EXIT_VMLOAD] = invalid_op_interception,
1341 [SVM_EXIT_VMSAVE] = invalid_op_interception,
1342 [SVM_EXIT_STGI] = invalid_op_interception,
1343 [SVM_EXIT_CLGI] = invalid_op_interception,
1344 [SVM_EXIT_SKINIT] = invalid_op_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001345 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01001346 [SVM_EXIT_MONITOR] = invalid_op_interception,
1347 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001348 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349};
1350
Avi Kivity04d2cc72007-09-10 18:10:54 +03001351static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001353 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001354 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001356 if (npt_enabled) {
1357 int mmu_reload = 0;
1358 if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
1359 svm_set_cr0(vcpu, svm->vmcb->save.cr0);
1360 mmu_reload = 1;
1361 }
1362 vcpu->arch.cr0 = svm->vmcb->save.cr0;
1363 vcpu->arch.cr3 = svm->vmcb->save.cr3;
1364 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1365 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1366 kvm_inject_gp(vcpu, 0);
1367 return 1;
1368 }
1369 }
1370 if (mmu_reload) {
1371 kvm_mmu_reset_context(vcpu);
1372 kvm_mmu_load(vcpu);
1373 }
1374 }
1375
Avi Kivity04d2cc72007-09-10 18:10:54 +03001376 kvm_reput_irq(svm);
1377
1378 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
1379 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
1380 kvm_run->fail_entry.hardware_entry_failure_reason
1381 = svm->vmcb->control.exit_code;
1382 return 0;
1383 }
1384
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001385 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001386 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1387 exit_code != SVM_EXIT_NPF)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
1389 "exit_code 0x%x\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001390 __FUNCTION__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 exit_code);
1392
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02001393 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08001394 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03001396 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397 return 0;
1398 }
1399
Rusty Russelle756fc62007-07-30 20:07:08 +10001400 return svm_exit_handlers[exit_code](svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401}
1402
1403static void reload_tss(struct kvm_vcpu *vcpu)
1404{
1405 int cpu = raw_smp_processor_id();
1406
1407 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04001408 svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409 load_TR_desc();
1410}
1411
Rusty Russelle756fc62007-07-30 20:07:08 +10001412static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413{
1414 int cpu = raw_smp_processor_id();
1415
1416 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
1417
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001418 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Rusty Russelle756fc62007-07-30 20:07:08 +10001419 if (svm->vcpu.cpu != cpu ||
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001420 svm->asid_generation != svm_data->asid_generation)
Rusty Russelle756fc62007-07-30 20:07:08 +10001421 new_asid(svm, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422}
1423
1424
Eddie Dong85f455f2007-07-06 12:20:49 +03001425static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426{
1427 struct vmcb_control_area *control;
1428
Rusty Russelle756fc62007-07-30 20:07:08 +10001429 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03001430 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431 control->int_ctl &= ~V_INTR_PRIO_MASK;
1432 control->int_ctl |= V_IRQ_MASK |
1433 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1434}
1435
Eddie Dong2a8067f2007-08-06 16:29:07 +03001436static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
1437{
1438 struct vcpu_svm *svm = to_svm(vcpu);
1439
1440 svm_inject_irq(svm, irq);
1441}
1442
Avi Kivity04d2cc72007-09-10 18:10:54 +03001443static void svm_intr_assist(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001445 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001446 struct vmcb *vmcb = svm->vmcb;
1447 int intr_vector = -1;
1448
1449 if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
1450 ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
1451 intr_vector = vmcb->control.exit_int_info &
1452 SVM_EVTINJ_VEC_MASK;
1453 vmcb->control.exit_int_info = 0;
1454 svm_inject_irq(svm, intr_vector);
1455 return;
1456 }
1457
1458 if (vmcb->control.int_ctl & V_IRQ_MASK)
1459 return;
1460
Eddie Dong1b9778d2007-09-03 16:56:58 +03001461 if (!kvm_cpu_has_interrupt(vcpu))
Eddie Dong85f455f2007-07-06 12:20:49 +03001462 return;
1463
1464 if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
1465 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
1466 (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
1467 /* unable to deliver irq, set pending irq */
1468 vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
1469 svm_inject_irq(svm, 0x0);
1470 return;
1471 }
1472 /* Okay, we can deliver the interrupt: grab it and update PIC state. */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001473 intr_vector = kvm_cpu_get_interrupt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001474 svm_inject_irq(svm, intr_vector);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001475 kvm_timer_intr_post(vcpu, intr_vector);
Eddie Dong85f455f2007-07-06 12:20:49 +03001476}
1477
1478static void kvm_reput_irq(struct vcpu_svm *svm)
1479{
Rusty Russelle756fc62007-07-30 20:07:08 +10001480 struct vmcb_control_area *control = &svm->vmcb->control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481
Eddie Dong7017fc32007-07-18 11:34:57 +03001482 if ((control->int_ctl & V_IRQ_MASK)
1483 && !irqchip_in_kernel(svm->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484 control->int_ctl &= ~V_IRQ_MASK;
Rusty Russelle756fc62007-07-30 20:07:08 +10001485 push_irq(&svm->vcpu, control->int_vector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486 }
Dor Laorc1150d82007-01-05 16:36:24 -08001487
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001488 svm->vcpu.arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001489 !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
1490}
1491
Eddie Dong85f455f2007-07-06 12:20:49 +03001492static void svm_do_inject_vector(struct vcpu_svm *svm)
1493{
1494 struct kvm_vcpu *vcpu = &svm->vcpu;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001495 int word_index = __ffs(vcpu->arch.irq_summary);
1496 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Eddie Dong85f455f2007-07-06 12:20:49 +03001497 int irq = word_index * BITS_PER_LONG + bit_index;
1498
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001499 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
1500 if (!vcpu->arch.irq_pending[word_index])
1501 clear_bit(word_index, &vcpu->arch.irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03001502 svm_inject_irq(svm, irq);
1503}
1504
Avi Kivity04d2cc72007-09-10 18:10:54 +03001505static void do_interrupt_requests(struct kvm_vcpu *vcpu,
Dor Laorc1150d82007-01-05 16:36:24 -08001506 struct kvm_run *kvm_run)
1507{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001508 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001509 struct vmcb_control_area *control = &svm->vmcb->control;
Dor Laorc1150d82007-01-05 16:36:24 -08001510
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001511 svm->vcpu.arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001512 (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001513 (svm->vmcb->save.rflags & X86_EFLAGS_IF));
Dor Laorc1150d82007-01-05 16:36:24 -08001514
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001515 if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary)
Dor Laorc1150d82007-01-05 16:36:24 -08001516 /*
1517 * If interrupts enabled, and not blocked by sti or mov ss. Good.
1518 */
Eddie Dong85f455f2007-07-06 12:20:49 +03001519 svm_do_inject_vector(svm);
Dor Laorc1150d82007-01-05 16:36:24 -08001520
1521 /*
1522 * Interrupts blocked. Wait for unblock.
1523 */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001524 if (!svm->vcpu.arch.interrupt_window_open &&
1525 (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window))
Dor Laorc1150d82007-01-05 16:36:24 -08001526 control->intercept |= 1ULL << INTERCEPT_VINTR;
Mike Dayd77c26f2007-10-08 09:02:08 -04001527 else
Dor Laorc1150d82007-01-05 16:36:24 -08001528 control->intercept &= ~(1ULL << INTERCEPT_VINTR);
1529}
1530
Izik Eiduscbc94022007-10-25 00:29:55 +02001531static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
1532{
1533 return 0;
1534}
1535
Avi Kivity6aa8b732006-12-10 02:21:36 -08001536static void save_db_regs(unsigned long *db_regs)
1537{
Avi Kivity5aff4582006-12-13 00:33:45 -08001538 asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
1539 asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
1540 asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
1541 asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001542}
1543
1544static void load_db_regs(unsigned long *db_regs)
1545{
Avi Kivity5aff4582006-12-13 00:33:45 -08001546 asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
1547 asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
1548 asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
1549 asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550}
1551
Avi Kivityd9e368d2007-06-07 19:18:30 +03001552static void svm_flush_tlb(struct kvm_vcpu *vcpu)
1553{
1554 force_new_asid(vcpu);
1555}
1556
Avi Kivity04d2cc72007-09-10 18:10:54 +03001557static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
1558{
1559}
1560
1561static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001562{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001563 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001564 u16 fs_selector;
1565 u16 gs_selector;
1566 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03001567
Rusty Russelle756fc62007-07-30 20:07:08 +10001568 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569
1570 save_host_msrs(vcpu);
1571 fs_selector = read_fs();
1572 gs_selector = read_gs();
1573 ldt_selector = read_ldt();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001574 svm->host_cr2 = kvm_read_cr2();
1575 svm->host_dr6 = read_dr6();
1576 svm->host_dr7 = read_dr7();
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001577 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001578 /* required for live migration with NPT */
1579 if (npt_enabled)
1580 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001582 if (svm->vmcb->save.dr7 & 0xff) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583 write_dr7(0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001584 save_db_regs(svm->host_db_regs);
1585 load_db_regs(svm->db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586 }
Avi Kivity36241b82006-12-22 01:05:20 -08001587
Avi Kivity04d2cc72007-09-10 18:10:54 +03001588 clgi();
1589
1590 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08001591
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592 asm volatile (
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001593#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02001594 "push %%rbp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595#else
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001596 "push %%ebp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597#endif
1598
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001599#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001600 "mov %c[rbx](%[svm]), %%rbx \n\t"
1601 "mov %c[rcx](%[svm]), %%rcx \n\t"
1602 "mov %c[rdx](%[svm]), %%rdx \n\t"
1603 "mov %c[rsi](%[svm]), %%rsi \n\t"
1604 "mov %c[rdi](%[svm]), %%rdi \n\t"
1605 "mov %c[rbp](%[svm]), %%rbp \n\t"
1606 "mov %c[r8](%[svm]), %%r8 \n\t"
1607 "mov %c[r9](%[svm]), %%r9 \n\t"
1608 "mov %c[r10](%[svm]), %%r10 \n\t"
1609 "mov %c[r11](%[svm]), %%r11 \n\t"
1610 "mov %c[r12](%[svm]), %%r12 \n\t"
1611 "mov %c[r13](%[svm]), %%r13 \n\t"
1612 "mov %c[r14](%[svm]), %%r14 \n\t"
1613 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001615 "mov %c[rbx](%[svm]), %%ebx \n\t"
1616 "mov %c[rcx](%[svm]), %%ecx \n\t"
1617 "mov %c[rdx](%[svm]), %%edx \n\t"
1618 "mov %c[rsi](%[svm]), %%esi \n\t"
1619 "mov %c[rdi](%[svm]), %%edi \n\t"
1620 "mov %c[rbp](%[svm]), %%ebp \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621#endif
1622
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001623#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624 /* Enter guest mode */
1625 "push %%rax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001626 "mov %c[vmcb](%[svm]), %%rax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627 SVM_VMLOAD "\n\t"
1628 SVM_VMRUN "\n\t"
1629 SVM_VMSAVE "\n\t"
1630 "pop %%rax \n\t"
1631#else
1632 /* Enter guest mode */
1633 "push %%eax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001634 "mov %c[vmcb](%[svm]), %%eax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635 SVM_VMLOAD "\n\t"
1636 SVM_VMRUN "\n\t"
1637 SVM_VMSAVE "\n\t"
1638 "pop %%eax \n\t"
1639#endif
1640
1641 /* Save guest registers, load host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001642#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001643 "mov %%rbx, %c[rbx](%[svm]) \n\t"
1644 "mov %%rcx, %c[rcx](%[svm]) \n\t"
1645 "mov %%rdx, %c[rdx](%[svm]) \n\t"
1646 "mov %%rsi, %c[rsi](%[svm]) \n\t"
1647 "mov %%rdi, %c[rdi](%[svm]) \n\t"
1648 "mov %%rbp, %c[rbp](%[svm]) \n\t"
1649 "mov %%r8, %c[r8](%[svm]) \n\t"
1650 "mov %%r9, %c[r9](%[svm]) \n\t"
1651 "mov %%r10, %c[r10](%[svm]) \n\t"
1652 "mov %%r11, %c[r11](%[svm]) \n\t"
1653 "mov %%r12, %c[r12](%[svm]) \n\t"
1654 "mov %%r13, %c[r13](%[svm]) \n\t"
1655 "mov %%r14, %c[r14](%[svm]) \n\t"
1656 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657
Laurent Vivier54a08c02007-10-25 14:18:53 +02001658 "pop %%rbp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001660 "mov %%ebx, %c[rbx](%[svm]) \n\t"
1661 "mov %%ecx, %c[rcx](%[svm]) \n\t"
1662 "mov %%edx, %c[rdx](%[svm]) \n\t"
1663 "mov %%esi, %c[rsi](%[svm]) \n\t"
1664 "mov %%edi, %c[rdi](%[svm]) \n\t"
1665 "mov %%ebp, %c[rbp](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001666
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001667 "pop %%ebp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668#endif
1669 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001670 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001672 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
1673 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
1674 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
1675 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
1676 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
1677 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001678#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001679 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
1680 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
1681 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
1682 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
1683 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
1684 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
1685 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
1686 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02001688 : "cc", "memory"
1689#ifdef CONFIG_X86_64
1690 , "rbx", "rcx", "rdx", "rsi", "rdi"
1691 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001692#else
1693 , "ebx", "ecx", "edx" , "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02001694#endif
1695 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001697 if ((svm->vmcb->save.dr7 & 0xff))
1698 load_db_regs(svm->host_db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001700 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001702 write_dr6(svm->host_dr6);
1703 write_dr7(svm->host_dr7);
1704 kvm_write_cr2(svm->host_cr2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705
1706 load_fs(fs_selector);
1707 load_gs(gs_selector);
1708 load_ldt(ldt_selector);
1709 load_host_msrs(vcpu);
1710
1711 reload_tss(vcpu);
1712
Avi Kivity56ba47d2007-11-07 17:14:18 +02001713 local_irq_disable();
1714
1715 stgi();
1716
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001717 svm->next_rip = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718}
1719
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
1721{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001722 struct vcpu_svm *svm = to_svm(vcpu);
1723
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001724 if (npt_enabled) {
1725 svm->vmcb->control.nested_cr3 = root;
1726 force_new_asid(vcpu);
1727 return;
1728 }
1729
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001730 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001731 force_new_asid(vcpu);
Anthony Liguori7807fa62007-04-23 09:17:21 -05001732
1733 if (vcpu->fpu_active) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001734 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
1735 svm->vmcb->save.cr0 |= X86_CR0_TS;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001736 vcpu->fpu_active = 0;
1737 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001738}
1739
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740static int is_disabled(void)
1741{
Joerg Roedel6031a612007-06-22 12:29:50 +03001742 u64 vm_cr;
1743
1744 rdmsrl(MSR_VM_CR, vm_cr);
1745 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
1746 return 1;
1747
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748 return 0;
1749}
1750
Ingo Molnar102d8322007-02-19 14:37:47 +02001751static void
1752svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1753{
1754 /*
1755 * Patch in the VMMCALL instruction:
1756 */
1757 hypercall[0] = 0x0f;
1758 hypercall[1] = 0x01;
1759 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02001760}
1761
Yang, Sheng002c7f72007-07-31 14:23:01 +03001762static void svm_check_processor_compat(void *rtn)
1763{
1764 *(int *)rtn = 0;
1765}
1766
Avi Kivity774ead32007-12-26 13:57:04 +02001767static bool svm_cpu_has_accelerated_tpr(void)
1768{
1769 return false;
1770}
1771
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001772static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773 .cpu_has_kvm_support = has_svm,
1774 .disabled_by_bios = is_disabled,
1775 .hardware_setup = svm_hardware_setup,
1776 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001777 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778 .hardware_enable = svm_hardware_enable,
1779 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02001780 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781
1782 .vcpu_create = svm_create_vcpu,
1783 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001784 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001785
Avi Kivity04d2cc72007-09-10 18:10:54 +03001786 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787 .vcpu_load = svm_vcpu_load,
1788 .vcpu_put = svm_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08001789 .vcpu_decache = svm_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790
1791 .set_guest_debug = svm_guest_debug,
1792 .get_msr = svm_get_msr,
1793 .set_msr = svm_set_msr,
1794 .get_segment_base = svm_get_segment_base,
1795 .get_segment = svm_get_segment,
1796 .set_segment = svm_set_segment,
Rusty Russell1747fb72007-09-06 01:21:32 +10001797 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03001798 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001799 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800 .set_cr3 = svm_set_cr3,
1801 .set_cr4 = svm_set_cr4,
1802 .set_efer = svm_set_efer,
1803 .get_idt = svm_get_idt,
1804 .set_idt = svm_set_idt,
1805 .get_gdt = svm_get_gdt,
1806 .set_gdt = svm_set_gdt,
1807 .get_dr = svm_get_dr,
1808 .set_dr = svm_set_dr,
1809 .cache_regs = svm_cache_regs,
1810 .decache_regs = svm_decache_regs,
1811 .get_rflags = svm_get_rflags,
1812 .set_rflags = svm_set_rflags,
1813
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001817 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001818 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02001819 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03001820 .get_irq = svm_get_irq,
1821 .set_irq = svm_set_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02001822 .queue_exception = svm_queue_exception,
1823 .exception_injected = svm_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001824 .inject_pending_irq = svm_intr_assist,
1825 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02001826
1827 .set_tss_addr = svm_set_tss_addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001828};
1829
1830static int __init svm_init(void)
1831{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001832 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10001833 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834}
1835
1836static void __exit svm_exit(void)
1837{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001838 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839}
1840
1841module_init(svm_init)
1842module_exit(svm_exit)