blob: 284c34c997a441ba6174769a29432e2acb4c781f [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.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02007 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08008 *
9 * Authors:
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
Avi Kivityedf88412007-12-16 11:02:48 +020017#include <linux/kvm_host.h>
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030021#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020022#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020025#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/vmalloc.h>
27#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030029#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080031
Joerg Roedel67ec6602010-05-17 14:43:35 +020032#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040033#include <asm/desc.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020034#include <asm/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Eduardo Habkost63d11422008-11-17 19:03:20 -020036#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030037#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020038
Avi Kivity4ecac3f2008-05-13 13:23:38 +030039#define __ex(x) __kvm_handle_fault_on_reboot(x)
40
Avi Kivity6aa8b732006-12-10 02:21:36 -080041MODULE_AUTHOR("Qumranet");
42MODULE_LICENSE("GPL");
43
44#define IOPM_ALLOC_ORDER 2
45#define MSRPM_ALLOC_ORDER 1
46
Avi Kivity6aa8b732006-12-10 02:21:36 -080047#define SEG_TYPE_LDT 2
48#define SEG_TYPE_BUSY_TSS16 3
49
Andre Przywara6bc31bd2010-04-11 23:07:28 +020050#define SVM_FEATURE_NPT (1 << 0)
51#define SVM_FEATURE_LBRV (1 << 1)
52#define SVM_FEATURE_SVML (1 << 2)
53#define SVM_FEATURE_NRIP (1 << 3)
54#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030055
Joerg Roedel410e4d52009-08-07 11:49:44 +020056#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
57#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
58#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
59
Joerg Roedel24e09cb2008-02-13 18:58:47 +010060#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
61
Joerg Roedel67ec6602010-05-17 14:43:35 +020062static bool erratum_383_found __read_mostly;
63
Avi Kivity6c8166a2009-05-31 18:15:37 +030064static const u32 host_save_user_msrs[] = {
65#ifdef CONFIG_X86_64
66 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
67 MSR_FS_BASE,
68#endif
69 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
70};
71
72#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
73
74struct kvm_vcpu;
75
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020076struct nested_state {
77 struct vmcb *hsave;
78 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010079 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020080 u64 vmcb;
81
82 /* These are the merged vectors */
83 u32 *msrpm;
84
85 /* gpa pointers to the real vectors */
86 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +010087 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020088
Joerg Roedelcd3ff652009-10-09 16:08:26 +020089 /* A VMEXIT is required but not yet emulated */
90 bool exit_required;
91
Joerg Roedelcda00082010-09-02 17:29:46 +020092 /*
93 * If we vmexit during an instruction emulation we need this to restore
94 * the l1 guest rip after the emulation
95 */
96 unsigned long vmexit_rip;
97 unsigned long vmexit_rsp;
98 unsigned long vmexit_rax;
99
Joerg Roedelaad42c62009-08-07 11:49:34 +0200100 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100101 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100102 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200103 u32 intercept_exceptions;
104 u64 intercept;
105
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200106 /* Nested Paging related state */
107 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200108};
109
Joerg Roedel323c3d82010-03-01 15:34:37 +0100110#define MSRPM_OFFSETS 16
111static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
112
Avi Kivity6c8166a2009-05-31 18:15:37 +0300113struct vcpu_svm {
114 struct kvm_vcpu vcpu;
115 struct vmcb *vmcb;
116 unsigned long vmcb_pa;
117 struct svm_cpu_data *svm_data;
118 uint64_t asid_generation;
119 uint64_t sysenter_esp;
120 uint64_t sysenter_eip;
121
122 u64 next_rip;
123
124 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200125 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200126 u16 fs;
127 u16 gs;
128 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200129 u64 gs_base;
130 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300131
132 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300133
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200134 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200135
136 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100137
138 unsigned int3_injected;
139 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200140 u32 apf_reason;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300141};
142
Joerg Roedel455716f2010-03-01 15:34:35 +0100143#define MSR_INVALID 0xffffffffU
144
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100145static struct svm_direct_access_msrs {
146 u32 index; /* Index of the MSR */
147 bool always; /* True if intercept is always on */
148} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400149 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100150 { .index = MSR_IA32_SYSENTER_CS, .always = true },
151#ifdef CONFIG_X86_64
152 { .index = MSR_GS_BASE, .always = true },
153 { .index = MSR_FS_BASE, .always = true },
154 { .index = MSR_KERNEL_GS_BASE, .always = true },
155 { .index = MSR_LSTAR, .always = true },
156 { .index = MSR_CSTAR, .always = true },
157 { .index = MSR_SYSCALL_MASK, .always = true },
158#endif
159 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
160 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
161 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
162 { .index = MSR_IA32_LASTINTTOIP, .always = false },
163 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164};
165
166/* enable NPT for AMD64 and X86 with PAE */
167#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
168static bool npt_enabled = true;
169#else
Joerg Roedele0231712010-02-24 18:59:10 +0100170static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171#endif
172static int npt = 1;
173
174module_param(npt, int, S_IRUGO);
175
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200176static int nested = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177module_param(nested, int, S_IRUGO);
178
179static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200180static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181
Joerg Roedel410e4d52009-08-07 11:49:44 +0200182static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100183static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
186 bool has_error_code, u32 error_code);
187
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100188enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100189 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
190 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100191 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100192 VMCB_DIRTY_MAX,
193};
194
195#define VMCB_ALWAYS_DIRTY_MASK 0U
196
197static inline void mark_all_dirty(struct vmcb *vmcb)
198{
199 vmcb->control.clean = 0;
200}
201
202static inline void mark_all_clean(struct vmcb *vmcb)
203{
204 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
205 & ~VMCB_ALWAYS_DIRTY_MASK;
206}
207
208static inline void mark_dirty(struct vmcb *vmcb, int bit)
209{
210 vmcb->control.clean &= ~(1 << bit);
211}
212
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
214{
215 return container_of(vcpu, struct vcpu_svm, vcpu);
216}
217
Joerg Roedel384c6362010-11-30 18:03:56 +0100218static void recalc_intercepts(struct vcpu_svm *svm)
219{
220 struct vmcb_control_area *c, *h;
221 struct nested_state *g;
222
Joerg Roedel116a0a22010-12-03 11:45:49 +0100223 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
224
Joerg Roedel384c6362010-11-30 18:03:56 +0100225 if (!is_guest_mode(&svm->vcpu))
226 return;
227
228 c = &svm->vmcb->control;
229 h = &svm->nested.hsave->control;
230 g = &svm->nested;
231
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100232 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100233 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100234 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
235 c->intercept = h->intercept | g->intercept;
236}
237
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100238static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
239{
240 if (is_guest_mode(&svm->vcpu))
241 return svm->nested.hsave;
242 else
243 return svm->vmcb;
244}
245
246static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
247{
248 struct vmcb *vmcb = get_host_vmcb(svm);
249
250 vmcb->control.intercept_cr |= (1U << bit);
251
252 recalc_intercepts(svm);
253}
254
255static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
256{
257 struct vmcb *vmcb = get_host_vmcb(svm);
258
259 vmcb->control.intercept_cr &= ~(1U << bit);
260
261 recalc_intercepts(svm);
262}
263
264static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
265{
266 struct vmcb *vmcb = get_host_vmcb(svm);
267
268 return vmcb->control.intercept_cr & (1U << bit);
269}
270
Joerg Roedel3aed0412010-11-30 18:03:58 +0100271static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
272{
273 struct vmcb *vmcb = get_host_vmcb(svm);
274
275 vmcb->control.intercept_dr |= (1U << bit);
276
277 recalc_intercepts(svm);
278}
279
280static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
281{
282 struct vmcb *vmcb = get_host_vmcb(svm);
283
284 vmcb->control.intercept_dr &= ~(1U << bit);
285
286 recalc_intercepts(svm);
287}
288
Joerg Roedel18c918c2010-11-30 18:03:59 +0100289static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
290{
291 struct vmcb *vmcb = get_host_vmcb(svm);
292
293 vmcb->control.intercept_exceptions |= (1U << bit);
294
295 recalc_intercepts(svm);
296}
297
298static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
299{
300 struct vmcb *vmcb = get_host_vmcb(svm);
301
302 vmcb->control.intercept_exceptions &= ~(1U << bit);
303
304 recalc_intercepts(svm);
305}
306
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100307static inline void set_intercept(struct vcpu_svm *svm, int bit)
308{
309 struct vmcb *vmcb = get_host_vmcb(svm);
310
311 vmcb->control.intercept |= (1ULL << bit);
312
313 recalc_intercepts(svm);
314}
315
316static inline void clr_intercept(struct vcpu_svm *svm, int bit)
317{
318 struct vmcb *vmcb = get_host_vmcb(svm);
319
320 vmcb->control.intercept &= ~(1ULL << bit);
321
322 recalc_intercepts(svm);
323}
324
Joerg Roedel2af91942009-08-07 11:49:28 +0200325static inline void enable_gif(struct vcpu_svm *svm)
326{
327 svm->vcpu.arch.hflags |= HF_GIF_MASK;
328}
329
330static inline void disable_gif(struct vcpu_svm *svm)
331{
332 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
333}
334
335static inline bool gif_set(struct vcpu_svm *svm)
336{
337 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
338}
339
Avi Kivity6aa8b732006-12-10 02:21:36 -0800340static unsigned long iopm_base;
341
342struct kvm_ldttss_desc {
343 u16 limit0;
344 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100345 unsigned base1:8, type:5, dpl:2, p:1;
346 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347 u32 base3;
348 u32 zero1;
349} __attribute__((packed));
350
351struct svm_cpu_data {
352 int cpu;
353
Avi Kivity5008fdf2007-04-02 13:05:50 +0300354 u64 asid_generation;
355 u32 max_asid;
356 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357 struct kvm_ldttss_desc *tss_desc;
358
359 struct page *save_area;
360};
361
362static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300363static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800364
365struct svm_init_data {
366 int cpu;
367 int r;
368};
369
370static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
371
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200372#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373#define MSRS_RANGE_SIZE 2048
374#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
375
Joerg Roedel455716f2010-03-01 15:34:35 +0100376static u32 svm_msrpm_offset(u32 msr)
377{
378 u32 offset;
379 int i;
380
381 for (i = 0; i < NUM_MSR_MAPS; i++) {
382 if (msr < msrpm_ranges[i] ||
383 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
384 continue;
385
386 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
387 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
388
389 /* Now we have the u8 offset - but need the u32 offset */
390 return offset / 4;
391 }
392
393 /* MSR not in any range */
394 return MSR_INVALID;
395}
396
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397#define MAX_INST_SIZE 15
398
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399static inline void clgi(void)
400{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300401 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402}
403
404static inline void stgi(void)
405{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300406 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407}
408
409static inline void invlpga(unsigned long addr, u32 asid)
410{
Joerg Roedele0231712010-02-24 18:59:10 +0100411 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412}
413
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414static inline void force_new_asid(struct kvm_vcpu *vcpu)
415{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400416 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417}
418
419static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
420{
421 force_new_asid(vcpu);
422}
423
Joerg Roedel4b161842010-09-10 17:31:03 +0200424static int get_npt_level(void)
425{
426#ifdef CONFIG_X86_64
427 return PT64_ROOT_LEVEL;
428#else
429 return PT32E_ROOT_LEVEL;
430#endif
431}
432
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
434{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000435 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100436 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600437 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438
Alexander Graf9962d032008-11-25 20:17:02 +0100439 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440}
441
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442static int is_external_interrupt(u32 info)
443{
444 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
445 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
446}
447
Glauber Costa2809f5d2009-05-12 16:21:05 -0400448static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
449{
450 struct vcpu_svm *svm = to_svm(vcpu);
451 u32 ret = 0;
452
453 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100454 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400455 return ret & mask;
456}
457
458static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
459{
460 struct vcpu_svm *svm = to_svm(vcpu);
461
462 if (mask == 0)
463 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
464 else
465 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
466
467}
468
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
470{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400471 struct vcpu_svm *svm = to_svm(vcpu);
472
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200473 if (svm->vmcb->control.next_rip != 0)
474 svm->next_rip = svm->vmcb->control.next_rip;
475
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400476 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300477 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300478 EMULATE_DONE)
479 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 return;
481 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300482 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
483 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
484 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300486 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400487 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800488}
489
Jan Kiszka116a4752010-02-23 17:47:54 +0100490static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200491 bool has_error_code, u32 error_code,
492 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100493{
494 struct vcpu_svm *svm = to_svm(vcpu);
495
Joerg Roedele0231712010-02-24 18:59:10 +0100496 /*
497 * If we are within a nested VM we'd better #VMEXIT and let the guest
498 * handle the exception
499 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200500 if (!reinject &&
501 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100502 return;
503
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200504 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100505 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
506
507 /*
508 * For guest debugging where we have to reinject #BP if some
509 * INT3 is guest-owned:
510 * Emulate nRIP by moving RIP forward. Will fail if injection
511 * raises a fault that is not intercepted. Still better than
512 * failing in all cases.
513 */
514 skip_emulated_instruction(&svm->vcpu);
515 rip = kvm_rip_read(&svm->vcpu);
516 svm->int3_rip = rip + svm->vmcb->save.cs.base;
517 svm->int3_injected = rip - old_rip;
518 }
519
Jan Kiszka116a4752010-02-23 17:47:54 +0100520 svm->vmcb->control.event_inj = nr
521 | SVM_EVTINJ_VALID
522 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
523 | SVM_EVTINJ_TYPE_EXEPT;
524 svm->vmcb->control.event_inj_err = error_code;
525}
526
Joerg Roedel67ec6602010-05-17 14:43:35 +0200527static void svm_init_erratum_383(void)
528{
529 u32 low, high;
530 int err;
531 u64 val;
532
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200533 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200534 return;
535
536 /* Use _safe variants to not break nested virtualization */
537 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
538 if (err)
539 return;
540
541 val |= (1ULL << 47);
542
543 low = lower_32_bits(val);
544 high = upper_32_bits(val);
545
546 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
547
548 erratum_383_found = true;
549}
550
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551static int has_svm(void)
552{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200553 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800554
Eduardo Habkost63d11422008-11-17 19:03:20 -0200555 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800556 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800557 return 0;
558 }
559
Avi Kivity6aa8b732006-12-10 02:21:36 -0800560 return 1;
561}
562
563static void svm_hardware_disable(void *garbage)
564{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200565 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566}
567
Alexander Graf10474ae2009-09-15 11:37:46 +0200568static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569{
570
Tejun Heo0fe1e002009-10-29 22:34:14 +0900571 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200573 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800574 struct desc_struct *gdt;
575 int me = raw_smp_processor_id();
576
Alexander Graf10474ae2009-09-15 11:37:46 +0200577 rdmsrl(MSR_EFER, efer);
578 if (efer & EFER_SVME)
579 return -EBUSY;
580
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000582 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
583 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200584 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900586 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587
Tejun Heo0fe1e002009-10-29 22:34:14 +0900588 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000589 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200591 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 }
593
Tejun Heo0fe1e002009-10-29 22:34:14 +0900594 sd->asid_generation = 1;
595 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
596 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800597
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200598 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200599 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900600 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800601
Alexander Graf9962d032008-11-25 20:17:02 +0100602 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800603
Linus Torvaldsd0316552009-12-14 09:58:24 -0800604 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200605
Joerg Roedel67ec6602010-05-17 14:43:35 +0200606 svm_init_erratum_383();
607
Alexander Graf10474ae2009-09-15 11:37:46 +0200608 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800609}
610
Joerg Roedel0da1db752008-07-02 16:02:11 +0200611static void svm_cpu_uninit(int cpu)
612{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900613 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200614
Tejun Heo0fe1e002009-10-29 22:34:14 +0900615 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200616 return;
617
618 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900619 __free_page(sd->save_area);
620 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200621}
622
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623static int svm_cpu_init(int cpu)
624{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900625 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626 int r;
627
Tejun Heo0fe1e002009-10-29 22:34:14 +0900628 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
629 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900631 sd->cpu = cpu;
632 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900634 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635 goto err_1;
636
Tejun Heo0fe1e002009-10-29 22:34:14 +0900637 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638
639 return 0;
640
641err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900642 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643 return r;
644
645}
646
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100647static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648{
649 int i;
650
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100651 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
652 if (direct_access_msrs[i].index == index)
653 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100655 return false;
656}
657
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658static void set_msr_interception(u32 *msrpm, unsigned msr,
659 int read, int write)
660{
Joerg Roedel455716f2010-03-01 15:34:35 +0100661 u8 bit_read, bit_write;
662 unsigned long tmp;
663 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100665 /*
666 * If this warning triggers extend the direct_access_msrs list at the
667 * beginning of the file
668 */
669 WARN_ON(!valid_msr_intercept(msr));
670
Joerg Roedel455716f2010-03-01 15:34:35 +0100671 offset = svm_msrpm_offset(msr);
672 bit_read = 2 * (msr & 0x0f);
673 bit_write = 2 * (msr & 0x0f) + 1;
674 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675
Joerg Roedel455716f2010-03-01 15:34:35 +0100676 BUG_ON(offset == MSR_INVALID);
677
678 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
679 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
680
681 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682}
683
Joerg Roedelf65c2292008-02-13 18:58:46 +0100684static void svm_vcpu_init_msrpm(u32 *msrpm)
685{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100686 int i;
687
Joerg Roedelf65c2292008-02-13 18:58:46 +0100688 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
689
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100690 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
691 if (!direct_access_msrs[i].always)
692 continue;
693
694 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
695 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100696}
697
Joerg Roedel323c3d82010-03-01 15:34:37 +0100698static void add_msr_offset(u32 offset)
699{
700 int i;
701
702 for (i = 0; i < MSRPM_OFFSETS; ++i) {
703
704 /* Offset already in list? */
705 if (msrpm_offsets[i] == offset)
706 return;
707
708 /* Slot used by another offset? */
709 if (msrpm_offsets[i] != MSR_INVALID)
710 continue;
711
712 /* Add offset to list */
713 msrpm_offsets[i] = offset;
714
715 return;
716 }
717
718 /*
719 * If this BUG triggers the msrpm_offsets table has an overflow. Just
720 * increase MSRPM_OFFSETS in this case.
721 */
722 BUG();
723}
724
725static void init_msrpm_offsets(void)
726{
727 int i;
728
729 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
730
731 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
732 u32 offset;
733
734 offset = svm_msrpm_offset(direct_access_msrs[i].index);
735 BUG_ON(offset == MSR_INVALID);
736
737 add_msr_offset(offset);
738 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739}
740
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100741static void svm_enable_lbrv(struct vcpu_svm *svm)
742{
743 u32 *msrpm = svm->msrpm;
744
745 svm->vmcb->control.lbr_ctl = 1;
746 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
747 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
748 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
749 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
750}
751
752static void svm_disable_lbrv(struct vcpu_svm *svm)
753{
754 u32 *msrpm = svm->msrpm;
755
756 svm->vmcb->control.lbr_ctl = 0;
757 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
758 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
759 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
760 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
761}
762
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763static __init int svm_hardware_setup(void)
764{
765 int cpu;
766 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100767 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 int r;
769
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
771
772 if (!iopm_pages)
773 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300774
775 iopm_va = page_address(iopm_pages);
776 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
778
Joerg Roedel323c3d82010-03-01 15:34:37 +0100779 init_msrpm_offsets();
780
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100781 if (boot_cpu_has(X86_FEATURE_NX))
782 kvm_enable_efer_bits(EFER_NX);
783
Alexander Graf1b2fd702009-02-02 16:23:51 +0100784 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
785 kvm_enable_efer_bits(EFER_FFXSR);
786
Alexander Graf236de052008-11-25 20:17:10 +0100787 if (nested) {
788 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200789 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100790 }
791
Zachary Amsden3230bb42009-09-29 11:38:37 -1000792 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 r = svm_cpu_init(cpu);
794 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100795 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100797
798 svm_features = cpuid_edx(SVM_CPUID_FUNC);
799
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200800 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100801 npt_enabled = false;
802
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100803 if (npt_enabled && !npt) {
804 printk(KERN_INFO "kvm: Nested Paging disabled\n");
805 npt_enabled = false;
806 }
807
Joerg Roedel18552672008-02-07 13:47:41 +0100808 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100809 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100810 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200811 } else
812 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100813
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814 return 0;
815
Joerg Roedelf65c2292008-02-13 18:58:46 +0100816err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
818 iopm_base = 0;
819 return r;
820}
821
822static __exit void svm_hardware_unsetup(void)
823{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200824 int cpu;
825
Zachary Amsden3230bb42009-09-29 11:38:37 -1000826 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200827 svm_cpu_uninit(cpu);
828
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100830 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831}
832
833static void init_seg(struct vmcb_seg *seg)
834{
835 seg->selector = 0;
836 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100837 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838 seg->limit = 0xffff;
839 seg->base = 0;
840}
841
842static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
843{
844 seg->selector = 0;
845 seg->attrib = SVM_SELECTOR_P_MASK | type;
846 seg->limit = 0xffff;
847 seg->base = 0;
848}
849
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000850static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
851{
852 struct vcpu_svm *svm = to_svm(vcpu);
853 u64 g_tsc_offset = 0;
854
Joerg Roedel20307532010-11-29 17:51:48 +0100855 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000856 g_tsc_offset = svm->vmcb->control.tsc_offset -
857 svm->nested.hsave->control.tsc_offset;
858 svm->nested.hsave->control.tsc_offset = offset;
859 }
860
861 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +0100862
863 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000864}
865
Zachary Amsdene48672f2010-08-19 22:07:23 -1000866static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
867{
868 struct vcpu_svm *svm = to_svm(vcpu);
869
870 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +0100871 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -1000872 svm->nested.hsave->control.tsc_offset += adjustment;
Joerg Roedel116a0a22010-12-03 11:45:49 +0100873 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -1000874}
875
Joerg Roedele6101a92008-02-13 18:58:45 +0100876static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877{
Joerg Roedele6101a92008-02-13 18:58:45 +0100878 struct vmcb_control_area *control = &svm->vmcb->control;
879 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880
Avi Kivitybff78272010-01-07 13:16:08 +0200881 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100882 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +0200883
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100884 set_cr_intercept(svm, INTERCEPT_CR0_READ);
885 set_cr_intercept(svm, INTERCEPT_CR3_READ);
886 set_cr_intercept(svm, INTERCEPT_CR4_READ);
887 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
888 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
889 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
890 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891
Joerg Roedel3aed0412010-11-30 18:03:58 +0100892 set_dr_intercept(svm, INTERCEPT_DR0_READ);
893 set_dr_intercept(svm, INTERCEPT_DR1_READ);
894 set_dr_intercept(svm, INTERCEPT_DR2_READ);
895 set_dr_intercept(svm, INTERCEPT_DR3_READ);
896 set_dr_intercept(svm, INTERCEPT_DR4_READ);
897 set_dr_intercept(svm, INTERCEPT_DR5_READ);
898 set_dr_intercept(svm, INTERCEPT_DR6_READ);
899 set_dr_intercept(svm, INTERCEPT_DR7_READ);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900
Joerg Roedel3aed0412010-11-30 18:03:58 +0100901 set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
902 set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
903 set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
904 set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
905 set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
906 set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
907 set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
908 set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909
Joerg Roedel18c918c2010-11-30 18:03:59 +0100910 set_exception_intercept(svm, PF_VECTOR);
911 set_exception_intercept(svm, UD_VECTOR);
912 set_exception_intercept(svm, MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100914 set_intercept(svm, INTERCEPT_INTR);
915 set_intercept(svm, INTERCEPT_NMI);
916 set_intercept(svm, INTERCEPT_SMI);
917 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
918 set_intercept(svm, INTERCEPT_CPUID);
919 set_intercept(svm, INTERCEPT_INVD);
920 set_intercept(svm, INTERCEPT_HLT);
921 set_intercept(svm, INTERCEPT_INVLPG);
922 set_intercept(svm, INTERCEPT_INVLPGA);
923 set_intercept(svm, INTERCEPT_IOIO_PROT);
924 set_intercept(svm, INTERCEPT_MSR_PROT);
925 set_intercept(svm, INTERCEPT_TASK_SWITCH);
926 set_intercept(svm, INTERCEPT_SHUTDOWN);
927 set_intercept(svm, INTERCEPT_VMRUN);
928 set_intercept(svm, INTERCEPT_VMMCALL);
929 set_intercept(svm, INTERCEPT_VMLOAD);
930 set_intercept(svm, INTERCEPT_VMSAVE);
931 set_intercept(svm, INTERCEPT_STGI);
932 set_intercept(svm, INTERCEPT_CLGI);
933 set_intercept(svm, INTERCEPT_SKINIT);
934 set_intercept(svm, INTERCEPT_WBINVD);
935 set_intercept(svm, INTERCEPT_MONITOR);
936 set_intercept(svm, INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937
938 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100939 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 control->int_ctl = V_INTR_MASKING_MASK;
941
942 init_seg(&save->es);
943 init_seg(&save->ss);
944 init_seg(&save->ds);
945 init_seg(&save->fs);
946 init_seg(&save->gs);
947
948 save->cs.selector = 0xf000;
949 /* Executable/Readable Code Segment */
950 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
951 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
952 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800953 /*
954 * cs.base should really be 0xffff0000, but vmx can't handle that, so
955 * be consistent with it.
956 *
957 * Replace when we have real mode working for vmx.
958 */
959 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960
961 save->gdtr.limit = 0xffff;
962 save->idtr.limit = 0xffff;
963
964 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
965 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
966
Marcelo Tosattieaa48512010-08-31 19:13:14 -0300967 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -0400968 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969 save->dr7 = 0x400;
970 save->rflags = 2;
971 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300972 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973
Joerg Roedele0231712010-02-24 18:59:10 +0100974 /*
975 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200976 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -0300978 svm->vcpu.arch.cr0 = 0;
979 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200980
Rusty Russell66aee912007-07-17 23:34:16 +1000981 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100983
984 if (npt_enabled) {
985 /* Setup VMCB for Nested Paging */
986 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100987 clr_intercept(svm, INTERCEPT_TASK_SWITCH);
988 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +0100989 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100990 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
991 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100992 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100993 save->cr3 = 0;
994 save->cr4 = 0;
995 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300996 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100997
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200998 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200999 svm->vcpu.arch.hflags = 0;
1000
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001001 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001002 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001003 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001004 }
1005
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001006 mark_all_dirty(svm->vmcb);
1007
Joerg Roedel2af91942009-08-07 11:49:28 +02001008 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009}
1010
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001011static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001012{
1013 struct vcpu_svm *svm = to_svm(vcpu);
1014
Joerg Roedele6101a92008-02-13 18:58:45 +01001015 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001016
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001017 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001018 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001019 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
1020 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +02001021 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001022 vcpu->arch.regs_avail = ~0;
1023 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001024
1025 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001026}
1027
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001028static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001030 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001032 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001033 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001034 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001035 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036
Rusty Russellc16f8622007-07-30 21:12:19 +10001037 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001038 if (!svm) {
1039 err = -ENOMEM;
1040 goto out;
1041 }
1042
1043 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1044 if (err)
1045 goto free_svm;
1046
Joerg Roedelf65c2292008-02-13 18:58:46 +01001047 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001048 page = alloc_page(GFP_KERNEL);
1049 if (!page)
1050 goto uninit;
1051
Joerg Roedelf65c2292008-02-13 18:58:46 +01001052 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1053 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001054 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001055
1056 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1057 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001058 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001059
Alexander Grafb286d5d2008-11-25 20:17:05 +01001060 hsave_page = alloc_page(GFP_KERNEL);
1061 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001062 goto free_page3;
1063
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001064 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001065
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001066 svm->msrpm = page_address(msrpm_pages);
1067 svm_vcpu_init_msrpm(svm->msrpm);
1068
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001069 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001070 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001071
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001072 svm->vmcb = page_address(page);
1073 clear_page(svm->vmcb);
1074 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1075 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +01001076 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001077 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001078
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001079 err = fx_init(&svm->vcpu);
1080 if (err)
1081 goto free_page4;
1082
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001083 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001084 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001085 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001087 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001088
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001089free_page4:
1090 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001091free_page3:
1092 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1093free_page2:
1094 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1095free_page1:
1096 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001097uninit:
1098 kvm_vcpu_uninit(&svm->vcpu);
1099free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001100 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001101out:
1102 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103}
1104
1105static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1106{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001107 struct vcpu_svm *svm = to_svm(vcpu);
1108
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001109 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001110 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001111 __free_page(virt_to_page(svm->nested.hsave));
1112 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001113 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001114 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115}
1116
Avi Kivity15ad7142007-07-11 18:17:21 +03001117static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001119 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001120 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001121
Avi Kivity0cc50642007-03-25 12:07:27 +02001122 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001123 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001124 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001125 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001126
Avi Kivity82ca2d12010-10-21 12:20:34 +02001127#ifdef CONFIG_X86_64
1128 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1129#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001130 savesegment(fs, svm->host.fs);
1131 savesegment(gs, svm->host.gs);
1132 svm->host.ldt = kvm_read_ldt();
1133
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001134 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001135 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136}
1137
1138static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1139{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001140 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001141 int i;
1142
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001143 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001144 kvm_load_ldt(svm->host.ldt);
1145#ifdef CONFIG_X86_64
1146 loadsegment(fs, svm->host.fs);
1147 load_gs_index(svm->host.gs);
1148 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
1149#else
1150 loadsegment(gs, svm->host.gs);
1151#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001152 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001153 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154}
1155
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1157{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001158 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159}
1160
1161static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1162{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001163 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164}
1165
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001166static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1167{
1168 switch (reg) {
1169 case VCPU_EXREG_PDPTR:
1170 BUG_ON(!npt_enabled);
Joerg Roedelff03a072010-09-10 17:30:57 +02001171 load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001172 break;
1173 default:
1174 BUG();
1175 }
1176}
1177
Alexander Graff0b85052008-11-25 20:17:01 +01001178static void svm_set_vintr(struct vcpu_svm *svm)
1179{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001180 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001181}
1182
1183static void svm_clear_vintr(struct vcpu_svm *svm)
1184{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001185 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001186}
1187
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1189{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001190 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191
1192 switch (seg) {
1193 case VCPU_SREG_CS: return &save->cs;
1194 case VCPU_SREG_DS: return &save->ds;
1195 case VCPU_SREG_ES: return &save->es;
1196 case VCPU_SREG_FS: return &save->fs;
1197 case VCPU_SREG_GS: return &save->gs;
1198 case VCPU_SREG_SS: return &save->ss;
1199 case VCPU_SREG_TR: return &save->tr;
1200 case VCPU_SREG_LDTR: return &save->ldtr;
1201 }
1202 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001203 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204}
1205
1206static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1207{
1208 struct vmcb_seg *s = svm_seg(vcpu, seg);
1209
1210 return s->base;
1211}
1212
1213static void svm_get_segment(struct kvm_vcpu *vcpu,
1214 struct kvm_segment *var, int seg)
1215{
1216 struct vmcb_seg *s = svm_seg(vcpu, seg);
1217
1218 var->base = s->base;
1219 var->limit = s->limit;
1220 var->selector = s->selector;
1221 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1222 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1223 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1224 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1225 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1226 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1227 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1228 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001229
Joerg Roedele0231712010-02-24 18:59:10 +01001230 /*
1231 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001232 * for cross vendor migration purposes by "not present"
1233 */
1234 var->unusable = !var->present || (var->type == 0);
1235
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001236 switch (seg) {
1237 case VCPU_SREG_CS:
1238 /*
1239 * SVM always stores 0 for the 'G' bit in the CS selector in
1240 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1241 * Intel's VMENTRY has a check on the 'G' bit.
1242 */
Amit Shah25022ac2008-10-27 09:04:17 +00001243 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001244 break;
1245 case VCPU_SREG_TR:
1246 /*
1247 * Work around a bug where the busy flag in the tr selector
1248 * isn't exposed
1249 */
Amit Shahc0d09822008-10-27 09:04:18 +00001250 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001251 break;
1252 case VCPU_SREG_DS:
1253 case VCPU_SREG_ES:
1254 case VCPU_SREG_FS:
1255 case VCPU_SREG_GS:
1256 /*
1257 * The accessed bit must always be set in the segment
1258 * descriptor cache, although it can be cleared in the
1259 * descriptor, the cached bit always remains at 1. Since
1260 * Intel has a check on this, set it here to support
1261 * cross-vendor migration.
1262 */
1263 if (!var->unusable)
1264 var->type |= 0x1;
1265 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001266 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001267 /*
1268 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001269 * descriptor is left as 1, although the whole segment has
1270 * been made unusable. Clear it here to pass an Intel VMX
1271 * entry check when cross vendor migrating.
1272 */
1273 if (var->unusable)
1274 var->db = 0;
1275 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001276 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277}
1278
Izik Eidus2e4d2652008-03-24 19:38:34 +02001279static int svm_get_cpl(struct kvm_vcpu *vcpu)
1280{
1281 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1282
1283 return save->cpl;
1284}
1285
Gleb Natapov89a27f42010-02-16 10:51:48 +02001286static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001288 struct vcpu_svm *svm = to_svm(vcpu);
1289
Gleb Natapov89a27f42010-02-16 10:51:48 +02001290 dt->size = svm->vmcb->save.idtr.limit;
1291 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292}
1293
Gleb Natapov89a27f42010-02-16 10:51:48 +02001294static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001296 struct vcpu_svm *svm = to_svm(vcpu);
1297
Gleb Natapov89a27f42010-02-16 10:51:48 +02001298 svm->vmcb->save.idtr.limit = dt->size;
1299 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300}
1301
Gleb Natapov89a27f42010-02-16 10:51:48 +02001302static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001304 struct vcpu_svm *svm = to_svm(vcpu);
1305
Gleb Natapov89a27f42010-02-16 10:51:48 +02001306 dt->size = svm->vmcb->save.gdtr.limit;
1307 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308}
1309
Gleb Natapov89a27f42010-02-16 10:51:48 +02001310static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001312 struct vcpu_svm *svm = to_svm(vcpu);
1313
Gleb Natapov89a27f42010-02-16 10:51:48 +02001314 svm->vmcb->save.gdtr.limit = dt->size;
1315 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316}
1317
Avi Kivitye8467fd2009-12-29 18:43:06 +02001318static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1319{
1320}
1321
Anthony Liguori25c4c272007-04-27 09:29:21 +03001322static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001323{
1324}
1325
Avi Kivityd2251572010-01-06 10:55:27 +02001326static void update_cr0_intercept(struct vcpu_svm *svm)
1327{
1328 ulong gcr0 = svm->vcpu.arch.cr0;
1329 u64 *hcr0 = &svm->vmcb->save.cr0;
1330
1331 if (!svm->vcpu.fpu_active)
1332 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1333 else
1334 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1335 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1336
1337
1338 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001339 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1340 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001341 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001342 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1343 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001344 }
1345}
1346
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1348{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001349 struct vcpu_svm *svm = to_svm(vcpu);
1350
Joerg Roedel20307532010-11-29 17:51:48 +01001351 if (is_guest_mode(vcpu)) {
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001352 /*
1353 * We are here because we run in nested mode, the host kvm
1354 * intercepts cr0 writes but the l1 hypervisor does not.
1355 * But the L1 hypervisor may intercept selective cr0 writes.
1356 * This needs to be checked here.
1357 */
1358 unsigned long old, new;
1359
1360 /* Remove bits that would trigger a real cr0 write intercept */
1361 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1362 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1363
1364 if (old == new) {
1365 /* cr0 write with ts and mp unchanged */
1366 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
Joerg Roedelcda00082010-09-02 17:29:46 +02001367 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
1368 svm->nested.vmexit_rip = kvm_rip_read(vcpu);
1369 svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
1370 svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001371 return;
Joerg Roedelcda00082010-09-02 17:29:46 +02001372 }
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001373 }
1374 }
1375
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001376#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001377 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001378 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001379 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001380 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381 }
1382
Mike Dayd77c26f2007-10-08 09:02:08 -04001383 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001384 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001385 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386 }
1387 }
1388#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001389 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001390
1391 if (!npt_enabled)
1392 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001393
1394 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001395 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001396 /*
1397 * re-enable caching here because the QEMU bios
1398 * does not do it - this results in some delay at
1399 * reboot
1400 */
1401 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001402 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001403 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404}
1405
1406static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1407{
Joerg Roedel6394b642008-04-09 14:15:29 +02001408 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001409 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1410
1411 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1412 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001413
Joerg Roedelec077262008-04-09 14:15:28 +02001414 vcpu->arch.cr4 = cr4;
1415 if (!npt_enabled)
1416 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001417 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001418 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419}
1420
1421static void svm_set_segment(struct kvm_vcpu *vcpu,
1422 struct kvm_segment *var, int seg)
1423{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001424 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425 struct vmcb_seg *s = svm_seg(vcpu, seg);
1426
1427 s->base = var->base;
1428 s->limit = var->limit;
1429 s->selector = var->selector;
1430 if (var->unusable)
1431 s->attrib = 0;
1432 else {
1433 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1434 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1435 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1436 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1437 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1438 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1439 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1440 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1441 }
1442 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001443 svm->vmcb->save.cpl
1444 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1446
1447}
1448
Gleb Natapov44c11432009-05-11 13:35:52 +03001449static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001451 struct vcpu_svm *svm = to_svm(vcpu);
1452
Joerg Roedel18c918c2010-11-30 18:03:59 +01001453 clr_exception_intercept(svm, DB_VECTOR);
1454 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001455
Jan Kiszka6be7d302009-10-18 13:24:54 +02001456 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001457 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001458
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001459 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1460 if (vcpu->guest_debug &
1461 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001462 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001463 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001464 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001465 } else
1466 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001467}
1468
Jan Kiszka355be0b2009-10-03 00:31:21 +02001469static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001470{
Gleb Natapov44c11432009-05-11 13:35:52 +03001471 struct vcpu_svm *svm = to_svm(vcpu);
1472
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001473 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1474 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1475 else
1476 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1477
Jan Kiszka355be0b2009-10-03 00:31:21 +02001478 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479}
1480
Tejun Heo0fe1e002009-10-29 22:34:14 +09001481static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001483 if (sd->next_asid > sd->max_asid) {
1484 ++sd->asid_generation;
1485 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001486 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487 }
1488
Tejun Heo0fe1e002009-10-29 22:34:14 +09001489 svm->asid_generation = sd->asid_generation;
1490 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491}
1492
Gleb Natapov020df072010-04-13 10:05:23 +03001493static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001495 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001496
Gleb Natapov020df072010-04-13 10:05:23 +03001497 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498}
1499
Avi Kivity851ba692009-08-24 11:10:17 +03001500static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501{
Gleb Natapov631bc482010-10-14 11:22:52 +02001502 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001504 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505
Gleb Natapov631bc482010-10-14 11:22:52 +02001506 switch (svm->apf_reason) {
1507 default:
1508 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001509
Gleb Natapov631bc482010-10-14 11:22:52 +02001510 trace_kvm_page_fault(fault_address, error_code);
1511 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1512 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1513 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
1514 break;
1515 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1516 svm->apf_reason = 0;
1517 local_irq_disable();
1518 kvm_async_pf_task_wait(fault_address);
1519 local_irq_enable();
1520 break;
1521 case KVM_PV_REASON_PAGE_READY:
1522 svm->apf_reason = 0;
1523 local_irq_disable();
1524 kvm_async_pf_task_wake(fault_address);
1525 local_irq_enable();
1526 break;
1527 }
1528 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529}
1530
Avi Kivity851ba692009-08-24 11:10:17 +03001531static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001532{
Avi Kivity851ba692009-08-24 11:10:17 +03001533 struct kvm_run *kvm_run = svm->vcpu.run;
1534
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001535 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001536 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001537 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001538 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1539 return 1;
1540 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001541
Jan Kiszka6be7d302009-10-18 13:24:54 +02001542 if (svm->nmi_singlestep) {
1543 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001544 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1545 svm->vmcb->save.rflags &=
1546 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1547 update_db_intercept(&svm->vcpu);
1548 }
1549
1550 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001551 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001552 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1553 kvm_run->debug.arch.pc =
1554 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1555 kvm_run->debug.arch.exception = DB_VECTOR;
1556 return 0;
1557 }
1558
1559 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001560}
1561
Avi Kivity851ba692009-08-24 11:10:17 +03001562static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001563{
Avi Kivity851ba692009-08-24 11:10:17 +03001564 struct kvm_run *kvm_run = svm->vcpu.run;
1565
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001566 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1567 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1568 kvm_run->debug.arch.exception = BP_VECTOR;
1569 return 0;
1570}
1571
Avi Kivity851ba692009-08-24 11:10:17 +03001572static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001573{
1574 int er;
1575
Avi Kivity851ba692009-08-24 11:10:17 +03001576 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001577 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001578 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001579 return 1;
1580}
1581
Avi Kivity6b52d182010-01-21 15:31:47 +02001582static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001583{
Avi Kivity6b52d182010-01-21 15:31:47 +02001584 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001585
Joerg Roedel18c918c2010-11-30 18:03:59 +01001586 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001587
Rusty Russelle756fc62007-07-30 20:07:08 +10001588 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001589 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001590}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001591
Avi Kivity6b52d182010-01-21 15:31:47 +02001592static int nm_interception(struct vcpu_svm *svm)
1593{
1594 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001595 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001596}
1597
Joerg Roedel67ec6602010-05-17 14:43:35 +02001598static bool is_erratum_383(void)
1599{
1600 int err, i;
1601 u64 value;
1602
1603 if (!erratum_383_found)
1604 return false;
1605
1606 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1607 if (err)
1608 return false;
1609
1610 /* Bit 62 may or may not be set for this mce */
1611 value &= ~(1ULL << 62);
1612
1613 if (value != 0xb600000000010015ULL)
1614 return false;
1615
1616 /* Clear MCi_STATUS registers */
1617 for (i = 0; i < 6; ++i)
1618 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1619
1620 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1621 if (!err) {
1622 u32 low, high;
1623
1624 value &= ~(1ULL << 2);
1625 low = lower_32_bits(value);
1626 high = upper_32_bits(value);
1627
1628 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1629 }
1630
1631 /* Flush tlb to evict multi-match entries */
1632 __flush_tlb_all();
1633
1634 return true;
1635}
1636
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001637static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001638{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001639 if (is_erratum_383()) {
1640 /*
1641 * Erratum 383 triggered. Guest state is corrupt so kill the
1642 * guest.
1643 */
1644 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1645
Avi Kivitya8eeb042010-05-10 12:34:53 +03001646 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001647
1648 return;
1649 }
1650
Joerg Roedel53371b52008-04-09 14:15:30 +02001651 /*
1652 * On an #MC intercept the MCE handler is not called automatically in
1653 * the host. So do it by hand here.
1654 */
1655 asm volatile (
1656 "int $0x12\n");
1657 /* not sure if we ever come back to this point */
1658
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001659 return;
1660}
1661
1662static int mc_interception(struct vcpu_svm *svm)
1663{
Joerg Roedel53371b52008-04-09 14:15:30 +02001664 return 1;
1665}
1666
Avi Kivity851ba692009-08-24 11:10:17 +03001667static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001668{
Avi Kivity851ba692009-08-24 11:10:17 +03001669 struct kvm_run *kvm_run = svm->vcpu.run;
1670
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001671 /*
1672 * VMCB is undefined after a SHUTDOWN intercept
1673 * so reinitialize it.
1674 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001675 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001676 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001677
1678 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1679 return 0;
1680}
1681
Avi Kivity851ba692009-08-24 11:10:17 +03001682static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001684 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001685 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001686 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001687 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688
Rusty Russelle756fc62007-07-30 20:07:08 +10001689 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001690 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001691 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001692 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001693 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001694
Avi Kivity039576c2007-03-20 12:46:50 +02001695 port = io_info >> 16;
1696 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001697 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001698 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001699
1700 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701}
1702
Avi Kivity851ba692009-08-24 11:10:17 +03001703static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001704{
1705 return 1;
1706}
1707
Avi Kivity851ba692009-08-24 11:10:17 +03001708static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001709{
1710 ++svm->vcpu.stat.irq_exits;
1711 return 1;
1712}
1713
Avi Kivity851ba692009-08-24 11:10:17 +03001714static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001715{
1716 return 1;
1717}
1718
Avi Kivity851ba692009-08-24 11:10:17 +03001719static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001721 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001722 skip_emulated_instruction(&svm->vcpu);
1723 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724}
1725
Avi Kivity851ba692009-08-24 11:10:17 +03001726static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001727{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001728 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001729 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001730 kvm_emulate_hypercall(&svm->vcpu);
1731 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001732}
1733
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001734static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1735{
1736 struct vcpu_svm *svm = to_svm(vcpu);
1737
1738 return svm->nested.nested_cr3;
1739}
1740
1741static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1742 unsigned long root)
1743{
1744 struct vcpu_svm *svm = to_svm(vcpu);
1745
1746 svm->vmcb->control.nested_cr3 = root;
1747 force_new_asid(vcpu);
1748}
1749
Avi Kivity6389ee92010-11-29 16:12:30 +02001750static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1751 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001752{
1753 struct vcpu_svm *svm = to_svm(vcpu);
1754
1755 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1756 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001757 svm->vmcb->control.exit_info_1 = fault->error_code;
1758 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001759
1760 nested_svm_vmexit(svm);
1761}
1762
Joerg Roedel4b161842010-09-10 17:31:03 +02001763static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1764{
1765 int r;
1766
1767 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1768
1769 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1770 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
1771 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1772 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1773 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1774
1775 return r;
1776}
1777
1778static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1779{
1780 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1781}
1782
Alexander Grafc0725422008-11-25 20:17:03 +01001783static int nested_svm_check_permissions(struct vcpu_svm *svm)
1784{
Avi Kivityf6801df2010-01-21 15:31:50 +02001785 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001786 || !is_paging(&svm->vcpu)) {
1787 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1788 return 1;
1789 }
1790
1791 if (svm->vmcb->save.cpl) {
1792 kvm_inject_gp(&svm->vcpu, 0);
1793 return 1;
1794 }
1795
1796 return 0;
1797}
1798
Alexander Grafcf74a782008-11-25 20:17:08 +01001799static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1800 bool has_error_code, u32 error_code)
1801{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001802 int vmexit;
1803
Joerg Roedel20307532010-11-29 17:51:48 +01001804 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02001805 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001806
Joerg Roedel0295ad72009-08-07 11:49:37 +02001807 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1808 svm->vmcb->control.exit_code_hi = 0;
1809 svm->vmcb->control.exit_info_1 = error_code;
1810 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1811
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001812 vmexit = nested_svm_intercept(svm);
1813 if (vmexit == NESTED_EXIT_DONE)
1814 svm->nested.exit_required = true;
1815
1816 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001817}
1818
Joerg Roedel8fe54652010-02-19 16:23:01 +01001819/* This function returns true if it is save to enable the irq window */
1820static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001821{
Joerg Roedel20307532010-11-29 17:51:48 +01001822 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001823 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001824
Joerg Roedel26666952009-08-07 11:49:46 +02001825 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001826 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001827
Joerg Roedel26666952009-08-07 11:49:46 +02001828 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001829 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001830
Gleb Natapova0a07cd2010-09-20 10:15:32 +02001831 /*
1832 * if vmexit was already requested (by intercepted exception
1833 * for instance) do not overwrite it with "external interrupt"
1834 * vmexit.
1835 */
1836 if (svm->nested.exit_required)
1837 return false;
1838
Joerg Roedel197717d2010-02-24 18:59:19 +01001839 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1840 svm->vmcb->control.exit_info_1 = 0;
1841 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001842
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001843 if (svm->nested.intercept & 1ULL) {
1844 /*
1845 * The #vmexit can't be emulated here directly because this
1846 * code path runs with irqs and preemtion disabled. A
1847 * #vmexit emulation might sleep. Only signal request for
1848 * the #vmexit here.
1849 */
1850 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001851 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001852 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001853 }
1854
Joerg Roedel8fe54652010-02-19 16:23:01 +01001855 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001856}
1857
Joerg Roedel887f5002010-02-24 18:59:12 +01001858/* This function returns true if it is save to enable the nmi window */
1859static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1860{
Joerg Roedel20307532010-11-29 17:51:48 +01001861 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01001862 return true;
1863
1864 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1865 return true;
1866
1867 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1868 svm->nested.exit_required = true;
1869
1870 return false;
1871}
1872
Joerg Roedel7597f122010-02-19 16:23:00 +01001873static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001874{
1875 struct page *page;
1876
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001877 might_sleep();
1878
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001879 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001880 if (is_error_page(page))
1881 goto error;
1882
Joerg Roedel7597f122010-02-19 16:23:00 +01001883 *_page = page;
1884
1885 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001886
1887error:
1888 kvm_release_page_clean(page);
1889 kvm_inject_gp(&svm->vcpu, 0);
1890
1891 return NULL;
1892}
1893
Joerg Roedel7597f122010-02-19 16:23:00 +01001894static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001895{
Joerg Roedel7597f122010-02-19 16:23:00 +01001896 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001897 kvm_release_page_dirty(page);
1898}
1899
Joerg Roedelce2ac082010-03-01 15:34:39 +01001900static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001901{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001902 unsigned port;
1903 u8 val, bit;
1904 u64 gpa;
1905
1906 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1907 return NESTED_EXIT_HOST;
1908
1909 port = svm->vmcb->control.exit_info_1 >> 16;
1910 gpa = svm->nested.vmcb_iopm + (port / 8);
1911 bit = port % 8;
1912 val = 0;
1913
1914 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1915 val &= (1 << bit);
1916
1917 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1918}
1919
Joerg Roedeld2477822010-03-01 15:34:34 +01001920static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001921{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001922 u32 offset, msr, value;
1923 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001924
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001925 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001926 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001927
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001928 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1929 offset = svm_msrpm_offset(msr);
1930 write = svm->vmcb->control.exit_info_1 & 1;
1931 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001932
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001933 if (offset == MSR_INVALID)
1934 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001935
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001936 /* Offset is in 32 bit units but need in 8 bit units */
1937 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001938
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001939 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1940 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001941
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001942 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001943}
1944
Joerg Roedel410e4d52009-08-07 11:49:44 +02001945static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001946{
Alexander Grafcf74a782008-11-25 20:17:08 +01001947 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001948
Joerg Roedel410e4d52009-08-07 11:49:44 +02001949 switch (exit_code) {
1950 case SVM_EXIT_INTR:
1951 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001952 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001953 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001954 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001955 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001956 if (npt_enabled)
1957 return NESTED_EXIT_HOST;
1958 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001959 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02001960 /* When we're shadowing, trap PFs, but not async PF */
1961 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001962 return NESTED_EXIT_HOST;
1963 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001964 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1965 nm_interception(svm);
1966 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001967 default:
1968 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001969 }
1970
Joerg Roedel410e4d52009-08-07 11:49:44 +02001971 return NESTED_EXIT_CONTINUE;
1972}
1973
1974/*
1975 * If this function returns true, this #vmexit was already handled
1976 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001977static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001978{
1979 u32 exit_code = svm->vmcb->control.exit_code;
1980 int vmexit = NESTED_EXIT_HOST;
1981
Alexander Grafcf74a782008-11-25 20:17:08 +01001982 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001983 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001984 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001985 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001986 case SVM_EXIT_IOIO:
1987 vmexit = nested_svm_intercept_ioio(svm);
1988 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001989 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1990 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
1991 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001992 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001993 break;
1994 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01001995 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1996 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
1997 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001998 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001999 break;
2000 }
2001 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2002 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002003 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002004 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002005 /* async page fault always cause vmexit */
2006 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2007 svm->apf_reason != 0)
2008 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002009 break;
2010 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002011 case SVM_EXIT_ERR: {
2012 vmexit = NESTED_EXIT_DONE;
2013 break;
2014 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002015 default: {
2016 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002017 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002018 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002019 }
2020 }
2021
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002022 return vmexit;
2023}
2024
2025static int nested_svm_exit_handled(struct vcpu_svm *svm)
2026{
2027 int vmexit;
2028
2029 vmexit = nested_svm_intercept(svm);
2030
2031 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002032 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002033
2034 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002035}
2036
Joerg Roedel0460a972009-08-07 11:49:31 +02002037static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2038{
2039 struct vmcb_control_area *dst = &dst_vmcb->control;
2040 struct vmcb_control_area *from = &from_vmcb->control;
2041
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002042 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002043 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002044 dst->intercept_exceptions = from->intercept_exceptions;
2045 dst->intercept = from->intercept;
2046 dst->iopm_base_pa = from->iopm_base_pa;
2047 dst->msrpm_base_pa = from->msrpm_base_pa;
2048 dst->tsc_offset = from->tsc_offset;
2049 dst->asid = from->asid;
2050 dst->tlb_ctl = from->tlb_ctl;
2051 dst->int_ctl = from->int_ctl;
2052 dst->int_vector = from->int_vector;
2053 dst->int_state = from->int_state;
2054 dst->exit_code = from->exit_code;
2055 dst->exit_code_hi = from->exit_code_hi;
2056 dst->exit_info_1 = from->exit_info_1;
2057 dst->exit_info_2 = from->exit_info_2;
2058 dst->exit_int_info = from->exit_int_info;
2059 dst->exit_int_info_err = from->exit_int_info_err;
2060 dst->nested_ctl = from->nested_ctl;
2061 dst->event_inj = from->event_inj;
2062 dst->event_inj_err = from->event_inj_err;
2063 dst->nested_cr3 = from->nested_cr3;
2064 dst->lbr_ctl = from->lbr_ctl;
2065}
2066
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002067static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002068{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002069 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002070 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002071 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002072 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002073
Joerg Roedel17897f32009-10-09 16:08:29 +02002074 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2075 vmcb->control.exit_info_1,
2076 vmcb->control.exit_info_2,
2077 vmcb->control.exit_int_info,
2078 vmcb->control.exit_int_info_err);
2079
Joerg Roedel7597f122010-02-19 16:23:00 +01002080 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002081 if (!nested_vmcb)
2082 return 1;
2083
Joerg Roedel20307532010-11-29 17:51:48 +01002084 /* Exit Guest-Mode */
2085 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002086 svm->nested.vmcb = 0;
2087
Alexander Grafcf74a782008-11-25 20:17:08 +01002088 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002089 disable_gif(svm);
2090
2091 nested_vmcb->save.es = vmcb->save.es;
2092 nested_vmcb->save.cs = vmcb->save.cs;
2093 nested_vmcb->save.ss = vmcb->save.ss;
2094 nested_vmcb->save.ds = vmcb->save.ds;
2095 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2096 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002097 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002098 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002099 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02002100 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002101 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02002102 nested_vmcb->save.rflags = vmcb->save.rflags;
2103 nested_vmcb->save.rip = vmcb->save.rip;
2104 nested_vmcb->save.rsp = vmcb->save.rsp;
2105 nested_vmcb->save.rax = vmcb->save.rax;
2106 nested_vmcb->save.dr7 = vmcb->save.dr7;
2107 nested_vmcb->save.dr6 = vmcb->save.dr6;
2108 nested_vmcb->save.cpl = vmcb->save.cpl;
2109
2110 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2111 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2112 nested_vmcb->control.int_state = vmcb->control.int_state;
2113 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2114 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2115 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2116 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2117 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2118 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002119 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002120
2121 /*
2122 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2123 * to make sure that we do not lose injected events. So check event_inj
2124 * here and copy it to exit_int_info if it is valid.
2125 * Exit_int_info and event_inj can't be both valid because the case
2126 * below only happens on a VMRUN instruction intercept which has
2127 * no valid exit_int_info set.
2128 */
2129 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2130 struct vmcb_control_area *nc = &nested_vmcb->control;
2131
2132 nc->exit_int_info = vmcb->control.event_inj;
2133 nc->exit_int_info_err = vmcb->control.event_inj_err;
2134 }
2135
Joerg Roedel33740e42009-08-07 11:49:29 +02002136 nested_vmcb->control.tlb_ctl = 0;
2137 nested_vmcb->control.event_inj = 0;
2138 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002139
2140 /* We always set V_INTR_MASKING and remember the old value in hflags */
2141 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2142 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2143
Alexander Grafcf74a782008-11-25 20:17:08 +01002144 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002145 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002146
Alexander Graf219b65d2009-06-15 15:21:25 +02002147 kvm_clear_exception_queue(&svm->vcpu);
2148 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002149
Joerg Roedel4b161842010-09-10 17:31:03 +02002150 svm->nested.nested_cr3 = 0;
2151
Alexander Grafcf74a782008-11-25 20:17:08 +01002152 /* Restore selected save entries */
2153 svm->vmcb->save.es = hsave->save.es;
2154 svm->vmcb->save.cs = hsave->save.cs;
2155 svm->vmcb->save.ss = hsave->save.ss;
2156 svm->vmcb->save.ds = hsave->save.ds;
2157 svm->vmcb->save.gdtr = hsave->save.gdtr;
2158 svm->vmcb->save.idtr = hsave->save.idtr;
2159 svm->vmcb->save.rflags = hsave->save.rflags;
2160 svm_set_efer(&svm->vcpu, hsave->save.efer);
2161 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2162 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2163 if (npt_enabled) {
2164 svm->vmcb->save.cr3 = hsave->save.cr3;
2165 svm->vcpu.arch.cr3 = hsave->save.cr3;
2166 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002167 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002168 }
2169 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2170 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2171 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2172 svm->vmcb->save.dr7 = 0;
2173 svm->vmcb->save.cpl = 0;
2174 svm->vmcb->control.exit_int_info = 0;
2175
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002176 mark_all_dirty(svm->vmcb);
2177
Joerg Roedel7597f122010-02-19 16:23:00 +01002178 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002179
Joerg Roedel4b161842010-09-10 17:31:03 +02002180 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002181 kvm_mmu_reset_context(&svm->vcpu);
2182 kvm_mmu_load(&svm->vcpu);
2183
2184 return 0;
2185}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002186
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002187static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002188{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002189 /*
2190 * This function merges the msr permission bitmaps of kvm and the
2191 * nested vmcb. It is omptimized in that it only merges the parts where
2192 * the kvm msr permission bitmap may contain zero bits
2193 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002194 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002195
Joerg Roedel323c3d82010-03-01 15:34:37 +01002196 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2197 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002198
Joerg Roedel323c3d82010-03-01 15:34:37 +01002199 for (i = 0; i < MSRPM_OFFSETS; i++) {
2200 u32 value, p;
2201 u64 offset;
2202
2203 if (msrpm_offsets[i] == 0xffffffff)
2204 break;
2205
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002206 p = msrpm_offsets[i];
2207 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002208
2209 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2210 return false;
2211
2212 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2213 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002214
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002215 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002216
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002217 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002218}
2219
Joerg Roedel52c65a302010-08-02 16:46:44 +02002220static bool nested_vmcb_checks(struct vmcb *vmcb)
2221{
2222 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2223 return false;
2224
Joerg Roedeldbe77582010-08-02 16:46:45 +02002225 if (vmcb->control.asid == 0)
2226 return false;
2227
Joerg Roedel4b161842010-09-10 17:31:03 +02002228 if (vmcb->control.nested_ctl && !npt_enabled)
2229 return false;
2230
Joerg Roedel52c65a302010-08-02 16:46:44 +02002231 return true;
2232}
2233
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002234static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002235{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002236 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002237 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002238 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002239 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002240 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002241
Joerg Roedel06fc77722010-02-19 16:23:07 +01002242 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002243
Joerg Roedel7597f122010-02-19 16:23:00 +01002244 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002245 if (!nested_vmcb)
2246 return false;
2247
Joerg Roedel52c65a302010-08-02 16:46:44 +02002248 if (!nested_vmcb_checks(nested_vmcb)) {
2249 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2250 nested_vmcb->control.exit_code_hi = 0;
2251 nested_vmcb->control.exit_info_1 = 0;
2252 nested_vmcb->control.exit_info_2 = 0;
2253
2254 nested_svm_unmap(page);
2255
2256 return false;
2257 }
2258
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002259 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002260 nested_vmcb->save.rip,
2261 nested_vmcb->control.int_ctl,
2262 nested_vmcb->control.event_inj,
2263 nested_vmcb->control.nested_ctl);
2264
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002265 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2266 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002267 nested_vmcb->control.intercept_exceptions,
2268 nested_vmcb->control.intercept);
2269
Alexander Graf3d6368e2008-11-25 20:17:07 +01002270 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002271 kvm_clear_exception_queue(&svm->vcpu);
2272 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002273
Joerg Roedele0231712010-02-24 18:59:10 +01002274 /*
2275 * Save the old vmcb, so we don't need to pick what we save, but can
2276 * restore everything when a VMEXIT occurs
2277 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002278 hsave->save.es = vmcb->save.es;
2279 hsave->save.cs = vmcb->save.cs;
2280 hsave->save.ss = vmcb->save.ss;
2281 hsave->save.ds = vmcb->save.ds;
2282 hsave->save.gdtr = vmcb->save.gdtr;
2283 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002284 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002285 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002286 hsave->save.cr4 = svm->vcpu.arch.cr4;
2287 hsave->save.rflags = vmcb->save.rflags;
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002288 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002289 hsave->save.rsp = vmcb->save.rsp;
2290 hsave->save.rax = vmcb->save.rax;
2291 if (npt_enabled)
2292 hsave->save.cr3 = vmcb->save.cr3;
2293 else
2294 hsave->save.cr3 = svm->vcpu.arch.cr3;
2295
Joerg Roedel0460a972009-08-07 11:49:31 +02002296 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002297
2298 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2299 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2300 else
2301 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2302
Joerg Roedel4b161842010-09-10 17:31:03 +02002303 if (nested_vmcb->control.nested_ctl) {
2304 kvm_mmu_unload(&svm->vcpu);
2305 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2306 nested_svm_init_mmu_context(&svm->vcpu);
2307 }
2308
Alexander Graf3d6368e2008-11-25 20:17:07 +01002309 /* Load the nested guest state */
2310 svm->vmcb->save.es = nested_vmcb->save.es;
2311 svm->vmcb->save.cs = nested_vmcb->save.cs;
2312 svm->vmcb->save.ss = nested_vmcb->save.ss;
2313 svm->vmcb->save.ds = nested_vmcb->save.ds;
2314 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2315 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2316 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2317 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2318 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2319 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2320 if (npt_enabled) {
2321 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2322 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002323 } else
Avi Kivity23902182010-06-10 17:02:16 +03002324 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002325
2326 /* Guest paging mode is active - reset mmu */
2327 kvm_mmu_reset_context(&svm->vcpu);
2328
Joerg Roedeldefbba52009-08-07 11:49:30 +02002329 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002330 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2331 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2332 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002333
Alexander Graf3d6368e2008-11-25 20:17:07 +01002334 /* In case we don't even reach vcpu_run, the fields are not updated */
2335 svm->vmcb->save.rax = nested_vmcb->save.rax;
2336 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2337 svm->vmcb->save.rip = nested_vmcb->save.rip;
2338 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2339 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2340 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2341
Joerg Roedelf7138532010-03-01 15:34:40 +01002342 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002343 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002344
Joerg Roedelaad42c62009-08-07 11:49:34 +02002345 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002346 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002347 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002348 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2349 svm->nested.intercept = nested_vmcb->control.intercept;
2350
Alexander Graf3d6368e2008-11-25 20:17:07 +01002351 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002352 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002353 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2354 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2355 else
2356 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2357
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002358 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2359 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002360 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2361 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002362 }
2363
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002364 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002365 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002366
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002367 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002368 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2369 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2370 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002371 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2372 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2373
Joerg Roedel7597f122010-02-19 16:23:00 +01002374 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002375
Joerg Roedel20307532010-11-29 17:51:48 +01002376 /* Enter Guest-Mode */
2377 enter_guest_mode(&svm->vcpu);
2378
Joerg Roedel384c6362010-11-30 18:03:56 +01002379 /*
2380 * Merge guest and host intercepts - must be called with vcpu in
2381 * guest-mode to take affect here
2382 */
2383 recalc_intercepts(svm);
2384
Joerg Roedel06fc77722010-02-19 16:23:07 +01002385 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002386
Joerg Roedel2af91942009-08-07 11:49:28 +02002387 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002388
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002389 mark_all_dirty(svm->vmcb);
2390
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002391 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002392}
2393
Joerg Roedel9966bf62009-08-07 11:49:40 +02002394static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002395{
2396 to_vmcb->save.fs = from_vmcb->save.fs;
2397 to_vmcb->save.gs = from_vmcb->save.gs;
2398 to_vmcb->save.tr = from_vmcb->save.tr;
2399 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2400 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2401 to_vmcb->save.star = from_vmcb->save.star;
2402 to_vmcb->save.lstar = from_vmcb->save.lstar;
2403 to_vmcb->save.cstar = from_vmcb->save.cstar;
2404 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2405 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2406 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2407 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002408}
2409
Avi Kivity851ba692009-08-24 11:10:17 +03002410static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002411{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002412 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002413 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002414
Alexander Graf55426752008-11-25 20:17:06 +01002415 if (nested_svm_check_permissions(svm))
2416 return 1;
2417
2418 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2419 skip_emulated_instruction(&svm->vcpu);
2420
Joerg Roedel7597f122010-02-19 16:23:00 +01002421 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002422 if (!nested_vmcb)
2423 return 1;
2424
2425 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002426 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002427
2428 return 1;
2429}
2430
Avi Kivity851ba692009-08-24 11:10:17 +03002431static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002432{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002433 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002434 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002435
Alexander Graf55426752008-11-25 20:17:06 +01002436 if (nested_svm_check_permissions(svm))
2437 return 1;
2438
2439 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2440 skip_emulated_instruction(&svm->vcpu);
2441
Joerg Roedel7597f122010-02-19 16:23:00 +01002442 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002443 if (!nested_vmcb)
2444 return 1;
2445
2446 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002447 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002448
2449 return 1;
2450}
2451
Avi Kivity851ba692009-08-24 11:10:17 +03002452static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002453{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002454 if (nested_svm_check_permissions(svm))
2455 return 1;
2456
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002457 /* Save rip after vmrun instruction */
2458 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002459
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002460 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002461 return 1;
2462
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002463 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002464 goto failed;
2465
2466 return 1;
2467
2468failed:
2469
2470 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2471 svm->vmcb->control.exit_code_hi = 0;
2472 svm->vmcb->control.exit_info_1 = 0;
2473 svm->vmcb->control.exit_info_2 = 0;
2474
2475 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002476
2477 return 1;
2478}
2479
Avi Kivity851ba692009-08-24 11:10:17 +03002480static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002481{
2482 if (nested_svm_check_permissions(svm))
2483 return 1;
2484
2485 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2486 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002487 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002488
Joerg Roedel2af91942009-08-07 11:49:28 +02002489 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002490
2491 return 1;
2492}
2493
Avi Kivity851ba692009-08-24 11:10:17 +03002494static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002495{
2496 if (nested_svm_check_permissions(svm))
2497 return 1;
2498
2499 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2500 skip_emulated_instruction(&svm->vcpu);
2501
Joerg Roedel2af91942009-08-07 11:49:28 +02002502 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002503
2504 /* After a CLGI no interrupts should come */
2505 svm_clear_vintr(svm);
2506 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2507
2508 return 1;
2509}
2510
Avi Kivity851ba692009-08-24 11:10:17 +03002511static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002512{
2513 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002514
Joerg Roedelec1ff792009-10-09 16:08:31 +02002515 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2516 vcpu->arch.regs[VCPU_REGS_RAX]);
2517
Alexander Grafff092382009-06-15 15:21:24 +02002518 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2519 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2520
2521 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2522 skip_emulated_instruction(&svm->vcpu);
2523 return 1;
2524}
2525
Joerg Roedel532a46b2009-10-09 16:08:32 +02002526static int skinit_interception(struct vcpu_svm *svm)
2527{
2528 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2529
2530 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2531 return 1;
2532}
2533
Avi Kivity851ba692009-08-24 11:10:17 +03002534static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002536 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537 return 1;
2538}
2539
Avi Kivity851ba692009-08-24 11:10:17 +03002540static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541{
Izik Eidus37817f22008-03-24 23:14:53 +02002542 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002543 int reason;
2544 int int_type = svm->vmcb->control.exit_int_info &
2545 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002546 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002547 uint32_t type =
2548 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2549 uint32_t idt_v =
2550 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002551 bool has_error_code = false;
2552 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002553
2554 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002555
Izik Eidus37817f22008-03-24 23:14:53 +02002556 if (svm->vmcb->control.exit_info_2 &
2557 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002558 reason = TASK_SWITCH_IRET;
2559 else if (svm->vmcb->control.exit_info_2 &
2560 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2561 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002562 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002563 reason = TASK_SWITCH_GATE;
2564 else
2565 reason = TASK_SWITCH_CALL;
2566
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002567 if (reason == TASK_SWITCH_GATE) {
2568 switch (type) {
2569 case SVM_EXITINTINFO_TYPE_NMI:
2570 svm->vcpu.arch.nmi_injected = false;
2571 break;
2572 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002573 if (svm->vmcb->control.exit_info_2 &
2574 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2575 has_error_code = true;
2576 error_code =
2577 (u32)svm->vmcb->control.exit_info_2;
2578 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002579 kvm_clear_exception_queue(&svm->vcpu);
2580 break;
2581 case SVM_EXITINTINFO_TYPE_INTR:
2582 kvm_clear_interrupt_queue(&svm->vcpu);
2583 break;
2584 default:
2585 break;
2586 }
2587 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002588
Gleb Natapov8317c292009-04-12 13:37:02 +03002589 if (reason != TASK_SWITCH_GATE ||
2590 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2591 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002592 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2593 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002594
Gleb Natapovacb54512010-04-15 21:03:50 +03002595 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2596 has_error_code, error_code) == EMULATE_FAIL) {
2597 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2598 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2599 svm->vcpu.run->internal.ndata = 0;
2600 return 0;
2601 }
2602 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002603}
2604
Avi Kivity851ba692009-08-24 11:10:17 +03002605static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002607 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002608 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002609 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610}
2611
Avi Kivity851ba692009-08-24 11:10:17 +03002612static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002613{
2614 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002615 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002616 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002617 return 1;
2618}
2619
Avi Kivity851ba692009-08-24 11:10:17 +03002620static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002621{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002622 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002623}
2624
Avi Kivity851ba692009-08-24 11:10:17 +03002625static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002627 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628}
2629
Joerg Roedelcda00082010-09-02 17:29:46 +02002630static int cr0_write_interception(struct vcpu_svm *svm)
2631{
2632 struct kvm_vcpu *vcpu = &svm->vcpu;
2633 int r;
2634
2635 r = emulate_instruction(&svm->vcpu, 0, 0, 0);
2636
2637 if (svm->nested.vmexit_rip) {
2638 kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
2639 kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
2640 kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
2641 svm->nested.vmexit_rip = 0;
2642 }
2643
2644 return r == EMULATE_DONE;
2645}
2646
Avi Kivity851ba692009-08-24 11:10:17 +03002647static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002648{
Avi Kivity851ba692009-08-24 11:10:17 +03002649 struct kvm_run *kvm_run = svm->vcpu.run;
2650
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002651 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2652 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002653 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002654 if (irqchip_in_kernel(svm->vcpu.kvm)) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002655 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel1d075432007-12-06 21:02:25 +01002656 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002657 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002658 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2659 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002660 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2661 return 0;
2662}
2663
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2665{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002666 struct vcpu_svm *svm = to_svm(vcpu);
2667
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302669 case MSR_IA32_TSC: {
Joerg Roedel4cc70312010-11-30 18:04:01 +01002670 struct vmcb *vmcb = get_host_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671
Joerg Roedel4cc70312010-11-30 18:04:01 +01002672 *data = vmcb->control.tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673 break;
2674 }
Brian Gerst8c065852010-07-17 09:03:26 -04002675 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002676 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002678#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002680 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681 break;
2682 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002683 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684 break;
2685 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002686 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687 break;
2688 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002689 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690 break;
2691#endif
2692 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002693 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694 break;
2695 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002696 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002697 break;
2698 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002699 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002701 /*
2702 * Nobody will change the following 5 values in the VMCB so we can
2703 * safely return them on rdmsr. They will always be 0 until LBRV is
2704 * implemented.
2705 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002706 case MSR_IA32_DEBUGCTLMSR:
2707 *data = svm->vmcb->save.dbgctl;
2708 break;
2709 case MSR_IA32_LASTBRANCHFROMIP:
2710 *data = svm->vmcb->save.br_from;
2711 break;
2712 case MSR_IA32_LASTBRANCHTOIP:
2713 *data = svm->vmcb->save.br_to;
2714 break;
2715 case MSR_IA32_LASTINTFROMIP:
2716 *data = svm->vmcb->save.last_excp_from;
2717 break;
2718 case MSR_IA32_LASTINTTOIP:
2719 *data = svm->vmcb->save.last_excp_to;
2720 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002721 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002722 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002723 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002724 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002725 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002726 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002727 case MSR_IA32_UCODE_REV:
2728 *data = 0x01000065;
2729 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002731 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 }
2733 return 0;
2734}
2735
Avi Kivity851ba692009-08-24 11:10:17 +03002736static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002738 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 u64 data;
2740
Avi Kivity59200272010-01-25 19:47:02 +02002741 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2742 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002743 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002744 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002745 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002746
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002747 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002748 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002749 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002750 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 }
2752 return 1;
2753}
2754
Joerg Roedel4a810182010-02-24 18:59:15 +01002755static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2756{
2757 struct vcpu_svm *svm = to_svm(vcpu);
2758 int svm_dis, chg_mask;
2759
2760 if (data & ~SVM_VM_CR_VALID_MASK)
2761 return 1;
2762
2763 chg_mask = SVM_VM_CR_VALID_MASK;
2764
2765 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2766 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2767
2768 svm->nested.vm_cr_msr &= ~chg_mask;
2769 svm->nested.vm_cr_msr |= (data & chg_mask);
2770
2771 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2772
2773 /* check for svm_disable while efer.svme is set */
2774 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2775 return 1;
2776
2777 return 0;
2778}
2779
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2781{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002782 struct vcpu_svm *svm = to_svm(vcpu);
2783
Avi Kivity6aa8b732006-12-10 02:21:36 -08002784 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002785 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10002786 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002787 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002788 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002789 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002791#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002793 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794 break;
2795 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002796 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 break;
2798 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002799 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002800 break;
2801 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002802 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002803 break;
2804#endif
2805 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002806 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807 break;
2808 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002809 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002810 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 break;
2812 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002813 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002814 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002815 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002816 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02002817 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002818 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002819 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002820 break;
2821 }
2822 if (data & DEBUGCTL_RESERVED_BITS)
2823 return 1;
2824
2825 svm->vmcb->save.dbgctl = data;
2826 if (data & (1ULL<<0))
2827 svm_enable_lbrv(svm);
2828 else
2829 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002830 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002831 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002832 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002833 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002834 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002835 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002836 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002837 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2838 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002840 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841 }
2842 return 0;
2843}
2844
Avi Kivity851ba692009-08-24 11:10:17 +03002845static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002847 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002848 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002849 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002850
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002851
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002852 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002853 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2854 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002855 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002856 } else {
2857 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002858 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002859 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 return 1;
2861}
2862
Avi Kivity851ba692009-08-24 11:10:17 +03002863static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864{
Rusty Russelle756fc62007-07-30 20:07:08 +10002865 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002866 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002867 else
Avi Kivity851ba692009-08-24 11:10:17 +03002868 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869}
2870
Avi Kivity851ba692009-08-24 11:10:17 +03002871static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002872{
Avi Kivity851ba692009-08-24 11:10:17 +03002873 struct kvm_run *kvm_run = svm->vcpu.run;
2874
Avi Kivity3842d132010-07-27 12:30:24 +03002875 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01002876 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002877 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002878 /*
2879 * If the user space waits to inject interrupts, exit as soon as
2880 * possible
2881 */
Gleb Natapov80618232009-04-21 17:44:56 +03002882 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2883 kvm_run->request_interrupt_window &&
2884 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002885 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002886 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2887 return 0;
2888 }
2889
2890 return 1;
2891}
2892
Mark Langsdorf565d0992009-10-06 14:25:02 -05002893static int pause_interception(struct vcpu_svm *svm)
2894{
2895 kvm_vcpu_on_spin(&(svm->vcpu));
2896 return 1;
2897}
2898
Avi Kivity851ba692009-08-24 11:10:17 +03002899static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002900 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2901 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2902 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2903 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002904 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedelcda00082010-09-02 17:29:46 +02002905 [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002906 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2907 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2908 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2909 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2911 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2912 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002913 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2914 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2915 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2916 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2918 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2919 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2920 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002921 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002923 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002925 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2926 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002927 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002928 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2929 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2930 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2931 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002932 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933 [SVM_EXIT_SMI] = nop_on_interception,
2934 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002935 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002936 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002937 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002938 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002939 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002940 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002941 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002942 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002943 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944 [SVM_EXIT_MSR] = msr_interception,
2945 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002946 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002947 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002948 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002949 [SVM_EXIT_VMLOAD] = vmload_interception,
2950 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002951 [SVM_EXIT_STGI] = stgi_interception,
2952 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002953 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002954 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002955 [SVM_EXIT_MONITOR] = invalid_op_interception,
2956 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002957 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958};
2959
Joerg Roedel3f10c842010-05-05 16:04:42 +02002960void dump_vmcb(struct kvm_vcpu *vcpu)
2961{
2962 struct vcpu_svm *svm = to_svm(vcpu);
2963 struct vmcb_control_area *control = &svm->vmcb->control;
2964 struct vmcb_save_area *save = &svm->vmcb->save;
2965
2966 pr_err("VMCB Control Area:\n");
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002967 pr_err("cr_read: %04x\n", control->intercept_cr & 0xffff);
2968 pr_err("cr_write: %04x\n", control->intercept_cr >> 16);
Joerg Roedel3aed0412010-11-30 18:03:58 +01002969 pr_err("dr_read: %04x\n", control->intercept_dr & 0xffff);
2970 pr_err("dr_write: %04x\n", control->intercept_dr >> 16);
Joerg Roedel3f10c842010-05-05 16:04:42 +02002971 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2972 pr_err("intercepts: %016llx\n", control->intercept);
2973 pr_err("pause filter count: %d\n", control->pause_filter_count);
2974 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2975 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2976 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2977 pr_err("asid: %d\n", control->asid);
2978 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2979 pr_err("int_ctl: %08x\n", control->int_ctl);
2980 pr_err("int_vector: %08x\n", control->int_vector);
2981 pr_err("int_state: %08x\n", control->int_state);
2982 pr_err("exit_code: %08x\n", control->exit_code);
2983 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2984 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2985 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2986 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2987 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2988 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2989 pr_err("event_inj: %08x\n", control->event_inj);
2990 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2991 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
2992 pr_err("next_rip: %016llx\n", control->next_rip);
2993 pr_err("VMCB State Save Area:\n");
2994 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2995 save->es.selector, save->es.attrib,
2996 save->es.limit, save->es.base);
2997 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2998 save->cs.selector, save->cs.attrib,
2999 save->cs.limit, save->cs.base);
3000 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
3001 save->ss.selector, save->ss.attrib,
3002 save->ss.limit, save->ss.base);
3003 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
3004 save->ds.selector, save->ds.attrib,
3005 save->ds.limit, save->ds.base);
3006 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
3007 save->fs.selector, save->fs.attrib,
3008 save->fs.limit, save->fs.base);
3009 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
3010 save->gs.selector, save->gs.attrib,
3011 save->gs.limit, save->gs.base);
3012 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
3013 save->gdtr.selector, save->gdtr.attrib,
3014 save->gdtr.limit, save->gdtr.base);
3015 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
3016 save->ldtr.selector, save->ldtr.attrib,
3017 save->ldtr.limit, save->ldtr.base);
3018 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
3019 save->idtr.selector, save->idtr.attrib,
3020 save->idtr.limit, save->idtr.base);
3021 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
3022 save->tr.selector, save->tr.attrib,
3023 save->tr.limit, save->tr.base);
3024 pr_err("cpl: %d efer: %016llx\n",
3025 save->cpl, save->efer);
3026 pr_err("cr0: %016llx cr2: %016llx\n",
3027 save->cr0, save->cr2);
3028 pr_err("cr3: %016llx cr4: %016llx\n",
3029 save->cr3, save->cr4);
3030 pr_err("dr6: %016llx dr7: %016llx\n",
3031 save->dr6, save->dr7);
3032 pr_err("rip: %016llx rflags: %016llx\n",
3033 save->rip, save->rflags);
3034 pr_err("rsp: %016llx rax: %016llx\n",
3035 save->rsp, save->rax);
3036 pr_err("star: %016llx lstar: %016llx\n",
3037 save->star, save->lstar);
3038 pr_err("cstar: %016llx sfmask: %016llx\n",
3039 save->cstar, save->sfmask);
3040 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
3041 save->kernel_gs_base, save->sysenter_cs);
3042 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
3043 save->sysenter_esp, save->sysenter_eip);
3044 pr_err("gpat: %016llx dbgctl: %016llx\n",
3045 save->g_pat, save->dbgctl);
3046 pr_err("br_from: %016llx br_to: %016llx\n",
3047 save->br_from, save->br_to);
3048 pr_err("excp_from: %016llx excp_to: %016llx\n",
3049 save->last_excp_from, save->last_excp_to);
3050
3051}
3052
Avi Kivity586f9602010-11-18 13:09:54 +02003053static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3054{
3055 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3056
3057 *info1 = control->exit_info_1;
3058 *info2 = control->exit_info_2;
3059}
3060
Avi Kivity851ba692009-08-24 11:10:17 +03003061static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003063 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003064 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003065 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066
Avi Kivityaa179112010-11-17 18:44:19 +02003067 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003068
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003069 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003070 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3071 if (npt_enabled)
3072 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003073
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003074 if (unlikely(svm->nested.exit_required)) {
3075 nested_svm_vmexit(svm);
3076 svm->nested.exit_required = false;
3077
3078 return 1;
3079 }
3080
Joerg Roedel20307532010-11-29 17:51:48 +01003081 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003082 int vmexit;
3083
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003084 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3085 svm->vmcb->control.exit_info_1,
3086 svm->vmcb->control.exit_info_2,
3087 svm->vmcb->control.exit_int_info,
3088 svm->vmcb->control.exit_int_info_err);
3089
Joerg Roedel410e4d52009-08-07 11:49:44 +02003090 vmexit = nested_svm_exit_special(svm);
3091
3092 if (vmexit == NESTED_EXIT_CONTINUE)
3093 vmexit = nested_svm_exit_handled(svm);
3094
3095 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003096 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003097 }
3098
Joerg Roedela5c38322009-08-07 11:49:32 +02003099 svm_complete_interrupts(svm);
3100
Avi Kivity04d2cc72007-09-10 18:10:54 +03003101 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3102 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3103 kvm_run->fail_entry.hardware_entry_failure_reason
3104 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003105 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3106 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003107 return 0;
3108 }
3109
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003110 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003111 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003112 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3113 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3115 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003116 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003117 exit_code);
3118
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003119 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003120 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003122 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003123 return 0;
3124 }
3125
Avi Kivity851ba692009-08-24 11:10:17 +03003126 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127}
3128
3129static void reload_tss(struct kvm_vcpu *vcpu)
3130{
3131 int cpu = raw_smp_processor_id();
3132
Tejun Heo0fe1e002009-10-29 22:34:14 +09003133 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3134 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135 load_TR_desc();
3136}
3137
Rusty Russelle756fc62007-07-30 20:07:08 +10003138static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003139{
3140 int cpu = raw_smp_processor_id();
3141
Tejun Heo0fe1e002009-10-29 22:34:14 +09003142 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003144 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003145 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003146 if (svm->asid_generation != sd->asid_generation)
3147 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148}
3149
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003150static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3151{
3152 struct vcpu_svm *svm = to_svm(vcpu);
3153
3154 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3155 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003156 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003157 ++vcpu->stat.nmi_injections;
3158}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159
Eddie Dong85f455f2007-07-06 12:20:49 +03003160static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161{
3162 struct vmcb_control_area *control;
3163
Rusty Russelle756fc62007-07-30 20:07:08 +10003164 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003165 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166 control->int_ctl &= ~V_INTR_PRIO_MASK;
3167 control->int_ctl |= V_IRQ_MASK |
3168 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
3169}
3170
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003171static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003172{
3173 struct vcpu_svm *svm = to_svm(vcpu);
3174
Joerg Roedel2af91942009-08-07 11:49:28 +02003175 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003176
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003177 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3178 ++vcpu->stat.irq_injections;
3179
Alexander Graf219b65d2009-06-15 15:21:25 +02003180 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3181 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003182}
3183
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003184static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3185{
3186 struct vcpu_svm *svm = to_svm(vcpu);
3187
Joerg Roedel20307532010-11-29 17:51:48 +01003188 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003189 return;
3190
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003191 if (irr == -1)
3192 return;
3193
3194 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003195 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003196}
3197
3198static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003199{
3200 struct vcpu_svm *svm = to_svm(vcpu);
3201 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003202 int ret;
3203 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3204 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3205 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3206
3207 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003208}
3209
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003210static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3211{
3212 struct vcpu_svm *svm = to_svm(vcpu);
3213
3214 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3215}
3216
3217static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3218{
3219 struct vcpu_svm *svm = to_svm(vcpu);
3220
3221 if (masked) {
3222 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003223 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003224 } else {
3225 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003226 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003227 }
3228}
3229
Gleb Natapov78646122009-03-23 12:12:11 +02003230static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3231{
3232 struct vcpu_svm *svm = to_svm(vcpu);
3233 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003234 int ret;
3235
3236 if (!gif_set(svm) ||
3237 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3238 return 0;
3239
3240 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3241
Joerg Roedel20307532010-11-29 17:51:48 +01003242 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003243 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3244
3245 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003246}
3247
Gleb Natapov9222be12009-04-23 17:14:37 +03003248static void enable_irq_window(struct kvm_vcpu *vcpu)
3249{
Alexander Graf219b65d2009-06-15 15:21:25 +02003250 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003251
Joerg Roedele0231712010-02-24 18:59:10 +01003252 /*
3253 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3254 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3255 * get that intercept, this function will be called again though and
3256 * we'll get the vintr intercept.
3257 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003258 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003259 svm_set_vintr(svm);
3260 svm_inject_irq(svm, 0x0);
3261 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003262}
3263
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003264static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003266 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003267
Gleb Natapov44c11432009-05-11 13:35:52 +03003268 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3269 == HF_NMI_MASK)
3270 return; /* IRET will cause a vm exit */
3271
Joerg Roedele0231712010-02-24 18:59:10 +01003272 /*
3273 * Something prevents NMI from been injected. Single step over possible
3274 * problem (IRET or exception injection or interrupt shadow)
3275 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003276 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003277 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3278 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003279}
3280
Izik Eiduscbc94022007-10-25 00:29:55 +02003281static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3282{
3283 return 0;
3284}
3285
Avi Kivityd9e368d2007-06-07 19:18:30 +03003286static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3287{
3288 force_new_asid(vcpu);
3289}
3290
Avi Kivity04d2cc72007-09-10 18:10:54 +03003291static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3292{
3293}
3294
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003295static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3296{
3297 struct vcpu_svm *svm = to_svm(vcpu);
3298
Joerg Roedel20307532010-11-29 17:51:48 +01003299 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003300 return;
3301
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003302 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003303 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003304 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003305 }
3306}
3307
Joerg Roedel649d6862008-04-16 16:51:15 +02003308static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3309{
3310 struct vcpu_svm *svm = to_svm(vcpu);
3311 u64 cr8;
3312
Joerg Roedel20307532010-11-29 17:51:48 +01003313 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003314 return;
3315
Joerg Roedel649d6862008-04-16 16:51:15 +02003316 cr8 = kvm_get_cr8(vcpu);
3317 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3318 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3319}
3320
Gleb Natapov9222be12009-04-23 17:14:37 +03003321static void svm_complete_interrupts(struct vcpu_svm *svm)
3322{
3323 u8 vector;
3324 int type;
3325 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003326 unsigned int3_injected = svm->int3_injected;
3327
3328 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003329
Avi Kivity3842d132010-07-27 12:30:24 +03003330 if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003331 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003332 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3333 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003334
Gleb Natapov9222be12009-04-23 17:14:37 +03003335 svm->vcpu.arch.nmi_injected = false;
3336 kvm_clear_exception_queue(&svm->vcpu);
3337 kvm_clear_interrupt_queue(&svm->vcpu);
3338
3339 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3340 return;
3341
Avi Kivity3842d132010-07-27 12:30:24 +03003342 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3343
Gleb Natapov9222be12009-04-23 17:14:37 +03003344 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3345 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3346
3347 switch (type) {
3348 case SVM_EXITINTINFO_TYPE_NMI:
3349 svm->vcpu.arch.nmi_injected = true;
3350 break;
3351 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003352 /*
3353 * In case of software exceptions, do not reinject the vector,
3354 * but re-execute the instruction instead. Rewind RIP first
3355 * if we emulated INT3 before.
3356 */
3357 if (kvm_exception_is_soft(vector)) {
3358 if (vector == BP_VECTOR && int3_injected &&
3359 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3360 kvm_rip_write(&svm->vcpu,
3361 kvm_rip_read(&svm->vcpu) -
3362 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003363 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003364 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003365 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3366 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003367 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003368
3369 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003370 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003371 break;
3372 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003373 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003374 break;
3375 default:
3376 break;
3377 }
3378}
3379
Avi Kivityb463a6f2010-07-20 15:06:17 +03003380static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3381{
3382 struct vcpu_svm *svm = to_svm(vcpu);
3383 struct vmcb_control_area *control = &svm->vmcb->control;
3384
3385 control->exit_int_info = control->event_inj;
3386 control->exit_int_info_err = control->event_inj_err;
3387 control->event_inj = 0;
3388 svm_complete_interrupts(svm);
3389}
3390
Avi Kivity80e31d42008-07-14 14:44:59 +03003391#ifdef CONFIG_X86_64
3392#define R "r"
3393#else
3394#define R "e"
3395#endif
3396
Avi Kivity851ba692009-08-24 11:10:17 +03003397static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003398{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003399 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003400
Joerg Roedel2041a062010-04-22 12:33:08 +02003401 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3402 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3403 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3404
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003405 /*
3406 * A vmexit emulation is required before the vcpu can be executed
3407 * again.
3408 */
3409 if (unlikely(svm->nested.exit_required))
3410 return;
3411
Rusty Russelle756fc62007-07-30 20:07:08 +10003412 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003413
Joerg Roedel649d6862008-04-16 16:51:15 +02003414 sync_lapic_to_cr8(vcpu);
3415
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003416 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417
Avi Kivity04d2cc72007-09-10 18:10:54 +03003418 clgi();
3419
3420 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003421
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003423 "push %%"R"bp; \n\t"
3424 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3425 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3426 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3427 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3428 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3429 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003430#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003431 "mov %c[r8](%[svm]), %%r8 \n\t"
3432 "mov %c[r9](%[svm]), %%r9 \n\t"
3433 "mov %c[r10](%[svm]), %%r10 \n\t"
3434 "mov %c[r11](%[svm]), %%r11 \n\t"
3435 "mov %c[r12](%[svm]), %%r12 \n\t"
3436 "mov %c[r13](%[svm]), %%r13 \n\t"
3437 "mov %c[r14](%[svm]), %%r14 \n\t"
3438 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003439#endif
3440
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003442 "push %%"R"ax \n\t"
3443 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003444 __ex(SVM_VMLOAD) "\n\t"
3445 __ex(SVM_VMRUN) "\n\t"
3446 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003447 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448
3449 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003450 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3451 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3452 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3453 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3454 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3455 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003456#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003457 "mov %%r8, %c[r8](%[svm]) \n\t"
3458 "mov %%r9, %c[r9](%[svm]) \n\t"
3459 "mov %%r10, %c[r10](%[svm]) \n\t"
3460 "mov %%r11, %c[r11](%[svm]) \n\t"
3461 "mov %%r12, %c[r12](%[svm]) \n\t"
3462 "mov %%r13, %c[r13](%[svm]) \n\t"
3463 "mov %%r14, %c[r14](%[svm]) \n\t"
3464 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003465#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003466 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003467 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003468 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003469 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003470 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3471 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3472 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3473 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3474 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3475 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003476#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003477 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3478 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3479 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3480 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3481 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3482 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3483 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3484 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003485#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003486 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003487 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003488#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003489 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3490#endif
3491 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492
Avi Kivity82ca2d12010-10-21 12:20:34 +02003493#ifdef CONFIG_X86_64
3494 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3495#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003496 loadsegment(fs, svm->host.fs);
Avi Kivity9581d442010-10-19 16:46:55 +02003497#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003498
3499 reload_tss(vcpu);
3500
Avi Kivity56ba47d2007-11-07 17:14:18 +02003501 local_irq_disable();
3502
3503 stgi();
3504
Avi Kivity13c34e02010-10-21 12:20:31 +02003505 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3506 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3507 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3508 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3509
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003510 sync_cr8_to_lapic(vcpu);
3511
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003512 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003513
Gleb Natapov631bc482010-10-14 11:22:52 +02003514 /* if exit due to PF check for async PF */
3515 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3516 svm->apf_reason = kvm_read_and_reset_pf_reason();
3517
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003518 if (npt_enabled) {
3519 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3520 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3521 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003522
3523 /*
3524 * We need to handle MC intercepts here before the vcpu has a chance to
3525 * change the physical cpu
3526 */
3527 if (unlikely(svm->vmcb->control.exit_code ==
3528 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3529 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003530
3531 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003532}
3533
Avi Kivity80e31d42008-07-14 14:44:59 +03003534#undef R
3535
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3537{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003538 struct vcpu_svm *svm = to_svm(vcpu);
3539
3540 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003541 force_new_asid(vcpu);
3542}
3543
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003544static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3545{
3546 struct vcpu_svm *svm = to_svm(vcpu);
3547
3548 svm->vmcb->control.nested_cr3 = root;
3549
3550 /* Also sync guest cr3 here in case we live migrate */
3551 svm->vmcb->save.cr3 = vcpu->arch.cr3;
3552
3553 force_new_asid(vcpu);
3554}
3555
Avi Kivity6aa8b732006-12-10 02:21:36 -08003556static int is_disabled(void)
3557{
Joerg Roedel6031a612007-06-22 12:29:50 +03003558 u64 vm_cr;
3559
3560 rdmsrl(MSR_VM_CR, vm_cr);
3561 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3562 return 1;
3563
Avi Kivity6aa8b732006-12-10 02:21:36 -08003564 return 0;
3565}
3566
Ingo Molnar102d8322007-02-19 14:37:47 +02003567static void
3568svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3569{
3570 /*
3571 * Patch in the VMMCALL instruction:
3572 */
3573 hypercall[0] = 0x0f;
3574 hypercall[1] = 0x01;
3575 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003576}
3577
Yang, Sheng002c7f72007-07-31 14:23:01 +03003578static void svm_check_processor_compat(void *rtn)
3579{
3580 *(int *)rtn = 0;
3581}
3582
Avi Kivity774ead32007-12-26 13:57:04 +02003583static bool svm_cpu_has_accelerated_tpr(void)
3584{
3585 return false;
3586}
3587
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003588static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003589{
3590 return 0;
3591}
3592
Sheng Yang0e851882009-12-18 16:48:46 +08003593static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3594{
3595}
3596
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003597static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3598{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003599 switch (func) {
Joerg Roedel24d1b152010-12-07 17:15:05 +01003600 case 0x00000001:
3601 /* Mask out xsave bit as long as it is not supported by SVM */
3602 entry->ecx &= ~(bit(X86_FEATURE_XSAVE));
3603 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02003604 case 0x80000001:
3605 if (nested)
3606 entry->ecx |= (1 << 2); /* Set SVM bit */
3607 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003608 case 0x8000000A:
3609 entry->eax = 1; /* SVM revision 1 */
3610 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3611 ASID emulation to nested SVM */
3612 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003613 entry->edx = 0; /* Per default do not support any
3614 additional features */
3615
3616 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003617 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02003618 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003619
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02003620 /* Support NPT for the guest if enabled */
3621 if (npt_enabled)
3622 entry->edx |= SVM_FEATURE_NPT;
3623
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003624 break;
3625 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003626}
3627
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003628static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003629 { SVM_EXIT_READ_CR0, "read_cr0" },
3630 { SVM_EXIT_READ_CR3, "read_cr3" },
3631 { SVM_EXIT_READ_CR4, "read_cr4" },
3632 { SVM_EXIT_READ_CR8, "read_cr8" },
3633 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3634 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3635 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3636 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3637 { SVM_EXIT_READ_DR0, "read_dr0" },
3638 { SVM_EXIT_READ_DR1, "read_dr1" },
3639 { SVM_EXIT_READ_DR2, "read_dr2" },
3640 { SVM_EXIT_READ_DR3, "read_dr3" },
3641 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3642 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3643 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3644 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3645 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3646 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003647 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3648 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3649 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3650 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3651 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3652 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3653 { SVM_EXIT_INTR, "interrupt" },
3654 { SVM_EXIT_NMI, "nmi" },
3655 { SVM_EXIT_SMI, "smi" },
3656 { SVM_EXIT_INIT, "init" },
3657 { SVM_EXIT_VINTR, "vintr" },
3658 { SVM_EXIT_CPUID, "cpuid" },
3659 { SVM_EXIT_INVD, "invd" },
3660 { SVM_EXIT_HLT, "hlt" },
3661 { SVM_EXIT_INVLPG, "invlpg" },
3662 { SVM_EXIT_INVLPGA, "invlpga" },
3663 { SVM_EXIT_IOIO, "io" },
3664 { SVM_EXIT_MSR, "msr" },
3665 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3666 { SVM_EXIT_SHUTDOWN, "shutdown" },
3667 { SVM_EXIT_VMRUN, "vmrun" },
3668 { SVM_EXIT_VMMCALL, "hypercall" },
3669 { SVM_EXIT_VMLOAD, "vmload" },
3670 { SVM_EXIT_VMSAVE, "vmsave" },
3671 { SVM_EXIT_STGI, "stgi" },
3672 { SVM_EXIT_CLGI, "clgi" },
3673 { SVM_EXIT_SKINIT, "skinit" },
3674 { SVM_EXIT_WBINVD, "wbinvd" },
3675 { SVM_EXIT_MONITOR, "monitor" },
3676 { SVM_EXIT_MWAIT, "mwait" },
3677 { SVM_EXIT_NPF, "npf" },
3678 { -1, NULL }
3679};
3680
Sheng Yang17cc3932010-01-05 19:02:27 +08003681static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003682{
Sheng Yang17cc3932010-01-05 19:02:27 +08003683 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003684}
3685
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003686static bool svm_rdtscp_supported(void)
3687{
3688 return false;
3689}
3690
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003691static bool svm_has_wbinvd_exit(void)
3692{
3693 return true;
3694}
3695
Avi Kivity02daab22009-12-30 12:40:26 +02003696static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3697{
3698 struct vcpu_svm *svm = to_svm(vcpu);
3699
Joerg Roedel18c918c2010-11-30 18:03:59 +01003700 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01003701 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003702}
3703
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003704static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003705 .cpu_has_kvm_support = has_svm,
3706 .disabled_by_bios = is_disabled,
3707 .hardware_setup = svm_hardware_setup,
3708 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003709 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003710 .hardware_enable = svm_hardware_enable,
3711 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003712 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003713
3714 .vcpu_create = svm_create_vcpu,
3715 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003716 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003717
Avi Kivity04d2cc72007-09-10 18:10:54 +03003718 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719 .vcpu_load = svm_vcpu_load,
3720 .vcpu_put = svm_vcpu_put,
3721
3722 .set_guest_debug = svm_guest_debug,
3723 .get_msr = svm_get_msr,
3724 .set_msr = svm_set_msr,
3725 .get_segment_base = svm_get_segment_base,
3726 .get_segment = svm_get_segment,
3727 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003728 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003729 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003730 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003731 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003732 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003733 .set_cr3 = svm_set_cr3,
3734 .set_cr4 = svm_set_cr4,
3735 .set_efer = svm_set_efer,
3736 .get_idt = svm_get_idt,
3737 .set_idt = svm_set_idt,
3738 .get_gdt = svm_get_gdt,
3739 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003740 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003741 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003742 .get_rflags = svm_get_rflags,
3743 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003744 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003745 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003746
Avi Kivity6aa8b732006-12-10 02:21:36 -08003747 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003748
Avi Kivity6aa8b732006-12-10 02:21:36 -08003749 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003750 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003751 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003752 .set_interrupt_shadow = svm_set_interrupt_shadow,
3753 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003754 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003755 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003756 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003757 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03003758 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02003759 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003760 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003761 .get_nmi_mask = svm_get_nmi_mask,
3762 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003763 .enable_nmi_window = enable_nmi_window,
3764 .enable_irq_window = enable_irq_window,
3765 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003766
3767 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003768 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003769 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003770
Avi Kivity586f9602010-11-18 13:09:54 +02003771 .get_exit_info = svm_get_exit_info,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003772 .exit_reasons_str = svm_exit_reasons_str,
Avi Kivity586f9602010-11-18 13:09:54 +02003773
Sheng Yang17cc3932010-01-05 19:02:27 +08003774 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003775
3776 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003777
3778 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003779
3780 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003781
3782 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10003783
3784 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10003785 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003786
3787 .set_tdp_cr3 = set_tdp_cr3,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003788};
3789
3790static int __init svm_init(void)
3791{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003792 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003793 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003794}
3795
3796static void __exit svm_exit(void)
3797{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003798 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799}
3800
3801module_init(svm_init)
3802module_exit(svm_exit)