blob: 052561c347531ffbcb2a6ba9c7b1479fd31b8e30 [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 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100191 VMCB_DIRTY_MAX,
192};
193
194#define VMCB_ALWAYS_DIRTY_MASK 0U
195
196static inline void mark_all_dirty(struct vmcb *vmcb)
197{
198 vmcb->control.clean = 0;
199}
200
201static inline void mark_all_clean(struct vmcb *vmcb)
202{
203 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
204 & ~VMCB_ALWAYS_DIRTY_MASK;
205}
206
207static inline void mark_dirty(struct vmcb *vmcb, int bit)
208{
209 vmcb->control.clean &= ~(1 << bit);
210}
211
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
213{
214 return container_of(vcpu, struct vcpu_svm, vcpu);
215}
216
Joerg Roedel384c6362010-11-30 18:03:56 +0100217static void recalc_intercepts(struct vcpu_svm *svm)
218{
219 struct vmcb_control_area *c, *h;
220 struct nested_state *g;
221
Joerg Roedel116a0a22010-12-03 11:45:49 +0100222 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
223
Joerg Roedel384c6362010-11-30 18:03:56 +0100224 if (!is_guest_mode(&svm->vcpu))
225 return;
226
227 c = &svm->vmcb->control;
228 h = &svm->nested.hsave->control;
229 g = &svm->nested;
230
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100231 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100232 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100233 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
234 c->intercept = h->intercept | g->intercept;
235}
236
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100237static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
238{
239 if (is_guest_mode(&svm->vcpu))
240 return svm->nested.hsave;
241 else
242 return svm->vmcb;
243}
244
245static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
246{
247 struct vmcb *vmcb = get_host_vmcb(svm);
248
249 vmcb->control.intercept_cr |= (1U << bit);
250
251 recalc_intercepts(svm);
252}
253
254static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
255{
256 struct vmcb *vmcb = get_host_vmcb(svm);
257
258 vmcb->control.intercept_cr &= ~(1U << bit);
259
260 recalc_intercepts(svm);
261}
262
263static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
264{
265 struct vmcb *vmcb = get_host_vmcb(svm);
266
267 return vmcb->control.intercept_cr & (1U << bit);
268}
269
Joerg Roedel3aed0412010-11-30 18:03:58 +0100270static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
271{
272 struct vmcb *vmcb = get_host_vmcb(svm);
273
274 vmcb->control.intercept_dr |= (1U << bit);
275
276 recalc_intercepts(svm);
277}
278
279static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
280{
281 struct vmcb *vmcb = get_host_vmcb(svm);
282
283 vmcb->control.intercept_dr &= ~(1U << bit);
284
285 recalc_intercepts(svm);
286}
287
Joerg Roedel18c918c2010-11-30 18:03:59 +0100288static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
289{
290 struct vmcb *vmcb = get_host_vmcb(svm);
291
292 vmcb->control.intercept_exceptions |= (1U << bit);
293
294 recalc_intercepts(svm);
295}
296
297static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
298{
299 struct vmcb *vmcb = get_host_vmcb(svm);
300
301 vmcb->control.intercept_exceptions &= ~(1U << bit);
302
303 recalc_intercepts(svm);
304}
305
Joerg Roedel8a05a1b2010-11-30 18:04:00 +0100306static inline void set_intercept(struct vcpu_svm *svm, int bit)
307{
308 struct vmcb *vmcb = get_host_vmcb(svm);
309
310 vmcb->control.intercept |= (1ULL << bit);
311
312 recalc_intercepts(svm);
313}
314
315static inline void clr_intercept(struct vcpu_svm *svm, int bit)
316{
317 struct vmcb *vmcb = get_host_vmcb(svm);
318
319 vmcb->control.intercept &= ~(1ULL << bit);
320
321 recalc_intercepts(svm);
322}
323
Joerg Roedel2af91942009-08-07 11:49:28 +0200324static inline void enable_gif(struct vcpu_svm *svm)
325{
326 svm->vcpu.arch.hflags |= HF_GIF_MASK;
327}
328
329static inline void disable_gif(struct vcpu_svm *svm)
330{
331 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
332}
333
334static inline bool gif_set(struct vcpu_svm *svm)
335{
336 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
337}
338
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339static unsigned long iopm_base;
340
341struct kvm_ldttss_desc {
342 u16 limit0;
343 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100344 unsigned base1:8, type:5, dpl:2, p:1;
345 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800346 u32 base3;
347 u32 zero1;
348} __attribute__((packed));
349
350struct svm_cpu_data {
351 int cpu;
352
Avi Kivity5008fdf2007-04-02 13:05:50 +0300353 u64 asid_generation;
354 u32 max_asid;
355 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356 struct kvm_ldttss_desc *tss_desc;
357
358 struct page *save_area;
359};
360
361static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300362static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800363
364struct svm_init_data {
365 int cpu;
366 int r;
367};
368
369static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
370
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200371#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800372#define MSRS_RANGE_SIZE 2048
373#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
374
Joerg Roedel455716f2010-03-01 15:34:35 +0100375static u32 svm_msrpm_offset(u32 msr)
376{
377 u32 offset;
378 int i;
379
380 for (i = 0; i < NUM_MSR_MAPS; i++) {
381 if (msr < msrpm_ranges[i] ||
382 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
383 continue;
384
385 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
386 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
387
388 /* Now we have the u8 offset - but need the u32 offset */
389 return offset / 4;
390 }
391
392 /* MSR not in any range */
393 return MSR_INVALID;
394}
395
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396#define MAX_INST_SIZE 15
397
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398static inline void clgi(void)
399{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300400 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800401}
402
403static inline void stgi(void)
404{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300405 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406}
407
408static inline void invlpga(unsigned long addr, u32 asid)
409{
Joerg Roedele0231712010-02-24 18:59:10 +0100410 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411}
412
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413static inline void force_new_asid(struct kvm_vcpu *vcpu)
414{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400415 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800416}
417
418static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
419{
420 force_new_asid(vcpu);
421}
422
Joerg Roedel4b161842010-09-10 17:31:03 +0200423static int get_npt_level(void)
424{
425#ifdef CONFIG_X86_64
426 return PT64_ROOT_LEVEL;
427#else
428 return PT32E_ROOT_LEVEL;
429#endif
430}
431
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
433{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000434 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100435 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600436 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437
Alexander Graf9962d032008-11-25 20:17:02 +0100438 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439}
440
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441static int is_external_interrupt(u32 info)
442{
443 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
444 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
445}
446
Glauber Costa2809f5d2009-05-12 16:21:05 -0400447static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
448{
449 struct vcpu_svm *svm = to_svm(vcpu);
450 u32 ret = 0;
451
452 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100453 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400454 return ret & mask;
455}
456
457static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
458{
459 struct vcpu_svm *svm = to_svm(vcpu);
460
461 if (mask == 0)
462 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
463 else
464 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
465
466}
467
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
469{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400470 struct vcpu_svm *svm = to_svm(vcpu);
471
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200472 if (svm->vmcb->control.next_rip != 0)
473 svm->next_rip = svm->vmcb->control.next_rip;
474
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400475 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300476 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300477 EMULATE_DONE)
478 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 return;
480 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300481 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
482 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
483 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300485 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400486 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487}
488
Jan Kiszka116a4752010-02-23 17:47:54 +0100489static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200490 bool has_error_code, u32 error_code,
491 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100492{
493 struct vcpu_svm *svm = to_svm(vcpu);
494
Joerg Roedele0231712010-02-24 18:59:10 +0100495 /*
496 * If we are within a nested VM we'd better #VMEXIT and let the guest
497 * handle the exception
498 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200499 if (!reinject &&
500 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100501 return;
502
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200503 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100504 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
505
506 /*
507 * For guest debugging where we have to reinject #BP if some
508 * INT3 is guest-owned:
509 * Emulate nRIP by moving RIP forward. Will fail if injection
510 * raises a fault that is not intercepted. Still better than
511 * failing in all cases.
512 */
513 skip_emulated_instruction(&svm->vcpu);
514 rip = kvm_rip_read(&svm->vcpu);
515 svm->int3_rip = rip + svm->vmcb->save.cs.base;
516 svm->int3_injected = rip - old_rip;
517 }
518
Jan Kiszka116a4752010-02-23 17:47:54 +0100519 svm->vmcb->control.event_inj = nr
520 | SVM_EVTINJ_VALID
521 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
522 | SVM_EVTINJ_TYPE_EXEPT;
523 svm->vmcb->control.event_inj_err = error_code;
524}
525
Joerg Roedel67ec6602010-05-17 14:43:35 +0200526static void svm_init_erratum_383(void)
527{
528 u32 low, high;
529 int err;
530 u64 val;
531
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200532 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200533 return;
534
535 /* Use _safe variants to not break nested virtualization */
536 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
537 if (err)
538 return;
539
540 val |= (1ULL << 47);
541
542 low = lower_32_bits(val);
543 high = upper_32_bits(val);
544
545 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
546
547 erratum_383_found = true;
548}
549
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550static int has_svm(void)
551{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200552 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553
Eduardo Habkost63d11422008-11-17 19:03:20 -0200554 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800555 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800556 return 0;
557 }
558
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559 return 1;
560}
561
562static void svm_hardware_disable(void *garbage)
563{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200564 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565}
566
Alexander Graf10474ae2009-09-15 11:37:46 +0200567static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800568{
569
Tejun Heo0fe1e002009-10-29 22:34:14 +0900570 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200572 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 struct desc_struct *gdt;
574 int me = raw_smp_processor_id();
575
Alexander Graf10474ae2009-09-15 11:37:46 +0200576 rdmsrl(MSR_EFER, efer);
577 if (efer & EFER_SVME)
578 return -EBUSY;
579
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000581 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
582 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200583 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900585 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800586
Tejun Heo0fe1e002009-10-29 22:34:14 +0900587 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000588 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200590 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591 }
592
Tejun Heo0fe1e002009-10-29 22:34:14 +0900593 sd->asid_generation = 1;
594 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
595 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200597 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200598 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900599 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600
Alexander Graf9962d032008-11-25 20:17:02 +0100601 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800602
Linus Torvaldsd0316552009-12-14 09:58:24 -0800603 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200604
Joerg Roedel67ec6602010-05-17 14:43:35 +0200605 svm_init_erratum_383();
606
Alexander Graf10474ae2009-09-15 11:37:46 +0200607 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800608}
609
Joerg Roedel0da1db752008-07-02 16:02:11 +0200610static void svm_cpu_uninit(int cpu)
611{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900612 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200613
Tejun Heo0fe1e002009-10-29 22:34:14 +0900614 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200615 return;
616
617 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900618 __free_page(sd->save_area);
619 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200620}
621
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622static int svm_cpu_init(int cpu)
623{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900624 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625 int r;
626
Tejun Heo0fe1e002009-10-29 22:34:14 +0900627 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
628 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900630 sd->cpu = cpu;
631 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900633 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634 goto err_1;
635
Tejun Heo0fe1e002009-10-29 22:34:14 +0900636 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637
638 return 0;
639
640err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900641 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642 return r;
643
644}
645
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100646static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647{
648 int i;
649
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100650 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
651 if (direct_access_msrs[i].index == index)
652 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100654 return false;
655}
656
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657static void set_msr_interception(u32 *msrpm, unsigned msr,
658 int read, int write)
659{
Joerg Roedel455716f2010-03-01 15:34:35 +0100660 u8 bit_read, bit_write;
661 unsigned long tmp;
662 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100664 /*
665 * If this warning triggers extend the direct_access_msrs list at the
666 * beginning of the file
667 */
668 WARN_ON(!valid_msr_intercept(msr));
669
Joerg Roedel455716f2010-03-01 15:34:35 +0100670 offset = svm_msrpm_offset(msr);
671 bit_read = 2 * (msr & 0x0f);
672 bit_write = 2 * (msr & 0x0f) + 1;
673 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800674
Joerg Roedel455716f2010-03-01 15:34:35 +0100675 BUG_ON(offset == MSR_INVALID);
676
677 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
678 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
679
680 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681}
682
Joerg Roedelf65c2292008-02-13 18:58:46 +0100683static void svm_vcpu_init_msrpm(u32 *msrpm)
684{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100685 int i;
686
Joerg Roedelf65c2292008-02-13 18:58:46 +0100687 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
688
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100689 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
690 if (!direct_access_msrs[i].always)
691 continue;
692
693 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
694 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100695}
696
Joerg Roedel323c3d82010-03-01 15:34:37 +0100697static void add_msr_offset(u32 offset)
698{
699 int i;
700
701 for (i = 0; i < MSRPM_OFFSETS; ++i) {
702
703 /* Offset already in list? */
704 if (msrpm_offsets[i] == offset)
705 return;
706
707 /* Slot used by another offset? */
708 if (msrpm_offsets[i] != MSR_INVALID)
709 continue;
710
711 /* Add offset to list */
712 msrpm_offsets[i] = offset;
713
714 return;
715 }
716
717 /*
718 * If this BUG triggers the msrpm_offsets table has an overflow. Just
719 * increase MSRPM_OFFSETS in this case.
720 */
721 BUG();
722}
723
724static void init_msrpm_offsets(void)
725{
726 int i;
727
728 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
729
730 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
731 u32 offset;
732
733 offset = svm_msrpm_offset(direct_access_msrs[i].index);
734 BUG_ON(offset == MSR_INVALID);
735
736 add_msr_offset(offset);
737 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738}
739
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100740static void svm_enable_lbrv(struct vcpu_svm *svm)
741{
742 u32 *msrpm = svm->msrpm;
743
744 svm->vmcb->control.lbr_ctl = 1;
745 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
746 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
747 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
748 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
749}
750
751static void svm_disable_lbrv(struct vcpu_svm *svm)
752{
753 u32 *msrpm = svm->msrpm;
754
755 svm->vmcb->control.lbr_ctl = 0;
756 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
757 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
758 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
759 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
760}
761
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762static __init int svm_hardware_setup(void)
763{
764 int cpu;
765 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100766 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767 int r;
768
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
770
771 if (!iopm_pages)
772 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300773
774 iopm_va = page_address(iopm_pages);
775 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
777
Joerg Roedel323c3d82010-03-01 15:34:37 +0100778 init_msrpm_offsets();
779
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100780 if (boot_cpu_has(X86_FEATURE_NX))
781 kvm_enable_efer_bits(EFER_NX);
782
Alexander Graf1b2fd702009-02-02 16:23:51 +0100783 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
784 kvm_enable_efer_bits(EFER_FFXSR);
785
Alexander Graf236de052008-11-25 20:17:10 +0100786 if (nested) {
787 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200788 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100789 }
790
Zachary Amsden3230bb42009-09-29 11:38:37 -1000791 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792 r = svm_cpu_init(cpu);
793 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100794 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100796
797 svm_features = cpuid_edx(SVM_CPUID_FUNC);
798
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200799 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100800 npt_enabled = false;
801
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100802 if (npt_enabled && !npt) {
803 printk(KERN_INFO "kvm: Nested Paging disabled\n");
804 npt_enabled = false;
805 }
806
Joerg Roedel18552672008-02-07 13:47:41 +0100807 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100808 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100809 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200810 } else
811 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100812
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813 return 0;
814
Joerg Roedelf65c2292008-02-13 18:58:46 +0100815err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
817 iopm_base = 0;
818 return r;
819}
820
821static __exit void svm_hardware_unsetup(void)
822{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200823 int cpu;
824
Zachary Amsden3230bb42009-09-29 11:38:37 -1000825 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200826 svm_cpu_uninit(cpu);
827
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100829 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830}
831
832static void init_seg(struct vmcb_seg *seg)
833{
834 seg->selector = 0;
835 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100836 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837 seg->limit = 0xffff;
838 seg->base = 0;
839}
840
841static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
842{
843 seg->selector = 0;
844 seg->attrib = SVM_SELECTOR_P_MASK | type;
845 seg->limit = 0xffff;
846 seg->base = 0;
847}
848
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000849static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
850{
851 struct vcpu_svm *svm = to_svm(vcpu);
852 u64 g_tsc_offset = 0;
853
Joerg Roedel20307532010-11-29 17:51:48 +0100854 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000855 g_tsc_offset = svm->vmcb->control.tsc_offset -
856 svm->nested.hsave->control.tsc_offset;
857 svm->nested.hsave->control.tsc_offset = offset;
858 }
859
860 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +0100861
862 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000863}
864
Zachary Amsdene48672f2010-08-19 22:07:23 -1000865static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
866{
867 struct vcpu_svm *svm = to_svm(vcpu);
868
869 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +0100870 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -1000871 svm->nested.hsave->control.tsc_offset += adjustment;
Joerg Roedel116a0a22010-12-03 11:45:49 +0100872 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -1000873}
874
Joerg Roedele6101a92008-02-13 18:58:45 +0100875static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876{
Joerg Roedele6101a92008-02-13 18:58:45 +0100877 struct vmcb_control_area *control = &svm->vmcb->control;
878 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879
Avi Kivitybff78272010-01-07 13:16:08 +0200880 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100881 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +0200882
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100883 set_cr_intercept(svm, INTERCEPT_CR0_READ);
884 set_cr_intercept(svm, INTERCEPT_CR3_READ);
885 set_cr_intercept(svm, INTERCEPT_CR4_READ);
886 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
887 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
888 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
889 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800890
Joerg Roedel3aed0412010-11-30 18:03:58 +0100891 set_dr_intercept(svm, INTERCEPT_DR0_READ);
892 set_dr_intercept(svm, INTERCEPT_DR1_READ);
893 set_dr_intercept(svm, INTERCEPT_DR2_READ);
894 set_dr_intercept(svm, INTERCEPT_DR3_READ);
895 set_dr_intercept(svm, INTERCEPT_DR4_READ);
896 set_dr_intercept(svm, INTERCEPT_DR5_READ);
897 set_dr_intercept(svm, INTERCEPT_DR6_READ);
898 set_dr_intercept(svm, INTERCEPT_DR7_READ);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899
Joerg Roedel3aed0412010-11-30 18:03:58 +0100900 set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
901 set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
902 set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
903 set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
904 set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
905 set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
906 set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
907 set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908
Joerg Roedel18c918c2010-11-30 18:03:59 +0100909 set_exception_intercept(svm, PF_VECTOR);
910 set_exception_intercept(svm, UD_VECTOR);
911 set_exception_intercept(svm, MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912
Joerg Roedel8a05a1b2010-11-30 18:04:00 +0100913 set_intercept(svm, INTERCEPT_INTR);
914 set_intercept(svm, INTERCEPT_NMI);
915 set_intercept(svm, INTERCEPT_SMI);
916 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
917 set_intercept(svm, INTERCEPT_CPUID);
918 set_intercept(svm, INTERCEPT_INVD);
919 set_intercept(svm, INTERCEPT_HLT);
920 set_intercept(svm, INTERCEPT_INVLPG);
921 set_intercept(svm, INTERCEPT_INVLPGA);
922 set_intercept(svm, INTERCEPT_IOIO_PROT);
923 set_intercept(svm, INTERCEPT_MSR_PROT);
924 set_intercept(svm, INTERCEPT_TASK_SWITCH);
925 set_intercept(svm, INTERCEPT_SHUTDOWN);
926 set_intercept(svm, INTERCEPT_VMRUN);
927 set_intercept(svm, INTERCEPT_VMMCALL);
928 set_intercept(svm, INTERCEPT_VMLOAD);
929 set_intercept(svm, INTERCEPT_VMSAVE);
930 set_intercept(svm, INTERCEPT_STGI);
931 set_intercept(svm, INTERCEPT_CLGI);
932 set_intercept(svm, INTERCEPT_SKINIT);
933 set_intercept(svm, INTERCEPT_WBINVD);
934 set_intercept(svm, INTERCEPT_MONITOR);
935 set_intercept(svm, INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936
937 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100938 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 control->int_ctl = V_INTR_MASKING_MASK;
940
941 init_seg(&save->es);
942 init_seg(&save->ss);
943 init_seg(&save->ds);
944 init_seg(&save->fs);
945 init_seg(&save->gs);
946
947 save->cs.selector = 0xf000;
948 /* Executable/Readable Code Segment */
949 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
950 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
951 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800952 /*
953 * cs.base should really be 0xffff0000, but vmx can't handle that, so
954 * be consistent with it.
955 *
956 * Replace when we have real mode working for vmx.
957 */
958 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959
960 save->gdtr.limit = 0xffff;
961 save->idtr.limit = 0xffff;
962
963 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
964 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
965
Marcelo Tosattieaa48512010-08-31 19:13:14 -0300966 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -0400967 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 save->dr7 = 0x400;
969 save->rflags = 2;
970 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300971 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972
Joerg Roedele0231712010-02-24 18:59:10 +0100973 /*
974 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200975 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -0300977 svm->vcpu.arch.cr0 = 0;
978 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200979
Rusty Russell66aee912007-07-17 23:34:16 +1000980 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100982
983 if (npt_enabled) {
984 /* Setup VMCB for Nested Paging */
985 control->nested_ctl = 1;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +0100986 clr_intercept(svm, INTERCEPT_TASK_SWITCH);
987 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +0100988 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100989 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
990 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100991 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100992 save->cr3 = 0;
993 save->cr4 = 0;
994 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300995 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100996
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200997 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200998 svm->vcpu.arch.hflags = 0;
999
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001000 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001001 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001002 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001003 }
1004
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001005 mark_all_dirty(svm->vmcb);
1006
Joerg Roedel2af91942009-08-07 11:49:28 +02001007 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008}
1009
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001010static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001011{
1012 struct vcpu_svm *svm = to_svm(vcpu);
1013
Joerg Roedele6101a92008-02-13 18:58:45 +01001014 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001015
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001016 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001017 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001018 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
1019 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +02001020 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001021 vcpu->arch.regs_avail = ~0;
1022 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001023
1024 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001025}
1026
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001027static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001029 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001031 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001032 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001033 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001034 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035
Rusty Russellc16f8622007-07-30 21:12:19 +10001036 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001037 if (!svm) {
1038 err = -ENOMEM;
1039 goto out;
1040 }
1041
1042 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1043 if (err)
1044 goto free_svm;
1045
Joerg Roedelf65c2292008-02-13 18:58:46 +01001046 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001047 page = alloc_page(GFP_KERNEL);
1048 if (!page)
1049 goto uninit;
1050
Joerg Roedelf65c2292008-02-13 18:58:46 +01001051 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1052 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001053 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001054
1055 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1056 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001057 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001058
Alexander Grafb286d5d2008-11-25 20:17:05 +01001059 hsave_page = alloc_page(GFP_KERNEL);
1060 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001061 goto free_page3;
1062
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001063 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001064
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001065 svm->msrpm = page_address(msrpm_pages);
1066 svm_vcpu_init_msrpm(svm->msrpm);
1067
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001068 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001069 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001070
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001071 svm->vmcb = page_address(page);
1072 clear_page(svm->vmcb);
1073 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1074 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +01001075 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001076 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001077
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001078 err = fx_init(&svm->vcpu);
1079 if (err)
1080 goto free_page4;
1081
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001082 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001083 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001084 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001086 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001087
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001088free_page4:
1089 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001090free_page3:
1091 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1092free_page2:
1093 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1094free_page1:
1095 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001096uninit:
1097 kvm_vcpu_uninit(&svm->vcpu);
1098free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001099 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001100out:
1101 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102}
1103
1104static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1105{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001106 struct vcpu_svm *svm = to_svm(vcpu);
1107
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001108 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001109 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001110 __free_page(virt_to_page(svm->nested.hsave));
1111 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001112 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001113 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114}
1115
Avi Kivity15ad7142007-07-11 18:17:21 +03001116static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001118 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001119 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001120
Avi Kivity0cc50642007-03-25 12:07:27 +02001121 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001122 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001123 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001124 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001125
Avi Kivity82ca2d12010-10-21 12:20:34 +02001126#ifdef CONFIG_X86_64
1127 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1128#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001129 savesegment(fs, svm->host.fs);
1130 savesegment(gs, svm->host.gs);
1131 svm->host.ldt = kvm_read_ldt();
1132
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001133 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001134 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135}
1136
1137static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1138{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001139 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001140 int i;
1141
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001142 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001143 kvm_load_ldt(svm->host.ldt);
1144#ifdef CONFIG_X86_64
1145 loadsegment(fs, svm->host.fs);
1146 load_gs_index(svm->host.gs);
1147 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
1148#else
1149 loadsegment(gs, svm->host.gs);
1150#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001151 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001152 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153}
1154
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1156{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001157 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158}
1159
1160static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1161{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001162 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163}
1164
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001165static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1166{
1167 switch (reg) {
1168 case VCPU_EXREG_PDPTR:
1169 BUG_ON(!npt_enabled);
Joerg Roedelff03a072010-09-10 17:30:57 +02001170 load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001171 break;
1172 default:
1173 BUG();
1174 }
1175}
1176
Alexander Graff0b85052008-11-25 20:17:01 +01001177static void svm_set_vintr(struct vcpu_svm *svm)
1178{
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001179 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001180}
1181
1182static void svm_clear_vintr(struct vcpu_svm *svm)
1183{
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001184 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001185}
1186
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1188{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001189 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190
1191 switch (seg) {
1192 case VCPU_SREG_CS: return &save->cs;
1193 case VCPU_SREG_DS: return &save->ds;
1194 case VCPU_SREG_ES: return &save->es;
1195 case VCPU_SREG_FS: return &save->fs;
1196 case VCPU_SREG_GS: return &save->gs;
1197 case VCPU_SREG_SS: return &save->ss;
1198 case VCPU_SREG_TR: return &save->tr;
1199 case VCPU_SREG_LDTR: return &save->ldtr;
1200 }
1201 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001202 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203}
1204
1205static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1206{
1207 struct vmcb_seg *s = svm_seg(vcpu, seg);
1208
1209 return s->base;
1210}
1211
1212static void svm_get_segment(struct kvm_vcpu *vcpu,
1213 struct kvm_segment *var, int seg)
1214{
1215 struct vmcb_seg *s = svm_seg(vcpu, seg);
1216
1217 var->base = s->base;
1218 var->limit = s->limit;
1219 var->selector = s->selector;
1220 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1221 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1222 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1223 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1224 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1225 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1226 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1227 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001228
Joerg Roedele0231712010-02-24 18:59:10 +01001229 /*
1230 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001231 * for cross vendor migration purposes by "not present"
1232 */
1233 var->unusable = !var->present || (var->type == 0);
1234
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001235 switch (seg) {
1236 case VCPU_SREG_CS:
1237 /*
1238 * SVM always stores 0 for the 'G' bit in the CS selector in
1239 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1240 * Intel's VMENTRY has a check on the 'G' bit.
1241 */
Amit Shah25022ac2008-10-27 09:04:17 +00001242 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001243 break;
1244 case VCPU_SREG_TR:
1245 /*
1246 * Work around a bug where the busy flag in the tr selector
1247 * isn't exposed
1248 */
Amit Shahc0d09822008-10-27 09:04:18 +00001249 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001250 break;
1251 case VCPU_SREG_DS:
1252 case VCPU_SREG_ES:
1253 case VCPU_SREG_FS:
1254 case VCPU_SREG_GS:
1255 /*
1256 * The accessed bit must always be set in the segment
1257 * descriptor cache, although it can be cleared in the
1258 * descriptor, the cached bit always remains at 1. Since
1259 * Intel has a check on this, set it here to support
1260 * cross-vendor migration.
1261 */
1262 if (!var->unusable)
1263 var->type |= 0x1;
1264 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001265 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001266 /*
1267 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001268 * descriptor is left as 1, although the whole segment has
1269 * been made unusable. Clear it here to pass an Intel VMX
1270 * entry check when cross vendor migrating.
1271 */
1272 if (var->unusable)
1273 var->db = 0;
1274 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001275 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276}
1277
Izik Eidus2e4d2652008-03-24 19:38:34 +02001278static int svm_get_cpl(struct kvm_vcpu *vcpu)
1279{
1280 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1281
1282 return save->cpl;
1283}
1284
Gleb Natapov89a27f42010-02-16 10:51:48 +02001285static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001287 struct vcpu_svm *svm = to_svm(vcpu);
1288
Gleb Natapov89a27f42010-02-16 10:51:48 +02001289 dt->size = svm->vmcb->save.idtr.limit;
1290 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291}
1292
Gleb Natapov89a27f42010-02-16 10:51:48 +02001293static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001295 struct vcpu_svm *svm = to_svm(vcpu);
1296
Gleb Natapov89a27f42010-02-16 10:51:48 +02001297 svm->vmcb->save.idtr.limit = dt->size;
1298 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299}
1300
Gleb Natapov89a27f42010-02-16 10:51:48 +02001301static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001303 struct vcpu_svm *svm = to_svm(vcpu);
1304
Gleb Natapov89a27f42010-02-16 10:51:48 +02001305 dt->size = svm->vmcb->save.gdtr.limit;
1306 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307}
1308
Gleb Natapov89a27f42010-02-16 10:51:48 +02001309static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001311 struct vcpu_svm *svm = to_svm(vcpu);
1312
Gleb Natapov89a27f42010-02-16 10:51:48 +02001313 svm->vmcb->save.gdtr.limit = dt->size;
1314 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315}
1316
Avi Kivitye8467fd2009-12-29 18:43:06 +02001317static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1318{
1319}
1320
Anthony Liguori25c4c272007-04-27 09:29:21 +03001321static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001322{
1323}
1324
Avi Kivityd2251572010-01-06 10:55:27 +02001325static void update_cr0_intercept(struct vcpu_svm *svm)
1326{
1327 ulong gcr0 = svm->vcpu.arch.cr0;
1328 u64 *hcr0 = &svm->vmcb->save.cr0;
1329
1330 if (!svm->vcpu.fpu_active)
1331 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1332 else
1333 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1334 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1335
1336
1337 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001338 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1339 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001340 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001341 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1342 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001343 }
1344}
1345
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1347{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001348 struct vcpu_svm *svm = to_svm(vcpu);
1349
Joerg Roedel20307532010-11-29 17:51:48 +01001350 if (is_guest_mode(vcpu)) {
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001351 /*
1352 * We are here because we run in nested mode, the host kvm
1353 * intercepts cr0 writes but the l1 hypervisor does not.
1354 * But the L1 hypervisor may intercept selective cr0 writes.
1355 * This needs to be checked here.
1356 */
1357 unsigned long old, new;
1358
1359 /* Remove bits that would trigger a real cr0 write intercept */
1360 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1361 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1362
1363 if (old == new) {
1364 /* cr0 write with ts and mp unchanged */
1365 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
Joerg Roedelcda00082010-09-02 17:29:46 +02001366 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
1367 svm->nested.vmexit_rip = kvm_rip_read(vcpu);
1368 svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
1369 svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001370 return;
Joerg Roedelcda00082010-09-02 17:29:46 +02001371 }
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001372 }
1373 }
1374
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001375#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001376 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001377 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001378 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001379 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380 }
1381
Mike Dayd77c26f2007-10-08 09:02:08 -04001382 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001383 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001384 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 }
1386 }
1387#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001388 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001389
1390 if (!npt_enabled)
1391 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001392
1393 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001394 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001395 /*
1396 * re-enable caching here because the QEMU bios
1397 * does not do it - this results in some delay at
1398 * reboot
1399 */
1400 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001401 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001402 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403}
1404
1405static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1406{
Joerg Roedel6394b642008-04-09 14:15:29 +02001407 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001408 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1409
1410 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1411 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001412
Joerg Roedelec077262008-04-09 14:15:28 +02001413 vcpu->arch.cr4 = cr4;
1414 if (!npt_enabled)
1415 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001416 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001417 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418}
1419
1420static void svm_set_segment(struct kvm_vcpu *vcpu,
1421 struct kvm_segment *var, int seg)
1422{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001423 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 struct vmcb_seg *s = svm_seg(vcpu, seg);
1425
1426 s->base = var->base;
1427 s->limit = var->limit;
1428 s->selector = var->selector;
1429 if (var->unusable)
1430 s->attrib = 0;
1431 else {
1432 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1433 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1434 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1435 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1436 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1437 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1438 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1439 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1440 }
1441 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001442 svm->vmcb->save.cpl
1443 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1445
1446}
1447
Gleb Natapov44c11432009-05-11 13:35:52 +03001448static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001450 struct vcpu_svm *svm = to_svm(vcpu);
1451
Joerg Roedel18c918c2010-11-30 18:03:59 +01001452 clr_exception_intercept(svm, DB_VECTOR);
1453 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001454
Jan Kiszka6be7d302009-10-18 13:24:54 +02001455 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001456 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001457
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001458 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1459 if (vcpu->guest_debug &
1460 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001461 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001462 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001463 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001464 } else
1465 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001466}
1467
Jan Kiszka355be0b2009-10-03 00:31:21 +02001468static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001469{
Gleb Natapov44c11432009-05-11 13:35:52 +03001470 struct vcpu_svm *svm = to_svm(vcpu);
1471
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001472 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1473 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1474 else
1475 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1476
Jan Kiszka355be0b2009-10-03 00:31:21 +02001477 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478}
1479
Tejun Heo0fe1e002009-10-29 22:34:14 +09001480static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001482 if (sd->next_asid > sd->max_asid) {
1483 ++sd->asid_generation;
1484 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001485 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486 }
1487
Tejun Heo0fe1e002009-10-29 22:34:14 +09001488 svm->asid_generation = sd->asid_generation;
1489 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490}
1491
Gleb Natapov020df072010-04-13 10:05:23 +03001492static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001494 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001495
Gleb Natapov020df072010-04-13 10:05:23 +03001496 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497}
1498
Avi Kivity851ba692009-08-24 11:10:17 +03001499static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500{
Gleb Natapov631bc482010-10-14 11:22:52 +02001501 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001503 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504
Gleb Natapov631bc482010-10-14 11:22:52 +02001505 switch (svm->apf_reason) {
1506 default:
1507 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001508
Gleb Natapov631bc482010-10-14 11:22:52 +02001509 trace_kvm_page_fault(fault_address, error_code);
1510 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1511 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1512 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
1513 break;
1514 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1515 svm->apf_reason = 0;
1516 local_irq_disable();
1517 kvm_async_pf_task_wait(fault_address);
1518 local_irq_enable();
1519 break;
1520 case KVM_PV_REASON_PAGE_READY:
1521 svm->apf_reason = 0;
1522 local_irq_disable();
1523 kvm_async_pf_task_wake(fault_address);
1524 local_irq_enable();
1525 break;
1526 }
1527 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528}
1529
Avi Kivity851ba692009-08-24 11:10:17 +03001530static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001531{
Avi Kivity851ba692009-08-24 11:10:17 +03001532 struct kvm_run *kvm_run = svm->vcpu.run;
1533
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001534 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001535 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001536 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001537 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1538 return 1;
1539 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001540
Jan Kiszka6be7d302009-10-18 13:24:54 +02001541 if (svm->nmi_singlestep) {
1542 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001543 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1544 svm->vmcb->save.rflags &=
1545 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1546 update_db_intercept(&svm->vcpu);
1547 }
1548
1549 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001550 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001551 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1552 kvm_run->debug.arch.pc =
1553 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1554 kvm_run->debug.arch.exception = DB_VECTOR;
1555 return 0;
1556 }
1557
1558 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001559}
1560
Avi Kivity851ba692009-08-24 11:10:17 +03001561static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001562{
Avi Kivity851ba692009-08-24 11:10:17 +03001563 struct kvm_run *kvm_run = svm->vcpu.run;
1564
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001565 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1566 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1567 kvm_run->debug.arch.exception = BP_VECTOR;
1568 return 0;
1569}
1570
Avi Kivity851ba692009-08-24 11:10:17 +03001571static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001572{
1573 int er;
1574
Avi Kivity851ba692009-08-24 11:10:17 +03001575 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001576 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001577 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001578 return 1;
1579}
1580
Avi Kivity6b52d182010-01-21 15:31:47 +02001581static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001582{
Avi Kivity6b52d182010-01-21 15:31:47 +02001583 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001584
Joerg Roedel18c918c2010-11-30 18:03:59 +01001585 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001586
Rusty Russelle756fc62007-07-30 20:07:08 +10001587 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001588 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001589}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001590
Avi Kivity6b52d182010-01-21 15:31:47 +02001591static int nm_interception(struct vcpu_svm *svm)
1592{
1593 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001594 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001595}
1596
Joerg Roedel67ec6602010-05-17 14:43:35 +02001597static bool is_erratum_383(void)
1598{
1599 int err, i;
1600 u64 value;
1601
1602 if (!erratum_383_found)
1603 return false;
1604
1605 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1606 if (err)
1607 return false;
1608
1609 /* Bit 62 may or may not be set for this mce */
1610 value &= ~(1ULL << 62);
1611
1612 if (value != 0xb600000000010015ULL)
1613 return false;
1614
1615 /* Clear MCi_STATUS registers */
1616 for (i = 0; i < 6; ++i)
1617 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1618
1619 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1620 if (!err) {
1621 u32 low, high;
1622
1623 value &= ~(1ULL << 2);
1624 low = lower_32_bits(value);
1625 high = upper_32_bits(value);
1626
1627 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1628 }
1629
1630 /* Flush tlb to evict multi-match entries */
1631 __flush_tlb_all();
1632
1633 return true;
1634}
1635
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001636static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001637{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001638 if (is_erratum_383()) {
1639 /*
1640 * Erratum 383 triggered. Guest state is corrupt so kill the
1641 * guest.
1642 */
1643 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1644
Avi Kivitya8eeb042010-05-10 12:34:53 +03001645 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001646
1647 return;
1648 }
1649
Joerg Roedel53371b52008-04-09 14:15:30 +02001650 /*
1651 * On an #MC intercept the MCE handler is not called automatically in
1652 * the host. So do it by hand here.
1653 */
1654 asm volatile (
1655 "int $0x12\n");
1656 /* not sure if we ever come back to this point */
1657
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001658 return;
1659}
1660
1661static int mc_interception(struct vcpu_svm *svm)
1662{
Joerg Roedel53371b52008-04-09 14:15:30 +02001663 return 1;
1664}
1665
Avi Kivity851ba692009-08-24 11:10:17 +03001666static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001667{
Avi Kivity851ba692009-08-24 11:10:17 +03001668 struct kvm_run *kvm_run = svm->vcpu.run;
1669
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001670 /*
1671 * VMCB is undefined after a SHUTDOWN intercept
1672 * so reinitialize it.
1673 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001674 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001675 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001676
1677 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1678 return 0;
1679}
1680
Avi Kivity851ba692009-08-24 11:10:17 +03001681static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001683 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001684 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001685 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001686 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687
Rusty Russelle756fc62007-07-30 20:07:08 +10001688 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001689 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001690 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001691 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001692 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001693
Avi Kivity039576c2007-03-20 12:46:50 +02001694 port = io_info >> 16;
1695 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001696 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001697 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001698
1699 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700}
1701
Avi Kivity851ba692009-08-24 11:10:17 +03001702static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001703{
1704 return 1;
1705}
1706
Avi Kivity851ba692009-08-24 11:10:17 +03001707static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001708{
1709 ++svm->vcpu.stat.irq_exits;
1710 return 1;
1711}
1712
Avi Kivity851ba692009-08-24 11:10:17 +03001713static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714{
1715 return 1;
1716}
1717
Avi Kivity851ba692009-08-24 11:10:17 +03001718static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001720 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001721 skip_emulated_instruction(&svm->vcpu);
1722 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723}
1724
Avi Kivity851ba692009-08-24 11:10:17 +03001725static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001726{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001727 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001728 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001729 kvm_emulate_hypercall(&svm->vcpu);
1730 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001731}
1732
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001733static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1734{
1735 struct vcpu_svm *svm = to_svm(vcpu);
1736
1737 return svm->nested.nested_cr3;
1738}
1739
1740static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1741 unsigned long root)
1742{
1743 struct vcpu_svm *svm = to_svm(vcpu);
1744
1745 svm->vmcb->control.nested_cr3 = root;
1746 force_new_asid(vcpu);
1747}
1748
Avi Kivity6389ee92010-11-29 16:12:30 +02001749static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1750 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001751{
1752 struct vcpu_svm *svm = to_svm(vcpu);
1753
1754 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1755 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001756 svm->vmcb->control.exit_info_1 = fault->error_code;
1757 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001758
1759 nested_svm_vmexit(svm);
1760}
1761
Joerg Roedel4b161842010-09-10 17:31:03 +02001762static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1763{
1764 int r;
1765
1766 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1767
1768 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1769 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
1770 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1771 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1772 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1773
1774 return r;
1775}
1776
1777static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1778{
1779 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1780}
1781
Alexander Grafc0725422008-11-25 20:17:03 +01001782static int nested_svm_check_permissions(struct vcpu_svm *svm)
1783{
Avi Kivityf6801df2010-01-21 15:31:50 +02001784 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001785 || !is_paging(&svm->vcpu)) {
1786 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1787 return 1;
1788 }
1789
1790 if (svm->vmcb->save.cpl) {
1791 kvm_inject_gp(&svm->vcpu, 0);
1792 return 1;
1793 }
1794
1795 return 0;
1796}
1797
Alexander Grafcf74a782008-11-25 20:17:08 +01001798static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1799 bool has_error_code, u32 error_code)
1800{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001801 int vmexit;
1802
Joerg Roedel20307532010-11-29 17:51:48 +01001803 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02001804 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001805
Joerg Roedel0295ad72009-08-07 11:49:37 +02001806 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1807 svm->vmcb->control.exit_code_hi = 0;
1808 svm->vmcb->control.exit_info_1 = error_code;
1809 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1810
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001811 vmexit = nested_svm_intercept(svm);
1812 if (vmexit == NESTED_EXIT_DONE)
1813 svm->nested.exit_required = true;
1814
1815 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001816}
1817
Joerg Roedel8fe54652010-02-19 16:23:01 +01001818/* This function returns true if it is save to enable the irq window */
1819static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001820{
Joerg Roedel20307532010-11-29 17:51:48 +01001821 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001822 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001823
Joerg Roedel26666952009-08-07 11:49:46 +02001824 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001825 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001826
Joerg Roedel26666952009-08-07 11:49:46 +02001827 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001828 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001829
Gleb Natapova0a07cd2010-09-20 10:15:32 +02001830 /*
1831 * if vmexit was already requested (by intercepted exception
1832 * for instance) do not overwrite it with "external interrupt"
1833 * vmexit.
1834 */
1835 if (svm->nested.exit_required)
1836 return false;
1837
Joerg Roedel197717d2010-02-24 18:59:19 +01001838 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1839 svm->vmcb->control.exit_info_1 = 0;
1840 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001841
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001842 if (svm->nested.intercept & 1ULL) {
1843 /*
1844 * The #vmexit can't be emulated here directly because this
1845 * code path runs with irqs and preemtion disabled. A
1846 * #vmexit emulation might sleep. Only signal request for
1847 * the #vmexit here.
1848 */
1849 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001850 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001851 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001852 }
1853
Joerg Roedel8fe54652010-02-19 16:23:01 +01001854 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001855}
1856
Joerg Roedel887f5002010-02-24 18:59:12 +01001857/* This function returns true if it is save to enable the nmi window */
1858static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1859{
Joerg Roedel20307532010-11-29 17:51:48 +01001860 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01001861 return true;
1862
1863 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1864 return true;
1865
1866 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1867 svm->nested.exit_required = true;
1868
1869 return false;
1870}
1871
Joerg Roedel7597f122010-02-19 16:23:00 +01001872static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001873{
1874 struct page *page;
1875
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001876 might_sleep();
1877
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001878 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001879 if (is_error_page(page))
1880 goto error;
1881
Joerg Roedel7597f122010-02-19 16:23:00 +01001882 *_page = page;
1883
1884 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001885
1886error:
1887 kvm_release_page_clean(page);
1888 kvm_inject_gp(&svm->vcpu, 0);
1889
1890 return NULL;
1891}
1892
Joerg Roedel7597f122010-02-19 16:23:00 +01001893static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001894{
Joerg Roedel7597f122010-02-19 16:23:00 +01001895 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001896 kvm_release_page_dirty(page);
1897}
1898
Joerg Roedelce2ac082010-03-01 15:34:39 +01001899static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001900{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001901 unsigned port;
1902 u8 val, bit;
1903 u64 gpa;
1904
1905 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1906 return NESTED_EXIT_HOST;
1907
1908 port = svm->vmcb->control.exit_info_1 >> 16;
1909 gpa = svm->nested.vmcb_iopm + (port / 8);
1910 bit = port % 8;
1911 val = 0;
1912
1913 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1914 val &= (1 << bit);
1915
1916 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1917}
1918
Joerg Roedeld2477822010-03-01 15:34:34 +01001919static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001920{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001921 u32 offset, msr, value;
1922 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001923
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001924 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001925 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001926
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001927 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1928 offset = svm_msrpm_offset(msr);
1929 write = svm->vmcb->control.exit_info_1 & 1;
1930 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001931
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001932 if (offset == MSR_INVALID)
1933 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001934
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001935 /* Offset is in 32 bit units but need in 8 bit units */
1936 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001937
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001938 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1939 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001940
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001941 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001942}
1943
Joerg Roedel410e4d52009-08-07 11:49:44 +02001944static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001945{
Alexander Grafcf74a782008-11-25 20:17:08 +01001946 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001947
Joerg Roedel410e4d52009-08-07 11:49:44 +02001948 switch (exit_code) {
1949 case SVM_EXIT_INTR:
1950 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001951 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001952 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001953 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001954 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001955 if (npt_enabled)
1956 return NESTED_EXIT_HOST;
1957 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001958 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02001959 /* When we're shadowing, trap PFs, but not async PF */
1960 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001961 return NESTED_EXIT_HOST;
1962 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001963 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1964 nm_interception(svm);
1965 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001966 default:
1967 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001968 }
1969
Joerg Roedel410e4d52009-08-07 11:49:44 +02001970 return NESTED_EXIT_CONTINUE;
1971}
1972
1973/*
1974 * If this function returns true, this #vmexit was already handled
1975 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001976static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001977{
1978 u32 exit_code = svm->vmcb->control.exit_code;
1979 int vmexit = NESTED_EXIT_HOST;
1980
Alexander Grafcf74a782008-11-25 20:17:08 +01001981 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001982 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001983 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001984 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001985 case SVM_EXIT_IOIO:
1986 vmexit = nested_svm_intercept_ioio(svm);
1987 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001988 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1989 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
1990 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001991 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001992 break;
1993 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01001994 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1995 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
1996 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001997 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001998 break;
1999 }
2000 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2001 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002002 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002003 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002004 /* async page fault always cause vmexit */
2005 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2006 svm->apf_reason != 0)
2007 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002008 break;
2009 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002010 case SVM_EXIT_ERR: {
2011 vmexit = NESTED_EXIT_DONE;
2012 break;
2013 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002014 default: {
2015 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002016 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002017 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002018 }
2019 }
2020
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002021 return vmexit;
2022}
2023
2024static int nested_svm_exit_handled(struct vcpu_svm *svm)
2025{
2026 int vmexit;
2027
2028 vmexit = nested_svm_intercept(svm);
2029
2030 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002031 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002032
2033 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002034}
2035
Joerg Roedel0460a972009-08-07 11:49:31 +02002036static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2037{
2038 struct vmcb_control_area *dst = &dst_vmcb->control;
2039 struct vmcb_control_area *from = &from_vmcb->control;
2040
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002041 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002042 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002043 dst->intercept_exceptions = from->intercept_exceptions;
2044 dst->intercept = from->intercept;
2045 dst->iopm_base_pa = from->iopm_base_pa;
2046 dst->msrpm_base_pa = from->msrpm_base_pa;
2047 dst->tsc_offset = from->tsc_offset;
2048 dst->asid = from->asid;
2049 dst->tlb_ctl = from->tlb_ctl;
2050 dst->int_ctl = from->int_ctl;
2051 dst->int_vector = from->int_vector;
2052 dst->int_state = from->int_state;
2053 dst->exit_code = from->exit_code;
2054 dst->exit_code_hi = from->exit_code_hi;
2055 dst->exit_info_1 = from->exit_info_1;
2056 dst->exit_info_2 = from->exit_info_2;
2057 dst->exit_int_info = from->exit_int_info;
2058 dst->exit_int_info_err = from->exit_int_info_err;
2059 dst->nested_ctl = from->nested_ctl;
2060 dst->event_inj = from->event_inj;
2061 dst->event_inj_err = from->event_inj_err;
2062 dst->nested_cr3 = from->nested_cr3;
2063 dst->lbr_ctl = from->lbr_ctl;
2064}
2065
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002066static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002067{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002068 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002069 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002070 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002071 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002072
Joerg Roedel17897f32009-10-09 16:08:29 +02002073 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2074 vmcb->control.exit_info_1,
2075 vmcb->control.exit_info_2,
2076 vmcb->control.exit_int_info,
2077 vmcb->control.exit_int_info_err);
2078
Joerg Roedel7597f122010-02-19 16:23:00 +01002079 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002080 if (!nested_vmcb)
2081 return 1;
2082
Joerg Roedel20307532010-11-29 17:51:48 +01002083 /* Exit Guest-Mode */
2084 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002085 svm->nested.vmcb = 0;
2086
Alexander Grafcf74a782008-11-25 20:17:08 +01002087 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002088 disable_gif(svm);
2089
2090 nested_vmcb->save.es = vmcb->save.es;
2091 nested_vmcb->save.cs = vmcb->save.cs;
2092 nested_vmcb->save.ss = vmcb->save.ss;
2093 nested_vmcb->save.ds = vmcb->save.ds;
2094 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2095 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002096 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002097 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002098 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02002099 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002100 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02002101 nested_vmcb->save.rflags = vmcb->save.rflags;
2102 nested_vmcb->save.rip = vmcb->save.rip;
2103 nested_vmcb->save.rsp = vmcb->save.rsp;
2104 nested_vmcb->save.rax = vmcb->save.rax;
2105 nested_vmcb->save.dr7 = vmcb->save.dr7;
2106 nested_vmcb->save.dr6 = vmcb->save.dr6;
2107 nested_vmcb->save.cpl = vmcb->save.cpl;
2108
2109 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2110 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2111 nested_vmcb->control.int_state = vmcb->control.int_state;
2112 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2113 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2114 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2115 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2116 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2117 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002118 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002119
2120 /*
2121 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2122 * to make sure that we do not lose injected events. So check event_inj
2123 * here and copy it to exit_int_info if it is valid.
2124 * Exit_int_info and event_inj can't be both valid because the case
2125 * below only happens on a VMRUN instruction intercept which has
2126 * no valid exit_int_info set.
2127 */
2128 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2129 struct vmcb_control_area *nc = &nested_vmcb->control;
2130
2131 nc->exit_int_info = vmcb->control.event_inj;
2132 nc->exit_int_info_err = vmcb->control.event_inj_err;
2133 }
2134
Joerg Roedel33740e42009-08-07 11:49:29 +02002135 nested_vmcb->control.tlb_ctl = 0;
2136 nested_vmcb->control.event_inj = 0;
2137 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002138
2139 /* We always set V_INTR_MASKING and remember the old value in hflags */
2140 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2141 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2142
Alexander Grafcf74a782008-11-25 20:17:08 +01002143 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002144 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002145
Alexander Graf219b65d2009-06-15 15:21:25 +02002146 kvm_clear_exception_queue(&svm->vcpu);
2147 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002148
Joerg Roedel4b161842010-09-10 17:31:03 +02002149 svm->nested.nested_cr3 = 0;
2150
Alexander Grafcf74a782008-11-25 20:17:08 +01002151 /* Restore selected save entries */
2152 svm->vmcb->save.es = hsave->save.es;
2153 svm->vmcb->save.cs = hsave->save.cs;
2154 svm->vmcb->save.ss = hsave->save.ss;
2155 svm->vmcb->save.ds = hsave->save.ds;
2156 svm->vmcb->save.gdtr = hsave->save.gdtr;
2157 svm->vmcb->save.idtr = hsave->save.idtr;
2158 svm->vmcb->save.rflags = hsave->save.rflags;
2159 svm_set_efer(&svm->vcpu, hsave->save.efer);
2160 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2161 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2162 if (npt_enabled) {
2163 svm->vmcb->save.cr3 = hsave->save.cr3;
2164 svm->vcpu.arch.cr3 = hsave->save.cr3;
2165 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002166 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002167 }
2168 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2169 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2170 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2171 svm->vmcb->save.dr7 = 0;
2172 svm->vmcb->save.cpl = 0;
2173 svm->vmcb->control.exit_int_info = 0;
2174
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002175 mark_all_dirty(svm->vmcb);
2176
Joerg Roedel7597f122010-02-19 16:23:00 +01002177 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002178
Joerg Roedel4b161842010-09-10 17:31:03 +02002179 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002180 kvm_mmu_reset_context(&svm->vcpu);
2181 kvm_mmu_load(&svm->vcpu);
2182
2183 return 0;
2184}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002185
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002186static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002187{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002188 /*
2189 * This function merges the msr permission bitmaps of kvm and the
2190 * nested vmcb. It is omptimized in that it only merges the parts where
2191 * the kvm msr permission bitmap may contain zero bits
2192 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002193 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002194
Joerg Roedel323c3d82010-03-01 15:34:37 +01002195 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2196 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002197
Joerg Roedel323c3d82010-03-01 15:34:37 +01002198 for (i = 0; i < MSRPM_OFFSETS; i++) {
2199 u32 value, p;
2200 u64 offset;
2201
2202 if (msrpm_offsets[i] == 0xffffffff)
2203 break;
2204
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002205 p = msrpm_offsets[i];
2206 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002207
2208 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2209 return false;
2210
2211 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2212 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002213
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002214 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002215
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002216 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002217}
2218
Joerg Roedel52c65a302010-08-02 16:46:44 +02002219static bool nested_vmcb_checks(struct vmcb *vmcb)
2220{
2221 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2222 return false;
2223
Joerg Roedeldbe77582010-08-02 16:46:45 +02002224 if (vmcb->control.asid == 0)
2225 return false;
2226
Joerg Roedel4b161842010-09-10 17:31:03 +02002227 if (vmcb->control.nested_ctl && !npt_enabled)
2228 return false;
2229
Joerg Roedel52c65a302010-08-02 16:46:44 +02002230 return true;
2231}
2232
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002233static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002234{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002235 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002236 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002237 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002238 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002239 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002240
Joerg Roedel06fc77722010-02-19 16:23:07 +01002241 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002242
Joerg Roedel7597f122010-02-19 16:23:00 +01002243 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002244 if (!nested_vmcb)
2245 return false;
2246
Joerg Roedel52c65a302010-08-02 16:46:44 +02002247 if (!nested_vmcb_checks(nested_vmcb)) {
2248 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2249 nested_vmcb->control.exit_code_hi = 0;
2250 nested_vmcb->control.exit_info_1 = 0;
2251 nested_vmcb->control.exit_info_2 = 0;
2252
2253 nested_svm_unmap(page);
2254
2255 return false;
2256 }
2257
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002258 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002259 nested_vmcb->save.rip,
2260 nested_vmcb->control.int_ctl,
2261 nested_vmcb->control.event_inj,
2262 nested_vmcb->control.nested_ctl);
2263
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002264 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2265 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002266 nested_vmcb->control.intercept_exceptions,
2267 nested_vmcb->control.intercept);
2268
Alexander Graf3d6368e2008-11-25 20:17:07 +01002269 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002270 kvm_clear_exception_queue(&svm->vcpu);
2271 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002272
Joerg Roedele0231712010-02-24 18:59:10 +01002273 /*
2274 * Save the old vmcb, so we don't need to pick what we save, but can
2275 * restore everything when a VMEXIT occurs
2276 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002277 hsave->save.es = vmcb->save.es;
2278 hsave->save.cs = vmcb->save.cs;
2279 hsave->save.ss = vmcb->save.ss;
2280 hsave->save.ds = vmcb->save.ds;
2281 hsave->save.gdtr = vmcb->save.gdtr;
2282 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002283 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002284 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002285 hsave->save.cr4 = svm->vcpu.arch.cr4;
2286 hsave->save.rflags = vmcb->save.rflags;
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002287 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002288 hsave->save.rsp = vmcb->save.rsp;
2289 hsave->save.rax = vmcb->save.rax;
2290 if (npt_enabled)
2291 hsave->save.cr3 = vmcb->save.cr3;
2292 else
2293 hsave->save.cr3 = svm->vcpu.arch.cr3;
2294
Joerg Roedel0460a972009-08-07 11:49:31 +02002295 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002296
2297 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2298 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2299 else
2300 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2301
Joerg Roedel4b161842010-09-10 17:31:03 +02002302 if (nested_vmcb->control.nested_ctl) {
2303 kvm_mmu_unload(&svm->vcpu);
2304 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2305 nested_svm_init_mmu_context(&svm->vcpu);
2306 }
2307
Alexander Graf3d6368e2008-11-25 20:17:07 +01002308 /* Load the nested guest state */
2309 svm->vmcb->save.es = nested_vmcb->save.es;
2310 svm->vmcb->save.cs = nested_vmcb->save.cs;
2311 svm->vmcb->save.ss = nested_vmcb->save.ss;
2312 svm->vmcb->save.ds = nested_vmcb->save.ds;
2313 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2314 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2315 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2316 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2317 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2318 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2319 if (npt_enabled) {
2320 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2321 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002322 } else
Avi Kivity23902182010-06-10 17:02:16 +03002323 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002324
2325 /* Guest paging mode is active - reset mmu */
2326 kvm_mmu_reset_context(&svm->vcpu);
2327
Joerg Roedeldefbba52009-08-07 11:49:30 +02002328 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002329 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2330 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2331 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002332
Alexander Graf3d6368e2008-11-25 20:17:07 +01002333 /* In case we don't even reach vcpu_run, the fields are not updated */
2334 svm->vmcb->save.rax = nested_vmcb->save.rax;
2335 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2336 svm->vmcb->save.rip = nested_vmcb->save.rip;
2337 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2338 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2339 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2340
Joerg Roedelf7138532010-03-01 15:34:40 +01002341 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002342 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002343
Joerg Roedelaad42c62009-08-07 11:49:34 +02002344 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002345 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002346 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002347 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2348 svm->nested.intercept = nested_vmcb->control.intercept;
2349
Alexander Graf3d6368e2008-11-25 20:17:07 +01002350 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002351 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002352 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2353 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2354 else
2355 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2356
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002357 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2358 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002359 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2360 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002361 }
2362
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002363 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01002364 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002365
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002366 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002367 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2368 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2369 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002370 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2371 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2372
Joerg Roedel7597f122010-02-19 16:23:00 +01002373 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002374
Joerg Roedel20307532010-11-29 17:51:48 +01002375 /* Enter Guest-Mode */
2376 enter_guest_mode(&svm->vcpu);
2377
Joerg Roedel384c6362010-11-30 18:03:56 +01002378 /*
2379 * Merge guest and host intercepts - must be called with vcpu in
2380 * guest-mode to take affect here
2381 */
2382 recalc_intercepts(svm);
2383
Joerg Roedel06fc77722010-02-19 16:23:07 +01002384 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002385
Joerg Roedel2af91942009-08-07 11:49:28 +02002386 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002387
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002388 mark_all_dirty(svm->vmcb);
2389
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002390 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002391}
2392
Joerg Roedel9966bf62009-08-07 11:49:40 +02002393static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002394{
2395 to_vmcb->save.fs = from_vmcb->save.fs;
2396 to_vmcb->save.gs = from_vmcb->save.gs;
2397 to_vmcb->save.tr = from_vmcb->save.tr;
2398 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2399 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2400 to_vmcb->save.star = from_vmcb->save.star;
2401 to_vmcb->save.lstar = from_vmcb->save.lstar;
2402 to_vmcb->save.cstar = from_vmcb->save.cstar;
2403 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2404 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2405 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2406 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002407}
2408
Avi Kivity851ba692009-08-24 11:10:17 +03002409static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002410{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002411 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002412 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002413
Alexander Graf55426752008-11-25 20:17:06 +01002414 if (nested_svm_check_permissions(svm))
2415 return 1;
2416
2417 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2418 skip_emulated_instruction(&svm->vcpu);
2419
Joerg Roedel7597f122010-02-19 16:23:00 +01002420 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002421 if (!nested_vmcb)
2422 return 1;
2423
2424 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002425 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002426
2427 return 1;
2428}
2429
Avi Kivity851ba692009-08-24 11:10:17 +03002430static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002431{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002432 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002433 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002434
Alexander Graf55426752008-11-25 20:17:06 +01002435 if (nested_svm_check_permissions(svm))
2436 return 1;
2437
2438 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2439 skip_emulated_instruction(&svm->vcpu);
2440
Joerg Roedel7597f122010-02-19 16:23:00 +01002441 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002442 if (!nested_vmcb)
2443 return 1;
2444
2445 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002446 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002447
2448 return 1;
2449}
2450
Avi Kivity851ba692009-08-24 11:10:17 +03002451static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002452{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002453 if (nested_svm_check_permissions(svm))
2454 return 1;
2455
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002456 /* Save rip after vmrun instruction */
2457 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002458
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002459 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002460 return 1;
2461
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002462 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002463 goto failed;
2464
2465 return 1;
2466
2467failed:
2468
2469 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2470 svm->vmcb->control.exit_code_hi = 0;
2471 svm->vmcb->control.exit_info_1 = 0;
2472 svm->vmcb->control.exit_info_2 = 0;
2473
2474 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002475
2476 return 1;
2477}
2478
Avi Kivity851ba692009-08-24 11:10:17 +03002479static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002480{
2481 if (nested_svm_check_permissions(svm))
2482 return 1;
2483
2484 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2485 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002486 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002487
Joerg Roedel2af91942009-08-07 11:49:28 +02002488 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002489
2490 return 1;
2491}
2492
Avi Kivity851ba692009-08-24 11:10:17 +03002493static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002494{
2495 if (nested_svm_check_permissions(svm))
2496 return 1;
2497
2498 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2499 skip_emulated_instruction(&svm->vcpu);
2500
Joerg Roedel2af91942009-08-07 11:49:28 +02002501 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002502
2503 /* After a CLGI no interrupts should come */
2504 svm_clear_vintr(svm);
2505 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2506
2507 return 1;
2508}
2509
Avi Kivity851ba692009-08-24 11:10:17 +03002510static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002511{
2512 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002513
Joerg Roedelec1ff792009-10-09 16:08:31 +02002514 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2515 vcpu->arch.regs[VCPU_REGS_RAX]);
2516
Alexander Grafff092382009-06-15 15:21:24 +02002517 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2518 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2519
2520 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2521 skip_emulated_instruction(&svm->vcpu);
2522 return 1;
2523}
2524
Joerg Roedel532a46b2009-10-09 16:08:32 +02002525static int skinit_interception(struct vcpu_svm *svm)
2526{
2527 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2528
2529 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2530 return 1;
2531}
2532
Avi Kivity851ba692009-08-24 11:10:17 +03002533static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002535 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536 return 1;
2537}
2538
Avi Kivity851ba692009-08-24 11:10:17 +03002539static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540{
Izik Eidus37817f22008-03-24 23:14:53 +02002541 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002542 int reason;
2543 int int_type = svm->vmcb->control.exit_int_info &
2544 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002545 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002546 uint32_t type =
2547 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2548 uint32_t idt_v =
2549 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002550 bool has_error_code = false;
2551 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002552
2553 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002554
Izik Eidus37817f22008-03-24 23:14:53 +02002555 if (svm->vmcb->control.exit_info_2 &
2556 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002557 reason = TASK_SWITCH_IRET;
2558 else if (svm->vmcb->control.exit_info_2 &
2559 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2560 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002561 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002562 reason = TASK_SWITCH_GATE;
2563 else
2564 reason = TASK_SWITCH_CALL;
2565
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002566 if (reason == TASK_SWITCH_GATE) {
2567 switch (type) {
2568 case SVM_EXITINTINFO_TYPE_NMI:
2569 svm->vcpu.arch.nmi_injected = false;
2570 break;
2571 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002572 if (svm->vmcb->control.exit_info_2 &
2573 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2574 has_error_code = true;
2575 error_code =
2576 (u32)svm->vmcb->control.exit_info_2;
2577 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002578 kvm_clear_exception_queue(&svm->vcpu);
2579 break;
2580 case SVM_EXITINTINFO_TYPE_INTR:
2581 kvm_clear_interrupt_queue(&svm->vcpu);
2582 break;
2583 default:
2584 break;
2585 }
2586 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002587
Gleb Natapov8317c292009-04-12 13:37:02 +03002588 if (reason != TASK_SWITCH_GATE ||
2589 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2590 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002591 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2592 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002593
Gleb Natapovacb54512010-04-15 21:03:50 +03002594 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2595 has_error_code, error_code) == EMULATE_FAIL) {
2596 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2597 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2598 svm->vcpu.run->internal.ndata = 0;
2599 return 0;
2600 }
2601 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602}
2603
Avi Kivity851ba692009-08-24 11:10:17 +03002604static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002605{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002606 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002607 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002608 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002609}
2610
Avi Kivity851ba692009-08-24 11:10:17 +03002611static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002612{
2613 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01002614 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002615 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002616 return 1;
2617}
2618
Avi Kivity851ba692009-08-24 11:10:17 +03002619static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002620{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002621 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002622}
2623
Avi Kivity851ba692009-08-24 11:10:17 +03002624static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002626 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002627}
2628
Joerg Roedelcda00082010-09-02 17:29:46 +02002629static int cr0_write_interception(struct vcpu_svm *svm)
2630{
2631 struct kvm_vcpu *vcpu = &svm->vcpu;
2632 int r;
2633
2634 r = emulate_instruction(&svm->vcpu, 0, 0, 0);
2635
2636 if (svm->nested.vmexit_rip) {
2637 kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
2638 kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
2639 kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
2640 svm->nested.vmexit_rip = 0;
2641 }
2642
2643 return r == EMULATE_DONE;
2644}
2645
Avi Kivity851ba692009-08-24 11:10:17 +03002646static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002647{
Avi Kivity851ba692009-08-24 11:10:17 +03002648 struct kvm_run *kvm_run = svm->vcpu.run;
2649
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002650 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2651 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002652 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002653 if (irqchip_in_kernel(svm->vcpu.kvm)) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002654 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel1d075432007-12-06 21:02:25 +01002655 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002656 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002657 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2658 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002659 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2660 return 0;
2661}
2662
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2664{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002665 struct vcpu_svm *svm = to_svm(vcpu);
2666
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302668 case MSR_IA32_TSC: {
Joerg Roedel4cc70312010-11-30 18:04:01 +01002669 struct vmcb *vmcb = get_host_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670
Joerg Roedel4cc70312010-11-30 18:04:01 +01002671 *data = vmcb->control.tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672 break;
2673 }
Brian Gerst8c065852010-07-17 09:03:26 -04002674 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002675 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002677#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002679 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 break;
2681 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002682 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683 break;
2684 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002685 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686 break;
2687 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002688 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689 break;
2690#endif
2691 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002692 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693 break;
2694 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002695 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696 break;
2697 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002698 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002700 /*
2701 * Nobody will change the following 5 values in the VMCB so we can
2702 * safely return them on rdmsr. They will always be 0 until LBRV is
2703 * implemented.
2704 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002705 case MSR_IA32_DEBUGCTLMSR:
2706 *data = svm->vmcb->save.dbgctl;
2707 break;
2708 case MSR_IA32_LASTBRANCHFROMIP:
2709 *data = svm->vmcb->save.br_from;
2710 break;
2711 case MSR_IA32_LASTBRANCHTOIP:
2712 *data = svm->vmcb->save.br_to;
2713 break;
2714 case MSR_IA32_LASTINTFROMIP:
2715 *data = svm->vmcb->save.last_excp_from;
2716 break;
2717 case MSR_IA32_LASTINTTOIP:
2718 *data = svm->vmcb->save.last_excp_to;
2719 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002720 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002721 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002722 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002723 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002724 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002725 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002726 case MSR_IA32_UCODE_REV:
2727 *data = 0x01000065;
2728 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002730 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 }
2732 return 0;
2733}
2734
Avi Kivity851ba692009-08-24 11:10:17 +03002735static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002737 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738 u64 data;
2739
Avi Kivity59200272010-01-25 19:47:02 +02002740 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2741 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002742 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002743 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002744 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002745
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002746 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002747 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002748 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002749 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 }
2751 return 1;
2752}
2753
Joerg Roedel4a810182010-02-24 18:59:15 +01002754static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2755{
2756 struct vcpu_svm *svm = to_svm(vcpu);
2757 int svm_dis, chg_mask;
2758
2759 if (data & ~SVM_VM_CR_VALID_MASK)
2760 return 1;
2761
2762 chg_mask = SVM_VM_CR_VALID_MASK;
2763
2764 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2765 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2766
2767 svm->nested.vm_cr_msr &= ~chg_mask;
2768 svm->nested.vm_cr_msr |= (data & chg_mask);
2769
2770 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2771
2772 /* check for svm_disable while efer.svme is set */
2773 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2774 return 1;
2775
2776 return 0;
2777}
2778
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2780{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002781 struct vcpu_svm *svm = to_svm(vcpu);
2782
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002784 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10002785 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002787 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002788 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002789 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002790#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002792 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002793 break;
2794 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002795 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796 break;
2797 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002798 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 break;
2800 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002801 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 break;
2803#endif
2804 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002805 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806 break;
2807 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002808 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002809 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 break;
2811 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002812 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002813 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002815 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02002816 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002817 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002818 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002819 break;
2820 }
2821 if (data & DEBUGCTL_RESERVED_BITS)
2822 return 1;
2823
2824 svm->vmcb->save.dbgctl = data;
2825 if (data & (1ULL<<0))
2826 svm_enable_lbrv(svm);
2827 else
2828 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002829 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002830 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002831 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002832 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002833 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002834 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002835 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002836 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2837 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002839 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840 }
2841 return 0;
2842}
2843
Avi Kivity851ba692009-08-24 11:10:17 +03002844static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002846 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002847 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002848 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002849
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002850
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002851 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002852 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2853 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002854 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002855 } else {
2856 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002857 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002858 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859 return 1;
2860}
2861
Avi Kivity851ba692009-08-24 11:10:17 +03002862static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863{
Rusty Russelle756fc62007-07-30 20:07:08 +10002864 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002865 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 else
Avi Kivity851ba692009-08-24 11:10:17 +03002867 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868}
2869
Avi Kivity851ba692009-08-24 11:10:17 +03002870static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002871{
Avi Kivity851ba692009-08-24 11:10:17 +03002872 struct kvm_run *kvm_run = svm->vcpu.run;
2873
Avi Kivity3842d132010-07-27 12:30:24 +03002874 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01002875 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002876 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002877 /*
2878 * If the user space waits to inject interrupts, exit as soon as
2879 * possible
2880 */
Gleb Natapov80618232009-04-21 17:44:56 +03002881 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2882 kvm_run->request_interrupt_window &&
2883 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002884 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002885 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2886 return 0;
2887 }
2888
2889 return 1;
2890}
2891
Mark Langsdorf565d0992009-10-06 14:25:02 -05002892static int pause_interception(struct vcpu_svm *svm)
2893{
2894 kvm_vcpu_on_spin(&(svm->vcpu));
2895 return 1;
2896}
2897
Avi Kivity851ba692009-08-24 11:10:17 +03002898static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002899 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2900 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2901 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2902 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002903 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedelcda00082010-09-02 17:29:46 +02002904 [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002905 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2906 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2907 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2908 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2910 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2911 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002912 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2913 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2914 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2915 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2917 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2918 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2919 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002920 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002922 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002923 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002924 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2925 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002926 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002927 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2928 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2929 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2930 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002931 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932 [SVM_EXIT_SMI] = nop_on_interception,
2933 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002934 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002935 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002936 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002937 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002938 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002939 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002940 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002941 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002942 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002943 [SVM_EXIT_MSR] = msr_interception,
2944 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002945 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002946 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002947 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002948 [SVM_EXIT_VMLOAD] = vmload_interception,
2949 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002950 [SVM_EXIT_STGI] = stgi_interception,
2951 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002952 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002953 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002954 [SVM_EXIT_MONITOR] = invalid_op_interception,
2955 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002956 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957};
2958
Joerg Roedel3f10c842010-05-05 16:04:42 +02002959void dump_vmcb(struct kvm_vcpu *vcpu)
2960{
2961 struct vcpu_svm *svm = to_svm(vcpu);
2962 struct vmcb_control_area *control = &svm->vmcb->control;
2963 struct vmcb_save_area *save = &svm->vmcb->save;
2964
2965 pr_err("VMCB Control Area:\n");
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002966 pr_err("cr_read: %04x\n", control->intercept_cr & 0xffff);
2967 pr_err("cr_write: %04x\n", control->intercept_cr >> 16);
Joerg Roedel3aed0412010-11-30 18:03:58 +01002968 pr_err("dr_read: %04x\n", control->intercept_dr & 0xffff);
2969 pr_err("dr_write: %04x\n", control->intercept_dr >> 16);
Joerg Roedel3f10c842010-05-05 16:04:42 +02002970 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2971 pr_err("intercepts: %016llx\n", control->intercept);
2972 pr_err("pause filter count: %d\n", control->pause_filter_count);
2973 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2974 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2975 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2976 pr_err("asid: %d\n", control->asid);
2977 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2978 pr_err("int_ctl: %08x\n", control->int_ctl);
2979 pr_err("int_vector: %08x\n", control->int_vector);
2980 pr_err("int_state: %08x\n", control->int_state);
2981 pr_err("exit_code: %08x\n", control->exit_code);
2982 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2983 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2984 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2985 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2986 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2987 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2988 pr_err("event_inj: %08x\n", control->event_inj);
2989 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2990 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
2991 pr_err("next_rip: %016llx\n", control->next_rip);
2992 pr_err("VMCB State Save Area:\n");
2993 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2994 save->es.selector, save->es.attrib,
2995 save->es.limit, save->es.base);
2996 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2997 save->cs.selector, save->cs.attrib,
2998 save->cs.limit, save->cs.base);
2999 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
3000 save->ss.selector, save->ss.attrib,
3001 save->ss.limit, save->ss.base);
3002 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
3003 save->ds.selector, save->ds.attrib,
3004 save->ds.limit, save->ds.base);
3005 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
3006 save->fs.selector, save->fs.attrib,
3007 save->fs.limit, save->fs.base);
3008 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
3009 save->gs.selector, save->gs.attrib,
3010 save->gs.limit, save->gs.base);
3011 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
3012 save->gdtr.selector, save->gdtr.attrib,
3013 save->gdtr.limit, save->gdtr.base);
3014 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
3015 save->ldtr.selector, save->ldtr.attrib,
3016 save->ldtr.limit, save->ldtr.base);
3017 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
3018 save->idtr.selector, save->idtr.attrib,
3019 save->idtr.limit, save->idtr.base);
3020 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
3021 save->tr.selector, save->tr.attrib,
3022 save->tr.limit, save->tr.base);
3023 pr_err("cpl: %d efer: %016llx\n",
3024 save->cpl, save->efer);
3025 pr_err("cr0: %016llx cr2: %016llx\n",
3026 save->cr0, save->cr2);
3027 pr_err("cr3: %016llx cr4: %016llx\n",
3028 save->cr3, save->cr4);
3029 pr_err("dr6: %016llx dr7: %016llx\n",
3030 save->dr6, save->dr7);
3031 pr_err("rip: %016llx rflags: %016llx\n",
3032 save->rip, save->rflags);
3033 pr_err("rsp: %016llx rax: %016llx\n",
3034 save->rsp, save->rax);
3035 pr_err("star: %016llx lstar: %016llx\n",
3036 save->star, save->lstar);
3037 pr_err("cstar: %016llx sfmask: %016llx\n",
3038 save->cstar, save->sfmask);
3039 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
3040 save->kernel_gs_base, save->sysenter_cs);
3041 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
3042 save->sysenter_esp, save->sysenter_eip);
3043 pr_err("gpat: %016llx dbgctl: %016llx\n",
3044 save->g_pat, save->dbgctl);
3045 pr_err("br_from: %016llx br_to: %016llx\n",
3046 save->br_from, save->br_to);
3047 pr_err("excp_from: %016llx excp_to: %016llx\n",
3048 save->last_excp_from, save->last_excp_to);
3049
3050}
3051
Avi Kivity586f9602010-11-18 13:09:54 +02003052static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3053{
3054 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3055
3056 *info1 = control->exit_info_1;
3057 *info2 = control->exit_info_2;
3058}
3059
Avi Kivity851ba692009-08-24 11:10:17 +03003060static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003061{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003062 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003063 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003064 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003065
Avi Kivityaa179112010-11-17 18:44:19 +02003066 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003067
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003068 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003069 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3070 if (npt_enabled)
3071 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003073 if (unlikely(svm->nested.exit_required)) {
3074 nested_svm_vmexit(svm);
3075 svm->nested.exit_required = false;
3076
3077 return 1;
3078 }
3079
Joerg Roedel20307532010-11-29 17:51:48 +01003080 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003081 int vmexit;
3082
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003083 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3084 svm->vmcb->control.exit_info_1,
3085 svm->vmcb->control.exit_info_2,
3086 svm->vmcb->control.exit_int_info,
3087 svm->vmcb->control.exit_int_info_err);
3088
Joerg Roedel410e4d52009-08-07 11:49:44 +02003089 vmexit = nested_svm_exit_special(svm);
3090
3091 if (vmexit == NESTED_EXIT_CONTINUE)
3092 vmexit = nested_svm_exit_handled(svm);
3093
3094 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003095 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003096 }
3097
Joerg Roedela5c38322009-08-07 11:49:32 +02003098 svm_complete_interrupts(svm);
3099
Avi Kivity04d2cc72007-09-10 18:10:54 +03003100 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3101 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3102 kvm_run->fail_entry.hardware_entry_failure_reason
3103 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003104 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3105 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003106 return 0;
3107 }
3108
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003109 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003110 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003111 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3112 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3114 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003115 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116 exit_code);
3117
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003118 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003119 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003120 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003121 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003122 return 0;
3123 }
3124
Avi Kivity851ba692009-08-24 11:10:17 +03003125 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003126}
3127
3128static void reload_tss(struct kvm_vcpu *vcpu)
3129{
3130 int cpu = raw_smp_processor_id();
3131
Tejun Heo0fe1e002009-10-29 22:34:14 +09003132 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3133 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 load_TR_desc();
3135}
3136
Rusty Russelle756fc62007-07-30 20:07:08 +10003137static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138{
3139 int cpu = raw_smp_processor_id();
3140
Tejun Heo0fe1e002009-10-29 22:34:14 +09003141 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003143 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003144 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003145 if (svm->asid_generation != sd->asid_generation)
3146 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147}
3148
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003149static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3150{
3151 struct vcpu_svm *svm = to_svm(vcpu);
3152
3153 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3154 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01003155 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003156 ++vcpu->stat.nmi_injections;
3157}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158
Eddie Dong85f455f2007-07-06 12:20:49 +03003159static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160{
3161 struct vmcb_control_area *control;
3162
Rusty Russelle756fc62007-07-30 20:07:08 +10003163 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003164 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 control->int_ctl &= ~V_INTR_PRIO_MASK;
3166 control->int_ctl |= V_IRQ_MASK |
3167 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
3168}
3169
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003170static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003171{
3172 struct vcpu_svm *svm = to_svm(vcpu);
3173
Joerg Roedel2af91942009-08-07 11:49:28 +02003174 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003175
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003176 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3177 ++vcpu->stat.irq_injections;
3178
Alexander Graf219b65d2009-06-15 15:21:25 +02003179 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3180 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003181}
3182
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003183static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3184{
3185 struct vcpu_svm *svm = to_svm(vcpu);
3186
Joerg Roedel20307532010-11-29 17:51:48 +01003187 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003188 return;
3189
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003190 if (irr == -1)
3191 return;
3192
3193 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003194 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003195}
3196
3197static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003198{
3199 struct vcpu_svm *svm = to_svm(vcpu);
3200 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003201 int ret;
3202 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3203 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3204 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3205
3206 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003207}
3208
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003209static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3210{
3211 struct vcpu_svm *svm = to_svm(vcpu);
3212
3213 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3214}
3215
3216static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3217{
3218 struct vcpu_svm *svm = to_svm(vcpu);
3219
3220 if (masked) {
3221 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01003222 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003223 } else {
3224 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01003225 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003226 }
3227}
3228
Gleb Natapov78646122009-03-23 12:12:11 +02003229static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3230{
3231 struct vcpu_svm *svm = to_svm(vcpu);
3232 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003233 int ret;
3234
3235 if (!gif_set(svm) ||
3236 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3237 return 0;
3238
3239 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3240
Joerg Roedel20307532010-11-29 17:51:48 +01003241 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003242 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3243
3244 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003245}
3246
Gleb Natapov9222be12009-04-23 17:14:37 +03003247static void enable_irq_window(struct kvm_vcpu *vcpu)
3248{
Alexander Graf219b65d2009-06-15 15:21:25 +02003249 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003250
Joerg Roedele0231712010-02-24 18:59:10 +01003251 /*
3252 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3253 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3254 * get that intercept, this function will be called again though and
3255 * we'll get the vintr intercept.
3256 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003257 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003258 svm_set_vintr(svm);
3259 svm_inject_irq(svm, 0x0);
3260 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003261}
3262
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003263static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003264{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003265 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003266
Gleb Natapov44c11432009-05-11 13:35:52 +03003267 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3268 == HF_NMI_MASK)
3269 return; /* IRET will cause a vm exit */
3270
Joerg Roedele0231712010-02-24 18:59:10 +01003271 /*
3272 * Something prevents NMI from been injected. Single step over possible
3273 * problem (IRET or exception injection or interrupt shadow)
3274 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003275 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003276 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3277 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003278}
3279
Izik Eiduscbc94022007-10-25 00:29:55 +02003280static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3281{
3282 return 0;
3283}
3284
Avi Kivityd9e368d2007-06-07 19:18:30 +03003285static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3286{
3287 force_new_asid(vcpu);
3288}
3289
Avi Kivity04d2cc72007-09-10 18:10:54 +03003290static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3291{
3292}
3293
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003294static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3295{
3296 struct vcpu_svm *svm = to_svm(vcpu);
3297
Joerg Roedel20307532010-11-29 17:51:48 +01003298 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003299 return;
3300
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003301 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003302 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003303 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003304 }
3305}
3306
Joerg Roedel649d6862008-04-16 16:51:15 +02003307static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3308{
3309 struct vcpu_svm *svm = to_svm(vcpu);
3310 u64 cr8;
3311
Joerg Roedel20307532010-11-29 17:51:48 +01003312 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003313 return;
3314
Joerg Roedel649d6862008-04-16 16:51:15 +02003315 cr8 = kvm_get_cr8(vcpu);
3316 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3317 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3318}
3319
Gleb Natapov9222be12009-04-23 17:14:37 +03003320static void svm_complete_interrupts(struct vcpu_svm *svm)
3321{
3322 u8 vector;
3323 int type;
3324 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003325 unsigned int3_injected = svm->int3_injected;
3326
3327 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003328
Avi Kivity3842d132010-07-27 12:30:24 +03003329 if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003330 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003331 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3332 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003333
Gleb Natapov9222be12009-04-23 17:14:37 +03003334 svm->vcpu.arch.nmi_injected = false;
3335 kvm_clear_exception_queue(&svm->vcpu);
3336 kvm_clear_interrupt_queue(&svm->vcpu);
3337
3338 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3339 return;
3340
Avi Kivity3842d132010-07-27 12:30:24 +03003341 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3342
Gleb Natapov9222be12009-04-23 17:14:37 +03003343 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3344 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3345
3346 switch (type) {
3347 case SVM_EXITINTINFO_TYPE_NMI:
3348 svm->vcpu.arch.nmi_injected = true;
3349 break;
3350 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003351 /*
3352 * In case of software exceptions, do not reinject the vector,
3353 * but re-execute the instruction instead. Rewind RIP first
3354 * if we emulated INT3 before.
3355 */
3356 if (kvm_exception_is_soft(vector)) {
3357 if (vector == BP_VECTOR && int3_injected &&
3358 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3359 kvm_rip_write(&svm->vcpu,
3360 kvm_rip_read(&svm->vcpu) -
3361 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003362 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003363 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003364 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3365 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003366 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003367
3368 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003369 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003370 break;
3371 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003372 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003373 break;
3374 default:
3375 break;
3376 }
3377}
3378
Avi Kivityb463a6f2010-07-20 15:06:17 +03003379static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3380{
3381 struct vcpu_svm *svm = to_svm(vcpu);
3382 struct vmcb_control_area *control = &svm->vmcb->control;
3383
3384 control->exit_int_info = control->event_inj;
3385 control->exit_int_info_err = control->event_inj_err;
3386 control->event_inj = 0;
3387 svm_complete_interrupts(svm);
3388}
3389
Avi Kivity80e31d42008-07-14 14:44:59 +03003390#ifdef CONFIG_X86_64
3391#define R "r"
3392#else
3393#define R "e"
3394#endif
3395
Avi Kivity851ba692009-08-24 11:10:17 +03003396static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003398 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003399
Joerg Roedel2041a062010-04-22 12:33:08 +02003400 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3401 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3402 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3403
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003404 /*
3405 * A vmexit emulation is required before the vcpu can be executed
3406 * again.
3407 */
3408 if (unlikely(svm->nested.exit_required))
3409 return;
3410
Rusty Russelle756fc62007-07-30 20:07:08 +10003411 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003412
Joerg Roedel649d6862008-04-16 16:51:15 +02003413 sync_lapic_to_cr8(vcpu);
3414
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003415 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003416
Avi Kivity04d2cc72007-09-10 18:10:54 +03003417 clgi();
3418
3419 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003420
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003422 "push %%"R"bp; \n\t"
3423 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3424 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3425 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3426 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3427 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3428 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003429#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003430 "mov %c[r8](%[svm]), %%r8 \n\t"
3431 "mov %c[r9](%[svm]), %%r9 \n\t"
3432 "mov %c[r10](%[svm]), %%r10 \n\t"
3433 "mov %c[r11](%[svm]), %%r11 \n\t"
3434 "mov %c[r12](%[svm]), %%r12 \n\t"
3435 "mov %c[r13](%[svm]), %%r13 \n\t"
3436 "mov %c[r14](%[svm]), %%r14 \n\t"
3437 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438#endif
3439
Avi Kivity6aa8b732006-12-10 02:21:36 -08003440 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003441 "push %%"R"ax \n\t"
3442 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003443 __ex(SVM_VMLOAD) "\n\t"
3444 __ex(SVM_VMRUN) "\n\t"
3445 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003446 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003447
3448 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003449 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3450 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3451 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3452 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3453 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3454 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003455#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003456 "mov %%r8, %c[r8](%[svm]) \n\t"
3457 "mov %%r9, %c[r9](%[svm]) \n\t"
3458 "mov %%r10, %c[r10](%[svm]) \n\t"
3459 "mov %%r11, %c[r11](%[svm]) \n\t"
3460 "mov %%r12, %c[r12](%[svm]) \n\t"
3461 "mov %%r13, %c[r13](%[svm]) \n\t"
3462 "mov %%r14, %c[r14](%[svm]) \n\t"
3463 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003464#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003465 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003466 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003467 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003468 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003469 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3470 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3471 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3472 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3473 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3474 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003475#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003476 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3477 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3478 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3479 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3480 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3481 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3482 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3483 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003485 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003486 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003487#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003488 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3489#endif
3490 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003491
Avi Kivity82ca2d12010-10-21 12:20:34 +02003492#ifdef CONFIG_X86_64
3493 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3494#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003495 loadsegment(fs, svm->host.fs);
Avi Kivity9581d442010-10-19 16:46:55 +02003496#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003497
3498 reload_tss(vcpu);
3499
Avi Kivity56ba47d2007-11-07 17:14:18 +02003500 local_irq_disable();
3501
3502 stgi();
3503
Avi Kivity13c34e02010-10-21 12:20:31 +02003504 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3505 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3506 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3507 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3508
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003509 sync_cr8_to_lapic(vcpu);
3510
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003511 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003512
Gleb Natapov631bc482010-10-14 11:22:52 +02003513 /* if exit due to PF check for async PF */
3514 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3515 svm->apf_reason = kvm_read_and_reset_pf_reason();
3516
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003517 if (npt_enabled) {
3518 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3519 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3520 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003521
3522 /*
3523 * We need to handle MC intercepts here before the vcpu has a chance to
3524 * change the physical cpu
3525 */
3526 if (unlikely(svm->vmcb->control.exit_code ==
3527 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3528 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003529
3530 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531}
3532
Avi Kivity80e31d42008-07-14 14:44:59 +03003533#undef R
3534
Avi Kivity6aa8b732006-12-10 02:21:36 -08003535static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3536{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003537 struct vcpu_svm *svm = to_svm(vcpu);
3538
3539 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540 force_new_asid(vcpu);
3541}
3542
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003543static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3544{
3545 struct vcpu_svm *svm = to_svm(vcpu);
3546
3547 svm->vmcb->control.nested_cr3 = root;
3548
3549 /* Also sync guest cr3 here in case we live migrate */
3550 svm->vmcb->save.cr3 = vcpu->arch.cr3;
3551
3552 force_new_asid(vcpu);
3553}
3554
Avi Kivity6aa8b732006-12-10 02:21:36 -08003555static int is_disabled(void)
3556{
Joerg Roedel6031a612007-06-22 12:29:50 +03003557 u64 vm_cr;
3558
3559 rdmsrl(MSR_VM_CR, vm_cr);
3560 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3561 return 1;
3562
Avi Kivity6aa8b732006-12-10 02:21:36 -08003563 return 0;
3564}
3565
Ingo Molnar102d8322007-02-19 14:37:47 +02003566static void
3567svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3568{
3569 /*
3570 * Patch in the VMMCALL instruction:
3571 */
3572 hypercall[0] = 0x0f;
3573 hypercall[1] = 0x01;
3574 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003575}
3576
Yang, Sheng002c7f72007-07-31 14:23:01 +03003577static void svm_check_processor_compat(void *rtn)
3578{
3579 *(int *)rtn = 0;
3580}
3581
Avi Kivity774ead32007-12-26 13:57:04 +02003582static bool svm_cpu_has_accelerated_tpr(void)
3583{
3584 return false;
3585}
3586
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003587static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003588{
3589 return 0;
3590}
3591
Sheng Yang0e851882009-12-18 16:48:46 +08003592static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3593{
3594}
3595
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003596static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3597{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003598 switch (func) {
Joerg Roedel24d1b152010-12-07 17:15:05 +01003599 case 0x00000001:
3600 /* Mask out xsave bit as long as it is not supported by SVM */
3601 entry->ecx &= ~(bit(X86_FEATURE_XSAVE));
3602 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02003603 case 0x80000001:
3604 if (nested)
3605 entry->ecx |= (1 << 2); /* Set SVM bit */
3606 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003607 case 0x8000000A:
3608 entry->eax = 1; /* SVM revision 1 */
3609 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3610 ASID emulation to nested SVM */
3611 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003612 entry->edx = 0; /* Per default do not support any
3613 additional features */
3614
3615 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003616 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02003617 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003618
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02003619 /* Support NPT for the guest if enabled */
3620 if (npt_enabled)
3621 entry->edx |= SVM_FEATURE_NPT;
3622
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003623 break;
3624 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003625}
3626
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003627static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003628 { SVM_EXIT_READ_CR0, "read_cr0" },
3629 { SVM_EXIT_READ_CR3, "read_cr3" },
3630 { SVM_EXIT_READ_CR4, "read_cr4" },
3631 { SVM_EXIT_READ_CR8, "read_cr8" },
3632 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3633 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3634 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3635 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3636 { SVM_EXIT_READ_DR0, "read_dr0" },
3637 { SVM_EXIT_READ_DR1, "read_dr1" },
3638 { SVM_EXIT_READ_DR2, "read_dr2" },
3639 { SVM_EXIT_READ_DR3, "read_dr3" },
3640 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3641 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3642 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3643 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3644 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3645 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003646 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3647 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3648 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3649 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3650 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3651 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3652 { SVM_EXIT_INTR, "interrupt" },
3653 { SVM_EXIT_NMI, "nmi" },
3654 { SVM_EXIT_SMI, "smi" },
3655 { SVM_EXIT_INIT, "init" },
3656 { SVM_EXIT_VINTR, "vintr" },
3657 { SVM_EXIT_CPUID, "cpuid" },
3658 { SVM_EXIT_INVD, "invd" },
3659 { SVM_EXIT_HLT, "hlt" },
3660 { SVM_EXIT_INVLPG, "invlpg" },
3661 { SVM_EXIT_INVLPGA, "invlpga" },
3662 { SVM_EXIT_IOIO, "io" },
3663 { SVM_EXIT_MSR, "msr" },
3664 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3665 { SVM_EXIT_SHUTDOWN, "shutdown" },
3666 { SVM_EXIT_VMRUN, "vmrun" },
3667 { SVM_EXIT_VMMCALL, "hypercall" },
3668 { SVM_EXIT_VMLOAD, "vmload" },
3669 { SVM_EXIT_VMSAVE, "vmsave" },
3670 { SVM_EXIT_STGI, "stgi" },
3671 { SVM_EXIT_CLGI, "clgi" },
3672 { SVM_EXIT_SKINIT, "skinit" },
3673 { SVM_EXIT_WBINVD, "wbinvd" },
3674 { SVM_EXIT_MONITOR, "monitor" },
3675 { SVM_EXIT_MWAIT, "mwait" },
3676 { SVM_EXIT_NPF, "npf" },
3677 { -1, NULL }
3678};
3679
Sheng Yang17cc3932010-01-05 19:02:27 +08003680static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003681{
Sheng Yang17cc3932010-01-05 19:02:27 +08003682 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003683}
3684
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003685static bool svm_rdtscp_supported(void)
3686{
3687 return false;
3688}
3689
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003690static bool svm_has_wbinvd_exit(void)
3691{
3692 return true;
3693}
3694
Avi Kivity02daab22009-12-30 12:40:26 +02003695static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3696{
3697 struct vcpu_svm *svm = to_svm(vcpu);
3698
Joerg Roedel18c918c2010-11-30 18:03:59 +01003699 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01003700 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003701}
3702
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003703static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003704 .cpu_has_kvm_support = has_svm,
3705 .disabled_by_bios = is_disabled,
3706 .hardware_setup = svm_hardware_setup,
3707 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003708 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003709 .hardware_enable = svm_hardware_enable,
3710 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003711 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712
3713 .vcpu_create = svm_create_vcpu,
3714 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003715 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003716
Avi Kivity04d2cc72007-09-10 18:10:54 +03003717 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718 .vcpu_load = svm_vcpu_load,
3719 .vcpu_put = svm_vcpu_put,
3720
3721 .set_guest_debug = svm_guest_debug,
3722 .get_msr = svm_get_msr,
3723 .set_msr = svm_set_msr,
3724 .get_segment_base = svm_get_segment_base,
3725 .get_segment = svm_get_segment,
3726 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003727 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003728 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003729 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003730 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003731 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003732 .set_cr3 = svm_set_cr3,
3733 .set_cr4 = svm_set_cr4,
3734 .set_efer = svm_set_efer,
3735 .get_idt = svm_get_idt,
3736 .set_idt = svm_set_idt,
3737 .get_gdt = svm_get_gdt,
3738 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003739 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003740 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003741 .get_rflags = svm_get_rflags,
3742 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003743 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003744 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003745
Avi Kivity6aa8b732006-12-10 02:21:36 -08003746 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003747
Avi Kivity6aa8b732006-12-10 02:21:36 -08003748 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003749 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003750 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003751 .set_interrupt_shadow = svm_set_interrupt_shadow,
3752 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003753 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003754 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003755 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003756 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03003757 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02003758 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003759 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003760 .get_nmi_mask = svm_get_nmi_mask,
3761 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003762 .enable_nmi_window = enable_nmi_window,
3763 .enable_irq_window = enable_irq_window,
3764 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003765
3766 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003767 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003768 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003769
Avi Kivity586f9602010-11-18 13:09:54 +02003770 .get_exit_info = svm_get_exit_info,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003771 .exit_reasons_str = svm_exit_reasons_str,
Avi Kivity586f9602010-11-18 13:09:54 +02003772
Sheng Yang17cc3932010-01-05 19:02:27 +08003773 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003774
3775 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003776
3777 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003778
3779 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003780
3781 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10003782
3783 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10003784 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003785
3786 .set_tdp_cr3 = set_tdp_cr3,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003787};
3788
3789static int __init svm_init(void)
3790{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003791 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003792 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003793}
3794
3795static void __exit svm_exit(void)
3796{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003797 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003798}
3799
3800module_init(svm_init)
3801module_exit(svm_exit)