blob: ab39d84dee0037281dba48d942c4c6e1e894768f [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)
Andre Przywaraddce97a2010-12-21 11:12:03 +010054#define SVM_FEATURE_TSC_RATE (1 << 4)
55#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
56#define SVM_FEATURE_FLUSH_ASID (1 << 6)
57#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020058#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030059
Joerg Roedel410e4d52009-08-07 11:49:44 +020060#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
61#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
62#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
63
Joerg Roedel24e09cb2008-02-13 18:58:47 +010064#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
65
Joerg Roedelfbc0db72011-03-25 09:44:46 +010066#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010067#define TSC_RATIO_MIN 0x0000000000000001ULL
68#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010069
Joerg Roedel67ec6602010-05-17 14:43:35 +020070static bool erratum_383_found __read_mostly;
71
Avi Kivity6c8166a2009-05-31 18:15:37 +030072static const u32 host_save_user_msrs[] = {
73#ifdef CONFIG_X86_64
74 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
75 MSR_FS_BASE,
76#endif
77 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
78};
79
80#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
81
82struct kvm_vcpu;
83
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020084struct nested_state {
85 struct vmcb *hsave;
86 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010087 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020088 u64 vmcb;
89
90 /* These are the merged vectors */
91 u32 *msrpm;
92
93 /* gpa pointers to the real vectors */
94 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +010095 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020096
Joerg Roedelcd3ff652009-10-09 16:08:26 +020097 /* A VMEXIT is required but not yet emulated */
98 bool exit_required;
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
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500113/*
114 * Set osvw_len to higher value when updated Revision Guides
115 * are published and we know what the new status bits are
116 */
117static uint64_t osvw_len = 4, osvw_status;
118
Avi Kivity6c8166a2009-05-31 18:15:37 +0300119struct vcpu_svm {
120 struct kvm_vcpu vcpu;
121 struct vmcb *vmcb;
122 unsigned long vmcb_pa;
123 struct svm_cpu_data *svm_data;
124 uint64_t asid_generation;
125 uint64_t sysenter_esp;
126 uint64_t sysenter_eip;
127
128 u64 next_rip;
129
130 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200131 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200132 u16 fs;
133 u16 gs;
134 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200135 u64 gs_base;
136 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300137
138 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300139
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200140 ulong nmi_iret_rip;
141
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200142 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200143
144 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100145
146 unsigned int3_injected;
147 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200148 u32 apf_reason;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100149
150 u64 tsc_ratio;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300151};
152
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100153static DEFINE_PER_CPU(u64, current_tsc_ratio);
154#define TSC_RATIO_DEFAULT 0x0100000000ULL
155
Joerg Roedel455716f2010-03-01 15:34:35 +0100156#define MSR_INVALID 0xffffffffU
157
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100158static struct svm_direct_access_msrs {
159 u32 index; /* Index of the MSR */
160 bool always; /* True if intercept is always on */
161} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400162 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100163 { .index = MSR_IA32_SYSENTER_CS, .always = true },
164#ifdef CONFIG_X86_64
165 { .index = MSR_GS_BASE, .always = true },
166 { .index = MSR_FS_BASE, .always = true },
167 { .index = MSR_KERNEL_GS_BASE, .always = true },
168 { .index = MSR_LSTAR, .always = true },
169 { .index = MSR_CSTAR, .always = true },
170 { .index = MSR_SYSCALL_MASK, .always = true },
171#endif
172 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
173 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
174 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
175 { .index = MSR_IA32_LASTINTTOIP, .always = false },
176 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177};
178
179/* enable NPT for AMD64 and X86 with PAE */
180#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
181static bool npt_enabled = true;
182#else
Joerg Roedele0231712010-02-24 18:59:10 +0100183static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184#endif
185
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100186/* allow nested paging (virtualized MMU) for all guests */
187static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188module_param(npt, int, S_IRUGO);
189
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100190/* allow nested virtualization in KVM/SVM */
191static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192module_param(nested, int, S_IRUGO);
193
194static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200195static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196
Joerg Roedel410e4d52009-08-07 11:49:44 +0200197static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100198static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
201 bool has_error_code, u32 error_code);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100202static u64 __scale_tsc(u64 ratio, u64 tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100204enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100205 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
206 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100207 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100208 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100209 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100210 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100211 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100212 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100213 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100214 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100215 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100216 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100217 VMCB_DIRTY_MAX,
218};
219
Joerg Roedel0574dec2010-12-03 11:45:58 +0100220/* TPR and CR2 are always written before VMRUN */
221#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100222
223static inline void mark_all_dirty(struct vmcb *vmcb)
224{
225 vmcb->control.clean = 0;
226}
227
228static inline void mark_all_clean(struct vmcb *vmcb)
229{
230 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
231 & ~VMCB_ALWAYS_DIRTY_MASK;
232}
233
234static inline void mark_dirty(struct vmcb *vmcb, int bit)
235{
236 vmcb->control.clean &= ~(1 << bit);
237}
238
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
240{
241 return container_of(vcpu, struct vcpu_svm, vcpu);
242}
243
Joerg Roedel384c6362010-11-30 18:03:56 +0100244static void recalc_intercepts(struct vcpu_svm *svm)
245{
246 struct vmcb_control_area *c, *h;
247 struct nested_state *g;
248
Joerg Roedel116a0a22010-12-03 11:45:49 +0100249 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
250
Joerg Roedel384c6362010-11-30 18:03:56 +0100251 if (!is_guest_mode(&svm->vcpu))
252 return;
253
254 c = &svm->vmcb->control;
255 h = &svm->nested.hsave->control;
256 g = &svm->nested;
257
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100258 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100259 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100260 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
261 c->intercept = h->intercept | g->intercept;
262}
263
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100264static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
265{
266 if (is_guest_mode(&svm->vcpu))
267 return svm->nested.hsave;
268 else
269 return svm->vmcb;
270}
271
272static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
273{
274 struct vmcb *vmcb = get_host_vmcb(svm);
275
276 vmcb->control.intercept_cr |= (1U << bit);
277
278 recalc_intercepts(svm);
279}
280
281static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
282{
283 struct vmcb *vmcb = get_host_vmcb(svm);
284
285 vmcb->control.intercept_cr &= ~(1U << bit);
286
287 recalc_intercepts(svm);
288}
289
290static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
291{
292 struct vmcb *vmcb = get_host_vmcb(svm);
293
294 return vmcb->control.intercept_cr & (1U << bit);
295}
296
Joerg Roedel3aed0412010-11-30 18:03:58 +0100297static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
298{
299 struct vmcb *vmcb = get_host_vmcb(svm);
300
301 vmcb->control.intercept_dr |= (1U << bit);
302
303 recalc_intercepts(svm);
304}
305
306static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
307{
308 struct vmcb *vmcb = get_host_vmcb(svm);
309
310 vmcb->control.intercept_dr &= ~(1U << bit);
311
312 recalc_intercepts(svm);
313}
314
Joerg Roedel18c918c2010-11-30 18:03:59 +0100315static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
316{
317 struct vmcb *vmcb = get_host_vmcb(svm);
318
319 vmcb->control.intercept_exceptions |= (1U << bit);
320
321 recalc_intercepts(svm);
322}
323
324static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
325{
326 struct vmcb *vmcb = get_host_vmcb(svm);
327
328 vmcb->control.intercept_exceptions &= ~(1U << bit);
329
330 recalc_intercepts(svm);
331}
332
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100333static inline void set_intercept(struct vcpu_svm *svm, int bit)
334{
335 struct vmcb *vmcb = get_host_vmcb(svm);
336
337 vmcb->control.intercept |= (1ULL << bit);
338
339 recalc_intercepts(svm);
340}
341
342static inline void clr_intercept(struct vcpu_svm *svm, int bit)
343{
344 struct vmcb *vmcb = get_host_vmcb(svm);
345
346 vmcb->control.intercept &= ~(1ULL << bit);
347
348 recalc_intercepts(svm);
349}
350
Joerg Roedel2af91942009-08-07 11:49:28 +0200351static inline void enable_gif(struct vcpu_svm *svm)
352{
353 svm->vcpu.arch.hflags |= HF_GIF_MASK;
354}
355
356static inline void disable_gif(struct vcpu_svm *svm)
357{
358 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
359}
360
361static inline bool gif_set(struct vcpu_svm *svm)
362{
363 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
364}
365
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366static unsigned long iopm_base;
367
368struct kvm_ldttss_desc {
369 u16 limit0;
370 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100371 unsigned base1:8, type:5, dpl:2, p:1;
372 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373 u32 base3;
374 u32 zero1;
375} __attribute__((packed));
376
377struct svm_cpu_data {
378 int cpu;
379
Avi Kivity5008fdf2007-04-02 13:05:50 +0300380 u64 asid_generation;
381 u32 max_asid;
382 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383 struct kvm_ldttss_desc *tss_desc;
384
385 struct page *save_area;
386};
387
388static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
389
390struct svm_init_data {
391 int cpu;
392 int r;
393};
394
395static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
396
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200397#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398#define MSRS_RANGE_SIZE 2048
399#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
400
Joerg Roedel455716f2010-03-01 15:34:35 +0100401static u32 svm_msrpm_offset(u32 msr)
402{
403 u32 offset;
404 int i;
405
406 for (i = 0; i < NUM_MSR_MAPS; i++) {
407 if (msr < msrpm_ranges[i] ||
408 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
409 continue;
410
411 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
412 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
413
414 /* Now we have the u8 offset - but need the u32 offset */
415 return offset / 4;
416 }
417
418 /* MSR not in any range */
419 return MSR_INVALID;
420}
421
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422#define MAX_INST_SIZE 15
423
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424static inline void clgi(void)
425{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300426 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427}
428
429static inline void stgi(void)
430{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300431 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432}
433
434static inline void invlpga(unsigned long addr, u32 asid)
435{
Joerg Roedele0231712010-02-24 18:59:10 +0100436 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437}
438
Joerg Roedel4b161842010-09-10 17:31:03 +0200439static int get_npt_level(void)
440{
441#ifdef CONFIG_X86_64
442 return PT64_ROOT_LEVEL;
443#else
444 return PT32E_ROOT_LEVEL;
445#endif
446}
447
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
449{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000450 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100451 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600452 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453
Alexander Graf9962d032008-11-25 20:17:02 +0100454 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100455 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456}
457
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458static int is_external_interrupt(u32 info)
459{
460 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
461 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
462}
463
Glauber Costa2809f5d2009-05-12 16:21:05 -0400464static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
465{
466 struct vcpu_svm *svm = to_svm(vcpu);
467 u32 ret = 0;
468
469 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100470 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400471 return ret & mask;
472}
473
474static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
475{
476 struct vcpu_svm *svm = to_svm(vcpu);
477
478 if (mask == 0)
479 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
480 else
481 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
482
483}
484
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
486{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400487 struct vcpu_svm *svm = to_svm(vcpu);
488
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200489 if (svm->vmcb->control.next_rip != 0)
490 svm->next_rip = svm->vmcb->control.next_rip;
491
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400492 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100493 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300494 EMULATE_DONE)
495 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 return;
497 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300498 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
499 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
500 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300502 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400503 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504}
505
Jan Kiszka116a4752010-02-23 17:47:54 +0100506static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200507 bool has_error_code, u32 error_code,
508 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100509{
510 struct vcpu_svm *svm = to_svm(vcpu);
511
Joerg Roedele0231712010-02-24 18:59:10 +0100512 /*
513 * If we are within a nested VM we'd better #VMEXIT and let the guest
514 * handle the exception
515 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200516 if (!reinject &&
517 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100518 return;
519
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200520 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100521 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
522
523 /*
524 * For guest debugging where we have to reinject #BP if some
525 * INT3 is guest-owned:
526 * Emulate nRIP by moving RIP forward. Will fail if injection
527 * raises a fault that is not intercepted. Still better than
528 * failing in all cases.
529 */
530 skip_emulated_instruction(&svm->vcpu);
531 rip = kvm_rip_read(&svm->vcpu);
532 svm->int3_rip = rip + svm->vmcb->save.cs.base;
533 svm->int3_injected = rip - old_rip;
534 }
535
Jan Kiszka116a4752010-02-23 17:47:54 +0100536 svm->vmcb->control.event_inj = nr
537 | SVM_EVTINJ_VALID
538 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
539 | SVM_EVTINJ_TYPE_EXEPT;
540 svm->vmcb->control.event_inj_err = error_code;
541}
542
Joerg Roedel67ec6602010-05-17 14:43:35 +0200543static void svm_init_erratum_383(void)
544{
545 u32 low, high;
546 int err;
547 u64 val;
548
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200549 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200550 return;
551
552 /* Use _safe variants to not break nested virtualization */
553 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
554 if (err)
555 return;
556
557 val |= (1ULL << 47);
558
559 low = lower_32_bits(val);
560 high = upper_32_bits(val);
561
562 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
563
564 erratum_383_found = true;
565}
566
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500567static void svm_init_osvw(struct kvm_vcpu *vcpu)
568{
569 /*
570 * Guests should see errata 400 and 415 as fixed (assuming that
571 * HLT and IO instructions are intercepted).
572 */
573 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
574 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
575
576 /*
577 * By increasing VCPU's osvw.length to 3 we are telling the guest that
578 * all osvw.status bits inside that length, including bit 0 (which is
579 * reserved for erratum 298), are valid. However, if host processor's
580 * osvw_len is 0 then osvw_status[0] carries no information. We need to
581 * be conservative here and therefore we tell the guest that erratum 298
582 * is present (because we really don't know).
583 */
584 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
585 vcpu->arch.osvw.status |= 1;
586}
587
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588static int has_svm(void)
589{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200590 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591
Eduardo Habkost63d11422008-11-17 19:03:20 -0200592 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800593 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594 return 0;
595 }
596
Avi Kivity6aa8b732006-12-10 02:21:36 -0800597 return 1;
598}
599
600static void svm_hardware_disable(void *garbage)
601{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100602 /* Make sure we clean up behind us */
603 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
604 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
605
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200606 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607}
608
Alexander Graf10474ae2009-09-15 11:37:46 +0200609static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800610{
611
Tejun Heo0fe1e002009-10-29 22:34:14 +0900612 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200614 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615 struct desc_struct *gdt;
616 int me = raw_smp_processor_id();
617
Alexander Graf10474ae2009-09-15 11:37:46 +0200618 rdmsrl(MSR_EFER, efer);
619 if (efer & EFER_SVME)
620 return -EBUSY;
621
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000623 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
624 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200625 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900627 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628
Tejun Heo0fe1e002009-10-29 22:34:14 +0900629 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000630 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200632 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633 }
634
Tejun Heo0fe1e002009-10-29 22:34:14 +0900635 sd->asid_generation = 1;
636 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
637 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200639 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200640 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900641 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642
Alexander Graf9962d032008-11-25 20:17:02 +0100643 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644
Linus Torvaldsd0316552009-12-14 09:58:24 -0800645 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200646
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100647 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
648 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
649 __get_cpu_var(current_tsc_ratio) = TSC_RATIO_DEFAULT;
650 }
651
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500652
653 /*
654 * Get OSVW bits.
655 *
656 * Note that it is possible to have a system with mixed processor
657 * revisions and therefore different OSVW bits. If bits are not the same
658 * on different processors then choose the worst case (i.e. if erratum
659 * is present on one processor and not on another then assume that the
660 * erratum is present everywhere).
661 */
662 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
663 uint64_t len, status = 0;
664 int err;
665
666 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
667 if (!err)
668 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
669 &err);
670
671 if (err)
672 osvw_status = osvw_len = 0;
673 else {
674 if (len < osvw_len)
675 osvw_len = len;
676 osvw_status |= status;
677 osvw_status &= (1ULL << osvw_len) - 1;
678 }
679 } else
680 osvw_status = osvw_len = 0;
681
Joerg Roedel67ec6602010-05-17 14:43:35 +0200682 svm_init_erratum_383();
683
Alexander Graf10474ae2009-09-15 11:37:46 +0200684 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685}
686
Joerg Roedel0da1db752008-07-02 16:02:11 +0200687static void svm_cpu_uninit(int cpu)
688{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900689 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200690
Tejun Heo0fe1e002009-10-29 22:34:14 +0900691 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200692 return;
693
694 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900695 __free_page(sd->save_area);
696 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200697}
698
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699static int svm_cpu_init(int cpu)
700{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900701 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702 int r;
703
Tejun Heo0fe1e002009-10-29 22:34:14 +0900704 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
705 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900707 sd->cpu = cpu;
708 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900710 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711 goto err_1;
712
Tejun Heo0fe1e002009-10-29 22:34:14 +0900713 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714
715 return 0;
716
717err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900718 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719 return r;
720
721}
722
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100723static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724{
725 int i;
726
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100727 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
728 if (direct_access_msrs[i].index == index)
729 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100731 return false;
732}
733
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734static void set_msr_interception(u32 *msrpm, unsigned msr,
735 int read, int write)
736{
Joerg Roedel455716f2010-03-01 15:34:35 +0100737 u8 bit_read, bit_write;
738 unsigned long tmp;
739 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100741 /*
742 * If this warning triggers extend the direct_access_msrs list at the
743 * beginning of the file
744 */
745 WARN_ON(!valid_msr_intercept(msr));
746
Joerg Roedel455716f2010-03-01 15:34:35 +0100747 offset = svm_msrpm_offset(msr);
748 bit_read = 2 * (msr & 0x0f);
749 bit_write = 2 * (msr & 0x0f) + 1;
750 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751
Joerg Roedel455716f2010-03-01 15:34:35 +0100752 BUG_ON(offset == MSR_INVALID);
753
754 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
755 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
756
757 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758}
759
Joerg Roedelf65c2292008-02-13 18:58:46 +0100760static void svm_vcpu_init_msrpm(u32 *msrpm)
761{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100762 int i;
763
Joerg Roedelf65c2292008-02-13 18:58:46 +0100764 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
765
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100766 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
767 if (!direct_access_msrs[i].always)
768 continue;
769
770 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
771 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100772}
773
Joerg Roedel323c3d82010-03-01 15:34:37 +0100774static void add_msr_offset(u32 offset)
775{
776 int i;
777
778 for (i = 0; i < MSRPM_OFFSETS; ++i) {
779
780 /* Offset already in list? */
781 if (msrpm_offsets[i] == offset)
782 return;
783
784 /* Slot used by another offset? */
785 if (msrpm_offsets[i] != MSR_INVALID)
786 continue;
787
788 /* Add offset to list */
789 msrpm_offsets[i] = offset;
790
791 return;
792 }
793
794 /*
795 * If this BUG triggers the msrpm_offsets table has an overflow. Just
796 * increase MSRPM_OFFSETS in this case.
797 */
798 BUG();
799}
800
801static void init_msrpm_offsets(void)
802{
803 int i;
804
805 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
806
807 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
808 u32 offset;
809
810 offset = svm_msrpm_offset(direct_access_msrs[i].index);
811 BUG_ON(offset == MSR_INVALID);
812
813 add_msr_offset(offset);
814 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815}
816
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100817static void svm_enable_lbrv(struct vcpu_svm *svm)
818{
819 u32 *msrpm = svm->msrpm;
820
821 svm->vmcb->control.lbr_ctl = 1;
822 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
823 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
824 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
825 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
826}
827
828static void svm_disable_lbrv(struct vcpu_svm *svm)
829{
830 u32 *msrpm = svm->msrpm;
831
832 svm->vmcb->control.lbr_ctl = 0;
833 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
834 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
835 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
836 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
837}
838
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839static __init int svm_hardware_setup(void)
840{
841 int cpu;
842 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100843 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844 int r;
845
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
847
848 if (!iopm_pages)
849 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300850
851 iopm_va = page_address(iopm_pages);
852 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
854
Joerg Roedel323c3d82010-03-01 15:34:37 +0100855 init_msrpm_offsets();
856
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100857 if (boot_cpu_has(X86_FEATURE_NX))
858 kvm_enable_efer_bits(EFER_NX);
859
Alexander Graf1b2fd702009-02-02 16:23:51 +0100860 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
861 kvm_enable_efer_bits(EFER_FFXSR);
862
Joerg Roedel92a1f122011-03-25 09:44:51 +0100863 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
864 u64 max;
865
866 kvm_has_tsc_control = true;
867
868 /*
869 * Make sure the user can only configure tsc_khz values that
870 * fit into a signed integer.
871 * A min value is not calculated needed because it will always
872 * be 1 on all machines and a value of 0 is used to disable
873 * tsc-scaling for the vcpu.
874 */
875 max = min(0x7fffffffULL, __scale_tsc(tsc_khz, TSC_RATIO_MAX));
876
877 kvm_max_guest_tsc_khz = max;
878 }
879
Alexander Graf236de052008-11-25 20:17:10 +0100880 if (nested) {
881 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200882 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100883 }
884
Zachary Amsden3230bb42009-09-29 11:38:37 -1000885 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886 r = svm_cpu_init(cpu);
887 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100888 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100890
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200891 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100892 npt_enabled = false;
893
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100894 if (npt_enabled && !npt) {
895 printk(KERN_INFO "kvm: Nested Paging disabled\n");
896 npt_enabled = false;
897 }
898
Joerg Roedel18552672008-02-07 13:47:41 +0100899 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100900 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100901 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200902 } else
903 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100904
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 return 0;
906
Joerg Roedelf65c2292008-02-13 18:58:46 +0100907err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
909 iopm_base = 0;
910 return r;
911}
912
913static __exit void svm_hardware_unsetup(void)
914{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200915 int cpu;
916
Zachary Amsden3230bb42009-09-29 11:38:37 -1000917 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200918 svm_cpu_uninit(cpu);
919
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100921 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922}
923
924static void init_seg(struct vmcb_seg *seg)
925{
926 seg->selector = 0;
927 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100928 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 seg->limit = 0xffff;
930 seg->base = 0;
931}
932
933static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
934{
935 seg->selector = 0;
936 seg->attrib = SVM_SELECTOR_P_MASK | type;
937 seg->limit = 0xffff;
938 seg->base = 0;
939}
940
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100941static u64 __scale_tsc(u64 ratio, u64 tsc)
942{
943 u64 mult, frac, _tsc;
944
945 mult = ratio >> 32;
946 frac = ratio & ((1ULL << 32) - 1);
947
948 _tsc = tsc;
949 _tsc *= mult;
950 _tsc += (tsc >> 32) * frac;
951 _tsc += ((tsc & ((1ULL << 32) - 1)) * frac) >> 32;
952
953 return _tsc;
954}
955
956static u64 svm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
957{
958 struct vcpu_svm *svm = to_svm(vcpu);
959 u64 _tsc = tsc;
960
961 if (svm->tsc_ratio != TSC_RATIO_DEFAULT)
962 _tsc = __scale_tsc(svm->tsc_ratio, tsc);
963
964 return _tsc;
965}
966
Zachary Amsdencc578282012-02-03 15:43:50 -0200967static void svm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +0100968{
969 struct vcpu_svm *svm = to_svm(vcpu);
970 u64 ratio;
971 u64 khz;
972
Zachary Amsdencc578282012-02-03 15:43:50 -0200973 /* Guest TSC same frequency as host TSC? */
974 if (!scale) {
Joerg Roedel4051b182011-03-25 09:44:49 +0100975 svm->tsc_ratio = TSC_RATIO_DEFAULT;
976 return;
977 }
978
Zachary Amsdencc578282012-02-03 15:43:50 -0200979 /* TSC scaling supported? */
980 if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
981 if (user_tsc_khz > tsc_khz) {
982 vcpu->arch.tsc_catchup = 1;
983 vcpu->arch.tsc_always_catchup = 1;
984 } else
985 WARN(1, "user requested TSC rate below hardware speed\n");
986 return;
987 }
988
Joerg Roedel4051b182011-03-25 09:44:49 +0100989 khz = user_tsc_khz;
990
991 /* TSC scaling required - calculate ratio */
992 ratio = khz << 32;
993 do_div(ratio, tsc_khz);
994
995 if (ratio == 0 || ratio & TSC_RATIO_RSVD) {
996 WARN_ONCE(1, "Invalid TSC ratio - virtual-tsc-khz=%u\n",
997 user_tsc_khz);
998 return;
999 }
Joerg Roedel4051b182011-03-25 09:44:49 +01001000 svm->tsc_ratio = ratio;
1001}
1002
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001003static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1004{
1005 struct vcpu_svm *svm = to_svm(vcpu);
1006 u64 g_tsc_offset = 0;
1007
Joerg Roedel20307532010-11-29 17:51:48 +01001008 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001009 g_tsc_offset = svm->vmcb->control.tsc_offset -
1010 svm->nested.hsave->control.tsc_offset;
1011 svm->nested.hsave->control.tsc_offset = offset;
1012 }
1013
1014 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001015
1016 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001017}
1018
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001019static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10001020{
1021 struct vcpu_svm *svm = to_svm(vcpu);
1022
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001023 WARN_ON(adjustment < 0);
1024 if (host)
1025 adjustment = svm_scale_tsc(vcpu, adjustment);
1026
Zachary Amsdene48672f2010-08-19 22:07:23 -10001027 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +01001028 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -10001029 svm->nested.hsave->control.tsc_offset += adjustment;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001030 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001031}
1032
Joerg Roedel857e4092011-03-25 09:44:50 +01001033static u64 svm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1034{
1035 u64 tsc;
1036
1037 tsc = svm_scale_tsc(vcpu, native_read_tsc());
1038
1039 return target_tsc - tsc;
1040}
1041
Joerg Roedele6101a92008-02-13 18:58:45 +01001042static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043{
Joerg Roedele6101a92008-02-13 18:58:45 +01001044 struct vmcb_control_area *control = &svm->vmcb->control;
1045 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046
Avi Kivitybff78272010-01-07 13:16:08 +02001047 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001048 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001049
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001050 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1051 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1052 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1053 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1054 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1055 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
1056 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057
Joerg Roedel3aed0412010-11-30 18:03:58 +01001058 set_dr_intercept(svm, INTERCEPT_DR0_READ);
1059 set_dr_intercept(svm, INTERCEPT_DR1_READ);
1060 set_dr_intercept(svm, INTERCEPT_DR2_READ);
1061 set_dr_intercept(svm, INTERCEPT_DR3_READ);
1062 set_dr_intercept(svm, INTERCEPT_DR4_READ);
1063 set_dr_intercept(svm, INTERCEPT_DR5_READ);
1064 set_dr_intercept(svm, INTERCEPT_DR6_READ);
1065 set_dr_intercept(svm, INTERCEPT_DR7_READ);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066
Joerg Roedel3aed0412010-11-30 18:03:58 +01001067 set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
1068 set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
1069 set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
1070 set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
1071 set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
1072 set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
1073 set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
1074 set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075
Joerg Roedel18c918c2010-11-30 18:03:59 +01001076 set_exception_intercept(svm, PF_VECTOR);
1077 set_exception_intercept(svm, UD_VECTOR);
1078 set_exception_intercept(svm, MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001080 set_intercept(svm, INTERCEPT_INTR);
1081 set_intercept(svm, INTERCEPT_NMI);
1082 set_intercept(svm, INTERCEPT_SMI);
1083 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001084 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001085 set_intercept(svm, INTERCEPT_CPUID);
1086 set_intercept(svm, INTERCEPT_INVD);
1087 set_intercept(svm, INTERCEPT_HLT);
1088 set_intercept(svm, INTERCEPT_INVLPG);
1089 set_intercept(svm, INTERCEPT_INVLPGA);
1090 set_intercept(svm, INTERCEPT_IOIO_PROT);
1091 set_intercept(svm, INTERCEPT_MSR_PROT);
1092 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1093 set_intercept(svm, INTERCEPT_SHUTDOWN);
1094 set_intercept(svm, INTERCEPT_VMRUN);
1095 set_intercept(svm, INTERCEPT_VMMCALL);
1096 set_intercept(svm, INTERCEPT_VMLOAD);
1097 set_intercept(svm, INTERCEPT_VMSAVE);
1098 set_intercept(svm, INTERCEPT_STGI);
1099 set_intercept(svm, INTERCEPT_CLGI);
1100 set_intercept(svm, INTERCEPT_SKINIT);
1101 set_intercept(svm, INTERCEPT_WBINVD);
1102 set_intercept(svm, INTERCEPT_MONITOR);
1103 set_intercept(svm, INTERCEPT_MWAIT);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001104 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105
1106 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001107 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 control->int_ctl = V_INTR_MASKING_MASK;
1109
1110 init_seg(&save->es);
1111 init_seg(&save->ss);
1112 init_seg(&save->ds);
1113 init_seg(&save->fs);
1114 init_seg(&save->gs);
1115
1116 save->cs.selector = 0xf000;
1117 /* Executable/Readable Code Segment */
1118 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1119 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1120 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -08001121 /*
1122 * cs.base should really be 0xffff0000, but vmx can't handle that, so
1123 * be consistent with it.
1124 *
1125 * Replace when we have real mode working for vmx.
1126 */
1127 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128
1129 save->gdtr.limit = 0xffff;
1130 save->idtr.limit = 0xffff;
1131
1132 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1133 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1134
Marcelo Tosattieaa48512010-08-31 19:13:14 -03001135 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001136 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 save->dr7 = 0x400;
Avi Kivityf6e78472010-08-02 15:30:20 +03001138 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001140 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141
Joerg Roedele0231712010-02-24 18:59:10 +01001142 /*
1143 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001144 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -03001146 svm->vcpu.arch.cr0 = 0;
1147 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001148
Rusty Russell66aee912007-07-17 23:34:16 +10001149 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001151
1152 if (npt_enabled) {
1153 /* Setup VMCB for Nested Paging */
1154 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001155 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001156 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001157 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1158 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001159 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001160 save->cr3 = 0;
1161 save->cr4 = 0;
1162 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001163 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001164
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001165 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001166 svm->vcpu.arch.hflags = 0;
1167
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001168 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001169 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001170 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001171 }
1172
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001173 mark_all_dirty(svm->vmcb);
1174
Joerg Roedel2af91942009-08-07 11:49:28 +02001175 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176}
1177
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001178static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001179{
1180 struct vcpu_svm *svm = to_svm(vcpu);
1181
Joerg Roedele6101a92008-02-13 18:58:45 +01001182 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001183
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001184 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001185 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001186 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
1187 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +02001188 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001189 vcpu->arch.regs_avail = ~0;
1190 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001191
1192 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001193}
1194
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001195static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001197 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001199 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001200 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001201 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001202 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203
Rusty Russellc16f8622007-07-30 21:12:19 +10001204 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001205 if (!svm) {
1206 err = -ENOMEM;
1207 goto out;
1208 }
1209
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001210 svm->tsc_ratio = TSC_RATIO_DEFAULT;
1211
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001212 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1213 if (err)
1214 goto free_svm;
1215
Joerg Roedelf65c2292008-02-13 18:58:46 +01001216 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001217 page = alloc_page(GFP_KERNEL);
1218 if (!page)
1219 goto uninit;
1220
Joerg Roedelf65c2292008-02-13 18:58:46 +01001221 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1222 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001223 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001224
1225 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1226 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001227 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001228
Alexander Grafb286d5d2008-11-25 20:17:05 +01001229 hsave_page = alloc_page(GFP_KERNEL);
1230 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001231 goto free_page3;
1232
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001233 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001234
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001235 svm->msrpm = page_address(msrpm_pages);
1236 svm_vcpu_init_msrpm(svm->msrpm);
1237
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001238 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001239 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001240
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001241 svm->vmcb = page_address(page);
1242 clear_page(svm->vmcb);
1243 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1244 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +01001245 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001246 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001247
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001248 err = fx_init(&svm->vcpu);
1249 if (err)
1250 goto free_page4;
1251
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001252 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001253 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001254 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001256 svm_init_osvw(&svm->vcpu);
1257
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001258 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001259
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001260free_page4:
1261 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001262free_page3:
1263 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1264free_page2:
1265 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1266free_page1:
1267 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001268uninit:
1269 kvm_vcpu_uninit(&svm->vcpu);
1270free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001271 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001272out:
1273 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274}
1275
1276static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1277{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001278 struct vcpu_svm *svm = to_svm(vcpu);
1279
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001280 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001281 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001282 __free_page(virt_to_page(svm->nested.hsave));
1283 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001284 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001285 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286}
1287
Avi Kivity15ad7142007-07-11 18:17:21 +03001288static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001290 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001291 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001292
Avi Kivity0cc50642007-03-25 12:07:27 +02001293 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001294 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001295 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001296 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001297
Avi Kivity82ca2d12010-10-21 12:20:34 +02001298#ifdef CONFIG_X86_64
1299 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1300#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001301 savesegment(fs, svm->host.fs);
1302 savesegment(gs, svm->host.gs);
1303 svm->host.ldt = kvm_read_ldt();
1304
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001305 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001306 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001307
1308 if (static_cpu_has(X86_FEATURE_TSCRATEMSR) &&
1309 svm->tsc_ratio != __get_cpu_var(current_tsc_ratio)) {
1310 __get_cpu_var(current_tsc_ratio) = svm->tsc_ratio;
1311 wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_ratio);
1312 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313}
1314
1315static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1316{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001317 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001318 int i;
1319
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001320 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001321 kvm_load_ldt(svm->host.ldt);
1322#ifdef CONFIG_X86_64
1323 loadsegment(fs, svm->host.fs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001324 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001325 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001326#else
Avi Kivity831ca602011-03-08 16:09:51 +02001327#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001328 loadsegment(gs, svm->host.gs);
1329#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001330#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001331 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001332 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333}
1334
Kevin Wolfea5e97e2012-02-08 14:34:40 +01001335static void svm_update_cpl(struct kvm_vcpu *vcpu)
1336{
1337 struct vcpu_svm *svm = to_svm(vcpu);
1338 int cpl;
1339
1340 if (!is_protmode(vcpu))
1341 cpl = 0;
1342 else if (svm->vmcb->save.rflags & X86_EFLAGS_VM)
1343 cpl = 3;
1344 else
1345 cpl = svm->vmcb->save.cs.selector & 0x3;
1346
1347 svm->vmcb->save.cpl = cpl;
1348}
1349
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1351{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001352 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353}
1354
1355static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1356{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001357 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358}
1359
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001360static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1361{
1362 switch (reg) {
1363 case VCPU_EXREG_PDPTR:
1364 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001365 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001366 break;
1367 default:
1368 BUG();
1369 }
1370}
1371
Alexander Graff0b85052008-11-25 20:17:01 +01001372static void svm_set_vintr(struct vcpu_svm *svm)
1373{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001374 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001375}
1376
1377static void svm_clear_vintr(struct vcpu_svm *svm)
1378{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001379 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001380}
1381
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1383{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001384 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385
1386 switch (seg) {
1387 case VCPU_SREG_CS: return &save->cs;
1388 case VCPU_SREG_DS: return &save->ds;
1389 case VCPU_SREG_ES: return &save->es;
1390 case VCPU_SREG_FS: return &save->fs;
1391 case VCPU_SREG_GS: return &save->gs;
1392 case VCPU_SREG_SS: return &save->ss;
1393 case VCPU_SREG_TR: return &save->tr;
1394 case VCPU_SREG_LDTR: return &save->ldtr;
1395 }
1396 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001397 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398}
1399
1400static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1401{
1402 struct vmcb_seg *s = svm_seg(vcpu, seg);
1403
1404 return s->base;
1405}
1406
1407static void svm_get_segment(struct kvm_vcpu *vcpu,
1408 struct kvm_segment *var, int seg)
1409{
1410 struct vmcb_seg *s = svm_seg(vcpu, seg);
1411
1412 var->base = s->base;
1413 var->limit = s->limit;
1414 var->selector = s->selector;
1415 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1416 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1417 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1418 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1419 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1420 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1421 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1422 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001423
Joerg Roedele0231712010-02-24 18:59:10 +01001424 /*
1425 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001426 * for cross vendor migration purposes by "not present"
1427 */
1428 var->unusable = !var->present || (var->type == 0);
1429
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001430 switch (seg) {
1431 case VCPU_SREG_CS:
1432 /*
1433 * SVM always stores 0 for the 'G' bit in the CS selector in
1434 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1435 * Intel's VMENTRY has a check on the 'G' bit.
1436 */
Amit Shah25022ac2008-10-27 09:04:17 +00001437 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001438 break;
1439 case VCPU_SREG_TR:
1440 /*
1441 * Work around a bug where the busy flag in the tr selector
1442 * isn't exposed
1443 */
Amit Shahc0d09822008-10-27 09:04:18 +00001444 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001445 break;
1446 case VCPU_SREG_DS:
1447 case VCPU_SREG_ES:
1448 case VCPU_SREG_FS:
1449 case VCPU_SREG_GS:
1450 /*
1451 * The accessed bit must always be set in the segment
1452 * descriptor cache, although it can be cleared in the
1453 * descriptor, the cached bit always remains at 1. Since
1454 * Intel has a check on this, set it here to support
1455 * cross-vendor migration.
1456 */
1457 if (!var->unusable)
1458 var->type |= 0x1;
1459 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001460 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001461 /*
1462 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001463 * descriptor is left as 1, although the whole segment has
1464 * been made unusable. Clear it here to pass an Intel VMX
1465 * entry check when cross vendor migrating.
1466 */
1467 if (var->unusable)
1468 var->db = 0;
1469 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001470 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471}
1472
Izik Eidus2e4d2652008-03-24 19:38:34 +02001473static int svm_get_cpl(struct kvm_vcpu *vcpu)
1474{
1475 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1476
1477 return save->cpl;
1478}
1479
Gleb Natapov89a27f42010-02-16 10:51:48 +02001480static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001482 struct vcpu_svm *svm = to_svm(vcpu);
1483
Gleb Natapov89a27f42010-02-16 10:51:48 +02001484 dt->size = svm->vmcb->save.idtr.limit;
1485 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486}
1487
Gleb Natapov89a27f42010-02-16 10:51:48 +02001488static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001490 struct vcpu_svm *svm = to_svm(vcpu);
1491
Gleb Natapov89a27f42010-02-16 10:51:48 +02001492 svm->vmcb->save.idtr.limit = dt->size;
1493 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001494 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495}
1496
Gleb Natapov89a27f42010-02-16 10:51:48 +02001497static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001499 struct vcpu_svm *svm = to_svm(vcpu);
1500
Gleb Natapov89a27f42010-02-16 10:51:48 +02001501 dt->size = svm->vmcb->save.gdtr.limit;
1502 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503}
1504
Gleb Natapov89a27f42010-02-16 10:51:48 +02001505static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001507 struct vcpu_svm *svm = to_svm(vcpu);
1508
Gleb Natapov89a27f42010-02-16 10:51:48 +02001509 svm->vmcb->save.gdtr.limit = dt->size;
1510 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001511 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512}
1513
Avi Kivitye8467fd2009-12-29 18:43:06 +02001514static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1515{
1516}
1517
Avi Kivityaff48ba2010-12-05 18:56:11 +02001518static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1519{
1520}
1521
Anthony Liguori25c4c272007-04-27 09:29:21 +03001522static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001523{
1524}
1525
Avi Kivityd2251572010-01-06 10:55:27 +02001526static void update_cr0_intercept(struct vcpu_svm *svm)
1527{
1528 ulong gcr0 = svm->vcpu.arch.cr0;
1529 u64 *hcr0 = &svm->vmcb->save.cr0;
1530
1531 if (!svm->vcpu.fpu_active)
1532 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1533 else
1534 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1535 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1536
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001537 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001538
1539 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001540 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1541 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001542 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001543 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1544 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001545 }
1546}
1547
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1549{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001550 struct vcpu_svm *svm = to_svm(vcpu);
1551
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001552#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001553 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001554 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001555 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001556 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557 }
1558
Mike Dayd77c26f2007-10-08 09:02:08 -04001559 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001560 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001561 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001562 }
1563 }
1564#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001565 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001566
1567 if (!npt_enabled)
1568 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001569
1570 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001571 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001572 /*
1573 * re-enable caching here because the QEMU bios
1574 * does not do it - this results in some delay at
1575 * reboot
1576 */
1577 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001578 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001579 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001580 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581}
1582
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001583static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584{
Joerg Roedel6394b642008-04-09 14:15:29 +02001585 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001586 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1587
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001588 if (cr4 & X86_CR4_VMXE)
1589 return 1;
1590
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001591 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001592 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001593
Joerg Roedelec077262008-04-09 14:15:28 +02001594 vcpu->arch.cr4 = cr4;
1595 if (!npt_enabled)
1596 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001597 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001598 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001599 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001600 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601}
1602
1603static void svm_set_segment(struct kvm_vcpu *vcpu,
1604 struct kvm_segment *var, int seg)
1605{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001606 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607 struct vmcb_seg *s = svm_seg(vcpu, seg);
1608
1609 s->base = var->base;
1610 s->limit = var->limit;
1611 s->selector = var->selector;
1612 if (var->unusable)
1613 s->attrib = 0;
1614 else {
1615 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1616 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1617 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1618 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1619 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1620 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1621 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1622 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1623 }
1624 if (seg == VCPU_SREG_CS)
Kevin Wolfea5e97e2012-02-08 14:34:40 +01001625 svm_update_cpl(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626
Joerg Roedel060d0c92010-12-03 11:45:57 +01001627 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628}
1629
Gleb Natapov44c11432009-05-11 13:35:52 +03001630static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001632 struct vcpu_svm *svm = to_svm(vcpu);
1633
Joerg Roedel18c918c2010-11-30 18:03:59 +01001634 clr_exception_intercept(svm, DB_VECTOR);
1635 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001636
Jan Kiszka6be7d302009-10-18 13:24:54 +02001637 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001638 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001639
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001640 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1641 if (vcpu->guest_debug &
1642 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001643 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001644 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001645 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001646 } else
1647 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001648}
1649
Jan Kiszka355be0b2009-10-03 00:31:21 +02001650static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001651{
Gleb Natapov44c11432009-05-11 13:35:52 +03001652 struct vcpu_svm *svm = to_svm(vcpu);
1653
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001654 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1655 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1656 else
1657 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1658
Joerg Roedel72214b92010-12-03 11:45:55 +01001659 mark_dirty(svm->vmcb, VMCB_DR);
1660
Jan Kiszka355be0b2009-10-03 00:31:21 +02001661 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662}
1663
Tejun Heo0fe1e002009-10-29 22:34:14 +09001664static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001666 if (sd->next_asid > sd->max_asid) {
1667 ++sd->asid_generation;
1668 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001669 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001670 }
1671
Tejun Heo0fe1e002009-10-29 22:34:14 +09001672 svm->asid_generation = sd->asid_generation;
1673 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001674
1675 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001676}
1677
Gleb Natapov020df072010-04-13 10:05:23 +03001678static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001680 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001681
Gleb Natapov020df072010-04-13 10:05:23 +03001682 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01001683 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684}
1685
Avi Kivity851ba692009-08-24 11:10:17 +03001686static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687{
Gleb Natapov631bc482010-10-14 11:22:52 +02001688 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001690 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691
Gleb Natapov631bc482010-10-14 11:22:52 +02001692 switch (svm->apf_reason) {
1693 default:
1694 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001695
Gleb Natapov631bc482010-10-14 11:22:52 +02001696 trace_kvm_page_fault(fault_address, error_code);
1697 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1698 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01001699 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1700 svm->vmcb->control.insn_bytes,
1701 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02001702 break;
1703 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1704 svm->apf_reason = 0;
1705 local_irq_disable();
1706 kvm_async_pf_task_wait(fault_address);
1707 local_irq_enable();
1708 break;
1709 case KVM_PV_REASON_PAGE_READY:
1710 svm->apf_reason = 0;
1711 local_irq_disable();
1712 kvm_async_pf_task_wake(fault_address);
1713 local_irq_enable();
1714 break;
1715 }
1716 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717}
1718
Avi Kivity851ba692009-08-24 11:10:17 +03001719static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001720{
Avi Kivity851ba692009-08-24 11:10:17 +03001721 struct kvm_run *kvm_run = svm->vcpu.run;
1722
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001723 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001724 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001725 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001726 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1727 return 1;
1728 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001729
Jan Kiszka6be7d302009-10-18 13:24:54 +02001730 if (svm->nmi_singlestep) {
1731 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001732 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1733 svm->vmcb->save.rflags &=
1734 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1735 update_db_intercept(&svm->vcpu);
1736 }
1737
1738 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001739 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001740 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1741 kvm_run->debug.arch.pc =
1742 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1743 kvm_run->debug.arch.exception = DB_VECTOR;
1744 return 0;
1745 }
1746
1747 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001748}
1749
Avi Kivity851ba692009-08-24 11:10:17 +03001750static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001751{
Avi Kivity851ba692009-08-24 11:10:17 +03001752 struct kvm_run *kvm_run = svm->vcpu.run;
1753
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001754 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1755 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1756 kvm_run->debug.arch.exception = BP_VECTOR;
1757 return 0;
1758}
1759
Avi Kivity851ba692009-08-24 11:10:17 +03001760static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001761{
1762 int er;
1763
Andre Przywara51d8b662010-12-21 11:12:02 +01001764 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001765 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001766 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001767 return 1;
1768}
1769
Avi Kivity6b52d182010-01-21 15:31:47 +02001770static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001771{
Avi Kivity6b52d182010-01-21 15:31:47 +02001772 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001773
Joerg Roedel18c918c2010-11-30 18:03:59 +01001774 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001775
Rusty Russelle756fc62007-07-30 20:07:08 +10001776 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001777 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001778}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001779
Avi Kivity6b52d182010-01-21 15:31:47 +02001780static int nm_interception(struct vcpu_svm *svm)
1781{
1782 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001783 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001784}
1785
Joerg Roedel67ec6602010-05-17 14:43:35 +02001786static bool is_erratum_383(void)
1787{
1788 int err, i;
1789 u64 value;
1790
1791 if (!erratum_383_found)
1792 return false;
1793
1794 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1795 if (err)
1796 return false;
1797
1798 /* Bit 62 may or may not be set for this mce */
1799 value &= ~(1ULL << 62);
1800
1801 if (value != 0xb600000000010015ULL)
1802 return false;
1803
1804 /* Clear MCi_STATUS registers */
1805 for (i = 0; i < 6; ++i)
1806 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1807
1808 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1809 if (!err) {
1810 u32 low, high;
1811
1812 value &= ~(1ULL << 2);
1813 low = lower_32_bits(value);
1814 high = upper_32_bits(value);
1815
1816 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1817 }
1818
1819 /* Flush tlb to evict multi-match entries */
1820 __flush_tlb_all();
1821
1822 return true;
1823}
1824
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001825static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001826{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001827 if (is_erratum_383()) {
1828 /*
1829 * Erratum 383 triggered. Guest state is corrupt so kill the
1830 * guest.
1831 */
1832 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1833
Avi Kivitya8eeb042010-05-10 12:34:53 +03001834 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001835
1836 return;
1837 }
1838
Joerg Roedel53371b52008-04-09 14:15:30 +02001839 /*
1840 * On an #MC intercept the MCE handler is not called automatically in
1841 * the host. So do it by hand here.
1842 */
1843 asm volatile (
1844 "int $0x12\n");
1845 /* not sure if we ever come back to this point */
1846
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001847 return;
1848}
1849
1850static int mc_interception(struct vcpu_svm *svm)
1851{
Joerg Roedel53371b52008-04-09 14:15:30 +02001852 return 1;
1853}
1854
Avi Kivity851ba692009-08-24 11:10:17 +03001855static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001856{
Avi Kivity851ba692009-08-24 11:10:17 +03001857 struct kvm_run *kvm_run = svm->vcpu.run;
1858
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001859 /*
1860 * VMCB is undefined after a SHUTDOWN intercept
1861 * so reinitialize it.
1862 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001863 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001864 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001865
1866 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1867 return 0;
1868}
1869
Avi Kivity851ba692009-08-24 11:10:17 +03001870static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001871{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001872 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001873 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001874 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001875 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001876
Rusty Russelle756fc62007-07-30 20:07:08 +10001877 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001878 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001879 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001880 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01001881 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001882
Avi Kivity039576c2007-03-20 12:46:50 +02001883 port = io_info >> 16;
1884 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001885 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001886 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001887
1888 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889}
1890
Avi Kivity851ba692009-08-24 11:10:17 +03001891static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001892{
1893 return 1;
1894}
1895
Avi Kivity851ba692009-08-24 11:10:17 +03001896static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001897{
1898 ++svm->vcpu.stat.irq_exits;
1899 return 1;
1900}
1901
Avi Kivity851ba692009-08-24 11:10:17 +03001902static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903{
1904 return 1;
1905}
1906
Avi Kivity851ba692009-08-24 11:10:17 +03001907static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001909 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001910 skip_emulated_instruction(&svm->vcpu);
1911 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912}
1913
Avi Kivity851ba692009-08-24 11:10:17 +03001914static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001915{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001916 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001917 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001918 kvm_emulate_hypercall(&svm->vcpu);
1919 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001920}
1921
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001922static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1923{
1924 struct vcpu_svm *svm = to_svm(vcpu);
1925
1926 return svm->nested.nested_cr3;
1927}
1928
Avi Kivitye4e517b2011-07-28 11:36:17 +03001929static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
1930{
1931 struct vcpu_svm *svm = to_svm(vcpu);
1932 u64 cr3 = svm->nested.nested_cr3;
1933 u64 pdpte;
1934 int ret;
1935
1936 ret = kvm_read_guest_page(vcpu->kvm, gpa_to_gfn(cr3), &pdpte,
1937 offset_in_page(cr3) + index * 8, 8);
1938 if (ret)
1939 return 0;
1940 return pdpte;
1941}
1942
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001943static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1944 unsigned long root)
1945{
1946 struct vcpu_svm *svm = to_svm(vcpu);
1947
1948 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01001949 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001950 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001951}
1952
Avi Kivity6389ee92010-11-29 16:12:30 +02001953static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1954 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001955{
1956 struct vcpu_svm *svm = to_svm(vcpu);
1957
1958 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1959 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001960 svm->vmcb->control.exit_info_1 = fault->error_code;
1961 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001962
1963 nested_svm_vmexit(svm);
1964}
1965
Joerg Roedel4b161842010-09-10 17:31:03 +02001966static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1967{
1968 int r;
1969
1970 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1971
1972 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1973 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03001974 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02001975 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1976 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1977 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1978
1979 return r;
1980}
1981
1982static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1983{
1984 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1985}
1986
Alexander Grafc0725422008-11-25 20:17:03 +01001987static int nested_svm_check_permissions(struct vcpu_svm *svm)
1988{
Avi Kivityf6801df2010-01-21 15:31:50 +02001989 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001990 || !is_paging(&svm->vcpu)) {
1991 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1992 return 1;
1993 }
1994
1995 if (svm->vmcb->save.cpl) {
1996 kvm_inject_gp(&svm->vcpu, 0);
1997 return 1;
1998 }
1999
2000 return 0;
2001}
2002
Alexander Grafcf74a782008-11-25 20:17:08 +01002003static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2004 bool has_error_code, u32 error_code)
2005{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002006 int vmexit;
2007
Joerg Roedel20307532010-11-29 17:51:48 +01002008 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002009 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002010
Joerg Roedel0295ad72009-08-07 11:49:37 +02002011 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2012 svm->vmcb->control.exit_code_hi = 0;
2013 svm->vmcb->control.exit_info_1 = error_code;
2014 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
2015
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002016 vmexit = nested_svm_intercept(svm);
2017 if (vmexit == NESTED_EXIT_DONE)
2018 svm->nested.exit_required = true;
2019
2020 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002021}
2022
Joerg Roedel8fe54652010-02-19 16:23:01 +01002023/* This function returns true if it is save to enable the irq window */
2024static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002025{
Joerg Roedel20307532010-11-29 17:51:48 +01002026 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002027 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002028
Joerg Roedel26666952009-08-07 11:49:46 +02002029 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002030 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002031
Joerg Roedel26666952009-08-07 11:49:46 +02002032 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002033 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002034
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002035 /*
2036 * if vmexit was already requested (by intercepted exception
2037 * for instance) do not overwrite it with "external interrupt"
2038 * vmexit.
2039 */
2040 if (svm->nested.exit_required)
2041 return false;
2042
Joerg Roedel197717d2010-02-24 18:59:19 +01002043 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2044 svm->vmcb->control.exit_info_1 = 0;
2045 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002046
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002047 if (svm->nested.intercept & 1ULL) {
2048 /*
2049 * The #vmexit can't be emulated here directly because this
2050 * code path runs with irqs and preemtion disabled. A
2051 * #vmexit emulation might sleep. Only signal request for
2052 * the #vmexit here.
2053 */
2054 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002055 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002056 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002057 }
2058
Joerg Roedel8fe54652010-02-19 16:23:01 +01002059 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002060}
2061
Joerg Roedel887f5002010-02-24 18:59:12 +01002062/* This function returns true if it is save to enable the nmi window */
2063static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2064{
Joerg Roedel20307532010-11-29 17:51:48 +01002065 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002066 return true;
2067
2068 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2069 return true;
2070
2071 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2072 svm->nested.exit_required = true;
2073
2074 return false;
2075}
2076
Joerg Roedel7597f122010-02-19 16:23:00 +01002077static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002078{
2079 struct page *page;
2080
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002081 might_sleep();
2082
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002083 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002084 if (is_error_page(page))
2085 goto error;
2086
Joerg Roedel7597f122010-02-19 16:23:00 +01002087 *_page = page;
2088
2089 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002090
2091error:
2092 kvm_release_page_clean(page);
2093 kvm_inject_gp(&svm->vcpu, 0);
2094
2095 return NULL;
2096}
2097
Joerg Roedel7597f122010-02-19 16:23:00 +01002098static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002099{
Joerg Roedel7597f122010-02-19 16:23:00 +01002100 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002101 kvm_release_page_dirty(page);
2102}
2103
Joerg Roedelce2ac082010-03-01 15:34:39 +01002104static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002105{
Joerg Roedelce2ac082010-03-01 15:34:39 +01002106 unsigned port;
2107 u8 val, bit;
2108 u64 gpa;
2109
2110 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2111 return NESTED_EXIT_HOST;
2112
2113 port = svm->vmcb->control.exit_info_1 >> 16;
2114 gpa = svm->nested.vmcb_iopm + (port / 8);
2115 bit = port % 8;
2116 val = 0;
2117
2118 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
2119 val &= (1 << bit);
2120
2121 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
2122}
2123
Joerg Roedeld2477822010-03-01 15:34:34 +01002124static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002125{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002126 u32 offset, msr, value;
2127 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002128
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002129 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002130 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002131
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002132 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2133 offset = svm_msrpm_offset(msr);
2134 write = svm->vmcb->control.exit_info_1 & 1;
2135 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002136
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002137 if (offset == MSR_INVALID)
2138 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002139
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002140 /* Offset is in 32 bit units but need in 8 bit units */
2141 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002142
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002143 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
2144 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002145
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002146 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002147}
2148
Joerg Roedel410e4d52009-08-07 11:49:44 +02002149static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002150{
Alexander Grafcf74a782008-11-25 20:17:08 +01002151 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002152
Joerg Roedel410e4d52009-08-07 11:49:44 +02002153 switch (exit_code) {
2154 case SVM_EXIT_INTR:
2155 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002156 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002157 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002158 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002159 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002160 if (npt_enabled)
2161 return NESTED_EXIT_HOST;
2162 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002163 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002164 /* When we're shadowing, trap PFs, but not async PF */
2165 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002166 return NESTED_EXIT_HOST;
2167 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01002168 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
2169 nm_interception(svm);
2170 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002171 default:
2172 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002173 }
2174
Joerg Roedel410e4d52009-08-07 11:49:44 +02002175 return NESTED_EXIT_CONTINUE;
2176}
2177
2178/*
2179 * If this function returns true, this #vmexit was already handled
2180 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002181static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002182{
2183 u32 exit_code = svm->vmcb->control.exit_code;
2184 int vmexit = NESTED_EXIT_HOST;
2185
Alexander Grafcf74a782008-11-25 20:17:08 +01002186 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002187 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002188 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002189 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002190 case SVM_EXIT_IOIO:
2191 vmexit = nested_svm_intercept_ioio(svm);
2192 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002193 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2194 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2195 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002196 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002197 break;
2198 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002199 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2200 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2201 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002202 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002203 break;
2204 }
2205 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2206 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002207 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002208 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002209 /* async page fault always cause vmexit */
2210 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2211 svm->apf_reason != 0)
2212 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002213 break;
2214 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002215 case SVM_EXIT_ERR: {
2216 vmexit = NESTED_EXIT_DONE;
2217 break;
2218 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002219 default: {
2220 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002221 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002222 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002223 }
2224 }
2225
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002226 return vmexit;
2227}
2228
2229static int nested_svm_exit_handled(struct vcpu_svm *svm)
2230{
2231 int vmexit;
2232
2233 vmexit = nested_svm_intercept(svm);
2234
2235 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002236 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002237
2238 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002239}
2240
Joerg Roedel0460a972009-08-07 11:49:31 +02002241static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2242{
2243 struct vmcb_control_area *dst = &dst_vmcb->control;
2244 struct vmcb_control_area *from = &from_vmcb->control;
2245
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002246 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002247 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002248 dst->intercept_exceptions = from->intercept_exceptions;
2249 dst->intercept = from->intercept;
2250 dst->iopm_base_pa = from->iopm_base_pa;
2251 dst->msrpm_base_pa = from->msrpm_base_pa;
2252 dst->tsc_offset = from->tsc_offset;
2253 dst->asid = from->asid;
2254 dst->tlb_ctl = from->tlb_ctl;
2255 dst->int_ctl = from->int_ctl;
2256 dst->int_vector = from->int_vector;
2257 dst->int_state = from->int_state;
2258 dst->exit_code = from->exit_code;
2259 dst->exit_code_hi = from->exit_code_hi;
2260 dst->exit_info_1 = from->exit_info_1;
2261 dst->exit_info_2 = from->exit_info_2;
2262 dst->exit_int_info = from->exit_int_info;
2263 dst->exit_int_info_err = from->exit_int_info_err;
2264 dst->nested_ctl = from->nested_ctl;
2265 dst->event_inj = from->event_inj;
2266 dst->event_inj_err = from->event_inj_err;
2267 dst->nested_cr3 = from->nested_cr3;
2268 dst->lbr_ctl = from->lbr_ctl;
2269}
2270
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002271static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002272{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002273 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002274 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002275 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002276 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002277
Joerg Roedel17897f32009-10-09 16:08:29 +02002278 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2279 vmcb->control.exit_info_1,
2280 vmcb->control.exit_info_2,
2281 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002282 vmcb->control.exit_int_info_err,
2283 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002284
Joerg Roedel7597f122010-02-19 16:23:00 +01002285 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002286 if (!nested_vmcb)
2287 return 1;
2288
Joerg Roedel20307532010-11-29 17:51:48 +01002289 /* Exit Guest-Mode */
2290 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002291 svm->nested.vmcb = 0;
2292
Alexander Grafcf74a782008-11-25 20:17:08 +01002293 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002294 disable_gif(svm);
2295
2296 nested_vmcb->save.es = vmcb->save.es;
2297 nested_vmcb->save.cs = vmcb->save.cs;
2298 nested_vmcb->save.ss = vmcb->save.ss;
2299 nested_vmcb->save.ds = vmcb->save.ds;
2300 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2301 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002302 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002303 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002304 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002305 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002306 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002307 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002308 nested_vmcb->save.rip = vmcb->save.rip;
2309 nested_vmcb->save.rsp = vmcb->save.rsp;
2310 nested_vmcb->save.rax = vmcb->save.rax;
2311 nested_vmcb->save.dr7 = vmcb->save.dr7;
2312 nested_vmcb->save.dr6 = vmcb->save.dr6;
2313 nested_vmcb->save.cpl = vmcb->save.cpl;
2314
2315 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2316 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2317 nested_vmcb->control.int_state = vmcb->control.int_state;
2318 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2319 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2320 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2321 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2322 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2323 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002324 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002325
2326 /*
2327 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2328 * to make sure that we do not lose injected events. So check event_inj
2329 * here and copy it to exit_int_info if it is valid.
2330 * Exit_int_info and event_inj can't be both valid because the case
2331 * below only happens on a VMRUN instruction intercept which has
2332 * no valid exit_int_info set.
2333 */
2334 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2335 struct vmcb_control_area *nc = &nested_vmcb->control;
2336
2337 nc->exit_int_info = vmcb->control.event_inj;
2338 nc->exit_int_info_err = vmcb->control.event_inj_err;
2339 }
2340
Joerg Roedel33740e42009-08-07 11:49:29 +02002341 nested_vmcb->control.tlb_ctl = 0;
2342 nested_vmcb->control.event_inj = 0;
2343 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002344
2345 /* We always set V_INTR_MASKING and remember the old value in hflags */
2346 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2347 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2348
Alexander Grafcf74a782008-11-25 20:17:08 +01002349 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002350 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002351
Alexander Graf219b65d2009-06-15 15:21:25 +02002352 kvm_clear_exception_queue(&svm->vcpu);
2353 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002354
Joerg Roedel4b161842010-09-10 17:31:03 +02002355 svm->nested.nested_cr3 = 0;
2356
Alexander Grafcf74a782008-11-25 20:17:08 +01002357 /* Restore selected save entries */
2358 svm->vmcb->save.es = hsave->save.es;
2359 svm->vmcb->save.cs = hsave->save.cs;
2360 svm->vmcb->save.ss = hsave->save.ss;
2361 svm->vmcb->save.ds = hsave->save.ds;
2362 svm->vmcb->save.gdtr = hsave->save.gdtr;
2363 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002364 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002365 svm_set_efer(&svm->vcpu, hsave->save.efer);
2366 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2367 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2368 if (npt_enabled) {
2369 svm->vmcb->save.cr3 = hsave->save.cr3;
2370 svm->vcpu.arch.cr3 = hsave->save.cr3;
2371 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002372 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002373 }
2374 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2375 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2376 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2377 svm->vmcb->save.dr7 = 0;
2378 svm->vmcb->save.cpl = 0;
2379 svm->vmcb->control.exit_int_info = 0;
2380
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002381 mark_all_dirty(svm->vmcb);
2382
Joerg Roedel7597f122010-02-19 16:23:00 +01002383 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002384
Joerg Roedel4b161842010-09-10 17:31:03 +02002385 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002386 kvm_mmu_reset_context(&svm->vcpu);
2387 kvm_mmu_load(&svm->vcpu);
2388
2389 return 0;
2390}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002391
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002392static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002393{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002394 /*
2395 * This function merges the msr permission bitmaps of kvm and the
2396 * nested vmcb. It is omptimized in that it only merges the parts where
2397 * the kvm msr permission bitmap may contain zero bits
2398 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002399 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002400
Joerg Roedel323c3d82010-03-01 15:34:37 +01002401 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2402 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002403
Joerg Roedel323c3d82010-03-01 15:34:37 +01002404 for (i = 0; i < MSRPM_OFFSETS; i++) {
2405 u32 value, p;
2406 u64 offset;
2407
2408 if (msrpm_offsets[i] == 0xffffffff)
2409 break;
2410
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002411 p = msrpm_offsets[i];
2412 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002413
2414 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2415 return false;
2416
2417 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2418 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002419
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002420 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002421
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002422 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002423}
2424
Joerg Roedel52c65a302010-08-02 16:46:44 +02002425static bool nested_vmcb_checks(struct vmcb *vmcb)
2426{
2427 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2428 return false;
2429
Joerg Roedeldbe77582010-08-02 16:46:45 +02002430 if (vmcb->control.asid == 0)
2431 return false;
2432
Joerg Roedel4b161842010-09-10 17:31:03 +02002433 if (vmcb->control.nested_ctl && !npt_enabled)
2434 return false;
2435
Joerg Roedel52c65a302010-08-02 16:46:44 +02002436 return true;
2437}
2438
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002439static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002440{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002441 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002442 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002443 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002444 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002445 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002446
Joerg Roedel06fc77722010-02-19 16:23:07 +01002447 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002448
Joerg Roedel7597f122010-02-19 16:23:00 +01002449 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002450 if (!nested_vmcb)
2451 return false;
2452
Joerg Roedel52c65a302010-08-02 16:46:44 +02002453 if (!nested_vmcb_checks(nested_vmcb)) {
2454 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2455 nested_vmcb->control.exit_code_hi = 0;
2456 nested_vmcb->control.exit_info_1 = 0;
2457 nested_vmcb->control.exit_info_2 = 0;
2458
2459 nested_svm_unmap(page);
2460
2461 return false;
2462 }
2463
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002464 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002465 nested_vmcb->save.rip,
2466 nested_vmcb->control.int_ctl,
2467 nested_vmcb->control.event_inj,
2468 nested_vmcb->control.nested_ctl);
2469
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002470 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2471 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002472 nested_vmcb->control.intercept_exceptions,
2473 nested_vmcb->control.intercept);
2474
Alexander Graf3d6368e2008-11-25 20:17:07 +01002475 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002476 kvm_clear_exception_queue(&svm->vcpu);
2477 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002478
Joerg Roedele0231712010-02-24 18:59:10 +01002479 /*
2480 * Save the old vmcb, so we don't need to pick what we save, but can
2481 * restore everything when a VMEXIT occurs
2482 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002483 hsave->save.es = vmcb->save.es;
2484 hsave->save.cs = vmcb->save.cs;
2485 hsave->save.ss = vmcb->save.ss;
2486 hsave->save.ds = vmcb->save.ds;
2487 hsave->save.gdtr = vmcb->save.gdtr;
2488 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002489 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002490 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002491 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002492 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002493 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002494 hsave->save.rsp = vmcb->save.rsp;
2495 hsave->save.rax = vmcb->save.rax;
2496 if (npt_enabled)
2497 hsave->save.cr3 = vmcb->save.cr3;
2498 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002499 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002500
Joerg Roedel0460a972009-08-07 11:49:31 +02002501 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002502
Avi Kivityf6e78472010-08-02 15:30:20 +03002503 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002504 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2505 else
2506 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2507
Joerg Roedel4b161842010-09-10 17:31:03 +02002508 if (nested_vmcb->control.nested_ctl) {
2509 kvm_mmu_unload(&svm->vcpu);
2510 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2511 nested_svm_init_mmu_context(&svm->vcpu);
2512 }
2513
Alexander Graf3d6368e2008-11-25 20:17:07 +01002514 /* Load the nested guest state */
2515 svm->vmcb->save.es = nested_vmcb->save.es;
2516 svm->vmcb->save.cs = nested_vmcb->save.cs;
2517 svm->vmcb->save.ss = nested_vmcb->save.ss;
2518 svm->vmcb->save.ds = nested_vmcb->save.ds;
2519 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2520 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002521 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002522 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2523 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2524 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2525 if (npt_enabled) {
2526 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2527 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002528 } else
Avi Kivity23902182010-06-10 17:02:16 +03002529 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002530
2531 /* Guest paging mode is active - reset mmu */
2532 kvm_mmu_reset_context(&svm->vcpu);
2533
Joerg Roedeldefbba52009-08-07 11:49:30 +02002534 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002535 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2536 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2537 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002538
Alexander Graf3d6368e2008-11-25 20:17:07 +01002539 /* In case we don't even reach vcpu_run, the fields are not updated */
2540 svm->vmcb->save.rax = nested_vmcb->save.rax;
2541 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2542 svm->vmcb->save.rip = nested_vmcb->save.rip;
2543 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2544 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2545 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2546
Joerg Roedelf7138532010-03-01 15:34:40 +01002547 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002548 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002549
Joerg Roedelaad42c62009-08-07 11:49:34 +02002550 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002551 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002552 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002553 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2554 svm->nested.intercept = nested_vmcb->control.intercept;
2555
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002556 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002557 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002558 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2559 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2560 else
2561 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2562
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002563 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2564 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002565 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2566 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002567 }
2568
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002569 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002570 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002571
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002572 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002573 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2574 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2575 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002576 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2577 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2578
Joerg Roedel7597f122010-02-19 16:23:00 +01002579 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002580
Joerg Roedel20307532010-11-29 17:51:48 +01002581 /* Enter Guest-Mode */
2582 enter_guest_mode(&svm->vcpu);
2583
Joerg Roedel384c6362010-11-30 18:03:56 +01002584 /*
2585 * Merge guest and host intercepts - must be called with vcpu in
2586 * guest-mode to take affect here
2587 */
2588 recalc_intercepts(svm);
2589
Joerg Roedel06fc77722010-02-19 16:23:07 +01002590 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002591
Joerg Roedel2af91942009-08-07 11:49:28 +02002592 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002593
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002594 mark_all_dirty(svm->vmcb);
2595
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002596 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002597}
2598
Joerg Roedel9966bf62009-08-07 11:49:40 +02002599static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002600{
2601 to_vmcb->save.fs = from_vmcb->save.fs;
2602 to_vmcb->save.gs = from_vmcb->save.gs;
2603 to_vmcb->save.tr = from_vmcb->save.tr;
2604 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2605 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2606 to_vmcb->save.star = from_vmcb->save.star;
2607 to_vmcb->save.lstar = from_vmcb->save.lstar;
2608 to_vmcb->save.cstar = from_vmcb->save.cstar;
2609 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2610 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2611 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2612 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002613}
2614
Avi Kivity851ba692009-08-24 11:10:17 +03002615static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002616{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002617 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002618 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002619
Alexander Graf55426752008-11-25 20:17:06 +01002620 if (nested_svm_check_permissions(svm))
2621 return 1;
2622
Joerg Roedel7597f122010-02-19 16:23:00 +01002623 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002624 if (!nested_vmcb)
2625 return 1;
2626
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002627 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2628 skip_emulated_instruction(&svm->vcpu);
2629
Joerg Roedel9966bf62009-08-07 11:49:40 +02002630 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002631 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002632
2633 return 1;
2634}
2635
Avi Kivity851ba692009-08-24 11:10:17 +03002636static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002637{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002638 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002639 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002640
Alexander Graf55426752008-11-25 20:17:06 +01002641 if (nested_svm_check_permissions(svm))
2642 return 1;
2643
Joerg Roedel7597f122010-02-19 16:23:00 +01002644 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002645 if (!nested_vmcb)
2646 return 1;
2647
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002648 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2649 skip_emulated_instruction(&svm->vcpu);
2650
Joerg Roedel9966bf62009-08-07 11:49:40 +02002651 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002652 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002653
2654 return 1;
2655}
2656
Avi Kivity851ba692009-08-24 11:10:17 +03002657static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002658{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002659 if (nested_svm_check_permissions(svm))
2660 return 1;
2661
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002662 /* Save rip after vmrun instruction */
2663 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002664
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002665 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002666 return 1;
2667
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002668 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002669 goto failed;
2670
2671 return 1;
2672
2673failed:
2674
2675 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2676 svm->vmcb->control.exit_code_hi = 0;
2677 svm->vmcb->control.exit_info_1 = 0;
2678 svm->vmcb->control.exit_info_2 = 0;
2679
2680 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002681
2682 return 1;
2683}
2684
Avi Kivity851ba692009-08-24 11:10:17 +03002685static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002686{
2687 if (nested_svm_check_permissions(svm))
2688 return 1;
2689
2690 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2691 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002692 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002693
Joerg Roedel2af91942009-08-07 11:49:28 +02002694 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002695
2696 return 1;
2697}
2698
Avi Kivity851ba692009-08-24 11:10:17 +03002699static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002700{
2701 if (nested_svm_check_permissions(svm))
2702 return 1;
2703
2704 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2705 skip_emulated_instruction(&svm->vcpu);
2706
Joerg Roedel2af91942009-08-07 11:49:28 +02002707 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002708
2709 /* After a CLGI no interrupts should come */
2710 svm_clear_vintr(svm);
2711 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2712
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002713 mark_dirty(svm->vmcb, VMCB_INTR);
2714
Alexander Graf1371d902008-11-25 20:17:04 +01002715 return 1;
2716}
2717
Avi Kivity851ba692009-08-24 11:10:17 +03002718static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002719{
2720 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002721
Joerg Roedelec1ff792009-10-09 16:08:31 +02002722 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2723 vcpu->arch.regs[VCPU_REGS_RAX]);
2724
Alexander Grafff092382009-06-15 15:21:24 +02002725 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2726 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2727
2728 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2729 skip_emulated_instruction(&svm->vcpu);
2730 return 1;
2731}
2732
Joerg Roedel532a46b2009-10-09 16:08:32 +02002733static int skinit_interception(struct vcpu_svm *svm)
2734{
2735 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2736
2737 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2738 return 1;
2739}
2740
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002741static int xsetbv_interception(struct vcpu_svm *svm)
2742{
2743 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
2744 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
2745
2746 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
2747 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2748 skip_emulated_instruction(&svm->vcpu);
2749 }
2750
2751 return 1;
2752}
2753
Avi Kivity851ba692009-08-24 11:10:17 +03002754static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002756 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757 return 1;
2758}
2759
Avi Kivity851ba692009-08-24 11:10:17 +03002760static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761{
Izik Eidus37817f22008-03-24 23:14:53 +02002762 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002763 int reason;
2764 int int_type = svm->vmcb->control.exit_int_info &
2765 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002766 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002767 uint32_t type =
2768 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2769 uint32_t idt_v =
2770 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002771 bool has_error_code = false;
2772 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002773
2774 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002775
Izik Eidus37817f22008-03-24 23:14:53 +02002776 if (svm->vmcb->control.exit_info_2 &
2777 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002778 reason = TASK_SWITCH_IRET;
2779 else if (svm->vmcb->control.exit_info_2 &
2780 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2781 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002782 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002783 reason = TASK_SWITCH_GATE;
2784 else
2785 reason = TASK_SWITCH_CALL;
2786
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002787 if (reason == TASK_SWITCH_GATE) {
2788 switch (type) {
2789 case SVM_EXITINTINFO_TYPE_NMI:
2790 svm->vcpu.arch.nmi_injected = false;
2791 break;
2792 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002793 if (svm->vmcb->control.exit_info_2 &
2794 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2795 has_error_code = true;
2796 error_code =
2797 (u32)svm->vmcb->control.exit_info_2;
2798 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002799 kvm_clear_exception_queue(&svm->vcpu);
2800 break;
2801 case SVM_EXITINTINFO_TYPE_INTR:
2802 kvm_clear_interrupt_queue(&svm->vcpu);
2803 break;
2804 default:
2805 break;
2806 }
2807 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002808
Gleb Natapov8317c292009-04-12 13:37:02 +03002809 if (reason != TASK_SWITCH_GATE ||
2810 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2811 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002812 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2813 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002814
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002815 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2816 int_vec = -1;
2817
2818 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03002819 has_error_code, error_code) == EMULATE_FAIL) {
2820 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2821 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2822 svm->vcpu.run->internal.ndata = 0;
2823 return 0;
2824 }
2825 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826}
2827
Avi Kivity851ba692009-08-24 11:10:17 +03002828static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002830 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002831 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002832 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833}
2834
Avi Kivity851ba692009-08-24 11:10:17 +03002835static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002836{
2837 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002838 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002839 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02002840 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002841 return 1;
2842}
2843
Avi Kivity851ba692009-08-24 11:10:17 +03002844static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002845{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002846 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2847 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
2848
2849 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
2850 skip_emulated_instruction(&svm->vcpu);
2851 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002852}
2853
Avi Kivity851ba692009-08-24 11:10:17 +03002854static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855{
Andre Przywara51d8b662010-12-21 11:12:02 +01002856 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857}
2858
Avi Kivity332b56e2011-11-10 14:57:24 +02002859static int rdpmc_interception(struct vcpu_svm *svm)
2860{
2861 int err;
2862
2863 if (!static_cpu_has(X86_FEATURE_NRIPS))
2864 return emulate_on_interception(svm);
2865
2866 err = kvm_rdpmc(&svm->vcpu);
2867 kvm_complete_insn_gp(&svm->vcpu, err);
2868
2869 return 1;
2870}
2871
Joerg Roedel628afd22011-04-04 12:39:36 +02002872bool check_selective_cr0_intercepted(struct vcpu_svm *svm, unsigned long val)
2873{
2874 unsigned long cr0 = svm->vcpu.arch.cr0;
2875 bool ret = false;
2876 u64 intercept;
2877
2878 intercept = svm->nested.intercept;
2879
2880 if (!is_guest_mode(&svm->vcpu) ||
2881 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
2882 return false;
2883
2884 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2885 val &= ~SVM_CR0_SELECTIVE_MASK;
2886
2887 if (cr0 ^ val) {
2888 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2889 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2890 }
2891
2892 return ret;
2893}
2894
Andre Przywara7ff76d52010-12-21 11:12:04 +01002895#define CR_VALID (1ULL << 63)
2896
2897static int cr_interception(struct vcpu_svm *svm)
2898{
2899 int reg, cr;
2900 unsigned long val;
2901 int err;
2902
2903 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2904 return emulate_on_interception(svm);
2905
2906 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2907 return emulate_on_interception(svm);
2908
2909 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2910 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
2911
2912 err = 0;
2913 if (cr >= 16) { /* mov to cr */
2914 cr -= 16;
2915 val = kvm_register_read(&svm->vcpu, reg);
2916 switch (cr) {
2917 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02002918 if (!check_selective_cr0_intercepted(svm, val))
2919 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002920 else
2921 return 1;
2922
Andre Przywara7ff76d52010-12-21 11:12:04 +01002923 break;
2924 case 3:
2925 err = kvm_set_cr3(&svm->vcpu, val);
2926 break;
2927 case 4:
2928 err = kvm_set_cr4(&svm->vcpu, val);
2929 break;
2930 case 8:
2931 err = kvm_set_cr8(&svm->vcpu, val);
2932 break;
2933 default:
2934 WARN(1, "unhandled write to CR%d", cr);
2935 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2936 return 1;
2937 }
2938 } else { /* mov from cr */
2939 switch (cr) {
2940 case 0:
2941 val = kvm_read_cr0(&svm->vcpu);
2942 break;
2943 case 2:
2944 val = svm->vcpu.arch.cr2;
2945 break;
2946 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02002947 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002948 break;
2949 case 4:
2950 val = kvm_read_cr4(&svm->vcpu);
2951 break;
2952 case 8:
2953 val = kvm_get_cr8(&svm->vcpu);
2954 break;
2955 default:
2956 WARN(1, "unhandled read from CR%d", cr);
2957 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2958 return 1;
2959 }
2960 kvm_register_write(&svm->vcpu, reg, val);
2961 }
2962 kvm_complete_insn_gp(&svm->vcpu, err);
2963
2964 return 1;
2965}
2966
Andre Przywaracae37972010-12-21 11:12:05 +01002967static int dr_interception(struct vcpu_svm *svm)
2968{
2969 int reg, dr;
2970 unsigned long val;
2971 int err;
2972
2973 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
2974 return emulate_on_interception(svm);
2975
2976 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2977 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
2978
2979 if (dr >= 16) { /* mov to DRn */
2980 val = kvm_register_read(&svm->vcpu, reg);
2981 kvm_set_dr(&svm->vcpu, dr - 16, val);
2982 } else {
2983 err = kvm_get_dr(&svm->vcpu, dr, &val);
2984 if (!err)
2985 kvm_register_write(&svm->vcpu, reg, val);
2986 }
2987
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01002988 skip_emulated_instruction(&svm->vcpu);
2989
Andre Przywaracae37972010-12-21 11:12:05 +01002990 return 1;
2991}
2992
Avi Kivity851ba692009-08-24 11:10:17 +03002993static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002994{
Avi Kivity851ba692009-08-24 11:10:17 +03002995 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01002996 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03002997
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002998 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2999 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003000 r = cr_interception(svm);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003001 if (irqchip_in_kernel(svm->vcpu.kvm)) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003002 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003003 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003004 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003005 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003006 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003007 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3008 return 0;
3009}
3010
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003011u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu)
3012{
3013 struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
3014 return vmcb->control.tsc_offset +
3015 svm_scale_tsc(vcpu, native_read_tsc());
3016}
3017
Avi Kivity6aa8b732006-12-10 02:21:36 -08003018static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
3019{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003020 struct vcpu_svm *svm = to_svm(vcpu);
3021
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303023 case MSR_IA32_TSC: {
Nadav Har'El45133ec2011-08-02 15:55:23 +03003024 *data = svm->vmcb->control.tsc_offset +
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003025 svm_scale_tsc(vcpu, native_read_tsc());
3026
Avi Kivity6aa8b732006-12-10 02:21:36 -08003027 break;
3028 }
Brian Gerst8c065852010-07-17 09:03:26 -04003029 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003030 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003032#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003034 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035 break;
3036 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003037 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038 break;
3039 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003040 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003041 break;
3042 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003043 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044 break;
3045#endif
3046 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003047 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003048 break;
3049 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003050 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051 break;
3052 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003053 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003054 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003055 /*
3056 * Nobody will change the following 5 values in the VMCB so we can
3057 * safely return them on rdmsr. They will always be 0 until LBRV is
3058 * implemented.
3059 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003060 case MSR_IA32_DEBUGCTLMSR:
3061 *data = svm->vmcb->save.dbgctl;
3062 break;
3063 case MSR_IA32_LASTBRANCHFROMIP:
3064 *data = svm->vmcb->save.br_from;
3065 break;
3066 case MSR_IA32_LASTBRANCHTOIP:
3067 *data = svm->vmcb->save.br_to;
3068 break;
3069 case MSR_IA32_LASTINTFROMIP:
3070 *data = svm->vmcb->save.last_excp_from;
3071 break;
3072 case MSR_IA32_LASTINTTOIP:
3073 *data = svm->vmcb->save.last_excp_to;
3074 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003075 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003076 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003077 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003078 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003079 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003080 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003081 case MSR_IA32_UCODE_REV:
3082 *data = 0x01000065;
3083 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08003085 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086 }
3087 return 0;
3088}
3089
Avi Kivity851ba692009-08-24 11:10:17 +03003090static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003092 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093 u64 data;
3094
Avi Kivity59200272010-01-25 19:47:02 +02003095 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
3096 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003097 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003098 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003099 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003100
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003101 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003102 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003103 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003104 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105 }
3106 return 1;
3107}
3108
Joerg Roedel4a810182010-02-24 18:59:15 +01003109static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3110{
3111 struct vcpu_svm *svm = to_svm(vcpu);
3112 int svm_dis, chg_mask;
3113
3114 if (data & ~SVM_VM_CR_VALID_MASK)
3115 return 1;
3116
3117 chg_mask = SVM_VM_CR_VALID_MASK;
3118
3119 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3120 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3121
3122 svm->nested.vm_cr_msr &= ~chg_mask;
3123 svm->nested.vm_cr_msr |= (data & chg_mask);
3124
3125 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3126
3127 /* check for svm_disable while efer.svme is set */
3128 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3129 return 1;
3130
3131 return 0;
3132}
3133
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
3135{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003136 struct vcpu_svm *svm = to_svm(vcpu);
3137
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003139 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10003140 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003142 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003143 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003144 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003145#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003147 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148 break;
3149 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003150 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151 break;
3152 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003153 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154 break;
3155 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003156 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157 break;
3158#endif
3159 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003160 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161 break;
3162 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003163 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003164 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 break;
3166 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003167 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003168 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003170 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003171 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003172 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003173 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003174 break;
3175 }
3176 if (data & DEBUGCTL_RESERVED_BITS)
3177 return 1;
3178
3179 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003180 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003181 if (data & (1ULL<<0))
3182 svm_enable_lbrv(svm);
3183 else
3184 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003185 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003186 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003187 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003188 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003189 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003190 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003191 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003192 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
3193 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08003195 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003196 }
3197 return 0;
3198}
3199
Avi Kivity851ba692009-08-24 11:10:17 +03003200static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003202 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003203 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003204 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003205
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003206
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003207 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02003208 if (svm_set_msr(&svm->vcpu, ecx, data)) {
3209 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003210 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003211 } else {
3212 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10003213 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003214 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215 return 1;
3216}
3217
Avi Kivity851ba692009-08-24 11:10:17 +03003218static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219{
Rusty Russelle756fc62007-07-30 20:07:08 +10003220 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003221 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222 else
Avi Kivity851ba692009-08-24 11:10:17 +03003223 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224}
3225
Avi Kivity851ba692009-08-24 11:10:17 +03003226static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003227{
Avi Kivity851ba692009-08-24 11:10:17 +03003228 struct kvm_run *kvm_run = svm->vcpu.run;
3229
Avi Kivity3842d132010-07-27 12:30:24 +03003230 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003231 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003232 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003233 mark_dirty(svm->vmcb, VMCB_INTR);
Dor Laorc1150d82007-01-05 16:36:24 -08003234 /*
3235 * If the user space waits to inject interrupts, exit as soon as
3236 * possible
3237 */
Gleb Natapov80618232009-04-21 17:44:56 +03003238 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
3239 kvm_run->request_interrupt_window &&
3240 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10003241 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003242 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
3243 return 0;
3244 }
3245
3246 return 1;
3247}
3248
Mark Langsdorf565d0992009-10-06 14:25:02 -05003249static int pause_interception(struct vcpu_svm *svm)
3250{
3251 kvm_vcpu_on_spin(&(svm->vcpu));
3252 return 1;
3253}
3254
Avi Kivity851ba692009-08-24 11:10:17 +03003255static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003256 [SVM_EXIT_READ_CR0] = cr_interception,
3257 [SVM_EXIT_READ_CR3] = cr_interception,
3258 [SVM_EXIT_READ_CR4] = cr_interception,
3259 [SVM_EXIT_READ_CR8] = cr_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02003260 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003261 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003262 [SVM_EXIT_WRITE_CR3] = cr_interception,
3263 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003264 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003265 [SVM_EXIT_READ_DR0] = dr_interception,
3266 [SVM_EXIT_READ_DR1] = dr_interception,
3267 [SVM_EXIT_READ_DR2] = dr_interception,
3268 [SVM_EXIT_READ_DR3] = dr_interception,
3269 [SVM_EXIT_READ_DR4] = dr_interception,
3270 [SVM_EXIT_READ_DR5] = dr_interception,
3271 [SVM_EXIT_READ_DR6] = dr_interception,
3272 [SVM_EXIT_READ_DR7] = dr_interception,
3273 [SVM_EXIT_WRITE_DR0] = dr_interception,
3274 [SVM_EXIT_WRITE_DR1] = dr_interception,
3275 [SVM_EXIT_WRITE_DR2] = dr_interception,
3276 [SVM_EXIT_WRITE_DR3] = dr_interception,
3277 [SVM_EXIT_WRITE_DR4] = dr_interception,
3278 [SVM_EXIT_WRITE_DR5] = dr_interception,
3279 [SVM_EXIT_WRITE_DR6] = dr_interception,
3280 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003281 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3282 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003283 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003284 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3285 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
3286 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
3287 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003288 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003289 [SVM_EXIT_SMI] = nop_on_interception,
3290 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003291 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02003292 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003294 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003295 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003296 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003298 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003299 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003300 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003301 [SVM_EXIT_MSR] = msr_interception,
3302 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003303 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003304 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003305 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003306 [SVM_EXIT_VMLOAD] = vmload_interception,
3307 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003308 [SVM_EXIT_STGI] = stgi_interception,
3309 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003310 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003311 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01003312 [SVM_EXIT_MONITOR] = invalid_op_interception,
3313 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003314 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003315 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003316};
3317
Joe Perchesae8cc052011-04-24 22:00:50 -07003318static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003319{
3320 struct vcpu_svm *svm = to_svm(vcpu);
3321 struct vmcb_control_area *control = &svm->vmcb->control;
3322 struct vmcb_save_area *save = &svm->vmcb->save;
3323
3324 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003325 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
3326 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
3327 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
3328 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
3329 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
3330 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
3331 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3332 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3333 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3334 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3335 pr_err("%-20s%d\n", "asid:", control->asid);
3336 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3337 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3338 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3339 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3340 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3341 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3342 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3343 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3344 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3345 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3346 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
3347 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3348 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3349 pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
3350 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003351 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003352 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3353 "es:",
3354 save->es.selector, save->es.attrib,
3355 save->es.limit, save->es.base);
3356 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3357 "cs:",
3358 save->cs.selector, save->cs.attrib,
3359 save->cs.limit, save->cs.base);
3360 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3361 "ss:",
3362 save->ss.selector, save->ss.attrib,
3363 save->ss.limit, save->ss.base);
3364 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3365 "ds:",
3366 save->ds.selector, save->ds.attrib,
3367 save->ds.limit, save->ds.base);
3368 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3369 "fs:",
3370 save->fs.selector, save->fs.attrib,
3371 save->fs.limit, save->fs.base);
3372 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3373 "gs:",
3374 save->gs.selector, save->gs.attrib,
3375 save->gs.limit, save->gs.base);
3376 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3377 "gdtr:",
3378 save->gdtr.selector, save->gdtr.attrib,
3379 save->gdtr.limit, save->gdtr.base);
3380 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3381 "ldtr:",
3382 save->ldtr.selector, save->ldtr.attrib,
3383 save->ldtr.limit, save->ldtr.base);
3384 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3385 "idtr:",
3386 save->idtr.selector, save->idtr.attrib,
3387 save->idtr.limit, save->idtr.base);
3388 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3389 "tr:",
3390 save->tr.selector, save->tr.attrib,
3391 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003392 pr_err("cpl: %d efer: %016llx\n",
3393 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003394 pr_err("%-15s %016llx %-13s %016llx\n",
3395 "cr0:", save->cr0, "cr2:", save->cr2);
3396 pr_err("%-15s %016llx %-13s %016llx\n",
3397 "cr3:", save->cr3, "cr4:", save->cr4);
3398 pr_err("%-15s %016llx %-13s %016llx\n",
3399 "dr6:", save->dr6, "dr7:", save->dr7);
3400 pr_err("%-15s %016llx %-13s %016llx\n",
3401 "rip:", save->rip, "rflags:", save->rflags);
3402 pr_err("%-15s %016llx %-13s %016llx\n",
3403 "rsp:", save->rsp, "rax:", save->rax);
3404 pr_err("%-15s %016llx %-13s %016llx\n",
3405 "star:", save->star, "lstar:", save->lstar);
3406 pr_err("%-15s %016llx %-13s %016llx\n",
3407 "cstar:", save->cstar, "sfmask:", save->sfmask);
3408 pr_err("%-15s %016llx %-13s %016llx\n",
3409 "kernel_gs_base:", save->kernel_gs_base,
3410 "sysenter_cs:", save->sysenter_cs);
3411 pr_err("%-15s %016llx %-13s %016llx\n",
3412 "sysenter_esp:", save->sysenter_esp,
3413 "sysenter_eip:", save->sysenter_eip);
3414 pr_err("%-15s %016llx %-13s %016llx\n",
3415 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3416 pr_err("%-15s %016llx %-13s %016llx\n",
3417 "br_from:", save->br_from, "br_to:", save->br_to);
3418 pr_err("%-15s %016llx %-13s %016llx\n",
3419 "excp_from:", save->last_excp_from,
3420 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003421}
3422
Avi Kivity586f9602010-11-18 13:09:54 +02003423static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3424{
3425 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3426
3427 *info1 = control->exit_info_1;
3428 *info2 = control->exit_info_2;
3429}
3430
Avi Kivity851ba692009-08-24 11:10:17 +03003431static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003433 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003434 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003435 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003437 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003438 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3439 if (npt_enabled)
3440 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003441
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003442 if (unlikely(svm->nested.exit_required)) {
3443 nested_svm_vmexit(svm);
3444 svm->nested.exit_required = false;
3445
3446 return 1;
3447 }
3448
Joerg Roedel20307532010-11-29 17:51:48 +01003449 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003450 int vmexit;
3451
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003452 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3453 svm->vmcb->control.exit_info_1,
3454 svm->vmcb->control.exit_info_2,
3455 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003456 svm->vmcb->control.exit_int_info_err,
3457 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003458
Joerg Roedel410e4d52009-08-07 11:49:44 +02003459 vmexit = nested_svm_exit_special(svm);
3460
3461 if (vmexit == NESTED_EXIT_CONTINUE)
3462 vmexit = nested_svm_exit_handled(svm);
3463
3464 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003465 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003466 }
3467
Joerg Roedela5c38322009-08-07 11:49:32 +02003468 svm_complete_interrupts(svm);
3469
Avi Kivity04d2cc72007-09-10 18:10:54 +03003470 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3471 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3472 kvm_run->fail_entry.hardware_entry_failure_reason
3473 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003474 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3475 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003476 return 0;
3477 }
3478
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003479 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003480 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003481 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3482 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003483 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3484 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003485 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003486 exit_code);
3487
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003488 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003489 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003491 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492 return 0;
3493 }
3494
Avi Kivity851ba692009-08-24 11:10:17 +03003495 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003496}
3497
3498static void reload_tss(struct kvm_vcpu *vcpu)
3499{
3500 int cpu = raw_smp_processor_id();
3501
Tejun Heo0fe1e002009-10-29 22:34:14 +09003502 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3503 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003504 load_TR_desc();
3505}
3506
Rusty Russelle756fc62007-07-30 20:07:08 +10003507static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003508{
3509 int cpu = raw_smp_processor_id();
3510
Tejun Heo0fe1e002009-10-29 22:34:14 +09003511 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003512
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003513 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003514 if (svm->asid_generation != sd->asid_generation)
3515 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516}
3517
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003518static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3519{
3520 struct vcpu_svm *svm = to_svm(vcpu);
3521
3522 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3523 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003524 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003525 ++vcpu->stat.nmi_injections;
3526}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003527
Eddie Dong85f455f2007-07-06 12:20:49 +03003528static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529{
3530 struct vmcb_control_area *control;
3531
Rusty Russelle756fc62007-07-30 20:07:08 +10003532 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003533 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003534 control->int_ctl &= ~V_INTR_PRIO_MASK;
3535 control->int_ctl |= V_IRQ_MASK |
3536 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003537 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003538}
3539
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003540static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003541{
3542 struct vcpu_svm *svm = to_svm(vcpu);
3543
Joerg Roedel2af91942009-08-07 11:49:28 +02003544 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003545
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003546 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3547 ++vcpu->stat.irq_injections;
3548
Alexander Graf219b65d2009-06-15 15:21:25 +02003549 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3550 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003551}
3552
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003553static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3554{
3555 struct vcpu_svm *svm = to_svm(vcpu);
3556
Joerg Roedel20307532010-11-29 17:51:48 +01003557 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003558 return;
3559
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003560 if (irr == -1)
3561 return;
3562
3563 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003564 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003565}
3566
3567static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003568{
3569 struct vcpu_svm *svm = to_svm(vcpu);
3570 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003571 int ret;
3572 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3573 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3574 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3575
3576 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003577}
3578
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003579static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3580{
3581 struct vcpu_svm *svm = to_svm(vcpu);
3582
3583 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3584}
3585
3586static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3587{
3588 struct vcpu_svm *svm = to_svm(vcpu);
3589
3590 if (masked) {
3591 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003592 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003593 } else {
3594 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003595 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003596 }
3597}
3598
Gleb Natapov78646122009-03-23 12:12:11 +02003599static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3600{
3601 struct vcpu_svm *svm = to_svm(vcpu);
3602 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003603 int ret;
3604
3605 if (!gif_set(svm) ||
3606 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3607 return 0;
3608
Avi Kivityf6e78472010-08-02 15:30:20 +03003609 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003610
Joerg Roedel20307532010-11-29 17:51:48 +01003611 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003612 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3613
3614 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003615}
3616
Gleb Natapov9222be12009-04-23 17:14:37 +03003617static void enable_irq_window(struct kvm_vcpu *vcpu)
3618{
Alexander Graf219b65d2009-06-15 15:21:25 +02003619 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003620
Joerg Roedele0231712010-02-24 18:59:10 +01003621 /*
3622 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3623 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3624 * get that intercept, this function will be called again though and
3625 * we'll get the vintr intercept.
3626 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003627 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003628 svm_set_vintr(svm);
3629 svm_inject_irq(svm, 0x0);
3630 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003631}
3632
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003633static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003634{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003635 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003636
Gleb Natapov44c11432009-05-11 13:35:52 +03003637 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3638 == HF_NMI_MASK)
3639 return; /* IRET will cause a vm exit */
3640
Joerg Roedele0231712010-02-24 18:59:10 +01003641 /*
3642 * Something prevents NMI from been injected. Single step over possible
3643 * problem (IRET or exception injection or interrupt shadow)
3644 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003645 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003646 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3647 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003648}
3649
Izik Eiduscbc94022007-10-25 00:29:55 +02003650static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3651{
3652 return 0;
3653}
3654
Avi Kivityd9e368d2007-06-07 19:18:30 +03003655static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3656{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003657 struct vcpu_svm *svm = to_svm(vcpu);
3658
3659 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3660 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3661 else
3662 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003663}
3664
Avi Kivity04d2cc72007-09-10 18:10:54 +03003665static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3666{
3667}
3668
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003669static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3670{
3671 struct vcpu_svm *svm = to_svm(vcpu);
3672
Joerg Roedel20307532010-11-29 17:51:48 +01003673 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003674 return;
3675
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003676 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003677 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003678 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003679 }
3680}
3681
Joerg Roedel649d6862008-04-16 16:51:15 +02003682static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3683{
3684 struct vcpu_svm *svm = to_svm(vcpu);
3685 u64 cr8;
3686
Joerg Roedel20307532010-11-29 17:51:48 +01003687 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003688 return;
3689
Joerg Roedel649d6862008-04-16 16:51:15 +02003690 cr8 = kvm_get_cr8(vcpu);
3691 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3692 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3693}
3694
Gleb Natapov9222be12009-04-23 17:14:37 +03003695static void svm_complete_interrupts(struct vcpu_svm *svm)
3696{
3697 u8 vector;
3698 int type;
3699 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003700 unsigned int3_injected = svm->int3_injected;
3701
3702 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003703
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003704 /*
3705 * If we've made progress since setting HF_IRET_MASK, we've
3706 * executed an IRET and can allow NMI injection.
3707 */
3708 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
3709 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003710 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003711 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3712 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003713
Gleb Natapov9222be12009-04-23 17:14:37 +03003714 svm->vcpu.arch.nmi_injected = false;
3715 kvm_clear_exception_queue(&svm->vcpu);
3716 kvm_clear_interrupt_queue(&svm->vcpu);
3717
3718 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3719 return;
3720
Avi Kivity3842d132010-07-27 12:30:24 +03003721 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3722
Gleb Natapov9222be12009-04-23 17:14:37 +03003723 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3724 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3725
3726 switch (type) {
3727 case SVM_EXITINTINFO_TYPE_NMI:
3728 svm->vcpu.arch.nmi_injected = true;
3729 break;
3730 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003731 /*
3732 * In case of software exceptions, do not reinject the vector,
3733 * but re-execute the instruction instead. Rewind RIP first
3734 * if we emulated INT3 before.
3735 */
3736 if (kvm_exception_is_soft(vector)) {
3737 if (vector == BP_VECTOR && int3_injected &&
3738 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3739 kvm_rip_write(&svm->vcpu,
3740 kvm_rip_read(&svm->vcpu) -
3741 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003742 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003743 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003744 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3745 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003746 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003747
3748 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003749 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003750 break;
3751 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003752 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003753 break;
3754 default:
3755 break;
3756 }
3757}
3758
Avi Kivityb463a6f2010-07-20 15:06:17 +03003759static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3760{
3761 struct vcpu_svm *svm = to_svm(vcpu);
3762 struct vmcb_control_area *control = &svm->vmcb->control;
3763
3764 control->exit_int_info = control->event_inj;
3765 control->exit_int_info_err = control->event_inj_err;
3766 control->event_inj = 0;
3767 svm_complete_interrupts(svm);
3768}
3769
Avi Kivity80e31d42008-07-14 14:44:59 +03003770#ifdef CONFIG_X86_64
3771#define R "r"
3772#else
3773#define R "e"
3774#endif
3775
Avi Kivity851ba692009-08-24 11:10:17 +03003776static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003777{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003778 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003779
Joerg Roedel2041a062010-04-22 12:33:08 +02003780 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3781 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3782 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3783
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003784 /*
3785 * A vmexit emulation is required before the vcpu can be executed
3786 * again.
3787 */
3788 if (unlikely(svm->nested.exit_required))
3789 return;
3790
Rusty Russelle756fc62007-07-30 20:07:08 +10003791 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003792
Joerg Roedel649d6862008-04-16 16:51:15 +02003793 sync_lapic_to_cr8(vcpu);
3794
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003795 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796
Avi Kivity04d2cc72007-09-10 18:10:54 +03003797 clgi();
3798
3799 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003800
Avi Kivity6aa8b732006-12-10 02:21:36 -08003801 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003802 "push %%"R"bp; \n\t"
3803 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3804 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3805 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3806 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3807 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3808 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003809#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003810 "mov %c[r8](%[svm]), %%r8 \n\t"
3811 "mov %c[r9](%[svm]), %%r9 \n\t"
3812 "mov %c[r10](%[svm]), %%r10 \n\t"
3813 "mov %c[r11](%[svm]), %%r11 \n\t"
3814 "mov %c[r12](%[svm]), %%r12 \n\t"
3815 "mov %c[r13](%[svm]), %%r13 \n\t"
3816 "mov %c[r14](%[svm]), %%r14 \n\t"
3817 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003818#endif
3819
Avi Kivity6aa8b732006-12-10 02:21:36 -08003820 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003821 "push %%"R"ax \n\t"
3822 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003823 __ex(SVM_VMLOAD) "\n\t"
3824 __ex(SVM_VMRUN) "\n\t"
3825 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003826 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003827
3828 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003829 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3830 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3831 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3832 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3833 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3834 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003835#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003836 "mov %%r8, %c[r8](%[svm]) \n\t"
3837 "mov %%r9, %c[r9](%[svm]) \n\t"
3838 "mov %%r10, %c[r10](%[svm]) \n\t"
3839 "mov %%r11, %c[r11](%[svm]) \n\t"
3840 "mov %%r12, %c[r12](%[svm]) \n\t"
3841 "mov %%r13, %c[r13](%[svm]) \n\t"
3842 "mov %%r14, %c[r14](%[svm]) \n\t"
3843 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003845 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003846 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003847 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003848 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003849 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3850 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3851 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3852 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3853 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3854 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003855#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003856 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3857 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3858 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3859 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3860 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3861 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3862 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3863 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003865 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003866 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003867#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003868 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3869#endif
3870 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871
Avi Kivity82ca2d12010-10-21 12:20:34 +02003872#ifdef CONFIG_X86_64
3873 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3874#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003875 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02003876#ifndef CONFIG_X86_32_LAZY_GS
3877 loadsegment(gs, svm->host.gs);
3878#endif
Avi Kivity9581d442010-10-19 16:46:55 +02003879#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880
3881 reload_tss(vcpu);
3882
Avi Kivity56ba47d2007-11-07 17:14:18 +02003883 local_irq_disable();
3884
Avi Kivity13c34e02010-10-21 12:20:31 +02003885 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3886 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3887 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3888 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3889
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02003890 trace_kvm_exit(svm->vmcb->control.exit_code, vcpu, KVM_ISA_SVM);
3891
Joerg Roedel3781c012011-01-14 16:45:02 +01003892 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3893 kvm_before_handle_nmi(&svm->vcpu);
3894
3895 stgi();
3896
3897 /* Any pending NMI will happen here */
3898
3899 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3900 kvm_after_handle_nmi(&svm->vcpu);
3901
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003902 sync_cr8_to_lapic(vcpu);
3903
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003904 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003905
Joerg Roedel38e5e922010-12-03 15:25:16 +01003906 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3907
Gleb Natapov631bc482010-10-14 11:22:52 +02003908 /* if exit due to PF check for async PF */
3909 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3910 svm->apf_reason = kvm_read_and_reset_pf_reason();
3911
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003912 if (npt_enabled) {
3913 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3914 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3915 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003916
3917 /*
3918 * We need to handle MC intercepts here before the vcpu has a chance to
3919 * change the physical cpu
3920 */
3921 if (unlikely(svm->vmcb->control.exit_code ==
3922 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3923 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003924
3925 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003926}
3927
Avi Kivity80e31d42008-07-14 14:44:59 +03003928#undef R
3929
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3931{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003932 struct vcpu_svm *svm = to_svm(vcpu);
3933
3934 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003935 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003936 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003937}
3938
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003939static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3940{
3941 struct vcpu_svm *svm = to_svm(vcpu);
3942
3943 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01003944 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003945
3946 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02003947 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003948 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003949
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003950 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003951}
3952
Avi Kivity6aa8b732006-12-10 02:21:36 -08003953static int is_disabled(void)
3954{
Joerg Roedel6031a612007-06-22 12:29:50 +03003955 u64 vm_cr;
3956
3957 rdmsrl(MSR_VM_CR, vm_cr);
3958 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3959 return 1;
3960
Avi Kivity6aa8b732006-12-10 02:21:36 -08003961 return 0;
3962}
3963
Ingo Molnar102d8322007-02-19 14:37:47 +02003964static void
3965svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3966{
3967 /*
3968 * Patch in the VMMCALL instruction:
3969 */
3970 hypercall[0] = 0x0f;
3971 hypercall[1] = 0x01;
3972 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003973}
3974
Yang, Sheng002c7f72007-07-31 14:23:01 +03003975static void svm_check_processor_compat(void *rtn)
3976{
3977 *(int *)rtn = 0;
3978}
3979
Avi Kivity774ead32007-12-26 13:57:04 +02003980static bool svm_cpu_has_accelerated_tpr(void)
3981{
3982 return false;
3983}
3984
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003985static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003986{
3987 return 0;
3988}
3989
Sheng Yang0e851882009-12-18 16:48:46 +08003990static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3991{
3992}
3993
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003994static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3995{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003996 switch (func) {
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02003997 case 0x80000001:
3998 if (nested)
3999 entry->ecx |= (1 << 2); /* Set SVM bit */
4000 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004001 case 0x8000000A:
4002 entry->eax = 1; /* SVM revision 1 */
4003 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
4004 ASID emulation to nested SVM */
4005 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02004006 entry->edx = 0; /* Per default do not support any
4007 additional features */
4008
4009 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004010 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02004011 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004012
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02004013 /* Support NPT for the guest if enabled */
4014 if (npt_enabled)
4015 entry->edx |= SVM_FEATURE_NPT;
4016
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004017 break;
4018 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004019}
4020
Sheng Yang17cc3932010-01-05 19:02:27 +08004021static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004022{
Sheng Yang17cc3932010-01-05 19:02:27 +08004023 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004024}
4025
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004026static bool svm_rdtscp_supported(void)
4027{
4028 return false;
4029}
4030
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004031static bool svm_has_wbinvd_exit(void)
4032{
4033 return true;
4034}
4035
Avi Kivity02daab22009-12-30 12:40:26 +02004036static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
4037{
4038 struct vcpu_svm *svm = to_svm(vcpu);
4039
Joerg Roedel18c918c2010-11-30 18:03:59 +01004040 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01004041 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02004042}
4043
Joerg Roedel80612522011-04-04 12:39:33 +02004044#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004045 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004046#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004047 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004048#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004049 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004050
4051static struct __x86_intercept {
4052 u32 exit_code;
4053 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004054} x86_intercept_map[] = {
4055 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4056 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4057 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4058 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4059 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004060 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4061 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004062 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4063 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4064 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4065 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4066 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4067 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4068 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4069 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004070 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4071 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4072 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4073 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4074 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4075 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4076 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4077 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004078 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4079 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4080 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004081 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4082 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4083 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4084 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4085 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4086 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4087 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4088 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4089 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004090 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4091 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4092 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4093 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4094 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4095 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4096 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004097 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4098 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4099 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4100 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004101};
4102
Joerg Roedel80612522011-04-04 12:39:33 +02004103#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004104#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004105#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004106
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004107static int svm_check_intercept(struct kvm_vcpu *vcpu,
4108 struct x86_instruction_info *info,
4109 enum x86_intercept_stage stage)
4110{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004111 struct vcpu_svm *svm = to_svm(vcpu);
4112 int vmexit, ret = X86EMUL_CONTINUE;
4113 struct __x86_intercept icpt_info;
4114 struct vmcb *vmcb = svm->vmcb;
4115
4116 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4117 goto out;
4118
4119 icpt_info = x86_intercept_map[info->intercept];
4120
Avi Kivity40e19b52011-04-21 12:35:41 +03004121 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004122 goto out;
4123
4124 switch (icpt_info.exit_code) {
4125 case SVM_EXIT_READ_CR0:
4126 if (info->intercept == x86_intercept_cr_read)
4127 icpt_info.exit_code += info->modrm_reg;
4128 break;
4129 case SVM_EXIT_WRITE_CR0: {
4130 unsigned long cr0, val;
4131 u64 intercept;
4132
4133 if (info->intercept == x86_intercept_cr_write)
4134 icpt_info.exit_code += info->modrm_reg;
4135
4136 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0)
4137 break;
4138
4139 intercept = svm->nested.intercept;
4140
4141 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
4142 break;
4143
4144 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4145 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4146
4147 if (info->intercept == x86_intercept_lmsw) {
4148 cr0 &= 0xfUL;
4149 val &= 0xfUL;
4150 /* lmsw can't clear PE - catch this here */
4151 if (cr0 & X86_CR0_PE)
4152 val |= X86_CR0_PE;
4153 }
4154
4155 if (cr0 ^ val)
4156 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4157
4158 break;
4159 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004160 case SVM_EXIT_READ_DR0:
4161 case SVM_EXIT_WRITE_DR0:
4162 icpt_info.exit_code += info->modrm_reg;
4163 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004164 case SVM_EXIT_MSR:
4165 if (info->intercept == x86_intercept_wrmsr)
4166 vmcb->control.exit_info_1 = 1;
4167 else
4168 vmcb->control.exit_info_1 = 0;
4169 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004170 case SVM_EXIT_PAUSE:
4171 /*
4172 * We get this for NOP only, but pause
4173 * is rep not, check this here
4174 */
4175 if (info->rep_prefix != REPE_PREFIX)
4176 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02004177 case SVM_EXIT_IOIO: {
4178 u64 exit_info;
4179 u32 bytes;
4180
4181 exit_info = (vcpu->arch.regs[VCPU_REGS_RDX] & 0xffff) << 16;
4182
4183 if (info->intercept == x86_intercept_in ||
4184 info->intercept == x86_intercept_ins) {
4185 exit_info |= SVM_IOIO_TYPE_MASK;
4186 bytes = info->src_bytes;
4187 } else {
4188 bytes = info->dst_bytes;
4189 }
4190
4191 if (info->intercept == x86_intercept_outs ||
4192 info->intercept == x86_intercept_ins)
4193 exit_info |= SVM_IOIO_STR_MASK;
4194
4195 if (info->rep_prefix)
4196 exit_info |= SVM_IOIO_REP_MASK;
4197
4198 bytes = min(bytes, 4u);
4199
4200 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4201
4202 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4203
4204 vmcb->control.exit_info_1 = exit_info;
4205 vmcb->control.exit_info_2 = info->next_rip;
4206
4207 break;
4208 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004209 default:
4210 break;
4211 }
4212
4213 vmcb->control.next_rip = info->next_rip;
4214 vmcb->control.exit_code = icpt_info.exit_code;
4215 vmexit = nested_svm_exit_handled(svm);
4216
4217 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4218 : X86EMUL_CONTINUE;
4219
4220out:
4221 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004222}
4223
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004224static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004225 .cpu_has_kvm_support = has_svm,
4226 .disabled_by_bios = is_disabled,
4227 .hardware_setup = svm_hardware_setup,
4228 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004229 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004230 .hardware_enable = svm_hardware_enable,
4231 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004232 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004233
4234 .vcpu_create = svm_create_vcpu,
4235 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004236 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237
Avi Kivity04d2cc72007-09-10 18:10:54 +03004238 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004239 .vcpu_load = svm_vcpu_load,
4240 .vcpu_put = svm_vcpu_put,
4241
4242 .set_guest_debug = svm_guest_debug,
4243 .get_msr = svm_get_msr,
4244 .set_msr = svm_set_msr,
4245 .get_segment_base = svm_get_segment_base,
4246 .get_segment = svm_get_segment,
4247 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004248 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004249 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004250 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004251 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004252 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004253 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004254 .set_cr3 = svm_set_cr3,
4255 .set_cr4 = svm_set_cr4,
4256 .set_efer = svm_set_efer,
4257 .get_idt = svm_get_idt,
4258 .set_idt = svm_set_idt,
4259 .get_gdt = svm_get_gdt,
4260 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004261 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004262 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004263 .get_rflags = svm_get_rflags,
4264 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02004265 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004266 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004267
Avi Kivity6aa8b732006-12-10 02:21:36 -08004268 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004269
Avi Kivity6aa8b732006-12-10 02:21:36 -08004270 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004271 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004273 .set_interrupt_shadow = svm_set_interrupt_shadow,
4274 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004275 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004276 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004277 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004278 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004279 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004280 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004281 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004282 .get_nmi_mask = svm_get_nmi_mask,
4283 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004284 .enable_nmi_window = enable_nmi_window,
4285 .enable_irq_window = enable_irq_window,
4286 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02004287
4288 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004289 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004290 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004291
Avi Kivity586f9602010-11-18 13:09:54 +02004292 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004293
Sheng Yang17cc3932010-01-05 19:02:27 +08004294 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004295
4296 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004297
4298 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004299
4300 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004301
4302 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004303
Joerg Roedel4051b182011-03-25 09:44:49 +01004304 .set_tsc_khz = svm_set_tsc_khz,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004305 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004306 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01004307 .compute_tsc_offset = svm_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03004308 .read_l1_tsc = svm_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004309
4310 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004311
4312 .check_intercept = svm_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004313};
4314
4315static int __init svm_init(void)
4316{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004317 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004318 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004319}
4320
4321static void __exit svm_exit(void)
4322{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004323 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004324}
4325
4326module_init(svm_init)
4327module_exit(svm_exit)