blob: f3167208562e90240712794229a603c8f7bb9f3f [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 Roedel1018faa2012-02-29 14:57:32 +010032#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020033#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040034#include <asm/desc.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020035#include <asm/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080036
Eduardo Habkost63d11422008-11-17 19:03:20 -020037#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030038#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020039
Avi Kivity4ecac3f2008-05-13 13:23:38 +030040#define __ex(x) __kvm_handle_fault_on_reboot(x)
41
Avi Kivity6aa8b732006-12-10 02:21:36 -080042MODULE_AUTHOR("Qumranet");
43MODULE_LICENSE("GPL");
44
45#define IOPM_ALLOC_ORDER 2
46#define MSRPM_ALLOC_ORDER 1
47
Avi Kivity6aa8b732006-12-10 02:21:36 -080048#define SEG_TYPE_LDT 2
49#define SEG_TYPE_BUSY_TSS16 3
50
Andre Przywara6bc31bd2010-04-11 23:07:28 +020051#define SVM_FEATURE_NPT (1 << 0)
52#define SVM_FEATURE_LBRV (1 << 1)
53#define SVM_FEATURE_SVML (1 << 2)
54#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010055#define SVM_FEATURE_TSC_RATE (1 << 4)
56#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
57#define SVM_FEATURE_FLUSH_ASID (1 << 6)
58#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020059#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030060
Joerg Roedel410e4d52009-08-07 11:49:44 +020061#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
62#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
63#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
64
Joerg Roedel24e09cb2008-02-13 18:58:47 +010065#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
66
Joerg Roedelfbc0db72011-03-25 09:44:46 +010067#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010068#define TSC_RATIO_MIN 0x0000000000000001ULL
69#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010070
Joerg Roedel67ec6602010-05-17 14:43:35 +020071static bool erratum_383_found __read_mostly;
72
Avi Kivity6c8166a2009-05-31 18:15:37 +030073static const u32 host_save_user_msrs[] = {
74#ifdef CONFIG_X86_64
75 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
76 MSR_FS_BASE,
77#endif
78 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
79};
80
81#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
82
83struct kvm_vcpu;
84
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020085struct nested_state {
86 struct vmcb *hsave;
87 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010088 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020089 u64 vmcb;
90
91 /* These are the merged vectors */
92 u32 *msrpm;
93
94 /* gpa pointers to the real vectors */
95 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +010096 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020097
Joerg Roedelcd3ff652009-10-09 16:08:26 +020098 /* A VMEXIT is required but not yet emulated */
99 bool exit_required;
100
Joerg Roedelaad42c62009-08-07 11:49:34 +0200101 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100102 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100103 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200104 u32 intercept_exceptions;
105 u64 intercept;
106
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200107 /* Nested Paging related state */
108 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200109};
110
Joerg Roedel323c3d82010-03-01 15:34:37 +0100111#define MSRPM_OFFSETS 16
112static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
113
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500114/*
115 * Set osvw_len to higher value when updated Revision Guides
116 * are published and we know what the new status bits are
117 */
118static uint64_t osvw_len = 4, osvw_status;
119
Avi Kivity6c8166a2009-05-31 18:15:37 +0300120struct vcpu_svm {
121 struct kvm_vcpu vcpu;
122 struct vmcb *vmcb;
123 unsigned long vmcb_pa;
124 struct svm_cpu_data *svm_data;
125 uint64_t asid_generation;
126 uint64_t sysenter_esp;
127 uint64_t sysenter_eip;
128
129 u64 next_rip;
130
131 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200132 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200133 u16 fs;
134 u16 gs;
135 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200136 u64 gs_base;
137 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300138
139 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300140
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200141 ulong nmi_iret_rip;
142
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200143 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200144
145 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100146
147 unsigned int3_injected;
148 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200149 u32 apf_reason;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100150
151 u64 tsc_ratio;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300152};
153
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100154static DEFINE_PER_CPU(u64, current_tsc_ratio);
155#define TSC_RATIO_DEFAULT 0x0100000000ULL
156
Joerg Roedel455716f2010-03-01 15:34:35 +0100157#define MSR_INVALID 0xffffffffU
158
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100159static struct svm_direct_access_msrs {
160 u32 index; /* Index of the MSR */
161 bool always; /* True if intercept is always on */
162} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400163 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100164 { .index = MSR_IA32_SYSENTER_CS, .always = true },
165#ifdef CONFIG_X86_64
166 { .index = MSR_GS_BASE, .always = true },
167 { .index = MSR_FS_BASE, .always = true },
168 { .index = MSR_KERNEL_GS_BASE, .always = true },
169 { .index = MSR_LSTAR, .always = true },
170 { .index = MSR_CSTAR, .always = true },
171 { .index = MSR_SYSCALL_MASK, .always = true },
172#endif
173 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
174 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
175 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
176 { .index = MSR_IA32_LASTINTTOIP, .always = false },
177 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800178};
179
180/* enable NPT for AMD64 and X86 with PAE */
181#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
182static bool npt_enabled = true;
183#else
Joerg Roedele0231712010-02-24 18:59:10 +0100184static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185#endif
186
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100187/* allow nested paging (virtualized MMU) for all guests */
188static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189module_param(npt, int, S_IRUGO);
190
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100191/* allow nested virtualization in KVM/SVM */
192static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193module_param(nested, int, S_IRUGO);
194
195static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200196static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197
Joerg Roedel410e4d52009-08-07 11:49:44 +0200198static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100199static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
202 bool has_error_code, u32 error_code);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100203static u64 __scale_tsc(u64 ratio, u64 tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800204
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100205enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100206 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
207 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100208 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100209 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100210 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100211 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100212 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100213 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100214 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100215 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100216 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100217 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100218 VMCB_DIRTY_MAX,
219};
220
Joerg Roedel0574dec2010-12-03 11:45:58 +0100221/* TPR and CR2 are always written before VMRUN */
222#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100223
224static inline void mark_all_dirty(struct vmcb *vmcb)
225{
226 vmcb->control.clean = 0;
227}
228
229static inline void mark_all_clean(struct vmcb *vmcb)
230{
231 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
232 & ~VMCB_ALWAYS_DIRTY_MASK;
233}
234
235static inline void mark_dirty(struct vmcb *vmcb, int bit)
236{
237 vmcb->control.clean &= ~(1 << bit);
238}
239
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
241{
242 return container_of(vcpu, struct vcpu_svm, vcpu);
243}
244
Joerg Roedel384c6362010-11-30 18:03:56 +0100245static void recalc_intercepts(struct vcpu_svm *svm)
246{
247 struct vmcb_control_area *c, *h;
248 struct nested_state *g;
249
Joerg Roedel116a0a22010-12-03 11:45:49 +0100250 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
251
Joerg Roedel384c6362010-11-30 18:03:56 +0100252 if (!is_guest_mode(&svm->vcpu))
253 return;
254
255 c = &svm->vmcb->control;
256 h = &svm->nested.hsave->control;
257 g = &svm->nested;
258
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100259 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100260 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100261 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
262 c->intercept = h->intercept | g->intercept;
263}
264
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100265static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
266{
267 if (is_guest_mode(&svm->vcpu))
268 return svm->nested.hsave;
269 else
270 return svm->vmcb;
271}
272
273static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
274{
275 struct vmcb *vmcb = get_host_vmcb(svm);
276
277 vmcb->control.intercept_cr |= (1U << bit);
278
279 recalc_intercepts(svm);
280}
281
282static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
283{
284 struct vmcb *vmcb = get_host_vmcb(svm);
285
286 vmcb->control.intercept_cr &= ~(1U << bit);
287
288 recalc_intercepts(svm);
289}
290
291static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
292{
293 struct vmcb *vmcb = get_host_vmcb(svm);
294
295 return vmcb->control.intercept_cr & (1U << bit);
296}
297
Joerg Roedel3aed0412010-11-30 18:03:58 +0100298static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
299{
300 struct vmcb *vmcb = get_host_vmcb(svm);
301
302 vmcb->control.intercept_dr |= (1U << bit);
303
304 recalc_intercepts(svm);
305}
306
307static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
308{
309 struct vmcb *vmcb = get_host_vmcb(svm);
310
311 vmcb->control.intercept_dr &= ~(1U << bit);
312
313 recalc_intercepts(svm);
314}
315
Joerg Roedel18c918c2010-11-30 18:03:59 +0100316static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
317{
318 struct vmcb *vmcb = get_host_vmcb(svm);
319
320 vmcb->control.intercept_exceptions |= (1U << bit);
321
322 recalc_intercepts(svm);
323}
324
325static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
326{
327 struct vmcb *vmcb = get_host_vmcb(svm);
328
329 vmcb->control.intercept_exceptions &= ~(1U << bit);
330
331 recalc_intercepts(svm);
332}
333
Joerg Roedel8a05a1b2010-11-30 18:04:00 +0100334static inline void set_intercept(struct vcpu_svm *svm, int bit)
335{
336 struct vmcb *vmcb = get_host_vmcb(svm);
337
338 vmcb->control.intercept |= (1ULL << bit);
339
340 recalc_intercepts(svm);
341}
342
343static inline void clr_intercept(struct vcpu_svm *svm, int bit)
344{
345 struct vmcb *vmcb = get_host_vmcb(svm);
346
347 vmcb->control.intercept &= ~(1ULL << bit);
348
349 recalc_intercepts(svm);
350}
351
Joerg Roedel2af91942009-08-07 11:49:28 +0200352static inline void enable_gif(struct vcpu_svm *svm)
353{
354 svm->vcpu.arch.hflags |= HF_GIF_MASK;
355}
356
357static inline void disable_gif(struct vcpu_svm *svm)
358{
359 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
360}
361
362static inline bool gif_set(struct vcpu_svm *svm)
363{
364 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
365}
366
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367static unsigned long iopm_base;
368
369struct kvm_ldttss_desc {
370 u16 limit0;
371 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100372 unsigned base1:8, type:5, dpl:2, p:1;
373 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374 u32 base3;
375 u32 zero1;
376} __attribute__((packed));
377
378struct svm_cpu_data {
379 int cpu;
380
Avi Kivity5008fdf2007-04-02 13:05:50 +0300381 u64 asid_generation;
382 u32 max_asid;
383 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384 struct kvm_ldttss_desc *tss_desc;
385
386 struct page *save_area;
387};
388
389static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
390
391struct svm_init_data {
392 int cpu;
393 int r;
394};
395
396static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
397
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200398#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399#define MSRS_RANGE_SIZE 2048
400#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
401
Joerg Roedel455716f2010-03-01 15:34:35 +0100402static u32 svm_msrpm_offset(u32 msr)
403{
404 u32 offset;
405 int i;
406
407 for (i = 0; i < NUM_MSR_MAPS; i++) {
408 if (msr < msrpm_ranges[i] ||
409 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
410 continue;
411
412 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
413 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
414
415 /* Now we have the u8 offset - but need the u32 offset */
416 return offset / 4;
417 }
418
419 /* MSR not in any range */
420 return MSR_INVALID;
421}
422
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423#define MAX_INST_SIZE 15
424
Avi Kivity6aa8b732006-12-10 02:21:36 -0800425static inline void clgi(void)
426{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300427 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428}
429
430static inline void stgi(void)
431{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300432 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433}
434
435static inline void invlpga(unsigned long addr, u32 asid)
436{
Joerg Roedele0231712010-02-24 18:59:10 +0100437 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438}
439
Joerg Roedel4b161842010-09-10 17:31:03 +0200440static int get_npt_level(void)
441{
442#ifdef CONFIG_X86_64
443 return PT64_ROOT_LEVEL;
444#else
445 return PT32E_ROOT_LEVEL;
446#endif
447}
448
Avi Kivity6aa8b732006-12-10 02:21:36 -0800449static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
450{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000451 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100452 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600453 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454
Alexander Graf9962d032008-11-25 20:17:02 +0100455 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100456 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457}
458
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459static int is_external_interrupt(u32 info)
460{
461 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
462 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
463}
464
Glauber Costa2809f5d2009-05-12 16:21:05 -0400465static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
466{
467 struct vcpu_svm *svm = to_svm(vcpu);
468 u32 ret = 0;
469
470 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100471 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400472 return ret & mask;
473}
474
475static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
476{
477 struct vcpu_svm *svm = to_svm(vcpu);
478
479 if (mask == 0)
480 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
481 else
482 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
483
484}
485
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
487{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400488 struct vcpu_svm *svm = to_svm(vcpu);
489
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200490 if (svm->vmcb->control.next_rip != 0)
491 svm->next_rip = svm->vmcb->control.next_rip;
492
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400493 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100494 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300495 EMULATE_DONE)
496 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 return;
498 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300499 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
500 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
501 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300503 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400504 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505}
506
Jan Kiszka116a4752010-02-23 17:47:54 +0100507static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200508 bool has_error_code, u32 error_code,
509 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100510{
511 struct vcpu_svm *svm = to_svm(vcpu);
512
Joerg Roedele0231712010-02-24 18:59:10 +0100513 /*
514 * If we are within a nested VM we'd better #VMEXIT and let the guest
515 * handle the exception
516 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200517 if (!reinject &&
518 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100519 return;
520
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200521 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100522 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
523
524 /*
525 * For guest debugging where we have to reinject #BP if some
526 * INT3 is guest-owned:
527 * Emulate nRIP by moving RIP forward. Will fail if injection
528 * raises a fault that is not intercepted. Still better than
529 * failing in all cases.
530 */
531 skip_emulated_instruction(&svm->vcpu);
532 rip = kvm_rip_read(&svm->vcpu);
533 svm->int3_rip = rip + svm->vmcb->save.cs.base;
534 svm->int3_injected = rip - old_rip;
535 }
536
Jan Kiszka116a4752010-02-23 17:47:54 +0100537 svm->vmcb->control.event_inj = nr
538 | SVM_EVTINJ_VALID
539 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
540 | SVM_EVTINJ_TYPE_EXEPT;
541 svm->vmcb->control.event_inj_err = error_code;
542}
543
Joerg Roedel67ec6602010-05-17 14:43:35 +0200544static void svm_init_erratum_383(void)
545{
546 u32 low, high;
547 int err;
548 u64 val;
549
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200550 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200551 return;
552
553 /* Use _safe variants to not break nested virtualization */
554 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
555 if (err)
556 return;
557
558 val |= (1ULL << 47);
559
560 low = lower_32_bits(val);
561 high = upper_32_bits(val);
562
563 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
564
565 erratum_383_found = true;
566}
567
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500568static void svm_init_osvw(struct kvm_vcpu *vcpu)
569{
570 /*
571 * Guests should see errata 400 and 415 as fixed (assuming that
572 * HLT and IO instructions are intercepted).
573 */
574 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
575 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
576
577 /*
578 * By increasing VCPU's osvw.length to 3 we are telling the guest that
579 * all osvw.status bits inside that length, including bit 0 (which is
580 * reserved for erratum 298), are valid. However, if host processor's
581 * osvw_len is 0 then osvw_status[0] carries no information. We need to
582 * be conservative here and therefore we tell the guest that erratum 298
583 * is present (because we really don't know).
584 */
585 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
586 vcpu->arch.osvw.status |= 1;
587}
588
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589static int has_svm(void)
590{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200591 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592
Eduardo Habkost63d11422008-11-17 19:03:20 -0200593 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800594 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595 return 0;
596 }
597
Avi Kivity6aa8b732006-12-10 02:21:36 -0800598 return 1;
599}
600
601static void svm_hardware_disable(void *garbage)
602{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100603 /* Make sure we clean up behind us */
604 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
605 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
606
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200607 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100608
609 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800610}
611
Alexander Graf10474ae2009-09-15 11:37:46 +0200612static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613{
614
Tejun Heo0fe1e002009-10-29 22:34:14 +0900615 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800616 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200617 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800618 struct desc_struct *gdt;
619 int me = raw_smp_processor_id();
620
Alexander Graf10474ae2009-09-15 11:37:46 +0200621 rdmsrl(MSR_EFER, efer);
622 if (efer & EFER_SVME)
623 return -EBUSY;
624
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000626 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
627 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200628 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900630 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631
Tejun Heo0fe1e002009-10-29 22:34:14 +0900632 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000633 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200635 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636 }
637
Tejun Heo0fe1e002009-10-29 22:34:14 +0900638 sd->asid_generation = 1;
639 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
640 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200642 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200643 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900644 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645
Alexander Graf9962d032008-11-25 20:17:02 +0100646 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647
Linus Torvaldsd0316552009-12-14 09:58:24 -0800648 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200649
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100650 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
651 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
652 __get_cpu_var(current_tsc_ratio) = TSC_RATIO_DEFAULT;
653 }
654
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500655
656 /*
657 * Get OSVW bits.
658 *
659 * Note that it is possible to have a system with mixed processor
660 * revisions and therefore different OSVW bits. If bits are not the same
661 * on different processors then choose the worst case (i.e. if erratum
662 * is present on one processor and not on another then assume that the
663 * erratum is present everywhere).
664 */
665 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
666 uint64_t len, status = 0;
667 int err;
668
669 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
670 if (!err)
671 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
672 &err);
673
674 if (err)
675 osvw_status = osvw_len = 0;
676 else {
677 if (len < osvw_len)
678 osvw_len = len;
679 osvw_status |= status;
680 osvw_status &= (1ULL << osvw_len) - 1;
681 }
682 } else
683 osvw_status = osvw_len = 0;
684
Joerg Roedel67ec6602010-05-17 14:43:35 +0200685 svm_init_erratum_383();
686
Joerg Roedel1018faa2012-02-29 14:57:32 +0100687 amd_pmu_enable_virt();
688
Alexander Graf10474ae2009-09-15 11:37:46 +0200689 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690}
691
Joerg Roedel0da1db752008-07-02 16:02:11 +0200692static void svm_cpu_uninit(int cpu)
693{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900694 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200695
Tejun Heo0fe1e002009-10-29 22:34:14 +0900696 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200697 return;
698
699 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900700 __free_page(sd->save_area);
701 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200702}
703
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704static int svm_cpu_init(int cpu)
705{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900706 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707 int r;
708
Tejun Heo0fe1e002009-10-29 22:34:14 +0900709 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
710 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900712 sd->cpu = cpu;
713 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900715 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 goto err_1;
717
Tejun Heo0fe1e002009-10-29 22:34:14 +0900718 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719
720 return 0;
721
722err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900723 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724 return r;
725
726}
727
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100728static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729{
730 int i;
731
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100732 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
733 if (direct_access_msrs[i].index == index)
734 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100736 return false;
737}
738
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739static void set_msr_interception(u32 *msrpm, unsigned msr,
740 int read, int write)
741{
Joerg Roedel455716f2010-03-01 15:34:35 +0100742 u8 bit_read, bit_write;
743 unsigned long tmp;
744 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100746 /*
747 * If this warning triggers extend the direct_access_msrs list at the
748 * beginning of the file
749 */
750 WARN_ON(!valid_msr_intercept(msr));
751
Joerg Roedel455716f2010-03-01 15:34:35 +0100752 offset = svm_msrpm_offset(msr);
753 bit_read = 2 * (msr & 0x0f);
754 bit_write = 2 * (msr & 0x0f) + 1;
755 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756
Joerg Roedel455716f2010-03-01 15:34:35 +0100757 BUG_ON(offset == MSR_INVALID);
758
759 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
760 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
761
762 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763}
764
Joerg Roedelf65c2292008-02-13 18:58:46 +0100765static void svm_vcpu_init_msrpm(u32 *msrpm)
766{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100767 int i;
768
Joerg Roedelf65c2292008-02-13 18:58:46 +0100769 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
770
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100771 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
772 if (!direct_access_msrs[i].always)
773 continue;
774
775 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
776 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100777}
778
Joerg Roedel323c3d82010-03-01 15:34:37 +0100779static void add_msr_offset(u32 offset)
780{
781 int i;
782
783 for (i = 0; i < MSRPM_OFFSETS; ++i) {
784
785 /* Offset already in list? */
786 if (msrpm_offsets[i] == offset)
787 return;
788
789 /* Slot used by another offset? */
790 if (msrpm_offsets[i] != MSR_INVALID)
791 continue;
792
793 /* Add offset to list */
794 msrpm_offsets[i] = offset;
795
796 return;
797 }
798
799 /*
800 * If this BUG triggers the msrpm_offsets table has an overflow. Just
801 * increase MSRPM_OFFSETS in this case.
802 */
803 BUG();
804}
805
806static void init_msrpm_offsets(void)
807{
808 int i;
809
810 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
811
812 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
813 u32 offset;
814
815 offset = svm_msrpm_offset(direct_access_msrs[i].index);
816 BUG_ON(offset == MSR_INVALID);
817
818 add_msr_offset(offset);
819 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820}
821
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100822static void svm_enable_lbrv(struct vcpu_svm *svm)
823{
824 u32 *msrpm = svm->msrpm;
825
826 svm->vmcb->control.lbr_ctl = 1;
827 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
828 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
829 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
830 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
831}
832
833static void svm_disable_lbrv(struct vcpu_svm *svm)
834{
835 u32 *msrpm = svm->msrpm;
836
837 svm->vmcb->control.lbr_ctl = 0;
838 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
839 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
840 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
841 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
842}
843
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844static __init int svm_hardware_setup(void)
845{
846 int cpu;
847 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100848 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849 int r;
850
Avi Kivity6aa8b732006-12-10 02:21:36 -0800851 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
852
853 if (!iopm_pages)
854 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300855
856 iopm_va = page_address(iopm_pages);
857 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
859
Joerg Roedel323c3d82010-03-01 15:34:37 +0100860 init_msrpm_offsets();
861
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100862 if (boot_cpu_has(X86_FEATURE_NX))
863 kvm_enable_efer_bits(EFER_NX);
864
Alexander Graf1b2fd702009-02-02 16:23:51 +0100865 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
866 kvm_enable_efer_bits(EFER_FFXSR);
867
Joerg Roedel92a1f122011-03-25 09:44:51 +0100868 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
869 u64 max;
870
871 kvm_has_tsc_control = true;
872
873 /*
874 * Make sure the user can only configure tsc_khz values that
875 * fit into a signed integer.
876 * A min value is not calculated needed because it will always
877 * be 1 on all machines and a value of 0 is used to disable
878 * tsc-scaling for the vcpu.
879 */
880 max = min(0x7fffffffULL, __scale_tsc(tsc_khz, TSC_RATIO_MAX));
881
882 kvm_max_guest_tsc_khz = max;
883 }
884
Alexander Graf236de052008-11-25 20:17:10 +0100885 if (nested) {
886 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200887 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100888 }
889
Zachary Amsden3230bb42009-09-29 11:38:37 -1000890 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 r = svm_cpu_init(cpu);
892 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100893 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100895
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200896 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100897 npt_enabled = false;
898
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100899 if (npt_enabled && !npt) {
900 printk(KERN_INFO "kvm: Nested Paging disabled\n");
901 npt_enabled = false;
902 }
903
Joerg Roedel18552672008-02-07 13:47:41 +0100904 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100905 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100906 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200907 } else
908 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100909
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 return 0;
911
Joerg Roedelf65c2292008-02-13 18:58:46 +0100912err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
914 iopm_base = 0;
915 return r;
916}
917
918static __exit void svm_hardware_unsetup(void)
919{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200920 int cpu;
921
Zachary Amsden3230bb42009-09-29 11:38:37 -1000922 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200923 svm_cpu_uninit(cpu);
924
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100926 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927}
928
929static void init_seg(struct vmcb_seg *seg)
930{
931 seg->selector = 0;
932 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100933 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934 seg->limit = 0xffff;
935 seg->base = 0;
936}
937
938static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
939{
940 seg->selector = 0;
941 seg->attrib = SVM_SELECTOR_P_MASK | type;
942 seg->limit = 0xffff;
943 seg->base = 0;
944}
945
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100946static u64 __scale_tsc(u64 ratio, u64 tsc)
947{
948 u64 mult, frac, _tsc;
949
950 mult = ratio >> 32;
951 frac = ratio & ((1ULL << 32) - 1);
952
953 _tsc = tsc;
954 _tsc *= mult;
955 _tsc += (tsc >> 32) * frac;
956 _tsc += ((tsc & ((1ULL << 32) - 1)) * frac) >> 32;
957
958 return _tsc;
959}
960
961static u64 svm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
962{
963 struct vcpu_svm *svm = to_svm(vcpu);
964 u64 _tsc = tsc;
965
966 if (svm->tsc_ratio != TSC_RATIO_DEFAULT)
967 _tsc = __scale_tsc(svm->tsc_ratio, tsc);
968
969 return _tsc;
970}
971
Zachary Amsdencc578282012-02-03 15:43:50 -0200972static void svm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +0100973{
974 struct vcpu_svm *svm = to_svm(vcpu);
975 u64 ratio;
976 u64 khz;
977
Zachary Amsdencc578282012-02-03 15:43:50 -0200978 /* Guest TSC same frequency as host TSC? */
979 if (!scale) {
Joerg Roedel4051b182011-03-25 09:44:49 +0100980 svm->tsc_ratio = TSC_RATIO_DEFAULT;
981 return;
982 }
983
Zachary Amsdencc578282012-02-03 15:43:50 -0200984 /* TSC scaling supported? */
985 if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
986 if (user_tsc_khz > tsc_khz) {
987 vcpu->arch.tsc_catchup = 1;
988 vcpu->arch.tsc_always_catchup = 1;
989 } else
990 WARN(1, "user requested TSC rate below hardware speed\n");
991 return;
992 }
993
Joerg Roedel4051b182011-03-25 09:44:49 +0100994 khz = user_tsc_khz;
995
996 /* TSC scaling required - calculate ratio */
997 ratio = khz << 32;
998 do_div(ratio, tsc_khz);
999
1000 if (ratio == 0 || ratio & TSC_RATIO_RSVD) {
1001 WARN_ONCE(1, "Invalid TSC ratio - virtual-tsc-khz=%u\n",
1002 user_tsc_khz);
1003 return;
1004 }
Joerg Roedel4051b182011-03-25 09:44:49 +01001005 svm->tsc_ratio = ratio;
1006}
1007
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001008static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1009{
1010 struct vcpu_svm *svm = to_svm(vcpu);
1011 u64 g_tsc_offset = 0;
1012
Joerg Roedel20307532010-11-29 17:51:48 +01001013 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001014 g_tsc_offset = svm->vmcb->control.tsc_offset -
1015 svm->nested.hsave->control.tsc_offset;
1016 svm->nested.hsave->control.tsc_offset = offset;
1017 }
1018
1019 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001020
1021 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001022}
1023
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001024static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10001025{
1026 struct vcpu_svm *svm = to_svm(vcpu);
1027
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001028 WARN_ON(adjustment < 0);
1029 if (host)
1030 adjustment = svm_scale_tsc(vcpu, adjustment);
1031
Zachary Amsdene48672f2010-08-19 22:07:23 -10001032 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +01001033 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -10001034 svm->nested.hsave->control.tsc_offset += adjustment;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001035 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001036}
1037
Joerg Roedel857e4092011-03-25 09:44:50 +01001038static u64 svm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1039{
1040 u64 tsc;
1041
1042 tsc = svm_scale_tsc(vcpu, native_read_tsc());
1043
1044 return target_tsc - tsc;
1045}
1046
Joerg Roedele6101a92008-02-13 18:58:45 +01001047static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048{
Joerg Roedele6101a92008-02-13 18:58:45 +01001049 struct vmcb_control_area *control = &svm->vmcb->control;
1050 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001051
Avi Kivitybff78272010-01-07 13:16:08 +02001052 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001053 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001054
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001055 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1056 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1057 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1058 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1059 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1060 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
1061 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062
Joerg Roedel3aed0412010-11-30 18:03:58 +01001063 set_dr_intercept(svm, INTERCEPT_DR0_READ);
1064 set_dr_intercept(svm, INTERCEPT_DR1_READ);
1065 set_dr_intercept(svm, INTERCEPT_DR2_READ);
1066 set_dr_intercept(svm, INTERCEPT_DR3_READ);
1067 set_dr_intercept(svm, INTERCEPT_DR4_READ);
1068 set_dr_intercept(svm, INTERCEPT_DR5_READ);
1069 set_dr_intercept(svm, INTERCEPT_DR6_READ);
1070 set_dr_intercept(svm, INTERCEPT_DR7_READ);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001071
Joerg Roedel3aed0412010-11-30 18:03:58 +01001072 set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
1073 set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
1074 set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
1075 set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
1076 set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
1077 set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
1078 set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
1079 set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080
Joerg Roedel18c918c2010-11-30 18:03:59 +01001081 set_exception_intercept(svm, PF_VECTOR);
1082 set_exception_intercept(svm, UD_VECTOR);
1083 set_exception_intercept(svm, MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001085 set_intercept(svm, INTERCEPT_INTR);
1086 set_intercept(svm, INTERCEPT_NMI);
1087 set_intercept(svm, INTERCEPT_SMI);
1088 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001089 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001090 set_intercept(svm, INTERCEPT_CPUID);
1091 set_intercept(svm, INTERCEPT_INVD);
1092 set_intercept(svm, INTERCEPT_HLT);
1093 set_intercept(svm, INTERCEPT_INVLPG);
1094 set_intercept(svm, INTERCEPT_INVLPGA);
1095 set_intercept(svm, INTERCEPT_IOIO_PROT);
1096 set_intercept(svm, INTERCEPT_MSR_PROT);
1097 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1098 set_intercept(svm, INTERCEPT_SHUTDOWN);
1099 set_intercept(svm, INTERCEPT_VMRUN);
1100 set_intercept(svm, INTERCEPT_VMMCALL);
1101 set_intercept(svm, INTERCEPT_VMLOAD);
1102 set_intercept(svm, INTERCEPT_VMSAVE);
1103 set_intercept(svm, INTERCEPT_STGI);
1104 set_intercept(svm, INTERCEPT_CLGI);
1105 set_intercept(svm, INTERCEPT_SKINIT);
1106 set_intercept(svm, INTERCEPT_WBINVD);
1107 set_intercept(svm, INTERCEPT_MONITOR);
1108 set_intercept(svm, INTERCEPT_MWAIT);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001109 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110
1111 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001112 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 control->int_ctl = V_INTR_MASKING_MASK;
1114
1115 init_seg(&save->es);
1116 init_seg(&save->ss);
1117 init_seg(&save->ds);
1118 init_seg(&save->fs);
1119 init_seg(&save->gs);
1120
1121 save->cs.selector = 0xf000;
1122 /* Executable/Readable Code Segment */
1123 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1124 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1125 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -08001126 /*
1127 * cs.base should really be 0xffff0000, but vmx can't handle that, so
1128 * be consistent with it.
1129 *
1130 * Replace when we have real mode working for vmx.
1131 */
1132 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133
1134 save->gdtr.limit = 0xffff;
1135 save->idtr.limit = 0xffff;
1136
1137 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1138 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1139
Marcelo Tosattieaa48512010-08-31 19:13:14 -03001140 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001141 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 save->dr7 = 0x400;
Avi Kivityf6e78472010-08-02 15:30:20 +03001143 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001145 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146
Joerg Roedele0231712010-02-24 18:59:10 +01001147 /*
1148 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001149 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -03001151 svm->vcpu.arch.cr0 = 0;
1152 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001153
Rusty Russell66aee912007-07-17 23:34:16 +10001154 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001156
1157 if (npt_enabled) {
1158 /* Setup VMCB for Nested Paging */
1159 control->nested_ctl = 1;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001160 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001161 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001162 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1163 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001164 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001165 save->cr3 = 0;
1166 save->cr4 = 0;
1167 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001168 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001169
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001170 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001171 svm->vcpu.arch.hflags = 0;
1172
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001173 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001174 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001175 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001176 }
1177
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001178 mark_all_dirty(svm->vmcb);
1179
Joerg Roedel2af91942009-08-07 11:49:28 +02001180 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181}
1182
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001183static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001184{
1185 struct vcpu_svm *svm = to_svm(vcpu);
1186
Joerg Roedele6101a92008-02-13 18:58:45 +01001187 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001188
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001189 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001190 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001191 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
1192 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +02001193 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001194 vcpu->arch.regs_avail = ~0;
1195 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001196
1197 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001198}
1199
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001200static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001202 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001204 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001205 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001206 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001207 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208
Rusty Russellc16f8622007-07-30 21:12:19 +10001209 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001210 if (!svm) {
1211 err = -ENOMEM;
1212 goto out;
1213 }
1214
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001215 svm->tsc_ratio = TSC_RATIO_DEFAULT;
1216
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001217 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1218 if (err)
1219 goto free_svm;
1220
Joerg Roedelf65c2292008-02-13 18:58:46 +01001221 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001222 page = alloc_page(GFP_KERNEL);
1223 if (!page)
1224 goto uninit;
1225
Joerg Roedelf65c2292008-02-13 18:58:46 +01001226 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1227 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001228 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001229
1230 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1231 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001232 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001233
Alexander Grafb286d5d2008-11-25 20:17:05 +01001234 hsave_page = alloc_page(GFP_KERNEL);
1235 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001236 goto free_page3;
1237
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001238 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001239
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001240 svm->msrpm = page_address(msrpm_pages);
1241 svm_vcpu_init_msrpm(svm->msrpm);
1242
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001243 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001244 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001245
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001246 svm->vmcb = page_address(page);
1247 clear_page(svm->vmcb);
1248 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1249 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +01001250 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001251 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001252
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001253 err = fx_init(&svm->vcpu);
1254 if (err)
1255 goto free_page4;
1256
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001257 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001258 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001259 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001261 svm_init_osvw(&svm->vcpu);
1262
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001263 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001264
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001265free_page4:
1266 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001267free_page3:
1268 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1269free_page2:
1270 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1271free_page1:
1272 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001273uninit:
1274 kvm_vcpu_uninit(&svm->vcpu);
1275free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001276 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001277out:
1278 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001279}
1280
1281static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1282{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001283 struct vcpu_svm *svm = to_svm(vcpu);
1284
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001285 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001286 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001287 __free_page(virt_to_page(svm->nested.hsave));
1288 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001289 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001290 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291}
1292
Avi Kivity15ad7142007-07-11 18:17:21 +03001293static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001295 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001296 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001297
Avi Kivity0cc50642007-03-25 12:07:27 +02001298 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001299 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001300 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001301 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001302
Avi Kivity82ca2d12010-10-21 12:20:34 +02001303#ifdef CONFIG_X86_64
1304 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1305#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001306 savesegment(fs, svm->host.fs);
1307 savesegment(gs, svm->host.gs);
1308 svm->host.ldt = kvm_read_ldt();
1309
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001310 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001311 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001312
1313 if (static_cpu_has(X86_FEATURE_TSCRATEMSR) &&
1314 svm->tsc_ratio != __get_cpu_var(current_tsc_ratio)) {
1315 __get_cpu_var(current_tsc_ratio) = svm->tsc_ratio;
1316 wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_ratio);
1317 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318}
1319
1320static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1321{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001322 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001323 int i;
1324
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001325 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001326 kvm_load_ldt(svm->host.ldt);
1327#ifdef CONFIG_X86_64
1328 loadsegment(fs, svm->host.fs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001329 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001330 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001331#else
Avi Kivity831ca602011-03-08 16:09:51 +02001332#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001333 loadsegment(gs, svm->host.gs);
1334#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001335#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001336 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001337 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338}
1339
Kevin Wolfea5e97e2012-02-08 14:34:40 +01001340static void svm_update_cpl(struct kvm_vcpu *vcpu)
1341{
1342 struct vcpu_svm *svm = to_svm(vcpu);
1343 int cpl;
1344
1345 if (!is_protmode(vcpu))
1346 cpl = 0;
1347 else if (svm->vmcb->save.rflags & X86_EFLAGS_VM)
1348 cpl = 3;
1349 else
1350 cpl = svm->vmcb->save.cs.selector & 0x3;
1351
1352 svm->vmcb->save.cpl = cpl;
1353}
1354
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1356{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001357 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358}
1359
1360static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1361{
Kevin Wolf4cee4792012-02-08 14:34:41 +01001362 unsigned long old_rflags = to_svm(vcpu)->vmcb->save.rflags;
1363
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001364 to_svm(vcpu)->vmcb->save.rflags = rflags;
Kevin Wolf4cee4792012-02-08 14:34:41 +01001365 if ((old_rflags ^ rflags) & X86_EFLAGS_VM)
1366 svm_update_cpl(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367}
1368
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001369static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1370{
1371 switch (reg) {
1372 case VCPU_EXREG_PDPTR:
1373 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001374 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001375 break;
1376 default:
1377 BUG();
1378 }
1379}
1380
Alexander Graff0b85052008-11-25 20:17:01 +01001381static void svm_set_vintr(struct vcpu_svm *svm)
1382{
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001383 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001384}
1385
1386static void svm_clear_vintr(struct vcpu_svm *svm)
1387{
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001388 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001389}
1390
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1392{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001393 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394
1395 switch (seg) {
1396 case VCPU_SREG_CS: return &save->cs;
1397 case VCPU_SREG_DS: return &save->ds;
1398 case VCPU_SREG_ES: return &save->es;
1399 case VCPU_SREG_FS: return &save->fs;
1400 case VCPU_SREG_GS: return &save->gs;
1401 case VCPU_SREG_SS: return &save->ss;
1402 case VCPU_SREG_TR: return &save->tr;
1403 case VCPU_SREG_LDTR: return &save->ldtr;
1404 }
1405 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001406 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407}
1408
1409static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1410{
1411 struct vmcb_seg *s = svm_seg(vcpu, seg);
1412
1413 return s->base;
1414}
1415
1416static void svm_get_segment(struct kvm_vcpu *vcpu,
1417 struct kvm_segment *var, int seg)
1418{
1419 struct vmcb_seg *s = svm_seg(vcpu, seg);
1420
1421 var->base = s->base;
1422 var->limit = s->limit;
1423 var->selector = s->selector;
1424 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1425 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1426 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1427 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1428 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1429 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1430 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1431 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001432
Joerg Roedele0231712010-02-24 18:59:10 +01001433 /*
1434 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001435 * for cross vendor migration purposes by "not present"
1436 */
1437 var->unusable = !var->present || (var->type == 0);
1438
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001439 switch (seg) {
1440 case VCPU_SREG_CS:
1441 /*
1442 * SVM always stores 0 for the 'G' bit in the CS selector in
1443 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1444 * Intel's VMENTRY has a check on the 'G' bit.
1445 */
Amit Shah25022ac2008-10-27 09:04:17 +00001446 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001447 break;
1448 case VCPU_SREG_TR:
1449 /*
1450 * Work around a bug where the busy flag in the tr selector
1451 * isn't exposed
1452 */
Amit Shahc0d09822008-10-27 09:04:18 +00001453 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001454 break;
1455 case VCPU_SREG_DS:
1456 case VCPU_SREG_ES:
1457 case VCPU_SREG_FS:
1458 case VCPU_SREG_GS:
1459 /*
1460 * The accessed bit must always be set in the segment
1461 * descriptor cache, although it can be cleared in the
1462 * descriptor, the cached bit always remains at 1. Since
1463 * Intel has a check on this, set it here to support
1464 * cross-vendor migration.
1465 */
1466 if (!var->unusable)
1467 var->type |= 0x1;
1468 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001469 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001470 /*
1471 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001472 * descriptor is left as 1, although the whole segment has
1473 * been made unusable. Clear it here to pass an Intel VMX
1474 * entry check when cross vendor migrating.
1475 */
1476 if (var->unusable)
1477 var->db = 0;
1478 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001479 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480}
1481
Izik Eidus2e4d2652008-03-24 19:38:34 +02001482static int svm_get_cpl(struct kvm_vcpu *vcpu)
1483{
1484 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1485
1486 return save->cpl;
1487}
1488
Gleb Natapov89a27f42010-02-16 10:51:48 +02001489static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001491 struct vcpu_svm *svm = to_svm(vcpu);
1492
Gleb Natapov89a27f42010-02-16 10:51:48 +02001493 dt->size = svm->vmcb->save.idtr.limit;
1494 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495}
1496
Gleb Natapov89a27f42010-02-16 10:51:48 +02001497static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001499 struct vcpu_svm *svm = to_svm(vcpu);
1500
Gleb Natapov89a27f42010-02-16 10:51:48 +02001501 svm->vmcb->save.idtr.limit = dt->size;
1502 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001503 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504}
1505
Gleb Natapov89a27f42010-02-16 10:51:48 +02001506static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001508 struct vcpu_svm *svm = to_svm(vcpu);
1509
Gleb Natapov89a27f42010-02-16 10:51:48 +02001510 dt->size = svm->vmcb->save.gdtr.limit;
1511 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512}
1513
Gleb Natapov89a27f42010-02-16 10:51:48 +02001514static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001516 struct vcpu_svm *svm = to_svm(vcpu);
1517
Gleb Natapov89a27f42010-02-16 10:51:48 +02001518 svm->vmcb->save.gdtr.limit = dt->size;
1519 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001520 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521}
1522
Avi Kivitye8467fd2009-12-29 18:43:06 +02001523static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1524{
1525}
1526
Avi Kivityaff48ba2010-12-05 18:56:11 +02001527static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1528{
1529}
1530
Anthony Liguori25c4c272007-04-27 09:29:21 +03001531static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001532{
1533}
1534
Avi Kivityd2251572010-01-06 10:55:27 +02001535static void update_cr0_intercept(struct vcpu_svm *svm)
1536{
1537 ulong gcr0 = svm->vcpu.arch.cr0;
1538 u64 *hcr0 = &svm->vmcb->save.cr0;
1539
1540 if (!svm->vcpu.fpu_active)
1541 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1542 else
1543 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1544 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1545
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001546 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001547
1548 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001549 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1550 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001551 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001552 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1553 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001554 }
1555}
1556
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1558{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001559 struct vcpu_svm *svm = to_svm(vcpu);
1560
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001561#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001562 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001563 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001564 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001565 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566 }
1567
Mike Dayd77c26f2007-10-08 09:02:08 -04001568 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001569 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001570 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 }
1572 }
1573#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001574 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001575
1576 if (!npt_enabled)
1577 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001578
1579 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001580 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001581 /*
1582 * re-enable caching here because the QEMU bios
1583 * does not do it - this results in some delay at
1584 * reboot
1585 */
1586 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001587 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001588 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001589 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590}
1591
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001592static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593{
Joerg Roedel6394b642008-04-09 14:15:29 +02001594 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001595 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1596
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001597 if (cr4 & X86_CR4_VMXE)
1598 return 1;
1599
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001600 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001601 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001602
Joerg Roedelec077262008-04-09 14:15:28 +02001603 vcpu->arch.cr4 = cr4;
1604 if (!npt_enabled)
1605 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001606 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001607 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001608 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001609 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610}
1611
1612static void svm_set_segment(struct kvm_vcpu *vcpu,
1613 struct kvm_segment *var, int seg)
1614{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001615 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616 struct vmcb_seg *s = svm_seg(vcpu, seg);
1617
1618 s->base = var->base;
1619 s->limit = var->limit;
1620 s->selector = var->selector;
1621 if (var->unusable)
1622 s->attrib = 0;
1623 else {
1624 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1625 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1626 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1627 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1628 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1629 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1630 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1631 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1632 }
1633 if (seg == VCPU_SREG_CS)
Kevin Wolfea5e97e2012-02-08 14:34:40 +01001634 svm_update_cpl(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635
Joerg Roedel060d0c92010-12-03 11:45:57 +01001636 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637}
1638
Gleb Natapov44c11432009-05-11 13:35:52 +03001639static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001641 struct vcpu_svm *svm = to_svm(vcpu);
1642
Joerg Roedel18c918c2010-11-30 18:03:59 +01001643 clr_exception_intercept(svm, DB_VECTOR);
1644 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001645
Jan Kiszka6be7d302009-10-18 13:24:54 +02001646 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001647 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001648
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001649 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1650 if (vcpu->guest_debug &
1651 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001652 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001653 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001654 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001655 } else
1656 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001657}
1658
Jan Kiszka355be0b2009-10-03 00:31:21 +02001659static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001660{
Gleb Natapov44c11432009-05-11 13:35:52 +03001661 struct vcpu_svm *svm = to_svm(vcpu);
1662
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001663 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1664 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1665 else
1666 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1667
Joerg Roedel72214b92010-12-03 11:45:55 +01001668 mark_dirty(svm->vmcb, VMCB_DR);
1669
Jan Kiszka355be0b2009-10-03 00:31:21 +02001670 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671}
1672
Tejun Heo0fe1e002009-10-29 22:34:14 +09001673static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001675 if (sd->next_asid > sd->max_asid) {
1676 ++sd->asid_generation;
1677 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001678 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679 }
1680
Tejun Heo0fe1e002009-10-29 22:34:14 +09001681 svm->asid_generation = sd->asid_generation;
1682 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001683
1684 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685}
1686
Gleb Natapov020df072010-04-13 10:05:23 +03001687static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001689 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001690
Gleb Natapov020df072010-04-13 10:05:23 +03001691 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01001692 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693}
1694
Avi Kivity851ba692009-08-24 11:10:17 +03001695static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696{
Gleb Natapov631bc482010-10-14 11:22:52 +02001697 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001699 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700
Gleb Natapov631bc482010-10-14 11:22:52 +02001701 switch (svm->apf_reason) {
1702 default:
1703 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001704
Gleb Natapov631bc482010-10-14 11:22:52 +02001705 trace_kvm_page_fault(fault_address, error_code);
1706 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1707 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01001708 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1709 svm->vmcb->control.insn_bytes,
1710 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02001711 break;
1712 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1713 svm->apf_reason = 0;
1714 local_irq_disable();
1715 kvm_async_pf_task_wait(fault_address);
1716 local_irq_enable();
1717 break;
1718 case KVM_PV_REASON_PAGE_READY:
1719 svm->apf_reason = 0;
1720 local_irq_disable();
1721 kvm_async_pf_task_wake(fault_address);
1722 local_irq_enable();
1723 break;
1724 }
1725 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726}
1727
Avi Kivity851ba692009-08-24 11:10:17 +03001728static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001729{
Avi Kivity851ba692009-08-24 11:10:17 +03001730 struct kvm_run *kvm_run = svm->vcpu.run;
1731
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001732 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001733 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001734 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001735 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1736 return 1;
1737 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001738
Jan Kiszka6be7d302009-10-18 13:24:54 +02001739 if (svm->nmi_singlestep) {
1740 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001741 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1742 svm->vmcb->save.rflags &=
1743 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1744 update_db_intercept(&svm->vcpu);
1745 }
1746
1747 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001748 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001749 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1750 kvm_run->debug.arch.pc =
1751 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1752 kvm_run->debug.arch.exception = DB_VECTOR;
1753 return 0;
1754 }
1755
1756 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001757}
1758
Avi Kivity851ba692009-08-24 11:10:17 +03001759static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001760{
Avi Kivity851ba692009-08-24 11:10:17 +03001761 struct kvm_run *kvm_run = svm->vcpu.run;
1762
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001763 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1764 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1765 kvm_run->debug.arch.exception = BP_VECTOR;
1766 return 0;
1767}
1768
Avi Kivity851ba692009-08-24 11:10:17 +03001769static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001770{
1771 int er;
1772
Andre Przywara51d8b662010-12-21 11:12:02 +01001773 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001774 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001775 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001776 return 1;
1777}
1778
Avi Kivity6b52d182010-01-21 15:31:47 +02001779static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001780{
Avi Kivity6b52d182010-01-21 15:31:47 +02001781 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001782
Joerg Roedel18c918c2010-11-30 18:03:59 +01001783 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001784
Rusty Russelle756fc62007-07-30 20:07:08 +10001785 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001786 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001787}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001788
Avi Kivity6b52d182010-01-21 15:31:47 +02001789static int nm_interception(struct vcpu_svm *svm)
1790{
1791 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001792 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001793}
1794
Joerg Roedel67ec6602010-05-17 14:43:35 +02001795static bool is_erratum_383(void)
1796{
1797 int err, i;
1798 u64 value;
1799
1800 if (!erratum_383_found)
1801 return false;
1802
1803 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1804 if (err)
1805 return false;
1806
1807 /* Bit 62 may or may not be set for this mce */
1808 value &= ~(1ULL << 62);
1809
1810 if (value != 0xb600000000010015ULL)
1811 return false;
1812
1813 /* Clear MCi_STATUS registers */
1814 for (i = 0; i < 6; ++i)
1815 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1816
1817 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1818 if (!err) {
1819 u32 low, high;
1820
1821 value &= ~(1ULL << 2);
1822 low = lower_32_bits(value);
1823 high = upper_32_bits(value);
1824
1825 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1826 }
1827
1828 /* Flush tlb to evict multi-match entries */
1829 __flush_tlb_all();
1830
1831 return true;
1832}
1833
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001834static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001835{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001836 if (is_erratum_383()) {
1837 /*
1838 * Erratum 383 triggered. Guest state is corrupt so kill the
1839 * guest.
1840 */
1841 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1842
Avi Kivitya8eeb042010-05-10 12:34:53 +03001843 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001844
1845 return;
1846 }
1847
Joerg Roedel53371b52008-04-09 14:15:30 +02001848 /*
1849 * On an #MC intercept the MCE handler is not called automatically in
1850 * the host. So do it by hand here.
1851 */
1852 asm volatile (
1853 "int $0x12\n");
1854 /* not sure if we ever come back to this point */
1855
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001856 return;
1857}
1858
1859static int mc_interception(struct vcpu_svm *svm)
1860{
Joerg Roedel53371b52008-04-09 14:15:30 +02001861 return 1;
1862}
1863
Avi Kivity851ba692009-08-24 11:10:17 +03001864static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001865{
Avi Kivity851ba692009-08-24 11:10:17 +03001866 struct kvm_run *kvm_run = svm->vcpu.run;
1867
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001868 /*
1869 * VMCB is undefined after a SHUTDOWN intercept
1870 * so reinitialize it.
1871 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001872 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001873 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001874
1875 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1876 return 0;
1877}
1878
Avi Kivity851ba692009-08-24 11:10:17 +03001879static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001881 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001882 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001883 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001884 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885
Rusty Russelle756fc62007-07-30 20:07:08 +10001886 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001887 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001888 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001889 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01001890 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001891
Avi Kivity039576c2007-03-20 12:46:50 +02001892 port = io_info >> 16;
1893 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001894 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001895 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001896
1897 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898}
1899
Avi Kivity851ba692009-08-24 11:10:17 +03001900static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001901{
1902 return 1;
1903}
1904
Avi Kivity851ba692009-08-24 11:10:17 +03001905static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001906{
1907 ++svm->vcpu.stat.irq_exits;
1908 return 1;
1909}
1910
Avi Kivity851ba692009-08-24 11:10:17 +03001911static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912{
1913 return 1;
1914}
1915
Avi Kivity851ba692009-08-24 11:10:17 +03001916static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001917{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001918 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001919 skip_emulated_instruction(&svm->vcpu);
1920 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921}
1922
Avi Kivity851ba692009-08-24 11:10:17 +03001923static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001924{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001925 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001926 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001927 kvm_emulate_hypercall(&svm->vcpu);
1928 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001929}
1930
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001931static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1932{
1933 struct vcpu_svm *svm = to_svm(vcpu);
1934
1935 return svm->nested.nested_cr3;
1936}
1937
Avi Kivitye4e517b2011-07-28 11:36:17 +03001938static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
1939{
1940 struct vcpu_svm *svm = to_svm(vcpu);
1941 u64 cr3 = svm->nested.nested_cr3;
1942 u64 pdpte;
1943 int ret;
1944
1945 ret = kvm_read_guest_page(vcpu->kvm, gpa_to_gfn(cr3), &pdpte,
1946 offset_in_page(cr3) + index * 8, 8);
1947 if (ret)
1948 return 0;
1949 return pdpte;
1950}
1951
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001952static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1953 unsigned long root)
1954{
1955 struct vcpu_svm *svm = to_svm(vcpu);
1956
1957 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01001958 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001959 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001960}
1961
Avi Kivity6389ee92010-11-29 16:12:30 +02001962static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1963 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001964{
1965 struct vcpu_svm *svm = to_svm(vcpu);
1966
1967 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1968 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001969 svm->vmcb->control.exit_info_1 = fault->error_code;
1970 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001971
1972 nested_svm_vmexit(svm);
1973}
1974
Joerg Roedel4b161842010-09-10 17:31:03 +02001975static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1976{
1977 int r;
1978
1979 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1980
1981 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1982 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03001983 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02001984 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1985 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1986 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1987
1988 return r;
1989}
1990
1991static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1992{
1993 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1994}
1995
Alexander Grafc0725422008-11-25 20:17:03 +01001996static int nested_svm_check_permissions(struct vcpu_svm *svm)
1997{
Avi Kivityf6801df2010-01-21 15:31:50 +02001998 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001999 || !is_paging(&svm->vcpu)) {
2000 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2001 return 1;
2002 }
2003
2004 if (svm->vmcb->save.cpl) {
2005 kvm_inject_gp(&svm->vcpu, 0);
2006 return 1;
2007 }
2008
2009 return 0;
2010}
2011
Alexander Grafcf74a782008-11-25 20:17:08 +01002012static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2013 bool has_error_code, u32 error_code)
2014{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002015 int vmexit;
2016
Joerg Roedel20307532010-11-29 17:51:48 +01002017 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002018 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002019
Joerg Roedel0295ad72009-08-07 11:49:37 +02002020 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2021 svm->vmcb->control.exit_code_hi = 0;
2022 svm->vmcb->control.exit_info_1 = error_code;
2023 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
2024
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002025 vmexit = nested_svm_intercept(svm);
2026 if (vmexit == NESTED_EXIT_DONE)
2027 svm->nested.exit_required = true;
2028
2029 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002030}
2031
Joerg Roedel8fe54652010-02-19 16:23:01 +01002032/* This function returns true if it is save to enable the irq window */
2033static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002034{
Joerg Roedel20307532010-11-29 17:51:48 +01002035 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002036 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002037
Joerg Roedel26666952009-08-07 11:49:46 +02002038 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002039 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002040
Joerg Roedel26666952009-08-07 11:49:46 +02002041 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002042 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002043
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002044 /*
2045 * if vmexit was already requested (by intercepted exception
2046 * for instance) do not overwrite it with "external interrupt"
2047 * vmexit.
2048 */
2049 if (svm->nested.exit_required)
2050 return false;
2051
Joerg Roedel197717d2010-02-24 18:59:19 +01002052 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2053 svm->vmcb->control.exit_info_1 = 0;
2054 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002055
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002056 if (svm->nested.intercept & 1ULL) {
2057 /*
2058 * The #vmexit can't be emulated here directly because this
2059 * code path runs with irqs and preemtion disabled. A
2060 * #vmexit emulation might sleep. Only signal request for
2061 * the #vmexit here.
2062 */
2063 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002064 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002065 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002066 }
2067
Joerg Roedel8fe54652010-02-19 16:23:01 +01002068 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002069}
2070
Joerg Roedel887f5002010-02-24 18:59:12 +01002071/* This function returns true if it is save to enable the nmi window */
2072static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2073{
Joerg Roedel20307532010-11-29 17:51:48 +01002074 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002075 return true;
2076
2077 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2078 return true;
2079
2080 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2081 svm->nested.exit_required = true;
2082
2083 return false;
2084}
2085
Joerg Roedel7597f122010-02-19 16:23:00 +01002086static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002087{
2088 struct page *page;
2089
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002090 might_sleep();
2091
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002092 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002093 if (is_error_page(page))
2094 goto error;
2095
Joerg Roedel7597f122010-02-19 16:23:00 +01002096 *_page = page;
2097
2098 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002099
2100error:
2101 kvm_release_page_clean(page);
2102 kvm_inject_gp(&svm->vcpu, 0);
2103
2104 return NULL;
2105}
2106
Joerg Roedel7597f122010-02-19 16:23:00 +01002107static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002108{
Joerg Roedel7597f122010-02-19 16:23:00 +01002109 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002110 kvm_release_page_dirty(page);
2111}
2112
Joerg Roedelce2ac082010-03-01 15:34:39 +01002113static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002114{
Joerg Roedelce2ac082010-03-01 15:34:39 +01002115 unsigned port;
2116 u8 val, bit;
2117 u64 gpa;
2118
2119 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2120 return NESTED_EXIT_HOST;
2121
2122 port = svm->vmcb->control.exit_info_1 >> 16;
2123 gpa = svm->nested.vmcb_iopm + (port / 8);
2124 bit = port % 8;
2125 val = 0;
2126
2127 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
2128 val &= (1 << bit);
2129
2130 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
2131}
2132
Joerg Roedeld2477822010-03-01 15:34:34 +01002133static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002134{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002135 u32 offset, msr, value;
2136 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002137
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002138 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002139 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002140
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002141 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2142 offset = svm_msrpm_offset(msr);
2143 write = svm->vmcb->control.exit_info_1 & 1;
2144 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002145
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002146 if (offset == MSR_INVALID)
2147 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002148
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002149 /* Offset is in 32 bit units but need in 8 bit units */
2150 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002151
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002152 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
2153 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002154
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002155 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002156}
2157
Joerg Roedel410e4d52009-08-07 11:49:44 +02002158static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002159{
Alexander Grafcf74a782008-11-25 20:17:08 +01002160 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002161
Joerg Roedel410e4d52009-08-07 11:49:44 +02002162 switch (exit_code) {
2163 case SVM_EXIT_INTR:
2164 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002165 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002166 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002167 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002168 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002169 if (npt_enabled)
2170 return NESTED_EXIT_HOST;
2171 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002172 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002173 /* When we're shadowing, trap PFs, but not async PF */
2174 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002175 return NESTED_EXIT_HOST;
2176 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01002177 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
2178 nm_interception(svm);
2179 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002180 default:
2181 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002182 }
2183
Joerg Roedel410e4d52009-08-07 11:49:44 +02002184 return NESTED_EXIT_CONTINUE;
2185}
2186
2187/*
2188 * If this function returns true, this #vmexit was already handled
2189 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002190static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002191{
2192 u32 exit_code = svm->vmcb->control.exit_code;
2193 int vmexit = NESTED_EXIT_HOST;
2194
Alexander Grafcf74a782008-11-25 20:17:08 +01002195 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002196 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002197 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002198 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002199 case SVM_EXIT_IOIO:
2200 vmexit = nested_svm_intercept_ioio(svm);
2201 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002202 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2203 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2204 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002205 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002206 break;
2207 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002208 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2209 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2210 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002211 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002212 break;
2213 }
2214 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2215 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002216 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002217 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002218 /* async page fault always cause vmexit */
2219 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2220 svm->apf_reason != 0)
2221 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002222 break;
2223 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002224 case SVM_EXIT_ERR: {
2225 vmexit = NESTED_EXIT_DONE;
2226 break;
2227 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002228 default: {
2229 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002230 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002231 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002232 }
2233 }
2234
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002235 return vmexit;
2236}
2237
2238static int nested_svm_exit_handled(struct vcpu_svm *svm)
2239{
2240 int vmexit;
2241
2242 vmexit = nested_svm_intercept(svm);
2243
2244 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002245 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002246
2247 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002248}
2249
Joerg Roedel0460a972009-08-07 11:49:31 +02002250static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2251{
2252 struct vmcb_control_area *dst = &dst_vmcb->control;
2253 struct vmcb_control_area *from = &from_vmcb->control;
2254
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002255 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002256 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002257 dst->intercept_exceptions = from->intercept_exceptions;
2258 dst->intercept = from->intercept;
2259 dst->iopm_base_pa = from->iopm_base_pa;
2260 dst->msrpm_base_pa = from->msrpm_base_pa;
2261 dst->tsc_offset = from->tsc_offset;
2262 dst->asid = from->asid;
2263 dst->tlb_ctl = from->tlb_ctl;
2264 dst->int_ctl = from->int_ctl;
2265 dst->int_vector = from->int_vector;
2266 dst->int_state = from->int_state;
2267 dst->exit_code = from->exit_code;
2268 dst->exit_code_hi = from->exit_code_hi;
2269 dst->exit_info_1 = from->exit_info_1;
2270 dst->exit_info_2 = from->exit_info_2;
2271 dst->exit_int_info = from->exit_int_info;
2272 dst->exit_int_info_err = from->exit_int_info_err;
2273 dst->nested_ctl = from->nested_ctl;
2274 dst->event_inj = from->event_inj;
2275 dst->event_inj_err = from->event_inj_err;
2276 dst->nested_cr3 = from->nested_cr3;
2277 dst->lbr_ctl = from->lbr_ctl;
2278}
2279
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002280static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002281{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002282 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002283 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002284 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002285 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002286
Joerg Roedel17897f32009-10-09 16:08:29 +02002287 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2288 vmcb->control.exit_info_1,
2289 vmcb->control.exit_info_2,
2290 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002291 vmcb->control.exit_int_info_err,
2292 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002293
Joerg Roedel7597f122010-02-19 16:23:00 +01002294 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002295 if (!nested_vmcb)
2296 return 1;
2297
Joerg Roedel20307532010-11-29 17:51:48 +01002298 /* Exit Guest-Mode */
2299 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002300 svm->nested.vmcb = 0;
2301
Alexander Grafcf74a782008-11-25 20:17:08 +01002302 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002303 disable_gif(svm);
2304
2305 nested_vmcb->save.es = vmcb->save.es;
2306 nested_vmcb->save.cs = vmcb->save.cs;
2307 nested_vmcb->save.ss = vmcb->save.ss;
2308 nested_vmcb->save.ds = vmcb->save.ds;
2309 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2310 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002311 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002312 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002313 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002314 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002315 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002316 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002317 nested_vmcb->save.rip = vmcb->save.rip;
2318 nested_vmcb->save.rsp = vmcb->save.rsp;
2319 nested_vmcb->save.rax = vmcb->save.rax;
2320 nested_vmcb->save.dr7 = vmcb->save.dr7;
2321 nested_vmcb->save.dr6 = vmcb->save.dr6;
2322 nested_vmcb->save.cpl = vmcb->save.cpl;
2323
2324 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2325 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2326 nested_vmcb->control.int_state = vmcb->control.int_state;
2327 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2328 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2329 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2330 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2331 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2332 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002333 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002334
2335 /*
2336 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2337 * to make sure that we do not lose injected events. So check event_inj
2338 * here and copy it to exit_int_info if it is valid.
2339 * Exit_int_info and event_inj can't be both valid because the case
2340 * below only happens on a VMRUN instruction intercept which has
2341 * no valid exit_int_info set.
2342 */
2343 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2344 struct vmcb_control_area *nc = &nested_vmcb->control;
2345
2346 nc->exit_int_info = vmcb->control.event_inj;
2347 nc->exit_int_info_err = vmcb->control.event_inj_err;
2348 }
2349
Joerg Roedel33740e42009-08-07 11:49:29 +02002350 nested_vmcb->control.tlb_ctl = 0;
2351 nested_vmcb->control.event_inj = 0;
2352 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002353
2354 /* We always set V_INTR_MASKING and remember the old value in hflags */
2355 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2356 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2357
Alexander Grafcf74a782008-11-25 20:17:08 +01002358 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002359 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002360
Alexander Graf219b65d2009-06-15 15:21:25 +02002361 kvm_clear_exception_queue(&svm->vcpu);
2362 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002363
Joerg Roedel4b161842010-09-10 17:31:03 +02002364 svm->nested.nested_cr3 = 0;
2365
Alexander Grafcf74a782008-11-25 20:17:08 +01002366 /* Restore selected save entries */
2367 svm->vmcb->save.es = hsave->save.es;
2368 svm->vmcb->save.cs = hsave->save.cs;
2369 svm->vmcb->save.ss = hsave->save.ss;
2370 svm->vmcb->save.ds = hsave->save.ds;
2371 svm->vmcb->save.gdtr = hsave->save.gdtr;
2372 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002373 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002374 svm_set_efer(&svm->vcpu, hsave->save.efer);
2375 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2376 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2377 if (npt_enabled) {
2378 svm->vmcb->save.cr3 = hsave->save.cr3;
2379 svm->vcpu.arch.cr3 = hsave->save.cr3;
2380 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002381 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002382 }
2383 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2384 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2385 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2386 svm->vmcb->save.dr7 = 0;
2387 svm->vmcb->save.cpl = 0;
2388 svm->vmcb->control.exit_int_info = 0;
2389
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002390 mark_all_dirty(svm->vmcb);
2391
Joerg Roedel7597f122010-02-19 16:23:00 +01002392 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002393
Joerg Roedel4b161842010-09-10 17:31:03 +02002394 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002395 kvm_mmu_reset_context(&svm->vcpu);
2396 kvm_mmu_load(&svm->vcpu);
2397
2398 return 0;
2399}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002400
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002401static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002402{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002403 /*
2404 * This function merges the msr permission bitmaps of kvm and the
2405 * nested vmcb. It is omptimized in that it only merges the parts where
2406 * the kvm msr permission bitmap may contain zero bits
2407 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002408 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002409
Joerg Roedel323c3d82010-03-01 15:34:37 +01002410 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2411 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002412
Joerg Roedel323c3d82010-03-01 15:34:37 +01002413 for (i = 0; i < MSRPM_OFFSETS; i++) {
2414 u32 value, p;
2415 u64 offset;
2416
2417 if (msrpm_offsets[i] == 0xffffffff)
2418 break;
2419
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002420 p = msrpm_offsets[i];
2421 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002422
2423 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2424 return false;
2425
2426 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2427 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002428
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002429 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002430
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002431 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002432}
2433
Joerg Roedel52c65a302010-08-02 16:46:44 +02002434static bool nested_vmcb_checks(struct vmcb *vmcb)
2435{
2436 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2437 return false;
2438
Joerg Roedeldbe77582010-08-02 16:46:45 +02002439 if (vmcb->control.asid == 0)
2440 return false;
2441
Joerg Roedel4b161842010-09-10 17:31:03 +02002442 if (vmcb->control.nested_ctl && !npt_enabled)
2443 return false;
2444
Joerg Roedel52c65a302010-08-02 16:46:44 +02002445 return true;
2446}
2447
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002448static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002449{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002450 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002451 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002452 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002453 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002454 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002455
Joerg Roedel06fc77722010-02-19 16:23:07 +01002456 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002457
Joerg Roedel7597f122010-02-19 16:23:00 +01002458 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002459 if (!nested_vmcb)
2460 return false;
2461
Joerg Roedel52c65a302010-08-02 16:46:44 +02002462 if (!nested_vmcb_checks(nested_vmcb)) {
2463 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2464 nested_vmcb->control.exit_code_hi = 0;
2465 nested_vmcb->control.exit_info_1 = 0;
2466 nested_vmcb->control.exit_info_2 = 0;
2467
2468 nested_svm_unmap(page);
2469
2470 return false;
2471 }
2472
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002473 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002474 nested_vmcb->save.rip,
2475 nested_vmcb->control.int_ctl,
2476 nested_vmcb->control.event_inj,
2477 nested_vmcb->control.nested_ctl);
2478
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002479 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2480 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002481 nested_vmcb->control.intercept_exceptions,
2482 nested_vmcb->control.intercept);
2483
Alexander Graf3d6368e2008-11-25 20:17:07 +01002484 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002485 kvm_clear_exception_queue(&svm->vcpu);
2486 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002487
Joerg Roedele0231712010-02-24 18:59:10 +01002488 /*
2489 * Save the old vmcb, so we don't need to pick what we save, but can
2490 * restore everything when a VMEXIT occurs
2491 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002492 hsave->save.es = vmcb->save.es;
2493 hsave->save.cs = vmcb->save.cs;
2494 hsave->save.ss = vmcb->save.ss;
2495 hsave->save.ds = vmcb->save.ds;
2496 hsave->save.gdtr = vmcb->save.gdtr;
2497 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002498 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002499 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002500 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002501 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002502 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002503 hsave->save.rsp = vmcb->save.rsp;
2504 hsave->save.rax = vmcb->save.rax;
2505 if (npt_enabled)
2506 hsave->save.cr3 = vmcb->save.cr3;
2507 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002508 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002509
Joerg Roedel0460a972009-08-07 11:49:31 +02002510 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002511
Avi Kivityf6e78472010-08-02 15:30:20 +03002512 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002513 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2514 else
2515 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2516
Joerg Roedel4b161842010-09-10 17:31:03 +02002517 if (nested_vmcb->control.nested_ctl) {
2518 kvm_mmu_unload(&svm->vcpu);
2519 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2520 nested_svm_init_mmu_context(&svm->vcpu);
2521 }
2522
Alexander Graf3d6368e2008-11-25 20:17:07 +01002523 /* Load the nested guest state */
2524 svm->vmcb->save.es = nested_vmcb->save.es;
2525 svm->vmcb->save.cs = nested_vmcb->save.cs;
2526 svm->vmcb->save.ss = nested_vmcb->save.ss;
2527 svm->vmcb->save.ds = nested_vmcb->save.ds;
2528 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2529 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002530 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002531 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2532 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2533 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2534 if (npt_enabled) {
2535 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2536 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002537 } else
Avi Kivity23902182010-06-10 17:02:16 +03002538 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002539
2540 /* Guest paging mode is active - reset mmu */
2541 kvm_mmu_reset_context(&svm->vcpu);
2542
Joerg Roedeldefbba52009-08-07 11:49:30 +02002543 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002544 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2545 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2546 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002547
Alexander Graf3d6368e2008-11-25 20:17:07 +01002548 /* In case we don't even reach vcpu_run, the fields are not updated */
2549 svm->vmcb->save.rax = nested_vmcb->save.rax;
2550 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2551 svm->vmcb->save.rip = nested_vmcb->save.rip;
2552 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2553 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2554 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2555
Joerg Roedelf7138532010-03-01 15:34:40 +01002556 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002557 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002558
Joerg Roedelaad42c62009-08-07 11:49:34 +02002559 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002560 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002561 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002562 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2563 svm->nested.intercept = nested_vmcb->control.intercept;
2564
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002565 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002566 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002567 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2568 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2569 else
2570 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2571
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002572 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2573 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002574 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2575 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002576 }
2577
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002578 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01002579 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002580
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002581 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002582 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2583 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2584 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002585 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2586 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2587
Joerg Roedel7597f122010-02-19 16:23:00 +01002588 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002589
Joerg Roedel20307532010-11-29 17:51:48 +01002590 /* Enter Guest-Mode */
2591 enter_guest_mode(&svm->vcpu);
2592
Joerg Roedel384c6362010-11-30 18:03:56 +01002593 /*
2594 * Merge guest and host intercepts - must be called with vcpu in
2595 * guest-mode to take affect here
2596 */
2597 recalc_intercepts(svm);
2598
Joerg Roedel06fc77722010-02-19 16:23:07 +01002599 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002600
Joerg Roedel2af91942009-08-07 11:49:28 +02002601 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002602
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002603 mark_all_dirty(svm->vmcb);
2604
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002605 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002606}
2607
Joerg Roedel9966bf62009-08-07 11:49:40 +02002608static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002609{
2610 to_vmcb->save.fs = from_vmcb->save.fs;
2611 to_vmcb->save.gs = from_vmcb->save.gs;
2612 to_vmcb->save.tr = from_vmcb->save.tr;
2613 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2614 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2615 to_vmcb->save.star = from_vmcb->save.star;
2616 to_vmcb->save.lstar = from_vmcb->save.lstar;
2617 to_vmcb->save.cstar = from_vmcb->save.cstar;
2618 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2619 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2620 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2621 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002622}
2623
Avi Kivity851ba692009-08-24 11:10:17 +03002624static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002625{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002626 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002627 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002628
Alexander Graf55426752008-11-25 20:17:06 +01002629 if (nested_svm_check_permissions(svm))
2630 return 1;
2631
Joerg Roedel7597f122010-02-19 16:23:00 +01002632 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002633 if (!nested_vmcb)
2634 return 1;
2635
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002636 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2637 skip_emulated_instruction(&svm->vcpu);
2638
Joerg Roedel9966bf62009-08-07 11:49:40 +02002639 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002640 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002641
2642 return 1;
2643}
2644
Avi Kivity851ba692009-08-24 11:10:17 +03002645static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002646{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002647 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002648 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002649
Alexander Graf55426752008-11-25 20:17:06 +01002650 if (nested_svm_check_permissions(svm))
2651 return 1;
2652
Joerg Roedel7597f122010-02-19 16:23:00 +01002653 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002654 if (!nested_vmcb)
2655 return 1;
2656
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002657 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2658 skip_emulated_instruction(&svm->vcpu);
2659
Joerg Roedel9966bf62009-08-07 11:49:40 +02002660 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002661 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002662
2663 return 1;
2664}
2665
Avi Kivity851ba692009-08-24 11:10:17 +03002666static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002667{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002668 if (nested_svm_check_permissions(svm))
2669 return 1;
2670
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002671 /* Save rip after vmrun instruction */
2672 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002673
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002674 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002675 return 1;
2676
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002677 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002678 goto failed;
2679
2680 return 1;
2681
2682failed:
2683
2684 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2685 svm->vmcb->control.exit_code_hi = 0;
2686 svm->vmcb->control.exit_info_1 = 0;
2687 svm->vmcb->control.exit_info_2 = 0;
2688
2689 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002690
2691 return 1;
2692}
2693
Avi Kivity851ba692009-08-24 11:10:17 +03002694static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002695{
2696 if (nested_svm_check_permissions(svm))
2697 return 1;
2698
2699 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2700 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002701 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002702
Joerg Roedel2af91942009-08-07 11:49:28 +02002703 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002704
2705 return 1;
2706}
2707
Avi Kivity851ba692009-08-24 11:10:17 +03002708static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002709{
2710 if (nested_svm_check_permissions(svm))
2711 return 1;
2712
2713 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2714 skip_emulated_instruction(&svm->vcpu);
2715
Joerg Roedel2af91942009-08-07 11:49:28 +02002716 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002717
2718 /* After a CLGI no interrupts should come */
2719 svm_clear_vintr(svm);
2720 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2721
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002722 mark_dirty(svm->vmcb, VMCB_INTR);
2723
Alexander Graf1371d902008-11-25 20:17:04 +01002724 return 1;
2725}
2726
Avi Kivity851ba692009-08-24 11:10:17 +03002727static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002728{
2729 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002730
Joerg Roedelec1ff792009-10-09 16:08:31 +02002731 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2732 vcpu->arch.regs[VCPU_REGS_RAX]);
2733
Alexander Grafff092382009-06-15 15:21:24 +02002734 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2735 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2736
2737 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2738 skip_emulated_instruction(&svm->vcpu);
2739 return 1;
2740}
2741
Joerg Roedel532a46b2009-10-09 16:08:32 +02002742static int skinit_interception(struct vcpu_svm *svm)
2743{
2744 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2745
2746 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2747 return 1;
2748}
2749
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002750static int xsetbv_interception(struct vcpu_svm *svm)
2751{
2752 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
2753 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
2754
2755 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
2756 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2757 skip_emulated_instruction(&svm->vcpu);
2758 }
2759
2760 return 1;
2761}
2762
Avi Kivity851ba692009-08-24 11:10:17 +03002763static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002765 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 return 1;
2767}
2768
Avi Kivity851ba692009-08-24 11:10:17 +03002769static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770{
Izik Eidus37817f22008-03-24 23:14:53 +02002771 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002772 int reason;
2773 int int_type = svm->vmcb->control.exit_int_info &
2774 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002775 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002776 uint32_t type =
2777 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2778 uint32_t idt_v =
2779 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002780 bool has_error_code = false;
2781 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002782
2783 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002784
Izik Eidus37817f22008-03-24 23:14:53 +02002785 if (svm->vmcb->control.exit_info_2 &
2786 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002787 reason = TASK_SWITCH_IRET;
2788 else if (svm->vmcb->control.exit_info_2 &
2789 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2790 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002791 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002792 reason = TASK_SWITCH_GATE;
2793 else
2794 reason = TASK_SWITCH_CALL;
2795
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002796 if (reason == TASK_SWITCH_GATE) {
2797 switch (type) {
2798 case SVM_EXITINTINFO_TYPE_NMI:
2799 svm->vcpu.arch.nmi_injected = false;
2800 break;
2801 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002802 if (svm->vmcb->control.exit_info_2 &
2803 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2804 has_error_code = true;
2805 error_code =
2806 (u32)svm->vmcb->control.exit_info_2;
2807 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002808 kvm_clear_exception_queue(&svm->vcpu);
2809 break;
2810 case SVM_EXITINTINFO_TYPE_INTR:
2811 kvm_clear_interrupt_queue(&svm->vcpu);
2812 break;
2813 default:
2814 break;
2815 }
2816 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002817
Gleb Natapov8317c292009-04-12 13:37:02 +03002818 if (reason != TASK_SWITCH_GATE ||
2819 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2820 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002821 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2822 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002823
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002824 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2825 int_vec = -1;
2826
2827 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03002828 has_error_code, error_code) == EMULATE_FAIL) {
2829 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2830 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2831 svm->vcpu.run->internal.ndata = 0;
2832 return 0;
2833 }
2834 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835}
2836
Avi Kivity851ba692009-08-24 11:10:17 +03002837static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002839 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002840 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002841 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842}
2843
Avi Kivity851ba692009-08-24 11:10:17 +03002844static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002845{
2846 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01002847 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002848 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02002849 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002850 return 1;
2851}
2852
Avi Kivity851ba692009-08-24 11:10:17 +03002853static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002854{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002855 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2856 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
2857
2858 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
2859 skip_emulated_instruction(&svm->vcpu);
2860 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002861}
2862
Avi Kivity851ba692009-08-24 11:10:17 +03002863static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864{
Andre Przywara51d8b662010-12-21 11:12:02 +01002865 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866}
2867
Avi Kivity332b56e2011-11-10 14:57:24 +02002868static int rdpmc_interception(struct vcpu_svm *svm)
2869{
2870 int err;
2871
2872 if (!static_cpu_has(X86_FEATURE_NRIPS))
2873 return emulate_on_interception(svm);
2874
2875 err = kvm_rdpmc(&svm->vcpu);
2876 kvm_complete_insn_gp(&svm->vcpu, err);
2877
2878 return 1;
2879}
2880
Joerg Roedel628afd22011-04-04 12:39:36 +02002881bool check_selective_cr0_intercepted(struct vcpu_svm *svm, unsigned long val)
2882{
2883 unsigned long cr0 = svm->vcpu.arch.cr0;
2884 bool ret = false;
2885 u64 intercept;
2886
2887 intercept = svm->nested.intercept;
2888
2889 if (!is_guest_mode(&svm->vcpu) ||
2890 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
2891 return false;
2892
2893 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2894 val &= ~SVM_CR0_SELECTIVE_MASK;
2895
2896 if (cr0 ^ val) {
2897 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2898 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2899 }
2900
2901 return ret;
2902}
2903
Andre Przywara7ff76d52010-12-21 11:12:04 +01002904#define CR_VALID (1ULL << 63)
2905
2906static int cr_interception(struct vcpu_svm *svm)
2907{
2908 int reg, cr;
2909 unsigned long val;
2910 int err;
2911
2912 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2913 return emulate_on_interception(svm);
2914
2915 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2916 return emulate_on_interception(svm);
2917
2918 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2919 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
2920
2921 err = 0;
2922 if (cr >= 16) { /* mov to cr */
2923 cr -= 16;
2924 val = kvm_register_read(&svm->vcpu, reg);
2925 switch (cr) {
2926 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02002927 if (!check_selective_cr0_intercepted(svm, val))
2928 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002929 else
2930 return 1;
2931
Andre Przywara7ff76d52010-12-21 11:12:04 +01002932 break;
2933 case 3:
2934 err = kvm_set_cr3(&svm->vcpu, val);
2935 break;
2936 case 4:
2937 err = kvm_set_cr4(&svm->vcpu, val);
2938 break;
2939 case 8:
2940 err = kvm_set_cr8(&svm->vcpu, val);
2941 break;
2942 default:
2943 WARN(1, "unhandled write to CR%d", cr);
2944 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2945 return 1;
2946 }
2947 } else { /* mov from cr */
2948 switch (cr) {
2949 case 0:
2950 val = kvm_read_cr0(&svm->vcpu);
2951 break;
2952 case 2:
2953 val = svm->vcpu.arch.cr2;
2954 break;
2955 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02002956 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002957 break;
2958 case 4:
2959 val = kvm_read_cr4(&svm->vcpu);
2960 break;
2961 case 8:
2962 val = kvm_get_cr8(&svm->vcpu);
2963 break;
2964 default:
2965 WARN(1, "unhandled read from CR%d", cr);
2966 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2967 return 1;
2968 }
2969 kvm_register_write(&svm->vcpu, reg, val);
2970 }
2971 kvm_complete_insn_gp(&svm->vcpu, err);
2972
2973 return 1;
2974}
2975
Andre Przywaracae37972010-12-21 11:12:05 +01002976static int dr_interception(struct vcpu_svm *svm)
2977{
2978 int reg, dr;
2979 unsigned long val;
2980 int err;
2981
2982 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
2983 return emulate_on_interception(svm);
2984
2985 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2986 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
2987
2988 if (dr >= 16) { /* mov to DRn */
2989 val = kvm_register_read(&svm->vcpu, reg);
2990 kvm_set_dr(&svm->vcpu, dr - 16, val);
2991 } else {
2992 err = kvm_get_dr(&svm->vcpu, dr, &val);
2993 if (!err)
2994 kvm_register_write(&svm->vcpu, reg, val);
2995 }
2996
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01002997 skip_emulated_instruction(&svm->vcpu);
2998
Andre Przywaracae37972010-12-21 11:12:05 +01002999 return 1;
3000}
3001
Avi Kivity851ba692009-08-24 11:10:17 +03003002static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003003{
Avi Kivity851ba692009-08-24 11:10:17 +03003004 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003005 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003006
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003007 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3008 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003009 r = cr_interception(svm);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003010 if (irqchip_in_kernel(svm->vcpu.kvm)) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003011 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003012 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003013 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003014 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003015 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003016 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3017 return 0;
3018}
3019
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003020u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu)
3021{
3022 struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
3023 return vmcb->control.tsc_offset +
3024 svm_scale_tsc(vcpu, native_read_tsc());
3025}
3026
Avi Kivity6aa8b732006-12-10 02:21:36 -08003027static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
3028{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003029 struct vcpu_svm *svm = to_svm(vcpu);
3030
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303032 case MSR_IA32_TSC: {
Nadav Har'El45133ec2011-08-02 15:55:23 +03003033 *data = svm->vmcb->control.tsc_offset +
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003034 svm_scale_tsc(vcpu, native_read_tsc());
3035
Avi Kivity6aa8b732006-12-10 02:21:36 -08003036 break;
3037 }
Brian Gerst8c065852010-07-17 09:03:26 -04003038 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003039 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003041#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003043 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044 break;
3045 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003046 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047 break;
3048 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003049 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050 break;
3051 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003052 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003053 break;
3054#endif
3055 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003056 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057 break;
3058 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003059 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060 break;
3061 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003062 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003063 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003064 /*
3065 * Nobody will change the following 5 values in the VMCB so we can
3066 * safely return them on rdmsr. They will always be 0 until LBRV is
3067 * implemented.
3068 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003069 case MSR_IA32_DEBUGCTLMSR:
3070 *data = svm->vmcb->save.dbgctl;
3071 break;
3072 case MSR_IA32_LASTBRANCHFROMIP:
3073 *data = svm->vmcb->save.br_from;
3074 break;
3075 case MSR_IA32_LASTBRANCHTOIP:
3076 *data = svm->vmcb->save.br_to;
3077 break;
3078 case MSR_IA32_LASTINTFROMIP:
3079 *data = svm->vmcb->save.last_excp_from;
3080 break;
3081 case MSR_IA32_LASTINTTOIP:
3082 *data = svm->vmcb->save.last_excp_to;
3083 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003084 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003085 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003086 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003087 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003088 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003089 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003090 case MSR_IA32_UCODE_REV:
3091 *data = 0x01000065;
3092 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08003094 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095 }
3096 return 0;
3097}
3098
Avi Kivity851ba692009-08-24 11:10:17 +03003099static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003101 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102 u64 data;
3103
Avi Kivity59200272010-01-25 19:47:02 +02003104 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
3105 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003106 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003107 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003108 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003109
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003110 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003111 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003112 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003113 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114 }
3115 return 1;
3116}
3117
Joerg Roedel4a810182010-02-24 18:59:15 +01003118static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3119{
3120 struct vcpu_svm *svm = to_svm(vcpu);
3121 int svm_dis, chg_mask;
3122
3123 if (data & ~SVM_VM_CR_VALID_MASK)
3124 return 1;
3125
3126 chg_mask = SVM_VM_CR_VALID_MASK;
3127
3128 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3129 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3130
3131 svm->nested.vm_cr_msr &= ~chg_mask;
3132 svm->nested.vm_cr_msr |= (data & chg_mask);
3133
3134 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3135
3136 /* check for svm_disable while efer.svme is set */
3137 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3138 return 1;
3139
3140 return 0;
3141}
3142
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
3144{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003145 struct vcpu_svm *svm = to_svm(vcpu);
3146
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003148 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10003149 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003151 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003152 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003154#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003156 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157 break;
3158 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003159 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160 break;
3161 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003162 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 break;
3164 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003165 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166 break;
3167#endif
3168 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003169 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 break;
3171 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003172 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003173 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 break;
3175 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003176 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003177 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003179 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003180 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003181 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003182 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003183 break;
3184 }
3185 if (data & DEBUGCTL_RESERVED_BITS)
3186 return 1;
3187
3188 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003189 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003190 if (data & (1ULL<<0))
3191 svm_enable_lbrv(svm);
3192 else
3193 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003194 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003195 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003196 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003197 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003198 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003199 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003200 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003201 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
3202 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08003204 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205 }
3206 return 0;
3207}
3208
Avi Kivity851ba692009-08-24 11:10:17 +03003209static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003211 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003212 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003213 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003214
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003215
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003216 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02003217 if (svm_set_msr(&svm->vcpu, ecx, data)) {
3218 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003219 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003220 } else {
3221 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10003222 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003223 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224 return 1;
3225}
3226
Avi Kivity851ba692009-08-24 11:10:17 +03003227static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228{
Rusty Russelle756fc62007-07-30 20:07:08 +10003229 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003230 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231 else
Avi Kivity851ba692009-08-24 11:10:17 +03003232 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233}
3234
Avi Kivity851ba692009-08-24 11:10:17 +03003235static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003236{
Avi Kivity851ba692009-08-24 11:10:17 +03003237 struct kvm_run *kvm_run = svm->vcpu.run;
3238
Avi Kivity3842d132010-07-27 12:30:24 +03003239 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003240 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003241 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003242 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08003243 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003244 /*
3245 * If the user space waits to inject interrupts, exit as soon as
3246 * possible
3247 */
Gleb Natapov80618232009-04-21 17:44:56 +03003248 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
3249 kvm_run->request_interrupt_window &&
3250 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Dor Laorc1150d82007-01-05 16:36:24 -08003251 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
3252 return 0;
3253 }
3254
3255 return 1;
3256}
3257
Mark Langsdorf565d0992009-10-06 14:25:02 -05003258static int pause_interception(struct vcpu_svm *svm)
3259{
3260 kvm_vcpu_on_spin(&(svm->vcpu));
3261 return 1;
3262}
3263
Avi Kivity851ba692009-08-24 11:10:17 +03003264static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003265 [SVM_EXIT_READ_CR0] = cr_interception,
3266 [SVM_EXIT_READ_CR3] = cr_interception,
3267 [SVM_EXIT_READ_CR4] = cr_interception,
3268 [SVM_EXIT_READ_CR8] = cr_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02003269 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003270 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003271 [SVM_EXIT_WRITE_CR3] = cr_interception,
3272 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003273 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003274 [SVM_EXIT_READ_DR0] = dr_interception,
3275 [SVM_EXIT_READ_DR1] = dr_interception,
3276 [SVM_EXIT_READ_DR2] = dr_interception,
3277 [SVM_EXIT_READ_DR3] = dr_interception,
3278 [SVM_EXIT_READ_DR4] = dr_interception,
3279 [SVM_EXIT_READ_DR5] = dr_interception,
3280 [SVM_EXIT_READ_DR6] = dr_interception,
3281 [SVM_EXIT_READ_DR7] = dr_interception,
3282 [SVM_EXIT_WRITE_DR0] = dr_interception,
3283 [SVM_EXIT_WRITE_DR1] = dr_interception,
3284 [SVM_EXIT_WRITE_DR2] = dr_interception,
3285 [SVM_EXIT_WRITE_DR3] = dr_interception,
3286 [SVM_EXIT_WRITE_DR4] = dr_interception,
3287 [SVM_EXIT_WRITE_DR5] = dr_interception,
3288 [SVM_EXIT_WRITE_DR6] = dr_interception,
3289 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003290 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3291 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003292 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003293 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3294 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
3295 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
3296 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003297 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003298 [SVM_EXIT_SMI] = nop_on_interception,
3299 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003300 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02003301 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003302 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003303 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003304 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003305 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003307 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003308 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003309 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 [SVM_EXIT_MSR] = msr_interception,
3311 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003312 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003313 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003314 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003315 [SVM_EXIT_VMLOAD] = vmload_interception,
3316 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003317 [SVM_EXIT_STGI] = stgi_interception,
3318 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003319 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003320 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01003321 [SVM_EXIT_MONITOR] = invalid_op_interception,
3322 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003323 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003324 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003325};
3326
Joe Perchesae8cc052011-04-24 22:00:50 -07003327static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003328{
3329 struct vcpu_svm *svm = to_svm(vcpu);
3330 struct vmcb_control_area *control = &svm->vmcb->control;
3331 struct vmcb_save_area *save = &svm->vmcb->save;
3332
3333 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003334 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
3335 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
3336 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
3337 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
3338 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
3339 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
3340 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3341 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3342 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3343 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3344 pr_err("%-20s%d\n", "asid:", control->asid);
3345 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3346 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3347 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3348 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3349 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3350 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3351 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3352 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3353 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3354 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3355 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
3356 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3357 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3358 pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
3359 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003360 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003361 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3362 "es:",
3363 save->es.selector, save->es.attrib,
3364 save->es.limit, save->es.base);
3365 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3366 "cs:",
3367 save->cs.selector, save->cs.attrib,
3368 save->cs.limit, save->cs.base);
3369 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3370 "ss:",
3371 save->ss.selector, save->ss.attrib,
3372 save->ss.limit, save->ss.base);
3373 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3374 "ds:",
3375 save->ds.selector, save->ds.attrib,
3376 save->ds.limit, save->ds.base);
3377 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3378 "fs:",
3379 save->fs.selector, save->fs.attrib,
3380 save->fs.limit, save->fs.base);
3381 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3382 "gs:",
3383 save->gs.selector, save->gs.attrib,
3384 save->gs.limit, save->gs.base);
3385 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3386 "gdtr:",
3387 save->gdtr.selector, save->gdtr.attrib,
3388 save->gdtr.limit, save->gdtr.base);
3389 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3390 "ldtr:",
3391 save->ldtr.selector, save->ldtr.attrib,
3392 save->ldtr.limit, save->ldtr.base);
3393 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3394 "idtr:",
3395 save->idtr.selector, save->idtr.attrib,
3396 save->idtr.limit, save->idtr.base);
3397 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3398 "tr:",
3399 save->tr.selector, save->tr.attrib,
3400 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003401 pr_err("cpl: %d efer: %016llx\n",
3402 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003403 pr_err("%-15s %016llx %-13s %016llx\n",
3404 "cr0:", save->cr0, "cr2:", save->cr2);
3405 pr_err("%-15s %016llx %-13s %016llx\n",
3406 "cr3:", save->cr3, "cr4:", save->cr4);
3407 pr_err("%-15s %016llx %-13s %016llx\n",
3408 "dr6:", save->dr6, "dr7:", save->dr7);
3409 pr_err("%-15s %016llx %-13s %016llx\n",
3410 "rip:", save->rip, "rflags:", save->rflags);
3411 pr_err("%-15s %016llx %-13s %016llx\n",
3412 "rsp:", save->rsp, "rax:", save->rax);
3413 pr_err("%-15s %016llx %-13s %016llx\n",
3414 "star:", save->star, "lstar:", save->lstar);
3415 pr_err("%-15s %016llx %-13s %016llx\n",
3416 "cstar:", save->cstar, "sfmask:", save->sfmask);
3417 pr_err("%-15s %016llx %-13s %016llx\n",
3418 "kernel_gs_base:", save->kernel_gs_base,
3419 "sysenter_cs:", save->sysenter_cs);
3420 pr_err("%-15s %016llx %-13s %016llx\n",
3421 "sysenter_esp:", save->sysenter_esp,
3422 "sysenter_eip:", save->sysenter_eip);
3423 pr_err("%-15s %016llx %-13s %016llx\n",
3424 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3425 pr_err("%-15s %016llx %-13s %016llx\n",
3426 "br_from:", save->br_from, "br_to:", save->br_to);
3427 pr_err("%-15s %016llx %-13s %016llx\n",
3428 "excp_from:", save->last_excp_from,
3429 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003430}
3431
Avi Kivity586f9602010-11-18 13:09:54 +02003432static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3433{
3434 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3435
3436 *info1 = control->exit_info_1;
3437 *info2 = control->exit_info_2;
3438}
3439
Avi Kivity851ba692009-08-24 11:10:17 +03003440static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003442 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003443 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003444 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003446 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003447 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3448 if (npt_enabled)
3449 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003450
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003451 if (unlikely(svm->nested.exit_required)) {
3452 nested_svm_vmexit(svm);
3453 svm->nested.exit_required = false;
3454
3455 return 1;
3456 }
3457
Joerg Roedel20307532010-11-29 17:51:48 +01003458 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003459 int vmexit;
3460
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003461 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3462 svm->vmcb->control.exit_info_1,
3463 svm->vmcb->control.exit_info_2,
3464 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003465 svm->vmcb->control.exit_int_info_err,
3466 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003467
Joerg Roedel410e4d52009-08-07 11:49:44 +02003468 vmexit = nested_svm_exit_special(svm);
3469
3470 if (vmexit == NESTED_EXIT_CONTINUE)
3471 vmexit = nested_svm_exit_handled(svm);
3472
3473 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003474 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003475 }
3476
Joerg Roedela5c38322009-08-07 11:49:32 +02003477 svm_complete_interrupts(svm);
3478
Avi Kivity04d2cc72007-09-10 18:10:54 +03003479 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3480 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3481 kvm_run->fail_entry.hardware_entry_failure_reason
3482 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003483 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3484 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003485 return 0;
3486 }
3487
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003488 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003489 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003490 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3491 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3493 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003494 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003495 exit_code);
3496
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003497 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003498 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003499 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003500 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501 return 0;
3502 }
3503
Avi Kivity851ba692009-08-24 11:10:17 +03003504 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505}
3506
3507static void reload_tss(struct kvm_vcpu *vcpu)
3508{
3509 int cpu = raw_smp_processor_id();
3510
Tejun Heo0fe1e002009-10-29 22:34:14 +09003511 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3512 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513 load_TR_desc();
3514}
3515
Rusty Russelle756fc62007-07-30 20:07:08 +10003516static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003517{
3518 int cpu = raw_smp_processor_id();
3519
Tejun Heo0fe1e002009-10-29 22:34:14 +09003520 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003522 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003523 if (svm->asid_generation != sd->asid_generation)
3524 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003525}
3526
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003527static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3528{
3529 struct vcpu_svm *svm = to_svm(vcpu);
3530
3531 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3532 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01003533 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003534 ++vcpu->stat.nmi_injections;
3535}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536
Eddie Dong85f455f2007-07-06 12:20:49 +03003537static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003538{
3539 struct vmcb_control_area *control;
3540
Rusty Russelle756fc62007-07-30 20:07:08 +10003541 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003542 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003543 control->int_ctl &= ~V_INTR_PRIO_MASK;
3544 control->int_ctl |= V_IRQ_MASK |
3545 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003546 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003547}
3548
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003549static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003550{
3551 struct vcpu_svm *svm = to_svm(vcpu);
3552
Joerg Roedel2af91942009-08-07 11:49:28 +02003553 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003554
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003555 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3556 ++vcpu->stat.irq_injections;
3557
Alexander Graf219b65d2009-06-15 15:21:25 +02003558 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3559 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003560}
3561
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003562static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3563{
3564 struct vcpu_svm *svm = to_svm(vcpu);
3565
Joerg Roedel20307532010-11-29 17:51:48 +01003566 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003567 return;
3568
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003569 if (irr == -1)
3570 return;
3571
3572 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003573 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003574}
3575
3576static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003577{
3578 struct vcpu_svm *svm = to_svm(vcpu);
3579 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003580 int ret;
3581 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3582 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3583 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3584
3585 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003586}
3587
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003588static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3589{
3590 struct vcpu_svm *svm = to_svm(vcpu);
3591
3592 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3593}
3594
3595static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3596{
3597 struct vcpu_svm *svm = to_svm(vcpu);
3598
3599 if (masked) {
3600 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01003601 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003602 } else {
3603 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01003604 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003605 }
3606}
3607
Gleb Natapov78646122009-03-23 12:12:11 +02003608static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3609{
3610 struct vcpu_svm *svm = to_svm(vcpu);
3611 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003612 int ret;
3613
3614 if (!gif_set(svm) ||
3615 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3616 return 0;
3617
Avi Kivityf6e78472010-08-02 15:30:20 +03003618 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003619
Joerg Roedel20307532010-11-29 17:51:48 +01003620 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003621 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3622
3623 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003624}
3625
Gleb Natapov9222be12009-04-23 17:14:37 +03003626static void enable_irq_window(struct kvm_vcpu *vcpu)
3627{
Alexander Graf219b65d2009-06-15 15:21:25 +02003628 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003629
Joerg Roedele0231712010-02-24 18:59:10 +01003630 /*
3631 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3632 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3633 * get that intercept, this function will be called again though and
3634 * we'll get the vintr intercept.
3635 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003636 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003637 svm_set_vintr(svm);
3638 svm_inject_irq(svm, 0x0);
3639 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003640}
3641
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003642static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003643{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003644 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003645
Gleb Natapov44c11432009-05-11 13:35:52 +03003646 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3647 == HF_NMI_MASK)
3648 return; /* IRET will cause a vm exit */
3649
Joerg Roedele0231712010-02-24 18:59:10 +01003650 /*
3651 * Something prevents NMI from been injected. Single step over possible
3652 * problem (IRET or exception injection or interrupt shadow)
3653 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003654 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003655 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3656 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003657}
3658
Izik Eiduscbc94022007-10-25 00:29:55 +02003659static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3660{
3661 return 0;
3662}
3663
Avi Kivityd9e368d2007-06-07 19:18:30 +03003664static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3665{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003666 struct vcpu_svm *svm = to_svm(vcpu);
3667
3668 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3669 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3670 else
3671 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003672}
3673
Avi Kivity04d2cc72007-09-10 18:10:54 +03003674static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3675{
3676}
3677
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003678static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3679{
3680 struct vcpu_svm *svm = to_svm(vcpu);
3681
Joerg Roedel20307532010-11-29 17:51:48 +01003682 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003683 return;
3684
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003685 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003686 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003687 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003688 }
3689}
3690
Joerg Roedel649d6862008-04-16 16:51:15 +02003691static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3692{
3693 struct vcpu_svm *svm = to_svm(vcpu);
3694 u64 cr8;
3695
Joerg Roedel20307532010-11-29 17:51:48 +01003696 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003697 return;
3698
Joerg Roedel649d6862008-04-16 16:51:15 +02003699 cr8 = kvm_get_cr8(vcpu);
3700 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3701 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3702}
3703
Gleb Natapov9222be12009-04-23 17:14:37 +03003704static void svm_complete_interrupts(struct vcpu_svm *svm)
3705{
3706 u8 vector;
3707 int type;
3708 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003709 unsigned int3_injected = svm->int3_injected;
3710
3711 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003712
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003713 /*
3714 * If we've made progress since setting HF_IRET_MASK, we've
3715 * executed an IRET and can allow NMI injection.
3716 */
3717 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
3718 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003719 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003720 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3721 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003722
Gleb Natapov9222be12009-04-23 17:14:37 +03003723 svm->vcpu.arch.nmi_injected = false;
3724 kvm_clear_exception_queue(&svm->vcpu);
3725 kvm_clear_interrupt_queue(&svm->vcpu);
3726
3727 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3728 return;
3729
Avi Kivity3842d132010-07-27 12:30:24 +03003730 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3731
Gleb Natapov9222be12009-04-23 17:14:37 +03003732 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3733 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3734
3735 switch (type) {
3736 case SVM_EXITINTINFO_TYPE_NMI:
3737 svm->vcpu.arch.nmi_injected = true;
3738 break;
3739 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003740 /*
3741 * In case of software exceptions, do not reinject the vector,
3742 * but re-execute the instruction instead. Rewind RIP first
3743 * if we emulated INT3 before.
3744 */
3745 if (kvm_exception_is_soft(vector)) {
3746 if (vector == BP_VECTOR && int3_injected &&
3747 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3748 kvm_rip_write(&svm->vcpu,
3749 kvm_rip_read(&svm->vcpu) -
3750 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003751 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003752 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003753 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3754 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003755 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003756
3757 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003758 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003759 break;
3760 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003761 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003762 break;
3763 default:
3764 break;
3765 }
3766}
3767
Avi Kivityb463a6f2010-07-20 15:06:17 +03003768static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3769{
3770 struct vcpu_svm *svm = to_svm(vcpu);
3771 struct vmcb_control_area *control = &svm->vmcb->control;
3772
3773 control->exit_int_info = control->event_inj;
3774 control->exit_int_info_err = control->event_inj_err;
3775 control->event_inj = 0;
3776 svm_complete_interrupts(svm);
3777}
3778
Avi Kivity80e31d42008-07-14 14:44:59 +03003779#ifdef CONFIG_X86_64
3780#define R "r"
3781#else
3782#define R "e"
3783#endif
3784
Avi Kivity851ba692009-08-24 11:10:17 +03003785static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003787 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003788
Joerg Roedel2041a062010-04-22 12:33:08 +02003789 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3790 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3791 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3792
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003793 /*
3794 * A vmexit emulation is required before the vcpu can be executed
3795 * again.
3796 */
3797 if (unlikely(svm->nested.exit_required))
3798 return;
3799
Rusty Russelle756fc62007-07-30 20:07:08 +10003800 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003801
Joerg Roedel649d6862008-04-16 16:51:15 +02003802 sync_lapic_to_cr8(vcpu);
3803
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003804 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003805
Avi Kivity04d2cc72007-09-10 18:10:54 +03003806 clgi();
3807
3808 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003809
Avi Kivity6aa8b732006-12-10 02:21:36 -08003810 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003811 "push %%"R"bp; \n\t"
3812 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3813 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3814 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3815 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3816 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3817 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003818#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003819 "mov %c[r8](%[svm]), %%r8 \n\t"
3820 "mov %c[r9](%[svm]), %%r9 \n\t"
3821 "mov %c[r10](%[svm]), %%r10 \n\t"
3822 "mov %c[r11](%[svm]), %%r11 \n\t"
3823 "mov %c[r12](%[svm]), %%r12 \n\t"
3824 "mov %c[r13](%[svm]), %%r13 \n\t"
3825 "mov %c[r14](%[svm]), %%r14 \n\t"
3826 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003827#endif
3828
Avi Kivity6aa8b732006-12-10 02:21:36 -08003829 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003830 "push %%"R"ax \n\t"
3831 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003832 __ex(SVM_VMLOAD) "\n\t"
3833 __ex(SVM_VMRUN) "\n\t"
3834 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003835 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003836
3837 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003838 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3839 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3840 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3841 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3842 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3843 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003844#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003845 "mov %%r8, %c[r8](%[svm]) \n\t"
3846 "mov %%r9, %c[r9](%[svm]) \n\t"
3847 "mov %%r10, %c[r10](%[svm]) \n\t"
3848 "mov %%r11, %c[r11](%[svm]) \n\t"
3849 "mov %%r12, %c[r12](%[svm]) \n\t"
3850 "mov %%r13, %c[r13](%[svm]) \n\t"
3851 "mov %%r14, %c[r14](%[svm]) \n\t"
3852 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003853#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003854 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003855 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003856 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003858 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3859 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3860 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3861 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3862 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3863 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003864#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003865 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3866 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3867 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3868 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3869 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3870 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3871 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3872 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003873#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003874 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003875 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003876#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003877 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3878#endif
3879 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880
Avi Kivity82ca2d12010-10-21 12:20:34 +02003881#ifdef CONFIG_X86_64
3882 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3883#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003884 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02003885#ifndef CONFIG_X86_32_LAZY_GS
3886 loadsegment(gs, svm->host.gs);
3887#endif
Avi Kivity9581d442010-10-19 16:46:55 +02003888#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003889
3890 reload_tss(vcpu);
3891
Avi Kivity56ba47d2007-11-07 17:14:18 +02003892 local_irq_disable();
3893
Avi Kivity13c34e02010-10-21 12:20:31 +02003894 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3895 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3896 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3897 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3898
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02003899 trace_kvm_exit(svm->vmcb->control.exit_code, vcpu, KVM_ISA_SVM);
3900
Joerg Roedel3781c012011-01-14 16:45:02 +01003901 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3902 kvm_before_handle_nmi(&svm->vcpu);
3903
3904 stgi();
3905
3906 /* Any pending NMI will happen here */
3907
3908 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3909 kvm_after_handle_nmi(&svm->vcpu);
3910
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003911 sync_cr8_to_lapic(vcpu);
3912
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003913 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003914
Joerg Roedel38e5e922010-12-03 15:25:16 +01003915 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3916
Gleb Natapov631bc482010-10-14 11:22:52 +02003917 /* if exit due to PF check for async PF */
3918 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3919 svm->apf_reason = kvm_read_and_reset_pf_reason();
3920
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003921 if (npt_enabled) {
3922 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3923 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3924 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003925
3926 /*
3927 * We need to handle MC intercepts here before the vcpu has a chance to
3928 * change the physical cpu
3929 */
3930 if (unlikely(svm->vmcb->control.exit_code ==
3931 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3932 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003933
3934 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003935}
3936
Avi Kivity80e31d42008-07-14 14:44:59 +03003937#undef R
3938
Avi Kivity6aa8b732006-12-10 02:21:36 -08003939static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3940{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003941 struct vcpu_svm *svm = to_svm(vcpu);
3942
3943 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003944 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003945 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003946}
3947
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003948static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3949{
3950 struct vcpu_svm *svm = to_svm(vcpu);
3951
3952 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01003953 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003954
3955 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02003956 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003957 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003958
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003959 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003960}
3961
Avi Kivity6aa8b732006-12-10 02:21:36 -08003962static int is_disabled(void)
3963{
Joerg Roedel6031a612007-06-22 12:29:50 +03003964 u64 vm_cr;
3965
3966 rdmsrl(MSR_VM_CR, vm_cr);
3967 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3968 return 1;
3969
Avi Kivity6aa8b732006-12-10 02:21:36 -08003970 return 0;
3971}
3972
Ingo Molnar102d8322007-02-19 14:37:47 +02003973static void
3974svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3975{
3976 /*
3977 * Patch in the VMMCALL instruction:
3978 */
3979 hypercall[0] = 0x0f;
3980 hypercall[1] = 0x01;
3981 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003982}
3983
Yang, Sheng002c7f72007-07-31 14:23:01 +03003984static void svm_check_processor_compat(void *rtn)
3985{
3986 *(int *)rtn = 0;
3987}
3988
Avi Kivity774ead32007-12-26 13:57:04 +02003989static bool svm_cpu_has_accelerated_tpr(void)
3990{
3991 return false;
3992}
3993
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003994static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003995{
3996 return 0;
3997}
3998
Sheng Yang0e851882009-12-18 16:48:46 +08003999static void svm_cpuid_update(struct kvm_vcpu *vcpu)
4000{
4001}
4002
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004003static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
4004{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004005 switch (func) {
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02004006 case 0x80000001:
4007 if (nested)
4008 entry->ecx |= (1 << 2); /* Set SVM bit */
4009 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004010 case 0x8000000A:
4011 entry->eax = 1; /* SVM revision 1 */
4012 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
4013 ASID emulation to nested SVM */
4014 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02004015 entry->edx = 0; /* Per default do not support any
4016 additional features */
4017
4018 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004019 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02004020 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004021
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02004022 /* Support NPT for the guest if enabled */
4023 if (npt_enabled)
4024 entry->edx |= SVM_FEATURE_NPT;
4025
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004026 break;
4027 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004028}
4029
Sheng Yang17cc3932010-01-05 19:02:27 +08004030static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004031{
Sheng Yang17cc3932010-01-05 19:02:27 +08004032 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004033}
4034
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004035static bool svm_rdtscp_supported(void)
4036{
4037 return false;
4038}
4039
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004040static bool svm_has_wbinvd_exit(void)
4041{
4042 return true;
4043}
4044
Avi Kivity02daab22009-12-30 12:40:26 +02004045static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
4046{
4047 struct vcpu_svm *svm = to_svm(vcpu);
4048
Joerg Roedel18c918c2010-11-30 18:03:59 +01004049 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01004050 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02004051}
4052
Joerg Roedel80612522011-04-04 12:39:33 +02004053#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004054 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004055#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004056 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004057#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004058 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004059
4060static struct __x86_intercept {
4061 u32 exit_code;
4062 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004063} x86_intercept_map[] = {
4064 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4065 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4066 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4067 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4068 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004069 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4070 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004071 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4072 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4073 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4074 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4075 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4076 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4077 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4078 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004079 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4080 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4081 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4082 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4083 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4084 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4085 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4086 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004087 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4088 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4089 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004090 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4091 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4092 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4093 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4094 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4095 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4096 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4097 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4098 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004099 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4100 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4101 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4102 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4103 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4104 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4105 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004106 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4107 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4108 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4109 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004110};
4111
Joerg Roedel80612522011-04-04 12:39:33 +02004112#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004113#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004114#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004115
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004116static int svm_check_intercept(struct kvm_vcpu *vcpu,
4117 struct x86_instruction_info *info,
4118 enum x86_intercept_stage stage)
4119{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004120 struct vcpu_svm *svm = to_svm(vcpu);
4121 int vmexit, ret = X86EMUL_CONTINUE;
4122 struct __x86_intercept icpt_info;
4123 struct vmcb *vmcb = svm->vmcb;
4124
4125 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4126 goto out;
4127
4128 icpt_info = x86_intercept_map[info->intercept];
4129
Avi Kivity40e19b52011-04-21 12:35:41 +03004130 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004131 goto out;
4132
4133 switch (icpt_info.exit_code) {
4134 case SVM_EXIT_READ_CR0:
4135 if (info->intercept == x86_intercept_cr_read)
4136 icpt_info.exit_code += info->modrm_reg;
4137 break;
4138 case SVM_EXIT_WRITE_CR0: {
4139 unsigned long cr0, val;
4140 u64 intercept;
4141
4142 if (info->intercept == x86_intercept_cr_write)
4143 icpt_info.exit_code += info->modrm_reg;
4144
4145 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0)
4146 break;
4147
4148 intercept = svm->nested.intercept;
4149
4150 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
4151 break;
4152
4153 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4154 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4155
4156 if (info->intercept == x86_intercept_lmsw) {
4157 cr0 &= 0xfUL;
4158 val &= 0xfUL;
4159 /* lmsw can't clear PE - catch this here */
4160 if (cr0 & X86_CR0_PE)
4161 val |= X86_CR0_PE;
4162 }
4163
4164 if (cr0 ^ val)
4165 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4166
4167 break;
4168 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004169 case SVM_EXIT_READ_DR0:
4170 case SVM_EXIT_WRITE_DR0:
4171 icpt_info.exit_code += info->modrm_reg;
4172 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004173 case SVM_EXIT_MSR:
4174 if (info->intercept == x86_intercept_wrmsr)
4175 vmcb->control.exit_info_1 = 1;
4176 else
4177 vmcb->control.exit_info_1 = 0;
4178 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004179 case SVM_EXIT_PAUSE:
4180 /*
4181 * We get this for NOP only, but pause
4182 * is rep not, check this here
4183 */
4184 if (info->rep_prefix != REPE_PREFIX)
4185 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02004186 case SVM_EXIT_IOIO: {
4187 u64 exit_info;
4188 u32 bytes;
4189
4190 exit_info = (vcpu->arch.regs[VCPU_REGS_RDX] & 0xffff) << 16;
4191
4192 if (info->intercept == x86_intercept_in ||
4193 info->intercept == x86_intercept_ins) {
4194 exit_info |= SVM_IOIO_TYPE_MASK;
4195 bytes = info->src_bytes;
4196 } else {
4197 bytes = info->dst_bytes;
4198 }
4199
4200 if (info->intercept == x86_intercept_outs ||
4201 info->intercept == x86_intercept_ins)
4202 exit_info |= SVM_IOIO_STR_MASK;
4203
4204 if (info->rep_prefix)
4205 exit_info |= SVM_IOIO_REP_MASK;
4206
4207 bytes = min(bytes, 4u);
4208
4209 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4210
4211 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4212
4213 vmcb->control.exit_info_1 = exit_info;
4214 vmcb->control.exit_info_2 = info->next_rip;
4215
4216 break;
4217 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004218 default:
4219 break;
4220 }
4221
4222 vmcb->control.next_rip = info->next_rip;
4223 vmcb->control.exit_code = icpt_info.exit_code;
4224 vmexit = nested_svm_exit_handled(svm);
4225
4226 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4227 : X86EMUL_CONTINUE;
4228
4229out:
4230 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004231}
4232
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004233static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004234 .cpu_has_kvm_support = has_svm,
4235 .disabled_by_bios = is_disabled,
4236 .hardware_setup = svm_hardware_setup,
4237 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004238 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004239 .hardware_enable = svm_hardware_enable,
4240 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004241 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004242
4243 .vcpu_create = svm_create_vcpu,
4244 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004245 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004246
Avi Kivity04d2cc72007-09-10 18:10:54 +03004247 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004248 .vcpu_load = svm_vcpu_load,
4249 .vcpu_put = svm_vcpu_put,
4250
4251 .set_guest_debug = svm_guest_debug,
4252 .get_msr = svm_get_msr,
4253 .set_msr = svm_set_msr,
4254 .get_segment_base = svm_get_segment_base,
4255 .get_segment = svm_get_segment,
4256 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004257 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004258 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004259 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004260 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004261 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004262 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004263 .set_cr3 = svm_set_cr3,
4264 .set_cr4 = svm_set_cr4,
4265 .set_efer = svm_set_efer,
4266 .get_idt = svm_get_idt,
4267 .set_idt = svm_set_idt,
4268 .get_gdt = svm_get_gdt,
4269 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004270 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004271 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272 .get_rflags = svm_get_rflags,
4273 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02004274 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004275 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004276
Avi Kivity6aa8b732006-12-10 02:21:36 -08004277 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004278
Avi Kivity6aa8b732006-12-10 02:21:36 -08004279 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004280 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004281 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004282 .set_interrupt_shadow = svm_set_interrupt_shadow,
4283 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004284 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004285 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004286 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004287 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004288 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004289 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004290 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004291 .get_nmi_mask = svm_get_nmi_mask,
4292 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004293 .enable_nmi_window = enable_nmi_window,
4294 .enable_irq_window = enable_irq_window,
4295 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02004296
4297 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004298 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004299 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004300
Avi Kivity586f9602010-11-18 13:09:54 +02004301 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004302
Sheng Yang17cc3932010-01-05 19:02:27 +08004303 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004304
4305 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004306
4307 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004308
4309 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004310
4311 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004312
Joerg Roedel4051b182011-03-25 09:44:49 +01004313 .set_tsc_khz = svm_set_tsc_khz,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004314 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004315 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01004316 .compute_tsc_offset = svm_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03004317 .read_l1_tsc = svm_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004318
4319 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004320
4321 .check_intercept = svm_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004322};
4323
4324static int __init svm_init(void)
4325{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004326 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004327 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004328}
4329
4330static void __exit svm_exit(void)
4331{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004332 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004333}
4334
4335module_init(svm_init)
4336module_exit(svm_exit)