blob: 192cad25d3219dbac9b31abe75aaec08c887b92a [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 */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050017
18#define pr_fmt(fmt) "SVM: " fmt
19
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
21
Eddie Dong85f455f2007-07-06 12:20:49 +030022#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080023#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020025#include "x86.h"
Julian Stecklina66f7b722012-12-05 15:26:19 +010026#include "cpuid.h"
Wei Huang25462f72015-06-19 15:45:05 +020027#include "pmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040028
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/module.h>
Josh Triplettae759542012-03-28 11:32:28 -070030#include <linux/mod_devicetable.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020031#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/vmalloc.h>
33#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040034#include <linux/sched.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040035#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080037
Joerg Roedel1018faa2012-02-29 14:57:32 +010038#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020039#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040040#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010041#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020042#include <asm/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043
Eduardo Habkost63d11422008-11-17 19:03:20 -020044#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030045#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020046
Avi Kivity4ecac3f2008-05-13 13:23:38 +030047#define __ex(x) __kvm_handle_fault_on_reboot(x)
48
Avi Kivity6aa8b732006-12-10 02:21:36 -080049MODULE_AUTHOR("Qumranet");
50MODULE_LICENSE("GPL");
51
Josh Triplettae759542012-03-28 11:32:28 -070052static const struct x86_cpu_id svm_cpu_id[] = {
53 X86_FEATURE_MATCH(X86_FEATURE_SVM),
54 {}
55};
56MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
57
Avi Kivity6aa8b732006-12-10 02:21:36 -080058#define IOPM_ALLOC_ORDER 2
59#define MSRPM_ALLOC_ORDER 1
60
Avi Kivity6aa8b732006-12-10 02:21:36 -080061#define SEG_TYPE_LDT 2
62#define SEG_TYPE_BUSY_TSS16 3
63
Andre Przywara6bc31bd2010-04-11 23:07:28 +020064#define SVM_FEATURE_NPT (1 << 0)
65#define SVM_FEATURE_LBRV (1 << 1)
66#define SVM_FEATURE_SVML (1 << 2)
67#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010068#define SVM_FEATURE_TSC_RATE (1 << 4)
69#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
70#define SVM_FEATURE_FLUSH_ASID (1 << 6)
71#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020072#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030073
Joerg Roedel410e4d52009-08-07 11:49:44 +020074#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
75#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
76#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
77
Joerg Roedel24e09cb2008-02-13 18:58:47 +010078#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
79
Joerg Roedelfbc0db72011-03-25 09:44:46 +010080#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010081#define TSC_RATIO_MIN 0x0000000000000001ULL
82#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010083
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050084#define AVIC_HPA_MASK ~((0xFFFULL << 52) || 0xFFF)
85
86/*
87 * 0xff is broadcast, so the max index allowed for physical APIC ID
88 * table is 0xfe. APIC IDs above 0xff are reserved.
89 */
90#define AVIC_MAX_PHYSICAL_ID_COUNT 255
91
Joerg Roedel67ec6602010-05-17 14:43:35 +020092static bool erratum_383_found __read_mostly;
93
Avi Kivity6c8166a2009-05-31 18:15:37 +030094static const u32 host_save_user_msrs[] = {
95#ifdef CONFIG_X86_64
96 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
97 MSR_FS_BASE,
98#endif
99 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Paolo Bonzini46896c72015-11-12 14:49:16 +0100100 MSR_TSC_AUX,
Avi Kivity6c8166a2009-05-31 18:15:37 +0300101};
102
103#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
104
105struct kvm_vcpu;
106
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200107struct nested_state {
108 struct vmcb *hsave;
109 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +0100110 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200111 u64 vmcb;
112
113 /* These are the merged vectors */
114 u32 *msrpm;
115
116 /* gpa pointers to the real vectors */
117 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100118 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200119
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200120 /* A VMEXIT is required but not yet emulated */
121 bool exit_required;
122
Joerg Roedelaad42c62009-08-07 11:49:34 +0200123 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100124 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100125 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200126 u32 intercept_exceptions;
127 u64 intercept;
128
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200129 /* Nested Paging related state */
130 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200131};
132
Joerg Roedel323c3d82010-03-01 15:34:37 +0100133#define MSRPM_OFFSETS 16
134static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
135
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500136/*
137 * Set osvw_len to higher value when updated Revision Guides
138 * are published and we know what the new status bits are
139 */
140static uint64_t osvw_len = 4, osvw_status;
141
Avi Kivity6c8166a2009-05-31 18:15:37 +0300142struct vcpu_svm {
143 struct kvm_vcpu vcpu;
144 struct vmcb *vmcb;
145 unsigned long vmcb_pa;
146 struct svm_cpu_data *svm_data;
147 uint64_t asid_generation;
148 uint64_t sysenter_esp;
149 uint64_t sysenter_eip;
Paolo Bonzini46896c72015-11-12 14:49:16 +0100150 uint64_t tsc_aux;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300151
152 u64 next_rip;
153
154 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200155 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200156 u16 fs;
157 u16 gs;
158 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200159 u64 gs_base;
160 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300161
162 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300163
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200164 ulong nmi_iret_rip;
165
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200166 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200167
168 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100169
170 unsigned int3_injected;
171 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200172 u32 apf_reason;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100173
Joerg Roedel6092d3d2015-10-14 15:10:54 +0200174 /* cached guest cpuid flags for faster access */
175 bool nrips_enabled : 1;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500176
177 struct page *avic_backing_page;
178 u64 *avic_physical_id_cache;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300179};
180
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500181#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
182#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
183
184#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
185#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
186#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
187#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
188
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100189static DEFINE_PER_CPU(u64, current_tsc_ratio);
190#define TSC_RATIO_DEFAULT 0x0100000000ULL
191
Joerg Roedel455716f2010-03-01 15:34:35 +0100192#define MSR_INVALID 0xffffffffU
193
Mathias Krause09941fb2012-08-30 01:30:20 +0200194static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100195 u32 index; /* Index of the MSR */
196 bool always; /* True if intercept is always on */
197} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400198 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100199 { .index = MSR_IA32_SYSENTER_CS, .always = true },
200#ifdef CONFIG_X86_64
201 { .index = MSR_GS_BASE, .always = true },
202 { .index = MSR_FS_BASE, .always = true },
203 { .index = MSR_KERNEL_GS_BASE, .always = true },
204 { .index = MSR_LSTAR, .always = true },
205 { .index = MSR_CSTAR, .always = true },
206 { .index = MSR_SYSCALL_MASK, .always = true },
207#endif
208 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
209 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
210 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
211 { .index = MSR_IA32_LASTINTTOIP, .always = false },
212 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213};
214
215/* enable NPT for AMD64 and X86 with PAE */
216#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
217static bool npt_enabled = true;
218#else
Joerg Roedele0231712010-02-24 18:59:10 +0100219static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220#endif
221
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100222/* allow nested paging (virtualized MMU) for all guests */
223static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224module_param(npt, int, S_IRUGO);
225
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100226/* allow nested virtualization in KVM/SVM */
227static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228module_param(nested, int, S_IRUGO);
229
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500230/* enable / disable AVIC */
231static int avic;
232module_param(avic, int, S_IRUGO);
233
Paolo Bonzini79a80592015-09-21 07:46:55 +0200234static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200236static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237
Joerg Roedel410e4d52009-08-07 11:49:44 +0200238static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100239static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
242 bool has_error_code, u32 error_code);
243
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100244enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100245 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
246 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100247 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100248 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100249 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100250 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100251 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100252 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100253 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100254 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100255 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100256 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500257 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
258 * AVIC PHYSICAL_TABLE pointer,
259 * AVIC LOGICAL_TABLE pointer
260 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100261 VMCB_DIRTY_MAX,
262};
263
Joerg Roedel0574dec2010-12-03 11:45:58 +0100264/* TPR and CR2 are always written before VMRUN */
265#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100266
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500267#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
268
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100269static inline void mark_all_dirty(struct vmcb *vmcb)
270{
271 vmcb->control.clean = 0;
272}
273
274static inline void mark_all_clean(struct vmcb *vmcb)
275{
276 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
277 & ~VMCB_ALWAYS_DIRTY_MASK;
278}
279
280static inline void mark_dirty(struct vmcb *vmcb, int bit)
281{
282 vmcb->control.clean &= ~(1 << bit);
283}
284
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
286{
287 return container_of(vcpu, struct vcpu_svm, vcpu);
288}
289
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500290static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
291{
292 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
293 mark_dirty(svm->vmcb, VMCB_AVIC);
294}
295
Joerg Roedel384c6362010-11-30 18:03:56 +0100296static void recalc_intercepts(struct vcpu_svm *svm)
297{
298 struct vmcb_control_area *c, *h;
299 struct nested_state *g;
300
Joerg Roedel116a0a22010-12-03 11:45:49 +0100301 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
302
Joerg Roedel384c6362010-11-30 18:03:56 +0100303 if (!is_guest_mode(&svm->vcpu))
304 return;
305
306 c = &svm->vmcb->control;
307 h = &svm->nested.hsave->control;
308 g = &svm->nested;
309
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100310 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100311 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100312 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
313 c->intercept = h->intercept | g->intercept;
314}
315
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100316static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
317{
318 if (is_guest_mode(&svm->vcpu))
319 return svm->nested.hsave;
320 else
321 return svm->vmcb;
322}
323
324static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
325{
326 struct vmcb *vmcb = get_host_vmcb(svm);
327
328 vmcb->control.intercept_cr |= (1U << bit);
329
330 recalc_intercepts(svm);
331}
332
333static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
334{
335 struct vmcb *vmcb = get_host_vmcb(svm);
336
337 vmcb->control.intercept_cr &= ~(1U << bit);
338
339 recalc_intercepts(svm);
340}
341
342static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
343{
344 struct vmcb *vmcb = get_host_vmcb(svm);
345
346 return vmcb->control.intercept_cr & (1U << bit);
347}
348
Paolo Bonzini5315c712014-03-03 13:08:29 +0100349static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100350{
351 struct vmcb *vmcb = get_host_vmcb(svm);
352
Paolo Bonzini5315c712014-03-03 13:08:29 +0100353 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
354 | (1 << INTERCEPT_DR1_READ)
355 | (1 << INTERCEPT_DR2_READ)
356 | (1 << INTERCEPT_DR3_READ)
357 | (1 << INTERCEPT_DR4_READ)
358 | (1 << INTERCEPT_DR5_READ)
359 | (1 << INTERCEPT_DR6_READ)
360 | (1 << INTERCEPT_DR7_READ)
361 | (1 << INTERCEPT_DR0_WRITE)
362 | (1 << INTERCEPT_DR1_WRITE)
363 | (1 << INTERCEPT_DR2_WRITE)
364 | (1 << INTERCEPT_DR3_WRITE)
365 | (1 << INTERCEPT_DR4_WRITE)
366 | (1 << INTERCEPT_DR5_WRITE)
367 | (1 << INTERCEPT_DR6_WRITE)
368 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100369
370 recalc_intercepts(svm);
371}
372
Paolo Bonzini5315c712014-03-03 13:08:29 +0100373static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100374{
375 struct vmcb *vmcb = get_host_vmcb(svm);
376
Paolo Bonzini5315c712014-03-03 13:08:29 +0100377 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100378
379 recalc_intercepts(svm);
380}
381
Joerg Roedel18c918c2010-11-30 18:03:59 +0100382static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
383{
384 struct vmcb *vmcb = get_host_vmcb(svm);
385
386 vmcb->control.intercept_exceptions |= (1U << bit);
387
388 recalc_intercepts(svm);
389}
390
391static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
392{
393 struct vmcb *vmcb = get_host_vmcb(svm);
394
395 vmcb->control.intercept_exceptions &= ~(1U << bit);
396
397 recalc_intercepts(svm);
398}
399
Joerg Roedel8a05a1b2010-11-30 18:04:00 +0100400static inline void set_intercept(struct vcpu_svm *svm, int bit)
401{
402 struct vmcb *vmcb = get_host_vmcb(svm);
403
404 vmcb->control.intercept |= (1ULL << bit);
405
406 recalc_intercepts(svm);
407}
408
409static inline void clr_intercept(struct vcpu_svm *svm, int bit)
410{
411 struct vmcb *vmcb = get_host_vmcb(svm);
412
413 vmcb->control.intercept &= ~(1ULL << bit);
414
415 recalc_intercepts(svm);
416}
417
Joerg Roedel2af91942009-08-07 11:49:28 +0200418static inline void enable_gif(struct vcpu_svm *svm)
419{
420 svm->vcpu.arch.hflags |= HF_GIF_MASK;
421}
422
423static inline void disable_gif(struct vcpu_svm *svm)
424{
425 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
426}
427
428static inline bool gif_set(struct vcpu_svm *svm)
429{
430 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
431}
432
Avi Kivity6aa8b732006-12-10 02:21:36 -0800433static unsigned long iopm_base;
434
435struct kvm_ldttss_desc {
436 u16 limit0;
437 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100438 unsigned base1:8, type:5, dpl:2, p:1;
439 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 u32 base3;
441 u32 zero1;
442} __attribute__((packed));
443
444struct svm_cpu_data {
445 int cpu;
446
Avi Kivity5008fdf2007-04-02 13:05:50 +0300447 u64 asid_generation;
448 u32 max_asid;
449 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 struct kvm_ldttss_desc *tss_desc;
451
452 struct page *save_area;
453};
454
455static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
456
457struct svm_init_data {
458 int cpu;
459 int r;
460};
461
Mathias Krause09941fb2012-08-30 01:30:20 +0200462static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200464#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465#define MSRS_RANGE_SIZE 2048
466#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
467
Joerg Roedel455716f2010-03-01 15:34:35 +0100468static u32 svm_msrpm_offset(u32 msr)
469{
470 u32 offset;
471 int i;
472
473 for (i = 0; i < NUM_MSR_MAPS; i++) {
474 if (msr < msrpm_ranges[i] ||
475 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
476 continue;
477
478 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
479 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
480
481 /* Now we have the u8 offset - but need the u32 offset */
482 return offset / 4;
483 }
484
485 /* MSR not in any range */
486 return MSR_INVALID;
487}
488
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489#define MAX_INST_SIZE 15
490
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491static inline void clgi(void)
492{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300493 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800494}
495
496static inline void stgi(void)
497{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300498 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499}
500
501static inline void invlpga(unsigned long addr, u32 asid)
502{
Joerg Roedele0231712010-02-24 18:59:10 +0100503 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504}
505
Joerg Roedel4b161842010-09-10 17:31:03 +0200506static int get_npt_level(void)
507{
508#ifdef CONFIG_X86_64
509 return PT64_ROOT_LEVEL;
510#else
511 return PT32E_ROOT_LEVEL;
512#endif
513}
514
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
516{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000517 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100518 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600519 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520
Alexander Graf9962d032008-11-25 20:17:02 +0100521 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100522 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800523}
524
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525static int is_external_interrupt(u32 info)
526{
527 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
528 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
529}
530
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200531static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400532{
533 struct vcpu_svm *svm = to_svm(vcpu);
534 u32 ret = 0;
535
536 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200537 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
538 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400539}
540
541static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
542{
543 struct vcpu_svm *svm = to_svm(vcpu);
544
545 if (mask == 0)
546 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
547 else
548 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
549
550}
551
Avi Kivity6aa8b732006-12-10 02:21:36 -0800552static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
553{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400554 struct vcpu_svm *svm = to_svm(vcpu);
555
Bandan Dasf1047652015-06-11 02:05:33 -0400556 if (svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200557 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200558 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400559 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200560
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400561 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100562 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300563 EMULATE_DONE)
564 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565 return;
566 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300567 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
568 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
569 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300571 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400572 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573}
574
Jan Kiszka116a4752010-02-23 17:47:54 +0100575static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200576 bool has_error_code, u32 error_code,
577 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100578{
579 struct vcpu_svm *svm = to_svm(vcpu);
580
Joerg Roedele0231712010-02-24 18:59:10 +0100581 /*
582 * If we are within a nested VM we'd better #VMEXIT and let the guest
583 * handle the exception
584 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200585 if (!reinject &&
586 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100587 return;
588
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200589 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100590 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
591
592 /*
593 * For guest debugging where we have to reinject #BP if some
594 * INT3 is guest-owned:
595 * Emulate nRIP by moving RIP forward. Will fail if injection
596 * raises a fault that is not intercepted. Still better than
597 * failing in all cases.
598 */
599 skip_emulated_instruction(&svm->vcpu);
600 rip = kvm_rip_read(&svm->vcpu);
601 svm->int3_rip = rip + svm->vmcb->save.cs.base;
602 svm->int3_injected = rip - old_rip;
603 }
604
Jan Kiszka116a4752010-02-23 17:47:54 +0100605 svm->vmcb->control.event_inj = nr
606 | SVM_EVTINJ_VALID
607 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
608 | SVM_EVTINJ_TYPE_EXEPT;
609 svm->vmcb->control.event_inj_err = error_code;
610}
611
Joerg Roedel67ec6602010-05-17 14:43:35 +0200612static void svm_init_erratum_383(void)
613{
614 u32 low, high;
615 int err;
616 u64 val;
617
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100618 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200619 return;
620
621 /* Use _safe variants to not break nested virtualization */
622 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
623 if (err)
624 return;
625
626 val |= (1ULL << 47);
627
628 low = lower_32_bits(val);
629 high = upper_32_bits(val);
630
631 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
632
633 erratum_383_found = true;
634}
635
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500636static void svm_init_osvw(struct kvm_vcpu *vcpu)
637{
638 /*
639 * Guests should see errata 400 and 415 as fixed (assuming that
640 * HLT and IO instructions are intercepted).
641 */
642 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
643 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
644
645 /*
646 * By increasing VCPU's osvw.length to 3 we are telling the guest that
647 * all osvw.status bits inside that length, including bit 0 (which is
648 * reserved for erratum 298), are valid. However, if host processor's
649 * osvw_len is 0 then osvw_status[0] carries no information. We need to
650 * be conservative here and therefore we tell the guest that erratum 298
651 * is present (because we really don't know).
652 */
653 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
654 vcpu->arch.osvw.status |= 1;
655}
656
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657static int has_svm(void)
658{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200659 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660
Eduardo Habkost63d11422008-11-17 19:03:20 -0200661 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800662 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663 return 0;
664 }
665
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 return 1;
667}
668
Radim Krčmář13a34e02014-08-28 15:13:03 +0200669static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800670{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100671 /* Make sure we clean up behind us */
672 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
673 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
674
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200675 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100676
677 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678}
679
Radim Krčmář13a34e02014-08-28 15:13:03 +0200680static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681{
682
Tejun Heo0fe1e002009-10-29 22:34:14 +0900683 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200685 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686 struct desc_struct *gdt;
687 int me = raw_smp_processor_id();
688
Alexander Graf10474ae2009-09-15 11:37:46 +0200689 rdmsrl(MSR_EFER, efer);
690 if (efer & EFER_SVME)
691 return -EBUSY;
692
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200694 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200695 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900697 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900698 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200699 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200700 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701 }
702
Tejun Heo0fe1e002009-10-29 22:34:14 +0900703 sd->asid_generation = 1;
704 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
705 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200707 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200708 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900709 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710
Alexander Graf9962d032008-11-25 20:17:02 +0100711 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712
Linus Torvaldsd0316552009-12-14 09:58:24 -0800713 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200714
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100715 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
716 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500717 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100718 }
719
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500720
721 /*
722 * Get OSVW bits.
723 *
724 * Note that it is possible to have a system with mixed processor
725 * revisions and therefore different OSVW bits. If bits are not the same
726 * on different processors then choose the worst case (i.e. if erratum
727 * is present on one processor and not on another then assume that the
728 * erratum is present everywhere).
729 */
730 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
731 uint64_t len, status = 0;
732 int err;
733
734 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
735 if (!err)
736 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
737 &err);
738
739 if (err)
740 osvw_status = osvw_len = 0;
741 else {
742 if (len < osvw_len)
743 osvw_len = len;
744 osvw_status |= status;
745 osvw_status &= (1ULL << osvw_len) - 1;
746 }
747 } else
748 osvw_status = osvw_len = 0;
749
Joerg Roedel67ec6602010-05-17 14:43:35 +0200750 svm_init_erratum_383();
751
Joerg Roedel1018faa2012-02-29 14:57:32 +0100752 amd_pmu_enable_virt();
753
Alexander Graf10474ae2009-09-15 11:37:46 +0200754 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755}
756
Joerg Roedel0da1db752008-07-02 16:02:11 +0200757static void svm_cpu_uninit(int cpu)
758{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900759 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200760
Tejun Heo0fe1e002009-10-29 22:34:14 +0900761 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200762 return;
763
764 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900765 __free_page(sd->save_area);
766 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200767}
768
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769static int svm_cpu_init(int cpu)
770{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900771 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 int r;
773
Tejun Heo0fe1e002009-10-29 22:34:14 +0900774 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
775 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900777 sd->cpu = cpu;
778 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900780 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 goto err_1;
782
Tejun Heo0fe1e002009-10-29 22:34:14 +0900783 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784
785 return 0;
786
787err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900788 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 return r;
790
791}
792
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100793static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794{
795 int i;
796
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100797 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
798 if (direct_access_msrs[i].index == index)
799 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100801 return false;
802}
803
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804static void set_msr_interception(u32 *msrpm, unsigned msr,
805 int read, int write)
806{
Joerg Roedel455716f2010-03-01 15:34:35 +0100807 u8 bit_read, bit_write;
808 unsigned long tmp;
809 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100811 /*
812 * If this warning triggers extend the direct_access_msrs list at the
813 * beginning of the file
814 */
815 WARN_ON(!valid_msr_intercept(msr));
816
Joerg Roedel455716f2010-03-01 15:34:35 +0100817 offset = svm_msrpm_offset(msr);
818 bit_read = 2 * (msr & 0x0f);
819 bit_write = 2 * (msr & 0x0f) + 1;
820 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821
Joerg Roedel455716f2010-03-01 15:34:35 +0100822 BUG_ON(offset == MSR_INVALID);
823
824 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
825 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
826
827 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828}
829
Joerg Roedelf65c2292008-02-13 18:58:46 +0100830static void svm_vcpu_init_msrpm(u32 *msrpm)
831{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100832 int i;
833
Joerg Roedelf65c2292008-02-13 18:58:46 +0100834 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
835
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100836 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
837 if (!direct_access_msrs[i].always)
838 continue;
839
840 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
841 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100842}
843
Joerg Roedel323c3d82010-03-01 15:34:37 +0100844static void add_msr_offset(u32 offset)
845{
846 int i;
847
848 for (i = 0; i < MSRPM_OFFSETS; ++i) {
849
850 /* Offset already in list? */
851 if (msrpm_offsets[i] == offset)
852 return;
853
854 /* Slot used by another offset? */
855 if (msrpm_offsets[i] != MSR_INVALID)
856 continue;
857
858 /* Add offset to list */
859 msrpm_offsets[i] = offset;
860
861 return;
862 }
863
864 /*
865 * If this BUG triggers the msrpm_offsets table has an overflow. Just
866 * increase MSRPM_OFFSETS in this case.
867 */
868 BUG();
869}
870
871static void init_msrpm_offsets(void)
872{
873 int i;
874
875 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
876
877 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
878 u32 offset;
879
880 offset = svm_msrpm_offset(direct_access_msrs[i].index);
881 BUG_ON(offset == MSR_INVALID);
882
883 add_msr_offset(offset);
884 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885}
886
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100887static void svm_enable_lbrv(struct vcpu_svm *svm)
888{
889 u32 *msrpm = svm->msrpm;
890
891 svm->vmcb->control.lbr_ctl = 1;
892 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
893 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
894 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
895 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
896}
897
898static void svm_disable_lbrv(struct vcpu_svm *svm)
899{
900 u32 *msrpm = svm->msrpm;
901
902 svm->vmcb->control.lbr_ctl = 0;
903 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
904 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
905 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
906 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
907}
908
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909static __init int svm_hardware_setup(void)
910{
911 int cpu;
912 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100913 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 int r;
915
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
917
918 if (!iopm_pages)
919 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300920
921 iopm_va = page_address(iopm_pages);
922 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
924
Joerg Roedel323c3d82010-03-01 15:34:37 +0100925 init_msrpm_offsets();
926
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100927 if (boot_cpu_has(X86_FEATURE_NX))
928 kvm_enable_efer_bits(EFER_NX);
929
Alexander Graf1b2fd702009-02-02 16:23:51 +0100930 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
931 kvm_enable_efer_bits(EFER_FFXSR);
932
Joerg Roedel92a1f122011-03-25 09:44:51 +0100933 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +0100934 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800935 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
936 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100937 }
938
Alexander Graf236de052008-11-25 20:17:10 +0100939 if (nested) {
940 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200941 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100942 }
943
Zachary Amsden3230bb42009-09-29 11:38:37 -1000944 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 r = svm_cpu_init(cpu);
946 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100947 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100949
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200950 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100951 npt_enabled = false;
952
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100953 if (npt_enabled && !npt) {
954 printk(KERN_INFO "kvm: Nested Paging disabled\n");
955 npt_enabled = false;
956 }
957
Joerg Roedel18552672008-02-07 13:47:41 +0100958 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100959 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100960 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200961 } else
962 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100963
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500964 if (avic && (!npt_enabled || !boot_cpu_has(X86_FEATURE_AVIC)))
965 avic = false;
966
967 if (avic)
968 pr_info("AVIC enabled\n");
969
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 return 0;
971
Joerg Roedelf65c2292008-02-13 18:58:46 +0100972err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
974 iopm_base = 0;
975 return r;
976}
977
978static __exit void svm_hardware_unsetup(void)
979{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200980 int cpu;
981
Zachary Amsden3230bb42009-09-29 11:38:37 -1000982 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200983 svm_cpu_uninit(cpu);
984
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100986 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987}
988
989static void init_seg(struct vmcb_seg *seg)
990{
991 seg->selector = 0;
992 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100993 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 seg->limit = 0xffff;
995 seg->base = 0;
996}
997
998static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
999{
1000 seg->selector = 0;
1001 seg->attrib = SVM_SELECTOR_P_MASK | type;
1002 seg->limit = 0xffff;
1003 seg->base = 0;
1004}
1005
Will Auldba904632012-11-29 12:42:50 -08001006static u64 svm_read_tsc_offset(struct kvm_vcpu *vcpu)
1007{
1008 struct vcpu_svm *svm = to_svm(vcpu);
1009
1010 return svm->vmcb->control.tsc_offset;
1011}
1012
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001013static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1014{
1015 struct vcpu_svm *svm = to_svm(vcpu);
1016 u64 g_tsc_offset = 0;
1017
Joerg Roedel20307532010-11-29 17:51:48 +01001018 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001019 g_tsc_offset = svm->vmcb->control.tsc_offset -
1020 svm->nested.hsave->control.tsc_offset;
1021 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001022 } else
1023 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1024 svm->vmcb->control.tsc_offset,
1025 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001026
1027 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001028
1029 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001030}
1031
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001032static void svm_adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, s64 adjustment)
Zachary Amsdene48672f2010-08-19 22:07:23 -10001033{
1034 struct vcpu_svm *svm = to_svm(vcpu);
1035
1036 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +01001037 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -10001038 svm->nested.hsave->control.tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001039 else
1040 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1041 svm->vmcb->control.tsc_offset - adjustment,
1042 svm->vmcb->control.tsc_offset);
1043
Joerg Roedel116a0a22010-12-03 11:45:49 +01001044 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001045}
1046
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001047static void avic_init_vmcb(struct vcpu_svm *svm)
1048{
1049 struct vmcb *vmcb = svm->vmcb;
1050 struct kvm_arch *vm_data = &svm->vcpu.kvm->arch;
1051 phys_addr_t bpa = page_to_phys(svm->avic_backing_page);
1052 phys_addr_t lpa = page_to_phys(vm_data->avic_logical_id_table_page);
1053 phys_addr_t ppa = page_to_phys(vm_data->avic_physical_id_table_page);
1054
1055 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1056 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1057 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1058 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1059 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
1060 svm->vcpu.arch.apicv_active = true;
1061}
1062
Paolo Bonzini56908912015-10-19 11:30:19 +02001063static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064{
Joerg Roedele6101a92008-02-13 18:58:45 +01001065 struct vmcb_control_area *control = &svm->vmcb->control;
1066 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067
Avi Kivitybff78272010-01-07 13:16:08 +02001068 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001069 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001070
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001071 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1072 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1073 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1074 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1075 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1076 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
1077 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078
Paolo Bonzini5315c712014-03-03 13:08:29 +01001079 set_dr_intercepts(svm);
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);
Eric Northup54a20552015-11-03 18:03:53 +01001084 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001085 set_exception_intercept(svm, DB_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001087 set_intercept(svm, INTERCEPT_INTR);
1088 set_intercept(svm, INTERCEPT_NMI);
1089 set_intercept(svm, INTERCEPT_SMI);
1090 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001091 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001092 set_intercept(svm, INTERCEPT_CPUID);
1093 set_intercept(svm, INTERCEPT_INVD);
1094 set_intercept(svm, INTERCEPT_HLT);
1095 set_intercept(svm, INTERCEPT_INVLPG);
1096 set_intercept(svm, INTERCEPT_INVLPGA);
1097 set_intercept(svm, INTERCEPT_IOIO_PROT);
1098 set_intercept(svm, INTERCEPT_MSR_PROT);
1099 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1100 set_intercept(svm, INTERCEPT_SHUTDOWN);
1101 set_intercept(svm, INTERCEPT_VMRUN);
1102 set_intercept(svm, INTERCEPT_VMMCALL);
1103 set_intercept(svm, INTERCEPT_VMLOAD);
1104 set_intercept(svm, INTERCEPT_VMSAVE);
1105 set_intercept(svm, INTERCEPT_STGI);
1106 set_intercept(svm, INTERCEPT_CLGI);
1107 set_intercept(svm, INTERCEPT_SKINIT);
1108 set_intercept(svm, INTERCEPT_WBINVD);
1109 set_intercept(svm, INTERCEPT_MONITOR);
1110 set_intercept(svm, INTERCEPT_MWAIT);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001111 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112
1113 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001114 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115 control->int_ctl = V_INTR_MASKING_MASK;
1116
1117 init_seg(&save->es);
1118 init_seg(&save->ss);
1119 init_seg(&save->ds);
1120 init_seg(&save->fs);
1121 init_seg(&save->gs);
1122
1123 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001124 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125 /* Executable/Readable Code Segment */
1126 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1127 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1128 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129
1130 save->gdtr.limit = 0xffff;
1131 save->idtr.limit = 0xffff;
1132
1133 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1134 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1135
Paolo Bonzini56908912015-10-19 11:30:19 +02001136 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001137 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001138 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001140 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141
Joerg Roedele0231712010-02-24 18:59:10 +01001142 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001143 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001144 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001146 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001147 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001148
Rusty Russell66aee912007-07-17 23:34:16 +10001149 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001151
1152 if (npt_enabled) {
1153 /* Setup VMCB for Nested Paging */
1154 control->nested_ctl = 1;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001155 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001156 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001157 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1158 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001159 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001160 save->cr3 = 0;
1161 save->cr4 = 0;
1162 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001163 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001164
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001165 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001166 svm->vcpu.arch.hflags = 0;
1167
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001168 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001169 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001170 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001171 }
1172
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001173 if (avic)
1174 avic_init_vmcb(svm);
1175
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001176 mark_all_dirty(svm->vmcb);
1177
Joerg Roedel2af91942009-08-07 11:49:28 +02001178 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001179
1180}
1181
1182static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu, int index)
1183{
1184 u64 *avic_physical_id_table;
1185 struct kvm_arch *vm_data = &vcpu->kvm->arch;
1186
1187 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1188 return NULL;
1189
1190 avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page);
1191
1192 return &avic_physical_id_table[index];
1193}
1194
1195/**
1196 * Note:
1197 * AVIC hardware walks the nested page table to check permissions,
1198 * but does not use the SPA address specified in the leaf page
1199 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1200 * field of the VMCB. Therefore, we set up the
1201 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1202 */
1203static int avic_init_access_page(struct kvm_vcpu *vcpu)
1204{
1205 struct kvm *kvm = vcpu->kvm;
1206 int ret;
1207
1208 if (kvm->arch.apic_access_page_done)
1209 return 0;
1210
1211 ret = x86_set_memory_region(kvm,
1212 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1213 APIC_DEFAULT_PHYS_BASE,
1214 PAGE_SIZE);
1215 if (ret)
1216 return ret;
1217
1218 kvm->arch.apic_access_page_done = true;
1219 return 0;
1220}
1221
1222static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1223{
1224 int ret;
1225 u64 *entry, new_entry;
1226 int id = vcpu->vcpu_id;
1227 struct vcpu_svm *svm = to_svm(vcpu);
1228
1229 ret = avic_init_access_page(vcpu);
1230 if (ret)
1231 return ret;
1232
1233 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1234 return -EINVAL;
1235
1236 if (!svm->vcpu.arch.apic->regs)
1237 return -EINVAL;
1238
1239 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1240
1241 /* Setting AVIC backing page address in the phy APIC ID table */
1242 entry = avic_get_physical_id_entry(vcpu, id);
1243 if (!entry)
1244 return -EINVAL;
1245
1246 new_entry = READ_ONCE(*entry);
1247 new_entry = (page_to_phys(svm->avic_backing_page) &
1248 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1249 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK;
1250 WRITE_ONCE(*entry, new_entry);
1251
1252 svm->avic_physical_id_cache = entry;
1253
1254 return 0;
1255}
1256
1257static void avic_vm_destroy(struct kvm *kvm)
1258{
1259 struct kvm_arch *vm_data = &kvm->arch;
1260
1261 if (vm_data->avic_logical_id_table_page)
1262 __free_page(vm_data->avic_logical_id_table_page);
1263 if (vm_data->avic_physical_id_table_page)
1264 __free_page(vm_data->avic_physical_id_table_page);
1265}
1266
1267static int avic_vm_init(struct kvm *kvm)
1268{
1269 int err = -ENOMEM;
1270 struct kvm_arch *vm_data = &kvm->arch;
1271 struct page *p_page;
1272 struct page *l_page;
1273
1274 if (!avic)
1275 return 0;
1276
1277 /* Allocating physical APIC ID table (4KB) */
1278 p_page = alloc_page(GFP_KERNEL);
1279 if (!p_page)
1280 goto free_avic;
1281
1282 vm_data->avic_physical_id_table_page = p_page;
1283 clear_page(page_address(p_page));
1284
1285 /* Allocating logical APIC ID table (4KB) */
1286 l_page = alloc_page(GFP_KERNEL);
1287 if (!l_page)
1288 goto free_avic;
1289
1290 vm_data->avic_logical_id_table_page = l_page;
1291 clear_page(page_address(l_page));
1292
1293 return 0;
1294
1295free_avic:
1296 avic_vm_destroy(kvm);
1297 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298}
1299
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001300static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001301{
1302 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001303 u32 dummy;
1304 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001305
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001306 if (!init_event) {
1307 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1308 MSR_IA32_APICBASE_ENABLE;
1309 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1310 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1311 }
Paolo Bonzini56908912015-10-19 11:30:19 +02001312 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001313
Julian Stecklina66f7b722012-12-05 15:26:19 +01001314 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy);
1315 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001316
1317 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1318 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001319}
1320
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001321static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001323 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001325 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001326 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001327 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001328 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329
Rusty Russellc16f8622007-07-30 21:12:19 +10001330 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001331 if (!svm) {
1332 err = -ENOMEM;
1333 goto out;
1334 }
1335
1336 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1337 if (err)
1338 goto free_svm;
1339
Joerg Roedelf65c2292008-02-13 18:58:46 +01001340 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001341 page = alloc_page(GFP_KERNEL);
1342 if (!page)
1343 goto uninit;
1344
Joerg Roedelf65c2292008-02-13 18:58:46 +01001345 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1346 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001347 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001348
1349 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1350 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001351 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001352
Alexander Grafb286d5d2008-11-25 20:17:05 +01001353 hsave_page = alloc_page(GFP_KERNEL);
1354 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001355 goto free_page3;
1356
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001357 if (avic) {
1358 err = avic_init_backing_page(&svm->vcpu);
1359 if (err)
1360 goto free_page4;
1361 }
1362
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001363 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001364
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001365 svm->msrpm = page_address(msrpm_pages);
1366 svm_vcpu_init_msrpm(svm->msrpm);
1367
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001368 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001369 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001370
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001371 svm->vmcb = page_address(page);
1372 clear_page(svm->vmcb);
1373 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1374 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02001375 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001377 svm_init_osvw(&svm->vcpu);
1378
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001379 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001380
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001381free_page4:
1382 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001383free_page3:
1384 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1385free_page2:
1386 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1387free_page1:
1388 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001389uninit:
1390 kvm_vcpu_uninit(&svm->vcpu);
1391free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001392 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001393out:
1394 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395}
1396
1397static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1398{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001399 struct vcpu_svm *svm = to_svm(vcpu);
1400
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001401 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001402 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001403 __free_page(virt_to_page(svm->nested.hsave));
1404 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001405 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001406 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407}
1408
Avi Kivity15ad7142007-07-11 18:17:21 +03001409static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001411 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001412 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001413
Avi Kivity0cc50642007-03-25 12:07:27 +02001414 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001415 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001416 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001417 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001418
Avi Kivity82ca2d12010-10-21 12:20:34 +02001419#ifdef CONFIG_X86_64
1420 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1421#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001422 savesegment(fs, svm->host.fs);
1423 savesegment(gs, svm->host.gs);
1424 svm->host.ldt = kvm_read_ldt();
1425
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001426 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001427 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001428
Haozhong Zhangad721882015-10-20 15:39:02 +08001429 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1430 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1431 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1432 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1433 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1434 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001435 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01001436 /* This assumes that the kernel never uses MSR_TSC_AUX */
1437 if (static_cpu_has(X86_FEATURE_RDTSCP))
1438 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439}
1440
1441static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1442{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001443 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001444 int i;
1445
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001446 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001447 kvm_load_ldt(svm->host.ldt);
1448#ifdef CONFIG_X86_64
1449 loadsegment(fs, svm->host.fs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001450 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001451 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001452#else
Avi Kivity831ca602011-03-08 16:09:51 +02001453#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001454 loadsegment(gs, svm->host.gs);
1455#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001456#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001457 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001458 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459}
1460
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1462{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001463 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464}
1465
1466static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1467{
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001468 /*
1469 * Any change of EFLAGS.VM is accompained by a reload of SS
1470 * (caused by either a task switch or an inter-privilege IRET),
1471 * so we do not need to update the CPL here.
1472 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001473 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001474}
1475
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08001476static u32 svm_get_pkru(struct kvm_vcpu *vcpu)
1477{
1478 return 0;
1479}
1480
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001481static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1482{
1483 switch (reg) {
1484 case VCPU_EXREG_PDPTR:
1485 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001486 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001487 break;
1488 default:
1489 BUG();
1490 }
1491}
1492
Alexander Graff0b85052008-11-25 20:17:01 +01001493static void svm_set_vintr(struct vcpu_svm *svm)
1494{
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001495 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001496}
1497
1498static void svm_clear_vintr(struct vcpu_svm *svm)
1499{
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001500 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001501}
1502
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1504{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001505 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506
1507 switch (seg) {
1508 case VCPU_SREG_CS: return &save->cs;
1509 case VCPU_SREG_DS: return &save->ds;
1510 case VCPU_SREG_ES: return &save->es;
1511 case VCPU_SREG_FS: return &save->fs;
1512 case VCPU_SREG_GS: return &save->gs;
1513 case VCPU_SREG_SS: return &save->ss;
1514 case VCPU_SREG_TR: return &save->tr;
1515 case VCPU_SREG_LDTR: return &save->ldtr;
1516 }
1517 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001518 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519}
1520
1521static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1522{
1523 struct vmcb_seg *s = svm_seg(vcpu, seg);
1524
1525 return s->base;
1526}
1527
1528static void svm_get_segment(struct kvm_vcpu *vcpu,
1529 struct kvm_segment *var, int seg)
1530{
1531 struct vmcb_seg *s = svm_seg(vcpu, seg);
1532
1533 var->base = s->base;
1534 var->limit = s->limit;
1535 var->selector = s->selector;
1536 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1537 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1538 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1539 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1540 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1541 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1542 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301543
1544 /*
1545 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1546 * However, the SVM spec states that the G bit is not observed by the
1547 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1548 * So let's synthesize a legal G bit for all segments, this helps
1549 * running KVM nested. It also helps cross-vendor migration, because
1550 * Intel's vmentry has a check on the 'G' bit.
1551 */
1552 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001553
Joerg Roedele0231712010-02-24 18:59:10 +01001554 /*
1555 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001556 * for cross vendor migration purposes by "not present"
1557 */
1558 var->unusable = !var->present || (var->type == 0);
1559
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001560 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001561 case VCPU_SREG_TR:
1562 /*
1563 * Work around a bug where the busy flag in the tr selector
1564 * isn't exposed
1565 */
Amit Shahc0d09822008-10-27 09:04:18 +00001566 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001567 break;
1568 case VCPU_SREG_DS:
1569 case VCPU_SREG_ES:
1570 case VCPU_SREG_FS:
1571 case VCPU_SREG_GS:
1572 /*
1573 * The accessed bit must always be set in the segment
1574 * descriptor cache, although it can be cleared in the
1575 * descriptor, the cached bit always remains at 1. Since
1576 * Intel has a check on this, set it here to support
1577 * cross-vendor migration.
1578 */
1579 if (!var->unusable)
1580 var->type |= 0x1;
1581 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001582 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001583 /*
1584 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001585 * descriptor is left as 1, although the whole segment has
1586 * been made unusable. Clear it here to pass an Intel VMX
1587 * entry check when cross vendor migrating.
1588 */
1589 if (var->unusable)
1590 var->db = 0;
Jan Kiszka33b458d2014-06-29 17:12:43 +02001591 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02001592 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001593 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594}
1595
Izik Eidus2e4d2652008-03-24 19:38:34 +02001596static int svm_get_cpl(struct kvm_vcpu *vcpu)
1597{
1598 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1599
1600 return save->cpl;
1601}
1602
Gleb Natapov89a27f42010-02-16 10:51:48 +02001603static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001605 struct vcpu_svm *svm = to_svm(vcpu);
1606
Gleb Natapov89a27f42010-02-16 10:51:48 +02001607 dt->size = svm->vmcb->save.idtr.limit;
1608 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609}
1610
Gleb Natapov89a27f42010-02-16 10:51:48 +02001611static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001613 struct vcpu_svm *svm = to_svm(vcpu);
1614
Gleb Natapov89a27f42010-02-16 10:51:48 +02001615 svm->vmcb->save.idtr.limit = dt->size;
1616 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001617 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618}
1619
Gleb Natapov89a27f42010-02-16 10:51:48 +02001620static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001622 struct vcpu_svm *svm = to_svm(vcpu);
1623
Gleb Natapov89a27f42010-02-16 10:51:48 +02001624 dt->size = svm->vmcb->save.gdtr.limit;
1625 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626}
1627
Gleb Natapov89a27f42010-02-16 10:51:48 +02001628static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001630 struct vcpu_svm *svm = to_svm(vcpu);
1631
Gleb Natapov89a27f42010-02-16 10:51:48 +02001632 svm->vmcb->save.gdtr.limit = dt->size;
1633 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001634 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635}
1636
Avi Kivitye8467fd2009-12-29 18:43:06 +02001637static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1638{
1639}
1640
Avi Kivityaff48ba2010-12-05 18:56:11 +02001641static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1642{
1643}
1644
Anthony Liguori25c4c272007-04-27 09:29:21 +03001645static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001646{
1647}
1648
Avi Kivityd2251572010-01-06 10:55:27 +02001649static void update_cr0_intercept(struct vcpu_svm *svm)
1650{
1651 ulong gcr0 = svm->vcpu.arch.cr0;
1652 u64 *hcr0 = &svm->vmcb->save.cr0;
1653
1654 if (!svm->vcpu.fpu_active)
1655 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1656 else
1657 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1658 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1659
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001660 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001661
1662 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001663 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1664 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001665 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001666 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1667 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001668 }
1669}
1670
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1672{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001673 struct vcpu_svm *svm = to_svm(vcpu);
1674
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001675#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001676 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001677 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001678 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001679 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680 }
1681
Mike Dayd77c26f2007-10-08 09:02:08 -04001682 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001683 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001684 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685 }
1686 }
1687#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001688 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001689
1690 if (!npt_enabled)
1691 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001692
1693 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001694 cr0 |= X86_CR0_TS;
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02001695 /*
1696 * re-enable caching here because the QEMU bios
1697 * does not do it - this results in some delay at
1698 * reboot
1699 */
1700 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
1701 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001702 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001703 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001704 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705}
1706
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001707static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001709 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001710 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1711
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001712 if (cr4 & X86_CR4_VMXE)
1713 return 1;
1714
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001715 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001716 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001717
Joerg Roedelec077262008-04-09 14:15:28 +02001718 vcpu->arch.cr4 = cr4;
1719 if (!npt_enabled)
1720 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001721 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001722 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001723 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001724 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725}
1726
1727static void svm_set_segment(struct kvm_vcpu *vcpu,
1728 struct kvm_segment *var, int seg)
1729{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001730 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001731 struct vmcb_seg *s = svm_seg(vcpu, seg);
1732
1733 s->base = var->base;
1734 s->limit = var->limit;
1735 s->selector = var->selector;
1736 if (var->unusable)
1737 s->attrib = 0;
1738 else {
1739 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1740 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1741 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1742 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1743 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1744 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1745 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1746 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1747 }
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001748
1749 /*
1750 * This is always accurate, except if SYSRET returned to a segment
1751 * with SS.DPL != 3. Intel does not have this quirk, and always
1752 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1753 * would entail passing the CPL to userspace and back.
1754 */
1755 if (seg == VCPU_SREG_SS)
1756 svm->vmcb->save.cpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757
Joerg Roedel060d0c92010-12-03 11:45:57 +01001758 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759}
1760
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001761static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001763 struct vcpu_svm *svm = to_svm(vcpu);
1764
Joerg Roedel18c918c2010-11-30 18:03:59 +01001765 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001766
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001767 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001768 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001769 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001770 } else
1771 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001772}
1773
Tejun Heo0fe1e002009-10-29 22:34:14 +09001774static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001775{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001776 if (sd->next_asid > sd->max_asid) {
1777 ++sd->asid_generation;
1778 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001779 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780 }
1781
Tejun Heo0fe1e002009-10-29 22:34:14 +09001782 svm->asid_generation = sd->asid_generation;
1783 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001784
1785 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786}
1787
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001788static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
1789{
1790 return to_svm(vcpu)->vmcb->save.dr6;
1791}
1792
1793static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
1794{
1795 struct vcpu_svm *svm = to_svm(vcpu);
1796
1797 svm->vmcb->save.dr6 = value;
1798 mark_dirty(svm->vmcb, VMCB_DR);
1799}
1800
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001801static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1802{
1803 struct vcpu_svm *svm = to_svm(vcpu);
1804
1805 get_debugreg(vcpu->arch.db[0], 0);
1806 get_debugreg(vcpu->arch.db[1], 1);
1807 get_debugreg(vcpu->arch.db[2], 2);
1808 get_debugreg(vcpu->arch.db[3], 3);
1809 vcpu->arch.dr6 = svm_get_dr6(vcpu);
1810 vcpu->arch.dr7 = svm->vmcb->save.dr7;
1811
1812 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1813 set_dr_intercepts(svm);
1814}
1815
Gleb Natapov020df072010-04-13 10:05:23 +03001816static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001818 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001819
Gleb Natapov020df072010-04-13 10:05:23 +03001820 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01001821 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822}
1823
Avi Kivity851ba692009-08-24 11:10:17 +03001824static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001825{
Gleb Natapov631bc482010-10-14 11:22:52 +02001826 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001827 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001828 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829
Gleb Natapov631bc482010-10-14 11:22:52 +02001830 switch (svm->apf_reason) {
1831 default:
1832 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001833
Gleb Natapov631bc482010-10-14 11:22:52 +02001834 trace_kvm_page_fault(fault_address, error_code);
1835 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1836 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01001837 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1838 svm->vmcb->control.insn_bytes,
1839 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02001840 break;
1841 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1842 svm->apf_reason = 0;
1843 local_irq_disable();
1844 kvm_async_pf_task_wait(fault_address);
1845 local_irq_enable();
1846 break;
1847 case KVM_PV_REASON_PAGE_READY:
1848 svm->apf_reason = 0;
1849 local_irq_disable();
1850 kvm_async_pf_task_wake(fault_address);
1851 local_irq_enable();
1852 break;
1853 }
1854 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855}
1856
Avi Kivity851ba692009-08-24 11:10:17 +03001857static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001858{
Avi Kivity851ba692009-08-24 11:10:17 +03001859 struct kvm_run *kvm_run = svm->vcpu.run;
1860
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001861 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001862 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001863 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001864 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1865 return 1;
1866 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001867
Jan Kiszka6be7d302009-10-18 13:24:54 +02001868 if (svm->nmi_singlestep) {
1869 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001870 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1871 svm->vmcb->save.rflags &=
1872 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Gleb Natapov44c11432009-05-11 13:35:52 +03001873 }
1874
1875 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001876 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001877 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1878 kvm_run->debug.arch.pc =
1879 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1880 kvm_run->debug.arch.exception = DB_VECTOR;
1881 return 0;
1882 }
1883
1884 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001885}
1886
Avi Kivity851ba692009-08-24 11:10:17 +03001887static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001888{
Avi Kivity851ba692009-08-24 11:10:17 +03001889 struct kvm_run *kvm_run = svm->vcpu.run;
1890
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001891 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1892 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1893 kvm_run->debug.arch.exception = BP_VECTOR;
1894 return 0;
1895}
1896
Avi Kivity851ba692009-08-24 11:10:17 +03001897static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001898{
1899 int er;
1900
Andre Przywara51d8b662010-12-21 11:12:02 +01001901 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001902 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001903 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001904 return 1;
1905}
1906
Eric Northup54a20552015-11-03 18:03:53 +01001907static int ac_interception(struct vcpu_svm *svm)
1908{
1909 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
1910 return 1;
1911}
1912
Avi Kivity6b52d182010-01-21 15:31:47 +02001913static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001914{
Avi Kivity6b52d182010-01-21 15:31:47 +02001915 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001916
Joerg Roedel18c918c2010-11-30 18:03:59 +01001917 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001918
Rusty Russelle756fc62007-07-30 20:07:08 +10001919 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001920 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001921}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001922
Avi Kivity6b52d182010-01-21 15:31:47 +02001923static int nm_interception(struct vcpu_svm *svm)
1924{
1925 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001926 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001927}
1928
Joerg Roedel67ec6602010-05-17 14:43:35 +02001929static bool is_erratum_383(void)
1930{
1931 int err, i;
1932 u64 value;
1933
1934 if (!erratum_383_found)
1935 return false;
1936
1937 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1938 if (err)
1939 return false;
1940
1941 /* Bit 62 may or may not be set for this mce */
1942 value &= ~(1ULL << 62);
1943
1944 if (value != 0xb600000000010015ULL)
1945 return false;
1946
1947 /* Clear MCi_STATUS registers */
1948 for (i = 0; i < 6; ++i)
1949 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1950
1951 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1952 if (!err) {
1953 u32 low, high;
1954
1955 value &= ~(1ULL << 2);
1956 low = lower_32_bits(value);
1957 high = upper_32_bits(value);
1958
1959 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1960 }
1961
1962 /* Flush tlb to evict multi-match entries */
1963 __flush_tlb_all();
1964
1965 return true;
1966}
1967
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001968static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001969{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001970 if (is_erratum_383()) {
1971 /*
1972 * Erratum 383 triggered. Guest state is corrupt so kill the
1973 * guest.
1974 */
1975 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1976
Avi Kivitya8eeb042010-05-10 12:34:53 +03001977 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001978
1979 return;
1980 }
1981
Joerg Roedel53371b52008-04-09 14:15:30 +02001982 /*
1983 * On an #MC intercept the MCE handler is not called automatically in
1984 * the host. So do it by hand here.
1985 */
1986 asm volatile (
1987 "int $0x12\n");
1988 /* not sure if we ever come back to this point */
1989
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001990 return;
1991}
1992
1993static int mc_interception(struct vcpu_svm *svm)
1994{
Joerg Roedel53371b52008-04-09 14:15:30 +02001995 return 1;
1996}
1997
Avi Kivity851ba692009-08-24 11:10:17 +03001998static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001999{
Avi Kivity851ba692009-08-24 11:10:17 +03002000 struct kvm_run *kvm_run = svm->vcpu.run;
2001
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002002 /*
2003 * VMCB is undefined after a SHUTDOWN intercept
2004 * so reinitialize it.
2005 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002006 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002007 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002008
2009 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2010 return 0;
2011}
2012
Avi Kivity851ba692009-08-24 11:10:17 +03002013static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002014{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002015 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002016 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01002017 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002018 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002019
Rusty Russelle756fc62007-07-30 20:07:08 +10002020 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002021 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002022 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002023 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01002024 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002025
Avi Kivity039576c2007-03-20 12:46:50 +02002026 port = io_info >> 16;
2027 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002028 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002029 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002030
2031 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032}
2033
Avi Kivity851ba692009-08-24 11:10:17 +03002034static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002035{
2036 return 1;
2037}
2038
Avi Kivity851ba692009-08-24 11:10:17 +03002039static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002040{
2041 ++svm->vcpu.stat.irq_exits;
2042 return 1;
2043}
2044
Avi Kivity851ba692009-08-24 11:10:17 +03002045static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002046{
2047 return 1;
2048}
2049
Avi Kivity851ba692009-08-24 11:10:17 +03002050static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002052 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002053 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054}
2055
Avi Kivity851ba692009-08-24 11:10:17 +03002056static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002057{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002058 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002059 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002060}
2061
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002062static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2063{
2064 struct vcpu_svm *svm = to_svm(vcpu);
2065
2066 return svm->nested.nested_cr3;
2067}
2068
Avi Kivitye4e517b2011-07-28 11:36:17 +03002069static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2070{
2071 struct vcpu_svm *svm = to_svm(vcpu);
2072 u64 cr3 = svm->nested.nested_cr3;
2073 u64 pdpte;
2074 int ret;
2075
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002076 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
2077 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002078 if (ret)
2079 return 0;
2080 return pdpte;
2081}
2082
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002083static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2084 unsigned long root)
2085{
2086 struct vcpu_svm *svm = to_svm(vcpu);
2087
2088 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01002089 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002090 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002091}
2092
Avi Kivity6389ee92010-11-29 16:12:30 +02002093static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2094 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002095{
2096 struct vcpu_svm *svm = to_svm(vcpu);
2097
Paolo Bonzini5e352512014-09-02 13:18:37 +02002098 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2099 /*
2100 * TODO: track the cause of the nested page fault, and
2101 * correctly fill in the high bits of exit_info_1.
2102 */
2103 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2104 svm->vmcb->control.exit_code_hi = 0;
2105 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2106 svm->vmcb->control.exit_info_2 = fault->address;
2107 }
2108
2109 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2110 svm->vmcb->control.exit_info_1 |= fault->error_code;
2111
2112 /*
2113 * The present bit is always zero for page structure faults on real
2114 * hardware.
2115 */
2116 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2117 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002118
2119 nested_svm_vmexit(svm);
2120}
2121
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002122static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002123{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002124 WARN_ON(mmu_is_nested(vcpu));
2125 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002126 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2127 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002128 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002129 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
2130 vcpu->arch.mmu.shadow_root_level = get_npt_level();
Xiao Guangrongc258b622015-08-05 12:04:24 +08002131 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002132 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002133}
2134
2135static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2136{
2137 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2138}
2139
Alexander Grafc0725422008-11-25 20:17:03 +01002140static int nested_svm_check_permissions(struct vcpu_svm *svm)
2141{
Avi Kivityf6801df2010-01-21 15:31:50 +02002142 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01002143 || !is_paging(&svm->vcpu)) {
2144 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2145 return 1;
2146 }
2147
2148 if (svm->vmcb->save.cpl) {
2149 kvm_inject_gp(&svm->vcpu, 0);
2150 return 1;
2151 }
2152
2153 return 0;
2154}
2155
Alexander Grafcf74a782008-11-25 20:17:08 +01002156static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2157 bool has_error_code, u32 error_code)
2158{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002159 int vmexit;
2160
Joerg Roedel20307532010-11-29 17:51:48 +01002161 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002162 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002163
Joerg Roedel0295ad72009-08-07 11:49:37 +02002164 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2165 svm->vmcb->control.exit_code_hi = 0;
2166 svm->vmcb->control.exit_info_1 = error_code;
2167 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
2168
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002169 vmexit = nested_svm_intercept(svm);
2170 if (vmexit == NESTED_EXIT_DONE)
2171 svm->nested.exit_required = true;
2172
2173 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002174}
2175
Joerg Roedel8fe54652010-02-19 16:23:01 +01002176/* This function returns true if it is save to enable the irq window */
2177static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002178{
Joerg Roedel20307532010-11-29 17:51:48 +01002179 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002180 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002181
Joerg Roedel26666952009-08-07 11:49:46 +02002182 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002183 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002184
Joerg Roedel26666952009-08-07 11:49:46 +02002185 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002186 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002187
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002188 /*
2189 * if vmexit was already requested (by intercepted exception
2190 * for instance) do not overwrite it with "external interrupt"
2191 * vmexit.
2192 */
2193 if (svm->nested.exit_required)
2194 return false;
2195
Joerg Roedel197717d2010-02-24 18:59:19 +01002196 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2197 svm->vmcb->control.exit_info_1 = 0;
2198 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002199
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002200 if (svm->nested.intercept & 1ULL) {
2201 /*
2202 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002203 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002204 * #vmexit emulation might sleep. Only signal request for
2205 * the #vmexit here.
2206 */
2207 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002208 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002209 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002210 }
2211
Joerg Roedel8fe54652010-02-19 16:23:01 +01002212 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002213}
2214
Joerg Roedel887f5002010-02-24 18:59:12 +01002215/* This function returns true if it is save to enable the nmi window */
2216static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2217{
Joerg Roedel20307532010-11-29 17:51:48 +01002218 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002219 return true;
2220
2221 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2222 return true;
2223
2224 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2225 svm->nested.exit_required = true;
2226
2227 return false;
2228}
2229
Joerg Roedel7597f122010-02-19 16:23:00 +01002230static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002231{
2232 struct page *page;
2233
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002234 might_sleep();
2235
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002236 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002237 if (is_error_page(page))
2238 goto error;
2239
Joerg Roedel7597f122010-02-19 16:23:00 +01002240 *_page = page;
2241
2242 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002243
2244error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002245 kvm_inject_gp(&svm->vcpu, 0);
2246
2247 return NULL;
2248}
2249
Joerg Roedel7597f122010-02-19 16:23:00 +01002250static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002251{
Joerg Roedel7597f122010-02-19 16:23:00 +01002252 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002253 kvm_release_page_dirty(page);
2254}
2255
Joerg Roedelce2ac082010-03-01 15:34:39 +01002256static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002257{
Jan Kiszka9bf41832014-06-30 10:54:17 +02002258 unsigned port, size, iopm_len;
2259 u16 val, mask;
2260 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002261 u64 gpa;
2262
2263 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2264 return NESTED_EXIT_HOST;
2265
2266 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02002267 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2268 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002269 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02002270 start_bit = port % 8;
2271 iopm_len = (start_bit + size > 8) ? 2 : 1;
2272 mask = (0xf >> (4 - size)) << start_bit;
2273 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002274
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002275 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02002276 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002277
Jan Kiszka9bf41832014-06-30 10:54:17 +02002278 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002279}
2280
Joerg Roedeld2477822010-03-01 15:34:34 +01002281static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002282{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002283 u32 offset, msr, value;
2284 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002285
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002286 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002287 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002288
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002289 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2290 offset = svm_msrpm_offset(msr);
2291 write = svm->vmcb->control.exit_info_1 & 1;
2292 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002293
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002294 if (offset == MSR_INVALID)
2295 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002296
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002297 /* Offset is in 32 bit units but need in 8 bit units */
2298 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002299
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002300 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002301 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002302
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002303 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002304}
2305
Joerg Roedel410e4d52009-08-07 11:49:44 +02002306static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002307{
Alexander Grafcf74a782008-11-25 20:17:08 +01002308 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002309
Joerg Roedel410e4d52009-08-07 11:49:44 +02002310 switch (exit_code) {
2311 case SVM_EXIT_INTR:
2312 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002313 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002314 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002315 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002316 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002317 if (npt_enabled)
2318 return NESTED_EXIT_HOST;
2319 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002320 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002321 /* When we're shadowing, trap PFs, but not async PF */
2322 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002323 return NESTED_EXIT_HOST;
2324 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01002325 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
2326 nm_interception(svm);
2327 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002328 default:
2329 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002330 }
2331
Joerg Roedel410e4d52009-08-07 11:49:44 +02002332 return NESTED_EXIT_CONTINUE;
2333}
2334
2335/*
2336 * If this function returns true, this #vmexit was already handled
2337 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002338static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002339{
2340 u32 exit_code = svm->vmcb->control.exit_code;
2341 int vmexit = NESTED_EXIT_HOST;
2342
Alexander Grafcf74a782008-11-25 20:17:08 +01002343 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002344 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002345 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002346 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002347 case SVM_EXIT_IOIO:
2348 vmexit = nested_svm_intercept_ioio(svm);
2349 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002350 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2351 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2352 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002353 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002354 break;
2355 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002356 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2357 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2358 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002359 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002360 break;
2361 }
2362 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2363 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002364 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002365 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002366 /* async page fault always cause vmexit */
2367 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2368 svm->apf_reason != 0)
2369 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002370 break;
2371 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002372 case SVM_EXIT_ERR: {
2373 vmexit = NESTED_EXIT_DONE;
2374 break;
2375 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002376 default: {
2377 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002378 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002379 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002380 }
2381 }
2382
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002383 return vmexit;
2384}
2385
2386static int nested_svm_exit_handled(struct vcpu_svm *svm)
2387{
2388 int vmexit;
2389
2390 vmexit = nested_svm_intercept(svm);
2391
2392 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002393 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002394
2395 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002396}
2397
Joerg Roedel0460a972009-08-07 11:49:31 +02002398static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2399{
2400 struct vmcb_control_area *dst = &dst_vmcb->control;
2401 struct vmcb_control_area *from = &from_vmcb->control;
2402
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002403 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002404 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002405 dst->intercept_exceptions = from->intercept_exceptions;
2406 dst->intercept = from->intercept;
2407 dst->iopm_base_pa = from->iopm_base_pa;
2408 dst->msrpm_base_pa = from->msrpm_base_pa;
2409 dst->tsc_offset = from->tsc_offset;
2410 dst->asid = from->asid;
2411 dst->tlb_ctl = from->tlb_ctl;
2412 dst->int_ctl = from->int_ctl;
2413 dst->int_vector = from->int_vector;
2414 dst->int_state = from->int_state;
2415 dst->exit_code = from->exit_code;
2416 dst->exit_code_hi = from->exit_code_hi;
2417 dst->exit_info_1 = from->exit_info_1;
2418 dst->exit_info_2 = from->exit_info_2;
2419 dst->exit_int_info = from->exit_int_info;
2420 dst->exit_int_info_err = from->exit_int_info_err;
2421 dst->nested_ctl = from->nested_ctl;
2422 dst->event_inj = from->event_inj;
2423 dst->event_inj_err = from->event_inj_err;
2424 dst->nested_cr3 = from->nested_cr3;
2425 dst->lbr_ctl = from->lbr_ctl;
2426}
2427
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002428static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002429{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002430 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002431 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002432 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002433 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002434
Joerg Roedel17897f32009-10-09 16:08:29 +02002435 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2436 vmcb->control.exit_info_1,
2437 vmcb->control.exit_info_2,
2438 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002439 vmcb->control.exit_int_info_err,
2440 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002441
Joerg Roedel7597f122010-02-19 16:23:00 +01002442 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002443 if (!nested_vmcb)
2444 return 1;
2445
Joerg Roedel20307532010-11-29 17:51:48 +01002446 /* Exit Guest-Mode */
2447 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002448 svm->nested.vmcb = 0;
2449
Alexander Grafcf74a782008-11-25 20:17:08 +01002450 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002451 disable_gif(svm);
2452
2453 nested_vmcb->save.es = vmcb->save.es;
2454 nested_vmcb->save.cs = vmcb->save.cs;
2455 nested_vmcb->save.ss = vmcb->save.ss;
2456 nested_vmcb->save.ds = vmcb->save.ds;
2457 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2458 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002459 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002460 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002461 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002462 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002463 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002464 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002465 nested_vmcb->save.rip = vmcb->save.rip;
2466 nested_vmcb->save.rsp = vmcb->save.rsp;
2467 nested_vmcb->save.rax = vmcb->save.rax;
2468 nested_vmcb->save.dr7 = vmcb->save.dr7;
2469 nested_vmcb->save.dr6 = vmcb->save.dr6;
2470 nested_vmcb->save.cpl = vmcb->save.cpl;
2471
2472 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2473 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2474 nested_vmcb->control.int_state = vmcb->control.int_state;
2475 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2476 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2477 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2478 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2479 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2480 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02002481
2482 if (svm->nrips_enabled)
2483 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002484
2485 /*
2486 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2487 * to make sure that we do not lose injected events. So check event_inj
2488 * here and copy it to exit_int_info if it is valid.
2489 * Exit_int_info and event_inj can't be both valid because the case
2490 * below only happens on a VMRUN instruction intercept which has
2491 * no valid exit_int_info set.
2492 */
2493 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2494 struct vmcb_control_area *nc = &nested_vmcb->control;
2495
2496 nc->exit_int_info = vmcb->control.event_inj;
2497 nc->exit_int_info_err = vmcb->control.event_inj_err;
2498 }
2499
Joerg Roedel33740e42009-08-07 11:49:29 +02002500 nested_vmcb->control.tlb_ctl = 0;
2501 nested_vmcb->control.event_inj = 0;
2502 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002503
2504 /* We always set V_INTR_MASKING and remember the old value in hflags */
2505 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2506 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2507
Alexander Grafcf74a782008-11-25 20:17:08 +01002508 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002509 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002510
Alexander Graf219b65d2009-06-15 15:21:25 +02002511 kvm_clear_exception_queue(&svm->vcpu);
2512 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002513
Joerg Roedel4b161842010-09-10 17:31:03 +02002514 svm->nested.nested_cr3 = 0;
2515
Alexander Grafcf74a782008-11-25 20:17:08 +01002516 /* Restore selected save entries */
2517 svm->vmcb->save.es = hsave->save.es;
2518 svm->vmcb->save.cs = hsave->save.cs;
2519 svm->vmcb->save.ss = hsave->save.ss;
2520 svm->vmcb->save.ds = hsave->save.ds;
2521 svm->vmcb->save.gdtr = hsave->save.gdtr;
2522 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002523 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002524 svm_set_efer(&svm->vcpu, hsave->save.efer);
2525 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2526 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2527 if (npt_enabled) {
2528 svm->vmcb->save.cr3 = hsave->save.cr3;
2529 svm->vcpu.arch.cr3 = hsave->save.cr3;
2530 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002531 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002532 }
2533 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2534 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2535 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2536 svm->vmcb->save.dr7 = 0;
2537 svm->vmcb->save.cpl = 0;
2538 svm->vmcb->control.exit_int_info = 0;
2539
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002540 mark_all_dirty(svm->vmcb);
2541
Joerg Roedel7597f122010-02-19 16:23:00 +01002542 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002543
Joerg Roedel4b161842010-09-10 17:31:03 +02002544 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002545 kvm_mmu_reset_context(&svm->vcpu);
2546 kvm_mmu_load(&svm->vcpu);
2547
2548 return 0;
2549}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002550
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002551static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002552{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002553 /*
2554 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002555 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01002556 * the kvm msr permission bitmap may contain zero bits
2557 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002558 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002559
Joerg Roedel323c3d82010-03-01 15:34:37 +01002560 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2561 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002562
Joerg Roedel323c3d82010-03-01 15:34:37 +01002563 for (i = 0; i < MSRPM_OFFSETS; i++) {
2564 u32 value, p;
2565 u64 offset;
2566
2567 if (msrpm_offsets[i] == 0xffffffff)
2568 break;
2569
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002570 p = msrpm_offsets[i];
2571 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002572
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002573 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01002574 return false;
2575
2576 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2577 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002578
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002579 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002580
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002581 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002582}
2583
Joerg Roedel52c65a302010-08-02 16:46:44 +02002584static bool nested_vmcb_checks(struct vmcb *vmcb)
2585{
2586 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2587 return false;
2588
Joerg Roedeldbe77582010-08-02 16:46:45 +02002589 if (vmcb->control.asid == 0)
2590 return false;
2591
Joerg Roedel4b161842010-09-10 17:31:03 +02002592 if (vmcb->control.nested_ctl && !npt_enabled)
2593 return false;
2594
Joerg Roedel52c65a302010-08-02 16:46:44 +02002595 return true;
2596}
2597
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002598static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002599{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002600 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002601 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002602 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002603 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002604 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002605
Joerg Roedel06fc77722010-02-19 16:23:07 +01002606 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002607
Joerg Roedel7597f122010-02-19 16:23:00 +01002608 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002609 if (!nested_vmcb)
2610 return false;
2611
Joerg Roedel52c65a302010-08-02 16:46:44 +02002612 if (!nested_vmcb_checks(nested_vmcb)) {
2613 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2614 nested_vmcb->control.exit_code_hi = 0;
2615 nested_vmcb->control.exit_info_1 = 0;
2616 nested_vmcb->control.exit_info_2 = 0;
2617
2618 nested_svm_unmap(page);
2619
2620 return false;
2621 }
2622
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002623 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002624 nested_vmcb->save.rip,
2625 nested_vmcb->control.int_ctl,
2626 nested_vmcb->control.event_inj,
2627 nested_vmcb->control.nested_ctl);
2628
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002629 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2630 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002631 nested_vmcb->control.intercept_exceptions,
2632 nested_vmcb->control.intercept);
2633
Alexander Graf3d6368e2008-11-25 20:17:07 +01002634 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002635 kvm_clear_exception_queue(&svm->vcpu);
2636 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002637
Joerg Roedele0231712010-02-24 18:59:10 +01002638 /*
2639 * Save the old vmcb, so we don't need to pick what we save, but can
2640 * restore everything when a VMEXIT occurs
2641 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002642 hsave->save.es = vmcb->save.es;
2643 hsave->save.cs = vmcb->save.cs;
2644 hsave->save.ss = vmcb->save.ss;
2645 hsave->save.ds = vmcb->save.ds;
2646 hsave->save.gdtr = vmcb->save.gdtr;
2647 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002648 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002649 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002650 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002651 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002652 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002653 hsave->save.rsp = vmcb->save.rsp;
2654 hsave->save.rax = vmcb->save.rax;
2655 if (npt_enabled)
2656 hsave->save.cr3 = vmcb->save.cr3;
2657 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002658 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002659
Joerg Roedel0460a972009-08-07 11:49:31 +02002660 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002661
Avi Kivityf6e78472010-08-02 15:30:20 +03002662 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002663 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2664 else
2665 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2666
Joerg Roedel4b161842010-09-10 17:31:03 +02002667 if (nested_vmcb->control.nested_ctl) {
2668 kvm_mmu_unload(&svm->vcpu);
2669 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2670 nested_svm_init_mmu_context(&svm->vcpu);
2671 }
2672
Alexander Graf3d6368e2008-11-25 20:17:07 +01002673 /* Load the nested guest state */
2674 svm->vmcb->save.es = nested_vmcb->save.es;
2675 svm->vmcb->save.cs = nested_vmcb->save.cs;
2676 svm->vmcb->save.ss = nested_vmcb->save.ss;
2677 svm->vmcb->save.ds = nested_vmcb->save.ds;
2678 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2679 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002680 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002681 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2682 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2683 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2684 if (npt_enabled) {
2685 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2686 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002687 } else
Avi Kivity23902182010-06-10 17:02:16 +03002688 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002689
2690 /* Guest paging mode is active - reset mmu */
2691 kvm_mmu_reset_context(&svm->vcpu);
2692
Joerg Roedeldefbba52009-08-07 11:49:30 +02002693 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002694 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2695 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2696 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002697
Alexander Graf3d6368e2008-11-25 20:17:07 +01002698 /* In case we don't even reach vcpu_run, the fields are not updated */
2699 svm->vmcb->save.rax = nested_vmcb->save.rax;
2700 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2701 svm->vmcb->save.rip = nested_vmcb->save.rip;
2702 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2703 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2704 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2705
Joerg Roedelf7138532010-03-01 15:34:40 +01002706 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002707 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002708
Joerg Roedelaad42c62009-08-07 11:49:34 +02002709 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002710 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002711 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002712 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2713 svm->nested.intercept = nested_vmcb->control.intercept;
2714
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002715 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002716 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002717 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2718 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2719 else
2720 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2721
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002722 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2723 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002724 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2725 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002726 }
2727
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002728 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01002729 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002730
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002731 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002732 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2733 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2734 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002735 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2736 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2737
Joerg Roedel7597f122010-02-19 16:23:00 +01002738 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002739
Joerg Roedel20307532010-11-29 17:51:48 +01002740 /* Enter Guest-Mode */
2741 enter_guest_mode(&svm->vcpu);
2742
Joerg Roedel384c6362010-11-30 18:03:56 +01002743 /*
2744 * Merge guest and host intercepts - must be called with vcpu in
2745 * guest-mode to take affect here
2746 */
2747 recalc_intercepts(svm);
2748
Joerg Roedel06fc77722010-02-19 16:23:07 +01002749 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002750
Joerg Roedel2af91942009-08-07 11:49:28 +02002751 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002752
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002753 mark_all_dirty(svm->vmcb);
2754
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002755 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002756}
2757
Joerg Roedel9966bf62009-08-07 11:49:40 +02002758static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002759{
2760 to_vmcb->save.fs = from_vmcb->save.fs;
2761 to_vmcb->save.gs = from_vmcb->save.gs;
2762 to_vmcb->save.tr = from_vmcb->save.tr;
2763 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2764 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2765 to_vmcb->save.star = from_vmcb->save.star;
2766 to_vmcb->save.lstar = from_vmcb->save.lstar;
2767 to_vmcb->save.cstar = from_vmcb->save.cstar;
2768 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2769 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2770 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2771 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002772}
2773
Avi Kivity851ba692009-08-24 11:10:17 +03002774static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002775{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002776 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002777 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002778
Alexander Graf55426752008-11-25 20:17:06 +01002779 if (nested_svm_check_permissions(svm))
2780 return 1;
2781
Joerg Roedel7597f122010-02-19 16:23:00 +01002782 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002783 if (!nested_vmcb)
2784 return 1;
2785
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002786 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2787 skip_emulated_instruction(&svm->vcpu);
2788
Joerg Roedel9966bf62009-08-07 11:49:40 +02002789 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002790 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002791
2792 return 1;
2793}
2794
Avi Kivity851ba692009-08-24 11:10:17 +03002795static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002796{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002797 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002798 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002799
Alexander Graf55426752008-11-25 20:17:06 +01002800 if (nested_svm_check_permissions(svm))
2801 return 1;
2802
Joerg Roedel7597f122010-02-19 16:23:00 +01002803 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002804 if (!nested_vmcb)
2805 return 1;
2806
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002807 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2808 skip_emulated_instruction(&svm->vcpu);
2809
Joerg Roedel9966bf62009-08-07 11:49:40 +02002810 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002811 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002812
2813 return 1;
2814}
2815
Avi Kivity851ba692009-08-24 11:10:17 +03002816static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002817{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002818 if (nested_svm_check_permissions(svm))
2819 return 1;
2820
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002821 /* Save rip after vmrun instruction */
2822 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002823
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002824 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002825 return 1;
2826
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002827 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002828 goto failed;
2829
2830 return 1;
2831
2832failed:
2833
2834 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2835 svm->vmcb->control.exit_code_hi = 0;
2836 svm->vmcb->control.exit_info_1 = 0;
2837 svm->vmcb->control.exit_info_2 = 0;
2838
2839 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002840
2841 return 1;
2842}
2843
Avi Kivity851ba692009-08-24 11:10:17 +03002844static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002845{
2846 if (nested_svm_check_permissions(svm))
2847 return 1;
2848
2849 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2850 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002851 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002852
Joerg Roedel2af91942009-08-07 11:49:28 +02002853 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002854
2855 return 1;
2856}
2857
Avi Kivity851ba692009-08-24 11:10:17 +03002858static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002859{
2860 if (nested_svm_check_permissions(svm))
2861 return 1;
2862
2863 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2864 skip_emulated_instruction(&svm->vcpu);
2865
Joerg Roedel2af91942009-08-07 11:49:28 +02002866 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002867
2868 /* After a CLGI no interrupts should come */
2869 svm_clear_vintr(svm);
2870 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2871
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002872 mark_dirty(svm->vmcb, VMCB_INTR);
2873
Alexander Graf1371d902008-11-25 20:17:04 +01002874 return 1;
2875}
2876
Avi Kivity851ba692009-08-24 11:10:17 +03002877static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002878{
2879 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002880
David Kaplan668f1982015-02-20 16:02:10 -06002881 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
2882 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02002883
Alexander Grafff092382009-06-15 15:21:24 +02002884 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06002885 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02002886
2887 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2888 skip_emulated_instruction(&svm->vcpu);
2889 return 1;
2890}
2891
Joerg Roedel532a46b2009-10-09 16:08:32 +02002892static int skinit_interception(struct vcpu_svm *svm)
2893{
David Kaplan668f1982015-02-20 16:02:10 -06002894 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02002895
2896 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2897 return 1;
2898}
2899
David Kaplandab429a2015-03-02 13:43:37 -06002900static int wbinvd_interception(struct vcpu_svm *svm)
2901{
2902 kvm_emulate_wbinvd(&svm->vcpu);
2903 return 1;
2904}
2905
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002906static int xsetbv_interception(struct vcpu_svm *svm)
2907{
2908 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
2909 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
2910
2911 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
2912 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2913 skip_emulated_instruction(&svm->vcpu);
2914 }
2915
2916 return 1;
2917}
2918
Avi Kivity851ba692009-08-24 11:10:17 +03002919static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920{
Izik Eidus37817f22008-03-24 23:14:53 +02002921 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002922 int reason;
2923 int int_type = svm->vmcb->control.exit_int_info &
2924 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002925 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002926 uint32_t type =
2927 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2928 uint32_t idt_v =
2929 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002930 bool has_error_code = false;
2931 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002932
2933 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002934
Izik Eidus37817f22008-03-24 23:14:53 +02002935 if (svm->vmcb->control.exit_info_2 &
2936 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002937 reason = TASK_SWITCH_IRET;
2938 else if (svm->vmcb->control.exit_info_2 &
2939 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2940 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002941 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002942 reason = TASK_SWITCH_GATE;
2943 else
2944 reason = TASK_SWITCH_CALL;
2945
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002946 if (reason == TASK_SWITCH_GATE) {
2947 switch (type) {
2948 case SVM_EXITINTINFO_TYPE_NMI:
2949 svm->vcpu.arch.nmi_injected = false;
2950 break;
2951 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002952 if (svm->vmcb->control.exit_info_2 &
2953 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2954 has_error_code = true;
2955 error_code =
2956 (u32)svm->vmcb->control.exit_info_2;
2957 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002958 kvm_clear_exception_queue(&svm->vcpu);
2959 break;
2960 case SVM_EXITINTINFO_TYPE_INTR:
2961 kvm_clear_interrupt_queue(&svm->vcpu);
2962 break;
2963 default:
2964 break;
2965 }
2966 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002967
Gleb Natapov8317c292009-04-12 13:37:02 +03002968 if (reason != TASK_SWITCH_GATE ||
2969 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2970 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002971 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2972 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002973
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002974 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2975 int_vec = -1;
2976
2977 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03002978 has_error_code, error_code) == EMULATE_FAIL) {
2979 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2980 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2981 svm->vcpu.run->internal.ndata = 0;
2982 return 0;
2983 }
2984 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002985}
2986
Avi Kivity851ba692009-08-24 11:10:17 +03002987static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002988{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002989 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002990 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002991 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002992}
2993
Avi Kivity851ba692009-08-24 11:10:17 +03002994static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002995{
2996 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01002997 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002998 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02002999 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003000 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003001 return 1;
3002}
3003
Avi Kivity851ba692009-08-24 11:10:17 +03003004static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003005{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003006 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3007 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3008
3009 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
3010 skip_emulated_instruction(&svm->vcpu);
3011 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003012}
3013
Avi Kivity851ba692009-08-24 11:10:17 +03003014static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003015{
Andre Przywara51d8b662010-12-21 11:12:02 +01003016 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017}
3018
Avi Kivity332b56e2011-11-10 14:57:24 +02003019static int rdpmc_interception(struct vcpu_svm *svm)
3020{
3021 int err;
3022
3023 if (!static_cpu_has(X86_FEATURE_NRIPS))
3024 return emulate_on_interception(svm);
3025
3026 err = kvm_rdpmc(&svm->vcpu);
3027 kvm_complete_insn_gp(&svm->vcpu, err);
3028
3029 return 1;
3030}
3031
Xiubo Li52eb5a62015-03-13 17:39:45 +08003032static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3033 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003034{
3035 unsigned long cr0 = svm->vcpu.arch.cr0;
3036 bool ret = false;
3037 u64 intercept;
3038
3039 intercept = svm->nested.intercept;
3040
3041 if (!is_guest_mode(&svm->vcpu) ||
3042 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3043 return false;
3044
3045 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3046 val &= ~SVM_CR0_SELECTIVE_MASK;
3047
3048 if (cr0 ^ val) {
3049 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3050 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3051 }
3052
3053 return ret;
3054}
3055
Andre Przywara7ff76d52010-12-21 11:12:04 +01003056#define CR_VALID (1ULL << 63)
3057
3058static int cr_interception(struct vcpu_svm *svm)
3059{
3060 int reg, cr;
3061 unsigned long val;
3062 int err;
3063
3064 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3065 return emulate_on_interception(svm);
3066
3067 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3068 return emulate_on_interception(svm);
3069
3070 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003071 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3072 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3073 else
3074 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003075
3076 err = 0;
3077 if (cr >= 16) { /* mov to cr */
3078 cr -= 16;
3079 val = kvm_register_read(&svm->vcpu, reg);
3080 switch (cr) {
3081 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003082 if (!check_selective_cr0_intercepted(svm, val))
3083 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003084 else
3085 return 1;
3086
Andre Przywara7ff76d52010-12-21 11:12:04 +01003087 break;
3088 case 3:
3089 err = kvm_set_cr3(&svm->vcpu, val);
3090 break;
3091 case 4:
3092 err = kvm_set_cr4(&svm->vcpu, val);
3093 break;
3094 case 8:
3095 err = kvm_set_cr8(&svm->vcpu, val);
3096 break;
3097 default:
3098 WARN(1, "unhandled write to CR%d", cr);
3099 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3100 return 1;
3101 }
3102 } else { /* mov from cr */
3103 switch (cr) {
3104 case 0:
3105 val = kvm_read_cr0(&svm->vcpu);
3106 break;
3107 case 2:
3108 val = svm->vcpu.arch.cr2;
3109 break;
3110 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003111 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003112 break;
3113 case 4:
3114 val = kvm_read_cr4(&svm->vcpu);
3115 break;
3116 case 8:
3117 val = kvm_get_cr8(&svm->vcpu);
3118 break;
3119 default:
3120 WARN(1, "unhandled read from CR%d", cr);
3121 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3122 return 1;
3123 }
3124 kvm_register_write(&svm->vcpu, reg, val);
3125 }
3126 kvm_complete_insn_gp(&svm->vcpu, err);
3127
3128 return 1;
3129}
3130
Andre Przywaracae37972010-12-21 11:12:05 +01003131static int dr_interception(struct vcpu_svm *svm)
3132{
3133 int reg, dr;
3134 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003135
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003136 if (svm->vcpu.guest_debug == 0) {
3137 /*
3138 * No more DR vmexits; force a reload of the debug registers
3139 * and reenter on this instruction. The next vmexit will
3140 * retrieve the full state of the debug registers.
3141 */
3142 clr_dr_intercepts(svm);
3143 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3144 return 1;
3145 }
3146
Andre Przywaracae37972010-12-21 11:12:05 +01003147 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3148 return emulate_on_interception(svm);
3149
3150 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3151 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3152
3153 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003154 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3155 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003156 val = kvm_register_read(&svm->vcpu, reg);
3157 kvm_set_dr(&svm->vcpu, dr - 16, val);
3158 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003159 if (!kvm_require_dr(&svm->vcpu, dr))
3160 return 1;
3161 kvm_get_dr(&svm->vcpu, dr, &val);
3162 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003163 }
3164
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01003165 skip_emulated_instruction(&svm->vcpu);
3166
Andre Przywaracae37972010-12-21 11:12:05 +01003167 return 1;
3168}
3169
Avi Kivity851ba692009-08-24 11:10:17 +03003170static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003171{
Avi Kivity851ba692009-08-24 11:10:17 +03003172 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003173 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003174
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003175 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3176 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003177 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003178 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003179 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003180 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003181 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003182 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3183 return 0;
3184}
3185
Paolo Bonzini48d89b92014-08-26 13:27:46 +02003186static u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003187{
3188 struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
Haozhong Zhang4ba76532015-10-20 15:39:07 +08003189 return vmcb->control.tsc_offset + host_tsc;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003190}
3191
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003192static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003193{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003194 struct vcpu_svm *svm = to_svm(vcpu);
3195
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003196 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303197 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003198 msr_info->data = svm->vmcb->control.tsc_offset +
Haozhong Zhang35181e82015-10-20 15:39:03 +08003199 kvm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003200
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201 break;
3202 }
Brian Gerst8c065852010-07-17 09:03:26 -04003203 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003204 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003205 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003206#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003207 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003208 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209 break;
3210 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003211 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003212 break;
3213 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003214 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215 break;
3216 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003217 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218 break;
3219#endif
3220 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003221 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222 break;
3223 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003224 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225 break;
3226 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003227 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003229 case MSR_TSC_AUX:
3230 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3231 return 1;
3232 msr_info->data = svm->tsc_aux;
3233 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003234 /*
3235 * Nobody will change the following 5 values in the VMCB so we can
3236 * safely return them on rdmsr. They will always be 0 until LBRV is
3237 * implemented.
3238 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003239 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003240 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01003241 break;
3242 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003243 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003244 break;
3245 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003246 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003247 break;
3248 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003249 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003250 break;
3251 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003252 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003253 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003254 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003255 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003256 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003257 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003258 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003259 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003260 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003261 msr_info->data = 0x01000065;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003262 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01003263 case MSR_F15H_IC_CFG: {
3264
3265 int family, model;
3266
3267 family = guest_cpuid_family(vcpu);
3268 model = guest_cpuid_model(vcpu);
3269
3270 if (family < 0 || model < 0)
3271 return kvm_get_msr_common(vcpu, msr_info);
3272
3273 msr_info->data = 0;
3274
3275 if (family == 0x15 &&
3276 (model >= 0x2 && model < 0x20))
3277 msr_info->data = 0x1E;
3278 }
3279 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003280 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003281 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282 }
3283 return 0;
3284}
3285
Avi Kivity851ba692009-08-24 11:10:17 +03003286static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003287{
David Kaplan668f1982015-02-20 16:02:10 -06003288 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003289 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003290
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003291 msr_info.index = ecx;
3292 msr_info.host_initiated = false;
3293 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02003294 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003295 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003296 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003297 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003298
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003299 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
3300 msr_info.data & 0xffffffff);
3301 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
3302 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003303 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003304 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305 }
3306 return 1;
3307}
3308
Joerg Roedel4a810182010-02-24 18:59:15 +01003309static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3310{
3311 struct vcpu_svm *svm = to_svm(vcpu);
3312 int svm_dis, chg_mask;
3313
3314 if (data & ~SVM_VM_CR_VALID_MASK)
3315 return 1;
3316
3317 chg_mask = SVM_VM_CR_VALID_MASK;
3318
3319 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3320 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3321
3322 svm->nested.vm_cr_msr &= ~chg_mask;
3323 svm->nested.vm_cr_msr |= (data & chg_mask);
3324
3325 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3326
3327 /* check for svm_disable while efer.svme is set */
3328 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3329 return 1;
3330
3331 return 0;
3332}
3333
Will Auld8fe8ab42012-11-29 12:42:12 -08003334static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003335{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003336 struct vcpu_svm *svm = to_svm(vcpu);
3337
Will Auld8fe8ab42012-11-29 12:42:12 -08003338 u32 ecx = msr->index;
3339 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003341 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003342 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003343 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003344 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003345 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003346 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003347#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003349 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350 break;
3351 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003352 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003353 break;
3354 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003355 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003356 break;
3357 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003358 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003359 break;
3360#endif
3361 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003362 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363 break;
3364 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003365 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003366 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003367 break;
3368 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003369 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003370 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003371 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003372 case MSR_TSC_AUX:
3373 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3374 return 1;
3375
3376 /*
3377 * This is rare, so we update the MSR here instead of using
3378 * direct_access_msrs. Doing that would require a rdmsr in
3379 * svm_vcpu_put.
3380 */
3381 svm->tsc_aux = data;
3382 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
3383 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003384 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003385 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003386 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
3387 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003388 break;
3389 }
3390 if (data & DEBUGCTL_RESERVED_BITS)
3391 return 1;
3392
3393 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003394 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003395 if (data & (1ULL<<0))
3396 svm_enable_lbrv(svm);
3397 else
3398 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003399 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003400 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003401 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003402 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003403 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003404 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003405 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03003406 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003407 break;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003408 case MSR_IA32_APICBASE:
3409 if (kvm_vcpu_apicv_active(vcpu))
3410 avic_update_vapic_bar(to_svm(vcpu), data);
3411 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003412 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08003413 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003414 }
3415 return 0;
3416}
3417
Avi Kivity851ba692009-08-24 11:10:17 +03003418static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419{
Will Auld8fe8ab42012-11-29 12:42:12 -08003420 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06003421 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3422 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003423
Will Auld8fe8ab42012-11-29 12:42:12 -08003424 msr.data = data;
3425 msr.index = ecx;
3426 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003427
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003428 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03003429 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02003430 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003431 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003432 } else {
3433 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10003434 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003435 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436 return 1;
3437}
3438
Avi Kivity851ba692009-08-24 11:10:17 +03003439static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003440{
Rusty Russelle756fc62007-07-30 20:07:08 +10003441 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003442 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443 else
Avi Kivity851ba692009-08-24 11:10:17 +03003444 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445}
3446
Avi Kivity851ba692009-08-24 11:10:17 +03003447static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003448{
Avi Kivity3842d132010-07-27 12:30:24 +03003449 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003450 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003451 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003452 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08003453 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003454 return 1;
3455}
3456
Mark Langsdorf565d0992009-10-06 14:25:02 -05003457static int pause_interception(struct vcpu_svm *svm)
3458{
3459 kvm_vcpu_on_spin(&(svm->vcpu));
3460 return 1;
3461}
3462
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003463static int nop_interception(struct vcpu_svm *svm)
3464{
3465 skip_emulated_instruction(&(svm->vcpu));
3466 return 1;
3467}
3468
3469static int monitor_interception(struct vcpu_svm *svm)
3470{
3471 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
3472 return nop_interception(svm);
3473}
3474
3475static int mwait_interception(struct vcpu_svm *svm)
3476{
3477 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
3478 return nop_interception(svm);
3479}
3480
Mathias Krause09941fb2012-08-30 01:30:20 +02003481static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003482 [SVM_EXIT_READ_CR0] = cr_interception,
3483 [SVM_EXIT_READ_CR3] = cr_interception,
3484 [SVM_EXIT_READ_CR4] = cr_interception,
3485 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06003486 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003487 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003488 [SVM_EXIT_WRITE_CR3] = cr_interception,
3489 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003490 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003491 [SVM_EXIT_READ_DR0] = dr_interception,
3492 [SVM_EXIT_READ_DR1] = dr_interception,
3493 [SVM_EXIT_READ_DR2] = dr_interception,
3494 [SVM_EXIT_READ_DR3] = dr_interception,
3495 [SVM_EXIT_READ_DR4] = dr_interception,
3496 [SVM_EXIT_READ_DR5] = dr_interception,
3497 [SVM_EXIT_READ_DR6] = dr_interception,
3498 [SVM_EXIT_READ_DR7] = dr_interception,
3499 [SVM_EXIT_WRITE_DR0] = dr_interception,
3500 [SVM_EXIT_WRITE_DR1] = dr_interception,
3501 [SVM_EXIT_WRITE_DR2] = dr_interception,
3502 [SVM_EXIT_WRITE_DR3] = dr_interception,
3503 [SVM_EXIT_WRITE_DR4] = dr_interception,
3504 [SVM_EXIT_WRITE_DR5] = dr_interception,
3505 [SVM_EXIT_WRITE_DR6] = dr_interception,
3506 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003507 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3508 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003509 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003510 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3511 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
3512 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01003513 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003514 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003515 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516 [SVM_EXIT_SMI] = nop_on_interception,
3517 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003518 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02003519 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003521 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003522 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003523 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003525 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003526 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003527 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003528 [SVM_EXIT_MSR] = msr_interception,
3529 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003530 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003531 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003532 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003533 [SVM_EXIT_VMLOAD] = vmload_interception,
3534 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003535 [SVM_EXIT_STGI] = stgi_interception,
3536 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003537 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06003538 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003539 [SVM_EXIT_MONITOR] = monitor_interception,
3540 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003541 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003542 [SVM_EXIT_NPF] = pf_interception,
Paolo Bonzini64d60672015-05-07 11:36:11 +02003543 [SVM_EXIT_RSM] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003544};
3545
Joe Perchesae8cc052011-04-24 22:00:50 -07003546static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003547{
3548 struct vcpu_svm *svm = to_svm(vcpu);
3549 struct vmcb_control_area *control = &svm->vmcb->control;
3550 struct vmcb_save_area *save = &svm->vmcb->save;
3551
3552 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003553 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
3554 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
3555 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
3556 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
3557 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
3558 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
3559 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3560 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3561 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3562 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3563 pr_err("%-20s%d\n", "asid:", control->asid);
3564 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3565 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3566 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3567 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3568 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3569 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3570 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3571 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3572 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3573 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3574 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003575 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07003576 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3577 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3578 pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
3579 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003580 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3581 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3582 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003583 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003584 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3585 "es:",
3586 save->es.selector, save->es.attrib,
3587 save->es.limit, save->es.base);
3588 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3589 "cs:",
3590 save->cs.selector, save->cs.attrib,
3591 save->cs.limit, save->cs.base);
3592 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3593 "ss:",
3594 save->ss.selector, save->ss.attrib,
3595 save->ss.limit, save->ss.base);
3596 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3597 "ds:",
3598 save->ds.selector, save->ds.attrib,
3599 save->ds.limit, save->ds.base);
3600 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3601 "fs:",
3602 save->fs.selector, save->fs.attrib,
3603 save->fs.limit, save->fs.base);
3604 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3605 "gs:",
3606 save->gs.selector, save->gs.attrib,
3607 save->gs.limit, save->gs.base);
3608 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3609 "gdtr:",
3610 save->gdtr.selector, save->gdtr.attrib,
3611 save->gdtr.limit, save->gdtr.base);
3612 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3613 "ldtr:",
3614 save->ldtr.selector, save->ldtr.attrib,
3615 save->ldtr.limit, save->ldtr.base);
3616 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3617 "idtr:",
3618 save->idtr.selector, save->idtr.attrib,
3619 save->idtr.limit, save->idtr.base);
3620 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3621 "tr:",
3622 save->tr.selector, save->tr.attrib,
3623 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003624 pr_err("cpl: %d efer: %016llx\n",
3625 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003626 pr_err("%-15s %016llx %-13s %016llx\n",
3627 "cr0:", save->cr0, "cr2:", save->cr2);
3628 pr_err("%-15s %016llx %-13s %016llx\n",
3629 "cr3:", save->cr3, "cr4:", save->cr4);
3630 pr_err("%-15s %016llx %-13s %016llx\n",
3631 "dr6:", save->dr6, "dr7:", save->dr7);
3632 pr_err("%-15s %016llx %-13s %016llx\n",
3633 "rip:", save->rip, "rflags:", save->rflags);
3634 pr_err("%-15s %016llx %-13s %016llx\n",
3635 "rsp:", save->rsp, "rax:", save->rax);
3636 pr_err("%-15s %016llx %-13s %016llx\n",
3637 "star:", save->star, "lstar:", save->lstar);
3638 pr_err("%-15s %016llx %-13s %016llx\n",
3639 "cstar:", save->cstar, "sfmask:", save->sfmask);
3640 pr_err("%-15s %016llx %-13s %016llx\n",
3641 "kernel_gs_base:", save->kernel_gs_base,
3642 "sysenter_cs:", save->sysenter_cs);
3643 pr_err("%-15s %016llx %-13s %016llx\n",
3644 "sysenter_esp:", save->sysenter_esp,
3645 "sysenter_eip:", save->sysenter_eip);
3646 pr_err("%-15s %016llx %-13s %016llx\n",
3647 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3648 pr_err("%-15s %016llx %-13s %016llx\n",
3649 "br_from:", save->br_from, "br_to:", save->br_to);
3650 pr_err("%-15s %016llx %-13s %016llx\n",
3651 "excp_from:", save->last_excp_from,
3652 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003653}
3654
Avi Kivity586f9602010-11-18 13:09:54 +02003655static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3656{
3657 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3658
3659 *info1 = control->exit_info_1;
3660 *info2 = control->exit_info_2;
3661}
3662
Avi Kivity851ba692009-08-24 11:10:17 +03003663static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003664{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003665 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003666 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003667 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003668
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01003669 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
3670
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003671 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003672 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3673 if (npt_enabled)
3674 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003675
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003676 if (unlikely(svm->nested.exit_required)) {
3677 nested_svm_vmexit(svm);
3678 svm->nested.exit_required = false;
3679
3680 return 1;
3681 }
3682
Joerg Roedel20307532010-11-29 17:51:48 +01003683 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003684 int vmexit;
3685
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003686 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3687 svm->vmcb->control.exit_info_1,
3688 svm->vmcb->control.exit_info_2,
3689 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003690 svm->vmcb->control.exit_int_info_err,
3691 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003692
Joerg Roedel410e4d52009-08-07 11:49:44 +02003693 vmexit = nested_svm_exit_special(svm);
3694
3695 if (vmexit == NESTED_EXIT_CONTINUE)
3696 vmexit = nested_svm_exit_handled(svm);
3697
3698 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003699 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003700 }
3701
Joerg Roedela5c38322009-08-07 11:49:32 +02003702 svm_complete_interrupts(svm);
3703
Avi Kivity04d2cc72007-09-10 18:10:54 +03003704 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3705 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3706 kvm_run->fail_entry.hardware_entry_failure_reason
3707 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003708 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3709 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003710 return 0;
3711 }
3712
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003713 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003714 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003715 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3716 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02003717 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003719 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003720 exit_code);
3721
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003722 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003723 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04003724 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03003725 kvm_queue_exception(vcpu, UD_VECTOR);
3726 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003727 }
3728
Avi Kivity851ba692009-08-24 11:10:17 +03003729 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003730}
3731
3732static void reload_tss(struct kvm_vcpu *vcpu)
3733{
3734 int cpu = raw_smp_processor_id();
3735
Tejun Heo0fe1e002009-10-29 22:34:14 +09003736 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3737 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003738 load_TR_desc();
3739}
3740
Rusty Russelle756fc62007-07-30 20:07:08 +10003741static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003742{
3743 int cpu = raw_smp_processor_id();
3744
Tejun Heo0fe1e002009-10-29 22:34:14 +09003745 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003746
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003747 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003748 if (svm->asid_generation != sd->asid_generation)
3749 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003750}
3751
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003752static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3753{
3754 struct vcpu_svm *svm = to_svm(vcpu);
3755
3756 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3757 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01003758 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003759 ++vcpu->stat.nmi_injections;
3760}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003761
Eddie Dong85f455f2007-07-06 12:20:49 +03003762static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003763{
3764 struct vmcb_control_area *control;
3765
Rusty Russelle756fc62007-07-30 20:07:08 +10003766 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003767 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003768 control->int_ctl &= ~V_INTR_PRIO_MASK;
3769 control->int_ctl |= V_IRQ_MASK |
3770 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003771 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003772}
3773
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003774static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003775{
3776 struct vcpu_svm *svm = to_svm(vcpu);
3777
Joerg Roedel2af91942009-08-07 11:49:28 +02003778 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003779
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003780 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3781 ++vcpu->stat.irq_injections;
3782
Alexander Graf219b65d2009-06-15 15:21:25 +02003783 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3784 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003785}
3786
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003787static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3788{
3789 struct vcpu_svm *svm = to_svm(vcpu);
3790
Joerg Roedel20307532010-11-29 17:51:48 +01003791 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003792 return;
3793
Radim Krčmář596f3142014-03-11 19:11:18 +01003794 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
3795
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003796 if (irr == -1)
3797 return;
3798
3799 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003800 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003801}
3802
Yang Zhang8d146952013-01-25 10:18:50 +08003803static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
3804{
3805 return;
3806}
3807
Andrey Smetanind62caab2015-11-10 15:36:33 +03003808static bool svm_get_enable_apicv(void)
Yang Zhangc7c9c562013-01-25 10:18:51 +08003809{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003810 return avic;
Yang Zhangc7c9c562013-01-25 10:18:51 +08003811}
3812
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003813static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
3814{
3815}
3816
3817static void svm_hwapic_isr_update(struct kvm *kvm, int isr)
3818{
3819}
3820
3821/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03003822static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
3823{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003824 struct vcpu_svm *svm = to_svm(vcpu);
3825 struct vmcb *vmcb = svm->vmcb;
3826
3827 if (!avic)
3828 return;
3829
3830 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
3831 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08003832}
3833
Andrey Smetanin63086302015-11-10 15:36:32 +03003834static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08003835{
3836 return;
3837}
3838
Yang Zhanga20ed542013-04-11 19:25:15 +08003839static void svm_sync_pir_to_irr(struct kvm_vcpu *vcpu)
3840{
3841 return;
3842}
3843
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003844static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003845{
3846 struct vcpu_svm *svm = to_svm(vcpu);
3847 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003848 int ret;
3849 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3850 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3851 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3852
3853 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003854}
3855
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003856static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3857{
3858 struct vcpu_svm *svm = to_svm(vcpu);
3859
3860 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3861}
3862
3863static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3864{
3865 struct vcpu_svm *svm = to_svm(vcpu);
3866
3867 if (masked) {
3868 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01003869 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003870 } else {
3871 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01003872 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003873 }
3874}
3875
Gleb Natapov78646122009-03-23 12:12:11 +02003876static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3877{
3878 struct vcpu_svm *svm = to_svm(vcpu);
3879 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003880 int ret;
3881
3882 if (!gif_set(svm) ||
3883 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3884 return 0;
3885
Avi Kivityf6e78472010-08-02 15:30:20 +03003886 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003887
Joerg Roedel20307532010-11-29 17:51:48 +01003888 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003889 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3890
3891 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003892}
3893
Jan Kiszkac9a79532014-03-07 20:03:15 +01003894static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003895{
Alexander Graf219b65d2009-06-15 15:21:25 +02003896 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003897
Joerg Roedele0231712010-02-24 18:59:10 +01003898 /*
3899 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3900 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3901 * get that intercept, this function will be called again though and
3902 * we'll get the vintr intercept.
3903 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003904 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003905 svm_set_vintr(svm);
3906 svm_inject_irq(svm, 0x0);
3907 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003908}
3909
Jan Kiszkac9a79532014-03-07 20:03:15 +01003910static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003911{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003912 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003913
Gleb Natapov44c11432009-05-11 13:35:52 +03003914 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3915 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01003916 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03003917
Joerg Roedele0231712010-02-24 18:59:10 +01003918 /*
3919 * Something prevents NMI from been injected. Single step over possible
3920 * problem (IRET or exception injection or interrupt shadow)
3921 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003922 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003923 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03003924}
3925
Izik Eiduscbc94022007-10-25 00:29:55 +02003926static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3927{
3928 return 0;
3929}
3930
Avi Kivityd9e368d2007-06-07 19:18:30 +03003931static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3932{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003933 struct vcpu_svm *svm = to_svm(vcpu);
3934
3935 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3936 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3937 else
3938 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003939}
3940
Avi Kivity04d2cc72007-09-10 18:10:54 +03003941static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3942{
3943}
3944
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003945static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3946{
3947 struct vcpu_svm *svm = to_svm(vcpu);
3948
Joerg Roedel20307532010-11-29 17:51:48 +01003949 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003950 return;
3951
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003952 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003953 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003954 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003955 }
3956}
3957
Joerg Roedel649d6862008-04-16 16:51:15 +02003958static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3959{
3960 struct vcpu_svm *svm = to_svm(vcpu);
3961 u64 cr8;
3962
Joerg Roedel20307532010-11-29 17:51:48 +01003963 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003964 return;
3965
Joerg Roedel649d6862008-04-16 16:51:15 +02003966 cr8 = kvm_get_cr8(vcpu);
3967 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3968 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3969}
3970
Gleb Natapov9222be12009-04-23 17:14:37 +03003971static void svm_complete_interrupts(struct vcpu_svm *svm)
3972{
3973 u8 vector;
3974 int type;
3975 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003976 unsigned int3_injected = svm->int3_injected;
3977
3978 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003979
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003980 /*
3981 * If we've made progress since setting HF_IRET_MASK, we've
3982 * executed an IRET and can allow NMI injection.
3983 */
3984 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
3985 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003986 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003987 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3988 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003989
Gleb Natapov9222be12009-04-23 17:14:37 +03003990 svm->vcpu.arch.nmi_injected = false;
3991 kvm_clear_exception_queue(&svm->vcpu);
3992 kvm_clear_interrupt_queue(&svm->vcpu);
3993
3994 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3995 return;
3996
Avi Kivity3842d132010-07-27 12:30:24 +03003997 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3998
Gleb Natapov9222be12009-04-23 17:14:37 +03003999 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
4000 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
4001
4002 switch (type) {
4003 case SVM_EXITINTINFO_TYPE_NMI:
4004 svm->vcpu.arch.nmi_injected = true;
4005 break;
4006 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01004007 /*
4008 * In case of software exceptions, do not reinject the vector,
4009 * but re-execute the instruction instead. Rewind RIP first
4010 * if we emulated INT3 before.
4011 */
4012 if (kvm_exception_is_soft(vector)) {
4013 if (vector == BP_VECTOR && int3_injected &&
4014 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
4015 kvm_rip_write(&svm->vcpu,
4016 kvm_rip_read(&svm->vcpu) -
4017 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02004018 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01004019 }
Gleb Natapov9222be12009-04-23 17:14:37 +03004020 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
4021 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02004022 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03004023
4024 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02004025 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03004026 break;
4027 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004028 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03004029 break;
4030 default:
4031 break;
4032 }
4033}
4034
Avi Kivityb463a6f2010-07-20 15:06:17 +03004035static void svm_cancel_injection(struct kvm_vcpu *vcpu)
4036{
4037 struct vcpu_svm *svm = to_svm(vcpu);
4038 struct vmcb_control_area *control = &svm->vmcb->control;
4039
4040 control->exit_int_info = control->event_inj;
4041 control->exit_int_info_err = control->event_inj_err;
4042 control->event_inj = 0;
4043 svm_complete_interrupts(svm);
4044}
4045
Avi Kivity851ba692009-08-24 11:10:17 +03004046static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004047{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004048 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03004049
Joerg Roedel2041a062010-04-22 12:33:08 +02004050 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4051 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4052 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4053
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004054 /*
4055 * A vmexit emulation is required before the vcpu can be executed
4056 * again.
4057 */
4058 if (unlikely(svm->nested.exit_required))
4059 return;
4060
Rusty Russelle756fc62007-07-30 20:07:08 +10004061 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062
Joerg Roedel649d6862008-04-16 16:51:15 +02004063 sync_lapic_to_cr8(vcpu);
4064
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02004065 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004066
Avi Kivity04d2cc72007-09-10 18:10:54 +03004067 clgi();
4068
4069 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08004070
Avi Kivity6aa8b732006-12-10 02:21:36 -08004071 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03004072 "push %%" _ASM_BP "; \n\t"
4073 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
4074 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
4075 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
4076 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
4077 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
4078 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004079#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004080 "mov %c[r8](%[svm]), %%r8 \n\t"
4081 "mov %c[r9](%[svm]), %%r9 \n\t"
4082 "mov %c[r10](%[svm]), %%r10 \n\t"
4083 "mov %c[r11](%[svm]), %%r11 \n\t"
4084 "mov %c[r12](%[svm]), %%r12 \n\t"
4085 "mov %c[r13](%[svm]), %%r13 \n\t"
4086 "mov %c[r14](%[svm]), %%r14 \n\t"
4087 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004088#endif
4089
Avi Kivity6aa8b732006-12-10 02:21:36 -08004090 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03004091 "push %%" _ASM_AX " \n\t"
4092 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004093 __ex(SVM_VMLOAD) "\n\t"
4094 __ex(SVM_VMRUN) "\n\t"
4095 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03004096 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004097
4098 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03004099 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
4100 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
4101 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
4102 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
4103 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
4104 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004105#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004106 "mov %%r8, %c[r8](%[svm]) \n\t"
4107 "mov %%r9, %c[r9](%[svm]) \n\t"
4108 "mov %%r10, %c[r10](%[svm]) \n\t"
4109 "mov %%r11, %c[r11](%[svm]) \n\t"
4110 "mov %%r12, %c[r12](%[svm]) \n\t"
4111 "mov %%r13, %c[r13](%[svm]) \n\t"
4112 "mov %%r14, %c[r14](%[svm]) \n\t"
4113 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004114#endif
Avi Kivity74547662012-09-16 15:10:59 +03004115 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08004116 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004117 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08004118 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004119 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
4120 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
4121 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
4122 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
4123 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
4124 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004125#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004126 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
4127 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
4128 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
4129 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
4130 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
4131 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
4132 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
4133 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02004135 : "cc", "memory"
4136#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03004137 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02004138 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03004139#else
4140 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02004141#endif
4142 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08004143
Avi Kivity82ca2d12010-10-21 12:20:34 +02004144#ifdef CONFIG_X86_64
4145 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
4146#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02004147 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02004148#ifndef CONFIG_X86_32_LAZY_GS
4149 loadsegment(gs, svm->host.gs);
4150#endif
Avi Kivity9581d442010-10-19 16:46:55 +02004151#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152
4153 reload_tss(vcpu);
4154
Avi Kivity56ba47d2007-11-07 17:14:18 +02004155 local_irq_disable();
4156
Avi Kivity13c34e02010-10-21 12:20:31 +02004157 vcpu->arch.cr2 = svm->vmcb->save.cr2;
4158 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
4159 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
4160 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
4161
Joerg Roedel3781c012011-01-14 16:45:02 +01004162 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
4163 kvm_before_handle_nmi(&svm->vcpu);
4164
4165 stgi();
4166
4167 /* Any pending NMI will happen here */
4168
4169 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
4170 kvm_after_handle_nmi(&svm->vcpu);
4171
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004172 sync_cr8_to_lapic(vcpu);
4173
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004174 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03004175
Joerg Roedel38e5e922010-12-03 15:25:16 +01004176 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
4177
Gleb Natapov631bc482010-10-14 11:22:52 +02004178 /* if exit due to PF check for async PF */
4179 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
4180 svm->apf_reason = kvm_read_and_reset_pf_reason();
4181
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004182 if (npt_enabled) {
4183 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
4184 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
4185 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02004186
4187 /*
4188 * We need to handle MC intercepts here before the vcpu has a chance to
4189 * change the physical cpu
4190 */
4191 if (unlikely(svm->vmcb->control.exit_code ==
4192 SVM_EXIT_EXCP_BASE + MC_VECTOR))
4193 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01004194
4195 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004196}
4197
Avi Kivity6aa8b732006-12-10 02:21:36 -08004198static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
4199{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004200 struct vcpu_svm *svm = to_svm(vcpu);
4201
4202 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01004203 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01004204 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004205}
4206
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004207static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
4208{
4209 struct vcpu_svm *svm = to_svm(vcpu);
4210
4211 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01004212 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004213
4214 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02004215 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01004216 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004217
Joerg Roedelf40f6a42010-12-03 15:25:15 +01004218 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004219}
4220
Avi Kivity6aa8b732006-12-10 02:21:36 -08004221static int is_disabled(void)
4222{
Joerg Roedel6031a612007-06-22 12:29:50 +03004223 u64 vm_cr;
4224
4225 rdmsrl(MSR_VM_CR, vm_cr);
4226 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
4227 return 1;
4228
Avi Kivity6aa8b732006-12-10 02:21:36 -08004229 return 0;
4230}
4231
Ingo Molnar102d8322007-02-19 14:37:47 +02004232static void
4233svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4234{
4235 /*
4236 * Patch in the VMMCALL instruction:
4237 */
4238 hypercall[0] = 0x0f;
4239 hypercall[1] = 0x01;
4240 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02004241}
4242
Yang, Sheng002c7f72007-07-31 14:23:01 +03004243static void svm_check_processor_compat(void *rtn)
4244{
4245 *(int *)rtn = 0;
4246}
4247
Avi Kivity774ead32007-12-26 13:57:04 +02004248static bool svm_cpu_has_accelerated_tpr(void)
4249{
4250 return false;
4251}
4252
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004253static bool svm_has_high_real_mode_segbase(void)
4254{
4255 return true;
4256}
4257
Paolo Bonzinifc07e762015-10-01 13:20:22 +02004258static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
4259{
4260 return 0;
4261}
4262
Sheng Yang0e851882009-12-18 16:48:46 +08004263static void svm_cpuid_update(struct kvm_vcpu *vcpu)
4264{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004265 struct vcpu_svm *svm = to_svm(vcpu);
4266
4267 /* Update nrips enabled cache */
4268 svm->nrips_enabled = !!guest_cpuid_has_nrips(&svm->vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08004269}
4270
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004271static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
4272{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004273 switch (func) {
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02004274 case 0x80000001:
4275 if (nested)
4276 entry->ecx |= (1 << 2); /* Set SVM bit */
4277 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004278 case 0x8000000A:
4279 entry->eax = 1; /* SVM revision 1 */
4280 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
4281 ASID emulation to nested SVM */
4282 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02004283 entry->edx = 0; /* Per default do not support any
4284 additional features */
4285
4286 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004287 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02004288 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004289
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02004290 /* Support NPT for the guest if enabled */
4291 if (npt_enabled)
4292 entry->edx |= SVM_FEATURE_NPT;
4293
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004294 break;
4295 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004296}
4297
Sheng Yang17cc3932010-01-05 19:02:27 +08004298static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004299{
Sheng Yang17cc3932010-01-05 19:02:27 +08004300 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004301}
4302
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004303static bool svm_rdtscp_supported(void)
4304{
Paolo Bonzini46896c72015-11-12 14:49:16 +01004305 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004306}
4307
Mao, Junjiead756a12012-07-02 01:18:48 +00004308static bool svm_invpcid_supported(void)
4309{
4310 return false;
4311}
4312
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004313static bool svm_mpx_supported(void)
4314{
4315 return false;
4316}
4317
Wanpeng Li55412b22014-12-02 19:21:30 +08004318static bool svm_xsaves_supported(void)
4319{
4320 return false;
4321}
4322
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004323static bool svm_has_wbinvd_exit(void)
4324{
4325 return true;
4326}
4327
Avi Kivity02daab22009-12-30 12:40:26 +02004328static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
4329{
4330 struct vcpu_svm *svm = to_svm(vcpu);
4331
Joerg Roedel18c918c2010-11-30 18:03:59 +01004332 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01004333 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02004334}
4335
Joerg Roedel80612522011-04-04 12:39:33 +02004336#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004337 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004338#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004339 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004340#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004341 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004342
Mathias Krause09941fb2012-08-30 01:30:20 +02004343static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004344 u32 exit_code;
4345 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004346} x86_intercept_map[] = {
4347 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4348 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4349 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4350 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4351 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004352 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4353 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004354 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4355 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4356 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4357 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4358 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4359 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4360 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4361 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004362 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4363 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4364 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4365 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4366 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4367 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4368 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4369 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004370 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4371 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4372 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004373 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4374 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4375 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4376 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4377 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4378 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4379 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4380 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4381 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004382 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4383 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4384 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4385 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4386 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4387 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4388 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004389 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4390 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4391 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4392 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004393};
4394
Joerg Roedel80612522011-04-04 12:39:33 +02004395#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004396#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004397#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004398
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004399static int svm_check_intercept(struct kvm_vcpu *vcpu,
4400 struct x86_instruction_info *info,
4401 enum x86_intercept_stage stage)
4402{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004403 struct vcpu_svm *svm = to_svm(vcpu);
4404 int vmexit, ret = X86EMUL_CONTINUE;
4405 struct __x86_intercept icpt_info;
4406 struct vmcb *vmcb = svm->vmcb;
4407
4408 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4409 goto out;
4410
4411 icpt_info = x86_intercept_map[info->intercept];
4412
Avi Kivity40e19b52011-04-21 12:35:41 +03004413 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004414 goto out;
4415
4416 switch (icpt_info.exit_code) {
4417 case SVM_EXIT_READ_CR0:
4418 if (info->intercept == x86_intercept_cr_read)
4419 icpt_info.exit_code += info->modrm_reg;
4420 break;
4421 case SVM_EXIT_WRITE_CR0: {
4422 unsigned long cr0, val;
4423 u64 intercept;
4424
4425 if (info->intercept == x86_intercept_cr_write)
4426 icpt_info.exit_code += info->modrm_reg;
4427
Jan Kiszka62baf442014-06-29 21:55:53 +02004428 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4429 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004430 break;
4431
4432 intercept = svm->nested.intercept;
4433
4434 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
4435 break;
4436
4437 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4438 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4439
4440 if (info->intercept == x86_intercept_lmsw) {
4441 cr0 &= 0xfUL;
4442 val &= 0xfUL;
4443 /* lmsw can't clear PE - catch this here */
4444 if (cr0 & X86_CR0_PE)
4445 val |= X86_CR0_PE;
4446 }
4447
4448 if (cr0 ^ val)
4449 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4450
4451 break;
4452 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004453 case SVM_EXIT_READ_DR0:
4454 case SVM_EXIT_WRITE_DR0:
4455 icpt_info.exit_code += info->modrm_reg;
4456 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004457 case SVM_EXIT_MSR:
4458 if (info->intercept == x86_intercept_wrmsr)
4459 vmcb->control.exit_info_1 = 1;
4460 else
4461 vmcb->control.exit_info_1 = 0;
4462 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004463 case SVM_EXIT_PAUSE:
4464 /*
4465 * We get this for NOP only, but pause
4466 * is rep not, check this here
4467 */
4468 if (info->rep_prefix != REPE_PREFIX)
4469 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02004470 case SVM_EXIT_IOIO: {
4471 u64 exit_info;
4472 u32 bytes;
4473
Joerg Roedelf6511932011-04-04 12:39:35 +02004474 if (info->intercept == x86_intercept_in ||
4475 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004476 exit_info = ((info->src_val & 0xffff) << 16) |
4477 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02004478 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02004479 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004480 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02004481 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02004482 }
4483
4484 if (info->intercept == x86_intercept_outs ||
4485 info->intercept == x86_intercept_ins)
4486 exit_info |= SVM_IOIO_STR_MASK;
4487
4488 if (info->rep_prefix)
4489 exit_info |= SVM_IOIO_REP_MASK;
4490
4491 bytes = min(bytes, 4u);
4492
4493 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4494
4495 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4496
4497 vmcb->control.exit_info_1 = exit_info;
4498 vmcb->control.exit_info_2 = info->next_rip;
4499
4500 break;
4501 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004502 default:
4503 break;
4504 }
4505
Bandan Dasf1047652015-06-11 02:05:33 -04004506 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4507 if (static_cpu_has(X86_FEATURE_NRIPS))
4508 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004509 vmcb->control.exit_code = icpt_info.exit_code;
4510 vmexit = nested_svm_exit_handled(svm);
4511
4512 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4513 : X86EMUL_CONTINUE;
4514
4515out:
4516 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004517}
4518
Yang Zhanga547c6d2013-04-11 19:25:10 +08004519static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
4520{
4521 local_irq_enable();
4522}
4523
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004524static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4525{
4526}
4527
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004528static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004529 .cpu_has_kvm_support = has_svm,
4530 .disabled_by_bios = is_disabled,
4531 .hardware_setup = svm_hardware_setup,
4532 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004533 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004534 .hardware_enable = svm_hardware_enable,
4535 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004536 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004537 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004538
4539 .vcpu_create = svm_create_vcpu,
4540 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004541 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004542
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004543 .vm_init = avic_vm_init,
4544 .vm_destroy = avic_vm_destroy,
4545
Avi Kivity04d2cc72007-09-10 18:10:54 +03004546 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004547 .vcpu_load = svm_vcpu_load,
4548 .vcpu_put = svm_vcpu_put,
4549
Paolo Bonzinia96036b2015-11-10 11:55:36 +01004550 .update_bp_intercept = update_bp_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004551 .get_msr = svm_get_msr,
4552 .set_msr = svm_set_msr,
4553 .get_segment_base = svm_get_segment_base,
4554 .get_segment = svm_get_segment,
4555 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004556 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004557 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004558 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004559 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004560 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004561 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004562 .set_cr3 = svm_set_cr3,
4563 .set_cr4 = svm_set_cr4,
4564 .set_efer = svm_set_efer,
4565 .get_idt = svm_get_idt,
4566 .set_idt = svm_set_idt,
4567 .get_gdt = svm_get_gdt,
4568 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004569 .get_dr6 = svm_get_dr6,
4570 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03004571 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01004572 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004573 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004574 .get_rflags = svm_get_rflags,
4575 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08004576
4577 .get_pkru = svm_get_pkru,
4578
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +02004579 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004580 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004581
Avi Kivity6aa8b732006-12-10 02:21:36 -08004582 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004583
Avi Kivity6aa8b732006-12-10 02:21:36 -08004584 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004585 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004586 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004587 .set_interrupt_shadow = svm_set_interrupt_shadow,
4588 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004589 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004590 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004591 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004592 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004593 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004594 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004595 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004596 .get_nmi_mask = svm_get_nmi_mask,
4597 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004598 .enable_nmi_window = enable_nmi_window,
4599 .enable_irq_window = enable_irq_window,
4600 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08004601 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03004602 .get_enable_apicv = svm_get_enable_apicv,
4603 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08004604 .load_eoi_exitmap = svm_load_eoi_exitmap,
Yang Zhanga20ed542013-04-11 19:25:15 +08004605 .sync_pir_to_irr = svm_sync_pir_to_irr,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004606 .hwapic_irr_update = svm_hwapic_irr_update,
4607 .hwapic_isr_update = svm_hwapic_isr_update,
Izik Eiduscbc94022007-10-25 00:29:55 +02004608
4609 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004610 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004611 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004612
Avi Kivity586f9602010-11-18 13:09:54 +02004613 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004614
Sheng Yang17cc3932010-01-05 19:02:27 +08004615 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004616
4617 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004618
4619 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00004620 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004621 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08004622 .xsaves_supported = svm_xsaves_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004623
4624 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004625
4626 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004627
Will Auldba904632012-11-29 12:42:50 -08004628 .read_tsc_offset = svm_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004629 .write_tsc_offset = svm_write_tsc_offset,
Haozhong Zhang58ea6762015-10-20 15:39:06 +08004630 .adjust_tsc_offset_guest = svm_adjust_tsc_offset_guest,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03004631 .read_l1_tsc = svm_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004632
4633 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004634
4635 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08004636 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004637
4638 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02004639
4640 .pmu_ops = &amd_pmu_ops,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004641};
4642
4643static int __init svm_init(void)
4644{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004645 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004646 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004647}
4648
4649static void __exit svm_exit(void)
4650{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004651 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004652}
4653
4654module_init(svm_init)
4655module_exit(svm_exit)