blob: e12026e5244ee217dd603778eeb38ef229ba4275 [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
Zachary Amsdene48672f2010-08-19 22:07:23 -10001019static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
1020{
1021 struct vcpu_svm *svm = to_svm(vcpu);
1022
1023 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +01001024 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -10001025 svm->nested.hsave->control.tsc_offset += adjustment;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001026 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001027}
1028
Joerg Roedel857e4092011-03-25 09:44:50 +01001029static u64 svm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1030{
1031 u64 tsc;
1032
1033 tsc = svm_scale_tsc(vcpu, native_read_tsc());
1034
1035 return target_tsc - tsc;
1036}
1037
Joerg Roedele6101a92008-02-13 18:58:45 +01001038static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039{
Joerg Roedele6101a92008-02-13 18:58:45 +01001040 struct vmcb_control_area *control = &svm->vmcb->control;
1041 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042
Avi Kivitybff78272010-01-07 13:16:08 +02001043 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001044 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001045
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001046 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1047 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1048 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1049 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1050 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1051 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
1052 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053
Joerg Roedel3aed0412010-11-30 18:03:58 +01001054 set_dr_intercept(svm, INTERCEPT_DR0_READ);
1055 set_dr_intercept(svm, INTERCEPT_DR1_READ);
1056 set_dr_intercept(svm, INTERCEPT_DR2_READ);
1057 set_dr_intercept(svm, INTERCEPT_DR3_READ);
1058 set_dr_intercept(svm, INTERCEPT_DR4_READ);
1059 set_dr_intercept(svm, INTERCEPT_DR5_READ);
1060 set_dr_intercept(svm, INTERCEPT_DR6_READ);
1061 set_dr_intercept(svm, INTERCEPT_DR7_READ);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062
Joerg Roedel3aed0412010-11-30 18:03:58 +01001063 set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
1064 set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
1065 set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
1066 set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
1067 set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
1068 set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
1069 set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
1070 set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071
Joerg Roedel18c918c2010-11-30 18:03:59 +01001072 set_exception_intercept(svm, PF_VECTOR);
1073 set_exception_intercept(svm, UD_VECTOR);
1074 set_exception_intercept(svm, MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001076 set_intercept(svm, INTERCEPT_INTR);
1077 set_intercept(svm, INTERCEPT_NMI);
1078 set_intercept(svm, INTERCEPT_SMI);
1079 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001080 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001081 set_intercept(svm, INTERCEPT_CPUID);
1082 set_intercept(svm, INTERCEPT_INVD);
1083 set_intercept(svm, INTERCEPT_HLT);
1084 set_intercept(svm, INTERCEPT_INVLPG);
1085 set_intercept(svm, INTERCEPT_INVLPGA);
1086 set_intercept(svm, INTERCEPT_IOIO_PROT);
1087 set_intercept(svm, INTERCEPT_MSR_PROT);
1088 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1089 set_intercept(svm, INTERCEPT_SHUTDOWN);
1090 set_intercept(svm, INTERCEPT_VMRUN);
1091 set_intercept(svm, INTERCEPT_VMMCALL);
1092 set_intercept(svm, INTERCEPT_VMLOAD);
1093 set_intercept(svm, INTERCEPT_VMSAVE);
1094 set_intercept(svm, INTERCEPT_STGI);
1095 set_intercept(svm, INTERCEPT_CLGI);
1096 set_intercept(svm, INTERCEPT_SKINIT);
1097 set_intercept(svm, INTERCEPT_WBINVD);
1098 set_intercept(svm, INTERCEPT_MONITOR);
1099 set_intercept(svm, INTERCEPT_MWAIT);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001100 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101
1102 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001103 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 control->int_ctl = V_INTR_MASKING_MASK;
1105
1106 init_seg(&save->es);
1107 init_seg(&save->ss);
1108 init_seg(&save->ds);
1109 init_seg(&save->fs);
1110 init_seg(&save->gs);
1111
1112 save->cs.selector = 0xf000;
1113 /* Executable/Readable Code Segment */
1114 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1115 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1116 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -08001117 /*
1118 * cs.base should really be 0xffff0000, but vmx can't handle that, so
1119 * be consistent with it.
1120 *
1121 * Replace when we have real mode working for vmx.
1122 */
1123 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124
1125 save->gdtr.limit = 0xffff;
1126 save->idtr.limit = 0xffff;
1127
1128 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1129 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1130
Marcelo Tosattieaa48512010-08-31 19:13:14 -03001131 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001132 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133 save->dr7 = 0x400;
Avi Kivityf6e78472010-08-02 15:30:20 +03001134 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001136 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137
Joerg Roedele0231712010-02-24 18:59:10 +01001138 /*
1139 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001140 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -03001142 svm->vcpu.arch.cr0 = 0;
1143 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001144
Rusty Russell66aee912007-07-17 23:34:16 +10001145 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001147
1148 if (npt_enabled) {
1149 /* Setup VMCB for Nested Paging */
1150 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001151 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001152 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001153 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1154 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001155 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001156 save->cr3 = 0;
1157 save->cr4 = 0;
1158 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001159 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001160
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001161 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001162 svm->vcpu.arch.hflags = 0;
1163
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001164 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001165 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001166 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001167 }
1168
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001169 mark_all_dirty(svm->vmcb);
1170
Joerg Roedel2af91942009-08-07 11:49:28 +02001171 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172}
1173
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001174static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001175{
1176 struct vcpu_svm *svm = to_svm(vcpu);
1177
Joerg Roedele6101a92008-02-13 18:58:45 +01001178 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001179
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001180 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001181 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001182 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
1183 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +02001184 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001185 vcpu->arch.regs_avail = ~0;
1186 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001187
1188 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001189}
1190
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001191static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001193 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001195 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001196 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001197 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001198 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199
Rusty Russellc16f8622007-07-30 21:12:19 +10001200 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001201 if (!svm) {
1202 err = -ENOMEM;
1203 goto out;
1204 }
1205
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001206 svm->tsc_ratio = TSC_RATIO_DEFAULT;
1207
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001208 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1209 if (err)
1210 goto free_svm;
1211
Joerg Roedelf65c2292008-02-13 18:58:46 +01001212 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001213 page = alloc_page(GFP_KERNEL);
1214 if (!page)
1215 goto uninit;
1216
Joerg Roedelf65c2292008-02-13 18:58:46 +01001217 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1218 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001219 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001220
1221 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1222 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001223 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001224
Alexander Grafb286d5d2008-11-25 20:17:05 +01001225 hsave_page = alloc_page(GFP_KERNEL);
1226 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001227 goto free_page3;
1228
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001229 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001230
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001231 svm->msrpm = page_address(msrpm_pages);
1232 svm_vcpu_init_msrpm(svm->msrpm);
1233
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001234 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001235 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001236
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001237 svm->vmcb = page_address(page);
1238 clear_page(svm->vmcb);
1239 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1240 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +01001241 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001242 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001243
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001244 err = fx_init(&svm->vcpu);
1245 if (err)
1246 goto free_page4;
1247
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001248 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001249 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001250 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001252 svm_init_osvw(&svm->vcpu);
1253
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001254 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001255
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001256free_page4:
1257 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001258free_page3:
1259 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1260free_page2:
1261 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1262free_page1:
1263 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001264uninit:
1265 kvm_vcpu_uninit(&svm->vcpu);
1266free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001267 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001268out:
1269 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270}
1271
1272static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1273{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001274 struct vcpu_svm *svm = to_svm(vcpu);
1275
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001276 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001277 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001278 __free_page(virt_to_page(svm->nested.hsave));
1279 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001280 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001281 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282}
1283
Avi Kivity15ad7142007-07-11 18:17:21 +03001284static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001286 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001287 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001288
Avi Kivity0cc50642007-03-25 12:07:27 +02001289 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001290 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001291 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001292 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001293
Avi Kivity82ca2d12010-10-21 12:20:34 +02001294#ifdef CONFIG_X86_64
1295 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1296#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001297 savesegment(fs, svm->host.fs);
1298 savesegment(gs, svm->host.gs);
1299 svm->host.ldt = kvm_read_ldt();
1300
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001301 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001302 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001303
1304 if (static_cpu_has(X86_FEATURE_TSCRATEMSR) &&
1305 svm->tsc_ratio != __get_cpu_var(current_tsc_ratio)) {
1306 __get_cpu_var(current_tsc_ratio) = svm->tsc_ratio;
1307 wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_ratio);
1308 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309}
1310
1311static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1312{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001313 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001314 int i;
1315
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001316 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001317 kvm_load_ldt(svm->host.ldt);
1318#ifdef CONFIG_X86_64
1319 loadsegment(fs, svm->host.fs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001320 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001321 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001322#else
Avi Kivity831ca602011-03-08 16:09:51 +02001323#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001324 loadsegment(gs, svm->host.gs);
1325#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001326#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001327 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001328 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329}
1330
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1332{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001333 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001334}
1335
1336static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1337{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001338 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339}
1340
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001341static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1342{
1343 switch (reg) {
1344 case VCPU_EXREG_PDPTR:
1345 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001346 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001347 break;
1348 default:
1349 BUG();
1350 }
1351}
1352
Alexander Graff0b85052008-11-25 20:17:01 +01001353static void svm_set_vintr(struct vcpu_svm *svm)
1354{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001355 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001356}
1357
1358static void svm_clear_vintr(struct vcpu_svm *svm)
1359{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001360 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001361}
1362
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1364{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001365 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366
1367 switch (seg) {
1368 case VCPU_SREG_CS: return &save->cs;
1369 case VCPU_SREG_DS: return &save->ds;
1370 case VCPU_SREG_ES: return &save->es;
1371 case VCPU_SREG_FS: return &save->fs;
1372 case VCPU_SREG_GS: return &save->gs;
1373 case VCPU_SREG_SS: return &save->ss;
1374 case VCPU_SREG_TR: return &save->tr;
1375 case VCPU_SREG_LDTR: return &save->ldtr;
1376 }
1377 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001378 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379}
1380
1381static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1382{
1383 struct vmcb_seg *s = svm_seg(vcpu, seg);
1384
1385 return s->base;
1386}
1387
1388static void svm_get_segment(struct kvm_vcpu *vcpu,
1389 struct kvm_segment *var, int seg)
1390{
1391 struct vmcb_seg *s = svm_seg(vcpu, seg);
1392
1393 var->base = s->base;
1394 var->limit = s->limit;
1395 var->selector = s->selector;
1396 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1397 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1398 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1399 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1400 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1401 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1402 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1403 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001404
Joerg Roedele0231712010-02-24 18:59:10 +01001405 /*
1406 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001407 * for cross vendor migration purposes by "not present"
1408 */
1409 var->unusable = !var->present || (var->type == 0);
1410
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001411 switch (seg) {
1412 case VCPU_SREG_CS:
1413 /*
1414 * SVM always stores 0 for the 'G' bit in the CS selector in
1415 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1416 * Intel's VMENTRY has a check on the 'G' bit.
1417 */
Amit Shah25022ac2008-10-27 09:04:17 +00001418 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001419 break;
1420 case VCPU_SREG_TR:
1421 /*
1422 * Work around a bug where the busy flag in the tr selector
1423 * isn't exposed
1424 */
Amit Shahc0d09822008-10-27 09:04:18 +00001425 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001426 break;
1427 case VCPU_SREG_DS:
1428 case VCPU_SREG_ES:
1429 case VCPU_SREG_FS:
1430 case VCPU_SREG_GS:
1431 /*
1432 * The accessed bit must always be set in the segment
1433 * descriptor cache, although it can be cleared in the
1434 * descriptor, the cached bit always remains at 1. Since
1435 * Intel has a check on this, set it here to support
1436 * cross-vendor migration.
1437 */
1438 if (!var->unusable)
1439 var->type |= 0x1;
1440 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001441 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001442 /*
1443 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001444 * descriptor is left as 1, although the whole segment has
1445 * been made unusable. Clear it here to pass an Intel VMX
1446 * entry check when cross vendor migrating.
1447 */
1448 if (var->unusable)
1449 var->db = 0;
1450 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001451 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452}
1453
Izik Eidus2e4d2652008-03-24 19:38:34 +02001454static int svm_get_cpl(struct kvm_vcpu *vcpu)
1455{
1456 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1457
1458 return save->cpl;
1459}
1460
Gleb Natapov89a27f42010-02-16 10:51:48 +02001461static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001463 struct vcpu_svm *svm = to_svm(vcpu);
1464
Gleb Natapov89a27f42010-02-16 10:51:48 +02001465 dt->size = svm->vmcb->save.idtr.limit;
1466 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467}
1468
Gleb Natapov89a27f42010-02-16 10:51:48 +02001469static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001471 struct vcpu_svm *svm = to_svm(vcpu);
1472
Gleb Natapov89a27f42010-02-16 10:51:48 +02001473 svm->vmcb->save.idtr.limit = dt->size;
1474 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001475 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476}
1477
Gleb Natapov89a27f42010-02-16 10:51:48 +02001478static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001480 struct vcpu_svm *svm = to_svm(vcpu);
1481
Gleb Natapov89a27f42010-02-16 10:51:48 +02001482 dt->size = svm->vmcb->save.gdtr.limit;
1483 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484}
1485
Gleb Natapov89a27f42010-02-16 10:51:48 +02001486static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001488 struct vcpu_svm *svm = to_svm(vcpu);
1489
Gleb Natapov89a27f42010-02-16 10:51:48 +02001490 svm->vmcb->save.gdtr.limit = dt->size;
1491 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001492 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493}
1494
Avi Kivitye8467fd2009-12-29 18:43:06 +02001495static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1496{
1497}
1498
Avi Kivityaff48ba2010-12-05 18:56:11 +02001499static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1500{
1501}
1502
Anthony Liguori25c4c272007-04-27 09:29:21 +03001503static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001504{
1505}
1506
Avi Kivityd2251572010-01-06 10:55:27 +02001507static void update_cr0_intercept(struct vcpu_svm *svm)
1508{
1509 ulong gcr0 = svm->vcpu.arch.cr0;
1510 u64 *hcr0 = &svm->vmcb->save.cr0;
1511
1512 if (!svm->vcpu.fpu_active)
1513 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1514 else
1515 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1516 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1517
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001518 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001519
1520 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001521 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1522 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001523 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001524 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1525 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001526 }
1527}
1528
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1530{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001531 struct vcpu_svm *svm = to_svm(vcpu);
1532
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001533#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001534 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001535 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001536 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001537 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538 }
1539
Mike Dayd77c26f2007-10-08 09:02:08 -04001540 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001541 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001542 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001543 }
1544 }
1545#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001546 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001547
1548 if (!npt_enabled)
1549 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001550
1551 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001552 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001553 /*
1554 * re-enable caching here because the QEMU bios
1555 * does not do it - this results in some delay at
1556 * reboot
1557 */
1558 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001559 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001560 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001561 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001562}
1563
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001564static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565{
Joerg Roedel6394b642008-04-09 14:15:29 +02001566 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001567 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1568
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001569 if (cr4 & X86_CR4_VMXE)
1570 return 1;
1571
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001572 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001573 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001574
Joerg Roedelec077262008-04-09 14:15:28 +02001575 vcpu->arch.cr4 = cr4;
1576 if (!npt_enabled)
1577 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001578 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001579 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001580 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001581 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001582}
1583
1584static void svm_set_segment(struct kvm_vcpu *vcpu,
1585 struct kvm_segment *var, int seg)
1586{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001587 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588 struct vmcb_seg *s = svm_seg(vcpu, seg);
1589
1590 s->base = var->base;
1591 s->limit = var->limit;
1592 s->selector = var->selector;
1593 if (var->unusable)
1594 s->attrib = 0;
1595 else {
1596 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1597 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1598 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1599 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1600 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1601 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1602 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1603 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1604 }
1605 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001606 svm->vmcb->save.cpl
1607 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1609
Joerg Roedel060d0c92010-12-03 11:45:57 +01001610 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611}
1612
Gleb Natapov44c11432009-05-11 13:35:52 +03001613static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001615 struct vcpu_svm *svm = to_svm(vcpu);
1616
Joerg Roedel18c918c2010-11-30 18:03:59 +01001617 clr_exception_intercept(svm, DB_VECTOR);
1618 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001619
Jan Kiszka6be7d302009-10-18 13:24:54 +02001620 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001621 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001622
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001623 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1624 if (vcpu->guest_debug &
1625 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001626 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001627 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001628 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001629 } else
1630 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001631}
1632
Jan Kiszka355be0b2009-10-03 00:31:21 +02001633static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001634{
Gleb Natapov44c11432009-05-11 13:35:52 +03001635 struct vcpu_svm *svm = to_svm(vcpu);
1636
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001637 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1638 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1639 else
1640 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1641
Joerg Roedel72214b92010-12-03 11:45:55 +01001642 mark_dirty(svm->vmcb, VMCB_DR);
1643
Jan Kiszka355be0b2009-10-03 00:31:21 +02001644 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645}
1646
Tejun Heo0fe1e002009-10-29 22:34:14 +09001647static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001649 if (sd->next_asid > sd->max_asid) {
1650 ++sd->asid_generation;
1651 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001652 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653 }
1654
Tejun Heo0fe1e002009-10-29 22:34:14 +09001655 svm->asid_generation = sd->asid_generation;
1656 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001657
1658 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659}
1660
Gleb Natapov020df072010-04-13 10:05:23 +03001661static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001662{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001663 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001664
Gleb Natapov020df072010-04-13 10:05:23 +03001665 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01001666 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667}
1668
Avi Kivity851ba692009-08-24 11:10:17 +03001669static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001670{
Gleb Natapov631bc482010-10-14 11:22:52 +02001671 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001673 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674
Gleb Natapov631bc482010-10-14 11:22:52 +02001675 switch (svm->apf_reason) {
1676 default:
1677 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001678
Gleb Natapov631bc482010-10-14 11:22:52 +02001679 trace_kvm_page_fault(fault_address, error_code);
1680 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1681 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01001682 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1683 svm->vmcb->control.insn_bytes,
1684 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02001685 break;
1686 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1687 svm->apf_reason = 0;
1688 local_irq_disable();
1689 kvm_async_pf_task_wait(fault_address);
1690 local_irq_enable();
1691 break;
1692 case KVM_PV_REASON_PAGE_READY:
1693 svm->apf_reason = 0;
1694 local_irq_disable();
1695 kvm_async_pf_task_wake(fault_address);
1696 local_irq_enable();
1697 break;
1698 }
1699 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700}
1701
Avi Kivity851ba692009-08-24 11:10:17 +03001702static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001703{
Avi Kivity851ba692009-08-24 11:10:17 +03001704 struct kvm_run *kvm_run = svm->vcpu.run;
1705
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001706 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001707 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001708 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001709 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1710 return 1;
1711 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001712
Jan Kiszka6be7d302009-10-18 13:24:54 +02001713 if (svm->nmi_singlestep) {
1714 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001715 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1716 svm->vmcb->save.rflags &=
1717 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1718 update_db_intercept(&svm->vcpu);
1719 }
1720
1721 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001722 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001723 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1724 kvm_run->debug.arch.pc =
1725 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1726 kvm_run->debug.arch.exception = DB_VECTOR;
1727 return 0;
1728 }
1729
1730 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001731}
1732
Avi Kivity851ba692009-08-24 11:10:17 +03001733static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001734{
Avi Kivity851ba692009-08-24 11:10:17 +03001735 struct kvm_run *kvm_run = svm->vcpu.run;
1736
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001737 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1738 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1739 kvm_run->debug.arch.exception = BP_VECTOR;
1740 return 0;
1741}
1742
Avi Kivity851ba692009-08-24 11:10:17 +03001743static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001744{
1745 int er;
1746
Andre Przywara51d8b662010-12-21 11:12:02 +01001747 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001748 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001749 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001750 return 1;
1751}
1752
Avi Kivity6b52d182010-01-21 15:31:47 +02001753static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001754{
Avi Kivity6b52d182010-01-21 15:31:47 +02001755 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001756
Joerg Roedel18c918c2010-11-30 18:03:59 +01001757 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001758
Rusty Russelle756fc62007-07-30 20:07:08 +10001759 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001760 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001761}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001762
Avi Kivity6b52d182010-01-21 15:31:47 +02001763static int nm_interception(struct vcpu_svm *svm)
1764{
1765 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001766 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001767}
1768
Joerg Roedel67ec6602010-05-17 14:43:35 +02001769static bool is_erratum_383(void)
1770{
1771 int err, i;
1772 u64 value;
1773
1774 if (!erratum_383_found)
1775 return false;
1776
1777 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1778 if (err)
1779 return false;
1780
1781 /* Bit 62 may or may not be set for this mce */
1782 value &= ~(1ULL << 62);
1783
1784 if (value != 0xb600000000010015ULL)
1785 return false;
1786
1787 /* Clear MCi_STATUS registers */
1788 for (i = 0; i < 6; ++i)
1789 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1790
1791 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1792 if (!err) {
1793 u32 low, high;
1794
1795 value &= ~(1ULL << 2);
1796 low = lower_32_bits(value);
1797 high = upper_32_bits(value);
1798
1799 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1800 }
1801
1802 /* Flush tlb to evict multi-match entries */
1803 __flush_tlb_all();
1804
1805 return true;
1806}
1807
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001808static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001809{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001810 if (is_erratum_383()) {
1811 /*
1812 * Erratum 383 triggered. Guest state is corrupt so kill the
1813 * guest.
1814 */
1815 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1816
Avi Kivitya8eeb042010-05-10 12:34:53 +03001817 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001818
1819 return;
1820 }
1821
Joerg Roedel53371b52008-04-09 14:15:30 +02001822 /*
1823 * On an #MC intercept the MCE handler is not called automatically in
1824 * the host. So do it by hand here.
1825 */
1826 asm volatile (
1827 "int $0x12\n");
1828 /* not sure if we ever come back to this point */
1829
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001830 return;
1831}
1832
1833static int mc_interception(struct vcpu_svm *svm)
1834{
Joerg Roedel53371b52008-04-09 14:15:30 +02001835 return 1;
1836}
1837
Avi Kivity851ba692009-08-24 11:10:17 +03001838static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001839{
Avi Kivity851ba692009-08-24 11:10:17 +03001840 struct kvm_run *kvm_run = svm->vcpu.run;
1841
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001842 /*
1843 * VMCB is undefined after a SHUTDOWN intercept
1844 * so reinitialize it.
1845 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001846 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001847 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001848
1849 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1850 return 0;
1851}
1852
Avi Kivity851ba692009-08-24 11:10:17 +03001853static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001855 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001856 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001857 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001858 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001859
Rusty Russelle756fc62007-07-30 20:07:08 +10001860 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001861 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001862 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001863 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01001864 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001865
Avi Kivity039576c2007-03-20 12:46:50 +02001866 port = io_info >> 16;
1867 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001868 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001869 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001870
1871 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872}
1873
Avi Kivity851ba692009-08-24 11:10:17 +03001874static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001875{
1876 return 1;
1877}
1878
Avi Kivity851ba692009-08-24 11:10:17 +03001879static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001880{
1881 ++svm->vcpu.stat.irq_exits;
1882 return 1;
1883}
1884
Avi Kivity851ba692009-08-24 11:10:17 +03001885static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001886{
1887 return 1;
1888}
1889
Avi Kivity851ba692009-08-24 11:10:17 +03001890static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001892 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001893 skip_emulated_instruction(&svm->vcpu);
1894 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895}
1896
Avi Kivity851ba692009-08-24 11:10:17 +03001897static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001898{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001899 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001900 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001901 kvm_emulate_hypercall(&svm->vcpu);
1902 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001903}
1904
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001905static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1906{
1907 struct vcpu_svm *svm = to_svm(vcpu);
1908
1909 return svm->nested.nested_cr3;
1910}
1911
Avi Kivitye4e517b2011-07-28 11:36:17 +03001912static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
1913{
1914 struct vcpu_svm *svm = to_svm(vcpu);
1915 u64 cr3 = svm->nested.nested_cr3;
1916 u64 pdpte;
1917 int ret;
1918
1919 ret = kvm_read_guest_page(vcpu->kvm, gpa_to_gfn(cr3), &pdpte,
1920 offset_in_page(cr3) + index * 8, 8);
1921 if (ret)
1922 return 0;
1923 return pdpte;
1924}
1925
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001926static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1927 unsigned long root)
1928{
1929 struct vcpu_svm *svm = to_svm(vcpu);
1930
1931 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01001932 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001933 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001934}
1935
Avi Kivity6389ee92010-11-29 16:12:30 +02001936static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1937 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001938{
1939 struct vcpu_svm *svm = to_svm(vcpu);
1940
1941 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1942 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001943 svm->vmcb->control.exit_info_1 = fault->error_code;
1944 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001945
1946 nested_svm_vmexit(svm);
1947}
1948
Joerg Roedel4b161842010-09-10 17:31:03 +02001949static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1950{
1951 int r;
1952
1953 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1954
1955 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1956 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03001957 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02001958 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1959 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1960 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1961
1962 return r;
1963}
1964
1965static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1966{
1967 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1968}
1969
Alexander Grafc0725422008-11-25 20:17:03 +01001970static int nested_svm_check_permissions(struct vcpu_svm *svm)
1971{
Avi Kivityf6801df2010-01-21 15:31:50 +02001972 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001973 || !is_paging(&svm->vcpu)) {
1974 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1975 return 1;
1976 }
1977
1978 if (svm->vmcb->save.cpl) {
1979 kvm_inject_gp(&svm->vcpu, 0);
1980 return 1;
1981 }
1982
1983 return 0;
1984}
1985
Alexander Grafcf74a782008-11-25 20:17:08 +01001986static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1987 bool has_error_code, u32 error_code)
1988{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001989 int vmexit;
1990
Joerg Roedel20307532010-11-29 17:51:48 +01001991 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02001992 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001993
Joerg Roedel0295ad72009-08-07 11:49:37 +02001994 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1995 svm->vmcb->control.exit_code_hi = 0;
1996 svm->vmcb->control.exit_info_1 = error_code;
1997 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1998
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001999 vmexit = nested_svm_intercept(svm);
2000 if (vmexit == NESTED_EXIT_DONE)
2001 svm->nested.exit_required = true;
2002
2003 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002004}
2005
Joerg Roedel8fe54652010-02-19 16:23:01 +01002006/* This function returns true if it is save to enable the irq window */
2007static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002008{
Joerg Roedel20307532010-11-29 17:51:48 +01002009 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002010 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002011
Joerg Roedel26666952009-08-07 11:49:46 +02002012 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002013 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002014
Joerg Roedel26666952009-08-07 11:49:46 +02002015 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002016 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002017
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002018 /*
2019 * if vmexit was already requested (by intercepted exception
2020 * for instance) do not overwrite it with "external interrupt"
2021 * vmexit.
2022 */
2023 if (svm->nested.exit_required)
2024 return false;
2025
Joerg Roedel197717d2010-02-24 18:59:19 +01002026 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2027 svm->vmcb->control.exit_info_1 = 0;
2028 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002029
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002030 if (svm->nested.intercept & 1ULL) {
2031 /*
2032 * The #vmexit can't be emulated here directly because this
2033 * code path runs with irqs and preemtion disabled. A
2034 * #vmexit emulation might sleep. Only signal request for
2035 * the #vmexit here.
2036 */
2037 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002038 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002039 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002040 }
2041
Joerg Roedel8fe54652010-02-19 16:23:01 +01002042 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002043}
2044
Joerg Roedel887f5002010-02-24 18:59:12 +01002045/* This function returns true if it is save to enable the nmi window */
2046static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2047{
Joerg Roedel20307532010-11-29 17:51:48 +01002048 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002049 return true;
2050
2051 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2052 return true;
2053
2054 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2055 svm->nested.exit_required = true;
2056
2057 return false;
2058}
2059
Joerg Roedel7597f122010-02-19 16:23:00 +01002060static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002061{
2062 struct page *page;
2063
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002064 might_sleep();
2065
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002066 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002067 if (is_error_page(page))
2068 goto error;
2069
Joerg Roedel7597f122010-02-19 16:23:00 +01002070 *_page = page;
2071
2072 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002073
2074error:
2075 kvm_release_page_clean(page);
2076 kvm_inject_gp(&svm->vcpu, 0);
2077
2078 return NULL;
2079}
2080
Joerg Roedel7597f122010-02-19 16:23:00 +01002081static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002082{
Joerg Roedel7597f122010-02-19 16:23:00 +01002083 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002084 kvm_release_page_dirty(page);
2085}
2086
Joerg Roedelce2ac082010-03-01 15:34:39 +01002087static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002088{
Joerg Roedelce2ac082010-03-01 15:34:39 +01002089 unsigned port;
2090 u8 val, bit;
2091 u64 gpa;
2092
2093 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2094 return NESTED_EXIT_HOST;
2095
2096 port = svm->vmcb->control.exit_info_1 >> 16;
2097 gpa = svm->nested.vmcb_iopm + (port / 8);
2098 bit = port % 8;
2099 val = 0;
2100
2101 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
2102 val &= (1 << bit);
2103
2104 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
2105}
2106
Joerg Roedeld2477822010-03-01 15:34:34 +01002107static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002108{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002109 u32 offset, msr, value;
2110 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002111
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002112 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002113 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002114
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002115 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2116 offset = svm_msrpm_offset(msr);
2117 write = svm->vmcb->control.exit_info_1 & 1;
2118 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002119
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002120 if (offset == MSR_INVALID)
2121 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002122
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002123 /* Offset is in 32 bit units but need in 8 bit units */
2124 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002125
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002126 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
2127 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002128
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002129 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002130}
2131
Joerg Roedel410e4d52009-08-07 11:49:44 +02002132static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002133{
Alexander Grafcf74a782008-11-25 20:17:08 +01002134 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002135
Joerg Roedel410e4d52009-08-07 11:49:44 +02002136 switch (exit_code) {
2137 case SVM_EXIT_INTR:
2138 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002139 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002140 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002141 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002142 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002143 if (npt_enabled)
2144 return NESTED_EXIT_HOST;
2145 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002146 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002147 /* When we're shadowing, trap PFs, but not async PF */
2148 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002149 return NESTED_EXIT_HOST;
2150 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01002151 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
2152 nm_interception(svm);
2153 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002154 default:
2155 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002156 }
2157
Joerg Roedel410e4d52009-08-07 11:49:44 +02002158 return NESTED_EXIT_CONTINUE;
2159}
2160
2161/*
2162 * If this function returns true, this #vmexit was already handled
2163 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002164static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002165{
2166 u32 exit_code = svm->vmcb->control.exit_code;
2167 int vmexit = NESTED_EXIT_HOST;
2168
Alexander Grafcf74a782008-11-25 20:17:08 +01002169 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002170 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002171 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002172 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002173 case SVM_EXIT_IOIO:
2174 vmexit = nested_svm_intercept_ioio(svm);
2175 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002176 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2177 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2178 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002179 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002180 break;
2181 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002182 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2183 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2184 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002185 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002186 break;
2187 }
2188 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2189 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002190 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002191 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002192 /* async page fault always cause vmexit */
2193 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2194 svm->apf_reason != 0)
2195 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002196 break;
2197 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002198 case SVM_EXIT_ERR: {
2199 vmexit = NESTED_EXIT_DONE;
2200 break;
2201 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002202 default: {
2203 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002204 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002205 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002206 }
2207 }
2208
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002209 return vmexit;
2210}
2211
2212static int nested_svm_exit_handled(struct vcpu_svm *svm)
2213{
2214 int vmexit;
2215
2216 vmexit = nested_svm_intercept(svm);
2217
2218 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002219 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002220
2221 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002222}
2223
Joerg Roedel0460a972009-08-07 11:49:31 +02002224static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2225{
2226 struct vmcb_control_area *dst = &dst_vmcb->control;
2227 struct vmcb_control_area *from = &from_vmcb->control;
2228
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002229 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002230 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002231 dst->intercept_exceptions = from->intercept_exceptions;
2232 dst->intercept = from->intercept;
2233 dst->iopm_base_pa = from->iopm_base_pa;
2234 dst->msrpm_base_pa = from->msrpm_base_pa;
2235 dst->tsc_offset = from->tsc_offset;
2236 dst->asid = from->asid;
2237 dst->tlb_ctl = from->tlb_ctl;
2238 dst->int_ctl = from->int_ctl;
2239 dst->int_vector = from->int_vector;
2240 dst->int_state = from->int_state;
2241 dst->exit_code = from->exit_code;
2242 dst->exit_code_hi = from->exit_code_hi;
2243 dst->exit_info_1 = from->exit_info_1;
2244 dst->exit_info_2 = from->exit_info_2;
2245 dst->exit_int_info = from->exit_int_info;
2246 dst->exit_int_info_err = from->exit_int_info_err;
2247 dst->nested_ctl = from->nested_ctl;
2248 dst->event_inj = from->event_inj;
2249 dst->event_inj_err = from->event_inj_err;
2250 dst->nested_cr3 = from->nested_cr3;
2251 dst->lbr_ctl = from->lbr_ctl;
2252}
2253
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002254static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002255{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002256 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002257 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002258 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002259 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002260
Joerg Roedel17897f32009-10-09 16:08:29 +02002261 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2262 vmcb->control.exit_info_1,
2263 vmcb->control.exit_info_2,
2264 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002265 vmcb->control.exit_int_info_err,
2266 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002267
Joerg Roedel7597f122010-02-19 16:23:00 +01002268 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002269 if (!nested_vmcb)
2270 return 1;
2271
Joerg Roedel20307532010-11-29 17:51:48 +01002272 /* Exit Guest-Mode */
2273 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002274 svm->nested.vmcb = 0;
2275
Alexander Grafcf74a782008-11-25 20:17:08 +01002276 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002277 disable_gif(svm);
2278
2279 nested_vmcb->save.es = vmcb->save.es;
2280 nested_vmcb->save.cs = vmcb->save.cs;
2281 nested_vmcb->save.ss = vmcb->save.ss;
2282 nested_vmcb->save.ds = vmcb->save.ds;
2283 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2284 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002285 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002286 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002287 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002288 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002289 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002290 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002291 nested_vmcb->save.rip = vmcb->save.rip;
2292 nested_vmcb->save.rsp = vmcb->save.rsp;
2293 nested_vmcb->save.rax = vmcb->save.rax;
2294 nested_vmcb->save.dr7 = vmcb->save.dr7;
2295 nested_vmcb->save.dr6 = vmcb->save.dr6;
2296 nested_vmcb->save.cpl = vmcb->save.cpl;
2297
2298 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2299 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2300 nested_vmcb->control.int_state = vmcb->control.int_state;
2301 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2302 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2303 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2304 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2305 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2306 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002307 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002308
2309 /*
2310 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2311 * to make sure that we do not lose injected events. So check event_inj
2312 * here and copy it to exit_int_info if it is valid.
2313 * Exit_int_info and event_inj can't be both valid because the case
2314 * below only happens on a VMRUN instruction intercept which has
2315 * no valid exit_int_info set.
2316 */
2317 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2318 struct vmcb_control_area *nc = &nested_vmcb->control;
2319
2320 nc->exit_int_info = vmcb->control.event_inj;
2321 nc->exit_int_info_err = vmcb->control.event_inj_err;
2322 }
2323
Joerg Roedel33740e42009-08-07 11:49:29 +02002324 nested_vmcb->control.tlb_ctl = 0;
2325 nested_vmcb->control.event_inj = 0;
2326 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002327
2328 /* We always set V_INTR_MASKING and remember the old value in hflags */
2329 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2330 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2331
Alexander Grafcf74a782008-11-25 20:17:08 +01002332 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002333 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002334
Alexander Graf219b65d2009-06-15 15:21:25 +02002335 kvm_clear_exception_queue(&svm->vcpu);
2336 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002337
Joerg Roedel4b161842010-09-10 17:31:03 +02002338 svm->nested.nested_cr3 = 0;
2339
Alexander Grafcf74a782008-11-25 20:17:08 +01002340 /* Restore selected save entries */
2341 svm->vmcb->save.es = hsave->save.es;
2342 svm->vmcb->save.cs = hsave->save.cs;
2343 svm->vmcb->save.ss = hsave->save.ss;
2344 svm->vmcb->save.ds = hsave->save.ds;
2345 svm->vmcb->save.gdtr = hsave->save.gdtr;
2346 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002347 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002348 svm_set_efer(&svm->vcpu, hsave->save.efer);
2349 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2350 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2351 if (npt_enabled) {
2352 svm->vmcb->save.cr3 = hsave->save.cr3;
2353 svm->vcpu.arch.cr3 = hsave->save.cr3;
2354 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002355 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002356 }
2357 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2358 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2359 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2360 svm->vmcb->save.dr7 = 0;
2361 svm->vmcb->save.cpl = 0;
2362 svm->vmcb->control.exit_int_info = 0;
2363
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002364 mark_all_dirty(svm->vmcb);
2365
Joerg Roedel7597f122010-02-19 16:23:00 +01002366 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002367
Joerg Roedel4b161842010-09-10 17:31:03 +02002368 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002369 kvm_mmu_reset_context(&svm->vcpu);
2370 kvm_mmu_load(&svm->vcpu);
2371
2372 return 0;
2373}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002374
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002375static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002376{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002377 /*
2378 * This function merges the msr permission bitmaps of kvm and the
2379 * nested vmcb. It is omptimized in that it only merges the parts where
2380 * the kvm msr permission bitmap may contain zero bits
2381 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002382 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002383
Joerg Roedel323c3d82010-03-01 15:34:37 +01002384 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2385 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002386
Joerg Roedel323c3d82010-03-01 15:34:37 +01002387 for (i = 0; i < MSRPM_OFFSETS; i++) {
2388 u32 value, p;
2389 u64 offset;
2390
2391 if (msrpm_offsets[i] == 0xffffffff)
2392 break;
2393
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002394 p = msrpm_offsets[i];
2395 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002396
2397 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2398 return false;
2399
2400 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2401 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002402
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002403 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002404
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002405 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002406}
2407
Joerg Roedel52c65a302010-08-02 16:46:44 +02002408static bool nested_vmcb_checks(struct vmcb *vmcb)
2409{
2410 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2411 return false;
2412
Joerg Roedeldbe77582010-08-02 16:46:45 +02002413 if (vmcb->control.asid == 0)
2414 return false;
2415
Joerg Roedel4b161842010-09-10 17:31:03 +02002416 if (vmcb->control.nested_ctl && !npt_enabled)
2417 return false;
2418
Joerg Roedel52c65a302010-08-02 16:46:44 +02002419 return true;
2420}
2421
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002422static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002423{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002424 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002425 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002426 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002427 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002428 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002429
Joerg Roedel06fc77722010-02-19 16:23:07 +01002430 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002431
Joerg Roedel7597f122010-02-19 16:23:00 +01002432 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002433 if (!nested_vmcb)
2434 return false;
2435
Joerg Roedel52c65a302010-08-02 16:46:44 +02002436 if (!nested_vmcb_checks(nested_vmcb)) {
2437 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2438 nested_vmcb->control.exit_code_hi = 0;
2439 nested_vmcb->control.exit_info_1 = 0;
2440 nested_vmcb->control.exit_info_2 = 0;
2441
2442 nested_svm_unmap(page);
2443
2444 return false;
2445 }
2446
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002447 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002448 nested_vmcb->save.rip,
2449 nested_vmcb->control.int_ctl,
2450 nested_vmcb->control.event_inj,
2451 nested_vmcb->control.nested_ctl);
2452
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002453 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2454 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002455 nested_vmcb->control.intercept_exceptions,
2456 nested_vmcb->control.intercept);
2457
Alexander Graf3d6368e2008-11-25 20:17:07 +01002458 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002459 kvm_clear_exception_queue(&svm->vcpu);
2460 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002461
Joerg Roedele0231712010-02-24 18:59:10 +01002462 /*
2463 * Save the old vmcb, so we don't need to pick what we save, but can
2464 * restore everything when a VMEXIT occurs
2465 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002466 hsave->save.es = vmcb->save.es;
2467 hsave->save.cs = vmcb->save.cs;
2468 hsave->save.ss = vmcb->save.ss;
2469 hsave->save.ds = vmcb->save.ds;
2470 hsave->save.gdtr = vmcb->save.gdtr;
2471 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002472 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002473 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002474 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002475 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002476 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002477 hsave->save.rsp = vmcb->save.rsp;
2478 hsave->save.rax = vmcb->save.rax;
2479 if (npt_enabled)
2480 hsave->save.cr3 = vmcb->save.cr3;
2481 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002482 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002483
Joerg Roedel0460a972009-08-07 11:49:31 +02002484 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002485
Avi Kivityf6e78472010-08-02 15:30:20 +03002486 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002487 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2488 else
2489 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2490
Joerg Roedel4b161842010-09-10 17:31:03 +02002491 if (nested_vmcb->control.nested_ctl) {
2492 kvm_mmu_unload(&svm->vcpu);
2493 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2494 nested_svm_init_mmu_context(&svm->vcpu);
2495 }
2496
Alexander Graf3d6368e2008-11-25 20:17:07 +01002497 /* Load the nested guest state */
2498 svm->vmcb->save.es = nested_vmcb->save.es;
2499 svm->vmcb->save.cs = nested_vmcb->save.cs;
2500 svm->vmcb->save.ss = nested_vmcb->save.ss;
2501 svm->vmcb->save.ds = nested_vmcb->save.ds;
2502 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2503 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002504 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002505 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2506 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2507 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2508 if (npt_enabled) {
2509 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2510 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002511 } else
Avi Kivity23902182010-06-10 17:02:16 +03002512 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002513
2514 /* Guest paging mode is active - reset mmu */
2515 kvm_mmu_reset_context(&svm->vcpu);
2516
Joerg Roedeldefbba52009-08-07 11:49:30 +02002517 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002518 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2519 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2520 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002521
Alexander Graf3d6368e2008-11-25 20:17:07 +01002522 /* In case we don't even reach vcpu_run, the fields are not updated */
2523 svm->vmcb->save.rax = nested_vmcb->save.rax;
2524 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2525 svm->vmcb->save.rip = nested_vmcb->save.rip;
2526 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2527 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2528 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2529
Joerg Roedelf7138532010-03-01 15:34:40 +01002530 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002531 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002532
Joerg Roedelaad42c62009-08-07 11:49:34 +02002533 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002534 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002535 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002536 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2537 svm->nested.intercept = nested_vmcb->control.intercept;
2538
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002539 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002540 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002541 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2542 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2543 else
2544 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2545
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002546 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2547 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002548 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2549 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002550 }
2551
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002552 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002553 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002554
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002555 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002556 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2557 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2558 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002559 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2560 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2561
Joerg Roedel7597f122010-02-19 16:23:00 +01002562 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002563
Joerg Roedel20307532010-11-29 17:51:48 +01002564 /* Enter Guest-Mode */
2565 enter_guest_mode(&svm->vcpu);
2566
Joerg Roedel384c6362010-11-30 18:03:56 +01002567 /*
2568 * Merge guest and host intercepts - must be called with vcpu in
2569 * guest-mode to take affect here
2570 */
2571 recalc_intercepts(svm);
2572
Joerg Roedel06fc77722010-02-19 16:23:07 +01002573 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002574
Joerg Roedel2af91942009-08-07 11:49:28 +02002575 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002576
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002577 mark_all_dirty(svm->vmcb);
2578
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002579 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002580}
2581
Joerg Roedel9966bf62009-08-07 11:49:40 +02002582static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002583{
2584 to_vmcb->save.fs = from_vmcb->save.fs;
2585 to_vmcb->save.gs = from_vmcb->save.gs;
2586 to_vmcb->save.tr = from_vmcb->save.tr;
2587 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2588 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2589 to_vmcb->save.star = from_vmcb->save.star;
2590 to_vmcb->save.lstar = from_vmcb->save.lstar;
2591 to_vmcb->save.cstar = from_vmcb->save.cstar;
2592 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2593 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2594 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2595 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002596}
2597
Avi Kivity851ba692009-08-24 11:10:17 +03002598static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002599{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002600 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002601 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002602
Alexander Graf55426752008-11-25 20:17:06 +01002603 if (nested_svm_check_permissions(svm))
2604 return 1;
2605
Joerg Roedel7597f122010-02-19 16:23:00 +01002606 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002607 if (!nested_vmcb)
2608 return 1;
2609
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002610 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2611 skip_emulated_instruction(&svm->vcpu);
2612
Joerg Roedel9966bf62009-08-07 11:49:40 +02002613 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002614 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002615
2616 return 1;
2617}
2618
Avi Kivity851ba692009-08-24 11:10:17 +03002619static int vmsave_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(svm->vmcb, nested_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 vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002641{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002642 if (nested_svm_check_permissions(svm))
2643 return 1;
2644
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002645 /* Save rip after vmrun instruction */
2646 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002647
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002648 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002649 return 1;
2650
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002651 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002652 goto failed;
2653
2654 return 1;
2655
2656failed:
2657
2658 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2659 svm->vmcb->control.exit_code_hi = 0;
2660 svm->vmcb->control.exit_info_1 = 0;
2661 svm->vmcb->control.exit_info_2 = 0;
2662
2663 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002664
2665 return 1;
2666}
2667
Avi Kivity851ba692009-08-24 11:10:17 +03002668static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002669{
2670 if (nested_svm_check_permissions(svm))
2671 return 1;
2672
2673 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2674 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002675 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002676
Joerg Roedel2af91942009-08-07 11:49:28 +02002677 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002678
2679 return 1;
2680}
2681
Avi Kivity851ba692009-08-24 11:10:17 +03002682static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002683{
2684 if (nested_svm_check_permissions(svm))
2685 return 1;
2686
2687 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2688 skip_emulated_instruction(&svm->vcpu);
2689
Joerg Roedel2af91942009-08-07 11:49:28 +02002690 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002691
2692 /* After a CLGI no interrupts should come */
2693 svm_clear_vintr(svm);
2694 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2695
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002696 mark_dirty(svm->vmcb, VMCB_INTR);
2697
Alexander Graf1371d902008-11-25 20:17:04 +01002698 return 1;
2699}
2700
Avi Kivity851ba692009-08-24 11:10:17 +03002701static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002702{
2703 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002704
Joerg Roedelec1ff792009-10-09 16:08:31 +02002705 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2706 vcpu->arch.regs[VCPU_REGS_RAX]);
2707
Alexander Grafff092382009-06-15 15:21:24 +02002708 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2709 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2710
2711 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2712 skip_emulated_instruction(&svm->vcpu);
2713 return 1;
2714}
2715
Joerg Roedel532a46b2009-10-09 16:08:32 +02002716static int skinit_interception(struct vcpu_svm *svm)
2717{
2718 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2719
2720 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2721 return 1;
2722}
2723
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002724static int xsetbv_interception(struct vcpu_svm *svm)
2725{
2726 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
2727 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
2728
2729 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
2730 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2731 skip_emulated_instruction(&svm->vcpu);
2732 }
2733
2734 return 1;
2735}
2736
Avi Kivity851ba692009-08-24 11:10:17 +03002737static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002739 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740 return 1;
2741}
2742
Avi Kivity851ba692009-08-24 11:10:17 +03002743static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744{
Izik Eidus37817f22008-03-24 23:14:53 +02002745 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002746 int reason;
2747 int int_type = svm->vmcb->control.exit_int_info &
2748 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002749 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002750 uint32_t type =
2751 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2752 uint32_t idt_v =
2753 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002754 bool has_error_code = false;
2755 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002756
2757 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002758
Izik Eidus37817f22008-03-24 23:14:53 +02002759 if (svm->vmcb->control.exit_info_2 &
2760 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002761 reason = TASK_SWITCH_IRET;
2762 else if (svm->vmcb->control.exit_info_2 &
2763 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2764 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002765 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002766 reason = TASK_SWITCH_GATE;
2767 else
2768 reason = TASK_SWITCH_CALL;
2769
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002770 if (reason == TASK_SWITCH_GATE) {
2771 switch (type) {
2772 case SVM_EXITINTINFO_TYPE_NMI:
2773 svm->vcpu.arch.nmi_injected = false;
2774 break;
2775 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002776 if (svm->vmcb->control.exit_info_2 &
2777 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2778 has_error_code = true;
2779 error_code =
2780 (u32)svm->vmcb->control.exit_info_2;
2781 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002782 kvm_clear_exception_queue(&svm->vcpu);
2783 break;
2784 case SVM_EXITINTINFO_TYPE_INTR:
2785 kvm_clear_interrupt_queue(&svm->vcpu);
2786 break;
2787 default:
2788 break;
2789 }
2790 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002791
Gleb Natapov8317c292009-04-12 13:37:02 +03002792 if (reason != TASK_SWITCH_GATE ||
2793 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2794 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002795 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2796 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002797
Gleb Natapovacb54512010-04-15 21:03:50 +03002798 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2799 has_error_code, error_code) == EMULATE_FAIL) {
2800 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2801 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2802 svm->vcpu.run->internal.ndata = 0;
2803 return 0;
2804 }
2805 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806}
2807
Avi Kivity851ba692009-08-24 11:10:17 +03002808static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002810 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002811 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002812 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813}
2814
Avi Kivity851ba692009-08-24 11:10:17 +03002815static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002816{
2817 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002818 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002819 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02002820 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002821 return 1;
2822}
2823
Avi Kivity851ba692009-08-24 11:10:17 +03002824static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002825{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002826 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2827 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
2828
2829 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
2830 skip_emulated_instruction(&svm->vcpu);
2831 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002832}
2833
Avi Kivity851ba692009-08-24 11:10:17 +03002834static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835{
Andre Przywara51d8b662010-12-21 11:12:02 +01002836 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837}
2838
Avi Kivity332b56e2011-11-10 14:57:24 +02002839static int rdpmc_interception(struct vcpu_svm *svm)
2840{
2841 int err;
2842
2843 if (!static_cpu_has(X86_FEATURE_NRIPS))
2844 return emulate_on_interception(svm);
2845
2846 err = kvm_rdpmc(&svm->vcpu);
2847 kvm_complete_insn_gp(&svm->vcpu, err);
2848
2849 return 1;
2850}
2851
Joerg Roedel628afd22011-04-04 12:39:36 +02002852bool check_selective_cr0_intercepted(struct vcpu_svm *svm, unsigned long val)
2853{
2854 unsigned long cr0 = svm->vcpu.arch.cr0;
2855 bool ret = false;
2856 u64 intercept;
2857
2858 intercept = svm->nested.intercept;
2859
2860 if (!is_guest_mode(&svm->vcpu) ||
2861 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
2862 return false;
2863
2864 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2865 val &= ~SVM_CR0_SELECTIVE_MASK;
2866
2867 if (cr0 ^ val) {
2868 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2869 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2870 }
2871
2872 return ret;
2873}
2874
Andre Przywara7ff76d52010-12-21 11:12:04 +01002875#define CR_VALID (1ULL << 63)
2876
2877static int cr_interception(struct vcpu_svm *svm)
2878{
2879 int reg, cr;
2880 unsigned long val;
2881 int err;
2882
2883 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2884 return emulate_on_interception(svm);
2885
2886 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2887 return emulate_on_interception(svm);
2888
2889 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2890 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
2891
2892 err = 0;
2893 if (cr >= 16) { /* mov to cr */
2894 cr -= 16;
2895 val = kvm_register_read(&svm->vcpu, reg);
2896 switch (cr) {
2897 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02002898 if (!check_selective_cr0_intercepted(svm, val))
2899 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002900 else
2901 return 1;
2902
Andre Przywara7ff76d52010-12-21 11:12:04 +01002903 break;
2904 case 3:
2905 err = kvm_set_cr3(&svm->vcpu, val);
2906 break;
2907 case 4:
2908 err = kvm_set_cr4(&svm->vcpu, val);
2909 break;
2910 case 8:
2911 err = kvm_set_cr8(&svm->vcpu, val);
2912 break;
2913 default:
2914 WARN(1, "unhandled write to CR%d", cr);
2915 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2916 return 1;
2917 }
2918 } else { /* mov from cr */
2919 switch (cr) {
2920 case 0:
2921 val = kvm_read_cr0(&svm->vcpu);
2922 break;
2923 case 2:
2924 val = svm->vcpu.arch.cr2;
2925 break;
2926 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02002927 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002928 break;
2929 case 4:
2930 val = kvm_read_cr4(&svm->vcpu);
2931 break;
2932 case 8:
2933 val = kvm_get_cr8(&svm->vcpu);
2934 break;
2935 default:
2936 WARN(1, "unhandled read from CR%d", cr);
2937 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2938 return 1;
2939 }
2940 kvm_register_write(&svm->vcpu, reg, val);
2941 }
2942 kvm_complete_insn_gp(&svm->vcpu, err);
2943
2944 return 1;
2945}
2946
Andre Przywaracae37972010-12-21 11:12:05 +01002947static int dr_interception(struct vcpu_svm *svm)
2948{
2949 int reg, dr;
2950 unsigned long val;
2951 int err;
2952
2953 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
2954 return emulate_on_interception(svm);
2955
2956 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2957 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
2958
2959 if (dr >= 16) { /* mov to DRn */
2960 val = kvm_register_read(&svm->vcpu, reg);
2961 kvm_set_dr(&svm->vcpu, dr - 16, val);
2962 } else {
2963 err = kvm_get_dr(&svm->vcpu, dr, &val);
2964 if (!err)
2965 kvm_register_write(&svm->vcpu, reg, val);
2966 }
2967
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01002968 skip_emulated_instruction(&svm->vcpu);
2969
Andre Przywaracae37972010-12-21 11:12:05 +01002970 return 1;
2971}
2972
Avi Kivity851ba692009-08-24 11:10:17 +03002973static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002974{
Avi Kivity851ba692009-08-24 11:10:17 +03002975 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01002976 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03002977
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002978 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2979 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01002980 r = cr_interception(svm);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002981 if (irqchip_in_kernel(svm->vcpu.kvm)) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002982 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002983 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002984 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002985 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002986 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01002987 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2988 return 0;
2989}
2990
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002991u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu)
2992{
2993 struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
2994 return vmcb->control.tsc_offset +
2995 svm_scale_tsc(vcpu, native_read_tsc());
2996}
2997
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2999{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003000 struct vcpu_svm *svm = to_svm(vcpu);
3001
Avi Kivity6aa8b732006-12-10 02:21:36 -08003002 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303003 case MSR_IA32_TSC: {
Nadav Har'El45133ec2011-08-02 15:55:23 +03003004 *data = svm->vmcb->control.tsc_offset +
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003005 svm_scale_tsc(vcpu, native_read_tsc());
3006
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007 break;
3008 }
Brian Gerst8c065852010-07-17 09:03:26 -04003009 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003010 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003011 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003012#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003014 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003015 break;
3016 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003017 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003018 break;
3019 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003020 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021 break;
3022 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003023 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003024 break;
3025#endif
3026 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003027 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028 break;
3029 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003030 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031 break;
3032 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003033 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003034 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003035 /*
3036 * Nobody will change the following 5 values in the VMCB so we can
3037 * safely return them on rdmsr. They will always be 0 until LBRV is
3038 * implemented.
3039 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003040 case MSR_IA32_DEBUGCTLMSR:
3041 *data = svm->vmcb->save.dbgctl;
3042 break;
3043 case MSR_IA32_LASTBRANCHFROMIP:
3044 *data = svm->vmcb->save.br_from;
3045 break;
3046 case MSR_IA32_LASTBRANCHTOIP:
3047 *data = svm->vmcb->save.br_to;
3048 break;
3049 case MSR_IA32_LASTINTFROMIP:
3050 *data = svm->vmcb->save.last_excp_from;
3051 break;
3052 case MSR_IA32_LASTINTTOIP:
3053 *data = svm->vmcb->save.last_excp_to;
3054 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003055 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003056 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003057 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003058 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003059 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003060 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003061 case MSR_IA32_UCODE_REV:
3062 *data = 0x01000065;
3063 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08003065 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066 }
3067 return 0;
3068}
3069
Avi Kivity851ba692009-08-24 11:10:17 +03003070static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003071{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003072 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003073 u64 data;
3074
Avi Kivity59200272010-01-25 19:47:02 +02003075 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
3076 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003077 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003078 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003079 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003080
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003081 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003082 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003083 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003084 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003085 }
3086 return 1;
3087}
3088
Joerg Roedel4a810182010-02-24 18:59:15 +01003089static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3090{
3091 struct vcpu_svm *svm = to_svm(vcpu);
3092 int svm_dis, chg_mask;
3093
3094 if (data & ~SVM_VM_CR_VALID_MASK)
3095 return 1;
3096
3097 chg_mask = SVM_VM_CR_VALID_MASK;
3098
3099 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3100 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3101
3102 svm->nested.vm_cr_msr &= ~chg_mask;
3103 svm->nested.vm_cr_msr |= (data & chg_mask);
3104
3105 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3106
3107 /* check for svm_disable while efer.svme is set */
3108 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3109 return 1;
3110
3111 return 0;
3112}
3113
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
3115{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003116 struct vcpu_svm *svm = to_svm(vcpu);
3117
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003119 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10003120 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003122 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003123 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003125#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003126 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003127 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128 break;
3129 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003130 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131 break;
3132 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003133 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 break;
3135 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003136 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003137 break;
3138#endif
3139 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003140 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141 break;
3142 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003143 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003144 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145 break;
3146 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003147 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003148 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003150 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003151 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003152 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003153 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003154 break;
3155 }
3156 if (data & DEBUGCTL_RESERVED_BITS)
3157 return 1;
3158
3159 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003160 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003161 if (data & (1ULL<<0))
3162 svm_enable_lbrv(svm);
3163 else
3164 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003165 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003166 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003167 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003168 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003169 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003170 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003171 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003172 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
3173 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08003175 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176 }
3177 return 0;
3178}
3179
Avi Kivity851ba692009-08-24 11:10:17 +03003180static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003182 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003183 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003184 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003185
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003186
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003187 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02003188 if (svm_set_msr(&svm->vcpu, ecx, data)) {
3189 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003190 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003191 } else {
3192 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10003193 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003194 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195 return 1;
3196}
3197
Avi Kivity851ba692009-08-24 11:10:17 +03003198static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199{
Rusty Russelle756fc62007-07-30 20:07:08 +10003200 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003201 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202 else
Avi Kivity851ba692009-08-24 11:10:17 +03003203 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204}
3205
Avi Kivity851ba692009-08-24 11:10:17 +03003206static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003207{
Avi Kivity851ba692009-08-24 11:10:17 +03003208 struct kvm_run *kvm_run = svm->vcpu.run;
3209
Avi Kivity3842d132010-07-27 12:30:24 +03003210 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003211 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003212 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003213 mark_dirty(svm->vmcb, VMCB_INTR);
Dor Laorc1150d82007-01-05 16:36:24 -08003214 /*
3215 * If the user space waits to inject interrupts, exit as soon as
3216 * possible
3217 */
Gleb Natapov80618232009-04-21 17:44:56 +03003218 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
3219 kvm_run->request_interrupt_window &&
3220 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10003221 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003222 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
3223 return 0;
3224 }
3225
3226 return 1;
3227}
3228
Mark Langsdorf565d0992009-10-06 14:25:02 -05003229static int pause_interception(struct vcpu_svm *svm)
3230{
3231 kvm_vcpu_on_spin(&(svm->vcpu));
3232 return 1;
3233}
3234
Avi Kivity851ba692009-08-24 11:10:17 +03003235static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003236 [SVM_EXIT_READ_CR0] = cr_interception,
3237 [SVM_EXIT_READ_CR3] = cr_interception,
3238 [SVM_EXIT_READ_CR4] = cr_interception,
3239 [SVM_EXIT_READ_CR8] = cr_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02003240 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003241 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003242 [SVM_EXIT_WRITE_CR3] = cr_interception,
3243 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003244 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003245 [SVM_EXIT_READ_DR0] = dr_interception,
3246 [SVM_EXIT_READ_DR1] = dr_interception,
3247 [SVM_EXIT_READ_DR2] = dr_interception,
3248 [SVM_EXIT_READ_DR3] = dr_interception,
3249 [SVM_EXIT_READ_DR4] = dr_interception,
3250 [SVM_EXIT_READ_DR5] = dr_interception,
3251 [SVM_EXIT_READ_DR6] = dr_interception,
3252 [SVM_EXIT_READ_DR7] = dr_interception,
3253 [SVM_EXIT_WRITE_DR0] = dr_interception,
3254 [SVM_EXIT_WRITE_DR1] = dr_interception,
3255 [SVM_EXIT_WRITE_DR2] = dr_interception,
3256 [SVM_EXIT_WRITE_DR3] = dr_interception,
3257 [SVM_EXIT_WRITE_DR4] = dr_interception,
3258 [SVM_EXIT_WRITE_DR5] = dr_interception,
3259 [SVM_EXIT_WRITE_DR6] = dr_interception,
3260 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003261 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3262 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003263 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003264 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3265 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
3266 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
3267 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003268 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269 [SVM_EXIT_SMI] = nop_on_interception,
3270 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003271 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02003272 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003274 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003275 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003276 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003278 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003279 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003280 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 [SVM_EXIT_MSR] = msr_interception,
3282 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003283 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003284 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003285 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003286 [SVM_EXIT_VMLOAD] = vmload_interception,
3287 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003288 [SVM_EXIT_STGI] = stgi_interception,
3289 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003290 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003291 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01003292 [SVM_EXIT_MONITOR] = invalid_op_interception,
3293 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003294 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003295 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003296};
3297
Joe Perchesae8cc052011-04-24 22:00:50 -07003298static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003299{
3300 struct vcpu_svm *svm = to_svm(vcpu);
3301 struct vmcb_control_area *control = &svm->vmcb->control;
3302 struct vmcb_save_area *save = &svm->vmcb->save;
3303
3304 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003305 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
3306 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
3307 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
3308 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
3309 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
3310 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
3311 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3312 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3313 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3314 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3315 pr_err("%-20s%d\n", "asid:", control->asid);
3316 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3317 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3318 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3319 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3320 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3321 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3322 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3323 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3324 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3325 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3326 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
3327 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3328 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3329 pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
3330 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003331 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003332 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3333 "es:",
3334 save->es.selector, save->es.attrib,
3335 save->es.limit, save->es.base);
3336 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3337 "cs:",
3338 save->cs.selector, save->cs.attrib,
3339 save->cs.limit, save->cs.base);
3340 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3341 "ss:",
3342 save->ss.selector, save->ss.attrib,
3343 save->ss.limit, save->ss.base);
3344 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3345 "ds:",
3346 save->ds.selector, save->ds.attrib,
3347 save->ds.limit, save->ds.base);
3348 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3349 "fs:",
3350 save->fs.selector, save->fs.attrib,
3351 save->fs.limit, save->fs.base);
3352 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3353 "gs:",
3354 save->gs.selector, save->gs.attrib,
3355 save->gs.limit, save->gs.base);
3356 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3357 "gdtr:",
3358 save->gdtr.selector, save->gdtr.attrib,
3359 save->gdtr.limit, save->gdtr.base);
3360 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3361 "ldtr:",
3362 save->ldtr.selector, save->ldtr.attrib,
3363 save->ldtr.limit, save->ldtr.base);
3364 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3365 "idtr:",
3366 save->idtr.selector, save->idtr.attrib,
3367 save->idtr.limit, save->idtr.base);
3368 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3369 "tr:",
3370 save->tr.selector, save->tr.attrib,
3371 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003372 pr_err("cpl: %d efer: %016llx\n",
3373 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003374 pr_err("%-15s %016llx %-13s %016llx\n",
3375 "cr0:", save->cr0, "cr2:", save->cr2);
3376 pr_err("%-15s %016llx %-13s %016llx\n",
3377 "cr3:", save->cr3, "cr4:", save->cr4);
3378 pr_err("%-15s %016llx %-13s %016llx\n",
3379 "dr6:", save->dr6, "dr7:", save->dr7);
3380 pr_err("%-15s %016llx %-13s %016llx\n",
3381 "rip:", save->rip, "rflags:", save->rflags);
3382 pr_err("%-15s %016llx %-13s %016llx\n",
3383 "rsp:", save->rsp, "rax:", save->rax);
3384 pr_err("%-15s %016llx %-13s %016llx\n",
3385 "star:", save->star, "lstar:", save->lstar);
3386 pr_err("%-15s %016llx %-13s %016llx\n",
3387 "cstar:", save->cstar, "sfmask:", save->sfmask);
3388 pr_err("%-15s %016llx %-13s %016llx\n",
3389 "kernel_gs_base:", save->kernel_gs_base,
3390 "sysenter_cs:", save->sysenter_cs);
3391 pr_err("%-15s %016llx %-13s %016llx\n",
3392 "sysenter_esp:", save->sysenter_esp,
3393 "sysenter_eip:", save->sysenter_eip);
3394 pr_err("%-15s %016llx %-13s %016llx\n",
3395 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3396 pr_err("%-15s %016llx %-13s %016llx\n",
3397 "br_from:", save->br_from, "br_to:", save->br_to);
3398 pr_err("%-15s %016llx %-13s %016llx\n",
3399 "excp_from:", save->last_excp_from,
3400 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003401}
3402
Avi Kivity586f9602010-11-18 13:09:54 +02003403static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3404{
3405 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3406
3407 *info1 = control->exit_info_1;
3408 *info2 = control->exit_info_2;
3409}
3410
Avi Kivity851ba692009-08-24 11:10:17 +03003411static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003412{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003413 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003414 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003415 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003416
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003417 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003418 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3419 if (npt_enabled)
3420 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003421
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003422 if (unlikely(svm->nested.exit_required)) {
3423 nested_svm_vmexit(svm);
3424 svm->nested.exit_required = false;
3425
3426 return 1;
3427 }
3428
Joerg Roedel20307532010-11-29 17:51:48 +01003429 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003430 int vmexit;
3431
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003432 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3433 svm->vmcb->control.exit_info_1,
3434 svm->vmcb->control.exit_info_2,
3435 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003436 svm->vmcb->control.exit_int_info_err,
3437 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003438
Joerg Roedel410e4d52009-08-07 11:49:44 +02003439 vmexit = nested_svm_exit_special(svm);
3440
3441 if (vmexit == NESTED_EXIT_CONTINUE)
3442 vmexit = nested_svm_exit_handled(svm);
3443
3444 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003445 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003446 }
3447
Joerg Roedela5c38322009-08-07 11:49:32 +02003448 svm_complete_interrupts(svm);
3449
Avi Kivity04d2cc72007-09-10 18:10:54 +03003450 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3451 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3452 kvm_run->fail_entry.hardware_entry_failure_reason
3453 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003454 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3455 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003456 return 0;
3457 }
3458
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003459 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003460 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003461 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3462 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003463 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3464 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003465 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003466 exit_code);
3467
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003468 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003469 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003470 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003471 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003472 return 0;
3473 }
3474
Avi Kivity851ba692009-08-24 11:10:17 +03003475 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476}
3477
3478static void reload_tss(struct kvm_vcpu *vcpu)
3479{
3480 int cpu = raw_smp_processor_id();
3481
Tejun Heo0fe1e002009-10-29 22:34:14 +09003482 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3483 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484 load_TR_desc();
3485}
3486
Rusty Russelle756fc62007-07-30 20:07:08 +10003487static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003488{
3489 int cpu = raw_smp_processor_id();
3490
Tejun Heo0fe1e002009-10-29 22:34:14 +09003491 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003493 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003494 if (svm->asid_generation != sd->asid_generation)
3495 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003496}
3497
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003498static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3499{
3500 struct vcpu_svm *svm = to_svm(vcpu);
3501
3502 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3503 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003504 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003505 ++vcpu->stat.nmi_injections;
3506}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003507
Eddie Dong85f455f2007-07-06 12:20:49 +03003508static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509{
3510 struct vmcb_control_area *control;
3511
Rusty Russelle756fc62007-07-30 20:07:08 +10003512 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003513 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003514 control->int_ctl &= ~V_INTR_PRIO_MASK;
3515 control->int_ctl |= V_IRQ_MASK |
3516 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003517 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518}
3519
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003520static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003521{
3522 struct vcpu_svm *svm = to_svm(vcpu);
3523
Joerg Roedel2af91942009-08-07 11:49:28 +02003524 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003525
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003526 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3527 ++vcpu->stat.irq_injections;
3528
Alexander Graf219b65d2009-06-15 15:21:25 +02003529 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3530 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003531}
3532
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003533static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3534{
3535 struct vcpu_svm *svm = to_svm(vcpu);
3536
Joerg Roedel20307532010-11-29 17:51:48 +01003537 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003538 return;
3539
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003540 if (irr == -1)
3541 return;
3542
3543 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003544 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003545}
3546
3547static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003548{
3549 struct vcpu_svm *svm = to_svm(vcpu);
3550 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003551 int ret;
3552 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3553 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3554 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3555
3556 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003557}
3558
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003559static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3560{
3561 struct vcpu_svm *svm = to_svm(vcpu);
3562
3563 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3564}
3565
3566static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3567{
3568 struct vcpu_svm *svm = to_svm(vcpu);
3569
3570 if (masked) {
3571 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003572 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003573 } else {
3574 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003575 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003576 }
3577}
3578
Gleb Natapov78646122009-03-23 12:12:11 +02003579static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3580{
3581 struct vcpu_svm *svm = to_svm(vcpu);
3582 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003583 int ret;
3584
3585 if (!gif_set(svm) ||
3586 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3587 return 0;
3588
Avi Kivityf6e78472010-08-02 15:30:20 +03003589 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003590
Joerg Roedel20307532010-11-29 17:51:48 +01003591 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003592 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3593
3594 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003595}
3596
Gleb Natapov9222be12009-04-23 17:14:37 +03003597static void enable_irq_window(struct kvm_vcpu *vcpu)
3598{
Alexander Graf219b65d2009-06-15 15:21:25 +02003599 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003600
Joerg Roedele0231712010-02-24 18:59:10 +01003601 /*
3602 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3603 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3604 * get that intercept, this function will be called again though and
3605 * we'll get the vintr intercept.
3606 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003607 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003608 svm_set_vintr(svm);
3609 svm_inject_irq(svm, 0x0);
3610 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003611}
3612
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003613static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003614{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003615 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003616
Gleb Natapov44c11432009-05-11 13:35:52 +03003617 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3618 == HF_NMI_MASK)
3619 return; /* IRET will cause a vm exit */
3620
Joerg Roedele0231712010-02-24 18:59:10 +01003621 /*
3622 * Something prevents NMI from been injected. Single step over possible
3623 * problem (IRET or exception injection or interrupt shadow)
3624 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003625 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003626 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3627 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003628}
3629
Izik Eiduscbc94022007-10-25 00:29:55 +02003630static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3631{
3632 return 0;
3633}
3634
Avi Kivityd9e368d2007-06-07 19:18:30 +03003635static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3636{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003637 struct vcpu_svm *svm = to_svm(vcpu);
3638
3639 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3640 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3641 else
3642 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003643}
3644
Avi Kivity04d2cc72007-09-10 18:10:54 +03003645static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3646{
3647}
3648
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003649static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3650{
3651 struct vcpu_svm *svm = to_svm(vcpu);
3652
Joerg Roedel20307532010-11-29 17:51:48 +01003653 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003654 return;
3655
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003656 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003657 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003658 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003659 }
3660}
3661
Joerg Roedel649d6862008-04-16 16:51:15 +02003662static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3663{
3664 struct vcpu_svm *svm = to_svm(vcpu);
3665 u64 cr8;
3666
Joerg Roedel20307532010-11-29 17:51:48 +01003667 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003668 return;
3669
Joerg Roedel649d6862008-04-16 16:51:15 +02003670 cr8 = kvm_get_cr8(vcpu);
3671 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3672 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3673}
3674
Gleb Natapov9222be12009-04-23 17:14:37 +03003675static void svm_complete_interrupts(struct vcpu_svm *svm)
3676{
3677 u8 vector;
3678 int type;
3679 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003680 unsigned int3_injected = svm->int3_injected;
3681
3682 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003683
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003684 /*
3685 * If we've made progress since setting HF_IRET_MASK, we've
3686 * executed an IRET and can allow NMI injection.
3687 */
3688 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
3689 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003690 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003691 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3692 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003693
Gleb Natapov9222be12009-04-23 17:14:37 +03003694 svm->vcpu.arch.nmi_injected = false;
3695 kvm_clear_exception_queue(&svm->vcpu);
3696 kvm_clear_interrupt_queue(&svm->vcpu);
3697
3698 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3699 return;
3700
Avi Kivity3842d132010-07-27 12:30:24 +03003701 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3702
Gleb Natapov9222be12009-04-23 17:14:37 +03003703 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3704 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3705
3706 switch (type) {
3707 case SVM_EXITINTINFO_TYPE_NMI:
3708 svm->vcpu.arch.nmi_injected = true;
3709 break;
3710 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003711 /*
3712 * In case of software exceptions, do not reinject the vector,
3713 * but re-execute the instruction instead. Rewind RIP first
3714 * if we emulated INT3 before.
3715 */
3716 if (kvm_exception_is_soft(vector)) {
3717 if (vector == BP_VECTOR && int3_injected &&
3718 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3719 kvm_rip_write(&svm->vcpu,
3720 kvm_rip_read(&svm->vcpu) -
3721 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003722 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003723 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003724 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3725 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003726 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003727
3728 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003729 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003730 break;
3731 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003732 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003733 break;
3734 default:
3735 break;
3736 }
3737}
3738
Avi Kivityb463a6f2010-07-20 15:06:17 +03003739static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3740{
3741 struct vcpu_svm *svm = to_svm(vcpu);
3742 struct vmcb_control_area *control = &svm->vmcb->control;
3743
3744 control->exit_int_info = control->event_inj;
3745 control->exit_int_info_err = control->event_inj_err;
3746 control->event_inj = 0;
3747 svm_complete_interrupts(svm);
3748}
3749
Avi Kivity80e31d42008-07-14 14:44:59 +03003750#ifdef CONFIG_X86_64
3751#define R "r"
3752#else
3753#define R "e"
3754#endif
3755
Avi Kivity851ba692009-08-24 11:10:17 +03003756static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003757{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003758 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003759
Joerg Roedel2041a062010-04-22 12:33:08 +02003760 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3761 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3762 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3763
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003764 /*
3765 * A vmexit emulation is required before the vcpu can be executed
3766 * again.
3767 */
3768 if (unlikely(svm->nested.exit_required))
3769 return;
3770
Rusty Russelle756fc62007-07-30 20:07:08 +10003771 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003772
Joerg Roedel649d6862008-04-16 16:51:15 +02003773 sync_lapic_to_cr8(vcpu);
3774
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003775 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003776
Avi Kivity04d2cc72007-09-10 18:10:54 +03003777 clgi();
3778
3779 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003780
Avi Kivity6aa8b732006-12-10 02:21:36 -08003781 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003782 "push %%"R"bp; \n\t"
3783 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3784 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3785 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3786 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3787 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3788 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003789#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003790 "mov %c[r8](%[svm]), %%r8 \n\t"
3791 "mov %c[r9](%[svm]), %%r9 \n\t"
3792 "mov %c[r10](%[svm]), %%r10 \n\t"
3793 "mov %c[r11](%[svm]), %%r11 \n\t"
3794 "mov %c[r12](%[svm]), %%r12 \n\t"
3795 "mov %c[r13](%[svm]), %%r13 \n\t"
3796 "mov %c[r14](%[svm]), %%r14 \n\t"
3797 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003798#endif
3799
Avi Kivity6aa8b732006-12-10 02:21:36 -08003800 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003801 "push %%"R"ax \n\t"
3802 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003803 __ex(SVM_VMLOAD) "\n\t"
3804 __ex(SVM_VMRUN) "\n\t"
3805 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003806 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003807
3808 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003809 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3810 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3811 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3812 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3813 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3814 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003815#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003816 "mov %%r8, %c[r8](%[svm]) \n\t"
3817 "mov %%r9, %c[r9](%[svm]) \n\t"
3818 "mov %%r10, %c[r10](%[svm]) \n\t"
3819 "mov %%r11, %c[r11](%[svm]) \n\t"
3820 "mov %%r12, %c[r12](%[svm]) \n\t"
3821 "mov %%r13, %c[r13](%[svm]) \n\t"
3822 "mov %%r14, %c[r14](%[svm]) \n\t"
3823 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003824#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003825 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003826 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003827 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003828 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003829 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3830 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3831 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3832 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3833 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3834 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003835#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003836 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3837 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3838 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3839 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3840 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3841 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3842 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3843 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003845 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003846 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003847#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003848 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3849#endif
3850 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003851
Avi Kivity82ca2d12010-10-21 12:20:34 +02003852#ifdef CONFIG_X86_64
3853 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3854#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003855 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02003856#ifndef CONFIG_X86_32_LAZY_GS
3857 loadsegment(gs, svm->host.gs);
3858#endif
Avi Kivity9581d442010-10-19 16:46:55 +02003859#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003860
3861 reload_tss(vcpu);
3862
Avi Kivity56ba47d2007-11-07 17:14:18 +02003863 local_irq_disable();
3864
Avi Kivity13c34e02010-10-21 12:20:31 +02003865 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3866 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3867 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3868 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3869
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02003870 trace_kvm_exit(svm->vmcb->control.exit_code, vcpu, KVM_ISA_SVM);
3871
Joerg Roedel3781c012011-01-14 16:45:02 +01003872 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3873 kvm_before_handle_nmi(&svm->vcpu);
3874
3875 stgi();
3876
3877 /* Any pending NMI will happen here */
3878
3879 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3880 kvm_after_handle_nmi(&svm->vcpu);
3881
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003882 sync_cr8_to_lapic(vcpu);
3883
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003884 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003885
Joerg Roedel38e5e922010-12-03 15:25:16 +01003886 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3887
Gleb Natapov631bc482010-10-14 11:22:52 +02003888 /* if exit due to PF check for async PF */
3889 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3890 svm->apf_reason = kvm_read_and_reset_pf_reason();
3891
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003892 if (npt_enabled) {
3893 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3894 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3895 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003896
3897 /*
3898 * We need to handle MC intercepts here before the vcpu has a chance to
3899 * change the physical cpu
3900 */
3901 if (unlikely(svm->vmcb->control.exit_code ==
3902 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3903 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003904
3905 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003906}
3907
Avi Kivity80e31d42008-07-14 14:44:59 +03003908#undef R
3909
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3911{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003912 struct vcpu_svm *svm = to_svm(vcpu);
3913
3914 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003915 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003916 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003917}
3918
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003919static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3920{
3921 struct vcpu_svm *svm = to_svm(vcpu);
3922
3923 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01003924 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003925
3926 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02003927 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003928 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003929
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003930 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003931}
3932
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933static int is_disabled(void)
3934{
Joerg Roedel6031a612007-06-22 12:29:50 +03003935 u64 vm_cr;
3936
3937 rdmsrl(MSR_VM_CR, vm_cr);
3938 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3939 return 1;
3940
Avi Kivity6aa8b732006-12-10 02:21:36 -08003941 return 0;
3942}
3943
Ingo Molnar102d8322007-02-19 14:37:47 +02003944static void
3945svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3946{
3947 /*
3948 * Patch in the VMMCALL instruction:
3949 */
3950 hypercall[0] = 0x0f;
3951 hypercall[1] = 0x01;
3952 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003953}
3954
Yang, Sheng002c7f72007-07-31 14:23:01 +03003955static void svm_check_processor_compat(void *rtn)
3956{
3957 *(int *)rtn = 0;
3958}
3959
Avi Kivity774ead32007-12-26 13:57:04 +02003960static bool svm_cpu_has_accelerated_tpr(void)
3961{
3962 return false;
3963}
3964
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003965static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003966{
3967 return 0;
3968}
3969
Sheng Yang0e851882009-12-18 16:48:46 +08003970static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3971{
3972}
3973
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003974static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3975{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003976 switch (func) {
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02003977 case 0x80000001:
3978 if (nested)
3979 entry->ecx |= (1 << 2); /* Set SVM bit */
3980 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003981 case 0x8000000A:
3982 entry->eax = 1; /* SVM revision 1 */
3983 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3984 ASID emulation to nested SVM */
3985 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003986 entry->edx = 0; /* Per default do not support any
3987 additional features */
3988
3989 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003990 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02003991 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003992
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02003993 /* Support NPT for the guest if enabled */
3994 if (npt_enabled)
3995 entry->edx |= SVM_FEATURE_NPT;
3996
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003997 break;
3998 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003999}
4000
Sheng Yang17cc3932010-01-05 19:02:27 +08004001static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004002{
Sheng Yang17cc3932010-01-05 19:02:27 +08004003 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004004}
4005
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004006static bool svm_rdtscp_supported(void)
4007{
4008 return false;
4009}
4010
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004011static bool svm_has_wbinvd_exit(void)
4012{
4013 return true;
4014}
4015
Avi Kivity02daab22009-12-30 12:40:26 +02004016static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
4017{
4018 struct vcpu_svm *svm = to_svm(vcpu);
4019
Joerg Roedel18c918c2010-11-30 18:03:59 +01004020 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01004021 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02004022}
4023
Joerg Roedel80612522011-04-04 12:39:33 +02004024#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004025 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004026#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004027 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004028#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004029 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004030
4031static struct __x86_intercept {
4032 u32 exit_code;
4033 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004034} x86_intercept_map[] = {
4035 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4036 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4037 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4038 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4039 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004040 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4041 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004042 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4043 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4044 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4045 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4046 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4047 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4048 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4049 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004050 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4051 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4052 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4053 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4054 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4055 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4056 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4057 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004058 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4059 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4060 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004061 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4062 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4063 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4064 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4065 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4066 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4067 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4068 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4069 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004070 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4071 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4072 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4073 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4074 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4075 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4076 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004077 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4078 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4079 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4080 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004081};
4082
Joerg Roedel80612522011-04-04 12:39:33 +02004083#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004084#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004085#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004086
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004087static int svm_check_intercept(struct kvm_vcpu *vcpu,
4088 struct x86_instruction_info *info,
4089 enum x86_intercept_stage stage)
4090{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004091 struct vcpu_svm *svm = to_svm(vcpu);
4092 int vmexit, ret = X86EMUL_CONTINUE;
4093 struct __x86_intercept icpt_info;
4094 struct vmcb *vmcb = svm->vmcb;
4095
4096 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4097 goto out;
4098
4099 icpt_info = x86_intercept_map[info->intercept];
4100
Avi Kivity40e19b52011-04-21 12:35:41 +03004101 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004102 goto out;
4103
4104 switch (icpt_info.exit_code) {
4105 case SVM_EXIT_READ_CR0:
4106 if (info->intercept == x86_intercept_cr_read)
4107 icpt_info.exit_code += info->modrm_reg;
4108 break;
4109 case SVM_EXIT_WRITE_CR0: {
4110 unsigned long cr0, val;
4111 u64 intercept;
4112
4113 if (info->intercept == x86_intercept_cr_write)
4114 icpt_info.exit_code += info->modrm_reg;
4115
4116 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0)
4117 break;
4118
4119 intercept = svm->nested.intercept;
4120
4121 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
4122 break;
4123
4124 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4125 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4126
4127 if (info->intercept == x86_intercept_lmsw) {
4128 cr0 &= 0xfUL;
4129 val &= 0xfUL;
4130 /* lmsw can't clear PE - catch this here */
4131 if (cr0 & X86_CR0_PE)
4132 val |= X86_CR0_PE;
4133 }
4134
4135 if (cr0 ^ val)
4136 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4137
4138 break;
4139 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004140 case SVM_EXIT_READ_DR0:
4141 case SVM_EXIT_WRITE_DR0:
4142 icpt_info.exit_code += info->modrm_reg;
4143 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004144 case SVM_EXIT_MSR:
4145 if (info->intercept == x86_intercept_wrmsr)
4146 vmcb->control.exit_info_1 = 1;
4147 else
4148 vmcb->control.exit_info_1 = 0;
4149 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004150 case SVM_EXIT_PAUSE:
4151 /*
4152 * We get this for NOP only, but pause
4153 * is rep not, check this here
4154 */
4155 if (info->rep_prefix != REPE_PREFIX)
4156 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02004157 case SVM_EXIT_IOIO: {
4158 u64 exit_info;
4159 u32 bytes;
4160
4161 exit_info = (vcpu->arch.regs[VCPU_REGS_RDX] & 0xffff) << 16;
4162
4163 if (info->intercept == x86_intercept_in ||
4164 info->intercept == x86_intercept_ins) {
4165 exit_info |= SVM_IOIO_TYPE_MASK;
4166 bytes = info->src_bytes;
4167 } else {
4168 bytes = info->dst_bytes;
4169 }
4170
4171 if (info->intercept == x86_intercept_outs ||
4172 info->intercept == x86_intercept_ins)
4173 exit_info |= SVM_IOIO_STR_MASK;
4174
4175 if (info->rep_prefix)
4176 exit_info |= SVM_IOIO_REP_MASK;
4177
4178 bytes = min(bytes, 4u);
4179
4180 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4181
4182 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4183
4184 vmcb->control.exit_info_1 = exit_info;
4185 vmcb->control.exit_info_2 = info->next_rip;
4186
4187 break;
4188 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004189 default:
4190 break;
4191 }
4192
4193 vmcb->control.next_rip = info->next_rip;
4194 vmcb->control.exit_code = icpt_info.exit_code;
4195 vmexit = nested_svm_exit_handled(svm);
4196
4197 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4198 : X86EMUL_CONTINUE;
4199
4200out:
4201 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004202}
4203
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004204static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004205 .cpu_has_kvm_support = has_svm,
4206 .disabled_by_bios = is_disabled,
4207 .hardware_setup = svm_hardware_setup,
4208 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004209 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004210 .hardware_enable = svm_hardware_enable,
4211 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004212 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004213
4214 .vcpu_create = svm_create_vcpu,
4215 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004216 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004217
Avi Kivity04d2cc72007-09-10 18:10:54 +03004218 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004219 .vcpu_load = svm_vcpu_load,
4220 .vcpu_put = svm_vcpu_put,
4221
4222 .set_guest_debug = svm_guest_debug,
4223 .get_msr = svm_get_msr,
4224 .set_msr = svm_set_msr,
4225 .get_segment_base = svm_get_segment_base,
4226 .get_segment = svm_get_segment,
4227 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004228 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004229 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004230 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004231 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004232 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004233 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004234 .set_cr3 = svm_set_cr3,
4235 .set_cr4 = svm_set_cr4,
4236 .set_efer = svm_set_efer,
4237 .get_idt = svm_get_idt,
4238 .set_idt = svm_set_idt,
4239 .get_gdt = svm_get_gdt,
4240 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004241 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004242 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004243 .get_rflags = svm_get_rflags,
4244 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02004245 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004246 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004247
Avi Kivity6aa8b732006-12-10 02:21:36 -08004248 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004249
Avi Kivity6aa8b732006-12-10 02:21:36 -08004250 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004251 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004252 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004253 .set_interrupt_shadow = svm_set_interrupt_shadow,
4254 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004255 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004256 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004257 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004258 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004259 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004260 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004261 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004262 .get_nmi_mask = svm_get_nmi_mask,
4263 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004264 .enable_nmi_window = enable_nmi_window,
4265 .enable_irq_window = enable_irq_window,
4266 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02004267
4268 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004269 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004270 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004271
Avi Kivity586f9602010-11-18 13:09:54 +02004272 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004273
Sheng Yang17cc3932010-01-05 19:02:27 +08004274 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004275
4276 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004277
4278 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004279
4280 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004281
4282 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004283
Joerg Roedel4051b182011-03-25 09:44:49 +01004284 .set_tsc_khz = svm_set_tsc_khz,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004285 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004286 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01004287 .compute_tsc_offset = svm_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03004288 .read_l1_tsc = svm_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004289
4290 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004291
4292 .check_intercept = svm_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004293};
4294
4295static int __init svm_init(void)
4296{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004297 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004298 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004299}
4300
4301static void __exit svm_exit(void)
4302{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004303 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004304}
4305
4306module_init(svm_init)
4307module_exit(svm_exit)