blob: 53efd597f39e23e192f548adcf984e38e6f0c17a [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * AMD SVM support
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02007 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08008 *
9 * Authors:
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
Avi Kivityedf88412007-12-16 11:02:48 +020017#include <linux/kvm_host.h>
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030021#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020022#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020025#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/vmalloc.h>
27#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030029#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080031
Joerg Roedel67ec6602010-05-17 14:43:35 +020032#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040033#include <asm/desc.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020034#include <asm/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Eduardo Habkost63d11422008-11-17 19:03:20 -020036#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030037#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020038
Avi Kivity4ecac3f2008-05-13 13:23:38 +030039#define __ex(x) __kvm_handle_fault_on_reboot(x)
40
Avi Kivity6aa8b732006-12-10 02:21:36 -080041MODULE_AUTHOR("Qumranet");
42MODULE_LICENSE("GPL");
43
44#define IOPM_ALLOC_ORDER 2
45#define MSRPM_ALLOC_ORDER 1
46
Avi Kivity6aa8b732006-12-10 02:21:36 -080047#define SEG_TYPE_LDT 2
48#define SEG_TYPE_BUSY_TSS16 3
49
Andre Przywara6bc31bd2010-04-11 23:07:28 +020050#define SVM_FEATURE_NPT (1 << 0)
51#define SVM_FEATURE_LBRV (1 << 1)
52#define SVM_FEATURE_SVML (1 << 2)
53#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010054#define SVM_FEATURE_TSC_RATE (1 << 4)
55#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
56#define SVM_FEATURE_FLUSH_ASID (1 << 6)
57#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020058#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030059
Joerg Roedel410e4d52009-08-07 11:49:44 +020060#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
61#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
62#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
63
Joerg Roedel24e09cb2008-02-13 18:58:47 +010064#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
65
Joerg Roedelfbc0db72011-03-25 09:44:46 +010066#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010067#define TSC_RATIO_MIN 0x0000000000000001ULL
68#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010069
Joerg Roedel67ec6602010-05-17 14:43:35 +020070static bool erratum_383_found __read_mostly;
71
Avi Kivity6c8166a2009-05-31 18:15:37 +030072static const u32 host_save_user_msrs[] = {
73#ifdef CONFIG_X86_64
74 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
75 MSR_FS_BASE,
76#endif
77 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
78};
79
80#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
81
82struct kvm_vcpu;
83
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020084struct nested_state {
85 struct vmcb *hsave;
86 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010087 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020088 u64 vmcb;
89
90 /* These are the merged vectors */
91 u32 *msrpm;
92
93 /* gpa pointers to the real vectors */
94 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +010095 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020096
Joerg Roedelcd3ff652009-10-09 16:08:26 +020097 /* A VMEXIT is required but not yet emulated */
98 bool exit_required;
99
Joerg Roedelaad42c62009-08-07 11:49:34 +0200100 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100101 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100102 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200103 u32 intercept_exceptions;
104 u64 intercept;
105
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200106 /* Nested Paging related state */
107 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200108};
109
Joerg Roedel323c3d82010-03-01 15:34:37 +0100110#define MSRPM_OFFSETS 16
111static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
112
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500113/*
114 * Set osvw_len to higher value when updated Revision Guides
115 * are published and we know what the new status bits are
116 */
117static uint64_t osvw_len = 4, osvw_status;
118
Avi Kivity6c8166a2009-05-31 18:15:37 +0300119struct vcpu_svm {
120 struct kvm_vcpu vcpu;
121 struct vmcb *vmcb;
122 unsigned long vmcb_pa;
123 struct svm_cpu_data *svm_data;
124 uint64_t asid_generation;
125 uint64_t sysenter_esp;
126 uint64_t sysenter_eip;
127
128 u64 next_rip;
129
130 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200131 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200132 u16 fs;
133 u16 gs;
134 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200135 u64 gs_base;
136 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300137
138 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300139
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200140 ulong nmi_iret_rip;
141
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200142 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200143
144 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100145
146 unsigned int3_injected;
147 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200148 u32 apf_reason;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100149
150 u64 tsc_ratio;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300151};
152
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100153static DEFINE_PER_CPU(u64, current_tsc_ratio);
154#define TSC_RATIO_DEFAULT 0x0100000000ULL
155
Joerg Roedel455716f2010-03-01 15:34:35 +0100156#define MSR_INVALID 0xffffffffU
157
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100158static struct svm_direct_access_msrs {
159 u32 index; /* Index of the MSR */
160 bool always; /* True if intercept is always on */
161} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400162 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100163 { .index = MSR_IA32_SYSENTER_CS, .always = true },
164#ifdef CONFIG_X86_64
165 { .index = MSR_GS_BASE, .always = true },
166 { .index = MSR_FS_BASE, .always = true },
167 { .index = MSR_KERNEL_GS_BASE, .always = true },
168 { .index = MSR_LSTAR, .always = true },
169 { .index = MSR_CSTAR, .always = true },
170 { .index = MSR_SYSCALL_MASK, .always = true },
171#endif
172 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
173 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
174 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
175 { .index = MSR_IA32_LASTINTTOIP, .always = false },
176 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177};
178
179/* enable NPT for AMD64 and X86 with PAE */
180#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
181static bool npt_enabled = true;
182#else
Joerg Roedele0231712010-02-24 18:59:10 +0100183static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184#endif
185
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100186/* allow nested paging (virtualized MMU) for all guests */
187static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188module_param(npt, int, S_IRUGO);
189
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100190/* allow nested virtualization in KVM/SVM */
191static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192module_param(nested, int, S_IRUGO);
193
194static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200195static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196
Joerg Roedel410e4d52009-08-07 11:49:44 +0200197static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100198static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
201 bool has_error_code, u32 error_code);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100202static u64 __scale_tsc(u64 ratio, u64 tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100204enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100205 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
206 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100207 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100208 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100209 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100210 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100211 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100212 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100213 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100214 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100215 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100216 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100217 VMCB_DIRTY_MAX,
218};
219
Joerg Roedel0574dec2010-12-03 11:45:58 +0100220/* TPR and CR2 are always written before VMRUN */
221#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100222
223static inline void mark_all_dirty(struct vmcb *vmcb)
224{
225 vmcb->control.clean = 0;
226}
227
228static inline void mark_all_clean(struct vmcb *vmcb)
229{
230 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
231 & ~VMCB_ALWAYS_DIRTY_MASK;
232}
233
234static inline void mark_dirty(struct vmcb *vmcb, int bit)
235{
236 vmcb->control.clean &= ~(1 << bit);
237}
238
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
240{
241 return container_of(vcpu, struct vcpu_svm, vcpu);
242}
243
Joerg Roedel384c6362010-11-30 18:03:56 +0100244static void recalc_intercepts(struct vcpu_svm *svm)
245{
246 struct vmcb_control_area *c, *h;
247 struct nested_state *g;
248
Joerg Roedel116a0a22010-12-03 11:45:49 +0100249 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
250
Joerg Roedel384c6362010-11-30 18:03:56 +0100251 if (!is_guest_mode(&svm->vcpu))
252 return;
253
254 c = &svm->vmcb->control;
255 h = &svm->nested.hsave->control;
256 g = &svm->nested;
257
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100258 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100259 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100260 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
261 c->intercept = h->intercept | g->intercept;
262}
263
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100264static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
265{
266 if (is_guest_mode(&svm->vcpu))
267 return svm->nested.hsave;
268 else
269 return svm->vmcb;
270}
271
272static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
273{
274 struct vmcb *vmcb = get_host_vmcb(svm);
275
276 vmcb->control.intercept_cr |= (1U << bit);
277
278 recalc_intercepts(svm);
279}
280
281static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
282{
283 struct vmcb *vmcb = get_host_vmcb(svm);
284
285 vmcb->control.intercept_cr &= ~(1U << bit);
286
287 recalc_intercepts(svm);
288}
289
290static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
291{
292 struct vmcb *vmcb = get_host_vmcb(svm);
293
294 return vmcb->control.intercept_cr & (1U << bit);
295}
296
Joerg Roedel3aed0412010-11-30 18:03:58 +0100297static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
298{
299 struct vmcb *vmcb = get_host_vmcb(svm);
300
301 vmcb->control.intercept_dr |= (1U << bit);
302
303 recalc_intercepts(svm);
304}
305
306static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
307{
308 struct vmcb *vmcb = get_host_vmcb(svm);
309
310 vmcb->control.intercept_dr &= ~(1U << bit);
311
312 recalc_intercepts(svm);
313}
314
Joerg Roedel18c918c2010-11-30 18:03:59 +0100315static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
316{
317 struct vmcb *vmcb = get_host_vmcb(svm);
318
319 vmcb->control.intercept_exceptions |= (1U << bit);
320
321 recalc_intercepts(svm);
322}
323
324static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
325{
326 struct vmcb *vmcb = get_host_vmcb(svm);
327
328 vmcb->control.intercept_exceptions &= ~(1U << bit);
329
330 recalc_intercepts(svm);
331}
332
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100333static inline void set_intercept(struct vcpu_svm *svm, int bit)
334{
335 struct vmcb *vmcb = get_host_vmcb(svm);
336
337 vmcb->control.intercept |= (1ULL << bit);
338
339 recalc_intercepts(svm);
340}
341
342static inline void clr_intercept(struct vcpu_svm *svm, int bit)
343{
344 struct vmcb *vmcb = get_host_vmcb(svm);
345
346 vmcb->control.intercept &= ~(1ULL << bit);
347
348 recalc_intercepts(svm);
349}
350
Joerg Roedel2af91942009-08-07 11:49:28 +0200351static inline void enable_gif(struct vcpu_svm *svm)
352{
353 svm->vcpu.arch.hflags |= HF_GIF_MASK;
354}
355
356static inline void disable_gif(struct vcpu_svm *svm)
357{
358 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
359}
360
361static inline bool gif_set(struct vcpu_svm *svm)
362{
363 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
364}
365
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366static unsigned long iopm_base;
367
368struct kvm_ldttss_desc {
369 u16 limit0;
370 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100371 unsigned base1:8, type:5, dpl:2, p:1;
372 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373 u32 base3;
374 u32 zero1;
375} __attribute__((packed));
376
377struct svm_cpu_data {
378 int cpu;
379
Avi Kivity5008fdf2007-04-02 13:05:50 +0300380 u64 asid_generation;
381 u32 max_asid;
382 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800383 struct kvm_ldttss_desc *tss_desc;
384
385 struct page *save_area;
386};
387
388static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
389
390struct svm_init_data {
391 int cpu;
392 int r;
393};
394
395static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
396
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200397#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398#define MSRS_RANGE_SIZE 2048
399#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
400
Joerg Roedel455716f2010-03-01 15:34:35 +0100401static u32 svm_msrpm_offset(u32 msr)
402{
403 u32 offset;
404 int i;
405
406 for (i = 0; i < NUM_MSR_MAPS; i++) {
407 if (msr < msrpm_ranges[i] ||
408 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
409 continue;
410
411 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
412 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
413
414 /* Now we have the u8 offset - but need the u32 offset */
415 return offset / 4;
416 }
417
418 /* MSR not in any range */
419 return MSR_INVALID;
420}
421
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422#define MAX_INST_SIZE 15
423
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424static inline void clgi(void)
425{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300426 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427}
428
429static inline void stgi(void)
430{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300431 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432}
433
434static inline void invlpga(unsigned long addr, u32 asid)
435{
Joerg Roedele0231712010-02-24 18:59:10 +0100436 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437}
438
Joerg Roedel4b161842010-09-10 17:31:03 +0200439static int get_npt_level(void)
440{
441#ifdef CONFIG_X86_64
442 return PT64_ROOT_LEVEL;
443#else
444 return PT32E_ROOT_LEVEL;
445#endif
446}
447
Avi Kivity6aa8b732006-12-10 02:21:36 -0800448static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
449{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000450 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100451 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600452 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800453
Alexander Graf9962d032008-11-25 20:17:02 +0100454 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100455 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456}
457
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458static int is_external_interrupt(u32 info)
459{
460 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
461 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
462}
463
Glauber Costa2809f5d2009-05-12 16:21:05 -0400464static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
465{
466 struct vcpu_svm *svm = to_svm(vcpu);
467 u32 ret = 0;
468
469 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100470 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400471 return ret & mask;
472}
473
474static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
475{
476 struct vcpu_svm *svm = to_svm(vcpu);
477
478 if (mask == 0)
479 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
480 else
481 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
482
483}
484
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
486{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400487 struct vcpu_svm *svm = to_svm(vcpu);
488
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200489 if (svm->vmcb->control.next_rip != 0)
490 svm->next_rip = svm->vmcb->control.next_rip;
491
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400492 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100493 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300494 EMULATE_DONE)
495 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 return;
497 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300498 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
499 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
500 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300502 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400503 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504}
505
Jan Kiszka116a4752010-02-23 17:47:54 +0100506static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200507 bool has_error_code, u32 error_code,
508 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100509{
510 struct vcpu_svm *svm = to_svm(vcpu);
511
Joerg Roedele0231712010-02-24 18:59:10 +0100512 /*
513 * If we are within a nested VM we'd better #VMEXIT and let the guest
514 * handle the exception
515 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200516 if (!reinject &&
517 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100518 return;
519
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200520 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100521 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
522
523 /*
524 * For guest debugging where we have to reinject #BP if some
525 * INT3 is guest-owned:
526 * Emulate nRIP by moving RIP forward. Will fail if injection
527 * raises a fault that is not intercepted. Still better than
528 * failing in all cases.
529 */
530 skip_emulated_instruction(&svm->vcpu);
531 rip = kvm_rip_read(&svm->vcpu);
532 svm->int3_rip = rip + svm->vmcb->save.cs.base;
533 svm->int3_injected = rip - old_rip;
534 }
535
Jan Kiszka116a4752010-02-23 17:47:54 +0100536 svm->vmcb->control.event_inj = nr
537 | SVM_EVTINJ_VALID
538 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
539 | SVM_EVTINJ_TYPE_EXEPT;
540 svm->vmcb->control.event_inj_err = error_code;
541}
542
Joerg Roedel67ec6602010-05-17 14:43:35 +0200543static void svm_init_erratum_383(void)
544{
545 u32 low, high;
546 int err;
547 u64 val;
548
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200549 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200550 return;
551
552 /* Use _safe variants to not break nested virtualization */
553 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
554 if (err)
555 return;
556
557 val |= (1ULL << 47);
558
559 low = lower_32_bits(val);
560 high = upper_32_bits(val);
561
562 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
563
564 erratum_383_found = true;
565}
566
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500567static void svm_init_osvw(struct kvm_vcpu *vcpu)
568{
569 /*
570 * Guests should see errata 400 and 415 as fixed (assuming that
571 * HLT and IO instructions are intercepted).
572 */
573 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
574 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
575
576 /*
577 * By increasing VCPU's osvw.length to 3 we are telling the guest that
578 * all osvw.status bits inside that length, including bit 0 (which is
579 * reserved for erratum 298), are valid. However, if host processor's
580 * osvw_len is 0 then osvw_status[0] carries no information. We need to
581 * be conservative here and therefore we tell the guest that erratum 298
582 * is present (because we really don't know).
583 */
584 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
585 vcpu->arch.osvw.status |= 1;
586}
587
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588static int has_svm(void)
589{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200590 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591
Eduardo Habkost63d11422008-11-17 19:03:20 -0200592 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800593 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594 return 0;
595 }
596
Avi Kivity6aa8b732006-12-10 02:21:36 -0800597 return 1;
598}
599
600static void svm_hardware_disable(void *garbage)
601{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100602 /* Make sure we clean up behind us */
603 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
604 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
605
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200606 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607}
608
Alexander Graf10474ae2009-09-15 11:37:46 +0200609static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800610{
611
Tejun Heo0fe1e002009-10-29 22:34:14 +0900612 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200614 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615 struct desc_struct *gdt;
616 int me = raw_smp_processor_id();
617
Alexander Graf10474ae2009-09-15 11:37:46 +0200618 rdmsrl(MSR_EFER, efer);
619 if (efer & EFER_SVME)
620 return -EBUSY;
621
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000623 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
624 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200625 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900627 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628
Tejun Heo0fe1e002009-10-29 22:34:14 +0900629 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000630 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200632 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633 }
634
Tejun Heo0fe1e002009-10-29 22:34:14 +0900635 sd->asid_generation = 1;
636 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
637 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200639 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200640 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900641 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642
Alexander Graf9962d032008-11-25 20:17:02 +0100643 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644
Linus Torvaldsd0316552009-12-14 09:58:24 -0800645 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200646
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100647 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
648 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
649 __get_cpu_var(current_tsc_ratio) = TSC_RATIO_DEFAULT;
650 }
651
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500652
653 /*
654 * Get OSVW bits.
655 *
656 * Note that it is possible to have a system with mixed processor
657 * revisions and therefore different OSVW bits. If bits are not the same
658 * on different processors then choose the worst case (i.e. if erratum
659 * is present on one processor and not on another then assume that the
660 * erratum is present everywhere).
661 */
662 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
663 uint64_t len, status = 0;
664 int err;
665
666 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
667 if (!err)
668 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
669 &err);
670
671 if (err)
672 osvw_status = osvw_len = 0;
673 else {
674 if (len < osvw_len)
675 osvw_len = len;
676 osvw_status |= status;
677 osvw_status &= (1ULL << osvw_len) - 1;
678 }
679 } else
680 osvw_status = osvw_len = 0;
681
Joerg Roedel67ec6602010-05-17 14:43:35 +0200682 svm_init_erratum_383();
683
Alexander Graf10474ae2009-09-15 11:37:46 +0200684 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685}
686
Joerg Roedel0da1db752008-07-02 16:02:11 +0200687static void svm_cpu_uninit(int cpu)
688{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900689 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200690
Tejun Heo0fe1e002009-10-29 22:34:14 +0900691 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200692 return;
693
694 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900695 __free_page(sd->save_area);
696 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200697}
698
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699static int svm_cpu_init(int cpu)
700{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900701 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702 int r;
703
Tejun Heo0fe1e002009-10-29 22:34:14 +0900704 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
705 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900707 sd->cpu = cpu;
708 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900710 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711 goto err_1;
712
Tejun Heo0fe1e002009-10-29 22:34:14 +0900713 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714
715 return 0;
716
717err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900718 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719 return r;
720
721}
722
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100723static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724{
725 int i;
726
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100727 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
728 if (direct_access_msrs[i].index == index)
729 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100731 return false;
732}
733
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734static void set_msr_interception(u32 *msrpm, unsigned msr,
735 int read, int write)
736{
Joerg Roedel455716f2010-03-01 15:34:35 +0100737 u8 bit_read, bit_write;
738 unsigned long tmp;
739 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100741 /*
742 * If this warning triggers extend the direct_access_msrs list at the
743 * beginning of the file
744 */
745 WARN_ON(!valid_msr_intercept(msr));
746
Joerg Roedel455716f2010-03-01 15:34:35 +0100747 offset = svm_msrpm_offset(msr);
748 bit_read = 2 * (msr & 0x0f);
749 bit_write = 2 * (msr & 0x0f) + 1;
750 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751
Joerg Roedel455716f2010-03-01 15:34:35 +0100752 BUG_ON(offset == MSR_INVALID);
753
754 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
755 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
756
757 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758}
759
Joerg Roedelf65c2292008-02-13 18:58:46 +0100760static void svm_vcpu_init_msrpm(u32 *msrpm)
761{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100762 int i;
763
Joerg Roedelf65c2292008-02-13 18:58:46 +0100764 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
765
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100766 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
767 if (!direct_access_msrs[i].always)
768 continue;
769
770 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
771 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100772}
773
Joerg Roedel323c3d82010-03-01 15:34:37 +0100774static void add_msr_offset(u32 offset)
775{
776 int i;
777
778 for (i = 0; i < MSRPM_OFFSETS; ++i) {
779
780 /* Offset already in list? */
781 if (msrpm_offsets[i] == offset)
782 return;
783
784 /* Slot used by another offset? */
785 if (msrpm_offsets[i] != MSR_INVALID)
786 continue;
787
788 /* Add offset to list */
789 msrpm_offsets[i] = offset;
790
791 return;
792 }
793
794 /*
795 * If this BUG triggers the msrpm_offsets table has an overflow. Just
796 * increase MSRPM_OFFSETS in this case.
797 */
798 BUG();
799}
800
801static void init_msrpm_offsets(void)
802{
803 int i;
804
805 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
806
807 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
808 u32 offset;
809
810 offset = svm_msrpm_offset(direct_access_msrs[i].index);
811 BUG_ON(offset == MSR_INVALID);
812
813 add_msr_offset(offset);
814 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815}
816
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100817static void svm_enable_lbrv(struct vcpu_svm *svm)
818{
819 u32 *msrpm = svm->msrpm;
820
821 svm->vmcb->control.lbr_ctl = 1;
822 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
823 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
824 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
825 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
826}
827
828static void svm_disable_lbrv(struct vcpu_svm *svm)
829{
830 u32 *msrpm = svm->msrpm;
831
832 svm->vmcb->control.lbr_ctl = 0;
833 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
834 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
835 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
836 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
837}
838
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839static __init int svm_hardware_setup(void)
840{
841 int cpu;
842 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100843 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844 int r;
845
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
847
848 if (!iopm_pages)
849 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300850
851 iopm_va = page_address(iopm_pages);
852 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
854
Joerg Roedel323c3d82010-03-01 15:34:37 +0100855 init_msrpm_offsets();
856
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100857 if (boot_cpu_has(X86_FEATURE_NX))
858 kvm_enable_efer_bits(EFER_NX);
859
Alexander Graf1b2fd702009-02-02 16:23:51 +0100860 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
861 kvm_enable_efer_bits(EFER_FFXSR);
862
Joerg Roedel92a1f122011-03-25 09:44:51 +0100863 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
864 u64 max;
865
866 kvm_has_tsc_control = true;
867
868 /*
869 * Make sure the user can only configure tsc_khz values that
870 * fit into a signed integer.
871 * A min value is not calculated needed because it will always
872 * be 1 on all machines and a value of 0 is used to disable
873 * tsc-scaling for the vcpu.
874 */
875 max = min(0x7fffffffULL, __scale_tsc(tsc_khz, TSC_RATIO_MAX));
876
877 kvm_max_guest_tsc_khz = max;
878 }
879
Alexander Graf236de052008-11-25 20:17:10 +0100880 if (nested) {
881 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200882 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100883 }
884
Zachary Amsden3230bb42009-09-29 11:38:37 -1000885 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886 r = svm_cpu_init(cpu);
887 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100888 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100890
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200891 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100892 npt_enabled = false;
893
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100894 if (npt_enabled && !npt) {
895 printk(KERN_INFO "kvm: Nested Paging disabled\n");
896 npt_enabled = false;
897 }
898
Joerg Roedel18552672008-02-07 13:47:41 +0100899 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100900 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100901 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200902 } else
903 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100904
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 return 0;
906
Joerg Roedelf65c2292008-02-13 18:58:46 +0100907err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
909 iopm_base = 0;
910 return r;
911}
912
913static __exit void svm_hardware_unsetup(void)
914{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200915 int cpu;
916
Zachary Amsden3230bb42009-09-29 11:38:37 -1000917 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200918 svm_cpu_uninit(cpu);
919
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100921 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922}
923
924static void init_seg(struct vmcb_seg *seg)
925{
926 seg->selector = 0;
927 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100928 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 seg->limit = 0xffff;
930 seg->base = 0;
931}
932
933static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
934{
935 seg->selector = 0;
936 seg->attrib = SVM_SELECTOR_P_MASK | type;
937 seg->limit = 0xffff;
938 seg->base = 0;
939}
940
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100941static u64 __scale_tsc(u64 ratio, u64 tsc)
942{
943 u64 mult, frac, _tsc;
944
945 mult = ratio >> 32;
946 frac = ratio & ((1ULL << 32) - 1);
947
948 _tsc = tsc;
949 _tsc *= mult;
950 _tsc += (tsc >> 32) * frac;
951 _tsc += ((tsc & ((1ULL << 32) - 1)) * frac) >> 32;
952
953 return _tsc;
954}
955
956static u64 svm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
957{
958 struct vcpu_svm *svm = to_svm(vcpu);
959 u64 _tsc = tsc;
960
961 if (svm->tsc_ratio != TSC_RATIO_DEFAULT)
962 _tsc = __scale_tsc(svm->tsc_ratio, tsc);
963
964 return _tsc;
965}
966
Zachary Amsdencc578282012-02-03 15:43:50 -0200967static void svm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +0100968{
969 struct vcpu_svm *svm = to_svm(vcpu);
970 u64 ratio;
971 u64 khz;
972
Zachary Amsdencc578282012-02-03 15:43:50 -0200973 /* Guest TSC same frequency as host TSC? */
974 if (!scale) {
Joerg Roedel4051b182011-03-25 09:44:49 +0100975 svm->tsc_ratio = TSC_RATIO_DEFAULT;
976 return;
977 }
978
Zachary Amsdencc578282012-02-03 15:43:50 -0200979 /* TSC scaling supported? */
980 if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
981 if (user_tsc_khz > tsc_khz) {
982 vcpu->arch.tsc_catchup = 1;
983 vcpu->arch.tsc_always_catchup = 1;
984 } else
985 WARN(1, "user requested TSC rate below hardware speed\n");
986 return;
987 }
988
Joerg Roedel4051b182011-03-25 09:44:49 +0100989 khz = user_tsc_khz;
990
991 /* TSC scaling required - calculate ratio */
992 ratio = khz << 32;
993 do_div(ratio, tsc_khz);
994
995 if (ratio == 0 || ratio & TSC_RATIO_RSVD) {
996 WARN_ONCE(1, "Invalid TSC ratio - virtual-tsc-khz=%u\n",
997 user_tsc_khz);
998 return;
999 }
Joerg Roedel4051b182011-03-25 09:44:49 +01001000 svm->tsc_ratio = ratio;
1001}
1002
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001003static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1004{
1005 struct vcpu_svm *svm = to_svm(vcpu);
1006 u64 g_tsc_offset = 0;
1007
Joerg Roedel20307532010-11-29 17:51:48 +01001008 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001009 g_tsc_offset = svm->vmcb->control.tsc_offset -
1010 svm->nested.hsave->control.tsc_offset;
1011 svm->nested.hsave->control.tsc_offset = offset;
1012 }
1013
1014 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001015
1016 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001017}
1018
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001019static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10001020{
1021 struct vcpu_svm *svm = to_svm(vcpu);
1022
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001023 WARN_ON(adjustment < 0);
1024 if (host)
1025 adjustment = svm_scale_tsc(vcpu, adjustment);
1026
Zachary Amsdene48672f2010-08-19 22:07:23 -10001027 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +01001028 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -10001029 svm->nested.hsave->control.tsc_offset += adjustment;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001030 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001031}
1032
Joerg Roedel857e4092011-03-25 09:44:50 +01001033static u64 svm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1034{
1035 u64 tsc;
1036
1037 tsc = svm_scale_tsc(vcpu, native_read_tsc());
1038
1039 return target_tsc - tsc;
1040}
1041
Joerg Roedele6101a92008-02-13 18:58:45 +01001042static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043{
Joerg Roedele6101a92008-02-13 18:58:45 +01001044 struct vmcb_control_area *control = &svm->vmcb->control;
1045 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046
Avi Kivitybff78272010-01-07 13:16:08 +02001047 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001048 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001049
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001050 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1051 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1052 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1053 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1054 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1055 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
1056 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057
Joerg Roedel3aed0412010-11-30 18:03:58 +01001058 set_dr_intercept(svm, INTERCEPT_DR0_READ);
1059 set_dr_intercept(svm, INTERCEPT_DR1_READ);
1060 set_dr_intercept(svm, INTERCEPT_DR2_READ);
1061 set_dr_intercept(svm, INTERCEPT_DR3_READ);
1062 set_dr_intercept(svm, INTERCEPT_DR4_READ);
1063 set_dr_intercept(svm, INTERCEPT_DR5_READ);
1064 set_dr_intercept(svm, INTERCEPT_DR6_READ);
1065 set_dr_intercept(svm, INTERCEPT_DR7_READ);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066
Joerg Roedel3aed0412010-11-30 18:03:58 +01001067 set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
1068 set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
1069 set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
1070 set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
1071 set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
1072 set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
1073 set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
1074 set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075
Joerg Roedel18c918c2010-11-30 18:03:59 +01001076 set_exception_intercept(svm, PF_VECTOR);
1077 set_exception_intercept(svm, UD_VECTOR);
1078 set_exception_intercept(svm, MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001080 set_intercept(svm, INTERCEPT_INTR);
1081 set_intercept(svm, INTERCEPT_NMI);
1082 set_intercept(svm, INTERCEPT_SMI);
1083 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001084 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001085 set_intercept(svm, INTERCEPT_CPUID);
1086 set_intercept(svm, INTERCEPT_INVD);
1087 set_intercept(svm, INTERCEPT_HLT);
1088 set_intercept(svm, INTERCEPT_INVLPG);
1089 set_intercept(svm, INTERCEPT_INVLPGA);
1090 set_intercept(svm, INTERCEPT_IOIO_PROT);
1091 set_intercept(svm, INTERCEPT_MSR_PROT);
1092 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1093 set_intercept(svm, INTERCEPT_SHUTDOWN);
1094 set_intercept(svm, INTERCEPT_VMRUN);
1095 set_intercept(svm, INTERCEPT_VMMCALL);
1096 set_intercept(svm, INTERCEPT_VMLOAD);
1097 set_intercept(svm, INTERCEPT_VMSAVE);
1098 set_intercept(svm, INTERCEPT_STGI);
1099 set_intercept(svm, INTERCEPT_CLGI);
1100 set_intercept(svm, INTERCEPT_SKINIT);
1101 set_intercept(svm, INTERCEPT_WBINVD);
1102 set_intercept(svm, INTERCEPT_MONITOR);
1103 set_intercept(svm, INTERCEPT_MWAIT);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001104 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105
1106 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001107 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108 control->int_ctl = V_INTR_MASKING_MASK;
1109
1110 init_seg(&save->es);
1111 init_seg(&save->ss);
1112 init_seg(&save->ds);
1113 init_seg(&save->fs);
1114 init_seg(&save->gs);
1115
1116 save->cs.selector = 0xf000;
1117 /* Executable/Readable Code Segment */
1118 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1119 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1120 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -08001121 /*
1122 * cs.base should really be 0xffff0000, but vmx can't handle that, so
1123 * be consistent with it.
1124 *
1125 * Replace when we have real mode working for vmx.
1126 */
1127 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128
1129 save->gdtr.limit = 0xffff;
1130 save->idtr.limit = 0xffff;
1131
1132 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1133 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1134
Marcelo Tosattieaa48512010-08-31 19:13:14 -03001135 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001136 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 save->dr7 = 0x400;
Avi Kivityf6e78472010-08-02 15:30:20 +03001138 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001140 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141
Joerg Roedele0231712010-02-24 18:59:10 +01001142 /*
1143 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001144 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -03001146 svm->vcpu.arch.cr0 = 0;
1147 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001148
Rusty Russell66aee912007-07-17 23:34:16 +10001149 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001151
1152 if (npt_enabled) {
1153 /* Setup VMCB for Nested Paging */
1154 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001155 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001156 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001157 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1158 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001159 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001160 save->cr3 = 0;
1161 save->cr4 = 0;
1162 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001163 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001164
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001165 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001166 svm->vcpu.arch.hflags = 0;
1167
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001168 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001169 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001170 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001171 }
1172
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001173 mark_all_dirty(svm->vmcb);
1174
Joerg Roedel2af91942009-08-07 11:49:28 +02001175 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176}
1177
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001178static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001179{
1180 struct vcpu_svm *svm = to_svm(vcpu);
1181
Joerg Roedele6101a92008-02-13 18:58:45 +01001182 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001183
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001184 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001185 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001186 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
1187 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +02001188 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001189 vcpu->arch.regs_avail = ~0;
1190 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001191
1192 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001193}
1194
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001195static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001197 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001199 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001200 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001201 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001202 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203
Rusty Russellc16f8622007-07-30 21:12:19 +10001204 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001205 if (!svm) {
1206 err = -ENOMEM;
1207 goto out;
1208 }
1209
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001210 svm->tsc_ratio = TSC_RATIO_DEFAULT;
1211
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001212 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1213 if (err)
1214 goto free_svm;
1215
Joerg Roedelf65c2292008-02-13 18:58:46 +01001216 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001217 page = alloc_page(GFP_KERNEL);
1218 if (!page)
1219 goto uninit;
1220
Joerg Roedelf65c2292008-02-13 18:58:46 +01001221 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1222 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001223 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001224
1225 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1226 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001227 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001228
Alexander Grafb286d5d2008-11-25 20:17:05 +01001229 hsave_page = alloc_page(GFP_KERNEL);
1230 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001231 goto free_page3;
1232
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001233 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001234
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001235 svm->msrpm = page_address(msrpm_pages);
1236 svm_vcpu_init_msrpm(svm->msrpm);
1237
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001238 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001239 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001240
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001241 svm->vmcb = page_address(page);
1242 clear_page(svm->vmcb);
1243 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1244 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +01001245 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001246 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001247
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001248 err = fx_init(&svm->vcpu);
1249 if (err)
1250 goto free_page4;
1251
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001252 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001253 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001254 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001256 svm_init_osvw(&svm->vcpu);
1257
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001258 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001259
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001260free_page4:
1261 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001262free_page3:
1263 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1264free_page2:
1265 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1266free_page1:
1267 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001268uninit:
1269 kvm_vcpu_uninit(&svm->vcpu);
1270free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001271 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001272out:
1273 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274}
1275
1276static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1277{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001278 struct vcpu_svm *svm = to_svm(vcpu);
1279
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001280 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001281 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001282 __free_page(virt_to_page(svm->nested.hsave));
1283 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001284 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001285 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286}
1287
Avi Kivity15ad7142007-07-11 18:17:21 +03001288static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001290 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001291 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001292
Avi Kivity0cc50642007-03-25 12:07:27 +02001293 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001294 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001295 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001296 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001297
Avi Kivity82ca2d12010-10-21 12:20:34 +02001298#ifdef CONFIG_X86_64
1299 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1300#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001301 savesegment(fs, svm->host.fs);
1302 savesegment(gs, svm->host.gs);
1303 svm->host.ldt = kvm_read_ldt();
1304
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001305 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001306 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001307
1308 if (static_cpu_has(X86_FEATURE_TSCRATEMSR) &&
1309 svm->tsc_ratio != __get_cpu_var(current_tsc_ratio)) {
1310 __get_cpu_var(current_tsc_ratio) = svm->tsc_ratio;
1311 wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_ratio);
1312 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313}
1314
1315static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1316{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001317 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001318 int i;
1319
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001320 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001321 kvm_load_ldt(svm->host.ldt);
1322#ifdef CONFIG_X86_64
1323 loadsegment(fs, svm->host.fs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001324 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001325 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001326#else
Avi Kivity831ca602011-03-08 16:09:51 +02001327#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001328 loadsegment(gs, svm->host.gs);
1329#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001330#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001331 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001332 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333}
1334
Kevin Wolfea5e97e2012-02-08 14:34:40 +01001335static void svm_update_cpl(struct kvm_vcpu *vcpu)
1336{
1337 struct vcpu_svm *svm = to_svm(vcpu);
1338 int cpl;
1339
1340 if (!is_protmode(vcpu))
1341 cpl = 0;
1342 else if (svm->vmcb->save.rflags & X86_EFLAGS_VM)
1343 cpl = 3;
1344 else
1345 cpl = svm->vmcb->save.cs.selector & 0x3;
1346
1347 svm->vmcb->save.cpl = cpl;
1348}
1349
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1351{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001352 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353}
1354
1355static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1356{
Kevin Wolf4cee4792012-02-08 14:34:41 +01001357 unsigned long old_rflags = to_svm(vcpu)->vmcb->save.rflags;
1358
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001359 to_svm(vcpu)->vmcb->save.rflags = rflags;
Kevin Wolf4cee4792012-02-08 14:34:41 +01001360 if ((old_rflags ^ rflags) & X86_EFLAGS_VM)
1361 svm_update_cpl(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362}
1363
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001364static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1365{
1366 switch (reg) {
1367 case VCPU_EXREG_PDPTR:
1368 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001369 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001370 break;
1371 default:
1372 BUG();
1373 }
1374}
1375
Alexander Graff0b85052008-11-25 20:17:01 +01001376static void svm_set_vintr(struct vcpu_svm *svm)
1377{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001378 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001379}
1380
1381static void svm_clear_vintr(struct vcpu_svm *svm)
1382{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001383 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001384}
1385
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1387{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001388 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389
1390 switch (seg) {
1391 case VCPU_SREG_CS: return &save->cs;
1392 case VCPU_SREG_DS: return &save->ds;
1393 case VCPU_SREG_ES: return &save->es;
1394 case VCPU_SREG_FS: return &save->fs;
1395 case VCPU_SREG_GS: return &save->gs;
1396 case VCPU_SREG_SS: return &save->ss;
1397 case VCPU_SREG_TR: return &save->tr;
1398 case VCPU_SREG_LDTR: return &save->ldtr;
1399 }
1400 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001401 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402}
1403
1404static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1405{
1406 struct vmcb_seg *s = svm_seg(vcpu, seg);
1407
1408 return s->base;
1409}
1410
1411static void svm_get_segment(struct kvm_vcpu *vcpu,
1412 struct kvm_segment *var, int seg)
1413{
1414 struct vmcb_seg *s = svm_seg(vcpu, seg);
1415
1416 var->base = s->base;
1417 var->limit = s->limit;
1418 var->selector = s->selector;
1419 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1420 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1421 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1422 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1423 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1424 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1425 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1426 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001427
Joerg Roedele0231712010-02-24 18:59:10 +01001428 /*
1429 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001430 * for cross vendor migration purposes by "not present"
1431 */
1432 var->unusable = !var->present || (var->type == 0);
1433
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001434 switch (seg) {
1435 case VCPU_SREG_CS:
1436 /*
1437 * SVM always stores 0 for the 'G' bit in the CS selector in
1438 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1439 * Intel's VMENTRY has a check on the 'G' bit.
1440 */
Amit Shah25022ac2008-10-27 09:04:17 +00001441 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001442 break;
1443 case VCPU_SREG_TR:
1444 /*
1445 * Work around a bug where the busy flag in the tr selector
1446 * isn't exposed
1447 */
Amit Shahc0d09822008-10-27 09:04:18 +00001448 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001449 break;
1450 case VCPU_SREG_DS:
1451 case VCPU_SREG_ES:
1452 case VCPU_SREG_FS:
1453 case VCPU_SREG_GS:
1454 /*
1455 * The accessed bit must always be set in the segment
1456 * descriptor cache, although it can be cleared in the
1457 * descriptor, the cached bit always remains at 1. Since
1458 * Intel has a check on this, set it here to support
1459 * cross-vendor migration.
1460 */
1461 if (!var->unusable)
1462 var->type |= 0x1;
1463 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001464 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001465 /*
1466 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001467 * descriptor is left as 1, although the whole segment has
1468 * been made unusable. Clear it here to pass an Intel VMX
1469 * entry check when cross vendor migrating.
1470 */
1471 if (var->unusable)
1472 var->db = 0;
1473 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001474 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475}
1476
Izik Eidus2e4d2652008-03-24 19:38:34 +02001477static int svm_get_cpl(struct kvm_vcpu *vcpu)
1478{
1479 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1480
1481 return save->cpl;
1482}
1483
Gleb Natapov89a27f42010-02-16 10:51:48 +02001484static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001486 struct vcpu_svm *svm = to_svm(vcpu);
1487
Gleb Natapov89a27f42010-02-16 10:51:48 +02001488 dt->size = svm->vmcb->save.idtr.limit;
1489 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490}
1491
Gleb Natapov89a27f42010-02-16 10:51:48 +02001492static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001494 struct vcpu_svm *svm = to_svm(vcpu);
1495
Gleb Natapov89a27f42010-02-16 10:51:48 +02001496 svm->vmcb->save.idtr.limit = dt->size;
1497 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001498 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499}
1500
Gleb Natapov89a27f42010-02-16 10:51:48 +02001501static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001503 struct vcpu_svm *svm = to_svm(vcpu);
1504
Gleb Natapov89a27f42010-02-16 10:51:48 +02001505 dt->size = svm->vmcb->save.gdtr.limit;
1506 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507}
1508
Gleb Natapov89a27f42010-02-16 10:51:48 +02001509static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001511 struct vcpu_svm *svm = to_svm(vcpu);
1512
Gleb Natapov89a27f42010-02-16 10:51:48 +02001513 svm->vmcb->save.gdtr.limit = dt->size;
1514 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001515 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516}
1517
Avi Kivitye8467fd2009-12-29 18:43:06 +02001518static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1519{
1520}
1521
Avi Kivityaff48ba2010-12-05 18:56:11 +02001522static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1523{
1524}
1525
Anthony Liguori25c4c272007-04-27 09:29:21 +03001526static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001527{
1528}
1529
Avi Kivityd2251572010-01-06 10:55:27 +02001530static void update_cr0_intercept(struct vcpu_svm *svm)
1531{
1532 ulong gcr0 = svm->vcpu.arch.cr0;
1533 u64 *hcr0 = &svm->vmcb->save.cr0;
1534
1535 if (!svm->vcpu.fpu_active)
1536 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1537 else
1538 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1539 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1540
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001541 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001542
1543 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001544 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1545 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001546 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001547 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1548 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001549 }
1550}
1551
Avi Kivity6aa8b732006-12-10 02:21:36 -08001552static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1553{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001554 struct vcpu_svm *svm = to_svm(vcpu);
1555
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001556#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001557 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001558 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001559 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001560 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001561 }
1562
Mike Dayd77c26f2007-10-08 09:02:08 -04001563 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001564 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001565 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566 }
1567 }
1568#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001569 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001570
1571 if (!npt_enabled)
1572 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001573
1574 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001575 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001576 /*
1577 * re-enable caching here because the QEMU bios
1578 * does not do it - this results in some delay at
1579 * reboot
1580 */
1581 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001582 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001583 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001584 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585}
1586
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001587static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588{
Joerg Roedel6394b642008-04-09 14:15:29 +02001589 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001590 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1591
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001592 if (cr4 & X86_CR4_VMXE)
1593 return 1;
1594
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001595 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001596 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001597
Joerg Roedelec077262008-04-09 14:15:28 +02001598 vcpu->arch.cr4 = cr4;
1599 if (!npt_enabled)
1600 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001601 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001602 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001603 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001604 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605}
1606
1607static void svm_set_segment(struct kvm_vcpu *vcpu,
1608 struct kvm_segment *var, int seg)
1609{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001610 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611 struct vmcb_seg *s = svm_seg(vcpu, seg);
1612
1613 s->base = var->base;
1614 s->limit = var->limit;
1615 s->selector = var->selector;
1616 if (var->unusable)
1617 s->attrib = 0;
1618 else {
1619 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1620 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1621 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1622 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1623 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1624 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1625 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1626 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1627 }
1628 if (seg == VCPU_SREG_CS)
Kevin Wolfea5e97e2012-02-08 14:34:40 +01001629 svm_update_cpl(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630
Joerg Roedel060d0c92010-12-03 11:45:57 +01001631 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632}
1633
Gleb Natapov44c11432009-05-11 13:35:52 +03001634static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001636 struct vcpu_svm *svm = to_svm(vcpu);
1637
Joerg Roedel18c918c2010-11-30 18:03:59 +01001638 clr_exception_intercept(svm, DB_VECTOR);
1639 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001640
Jan Kiszka6be7d302009-10-18 13:24:54 +02001641 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001642 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001643
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001644 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1645 if (vcpu->guest_debug &
1646 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001647 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001648 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001649 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001650 } else
1651 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001652}
1653
Jan Kiszka355be0b2009-10-03 00:31:21 +02001654static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001655{
Gleb Natapov44c11432009-05-11 13:35:52 +03001656 struct vcpu_svm *svm = to_svm(vcpu);
1657
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001658 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1659 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1660 else
1661 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1662
Joerg Roedel72214b92010-12-03 11:45:55 +01001663 mark_dirty(svm->vmcb, VMCB_DR);
1664
Jan Kiszka355be0b2009-10-03 00:31:21 +02001665 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001666}
1667
Tejun Heo0fe1e002009-10-29 22:34:14 +09001668static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001670 if (sd->next_asid > sd->max_asid) {
1671 ++sd->asid_generation;
1672 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001673 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 }
1675
Tejun Heo0fe1e002009-10-29 22:34:14 +09001676 svm->asid_generation = sd->asid_generation;
1677 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001678
1679 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680}
1681
Gleb Natapov020df072010-04-13 10:05:23 +03001682static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001684 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001685
Gleb Natapov020df072010-04-13 10:05:23 +03001686 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01001687 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688}
1689
Avi Kivity851ba692009-08-24 11:10:17 +03001690static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691{
Gleb Natapov631bc482010-10-14 11:22:52 +02001692 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001694 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695
Gleb Natapov631bc482010-10-14 11:22:52 +02001696 switch (svm->apf_reason) {
1697 default:
1698 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001699
Gleb Natapov631bc482010-10-14 11:22:52 +02001700 trace_kvm_page_fault(fault_address, error_code);
1701 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1702 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01001703 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1704 svm->vmcb->control.insn_bytes,
1705 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02001706 break;
1707 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1708 svm->apf_reason = 0;
1709 local_irq_disable();
1710 kvm_async_pf_task_wait(fault_address);
1711 local_irq_enable();
1712 break;
1713 case KVM_PV_REASON_PAGE_READY:
1714 svm->apf_reason = 0;
1715 local_irq_disable();
1716 kvm_async_pf_task_wake(fault_address);
1717 local_irq_enable();
1718 break;
1719 }
1720 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721}
1722
Avi Kivity851ba692009-08-24 11:10:17 +03001723static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001724{
Avi Kivity851ba692009-08-24 11:10:17 +03001725 struct kvm_run *kvm_run = svm->vcpu.run;
1726
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001727 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001728 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001729 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001730 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1731 return 1;
1732 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001733
Jan Kiszka6be7d302009-10-18 13:24:54 +02001734 if (svm->nmi_singlestep) {
1735 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001736 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1737 svm->vmcb->save.rflags &=
1738 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1739 update_db_intercept(&svm->vcpu);
1740 }
1741
1742 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001743 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001744 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1745 kvm_run->debug.arch.pc =
1746 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1747 kvm_run->debug.arch.exception = DB_VECTOR;
1748 return 0;
1749 }
1750
1751 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001752}
1753
Avi Kivity851ba692009-08-24 11:10:17 +03001754static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001755{
Avi Kivity851ba692009-08-24 11:10:17 +03001756 struct kvm_run *kvm_run = svm->vcpu.run;
1757
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001758 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1759 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1760 kvm_run->debug.arch.exception = BP_VECTOR;
1761 return 0;
1762}
1763
Avi Kivity851ba692009-08-24 11:10:17 +03001764static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001765{
1766 int er;
1767
Andre Przywara51d8b662010-12-21 11:12:02 +01001768 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001769 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001770 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001771 return 1;
1772}
1773
Avi Kivity6b52d182010-01-21 15:31:47 +02001774static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001775{
Avi Kivity6b52d182010-01-21 15:31:47 +02001776 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001777
Joerg Roedel18c918c2010-11-30 18:03:59 +01001778 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001779
Rusty Russelle756fc62007-07-30 20:07:08 +10001780 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001781 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001782}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001783
Avi Kivity6b52d182010-01-21 15:31:47 +02001784static int nm_interception(struct vcpu_svm *svm)
1785{
1786 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001787 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001788}
1789
Joerg Roedel67ec6602010-05-17 14:43:35 +02001790static bool is_erratum_383(void)
1791{
1792 int err, i;
1793 u64 value;
1794
1795 if (!erratum_383_found)
1796 return false;
1797
1798 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1799 if (err)
1800 return false;
1801
1802 /* Bit 62 may or may not be set for this mce */
1803 value &= ~(1ULL << 62);
1804
1805 if (value != 0xb600000000010015ULL)
1806 return false;
1807
1808 /* Clear MCi_STATUS registers */
1809 for (i = 0; i < 6; ++i)
1810 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1811
1812 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1813 if (!err) {
1814 u32 low, high;
1815
1816 value &= ~(1ULL << 2);
1817 low = lower_32_bits(value);
1818 high = upper_32_bits(value);
1819
1820 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1821 }
1822
1823 /* Flush tlb to evict multi-match entries */
1824 __flush_tlb_all();
1825
1826 return true;
1827}
1828
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001829static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001830{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001831 if (is_erratum_383()) {
1832 /*
1833 * Erratum 383 triggered. Guest state is corrupt so kill the
1834 * guest.
1835 */
1836 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1837
Avi Kivitya8eeb042010-05-10 12:34:53 +03001838 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001839
1840 return;
1841 }
1842
Joerg Roedel53371b52008-04-09 14:15:30 +02001843 /*
1844 * On an #MC intercept the MCE handler is not called automatically in
1845 * the host. So do it by hand here.
1846 */
1847 asm volatile (
1848 "int $0x12\n");
1849 /* not sure if we ever come back to this point */
1850
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001851 return;
1852}
1853
1854static int mc_interception(struct vcpu_svm *svm)
1855{
Joerg Roedel53371b52008-04-09 14:15:30 +02001856 return 1;
1857}
1858
Avi Kivity851ba692009-08-24 11:10:17 +03001859static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001860{
Avi Kivity851ba692009-08-24 11:10:17 +03001861 struct kvm_run *kvm_run = svm->vcpu.run;
1862
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001863 /*
1864 * VMCB is undefined after a SHUTDOWN intercept
1865 * so reinitialize it.
1866 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001867 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001868 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001869
1870 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1871 return 0;
1872}
1873
Avi Kivity851ba692009-08-24 11:10:17 +03001874static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001876 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001877 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001878 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001879 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880
Rusty Russelle756fc62007-07-30 20:07:08 +10001881 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001882 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001883 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001884 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01001885 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001886
Avi Kivity039576c2007-03-20 12:46:50 +02001887 port = io_info >> 16;
1888 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001889 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001890 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001891
1892 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893}
1894
Avi Kivity851ba692009-08-24 11:10:17 +03001895static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001896{
1897 return 1;
1898}
1899
Avi Kivity851ba692009-08-24 11:10:17 +03001900static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001901{
1902 ++svm->vcpu.stat.irq_exits;
1903 return 1;
1904}
1905
Avi Kivity851ba692009-08-24 11:10:17 +03001906static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907{
1908 return 1;
1909}
1910
Avi Kivity851ba692009-08-24 11:10:17 +03001911static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001913 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001914 skip_emulated_instruction(&svm->vcpu);
1915 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916}
1917
Avi Kivity851ba692009-08-24 11:10:17 +03001918static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001919{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001920 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001921 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001922 kvm_emulate_hypercall(&svm->vcpu);
1923 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001924}
1925
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001926static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1927{
1928 struct vcpu_svm *svm = to_svm(vcpu);
1929
1930 return svm->nested.nested_cr3;
1931}
1932
Avi Kivitye4e517b2011-07-28 11:36:17 +03001933static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
1934{
1935 struct vcpu_svm *svm = to_svm(vcpu);
1936 u64 cr3 = svm->nested.nested_cr3;
1937 u64 pdpte;
1938 int ret;
1939
1940 ret = kvm_read_guest_page(vcpu->kvm, gpa_to_gfn(cr3), &pdpte,
1941 offset_in_page(cr3) + index * 8, 8);
1942 if (ret)
1943 return 0;
1944 return pdpte;
1945}
1946
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001947static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1948 unsigned long root)
1949{
1950 struct vcpu_svm *svm = to_svm(vcpu);
1951
1952 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01001953 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001954 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001955}
1956
Avi Kivity6389ee92010-11-29 16:12:30 +02001957static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1958 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001959{
1960 struct vcpu_svm *svm = to_svm(vcpu);
1961
1962 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1963 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001964 svm->vmcb->control.exit_info_1 = fault->error_code;
1965 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001966
1967 nested_svm_vmexit(svm);
1968}
1969
Joerg Roedel4b161842010-09-10 17:31:03 +02001970static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1971{
1972 int r;
1973
1974 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1975
1976 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1977 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03001978 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02001979 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1980 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1981 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1982
1983 return r;
1984}
1985
1986static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1987{
1988 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1989}
1990
Alexander Grafc0725422008-11-25 20:17:03 +01001991static int nested_svm_check_permissions(struct vcpu_svm *svm)
1992{
Avi Kivityf6801df2010-01-21 15:31:50 +02001993 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001994 || !is_paging(&svm->vcpu)) {
1995 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1996 return 1;
1997 }
1998
1999 if (svm->vmcb->save.cpl) {
2000 kvm_inject_gp(&svm->vcpu, 0);
2001 return 1;
2002 }
2003
2004 return 0;
2005}
2006
Alexander Grafcf74a782008-11-25 20:17:08 +01002007static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2008 bool has_error_code, u32 error_code)
2009{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002010 int vmexit;
2011
Joerg Roedel20307532010-11-29 17:51:48 +01002012 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002013 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002014
Joerg Roedel0295ad72009-08-07 11:49:37 +02002015 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2016 svm->vmcb->control.exit_code_hi = 0;
2017 svm->vmcb->control.exit_info_1 = error_code;
2018 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
2019
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002020 vmexit = nested_svm_intercept(svm);
2021 if (vmexit == NESTED_EXIT_DONE)
2022 svm->nested.exit_required = true;
2023
2024 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002025}
2026
Joerg Roedel8fe54652010-02-19 16:23:01 +01002027/* This function returns true if it is save to enable the irq window */
2028static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002029{
Joerg Roedel20307532010-11-29 17:51:48 +01002030 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002031 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002032
Joerg Roedel26666952009-08-07 11:49:46 +02002033 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002034 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002035
Joerg Roedel26666952009-08-07 11:49:46 +02002036 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002037 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002038
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002039 /*
2040 * if vmexit was already requested (by intercepted exception
2041 * for instance) do not overwrite it with "external interrupt"
2042 * vmexit.
2043 */
2044 if (svm->nested.exit_required)
2045 return false;
2046
Joerg Roedel197717d2010-02-24 18:59:19 +01002047 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2048 svm->vmcb->control.exit_info_1 = 0;
2049 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002050
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002051 if (svm->nested.intercept & 1ULL) {
2052 /*
2053 * The #vmexit can't be emulated here directly because this
2054 * code path runs with irqs and preemtion disabled. A
2055 * #vmexit emulation might sleep. Only signal request for
2056 * the #vmexit here.
2057 */
2058 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002059 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002060 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002061 }
2062
Joerg Roedel8fe54652010-02-19 16:23:01 +01002063 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002064}
2065
Joerg Roedel887f5002010-02-24 18:59:12 +01002066/* This function returns true if it is save to enable the nmi window */
2067static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2068{
Joerg Roedel20307532010-11-29 17:51:48 +01002069 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002070 return true;
2071
2072 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2073 return true;
2074
2075 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2076 svm->nested.exit_required = true;
2077
2078 return false;
2079}
2080
Joerg Roedel7597f122010-02-19 16:23:00 +01002081static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002082{
2083 struct page *page;
2084
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002085 might_sleep();
2086
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002087 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002088 if (is_error_page(page))
2089 goto error;
2090
Joerg Roedel7597f122010-02-19 16:23:00 +01002091 *_page = page;
2092
2093 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002094
2095error:
2096 kvm_release_page_clean(page);
2097 kvm_inject_gp(&svm->vcpu, 0);
2098
2099 return NULL;
2100}
2101
Joerg Roedel7597f122010-02-19 16:23:00 +01002102static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002103{
Joerg Roedel7597f122010-02-19 16:23:00 +01002104 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002105 kvm_release_page_dirty(page);
2106}
2107
Joerg Roedelce2ac082010-03-01 15:34:39 +01002108static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002109{
Joerg Roedelce2ac082010-03-01 15:34:39 +01002110 unsigned port;
2111 u8 val, bit;
2112 u64 gpa;
2113
2114 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2115 return NESTED_EXIT_HOST;
2116
2117 port = svm->vmcb->control.exit_info_1 >> 16;
2118 gpa = svm->nested.vmcb_iopm + (port / 8);
2119 bit = port % 8;
2120 val = 0;
2121
2122 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
2123 val &= (1 << bit);
2124
2125 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
2126}
2127
Joerg Roedeld2477822010-03-01 15:34:34 +01002128static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002129{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002130 u32 offset, msr, value;
2131 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002132
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002133 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002134 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002135
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002136 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2137 offset = svm_msrpm_offset(msr);
2138 write = svm->vmcb->control.exit_info_1 & 1;
2139 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002140
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002141 if (offset == MSR_INVALID)
2142 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002143
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002144 /* Offset is in 32 bit units but need in 8 bit units */
2145 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002146
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002147 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
2148 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002149
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002150 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002151}
2152
Joerg Roedel410e4d52009-08-07 11:49:44 +02002153static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002154{
Alexander Grafcf74a782008-11-25 20:17:08 +01002155 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002156
Joerg Roedel410e4d52009-08-07 11:49:44 +02002157 switch (exit_code) {
2158 case SVM_EXIT_INTR:
2159 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002160 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002161 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002162 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002163 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002164 if (npt_enabled)
2165 return NESTED_EXIT_HOST;
2166 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002167 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002168 /* When we're shadowing, trap PFs, but not async PF */
2169 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002170 return NESTED_EXIT_HOST;
2171 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01002172 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
2173 nm_interception(svm);
2174 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002175 default:
2176 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002177 }
2178
Joerg Roedel410e4d52009-08-07 11:49:44 +02002179 return NESTED_EXIT_CONTINUE;
2180}
2181
2182/*
2183 * If this function returns true, this #vmexit was already handled
2184 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002185static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002186{
2187 u32 exit_code = svm->vmcb->control.exit_code;
2188 int vmexit = NESTED_EXIT_HOST;
2189
Alexander Grafcf74a782008-11-25 20:17:08 +01002190 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002191 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002192 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002193 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002194 case SVM_EXIT_IOIO:
2195 vmexit = nested_svm_intercept_ioio(svm);
2196 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002197 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2198 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2199 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002200 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002201 break;
2202 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002203 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2204 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2205 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002206 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002207 break;
2208 }
2209 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2210 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002211 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002212 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002213 /* async page fault always cause vmexit */
2214 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2215 svm->apf_reason != 0)
2216 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002217 break;
2218 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002219 case SVM_EXIT_ERR: {
2220 vmexit = NESTED_EXIT_DONE;
2221 break;
2222 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002223 default: {
2224 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002225 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002226 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002227 }
2228 }
2229
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002230 return vmexit;
2231}
2232
2233static int nested_svm_exit_handled(struct vcpu_svm *svm)
2234{
2235 int vmexit;
2236
2237 vmexit = nested_svm_intercept(svm);
2238
2239 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002240 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002241
2242 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002243}
2244
Joerg Roedel0460a972009-08-07 11:49:31 +02002245static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2246{
2247 struct vmcb_control_area *dst = &dst_vmcb->control;
2248 struct vmcb_control_area *from = &from_vmcb->control;
2249
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002250 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002251 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002252 dst->intercept_exceptions = from->intercept_exceptions;
2253 dst->intercept = from->intercept;
2254 dst->iopm_base_pa = from->iopm_base_pa;
2255 dst->msrpm_base_pa = from->msrpm_base_pa;
2256 dst->tsc_offset = from->tsc_offset;
2257 dst->asid = from->asid;
2258 dst->tlb_ctl = from->tlb_ctl;
2259 dst->int_ctl = from->int_ctl;
2260 dst->int_vector = from->int_vector;
2261 dst->int_state = from->int_state;
2262 dst->exit_code = from->exit_code;
2263 dst->exit_code_hi = from->exit_code_hi;
2264 dst->exit_info_1 = from->exit_info_1;
2265 dst->exit_info_2 = from->exit_info_2;
2266 dst->exit_int_info = from->exit_int_info;
2267 dst->exit_int_info_err = from->exit_int_info_err;
2268 dst->nested_ctl = from->nested_ctl;
2269 dst->event_inj = from->event_inj;
2270 dst->event_inj_err = from->event_inj_err;
2271 dst->nested_cr3 = from->nested_cr3;
2272 dst->lbr_ctl = from->lbr_ctl;
2273}
2274
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002275static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002276{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002277 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002278 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002279 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002280 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002281
Joerg Roedel17897f32009-10-09 16:08:29 +02002282 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2283 vmcb->control.exit_info_1,
2284 vmcb->control.exit_info_2,
2285 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002286 vmcb->control.exit_int_info_err,
2287 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002288
Joerg Roedel7597f122010-02-19 16:23:00 +01002289 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002290 if (!nested_vmcb)
2291 return 1;
2292
Joerg Roedel20307532010-11-29 17:51:48 +01002293 /* Exit Guest-Mode */
2294 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002295 svm->nested.vmcb = 0;
2296
Alexander Grafcf74a782008-11-25 20:17:08 +01002297 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002298 disable_gif(svm);
2299
2300 nested_vmcb->save.es = vmcb->save.es;
2301 nested_vmcb->save.cs = vmcb->save.cs;
2302 nested_vmcb->save.ss = vmcb->save.ss;
2303 nested_vmcb->save.ds = vmcb->save.ds;
2304 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2305 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002306 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002307 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002308 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002309 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002310 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002311 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002312 nested_vmcb->save.rip = vmcb->save.rip;
2313 nested_vmcb->save.rsp = vmcb->save.rsp;
2314 nested_vmcb->save.rax = vmcb->save.rax;
2315 nested_vmcb->save.dr7 = vmcb->save.dr7;
2316 nested_vmcb->save.dr6 = vmcb->save.dr6;
2317 nested_vmcb->save.cpl = vmcb->save.cpl;
2318
2319 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2320 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2321 nested_vmcb->control.int_state = vmcb->control.int_state;
2322 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2323 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2324 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2325 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2326 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2327 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002328 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002329
2330 /*
2331 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2332 * to make sure that we do not lose injected events. So check event_inj
2333 * here and copy it to exit_int_info if it is valid.
2334 * Exit_int_info and event_inj can't be both valid because the case
2335 * below only happens on a VMRUN instruction intercept which has
2336 * no valid exit_int_info set.
2337 */
2338 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2339 struct vmcb_control_area *nc = &nested_vmcb->control;
2340
2341 nc->exit_int_info = vmcb->control.event_inj;
2342 nc->exit_int_info_err = vmcb->control.event_inj_err;
2343 }
2344
Joerg Roedel33740e42009-08-07 11:49:29 +02002345 nested_vmcb->control.tlb_ctl = 0;
2346 nested_vmcb->control.event_inj = 0;
2347 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002348
2349 /* We always set V_INTR_MASKING and remember the old value in hflags */
2350 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2351 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2352
Alexander Grafcf74a782008-11-25 20:17:08 +01002353 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002354 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002355
Alexander Graf219b65d2009-06-15 15:21:25 +02002356 kvm_clear_exception_queue(&svm->vcpu);
2357 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002358
Joerg Roedel4b161842010-09-10 17:31:03 +02002359 svm->nested.nested_cr3 = 0;
2360
Alexander Grafcf74a782008-11-25 20:17:08 +01002361 /* Restore selected save entries */
2362 svm->vmcb->save.es = hsave->save.es;
2363 svm->vmcb->save.cs = hsave->save.cs;
2364 svm->vmcb->save.ss = hsave->save.ss;
2365 svm->vmcb->save.ds = hsave->save.ds;
2366 svm->vmcb->save.gdtr = hsave->save.gdtr;
2367 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002368 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002369 svm_set_efer(&svm->vcpu, hsave->save.efer);
2370 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2371 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2372 if (npt_enabled) {
2373 svm->vmcb->save.cr3 = hsave->save.cr3;
2374 svm->vcpu.arch.cr3 = hsave->save.cr3;
2375 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002376 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002377 }
2378 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2379 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2380 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2381 svm->vmcb->save.dr7 = 0;
2382 svm->vmcb->save.cpl = 0;
2383 svm->vmcb->control.exit_int_info = 0;
2384
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002385 mark_all_dirty(svm->vmcb);
2386
Joerg Roedel7597f122010-02-19 16:23:00 +01002387 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002388
Joerg Roedel4b161842010-09-10 17:31:03 +02002389 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002390 kvm_mmu_reset_context(&svm->vcpu);
2391 kvm_mmu_load(&svm->vcpu);
2392
2393 return 0;
2394}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002395
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002396static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002397{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002398 /*
2399 * This function merges the msr permission bitmaps of kvm and the
2400 * nested vmcb. It is omptimized in that it only merges the parts where
2401 * the kvm msr permission bitmap may contain zero bits
2402 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002403 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002404
Joerg Roedel323c3d82010-03-01 15:34:37 +01002405 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2406 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002407
Joerg Roedel323c3d82010-03-01 15:34:37 +01002408 for (i = 0; i < MSRPM_OFFSETS; i++) {
2409 u32 value, p;
2410 u64 offset;
2411
2412 if (msrpm_offsets[i] == 0xffffffff)
2413 break;
2414
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002415 p = msrpm_offsets[i];
2416 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002417
2418 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2419 return false;
2420
2421 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2422 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002423
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002424 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002425
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002426 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002427}
2428
Joerg Roedel52c65a302010-08-02 16:46:44 +02002429static bool nested_vmcb_checks(struct vmcb *vmcb)
2430{
2431 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2432 return false;
2433
Joerg Roedeldbe77582010-08-02 16:46:45 +02002434 if (vmcb->control.asid == 0)
2435 return false;
2436
Joerg Roedel4b161842010-09-10 17:31:03 +02002437 if (vmcb->control.nested_ctl && !npt_enabled)
2438 return false;
2439
Joerg Roedel52c65a302010-08-02 16:46:44 +02002440 return true;
2441}
2442
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002443static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002444{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002445 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002446 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002447 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002448 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002449 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002450
Joerg Roedel06fc77722010-02-19 16:23:07 +01002451 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002452
Joerg Roedel7597f122010-02-19 16:23:00 +01002453 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002454 if (!nested_vmcb)
2455 return false;
2456
Joerg Roedel52c65a302010-08-02 16:46:44 +02002457 if (!nested_vmcb_checks(nested_vmcb)) {
2458 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2459 nested_vmcb->control.exit_code_hi = 0;
2460 nested_vmcb->control.exit_info_1 = 0;
2461 nested_vmcb->control.exit_info_2 = 0;
2462
2463 nested_svm_unmap(page);
2464
2465 return false;
2466 }
2467
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002468 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002469 nested_vmcb->save.rip,
2470 nested_vmcb->control.int_ctl,
2471 nested_vmcb->control.event_inj,
2472 nested_vmcb->control.nested_ctl);
2473
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002474 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2475 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002476 nested_vmcb->control.intercept_exceptions,
2477 nested_vmcb->control.intercept);
2478
Alexander Graf3d6368e2008-11-25 20:17:07 +01002479 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002480 kvm_clear_exception_queue(&svm->vcpu);
2481 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002482
Joerg Roedele0231712010-02-24 18:59:10 +01002483 /*
2484 * Save the old vmcb, so we don't need to pick what we save, but can
2485 * restore everything when a VMEXIT occurs
2486 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002487 hsave->save.es = vmcb->save.es;
2488 hsave->save.cs = vmcb->save.cs;
2489 hsave->save.ss = vmcb->save.ss;
2490 hsave->save.ds = vmcb->save.ds;
2491 hsave->save.gdtr = vmcb->save.gdtr;
2492 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002493 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002494 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002495 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002496 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002497 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002498 hsave->save.rsp = vmcb->save.rsp;
2499 hsave->save.rax = vmcb->save.rax;
2500 if (npt_enabled)
2501 hsave->save.cr3 = vmcb->save.cr3;
2502 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002503 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002504
Joerg Roedel0460a972009-08-07 11:49:31 +02002505 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002506
Avi Kivityf6e78472010-08-02 15:30:20 +03002507 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002508 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2509 else
2510 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2511
Joerg Roedel4b161842010-09-10 17:31:03 +02002512 if (nested_vmcb->control.nested_ctl) {
2513 kvm_mmu_unload(&svm->vcpu);
2514 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2515 nested_svm_init_mmu_context(&svm->vcpu);
2516 }
2517
Alexander Graf3d6368e2008-11-25 20:17:07 +01002518 /* Load the nested guest state */
2519 svm->vmcb->save.es = nested_vmcb->save.es;
2520 svm->vmcb->save.cs = nested_vmcb->save.cs;
2521 svm->vmcb->save.ss = nested_vmcb->save.ss;
2522 svm->vmcb->save.ds = nested_vmcb->save.ds;
2523 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2524 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002525 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002526 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2527 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2528 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2529 if (npt_enabled) {
2530 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2531 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002532 } else
Avi Kivity23902182010-06-10 17:02:16 +03002533 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002534
2535 /* Guest paging mode is active - reset mmu */
2536 kvm_mmu_reset_context(&svm->vcpu);
2537
Joerg Roedeldefbba52009-08-07 11:49:30 +02002538 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002539 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2540 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2541 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002542
Alexander Graf3d6368e2008-11-25 20:17:07 +01002543 /* In case we don't even reach vcpu_run, the fields are not updated */
2544 svm->vmcb->save.rax = nested_vmcb->save.rax;
2545 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2546 svm->vmcb->save.rip = nested_vmcb->save.rip;
2547 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2548 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2549 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2550
Joerg Roedelf7138532010-03-01 15:34:40 +01002551 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002552 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002553
Joerg Roedelaad42c62009-08-07 11:49:34 +02002554 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002555 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002556 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002557 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2558 svm->nested.intercept = nested_vmcb->control.intercept;
2559
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002560 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002561 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002562 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2563 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2564 else
2565 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2566
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002567 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2568 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002569 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2570 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002571 }
2572
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002573 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002574 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002575
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002576 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002577 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2578 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2579 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002580 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2581 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2582
Joerg Roedel7597f122010-02-19 16:23:00 +01002583 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002584
Joerg Roedel20307532010-11-29 17:51:48 +01002585 /* Enter Guest-Mode */
2586 enter_guest_mode(&svm->vcpu);
2587
Joerg Roedel384c6362010-11-30 18:03:56 +01002588 /*
2589 * Merge guest and host intercepts - must be called with vcpu in
2590 * guest-mode to take affect here
2591 */
2592 recalc_intercepts(svm);
2593
Joerg Roedel06fc77722010-02-19 16:23:07 +01002594 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002595
Joerg Roedel2af91942009-08-07 11:49:28 +02002596 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002597
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002598 mark_all_dirty(svm->vmcb);
2599
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002600 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002601}
2602
Joerg Roedel9966bf62009-08-07 11:49:40 +02002603static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002604{
2605 to_vmcb->save.fs = from_vmcb->save.fs;
2606 to_vmcb->save.gs = from_vmcb->save.gs;
2607 to_vmcb->save.tr = from_vmcb->save.tr;
2608 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2609 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2610 to_vmcb->save.star = from_vmcb->save.star;
2611 to_vmcb->save.lstar = from_vmcb->save.lstar;
2612 to_vmcb->save.cstar = from_vmcb->save.cstar;
2613 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2614 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2615 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2616 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002617}
2618
Avi Kivity851ba692009-08-24 11:10:17 +03002619static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002620{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002621 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002622 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002623
Alexander Graf55426752008-11-25 20:17:06 +01002624 if (nested_svm_check_permissions(svm))
2625 return 1;
2626
Joerg Roedel7597f122010-02-19 16:23:00 +01002627 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002628 if (!nested_vmcb)
2629 return 1;
2630
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002631 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2632 skip_emulated_instruction(&svm->vcpu);
2633
Joerg Roedel9966bf62009-08-07 11:49:40 +02002634 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002635 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002636
2637 return 1;
2638}
2639
Avi Kivity851ba692009-08-24 11:10:17 +03002640static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002641{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002642 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002643 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002644
Alexander Graf55426752008-11-25 20:17:06 +01002645 if (nested_svm_check_permissions(svm))
2646 return 1;
2647
Joerg Roedel7597f122010-02-19 16:23:00 +01002648 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002649 if (!nested_vmcb)
2650 return 1;
2651
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002652 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2653 skip_emulated_instruction(&svm->vcpu);
2654
Joerg Roedel9966bf62009-08-07 11:49:40 +02002655 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002656 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002657
2658 return 1;
2659}
2660
Avi Kivity851ba692009-08-24 11:10:17 +03002661static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002662{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002663 if (nested_svm_check_permissions(svm))
2664 return 1;
2665
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002666 /* Save rip after vmrun instruction */
2667 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002668
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002669 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002670 return 1;
2671
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002672 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002673 goto failed;
2674
2675 return 1;
2676
2677failed:
2678
2679 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2680 svm->vmcb->control.exit_code_hi = 0;
2681 svm->vmcb->control.exit_info_1 = 0;
2682 svm->vmcb->control.exit_info_2 = 0;
2683
2684 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002685
2686 return 1;
2687}
2688
Avi Kivity851ba692009-08-24 11:10:17 +03002689static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002690{
2691 if (nested_svm_check_permissions(svm))
2692 return 1;
2693
2694 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2695 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002696 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002697
Joerg Roedel2af91942009-08-07 11:49:28 +02002698 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002699
2700 return 1;
2701}
2702
Avi Kivity851ba692009-08-24 11:10:17 +03002703static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002704{
2705 if (nested_svm_check_permissions(svm))
2706 return 1;
2707
2708 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2709 skip_emulated_instruction(&svm->vcpu);
2710
Joerg Roedel2af91942009-08-07 11:49:28 +02002711 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002712
2713 /* After a CLGI no interrupts should come */
2714 svm_clear_vintr(svm);
2715 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2716
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002717 mark_dirty(svm->vmcb, VMCB_INTR);
2718
Alexander Graf1371d902008-11-25 20:17:04 +01002719 return 1;
2720}
2721
Avi Kivity851ba692009-08-24 11:10:17 +03002722static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002723{
2724 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002725
Joerg Roedelec1ff792009-10-09 16:08:31 +02002726 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2727 vcpu->arch.regs[VCPU_REGS_RAX]);
2728
Alexander Grafff092382009-06-15 15:21:24 +02002729 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2730 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2731
2732 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2733 skip_emulated_instruction(&svm->vcpu);
2734 return 1;
2735}
2736
Joerg Roedel532a46b2009-10-09 16:08:32 +02002737static int skinit_interception(struct vcpu_svm *svm)
2738{
2739 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2740
2741 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2742 return 1;
2743}
2744
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002745static int xsetbv_interception(struct vcpu_svm *svm)
2746{
2747 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
2748 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
2749
2750 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
2751 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2752 skip_emulated_instruction(&svm->vcpu);
2753 }
2754
2755 return 1;
2756}
2757
Avi Kivity851ba692009-08-24 11:10:17 +03002758static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002760 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761 return 1;
2762}
2763
Avi Kivity851ba692009-08-24 11:10:17 +03002764static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765{
Izik Eidus37817f22008-03-24 23:14:53 +02002766 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002767 int reason;
2768 int int_type = svm->vmcb->control.exit_int_info &
2769 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002770 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002771 uint32_t type =
2772 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2773 uint32_t idt_v =
2774 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002775 bool has_error_code = false;
2776 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002777
2778 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002779
Izik Eidus37817f22008-03-24 23:14:53 +02002780 if (svm->vmcb->control.exit_info_2 &
2781 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002782 reason = TASK_SWITCH_IRET;
2783 else if (svm->vmcb->control.exit_info_2 &
2784 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2785 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002786 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002787 reason = TASK_SWITCH_GATE;
2788 else
2789 reason = TASK_SWITCH_CALL;
2790
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002791 if (reason == TASK_SWITCH_GATE) {
2792 switch (type) {
2793 case SVM_EXITINTINFO_TYPE_NMI:
2794 svm->vcpu.arch.nmi_injected = false;
2795 break;
2796 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002797 if (svm->vmcb->control.exit_info_2 &
2798 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2799 has_error_code = true;
2800 error_code =
2801 (u32)svm->vmcb->control.exit_info_2;
2802 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002803 kvm_clear_exception_queue(&svm->vcpu);
2804 break;
2805 case SVM_EXITINTINFO_TYPE_INTR:
2806 kvm_clear_interrupt_queue(&svm->vcpu);
2807 break;
2808 default:
2809 break;
2810 }
2811 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002812
Gleb Natapov8317c292009-04-12 13:37:02 +03002813 if (reason != TASK_SWITCH_GATE ||
2814 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2815 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002816 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2817 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002818
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002819 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2820 int_vec = -1;
2821
2822 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03002823 has_error_code, error_code) == EMULATE_FAIL) {
2824 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2825 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2826 svm->vcpu.run->internal.ndata = 0;
2827 return 0;
2828 }
2829 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830}
2831
Avi Kivity851ba692009-08-24 11:10:17 +03002832static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002834 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002835 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002836 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837}
2838
Avi Kivity851ba692009-08-24 11:10:17 +03002839static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002840{
2841 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002842 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002843 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02002844 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002845 return 1;
2846}
2847
Avi Kivity851ba692009-08-24 11:10:17 +03002848static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002849{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002850 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2851 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
2852
2853 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
2854 skip_emulated_instruction(&svm->vcpu);
2855 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002856}
2857
Avi Kivity851ba692009-08-24 11:10:17 +03002858static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859{
Andre Przywara51d8b662010-12-21 11:12:02 +01002860 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861}
2862
Avi Kivity332b56e2011-11-10 14:57:24 +02002863static int rdpmc_interception(struct vcpu_svm *svm)
2864{
2865 int err;
2866
2867 if (!static_cpu_has(X86_FEATURE_NRIPS))
2868 return emulate_on_interception(svm);
2869
2870 err = kvm_rdpmc(&svm->vcpu);
2871 kvm_complete_insn_gp(&svm->vcpu, err);
2872
2873 return 1;
2874}
2875
Joerg Roedel628afd22011-04-04 12:39:36 +02002876bool check_selective_cr0_intercepted(struct vcpu_svm *svm, unsigned long val)
2877{
2878 unsigned long cr0 = svm->vcpu.arch.cr0;
2879 bool ret = false;
2880 u64 intercept;
2881
2882 intercept = svm->nested.intercept;
2883
2884 if (!is_guest_mode(&svm->vcpu) ||
2885 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
2886 return false;
2887
2888 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2889 val &= ~SVM_CR0_SELECTIVE_MASK;
2890
2891 if (cr0 ^ val) {
2892 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2893 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2894 }
2895
2896 return ret;
2897}
2898
Andre Przywara7ff76d52010-12-21 11:12:04 +01002899#define CR_VALID (1ULL << 63)
2900
2901static int cr_interception(struct vcpu_svm *svm)
2902{
2903 int reg, cr;
2904 unsigned long val;
2905 int err;
2906
2907 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2908 return emulate_on_interception(svm);
2909
2910 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2911 return emulate_on_interception(svm);
2912
2913 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2914 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
2915
2916 err = 0;
2917 if (cr >= 16) { /* mov to cr */
2918 cr -= 16;
2919 val = kvm_register_read(&svm->vcpu, reg);
2920 switch (cr) {
2921 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02002922 if (!check_selective_cr0_intercepted(svm, val))
2923 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002924 else
2925 return 1;
2926
Andre Przywara7ff76d52010-12-21 11:12:04 +01002927 break;
2928 case 3:
2929 err = kvm_set_cr3(&svm->vcpu, val);
2930 break;
2931 case 4:
2932 err = kvm_set_cr4(&svm->vcpu, val);
2933 break;
2934 case 8:
2935 err = kvm_set_cr8(&svm->vcpu, val);
2936 break;
2937 default:
2938 WARN(1, "unhandled write to CR%d", cr);
2939 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2940 return 1;
2941 }
2942 } else { /* mov from cr */
2943 switch (cr) {
2944 case 0:
2945 val = kvm_read_cr0(&svm->vcpu);
2946 break;
2947 case 2:
2948 val = svm->vcpu.arch.cr2;
2949 break;
2950 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02002951 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002952 break;
2953 case 4:
2954 val = kvm_read_cr4(&svm->vcpu);
2955 break;
2956 case 8:
2957 val = kvm_get_cr8(&svm->vcpu);
2958 break;
2959 default:
2960 WARN(1, "unhandled read from CR%d", cr);
2961 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2962 return 1;
2963 }
2964 kvm_register_write(&svm->vcpu, reg, val);
2965 }
2966 kvm_complete_insn_gp(&svm->vcpu, err);
2967
2968 return 1;
2969}
2970
Andre Przywaracae37972010-12-21 11:12:05 +01002971static int dr_interception(struct vcpu_svm *svm)
2972{
2973 int reg, dr;
2974 unsigned long val;
2975 int err;
2976
2977 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
2978 return emulate_on_interception(svm);
2979
2980 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2981 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
2982
2983 if (dr >= 16) { /* mov to DRn */
2984 val = kvm_register_read(&svm->vcpu, reg);
2985 kvm_set_dr(&svm->vcpu, dr - 16, val);
2986 } else {
2987 err = kvm_get_dr(&svm->vcpu, dr, &val);
2988 if (!err)
2989 kvm_register_write(&svm->vcpu, reg, val);
2990 }
2991
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01002992 skip_emulated_instruction(&svm->vcpu);
2993
Andre Przywaracae37972010-12-21 11:12:05 +01002994 return 1;
2995}
2996
Avi Kivity851ba692009-08-24 11:10:17 +03002997static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002998{
Avi Kivity851ba692009-08-24 11:10:17 +03002999 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003000 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003001
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003002 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3003 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003004 r = cr_interception(svm);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003005 if (irqchip_in_kernel(svm->vcpu.kvm)) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003006 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003007 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003008 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003009 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003010 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003011 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3012 return 0;
3013}
3014
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003015u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu)
3016{
3017 struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
3018 return vmcb->control.tsc_offset +
3019 svm_scale_tsc(vcpu, native_read_tsc());
3020}
3021
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
3023{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003024 struct vcpu_svm *svm = to_svm(vcpu);
3025
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303027 case MSR_IA32_TSC: {
Nadav Har'El45133ec2011-08-02 15:55:23 +03003028 *data = svm->vmcb->control.tsc_offset +
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003029 svm_scale_tsc(vcpu, native_read_tsc());
3030
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031 break;
3032 }
Brian Gerst8c065852010-07-17 09:03:26 -04003033 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003034 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003036#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003037 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003038 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003039 break;
3040 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003041 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 break;
3043 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003044 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003045 break;
3046 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003047 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003048 break;
3049#endif
3050 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003051 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003052 break;
3053 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003054 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055 break;
3056 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003057 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003058 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003059 /*
3060 * Nobody will change the following 5 values in the VMCB so we can
3061 * safely return them on rdmsr. They will always be 0 until LBRV is
3062 * implemented.
3063 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003064 case MSR_IA32_DEBUGCTLMSR:
3065 *data = svm->vmcb->save.dbgctl;
3066 break;
3067 case MSR_IA32_LASTBRANCHFROMIP:
3068 *data = svm->vmcb->save.br_from;
3069 break;
3070 case MSR_IA32_LASTBRANCHTOIP:
3071 *data = svm->vmcb->save.br_to;
3072 break;
3073 case MSR_IA32_LASTINTFROMIP:
3074 *data = svm->vmcb->save.last_excp_from;
3075 break;
3076 case MSR_IA32_LASTINTTOIP:
3077 *data = svm->vmcb->save.last_excp_to;
3078 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003079 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003080 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003081 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003082 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003083 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003084 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003085 case MSR_IA32_UCODE_REV:
3086 *data = 0x01000065;
3087 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003088 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08003089 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003090 }
3091 return 0;
3092}
3093
Avi Kivity851ba692009-08-24 11:10:17 +03003094static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003096 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097 u64 data;
3098
Avi Kivity59200272010-01-25 19:47:02 +02003099 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
3100 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003101 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003102 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003103 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003104
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003105 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003106 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003107 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003108 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109 }
3110 return 1;
3111}
3112
Joerg Roedel4a810182010-02-24 18:59:15 +01003113static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3114{
3115 struct vcpu_svm *svm = to_svm(vcpu);
3116 int svm_dis, chg_mask;
3117
3118 if (data & ~SVM_VM_CR_VALID_MASK)
3119 return 1;
3120
3121 chg_mask = SVM_VM_CR_VALID_MASK;
3122
3123 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3124 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3125
3126 svm->nested.vm_cr_msr &= ~chg_mask;
3127 svm->nested.vm_cr_msr |= (data & chg_mask);
3128
3129 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3130
3131 /* check for svm_disable while efer.svme is set */
3132 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3133 return 1;
3134
3135 return 0;
3136}
3137
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
3139{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003140 struct vcpu_svm *svm = to_svm(vcpu);
3141
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003143 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10003144 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003146 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003147 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003149#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003151 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152 break;
3153 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003154 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155 break;
3156 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003157 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158 break;
3159 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003160 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161 break;
3162#endif
3163 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003164 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 break;
3166 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003167 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003168 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169 break;
3170 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003171 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003172 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003174 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003175 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003176 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003177 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003178 break;
3179 }
3180 if (data & DEBUGCTL_RESERVED_BITS)
3181 return 1;
3182
3183 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003184 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003185 if (data & (1ULL<<0))
3186 svm_enable_lbrv(svm);
3187 else
3188 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003189 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003190 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003191 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003192 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003193 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003194 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003195 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003196 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
3197 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003198 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08003199 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200 }
3201 return 0;
3202}
3203
Avi Kivity851ba692009-08-24 11:10:17 +03003204static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003206 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003207 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003208 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003209
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003210
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003211 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02003212 if (svm_set_msr(&svm->vcpu, ecx, data)) {
3213 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003214 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003215 } else {
3216 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10003217 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003218 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219 return 1;
3220}
3221
Avi Kivity851ba692009-08-24 11:10:17 +03003222static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223{
Rusty Russelle756fc62007-07-30 20:07:08 +10003224 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003225 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226 else
Avi Kivity851ba692009-08-24 11:10:17 +03003227 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228}
3229
Avi Kivity851ba692009-08-24 11:10:17 +03003230static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003231{
Avi Kivity851ba692009-08-24 11:10:17 +03003232 struct kvm_run *kvm_run = svm->vcpu.run;
3233
Avi Kivity3842d132010-07-27 12:30:24 +03003234 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003235 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003236 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003237 mark_dirty(svm->vmcb, VMCB_INTR);
Dor Laorc1150d82007-01-05 16:36:24 -08003238 /*
3239 * If the user space waits to inject interrupts, exit as soon as
3240 * possible
3241 */
Gleb Natapov80618232009-04-21 17:44:56 +03003242 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
3243 kvm_run->request_interrupt_window &&
3244 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10003245 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003246 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
3247 return 0;
3248 }
3249
3250 return 1;
3251}
3252
Mark Langsdorf565d0992009-10-06 14:25:02 -05003253static int pause_interception(struct vcpu_svm *svm)
3254{
3255 kvm_vcpu_on_spin(&(svm->vcpu));
3256 return 1;
3257}
3258
Avi Kivity851ba692009-08-24 11:10:17 +03003259static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003260 [SVM_EXIT_READ_CR0] = cr_interception,
3261 [SVM_EXIT_READ_CR3] = cr_interception,
3262 [SVM_EXIT_READ_CR4] = cr_interception,
3263 [SVM_EXIT_READ_CR8] = cr_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02003264 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003265 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003266 [SVM_EXIT_WRITE_CR3] = cr_interception,
3267 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003268 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003269 [SVM_EXIT_READ_DR0] = dr_interception,
3270 [SVM_EXIT_READ_DR1] = dr_interception,
3271 [SVM_EXIT_READ_DR2] = dr_interception,
3272 [SVM_EXIT_READ_DR3] = dr_interception,
3273 [SVM_EXIT_READ_DR4] = dr_interception,
3274 [SVM_EXIT_READ_DR5] = dr_interception,
3275 [SVM_EXIT_READ_DR6] = dr_interception,
3276 [SVM_EXIT_READ_DR7] = dr_interception,
3277 [SVM_EXIT_WRITE_DR0] = dr_interception,
3278 [SVM_EXIT_WRITE_DR1] = dr_interception,
3279 [SVM_EXIT_WRITE_DR2] = dr_interception,
3280 [SVM_EXIT_WRITE_DR3] = dr_interception,
3281 [SVM_EXIT_WRITE_DR4] = dr_interception,
3282 [SVM_EXIT_WRITE_DR5] = dr_interception,
3283 [SVM_EXIT_WRITE_DR6] = dr_interception,
3284 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003285 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3286 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003287 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003288 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3289 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
3290 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
3291 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003292 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293 [SVM_EXIT_SMI] = nop_on_interception,
3294 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003295 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02003296 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003298 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003299 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003300 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003301 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003302 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003303 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003304 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305 [SVM_EXIT_MSR] = msr_interception,
3306 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003307 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003308 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003309 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003310 [SVM_EXIT_VMLOAD] = vmload_interception,
3311 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003312 [SVM_EXIT_STGI] = stgi_interception,
3313 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003314 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003315 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01003316 [SVM_EXIT_MONITOR] = invalid_op_interception,
3317 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003318 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003319 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003320};
3321
Joe Perchesae8cc052011-04-24 22:00:50 -07003322static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003323{
3324 struct vcpu_svm *svm = to_svm(vcpu);
3325 struct vmcb_control_area *control = &svm->vmcb->control;
3326 struct vmcb_save_area *save = &svm->vmcb->save;
3327
3328 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003329 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
3330 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
3331 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
3332 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
3333 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
3334 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
3335 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3336 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3337 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3338 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3339 pr_err("%-20s%d\n", "asid:", control->asid);
3340 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3341 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3342 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3343 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3344 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3345 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3346 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3347 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3348 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3349 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3350 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
3351 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3352 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3353 pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
3354 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003355 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003356 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3357 "es:",
3358 save->es.selector, save->es.attrib,
3359 save->es.limit, save->es.base);
3360 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3361 "cs:",
3362 save->cs.selector, save->cs.attrib,
3363 save->cs.limit, save->cs.base);
3364 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3365 "ss:",
3366 save->ss.selector, save->ss.attrib,
3367 save->ss.limit, save->ss.base);
3368 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3369 "ds:",
3370 save->ds.selector, save->ds.attrib,
3371 save->ds.limit, save->ds.base);
3372 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3373 "fs:",
3374 save->fs.selector, save->fs.attrib,
3375 save->fs.limit, save->fs.base);
3376 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3377 "gs:",
3378 save->gs.selector, save->gs.attrib,
3379 save->gs.limit, save->gs.base);
3380 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3381 "gdtr:",
3382 save->gdtr.selector, save->gdtr.attrib,
3383 save->gdtr.limit, save->gdtr.base);
3384 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3385 "ldtr:",
3386 save->ldtr.selector, save->ldtr.attrib,
3387 save->ldtr.limit, save->ldtr.base);
3388 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3389 "idtr:",
3390 save->idtr.selector, save->idtr.attrib,
3391 save->idtr.limit, save->idtr.base);
3392 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3393 "tr:",
3394 save->tr.selector, save->tr.attrib,
3395 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003396 pr_err("cpl: %d efer: %016llx\n",
3397 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003398 pr_err("%-15s %016llx %-13s %016llx\n",
3399 "cr0:", save->cr0, "cr2:", save->cr2);
3400 pr_err("%-15s %016llx %-13s %016llx\n",
3401 "cr3:", save->cr3, "cr4:", save->cr4);
3402 pr_err("%-15s %016llx %-13s %016llx\n",
3403 "dr6:", save->dr6, "dr7:", save->dr7);
3404 pr_err("%-15s %016llx %-13s %016llx\n",
3405 "rip:", save->rip, "rflags:", save->rflags);
3406 pr_err("%-15s %016llx %-13s %016llx\n",
3407 "rsp:", save->rsp, "rax:", save->rax);
3408 pr_err("%-15s %016llx %-13s %016llx\n",
3409 "star:", save->star, "lstar:", save->lstar);
3410 pr_err("%-15s %016llx %-13s %016llx\n",
3411 "cstar:", save->cstar, "sfmask:", save->sfmask);
3412 pr_err("%-15s %016llx %-13s %016llx\n",
3413 "kernel_gs_base:", save->kernel_gs_base,
3414 "sysenter_cs:", save->sysenter_cs);
3415 pr_err("%-15s %016llx %-13s %016llx\n",
3416 "sysenter_esp:", save->sysenter_esp,
3417 "sysenter_eip:", save->sysenter_eip);
3418 pr_err("%-15s %016llx %-13s %016llx\n",
3419 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3420 pr_err("%-15s %016llx %-13s %016llx\n",
3421 "br_from:", save->br_from, "br_to:", save->br_to);
3422 pr_err("%-15s %016llx %-13s %016llx\n",
3423 "excp_from:", save->last_excp_from,
3424 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003425}
3426
Avi Kivity586f9602010-11-18 13:09:54 +02003427static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3428{
3429 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3430
3431 *info1 = control->exit_info_1;
3432 *info2 = control->exit_info_2;
3433}
3434
Avi Kivity851ba692009-08-24 11:10:17 +03003435static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003437 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003438 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003439 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003440
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003441 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003442 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3443 if (npt_enabled)
3444 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003445
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003446 if (unlikely(svm->nested.exit_required)) {
3447 nested_svm_vmexit(svm);
3448 svm->nested.exit_required = false;
3449
3450 return 1;
3451 }
3452
Joerg Roedel20307532010-11-29 17:51:48 +01003453 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003454 int vmexit;
3455
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003456 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3457 svm->vmcb->control.exit_info_1,
3458 svm->vmcb->control.exit_info_2,
3459 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003460 svm->vmcb->control.exit_int_info_err,
3461 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003462
Joerg Roedel410e4d52009-08-07 11:49:44 +02003463 vmexit = nested_svm_exit_special(svm);
3464
3465 if (vmexit == NESTED_EXIT_CONTINUE)
3466 vmexit = nested_svm_exit_handled(svm);
3467
3468 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003469 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003470 }
3471
Joerg Roedela5c38322009-08-07 11:49:32 +02003472 svm_complete_interrupts(svm);
3473
Avi Kivity04d2cc72007-09-10 18:10:54 +03003474 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3475 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3476 kvm_run->fail_entry.hardware_entry_failure_reason
3477 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003478 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3479 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003480 return 0;
3481 }
3482
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003483 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003484 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003485 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3486 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003487 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3488 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003489 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490 exit_code);
3491
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003492 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003493 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003494 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003495 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003496 return 0;
3497 }
3498
Avi Kivity851ba692009-08-24 11:10:17 +03003499 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003500}
3501
3502static void reload_tss(struct kvm_vcpu *vcpu)
3503{
3504 int cpu = raw_smp_processor_id();
3505
Tejun Heo0fe1e002009-10-29 22:34:14 +09003506 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3507 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003508 load_TR_desc();
3509}
3510
Rusty Russelle756fc62007-07-30 20:07:08 +10003511static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003512{
3513 int cpu = raw_smp_processor_id();
3514
Tejun Heo0fe1e002009-10-29 22:34:14 +09003515 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003517 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003518 if (svm->asid_generation != sd->asid_generation)
3519 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520}
3521
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003522static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3523{
3524 struct vcpu_svm *svm = to_svm(vcpu);
3525
3526 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3527 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003528 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003529 ++vcpu->stat.nmi_injections;
3530}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531
Eddie Dong85f455f2007-07-06 12:20:49 +03003532static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003533{
3534 struct vmcb_control_area *control;
3535
Rusty Russelle756fc62007-07-30 20:07:08 +10003536 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003537 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003538 control->int_ctl &= ~V_INTR_PRIO_MASK;
3539 control->int_ctl |= V_IRQ_MASK |
3540 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003541 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542}
3543
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003544static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003545{
3546 struct vcpu_svm *svm = to_svm(vcpu);
3547
Joerg Roedel2af91942009-08-07 11:49:28 +02003548 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003549
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003550 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3551 ++vcpu->stat.irq_injections;
3552
Alexander Graf219b65d2009-06-15 15:21:25 +02003553 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3554 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003555}
3556
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003557static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3558{
3559 struct vcpu_svm *svm = to_svm(vcpu);
3560
Joerg Roedel20307532010-11-29 17:51:48 +01003561 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003562 return;
3563
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003564 if (irr == -1)
3565 return;
3566
3567 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003568 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003569}
3570
3571static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003572{
3573 struct vcpu_svm *svm = to_svm(vcpu);
3574 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003575 int ret;
3576 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3577 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3578 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3579
3580 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003581}
3582
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003583static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3584{
3585 struct vcpu_svm *svm = to_svm(vcpu);
3586
3587 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3588}
3589
3590static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3591{
3592 struct vcpu_svm *svm = to_svm(vcpu);
3593
3594 if (masked) {
3595 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003596 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003597 } else {
3598 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003599 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003600 }
3601}
3602
Gleb Natapov78646122009-03-23 12:12:11 +02003603static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3604{
3605 struct vcpu_svm *svm = to_svm(vcpu);
3606 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003607 int ret;
3608
3609 if (!gif_set(svm) ||
3610 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3611 return 0;
3612
Avi Kivityf6e78472010-08-02 15:30:20 +03003613 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003614
Joerg Roedel20307532010-11-29 17:51:48 +01003615 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003616 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3617
3618 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003619}
3620
Gleb Natapov9222be12009-04-23 17:14:37 +03003621static void enable_irq_window(struct kvm_vcpu *vcpu)
3622{
Alexander Graf219b65d2009-06-15 15:21:25 +02003623 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003624
Joerg Roedele0231712010-02-24 18:59:10 +01003625 /*
3626 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3627 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3628 * get that intercept, this function will be called again though and
3629 * we'll get the vintr intercept.
3630 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003631 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003632 svm_set_vintr(svm);
3633 svm_inject_irq(svm, 0x0);
3634 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003635}
3636
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003637static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003638{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003639 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003640
Gleb Natapov44c11432009-05-11 13:35:52 +03003641 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3642 == HF_NMI_MASK)
3643 return; /* IRET will cause a vm exit */
3644
Joerg Roedele0231712010-02-24 18:59:10 +01003645 /*
3646 * Something prevents NMI from been injected. Single step over possible
3647 * problem (IRET or exception injection or interrupt shadow)
3648 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003649 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003650 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3651 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003652}
3653
Izik Eiduscbc94022007-10-25 00:29:55 +02003654static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3655{
3656 return 0;
3657}
3658
Avi Kivityd9e368d2007-06-07 19:18:30 +03003659static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3660{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003661 struct vcpu_svm *svm = to_svm(vcpu);
3662
3663 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3664 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3665 else
3666 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003667}
3668
Avi Kivity04d2cc72007-09-10 18:10:54 +03003669static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3670{
3671}
3672
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003673static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3674{
3675 struct vcpu_svm *svm = to_svm(vcpu);
3676
Joerg Roedel20307532010-11-29 17:51:48 +01003677 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003678 return;
3679
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003680 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003681 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003682 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003683 }
3684}
3685
Joerg Roedel649d6862008-04-16 16:51:15 +02003686static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3687{
3688 struct vcpu_svm *svm = to_svm(vcpu);
3689 u64 cr8;
3690
Joerg Roedel20307532010-11-29 17:51:48 +01003691 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003692 return;
3693
Joerg Roedel649d6862008-04-16 16:51:15 +02003694 cr8 = kvm_get_cr8(vcpu);
3695 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3696 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3697}
3698
Gleb Natapov9222be12009-04-23 17:14:37 +03003699static void svm_complete_interrupts(struct vcpu_svm *svm)
3700{
3701 u8 vector;
3702 int type;
3703 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003704 unsigned int3_injected = svm->int3_injected;
3705
3706 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003707
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003708 /*
3709 * If we've made progress since setting HF_IRET_MASK, we've
3710 * executed an IRET and can allow NMI injection.
3711 */
3712 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
3713 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003714 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003715 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3716 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003717
Gleb Natapov9222be12009-04-23 17:14:37 +03003718 svm->vcpu.arch.nmi_injected = false;
3719 kvm_clear_exception_queue(&svm->vcpu);
3720 kvm_clear_interrupt_queue(&svm->vcpu);
3721
3722 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3723 return;
3724
Avi Kivity3842d132010-07-27 12:30:24 +03003725 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3726
Gleb Natapov9222be12009-04-23 17:14:37 +03003727 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3728 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3729
3730 switch (type) {
3731 case SVM_EXITINTINFO_TYPE_NMI:
3732 svm->vcpu.arch.nmi_injected = true;
3733 break;
3734 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003735 /*
3736 * In case of software exceptions, do not reinject the vector,
3737 * but re-execute the instruction instead. Rewind RIP first
3738 * if we emulated INT3 before.
3739 */
3740 if (kvm_exception_is_soft(vector)) {
3741 if (vector == BP_VECTOR && int3_injected &&
3742 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3743 kvm_rip_write(&svm->vcpu,
3744 kvm_rip_read(&svm->vcpu) -
3745 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003746 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003747 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003748 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3749 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003750 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003751
3752 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003753 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003754 break;
3755 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003756 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003757 break;
3758 default:
3759 break;
3760 }
3761}
3762
Avi Kivityb463a6f2010-07-20 15:06:17 +03003763static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3764{
3765 struct vcpu_svm *svm = to_svm(vcpu);
3766 struct vmcb_control_area *control = &svm->vmcb->control;
3767
3768 control->exit_int_info = control->event_inj;
3769 control->exit_int_info_err = control->event_inj_err;
3770 control->event_inj = 0;
3771 svm_complete_interrupts(svm);
3772}
3773
Avi Kivity80e31d42008-07-14 14:44:59 +03003774#ifdef CONFIG_X86_64
3775#define R "r"
3776#else
3777#define R "e"
3778#endif
3779
Avi Kivity851ba692009-08-24 11:10:17 +03003780static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003781{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003782 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003783
Joerg Roedel2041a062010-04-22 12:33:08 +02003784 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3785 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3786 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3787
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003788 /*
3789 * A vmexit emulation is required before the vcpu can be executed
3790 * again.
3791 */
3792 if (unlikely(svm->nested.exit_required))
3793 return;
3794
Rusty Russelle756fc62007-07-30 20:07:08 +10003795 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796
Joerg Roedel649d6862008-04-16 16:51:15 +02003797 sync_lapic_to_cr8(vcpu);
3798
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003799 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003800
Avi Kivity04d2cc72007-09-10 18:10:54 +03003801 clgi();
3802
3803 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003804
Avi Kivity6aa8b732006-12-10 02:21:36 -08003805 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003806 "push %%"R"bp; \n\t"
3807 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3808 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3809 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3810 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3811 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3812 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003813#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003814 "mov %c[r8](%[svm]), %%r8 \n\t"
3815 "mov %c[r9](%[svm]), %%r9 \n\t"
3816 "mov %c[r10](%[svm]), %%r10 \n\t"
3817 "mov %c[r11](%[svm]), %%r11 \n\t"
3818 "mov %c[r12](%[svm]), %%r12 \n\t"
3819 "mov %c[r13](%[svm]), %%r13 \n\t"
3820 "mov %c[r14](%[svm]), %%r14 \n\t"
3821 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003822#endif
3823
Avi Kivity6aa8b732006-12-10 02:21:36 -08003824 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003825 "push %%"R"ax \n\t"
3826 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003827 __ex(SVM_VMLOAD) "\n\t"
3828 __ex(SVM_VMRUN) "\n\t"
3829 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003830 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831
3832 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003833 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3834 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3835 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3836 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3837 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3838 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003839#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003840 "mov %%r8, %c[r8](%[svm]) \n\t"
3841 "mov %%r9, %c[r9](%[svm]) \n\t"
3842 "mov %%r10, %c[r10](%[svm]) \n\t"
3843 "mov %%r11, %c[r11](%[svm]) \n\t"
3844 "mov %%r12, %c[r12](%[svm]) \n\t"
3845 "mov %%r13, %c[r13](%[svm]) \n\t"
3846 "mov %%r14, %c[r14](%[svm]) \n\t"
3847 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003848#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003849 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003851 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003852 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003853 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3854 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3855 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3856 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3857 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3858 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003859#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003860 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3861 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3862 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3863 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3864 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3865 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3866 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3867 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003868#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003869 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003870 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003871#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003872 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3873#endif
3874 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003875
Avi Kivity82ca2d12010-10-21 12:20:34 +02003876#ifdef CONFIG_X86_64
3877 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3878#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003879 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02003880#ifndef CONFIG_X86_32_LAZY_GS
3881 loadsegment(gs, svm->host.gs);
3882#endif
Avi Kivity9581d442010-10-19 16:46:55 +02003883#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003884
3885 reload_tss(vcpu);
3886
Avi Kivity56ba47d2007-11-07 17:14:18 +02003887 local_irq_disable();
3888
Avi Kivity13c34e02010-10-21 12:20:31 +02003889 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3890 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3891 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3892 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3893
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02003894 trace_kvm_exit(svm->vmcb->control.exit_code, vcpu, KVM_ISA_SVM);
3895
Joerg Roedel3781c012011-01-14 16:45:02 +01003896 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3897 kvm_before_handle_nmi(&svm->vcpu);
3898
3899 stgi();
3900
3901 /* Any pending NMI will happen here */
3902
3903 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3904 kvm_after_handle_nmi(&svm->vcpu);
3905
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003906 sync_cr8_to_lapic(vcpu);
3907
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003908 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003909
Joerg Roedel38e5e922010-12-03 15:25:16 +01003910 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3911
Gleb Natapov631bc482010-10-14 11:22:52 +02003912 /* if exit due to PF check for async PF */
3913 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3914 svm->apf_reason = kvm_read_and_reset_pf_reason();
3915
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003916 if (npt_enabled) {
3917 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3918 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3919 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003920
3921 /*
3922 * We need to handle MC intercepts here before the vcpu has a chance to
3923 * change the physical cpu
3924 */
3925 if (unlikely(svm->vmcb->control.exit_code ==
3926 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3927 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003928
3929 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930}
3931
Avi Kivity80e31d42008-07-14 14:44:59 +03003932#undef R
3933
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3935{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003936 struct vcpu_svm *svm = to_svm(vcpu);
3937
3938 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003939 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003940 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003941}
3942
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003943static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3944{
3945 struct vcpu_svm *svm = to_svm(vcpu);
3946
3947 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01003948 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003949
3950 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02003951 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003952 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003953
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003954 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003955}
3956
Avi Kivity6aa8b732006-12-10 02:21:36 -08003957static int is_disabled(void)
3958{
Joerg Roedel6031a612007-06-22 12:29:50 +03003959 u64 vm_cr;
3960
3961 rdmsrl(MSR_VM_CR, vm_cr);
3962 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3963 return 1;
3964
Avi Kivity6aa8b732006-12-10 02:21:36 -08003965 return 0;
3966}
3967
Ingo Molnar102d8322007-02-19 14:37:47 +02003968static void
3969svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3970{
3971 /*
3972 * Patch in the VMMCALL instruction:
3973 */
3974 hypercall[0] = 0x0f;
3975 hypercall[1] = 0x01;
3976 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003977}
3978
Yang, Sheng002c7f72007-07-31 14:23:01 +03003979static void svm_check_processor_compat(void *rtn)
3980{
3981 *(int *)rtn = 0;
3982}
3983
Avi Kivity774ead32007-12-26 13:57:04 +02003984static bool svm_cpu_has_accelerated_tpr(void)
3985{
3986 return false;
3987}
3988
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003989static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003990{
3991 return 0;
3992}
3993
Sheng Yang0e851882009-12-18 16:48:46 +08003994static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3995{
3996}
3997
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003998static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3999{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004000 switch (func) {
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02004001 case 0x80000001:
4002 if (nested)
4003 entry->ecx |= (1 << 2); /* Set SVM bit */
4004 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004005 case 0x8000000A:
4006 entry->eax = 1; /* SVM revision 1 */
4007 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
4008 ASID emulation to nested SVM */
4009 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02004010 entry->edx = 0; /* Per default do not support any
4011 additional features */
4012
4013 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004014 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02004015 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004016
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02004017 /* Support NPT for the guest if enabled */
4018 if (npt_enabled)
4019 entry->edx |= SVM_FEATURE_NPT;
4020
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004021 break;
4022 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004023}
4024
Sheng Yang17cc3932010-01-05 19:02:27 +08004025static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004026{
Sheng Yang17cc3932010-01-05 19:02:27 +08004027 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004028}
4029
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004030static bool svm_rdtscp_supported(void)
4031{
4032 return false;
4033}
4034
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004035static bool svm_has_wbinvd_exit(void)
4036{
4037 return true;
4038}
4039
Avi Kivity02daab22009-12-30 12:40:26 +02004040static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
4041{
4042 struct vcpu_svm *svm = to_svm(vcpu);
4043
Joerg Roedel18c918c2010-11-30 18:03:59 +01004044 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01004045 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02004046}
4047
Joerg Roedel80612522011-04-04 12:39:33 +02004048#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004049 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004050#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004051 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004052#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004053 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004054
4055static struct __x86_intercept {
4056 u32 exit_code;
4057 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004058} x86_intercept_map[] = {
4059 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4060 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4061 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4062 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4063 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004064 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4065 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004066 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4067 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4068 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4069 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4070 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4071 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4072 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4073 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004074 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4075 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4076 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4077 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4078 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4079 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4080 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4081 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004082 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4083 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4084 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004085 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4086 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4087 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4088 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4089 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4090 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4091 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4092 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4093 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004094 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4095 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4096 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4097 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4098 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4099 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4100 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004101 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4102 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4103 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4104 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004105};
4106
Joerg Roedel80612522011-04-04 12:39:33 +02004107#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004108#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004109#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004110
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004111static int svm_check_intercept(struct kvm_vcpu *vcpu,
4112 struct x86_instruction_info *info,
4113 enum x86_intercept_stage stage)
4114{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004115 struct vcpu_svm *svm = to_svm(vcpu);
4116 int vmexit, ret = X86EMUL_CONTINUE;
4117 struct __x86_intercept icpt_info;
4118 struct vmcb *vmcb = svm->vmcb;
4119
4120 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4121 goto out;
4122
4123 icpt_info = x86_intercept_map[info->intercept];
4124
Avi Kivity40e19b52011-04-21 12:35:41 +03004125 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004126 goto out;
4127
4128 switch (icpt_info.exit_code) {
4129 case SVM_EXIT_READ_CR0:
4130 if (info->intercept == x86_intercept_cr_read)
4131 icpt_info.exit_code += info->modrm_reg;
4132 break;
4133 case SVM_EXIT_WRITE_CR0: {
4134 unsigned long cr0, val;
4135 u64 intercept;
4136
4137 if (info->intercept == x86_intercept_cr_write)
4138 icpt_info.exit_code += info->modrm_reg;
4139
4140 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0)
4141 break;
4142
4143 intercept = svm->nested.intercept;
4144
4145 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
4146 break;
4147
4148 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4149 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4150
4151 if (info->intercept == x86_intercept_lmsw) {
4152 cr0 &= 0xfUL;
4153 val &= 0xfUL;
4154 /* lmsw can't clear PE - catch this here */
4155 if (cr0 & X86_CR0_PE)
4156 val |= X86_CR0_PE;
4157 }
4158
4159 if (cr0 ^ val)
4160 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4161
4162 break;
4163 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004164 case SVM_EXIT_READ_DR0:
4165 case SVM_EXIT_WRITE_DR0:
4166 icpt_info.exit_code += info->modrm_reg;
4167 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004168 case SVM_EXIT_MSR:
4169 if (info->intercept == x86_intercept_wrmsr)
4170 vmcb->control.exit_info_1 = 1;
4171 else
4172 vmcb->control.exit_info_1 = 0;
4173 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004174 case SVM_EXIT_PAUSE:
4175 /*
4176 * We get this for NOP only, but pause
4177 * is rep not, check this here
4178 */
4179 if (info->rep_prefix != REPE_PREFIX)
4180 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02004181 case SVM_EXIT_IOIO: {
4182 u64 exit_info;
4183 u32 bytes;
4184
4185 exit_info = (vcpu->arch.regs[VCPU_REGS_RDX] & 0xffff) << 16;
4186
4187 if (info->intercept == x86_intercept_in ||
4188 info->intercept == x86_intercept_ins) {
4189 exit_info |= SVM_IOIO_TYPE_MASK;
4190 bytes = info->src_bytes;
4191 } else {
4192 bytes = info->dst_bytes;
4193 }
4194
4195 if (info->intercept == x86_intercept_outs ||
4196 info->intercept == x86_intercept_ins)
4197 exit_info |= SVM_IOIO_STR_MASK;
4198
4199 if (info->rep_prefix)
4200 exit_info |= SVM_IOIO_REP_MASK;
4201
4202 bytes = min(bytes, 4u);
4203
4204 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4205
4206 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4207
4208 vmcb->control.exit_info_1 = exit_info;
4209 vmcb->control.exit_info_2 = info->next_rip;
4210
4211 break;
4212 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004213 default:
4214 break;
4215 }
4216
4217 vmcb->control.next_rip = info->next_rip;
4218 vmcb->control.exit_code = icpt_info.exit_code;
4219 vmexit = nested_svm_exit_handled(svm);
4220
4221 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4222 : X86EMUL_CONTINUE;
4223
4224out:
4225 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004226}
4227
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004228static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004229 .cpu_has_kvm_support = has_svm,
4230 .disabled_by_bios = is_disabled,
4231 .hardware_setup = svm_hardware_setup,
4232 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004233 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004234 .hardware_enable = svm_hardware_enable,
4235 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004236 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237
4238 .vcpu_create = svm_create_vcpu,
4239 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004240 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004241
Avi Kivity04d2cc72007-09-10 18:10:54 +03004242 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004243 .vcpu_load = svm_vcpu_load,
4244 .vcpu_put = svm_vcpu_put,
4245
4246 .set_guest_debug = svm_guest_debug,
4247 .get_msr = svm_get_msr,
4248 .set_msr = svm_set_msr,
4249 .get_segment_base = svm_get_segment_base,
4250 .get_segment = svm_get_segment,
4251 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004252 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004253 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004254 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004255 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004256 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004257 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004258 .set_cr3 = svm_set_cr3,
4259 .set_cr4 = svm_set_cr4,
4260 .set_efer = svm_set_efer,
4261 .get_idt = svm_get_idt,
4262 .set_idt = svm_set_idt,
4263 .get_gdt = svm_get_gdt,
4264 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004265 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004266 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004267 .get_rflags = svm_get_rflags,
4268 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02004269 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004270 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004271
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004273
Avi Kivity6aa8b732006-12-10 02:21:36 -08004274 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004275 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004276 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004277 .set_interrupt_shadow = svm_set_interrupt_shadow,
4278 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004279 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004280 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004281 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004282 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004283 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004284 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004285 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004286 .get_nmi_mask = svm_get_nmi_mask,
4287 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004288 .enable_nmi_window = enable_nmi_window,
4289 .enable_irq_window = enable_irq_window,
4290 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02004291
4292 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004293 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004294 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004295
Avi Kivity586f9602010-11-18 13:09:54 +02004296 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004297
Sheng Yang17cc3932010-01-05 19:02:27 +08004298 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004299
4300 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004301
4302 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004303
4304 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004305
4306 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004307
Joerg Roedel4051b182011-03-25 09:44:49 +01004308 .set_tsc_khz = svm_set_tsc_khz,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004309 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004310 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01004311 .compute_tsc_offset = svm_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03004312 .read_l1_tsc = svm_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004313
4314 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004315
4316 .check_intercept = svm_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004317};
4318
4319static int __init svm_init(void)
4320{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004321 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004322 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004323}
4324
4325static void __exit svm_exit(void)
4326{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004327 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004328}
4329
4330module_init(svm_init)
4331module_exit(svm_exit)