blob: b756e876dce391b222c6e4ec84c7dd6447e738de [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
Avi Kivity4ecac3f2008-05-13 13:23:38 +030030#define __ex(x) __kvm_handle_fault_on_reboot(x)
31
Avi Kivity6aa8b732006-12-10 02:21:36 -080032MODULE_AUTHOR("Qumranet");
33MODULE_LICENSE("GPL");
34
35#define IOPM_ALLOC_ORDER 2
36#define MSRPM_ALLOC_ORDER 1
37
38#define DB_VECTOR 1
39#define UD_VECTOR 6
40#define GP_VECTOR 13
41
42#define DR7_GD_MASK (1 << 13)
43#define DR6_BD_MASK (1 << 13)
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
45#define SEG_TYPE_LDT 2
46#define SEG_TYPE_BUSY_TSS16 3
47
Joerg Roedel80b77062007-03-30 17:02:14 +030048#define SVM_FEATURE_NPT (1 << 0)
49#define SVM_FEATURE_LBRV (1 << 1)
50#define SVM_DEATURE_SVML (1 << 2)
51
Joerg Roedel24e09cb2008-02-13 18:58:47 +010052#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
53
Joerg Roedel709ddeb2008-02-07 13:47:45 +010054/* enable NPT for AMD64 and X86 with PAE */
55#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
56static bool npt_enabled = true;
57#else
Joerg Roedele3da3ac2008-02-07 13:47:39 +010058static bool npt_enabled = false;
Joerg Roedel709ddeb2008-02-07 13:47:45 +010059#endif
Joerg Roedel6c7dac72008-02-07 13:47:40 +010060static int npt = 1;
61
62module_param(npt, int, S_IRUGO);
Joerg Roedele3da3ac2008-02-07 13:47:39 +010063
Avi Kivity04d2cc72007-09-10 18:10:54 +030064static void kvm_reput_irq(struct vcpu_svm *svm);
65
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040066static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
67{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100068 return container_of(vcpu, struct vcpu_svm, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040069}
70
Harvey Harrison4866d5e2008-02-19 10:32:02 -080071static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -080072
73struct kvm_ldttss_desc {
74 u16 limit0;
75 u16 base0;
76 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
77 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
78 u32 base3;
79 u32 zero1;
80} __attribute__((packed));
81
82struct svm_cpu_data {
83 int cpu;
84
Avi Kivity5008fdf2007-04-02 13:05:50 +030085 u64 asid_generation;
86 u32 max_asid;
87 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -080088 struct kvm_ldttss_desc *tss_desc;
89
90 struct page *save_area;
91};
92
93static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +030094static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -080095
96struct svm_init_data {
97 int cpu;
98 int r;
99};
100
101static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
102
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200103#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104#define MSRS_RANGE_SIZE 2048
105#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
106
107#define MAX_INST_SIZE 15
108
Joerg Roedel80b77062007-03-30 17:02:14 +0300109static inline u32 svm_has(u32 feat)
110{
111 return svm_features & feat;
112}
113
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114static inline u8 pop_irq(struct kvm_vcpu *vcpu)
115{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800116 int word_index = __ffs(vcpu->arch.irq_summary);
117 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118 int irq = word_index * BITS_PER_LONG + bit_index;
119
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800120 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
121 if (!vcpu->arch.irq_pending[word_index])
122 clear_bit(word_index, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123 return irq;
124}
125
126static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq)
127{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800128 set_bit(irq, vcpu->arch.irq_pending);
129 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130}
131
132static inline void clgi(void)
133{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300134 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800135}
136
137static inline void stgi(void)
138{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300139 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140}
141
142static inline void invlpga(unsigned long addr, u32 asid)
143{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300144 asm volatile (__ex(SVM_INVLPGA) :: "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145}
146
147static inline unsigned long kvm_read_cr2(void)
148{
149 unsigned long cr2;
150
151 asm volatile ("mov %%cr2, %0" : "=r" (cr2));
152 return cr2;
153}
154
155static inline void kvm_write_cr2(unsigned long val)
156{
157 asm volatile ("mov %0, %%cr2" :: "r" (val));
158}
159
160static inline unsigned long read_dr6(void)
161{
162 unsigned long dr6;
163
164 asm volatile ("mov %%dr6, %0" : "=r" (dr6));
165 return dr6;
166}
167
168static inline void write_dr6(unsigned long val)
169{
170 asm volatile ("mov %0, %%dr6" :: "r" (val));
171}
172
173static inline unsigned long read_dr7(void)
174{
175 unsigned long dr7;
176
177 asm volatile ("mov %%dr7, %0" : "=r" (dr7));
178 return dr7;
179}
180
181static inline void write_dr7(unsigned long val)
182{
183 asm volatile ("mov %0, %%dr7" :: "r" (val));
184}
185
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186static inline void force_new_asid(struct kvm_vcpu *vcpu)
187{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400188 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189}
190
191static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
192{
193 force_new_asid(vcpu);
194}
195
196static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
197{
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100198 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600199 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400201 to_svm(vcpu)->vmcb->save.efer = efer | MSR_EFER_SVME_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800202 vcpu->arch.shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203}
204
Avi Kivity298101d2007-11-25 13:41:11 +0200205static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
206 bool has_error_code, u32 error_code)
207{
208 struct vcpu_svm *svm = to_svm(vcpu);
209
210 svm->vmcb->control.event_inj = nr
211 | SVM_EVTINJ_VALID
212 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
213 | SVM_EVTINJ_TYPE_EXEPT;
214 svm->vmcb->control.event_inj_err = error_code;
215}
216
217static bool svm_exception_injected(struct kvm_vcpu *vcpu)
218{
219 struct vcpu_svm *svm = to_svm(vcpu);
220
221 return !(svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID);
222}
223
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224static int is_external_interrupt(u32 info)
225{
226 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
227 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
228}
229
230static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
231{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400232 struct vcpu_svm *svm = to_svm(vcpu);
233
234 if (!svm->next_rip) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800235 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236 return;
237 }
Mike Dayd77c26f2007-10-08 09:02:08 -0400238 if (svm->next_rip - svm->vmcb->save.rip > MAX_INST_SIZE)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239 printk(KERN_ERR "%s: ip 0x%llx next 0x%llx\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800240 __func__,
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400241 svm->vmcb->save.rip,
242 svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800244 vcpu->arch.rip = svm->vmcb->save.rip = svm->next_rip;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400245 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -0800246
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800247 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248}
249
250static int has_svm(void)
251{
252 uint32_t eax, ebx, ecx, edx;
253
Avi Kivity1e885462006-12-29 16:49:34 -0800254 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255 printk(KERN_INFO "has_svm: not amd\n");
256 return 0;
257 }
258
259 cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
260 if (eax < SVM_CPUID_FUNC) {
261 printk(KERN_INFO "has_svm: can't execute cpuid_8000000a\n");
262 return 0;
263 }
264
265 cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
266 if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) {
267 printk(KERN_DEBUG "has_svm: svm not available\n");
268 return 0;
269 }
270 return 1;
271}
272
273static void svm_hardware_disable(void *garbage)
274{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200275 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276
Joerg Roedel0da1db752008-07-02 16:02:11 +0200277 wrmsrl(MSR_VM_HSAVE_PA, 0);
278 rdmsrl(MSR_EFER, efer);
279 wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280}
281
282static void svm_hardware_enable(void *garbage)
283{
284
285 struct svm_cpu_data *svm_data;
286 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288 struct desc_struct *gdt;
289 int me = raw_smp_processor_id();
290
291 if (!has_svm()) {
292 printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
293 return;
294 }
295 svm_data = per_cpu(svm_data, me);
296
297 if (!svm_data) {
298 printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
299 me);
300 return;
301 }
302
303 svm_data->asid_generation = 1;
304 svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
305 svm_data->next_asid = svm_data->max_asid + 1;
306
Mike Dayd77c26f2007-10-08 09:02:08 -0400307 asm volatile ("sgdt %0" : "=m"(gdt_descr));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308 gdt = (struct desc_struct *)gdt_descr.address;
309 svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
310
311 rdmsrl(MSR_EFER, efer);
312 wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK);
313
314 wrmsrl(MSR_VM_HSAVE_PA,
315 page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
316}
317
Joerg Roedel0da1db752008-07-02 16:02:11 +0200318static void svm_cpu_uninit(int cpu)
319{
320 struct svm_cpu_data *svm_data
321 = per_cpu(svm_data, raw_smp_processor_id());
322
323 if (!svm_data)
324 return;
325
326 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
327 __free_page(svm_data->save_area);
328 kfree(svm_data);
329}
330
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331static int svm_cpu_init(int cpu)
332{
333 struct svm_cpu_data *svm_data;
334 int r;
335
336 svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
337 if (!svm_data)
338 return -ENOMEM;
339 svm_data->cpu = cpu;
340 svm_data->save_area = alloc_page(GFP_KERNEL);
341 r = -ENOMEM;
342 if (!svm_data->save_area)
343 goto err_1;
344
345 per_cpu(svm_data, cpu) = svm_data;
346
347 return 0;
348
349err_1:
350 kfree(svm_data);
351 return r;
352
353}
354
Rusty Russellbfc733a2007-07-31 20:42:42 +1000355static void set_msr_interception(u32 *msrpm, unsigned msr,
356 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357{
358 int i;
359
360 for (i = 0; i < NUM_MSR_MAPS; i++) {
361 if (msr >= msrpm_ranges[i] &&
362 msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
363 u32 msr_offset = (i * MSRS_IN_RANGE + msr -
364 msrpm_ranges[i]) * 2;
365
366 u32 *base = msrpm + (msr_offset / 32);
367 u32 msr_shift = msr_offset % 32;
368 u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
369 *base = (*base & ~(0x3 << msr_shift)) |
370 (mask << msr_shift);
Rusty Russellbfc733a2007-07-31 20:42:42 +1000371 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800372 }
373 }
Rusty Russellbfc733a2007-07-31 20:42:42 +1000374 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375}
376
Joerg Roedelf65c2292008-02-13 18:58:46 +0100377static void svm_vcpu_init_msrpm(u32 *msrpm)
378{
379 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
380
381#ifdef CONFIG_X86_64
382 set_msr_interception(msrpm, MSR_GS_BASE, 1, 1);
383 set_msr_interception(msrpm, MSR_FS_BASE, 1, 1);
384 set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1);
385 set_msr_interception(msrpm, MSR_LSTAR, 1, 1);
386 set_msr_interception(msrpm, MSR_CSTAR, 1, 1);
387 set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1);
388#endif
389 set_msr_interception(msrpm, MSR_K6_STAR, 1, 1);
390 set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1);
391 set_msr_interception(msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
392 set_msr_interception(msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
393}
394
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100395static void svm_enable_lbrv(struct vcpu_svm *svm)
396{
397 u32 *msrpm = svm->msrpm;
398
399 svm->vmcb->control.lbr_ctl = 1;
400 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
401 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
402 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
403 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
404}
405
406static void svm_disable_lbrv(struct vcpu_svm *svm)
407{
408 u32 *msrpm = svm->msrpm;
409
410 svm->vmcb->control.lbr_ctl = 0;
411 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
412 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
413 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
414 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
415}
416
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417static __init int svm_hardware_setup(void)
418{
419 int cpu;
420 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100421 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422 int r;
423
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
425
426 if (!iopm_pages)
427 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300428
429 iopm_va = page_address(iopm_pages);
430 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
431 clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
433
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100434 if (boot_cpu_has(X86_FEATURE_NX))
435 kvm_enable_efer_bits(EFER_NX);
436
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437 for_each_online_cpu(cpu) {
438 r = svm_cpu_init(cpu);
439 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100440 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100442
443 svm_features = cpuid_edx(SVM_CPUID_FUNC);
444
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100445 if (!svm_has(SVM_FEATURE_NPT))
446 npt_enabled = false;
447
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100448 if (npt_enabled && !npt) {
449 printk(KERN_INFO "kvm: Nested Paging disabled\n");
450 npt_enabled = false;
451 }
452
Joerg Roedel18552672008-02-07 13:47:41 +0100453 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100454 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100455 kvm_enable_tdp();
456 }
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100457
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 return 0;
459
Joerg Roedelf65c2292008-02-13 18:58:46 +0100460err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
462 iopm_base = 0;
463 return r;
464}
465
466static __exit void svm_hardware_unsetup(void)
467{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200468 int cpu;
469
470 for_each_online_cpu(cpu)
471 svm_cpu_uninit(cpu);
472
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100474 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475}
476
477static void init_seg(struct vmcb_seg *seg)
478{
479 seg->selector = 0;
480 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
481 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
482 seg->limit = 0xffff;
483 seg->base = 0;
484}
485
486static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
487{
488 seg->selector = 0;
489 seg->attrib = SVM_SELECTOR_P_MASK | type;
490 seg->limit = 0xffff;
491 seg->base = 0;
492}
493
Joerg Roedele6101a92008-02-13 18:58:45 +0100494static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495{
Joerg Roedele6101a92008-02-13 18:58:45 +0100496 struct vmcb_control_area *control = &svm->vmcb->control;
497 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498
499 control->intercept_cr_read = INTERCEPT_CR0_MASK |
500 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200501 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502
503 control->intercept_cr_write = INTERCEPT_CR0_MASK |
504 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200505 INTERCEPT_CR4_MASK |
506 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800507
508 control->intercept_dr_read = INTERCEPT_DR0_MASK |
509 INTERCEPT_DR1_MASK |
510 INTERCEPT_DR2_MASK |
511 INTERCEPT_DR3_MASK;
512
513 control->intercept_dr_write = INTERCEPT_DR0_MASK |
514 INTERCEPT_DR1_MASK |
515 INTERCEPT_DR2_MASK |
516 INTERCEPT_DR3_MASK |
517 INTERCEPT_DR5_MASK |
518 INTERCEPT_DR7_MASK;
519
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500520 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200521 (1 << UD_VECTOR) |
522 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800523
524
525 control->intercept = (1ULL << INTERCEPT_INTR) |
526 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200527 (1ULL << INTERCEPT_SMI) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200529 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800530 (1ULL << INTERCEPT_HLT) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531 (1ULL << INTERCEPT_INVLPGA) |
532 (1ULL << INTERCEPT_IOIO_PROT) |
533 (1ULL << INTERCEPT_MSR_PROT) |
534 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800535 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536 (1ULL << INTERCEPT_VMRUN) |
537 (1ULL << INTERCEPT_VMMCALL) |
538 (1ULL << INTERCEPT_VMLOAD) |
539 (1ULL << INTERCEPT_VMSAVE) |
540 (1ULL << INTERCEPT_STGI) |
541 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100542 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200543 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100544 (1ULL << INTERCEPT_MONITOR) |
545 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546
547 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100548 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200549 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550 control->int_ctl = V_INTR_MASKING_MASK;
551
552 init_seg(&save->es);
553 init_seg(&save->ss);
554 init_seg(&save->ds);
555 init_seg(&save->fs);
556 init_seg(&save->gs);
557
558 save->cs.selector = 0xf000;
559 /* Executable/Readable Code Segment */
560 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
561 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
562 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800563 /*
564 * cs.base should really be 0xffff0000, but vmx can't handle that, so
565 * be consistent with it.
566 *
567 * Replace when we have real mode working for vmx.
568 */
569 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570
571 save->gdtr.limit = 0xffff;
572 save->idtr.limit = 0xffff;
573
574 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
575 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
576
577 save->efer = MSR_EFER_SVME_MASK;
Mike Dayd77c26f2007-10-08 09:02:08 -0400578 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579 save->dr7 = 0x400;
580 save->rflags = 2;
581 save->rip = 0x0000fff0;
582
583 /*
584 * cr0 val on cpu init should be 0x60000010, we enable cpu
585 * cache by default. the orderly way is to enable cache in bios.
586 */
Rusty Russell707d92fa2007-07-17 23:19:08 +1000587 save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
Rusty Russell66aee912007-07-17 23:34:16 +1000588 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100590
591 if (npt_enabled) {
592 /* Setup VMCB for Nested Paging */
593 control->nested_ctl = 1;
Joerg Roedel35649902008-04-09 16:04:32 +0200594 control->intercept &= ~(1ULL << INTERCEPT_TASK_SWITCH);
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100595 control->intercept_exceptions &= ~(1 << PF_VECTOR);
596 control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK|
597 INTERCEPT_CR3_MASK);
598 control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK|
599 INTERCEPT_CR3_MASK);
600 save->g_pat = 0x0007040600070406ULL;
601 /* enable caching because the QEMU Bios doesn't enable it */
602 save->cr0 = X86_CR0_ET;
603 save->cr3 = 0;
604 save->cr4 = 0;
605 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300606 force_new_asid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607}
608
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200609static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300610{
611 struct vcpu_svm *svm = to_svm(vcpu);
612
Joerg Roedele6101a92008-02-13 18:58:45 +0100613 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200614
615 if (vcpu->vcpu_id != 0) {
616 svm->vmcb->save.rip = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800617 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
618 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200619 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200620
621 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300622}
623
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000624static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400626 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100628 struct page *msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000629 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630
Rusty Russellc16f8622007-07-30 21:12:19 +1000631 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000632 if (!svm) {
633 err = -ENOMEM;
634 goto out;
635 }
636
637 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
638 if (err)
639 goto free_svm;
640
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 page = alloc_page(GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000642 if (!page) {
643 err = -ENOMEM;
644 goto uninit;
645 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646
Joerg Roedelf65c2292008-02-13 18:58:46 +0100647 err = -ENOMEM;
648 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
649 if (!msrpm_pages)
650 goto uninit;
651 svm->msrpm = page_address(msrpm_pages);
652 svm_vcpu_init_msrpm(svm->msrpm);
653
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400654 svm->vmcb = page_address(page);
655 clear_page(svm->vmcb);
656 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
657 svm->asid_generation = 0;
658 memset(svm->db_regs, 0, sizeof(svm->db_regs));
Joerg Roedele6101a92008-02-13 18:58:45 +0100659 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400660
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000661 fx_init(&svm->vcpu);
662 svm->vcpu.fpu_active = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800663 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000664 if (svm->vcpu.vcpu_id == 0)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800665 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000667 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800668
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000669uninit:
670 kvm_vcpu_uninit(&svm->vcpu);
671free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000672 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000673out:
674 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675}
676
677static void svm_free_vcpu(struct kvm_vcpu *vcpu)
678{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400679 struct vcpu_svm *svm = to_svm(vcpu);
680
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000681 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100682 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000683 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000684 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685}
686
Avi Kivity15ad7142007-07-11 18:17:21 +0300687static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400689 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300690 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200691
Avi Kivity0cc50642007-03-25 12:07:27 +0200692 if (unlikely(cpu != vcpu->cpu)) {
693 u64 tsc_this, delta;
694
695 /*
696 * Make sure that the guest sees a monotonically
697 * increasing TSC.
698 */
699 rdtscll(tsc_this);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800700 delta = vcpu->arch.host_tsc - tsc_this;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400701 svm->vmcb->control.tsc_offset += delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200702 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300703 kvm_migrate_timers(vcpu);
Avi Kivity0cc50642007-03-25 12:07:27 +0200704 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300705
706 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400707 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708}
709
710static void svm_vcpu_put(struct kvm_vcpu *vcpu)
711{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400712 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300713 int i;
714
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200715 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300716 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400717 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300718
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800719 rdtscll(vcpu->arch.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720}
721
722static void svm_cache_regs(struct kvm_vcpu *vcpu)
723{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400724 struct vcpu_svm *svm = to_svm(vcpu);
725
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800726 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
727 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
728 vcpu->arch.rip = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729}
730
731static void svm_decache_regs(struct kvm_vcpu *vcpu)
732{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400733 struct vcpu_svm *svm = to_svm(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800734 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
735 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
736 svm->vmcb->save.rip = vcpu->arch.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737}
738
739static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
740{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400741 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742}
743
744static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
745{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400746 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747}
748
749static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
750{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400751 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752
753 switch (seg) {
754 case VCPU_SREG_CS: return &save->cs;
755 case VCPU_SREG_DS: return &save->ds;
756 case VCPU_SREG_ES: return &save->es;
757 case VCPU_SREG_FS: return &save->fs;
758 case VCPU_SREG_GS: return &save->gs;
759 case VCPU_SREG_SS: return &save->ss;
760 case VCPU_SREG_TR: return &save->tr;
761 case VCPU_SREG_LDTR: return &save->ldtr;
762 }
763 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000764 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765}
766
767static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
768{
769 struct vmcb_seg *s = svm_seg(vcpu, seg);
770
771 return s->base;
772}
773
774static void svm_get_segment(struct kvm_vcpu *vcpu,
775 struct kvm_segment *var, int seg)
776{
777 struct vmcb_seg *s = svm_seg(vcpu, seg);
778
779 var->base = s->base;
780 var->limit = s->limit;
781 var->selector = s->selector;
782 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
783 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
784 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
785 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
786 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
787 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
788 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
789 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
790 var->unusable = !var->present;
791}
792
Izik Eidus2e4d2652008-03-24 19:38:34 +0200793static int svm_get_cpl(struct kvm_vcpu *vcpu)
794{
795 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
796
797 return save->cpl;
798}
799
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
801{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400802 struct vcpu_svm *svm = to_svm(vcpu);
803
804 dt->limit = svm->vmcb->save.idtr.limit;
805 dt->base = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806}
807
808static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
809{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400810 struct vcpu_svm *svm = to_svm(vcpu);
811
812 svm->vmcb->save.idtr.limit = dt->limit;
813 svm->vmcb->save.idtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814}
815
816static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
817{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400818 struct vcpu_svm *svm = to_svm(vcpu);
819
820 dt->limit = svm->vmcb->save.gdtr.limit;
821 dt->base = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822}
823
824static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
825{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400826 struct vcpu_svm *svm = to_svm(vcpu);
827
828 svm->vmcb->save.gdtr.limit = dt->limit;
829 svm->vmcb->save.gdtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830}
831
Anthony Liguori25c4c272007-04-27 09:29:21 +0300832static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -0800833{
834}
835
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
837{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400838 struct vcpu_svm *svm = to_svm(vcpu);
839
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800840#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800841 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +1000842 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800843 vcpu->arch.shadow_efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600844 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845 }
846
Mike Dayd77c26f2007-10-08 09:02:08 -0400847 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800848 vcpu->arch.shadow_efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600849 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850 }
851 }
852#endif
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100853 if (npt_enabled)
854 goto set;
855
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800856 if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400857 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Anthony Liguori7807fa62007-04-23 09:17:21 -0500858 vcpu->fpu_active = 1;
859 }
860
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800861 vcpu->arch.cr0 = cr0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000862 cr0 |= X86_CR0_PG | X86_CR0_WP;
Joerg Roedel6b390b62008-01-29 13:01:27 +0100863 if (!vcpu->fpu_active) {
864 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
Joerg Roedel334df502008-01-21 13:09:33 +0100865 cr0 |= X86_CR0_TS;
Joerg Roedel6b390b62008-01-29 13:01:27 +0100866 }
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100867set:
868 /*
869 * re-enable caching here because the QEMU bios
870 * does not do it - this results in some delay at
871 * reboot
872 */
873 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400874 svm->vmcb->save.cr0 = cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875}
876
877static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
878{
Joerg Roedel6394b642008-04-09 14:15:29 +0200879 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
880
Joerg Roedelec077262008-04-09 14:15:28 +0200881 vcpu->arch.cr4 = cr4;
882 if (!npt_enabled)
883 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +0200884 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +0200885 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886}
887
888static void svm_set_segment(struct kvm_vcpu *vcpu,
889 struct kvm_segment *var, int seg)
890{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400891 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892 struct vmcb_seg *s = svm_seg(vcpu, seg);
893
894 s->base = var->base;
895 s->limit = var->limit;
896 s->selector = var->selector;
897 if (var->unusable)
898 s->attrib = 0;
899 else {
900 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
901 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
902 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
903 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
904 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
905 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
906 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
907 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
908 }
909 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400910 svm->vmcb->save.cpl
911 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 >> SVM_SELECTOR_DPL_SHIFT) & 3;
913
914}
915
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
917{
918 return -EOPNOTSUPP;
919}
920
Eddie Dong2a8067f2007-08-06 16:29:07 +0300921static int svm_get_irq(struct kvm_vcpu *vcpu)
922{
923 struct vcpu_svm *svm = to_svm(vcpu);
924 u32 exit_int_info = svm->vmcb->control.exit_int_info;
925
926 if (is_external_interrupt(exit_int_info))
927 return exit_int_info & SVM_EVTINJ_VEC_MASK;
928 return -1;
929}
930
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931static void load_host_msrs(struct kvm_vcpu *vcpu)
932{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300933#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400934 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300935#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936}
937
938static void save_host_msrs(struct kvm_vcpu *vcpu)
939{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300940#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400941 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300942#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943}
944
Rusty Russelle756fc62007-07-30 20:07:08 +1000945static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800946{
947 if (svm_data->next_asid > svm_data->max_asid) {
948 ++svm_data->asid_generation;
949 svm_data->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400950 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 }
952
Rusty Russelle756fc62007-07-30 20:07:08 +1000953 svm->vcpu.cpu = svm_data->cpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400954 svm->asid_generation = svm_data->asid_generation;
955 svm->vmcb->control.asid = svm_data->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956}
957
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
959{
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +0200960 unsigned long val = to_svm(vcpu)->db_regs[dr];
961 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
962 return val;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963}
964
965static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
966 int *exception)
967{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400968 struct vcpu_svm *svm = to_svm(vcpu);
969
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 *exception = 0;
971
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400972 if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
973 svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
974 svm->vmcb->save.dr6 |= DR6_BD_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975 *exception = DB_VECTOR;
976 return;
977 }
978
979 switch (dr) {
980 case 0 ... 3:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400981 svm->db_regs[dr] = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 return;
983 case 4 ... 5:
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800984 if (vcpu->arch.cr4 & X86_CR4_DE) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 *exception = UD_VECTOR;
986 return;
987 }
988 case 7: {
989 if (value & ~((1ULL << 32) - 1)) {
990 *exception = GP_VECTOR;
991 return;
992 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400993 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 return;
995 }
996 default:
997 printk(KERN_DEBUG "%s: unexpected dr %u\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800998 __func__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999 *exception = UD_VECTOR;
1000 return;
1001 }
1002}
1003
Rusty Russelle756fc62007-07-30 20:07:08 +10001004static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001006 u32 exit_int_info = svm->vmcb->control.exit_int_info;
Rusty Russelle756fc62007-07-30 20:07:08 +10001007 struct kvm *kvm = svm->vcpu.kvm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 u64 fault_address;
1009 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010
Eddie Dong85f455f2007-07-06 12:20:49 +03001011 if (!irqchip_in_kernel(kvm) &&
1012 is_external_interrupt(exit_int_info))
Rusty Russelle756fc62007-07-30 20:07:08 +10001013 push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001015 fault_address = svm->vmcb->control.exit_info_2;
1016 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001017
1018 if (!npt_enabled)
1019 KVMTRACE_3D(PAGE_FAULT, &svm->vcpu, error_code,
1020 (u32)fault_address, (u32)(fault_address >> 32),
1021 handler);
Joerg Roedeld2ebb412008-04-30 17:56:04 +02001022 else
1023 KVMTRACE_3D(TDP_FAULT, &svm->vcpu, error_code,
1024 (u32)fault_address, (u32)(fault_address >> 32),
1025 handler);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001026
Avi Kivity30677142007-10-28 18:48:59 +02001027 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028}
1029
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001030static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1031{
1032 int er;
1033
Sheng Yang571008d2008-01-02 14:49:22 +08001034 er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001035 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001036 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001037 return 1;
1038}
1039
Rusty Russelle756fc62007-07-30 20:07:08 +10001040static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001041{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001042 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001043 if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001044 svm->vmcb->save.cr0 &= ~X86_CR0_TS;
Rusty Russelle756fc62007-07-30 20:07:08 +10001045 svm->vcpu.fpu_active = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001046
1047 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001048}
1049
Joerg Roedel53371b52008-04-09 14:15:30 +02001050static int mc_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1051{
1052 /*
1053 * On an #MC intercept the MCE handler is not called automatically in
1054 * the host. So do it by hand here.
1055 */
1056 asm volatile (
1057 "int $0x12\n");
1058 /* not sure if we ever come back to this point */
1059
1060 return 1;
1061}
1062
Rusty Russelle756fc62007-07-30 20:07:08 +10001063static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001064{
1065 /*
1066 * VMCB is undefined after a SHUTDOWN intercept
1067 * so reinitialize it.
1068 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001069 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001070 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001071
1072 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1073 return 0;
1074}
1075
Rusty Russelle756fc62007-07-30 20:07:08 +10001076static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077{
Mike Dayd77c26f2007-10-08 09:02:08 -04001078 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Avi Kivity039576c2007-03-20 12:46:50 +02001079 int size, down, in, string, rep;
1080 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081
Rusty Russelle756fc62007-07-30 20:07:08 +10001082 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001084 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085
Laurent Viviere70669a2007-08-05 10:36:40 +03001086 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1087
1088 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001089 if (emulate_instruction(&svm->vcpu,
1090 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001091 return 0;
1092 return 1;
1093 }
1094
Avi Kivity039576c2007-03-20 12:46:50 +02001095 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1096 port = io_info >> 16;
1097 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity039576c2007-03-20 12:46:50 +02001098 rep = (io_info & SVM_IOIO_REP_MASK) != 0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001099 down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100
Laurent Vivier3090dd72007-08-05 10:43:32 +03001101 return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102}
1103
Joerg Roedelc47f0982008-04-30 17:56:00 +02001104static int nmi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1105{
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001106 KVMTRACE_0D(NMI, &svm->vcpu, handler);
Joerg Roedelc47f0982008-04-30 17:56:00 +02001107 return 1;
1108}
1109
Joerg Roedela0698052008-04-30 17:56:01 +02001110static int intr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1111{
1112 ++svm->vcpu.stat.irq_exits;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001113 KVMTRACE_0D(INTR, &svm->vcpu, handler);
Joerg Roedela0698052008-04-30 17:56:01 +02001114 return 1;
1115}
1116
Rusty Russelle756fc62007-07-30 20:07:08 +10001117static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118{
1119 return 1;
1120}
1121
Rusty Russelle756fc62007-07-30 20:07:08 +10001122static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001124 svm->next_rip = svm->vmcb->save.rip + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001125 skip_emulated_instruction(&svm->vcpu);
1126 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127}
1128
Rusty Russelle756fc62007-07-30 20:07:08 +10001129static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity02e235b2007-02-19 14:37:47 +02001130{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001131 svm->next_rip = svm->vmcb->save.rip + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001132 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001133 kvm_emulate_hypercall(&svm->vcpu);
1134 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001135}
1136
Rusty Russelle756fc62007-07-30 20:07:08 +10001137static int invalid_op_interception(struct vcpu_svm *svm,
1138 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001140 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 return 1;
1142}
1143
Rusty Russelle756fc62007-07-30 20:07:08 +10001144static int task_switch_interception(struct vcpu_svm *svm,
1145 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146{
Izik Eidus37817f22008-03-24 23:14:53 +02001147 u16 tss_selector;
1148
1149 tss_selector = (u16)svm->vmcb->control.exit_info_1;
1150 if (svm->vmcb->control.exit_info_2 &
1151 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
1152 return kvm_task_switch(&svm->vcpu, tss_selector,
1153 TASK_SWITCH_IRET);
1154 if (svm->vmcb->control.exit_info_2 &
1155 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
1156 return kvm_task_switch(&svm->vcpu, tss_selector,
1157 TASK_SWITCH_JMP);
1158 return kvm_task_switch(&svm->vcpu, tss_selector, TASK_SWITCH_CALL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159}
1160
Rusty Russelle756fc62007-07-30 20:07:08 +10001161static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001162{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001163 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001164 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001165 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166}
1167
Rusty Russelle756fc62007-07-30 20:07:08 +10001168static int emulate_on_interception(struct vcpu_svm *svm,
1169 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170{
Laurent Vivier34273182007-09-18 11:27:37 +02001171 if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001172 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 return 1;
1174}
1175
Joerg Roedel1d075432007-12-06 21:02:25 +01001176static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1177{
1178 emulate_instruction(&svm->vcpu, NULL, 0, 0, 0);
1179 if (irqchip_in_kernel(svm->vcpu.kvm))
1180 return 1;
1181 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
1182 return 0;
1183}
1184
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
1186{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001187 struct vcpu_svm *svm = to_svm(vcpu);
1188
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190 case MSR_IA32_TIME_STAMP_COUNTER: {
1191 u64 tsc;
1192
1193 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001194 *data = svm->vmcb->control.tsc_offset + tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195 break;
1196 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001197 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001198 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08001200#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001202 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203 break;
1204 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001205 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206 break;
1207 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001208 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001209 break;
1210 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001211 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 break;
1213#endif
1214 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001215 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216 break;
1217 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001218 *data = svm->vmcb->save.sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 break;
1220 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001221 *data = svm->vmcb->save.sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01001223 /* Nobody will change the following 5 values in the VMCB so
1224 we can safely return them on rdmsr. They will always be 0
1225 until LBRV is implemented. */
1226 case MSR_IA32_DEBUGCTLMSR:
1227 *data = svm->vmcb->save.dbgctl;
1228 break;
1229 case MSR_IA32_LASTBRANCHFROMIP:
1230 *data = svm->vmcb->save.br_from;
1231 break;
1232 case MSR_IA32_LASTBRANCHTOIP:
1233 *data = svm->vmcb->save.br_to;
1234 break;
1235 case MSR_IA32_LASTINTFROMIP:
1236 *data = svm->vmcb->save.last_excp_from;
1237 break;
1238 case MSR_IA32_LASTINTTOIP:
1239 *data = svm->vmcb->save.last_excp_to;
1240 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001242 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243 }
1244 return 0;
1245}
1246
Rusty Russelle756fc62007-07-30 20:07:08 +10001247static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001249 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001250 u64 data;
1251
Rusty Russelle756fc62007-07-30 20:07:08 +10001252 if (svm_get_msr(&svm->vcpu, ecx, &data))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001253 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254 else {
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001255 KVMTRACE_3D(MSR_READ, &svm->vcpu, ecx, (u32)data,
1256 (u32)(data >> 32), handler);
1257
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001258 svm->vmcb->save.rax = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001259 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001260 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001261 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262 }
1263 return 1;
1264}
1265
1266static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
1267{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001268 struct vcpu_svm *svm = to_svm(vcpu);
1269
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270 switch (ecx) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271 case MSR_IA32_TIME_STAMP_COUNTER: {
1272 u64 tsc;
1273
1274 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001275 svm->vmcb->control.tsc_offset = data - tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276 break;
1277 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08001278 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001279 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08001281#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001283 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284 break;
1285 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001286 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287 break;
1288 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001289 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001290 break;
1291 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001292 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293 break;
1294#endif
1295 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001296 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 break;
1298 case MSR_IA32_SYSENTER_EIP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001299 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300 break;
1301 case MSR_IA32_SYSENTER_ESP:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001302 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01001304 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001305 if (!svm_has(SVM_FEATURE_LBRV)) {
1306 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001307 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001308 break;
1309 }
1310 if (data & DEBUGCTL_RESERVED_BITS)
1311 return 1;
1312
1313 svm->vmcb->save.dbgctl = data;
1314 if (data & (1ULL<<0))
1315 svm_enable_lbrv(svm);
1316 else
1317 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01001318 break;
Joerg Roedel62b9aba2007-12-11 15:36:57 +01001319 case MSR_K7_EVNTSEL0:
1320 case MSR_K7_EVNTSEL1:
1321 case MSR_K7_EVNTSEL2:
1322 case MSR_K7_EVNTSEL3:
Chris Lalancette14ae51b2008-05-05 13:05:16 -04001323 case MSR_K7_PERFCTR0:
1324 case MSR_K7_PERFCTR1:
1325 case MSR_K7_PERFCTR2:
1326 case MSR_K7_PERFCTR3:
Joerg Roedel62b9aba2007-12-11 15:36:57 +01001327 /*
Chris Lalancette14ae51b2008-05-05 13:05:16 -04001328 * Just discard all writes to the performance counters; this
1329 * should keep both older linux and windows 64-bit guests
1330 * happy
Joerg Roedel62b9aba2007-12-11 15:36:57 +01001331 */
Chris Lalancette14ae51b2008-05-05 13:05:16 -04001332 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", ecx, data);
1333
Joerg Roedel62b9aba2007-12-11 15:36:57 +01001334 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001336 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337 }
1338 return 0;
1339}
1340
Rusty Russelle756fc62007-07-30 20:07:08 +10001341static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001342{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001343 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001344 u64 data = (svm->vmcb->save.rax & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001345 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001346
1347 KVMTRACE_3D(MSR_WRITE, &svm->vcpu, ecx, (u32)data, (u32)(data >> 32),
1348 handler);
1349
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001350 svm->next_rip = svm->vmcb->save.rip + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001351 if (svm_set_msr(&svm->vcpu, ecx, data))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02001352 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001354 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355 return 1;
1356}
1357
Rusty Russelle756fc62007-07-30 20:07:08 +10001358static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359{
Rusty Russelle756fc62007-07-30 20:07:08 +10001360 if (svm->vmcb->control.exit_info_1)
1361 return wrmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 else
Rusty Russelle756fc62007-07-30 20:07:08 +10001363 return rdmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364}
1365
Rusty Russelle756fc62007-07-30 20:07:08 +10001366static int interrupt_window_interception(struct vcpu_svm *svm,
Dor Laorc1150d82007-01-05 16:36:24 -08001367 struct kvm_run *kvm_run)
1368{
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001369 KVMTRACE_0D(PEND_INTR, &svm->vcpu, handler);
1370
Eddie Dong85f455f2007-07-06 12:20:49 +03001371 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1372 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08001373 /*
1374 * If the user space waits to inject interrupts, exit as soon as
1375 * possible
1376 */
1377 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001378 !svm->vcpu.arch.irq_summary) {
Rusty Russelle756fc62007-07-30 20:07:08 +10001379 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08001380 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
1381 return 0;
1382 }
1383
1384 return 1;
1385}
1386
Rusty Russelle756fc62007-07-30 20:07:08 +10001387static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 struct kvm_run *kvm_run) = {
1389 [SVM_EXIT_READ_CR0] = emulate_on_interception,
1390 [SVM_EXIT_READ_CR3] = emulate_on_interception,
1391 [SVM_EXIT_READ_CR4] = emulate_on_interception,
Avi Kivity80a81192007-12-06 19:50:00 +02001392 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393 /* for now: */
1394 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
1395 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
1396 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
Joerg Roedel1d075432007-12-06 21:02:25 +01001397 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398 [SVM_EXIT_READ_DR0] = emulate_on_interception,
1399 [SVM_EXIT_READ_DR1] = emulate_on_interception,
1400 [SVM_EXIT_READ_DR2] = emulate_on_interception,
1401 [SVM_EXIT_READ_DR3] = emulate_on_interception,
1402 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
1403 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
1404 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
1405 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
1406 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
1407 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001408 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Anthony Liguori7807fa62007-04-23 09:17:21 -05001410 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
Joerg Roedel53371b52008-04-09 14:15:30 +02001411 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Joerg Roedela0698052008-04-30 17:56:01 +02001412 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02001413 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414 [SVM_EXIT_SMI] = nop_on_interception,
1415 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08001416 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
1418 [SVM_EXIT_CPUID] = cpuid_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001419 [SVM_EXIT_INVD] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420 [SVM_EXIT_HLT] = halt_interception,
1421 [SVM_EXIT_INVLPG] = emulate_on_interception,
1422 [SVM_EXIT_INVLPGA] = invalid_op_interception,
1423 [SVM_EXIT_IOIO] = io_interception,
1424 [SVM_EXIT_MSR] = msr_interception,
1425 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001426 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427 [SVM_EXIT_VMRUN] = invalid_op_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02001428 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 [SVM_EXIT_VMLOAD] = invalid_op_interception,
1430 [SVM_EXIT_VMSAVE] = invalid_op_interception,
1431 [SVM_EXIT_STGI] = invalid_op_interception,
1432 [SVM_EXIT_CLGI] = invalid_op_interception,
1433 [SVM_EXIT_SKINIT] = invalid_op_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02001434 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01001435 [SVM_EXIT_MONITOR] = invalid_op_interception,
1436 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001437 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438};
1439
Avi Kivity04d2cc72007-09-10 18:10:54 +03001440static int handle_exit(struct kvm_run *kvm_run, 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);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001443 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001445 KVMTRACE_3D(VMEXIT, vcpu, exit_code, (u32)svm->vmcb->save.rip,
1446 (u32)((u64)svm->vmcb->save.rip >> 32), entryexit);
1447
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001448 if (npt_enabled) {
1449 int mmu_reload = 0;
1450 if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
1451 svm_set_cr0(vcpu, svm->vmcb->save.cr0);
1452 mmu_reload = 1;
1453 }
1454 vcpu->arch.cr0 = svm->vmcb->save.cr0;
1455 vcpu->arch.cr3 = svm->vmcb->save.cr3;
1456 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1457 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1458 kvm_inject_gp(vcpu, 0);
1459 return 1;
1460 }
1461 }
1462 if (mmu_reload) {
1463 kvm_mmu_reset_context(vcpu);
1464 kvm_mmu_load(vcpu);
1465 }
1466 }
1467
Avi Kivity04d2cc72007-09-10 18:10:54 +03001468 kvm_reput_irq(svm);
1469
1470 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
1471 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
1472 kvm_run->fail_entry.hardware_entry_failure_reason
1473 = svm->vmcb->control.exit_code;
1474 return 0;
1475 }
1476
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001477 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001478 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1479 exit_code != SVM_EXIT_NPF)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
1481 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001482 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 exit_code);
1484
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02001485 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08001486 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03001488 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489 return 0;
1490 }
1491
Rusty Russelle756fc62007-07-30 20:07:08 +10001492 return svm_exit_handlers[exit_code](svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493}
1494
1495static void reload_tss(struct kvm_vcpu *vcpu)
1496{
1497 int cpu = raw_smp_processor_id();
1498
1499 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04001500 svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 load_TR_desc();
1502}
1503
Rusty Russelle756fc62007-07-30 20:07:08 +10001504static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505{
1506 int cpu = raw_smp_processor_id();
1507
1508 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
1509
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001510 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Rusty Russelle756fc62007-07-30 20:07:08 +10001511 if (svm->vcpu.cpu != cpu ||
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001512 svm->asid_generation != svm_data->asid_generation)
Rusty Russelle756fc62007-07-30 20:07:08 +10001513 new_asid(svm, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514}
1515
1516
Eddie Dong85f455f2007-07-06 12:20:49 +03001517static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518{
1519 struct vmcb_control_area *control;
1520
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001521 KVMTRACE_1D(INJ_VIRQ, &svm->vcpu, (u32)irq, handler);
1522
Rusty Russelle756fc62007-07-30 20:07:08 +10001523 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03001524 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525 control->int_ctl &= ~V_INTR_PRIO_MASK;
1526 control->int_ctl |= V_IRQ_MASK |
1527 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1528}
1529
Eddie Dong2a8067f2007-08-06 16:29:07 +03001530static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
1531{
1532 struct vcpu_svm *svm = to_svm(vcpu);
1533
1534 svm_inject_irq(svm, irq);
1535}
1536
Joerg Roedelaaacfc92008-04-16 16:51:18 +02001537static void update_cr8_intercept(struct kvm_vcpu *vcpu)
1538{
1539 struct vcpu_svm *svm = to_svm(vcpu);
1540 struct vmcb *vmcb = svm->vmcb;
1541 int max_irr, tpr;
1542
1543 if (!irqchip_in_kernel(vcpu->kvm) || vcpu->arch.apic->vapic_addr)
1544 return;
1545
1546 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
1547
1548 max_irr = kvm_lapic_find_highest_irr(vcpu);
1549 if (max_irr == -1)
1550 return;
1551
1552 tpr = kvm_lapic_get_cr8(vcpu) << 4;
1553
1554 if (tpr >= (max_irr & 0xf0))
1555 vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
1556}
1557
Avi Kivity04d2cc72007-09-10 18:10:54 +03001558static void svm_intr_assist(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001560 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001561 struct vmcb *vmcb = svm->vmcb;
1562 int intr_vector = -1;
1563
1564 if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
1565 ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
1566 intr_vector = vmcb->control.exit_int_info &
1567 SVM_EVTINJ_VEC_MASK;
1568 vmcb->control.exit_int_info = 0;
1569 svm_inject_irq(svm, intr_vector);
Joerg Roedelaaacfc92008-04-16 16:51:18 +02001570 goto out;
Eddie Dong85f455f2007-07-06 12:20:49 +03001571 }
1572
1573 if (vmcb->control.int_ctl & V_IRQ_MASK)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02001574 goto out;
Eddie Dong85f455f2007-07-06 12:20:49 +03001575
Eddie Dong1b9778d2007-09-03 16:56:58 +03001576 if (!kvm_cpu_has_interrupt(vcpu))
Joerg Roedelaaacfc92008-04-16 16:51:18 +02001577 goto out;
Eddie Dong85f455f2007-07-06 12:20:49 +03001578
1579 if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
1580 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
1581 (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
1582 /* unable to deliver irq, set pending irq */
1583 vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
1584 svm_inject_irq(svm, 0x0);
Joerg Roedelaaacfc92008-04-16 16:51:18 +02001585 goto out;
Eddie Dong85f455f2007-07-06 12:20:49 +03001586 }
1587 /* Okay, we can deliver the interrupt: grab it and update PIC state. */
Eddie Dong1b9778d2007-09-03 16:56:58 +03001588 intr_vector = kvm_cpu_get_interrupt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001589 svm_inject_irq(svm, intr_vector);
Eddie Dong1b9778d2007-09-03 16:56:58 +03001590 kvm_timer_intr_post(vcpu, intr_vector);
Joerg Roedelaaacfc92008-04-16 16:51:18 +02001591out:
1592 update_cr8_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03001593}
1594
1595static void kvm_reput_irq(struct vcpu_svm *svm)
1596{
Rusty Russelle756fc62007-07-30 20:07:08 +10001597 struct vmcb_control_area *control = &svm->vmcb->control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598
Eddie Dong7017fc32007-07-18 11:34:57 +03001599 if ((control->int_ctl & V_IRQ_MASK)
1600 && !irqchip_in_kernel(svm->vcpu.kvm)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601 control->int_ctl &= ~V_IRQ_MASK;
Rusty Russelle756fc62007-07-30 20:07:08 +10001602 push_irq(&svm->vcpu, control->int_vector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 }
Dor Laorc1150d82007-01-05 16:36:24 -08001604
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001605 svm->vcpu.arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001606 !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
1607}
1608
Eddie Dong85f455f2007-07-06 12:20:49 +03001609static void svm_do_inject_vector(struct vcpu_svm *svm)
1610{
1611 struct kvm_vcpu *vcpu = &svm->vcpu;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001612 int word_index = __ffs(vcpu->arch.irq_summary);
1613 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Eddie Dong85f455f2007-07-06 12:20:49 +03001614 int irq = word_index * BITS_PER_LONG + bit_index;
1615
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001616 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
1617 if (!vcpu->arch.irq_pending[word_index])
1618 clear_bit(word_index, &vcpu->arch.irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03001619 svm_inject_irq(svm, irq);
1620}
1621
Avi Kivity04d2cc72007-09-10 18:10:54 +03001622static void do_interrupt_requests(struct kvm_vcpu *vcpu,
Dor Laorc1150d82007-01-05 16:36:24 -08001623 struct kvm_run *kvm_run)
1624{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001625 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001626 struct vmcb_control_area *control = &svm->vmcb->control;
Dor Laorc1150d82007-01-05 16:36:24 -08001627
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001628 svm->vcpu.arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001629 (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001630 (svm->vmcb->save.rflags & X86_EFLAGS_IF));
Dor Laorc1150d82007-01-05 16:36:24 -08001631
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001632 if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary)
Dor Laorc1150d82007-01-05 16:36:24 -08001633 /*
1634 * If interrupts enabled, and not blocked by sti or mov ss. Good.
1635 */
Eddie Dong85f455f2007-07-06 12:20:49 +03001636 svm_do_inject_vector(svm);
Dor Laorc1150d82007-01-05 16:36:24 -08001637
1638 /*
1639 * Interrupts blocked. Wait for unblock.
1640 */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001641 if (!svm->vcpu.arch.interrupt_window_open &&
1642 (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window))
Dor Laorc1150d82007-01-05 16:36:24 -08001643 control->intercept |= 1ULL << INTERCEPT_VINTR;
Mike Dayd77c26f2007-10-08 09:02:08 -04001644 else
Dor Laorc1150d82007-01-05 16:36:24 -08001645 control->intercept &= ~(1ULL << INTERCEPT_VINTR);
1646}
1647
Izik Eiduscbc94022007-10-25 00:29:55 +02001648static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
1649{
1650 return 0;
1651}
1652
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653static void save_db_regs(unsigned long *db_regs)
1654{
Avi Kivity5aff4582006-12-13 00:33:45 -08001655 asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
1656 asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
1657 asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
1658 asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659}
1660
1661static void load_db_regs(unsigned long *db_regs)
1662{
Avi Kivity5aff4582006-12-13 00:33:45 -08001663 asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
1664 asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
1665 asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
1666 asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667}
1668
Avi Kivityd9e368d2007-06-07 19:18:30 +03001669static void svm_flush_tlb(struct kvm_vcpu *vcpu)
1670{
1671 force_new_asid(vcpu);
1672}
1673
Avi Kivity04d2cc72007-09-10 18:10:54 +03001674static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
1675{
1676}
1677
Joerg Roedeld7bf8222008-04-16 16:51:17 +02001678static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
1679{
1680 struct vcpu_svm *svm = to_svm(vcpu);
1681
1682 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
1683 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
1684 kvm_lapic_set_tpr(vcpu, cr8);
1685 }
1686}
1687
Joerg Roedel649d6862008-04-16 16:51:15 +02001688static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
1689{
1690 struct vcpu_svm *svm = to_svm(vcpu);
1691 u64 cr8;
1692
1693 if (!irqchip_in_kernel(vcpu->kvm))
1694 return;
1695
1696 cr8 = kvm_get_cr8(vcpu);
1697 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
1698 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
1699}
1700
Avi Kivity04d2cc72007-09-10 18:10:54 +03001701static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001703 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704 u16 fs_selector;
1705 u16 gs_selector;
1706 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03001707
Rusty Russelle756fc62007-07-30 20:07:08 +10001708 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709
Joerg Roedel649d6862008-04-16 16:51:15 +02001710 sync_lapic_to_cr8(vcpu);
1711
Avi Kivity6aa8b732006-12-10 02:21:36 -08001712 save_host_msrs(vcpu);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001713 fs_selector = kvm_read_fs();
1714 gs_selector = kvm_read_gs();
1715 ldt_selector = kvm_read_ldt();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001716 svm->host_cr2 = kvm_read_cr2();
1717 svm->host_dr6 = read_dr6();
1718 svm->host_dr7 = read_dr7();
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001719 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001720 /* required for live migration with NPT */
1721 if (npt_enabled)
1722 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001724 if (svm->vmcb->save.dr7 & 0xff) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725 write_dr7(0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001726 save_db_regs(svm->host_db_regs);
1727 load_db_regs(svm->db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001728 }
Avi Kivity36241b82006-12-22 01:05:20 -08001729
Avi Kivity04d2cc72007-09-10 18:10:54 +03001730 clgi();
1731
1732 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08001733
Avi Kivity6aa8b732006-12-10 02:21:36 -08001734 asm volatile (
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001735#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02001736 "push %%rbp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737#else
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001738 "push %%ebp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739#endif
1740
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001741#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001742 "mov %c[rbx](%[svm]), %%rbx \n\t"
1743 "mov %c[rcx](%[svm]), %%rcx \n\t"
1744 "mov %c[rdx](%[svm]), %%rdx \n\t"
1745 "mov %c[rsi](%[svm]), %%rsi \n\t"
1746 "mov %c[rdi](%[svm]), %%rdi \n\t"
1747 "mov %c[rbp](%[svm]), %%rbp \n\t"
1748 "mov %c[r8](%[svm]), %%r8 \n\t"
1749 "mov %c[r9](%[svm]), %%r9 \n\t"
1750 "mov %c[r10](%[svm]), %%r10 \n\t"
1751 "mov %c[r11](%[svm]), %%r11 \n\t"
1752 "mov %c[r12](%[svm]), %%r12 \n\t"
1753 "mov %c[r13](%[svm]), %%r13 \n\t"
1754 "mov %c[r14](%[svm]), %%r14 \n\t"
1755 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001756#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001757 "mov %c[rbx](%[svm]), %%ebx \n\t"
1758 "mov %c[rcx](%[svm]), %%ecx \n\t"
1759 "mov %c[rdx](%[svm]), %%edx \n\t"
1760 "mov %c[rsi](%[svm]), %%esi \n\t"
1761 "mov %c[rdi](%[svm]), %%edi \n\t"
1762 "mov %c[rbp](%[svm]), %%ebp \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001763#endif
1764
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001765#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001766 /* Enter guest mode */
1767 "push %%rax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001768 "mov %c[vmcb](%[svm]), %%rax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001769 __ex(SVM_VMLOAD) "\n\t"
1770 __ex(SVM_VMRUN) "\n\t"
1771 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772 "pop %%rax \n\t"
1773#else
1774 /* Enter guest mode */
1775 "push %%eax \n\t"
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001776 "mov %c[vmcb](%[svm]), %%eax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001777 __ex(SVM_VMLOAD) "\n\t"
1778 __ex(SVM_VMRUN) "\n\t"
1779 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780 "pop %%eax \n\t"
1781#endif
1782
1783 /* Save guest registers, load host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001784#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001785 "mov %%rbx, %c[rbx](%[svm]) \n\t"
1786 "mov %%rcx, %c[rcx](%[svm]) \n\t"
1787 "mov %%rdx, %c[rdx](%[svm]) \n\t"
1788 "mov %%rsi, %c[rsi](%[svm]) \n\t"
1789 "mov %%rdi, %c[rdi](%[svm]) \n\t"
1790 "mov %%rbp, %c[rbp](%[svm]) \n\t"
1791 "mov %%r8, %c[r8](%[svm]) \n\t"
1792 "mov %%r9, %c[r9](%[svm]) \n\t"
1793 "mov %%r10, %c[r10](%[svm]) \n\t"
1794 "mov %%r11, %c[r11](%[svm]) \n\t"
1795 "mov %%r12, %c[r12](%[svm]) \n\t"
1796 "mov %%r13, %c[r13](%[svm]) \n\t"
1797 "mov %%r14, %c[r14](%[svm]) \n\t"
1798 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001799
Laurent Vivier54a08c02007-10-25 14:18:53 +02001800 "pop %%rbp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801#else
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001802 "mov %%ebx, %c[rbx](%[svm]) \n\t"
1803 "mov %%ecx, %c[rcx](%[svm]) \n\t"
1804 "mov %%edx, %c[rdx](%[svm]) \n\t"
1805 "mov %%esi, %c[rsi](%[svm]) \n\t"
1806 "mov %%edi, %c[rdi](%[svm]) \n\t"
1807 "mov %%ebp, %c[rbp](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001808
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001809 "pop %%ebp; \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08001810#endif
1811 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001812 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08001813 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001814 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
1815 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
1816 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
1817 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
1818 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
1819 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001820#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001821 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
1822 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
1823 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
1824 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
1825 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
1826 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
1827 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
1828 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02001830 : "cc", "memory"
1831#ifdef CONFIG_X86_64
1832 , "rbx", "rcx", "rdx", "rsi", "rdi"
1833 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Laurent Vivierfe7935d2007-10-25 14:18:54 +02001834#else
1835 , "ebx", "ecx", "edx" , "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02001836#endif
1837 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001839 if ((svm->vmcb->save.dr7 & 0xff))
1840 load_db_regs(svm->host_db_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001842 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001844 write_dr6(svm->host_dr6);
1845 write_dr7(svm->host_dr7);
1846 kvm_write_cr2(svm->host_cr2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001848 kvm_load_fs(fs_selector);
1849 kvm_load_gs(gs_selector);
1850 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001851 load_host_msrs(vcpu);
1852
1853 reload_tss(vcpu);
1854
Avi Kivity56ba47d2007-11-07 17:14:18 +02001855 local_irq_disable();
1856
1857 stgi();
1858
Joerg Roedeld7bf8222008-04-16 16:51:17 +02001859 sync_cr8_to_lapic(vcpu);
1860
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001861 svm->next_rip = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862}
1863
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
1865{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001866 struct vcpu_svm *svm = to_svm(vcpu);
1867
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001868 if (npt_enabled) {
1869 svm->vmcb->control.nested_cr3 = root;
1870 force_new_asid(vcpu);
1871 return;
1872 }
1873
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001874 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875 force_new_asid(vcpu);
Anthony Liguori7807fa62007-04-23 09:17:21 -05001876
1877 if (vcpu->fpu_active) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001878 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
1879 svm->vmcb->save.cr0 |= X86_CR0_TS;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001880 vcpu->fpu_active = 0;
1881 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001882}
1883
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884static int is_disabled(void)
1885{
Joerg Roedel6031a612007-06-22 12:29:50 +03001886 u64 vm_cr;
1887
1888 rdmsrl(MSR_VM_CR, vm_cr);
1889 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
1890 return 1;
1891
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892 return 0;
1893}
1894
Ingo Molnar102d8322007-02-19 14:37:47 +02001895static void
1896svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1897{
1898 /*
1899 * Patch in the VMMCALL instruction:
1900 */
1901 hypercall[0] = 0x0f;
1902 hypercall[1] = 0x01;
1903 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02001904}
1905
Yang, Sheng002c7f72007-07-31 14:23:01 +03001906static void svm_check_processor_compat(void *rtn)
1907{
1908 *(int *)rtn = 0;
1909}
1910
Avi Kivity774ead32007-12-26 13:57:04 +02001911static bool svm_cpu_has_accelerated_tpr(void)
1912{
1913 return false;
1914}
1915
Sheng Yang67253af2008-04-25 10:20:22 +08001916static int get_npt_level(void)
1917{
1918#ifdef CONFIG_X86_64
1919 return PT64_ROOT_LEVEL;
1920#else
1921 return PT32E_ROOT_LEVEL;
1922#endif
1923}
1924
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001925static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926 .cpu_has_kvm_support = has_svm,
1927 .disabled_by_bios = is_disabled,
1928 .hardware_setup = svm_hardware_setup,
1929 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03001930 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931 .hardware_enable = svm_hardware_enable,
1932 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02001933 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934
1935 .vcpu_create = svm_create_vcpu,
1936 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001937 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938
Avi Kivity04d2cc72007-09-10 18:10:54 +03001939 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940 .vcpu_load = svm_vcpu_load,
1941 .vcpu_put = svm_vcpu_put,
1942
1943 .set_guest_debug = svm_guest_debug,
1944 .get_msr = svm_get_msr,
1945 .set_msr = svm_set_msr,
1946 .get_segment_base = svm_get_segment_base,
1947 .get_segment = svm_get_segment,
1948 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02001949 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10001950 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03001951 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953 .set_cr3 = svm_set_cr3,
1954 .set_cr4 = svm_set_cr4,
1955 .set_efer = svm_set_efer,
1956 .get_idt = svm_get_idt,
1957 .set_idt = svm_set_idt,
1958 .get_gdt = svm_get_gdt,
1959 .set_gdt = svm_set_gdt,
1960 .get_dr = svm_get_dr,
1961 .set_dr = svm_set_dr,
1962 .cache_regs = svm_cache_regs,
1963 .decache_regs = svm_decache_regs,
1964 .get_rflags = svm_get_rflags,
1965 .set_rflags = svm_set_rflags,
1966
Avi Kivity6aa8b732006-12-10 02:21:36 -08001967 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001968
Avi Kivity6aa8b732006-12-10 02:21:36 -08001969 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001970 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001971 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02001972 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03001973 .get_irq = svm_get_irq,
1974 .set_irq = svm_set_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02001975 .queue_exception = svm_queue_exception,
1976 .exception_injected = svm_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03001977 .inject_pending_irq = svm_intr_assist,
1978 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02001979
1980 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08001981 .get_tdp_level = get_npt_level,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982};
1983
1984static int __init svm_init(void)
1985{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001986 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10001987 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988}
1989
1990static void __exit svm_exit(void)
1991{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08001992 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001993}
1994
1995module_init(svm_init)
1996module_exit(svm_exit)