blob: 28b981409a8fea9f933fca08e9f7a68663f1583e [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
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030020#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020021#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040022
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/vmalloc.h>
26#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Avi Kivitye4956062007-06-28 14:15:57 -040029#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030
Eduardo Habkost63d11422008-11-17 19:03:20 -020031#include <asm/virtext.h>
32
Avi Kivity4ecac3f2008-05-13 13:23:38 +030033#define __ex(x) __kvm_handle_fault_on_reboot(x)
34
Avi Kivity6aa8b732006-12-10 02:21:36 -080035MODULE_AUTHOR("Qumranet");
36MODULE_LICENSE("GPL");
37
38#define IOPM_ALLOC_ORDER 2
39#define MSRPM_ALLOC_ORDER 1
40
Avi Kivity6aa8b732006-12-10 02:21:36 -080041#define SEG_TYPE_LDT 2
42#define SEG_TYPE_BUSY_TSS16 3
43
Joerg Roedel80b77062007-03-30 17:02:14 +030044#define SVM_FEATURE_NPT (1 << 0)
45#define SVM_FEATURE_LBRV (1 << 1)
Amit Shah94c935a12008-08-18 13:11:46 +030046#define SVM_FEATURE_SVML (1 << 2)
Joerg Roedel80b77062007-03-30 17:02:14 +030047
Joerg Roedel24e09cb2008-02-13 18:58:47 +010048#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
49
Alexander Grafc0725422008-11-25 20:17:03 +010050/* Turn on to get debugging output*/
51/* #define NESTED_DEBUG */
52
53#ifdef NESTED_DEBUG
54#define nsvm_printk(fmt, args...) printk(KERN_INFO fmt, ## args)
55#else
56#define nsvm_printk(fmt, args...) do {} while(0)
57#endif
58
Avi Kivity6c8166a2009-05-31 18:15:37 +030059static const u32 host_save_user_msrs[] = {
60#ifdef CONFIG_X86_64
61 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
62 MSR_FS_BASE,
63#endif
64 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
65};
66
67#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
68
69struct kvm_vcpu;
70
71struct vcpu_svm {
72 struct kvm_vcpu vcpu;
73 struct vmcb *vmcb;
74 unsigned long vmcb_pa;
75 struct svm_cpu_data *svm_data;
76 uint64_t asid_generation;
77 uint64_t sysenter_esp;
78 uint64_t sysenter_eip;
79
80 u64 next_rip;
81
82 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
83 u64 host_gs_base;
84 unsigned long host_cr2;
85
86 u32 *msrpm;
87 struct vmcb *hsave;
88 u64 hsave_msr;
89
90 u64 nested_vmcb;
91
92 /* These are the merged vectors */
93 u32 *nested_msrpm;
94
95 /* gpa pointers to the real vectors */
96 u64 nested_vmcb_msrpm;
97};
98
Joerg Roedel709ddeb2008-02-07 13:47:45 +010099/* enable NPT for AMD64 and X86 with PAE */
100#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
101static bool npt_enabled = true;
102#else
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100103static bool npt_enabled = false;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100104#endif
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100105static int npt = 1;
106
107module_param(npt, int, S_IRUGO);
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100108
Alexander Graf236de052008-11-25 20:17:10 +0100109static int nested = 0;
110module_param(nested, int, S_IRUGO);
111
Joerg Roedel44874f82008-08-27 14:18:43 +0200112static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +0300113
Alexander Grafcf74a782008-11-25 20:17:08 +0100114static int nested_svm_exit_handled(struct vcpu_svm *svm, bool kvm_override);
115static int nested_svm_vmexit(struct vcpu_svm *svm);
116static int nested_svm_vmsave(struct vcpu_svm *svm, void *nested_vmcb,
117 void *arg2, void *opaque);
118static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
119 bool has_error_code, u32 error_code);
120
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400121static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
122{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000123 return container_of(vcpu, struct vcpu_svm, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400124}
125
Alexander Graf3d6368e2008-11-25 20:17:07 +0100126static inline bool is_nested(struct vcpu_svm *svm)
127{
128 return svm->nested_vmcb;
129}
130
Harvey Harrison4866d5e2008-02-19 10:32:02 -0800131static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132
133struct kvm_ldttss_desc {
134 u16 limit0;
135 u16 base0;
136 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
137 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
138 u32 base3;
139 u32 zero1;
140} __attribute__((packed));
141
142struct svm_cpu_data {
143 int cpu;
144
Avi Kivity5008fdf2007-04-02 13:05:50 +0300145 u64 asid_generation;
146 u32 max_asid;
147 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148 struct kvm_ldttss_desc *tss_desc;
149
150 struct page *save_area;
151};
152
153static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300154static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155
156struct svm_init_data {
157 int cpu;
158 int r;
159};
160
161static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
162
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200163#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164#define MSRS_RANGE_SIZE 2048
165#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
166
167#define MAX_INST_SIZE 15
168
Joerg Roedel80b77062007-03-30 17:02:14 +0300169static inline u32 svm_has(u32 feat)
170{
171 return svm_features & feat;
172}
173
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174static inline void clgi(void)
175{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300176 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177}
178
179static inline void stgi(void)
180{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300181 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182}
183
184static inline void invlpga(unsigned long addr, u32 asid)
185{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300186 asm volatile (__ex(SVM_INVLPGA) :: "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187}
188
189static inline unsigned long kvm_read_cr2(void)
190{
191 unsigned long cr2;
192
193 asm volatile ("mov %%cr2, %0" : "=r" (cr2));
194 return cr2;
195}
196
197static inline void kvm_write_cr2(unsigned long val)
198{
199 asm volatile ("mov %0, %%cr2" :: "r" (val));
200}
201
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202static inline void force_new_asid(struct kvm_vcpu *vcpu)
203{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400204 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205}
206
207static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
208{
209 force_new_asid(vcpu);
210}
211
212static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
213{
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100214 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600215 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216
Alexander Graf9962d032008-11-25 20:17:02 +0100217 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800218 vcpu->arch.shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800219}
220
Avi Kivity298101d2007-11-25 13:41:11 +0200221static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
222 bool has_error_code, u32 error_code)
223{
224 struct vcpu_svm *svm = to_svm(vcpu);
225
Alexander Grafcf74a782008-11-25 20:17:08 +0100226 /* If we are within a nested VM we'd better #VMEXIT and let the
227 guest handle the exception */
228 if (nested_svm_check_exception(svm, nr, has_error_code, error_code))
229 return;
230
Avi Kivity298101d2007-11-25 13:41:11 +0200231 svm->vmcb->control.event_inj = nr
232 | SVM_EVTINJ_VALID
233 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
234 | SVM_EVTINJ_TYPE_EXEPT;
235 svm->vmcb->control.event_inj_err = error_code;
236}
237
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238static int is_external_interrupt(u32 info)
239{
240 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
241 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
242}
243
Glauber Costa2809f5d2009-05-12 16:21:05 -0400244static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
245{
246 struct vcpu_svm *svm = to_svm(vcpu);
247 u32 ret = 0;
248
249 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
250 ret |= X86_SHADOW_INT_STI | X86_SHADOW_INT_MOV_SS;
251 return ret & mask;
252}
253
254static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
255{
256 struct vcpu_svm *svm = to_svm(vcpu);
257
258 if (mask == 0)
259 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
260 else
261 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
262
263}
264
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
266{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400267 struct vcpu_svm *svm = to_svm(vcpu);
268
269 if (!svm->next_rip) {
Gleb Natapovf629cf82009-05-11 13:35:49 +0300270 if (emulate_instruction(vcpu, vcpu->run, 0, 0, EMULTYPE_SKIP) !=
271 EMULATE_DONE)
272 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273 return;
274 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300275 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
276 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
277 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300279 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400280 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800281}
282
283static int has_svm(void)
284{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200285 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286
Eduardo Habkost63d11422008-11-17 19:03:20 -0200287 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800288 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289 return 0;
290 }
291
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292 return 1;
293}
294
295static void svm_hardware_disable(void *garbage)
296{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200297 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298}
299
300static void svm_hardware_enable(void *garbage)
301{
302
303 struct svm_cpu_data *svm_data;
304 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306 struct desc_struct *gdt;
307 int me = raw_smp_processor_id();
308
309 if (!has_svm()) {
310 printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
311 return;
312 }
313 svm_data = per_cpu(svm_data, me);
314
315 if (!svm_data) {
316 printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
317 me);
318 return;
319 }
320
321 svm_data->asid_generation = 1;
322 svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
323 svm_data->next_asid = svm_data->max_asid + 1;
324
Mike Dayd77c26f2007-10-08 09:02:08 -0400325 asm volatile ("sgdt %0" : "=m"(gdt_descr));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326 gdt = (struct desc_struct *)gdt_descr.address;
327 svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
328
329 rdmsrl(MSR_EFER, efer);
Alexander Graf9962d032008-11-25 20:17:02 +0100330 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331
332 wrmsrl(MSR_VM_HSAVE_PA,
333 page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
334}
335
Joerg Roedel0da1db752008-07-02 16:02:11 +0200336static void svm_cpu_uninit(int cpu)
337{
338 struct svm_cpu_data *svm_data
339 = per_cpu(svm_data, raw_smp_processor_id());
340
341 if (!svm_data)
342 return;
343
344 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
345 __free_page(svm_data->save_area);
346 kfree(svm_data);
347}
348
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349static int svm_cpu_init(int cpu)
350{
351 struct svm_cpu_data *svm_data;
352 int r;
353
354 svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
355 if (!svm_data)
356 return -ENOMEM;
357 svm_data->cpu = cpu;
358 svm_data->save_area = alloc_page(GFP_KERNEL);
359 r = -ENOMEM;
360 if (!svm_data->save_area)
361 goto err_1;
362
363 per_cpu(svm_data, cpu) = svm_data;
364
365 return 0;
366
367err_1:
368 kfree(svm_data);
369 return r;
370
371}
372
Rusty Russellbfc733a2007-07-31 20:42:42 +1000373static void set_msr_interception(u32 *msrpm, unsigned msr,
374 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375{
376 int i;
377
378 for (i = 0; i < NUM_MSR_MAPS; i++) {
379 if (msr >= msrpm_ranges[i] &&
380 msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
381 u32 msr_offset = (i * MSRS_IN_RANGE + msr -
382 msrpm_ranges[i]) * 2;
383
384 u32 *base = msrpm + (msr_offset / 32);
385 u32 msr_shift = msr_offset % 32;
386 u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
387 *base = (*base & ~(0x3 << msr_shift)) |
388 (mask << msr_shift);
Rusty Russellbfc733a2007-07-31 20:42:42 +1000389 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390 }
391 }
Rusty Russellbfc733a2007-07-31 20:42:42 +1000392 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393}
394
Joerg Roedelf65c2292008-02-13 18:58:46 +0100395static void svm_vcpu_init_msrpm(u32 *msrpm)
396{
397 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
398
399#ifdef CONFIG_X86_64
400 set_msr_interception(msrpm, MSR_GS_BASE, 1, 1);
401 set_msr_interception(msrpm, MSR_FS_BASE, 1, 1);
402 set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1);
403 set_msr_interception(msrpm, MSR_LSTAR, 1, 1);
404 set_msr_interception(msrpm, MSR_CSTAR, 1, 1);
405 set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1);
406#endif
407 set_msr_interception(msrpm, MSR_K6_STAR, 1, 1);
408 set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100409}
410
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100411static void svm_enable_lbrv(struct vcpu_svm *svm)
412{
413 u32 *msrpm = svm->msrpm;
414
415 svm->vmcb->control.lbr_ctl = 1;
416 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
417 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
418 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
419 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
420}
421
422static void svm_disable_lbrv(struct vcpu_svm *svm)
423{
424 u32 *msrpm = svm->msrpm;
425
426 svm->vmcb->control.lbr_ctl = 0;
427 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
428 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
429 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
430 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
431}
432
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433static __init int svm_hardware_setup(void)
434{
435 int cpu;
436 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100437 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438 int r;
439
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
441
442 if (!iopm_pages)
443 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300444
445 iopm_va = page_address(iopm_pages);
446 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800447 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
448
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100449 if (boot_cpu_has(X86_FEATURE_NX))
450 kvm_enable_efer_bits(EFER_NX);
451
Alexander Graf1b2fd702009-02-02 16:23:51 +0100452 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
453 kvm_enable_efer_bits(EFER_FFXSR);
454
Alexander Graf236de052008-11-25 20:17:10 +0100455 if (nested) {
456 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
457 kvm_enable_efer_bits(EFER_SVME);
458 }
459
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 for_each_online_cpu(cpu) {
461 r = svm_cpu_init(cpu);
462 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100463 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100465
466 svm_features = cpuid_edx(SVM_CPUID_FUNC);
467
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100468 if (!svm_has(SVM_FEATURE_NPT))
469 npt_enabled = false;
470
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100471 if (npt_enabled && !npt) {
472 printk(KERN_INFO "kvm: Nested Paging disabled\n");
473 npt_enabled = false;
474 }
475
Joerg Roedel18552672008-02-07 13:47:41 +0100476 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100477 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100478 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200479 } else
480 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100481
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482 return 0;
483
Joerg Roedelf65c2292008-02-13 18:58:46 +0100484err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
486 iopm_base = 0;
487 return r;
488}
489
490static __exit void svm_hardware_unsetup(void)
491{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200492 int cpu;
493
494 for_each_online_cpu(cpu)
495 svm_cpu_uninit(cpu);
496
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100498 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499}
500
501static void init_seg(struct vmcb_seg *seg)
502{
503 seg->selector = 0;
504 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
505 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
506 seg->limit = 0xffff;
507 seg->base = 0;
508}
509
510static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
511{
512 seg->selector = 0;
513 seg->attrib = SVM_SELECTOR_P_MASK | type;
514 seg->limit = 0xffff;
515 seg->base = 0;
516}
517
Joerg Roedele6101a92008-02-13 18:58:45 +0100518static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519{
Joerg Roedele6101a92008-02-13 18:58:45 +0100520 struct vmcb_control_area *control = &svm->vmcb->control;
521 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522
523 control->intercept_cr_read = INTERCEPT_CR0_MASK |
524 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200525 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526
527 control->intercept_cr_write = INTERCEPT_CR0_MASK |
528 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200529 INTERCEPT_CR4_MASK |
530 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531
532 control->intercept_dr_read = INTERCEPT_DR0_MASK |
533 INTERCEPT_DR1_MASK |
534 INTERCEPT_DR2_MASK |
535 INTERCEPT_DR3_MASK;
536
537 control->intercept_dr_write = INTERCEPT_DR0_MASK |
538 INTERCEPT_DR1_MASK |
539 INTERCEPT_DR2_MASK |
540 INTERCEPT_DR3_MASK |
541 INTERCEPT_DR5_MASK |
542 INTERCEPT_DR7_MASK;
543
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500544 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200545 (1 << UD_VECTOR) |
546 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547
548
549 control->intercept = (1ULL << INTERCEPT_INTR) |
550 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200551 (1ULL << INTERCEPT_SMI) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800552 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200553 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800554 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300555 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800556 (1ULL << INTERCEPT_INVLPGA) |
557 (1ULL << INTERCEPT_IOIO_PROT) |
558 (1ULL << INTERCEPT_MSR_PROT) |
559 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800560 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561 (1ULL << INTERCEPT_VMRUN) |
562 (1ULL << INTERCEPT_VMMCALL) |
563 (1ULL << INTERCEPT_VMLOAD) |
564 (1ULL << INTERCEPT_VMSAVE) |
565 (1ULL << INTERCEPT_STGI) |
566 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100567 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200568 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100569 (1ULL << INTERCEPT_MONITOR) |
570 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571
572 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100573 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200574 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 control->int_ctl = V_INTR_MASKING_MASK;
576
577 init_seg(&save->es);
578 init_seg(&save->ss);
579 init_seg(&save->ds);
580 init_seg(&save->fs);
581 init_seg(&save->gs);
582
583 save->cs.selector = 0xf000;
584 /* Executable/Readable Code Segment */
585 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
586 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
587 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800588 /*
589 * cs.base should really be 0xffff0000, but vmx can't handle that, so
590 * be consistent with it.
591 *
592 * Replace when we have real mode working for vmx.
593 */
594 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595
596 save->gdtr.limit = 0xffff;
597 save->idtr.limit = 0xffff;
598
599 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
600 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
601
Alexander Graf9962d032008-11-25 20:17:02 +0100602 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400603 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800604 save->dr7 = 0x400;
605 save->rflags = 2;
606 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300607 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800608
609 /*
610 * cr0 val on cpu init should be 0x60000010, we enable cpu
611 * cache by default. the orderly way is to enable cache in bios.
612 */
Rusty Russell707d92fa2007-07-17 23:19:08 +1000613 save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
Rusty Russell66aee912007-07-17 23:34:16 +1000614 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100616
617 if (npt_enabled) {
618 /* Setup VMCB for Nested Paging */
619 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300620 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
621 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100622 control->intercept_exceptions &= ~(1 << PF_VECTOR);
623 control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK|
624 INTERCEPT_CR3_MASK);
625 control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK|
626 INTERCEPT_CR3_MASK);
627 save->g_pat = 0x0007040600070406ULL;
628 /* enable caching because the QEMU Bios doesn't enable it */
629 save->cr0 = X86_CR0_ET;
630 save->cr3 = 0;
631 save->cr4 = 0;
632 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300633 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100634
Alexander Graf3d6368e2008-11-25 20:17:07 +0100635 svm->nested_vmcb = 0;
Alexander Graf1371d902008-11-25 20:17:04 +0100636 svm->vcpu.arch.hflags = HF_GIF_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637}
638
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200639static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300640{
641 struct vcpu_svm *svm = to_svm(vcpu);
642
Joerg Roedele6101a92008-02-13 18:58:45 +0100643 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200644
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300645 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300646 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800647 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
648 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200649 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300650 vcpu->arch.regs_avail = ~0;
651 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200652
653 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300654}
655
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000656static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400658 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100660 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100661 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100662 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000663 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664
Rusty Russellc16f8622007-07-30 21:12:19 +1000665 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000666 if (!svm) {
667 err = -ENOMEM;
668 goto out;
669 }
670
671 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
672 if (err)
673 goto free_svm;
674
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675 page = alloc_page(GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000676 if (!page) {
677 err = -ENOMEM;
678 goto uninit;
679 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680
Joerg Roedelf65c2292008-02-13 18:58:46 +0100681 err = -ENOMEM;
682 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
683 if (!msrpm_pages)
684 goto uninit;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100685
686 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
687 if (!nested_msrpm_pages)
688 goto uninit;
689
Joerg Roedelf65c2292008-02-13 18:58:46 +0100690 svm->msrpm = page_address(msrpm_pages);
691 svm_vcpu_init_msrpm(svm->msrpm);
692
Alexander Grafb286d5d2008-11-25 20:17:05 +0100693 hsave_page = alloc_page(GFP_KERNEL);
694 if (!hsave_page)
695 goto uninit;
696 svm->hsave = page_address(hsave_page);
697
Alexander Graf3d6368e2008-11-25 20:17:07 +0100698 svm->nested_msrpm = page_address(nested_msrpm_pages);
699
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400700 svm->vmcb = page_address(page);
701 clear_page(svm->vmcb);
702 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
703 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100704 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400705
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000706 fx_init(&svm->vcpu);
707 svm->vcpu.fpu_active = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800708 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300709 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800710 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000712 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800713
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000714uninit:
715 kvm_vcpu_uninit(&svm->vcpu);
716free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000717 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000718out:
719 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720}
721
722static void svm_free_vcpu(struct kvm_vcpu *vcpu)
723{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400724 struct vcpu_svm *svm = to_svm(vcpu);
725
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000726 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100727 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100728 __free_page(virt_to_page(svm->hsave));
Alexander Graf3d6368e2008-11-25 20:17:07 +0100729 __free_pages(virt_to_page(svm->nested_msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000730 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000731 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732}
733
Avi Kivity15ad7142007-07-11 18:17:21 +0300734static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400736 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300737 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200738
Avi Kivity0cc50642007-03-25 12:07:27 +0200739 if (unlikely(cpu != vcpu->cpu)) {
740 u64 tsc_this, delta;
741
742 /*
743 * Make sure that the guest sees a monotonically
744 * increasing TSC.
745 */
746 rdtscll(tsc_this);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800747 delta = vcpu->arch.host_tsc - tsc_this;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400748 svm->vmcb->control.tsc_offset += delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200749 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300750 kvm_migrate_timers(vcpu);
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300751 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200752 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300753
754 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400755 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756}
757
758static void svm_vcpu_put(struct kvm_vcpu *vcpu)
759{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400760 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300761 int i;
762
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200763 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300764 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400765 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300766
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800767 rdtscll(vcpu->arch.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768}
769
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
771{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400772 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773}
774
775static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
776{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400777 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778}
779
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300780static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
781{
782 switch (reg) {
783 case VCPU_EXREG_PDPTR:
784 BUG_ON(!npt_enabled);
785 load_pdptrs(vcpu, vcpu->arch.cr3);
786 break;
787 default:
788 BUG();
789 }
790}
791
Alexander Graff0b85052008-11-25 20:17:01 +0100792static void svm_set_vintr(struct vcpu_svm *svm)
793{
794 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
795}
796
797static void svm_clear_vintr(struct vcpu_svm *svm)
798{
799 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
800}
801
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
803{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400804 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805
806 switch (seg) {
807 case VCPU_SREG_CS: return &save->cs;
808 case VCPU_SREG_DS: return &save->ds;
809 case VCPU_SREG_ES: return &save->es;
810 case VCPU_SREG_FS: return &save->fs;
811 case VCPU_SREG_GS: return &save->gs;
812 case VCPU_SREG_SS: return &save->ss;
813 case VCPU_SREG_TR: return &save->tr;
814 case VCPU_SREG_LDTR: return &save->ldtr;
815 }
816 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000817 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818}
819
820static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
821{
822 struct vmcb_seg *s = svm_seg(vcpu, seg);
823
824 return s->base;
825}
826
827static void svm_get_segment(struct kvm_vcpu *vcpu,
828 struct kvm_segment *var, int seg)
829{
830 struct vmcb_seg *s = svm_seg(vcpu, seg);
831
832 var->base = s->base;
833 var->limit = s->limit;
834 var->selector = s->selector;
835 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
836 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
837 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
838 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
839 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
840 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
841 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
842 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +0000843
Andre Przywara19bca6a2009-04-28 12:45:30 +0200844 /* AMD's VMCB does not have an explicit unusable field, so emulate it
845 * for cross vendor migration purposes by "not present"
846 */
847 var->unusable = !var->present || (var->type == 0);
848
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100849 switch (seg) {
850 case VCPU_SREG_CS:
851 /*
852 * SVM always stores 0 for the 'G' bit in the CS selector in
853 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
854 * Intel's VMENTRY has a check on the 'G' bit.
855 */
Amit Shah25022ac2008-10-27 09:04:17 +0000856 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100857 break;
858 case VCPU_SREG_TR:
859 /*
860 * Work around a bug where the busy flag in the tr selector
861 * isn't exposed
862 */
Amit Shahc0d09822008-10-27 09:04:18 +0000863 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100864 break;
865 case VCPU_SREG_DS:
866 case VCPU_SREG_ES:
867 case VCPU_SREG_FS:
868 case VCPU_SREG_GS:
869 /*
870 * The accessed bit must always be set in the segment
871 * descriptor cache, although it can be cleared in the
872 * descriptor, the cached bit always remains at 1. Since
873 * Intel has a check on this, set it here to support
874 * cross-vendor migration.
875 */
876 if (!var->unusable)
877 var->type |= 0x1;
878 break;
Andre Przywarab586eb02009-04-28 12:45:43 +0200879 case VCPU_SREG_SS:
880 /* On AMD CPUs sometimes the DB bit in the segment
881 * descriptor is left as 1, although the whole segment has
882 * been made unusable. Clear it here to pass an Intel VMX
883 * entry check when cross vendor migrating.
884 */
885 if (var->unusable)
886 var->db = 0;
887 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100888 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889}
890
Izik Eidus2e4d2652008-03-24 19:38:34 +0200891static int svm_get_cpl(struct kvm_vcpu *vcpu)
892{
893 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
894
895 return save->cpl;
896}
897
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
899{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400900 struct vcpu_svm *svm = to_svm(vcpu);
901
902 dt->limit = svm->vmcb->save.idtr.limit;
903 dt->base = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904}
905
906static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
907{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400908 struct vcpu_svm *svm = to_svm(vcpu);
909
910 svm->vmcb->save.idtr.limit = dt->limit;
911 svm->vmcb->save.idtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912}
913
914static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
915{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400916 struct vcpu_svm *svm = to_svm(vcpu);
917
918 dt->limit = svm->vmcb->save.gdtr.limit;
919 dt->base = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920}
921
922static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
923{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400924 struct vcpu_svm *svm = to_svm(vcpu);
925
926 svm->vmcb->save.gdtr.limit = dt->limit;
927 svm->vmcb->save.gdtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928}
929
Anthony Liguori25c4c272007-04-27 09:29:21 +0300930static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -0800931{
932}
933
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
935{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400936 struct vcpu_svm *svm = to_svm(vcpu);
937
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800938#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800939 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +1000940 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800941 vcpu->arch.shadow_efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600942 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 }
944
Mike Dayd77c26f2007-10-08 09:02:08 -0400945 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800946 vcpu->arch.shadow_efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600947 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 }
949 }
950#endif
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100951 if (npt_enabled)
952 goto set;
953
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800954 if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400955 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Anthony Liguori7807fa62007-04-23 09:17:21 -0500956 vcpu->fpu_active = 1;
957 }
958
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800959 vcpu->arch.cr0 = cr0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000960 cr0 |= X86_CR0_PG | X86_CR0_WP;
Joerg Roedel6b390b62008-01-29 13:01:27 +0100961 if (!vcpu->fpu_active) {
962 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
Joerg Roedel334df502008-01-21 13:09:33 +0100963 cr0 |= X86_CR0_TS;
Joerg Roedel6b390b62008-01-29 13:01:27 +0100964 }
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100965set:
966 /*
967 * re-enable caching here because the QEMU bios
968 * does not do it - this results in some delay at
969 * reboot
970 */
971 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400972 svm->vmcb->save.cr0 = cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973}
974
975static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
976{
Joerg Roedel6394b642008-04-09 14:15:29 +0200977 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +0200978 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
979
980 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
981 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +0200982
Joerg Roedelec077262008-04-09 14:15:28 +0200983 vcpu->arch.cr4 = cr4;
984 if (!npt_enabled)
985 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +0200986 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +0200987 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988}
989
990static void svm_set_segment(struct kvm_vcpu *vcpu,
991 struct kvm_segment *var, int seg)
992{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400993 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 struct vmcb_seg *s = svm_seg(vcpu, seg);
995
996 s->base = var->base;
997 s->limit = var->limit;
998 s->selector = var->selector;
999 if (var->unusable)
1000 s->attrib = 0;
1001 else {
1002 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1003 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1004 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1005 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1006 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1007 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1008 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1009 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1010 }
1011 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001012 svm->vmcb->save.cpl
1013 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1015
1016}
1017
Gleb Natapov44c11432009-05-11 13:35:52 +03001018static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001020 struct vcpu_svm *svm = to_svm(vcpu);
1021
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001022 svm->vmcb->control.intercept_exceptions &=
1023 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001024
1025 if (vcpu->arch.singlestep)
1026 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1027
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001028 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1029 if (vcpu->guest_debug &
1030 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1031 svm->vmcb->control.intercept_exceptions |=
1032 1 << DB_VECTOR;
1033 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1034 svm->vmcb->control.intercept_exceptions |=
1035 1 << BP_VECTOR;
1036 } else
1037 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001038}
1039
1040static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
1041{
1042 int old_debug = vcpu->guest_debug;
1043 struct vcpu_svm *svm = to_svm(vcpu);
1044
1045 vcpu->guest_debug = dbg->control;
1046
1047 update_db_intercept(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001048
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001049 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1050 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1051 else
1052 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1053
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001054 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1055 svm->vmcb->save.rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1056 else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
1057 svm->vmcb->save.rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1058
1059 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060}
1061
1062static void load_host_msrs(struct kvm_vcpu *vcpu)
1063{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001064#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001065 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001066#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067}
1068
1069static void save_host_msrs(struct kvm_vcpu *vcpu)
1070{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001071#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001072 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001073#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074}
1075
Rusty Russelle756fc62007-07-30 20:07:08 +10001076static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077{
1078 if (svm_data->next_asid > svm_data->max_asid) {
1079 ++svm_data->asid_generation;
1080 svm_data->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001081 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082 }
1083
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001084 svm->asid_generation = svm_data->asid_generation;
1085 svm->vmcb->control.asid = svm_data->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086}
1087
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
1089{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001090 struct vcpu_svm *svm = to_svm(vcpu);
1091 unsigned long val;
1092
1093 switch (dr) {
1094 case 0 ... 3:
1095 val = vcpu->arch.db[dr];
1096 break;
1097 case 6:
1098 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1099 val = vcpu->arch.dr6;
1100 else
1101 val = svm->vmcb->save.dr6;
1102 break;
1103 case 7:
1104 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1105 val = vcpu->arch.dr7;
1106 else
1107 val = svm->vmcb->save.dr7;
1108 break;
1109 default:
1110 val = 0;
1111 }
1112
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001113 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
1114 return val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115}
1116
1117static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
1118 int *exception)
1119{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001120 struct vcpu_svm *svm = to_svm(vcpu);
1121
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001122 KVMTRACE_2D(DR_WRITE, vcpu, (u32)dr, (u32)value, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001124 *exception = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125
1126 switch (dr) {
1127 case 0 ... 3:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001128 vcpu->arch.db[dr] = value;
1129 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1130 vcpu->arch.eff_db[dr] = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131 return;
1132 case 4 ... 5:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001133 if (vcpu->arch.cr4 & X86_CR4_DE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134 *exception = UD_VECTOR;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001135 return;
1136 case 6:
1137 if (value & 0xffffffff00000000ULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 *exception = GP_VECTOR;
1139 return;
1140 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001141 vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 return;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001143 case 7:
1144 if (value & 0xffffffff00000000ULL) {
1145 *exception = GP_VECTOR;
1146 return;
1147 }
1148 vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1;
1149 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1150 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1151 vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK);
1152 }
1153 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 default:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001155 /* FIXME: Possible case? */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 printk(KERN_DEBUG "%s: unexpected dr %u\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001157 __func__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158 *exception = UD_VECTOR;
1159 return;
1160 }
1161}
1162
Rusty Russelle756fc62007-07-30 20:07:08 +10001163static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165 u64 fault_address;
1166 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001168 fault_address = svm->vmcb->control.exit_info_2;
1169 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001170
1171 if (!npt_enabled)
1172 KVMTRACE_3D(PAGE_FAULT, &svm->vcpu, error_code,
1173 (u32)fault_address, (u32)(fault_address >> 32),
1174 handler);
Joerg Roedeld2ebb412008-04-30 17:56:04 +02001175 else
1176 KVMTRACE_3D(TDP_FAULT, &svm->vcpu, error_code,
1177 (u32)fault_address, (u32)(fault_address >> 32),
1178 handler);
Joerg Roedel44874f82008-08-27 14:18:43 +02001179 /*
1180 * FIXME: Tis shouldn't be necessary here, but there is a flush
1181 * missing in the MMU code. Until we find this bug, flush the
1182 * complete TLB here on an NPF
1183 */
1184 if (npt_enabled)
1185 svm_flush_tlb(&svm->vcpu);
Gleb Natapov9222be12009-04-23 17:14:37 +03001186 else {
Gleb Natapov3298b752009-05-11 13:35:46 +03001187 if (kvm_event_needs_reinjection(&svm->vcpu))
Gleb Natapov9222be12009-04-23 17:14:37 +03001188 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1189 }
Avi Kivity30677142007-10-28 18:48:59 +02001190 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191}
1192
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001193static int db_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1194{
1195 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001196 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
1197 !svm->vcpu.arch.singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001198 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1199 return 1;
1200 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001201
1202 if (svm->vcpu.arch.singlestep) {
1203 svm->vcpu.arch.singlestep = false;
1204 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1205 svm->vmcb->save.rflags &=
1206 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1207 update_db_intercept(&svm->vcpu);
1208 }
1209
1210 if (svm->vcpu.guest_debug &
1211 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)){
1212 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1213 kvm_run->debug.arch.pc =
1214 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1215 kvm_run->debug.arch.exception = DB_VECTOR;
1216 return 0;
1217 }
1218
1219 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001220}
1221
1222static int bp_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1223{
1224 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1225 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1226 kvm_run->debug.arch.exception = BP_VECTOR;
1227 return 0;
1228}
1229
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001230static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1231{
1232 int er;
1233
Sheng Yang571008d2008-01-02 14:49:22 +08001234 er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001235 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001236 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001237 return 1;
1238}
1239
Rusty Russelle756fc62007-07-30 20:07:08 +10001240static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001241{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001242 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001243 if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001244 svm->vmcb->save.cr0 &= ~X86_CR0_TS;
Rusty Russelle756fc62007-07-30 20:07:08 +10001245 svm->vcpu.fpu_active = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001246
1247 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001248}
1249
Joerg Roedel53371b52008-04-09 14:15:30 +02001250static int mc_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1251{
1252 /*
1253 * On an #MC intercept the MCE handler is not called automatically in
1254 * the host. So do it by hand here.
1255 */
1256 asm volatile (
1257 "int $0x12\n");
1258 /* not sure if we ever come back to this point */
1259
1260 return 1;
1261}
1262
Rusty Russelle756fc62007-07-30 20:07:08 +10001263static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001264{
1265 /*
1266 * VMCB is undefined after a SHUTDOWN intercept
1267 * so reinitialize it.
1268 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001269 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001270 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001271
1272 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1273 return 0;
1274}
1275
Rusty Russelle756fc62007-07-30 20:07:08 +10001276static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277{
Mike Dayd77c26f2007-10-08 09:02:08 -04001278 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001279 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001280 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281
Rusty Russelle756fc62007-07-30 20:07:08 +10001282 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001284 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285
Laurent Viviere70669a2007-08-05 10:36:40 +03001286 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1287
1288 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001289 if (emulate_instruction(&svm->vcpu,
1290 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001291 return 0;
1292 return 1;
1293 }
1294
Avi Kivity039576c2007-03-20 12:46:50 +02001295 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1296 port = io_info >> 16;
1297 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001299 skip_emulated_instruction(&svm->vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03001300 return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301}
1302
Joerg Roedelc47f0982008-04-30 17:56:00 +02001303static int nmi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1304{
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001305 KVMTRACE_0D(NMI, &svm->vcpu, handler);
Joerg Roedelc47f0982008-04-30 17:56:00 +02001306 return 1;
1307}
1308
Joerg Roedela0698052008-04-30 17:56:01 +02001309static int intr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1310{
1311 ++svm->vcpu.stat.irq_exits;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001312 KVMTRACE_0D(INTR, &svm->vcpu, handler);
Joerg Roedela0698052008-04-30 17:56:01 +02001313 return 1;
1314}
1315
Rusty Russelle756fc62007-07-30 20:07:08 +10001316static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317{
1318 return 1;
1319}
1320
Rusty Russelle756fc62007-07-30 20:07:08 +10001321static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001323 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001324 skip_emulated_instruction(&svm->vcpu);
1325 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326}
1327
Rusty Russelle756fc62007-07-30 20:07:08 +10001328static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity02e235b2007-02-19 14:37:47 +02001329{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001330 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001331 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001332 kvm_emulate_hypercall(&svm->vcpu);
1333 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001334}
1335
Alexander Grafc0725422008-11-25 20:17:03 +01001336static int nested_svm_check_permissions(struct vcpu_svm *svm)
1337{
1338 if (!(svm->vcpu.arch.shadow_efer & EFER_SVME)
1339 || !is_paging(&svm->vcpu)) {
1340 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1341 return 1;
1342 }
1343
1344 if (svm->vmcb->save.cpl) {
1345 kvm_inject_gp(&svm->vcpu, 0);
1346 return 1;
1347 }
1348
1349 return 0;
1350}
1351
Alexander Grafcf74a782008-11-25 20:17:08 +01001352static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1353 bool has_error_code, u32 error_code)
1354{
1355 if (is_nested(svm)) {
1356 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1357 svm->vmcb->control.exit_code_hi = 0;
1358 svm->vmcb->control.exit_info_1 = error_code;
1359 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1360 if (nested_svm_exit_handled(svm, false)) {
1361 nsvm_printk("VMexit -> EXCP 0x%x\n", nr);
1362
1363 nested_svm_vmexit(svm);
1364 return 1;
1365 }
1366 }
1367
1368 return 0;
1369}
1370
1371static inline int nested_svm_intr(struct vcpu_svm *svm)
1372{
1373 if (is_nested(svm)) {
1374 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1375 return 0;
1376
1377 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
1378 return 0;
1379
1380 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1381
1382 if (nested_svm_exit_handled(svm, false)) {
1383 nsvm_printk("VMexit -> INTR\n");
1384 nested_svm_vmexit(svm);
1385 return 1;
1386 }
1387 }
1388
1389 return 0;
1390}
1391
Alexander Grafc0725422008-11-25 20:17:03 +01001392static struct page *nested_svm_get_page(struct vcpu_svm *svm, u64 gpa)
1393{
1394 struct page *page;
1395
1396 down_read(&current->mm->mmap_sem);
1397 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
1398 up_read(&current->mm->mmap_sem);
1399
1400 if (is_error_page(page)) {
1401 printk(KERN_INFO "%s: could not find page at 0x%llx\n",
1402 __func__, gpa);
1403 kvm_release_page_clean(page);
1404 kvm_inject_gp(&svm->vcpu, 0);
1405 return NULL;
1406 }
1407 return page;
1408}
1409
1410static int nested_svm_do(struct vcpu_svm *svm,
1411 u64 arg1_gpa, u64 arg2_gpa, void *opaque,
1412 int (*handler)(struct vcpu_svm *svm,
1413 void *arg1,
1414 void *arg2,
1415 void *opaque))
1416{
1417 struct page *arg1_page;
1418 struct page *arg2_page = NULL;
1419 void *arg1;
1420 void *arg2 = NULL;
1421 int retval;
1422
1423 arg1_page = nested_svm_get_page(svm, arg1_gpa);
1424 if(arg1_page == NULL)
1425 return 1;
1426
1427 if (arg2_gpa) {
1428 arg2_page = nested_svm_get_page(svm, arg2_gpa);
1429 if(arg2_page == NULL) {
1430 kvm_release_page_clean(arg1_page);
1431 return 1;
1432 }
1433 }
1434
1435 arg1 = kmap_atomic(arg1_page, KM_USER0);
1436 if (arg2_gpa)
1437 arg2 = kmap_atomic(arg2_page, KM_USER1);
1438
1439 retval = handler(svm, arg1, arg2, opaque);
1440
1441 kunmap_atomic(arg1, KM_USER0);
1442 if (arg2_gpa)
1443 kunmap_atomic(arg2, KM_USER1);
1444
1445 kvm_release_page_dirty(arg1_page);
1446 if (arg2_gpa)
1447 kvm_release_page_dirty(arg2_page);
1448
1449 return retval;
1450}
1451
Alexander Grafcf74a782008-11-25 20:17:08 +01001452static int nested_svm_exit_handled_real(struct vcpu_svm *svm,
1453 void *arg1,
1454 void *arg2,
1455 void *opaque)
1456{
1457 struct vmcb *nested_vmcb = (struct vmcb *)arg1;
1458 bool kvm_overrides = *(bool *)opaque;
1459 u32 exit_code = svm->vmcb->control.exit_code;
1460
1461 if (kvm_overrides) {
1462 switch (exit_code) {
1463 case SVM_EXIT_INTR:
1464 case SVM_EXIT_NMI:
1465 return 0;
1466 /* For now we are always handling NPFs when using them */
1467 case SVM_EXIT_NPF:
1468 if (npt_enabled)
1469 return 0;
1470 break;
1471 /* When we're shadowing, trap PFs */
1472 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
1473 if (!npt_enabled)
1474 return 0;
1475 break;
1476 default:
1477 break;
1478 }
1479 }
1480
1481 switch (exit_code) {
1482 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1483 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
1484 if (nested_vmcb->control.intercept_cr_read & cr_bits)
1485 return 1;
1486 break;
1487 }
1488 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1489 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
1490 if (nested_vmcb->control.intercept_cr_write & cr_bits)
1491 return 1;
1492 break;
1493 }
1494 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1495 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
1496 if (nested_vmcb->control.intercept_dr_read & dr_bits)
1497 return 1;
1498 break;
1499 }
1500 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1501 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
1502 if (nested_vmcb->control.intercept_dr_write & dr_bits)
1503 return 1;
1504 break;
1505 }
1506 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1507 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1508 if (nested_vmcb->control.intercept_exceptions & excp_bits)
1509 return 1;
1510 break;
1511 }
1512 default: {
1513 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
1514 nsvm_printk("exit code: 0x%x\n", exit_code);
1515 if (nested_vmcb->control.intercept & exit_bits)
1516 return 1;
1517 }
1518 }
1519
1520 return 0;
1521}
1522
1523static int nested_svm_exit_handled_msr(struct vcpu_svm *svm,
1524 void *arg1, void *arg2,
1525 void *opaque)
1526{
1527 struct vmcb *nested_vmcb = (struct vmcb *)arg1;
1528 u8 *msrpm = (u8 *)arg2;
1529 u32 t0, t1;
1530 u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1531 u32 param = svm->vmcb->control.exit_info_1 & 1;
1532
1533 if (!(nested_vmcb->control.intercept & (1ULL << INTERCEPT_MSR_PROT)))
1534 return 0;
1535
1536 switch(msr) {
1537 case 0 ... 0x1fff:
1538 t0 = (msr * 2) % 8;
1539 t1 = msr / 8;
1540 break;
1541 case 0xc0000000 ... 0xc0001fff:
1542 t0 = (8192 + msr - 0xc0000000) * 2;
1543 t1 = (t0 / 8);
1544 t0 %= 8;
1545 break;
1546 case 0xc0010000 ... 0xc0011fff:
1547 t0 = (16384 + msr - 0xc0010000) * 2;
1548 t1 = (t0 / 8);
1549 t0 %= 8;
1550 break;
1551 default:
1552 return 1;
1553 break;
1554 }
1555 if (msrpm[t1] & ((1 << param) << t0))
1556 return 1;
1557
1558 return 0;
1559}
1560
1561static int nested_svm_exit_handled(struct vcpu_svm *svm, bool kvm_override)
1562{
1563 bool k = kvm_override;
1564
1565 switch (svm->vmcb->control.exit_code) {
1566 case SVM_EXIT_MSR:
1567 return nested_svm_do(svm, svm->nested_vmcb,
1568 svm->nested_vmcb_msrpm, NULL,
1569 nested_svm_exit_handled_msr);
1570 default: break;
1571 }
1572
1573 return nested_svm_do(svm, svm->nested_vmcb, 0, &k,
1574 nested_svm_exit_handled_real);
1575}
1576
1577static int nested_svm_vmexit_real(struct vcpu_svm *svm, void *arg1,
1578 void *arg2, void *opaque)
1579{
1580 struct vmcb *nested_vmcb = (struct vmcb *)arg1;
1581 struct vmcb *hsave = svm->hsave;
1582 u64 nested_save[] = { nested_vmcb->save.cr0,
1583 nested_vmcb->save.cr3,
1584 nested_vmcb->save.cr4,
1585 nested_vmcb->save.efer,
1586 nested_vmcb->control.intercept_cr_read,
1587 nested_vmcb->control.intercept_cr_write,
1588 nested_vmcb->control.intercept_dr_read,
1589 nested_vmcb->control.intercept_dr_write,
1590 nested_vmcb->control.intercept_exceptions,
1591 nested_vmcb->control.intercept,
1592 nested_vmcb->control.msrpm_base_pa,
1593 nested_vmcb->control.iopm_base_pa,
1594 nested_vmcb->control.tsc_offset };
1595
1596 /* Give the current vmcb to the guest */
1597 memcpy(nested_vmcb, svm->vmcb, sizeof(struct vmcb));
1598 nested_vmcb->save.cr0 = nested_save[0];
1599 if (!npt_enabled)
1600 nested_vmcb->save.cr3 = nested_save[1];
1601 nested_vmcb->save.cr4 = nested_save[2];
1602 nested_vmcb->save.efer = nested_save[3];
1603 nested_vmcb->control.intercept_cr_read = nested_save[4];
1604 nested_vmcb->control.intercept_cr_write = nested_save[5];
1605 nested_vmcb->control.intercept_dr_read = nested_save[6];
1606 nested_vmcb->control.intercept_dr_write = nested_save[7];
1607 nested_vmcb->control.intercept_exceptions = nested_save[8];
1608 nested_vmcb->control.intercept = nested_save[9];
1609 nested_vmcb->control.msrpm_base_pa = nested_save[10];
1610 nested_vmcb->control.iopm_base_pa = nested_save[11];
1611 nested_vmcb->control.tsc_offset = nested_save[12];
1612
1613 /* We always set V_INTR_MASKING and remember the old value in hflags */
1614 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1615 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1616
1617 if ((nested_vmcb->control.int_ctl & V_IRQ_MASK) &&
1618 (nested_vmcb->control.int_vector)) {
1619 nsvm_printk("WARNING: IRQ 0x%x still enabled on #VMEXIT\n",
1620 nested_vmcb->control.int_vector);
1621 }
1622
1623 /* Restore the original control entries */
1624 svm->vmcb->control = hsave->control;
1625
1626 /* Kill any pending exceptions */
1627 if (svm->vcpu.arch.exception.pending == true)
1628 nsvm_printk("WARNING: Pending Exception\n");
1629 svm->vcpu.arch.exception.pending = false;
1630
1631 /* Restore selected save entries */
1632 svm->vmcb->save.es = hsave->save.es;
1633 svm->vmcb->save.cs = hsave->save.cs;
1634 svm->vmcb->save.ss = hsave->save.ss;
1635 svm->vmcb->save.ds = hsave->save.ds;
1636 svm->vmcb->save.gdtr = hsave->save.gdtr;
1637 svm->vmcb->save.idtr = hsave->save.idtr;
1638 svm->vmcb->save.rflags = hsave->save.rflags;
1639 svm_set_efer(&svm->vcpu, hsave->save.efer);
1640 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1641 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1642 if (npt_enabled) {
1643 svm->vmcb->save.cr3 = hsave->save.cr3;
1644 svm->vcpu.arch.cr3 = hsave->save.cr3;
1645 } else {
1646 kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
1647 }
1648 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1649 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1650 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1651 svm->vmcb->save.dr7 = 0;
1652 svm->vmcb->save.cpl = 0;
1653 svm->vmcb->control.exit_int_info = 0;
1654
1655 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
1656 /* Exit nested SVM mode */
1657 svm->nested_vmcb = 0;
1658
1659 return 0;
1660}
1661
1662static int nested_svm_vmexit(struct vcpu_svm *svm)
1663{
1664 nsvm_printk("VMexit\n");
1665 if (nested_svm_do(svm, svm->nested_vmcb, 0,
1666 NULL, nested_svm_vmexit_real))
1667 return 1;
1668
1669 kvm_mmu_reset_context(&svm->vcpu);
1670 kvm_mmu_load(&svm->vcpu);
1671
1672 return 0;
1673}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001674
1675static int nested_svm_vmrun_msrpm(struct vcpu_svm *svm, void *arg1,
1676 void *arg2, void *opaque)
1677{
1678 int i;
1679 u32 *nested_msrpm = (u32*)arg1;
1680 for (i=0; i< PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++)
1681 svm->nested_msrpm[i] = svm->msrpm[i] | nested_msrpm[i];
1682 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested_msrpm);
1683
1684 return 0;
1685}
1686
1687static int nested_svm_vmrun(struct vcpu_svm *svm, void *arg1,
1688 void *arg2, void *opaque)
1689{
1690 struct vmcb *nested_vmcb = (struct vmcb *)arg1;
1691 struct vmcb *hsave = svm->hsave;
1692
1693 /* nested_vmcb is our indicator if nested SVM is activated */
1694 svm->nested_vmcb = svm->vmcb->save.rax;
1695
1696 /* Clear internal status */
1697 svm->vcpu.arch.exception.pending = false;
1698
1699 /* Save the old vmcb, so we don't need to pick what we save, but
1700 can restore everything when a VMEXIT occurs */
1701 memcpy(hsave, svm->vmcb, sizeof(struct vmcb));
1702 /* We need to remember the original CR3 in the SPT case */
1703 if (!npt_enabled)
1704 hsave->save.cr3 = svm->vcpu.arch.cr3;
1705 hsave->save.cr4 = svm->vcpu.arch.cr4;
1706 hsave->save.rip = svm->next_rip;
1707
1708 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
1709 svm->vcpu.arch.hflags |= HF_HIF_MASK;
1710 else
1711 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
1712
1713 /* Load the nested guest state */
1714 svm->vmcb->save.es = nested_vmcb->save.es;
1715 svm->vmcb->save.cs = nested_vmcb->save.cs;
1716 svm->vmcb->save.ss = nested_vmcb->save.ss;
1717 svm->vmcb->save.ds = nested_vmcb->save.ds;
1718 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
1719 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
1720 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
1721 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
1722 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
1723 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
1724 if (npt_enabled) {
1725 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
1726 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
1727 } else {
1728 kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
1729 kvm_mmu_reset_context(&svm->vcpu);
1730 }
1731 svm->vmcb->save.cr2 = nested_vmcb->save.cr2;
1732 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
1733 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
1734 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
1735 /* In case we don't even reach vcpu_run, the fields are not updated */
1736 svm->vmcb->save.rax = nested_vmcb->save.rax;
1737 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
1738 svm->vmcb->save.rip = nested_vmcb->save.rip;
1739 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
1740 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
1741 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
1742
1743 /* We don't want a nested guest to be more powerful than the guest,
1744 so all intercepts are ORed */
1745 svm->vmcb->control.intercept_cr_read |=
1746 nested_vmcb->control.intercept_cr_read;
1747 svm->vmcb->control.intercept_cr_write |=
1748 nested_vmcb->control.intercept_cr_write;
1749 svm->vmcb->control.intercept_dr_read |=
1750 nested_vmcb->control.intercept_dr_read;
1751 svm->vmcb->control.intercept_dr_write |=
1752 nested_vmcb->control.intercept_dr_write;
1753 svm->vmcb->control.intercept_exceptions |=
1754 nested_vmcb->control.intercept_exceptions;
1755
1756 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
1757
1758 svm->nested_vmcb_msrpm = nested_vmcb->control.msrpm_base_pa;
1759
1760 force_new_asid(&svm->vcpu);
1761 svm->vmcb->control.exit_int_info = nested_vmcb->control.exit_int_info;
1762 svm->vmcb->control.exit_int_info_err = nested_vmcb->control.exit_int_info_err;
1763 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
1764 if (nested_vmcb->control.int_ctl & V_IRQ_MASK) {
1765 nsvm_printk("nSVM Injecting Interrupt: 0x%x\n",
1766 nested_vmcb->control.int_ctl);
1767 }
1768 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
1769 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
1770 else
1771 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
1772
1773 nsvm_printk("nSVM exit_int_info: 0x%x | int_state: 0x%x\n",
1774 nested_vmcb->control.exit_int_info,
1775 nested_vmcb->control.int_state);
1776
1777 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
1778 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
1779 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
1780 if (nested_vmcb->control.event_inj & SVM_EVTINJ_VALID)
1781 nsvm_printk("Injecting Event: 0x%x\n",
1782 nested_vmcb->control.event_inj);
1783 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
1784 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
1785
1786 svm->vcpu.arch.hflags |= HF_GIF_MASK;
1787
1788 return 0;
1789}
1790
Alexander Graf55426752008-11-25 20:17:06 +01001791static int nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
1792{
1793 to_vmcb->save.fs = from_vmcb->save.fs;
1794 to_vmcb->save.gs = from_vmcb->save.gs;
1795 to_vmcb->save.tr = from_vmcb->save.tr;
1796 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
1797 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
1798 to_vmcb->save.star = from_vmcb->save.star;
1799 to_vmcb->save.lstar = from_vmcb->save.lstar;
1800 to_vmcb->save.cstar = from_vmcb->save.cstar;
1801 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
1802 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
1803 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
1804 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
1805
1806 return 1;
1807}
1808
1809static int nested_svm_vmload(struct vcpu_svm *svm, void *nested_vmcb,
1810 void *arg2, void *opaque)
1811{
1812 return nested_svm_vmloadsave((struct vmcb *)nested_vmcb, svm->vmcb);
1813}
1814
1815static int nested_svm_vmsave(struct vcpu_svm *svm, void *nested_vmcb,
1816 void *arg2, void *opaque)
1817{
1818 return nested_svm_vmloadsave(svm->vmcb, (struct vmcb *)nested_vmcb);
1819}
1820
1821static int vmload_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1822{
1823 if (nested_svm_check_permissions(svm))
1824 return 1;
1825
1826 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1827 skip_emulated_instruction(&svm->vcpu);
1828
1829 nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmload);
1830
1831 return 1;
1832}
1833
1834static int vmsave_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1835{
1836 if (nested_svm_check_permissions(svm))
1837 return 1;
1838
1839 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1840 skip_emulated_instruction(&svm->vcpu);
1841
1842 nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmsave);
1843
1844 return 1;
1845}
1846
Alexander Graf3d6368e2008-11-25 20:17:07 +01001847static int vmrun_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1848{
1849 nsvm_printk("VMrun\n");
1850 if (nested_svm_check_permissions(svm))
1851 return 1;
1852
1853 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1854 skip_emulated_instruction(&svm->vcpu);
1855
1856 if (nested_svm_do(svm, svm->vmcb->save.rax, 0,
1857 NULL, nested_svm_vmrun))
1858 return 1;
1859
1860 if (nested_svm_do(svm, svm->nested_vmcb_msrpm, 0,
1861 NULL, nested_svm_vmrun_msrpm))
1862 return 1;
1863
1864 return 1;
1865}
1866
Alexander Graf1371d902008-11-25 20:17:04 +01001867static int stgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1868{
1869 if (nested_svm_check_permissions(svm))
1870 return 1;
1871
1872 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1873 skip_emulated_instruction(&svm->vcpu);
1874
1875 svm->vcpu.arch.hflags |= HF_GIF_MASK;
1876
1877 return 1;
1878}
1879
1880static int clgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1881{
1882 if (nested_svm_check_permissions(svm))
1883 return 1;
1884
1885 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1886 skip_emulated_instruction(&svm->vcpu);
1887
1888 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
1889
1890 /* After a CLGI no interrupts should come */
1891 svm_clear_vintr(svm);
1892 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
1893
1894 return 1;
1895}
1896
Rusty Russelle756fc62007-07-30 20:07:08 +10001897static int invalid_op_interception(struct vcpu_svm *svm,
1898 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001900 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001901 return 1;
1902}
1903
Rusty Russelle756fc62007-07-30 20:07:08 +10001904static int task_switch_interception(struct vcpu_svm *svm,
1905 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906{
Izik Eidus37817f22008-03-24 23:14:53 +02001907 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03001908 int reason;
1909 int int_type = svm->vmcb->control.exit_int_info &
1910 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03001911 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03001912 uint32_t type =
1913 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
1914 uint32_t idt_v =
1915 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Izik Eidus37817f22008-03-24 23:14:53 +02001916
1917 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03001918
Izik Eidus37817f22008-03-24 23:14:53 +02001919 if (svm->vmcb->control.exit_info_2 &
1920 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03001921 reason = TASK_SWITCH_IRET;
1922 else if (svm->vmcb->control.exit_info_2 &
1923 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
1924 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03001925 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03001926 reason = TASK_SWITCH_GATE;
1927 else
1928 reason = TASK_SWITCH_CALL;
1929
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03001930 if (reason == TASK_SWITCH_GATE) {
1931 switch (type) {
1932 case SVM_EXITINTINFO_TYPE_NMI:
1933 svm->vcpu.arch.nmi_injected = false;
1934 break;
1935 case SVM_EXITINTINFO_TYPE_EXEPT:
1936 kvm_clear_exception_queue(&svm->vcpu);
1937 break;
1938 case SVM_EXITINTINFO_TYPE_INTR:
1939 kvm_clear_interrupt_queue(&svm->vcpu);
1940 break;
1941 default:
1942 break;
1943 }
1944 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03001945
Gleb Natapov8317c292009-04-12 13:37:02 +03001946 if (reason != TASK_SWITCH_GATE ||
1947 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
1948 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03001949 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
1950 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03001951
1952 return kvm_task_switch(&svm->vcpu, tss_selector, reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953}
1954
Rusty Russelle756fc62007-07-30 20:07:08 +10001955static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001957 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10001958 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02001959 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960}
1961
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001962static int iret_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1963{
1964 ++svm->vcpu.stat.nmi_window_exits;
1965 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03001966 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001967 return 1;
1968}
1969
Marcelo Tosattia7052892008-09-23 13:18:35 -03001970static int invlpg_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1971{
1972 if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0) != EMULATE_DONE)
1973 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
1974 return 1;
1975}
1976
Rusty Russelle756fc62007-07-30 20:07:08 +10001977static int emulate_on_interception(struct vcpu_svm *svm,
1978 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001979{
Laurent Vivier34273182007-09-18 11:27:37 +02001980 if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001981 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982 return 1;
1983}
1984
Joerg Roedel1d075432007-12-06 21:02:25 +01001985static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1986{
Gleb Natapov0a5fff192009-04-21 17:45:06 +03001987 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
1988 /* instruction emulation calls kvm_set_cr8() */
Joerg Roedel1d075432007-12-06 21:02:25 +01001989 emulate_instruction(&svm->vcpu, NULL, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001990 if (irqchip_in_kernel(svm->vcpu.kvm)) {
1991 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01001992 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001993 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03001994 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
1995 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01001996 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
1997 return 0;
1998}
1999
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2001{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002002 struct vcpu_svm *svm = to_svm(vcpu);
2003
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302005 case MSR_IA32_TSC: {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002006 u64 tsc;
2007
2008 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002009 *data = svm->vmcb->control.tsc_offset + tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010 break;
2011 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002012 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002013 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002014 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002015#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002016 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002017 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002018 break;
2019 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002020 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021 break;
2022 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002023 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002024 break;
2025 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002026 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027 break;
2028#endif
2029 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002030 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031 break;
2032 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002033 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002034 break;
2035 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002036 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002038 /* Nobody will change the following 5 values in the VMCB so
2039 we can safely return them on rdmsr. They will always be 0
2040 until LBRV is implemented. */
2041 case MSR_IA32_DEBUGCTLMSR:
2042 *data = svm->vmcb->save.dbgctl;
2043 break;
2044 case MSR_IA32_LASTBRANCHFROMIP:
2045 *data = svm->vmcb->save.br_from;
2046 break;
2047 case MSR_IA32_LASTBRANCHTOIP:
2048 *data = svm->vmcb->save.br_to;
2049 break;
2050 case MSR_IA32_LASTINTFROMIP:
2051 *data = svm->vmcb->save.last_excp_from;
2052 break;
2053 case MSR_IA32_LASTINTTOIP:
2054 *data = svm->vmcb->save.last_excp_to;
2055 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002056 case MSR_VM_HSAVE_PA:
2057 *data = svm->hsave_msr;
2058 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002059 case MSR_VM_CR:
2060 *data = 0;
2061 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002062 case MSR_IA32_UCODE_REV:
2063 *data = 0x01000065;
2064 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002066 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002067 }
2068 return 0;
2069}
2070
Rusty Russelle756fc62007-07-30 20:07:08 +10002071static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002072{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002073 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074 u64 data;
2075
Rusty Russelle756fc62007-07-30 20:07:08 +10002076 if (svm_get_msr(&svm->vcpu, ecx, &data))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002077 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078 else {
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002079 KVMTRACE_3D(MSR_READ, &svm->vcpu, ecx, (u32)data,
2080 (u32)(data >> 32), handler);
2081
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002082 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002083 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002084 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002085 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002086 }
2087 return 1;
2088}
2089
2090static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2091{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002092 struct vcpu_svm *svm = to_svm(vcpu);
2093
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302095 case MSR_IA32_TSC: {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096 u64 tsc;
2097
2098 rdtscll(tsc);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002099 svm->vmcb->control.tsc_offset = data - tsc;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100 break;
2101 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002102 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002103 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002105#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002107 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108 break;
2109 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002110 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111 break;
2112 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002113 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114 break;
2115 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002116 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117 break;
2118#endif
2119 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002120 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121 break;
2122 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002123 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002124 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125 break;
2126 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002127 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002128 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002130 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002131 if (!svm_has(SVM_FEATURE_LBRV)) {
2132 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002133 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002134 break;
2135 }
2136 if (data & DEBUGCTL_RESERVED_BITS)
2137 return 1;
2138
2139 svm->vmcb->save.dbgctl = data;
2140 if (data & (1ULL<<0))
2141 svm_enable_lbrv(svm);
2142 else
2143 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002144 break;
Joerg Roedel62b9aba2007-12-11 15:36:57 +01002145 case MSR_K7_EVNTSEL0:
2146 case MSR_K7_EVNTSEL1:
2147 case MSR_K7_EVNTSEL2:
2148 case MSR_K7_EVNTSEL3:
Chris Lalancette14ae51b2008-05-05 13:05:16 -04002149 case MSR_K7_PERFCTR0:
2150 case MSR_K7_PERFCTR1:
2151 case MSR_K7_PERFCTR2:
2152 case MSR_K7_PERFCTR3:
Joerg Roedel62b9aba2007-12-11 15:36:57 +01002153 /*
Chris Lalancette14ae51b2008-05-05 13:05:16 -04002154 * Just discard all writes to the performance counters; this
2155 * should keep both older linux and windows 64-bit guests
2156 * happy
Joerg Roedel62b9aba2007-12-11 15:36:57 +01002157 */
Chris Lalancette14ae51b2008-05-05 13:05:16 -04002158 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", ecx, data);
2159
Joerg Roedel62b9aba2007-12-11 15:36:57 +01002160 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002161 case MSR_VM_HSAVE_PA:
2162 svm->hsave_msr = data;
2163 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002165 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166 }
2167 return 0;
2168}
2169
Rusty Russelle756fc62007-07-30 20:07:08 +10002170static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002172 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002173 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002174 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002175
2176 KVMTRACE_3D(MSR_WRITE, &svm->vcpu, ecx, (u32)data, (u32)(data >> 32),
2177 handler);
2178
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002179 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002180 if (svm_set_msr(&svm->vcpu, ecx, data))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002181 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182 else
Rusty Russelle756fc62007-07-30 20:07:08 +10002183 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184 return 1;
2185}
2186
Rusty Russelle756fc62007-07-30 20:07:08 +10002187static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002188{
Rusty Russelle756fc62007-07-30 20:07:08 +10002189 if (svm->vmcb->control.exit_info_1)
2190 return wrmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191 else
Rusty Russelle756fc62007-07-30 20:07:08 +10002192 return rdmsr_interception(svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193}
2194
Rusty Russelle756fc62007-07-30 20:07:08 +10002195static int interrupt_window_interception(struct vcpu_svm *svm,
Dor Laorc1150d82007-01-05 16:36:24 -08002196 struct kvm_run *kvm_run)
2197{
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002198 KVMTRACE_0D(PEND_INTR, &svm->vcpu, handler);
2199
Alexander Graff0b85052008-11-25 20:17:01 +01002200 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002201 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002202 /*
2203 * If the user space waits to inject interrupts, exit as soon as
2204 * possible
2205 */
Gleb Natapov80618232009-04-21 17:44:56 +03002206 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2207 kvm_run->request_interrupt_window &&
2208 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002209 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002210 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2211 return 0;
2212 }
2213
2214 return 1;
2215}
2216
Rusty Russelle756fc62007-07-30 20:07:08 +10002217static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218 struct kvm_run *kvm_run) = {
2219 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2220 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2221 [SVM_EXIT_READ_CR4] = emulate_on_interception,
Avi Kivity80a81192007-12-06 19:50:00 +02002222 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223 /* for now: */
2224 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2225 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2226 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
Joerg Roedel1d075432007-12-06 21:02:25 +01002227 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228 [SVM_EXIT_READ_DR0] = emulate_on_interception,
2229 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2230 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2231 [SVM_EXIT_READ_DR3] = emulate_on_interception,
2232 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2233 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2234 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2235 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
2236 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
2237 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002238 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2239 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002240 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Anthony Liguori7807fa62007-04-23 09:17:21 -05002242 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
Joerg Roedel53371b52008-04-09 14:15:30 +02002243 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Joerg Roedela0698052008-04-30 17:56:01 +02002244 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002245 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246 [SVM_EXIT_SMI] = nop_on_interception,
2247 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002248 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002249 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
2250 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002251 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002252 [SVM_EXIT_INVD] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002254 [SVM_EXIT_INVLPG] = invlpg_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255 [SVM_EXIT_INVLPGA] = invalid_op_interception,
2256 [SVM_EXIT_IOIO] = io_interception,
2257 [SVM_EXIT_MSR] = msr_interception,
2258 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002259 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002260 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002261 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002262 [SVM_EXIT_VMLOAD] = vmload_interception,
2263 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002264 [SVM_EXIT_STGI] = stgi_interception,
2265 [SVM_EXIT_CLGI] = clgi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266 [SVM_EXIT_SKINIT] = invalid_op_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002267 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002268 [SVM_EXIT_MONITOR] = invalid_op_interception,
2269 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002270 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271};
2272
Avi Kivity04d2cc72007-09-10 18:10:54 +03002273static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002275 struct vcpu_svm *svm = to_svm(vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002276 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002278 KVMTRACE_3D(VMEXIT, vcpu, exit_code, (u32)svm->vmcb->save.rip,
2279 (u32)((u64)svm->vmcb->save.rip >> 32), entryexit);
2280
Alexander Grafcf74a782008-11-25 20:17:08 +01002281 if (is_nested(svm)) {
2282 nsvm_printk("nested handle_exit: 0x%x | 0x%lx | 0x%lx | 0x%lx\n",
2283 exit_code, svm->vmcb->control.exit_info_1,
2284 svm->vmcb->control.exit_info_2, svm->vmcb->save.rip);
2285 if (nested_svm_exit_handled(svm, true)) {
2286 nested_svm_vmexit(svm);
2287 nsvm_printk("-> #VMEXIT\n");
2288 return 1;
2289 }
2290 }
2291
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002292 if (npt_enabled) {
2293 int mmu_reload = 0;
2294 if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
2295 svm_set_cr0(vcpu, svm->vmcb->save.cr0);
2296 mmu_reload = 1;
2297 }
2298 vcpu->arch.cr0 = svm->vmcb->save.cr0;
2299 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002300 if (mmu_reload) {
2301 kvm_mmu_reset_context(vcpu);
2302 kvm_mmu_load(vcpu);
2303 }
2304 }
2305
Avi Kivity04d2cc72007-09-10 18:10:54 +03002306
2307 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2308 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2309 kvm_run->fail_entry.hardware_entry_failure_reason
2310 = svm->vmcb->control.exit_code;
2311 return 0;
2312 }
2313
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002314 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002315 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002316 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2318 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002319 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320 exit_code);
2321
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002322 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002323 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002325 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326 return 0;
2327 }
2328
Rusty Russelle756fc62007-07-30 20:07:08 +10002329 return svm_exit_handlers[exit_code](svm, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330}
2331
2332static void reload_tss(struct kvm_vcpu *vcpu)
2333{
2334 int cpu = raw_smp_processor_id();
2335
2336 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04002337 svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338 load_TR_desc();
2339}
2340
Rusty Russelle756fc62007-07-30 20:07:08 +10002341static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342{
2343 int cpu = raw_smp_processor_id();
2344
2345 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
2346
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002347 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002348 /* FIXME: handle wraparound of asid_generation */
2349 if (svm->asid_generation != svm_data->asid_generation)
Rusty Russelle756fc62007-07-30 20:07:08 +10002350 new_asid(svm, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351}
2352
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002353static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2354{
2355 struct vcpu_svm *svm = to_svm(vcpu);
2356
2357 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2358 vcpu->arch.hflags |= HF_NMI_MASK;
2359 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2360 ++vcpu->stat.nmi_injections;
2361}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362
Eddie Dong85f455f2007-07-06 12:20:49 +03002363static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364{
2365 struct vmcb_control_area *control;
2366
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002367 KVMTRACE_1D(INJ_VIRQ, &svm->vcpu, (u32)irq, handler);
2368
Avi Kivityfa89a812008-09-01 15:57:51 +03002369 ++svm->vcpu.stat.irq_injections;
Rusty Russelle756fc62007-07-30 20:07:08 +10002370 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002371 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372 control->int_ctl &= ~V_INTR_PRIO_MASK;
2373 control->int_ctl |= V_IRQ_MASK |
2374 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2375}
2376
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002377static void svm_queue_irq(struct kvm_vcpu *vcpu, unsigned nr)
Gleb Natapov9222be12009-04-23 17:14:37 +03002378{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002379 struct vcpu_svm *svm = to_svm(vcpu);
2380
Gleb Natapov9222be12009-04-23 17:14:37 +03002381 svm->vmcb->control.event_inj = nr |
2382 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
2383}
2384
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002385static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002386{
2387 struct vcpu_svm *svm = to_svm(vcpu);
2388
Alexander Grafcf74a782008-11-25 20:17:08 +01002389 nested_svm_intr(svm);
2390
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002391 svm_queue_irq(vcpu, vcpu->arch.interrupt.nr);
Eddie Dong2a8067f2007-08-06 16:29:07 +03002392}
2393
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002394static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2395{
2396 struct vcpu_svm *svm = to_svm(vcpu);
2397
2398 if (irr == -1)
2399 return;
2400
2401 if (tpr >= irr)
2402 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2403}
2404
2405static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002406{
2407 struct vcpu_svm *svm = to_svm(vcpu);
2408 struct vmcb *vmcb = svm->vmcb;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002409 return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2410 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002411}
2412
Gleb Natapov78646122009-03-23 12:12:11 +02002413static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
2414{
2415 struct vcpu_svm *svm = to_svm(vcpu);
2416 struct vmcb *vmcb = svm->vmcb;
2417 return (vmcb->save.rflags & X86_EFLAGS_IF) &&
2418 !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2419 (svm->vcpu.arch.hflags & HF_GIF_MASK);
2420}
2421
Gleb Natapov9222be12009-04-23 17:14:37 +03002422static void enable_irq_window(struct kvm_vcpu *vcpu)
2423{
2424 svm_set_vintr(to_svm(vcpu));
2425 svm_inject_irq(to_svm(vcpu), 0x0);
2426}
2427
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002428static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002430 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002431
Gleb Natapov44c11432009-05-11 13:35:52 +03002432 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
2433 == HF_NMI_MASK)
2434 return; /* IRET will cause a vm exit */
2435
2436 /* Something prevents NMI from been injected. Single step over
2437 possible problem (IRET or exception injection or interrupt
2438 shadow) */
2439 vcpu->arch.singlestep = true;
2440 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2441 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002442}
2443
Izik Eiduscbc94022007-10-25 00:29:55 +02002444static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
2445{
2446 return 0;
2447}
2448
Avi Kivityd9e368d2007-06-07 19:18:30 +03002449static void svm_flush_tlb(struct kvm_vcpu *vcpu)
2450{
2451 force_new_asid(vcpu);
2452}
2453
Avi Kivity04d2cc72007-09-10 18:10:54 +03002454static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
2455{
2456}
2457
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002458static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
2459{
2460 struct vcpu_svm *svm = to_svm(vcpu);
2461
2462 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
2463 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03002464 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002465 }
2466}
2467
Joerg Roedel649d6862008-04-16 16:51:15 +02002468static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
2469{
2470 struct vcpu_svm *svm = to_svm(vcpu);
2471 u64 cr8;
2472
Joerg Roedel649d6862008-04-16 16:51:15 +02002473 cr8 = kvm_get_cr8(vcpu);
2474 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
2475 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
2476}
2477
Gleb Natapov9222be12009-04-23 17:14:37 +03002478static void svm_complete_interrupts(struct vcpu_svm *svm)
2479{
2480 u8 vector;
2481 int type;
2482 u32 exitintinfo = svm->vmcb->control.exit_int_info;
2483
Gleb Natapov44c11432009-05-11 13:35:52 +03002484 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
2485 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
2486
Gleb Natapov9222be12009-04-23 17:14:37 +03002487 svm->vcpu.arch.nmi_injected = false;
2488 kvm_clear_exception_queue(&svm->vcpu);
2489 kvm_clear_interrupt_queue(&svm->vcpu);
2490
2491 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
2492 return;
2493
2494 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
2495 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
2496
2497 switch (type) {
2498 case SVM_EXITINTINFO_TYPE_NMI:
2499 svm->vcpu.arch.nmi_injected = true;
2500 break;
2501 case SVM_EXITINTINFO_TYPE_EXEPT:
2502 /* In case of software exception do not reinject an exception
2503 vector, but re-execute and instruction instead */
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002504 if (kvm_exception_is_soft(vector))
Gleb Natapov9222be12009-04-23 17:14:37 +03002505 break;
2506 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
2507 u32 err = svm->vmcb->control.exit_int_info_err;
2508 kvm_queue_exception_e(&svm->vcpu, vector, err);
2509
2510 } else
2511 kvm_queue_exception(&svm->vcpu, vector);
2512 break;
2513 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002514 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03002515 break;
2516 default:
2517 break;
2518 }
2519}
2520
Avi Kivity80e31d42008-07-14 14:44:59 +03002521#ifdef CONFIG_X86_64
2522#define R "r"
2523#else
2524#define R "e"
2525#endif
2526
Avi Kivity04d2cc72007-09-10 18:10:54 +03002527static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002529 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002530 u16 fs_selector;
2531 u16 gs_selector;
2532 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03002533
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002534 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
2535 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2536 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
2537
Rusty Russelle756fc62007-07-30 20:07:08 +10002538 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539
Joerg Roedel649d6862008-04-16 16:51:15 +02002540 sync_lapic_to_cr8(vcpu);
2541
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542 save_host_msrs(vcpu);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002543 fs_selector = kvm_read_fs();
2544 gs_selector = kvm_read_gs();
2545 ldt_selector = kvm_read_ldt();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002546 svm->host_cr2 = kvm_read_cr2();
Alexander Graf3d6368e2008-11-25 20:17:07 +01002547 if (!is_nested(svm))
2548 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002549 /* required for live migration with NPT */
2550 if (npt_enabled)
2551 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552
Avi Kivity04d2cc72007-09-10 18:10:54 +03002553 clgi();
2554
2555 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08002556
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03002558 "push %%"R"bp; \n\t"
2559 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
2560 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
2561 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
2562 "mov %c[rsi](%[svm]), %%"R"si \n\t"
2563 "mov %c[rdi](%[svm]), %%"R"di \n\t"
2564 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002565#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002566 "mov %c[r8](%[svm]), %%r8 \n\t"
2567 "mov %c[r9](%[svm]), %%r9 \n\t"
2568 "mov %c[r10](%[svm]), %%r10 \n\t"
2569 "mov %c[r11](%[svm]), %%r11 \n\t"
2570 "mov %c[r12](%[svm]), %%r12 \n\t"
2571 "mov %c[r13](%[svm]), %%r13 \n\t"
2572 "mov %c[r14](%[svm]), %%r14 \n\t"
2573 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574#endif
2575
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03002577 "push %%"R"ax \n\t"
2578 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002579 __ex(SVM_VMLOAD) "\n\t"
2580 __ex(SVM_VMRUN) "\n\t"
2581 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03002582 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583
2584 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03002585 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
2586 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
2587 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
2588 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
2589 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
2590 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002591#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002592 "mov %%r8, %c[r8](%[svm]) \n\t"
2593 "mov %%r9, %c[r9](%[svm]) \n\t"
2594 "mov %%r10, %c[r10](%[svm]) \n\t"
2595 "mov %%r11, %c[r11](%[svm]) \n\t"
2596 "mov %%r12, %c[r12](%[svm]) \n\t"
2597 "mov %%r13, %c[r13](%[svm]) \n\t"
2598 "mov %%r14, %c[r14](%[svm]) \n\t"
2599 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03002601 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002603 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002605 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
2606 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
2607 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
2608 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
2609 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
2610 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002611#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002612 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
2613 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
2614 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
2615 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
2616 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
2617 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
2618 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
2619 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02002621 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03002622 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02002623#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02002624 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
2625#endif
2626 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08002627
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002628 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002629 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
2630 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
2631 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002633 kvm_write_cr2(svm->host_cr2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002635 kvm_load_fs(fs_selector);
2636 kvm_load_gs(gs_selector);
2637 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638 load_host_msrs(vcpu);
2639
2640 reload_tss(vcpu);
2641
Avi Kivity56ba47d2007-11-07 17:14:18 +02002642 local_irq_disable();
2643
2644 stgi();
2645
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002646 sync_cr8_to_lapic(vcpu);
2647
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002648 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03002649
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002650 if (npt_enabled) {
2651 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
2652 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
2653 }
2654
Gleb Natapov9222be12009-04-23 17:14:37 +03002655 svm_complete_interrupts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002656}
2657
Avi Kivity80e31d42008-07-14 14:44:59 +03002658#undef R
2659
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
2661{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002662 struct vcpu_svm *svm = to_svm(vcpu);
2663
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002664 if (npt_enabled) {
2665 svm->vmcb->control.nested_cr3 = root;
2666 force_new_asid(vcpu);
2667 return;
2668 }
2669
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002670 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671 force_new_asid(vcpu);
Anthony Liguori7807fa62007-04-23 09:17:21 -05002672
2673 if (vcpu->fpu_active) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002674 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
2675 svm->vmcb->save.cr0 |= X86_CR0_TS;
Anthony Liguori7807fa62007-04-23 09:17:21 -05002676 vcpu->fpu_active = 0;
2677 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678}
2679
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680static int is_disabled(void)
2681{
Joerg Roedel6031a612007-06-22 12:29:50 +03002682 u64 vm_cr;
2683
2684 rdmsrl(MSR_VM_CR, vm_cr);
2685 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
2686 return 1;
2687
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688 return 0;
2689}
2690
Ingo Molnar102d8322007-02-19 14:37:47 +02002691static void
2692svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2693{
2694 /*
2695 * Patch in the VMMCALL instruction:
2696 */
2697 hypercall[0] = 0x0f;
2698 hypercall[1] = 0x01;
2699 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02002700}
2701
Yang, Sheng002c7f72007-07-31 14:23:01 +03002702static void svm_check_processor_compat(void *rtn)
2703{
2704 *(int *)rtn = 0;
2705}
2706
Avi Kivity774ead32007-12-26 13:57:04 +02002707static bool svm_cpu_has_accelerated_tpr(void)
2708{
2709 return false;
2710}
2711
Sheng Yang67253af2008-04-25 10:20:22 +08002712static int get_npt_level(void)
2713{
2714#ifdef CONFIG_X86_64
2715 return PT64_ROOT_LEVEL;
2716#else
2717 return PT32E_ROOT_LEVEL;
2718#endif
2719}
2720
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002721static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08002722{
2723 return 0;
2724}
2725
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002726static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 .cpu_has_kvm_support = has_svm,
2728 .disabled_by_bios = is_disabled,
2729 .hardware_setup = svm_hardware_setup,
2730 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03002731 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 .hardware_enable = svm_hardware_enable,
2733 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02002734 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735
2736 .vcpu_create = svm_create_vcpu,
2737 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002738 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739
Avi Kivity04d2cc72007-09-10 18:10:54 +03002740 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 .vcpu_load = svm_vcpu_load,
2742 .vcpu_put = svm_vcpu_put,
2743
2744 .set_guest_debug = svm_guest_debug,
2745 .get_msr = svm_get_msr,
2746 .set_msr = svm_set_msr,
2747 .get_segment_base = svm_get_segment_base,
2748 .get_segment = svm_get_segment,
2749 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02002750 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10002751 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002752 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002754 .set_cr3 = svm_set_cr3,
2755 .set_cr4 = svm_set_cr4,
2756 .set_efer = svm_set_efer,
2757 .get_idt = svm_get_idt,
2758 .set_idt = svm_set_idt,
2759 .get_gdt = svm_get_gdt,
2760 .set_gdt = svm_set_gdt,
2761 .get_dr = svm_get_dr,
2762 .set_dr = svm_set_dr,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002763 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764 .get_rflags = svm_get_rflags,
2765 .set_rflags = svm_set_rflags,
2766
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002770 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04002772 .set_interrupt_shadow = svm_set_interrupt_shadow,
2773 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02002774 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03002775 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002776 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02002777 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02002778 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002779 .nmi_allowed = svm_nmi_allowed,
2780 .enable_nmi_window = enable_nmi_window,
2781 .enable_irq_window = enable_irq_window,
2782 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02002783
2784 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08002785 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002786 .get_mt_mask = svm_get_mt_mask,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002787};
2788
2789static int __init svm_init(void)
2790{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002791 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10002792 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793}
2794
2795static void __exit svm_exit(void)
2796{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002797 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002798}
2799
2800module_init(svm_init)
2801module_exit(svm_exit)