blob: 0b7690ee20bdafa722fb160b25e214290d11915c [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
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1336{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001337 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338}
1339
1340static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1341{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001342 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343}
1344
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001345static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1346{
1347 switch (reg) {
1348 case VCPU_EXREG_PDPTR:
1349 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001350 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001351 break;
1352 default:
1353 BUG();
1354 }
1355}
1356
Alexander Graff0b85052008-11-25 20:17:01 +01001357static void svm_set_vintr(struct vcpu_svm *svm)
1358{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001359 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001360}
1361
1362static void svm_clear_vintr(struct vcpu_svm *svm)
1363{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001364 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001365}
1366
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1368{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001369 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370
1371 switch (seg) {
1372 case VCPU_SREG_CS: return &save->cs;
1373 case VCPU_SREG_DS: return &save->ds;
1374 case VCPU_SREG_ES: return &save->es;
1375 case VCPU_SREG_FS: return &save->fs;
1376 case VCPU_SREG_GS: return &save->gs;
1377 case VCPU_SREG_SS: return &save->ss;
1378 case VCPU_SREG_TR: return &save->tr;
1379 case VCPU_SREG_LDTR: return &save->ldtr;
1380 }
1381 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001382 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383}
1384
1385static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1386{
1387 struct vmcb_seg *s = svm_seg(vcpu, seg);
1388
1389 return s->base;
1390}
1391
1392static void svm_get_segment(struct kvm_vcpu *vcpu,
1393 struct kvm_segment *var, int seg)
1394{
1395 struct vmcb_seg *s = svm_seg(vcpu, seg);
1396
1397 var->base = s->base;
1398 var->limit = s->limit;
1399 var->selector = s->selector;
1400 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1401 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1402 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1403 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1404 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1405 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1406 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1407 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001408
Joerg Roedele0231712010-02-24 18:59:10 +01001409 /*
1410 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001411 * for cross vendor migration purposes by "not present"
1412 */
1413 var->unusable = !var->present || (var->type == 0);
1414
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001415 switch (seg) {
1416 case VCPU_SREG_CS:
1417 /*
1418 * SVM always stores 0 for the 'G' bit in the CS selector in
1419 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1420 * Intel's VMENTRY has a check on the 'G' bit.
1421 */
Amit Shah25022ac2008-10-27 09:04:17 +00001422 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001423 break;
1424 case VCPU_SREG_TR:
1425 /*
1426 * Work around a bug where the busy flag in the tr selector
1427 * isn't exposed
1428 */
Amit Shahc0d09822008-10-27 09:04:18 +00001429 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001430 break;
1431 case VCPU_SREG_DS:
1432 case VCPU_SREG_ES:
1433 case VCPU_SREG_FS:
1434 case VCPU_SREG_GS:
1435 /*
1436 * The accessed bit must always be set in the segment
1437 * descriptor cache, although it can be cleared in the
1438 * descriptor, the cached bit always remains at 1. Since
1439 * Intel has a check on this, set it here to support
1440 * cross-vendor migration.
1441 */
1442 if (!var->unusable)
1443 var->type |= 0x1;
1444 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001445 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001446 /*
1447 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001448 * descriptor is left as 1, although the whole segment has
1449 * been made unusable. Clear it here to pass an Intel VMX
1450 * entry check when cross vendor migrating.
1451 */
1452 if (var->unusable)
1453 var->db = 0;
1454 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001455 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456}
1457
Izik Eidus2e4d2652008-03-24 19:38:34 +02001458static int svm_get_cpl(struct kvm_vcpu *vcpu)
1459{
1460 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1461
1462 return save->cpl;
1463}
1464
Gleb Natapov89a27f42010-02-16 10:51:48 +02001465static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001467 struct vcpu_svm *svm = to_svm(vcpu);
1468
Gleb Natapov89a27f42010-02-16 10:51:48 +02001469 dt->size = svm->vmcb->save.idtr.limit;
1470 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471}
1472
Gleb Natapov89a27f42010-02-16 10:51:48 +02001473static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001475 struct vcpu_svm *svm = to_svm(vcpu);
1476
Gleb Natapov89a27f42010-02-16 10:51:48 +02001477 svm->vmcb->save.idtr.limit = dt->size;
1478 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001479 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480}
1481
Gleb Natapov89a27f42010-02-16 10:51:48 +02001482static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001484 struct vcpu_svm *svm = to_svm(vcpu);
1485
Gleb Natapov89a27f42010-02-16 10:51:48 +02001486 dt->size = svm->vmcb->save.gdtr.limit;
1487 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488}
1489
Gleb Natapov89a27f42010-02-16 10:51:48 +02001490static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001492 struct vcpu_svm *svm = to_svm(vcpu);
1493
Gleb Natapov89a27f42010-02-16 10:51:48 +02001494 svm->vmcb->save.gdtr.limit = dt->size;
1495 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001496 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497}
1498
Avi Kivitye8467fd2009-12-29 18:43:06 +02001499static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1500{
1501}
1502
Avi Kivityaff48ba2010-12-05 18:56:11 +02001503static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1504{
1505}
1506
Anthony Liguori25c4c272007-04-27 09:29:21 +03001507static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001508{
1509}
1510
Avi Kivityd2251572010-01-06 10:55:27 +02001511static void update_cr0_intercept(struct vcpu_svm *svm)
1512{
1513 ulong gcr0 = svm->vcpu.arch.cr0;
1514 u64 *hcr0 = &svm->vmcb->save.cr0;
1515
1516 if (!svm->vcpu.fpu_active)
1517 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1518 else
1519 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1520 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1521
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001522 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001523
1524 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001525 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1526 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001527 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001528 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1529 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001530 }
1531}
1532
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1534{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001535 struct vcpu_svm *svm = to_svm(vcpu);
1536
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001537#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001538 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001539 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001540 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001541 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001542 }
1543
Mike Dayd77c26f2007-10-08 09:02:08 -04001544 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001545 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001546 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547 }
1548 }
1549#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001550 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001551
1552 if (!npt_enabled)
1553 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001554
1555 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001556 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001557 /*
1558 * re-enable caching here because the QEMU bios
1559 * does not do it - this results in some delay at
1560 * reboot
1561 */
1562 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001563 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001564 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001565 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566}
1567
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001568static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569{
Joerg Roedel6394b642008-04-09 14:15:29 +02001570 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001571 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1572
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001573 if (cr4 & X86_CR4_VMXE)
1574 return 1;
1575
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001576 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001577 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001578
Joerg Roedelec077262008-04-09 14:15:28 +02001579 vcpu->arch.cr4 = cr4;
1580 if (!npt_enabled)
1581 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001582 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001583 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001584 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001585 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586}
1587
1588static void svm_set_segment(struct kvm_vcpu *vcpu,
1589 struct kvm_segment *var, int seg)
1590{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001591 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592 struct vmcb_seg *s = svm_seg(vcpu, seg);
1593
1594 s->base = var->base;
1595 s->limit = var->limit;
1596 s->selector = var->selector;
1597 if (var->unusable)
1598 s->attrib = 0;
1599 else {
1600 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1601 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1602 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1603 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1604 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1605 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1606 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1607 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1608 }
1609 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001610 svm->vmcb->save.cpl
1611 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1613
Joerg Roedel060d0c92010-12-03 11:45:57 +01001614 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615}
1616
Gleb Natapov44c11432009-05-11 13:35:52 +03001617static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001619 struct vcpu_svm *svm = to_svm(vcpu);
1620
Joerg Roedel18c918c2010-11-30 18:03:59 +01001621 clr_exception_intercept(svm, DB_VECTOR);
1622 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001623
Jan Kiszka6be7d302009-10-18 13:24:54 +02001624 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001625 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001626
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001627 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1628 if (vcpu->guest_debug &
1629 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001630 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001631 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001632 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001633 } else
1634 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001635}
1636
Jan Kiszka355be0b2009-10-03 00:31:21 +02001637static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001638{
Gleb Natapov44c11432009-05-11 13:35:52 +03001639 struct vcpu_svm *svm = to_svm(vcpu);
1640
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001641 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1642 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1643 else
1644 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1645
Joerg Roedel72214b92010-12-03 11:45:55 +01001646 mark_dirty(svm->vmcb, VMCB_DR);
1647
Jan Kiszka355be0b2009-10-03 00:31:21 +02001648 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649}
1650
Tejun Heo0fe1e002009-10-29 22:34:14 +09001651static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001653 if (sd->next_asid > sd->max_asid) {
1654 ++sd->asid_generation;
1655 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001656 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657 }
1658
Tejun Heo0fe1e002009-10-29 22:34:14 +09001659 svm->asid_generation = sd->asid_generation;
1660 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001661
1662 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663}
1664
Gleb Natapov020df072010-04-13 10:05:23 +03001665static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001666{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001667 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001668
Gleb Natapov020df072010-04-13 10:05:23 +03001669 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01001670 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671}
1672
Avi Kivity851ba692009-08-24 11:10:17 +03001673static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674{
Gleb Natapov631bc482010-10-14 11:22:52 +02001675 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001676 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001677 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678
Gleb Natapov631bc482010-10-14 11:22:52 +02001679 switch (svm->apf_reason) {
1680 default:
1681 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001682
Gleb Natapov631bc482010-10-14 11:22:52 +02001683 trace_kvm_page_fault(fault_address, error_code);
1684 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1685 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01001686 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1687 svm->vmcb->control.insn_bytes,
1688 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02001689 break;
1690 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1691 svm->apf_reason = 0;
1692 local_irq_disable();
1693 kvm_async_pf_task_wait(fault_address);
1694 local_irq_enable();
1695 break;
1696 case KVM_PV_REASON_PAGE_READY:
1697 svm->apf_reason = 0;
1698 local_irq_disable();
1699 kvm_async_pf_task_wake(fault_address);
1700 local_irq_enable();
1701 break;
1702 }
1703 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704}
1705
Avi Kivity851ba692009-08-24 11:10:17 +03001706static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001707{
Avi Kivity851ba692009-08-24 11:10:17 +03001708 struct kvm_run *kvm_run = svm->vcpu.run;
1709
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001710 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001711 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001712 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001713 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1714 return 1;
1715 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001716
Jan Kiszka6be7d302009-10-18 13:24:54 +02001717 if (svm->nmi_singlestep) {
1718 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001719 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1720 svm->vmcb->save.rflags &=
1721 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1722 update_db_intercept(&svm->vcpu);
1723 }
1724
1725 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001726 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001727 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1728 kvm_run->debug.arch.pc =
1729 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1730 kvm_run->debug.arch.exception = DB_VECTOR;
1731 return 0;
1732 }
1733
1734 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001735}
1736
Avi Kivity851ba692009-08-24 11:10:17 +03001737static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001738{
Avi Kivity851ba692009-08-24 11:10:17 +03001739 struct kvm_run *kvm_run = svm->vcpu.run;
1740
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001741 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1742 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1743 kvm_run->debug.arch.exception = BP_VECTOR;
1744 return 0;
1745}
1746
Avi Kivity851ba692009-08-24 11:10:17 +03001747static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001748{
1749 int er;
1750
Andre Przywara51d8b662010-12-21 11:12:02 +01001751 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001752 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001753 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001754 return 1;
1755}
1756
Avi Kivity6b52d182010-01-21 15:31:47 +02001757static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001758{
Avi Kivity6b52d182010-01-21 15:31:47 +02001759 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001760
Joerg Roedel18c918c2010-11-30 18:03:59 +01001761 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001762
Rusty Russelle756fc62007-07-30 20:07:08 +10001763 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001764 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001765}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001766
Avi Kivity6b52d182010-01-21 15:31:47 +02001767static int nm_interception(struct vcpu_svm *svm)
1768{
1769 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001770 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001771}
1772
Joerg Roedel67ec6602010-05-17 14:43:35 +02001773static bool is_erratum_383(void)
1774{
1775 int err, i;
1776 u64 value;
1777
1778 if (!erratum_383_found)
1779 return false;
1780
1781 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1782 if (err)
1783 return false;
1784
1785 /* Bit 62 may or may not be set for this mce */
1786 value &= ~(1ULL << 62);
1787
1788 if (value != 0xb600000000010015ULL)
1789 return false;
1790
1791 /* Clear MCi_STATUS registers */
1792 for (i = 0; i < 6; ++i)
1793 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1794
1795 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1796 if (!err) {
1797 u32 low, high;
1798
1799 value &= ~(1ULL << 2);
1800 low = lower_32_bits(value);
1801 high = upper_32_bits(value);
1802
1803 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1804 }
1805
1806 /* Flush tlb to evict multi-match entries */
1807 __flush_tlb_all();
1808
1809 return true;
1810}
1811
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001812static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001813{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001814 if (is_erratum_383()) {
1815 /*
1816 * Erratum 383 triggered. Guest state is corrupt so kill the
1817 * guest.
1818 */
1819 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1820
Avi Kivitya8eeb042010-05-10 12:34:53 +03001821 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001822
1823 return;
1824 }
1825
Joerg Roedel53371b52008-04-09 14:15:30 +02001826 /*
1827 * On an #MC intercept the MCE handler is not called automatically in
1828 * the host. So do it by hand here.
1829 */
1830 asm volatile (
1831 "int $0x12\n");
1832 /* not sure if we ever come back to this point */
1833
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001834 return;
1835}
1836
1837static int mc_interception(struct vcpu_svm *svm)
1838{
Joerg Roedel53371b52008-04-09 14:15:30 +02001839 return 1;
1840}
1841
Avi Kivity851ba692009-08-24 11:10:17 +03001842static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001843{
Avi Kivity851ba692009-08-24 11:10:17 +03001844 struct kvm_run *kvm_run = svm->vcpu.run;
1845
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001846 /*
1847 * VMCB is undefined after a SHUTDOWN intercept
1848 * so reinitialize it.
1849 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001850 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001851 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001852
1853 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1854 return 0;
1855}
1856
Avi Kivity851ba692009-08-24 11:10:17 +03001857static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001859 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001860 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001861 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001862 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863
Rusty Russelle756fc62007-07-30 20:07:08 +10001864 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001865 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001866 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001867 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01001868 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001869
Avi Kivity039576c2007-03-20 12:46:50 +02001870 port = io_info >> 16;
1871 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001872 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001873 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001874
1875 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001876}
1877
Avi Kivity851ba692009-08-24 11:10:17 +03001878static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001879{
1880 return 1;
1881}
1882
Avi Kivity851ba692009-08-24 11:10:17 +03001883static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001884{
1885 ++svm->vcpu.stat.irq_exits;
1886 return 1;
1887}
1888
Avi Kivity851ba692009-08-24 11:10:17 +03001889static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001890{
1891 return 1;
1892}
1893
Avi Kivity851ba692009-08-24 11:10:17 +03001894static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001896 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001897 skip_emulated_instruction(&svm->vcpu);
1898 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899}
1900
Avi Kivity851ba692009-08-24 11:10:17 +03001901static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001902{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001903 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001904 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001905 kvm_emulate_hypercall(&svm->vcpu);
1906 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001907}
1908
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001909static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1910{
1911 struct vcpu_svm *svm = to_svm(vcpu);
1912
1913 return svm->nested.nested_cr3;
1914}
1915
Avi Kivitye4e517b2011-07-28 11:36:17 +03001916static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
1917{
1918 struct vcpu_svm *svm = to_svm(vcpu);
1919 u64 cr3 = svm->nested.nested_cr3;
1920 u64 pdpte;
1921 int ret;
1922
1923 ret = kvm_read_guest_page(vcpu->kvm, gpa_to_gfn(cr3), &pdpte,
1924 offset_in_page(cr3) + index * 8, 8);
1925 if (ret)
1926 return 0;
1927 return pdpte;
1928}
1929
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001930static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1931 unsigned long root)
1932{
1933 struct vcpu_svm *svm = to_svm(vcpu);
1934
1935 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01001936 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001937 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001938}
1939
Avi Kivity6389ee92010-11-29 16:12:30 +02001940static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1941 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001942{
1943 struct vcpu_svm *svm = to_svm(vcpu);
1944
1945 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1946 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001947 svm->vmcb->control.exit_info_1 = fault->error_code;
1948 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001949
1950 nested_svm_vmexit(svm);
1951}
1952
Joerg Roedel4b161842010-09-10 17:31:03 +02001953static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1954{
1955 int r;
1956
1957 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1958
1959 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1960 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03001961 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02001962 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1963 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1964 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1965
1966 return r;
1967}
1968
1969static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1970{
1971 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1972}
1973
Alexander Grafc0725422008-11-25 20:17:03 +01001974static int nested_svm_check_permissions(struct vcpu_svm *svm)
1975{
Avi Kivityf6801df2010-01-21 15:31:50 +02001976 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001977 || !is_paging(&svm->vcpu)) {
1978 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1979 return 1;
1980 }
1981
1982 if (svm->vmcb->save.cpl) {
1983 kvm_inject_gp(&svm->vcpu, 0);
1984 return 1;
1985 }
1986
1987 return 0;
1988}
1989
Alexander Grafcf74a782008-11-25 20:17:08 +01001990static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1991 bool has_error_code, u32 error_code)
1992{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001993 int vmexit;
1994
Joerg Roedel20307532010-11-29 17:51:48 +01001995 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02001996 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001997
Joerg Roedel0295ad72009-08-07 11:49:37 +02001998 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1999 svm->vmcb->control.exit_code_hi = 0;
2000 svm->vmcb->control.exit_info_1 = error_code;
2001 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
2002
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002003 vmexit = nested_svm_intercept(svm);
2004 if (vmexit == NESTED_EXIT_DONE)
2005 svm->nested.exit_required = true;
2006
2007 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002008}
2009
Joerg Roedel8fe54652010-02-19 16:23:01 +01002010/* This function returns true if it is save to enable the irq window */
2011static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002012{
Joerg Roedel20307532010-11-29 17:51:48 +01002013 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002014 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002015
Joerg Roedel26666952009-08-07 11:49:46 +02002016 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002017 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002018
Joerg Roedel26666952009-08-07 11:49:46 +02002019 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002020 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002021
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002022 /*
2023 * if vmexit was already requested (by intercepted exception
2024 * for instance) do not overwrite it with "external interrupt"
2025 * vmexit.
2026 */
2027 if (svm->nested.exit_required)
2028 return false;
2029
Joerg Roedel197717d2010-02-24 18:59:19 +01002030 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2031 svm->vmcb->control.exit_info_1 = 0;
2032 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002033
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002034 if (svm->nested.intercept & 1ULL) {
2035 /*
2036 * The #vmexit can't be emulated here directly because this
2037 * code path runs with irqs and preemtion disabled. A
2038 * #vmexit emulation might sleep. Only signal request for
2039 * the #vmexit here.
2040 */
2041 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002042 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002043 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002044 }
2045
Joerg Roedel8fe54652010-02-19 16:23:01 +01002046 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002047}
2048
Joerg Roedel887f5002010-02-24 18:59:12 +01002049/* This function returns true if it is save to enable the nmi window */
2050static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2051{
Joerg Roedel20307532010-11-29 17:51:48 +01002052 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002053 return true;
2054
2055 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2056 return true;
2057
2058 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2059 svm->nested.exit_required = true;
2060
2061 return false;
2062}
2063
Joerg Roedel7597f122010-02-19 16:23:00 +01002064static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002065{
2066 struct page *page;
2067
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002068 might_sleep();
2069
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002070 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002071 if (is_error_page(page))
2072 goto error;
2073
Joerg Roedel7597f122010-02-19 16:23:00 +01002074 *_page = page;
2075
2076 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002077
2078error:
2079 kvm_release_page_clean(page);
2080 kvm_inject_gp(&svm->vcpu, 0);
2081
2082 return NULL;
2083}
2084
Joerg Roedel7597f122010-02-19 16:23:00 +01002085static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002086{
Joerg Roedel7597f122010-02-19 16:23:00 +01002087 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002088 kvm_release_page_dirty(page);
2089}
2090
Joerg Roedelce2ac082010-03-01 15:34:39 +01002091static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002092{
Joerg Roedelce2ac082010-03-01 15:34:39 +01002093 unsigned port;
2094 u8 val, bit;
2095 u64 gpa;
2096
2097 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2098 return NESTED_EXIT_HOST;
2099
2100 port = svm->vmcb->control.exit_info_1 >> 16;
2101 gpa = svm->nested.vmcb_iopm + (port / 8);
2102 bit = port % 8;
2103 val = 0;
2104
2105 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
2106 val &= (1 << bit);
2107
2108 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
2109}
2110
Joerg Roedeld2477822010-03-01 15:34:34 +01002111static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002112{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002113 u32 offset, msr, value;
2114 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002115
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002116 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002117 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002118
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002119 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2120 offset = svm_msrpm_offset(msr);
2121 write = svm->vmcb->control.exit_info_1 & 1;
2122 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002123
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002124 if (offset == MSR_INVALID)
2125 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002126
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002127 /* Offset is in 32 bit units but need in 8 bit units */
2128 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002129
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002130 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
2131 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002132
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002133 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002134}
2135
Joerg Roedel410e4d52009-08-07 11:49:44 +02002136static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002137{
Alexander Grafcf74a782008-11-25 20:17:08 +01002138 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002139
Joerg Roedel410e4d52009-08-07 11:49:44 +02002140 switch (exit_code) {
2141 case SVM_EXIT_INTR:
2142 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002143 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002144 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002145 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002146 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002147 if (npt_enabled)
2148 return NESTED_EXIT_HOST;
2149 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002150 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002151 /* When we're shadowing, trap PFs, but not async PF */
2152 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002153 return NESTED_EXIT_HOST;
2154 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01002155 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
2156 nm_interception(svm);
2157 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002158 default:
2159 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002160 }
2161
Joerg Roedel410e4d52009-08-07 11:49:44 +02002162 return NESTED_EXIT_CONTINUE;
2163}
2164
2165/*
2166 * If this function returns true, this #vmexit was already handled
2167 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002168static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002169{
2170 u32 exit_code = svm->vmcb->control.exit_code;
2171 int vmexit = NESTED_EXIT_HOST;
2172
Alexander Grafcf74a782008-11-25 20:17:08 +01002173 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002174 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002175 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002176 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002177 case SVM_EXIT_IOIO:
2178 vmexit = nested_svm_intercept_ioio(svm);
2179 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002180 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2181 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2182 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002183 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002184 break;
2185 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002186 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2187 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2188 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002189 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002190 break;
2191 }
2192 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2193 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002194 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002195 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002196 /* async page fault always cause vmexit */
2197 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2198 svm->apf_reason != 0)
2199 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002200 break;
2201 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002202 case SVM_EXIT_ERR: {
2203 vmexit = NESTED_EXIT_DONE;
2204 break;
2205 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002206 default: {
2207 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002208 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002209 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002210 }
2211 }
2212
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002213 return vmexit;
2214}
2215
2216static int nested_svm_exit_handled(struct vcpu_svm *svm)
2217{
2218 int vmexit;
2219
2220 vmexit = nested_svm_intercept(svm);
2221
2222 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002223 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002224
2225 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002226}
2227
Joerg Roedel0460a972009-08-07 11:49:31 +02002228static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2229{
2230 struct vmcb_control_area *dst = &dst_vmcb->control;
2231 struct vmcb_control_area *from = &from_vmcb->control;
2232
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002233 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002234 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002235 dst->intercept_exceptions = from->intercept_exceptions;
2236 dst->intercept = from->intercept;
2237 dst->iopm_base_pa = from->iopm_base_pa;
2238 dst->msrpm_base_pa = from->msrpm_base_pa;
2239 dst->tsc_offset = from->tsc_offset;
2240 dst->asid = from->asid;
2241 dst->tlb_ctl = from->tlb_ctl;
2242 dst->int_ctl = from->int_ctl;
2243 dst->int_vector = from->int_vector;
2244 dst->int_state = from->int_state;
2245 dst->exit_code = from->exit_code;
2246 dst->exit_code_hi = from->exit_code_hi;
2247 dst->exit_info_1 = from->exit_info_1;
2248 dst->exit_info_2 = from->exit_info_2;
2249 dst->exit_int_info = from->exit_int_info;
2250 dst->exit_int_info_err = from->exit_int_info_err;
2251 dst->nested_ctl = from->nested_ctl;
2252 dst->event_inj = from->event_inj;
2253 dst->event_inj_err = from->event_inj_err;
2254 dst->nested_cr3 = from->nested_cr3;
2255 dst->lbr_ctl = from->lbr_ctl;
2256}
2257
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002258static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002259{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002260 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002261 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002262 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002263 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002264
Joerg Roedel17897f32009-10-09 16:08:29 +02002265 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2266 vmcb->control.exit_info_1,
2267 vmcb->control.exit_info_2,
2268 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002269 vmcb->control.exit_int_info_err,
2270 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002271
Joerg Roedel7597f122010-02-19 16:23:00 +01002272 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002273 if (!nested_vmcb)
2274 return 1;
2275
Joerg Roedel20307532010-11-29 17:51:48 +01002276 /* Exit Guest-Mode */
2277 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002278 svm->nested.vmcb = 0;
2279
Alexander Grafcf74a782008-11-25 20:17:08 +01002280 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002281 disable_gif(svm);
2282
2283 nested_vmcb->save.es = vmcb->save.es;
2284 nested_vmcb->save.cs = vmcb->save.cs;
2285 nested_vmcb->save.ss = vmcb->save.ss;
2286 nested_vmcb->save.ds = vmcb->save.ds;
2287 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2288 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002289 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002290 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002291 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002292 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002293 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002294 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002295 nested_vmcb->save.rip = vmcb->save.rip;
2296 nested_vmcb->save.rsp = vmcb->save.rsp;
2297 nested_vmcb->save.rax = vmcb->save.rax;
2298 nested_vmcb->save.dr7 = vmcb->save.dr7;
2299 nested_vmcb->save.dr6 = vmcb->save.dr6;
2300 nested_vmcb->save.cpl = vmcb->save.cpl;
2301
2302 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2303 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2304 nested_vmcb->control.int_state = vmcb->control.int_state;
2305 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2306 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2307 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2308 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2309 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2310 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002311 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002312
2313 /*
2314 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2315 * to make sure that we do not lose injected events. So check event_inj
2316 * here and copy it to exit_int_info if it is valid.
2317 * Exit_int_info and event_inj can't be both valid because the case
2318 * below only happens on a VMRUN instruction intercept which has
2319 * no valid exit_int_info set.
2320 */
2321 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2322 struct vmcb_control_area *nc = &nested_vmcb->control;
2323
2324 nc->exit_int_info = vmcb->control.event_inj;
2325 nc->exit_int_info_err = vmcb->control.event_inj_err;
2326 }
2327
Joerg Roedel33740e42009-08-07 11:49:29 +02002328 nested_vmcb->control.tlb_ctl = 0;
2329 nested_vmcb->control.event_inj = 0;
2330 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002331
2332 /* We always set V_INTR_MASKING and remember the old value in hflags */
2333 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2334 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2335
Alexander Grafcf74a782008-11-25 20:17:08 +01002336 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002337 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002338
Alexander Graf219b65d2009-06-15 15:21:25 +02002339 kvm_clear_exception_queue(&svm->vcpu);
2340 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002341
Joerg Roedel4b161842010-09-10 17:31:03 +02002342 svm->nested.nested_cr3 = 0;
2343
Alexander Grafcf74a782008-11-25 20:17:08 +01002344 /* Restore selected save entries */
2345 svm->vmcb->save.es = hsave->save.es;
2346 svm->vmcb->save.cs = hsave->save.cs;
2347 svm->vmcb->save.ss = hsave->save.ss;
2348 svm->vmcb->save.ds = hsave->save.ds;
2349 svm->vmcb->save.gdtr = hsave->save.gdtr;
2350 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002351 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002352 svm_set_efer(&svm->vcpu, hsave->save.efer);
2353 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2354 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2355 if (npt_enabled) {
2356 svm->vmcb->save.cr3 = hsave->save.cr3;
2357 svm->vcpu.arch.cr3 = hsave->save.cr3;
2358 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002359 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002360 }
2361 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2362 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2363 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2364 svm->vmcb->save.dr7 = 0;
2365 svm->vmcb->save.cpl = 0;
2366 svm->vmcb->control.exit_int_info = 0;
2367
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002368 mark_all_dirty(svm->vmcb);
2369
Joerg Roedel7597f122010-02-19 16:23:00 +01002370 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002371
Joerg Roedel4b161842010-09-10 17:31:03 +02002372 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002373 kvm_mmu_reset_context(&svm->vcpu);
2374 kvm_mmu_load(&svm->vcpu);
2375
2376 return 0;
2377}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002378
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002379static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002380{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002381 /*
2382 * This function merges the msr permission bitmaps of kvm and the
2383 * nested vmcb. It is omptimized in that it only merges the parts where
2384 * the kvm msr permission bitmap may contain zero bits
2385 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002386 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002387
Joerg Roedel323c3d82010-03-01 15:34:37 +01002388 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2389 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002390
Joerg Roedel323c3d82010-03-01 15:34:37 +01002391 for (i = 0; i < MSRPM_OFFSETS; i++) {
2392 u32 value, p;
2393 u64 offset;
2394
2395 if (msrpm_offsets[i] == 0xffffffff)
2396 break;
2397
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002398 p = msrpm_offsets[i];
2399 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002400
2401 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2402 return false;
2403
2404 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2405 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002406
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002407 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002408
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002409 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002410}
2411
Joerg Roedel52c65a302010-08-02 16:46:44 +02002412static bool nested_vmcb_checks(struct vmcb *vmcb)
2413{
2414 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2415 return false;
2416
Joerg Roedeldbe77582010-08-02 16:46:45 +02002417 if (vmcb->control.asid == 0)
2418 return false;
2419
Joerg Roedel4b161842010-09-10 17:31:03 +02002420 if (vmcb->control.nested_ctl && !npt_enabled)
2421 return false;
2422
Joerg Roedel52c65a302010-08-02 16:46:44 +02002423 return true;
2424}
2425
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002426static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002427{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002428 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002429 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002430 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002431 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002432 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002433
Joerg Roedel06fc77722010-02-19 16:23:07 +01002434 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002435
Joerg Roedel7597f122010-02-19 16:23:00 +01002436 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002437 if (!nested_vmcb)
2438 return false;
2439
Joerg Roedel52c65a302010-08-02 16:46:44 +02002440 if (!nested_vmcb_checks(nested_vmcb)) {
2441 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2442 nested_vmcb->control.exit_code_hi = 0;
2443 nested_vmcb->control.exit_info_1 = 0;
2444 nested_vmcb->control.exit_info_2 = 0;
2445
2446 nested_svm_unmap(page);
2447
2448 return false;
2449 }
2450
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002451 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002452 nested_vmcb->save.rip,
2453 nested_vmcb->control.int_ctl,
2454 nested_vmcb->control.event_inj,
2455 nested_vmcb->control.nested_ctl);
2456
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002457 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2458 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002459 nested_vmcb->control.intercept_exceptions,
2460 nested_vmcb->control.intercept);
2461
Alexander Graf3d6368e2008-11-25 20:17:07 +01002462 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002463 kvm_clear_exception_queue(&svm->vcpu);
2464 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002465
Joerg Roedele0231712010-02-24 18:59:10 +01002466 /*
2467 * Save the old vmcb, so we don't need to pick what we save, but can
2468 * restore everything when a VMEXIT occurs
2469 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002470 hsave->save.es = vmcb->save.es;
2471 hsave->save.cs = vmcb->save.cs;
2472 hsave->save.ss = vmcb->save.ss;
2473 hsave->save.ds = vmcb->save.ds;
2474 hsave->save.gdtr = vmcb->save.gdtr;
2475 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002476 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002477 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002478 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002479 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002480 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002481 hsave->save.rsp = vmcb->save.rsp;
2482 hsave->save.rax = vmcb->save.rax;
2483 if (npt_enabled)
2484 hsave->save.cr3 = vmcb->save.cr3;
2485 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002486 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002487
Joerg Roedel0460a972009-08-07 11:49:31 +02002488 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002489
Avi Kivityf6e78472010-08-02 15:30:20 +03002490 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002491 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2492 else
2493 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2494
Joerg Roedel4b161842010-09-10 17:31:03 +02002495 if (nested_vmcb->control.nested_ctl) {
2496 kvm_mmu_unload(&svm->vcpu);
2497 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2498 nested_svm_init_mmu_context(&svm->vcpu);
2499 }
2500
Alexander Graf3d6368e2008-11-25 20:17:07 +01002501 /* Load the nested guest state */
2502 svm->vmcb->save.es = nested_vmcb->save.es;
2503 svm->vmcb->save.cs = nested_vmcb->save.cs;
2504 svm->vmcb->save.ss = nested_vmcb->save.ss;
2505 svm->vmcb->save.ds = nested_vmcb->save.ds;
2506 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2507 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002508 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002509 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2510 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2511 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2512 if (npt_enabled) {
2513 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2514 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002515 } else
Avi Kivity23902182010-06-10 17:02:16 +03002516 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002517
2518 /* Guest paging mode is active - reset mmu */
2519 kvm_mmu_reset_context(&svm->vcpu);
2520
Joerg Roedeldefbba52009-08-07 11:49:30 +02002521 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002522 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2523 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2524 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002525
Alexander Graf3d6368e2008-11-25 20:17:07 +01002526 /* In case we don't even reach vcpu_run, the fields are not updated */
2527 svm->vmcb->save.rax = nested_vmcb->save.rax;
2528 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2529 svm->vmcb->save.rip = nested_vmcb->save.rip;
2530 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2531 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2532 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2533
Joerg Roedelf7138532010-03-01 15:34:40 +01002534 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002535 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002536
Joerg Roedelaad42c62009-08-07 11:49:34 +02002537 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002538 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002539 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002540 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2541 svm->nested.intercept = nested_vmcb->control.intercept;
2542
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002543 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002544 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002545 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2546 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2547 else
2548 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2549
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002550 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2551 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002552 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2553 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002554 }
2555
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002556 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002557 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002558
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002559 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002560 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2561 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2562 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002563 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2564 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2565
Joerg Roedel7597f122010-02-19 16:23:00 +01002566 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002567
Joerg Roedel20307532010-11-29 17:51:48 +01002568 /* Enter Guest-Mode */
2569 enter_guest_mode(&svm->vcpu);
2570
Joerg Roedel384c6362010-11-30 18:03:56 +01002571 /*
2572 * Merge guest and host intercepts - must be called with vcpu in
2573 * guest-mode to take affect here
2574 */
2575 recalc_intercepts(svm);
2576
Joerg Roedel06fc77722010-02-19 16:23:07 +01002577 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002578
Joerg Roedel2af91942009-08-07 11:49:28 +02002579 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002580
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002581 mark_all_dirty(svm->vmcb);
2582
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002583 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002584}
2585
Joerg Roedel9966bf62009-08-07 11:49:40 +02002586static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002587{
2588 to_vmcb->save.fs = from_vmcb->save.fs;
2589 to_vmcb->save.gs = from_vmcb->save.gs;
2590 to_vmcb->save.tr = from_vmcb->save.tr;
2591 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2592 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2593 to_vmcb->save.star = from_vmcb->save.star;
2594 to_vmcb->save.lstar = from_vmcb->save.lstar;
2595 to_vmcb->save.cstar = from_vmcb->save.cstar;
2596 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2597 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2598 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2599 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002600}
2601
Avi Kivity851ba692009-08-24 11:10:17 +03002602static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002603{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002604 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002605 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002606
Alexander Graf55426752008-11-25 20:17:06 +01002607 if (nested_svm_check_permissions(svm))
2608 return 1;
2609
Joerg Roedel7597f122010-02-19 16:23:00 +01002610 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002611 if (!nested_vmcb)
2612 return 1;
2613
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002614 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2615 skip_emulated_instruction(&svm->vcpu);
2616
Joerg Roedel9966bf62009-08-07 11:49:40 +02002617 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002618 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002619
2620 return 1;
2621}
2622
Avi Kivity851ba692009-08-24 11:10:17 +03002623static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002624{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002625 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002626 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002627
Alexander Graf55426752008-11-25 20:17:06 +01002628 if (nested_svm_check_permissions(svm))
2629 return 1;
2630
Joerg Roedel7597f122010-02-19 16:23:00 +01002631 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002632 if (!nested_vmcb)
2633 return 1;
2634
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002635 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2636 skip_emulated_instruction(&svm->vcpu);
2637
Joerg Roedel9966bf62009-08-07 11:49:40 +02002638 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002639 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002640
2641 return 1;
2642}
2643
Avi Kivity851ba692009-08-24 11:10:17 +03002644static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002645{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002646 if (nested_svm_check_permissions(svm))
2647 return 1;
2648
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002649 /* Save rip after vmrun instruction */
2650 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002651
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002652 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002653 return 1;
2654
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002655 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002656 goto failed;
2657
2658 return 1;
2659
2660failed:
2661
2662 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2663 svm->vmcb->control.exit_code_hi = 0;
2664 svm->vmcb->control.exit_info_1 = 0;
2665 svm->vmcb->control.exit_info_2 = 0;
2666
2667 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002668
2669 return 1;
2670}
2671
Avi Kivity851ba692009-08-24 11:10:17 +03002672static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002673{
2674 if (nested_svm_check_permissions(svm))
2675 return 1;
2676
2677 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2678 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002679 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002680
Joerg Roedel2af91942009-08-07 11:49:28 +02002681 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002682
2683 return 1;
2684}
2685
Avi Kivity851ba692009-08-24 11:10:17 +03002686static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002687{
2688 if (nested_svm_check_permissions(svm))
2689 return 1;
2690
2691 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2692 skip_emulated_instruction(&svm->vcpu);
2693
Joerg Roedel2af91942009-08-07 11:49:28 +02002694 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002695
2696 /* After a CLGI no interrupts should come */
2697 svm_clear_vintr(svm);
2698 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2699
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002700 mark_dirty(svm->vmcb, VMCB_INTR);
2701
Alexander Graf1371d902008-11-25 20:17:04 +01002702 return 1;
2703}
2704
Avi Kivity851ba692009-08-24 11:10:17 +03002705static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002706{
2707 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002708
Joerg Roedelec1ff792009-10-09 16:08:31 +02002709 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2710 vcpu->arch.regs[VCPU_REGS_RAX]);
2711
Alexander Grafff092382009-06-15 15:21:24 +02002712 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2713 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2714
2715 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2716 skip_emulated_instruction(&svm->vcpu);
2717 return 1;
2718}
2719
Joerg Roedel532a46b2009-10-09 16:08:32 +02002720static int skinit_interception(struct vcpu_svm *svm)
2721{
2722 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2723
2724 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2725 return 1;
2726}
2727
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002728static int xsetbv_interception(struct vcpu_svm *svm)
2729{
2730 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
2731 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
2732
2733 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
2734 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2735 skip_emulated_instruction(&svm->vcpu);
2736 }
2737
2738 return 1;
2739}
2740
Avi Kivity851ba692009-08-24 11:10:17 +03002741static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002743 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744 return 1;
2745}
2746
Avi Kivity851ba692009-08-24 11:10:17 +03002747static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748{
Izik Eidus37817f22008-03-24 23:14:53 +02002749 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002750 int reason;
2751 int int_type = svm->vmcb->control.exit_int_info &
2752 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002753 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002754 uint32_t type =
2755 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2756 uint32_t idt_v =
2757 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002758 bool has_error_code = false;
2759 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002760
2761 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002762
Izik Eidus37817f22008-03-24 23:14:53 +02002763 if (svm->vmcb->control.exit_info_2 &
2764 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002765 reason = TASK_SWITCH_IRET;
2766 else if (svm->vmcb->control.exit_info_2 &
2767 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2768 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002769 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002770 reason = TASK_SWITCH_GATE;
2771 else
2772 reason = TASK_SWITCH_CALL;
2773
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002774 if (reason == TASK_SWITCH_GATE) {
2775 switch (type) {
2776 case SVM_EXITINTINFO_TYPE_NMI:
2777 svm->vcpu.arch.nmi_injected = false;
2778 break;
2779 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002780 if (svm->vmcb->control.exit_info_2 &
2781 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2782 has_error_code = true;
2783 error_code =
2784 (u32)svm->vmcb->control.exit_info_2;
2785 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002786 kvm_clear_exception_queue(&svm->vcpu);
2787 break;
2788 case SVM_EXITINTINFO_TYPE_INTR:
2789 kvm_clear_interrupt_queue(&svm->vcpu);
2790 break;
2791 default:
2792 break;
2793 }
2794 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002795
Gleb Natapov8317c292009-04-12 13:37:02 +03002796 if (reason != TASK_SWITCH_GATE ||
2797 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2798 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002799 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2800 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002801
Gleb Natapovacb54512010-04-15 21:03:50 +03002802 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2803 has_error_code, error_code) == EMULATE_FAIL) {
2804 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2805 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2806 svm->vcpu.run->internal.ndata = 0;
2807 return 0;
2808 }
2809 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810}
2811
Avi Kivity851ba692009-08-24 11:10:17 +03002812static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002814 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002815 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002816 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817}
2818
Avi Kivity851ba692009-08-24 11:10:17 +03002819static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002820{
2821 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002822 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002823 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02002824 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002825 return 1;
2826}
2827
Avi Kivity851ba692009-08-24 11:10:17 +03002828static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002829{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002830 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2831 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
2832
2833 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
2834 skip_emulated_instruction(&svm->vcpu);
2835 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002836}
2837
Avi Kivity851ba692009-08-24 11:10:17 +03002838static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839{
Andre Przywara51d8b662010-12-21 11:12:02 +01002840 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841}
2842
Avi Kivity332b56e2011-11-10 14:57:24 +02002843static int rdpmc_interception(struct vcpu_svm *svm)
2844{
2845 int err;
2846
2847 if (!static_cpu_has(X86_FEATURE_NRIPS))
2848 return emulate_on_interception(svm);
2849
2850 err = kvm_rdpmc(&svm->vcpu);
2851 kvm_complete_insn_gp(&svm->vcpu, err);
2852
2853 return 1;
2854}
2855
Joerg Roedel628afd22011-04-04 12:39:36 +02002856bool check_selective_cr0_intercepted(struct vcpu_svm *svm, unsigned long val)
2857{
2858 unsigned long cr0 = svm->vcpu.arch.cr0;
2859 bool ret = false;
2860 u64 intercept;
2861
2862 intercept = svm->nested.intercept;
2863
2864 if (!is_guest_mode(&svm->vcpu) ||
2865 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
2866 return false;
2867
2868 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2869 val &= ~SVM_CR0_SELECTIVE_MASK;
2870
2871 if (cr0 ^ val) {
2872 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2873 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2874 }
2875
2876 return ret;
2877}
2878
Andre Przywara7ff76d52010-12-21 11:12:04 +01002879#define CR_VALID (1ULL << 63)
2880
2881static int cr_interception(struct vcpu_svm *svm)
2882{
2883 int reg, cr;
2884 unsigned long val;
2885 int err;
2886
2887 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2888 return emulate_on_interception(svm);
2889
2890 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2891 return emulate_on_interception(svm);
2892
2893 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2894 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
2895
2896 err = 0;
2897 if (cr >= 16) { /* mov to cr */
2898 cr -= 16;
2899 val = kvm_register_read(&svm->vcpu, reg);
2900 switch (cr) {
2901 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02002902 if (!check_selective_cr0_intercepted(svm, val))
2903 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002904 else
2905 return 1;
2906
Andre Przywara7ff76d52010-12-21 11:12:04 +01002907 break;
2908 case 3:
2909 err = kvm_set_cr3(&svm->vcpu, val);
2910 break;
2911 case 4:
2912 err = kvm_set_cr4(&svm->vcpu, val);
2913 break;
2914 case 8:
2915 err = kvm_set_cr8(&svm->vcpu, val);
2916 break;
2917 default:
2918 WARN(1, "unhandled write to CR%d", cr);
2919 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2920 return 1;
2921 }
2922 } else { /* mov from cr */
2923 switch (cr) {
2924 case 0:
2925 val = kvm_read_cr0(&svm->vcpu);
2926 break;
2927 case 2:
2928 val = svm->vcpu.arch.cr2;
2929 break;
2930 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02002931 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002932 break;
2933 case 4:
2934 val = kvm_read_cr4(&svm->vcpu);
2935 break;
2936 case 8:
2937 val = kvm_get_cr8(&svm->vcpu);
2938 break;
2939 default:
2940 WARN(1, "unhandled read from CR%d", cr);
2941 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2942 return 1;
2943 }
2944 kvm_register_write(&svm->vcpu, reg, val);
2945 }
2946 kvm_complete_insn_gp(&svm->vcpu, err);
2947
2948 return 1;
2949}
2950
Andre Przywaracae37972010-12-21 11:12:05 +01002951static int dr_interception(struct vcpu_svm *svm)
2952{
2953 int reg, dr;
2954 unsigned long val;
2955 int err;
2956
2957 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
2958 return emulate_on_interception(svm);
2959
2960 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2961 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
2962
2963 if (dr >= 16) { /* mov to DRn */
2964 val = kvm_register_read(&svm->vcpu, reg);
2965 kvm_set_dr(&svm->vcpu, dr - 16, val);
2966 } else {
2967 err = kvm_get_dr(&svm->vcpu, dr, &val);
2968 if (!err)
2969 kvm_register_write(&svm->vcpu, reg, val);
2970 }
2971
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01002972 skip_emulated_instruction(&svm->vcpu);
2973
Andre Przywaracae37972010-12-21 11:12:05 +01002974 return 1;
2975}
2976
Avi Kivity851ba692009-08-24 11:10:17 +03002977static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002978{
Avi Kivity851ba692009-08-24 11:10:17 +03002979 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01002980 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03002981
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002982 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2983 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01002984 r = cr_interception(svm);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002985 if (irqchip_in_kernel(svm->vcpu.kvm)) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002986 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002987 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002988 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002989 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002990 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01002991 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2992 return 0;
2993}
2994
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002995u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu)
2996{
2997 struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
2998 return vmcb->control.tsc_offset +
2999 svm_scale_tsc(vcpu, native_read_tsc());
3000}
3001
Avi Kivity6aa8b732006-12-10 02:21:36 -08003002static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
3003{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003004 struct vcpu_svm *svm = to_svm(vcpu);
3005
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303007 case MSR_IA32_TSC: {
Nadav Har'El45133ec2011-08-02 15:55:23 +03003008 *data = svm->vmcb->control.tsc_offset +
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003009 svm_scale_tsc(vcpu, native_read_tsc());
3010
Avi Kivity6aa8b732006-12-10 02:21:36 -08003011 break;
3012 }
Brian Gerst8c065852010-07-17 09:03:26 -04003013 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003014 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003015 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003016#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003018 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019 break;
3020 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003021 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022 break;
3023 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003024 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025 break;
3026 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003027 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028 break;
3029#endif
3030 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003031 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003032 break;
3033 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003034 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035 break;
3036 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003037 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003039 /*
3040 * Nobody will change the following 5 values in the VMCB so we can
3041 * safely return them on rdmsr. They will always be 0 until LBRV is
3042 * implemented.
3043 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003044 case MSR_IA32_DEBUGCTLMSR:
3045 *data = svm->vmcb->save.dbgctl;
3046 break;
3047 case MSR_IA32_LASTBRANCHFROMIP:
3048 *data = svm->vmcb->save.br_from;
3049 break;
3050 case MSR_IA32_LASTBRANCHTOIP:
3051 *data = svm->vmcb->save.br_to;
3052 break;
3053 case MSR_IA32_LASTINTFROMIP:
3054 *data = svm->vmcb->save.last_excp_from;
3055 break;
3056 case MSR_IA32_LASTINTTOIP:
3057 *data = svm->vmcb->save.last_excp_to;
3058 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003059 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003060 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003061 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003062 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003063 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003064 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003065 case MSR_IA32_UCODE_REV:
3066 *data = 0x01000065;
3067 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08003069 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003070 }
3071 return 0;
3072}
3073
Avi Kivity851ba692009-08-24 11:10:17 +03003074static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003076 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 u64 data;
3078
Avi Kivity59200272010-01-25 19:47:02 +02003079 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
3080 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003081 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003082 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003083 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003084
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003085 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003086 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003087 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003088 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 }
3090 return 1;
3091}
3092
Joerg Roedel4a810182010-02-24 18:59:15 +01003093static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3094{
3095 struct vcpu_svm *svm = to_svm(vcpu);
3096 int svm_dis, chg_mask;
3097
3098 if (data & ~SVM_VM_CR_VALID_MASK)
3099 return 1;
3100
3101 chg_mask = SVM_VM_CR_VALID_MASK;
3102
3103 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3104 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3105
3106 svm->nested.vm_cr_msr &= ~chg_mask;
3107 svm->nested.vm_cr_msr |= (data & chg_mask);
3108
3109 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3110
3111 /* check for svm_disable while efer.svme is set */
3112 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3113 return 1;
3114
3115 return 0;
3116}
3117
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
3119{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003120 struct vcpu_svm *svm = to_svm(vcpu);
3121
Avi Kivity6aa8b732006-12-10 02:21:36 -08003122 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003123 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10003124 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003125 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003126 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003127 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003129#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003131 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132 break;
3133 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003134 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135 break;
3136 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003137 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138 break;
3139 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003140 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141 break;
3142#endif
3143 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003144 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145 break;
3146 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003147 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003148 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149 break;
3150 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003151 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003152 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003154 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003155 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003156 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003157 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003158 break;
3159 }
3160 if (data & DEBUGCTL_RESERVED_BITS)
3161 return 1;
3162
3163 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003164 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003165 if (data & (1ULL<<0))
3166 svm_enable_lbrv(svm);
3167 else
3168 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003169 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003170 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003171 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003172 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003173 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003174 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003175 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003176 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
3177 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08003179 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180 }
3181 return 0;
3182}
3183
Avi Kivity851ba692009-08-24 11:10:17 +03003184static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003186 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003187 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003188 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003189
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003190
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003191 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02003192 if (svm_set_msr(&svm->vcpu, ecx, data)) {
3193 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003194 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003195 } else {
3196 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10003197 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003198 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199 return 1;
3200}
3201
Avi Kivity851ba692009-08-24 11:10:17 +03003202static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203{
Rusty Russelle756fc62007-07-30 20:07:08 +10003204 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003205 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206 else
Avi Kivity851ba692009-08-24 11:10:17 +03003207 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208}
3209
Avi Kivity851ba692009-08-24 11:10:17 +03003210static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003211{
Avi Kivity851ba692009-08-24 11:10:17 +03003212 struct kvm_run *kvm_run = svm->vcpu.run;
3213
Avi Kivity3842d132010-07-27 12:30:24 +03003214 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003215 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003216 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003217 mark_dirty(svm->vmcb, VMCB_INTR);
Dor Laorc1150d82007-01-05 16:36:24 -08003218 /*
3219 * If the user space waits to inject interrupts, exit as soon as
3220 * possible
3221 */
Gleb Natapov80618232009-04-21 17:44:56 +03003222 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
3223 kvm_run->request_interrupt_window &&
3224 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10003225 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003226 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
3227 return 0;
3228 }
3229
3230 return 1;
3231}
3232
Mark Langsdorf565d0992009-10-06 14:25:02 -05003233static int pause_interception(struct vcpu_svm *svm)
3234{
3235 kvm_vcpu_on_spin(&(svm->vcpu));
3236 return 1;
3237}
3238
Avi Kivity851ba692009-08-24 11:10:17 +03003239static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003240 [SVM_EXIT_READ_CR0] = cr_interception,
3241 [SVM_EXIT_READ_CR3] = cr_interception,
3242 [SVM_EXIT_READ_CR4] = cr_interception,
3243 [SVM_EXIT_READ_CR8] = cr_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02003244 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003245 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003246 [SVM_EXIT_WRITE_CR3] = cr_interception,
3247 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003248 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003249 [SVM_EXIT_READ_DR0] = dr_interception,
3250 [SVM_EXIT_READ_DR1] = dr_interception,
3251 [SVM_EXIT_READ_DR2] = dr_interception,
3252 [SVM_EXIT_READ_DR3] = dr_interception,
3253 [SVM_EXIT_READ_DR4] = dr_interception,
3254 [SVM_EXIT_READ_DR5] = dr_interception,
3255 [SVM_EXIT_READ_DR6] = dr_interception,
3256 [SVM_EXIT_READ_DR7] = dr_interception,
3257 [SVM_EXIT_WRITE_DR0] = dr_interception,
3258 [SVM_EXIT_WRITE_DR1] = dr_interception,
3259 [SVM_EXIT_WRITE_DR2] = dr_interception,
3260 [SVM_EXIT_WRITE_DR3] = dr_interception,
3261 [SVM_EXIT_WRITE_DR4] = dr_interception,
3262 [SVM_EXIT_WRITE_DR5] = dr_interception,
3263 [SVM_EXIT_WRITE_DR6] = dr_interception,
3264 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003265 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3266 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003267 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003268 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3269 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
3270 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
3271 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003272 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273 [SVM_EXIT_SMI] = nop_on_interception,
3274 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003275 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02003276 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003278 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003279 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003280 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003282 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003283 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003284 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003285 [SVM_EXIT_MSR] = msr_interception,
3286 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003287 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003288 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003289 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003290 [SVM_EXIT_VMLOAD] = vmload_interception,
3291 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003292 [SVM_EXIT_STGI] = stgi_interception,
3293 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003294 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003295 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01003296 [SVM_EXIT_MONITOR] = invalid_op_interception,
3297 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003298 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003299 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003300};
3301
Joe Perchesae8cc052011-04-24 22:00:50 -07003302static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003303{
3304 struct vcpu_svm *svm = to_svm(vcpu);
3305 struct vmcb_control_area *control = &svm->vmcb->control;
3306 struct vmcb_save_area *save = &svm->vmcb->save;
3307
3308 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003309 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
3310 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
3311 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
3312 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
3313 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
3314 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
3315 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3316 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3317 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3318 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3319 pr_err("%-20s%d\n", "asid:", control->asid);
3320 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3321 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3322 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3323 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3324 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3325 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3326 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3327 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3328 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3329 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3330 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
3331 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3332 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3333 pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
3334 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003335 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003336 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3337 "es:",
3338 save->es.selector, save->es.attrib,
3339 save->es.limit, save->es.base);
3340 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3341 "cs:",
3342 save->cs.selector, save->cs.attrib,
3343 save->cs.limit, save->cs.base);
3344 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3345 "ss:",
3346 save->ss.selector, save->ss.attrib,
3347 save->ss.limit, save->ss.base);
3348 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3349 "ds:",
3350 save->ds.selector, save->ds.attrib,
3351 save->ds.limit, save->ds.base);
3352 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3353 "fs:",
3354 save->fs.selector, save->fs.attrib,
3355 save->fs.limit, save->fs.base);
3356 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3357 "gs:",
3358 save->gs.selector, save->gs.attrib,
3359 save->gs.limit, save->gs.base);
3360 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3361 "gdtr:",
3362 save->gdtr.selector, save->gdtr.attrib,
3363 save->gdtr.limit, save->gdtr.base);
3364 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3365 "ldtr:",
3366 save->ldtr.selector, save->ldtr.attrib,
3367 save->ldtr.limit, save->ldtr.base);
3368 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3369 "idtr:",
3370 save->idtr.selector, save->idtr.attrib,
3371 save->idtr.limit, save->idtr.base);
3372 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3373 "tr:",
3374 save->tr.selector, save->tr.attrib,
3375 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003376 pr_err("cpl: %d efer: %016llx\n",
3377 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003378 pr_err("%-15s %016llx %-13s %016llx\n",
3379 "cr0:", save->cr0, "cr2:", save->cr2);
3380 pr_err("%-15s %016llx %-13s %016llx\n",
3381 "cr3:", save->cr3, "cr4:", save->cr4);
3382 pr_err("%-15s %016llx %-13s %016llx\n",
3383 "dr6:", save->dr6, "dr7:", save->dr7);
3384 pr_err("%-15s %016llx %-13s %016llx\n",
3385 "rip:", save->rip, "rflags:", save->rflags);
3386 pr_err("%-15s %016llx %-13s %016llx\n",
3387 "rsp:", save->rsp, "rax:", save->rax);
3388 pr_err("%-15s %016llx %-13s %016llx\n",
3389 "star:", save->star, "lstar:", save->lstar);
3390 pr_err("%-15s %016llx %-13s %016llx\n",
3391 "cstar:", save->cstar, "sfmask:", save->sfmask);
3392 pr_err("%-15s %016llx %-13s %016llx\n",
3393 "kernel_gs_base:", save->kernel_gs_base,
3394 "sysenter_cs:", save->sysenter_cs);
3395 pr_err("%-15s %016llx %-13s %016llx\n",
3396 "sysenter_esp:", save->sysenter_esp,
3397 "sysenter_eip:", save->sysenter_eip);
3398 pr_err("%-15s %016llx %-13s %016llx\n",
3399 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3400 pr_err("%-15s %016llx %-13s %016llx\n",
3401 "br_from:", save->br_from, "br_to:", save->br_to);
3402 pr_err("%-15s %016llx %-13s %016llx\n",
3403 "excp_from:", save->last_excp_from,
3404 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003405}
3406
Avi Kivity586f9602010-11-18 13:09:54 +02003407static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3408{
3409 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3410
3411 *info1 = control->exit_info_1;
3412 *info2 = control->exit_info_2;
3413}
3414
Avi Kivity851ba692009-08-24 11:10:17 +03003415static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003416{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003417 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003418 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003419 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003420
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003421 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003422 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3423 if (npt_enabled)
3424 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003425
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003426 if (unlikely(svm->nested.exit_required)) {
3427 nested_svm_vmexit(svm);
3428 svm->nested.exit_required = false;
3429
3430 return 1;
3431 }
3432
Joerg Roedel20307532010-11-29 17:51:48 +01003433 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003434 int vmexit;
3435
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003436 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3437 svm->vmcb->control.exit_info_1,
3438 svm->vmcb->control.exit_info_2,
3439 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003440 svm->vmcb->control.exit_int_info_err,
3441 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003442
Joerg Roedel410e4d52009-08-07 11:49:44 +02003443 vmexit = nested_svm_exit_special(svm);
3444
3445 if (vmexit == NESTED_EXIT_CONTINUE)
3446 vmexit = nested_svm_exit_handled(svm);
3447
3448 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003449 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003450 }
3451
Joerg Roedela5c38322009-08-07 11:49:32 +02003452 svm_complete_interrupts(svm);
3453
Avi Kivity04d2cc72007-09-10 18:10:54 +03003454 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3455 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3456 kvm_run->fail_entry.hardware_entry_failure_reason
3457 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003458 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3459 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003460 return 0;
3461 }
3462
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003463 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003464 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003465 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3466 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003467 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3468 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003469 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003470 exit_code);
3471
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003472 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003473 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003474 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003475 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476 return 0;
3477 }
3478
Avi Kivity851ba692009-08-24 11:10:17 +03003479 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003480}
3481
3482static void reload_tss(struct kvm_vcpu *vcpu)
3483{
3484 int cpu = raw_smp_processor_id();
3485
Tejun Heo0fe1e002009-10-29 22:34:14 +09003486 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3487 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003488 load_TR_desc();
3489}
3490
Rusty Russelle756fc62007-07-30 20:07:08 +10003491static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492{
3493 int cpu = raw_smp_processor_id();
3494
Tejun Heo0fe1e002009-10-29 22:34:14 +09003495 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003496
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003497 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003498 if (svm->asid_generation != sd->asid_generation)
3499 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003500}
3501
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003502static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3503{
3504 struct vcpu_svm *svm = to_svm(vcpu);
3505
3506 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3507 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003508 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003509 ++vcpu->stat.nmi_injections;
3510}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003511
Eddie Dong85f455f2007-07-06 12:20:49 +03003512static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513{
3514 struct vmcb_control_area *control;
3515
Rusty Russelle756fc62007-07-30 20:07:08 +10003516 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003517 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518 control->int_ctl &= ~V_INTR_PRIO_MASK;
3519 control->int_ctl |= V_IRQ_MASK |
3520 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003521 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003522}
3523
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003524static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003525{
3526 struct vcpu_svm *svm = to_svm(vcpu);
3527
Joerg Roedel2af91942009-08-07 11:49:28 +02003528 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003529
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003530 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3531 ++vcpu->stat.irq_injections;
3532
Alexander Graf219b65d2009-06-15 15:21:25 +02003533 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3534 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003535}
3536
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003537static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3538{
3539 struct vcpu_svm *svm = to_svm(vcpu);
3540
Joerg Roedel20307532010-11-29 17:51:48 +01003541 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003542 return;
3543
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003544 if (irr == -1)
3545 return;
3546
3547 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003548 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003549}
3550
3551static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003552{
3553 struct vcpu_svm *svm = to_svm(vcpu);
3554 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003555 int ret;
3556 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3557 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3558 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3559
3560 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003561}
3562
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003563static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3564{
3565 struct vcpu_svm *svm = to_svm(vcpu);
3566
3567 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3568}
3569
3570static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3571{
3572 struct vcpu_svm *svm = to_svm(vcpu);
3573
3574 if (masked) {
3575 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003576 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003577 } else {
3578 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003579 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003580 }
3581}
3582
Gleb Natapov78646122009-03-23 12:12:11 +02003583static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3584{
3585 struct vcpu_svm *svm = to_svm(vcpu);
3586 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003587 int ret;
3588
3589 if (!gif_set(svm) ||
3590 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3591 return 0;
3592
Avi Kivityf6e78472010-08-02 15:30:20 +03003593 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003594
Joerg Roedel20307532010-11-29 17:51:48 +01003595 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003596 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3597
3598 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003599}
3600
Gleb Natapov9222be12009-04-23 17:14:37 +03003601static void enable_irq_window(struct kvm_vcpu *vcpu)
3602{
Alexander Graf219b65d2009-06-15 15:21:25 +02003603 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003604
Joerg Roedele0231712010-02-24 18:59:10 +01003605 /*
3606 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3607 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3608 * get that intercept, this function will be called again though and
3609 * we'll get the vintr intercept.
3610 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003611 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003612 svm_set_vintr(svm);
3613 svm_inject_irq(svm, 0x0);
3614 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003615}
3616
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003617static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003618{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003619 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003620
Gleb Natapov44c11432009-05-11 13:35:52 +03003621 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3622 == HF_NMI_MASK)
3623 return; /* IRET will cause a vm exit */
3624
Joerg Roedele0231712010-02-24 18:59:10 +01003625 /*
3626 * Something prevents NMI from been injected. Single step over possible
3627 * problem (IRET or exception injection or interrupt shadow)
3628 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003629 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003630 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3631 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003632}
3633
Izik Eiduscbc94022007-10-25 00:29:55 +02003634static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3635{
3636 return 0;
3637}
3638
Avi Kivityd9e368d2007-06-07 19:18:30 +03003639static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3640{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003641 struct vcpu_svm *svm = to_svm(vcpu);
3642
3643 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3644 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3645 else
3646 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003647}
3648
Avi Kivity04d2cc72007-09-10 18:10:54 +03003649static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3650{
3651}
3652
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003653static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3654{
3655 struct vcpu_svm *svm = to_svm(vcpu);
3656
Joerg Roedel20307532010-11-29 17:51:48 +01003657 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003658 return;
3659
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003660 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003661 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003662 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003663 }
3664}
3665
Joerg Roedel649d6862008-04-16 16:51:15 +02003666static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3667{
3668 struct vcpu_svm *svm = to_svm(vcpu);
3669 u64 cr8;
3670
Joerg Roedel20307532010-11-29 17:51:48 +01003671 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003672 return;
3673
Joerg Roedel649d6862008-04-16 16:51:15 +02003674 cr8 = kvm_get_cr8(vcpu);
3675 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3676 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3677}
3678
Gleb Natapov9222be12009-04-23 17:14:37 +03003679static void svm_complete_interrupts(struct vcpu_svm *svm)
3680{
3681 u8 vector;
3682 int type;
3683 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003684 unsigned int3_injected = svm->int3_injected;
3685
3686 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003687
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003688 /*
3689 * If we've made progress since setting HF_IRET_MASK, we've
3690 * executed an IRET and can allow NMI injection.
3691 */
3692 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
3693 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003694 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003695 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3696 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003697
Gleb Natapov9222be12009-04-23 17:14:37 +03003698 svm->vcpu.arch.nmi_injected = false;
3699 kvm_clear_exception_queue(&svm->vcpu);
3700 kvm_clear_interrupt_queue(&svm->vcpu);
3701
3702 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3703 return;
3704
Avi Kivity3842d132010-07-27 12:30:24 +03003705 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3706
Gleb Natapov9222be12009-04-23 17:14:37 +03003707 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3708 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3709
3710 switch (type) {
3711 case SVM_EXITINTINFO_TYPE_NMI:
3712 svm->vcpu.arch.nmi_injected = true;
3713 break;
3714 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003715 /*
3716 * In case of software exceptions, do not reinject the vector,
3717 * but re-execute the instruction instead. Rewind RIP first
3718 * if we emulated INT3 before.
3719 */
3720 if (kvm_exception_is_soft(vector)) {
3721 if (vector == BP_VECTOR && int3_injected &&
3722 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3723 kvm_rip_write(&svm->vcpu,
3724 kvm_rip_read(&svm->vcpu) -
3725 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003726 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003727 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003728 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3729 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003730 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003731
3732 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003733 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003734 break;
3735 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003736 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003737 break;
3738 default:
3739 break;
3740 }
3741}
3742
Avi Kivityb463a6f2010-07-20 15:06:17 +03003743static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3744{
3745 struct vcpu_svm *svm = to_svm(vcpu);
3746 struct vmcb_control_area *control = &svm->vmcb->control;
3747
3748 control->exit_int_info = control->event_inj;
3749 control->exit_int_info_err = control->event_inj_err;
3750 control->event_inj = 0;
3751 svm_complete_interrupts(svm);
3752}
3753
Avi Kivity80e31d42008-07-14 14:44:59 +03003754#ifdef CONFIG_X86_64
3755#define R "r"
3756#else
3757#define R "e"
3758#endif
3759
Avi Kivity851ba692009-08-24 11:10:17 +03003760static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003761{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003762 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003763
Joerg Roedel2041a062010-04-22 12:33:08 +02003764 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3765 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3766 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3767
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003768 /*
3769 * A vmexit emulation is required before the vcpu can be executed
3770 * again.
3771 */
3772 if (unlikely(svm->nested.exit_required))
3773 return;
3774
Rusty Russelle756fc62007-07-30 20:07:08 +10003775 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003776
Joerg Roedel649d6862008-04-16 16:51:15 +02003777 sync_lapic_to_cr8(vcpu);
3778
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003779 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003780
Avi Kivity04d2cc72007-09-10 18:10:54 +03003781 clgi();
3782
3783 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003784
Avi Kivity6aa8b732006-12-10 02:21:36 -08003785 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003786 "push %%"R"bp; \n\t"
3787 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3788 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3789 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3790 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3791 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3792 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003793#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003794 "mov %c[r8](%[svm]), %%r8 \n\t"
3795 "mov %c[r9](%[svm]), %%r9 \n\t"
3796 "mov %c[r10](%[svm]), %%r10 \n\t"
3797 "mov %c[r11](%[svm]), %%r11 \n\t"
3798 "mov %c[r12](%[svm]), %%r12 \n\t"
3799 "mov %c[r13](%[svm]), %%r13 \n\t"
3800 "mov %c[r14](%[svm]), %%r14 \n\t"
3801 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003802#endif
3803
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003805 "push %%"R"ax \n\t"
3806 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003807 __ex(SVM_VMLOAD) "\n\t"
3808 __ex(SVM_VMRUN) "\n\t"
3809 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003810 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003811
3812 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003813 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3814 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3815 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3816 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3817 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3818 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003819#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003820 "mov %%r8, %c[r8](%[svm]) \n\t"
3821 "mov %%r9, %c[r9](%[svm]) \n\t"
3822 "mov %%r10, %c[r10](%[svm]) \n\t"
3823 "mov %%r11, %c[r11](%[svm]) \n\t"
3824 "mov %%r12, %c[r12](%[svm]) \n\t"
3825 "mov %%r13, %c[r13](%[svm]) \n\t"
3826 "mov %%r14, %c[r14](%[svm]) \n\t"
3827 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003828#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003829 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003830 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003831 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003832 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003833 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3834 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3835 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3836 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3837 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3838 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003839#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003840 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3841 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3842 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3843 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3844 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3845 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3846 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3847 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003848#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003849 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003850 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003851#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003852 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3853#endif
3854 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003855
Avi Kivity82ca2d12010-10-21 12:20:34 +02003856#ifdef CONFIG_X86_64
3857 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3858#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003859 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02003860#ifndef CONFIG_X86_32_LAZY_GS
3861 loadsegment(gs, svm->host.gs);
3862#endif
Avi Kivity9581d442010-10-19 16:46:55 +02003863#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864
3865 reload_tss(vcpu);
3866
Avi Kivity56ba47d2007-11-07 17:14:18 +02003867 local_irq_disable();
3868
Avi Kivity13c34e02010-10-21 12:20:31 +02003869 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3870 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3871 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3872 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3873
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02003874 trace_kvm_exit(svm->vmcb->control.exit_code, vcpu, KVM_ISA_SVM);
3875
Joerg Roedel3781c012011-01-14 16:45:02 +01003876 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3877 kvm_before_handle_nmi(&svm->vcpu);
3878
3879 stgi();
3880
3881 /* Any pending NMI will happen here */
3882
3883 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3884 kvm_after_handle_nmi(&svm->vcpu);
3885
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003886 sync_cr8_to_lapic(vcpu);
3887
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003888 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003889
Joerg Roedel38e5e922010-12-03 15:25:16 +01003890 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3891
Gleb Natapov631bc482010-10-14 11:22:52 +02003892 /* if exit due to PF check for async PF */
3893 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3894 svm->apf_reason = kvm_read_and_reset_pf_reason();
3895
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003896 if (npt_enabled) {
3897 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3898 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3899 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003900
3901 /*
3902 * We need to handle MC intercepts here before the vcpu has a chance to
3903 * change the physical cpu
3904 */
3905 if (unlikely(svm->vmcb->control.exit_code ==
3906 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3907 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003908
3909 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910}
3911
Avi Kivity80e31d42008-07-14 14:44:59 +03003912#undef R
3913
Avi Kivity6aa8b732006-12-10 02:21:36 -08003914static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3915{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003916 struct vcpu_svm *svm = to_svm(vcpu);
3917
3918 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003919 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003920 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003921}
3922
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003923static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3924{
3925 struct vcpu_svm *svm = to_svm(vcpu);
3926
3927 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01003928 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003929
3930 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02003931 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003932 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003933
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003934 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003935}
3936
Avi Kivity6aa8b732006-12-10 02:21:36 -08003937static int is_disabled(void)
3938{
Joerg Roedel6031a612007-06-22 12:29:50 +03003939 u64 vm_cr;
3940
3941 rdmsrl(MSR_VM_CR, vm_cr);
3942 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3943 return 1;
3944
Avi Kivity6aa8b732006-12-10 02:21:36 -08003945 return 0;
3946}
3947
Ingo Molnar102d8322007-02-19 14:37:47 +02003948static void
3949svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3950{
3951 /*
3952 * Patch in the VMMCALL instruction:
3953 */
3954 hypercall[0] = 0x0f;
3955 hypercall[1] = 0x01;
3956 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003957}
3958
Yang, Sheng002c7f72007-07-31 14:23:01 +03003959static void svm_check_processor_compat(void *rtn)
3960{
3961 *(int *)rtn = 0;
3962}
3963
Avi Kivity774ead32007-12-26 13:57:04 +02003964static bool svm_cpu_has_accelerated_tpr(void)
3965{
3966 return false;
3967}
3968
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003969static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003970{
3971 return 0;
3972}
3973
Sheng Yang0e851882009-12-18 16:48:46 +08003974static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3975{
3976}
3977
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003978static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3979{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003980 switch (func) {
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02003981 case 0x80000001:
3982 if (nested)
3983 entry->ecx |= (1 << 2); /* Set SVM bit */
3984 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003985 case 0x8000000A:
3986 entry->eax = 1; /* SVM revision 1 */
3987 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3988 ASID emulation to nested SVM */
3989 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003990 entry->edx = 0; /* Per default do not support any
3991 additional features */
3992
3993 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003994 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02003995 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003996
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02003997 /* Support NPT for the guest if enabled */
3998 if (npt_enabled)
3999 entry->edx |= SVM_FEATURE_NPT;
4000
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004001 break;
4002 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004003}
4004
Sheng Yang17cc3932010-01-05 19:02:27 +08004005static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004006{
Sheng Yang17cc3932010-01-05 19:02:27 +08004007 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004008}
4009
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004010static bool svm_rdtscp_supported(void)
4011{
4012 return false;
4013}
4014
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004015static bool svm_has_wbinvd_exit(void)
4016{
4017 return true;
4018}
4019
Avi Kivity02daab22009-12-30 12:40:26 +02004020static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
4021{
4022 struct vcpu_svm *svm = to_svm(vcpu);
4023
Joerg Roedel18c918c2010-11-30 18:03:59 +01004024 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01004025 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02004026}
4027
Joerg Roedel80612522011-04-04 12:39:33 +02004028#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004029 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004030#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004031 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004032#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004033 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004034
4035static struct __x86_intercept {
4036 u32 exit_code;
4037 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004038} x86_intercept_map[] = {
4039 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4040 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4041 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4042 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4043 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004044 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4045 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004046 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4047 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4048 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4049 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4050 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4051 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4052 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4053 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004054 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4055 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4056 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4057 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4058 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4059 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4060 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4061 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004062 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4063 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4064 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004065 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4066 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4067 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4068 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4069 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4070 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4071 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4072 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4073 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004074 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4075 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4076 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4077 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4078 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4079 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4080 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004081 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4082 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4083 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4084 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004085};
4086
Joerg Roedel80612522011-04-04 12:39:33 +02004087#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004088#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004089#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004090
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004091static int svm_check_intercept(struct kvm_vcpu *vcpu,
4092 struct x86_instruction_info *info,
4093 enum x86_intercept_stage stage)
4094{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004095 struct vcpu_svm *svm = to_svm(vcpu);
4096 int vmexit, ret = X86EMUL_CONTINUE;
4097 struct __x86_intercept icpt_info;
4098 struct vmcb *vmcb = svm->vmcb;
4099
4100 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4101 goto out;
4102
4103 icpt_info = x86_intercept_map[info->intercept];
4104
Avi Kivity40e19b52011-04-21 12:35:41 +03004105 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004106 goto out;
4107
4108 switch (icpt_info.exit_code) {
4109 case SVM_EXIT_READ_CR0:
4110 if (info->intercept == x86_intercept_cr_read)
4111 icpt_info.exit_code += info->modrm_reg;
4112 break;
4113 case SVM_EXIT_WRITE_CR0: {
4114 unsigned long cr0, val;
4115 u64 intercept;
4116
4117 if (info->intercept == x86_intercept_cr_write)
4118 icpt_info.exit_code += info->modrm_reg;
4119
4120 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0)
4121 break;
4122
4123 intercept = svm->nested.intercept;
4124
4125 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
4126 break;
4127
4128 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4129 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4130
4131 if (info->intercept == x86_intercept_lmsw) {
4132 cr0 &= 0xfUL;
4133 val &= 0xfUL;
4134 /* lmsw can't clear PE - catch this here */
4135 if (cr0 & X86_CR0_PE)
4136 val |= X86_CR0_PE;
4137 }
4138
4139 if (cr0 ^ val)
4140 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4141
4142 break;
4143 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004144 case SVM_EXIT_READ_DR0:
4145 case SVM_EXIT_WRITE_DR0:
4146 icpt_info.exit_code += info->modrm_reg;
4147 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004148 case SVM_EXIT_MSR:
4149 if (info->intercept == x86_intercept_wrmsr)
4150 vmcb->control.exit_info_1 = 1;
4151 else
4152 vmcb->control.exit_info_1 = 0;
4153 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004154 case SVM_EXIT_PAUSE:
4155 /*
4156 * We get this for NOP only, but pause
4157 * is rep not, check this here
4158 */
4159 if (info->rep_prefix != REPE_PREFIX)
4160 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02004161 case SVM_EXIT_IOIO: {
4162 u64 exit_info;
4163 u32 bytes;
4164
4165 exit_info = (vcpu->arch.regs[VCPU_REGS_RDX] & 0xffff) << 16;
4166
4167 if (info->intercept == x86_intercept_in ||
4168 info->intercept == x86_intercept_ins) {
4169 exit_info |= SVM_IOIO_TYPE_MASK;
4170 bytes = info->src_bytes;
4171 } else {
4172 bytes = info->dst_bytes;
4173 }
4174
4175 if (info->intercept == x86_intercept_outs ||
4176 info->intercept == x86_intercept_ins)
4177 exit_info |= SVM_IOIO_STR_MASK;
4178
4179 if (info->rep_prefix)
4180 exit_info |= SVM_IOIO_REP_MASK;
4181
4182 bytes = min(bytes, 4u);
4183
4184 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4185
4186 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4187
4188 vmcb->control.exit_info_1 = exit_info;
4189 vmcb->control.exit_info_2 = info->next_rip;
4190
4191 break;
4192 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004193 default:
4194 break;
4195 }
4196
4197 vmcb->control.next_rip = info->next_rip;
4198 vmcb->control.exit_code = icpt_info.exit_code;
4199 vmexit = nested_svm_exit_handled(svm);
4200
4201 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4202 : X86EMUL_CONTINUE;
4203
4204out:
4205 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004206}
4207
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004208static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004209 .cpu_has_kvm_support = has_svm,
4210 .disabled_by_bios = is_disabled,
4211 .hardware_setup = svm_hardware_setup,
4212 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004213 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004214 .hardware_enable = svm_hardware_enable,
4215 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004216 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004217
4218 .vcpu_create = svm_create_vcpu,
4219 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004220 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004221
Avi Kivity04d2cc72007-09-10 18:10:54 +03004222 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004223 .vcpu_load = svm_vcpu_load,
4224 .vcpu_put = svm_vcpu_put,
4225
4226 .set_guest_debug = svm_guest_debug,
4227 .get_msr = svm_get_msr,
4228 .set_msr = svm_set_msr,
4229 .get_segment_base = svm_get_segment_base,
4230 .get_segment = svm_get_segment,
4231 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004232 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004233 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004234 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004235 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004236 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004238 .set_cr3 = svm_set_cr3,
4239 .set_cr4 = svm_set_cr4,
4240 .set_efer = svm_set_efer,
4241 .get_idt = svm_get_idt,
4242 .set_idt = svm_set_idt,
4243 .get_gdt = svm_get_gdt,
4244 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004245 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004246 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004247 .get_rflags = svm_get_rflags,
4248 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02004249 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004250 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004251
Avi Kivity6aa8b732006-12-10 02:21:36 -08004252 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004253
Avi Kivity6aa8b732006-12-10 02:21:36 -08004254 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004255 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004256 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004257 .set_interrupt_shadow = svm_set_interrupt_shadow,
4258 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004259 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004260 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004261 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004262 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004263 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004264 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004265 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004266 .get_nmi_mask = svm_get_nmi_mask,
4267 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004268 .enable_nmi_window = enable_nmi_window,
4269 .enable_irq_window = enable_irq_window,
4270 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02004271
4272 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004273 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004274 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004275
Avi Kivity586f9602010-11-18 13:09:54 +02004276 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004277
Sheng Yang17cc3932010-01-05 19:02:27 +08004278 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004279
4280 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004281
4282 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004283
4284 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004285
4286 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004287
Joerg Roedel4051b182011-03-25 09:44:49 +01004288 .set_tsc_khz = svm_set_tsc_khz,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004289 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004290 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01004291 .compute_tsc_offset = svm_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03004292 .read_l1_tsc = svm_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004293
4294 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004295
4296 .check_intercept = svm_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004297};
4298
4299static int __init svm_init(void)
4300{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004301 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004302 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004303}
4304
4305static void __exit svm_exit(void)
4306{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004307 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004308}
4309
4310module_init(svm_init)
4311module_exit(svm_exit)