blob: 281a2ffe122400be3748faa537fe6b7500256fc5 [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;
Avi Kivity6aa8b732006-12-10 02:21:36 -080068
69struct kvm_ldttss_desc {
70 u16 limit0;
71 u16 base0;
72 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
73 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
74 u32 base3;
75 u32 zero1;
76} __attribute__((packed));
77
78struct svm_cpu_data {
79 int cpu;
80
Avi Kivity5008fdf2007-04-02 13:05:50 +030081 u64 asid_generation;
82 u32 max_asid;
83 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -080084 struct kvm_ldttss_desc *tss_desc;
85
86 struct page *save_area;
87};
88
89static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +030090static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -080091
92struct svm_init_data {
93 int cpu;
94 int r;
95};
96
97static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
98
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020099#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100#define MSRS_RANGE_SIZE 2048
101#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
102
103#define MAX_INST_SIZE 15
104
Joerg Roedel80b77062007-03-30 17:02:14 +0300105static inline u32 svm_has(u32 feat)
106{
107 return svm_features & feat;
108}
109
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110static inline u8 pop_irq(struct kvm_vcpu *vcpu)
111{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800112 int word_index = __ffs(vcpu->arch.irq_summary);
113 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114 int irq = word_index * BITS_PER_LONG + bit_index;
115
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800116 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
117 if (!vcpu->arch.irq_pending[word_index])
118 clear_bit(word_index, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800119 return irq;
120}
121
122static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq)
123{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800124 set_bit(irq, vcpu->arch.irq_pending);
125 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126}
127
128static inline void clgi(void)
129{
130 asm volatile (SVM_CLGI);
131}
132
133static inline void stgi(void)
134{
135 asm volatile (SVM_STGI);
136}
137
138static inline void invlpga(unsigned long addr, u32 asid)
139{
140 asm volatile (SVM_INVLPGA :: "a"(addr), "c"(asid));
141}
142
143static inline unsigned long kvm_read_cr2(void)
144{
145 unsigned long cr2;
146
147 asm volatile ("mov %%cr2, %0" : "=r" (cr2));
148 return cr2;
149}
150
151static inline void kvm_write_cr2(unsigned long val)
152{
153 asm volatile ("mov %0, %%cr2" :: "r" (val));
154}
155
156static inline unsigned long read_dr6(void)
157{
158 unsigned long dr6;
159
160 asm volatile ("mov %%dr6, %0" : "=r" (dr6));
161 return dr6;
162}
163
164static inline void write_dr6(unsigned long val)
165{
166 asm volatile ("mov %0, %%dr6" :: "r" (val));
167}
168
169static inline unsigned long read_dr7(void)
170{
171 unsigned long dr7;
172
173 asm volatile ("mov %%dr7, %0" : "=r" (dr7));
174 return dr7;
175}
176
177static inline void write_dr7(unsigned long val)
178{
179 asm volatile ("mov %0, %%dr7" :: "r" (val));
180}
181
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182static inline void force_new_asid(struct kvm_vcpu *vcpu)
183{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400184 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185}
186
187static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
188{
189 force_new_asid(vcpu);
190}
191
192static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
193{
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100194 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600195 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400197 to_svm(vcpu)->vmcb->save.efer = efer | MSR_EFER_SVME_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800198 vcpu->arch.shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199}
200
Avi Kivity298101d2007-11-25 13:41:11 +0200201static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
202 bool has_error_code, u32 error_code)
203{
204 struct vcpu_svm *svm = to_svm(vcpu);
205
206 svm->vmcb->control.event_inj = nr
207 | SVM_EVTINJ_VALID
208 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
209 | SVM_EVTINJ_TYPE_EXEPT;
210 svm->vmcb->control.event_inj_err = error_code;
211}
212
213static bool svm_exception_injected(struct kvm_vcpu *vcpu)
214{
215 struct vcpu_svm *svm = to_svm(vcpu);
216
217 return !(svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID);
218}
219
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220static int is_external_interrupt(u32 info)
221{
222 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
223 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
224}
225
226static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
227{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400228 struct vcpu_svm *svm = to_svm(vcpu);
229
230 if (!svm->next_rip) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231 printk(KERN_DEBUG "%s: NOP\n", __FUNCTION__);
232 return;
233 }
Mike Dayd77c26f2007-10-08 09:02:08 -0400234 if (svm->next_rip - svm->vmcb->save.rip > MAX_INST_SIZE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235 printk(KERN_ERR "%s: ip 0x%llx next 0x%llx\n",
236 __FUNCTION__,
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400237 svm->vmcb->save.rip,
238 svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800240 vcpu->arch.rip = svm->vmcb->save.rip = svm->next_rip;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400241 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -0800242
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800243 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244}
245
246static int has_svm(void)
247{
248 uint32_t eax, ebx, ecx, edx;
249
Avi Kivity1e885462006-12-29 16:49:34 -0800250 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251 printk(KERN_INFO "has_svm: not amd\n");
252 return 0;
253 }
254
255 cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
256 if (eax < SVM_CPUID_FUNC) {
257 printk(KERN_INFO "has_svm: can't execute cpuid_8000000a\n");
258 return 0;
259 }
260
261 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
262 if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) {
263 printk(KERN_DEBUG "has_svm: svm not available\n");
264 return 0;
265 }
266 return 1;
267}
268
269static void svm_hardware_disable(void *garbage)
270{
271 struct svm_cpu_data *svm_data
272 = per_cpu(svm_data, raw_smp_processor_id());
273
274 if (svm_data) {
275 uint64_t efer;
276
277 wrmsrl(MSR_VM_HSAVE_PA, 0);
278 rdmsrl(MSR_EFER, efer);
279 wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK);
Al Viro8b6d44c2007-02-09 16:38:40 +0000280 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800281 __free_page(svm_data->save_area);
282 kfree(svm_data);
283 }
284}
285
286static void svm_hardware_enable(void *garbage)
287{
288
289 struct svm_cpu_data *svm_data;
290 uint64_t efer;
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800291#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292 struct desc_ptr gdt_descr;
293#else
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +0100294 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295#endif
296 struct desc_struct *gdt;
297 int me = raw_smp_processor_id();
298
299 if (!has_svm()) {
300 printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
301 return;
302 }
303 svm_data = per_cpu(svm_data, me);
304
305 if (!svm_data) {
306 printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
307 me);
308 return;
309 }
310
311 svm_data->asid_generation = 1;
312 svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
313 svm_data->next_asid = svm_data->max_asid + 1;
314
Mike Dayd77c26f2007-10-08 09:02:08 -0400315 asm volatile ("sgdt %0" : "=m"(gdt_descr));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316 gdt = (struct desc_struct *)gdt_descr.address;
317 svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
318
319 rdmsrl(MSR_EFER, efer);
320 wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK);
321
322 wrmsrl(MSR_VM_HSAVE_PA,
323 page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
324}
325
326static int svm_cpu_init(int cpu)
327{
328 struct svm_cpu_data *svm_data;
329 int r;
330
331 svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
332 if (!svm_data)
333 return -ENOMEM;
334 svm_data->cpu = cpu;
335 svm_data->save_area = alloc_page(GFP_KERNEL);
336 r = -ENOMEM;
337 if (!svm_data->save_area)
338 goto err_1;
339
340 per_cpu(svm_data, cpu) = svm_data;
341
342 return 0;
343
344err_1:
345 kfree(svm_data);
346 return r;
347
348}
349
Rusty Russellbfc733a2007-07-31 20:42:42 +1000350static void set_msr_interception(u32 *msrpm, unsigned msr,
351 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352{
353 int i;
354
355 for (i = 0; i < NUM_MSR_MAPS; i++) {
356 if (msr >= msrpm_ranges[i] &&
357 msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
358 u32 msr_offset = (i * MSRS_IN_RANGE + msr -
359 msrpm_ranges[i]) * 2;
360
361 u32 *base = msrpm + (msr_offset / 32);
362 u32 msr_shift = msr_offset % 32;
363 u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
364 *base = (*base & ~(0x3 << msr_shift)) |
365 (mask << msr_shift);
Rusty Russellbfc733a2007-07-31 20:42:42 +1000366 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 }
368 }
Rusty Russellbfc733a2007-07-31 20:42:42 +1000369 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800370}
371
Joerg Roedelf65c2292008-02-13 18:58:46 +0100372static void svm_vcpu_init_msrpm(u32 *msrpm)
373{
374 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
375
376#ifdef CONFIG_X86_64
377 set_msr_interception(msrpm, MSR_GS_BASE, 1, 1);
378 set_msr_interception(msrpm, MSR_FS_BASE, 1, 1);
379 set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1);
380 set_msr_interception(msrpm, MSR_LSTAR, 1, 1);
381 set_msr_interception(msrpm, MSR_CSTAR, 1, 1);
382 set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1);
383#endif
384 set_msr_interception(msrpm, MSR_K6_STAR, 1, 1);
385 set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1);
386 set_msr_interception(msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
387 set_msr_interception(msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
388}
389
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390static __init int svm_hardware_setup(void)
391{
392 int cpu;
393 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100394 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800395 int r;
396
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
398
399 if (!iopm_pages)
400 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300401
402 iopm_va = page_address(iopm_pages);
403 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
404 clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
406
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100407 if (boot_cpu_has(X86_FEATURE_NX))
408 kvm_enable_efer_bits(EFER_NX);
409
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410 for_each_online_cpu(cpu) {
411 r = svm_cpu_init(cpu);
412 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100413 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100415
416 svm_features = cpuid_edx(SVM_CPUID_FUNC);
417
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100418 if (!svm_has(SVM_FEATURE_NPT))
419 npt_enabled = false;
420
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100421 if (npt_enabled && !npt) {
422 printk(KERN_INFO "kvm: Nested Paging disabled\n");
423 npt_enabled = false;
424 }
425
Joerg Roedel18552672008-02-07 13:47:41 +0100426 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100427 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100428 kvm_enable_tdp();
429 }
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100430
Avi Kivity6aa8b732006-12-10 02:21:36 -0800431 return 0;
432
Joerg Roedelf65c2292008-02-13 18:58:46 +0100433err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
435 iopm_base = 0;
436 return r;
437}
438
439static __exit void svm_hardware_unsetup(void)
440{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100442 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443}
444
445static void init_seg(struct vmcb_seg *seg)
446{
447 seg->selector = 0;
448 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
449 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
450 seg->limit = 0xffff;
451 seg->base = 0;
452}
453
454static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
455{
456 seg->selector = 0;
457 seg->attrib = SVM_SELECTOR_P_MASK | type;
458 seg->limit = 0xffff;
459 seg->base = 0;
460}
461
Joerg Roedele6101a92008-02-13 18:58:45 +0100462static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463{
Joerg Roedele6101a92008-02-13 18:58:45 +0100464 struct vmcb_control_area *control = &svm->vmcb->control;
465 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466
467 control->intercept_cr_read = INTERCEPT_CR0_MASK |
468 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200469 INTERCEPT_CR4_MASK |
470 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800471
472 control->intercept_cr_write = INTERCEPT_CR0_MASK |
473 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200474 INTERCEPT_CR4_MASK |
475 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476
477 control->intercept_dr_read = INTERCEPT_DR0_MASK |
478 INTERCEPT_DR1_MASK |
479 INTERCEPT_DR2_MASK |
480 INTERCEPT_DR3_MASK;
481
482 control->intercept_dr_write = INTERCEPT_DR0_MASK |
483 INTERCEPT_DR1_MASK |
484 INTERCEPT_DR2_MASK |
485 INTERCEPT_DR3_MASK |
486 INTERCEPT_DR5_MASK |
487 INTERCEPT_DR7_MASK;
488
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500489 control->intercept_exceptions = (1 << PF_VECTOR) |
490 (1 << UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491
492
493 control->intercept = (1ULL << INTERCEPT_INTR) |
494 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200495 (1ULL << INTERCEPT_SMI) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 /*
497 * selective cr0 intercept bug?
498 * 0: 0f 22 d8 mov %eax,%cr3
499 * 3: 0f 20 c0 mov %cr0,%eax
500 * 6: 0d 00 00 00 80 or $0x80000000,%eax
501 * b: 0f 22 c0 mov %eax,%cr0
502 * set cr3 ->interception
503 * get cr0 ->interception
504 * set cr0 -> no interception
505 */
506 /* (1ULL << INTERCEPT_SELECTIVE_CR0) | */
507 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200508 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509 (1ULL << INTERCEPT_HLT) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510 (1ULL << INTERCEPT_INVLPGA) |
511 (1ULL << INTERCEPT_IOIO_PROT) |
512 (1ULL << INTERCEPT_MSR_PROT) |
513 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800514 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515 (1ULL << INTERCEPT_VMRUN) |
516 (1ULL << INTERCEPT_VMMCALL) |
517 (1ULL << INTERCEPT_VMLOAD) |
518 (1ULL << INTERCEPT_VMSAVE) |
519 (1ULL << INTERCEPT_STGI) |
520 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100521 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200522 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100523 (1ULL << INTERCEPT_MONITOR) |
524 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525
526 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100527 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200528 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529 control->int_ctl = V_INTR_MASKING_MASK;
530
531 init_seg(&save->es);
532 init_seg(&save->ss);
533 init_seg(&save->ds);
534 init_seg(&save->fs);
535 init_seg(&save->gs);
536
537 save->cs.selector = 0xf000;
538 /* Executable/Readable Code Segment */
539 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
540 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
541 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800542 /*
543 * cs.base should really be 0xffff0000, but vmx can't handle that, so
544 * be consistent with it.
545 *
546 * Replace when we have real mode working for vmx.
547 */
548 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549
550 save->gdtr.limit = 0xffff;
551 save->idtr.limit = 0xffff;
552
553 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
554 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
555
556 save->efer = MSR_EFER_SVME_MASK;
Mike Dayd77c26f2007-10-08 09:02:08 -0400557 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558 save->dr7 = 0x400;
559 save->rflags = 2;
560 save->rip = 0x0000fff0;
561
562 /*
563 * cr0 val on cpu init should be 0x60000010, we enable cpu
564 * cache by default. the orderly way is to enable cache in bios.
565 */
Rusty Russell707d92fa2007-07-17 23:19:08 +1000566 save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
Rusty Russell66aee912007-07-17 23:34:16 +1000567 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800568 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100569
570 if (npt_enabled) {
571 /* Setup VMCB for Nested Paging */
572 control->nested_ctl = 1;
573 control->intercept_exceptions &= ~(1 << PF_VECTOR);
574 control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK|
575 INTERCEPT_CR3_MASK);
576 control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK|
577 INTERCEPT_CR3_MASK);
578 save->g_pat = 0x0007040600070406ULL;
579 /* enable caching because the QEMU Bios doesn't enable it */
580 save->cr0 = X86_CR0_ET;
581 save->cr3 = 0;
582 save->cr4 = 0;
583 }
584
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585}
586
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200587static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300588{
589 struct vcpu_svm *svm = to_svm(vcpu);
590
Joerg Roedele6101a92008-02-13 18:58:45 +0100591 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200592
593 if (vcpu->vcpu_id != 0) {
594 svm->vmcb->save.rip = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800595 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
596 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200597 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200598
599 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300600}
601
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000602static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400604 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100606 struct page *msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000607 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800608
Rusty Russellc16f8622007-07-30 21:12:19 +1000609 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000610 if (!svm) {
611 err = -ENOMEM;
612 goto out;
613 }
614
615 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
616 if (err)
617 goto free_svm;
618
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619 page = alloc_page(GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000620 if (!page) {
621 err = -ENOMEM;
622 goto uninit;
623 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624
Joerg Roedelf65c2292008-02-13 18:58:46 +0100625 err = -ENOMEM;
626 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
627 if (!msrpm_pages)
628 goto uninit;
629 svm->msrpm = page_address(msrpm_pages);
630 svm_vcpu_init_msrpm(svm->msrpm);
631
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400632 svm->vmcb = page_address(page);
633 clear_page(svm->vmcb);
634 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
635 svm->asid_generation = 0;
636 memset(svm->db_regs, 0, sizeof(svm->db_regs));
Joerg Roedele6101a92008-02-13 18:58:45 +0100637 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400638
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000639 fx_init(&svm->vcpu);
640 svm->vcpu.fpu_active = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800641 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000642 if (svm->vcpu.vcpu_id == 0)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800643 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000645 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800646
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000647uninit:
648 kvm_vcpu_uninit(&svm->vcpu);
649free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000650 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000651out:
652 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653}
654
655static void svm_free_vcpu(struct kvm_vcpu *vcpu)
656{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400657 struct vcpu_svm *svm = to_svm(vcpu);
658
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000659 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100660 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000661 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000662 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663}
664
Avi Kivity15ad7142007-07-11 18:17:21 +0300665static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400667 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300668 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200669
Avi Kivity0cc50642007-03-25 12:07:27 +0200670 if (unlikely(cpu != vcpu->cpu)) {
671 u64 tsc_this, delta;
672
673 /*
674 * Make sure that the guest sees a monotonically
675 * increasing TSC.
676 */
677 rdtscll(tsc_this);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800678 delta = vcpu->arch.host_tsc - tsc_this;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400679 svm->vmcb->control.tsc_offset += delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200680 vcpu->cpu = cpu;
Eddie Donga3d7f852007-09-03 16:15:12 +0300681 kvm_migrate_apic_timer(vcpu);
Avi Kivity0cc50642007-03-25 12:07:27 +0200682 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300683
684 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400685 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686}
687
688static void svm_vcpu_put(struct kvm_vcpu *vcpu)
689{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400690 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300691 int i;
692
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200693 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300694 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400695 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300696
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800697 rdtscll(vcpu->arch.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698}
699
Avi Kivity774c47f2007-02-12 00:54:47 -0800700static void svm_vcpu_decache(struct kvm_vcpu *vcpu)
701{
702}
703
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704static void svm_cache_regs(struct kvm_vcpu *vcpu)
705{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400706 struct vcpu_svm *svm = to_svm(vcpu);
707
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800708 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
709 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
710 vcpu->arch.rip = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711}
712
713static void svm_decache_regs(struct kvm_vcpu *vcpu)
714{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400715 struct vcpu_svm *svm = to_svm(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800716 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
717 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
718 svm->vmcb->save.rip = vcpu->arch.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719}
720
721static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
722{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400723 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724}
725
726static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
727{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400728 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729}
730
731static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
732{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400733 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734
735 switch (seg) {
736 case VCPU_SREG_CS: return &save->cs;
737 case VCPU_SREG_DS: return &save->ds;
738 case VCPU_SREG_ES: return &save->es;
739 case VCPU_SREG_FS: return &save->fs;
740 case VCPU_SREG_GS: return &save->gs;
741 case VCPU_SREG_SS: return &save->ss;
742 case VCPU_SREG_TR: return &save->tr;
743 case VCPU_SREG_LDTR: return &save->ldtr;
744 }
745 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000746 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747}
748
749static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
750{
751 struct vmcb_seg *s = svm_seg(vcpu, seg);
752
753 return s->base;
754}
755
756static void svm_get_segment(struct kvm_vcpu *vcpu,
757 struct kvm_segment *var, int seg)
758{
759 struct vmcb_seg *s = svm_seg(vcpu, seg);
760
761 var->base = s->base;
762 var->limit = s->limit;
763 var->selector = s->selector;
764 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
765 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
766 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
767 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
768 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
769 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
770 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
771 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
772 var->unusable = !var->present;
773}
774
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
776{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400777 struct vcpu_svm *svm = to_svm(vcpu);
778
779 dt->limit = svm->vmcb->save.idtr.limit;
780 dt->base = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781}
782
783static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
784{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400785 struct vcpu_svm *svm = to_svm(vcpu);
786
787 svm->vmcb->save.idtr.limit = dt->limit;
788 svm->vmcb->save.idtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789}
790
791static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
792{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400793 struct vcpu_svm *svm = to_svm(vcpu);
794
795 dt->limit = svm->vmcb->save.gdtr.limit;
796 dt->base = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797}
798
799static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
800{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400801 struct vcpu_svm *svm = to_svm(vcpu);
802
803 svm->vmcb->save.gdtr.limit = dt->limit;
804 svm->vmcb->save.gdtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805}
806
Anthony Liguori25c4c272007-04-27 09:29:21 +0300807static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -0800808{
809}
810
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
812{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400813 struct vcpu_svm *svm = to_svm(vcpu);
814
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800815#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800816 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +1000817 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800818 vcpu->arch.shadow_efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600819 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820 }
821
Mike Dayd77c26f2007-10-08 09:02:08 -0400822 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800823 vcpu->arch.shadow_efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600824 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 }
826 }
827#endif
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100828 if (npt_enabled)
829 goto set;
830
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800831 if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400832 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Anthony Liguori7807fa62007-04-23 09:17:21 -0500833 vcpu->fpu_active = 1;
834 }
835
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800836 vcpu->arch.cr0 = cr0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000837 cr0 |= X86_CR0_PG | X86_CR0_WP;
Joerg Roedel6b390b62008-01-29 13:01:27 +0100838 if (!vcpu->fpu_active) {
839 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
Joerg Roedel334df502008-01-21 13:09:33 +0100840 cr0 |= X86_CR0_TS;
Joerg Roedel6b390b62008-01-29 13:01:27 +0100841 }
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100842set:
843 /*
844 * re-enable caching here because the QEMU bios
845 * does not do it - this results in some delay at
846 * reboot
847 */
848 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400849 svm->vmcb->save.cr0 = cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850}
851
852static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
853{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800854 vcpu->arch.cr4 = cr4;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100855 if (!npt_enabled)
856 cr4 |= X86_CR4_PAE;
857 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858}
859
860static void svm_set_segment(struct kvm_vcpu *vcpu,
861 struct kvm_segment *var, int seg)
862{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400863 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864 struct vmcb_seg *s = svm_seg(vcpu, seg);
865
866 s->base = var->base;
867 s->limit = var->limit;
868 s->selector = var->selector;
869 if (var->unusable)
870 s->attrib = 0;
871 else {
872 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
873 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
874 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
875 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
876 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
877 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
878 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
879 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
880 }
881 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400882 svm->vmcb->save.cpl
883 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 >> SVM_SELECTOR_DPL_SHIFT) & 3;
885
886}
887
888/* FIXME:
889
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400890 svm(vcpu)->vmcb->control.int_ctl &= ~V_TPR_MASK;
891 svm(vcpu)->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892
893*/
894
895static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
896{
897 return -EOPNOTSUPP;
898}
899
Eddie Dong2a8067f2007-08-06 16:29:07 +0300900static int svm_get_irq(struct kvm_vcpu *vcpu)
901{
902 struct vcpu_svm *svm = to_svm(vcpu);
903 u32 exit_int_info = svm->vmcb->control.exit_int_info;
904
905 if (is_external_interrupt(exit_int_info))
906 return exit_int_info & SVM_EVTINJ_VEC_MASK;
907 return -1;
908}
909
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910static void load_host_msrs(struct kvm_vcpu *vcpu)
911{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300912#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400913 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300914#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915}
916
917static void save_host_msrs(struct kvm_vcpu *vcpu)
918{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300919#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400920 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300921#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922}
923
Rusty Russelle756fc62007-07-30 20:07:08 +1000924static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925{
926 if (svm_data->next_asid > svm_data->max_asid) {
927 ++svm_data->asid_generation;
928 svm_data->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400929 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930 }
931
Rusty Russelle756fc62007-07-30 20:07:08 +1000932 svm->vcpu.cpu = svm_data->cpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400933 svm->asid_generation = svm_data->asid_generation;
934 svm->vmcb->control.asid = svm_data->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935}
936
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
938{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400939 return to_svm(vcpu)->db_regs[dr];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940}
941
942static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
943 int *exception)
944{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400945 struct vcpu_svm *svm = to_svm(vcpu);
946
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947 *exception = 0;
948
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400949 if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
950 svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
951 svm->vmcb->save.dr6 |= DR6_BD_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952 *exception = DB_VECTOR;
953 return;
954 }
955
956 switch (dr) {
957 case 0 ... 3:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400958 svm->db_regs[dr] = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959 return;
960 case 4 ... 5:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800961 if (vcpu->arch.cr4 & X86_CR4_DE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 *exception = UD_VECTOR;
963 return;
964 }
965 case 7: {
966 if (value & ~((1ULL << 32) - 1)) {
967 *exception = GP_VECTOR;
968 return;
969 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400970 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971 return;
972 }
973 default:
974 printk(KERN_DEBUG "%s: unexpected dr %u\n",
975 __FUNCTION__, dr);
976 *exception = UD_VECTOR;
977 return;
978 }
979}
980
Rusty Russelle756fc62007-07-30 20:07:08 +1000981static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400983 u32 exit_int_info = svm->vmcb->control.exit_int_info;
Rusty Russelle756fc62007-07-30 20:07:08 +1000984 struct kvm *kvm = svm->vcpu.kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 u64 fault_address;
986 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987
Eddie Dong85f455f2007-07-06 12:20:49 +0300988 if (!irqchip_in_kernel(kvm) &&
989 is_external_interrupt(exit_int_info))
Rusty Russelle756fc62007-07-30 20:07:08 +1000990 push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400992 fault_address = svm->vmcb->control.exit_info_2;
993 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity30677142007-10-28 18:48:59 +0200994 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995}
996
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500997static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
998{
999 int er;
1000
Sheng Yang571008d2008-01-02 14:49:22 +08001001 er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001002 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001003 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001004 return 1;
1005}
1006
Rusty Russelle756fc62007-07-30 20:07:08 +10001007static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001008{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001009 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001010 if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001011 svm->vmcb->save.cr0 &= ~X86_CR0_TS;
Rusty Russelle756fc62007-07-30 20:07:08 +10001012 svm->vcpu.fpu_active = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001013
1014 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001015}
1016
Rusty Russelle756fc62007-07-30 20:07:08 +10001017static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001018{
1019 /*
1020 * VMCB is undefined after a SHUTDOWN intercept
1021 * so reinitialize it.
1022 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001023 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001024 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001025
1026 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1027 return 0;
1028}
1029
Rusty Russelle756fc62007-07-30 20:07:08 +10001030static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031{
Mike Dayd77c26f2007-10-08 09:02:08 -04001032 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Avi Kivity039576c2007-03-20 12:46:50 +02001033 int size, down, in, string, rep;
1034 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035
Rusty Russelle756fc62007-07-30 20:07:08 +10001036 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001038 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039
Laurent Viviere70669a2007-08-05 10:36:40 +03001040 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1041
1042 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001043 if (emulate_instruction(&svm->vcpu,
1044 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001045 return 0;
1046 return 1;
1047 }
1048
Avi Kivity039576c2007-03-20 12:46:50 +02001049 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1050 port = io_info >> 16;
1051 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity039576c2007-03-20 12:46:50 +02001052 rep = (io_info & SVM_IOIO_REP_MASK) != 0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001053 down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054
Laurent Vivier3090dd72007-08-05 10:43:32 +03001055 return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056}
1057
Rusty Russelle756fc62007-07-30 20:07:08 +10001058static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059{
1060 return 1;
1061}
1062
Rusty Russelle756fc62007-07-30 20:07:08 +10001063static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001065 svm->next_rip = svm->vmcb->save.rip + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001066 skip_emulated_instruction(&svm->vcpu);
1067 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068}
1069
Rusty Russelle756fc62007-07-30 20:07:08 +10001070static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity02e235b2007-02-19 14:37:47 +02001071{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001072 svm->next_rip = svm->vmcb->save.rip + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001073 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001074 kvm_emulate_hypercall(&svm->vcpu);
1075 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001076}
1077
Rusty Russelle756fc62007-07-30 20:07:08 +10001078static int invalid_op_interception(struct vcpu_svm *svm,
1079 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001081 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082 return 1;
1083}
1084
Rusty Russelle756fc62007-07-30 20:07:08 +10001085static int task_switch_interception(struct vcpu_svm *svm,
1086 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087{
Rusty Russellf0242472007-08-01 10:48:02 +10001088 pr_unimpl(&svm->vcpu, "%s: task switch is unsupported\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
1090 return 0;
1091}
1092
Rusty Russelle756fc62007-07-30 20:07:08 +10001093static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001095 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001096 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001097 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098}
1099
Rusty Russelle756fc62007-07-30 20:07:08 +10001100static int emulate_on_interception(struct vcpu_svm *svm,
1101 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102{
Laurent Vivier34273182007-09-18 11:27:37 +02001103 if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
Rusty Russellf0242472007-08-01 10:48:02 +10001104 pr_unimpl(&svm->vcpu, "%s: failed\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 return 1;
1106}
1107
Joerg Roedel1d075432007-12-06 21:02:25 +01001108static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1109{
1110 emulate_instruction(&svm->vcpu, NULL, 0, 0, 0);
1111 if (irqchip_in_kernel(svm->vcpu.kvm))
1112 return 1;
1113 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
1114 return 0;
1115}
1116
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
1118{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001119 struct vcpu_svm *svm = to_svm(vcpu);
1120
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122 case MSR_IA32_TIME_STAMP_COUNTER: {
1123 u64 tsc;
1124
1125 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001126 *data = svm->vmcb->control.tsc_offset + tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127 break;
1128 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001129 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001130 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08001132#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001134 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 break;
1136 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001137 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 break;
1139 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001140 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 break;
1142 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001143 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144 break;
1145#endif
1146 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001147 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148 break;
1149 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001150 *data = svm->vmcb->save.sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 break;
1152 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001153 *data = svm->vmcb->save.sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01001155 /* Nobody will change the following 5 values in the VMCB so
1156 we can safely return them on rdmsr. They will always be 0
1157 until LBRV is implemented. */
1158 case MSR_IA32_DEBUGCTLMSR:
1159 *data = svm->vmcb->save.dbgctl;
1160 break;
1161 case MSR_IA32_LASTBRANCHFROMIP:
1162 *data = svm->vmcb->save.br_from;
1163 break;
1164 case MSR_IA32_LASTBRANCHTOIP:
1165 *data = svm->vmcb->save.br_to;
1166 break;
1167 case MSR_IA32_LASTINTFROMIP:
1168 *data = svm->vmcb->save.last_excp_from;
1169 break;
1170 case MSR_IA32_LASTINTTOIP:
1171 *data = svm->vmcb->save.last_excp_to;
1172 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001174 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175 }
1176 return 0;
1177}
1178
Rusty Russelle756fc62007-07-30 20:07:08 +10001179static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001181 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182 u64 data;
1183
Rusty Russelle756fc62007-07-30 20:07:08 +10001184 if (svm_get_msr(&svm->vcpu, ecx, &data))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001185 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 else {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001187 svm->vmcb->save.rax = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001188 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001189 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001190 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191 }
1192 return 1;
1193}
1194
1195static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
1196{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001197 struct vcpu_svm *svm = to_svm(vcpu);
1198
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200 case MSR_IA32_TIME_STAMP_COUNTER: {
1201 u64 tsc;
1202
1203 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001204 svm->vmcb->control.tsc_offset = data - tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205 break;
1206 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001207 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001208 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001209 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08001210#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001212 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213 break;
1214 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001215 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216 break;
1217 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001218 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 break;
1220 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001221 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 break;
1223#endif
1224 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001225 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 break;
1227 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001228 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229 break;
1230 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001231 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01001233 case MSR_IA32_DEBUGCTLMSR:
1234 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1235 __FUNCTION__, data);
1236 break;
Joerg Roedel62b9aba2007-12-11 15:36:57 +01001237 case MSR_K7_EVNTSEL0:
1238 case MSR_K7_EVNTSEL1:
1239 case MSR_K7_EVNTSEL2:
1240 case MSR_K7_EVNTSEL3:
1241 /*
1242 * only support writing 0 to the performance counters for now
1243 * to make Windows happy. Should be replaced by a real
1244 * performance counter emulation later.
1245 */
1246 if (data != 0)
1247 goto unhandled;
1248 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249 default:
Joerg Roedel62b9aba2007-12-11 15:36:57 +01001250 unhandled:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001251 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252 }
1253 return 0;
1254}
1255
Rusty Russelle756fc62007-07-30 20:07:08 +10001256static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001258 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001259 u64 data = (svm->vmcb->save.rax & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001260 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001261 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001262 if (svm_set_msr(&svm->vcpu, ecx, data))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001263 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001265 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 return 1;
1267}
1268
Rusty Russelle756fc62007-07-30 20:07:08 +10001269static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270{
Rusty Russelle756fc62007-07-30 20:07:08 +10001271 if (svm->vmcb->control.exit_info_1)
1272 return wrmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001274 return rdmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275}
1276
Rusty Russelle756fc62007-07-30 20:07:08 +10001277static int interrupt_window_interception(struct vcpu_svm *svm,
Dor Laorc1150d82007-01-05 16:36:24 -08001278 struct kvm_run *kvm_run)
1279{
Eddie Dong85f455f2007-07-06 12:20:49 +03001280 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1281 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08001282 /*
1283 * If the user space waits to inject interrupts, exit as soon as
1284 * possible
1285 */
1286 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001287 !svm->vcpu.arch.irq_summary) {
Rusty Russelle756fc62007-07-30 20:07:08 +10001288 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08001289 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
1290 return 0;
1291 }
1292
1293 return 1;
1294}
1295
Rusty Russelle756fc62007-07-30 20:07:08 +10001296static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 struct kvm_run *kvm_run) = {
1298 [SVM_EXIT_READ_CR0] = emulate_on_interception,
1299 [SVM_EXIT_READ_CR3] = emulate_on_interception,
1300 [SVM_EXIT_READ_CR4] = emulate_on_interception,
Avi Kivity80a81192007-12-06 19:50:00 +02001301 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302 /* for now: */
1303 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
1304 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
1305 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
Joerg Roedel1d075432007-12-06 21:02:25 +01001306 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307 [SVM_EXIT_READ_DR0] = emulate_on_interception,
1308 [SVM_EXIT_READ_DR1] = emulate_on_interception,
1309 [SVM_EXIT_READ_DR2] = emulate_on_interception,
1310 [SVM_EXIT_READ_DR3] = emulate_on_interception,
1311 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
1312 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
1313 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
1314 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
1315 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
1316 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001317 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Anthony Liguori7807fa62007-04-23 09:17:21 -05001319 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320 [SVM_EXIT_INTR] = nop_on_interception,
1321 [SVM_EXIT_NMI] = nop_on_interception,
1322 [SVM_EXIT_SMI] = nop_on_interception,
1323 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08001324 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
1326 [SVM_EXIT_CPUID] = cpuid_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001327 [SVM_EXIT_INVD] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328 [SVM_EXIT_HLT] = halt_interception,
1329 [SVM_EXIT_INVLPG] = emulate_on_interception,
1330 [SVM_EXIT_INVLPGA] = invalid_op_interception,
1331 [SVM_EXIT_IOIO] = io_interception,
1332 [SVM_EXIT_MSR] = msr_interception,
1333 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001334 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335 [SVM_EXIT_VMRUN] = invalid_op_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02001336 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337 [SVM_EXIT_VMLOAD] = invalid_op_interception,
1338 [SVM_EXIT_VMSAVE] = invalid_op_interception,
1339 [SVM_EXIT_STGI] = invalid_op_interception,
1340 [SVM_EXIT_CLGI] = invalid_op_interception,
1341 [SVM_EXIT_SKINIT] = invalid_op_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001342 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01001343 [SVM_EXIT_MONITOR] = invalid_op_interception,
1344 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001345 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346};
1347
Avi Kivity04d2cc72007-09-10 18:10:54 +03001348static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001350 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001351 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001353 if (npt_enabled) {
1354 int mmu_reload = 0;
1355 if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
1356 svm_set_cr0(vcpu, svm->vmcb->save.cr0);
1357 mmu_reload = 1;
1358 }
1359 vcpu->arch.cr0 = svm->vmcb->save.cr0;
1360 vcpu->arch.cr3 = svm->vmcb->save.cr3;
1361 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1362 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1363 kvm_inject_gp(vcpu, 0);
1364 return 1;
1365 }
1366 }
1367 if (mmu_reload) {
1368 kvm_mmu_reset_context(vcpu);
1369 kvm_mmu_load(vcpu);
1370 }
1371 }
1372
Avi Kivity04d2cc72007-09-10 18:10:54 +03001373 kvm_reput_irq(svm);
1374
1375 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
1376 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
1377 kvm_run->fail_entry.hardware_entry_failure_reason
1378 = svm->vmcb->control.exit_code;
1379 return 0;
1380 }
1381
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001382 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001383 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1384 exit_code != SVM_EXIT_NPF)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
1386 "exit_code 0x%x\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001387 __FUNCTION__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 exit_code);
1389
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02001390 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08001391 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03001393 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 return 0;
1395 }
1396
Rusty Russelle756fc62007-07-30 20:07:08 +10001397 return svm_exit_handlers[exit_code](svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398}
1399
1400static void reload_tss(struct kvm_vcpu *vcpu)
1401{
1402 int cpu = raw_smp_processor_id();
1403
1404 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04001405 svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406 load_TR_desc();
1407}
1408
Rusty Russelle756fc62007-07-30 20:07:08 +10001409static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410{
1411 int cpu = raw_smp_processor_id();
1412
1413 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
1414
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001415 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Rusty Russelle756fc62007-07-30 20:07:08 +10001416 if (svm->vcpu.cpu != cpu ||
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001417 svm->asid_generation != svm_data->asid_generation)
Rusty Russelle756fc62007-07-30 20:07:08 +10001418 new_asid(svm, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419}
1420
1421
Eddie Dong85f455f2007-07-06 12:20:49 +03001422static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423{
1424 struct vmcb_control_area *control;
1425
Rusty Russelle756fc62007-07-30 20:07:08 +10001426 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03001427 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428 control->int_ctl &= ~V_INTR_PRIO_MASK;
1429 control->int_ctl |= V_IRQ_MASK |
1430 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1431}
1432
Eddie Dong2a8067f2007-08-06 16:29:07 +03001433static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
1434{
1435 struct vcpu_svm *svm = to_svm(vcpu);
1436
1437 svm_inject_irq(svm, irq);
1438}
1439
Avi Kivity04d2cc72007-09-10 18:10:54 +03001440static void svm_intr_assist(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001442 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001443 struct vmcb *vmcb = svm->vmcb;
1444 int intr_vector = -1;
1445
1446 if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
1447 ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
1448 intr_vector = vmcb->control.exit_int_info &
1449 SVM_EVTINJ_VEC_MASK;
1450 vmcb->control.exit_int_info = 0;
1451 svm_inject_irq(svm, intr_vector);
1452 return;
1453 }
1454
1455 if (vmcb->control.int_ctl & V_IRQ_MASK)
1456 return;
1457
Eddie Dong1b9778d2007-09-03 16:56:58 +03001458 if (!kvm_cpu_has_interrupt(vcpu))
Eddie Dong85f455f2007-07-06 12:20:49 +03001459 return;
1460
1461 if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
1462 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
1463 (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
1464 /* unable to deliver irq, set pending irq */
1465 vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
1466 svm_inject_irq(svm, 0x0);
1467 return;
1468 }
1469 /* Okay, we can deliver the interrupt: grab it and update PIC state. */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001470 intr_vector = kvm_cpu_get_interrupt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001471 svm_inject_irq(svm, intr_vector);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001472 kvm_timer_intr_post(vcpu, intr_vector);
Eddie Dong85f455f2007-07-06 12:20:49 +03001473}
1474
1475static void kvm_reput_irq(struct vcpu_svm *svm)
1476{
Rusty Russelle756fc62007-07-30 20:07:08 +10001477 struct vmcb_control_area *control = &svm->vmcb->control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478
Eddie Dong7017fc32007-07-18 11:34:57 +03001479 if ((control->int_ctl & V_IRQ_MASK)
1480 && !irqchip_in_kernel(svm->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481 control->int_ctl &= ~V_IRQ_MASK;
Rusty Russelle756fc62007-07-30 20:07:08 +10001482 push_irq(&svm->vcpu, control->int_vector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 }
Dor Laorc1150d82007-01-05 16:36:24 -08001484
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001485 svm->vcpu.arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001486 !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
1487}
1488
Eddie Dong85f455f2007-07-06 12:20:49 +03001489static void svm_do_inject_vector(struct vcpu_svm *svm)
1490{
1491 struct kvm_vcpu *vcpu = &svm->vcpu;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001492 int word_index = __ffs(vcpu->arch.irq_summary);
1493 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Eddie Dong85f455f2007-07-06 12:20:49 +03001494 int irq = word_index * BITS_PER_LONG + bit_index;
1495
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001496 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
1497 if (!vcpu->arch.irq_pending[word_index])
1498 clear_bit(word_index, &vcpu->arch.irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03001499 svm_inject_irq(svm, irq);
1500}
1501
Avi Kivity04d2cc72007-09-10 18:10:54 +03001502static void do_interrupt_requests(struct kvm_vcpu *vcpu,
Dor Laorc1150d82007-01-05 16:36:24 -08001503 struct kvm_run *kvm_run)
1504{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001505 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001506 struct vmcb_control_area *control = &svm->vmcb->control;
Dor Laorc1150d82007-01-05 16:36:24 -08001507
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001508 svm->vcpu.arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001509 (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001510 (svm->vmcb->save.rflags & X86_EFLAGS_IF));
Dor Laorc1150d82007-01-05 16:36:24 -08001511
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001512 if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary)
Dor Laorc1150d82007-01-05 16:36:24 -08001513 /*
1514 * If interrupts enabled, and not blocked by sti or mov ss. Good.
1515 */
Eddie Dong85f455f2007-07-06 12:20:49 +03001516 svm_do_inject_vector(svm);
Dor Laorc1150d82007-01-05 16:36:24 -08001517
1518 /*
1519 * Interrupts blocked. Wait for unblock.
1520 */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001521 if (!svm->vcpu.arch.interrupt_window_open &&
1522 (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window))
Dor Laorc1150d82007-01-05 16:36:24 -08001523 control->intercept |= 1ULL << INTERCEPT_VINTR;
Mike Dayd77c26f2007-10-08 09:02:08 -04001524 else
Dor Laorc1150d82007-01-05 16:36:24 -08001525 control->intercept &= ~(1ULL << INTERCEPT_VINTR);
1526}
1527
Izik Eiduscbc94022007-10-25 00:29:55 +02001528static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
1529{
1530 return 0;
1531}
1532
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533static void save_db_regs(unsigned long *db_regs)
1534{
Avi Kivity5aff4582006-12-13 00:33:45 -08001535 asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
1536 asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
1537 asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
1538 asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001539}
1540
1541static void load_db_regs(unsigned long *db_regs)
1542{
Avi Kivity5aff4582006-12-13 00:33:45 -08001543 asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
1544 asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
1545 asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
1546 asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547}
1548
Avi Kivityd9e368d2007-06-07 19:18:30 +03001549static void svm_flush_tlb(struct kvm_vcpu *vcpu)
1550{
1551 force_new_asid(vcpu);
1552}
1553
Avi Kivity04d2cc72007-09-10 18:10:54 +03001554static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
1555{
1556}
1557
1558static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001560 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561 u16 fs_selector;
1562 u16 gs_selector;
1563 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03001564
Rusty Russelle756fc62007-07-30 20:07:08 +10001565 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566
1567 save_host_msrs(vcpu);
1568 fs_selector = read_fs();
1569 gs_selector = read_gs();
1570 ldt_selector = read_ldt();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001571 svm->host_cr2 = kvm_read_cr2();
1572 svm->host_dr6 = read_dr6();
1573 svm->host_dr7 = read_dr7();
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001574 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001575 /* required for live migration with NPT */
1576 if (npt_enabled)
1577 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001579 if (svm->vmcb->save.dr7 & 0xff) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580 write_dr7(0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001581 save_db_regs(svm->host_db_regs);
1582 load_db_regs(svm->db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583 }
Avi Kivity36241b82006-12-22 01:05:20 -08001584
Avi Kivity04d2cc72007-09-10 18:10:54 +03001585 clgi();
1586
1587 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08001588
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589 asm volatile (
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001590#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02001591 "push %%rbp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592#else
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001593 "push %%ebp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594#endif
1595
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001596#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001597 "mov %c[rbx](%[svm]), %%rbx \n\t"
1598 "mov %c[rcx](%[svm]), %%rcx \n\t"
1599 "mov %c[rdx](%[svm]), %%rdx \n\t"
1600 "mov %c[rsi](%[svm]), %%rsi \n\t"
1601 "mov %c[rdi](%[svm]), %%rdi \n\t"
1602 "mov %c[rbp](%[svm]), %%rbp \n\t"
1603 "mov %c[r8](%[svm]), %%r8 \n\t"
1604 "mov %c[r9](%[svm]), %%r9 \n\t"
1605 "mov %c[r10](%[svm]), %%r10 \n\t"
1606 "mov %c[r11](%[svm]), %%r11 \n\t"
1607 "mov %c[r12](%[svm]), %%r12 \n\t"
1608 "mov %c[r13](%[svm]), %%r13 \n\t"
1609 "mov %c[r14](%[svm]), %%r14 \n\t"
1610 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001612 "mov %c[rbx](%[svm]), %%ebx \n\t"
1613 "mov %c[rcx](%[svm]), %%ecx \n\t"
1614 "mov %c[rdx](%[svm]), %%edx \n\t"
1615 "mov %c[rsi](%[svm]), %%esi \n\t"
1616 "mov %c[rdi](%[svm]), %%edi \n\t"
1617 "mov %c[rbp](%[svm]), %%ebp \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618#endif
1619
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001620#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621 /* Enter guest mode */
1622 "push %%rax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001623 "mov %c[vmcb](%[svm]), %%rax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624 SVM_VMLOAD "\n\t"
1625 SVM_VMRUN "\n\t"
1626 SVM_VMSAVE "\n\t"
1627 "pop %%rax \n\t"
1628#else
1629 /* Enter guest mode */
1630 "push %%eax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001631 "mov %c[vmcb](%[svm]), %%eax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632 SVM_VMLOAD "\n\t"
1633 SVM_VMRUN "\n\t"
1634 SVM_VMSAVE "\n\t"
1635 "pop %%eax \n\t"
1636#endif
1637
1638 /* Save guest registers, load host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001639#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001640 "mov %%rbx, %c[rbx](%[svm]) \n\t"
1641 "mov %%rcx, %c[rcx](%[svm]) \n\t"
1642 "mov %%rdx, %c[rdx](%[svm]) \n\t"
1643 "mov %%rsi, %c[rsi](%[svm]) \n\t"
1644 "mov %%rdi, %c[rdi](%[svm]) \n\t"
1645 "mov %%rbp, %c[rbp](%[svm]) \n\t"
1646 "mov %%r8, %c[r8](%[svm]) \n\t"
1647 "mov %%r9, %c[r9](%[svm]) \n\t"
1648 "mov %%r10, %c[r10](%[svm]) \n\t"
1649 "mov %%r11, %c[r11](%[svm]) \n\t"
1650 "mov %%r12, %c[r12](%[svm]) \n\t"
1651 "mov %%r13, %c[r13](%[svm]) \n\t"
1652 "mov %%r14, %c[r14](%[svm]) \n\t"
1653 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001654
Laurent Vivier54a08c02007-10-25 14:18:53 +02001655 "pop %%rbp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001657 "mov %%ebx, %c[rbx](%[svm]) \n\t"
1658 "mov %%ecx, %c[rcx](%[svm]) \n\t"
1659 "mov %%edx, %c[rdx](%[svm]) \n\t"
1660 "mov %%esi, %c[rsi](%[svm]) \n\t"
1661 "mov %%edi, %c[rdi](%[svm]) \n\t"
1662 "mov %%ebp, %c[rbp](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001664 "pop %%ebp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665#endif
1666 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001667 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001669 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
1670 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
1671 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
1672 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
1673 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
1674 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001675#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001676 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
1677 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
1678 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
1679 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
1680 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
1681 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
1682 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
1683 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02001685 : "cc", "memory"
1686#ifdef CONFIG_X86_64
1687 , "rbx", "rcx", "rdx", "rsi", "rdi"
1688 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001689#else
1690 , "ebx", "ecx", "edx" , "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02001691#endif
1692 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001694 if ((svm->vmcb->save.dr7 & 0xff))
1695 load_db_regs(svm->host_db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001697 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001699 write_dr6(svm->host_dr6);
1700 write_dr7(svm->host_dr7);
1701 kvm_write_cr2(svm->host_cr2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702
1703 load_fs(fs_selector);
1704 load_gs(gs_selector);
1705 load_ldt(ldt_selector);
1706 load_host_msrs(vcpu);
1707
1708 reload_tss(vcpu);
1709
Avi Kivity56ba47d2007-11-07 17:14:18 +02001710 local_irq_disable();
1711
1712 stgi();
1713
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001714 svm->next_rip = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001715}
1716
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
1718{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001719 struct vcpu_svm *svm = to_svm(vcpu);
1720
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001721 if (npt_enabled) {
1722 svm->vmcb->control.nested_cr3 = root;
1723 force_new_asid(vcpu);
1724 return;
1725 }
1726
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001727 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001728 force_new_asid(vcpu);
Anthony Liguori7807fa62007-04-23 09:17:21 -05001729
1730 if (vcpu->fpu_active) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001731 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
1732 svm->vmcb->save.cr0 |= X86_CR0_TS;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001733 vcpu->fpu_active = 0;
1734 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735}
1736
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737static int is_disabled(void)
1738{
Joerg Roedel6031a612007-06-22 12:29:50 +03001739 u64 vm_cr;
1740
1741 rdmsrl(MSR_VM_CR, vm_cr);
1742 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
1743 return 1;
1744
Avi Kivity6aa8b732006-12-10 02:21:36 -08001745 return 0;
1746}
1747
Ingo Molnar102d8322007-02-19 14:37:47 +02001748static void
1749svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1750{
1751 /*
1752 * Patch in the VMMCALL instruction:
1753 */
1754 hypercall[0] = 0x0f;
1755 hypercall[1] = 0x01;
1756 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02001757}
1758
Yang, Sheng002c7f72007-07-31 14:23:01 +03001759static void svm_check_processor_compat(void *rtn)
1760{
1761 *(int *)rtn = 0;
1762}
1763
Avi Kivity774ead32007-12-26 13:57:04 +02001764static bool svm_cpu_has_accelerated_tpr(void)
1765{
1766 return false;
1767}
1768
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001769static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001770 .cpu_has_kvm_support = has_svm,
1771 .disabled_by_bios = is_disabled,
1772 .hardware_setup = svm_hardware_setup,
1773 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001774 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001775 .hardware_enable = svm_hardware_enable,
1776 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02001777 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778
1779 .vcpu_create = svm_create_vcpu,
1780 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001781 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782
Avi Kivity04d2cc72007-09-10 18:10:54 +03001783 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784 .vcpu_load = svm_vcpu_load,
1785 .vcpu_put = svm_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08001786 .vcpu_decache = svm_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787
1788 .set_guest_debug = svm_guest_debug,
1789 .get_msr = svm_get_msr,
1790 .set_msr = svm_set_msr,
1791 .get_segment_base = svm_get_segment_base,
1792 .get_segment = svm_get_segment,
1793 .set_segment = svm_set_segment,
Rusty Russell1747fb72007-09-06 01:21:32 +10001794 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03001795 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001797 .set_cr3 = svm_set_cr3,
1798 .set_cr4 = svm_set_cr4,
1799 .set_efer = svm_set_efer,
1800 .get_idt = svm_get_idt,
1801 .set_idt = svm_set_idt,
1802 .get_gdt = svm_get_gdt,
1803 .set_gdt = svm_set_gdt,
1804 .get_dr = svm_get_dr,
1805 .set_dr = svm_set_dr,
1806 .cache_regs = svm_cache_regs,
1807 .decache_regs = svm_decache_regs,
1808 .get_rflags = svm_get_rflags,
1809 .set_rflags = svm_set_rflags,
1810
Avi Kivity6aa8b732006-12-10 02:21:36 -08001811 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001812
Avi Kivity6aa8b732006-12-10 02:21:36 -08001813 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001814 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02001816 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03001817 .get_irq = svm_get_irq,
1818 .set_irq = svm_set_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02001819 .queue_exception = svm_queue_exception,
1820 .exception_injected = svm_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001821 .inject_pending_irq = svm_intr_assist,
1822 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02001823
1824 .set_tss_addr = svm_set_tss_addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001825};
1826
1827static int __init svm_init(void)
1828{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001829 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10001830 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831}
1832
1833static void __exit svm_exit(void)
1834{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001835 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836}
1837
1838module_init(svm_init)
1839module_exit(svm_exit)