blob: db2c140ed079dda2188c3f023087f5682955c61d [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
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -050074#define SVM_AVIC_DOORBELL 0xc001011b
75
Joerg Roedel410e4d52009-08-07 11:49:44 +020076#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
77#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
78#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
79
Joerg Roedel24e09cb2008-02-13 18:58:47 +010080#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
81
Joerg Roedelfbc0db72011-03-25 09:44:46 +010082#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010083#define TSC_RATIO_MIN 0x0000000000000001ULL
84#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010085
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050086#define AVIC_HPA_MASK ~((0xFFFULL << 52) || 0xFFF)
87
88/*
89 * 0xff is broadcast, so the max index allowed for physical APIC ID
90 * table is 0xfe. APIC IDs above 0xff are reserved.
91 */
92#define AVIC_MAX_PHYSICAL_ID_COUNT 255
93
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050094#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
95#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
96#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
97
Joerg Roedel67ec6602010-05-17 14:43:35 +020098static bool erratum_383_found __read_mostly;
99
Avi Kivity6c8166a2009-05-31 18:15:37 +0300100static const u32 host_save_user_msrs[] = {
101#ifdef CONFIG_X86_64
102 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
103 MSR_FS_BASE,
104#endif
105 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Paolo Bonzini46896c72015-11-12 14:49:16 +0100106 MSR_TSC_AUX,
Avi Kivity6c8166a2009-05-31 18:15:37 +0300107};
108
109#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
110
111struct kvm_vcpu;
112
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200113struct nested_state {
114 struct vmcb *hsave;
115 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +0100116 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200117 u64 vmcb;
118
119 /* These are the merged vectors */
120 u32 *msrpm;
121
122 /* gpa pointers to the real vectors */
123 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100124 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200125
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200126 /* A VMEXIT is required but not yet emulated */
127 bool exit_required;
128
Joerg Roedelaad42c62009-08-07 11:49:34 +0200129 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100130 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100131 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200132 u32 intercept_exceptions;
133 u64 intercept;
134
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200135 /* Nested Paging related state */
136 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200137};
138
Joerg Roedel323c3d82010-03-01 15:34:37 +0100139#define MSRPM_OFFSETS 16
140static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
141
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500142/*
143 * Set osvw_len to higher value when updated Revision Guides
144 * are published and we know what the new status bits are
145 */
146static uint64_t osvw_len = 4, osvw_status;
147
Avi Kivity6c8166a2009-05-31 18:15:37 +0300148struct vcpu_svm {
149 struct kvm_vcpu vcpu;
150 struct vmcb *vmcb;
151 unsigned long vmcb_pa;
152 struct svm_cpu_data *svm_data;
153 uint64_t asid_generation;
154 uint64_t sysenter_esp;
155 uint64_t sysenter_eip;
Paolo Bonzini46896c72015-11-12 14:49:16 +0100156 uint64_t tsc_aux;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300157
158 u64 next_rip;
159
160 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200161 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200162 u16 fs;
163 u16 gs;
164 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200165 u64 gs_base;
166 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300167
168 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300169
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200170 ulong nmi_iret_rip;
171
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200172 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200173
174 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100175
176 unsigned int3_injected;
177 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200178 u32 apf_reason;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100179
Joerg Roedel6092d3d2015-10-14 15:10:54 +0200180 /* cached guest cpuid flags for faster access */
181 bool nrips_enabled : 1;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500182
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500183 u32 ldr_reg;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500184 struct page *avic_backing_page;
185 u64 *avic_physical_id_cache;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300186};
187
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500188#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
189#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
190
191#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
192#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
193#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
194#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
195
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100196static DEFINE_PER_CPU(u64, current_tsc_ratio);
197#define TSC_RATIO_DEFAULT 0x0100000000ULL
198
Joerg Roedel455716f2010-03-01 15:34:35 +0100199#define MSR_INVALID 0xffffffffU
200
Mathias Krause09941fb2012-08-30 01:30:20 +0200201static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100202 u32 index; /* Index of the MSR */
203 bool always; /* True if intercept is always on */
204} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400205 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100206 { .index = MSR_IA32_SYSENTER_CS, .always = true },
207#ifdef CONFIG_X86_64
208 { .index = MSR_GS_BASE, .always = true },
209 { .index = MSR_FS_BASE, .always = true },
210 { .index = MSR_KERNEL_GS_BASE, .always = true },
211 { .index = MSR_LSTAR, .always = true },
212 { .index = MSR_CSTAR, .always = true },
213 { .index = MSR_SYSCALL_MASK, .always = true },
214#endif
215 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
216 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
217 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
218 { .index = MSR_IA32_LASTINTTOIP, .always = false },
219 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220};
221
222/* enable NPT for AMD64 and X86 with PAE */
223#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
224static bool npt_enabled = true;
225#else
Joerg Roedele0231712010-02-24 18:59:10 +0100226static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227#endif
228
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100229/* allow nested paging (virtualized MMU) for all guests */
230static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231module_param(npt, int, S_IRUGO);
232
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100233/* allow nested virtualization in KVM/SVM */
234static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235module_param(nested, int, S_IRUGO);
236
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500237/* enable / disable AVIC */
238static int avic;
239module_param(avic, int, S_IRUGO);
240
Paolo Bonzini79a80592015-09-21 07:46:55 +0200241static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200243static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244
Joerg Roedel410e4d52009-08-07 11:49:44 +0200245static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100246static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
249 bool has_error_code, u32 error_code);
250
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100251enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100252 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
253 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100254 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100255 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100256 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100257 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100258 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100259 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100260 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100261 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100262 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100263 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500264 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
265 * AVIC PHYSICAL_TABLE pointer,
266 * AVIC LOGICAL_TABLE pointer
267 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100268 VMCB_DIRTY_MAX,
269};
270
Joerg Roedel0574dec2010-12-03 11:45:58 +0100271/* TPR and CR2 are always written before VMRUN */
272#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100273
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500274#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
275
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100276static inline void mark_all_dirty(struct vmcb *vmcb)
277{
278 vmcb->control.clean = 0;
279}
280
281static inline void mark_all_clean(struct vmcb *vmcb)
282{
283 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
284 & ~VMCB_ALWAYS_DIRTY_MASK;
285}
286
287static inline void mark_dirty(struct vmcb *vmcb, int bit)
288{
289 vmcb->control.clean &= ~(1 << bit);
290}
291
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
293{
294 return container_of(vcpu, struct vcpu_svm, vcpu);
295}
296
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500297static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
298{
299 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
300 mark_dirty(svm->vmcb, VMCB_AVIC);
301}
302
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -0500303static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
304{
305 struct vcpu_svm *svm = to_svm(vcpu);
306 u64 *entry = svm->avic_physical_id_cache;
307
308 if (!entry)
309 return false;
310
311 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
312}
313
Joerg Roedel384c6362010-11-30 18:03:56 +0100314static void recalc_intercepts(struct vcpu_svm *svm)
315{
316 struct vmcb_control_area *c, *h;
317 struct nested_state *g;
318
Joerg Roedel116a0a22010-12-03 11:45:49 +0100319 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
320
Joerg Roedel384c6362010-11-30 18:03:56 +0100321 if (!is_guest_mode(&svm->vcpu))
322 return;
323
324 c = &svm->vmcb->control;
325 h = &svm->nested.hsave->control;
326 g = &svm->nested;
327
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100328 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100329 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100330 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
331 c->intercept = h->intercept | g->intercept;
332}
333
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100334static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
335{
336 if (is_guest_mode(&svm->vcpu))
337 return svm->nested.hsave;
338 else
339 return svm->vmcb;
340}
341
342static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
343{
344 struct vmcb *vmcb = get_host_vmcb(svm);
345
346 vmcb->control.intercept_cr |= (1U << bit);
347
348 recalc_intercepts(svm);
349}
350
351static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
352{
353 struct vmcb *vmcb = get_host_vmcb(svm);
354
355 vmcb->control.intercept_cr &= ~(1U << bit);
356
357 recalc_intercepts(svm);
358}
359
360static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
361{
362 struct vmcb *vmcb = get_host_vmcb(svm);
363
364 return vmcb->control.intercept_cr & (1U << bit);
365}
366
Paolo Bonzini5315c712014-03-03 13:08:29 +0100367static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100368{
369 struct vmcb *vmcb = get_host_vmcb(svm);
370
Paolo Bonzini5315c712014-03-03 13:08:29 +0100371 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
372 | (1 << INTERCEPT_DR1_READ)
373 | (1 << INTERCEPT_DR2_READ)
374 | (1 << INTERCEPT_DR3_READ)
375 | (1 << INTERCEPT_DR4_READ)
376 | (1 << INTERCEPT_DR5_READ)
377 | (1 << INTERCEPT_DR6_READ)
378 | (1 << INTERCEPT_DR7_READ)
379 | (1 << INTERCEPT_DR0_WRITE)
380 | (1 << INTERCEPT_DR1_WRITE)
381 | (1 << INTERCEPT_DR2_WRITE)
382 | (1 << INTERCEPT_DR3_WRITE)
383 | (1 << INTERCEPT_DR4_WRITE)
384 | (1 << INTERCEPT_DR5_WRITE)
385 | (1 << INTERCEPT_DR6_WRITE)
386 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100387
388 recalc_intercepts(svm);
389}
390
Paolo Bonzini5315c712014-03-03 13:08:29 +0100391static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100392{
393 struct vmcb *vmcb = get_host_vmcb(svm);
394
Paolo Bonzini5315c712014-03-03 13:08:29 +0100395 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100396
397 recalc_intercepts(svm);
398}
399
Joerg Roedel18c918c2010-11-30 18:03:59 +0100400static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
401{
402 struct vmcb *vmcb = get_host_vmcb(svm);
403
404 vmcb->control.intercept_exceptions |= (1U << bit);
405
406 recalc_intercepts(svm);
407}
408
409static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
410{
411 struct vmcb *vmcb = get_host_vmcb(svm);
412
413 vmcb->control.intercept_exceptions &= ~(1U << bit);
414
415 recalc_intercepts(svm);
416}
417
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100418static inline void set_intercept(struct vcpu_svm *svm, int bit)
419{
420 struct vmcb *vmcb = get_host_vmcb(svm);
421
422 vmcb->control.intercept |= (1ULL << bit);
423
424 recalc_intercepts(svm);
425}
426
427static inline void clr_intercept(struct vcpu_svm *svm, int bit)
428{
429 struct vmcb *vmcb = get_host_vmcb(svm);
430
431 vmcb->control.intercept &= ~(1ULL << bit);
432
433 recalc_intercepts(svm);
434}
435
Joerg Roedel2af91942009-08-07 11:49:28 +0200436static inline void enable_gif(struct vcpu_svm *svm)
437{
438 svm->vcpu.arch.hflags |= HF_GIF_MASK;
439}
440
441static inline void disable_gif(struct vcpu_svm *svm)
442{
443 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
444}
445
446static inline bool gif_set(struct vcpu_svm *svm)
447{
448 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
449}
450
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451static unsigned long iopm_base;
452
453struct kvm_ldttss_desc {
454 u16 limit0;
455 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100456 unsigned base1:8, type:5, dpl:2, p:1;
457 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458 u32 base3;
459 u32 zero1;
460} __attribute__((packed));
461
462struct svm_cpu_data {
463 int cpu;
464
Avi Kivity5008fdf2007-04-02 13:05:50 +0300465 u64 asid_generation;
466 u32 max_asid;
467 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468 struct kvm_ldttss_desc *tss_desc;
469
470 struct page *save_area;
471};
472
473static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
474
475struct svm_init_data {
476 int cpu;
477 int r;
478};
479
Mathias Krause09941fb2012-08-30 01:30:20 +0200480static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200482#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483#define MSRS_RANGE_SIZE 2048
484#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
485
Joerg Roedel455716f2010-03-01 15:34:35 +0100486static u32 svm_msrpm_offset(u32 msr)
487{
488 u32 offset;
489 int i;
490
491 for (i = 0; i < NUM_MSR_MAPS; i++) {
492 if (msr < msrpm_ranges[i] ||
493 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
494 continue;
495
496 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
497 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
498
499 /* Now we have the u8 offset - but need the u32 offset */
500 return offset / 4;
501 }
502
503 /* MSR not in any range */
504 return MSR_INVALID;
505}
506
Avi Kivity6aa8b732006-12-10 02:21:36 -0800507#define MAX_INST_SIZE 15
508
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509static inline void clgi(void)
510{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300511 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512}
513
514static inline void stgi(void)
515{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300516 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517}
518
519static inline void invlpga(unsigned long addr, u32 asid)
520{
Joerg Roedele0231712010-02-24 18:59:10 +0100521 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800522}
523
Joerg Roedel4b161842010-09-10 17:31:03 +0200524static int get_npt_level(void)
525{
526#ifdef CONFIG_X86_64
527 return PT64_ROOT_LEVEL;
528#else
529 return PT32E_ROOT_LEVEL;
530#endif
531}
532
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
534{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000535 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100536 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600537 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538
Alexander Graf9962d032008-11-25 20:17:02 +0100539 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100540 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541}
542
Avi Kivity6aa8b732006-12-10 02:21:36 -0800543static int is_external_interrupt(u32 info)
544{
545 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
546 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
547}
548
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200549static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400550{
551 struct vcpu_svm *svm = to_svm(vcpu);
552 u32 ret = 0;
553
554 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200555 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
556 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400557}
558
559static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
560{
561 struct vcpu_svm *svm = to_svm(vcpu);
562
563 if (mask == 0)
564 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
565 else
566 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
567
568}
569
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
571{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400572 struct vcpu_svm *svm = to_svm(vcpu);
573
Bandan Dasf1047652015-06-11 02:05:33 -0400574 if (svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200575 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200576 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400577 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200578
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400579 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100580 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300581 EMULATE_DONE)
582 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583 return;
584 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300585 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
586 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
587 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300589 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400590 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591}
592
Jan Kiszka116a4752010-02-23 17:47:54 +0100593static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200594 bool has_error_code, u32 error_code,
595 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100596{
597 struct vcpu_svm *svm = to_svm(vcpu);
598
Joerg Roedele0231712010-02-24 18:59:10 +0100599 /*
600 * If we are within a nested VM we'd better #VMEXIT and let the guest
601 * handle the exception
602 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200603 if (!reinject &&
604 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100605 return;
606
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200607 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100608 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
609
610 /*
611 * For guest debugging where we have to reinject #BP if some
612 * INT3 is guest-owned:
613 * Emulate nRIP by moving RIP forward. Will fail if injection
614 * raises a fault that is not intercepted. Still better than
615 * failing in all cases.
616 */
617 skip_emulated_instruction(&svm->vcpu);
618 rip = kvm_rip_read(&svm->vcpu);
619 svm->int3_rip = rip + svm->vmcb->save.cs.base;
620 svm->int3_injected = rip - old_rip;
621 }
622
Jan Kiszka116a4752010-02-23 17:47:54 +0100623 svm->vmcb->control.event_inj = nr
624 | SVM_EVTINJ_VALID
625 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
626 | SVM_EVTINJ_TYPE_EXEPT;
627 svm->vmcb->control.event_inj_err = error_code;
628}
629
Joerg Roedel67ec6602010-05-17 14:43:35 +0200630static void svm_init_erratum_383(void)
631{
632 u32 low, high;
633 int err;
634 u64 val;
635
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100636 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200637 return;
638
639 /* Use _safe variants to not break nested virtualization */
640 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
641 if (err)
642 return;
643
644 val |= (1ULL << 47);
645
646 low = lower_32_bits(val);
647 high = upper_32_bits(val);
648
649 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
650
651 erratum_383_found = true;
652}
653
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500654static void svm_init_osvw(struct kvm_vcpu *vcpu)
655{
656 /*
657 * Guests should see errata 400 and 415 as fixed (assuming that
658 * HLT and IO instructions are intercepted).
659 */
660 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
661 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
662
663 /*
664 * By increasing VCPU's osvw.length to 3 we are telling the guest that
665 * all osvw.status bits inside that length, including bit 0 (which is
666 * reserved for erratum 298), are valid. However, if host processor's
667 * osvw_len is 0 then osvw_status[0] carries no information. We need to
668 * be conservative here and therefore we tell the guest that erratum 298
669 * is present (because we really don't know).
670 */
671 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
672 vcpu->arch.osvw.status |= 1;
673}
674
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675static int has_svm(void)
676{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200677 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678
Eduardo Habkost63d11422008-11-17 19:03:20 -0200679 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800680 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681 return 0;
682 }
683
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684 return 1;
685}
686
Radim Krčmář13a34e02014-08-28 15:13:03 +0200687static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100689 /* Make sure we clean up behind us */
690 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
691 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
692
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200693 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100694
695 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696}
697
Radim Krčmář13a34e02014-08-28 15:13:03 +0200698static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699{
700
Tejun Heo0fe1e002009-10-29 22:34:14 +0900701 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800702 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200703 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704 struct desc_struct *gdt;
705 int me = raw_smp_processor_id();
706
Alexander Graf10474ae2009-09-15 11:37:46 +0200707 rdmsrl(MSR_EFER, efer);
708 if (efer & EFER_SVME)
709 return -EBUSY;
710
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200712 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200713 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900715 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900716 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200717 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200718 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719 }
720
Tejun Heo0fe1e002009-10-29 22:34:14 +0900721 sd->asid_generation = 1;
722 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
723 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200725 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200726 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900727 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728
Alexander Graf9962d032008-11-25 20:17:02 +0100729 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730
Linus Torvaldsd0316552009-12-14 09:58:24 -0800731 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200732
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100733 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
734 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500735 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100736 }
737
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500738
739 /*
740 * Get OSVW bits.
741 *
742 * Note that it is possible to have a system with mixed processor
743 * revisions and therefore different OSVW bits. If bits are not the same
744 * on different processors then choose the worst case (i.e. if erratum
745 * is present on one processor and not on another then assume that the
746 * erratum is present everywhere).
747 */
748 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
749 uint64_t len, status = 0;
750 int err;
751
752 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
753 if (!err)
754 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
755 &err);
756
757 if (err)
758 osvw_status = osvw_len = 0;
759 else {
760 if (len < osvw_len)
761 osvw_len = len;
762 osvw_status |= status;
763 osvw_status &= (1ULL << osvw_len) - 1;
764 }
765 } else
766 osvw_status = osvw_len = 0;
767
Joerg Roedel67ec6602010-05-17 14:43:35 +0200768 svm_init_erratum_383();
769
Joerg Roedel1018faa2012-02-29 14:57:32 +0100770 amd_pmu_enable_virt();
771
Alexander Graf10474ae2009-09-15 11:37:46 +0200772 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773}
774
Joerg Roedel0da1db752008-07-02 16:02:11 +0200775static void svm_cpu_uninit(int cpu)
776{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900777 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200778
Tejun Heo0fe1e002009-10-29 22:34:14 +0900779 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200780 return;
781
782 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900783 __free_page(sd->save_area);
784 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200785}
786
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787static int svm_cpu_init(int cpu)
788{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900789 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 int r;
791
Tejun Heo0fe1e002009-10-29 22:34:14 +0900792 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
793 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900795 sd->cpu = cpu;
796 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900798 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799 goto err_1;
800
Tejun Heo0fe1e002009-10-29 22:34:14 +0900801 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802
803 return 0;
804
805err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900806 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807 return r;
808
809}
810
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100811static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812{
813 int i;
814
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100815 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
816 if (direct_access_msrs[i].index == index)
817 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100819 return false;
820}
821
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822static void set_msr_interception(u32 *msrpm, unsigned msr,
823 int read, int write)
824{
Joerg Roedel455716f2010-03-01 15:34:35 +0100825 u8 bit_read, bit_write;
826 unsigned long tmp;
827 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100829 /*
830 * If this warning triggers extend the direct_access_msrs list at the
831 * beginning of the file
832 */
833 WARN_ON(!valid_msr_intercept(msr));
834
Joerg Roedel455716f2010-03-01 15:34:35 +0100835 offset = svm_msrpm_offset(msr);
836 bit_read = 2 * (msr & 0x0f);
837 bit_write = 2 * (msr & 0x0f) + 1;
838 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839
Joerg Roedel455716f2010-03-01 15:34:35 +0100840 BUG_ON(offset == MSR_INVALID);
841
842 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
843 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
844
845 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846}
847
Joerg Roedelf65c2292008-02-13 18:58:46 +0100848static void svm_vcpu_init_msrpm(u32 *msrpm)
849{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100850 int i;
851
Joerg Roedelf65c2292008-02-13 18:58:46 +0100852 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
853
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100854 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
855 if (!direct_access_msrs[i].always)
856 continue;
857
858 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
859 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100860}
861
Joerg Roedel323c3d82010-03-01 15:34:37 +0100862static void add_msr_offset(u32 offset)
863{
864 int i;
865
866 for (i = 0; i < MSRPM_OFFSETS; ++i) {
867
868 /* Offset already in list? */
869 if (msrpm_offsets[i] == offset)
870 return;
871
872 /* Slot used by another offset? */
873 if (msrpm_offsets[i] != MSR_INVALID)
874 continue;
875
876 /* Add offset to list */
877 msrpm_offsets[i] = offset;
878
879 return;
880 }
881
882 /*
883 * If this BUG triggers the msrpm_offsets table has an overflow. Just
884 * increase MSRPM_OFFSETS in this case.
885 */
886 BUG();
887}
888
889static void init_msrpm_offsets(void)
890{
891 int i;
892
893 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
894
895 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
896 u32 offset;
897
898 offset = svm_msrpm_offset(direct_access_msrs[i].index);
899 BUG_ON(offset == MSR_INVALID);
900
901 add_msr_offset(offset);
902 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903}
904
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100905static void svm_enable_lbrv(struct vcpu_svm *svm)
906{
907 u32 *msrpm = svm->msrpm;
908
909 svm->vmcb->control.lbr_ctl = 1;
910 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
911 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
912 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
913 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
914}
915
916static void svm_disable_lbrv(struct vcpu_svm *svm)
917{
918 u32 *msrpm = svm->msrpm;
919
920 svm->vmcb->control.lbr_ctl = 0;
921 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
922 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
923 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
924 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
925}
926
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927static __init int svm_hardware_setup(void)
928{
929 int cpu;
930 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100931 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932 int r;
933
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
935
936 if (!iopm_pages)
937 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300938
939 iopm_va = page_address(iopm_pages);
940 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
942
Joerg Roedel323c3d82010-03-01 15:34:37 +0100943 init_msrpm_offsets();
944
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100945 if (boot_cpu_has(X86_FEATURE_NX))
946 kvm_enable_efer_bits(EFER_NX);
947
Alexander Graf1b2fd702009-02-02 16:23:51 +0100948 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
949 kvm_enable_efer_bits(EFER_FFXSR);
950
Joerg Roedel92a1f122011-03-25 09:44:51 +0100951 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +0100952 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800953 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
954 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100955 }
956
Alexander Graf236de052008-11-25 20:17:10 +0100957 if (nested) {
958 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200959 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100960 }
961
Zachary Amsden3230bb42009-09-29 11:38:37 -1000962 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 r = svm_cpu_init(cpu);
964 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100965 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100967
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200968 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100969 npt_enabled = false;
970
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100971 if (npt_enabled && !npt) {
972 printk(KERN_INFO "kvm: Nested Paging disabled\n");
973 npt_enabled = false;
974 }
975
Joerg Roedel18552672008-02-07 13:47:41 +0100976 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100977 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100978 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200979 } else
980 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100981
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500982 if (avic && (!npt_enabled || !boot_cpu_has(X86_FEATURE_AVIC)))
983 avic = false;
984
985 if (avic)
986 pr_info("AVIC enabled\n");
987
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 return 0;
989
Joerg Roedelf65c2292008-02-13 18:58:46 +0100990err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
992 iopm_base = 0;
993 return r;
994}
995
996static __exit void svm_hardware_unsetup(void)
997{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200998 int cpu;
999
Zachary Amsden3230bb42009-09-29 11:38:37 -10001000 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +02001001 svm_cpu_uninit(cpu);
1002
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001004 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005}
1006
1007static void init_seg(struct vmcb_seg *seg)
1008{
1009 seg->selector = 0;
1010 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001011 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 seg->limit = 0xffff;
1013 seg->base = 0;
1014}
1015
1016static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1017{
1018 seg->selector = 0;
1019 seg->attrib = SVM_SELECTOR_P_MASK | type;
1020 seg->limit = 0xffff;
1021 seg->base = 0;
1022}
1023
Will Auldba904632012-11-29 12:42:50 -08001024static u64 svm_read_tsc_offset(struct kvm_vcpu *vcpu)
1025{
1026 struct vcpu_svm *svm = to_svm(vcpu);
1027
1028 return svm->vmcb->control.tsc_offset;
1029}
1030
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001031static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1032{
1033 struct vcpu_svm *svm = to_svm(vcpu);
1034 u64 g_tsc_offset = 0;
1035
Joerg Roedel20307532010-11-29 17:51:48 +01001036 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001037 g_tsc_offset = svm->vmcb->control.tsc_offset -
1038 svm->nested.hsave->control.tsc_offset;
1039 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001040 } else
1041 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1042 svm->vmcb->control.tsc_offset,
1043 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001044
1045 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001046
1047 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001048}
1049
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001050static void svm_adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, s64 adjustment)
Zachary Amsdene48672f2010-08-19 22:07:23 -10001051{
1052 struct vcpu_svm *svm = to_svm(vcpu);
1053
1054 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +01001055 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -10001056 svm->nested.hsave->control.tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001057 else
1058 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1059 svm->vmcb->control.tsc_offset - adjustment,
1060 svm->vmcb->control.tsc_offset);
1061
Joerg Roedel116a0a22010-12-03 11:45:49 +01001062 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001063}
1064
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001065static void avic_init_vmcb(struct vcpu_svm *svm)
1066{
1067 struct vmcb *vmcb = svm->vmcb;
1068 struct kvm_arch *vm_data = &svm->vcpu.kvm->arch;
1069 phys_addr_t bpa = page_to_phys(svm->avic_backing_page);
1070 phys_addr_t lpa = page_to_phys(vm_data->avic_logical_id_table_page);
1071 phys_addr_t ppa = page_to_phys(vm_data->avic_physical_id_table_page);
1072
1073 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1074 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1075 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1076 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1077 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
1078 svm->vcpu.arch.apicv_active = true;
1079}
1080
Paolo Bonzini56908912015-10-19 11:30:19 +02001081static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082{
Joerg Roedele6101a92008-02-13 18:58:45 +01001083 struct vmcb_control_area *control = &svm->vmcb->control;
1084 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085
Avi Kivitybff78272010-01-07 13:16:08 +02001086 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001087 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001088
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001089 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1090 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1091 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1092 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1093 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1094 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
1095 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096
Paolo Bonzini5315c712014-03-03 13:08:29 +01001097 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098
Joerg Roedel18c918c2010-11-30 18:03:59 +01001099 set_exception_intercept(svm, PF_VECTOR);
1100 set_exception_intercept(svm, UD_VECTOR);
1101 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001102 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001103 set_exception_intercept(svm, DB_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001105 set_intercept(svm, INTERCEPT_INTR);
1106 set_intercept(svm, INTERCEPT_NMI);
1107 set_intercept(svm, INTERCEPT_SMI);
1108 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001109 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001110 set_intercept(svm, INTERCEPT_CPUID);
1111 set_intercept(svm, INTERCEPT_INVD);
1112 set_intercept(svm, INTERCEPT_HLT);
1113 set_intercept(svm, INTERCEPT_INVLPG);
1114 set_intercept(svm, INTERCEPT_INVLPGA);
1115 set_intercept(svm, INTERCEPT_IOIO_PROT);
1116 set_intercept(svm, INTERCEPT_MSR_PROT);
1117 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1118 set_intercept(svm, INTERCEPT_SHUTDOWN);
1119 set_intercept(svm, INTERCEPT_VMRUN);
1120 set_intercept(svm, INTERCEPT_VMMCALL);
1121 set_intercept(svm, INTERCEPT_VMLOAD);
1122 set_intercept(svm, INTERCEPT_VMSAVE);
1123 set_intercept(svm, INTERCEPT_STGI);
1124 set_intercept(svm, INTERCEPT_CLGI);
1125 set_intercept(svm, INTERCEPT_SKINIT);
1126 set_intercept(svm, INTERCEPT_WBINVD);
1127 set_intercept(svm, INTERCEPT_MONITOR);
1128 set_intercept(svm, INTERCEPT_MWAIT);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001129 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130
1131 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001132 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133 control->int_ctl = V_INTR_MASKING_MASK;
1134
1135 init_seg(&save->es);
1136 init_seg(&save->ss);
1137 init_seg(&save->ds);
1138 init_seg(&save->fs);
1139 init_seg(&save->gs);
1140
1141 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001142 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143 /* Executable/Readable Code Segment */
1144 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1145 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1146 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147
1148 save->gdtr.limit = 0xffff;
1149 save->idtr.limit = 0xffff;
1150
1151 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1152 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1153
Paolo Bonzini56908912015-10-19 11:30:19 +02001154 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001155 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001156 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001158 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159
Joerg Roedele0231712010-02-24 18:59:10 +01001160 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001161 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001162 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001164 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001165 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001166
Rusty Russell66aee912007-07-17 23:34:16 +10001167 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001169
1170 if (npt_enabled) {
1171 /* Setup VMCB for Nested Paging */
1172 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001173 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001174 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001175 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1176 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001177 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001178 save->cr3 = 0;
1179 save->cr4 = 0;
1180 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001181 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001182
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001183 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001184 svm->vcpu.arch.hflags = 0;
1185
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001186 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001187 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001188 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001189 }
1190
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001191 if (avic)
1192 avic_init_vmcb(svm);
1193
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001194 mark_all_dirty(svm->vmcb);
1195
Joerg Roedel2af91942009-08-07 11:49:28 +02001196 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001197
1198}
1199
1200static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu, int index)
1201{
1202 u64 *avic_physical_id_table;
1203 struct kvm_arch *vm_data = &vcpu->kvm->arch;
1204
1205 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1206 return NULL;
1207
1208 avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page);
1209
1210 return &avic_physical_id_table[index];
1211}
1212
1213/**
1214 * Note:
1215 * AVIC hardware walks the nested page table to check permissions,
1216 * but does not use the SPA address specified in the leaf page
1217 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1218 * field of the VMCB. Therefore, we set up the
1219 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1220 */
1221static int avic_init_access_page(struct kvm_vcpu *vcpu)
1222{
1223 struct kvm *kvm = vcpu->kvm;
1224 int ret;
1225
1226 if (kvm->arch.apic_access_page_done)
1227 return 0;
1228
1229 ret = x86_set_memory_region(kvm,
1230 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1231 APIC_DEFAULT_PHYS_BASE,
1232 PAGE_SIZE);
1233 if (ret)
1234 return ret;
1235
1236 kvm->arch.apic_access_page_done = true;
1237 return 0;
1238}
1239
1240static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1241{
1242 int ret;
1243 u64 *entry, new_entry;
1244 int id = vcpu->vcpu_id;
1245 struct vcpu_svm *svm = to_svm(vcpu);
1246
1247 ret = avic_init_access_page(vcpu);
1248 if (ret)
1249 return ret;
1250
1251 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1252 return -EINVAL;
1253
1254 if (!svm->vcpu.arch.apic->regs)
1255 return -EINVAL;
1256
1257 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1258
1259 /* Setting AVIC backing page address in the phy APIC ID table */
1260 entry = avic_get_physical_id_entry(vcpu, id);
1261 if (!entry)
1262 return -EINVAL;
1263
1264 new_entry = READ_ONCE(*entry);
1265 new_entry = (page_to_phys(svm->avic_backing_page) &
1266 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1267 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK;
1268 WRITE_ONCE(*entry, new_entry);
1269
1270 svm->avic_physical_id_cache = entry;
1271
1272 return 0;
1273}
1274
1275static void avic_vm_destroy(struct kvm *kvm)
1276{
1277 struct kvm_arch *vm_data = &kvm->arch;
1278
1279 if (vm_data->avic_logical_id_table_page)
1280 __free_page(vm_data->avic_logical_id_table_page);
1281 if (vm_data->avic_physical_id_table_page)
1282 __free_page(vm_data->avic_physical_id_table_page);
1283}
1284
1285static int avic_vm_init(struct kvm *kvm)
1286{
1287 int err = -ENOMEM;
1288 struct kvm_arch *vm_data = &kvm->arch;
1289 struct page *p_page;
1290 struct page *l_page;
1291
1292 if (!avic)
1293 return 0;
1294
1295 /* Allocating physical APIC ID table (4KB) */
1296 p_page = alloc_page(GFP_KERNEL);
1297 if (!p_page)
1298 goto free_avic;
1299
1300 vm_data->avic_physical_id_table_page = p_page;
1301 clear_page(page_address(p_page));
1302
1303 /* Allocating logical APIC ID table (4KB) */
1304 l_page = alloc_page(GFP_KERNEL);
1305 if (!l_page)
1306 goto free_avic;
1307
1308 vm_data->avic_logical_id_table_page = l_page;
1309 clear_page(page_address(l_page));
1310
1311 return 0;
1312
1313free_avic:
1314 avic_vm_destroy(kvm);
1315 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316}
1317
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001318static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001319{
1320 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001321 u32 dummy;
1322 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001323
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001324 if (!init_event) {
1325 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1326 MSR_IA32_APICBASE_ENABLE;
1327 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1328 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1329 }
Paolo Bonzini56908912015-10-19 11:30:19 +02001330 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001331
Julian Stecklina66f7b722012-12-05 15:26:19 +01001332 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy);
1333 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001334
1335 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1336 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001337}
1338
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001339static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001341 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001342 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001343 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001344 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001345 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001346 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347
Rusty Russellc16f8622007-07-30 21:12:19 +10001348 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001349 if (!svm) {
1350 err = -ENOMEM;
1351 goto out;
1352 }
1353
1354 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1355 if (err)
1356 goto free_svm;
1357
Joerg Roedelf65c2292008-02-13 18:58:46 +01001358 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001359 page = alloc_page(GFP_KERNEL);
1360 if (!page)
1361 goto uninit;
1362
Joerg Roedelf65c2292008-02-13 18:58:46 +01001363 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1364 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001365 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001366
1367 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1368 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001369 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001370
Alexander Grafb286d5d2008-11-25 20:17:05 +01001371 hsave_page = alloc_page(GFP_KERNEL);
1372 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001373 goto free_page3;
1374
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001375 if (avic) {
1376 err = avic_init_backing_page(&svm->vcpu);
1377 if (err)
1378 goto free_page4;
1379 }
1380
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001381 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001382
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001383 svm->msrpm = page_address(msrpm_pages);
1384 svm_vcpu_init_msrpm(svm->msrpm);
1385
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001386 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001387 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001388
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001389 svm->vmcb = page_address(page);
1390 clear_page(svm->vmcb);
1391 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1392 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02001393 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001395 svm_init_osvw(&svm->vcpu);
1396
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001397 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001398
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001399free_page4:
1400 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001401free_page3:
1402 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1403free_page2:
1404 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1405free_page1:
1406 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001407uninit:
1408 kvm_vcpu_uninit(&svm->vcpu);
1409free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001410 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001411out:
1412 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413}
1414
1415static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1416{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001417 struct vcpu_svm *svm = to_svm(vcpu);
1418
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001419 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001420 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001421 __free_page(virt_to_page(svm->nested.hsave));
1422 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001423 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001424 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425}
1426
Avi Kivity15ad7142007-07-11 18:17:21 +03001427static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001429 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001430 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001431
Avi Kivity0cc50642007-03-25 12:07:27 +02001432 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001433 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001434 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001435 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001436
Avi Kivity82ca2d12010-10-21 12:20:34 +02001437#ifdef CONFIG_X86_64
1438 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1439#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001440 savesegment(fs, svm->host.fs);
1441 savesegment(gs, svm->host.gs);
1442 svm->host.ldt = kvm_read_ldt();
1443
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001444 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001445 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001446
Haozhong Zhangad7218832015-10-20 15:39:02 +08001447 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1448 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1449 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1450 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1451 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1452 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001453 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01001454 /* This assumes that the kernel never uses MSR_TSC_AUX */
1455 if (static_cpu_has(X86_FEATURE_RDTSCP))
1456 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457}
1458
1459static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1460{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001461 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001462 int i;
1463
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001464 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001465 kvm_load_ldt(svm->host.ldt);
1466#ifdef CONFIG_X86_64
1467 loadsegment(fs, svm->host.fs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001468 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001469 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001470#else
Avi Kivity831ca602011-03-08 16:09:51 +02001471#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001472 loadsegment(gs, svm->host.gs);
1473#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001474#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001475 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001476 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477}
1478
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1480{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001481 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482}
1483
1484static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1485{
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001486 /*
1487 * Any change of EFLAGS.VM is accompained by a reload of SS
1488 * (caused by either a task switch or an inter-privilege IRET),
1489 * so we do not need to update the CPL here.
1490 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001491 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492}
1493
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08001494static u32 svm_get_pkru(struct kvm_vcpu *vcpu)
1495{
1496 return 0;
1497}
1498
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001499static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1500{
1501 switch (reg) {
1502 case VCPU_EXREG_PDPTR:
1503 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001504 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001505 break;
1506 default:
1507 BUG();
1508 }
1509}
1510
Alexander Graff0b85052008-11-25 20:17:01 +01001511static void svm_set_vintr(struct vcpu_svm *svm)
1512{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001513 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001514}
1515
1516static void svm_clear_vintr(struct vcpu_svm *svm)
1517{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001518 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001519}
1520
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1522{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001523 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524
1525 switch (seg) {
1526 case VCPU_SREG_CS: return &save->cs;
1527 case VCPU_SREG_DS: return &save->ds;
1528 case VCPU_SREG_ES: return &save->es;
1529 case VCPU_SREG_FS: return &save->fs;
1530 case VCPU_SREG_GS: return &save->gs;
1531 case VCPU_SREG_SS: return &save->ss;
1532 case VCPU_SREG_TR: return &save->tr;
1533 case VCPU_SREG_LDTR: return &save->ldtr;
1534 }
1535 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001536 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537}
1538
1539static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1540{
1541 struct vmcb_seg *s = svm_seg(vcpu, seg);
1542
1543 return s->base;
1544}
1545
1546static void svm_get_segment(struct kvm_vcpu *vcpu,
1547 struct kvm_segment *var, int seg)
1548{
1549 struct vmcb_seg *s = svm_seg(vcpu, seg);
1550
1551 var->base = s->base;
1552 var->limit = s->limit;
1553 var->selector = s->selector;
1554 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1555 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1556 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1557 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1558 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1559 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1560 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301561
1562 /*
1563 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1564 * However, the SVM spec states that the G bit is not observed by the
1565 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1566 * So let's synthesize a legal G bit for all segments, this helps
1567 * running KVM nested. It also helps cross-vendor migration, because
1568 * Intel's vmentry has a check on the 'G' bit.
1569 */
1570 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001571
Joerg Roedele0231712010-02-24 18:59:10 +01001572 /*
1573 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001574 * for cross vendor migration purposes by "not present"
1575 */
1576 var->unusable = !var->present || (var->type == 0);
1577
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001578 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001579 case VCPU_SREG_TR:
1580 /*
1581 * Work around a bug where the busy flag in the tr selector
1582 * isn't exposed
1583 */
Amit Shahc0d09822008-10-27 09:04:18 +00001584 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001585 break;
1586 case VCPU_SREG_DS:
1587 case VCPU_SREG_ES:
1588 case VCPU_SREG_FS:
1589 case VCPU_SREG_GS:
1590 /*
1591 * The accessed bit must always be set in the segment
1592 * descriptor cache, although it can be cleared in the
1593 * descriptor, the cached bit always remains at 1. Since
1594 * Intel has a check on this, set it here to support
1595 * cross-vendor migration.
1596 */
1597 if (!var->unusable)
1598 var->type |= 0x1;
1599 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001600 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001601 /*
1602 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001603 * descriptor is left as 1, although the whole segment has
1604 * been made unusable. Clear it here to pass an Intel VMX
1605 * entry check when cross vendor migrating.
1606 */
1607 if (var->unusable)
1608 var->db = 0;
Jan Kiszka33b458d2014-06-29 17:12:43 +02001609 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02001610 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001611 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612}
1613
Izik Eidus2e4d2652008-03-24 19:38:34 +02001614static int svm_get_cpl(struct kvm_vcpu *vcpu)
1615{
1616 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1617
1618 return save->cpl;
1619}
1620
Gleb Natapov89a27f42010-02-16 10:51:48 +02001621static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001623 struct vcpu_svm *svm = to_svm(vcpu);
1624
Gleb Natapov89a27f42010-02-16 10:51:48 +02001625 dt->size = svm->vmcb->save.idtr.limit;
1626 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627}
1628
Gleb Natapov89a27f42010-02-16 10:51:48 +02001629static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001631 struct vcpu_svm *svm = to_svm(vcpu);
1632
Gleb Natapov89a27f42010-02-16 10:51:48 +02001633 svm->vmcb->save.idtr.limit = dt->size;
1634 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001635 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636}
1637
Gleb Natapov89a27f42010-02-16 10:51:48 +02001638static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001640 struct vcpu_svm *svm = to_svm(vcpu);
1641
Gleb Natapov89a27f42010-02-16 10:51:48 +02001642 dt->size = svm->vmcb->save.gdtr.limit;
1643 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644}
1645
Gleb Natapov89a27f42010-02-16 10:51:48 +02001646static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001648 struct vcpu_svm *svm = to_svm(vcpu);
1649
Gleb Natapov89a27f42010-02-16 10:51:48 +02001650 svm->vmcb->save.gdtr.limit = dt->size;
1651 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001652 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653}
1654
Avi Kivitye8467fd2009-12-29 18:43:06 +02001655static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1656{
1657}
1658
Avi Kivityaff48ba2010-12-05 18:56:11 +02001659static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1660{
1661}
1662
Anthony Liguori25c4c272007-04-27 09:29:21 +03001663static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001664{
1665}
1666
Avi Kivityd2251572010-01-06 10:55:27 +02001667static void update_cr0_intercept(struct vcpu_svm *svm)
1668{
1669 ulong gcr0 = svm->vcpu.arch.cr0;
1670 u64 *hcr0 = &svm->vmcb->save.cr0;
1671
1672 if (!svm->vcpu.fpu_active)
1673 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1674 else
1675 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1676 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1677
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001678 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001679
1680 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001681 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1682 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001683 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001684 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1685 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001686 }
1687}
1688
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1690{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001691 struct vcpu_svm *svm = to_svm(vcpu);
1692
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001693#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001694 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001695 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001696 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001697 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698 }
1699
Mike Dayd77c26f2007-10-08 09:02:08 -04001700 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001701 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001702 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703 }
1704 }
1705#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001706 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001707
1708 if (!npt_enabled)
1709 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001710
1711 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001712 cr0 |= X86_CR0_TS;
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02001713 /*
1714 * re-enable caching here because the QEMU bios
1715 * does not do it - this results in some delay at
1716 * reboot
1717 */
1718 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
1719 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001720 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001721 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001722 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723}
1724
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001725static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001727 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001728 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1729
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001730 if (cr4 & X86_CR4_VMXE)
1731 return 1;
1732
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001733 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001734 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001735
Joerg Roedelec077262008-04-09 14:15:28 +02001736 vcpu->arch.cr4 = cr4;
1737 if (!npt_enabled)
1738 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001739 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001740 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001741 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001742 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743}
1744
1745static void svm_set_segment(struct kvm_vcpu *vcpu,
1746 struct kvm_segment *var, int seg)
1747{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001748 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001749 struct vmcb_seg *s = svm_seg(vcpu, seg);
1750
1751 s->base = var->base;
1752 s->limit = var->limit;
1753 s->selector = var->selector;
1754 if (var->unusable)
1755 s->attrib = 0;
1756 else {
1757 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1758 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1759 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1760 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1761 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1762 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1763 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1764 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1765 }
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001766
1767 /*
1768 * This is always accurate, except if SYSRET returned to a segment
1769 * with SS.DPL != 3. Intel does not have this quirk, and always
1770 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1771 * would entail passing the CPL to userspace and back.
1772 */
1773 if (seg == VCPU_SREG_SS)
1774 svm->vmcb->save.cpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001775
Joerg Roedel060d0c92010-12-03 11:45:57 +01001776 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001777}
1778
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001779static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001781 struct vcpu_svm *svm = to_svm(vcpu);
1782
Joerg Roedel18c918c2010-11-30 18:03:59 +01001783 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001784
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001785 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001786 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001787 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001788 } else
1789 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001790}
1791
Tejun Heo0fe1e002009-10-29 22:34:14 +09001792static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001794 if (sd->next_asid > sd->max_asid) {
1795 ++sd->asid_generation;
1796 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001797 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798 }
1799
Tejun Heo0fe1e002009-10-29 22:34:14 +09001800 svm->asid_generation = sd->asid_generation;
1801 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001802
1803 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001804}
1805
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001806static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
1807{
1808 return to_svm(vcpu)->vmcb->save.dr6;
1809}
1810
1811static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
1812{
1813 struct vcpu_svm *svm = to_svm(vcpu);
1814
1815 svm->vmcb->save.dr6 = value;
1816 mark_dirty(svm->vmcb, VMCB_DR);
1817}
1818
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001819static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1820{
1821 struct vcpu_svm *svm = to_svm(vcpu);
1822
1823 get_debugreg(vcpu->arch.db[0], 0);
1824 get_debugreg(vcpu->arch.db[1], 1);
1825 get_debugreg(vcpu->arch.db[2], 2);
1826 get_debugreg(vcpu->arch.db[3], 3);
1827 vcpu->arch.dr6 = svm_get_dr6(vcpu);
1828 vcpu->arch.dr7 = svm->vmcb->save.dr7;
1829
1830 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1831 set_dr_intercepts(svm);
1832}
1833
Gleb Natapov020df072010-04-13 10:05:23 +03001834static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001836 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001837
Gleb Natapov020df072010-04-13 10:05:23 +03001838 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01001839 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840}
1841
Avi Kivity851ba692009-08-24 11:10:17 +03001842static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843{
Gleb Natapov631bc482010-10-14 11:22:52 +02001844 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001846 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001847
Gleb Natapov631bc482010-10-14 11:22:52 +02001848 switch (svm->apf_reason) {
1849 default:
1850 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001851
Gleb Natapov631bc482010-10-14 11:22:52 +02001852 trace_kvm_page_fault(fault_address, error_code);
1853 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1854 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01001855 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1856 svm->vmcb->control.insn_bytes,
1857 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02001858 break;
1859 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1860 svm->apf_reason = 0;
1861 local_irq_disable();
1862 kvm_async_pf_task_wait(fault_address);
1863 local_irq_enable();
1864 break;
1865 case KVM_PV_REASON_PAGE_READY:
1866 svm->apf_reason = 0;
1867 local_irq_disable();
1868 kvm_async_pf_task_wake(fault_address);
1869 local_irq_enable();
1870 break;
1871 }
1872 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873}
1874
Avi Kivity851ba692009-08-24 11:10:17 +03001875static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001876{
Avi Kivity851ba692009-08-24 11:10:17 +03001877 struct kvm_run *kvm_run = svm->vcpu.run;
1878
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001879 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001880 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001881 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001882 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1883 return 1;
1884 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001885
Jan Kiszka6be7d302009-10-18 13:24:54 +02001886 if (svm->nmi_singlestep) {
1887 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001888 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1889 svm->vmcb->save.rflags &=
1890 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Gleb Natapov44c11432009-05-11 13:35:52 +03001891 }
1892
1893 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001894 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001895 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1896 kvm_run->debug.arch.pc =
1897 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1898 kvm_run->debug.arch.exception = DB_VECTOR;
1899 return 0;
1900 }
1901
1902 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001903}
1904
Avi Kivity851ba692009-08-24 11:10:17 +03001905static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001906{
Avi Kivity851ba692009-08-24 11:10:17 +03001907 struct kvm_run *kvm_run = svm->vcpu.run;
1908
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001909 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1910 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1911 kvm_run->debug.arch.exception = BP_VECTOR;
1912 return 0;
1913}
1914
Avi Kivity851ba692009-08-24 11:10:17 +03001915static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001916{
1917 int er;
1918
Andre Przywara51d8b662010-12-21 11:12:02 +01001919 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001920 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001921 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001922 return 1;
1923}
1924
Eric Northup54a20552015-11-03 18:03:53 +01001925static int ac_interception(struct vcpu_svm *svm)
1926{
1927 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
1928 return 1;
1929}
1930
Avi Kivity6b52d182010-01-21 15:31:47 +02001931static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001932{
Avi Kivity6b52d182010-01-21 15:31:47 +02001933 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001934
Joerg Roedel18c918c2010-11-30 18:03:59 +01001935 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001936
Rusty Russelle756fc62007-07-30 20:07:08 +10001937 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001938 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001939}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001940
Avi Kivity6b52d182010-01-21 15:31:47 +02001941static int nm_interception(struct vcpu_svm *svm)
1942{
1943 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001944 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001945}
1946
Joerg Roedel67ec6602010-05-17 14:43:35 +02001947static bool is_erratum_383(void)
1948{
1949 int err, i;
1950 u64 value;
1951
1952 if (!erratum_383_found)
1953 return false;
1954
1955 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1956 if (err)
1957 return false;
1958
1959 /* Bit 62 may or may not be set for this mce */
1960 value &= ~(1ULL << 62);
1961
1962 if (value != 0xb600000000010015ULL)
1963 return false;
1964
1965 /* Clear MCi_STATUS registers */
1966 for (i = 0; i < 6; ++i)
1967 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1968
1969 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1970 if (!err) {
1971 u32 low, high;
1972
1973 value &= ~(1ULL << 2);
1974 low = lower_32_bits(value);
1975 high = upper_32_bits(value);
1976
1977 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1978 }
1979
1980 /* Flush tlb to evict multi-match entries */
1981 __flush_tlb_all();
1982
1983 return true;
1984}
1985
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001986static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001987{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001988 if (is_erratum_383()) {
1989 /*
1990 * Erratum 383 triggered. Guest state is corrupt so kill the
1991 * guest.
1992 */
1993 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1994
Avi Kivitya8eeb042010-05-10 12:34:53 +03001995 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001996
1997 return;
1998 }
1999
Joerg Roedel53371b52008-04-09 14:15:30 +02002000 /*
2001 * On an #MC intercept the MCE handler is not called automatically in
2002 * the host. So do it by hand here.
2003 */
2004 asm volatile (
2005 "int $0x12\n");
2006 /* not sure if we ever come back to this point */
2007
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002008 return;
2009}
2010
2011static int mc_interception(struct vcpu_svm *svm)
2012{
Joerg Roedel53371b52008-04-09 14:15:30 +02002013 return 1;
2014}
2015
Avi Kivity851ba692009-08-24 11:10:17 +03002016static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002017{
Avi Kivity851ba692009-08-24 11:10:17 +03002018 struct kvm_run *kvm_run = svm->vcpu.run;
2019
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002020 /*
2021 * VMCB is undefined after a SHUTDOWN intercept
2022 * so reinitialize it.
2023 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002024 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002025 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002026
2027 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2028 return 0;
2029}
2030
Avi Kivity851ba692009-08-24 11:10:17 +03002031static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002033 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002034 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01002035 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002036 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037
Rusty Russelle756fc62007-07-30 20:07:08 +10002038 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002039 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002040 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002041 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01002042 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002043
Avi Kivity039576c2007-03-20 12:46:50 +02002044 port = io_info >> 16;
2045 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002046 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002047 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002048
2049 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002050}
2051
Avi Kivity851ba692009-08-24 11:10:17 +03002052static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002053{
2054 return 1;
2055}
2056
Avi Kivity851ba692009-08-24 11:10:17 +03002057static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002058{
2059 ++svm->vcpu.stat.irq_exits;
2060 return 1;
2061}
2062
Avi Kivity851ba692009-08-24 11:10:17 +03002063static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064{
2065 return 1;
2066}
2067
Avi Kivity851ba692009-08-24 11:10:17 +03002068static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002069{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002070 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002071 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002072}
2073
Avi Kivity851ba692009-08-24 11:10:17 +03002074static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002075{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002076 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002077 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002078}
2079
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002080static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2081{
2082 struct vcpu_svm *svm = to_svm(vcpu);
2083
2084 return svm->nested.nested_cr3;
2085}
2086
Avi Kivitye4e517b2011-07-28 11:36:17 +03002087static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2088{
2089 struct vcpu_svm *svm = to_svm(vcpu);
2090 u64 cr3 = svm->nested.nested_cr3;
2091 u64 pdpte;
2092 int ret;
2093
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002094 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
2095 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002096 if (ret)
2097 return 0;
2098 return pdpte;
2099}
2100
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002101static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2102 unsigned long root)
2103{
2104 struct vcpu_svm *svm = to_svm(vcpu);
2105
2106 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01002107 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002108 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002109}
2110
Avi Kivity6389ee92010-11-29 16:12:30 +02002111static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2112 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002113{
2114 struct vcpu_svm *svm = to_svm(vcpu);
2115
Paolo Bonzini5e352512014-09-02 13:18:37 +02002116 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2117 /*
2118 * TODO: track the cause of the nested page fault, and
2119 * correctly fill in the high bits of exit_info_1.
2120 */
2121 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2122 svm->vmcb->control.exit_code_hi = 0;
2123 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2124 svm->vmcb->control.exit_info_2 = fault->address;
2125 }
2126
2127 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2128 svm->vmcb->control.exit_info_1 |= fault->error_code;
2129
2130 /*
2131 * The present bit is always zero for page structure faults on real
2132 * hardware.
2133 */
2134 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2135 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002136
2137 nested_svm_vmexit(svm);
2138}
2139
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002140static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002141{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002142 WARN_ON(mmu_is_nested(vcpu));
2143 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002144 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2145 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002146 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002147 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
2148 vcpu->arch.mmu.shadow_root_level = get_npt_level();
Xiao Guangrongc258b622015-08-05 12:04:24 +08002149 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002150 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002151}
2152
2153static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2154{
2155 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2156}
2157
Alexander Grafc0725422008-11-25 20:17:03 +01002158static int nested_svm_check_permissions(struct vcpu_svm *svm)
2159{
Avi Kivityf6801df2010-01-21 15:31:50 +02002160 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01002161 || !is_paging(&svm->vcpu)) {
2162 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2163 return 1;
2164 }
2165
2166 if (svm->vmcb->save.cpl) {
2167 kvm_inject_gp(&svm->vcpu, 0);
2168 return 1;
2169 }
2170
2171 return 0;
2172}
2173
Alexander Grafcf74a782008-11-25 20:17:08 +01002174static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2175 bool has_error_code, u32 error_code)
2176{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002177 int vmexit;
2178
Joerg Roedel20307532010-11-29 17:51:48 +01002179 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002180 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002181
Joerg Roedel0295ad72009-08-07 11:49:37 +02002182 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2183 svm->vmcb->control.exit_code_hi = 0;
2184 svm->vmcb->control.exit_info_1 = error_code;
2185 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
2186
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002187 vmexit = nested_svm_intercept(svm);
2188 if (vmexit == NESTED_EXIT_DONE)
2189 svm->nested.exit_required = true;
2190
2191 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002192}
2193
Joerg Roedel8fe54652010-02-19 16:23:01 +01002194/* This function returns true if it is save to enable the irq window */
2195static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002196{
Joerg Roedel20307532010-11-29 17:51:48 +01002197 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002198 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002199
Joerg Roedel26666952009-08-07 11:49:46 +02002200 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002201 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002202
Joerg Roedel26666952009-08-07 11:49:46 +02002203 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002204 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002205
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002206 /*
2207 * if vmexit was already requested (by intercepted exception
2208 * for instance) do not overwrite it with "external interrupt"
2209 * vmexit.
2210 */
2211 if (svm->nested.exit_required)
2212 return false;
2213
Joerg Roedel197717d2010-02-24 18:59:19 +01002214 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2215 svm->vmcb->control.exit_info_1 = 0;
2216 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002217
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002218 if (svm->nested.intercept & 1ULL) {
2219 /*
2220 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002221 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002222 * #vmexit emulation might sleep. Only signal request for
2223 * the #vmexit here.
2224 */
2225 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002226 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002227 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002228 }
2229
Joerg Roedel8fe54652010-02-19 16:23:01 +01002230 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002231}
2232
Joerg Roedel887f5002010-02-24 18:59:12 +01002233/* This function returns true if it is save to enable the nmi window */
2234static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2235{
Joerg Roedel20307532010-11-29 17:51:48 +01002236 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002237 return true;
2238
2239 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2240 return true;
2241
2242 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2243 svm->nested.exit_required = true;
2244
2245 return false;
2246}
2247
Joerg Roedel7597f122010-02-19 16:23:00 +01002248static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002249{
2250 struct page *page;
2251
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002252 might_sleep();
2253
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002254 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002255 if (is_error_page(page))
2256 goto error;
2257
Joerg Roedel7597f122010-02-19 16:23:00 +01002258 *_page = page;
2259
2260 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002261
2262error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002263 kvm_inject_gp(&svm->vcpu, 0);
2264
2265 return NULL;
2266}
2267
Joerg Roedel7597f122010-02-19 16:23:00 +01002268static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002269{
Joerg Roedel7597f122010-02-19 16:23:00 +01002270 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002271 kvm_release_page_dirty(page);
2272}
2273
Joerg Roedelce2ac082010-03-01 15:34:39 +01002274static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002275{
Jan Kiszka9bf41832014-06-30 10:54:17 +02002276 unsigned port, size, iopm_len;
2277 u16 val, mask;
2278 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002279 u64 gpa;
2280
2281 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2282 return NESTED_EXIT_HOST;
2283
2284 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02002285 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2286 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002287 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02002288 start_bit = port % 8;
2289 iopm_len = (start_bit + size > 8) ? 2 : 1;
2290 mask = (0xf >> (4 - size)) << start_bit;
2291 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002292
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002293 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02002294 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002295
Jan Kiszka9bf41832014-06-30 10:54:17 +02002296 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002297}
2298
Joerg Roedeld2477822010-03-01 15:34:34 +01002299static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002300{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002301 u32 offset, msr, value;
2302 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002303
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002304 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002305 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002306
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002307 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2308 offset = svm_msrpm_offset(msr);
2309 write = svm->vmcb->control.exit_info_1 & 1;
2310 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002311
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002312 if (offset == MSR_INVALID)
2313 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002314
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002315 /* Offset is in 32 bit units but need in 8 bit units */
2316 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002317
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002318 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002319 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002320
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002321 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002322}
2323
Joerg Roedel410e4d52009-08-07 11:49:44 +02002324static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002325{
Alexander Grafcf74a782008-11-25 20:17:08 +01002326 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002327
Joerg Roedel410e4d52009-08-07 11:49:44 +02002328 switch (exit_code) {
2329 case SVM_EXIT_INTR:
2330 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002331 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002332 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002333 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002334 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002335 if (npt_enabled)
2336 return NESTED_EXIT_HOST;
2337 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002338 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002339 /* When we're shadowing, trap PFs, but not async PF */
2340 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002341 return NESTED_EXIT_HOST;
2342 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01002343 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
2344 nm_interception(svm);
2345 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002346 default:
2347 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002348 }
2349
Joerg Roedel410e4d52009-08-07 11:49:44 +02002350 return NESTED_EXIT_CONTINUE;
2351}
2352
2353/*
2354 * If this function returns true, this #vmexit was already handled
2355 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002356static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002357{
2358 u32 exit_code = svm->vmcb->control.exit_code;
2359 int vmexit = NESTED_EXIT_HOST;
2360
Alexander Grafcf74a782008-11-25 20:17:08 +01002361 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002362 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002363 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002364 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002365 case SVM_EXIT_IOIO:
2366 vmexit = nested_svm_intercept_ioio(svm);
2367 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002368 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2369 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2370 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002371 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002372 break;
2373 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002374 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2375 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2376 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002377 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002378 break;
2379 }
2380 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2381 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002382 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002383 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002384 /* async page fault always cause vmexit */
2385 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2386 svm->apf_reason != 0)
2387 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002388 break;
2389 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002390 case SVM_EXIT_ERR: {
2391 vmexit = NESTED_EXIT_DONE;
2392 break;
2393 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002394 default: {
2395 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002396 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002397 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002398 }
2399 }
2400
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002401 return vmexit;
2402}
2403
2404static int nested_svm_exit_handled(struct vcpu_svm *svm)
2405{
2406 int vmexit;
2407
2408 vmexit = nested_svm_intercept(svm);
2409
2410 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002411 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002412
2413 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002414}
2415
Joerg Roedel0460a972009-08-07 11:49:31 +02002416static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2417{
2418 struct vmcb_control_area *dst = &dst_vmcb->control;
2419 struct vmcb_control_area *from = &from_vmcb->control;
2420
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002421 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002422 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002423 dst->intercept_exceptions = from->intercept_exceptions;
2424 dst->intercept = from->intercept;
2425 dst->iopm_base_pa = from->iopm_base_pa;
2426 dst->msrpm_base_pa = from->msrpm_base_pa;
2427 dst->tsc_offset = from->tsc_offset;
2428 dst->asid = from->asid;
2429 dst->tlb_ctl = from->tlb_ctl;
2430 dst->int_ctl = from->int_ctl;
2431 dst->int_vector = from->int_vector;
2432 dst->int_state = from->int_state;
2433 dst->exit_code = from->exit_code;
2434 dst->exit_code_hi = from->exit_code_hi;
2435 dst->exit_info_1 = from->exit_info_1;
2436 dst->exit_info_2 = from->exit_info_2;
2437 dst->exit_int_info = from->exit_int_info;
2438 dst->exit_int_info_err = from->exit_int_info_err;
2439 dst->nested_ctl = from->nested_ctl;
2440 dst->event_inj = from->event_inj;
2441 dst->event_inj_err = from->event_inj_err;
2442 dst->nested_cr3 = from->nested_cr3;
2443 dst->lbr_ctl = from->lbr_ctl;
2444}
2445
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002446static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002447{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002448 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002449 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002450 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002451 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002452
Joerg Roedel17897f32009-10-09 16:08:29 +02002453 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2454 vmcb->control.exit_info_1,
2455 vmcb->control.exit_info_2,
2456 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002457 vmcb->control.exit_int_info_err,
2458 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002459
Joerg Roedel7597f122010-02-19 16:23:00 +01002460 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002461 if (!nested_vmcb)
2462 return 1;
2463
Joerg Roedel20307532010-11-29 17:51:48 +01002464 /* Exit Guest-Mode */
2465 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002466 svm->nested.vmcb = 0;
2467
Alexander Grafcf74a782008-11-25 20:17:08 +01002468 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002469 disable_gif(svm);
2470
2471 nested_vmcb->save.es = vmcb->save.es;
2472 nested_vmcb->save.cs = vmcb->save.cs;
2473 nested_vmcb->save.ss = vmcb->save.ss;
2474 nested_vmcb->save.ds = vmcb->save.ds;
2475 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2476 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002477 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002478 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002479 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002480 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002481 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002482 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002483 nested_vmcb->save.rip = vmcb->save.rip;
2484 nested_vmcb->save.rsp = vmcb->save.rsp;
2485 nested_vmcb->save.rax = vmcb->save.rax;
2486 nested_vmcb->save.dr7 = vmcb->save.dr7;
2487 nested_vmcb->save.dr6 = vmcb->save.dr6;
2488 nested_vmcb->save.cpl = vmcb->save.cpl;
2489
2490 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2491 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2492 nested_vmcb->control.int_state = vmcb->control.int_state;
2493 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2494 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2495 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2496 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2497 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2498 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02002499
2500 if (svm->nrips_enabled)
2501 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002502
2503 /*
2504 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2505 * to make sure that we do not lose injected events. So check event_inj
2506 * here and copy it to exit_int_info if it is valid.
2507 * Exit_int_info and event_inj can't be both valid because the case
2508 * below only happens on a VMRUN instruction intercept which has
2509 * no valid exit_int_info set.
2510 */
2511 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2512 struct vmcb_control_area *nc = &nested_vmcb->control;
2513
2514 nc->exit_int_info = vmcb->control.event_inj;
2515 nc->exit_int_info_err = vmcb->control.event_inj_err;
2516 }
2517
Joerg Roedel33740e42009-08-07 11:49:29 +02002518 nested_vmcb->control.tlb_ctl = 0;
2519 nested_vmcb->control.event_inj = 0;
2520 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002521
2522 /* We always set V_INTR_MASKING and remember the old value in hflags */
2523 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2524 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2525
Alexander Grafcf74a782008-11-25 20:17:08 +01002526 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002527 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002528
Alexander Graf219b65d2009-06-15 15:21:25 +02002529 kvm_clear_exception_queue(&svm->vcpu);
2530 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002531
Joerg Roedel4b161842010-09-10 17:31:03 +02002532 svm->nested.nested_cr3 = 0;
2533
Alexander Grafcf74a782008-11-25 20:17:08 +01002534 /* Restore selected save entries */
2535 svm->vmcb->save.es = hsave->save.es;
2536 svm->vmcb->save.cs = hsave->save.cs;
2537 svm->vmcb->save.ss = hsave->save.ss;
2538 svm->vmcb->save.ds = hsave->save.ds;
2539 svm->vmcb->save.gdtr = hsave->save.gdtr;
2540 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002541 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002542 svm_set_efer(&svm->vcpu, hsave->save.efer);
2543 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2544 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2545 if (npt_enabled) {
2546 svm->vmcb->save.cr3 = hsave->save.cr3;
2547 svm->vcpu.arch.cr3 = hsave->save.cr3;
2548 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002549 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002550 }
2551 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2552 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2553 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2554 svm->vmcb->save.dr7 = 0;
2555 svm->vmcb->save.cpl = 0;
2556 svm->vmcb->control.exit_int_info = 0;
2557
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002558 mark_all_dirty(svm->vmcb);
2559
Joerg Roedel7597f122010-02-19 16:23:00 +01002560 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002561
Joerg Roedel4b161842010-09-10 17:31:03 +02002562 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002563 kvm_mmu_reset_context(&svm->vcpu);
2564 kvm_mmu_load(&svm->vcpu);
2565
2566 return 0;
2567}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002568
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002569static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002570{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002571 /*
2572 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002573 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01002574 * the kvm msr permission bitmap may contain zero bits
2575 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002576 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002577
Joerg Roedel323c3d82010-03-01 15:34:37 +01002578 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2579 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002580
Joerg Roedel323c3d82010-03-01 15:34:37 +01002581 for (i = 0; i < MSRPM_OFFSETS; i++) {
2582 u32 value, p;
2583 u64 offset;
2584
2585 if (msrpm_offsets[i] == 0xffffffff)
2586 break;
2587
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002588 p = msrpm_offsets[i];
2589 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002590
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002591 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01002592 return false;
2593
2594 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2595 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002596
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002597 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002598
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002599 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002600}
2601
Joerg Roedel52c65a302010-08-02 16:46:44 +02002602static bool nested_vmcb_checks(struct vmcb *vmcb)
2603{
2604 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2605 return false;
2606
Joerg Roedeldbe77582010-08-02 16:46:45 +02002607 if (vmcb->control.asid == 0)
2608 return false;
2609
Joerg Roedel4b161842010-09-10 17:31:03 +02002610 if (vmcb->control.nested_ctl && !npt_enabled)
2611 return false;
2612
Joerg Roedel52c65a302010-08-02 16:46:44 +02002613 return true;
2614}
2615
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002616static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002617{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002618 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002619 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002620 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002621 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002622 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002623
Joerg Roedel06fc77722010-02-19 16:23:07 +01002624 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002625
Joerg Roedel7597f122010-02-19 16:23:00 +01002626 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002627 if (!nested_vmcb)
2628 return false;
2629
Joerg Roedel52c65a302010-08-02 16:46:44 +02002630 if (!nested_vmcb_checks(nested_vmcb)) {
2631 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2632 nested_vmcb->control.exit_code_hi = 0;
2633 nested_vmcb->control.exit_info_1 = 0;
2634 nested_vmcb->control.exit_info_2 = 0;
2635
2636 nested_svm_unmap(page);
2637
2638 return false;
2639 }
2640
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002641 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002642 nested_vmcb->save.rip,
2643 nested_vmcb->control.int_ctl,
2644 nested_vmcb->control.event_inj,
2645 nested_vmcb->control.nested_ctl);
2646
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002647 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2648 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002649 nested_vmcb->control.intercept_exceptions,
2650 nested_vmcb->control.intercept);
2651
Alexander Graf3d6368e2008-11-25 20:17:07 +01002652 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002653 kvm_clear_exception_queue(&svm->vcpu);
2654 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002655
Joerg Roedele0231712010-02-24 18:59:10 +01002656 /*
2657 * Save the old vmcb, so we don't need to pick what we save, but can
2658 * restore everything when a VMEXIT occurs
2659 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002660 hsave->save.es = vmcb->save.es;
2661 hsave->save.cs = vmcb->save.cs;
2662 hsave->save.ss = vmcb->save.ss;
2663 hsave->save.ds = vmcb->save.ds;
2664 hsave->save.gdtr = vmcb->save.gdtr;
2665 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002666 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002667 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002668 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002669 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002670 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002671 hsave->save.rsp = vmcb->save.rsp;
2672 hsave->save.rax = vmcb->save.rax;
2673 if (npt_enabled)
2674 hsave->save.cr3 = vmcb->save.cr3;
2675 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002676 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002677
Joerg Roedel0460a972009-08-07 11:49:31 +02002678 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002679
Avi Kivityf6e78472010-08-02 15:30:20 +03002680 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002681 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2682 else
2683 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2684
Joerg Roedel4b161842010-09-10 17:31:03 +02002685 if (nested_vmcb->control.nested_ctl) {
2686 kvm_mmu_unload(&svm->vcpu);
2687 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2688 nested_svm_init_mmu_context(&svm->vcpu);
2689 }
2690
Alexander Graf3d6368e2008-11-25 20:17:07 +01002691 /* Load the nested guest state */
2692 svm->vmcb->save.es = nested_vmcb->save.es;
2693 svm->vmcb->save.cs = nested_vmcb->save.cs;
2694 svm->vmcb->save.ss = nested_vmcb->save.ss;
2695 svm->vmcb->save.ds = nested_vmcb->save.ds;
2696 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2697 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002698 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002699 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2700 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2701 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2702 if (npt_enabled) {
2703 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2704 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002705 } else
Avi Kivity23902182010-06-10 17:02:16 +03002706 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002707
2708 /* Guest paging mode is active - reset mmu */
2709 kvm_mmu_reset_context(&svm->vcpu);
2710
Joerg Roedeldefbba52009-08-07 11:49:30 +02002711 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002712 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2713 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2714 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002715
Alexander Graf3d6368e2008-11-25 20:17:07 +01002716 /* In case we don't even reach vcpu_run, the fields are not updated */
2717 svm->vmcb->save.rax = nested_vmcb->save.rax;
2718 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2719 svm->vmcb->save.rip = nested_vmcb->save.rip;
2720 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2721 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2722 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2723
Joerg Roedelf7138532010-03-01 15:34:40 +01002724 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002725 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002726
Joerg Roedelaad42c62009-08-07 11:49:34 +02002727 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002728 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002729 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002730 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2731 svm->nested.intercept = nested_vmcb->control.intercept;
2732
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002733 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002734 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002735 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2736 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2737 else
2738 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2739
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002740 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2741 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002742 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2743 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002744 }
2745
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002746 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002747 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002748
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002749 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002750 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2751 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2752 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002753 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2754 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2755
Joerg Roedel7597f122010-02-19 16:23:00 +01002756 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002757
Joerg Roedel20307532010-11-29 17:51:48 +01002758 /* Enter Guest-Mode */
2759 enter_guest_mode(&svm->vcpu);
2760
Joerg Roedel384c6362010-11-30 18:03:56 +01002761 /*
2762 * Merge guest and host intercepts - must be called with vcpu in
2763 * guest-mode to take affect here
2764 */
2765 recalc_intercepts(svm);
2766
Joerg Roedel06fc77722010-02-19 16:23:07 +01002767 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002768
Joerg Roedel2af91942009-08-07 11:49:28 +02002769 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002770
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002771 mark_all_dirty(svm->vmcb);
2772
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002773 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002774}
2775
Joerg Roedel9966bf62009-08-07 11:49:40 +02002776static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002777{
2778 to_vmcb->save.fs = from_vmcb->save.fs;
2779 to_vmcb->save.gs = from_vmcb->save.gs;
2780 to_vmcb->save.tr = from_vmcb->save.tr;
2781 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2782 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2783 to_vmcb->save.star = from_vmcb->save.star;
2784 to_vmcb->save.lstar = from_vmcb->save.lstar;
2785 to_vmcb->save.cstar = from_vmcb->save.cstar;
2786 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2787 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2788 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2789 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002790}
2791
Avi Kivity851ba692009-08-24 11:10:17 +03002792static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002793{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002794 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002795 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002796
Alexander Graf55426752008-11-25 20:17:06 +01002797 if (nested_svm_check_permissions(svm))
2798 return 1;
2799
Joerg Roedel7597f122010-02-19 16:23:00 +01002800 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002801 if (!nested_vmcb)
2802 return 1;
2803
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002804 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2805 skip_emulated_instruction(&svm->vcpu);
2806
Joerg Roedel9966bf62009-08-07 11:49:40 +02002807 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002808 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002809
2810 return 1;
2811}
2812
Avi Kivity851ba692009-08-24 11:10:17 +03002813static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002814{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002815 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002816 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002817
Alexander Graf55426752008-11-25 20:17:06 +01002818 if (nested_svm_check_permissions(svm))
2819 return 1;
2820
Joerg Roedel7597f122010-02-19 16:23:00 +01002821 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002822 if (!nested_vmcb)
2823 return 1;
2824
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002825 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2826 skip_emulated_instruction(&svm->vcpu);
2827
Joerg Roedel9966bf62009-08-07 11:49:40 +02002828 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002829 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002830
2831 return 1;
2832}
2833
Avi Kivity851ba692009-08-24 11:10:17 +03002834static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002835{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002836 if (nested_svm_check_permissions(svm))
2837 return 1;
2838
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002839 /* Save rip after vmrun instruction */
2840 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002841
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002842 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002843 return 1;
2844
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002845 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002846 goto failed;
2847
2848 return 1;
2849
2850failed:
2851
2852 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2853 svm->vmcb->control.exit_code_hi = 0;
2854 svm->vmcb->control.exit_info_1 = 0;
2855 svm->vmcb->control.exit_info_2 = 0;
2856
2857 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002858
2859 return 1;
2860}
2861
Avi Kivity851ba692009-08-24 11:10:17 +03002862static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002863{
2864 if (nested_svm_check_permissions(svm))
2865 return 1;
2866
2867 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2868 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002869 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002870
Joerg Roedel2af91942009-08-07 11:49:28 +02002871 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002872
2873 return 1;
2874}
2875
Avi Kivity851ba692009-08-24 11:10:17 +03002876static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002877{
2878 if (nested_svm_check_permissions(svm))
2879 return 1;
2880
2881 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2882 skip_emulated_instruction(&svm->vcpu);
2883
Joerg Roedel2af91942009-08-07 11:49:28 +02002884 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002885
2886 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05002887 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
2888 svm_clear_vintr(svm);
2889 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2890 mark_dirty(svm->vmcb, VMCB_INTR);
2891 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002892
Alexander Graf1371d902008-11-25 20:17:04 +01002893 return 1;
2894}
2895
Avi Kivity851ba692009-08-24 11:10:17 +03002896static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002897{
2898 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002899
David Kaplan668f1982015-02-20 16:02:10 -06002900 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
2901 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02002902
Alexander Grafff092382009-06-15 15:21:24 +02002903 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06002904 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02002905
2906 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2907 skip_emulated_instruction(&svm->vcpu);
2908 return 1;
2909}
2910
Joerg Roedel532a46b2009-10-09 16:08:32 +02002911static int skinit_interception(struct vcpu_svm *svm)
2912{
David Kaplan668f1982015-02-20 16:02:10 -06002913 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02002914
2915 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2916 return 1;
2917}
2918
David Kaplandab429a2015-03-02 13:43:37 -06002919static int wbinvd_interception(struct vcpu_svm *svm)
2920{
2921 kvm_emulate_wbinvd(&svm->vcpu);
2922 return 1;
2923}
2924
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002925static int xsetbv_interception(struct vcpu_svm *svm)
2926{
2927 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
2928 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
2929
2930 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
2931 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2932 skip_emulated_instruction(&svm->vcpu);
2933 }
2934
2935 return 1;
2936}
2937
Avi Kivity851ba692009-08-24 11:10:17 +03002938static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002939{
Izik Eidus37817f22008-03-24 23:14:53 +02002940 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002941 int reason;
2942 int int_type = svm->vmcb->control.exit_int_info &
2943 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002944 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002945 uint32_t type =
2946 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2947 uint32_t idt_v =
2948 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002949 bool has_error_code = false;
2950 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002951
2952 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002953
Izik Eidus37817f22008-03-24 23:14:53 +02002954 if (svm->vmcb->control.exit_info_2 &
2955 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002956 reason = TASK_SWITCH_IRET;
2957 else if (svm->vmcb->control.exit_info_2 &
2958 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2959 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002960 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002961 reason = TASK_SWITCH_GATE;
2962 else
2963 reason = TASK_SWITCH_CALL;
2964
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002965 if (reason == TASK_SWITCH_GATE) {
2966 switch (type) {
2967 case SVM_EXITINTINFO_TYPE_NMI:
2968 svm->vcpu.arch.nmi_injected = false;
2969 break;
2970 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002971 if (svm->vmcb->control.exit_info_2 &
2972 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2973 has_error_code = true;
2974 error_code =
2975 (u32)svm->vmcb->control.exit_info_2;
2976 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002977 kvm_clear_exception_queue(&svm->vcpu);
2978 break;
2979 case SVM_EXITINTINFO_TYPE_INTR:
2980 kvm_clear_interrupt_queue(&svm->vcpu);
2981 break;
2982 default:
2983 break;
2984 }
2985 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002986
Gleb Natapov8317c292009-04-12 13:37:02 +03002987 if (reason != TASK_SWITCH_GATE ||
2988 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2989 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002990 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2991 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002992
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002993 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2994 int_vec = -1;
2995
2996 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03002997 has_error_code, error_code) == EMULATE_FAIL) {
2998 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2999 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3000 svm->vcpu.run->internal.ndata = 0;
3001 return 0;
3002 }
3003 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003004}
3005
Avi Kivity851ba692009-08-24 11:10:17 +03003006static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003008 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003009 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02003010 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003011}
3012
Avi Kivity851ba692009-08-24 11:10:17 +03003013static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003014{
3015 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003016 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003017 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003018 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003019 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003020 return 1;
3021}
3022
Avi Kivity851ba692009-08-24 11:10:17 +03003023static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003024{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003025 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3026 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3027
3028 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
3029 skip_emulated_instruction(&svm->vcpu);
3030 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003031}
3032
Avi Kivity851ba692009-08-24 11:10:17 +03003033static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003034{
Andre Przywara51d8b662010-12-21 11:12:02 +01003035 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003036}
3037
Avi Kivity332b56e2011-11-10 14:57:24 +02003038static int rdpmc_interception(struct vcpu_svm *svm)
3039{
3040 int err;
3041
3042 if (!static_cpu_has(X86_FEATURE_NRIPS))
3043 return emulate_on_interception(svm);
3044
3045 err = kvm_rdpmc(&svm->vcpu);
3046 kvm_complete_insn_gp(&svm->vcpu, err);
3047
3048 return 1;
3049}
3050
Xiubo Li52eb5a62015-03-13 17:39:45 +08003051static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3052 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003053{
3054 unsigned long cr0 = svm->vcpu.arch.cr0;
3055 bool ret = false;
3056 u64 intercept;
3057
3058 intercept = svm->nested.intercept;
3059
3060 if (!is_guest_mode(&svm->vcpu) ||
3061 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3062 return false;
3063
3064 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3065 val &= ~SVM_CR0_SELECTIVE_MASK;
3066
3067 if (cr0 ^ val) {
3068 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3069 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3070 }
3071
3072 return ret;
3073}
3074
Andre Przywara7ff76d52010-12-21 11:12:04 +01003075#define CR_VALID (1ULL << 63)
3076
3077static int cr_interception(struct vcpu_svm *svm)
3078{
3079 int reg, cr;
3080 unsigned long val;
3081 int err;
3082
3083 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3084 return emulate_on_interception(svm);
3085
3086 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3087 return emulate_on_interception(svm);
3088
3089 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003090 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3091 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3092 else
3093 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003094
3095 err = 0;
3096 if (cr >= 16) { /* mov to cr */
3097 cr -= 16;
3098 val = kvm_register_read(&svm->vcpu, reg);
3099 switch (cr) {
3100 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003101 if (!check_selective_cr0_intercepted(svm, val))
3102 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003103 else
3104 return 1;
3105
Andre Przywara7ff76d52010-12-21 11:12:04 +01003106 break;
3107 case 3:
3108 err = kvm_set_cr3(&svm->vcpu, val);
3109 break;
3110 case 4:
3111 err = kvm_set_cr4(&svm->vcpu, val);
3112 break;
3113 case 8:
3114 err = kvm_set_cr8(&svm->vcpu, val);
3115 break;
3116 default:
3117 WARN(1, "unhandled write to CR%d", cr);
3118 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3119 return 1;
3120 }
3121 } else { /* mov from cr */
3122 switch (cr) {
3123 case 0:
3124 val = kvm_read_cr0(&svm->vcpu);
3125 break;
3126 case 2:
3127 val = svm->vcpu.arch.cr2;
3128 break;
3129 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003130 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003131 break;
3132 case 4:
3133 val = kvm_read_cr4(&svm->vcpu);
3134 break;
3135 case 8:
3136 val = kvm_get_cr8(&svm->vcpu);
3137 break;
3138 default:
3139 WARN(1, "unhandled read from CR%d", cr);
3140 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3141 return 1;
3142 }
3143 kvm_register_write(&svm->vcpu, reg, val);
3144 }
3145 kvm_complete_insn_gp(&svm->vcpu, err);
3146
3147 return 1;
3148}
3149
Andre Przywaracae37972010-12-21 11:12:05 +01003150static int dr_interception(struct vcpu_svm *svm)
3151{
3152 int reg, dr;
3153 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003154
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003155 if (svm->vcpu.guest_debug == 0) {
3156 /*
3157 * No more DR vmexits; force a reload of the debug registers
3158 * and reenter on this instruction. The next vmexit will
3159 * retrieve the full state of the debug registers.
3160 */
3161 clr_dr_intercepts(svm);
3162 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3163 return 1;
3164 }
3165
Andre Przywaracae37972010-12-21 11:12:05 +01003166 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3167 return emulate_on_interception(svm);
3168
3169 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3170 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3171
3172 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003173 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3174 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003175 val = kvm_register_read(&svm->vcpu, reg);
3176 kvm_set_dr(&svm->vcpu, dr - 16, val);
3177 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003178 if (!kvm_require_dr(&svm->vcpu, dr))
3179 return 1;
3180 kvm_get_dr(&svm->vcpu, dr, &val);
3181 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003182 }
3183
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01003184 skip_emulated_instruction(&svm->vcpu);
3185
Andre Przywaracae37972010-12-21 11:12:05 +01003186 return 1;
3187}
3188
Avi Kivity851ba692009-08-24 11:10:17 +03003189static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003190{
Avi Kivity851ba692009-08-24 11:10:17 +03003191 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003192 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003193
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003194 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3195 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003196 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003197 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003198 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003199 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003200 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003201 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3202 return 0;
3203}
3204
Paolo Bonzini48d89b92014-08-26 13:27:46 +02003205static u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003206{
3207 struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
Haozhong Zhang4ba76532015-10-20 15:39:07 +08003208 return vmcb->control.tsc_offset + host_tsc;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003209}
3210
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003211static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003212{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003213 struct vcpu_svm *svm = to_svm(vcpu);
3214
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003215 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303216 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003217 msr_info->data = svm->vmcb->control.tsc_offset +
Haozhong Zhang35181e82015-10-20 15:39:03 +08003218 kvm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003219
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 break;
3221 }
Brian Gerst8c065852010-07-17 09:03:26 -04003222 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003223 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003225#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003227 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228 break;
3229 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003230 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231 break;
3232 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003233 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234 break;
3235 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003236 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237 break;
3238#endif
3239 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003240 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241 break;
3242 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003243 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244 break;
3245 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003246 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003247 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003248 case MSR_TSC_AUX:
3249 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3250 return 1;
3251 msr_info->data = svm->tsc_aux;
3252 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003253 /*
3254 * Nobody will change the following 5 values in the VMCB so we can
3255 * safely return them on rdmsr. They will always be 0 until LBRV is
3256 * implemented.
3257 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003258 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003259 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01003260 break;
3261 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003262 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003263 break;
3264 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003265 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003266 break;
3267 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003268 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003269 break;
3270 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003271 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003272 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003273 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003274 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003275 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003276 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003277 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003278 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003279 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003280 msr_info->data = 0x01000065;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003281 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01003282 case MSR_F15H_IC_CFG: {
3283
3284 int family, model;
3285
3286 family = guest_cpuid_family(vcpu);
3287 model = guest_cpuid_model(vcpu);
3288
3289 if (family < 0 || model < 0)
3290 return kvm_get_msr_common(vcpu, msr_info);
3291
3292 msr_info->data = 0;
3293
3294 if (family == 0x15 &&
3295 (model >= 0x2 && model < 0x20))
3296 msr_info->data = 0x1E;
3297 }
3298 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003300 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003301 }
3302 return 0;
3303}
3304
Avi Kivity851ba692009-08-24 11:10:17 +03003305static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306{
David Kaplan668f1982015-02-20 16:02:10 -06003307 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003308 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003310 msr_info.index = ecx;
3311 msr_info.host_initiated = false;
3312 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02003313 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003314 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003315 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003316 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003317
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003318 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
3319 msr_info.data & 0xffffffff);
3320 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
3321 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003322 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003323 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324 }
3325 return 1;
3326}
3327
Joerg Roedel4a810182010-02-24 18:59:15 +01003328static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3329{
3330 struct vcpu_svm *svm = to_svm(vcpu);
3331 int svm_dis, chg_mask;
3332
3333 if (data & ~SVM_VM_CR_VALID_MASK)
3334 return 1;
3335
3336 chg_mask = SVM_VM_CR_VALID_MASK;
3337
3338 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3339 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3340
3341 svm->nested.vm_cr_msr &= ~chg_mask;
3342 svm->nested.vm_cr_msr |= (data & chg_mask);
3343
3344 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3345
3346 /* check for svm_disable while efer.svme is set */
3347 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3348 return 1;
3349
3350 return 0;
3351}
3352
Will Auld8fe8ab42012-11-29 12:42:12 -08003353static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003354{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003355 struct vcpu_svm *svm = to_svm(vcpu);
3356
Will Auld8fe8ab42012-11-29 12:42:12 -08003357 u32 ecx = msr->index;
3358 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003359 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003360 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003361 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003363 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003364 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003365 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003366#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003367 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003368 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003369 break;
3370 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003371 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003372 break;
3373 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003374 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003375 break;
3376 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003377 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003378 break;
3379#endif
3380 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003381 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003382 break;
3383 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003384 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003385 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003386 break;
3387 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003388 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003389 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003391 case MSR_TSC_AUX:
3392 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3393 return 1;
3394
3395 /*
3396 * This is rare, so we update the MSR here instead of using
3397 * direct_access_msrs. Doing that would require a rdmsr in
3398 * svm_vcpu_put.
3399 */
3400 svm->tsc_aux = data;
3401 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
3402 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003403 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003404 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003405 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
3406 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003407 break;
3408 }
3409 if (data & DEBUGCTL_RESERVED_BITS)
3410 return 1;
3411
3412 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003413 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003414 if (data & (1ULL<<0))
3415 svm_enable_lbrv(svm);
3416 else
3417 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003418 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003419 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003420 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003421 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003422 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003423 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003424 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03003425 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003426 break;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003427 case MSR_IA32_APICBASE:
3428 if (kvm_vcpu_apicv_active(vcpu))
3429 avic_update_vapic_bar(to_svm(vcpu), data);
3430 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003431 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08003432 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 }
3434 return 0;
3435}
3436
Avi Kivity851ba692009-08-24 11:10:17 +03003437static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438{
Will Auld8fe8ab42012-11-29 12:42:12 -08003439 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06003440 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3441 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003442
Will Auld8fe8ab42012-11-29 12:42:12 -08003443 msr.data = data;
3444 msr.index = ecx;
3445 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003446
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003447 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03003448 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02003449 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003450 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003451 } else {
3452 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10003453 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003454 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455 return 1;
3456}
3457
Avi Kivity851ba692009-08-24 11:10:17 +03003458static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459{
Rusty Russelle756fc62007-07-30 20:07:08 +10003460 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003461 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462 else
Avi Kivity851ba692009-08-24 11:10:17 +03003463 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003464}
3465
Avi Kivity851ba692009-08-24 11:10:17 +03003466static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003467{
Avi Kivity3842d132010-07-27 12:30:24 +03003468 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003469 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003470 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003471 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08003472 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003473 return 1;
3474}
3475
Mark Langsdorf565d0992009-10-06 14:25:02 -05003476static int pause_interception(struct vcpu_svm *svm)
3477{
3478 kvm_vcpu_on_spin(&(svm->vcpu));
3479 return 1;
3480}
3481
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003482static int nop_interception(struct vcpu_svm *svm)
3483{
3484 skip_emulated_instruction(&(svm->vcpu));
3485 return 1;
3486}
3487
3488static int monitor_interception(struct vcpu_svm *svm)
3489{
3490 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
3491 return nop_interception(svm);
3492}
3493
3494static int mwait_interception(struct vcpu_svm *svm)
3495{
3496 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
3497 return nop_interception(svm);
3498}
3499
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003500enum avic_ipi_failure_cause {
3501 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
3502 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
3503 AVIC_IPI_FAILURE_INVALID_TARGET,
3504 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
3505};
3506
3507static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
3508{
3509 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
3510 u32 icrl = svm->vmcb->control.exit_info_1;
3511 u32 id = svm->vmcb->control.exit_info_2 >> 32;
3512 u32 index = svm->vmcb->control.exit_info_2 && 0xFF;
3513 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3514
3515 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
3516
3517 switch (id) {
3518 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
3519 /*
3520 * AVIC hardware handles the generation of
3521 * IPIs when the specified Message Type is Fixed
3522 * (also known as fixed delivery mode) and
3523 * the Trigger Mode is edge-triggered. The hardware
3524 * also supports self and broadcast delivery modes
3525 * specified via the Destination Shorthand(DSH)
3526 * field of the ICRL. Logical and physical APIC ID
3527 * formats are supported. All other IPI types cause
3528 * a #VMEXIT, which needs to emulated.
3529 */
3530 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
3531 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
3532 break;
3533 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
3534 int i;
3535 struct kvm_vcpu *vcpu;
3536 struct kvm *kvm = svm->vcpu.kvm;
3537 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3538
3539 /*
3540 * At this point, we expect that the AVIC HW has already
3541 * set the appropriate IRR bits on the valid target
3542 * vcpus. So, we just need to kick the appropriate vcpu.
3543 */
3544 kvm_for_each_vcpu(i, vcpu, kvm) {
3545 bool m = kvm_apic_match_dest(vcpu, apic,
3546 icrl & KVM_APIC_SHORT_MASK,
3547 GET_APIC_DEST_FIELD(icrh),
3548 icrl & KVM_APIC_DEST_MASK);
3549
3550 if (m && !avic_vcpu_is_running(vcpu))
3551 kvm_vcpu_wake_up(vcpu);
3552 }
3553 break;
3554 }
3555 case AVIC_IPI_FAILURE_INVALID_TARGET:
3556 break;
3557 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
3558 WARN_ONCE(1, "Invalid backing page\n");
3559 break;
3560 default:
3561 pr_err("Unknown IPI interception\n");
3562 }
3563
3564 return 1;
3565}
3566
3567static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
3568{
3569 struct kvm_arch *vm_data = &vcpu->kvm->arch;
3570 int index;
3571 u32 *logical_apic_id_table;
3572 int dlid = GET_APIC_LOGICAL_ID(ldr);
3573
3574 if (!dlid)
3575 return NULL;
3576
3577 if (flat) { /* flat */
3578 index = ffs(dlid) - 1;
3579 if (index > 7)
3580 return NULL;
3581 } else { /* cluster */
3582 int cluster = (dlid & 0xf0) >> 4;
3583 int apic = ffs(dlid & 0x0f) - 1;
3584
3585 if ((apic < 0) || (apic > 7) ||
3586 (cluster >= 0xf))
3587 return NULL;
3588 index = (cluster << 2) + apic;
3589 }
3590
3591 logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page);
3592
3593 return &logical_apic_id_table[index];
3594}
3595
3596static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
3597 bool valid)
3598{
3599 bool flat;
3600 u32 *entry, new_entry;
3601
3602 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
3603 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
3604 if (!entry)
3605 return -EINVAL;
3606
3607 new_entry = READ_ONCE(*entry);
3608 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
3609 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
3610 if (valid)
3611 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
3612 else
3613 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
3614 WRITE_ONCE(*entry, new_entry);
3615
3616 return 0;
3617}
3618
3619static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
3620{
3621 int ret;
3622 struct vcpu_svm *svm = to_svm(vcpu);
3623 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
3624
3625 if (!ldr)
3626 return 1;
3627
3628 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
3629 if (ret && svm->ldr_reg) {
3630 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
3631 svm->ldr_reg = 0;
3632 } else {
3633 svm->ldr_reg = ldr;
3634 }
3635 return ret;
3636}
3637
3638static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
3639{
3640 u64 *old, *new;
3641 struct vcpu_svm *svm = to_svm(vcpu);
3642 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
3643 u32 id = (apic_id_reg >> 24) & 0xff;
3644
3645 if (vcpu->vcpu_id == id)
3646 return 0;
3647
3648 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
3649 new = avic_get_physical_id_entry(vcpu, id);
3650 if (!new || !old)
3651 return 1;
3652
3653 /* We need to move physical_id_entry to new offset */
3654 *new = *old;
3655 *old = 0ULL;
3656 to_svm(vcpu)->avic_physical_id_cache = new;
3657
3658 /*
3659 * Also update the guest physical APIC ID in the logical
3660 * APIC ID table entry if already setup the LDR.
3661 */
3662 if (svm->ldr_reg)
3663 avic_handle_ldr_update(vcpu);
3664
3665 return 0;
3666}
3667
3668static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
3669{
3670 struct vcpu_svm *svm = to_svm(vcpu);
3671 struct kvm_arch *vm_data = &vcpu->kvm->arch;
3672 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
3673 u32 mod = (dfr >> 28) & 0xf;
3674
3675 /*
3676 * We assume that all local APICs are using the same type.
3677 * If this changes, we need to flush the AVIC logical
3678 * APID id table.
3679 */
3680 if (vm_data->ldr_mode == mod)
3681 return 0;
3682
3683 clear_page(page_address(vm_data->avic_logical_id_table_page));
3684 vm_data->ldr_mode = mod;
3685
3686 if (svm->ldr_reg)
3687 avic_handle_ldr_update(vcpu);
3688 return 0;
3689}
3690
3691static int avic_unaccel_trap_write(struct vcpu_svm *svm)
3692{
3693 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3694 u32 offset = svm->vmcb->control.exit_info_1 &
3695 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
3696
3697 switch (offset) {
3698 case APIC_ID:
3699 if (avic_handle_apic_id_update(&svm->vcpu))
3700 return 0;
3701 break;
3702 case APIC_LDR:
3703 if (avic_handle_ldr_update(&svm->vcpu))
3704 return 0;
3705 break;
3706 case APIC_DFR:
3707 avic_handle_dfr_update(&svm->vcpu);
3708 break;
3709 default:
3710 break;
3711 }
3712
3713 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
3714
3715 return 1;
3716}
3717
3718static bool is_avic_unaccelerated_access_trap(u32 offset)
3719{
3720 bool ret = false;
3721
3722 switch (offset) {
3723 case APIC_ID:
3724 case APIC_EOI:
3725 case APIC_RRR:
3726 case APIC_LDR:
3727 case APIC_DFR:
3728 case APIC_SPIV:
3729 case APIC_ESR:
3730 case APIC_ICR:
3731 case APIC_LVTT:
3732 case APIC_LVTTHMR:
3733 case APIC_LVTPC:
3734 case APIC_LVT0:
3735 case APIC_LVT1:
3736 case APIC_LVTERR:
3737 case APIC_TMICT:
3738 case APIC_TDCR:
3739 ret = true;
3740 break;
3741 default:
3742 break;
3743 }
3744 return ret;
3745}
3746
3747static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
3748{
3749 int ret = 0;
3750 u32 offset = svm->vmcb->control.exit_info_1 &
3751 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
3752 u32 vector = svm->vmcb->control.exit_info_2 &
3753 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
3754 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
3755 AVIC_UNACCEL_ACCESS_WRITE_MASK;
3756 bool trap = is_avic_unaccelerated_access_trap(offset);
3757
3758 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
3759 trap, write, vector);
3760 if (trap) {
3761 /* Handling Trap */
3762 WARN_ONCE(!write, "svm: Handling trap read.\n");
3763 ret = avic_unaccel_trap_write(svm);
3764 } else {
3765 /* Handling Fault */
3766 ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
3767 }
3768
3769 return ret;
3770}
3771
Mathias Krause09941fb2012-08-30 01:30:20 +02003772static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003773 [SVM_EXIT_READ_CR0] = cr_interception,
3774 [SVM_EXIT_READ_CR3] = cr_interception,
3775 [SVM_EXIT_READ_CR4] = cr_interception,
3776 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06003777 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003778 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003779 [SVM_EXIT_WRITE_CR3] = cr_interception,
3780 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003781 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003782 [SVM_EXIT_READ_DR0] = dr_interception,
3783 [SVM_EXIT_READ_DR1] = dr_interception,
3784 [SVM_EXIT_READ_DR2] = dr_interception,
3785 [SVM_EXIT_READ_DR3] = dr_interception,
3786 [SVM_EXIT_READ_DR4] = dr_interception,
3787 [SVM_EXIT_READ_DR5] = dr_interception,
3788 [SVM_EXIT_READ_DR6] = dr_interception,
3789 [SVM_EXIT_READ_DR7] = dr_interception,
3790 [SVM_EXIT_WRITE_DR0] = dr_interception,
3791 [SVM_EXIT_WRITE_DR1] = dr_interception,
3792 [SVM_EXIT_WRITE_DR2] = dr_interception,
3793 [SVM_EXIT_WRITE_DR3] = dr_interception,
3794 [SVM_EXIT_WRITE_DR4] = dr_interception,
3795 [SVM_EXIT_WRITE_DR5] = dr_interception,
3796 [SVM_EXIT_WRITE_DR6] = dr_interception,
3797 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003798 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3799 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003800 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003801 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3802 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
3803 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01003804 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003805 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003806 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003807 [SVM_EXIT_SMI] = nop_on_interception,
3808 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003809 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02003810 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003811 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003812 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003813 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003814 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003815 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003816 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003817 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003818 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003819 [SVM_EXIT_MSR] = msr_interception,
3820 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003821 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003822 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003823 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003824 [SVM_EXIT_VMLOAD] = vmload_interception,
3825 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003826 [SVM_EXIT_STGI] = stgi_interception,
3827 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003828 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06003829 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003830 [SVM_EXIT_MONITOR] = monitor_interception,
3831 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003832 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003833 [SVM_EXIT_NPF] = pf_interception,
Paolo Bonzini64d60672015-05-07 11:36:11 +02003834 [SVM_EXIT_RSM] = emulate_on_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003835 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
3836 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003837};
3838
Joe Perchesae8cc052011-04-24 22:00:50 -07003839static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003840{
3841 struct vcpu_svm *svm = to_svm(vcpu);
3842 struct vmcb_control_area *control = &svm->vmcb->control;
3843 struct vmcb_save_area *save = &svm->vmcb->save;
3844
3845 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003846 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
3847 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
3848 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
3849 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
3850 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
3851 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
3852 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3853 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3854 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3855 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3856 pr_err("%-20s%d\n", "asid:", control->asid);
3857 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3858 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3859 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3860 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3861 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3862 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3863 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3864 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3865 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3866 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3867 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003868 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07003869 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3870 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3871 pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
3872 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003873 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3874 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3875 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003876 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003877 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3878 "es:",
3879 save->es.selector, save->es.attrib,
3880 save->es.limit, save->es.base);
3881 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3882 "cs:",
3883 save->cs.selector, save->cs.attrib,
3884 save->cs.limit, save->cs.base);
3885 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3886 "ss:",
3887 save->ss.selector, save->ss.attrib,
3888 save->ss.limit, save->ss.base);
3889 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3890 "ds:",
3891 save->ds.selector, save->ds.attrib,
3892 save->ds.limit, save->ds.base);
3893 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3894 "fs:",
3895 save->fs.selector, save->fs.attrib,
3896 save->fs.limit, save->fs.base);
3897 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3898 "gs:",
3899 save->gs.selector, save->gs.attrib,
3900 save->gs.limit, save->gs.base);
3901 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3902 "gdtr:",
3903 save->gdtr.selector, save->gdtr.attrib,
3904 save->gdtr.limit, save->gdtr.base);
3905 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3906 "ldtr:",
3907 save->ldtr.selector, save->ldtr.attrib,
3908 save->ldtr.limit, save->ldtr.base);
3909 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3910 "idtr:",
3911 save->idtr.selector, save->idtr.attrib,
3912 save->idtr.limit, save->idtr.base);
3913 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3914 "tr:",
3915 save->tr.selector, save->tr.attrib,
3916 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003917 pr_err("cpl: %d efer: %016llx\n",
3918 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003919 pr_err("%-15s %016llx %-13s %016llx\n",
3920 "cr0:", save->cr0, "cr2:", save->cr2);
3921 pr_err("%-15s %016llx %-13s %016llx\n",
3922 "cr3:", save->cr3, "cr4:", save->cr4);
3923 pr_err("%-15s %016llx %-13s %016llx\n",
3924 "dr6:", save->dr6, "dr7:", save->dr7);
3925 pr_err("%-15s %016llx %-13s %016llx\n",
3926 "rip:", save->rip, "rflags:", save->rflags);
3927 pr_err("%-15s %016llx %-13s %016llx\n",
3928 "rsp:", save->rsp, "rax:", save->rax);
3929 pr_err("%-15s %016llx %-13s %016llx\n",
3930 "star:", save->star, "lstar:", save->lstar);
3931 pr_err("%-15s %016llx %-13s %016llx\n",
3932 "cstar:", save->cstar, "sfmask:", save->sfmask);
3933 pr_err("%-15s %016llx %-13s %016llx\n",
3934 "kernel_gs_base:", save->kernel_gs_base,
3935 "sysenter_cs:", save->sysenter_cs);
3936 pr_err("%-15s %016llx %-13s %016llx\n",
3937 "sysenter_esp:", save->sysenter_esp,
3938 "sysenter_eip:", save->sysenter_eip);
3939 pr_err("%-15s %016llx %-13s %016llx\n",
3940 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3941 pr_err("%-15s %016llx %-13s %016llx\n",
3942 "br_from:", save->br_from, "br_to:", save->br_to);
3943 pr_err("%-15s %016llx %-13s %016llx\n",
3944 "excp_from:", save->last_excp_from,
3945 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003946}
3947
Avi Kivity586f9602010-11-18 13:09:54 +02003948static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3949{
3950 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3951
3952 *info1 = control->exit_info_1;
3953 *info2 = control->exit_info_2;
3954}
3955
Avi Kivity851ba692009-08-24 11:10:17 +03003956static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003957{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003958 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003959 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003960 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003961
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01003962 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
3963
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003964 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003965 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3966 if (npt_enabled)
3967 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003968
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003969 if (unlikely(svm->nested.exit_required)) {
3970 nested_svm_vmexit(svm);
3971 svm->nested.exit_required = false;
3972
3973 return 1;
3974 }
3975
Joerg Roedel20307532010-11-29 17:51:48 +01003976 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003977 int vmexit;
3978
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003979 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3980 svm->vmcb->control.exit_info_1,
3981 svm->vmcb->control.exit_info_2,
3982 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003983 svm->vmcb->control.exit_int_info_err,
3984 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003985
Joerg Roedel410e4d52009-08-07 11:49:44 +02003986 vmexit = nested_svm_exit_special(svm);
3987
3988 if (vmexit == NESTED_EXIT_CONTINUE)
3989 vmexit = nested_svm_exit_handled(svm);
3990
3991 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003992 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003993 }
3994
Joerg Roedela5c38322009-08-07 11:49:32 +02003995 svm_complete_interrupts(svm);
3996
Avi Kivity04d2cc72007-09-10 18:10:54 +03003997 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3998 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3999 kvm_run->fail_entry.hardware_entry_failure_reason
4000 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004001 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4002 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004003 return 0;
4004 }
4005
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004006 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004007 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004008 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4009 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004010 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004011 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004012 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013 exit_code);
4014
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004015 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004016 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04004017 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03004018 kvm_queue_exception(vcpu, UD_VECTOR);
4019 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004020 }
4021
Avi Kivity851ba692009-08-24 11:10:17 +03004022 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004023}
4024
4025static void reload_tss(struct kvm_vcpu *vcpu)
4026{
4027 int cpu = raw_smp_processor_id();
4028
Tejun Heo0fe1e002009-10-29 22:34:14 +09004029 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4030 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004031 load_TR_desc();
4032}
4033
Rusty Russelle756fc62007-07-30 20:07:08 +10004034static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004035{
4036 int cpu = raw_smp_processor_id();
4037
Tejun Heo0fe1e002009-10-29 22:34:14 +09004038 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004039
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03004040 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09004041 if (svm->asid_generation != sd->asid_generation)
4042 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004043}
4044
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004045static void svm_inject_nmi(struct kvm_vcpu *vcpu)
4046{
4047 struct vcpu_svm *svm = to_svm(vcpu);
4048
4049 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
4050 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004051 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004052 ++vcpu->stat.nmi_injections;
4053}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004054
Eddie Dong85f455f2007-07-06 12:20:49 +03004055static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056{
4057 struct vmcb_control_area *control;
4058
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004059 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10004060 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03004061 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062 control->int_ctl &= ~V_INTR_PRIO_MASK;
4063 control->int_ctl |= V_IRQ_MASK |
4064 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004065 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004066}
4067
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004068static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03004069{
4070 struct vcpu_svm *svm = to_svm(vcpu);
4071
Joerg Roedel2af91942009-08-07 11:49:28 +02004072 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01004073
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03004074 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
4075 ++vcpu->stat.irq_injections;
4076
Alexander Graf219b65d2009-06-15 15:21:25 +02004077 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
4078 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03004079}
4080
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004081static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
4082{
4083 struct vcpu_svm *svm = to_svm(vcpu);
4084
Joerg Roedel20307532010-11-29 17:51:48 +01004085 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004086 return;
4087
Radim Krčmář596f3142014-03-11 19:11:18 +01004088 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
4089
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004090 if (irr == -1)
4091 return;
4092
4093 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004094 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004095}
4096
Yang Zhang8d146952013-01-25 10:18:50 +08004097static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
4098{
4099 return;
4100}
4101
Andrey Smetanind62caab2015-11-10 15:36:33 +03004102static bool svm_get_enable_apicv(void)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004103{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004104 return avic;
Yang Zhangc7c9c562013-01-25 10:18:51 +08004105}
4106
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004107static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
4108{
4109}
4110
4111static void svm_hwapic_isr_update(struct kvm *kvm, int isr)
4112{
4113}
4114
4115/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03004116static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4117{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004118 struct vcpu_svm *svm = to_svm(vcpu);
4119 struct vmcb *vmcb = svm->vmcb;
4120
4121 if (!avic)
4122 return;
4123
4124 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
4125 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004126}
4127
Andrey Smetanin63086302015-11-10 15:36:32 +03004128static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004129{
4130 return;
4131}
4132
Yang Zhanga20ed542013-04-11 19:25:15 +08004133static void svm_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4134{
4135 return;
4136}
4137
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004138static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
4139{
4140 kvm_lapic_set_irr(vec, vcpu->arch.apic);
4141 smp_mb__after_atomic();
4142
4143 if (avic_vcpu_is_running(vcpu))
4144 wrmsrl(SVM_AVIC_DOORBELL,
4145 __default_cpu_present_to_apicid(vcpu->cpu));
4146 else
4147 kvm_vcpu_wake_up(vcpu);
4148}
4149
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004150static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02004151{
4152 struct vcpu_svm *svm = to_svm(vcpu);
4153 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02004154 int ret;
4155 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
4156 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
4157 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
4158
4159 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02004160}
4161
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004162static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
4163{
4164 struct vcpu_svm *svm = to_svm(vcpu);
4165
4166 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
4167}
4168
4169static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4170{
4171 struct vcpu_svm *svm = to_svm(vcpu);
4172
4173 if (masked) {
4174 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004175 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004176 } else {
4177 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004178 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004179 }
4180}
4181
Gleb Natapov78646122009-03-23 12:12:11 +02004182static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
4183{
4184 struct vcpu_svm *svm = to_svm(vcpu);
4185 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02004186 int ret;
4187
4188 if (!gif_set(svm) ||
4189 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
4190 return 0;
4191
Avi Kivityf6e78472010-08-02 15:30:20 +03004192 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02004193
Joerg Roedel20307532010-11-29 17:51:48 +01004194 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02004195 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
4196
4197 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02004198}
4199
Jan Kiszkac9a79532014-03-07 20:03:15 +01004200static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03004201{
Alexander Graf219b65d2009-06-15 15:21:25 +02004202 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02004203
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004204 if (kvm_vcpu_apicv_active(vcpu))
4205 return;
4206
Joerg Roedele0231712010-02-24 18:59:10 +01004207 /*
4208 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
4209 * 1, because that's a separate STGI/VMRUN intercept. The next time we
4210 * get that intercept, this function will be called again though and
4211 * we'll get the vintr intercept.
4212 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01004213 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02004214 svm_set_vintr(svm);
4215 svm_inject_irq(svm, 0x0);
4216 }
Gleb Natapov9222be12009-04-23 17:14:37 +03004217}
4218
Jan Kiszkac9a79532014-03-07 20:03:15 +01004219static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004220{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004221 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004222
Gleb Natapov44c11432009-05-11 13:35:52 +03004223 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
4224 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01004225 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03004226
Joerg Roedele0231712010-02-24 18:59:10 +01004227 /*
4228 * Something prevents NMI from been injected. Single step over possible
4229 * problem (IRET or exception injection or interrupt shadow)
4230 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02004231 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03004232 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03004233}
4234
Izik Eiduscbc94022007-10-25 00:29:55 +02004235static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
4236{
4237 return 0;
4238}
4239
Avi Kivityd9e368d2007-06-07 19:18:30 +03004240static void svm_flush_tlb(struct kvm_vcpu *vcpu)
4241{
Joerg Roedel38e5e922010-12-03 15:25:16 +01004242 struct vcpu_svm *svm = to_svm(vcpu);
4243
4244 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
4245 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4246 else
4247 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03004248}
4249
Avi Kivity04d2cc72007-09-10 18:10:54 +03004250static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
4251{
4252}
4253
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004254static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
4255{
4256 struct vcpu_svm *svm = to_svm(vcpu);
4257
Joerg Roedel20307532010-11-29 17:51:48 +01004258 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004259 return;
4260
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004261 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004262 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03004263 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004264 }
4265}
4266
Joerg Roedel649d6862008-04-16 16:51:15 +02004267static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
4268{
4269 struct vcpu_svm *svm = to_svm(vcpu);
4270 u64 cr8;
4271
Joerg Roedel20307532010-11-29 17:51:48 +01004272 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004273 return;
4274
Joerg Roedel649d6862008-04-16 16:51:15 +02004275 cr8 = kvm_get_cr8(vcpu);
4276 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
4277 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
4278}
4279
Gleb Natapov9222be12009-04-23 17:14:37 +03004280static void svm_complete_interrupts(struct vcpu_svm *svm)
4281{
4282 u8 vector;
4283 int type;
4284 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01004285 unsigned int3_injected = svm->int3_injected;
4286
4287 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03004288
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02004289 /*
4290 * If we've made progress since setting HF_IRET_MASK, we've
4291 * executed an IRET and can allow NMI injection.
4292 */
4293 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
4294 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03004295 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03004296 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
4297 }
Gleb Natapov44c11432009-05-11 13:35:52 +03004298
Gleb Natapov9222be12009-04-23 17:14:37 +03004299 svm->vcpu.arch.nmi_injected = false;
4300 kvm_clear_exception_queue(&svm->vcpu);
4301 kvm_clear_interrupt_queue(&svm->vcpu);
4302
4303 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
4304 return;
4305
Avi Kivity3842d132010-07-27 12:30:24 +03004306 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
4307
Gleb Natapov9222be12009-04-23 17:14:37 +03004308 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
4309 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
4310
4311 switch (type) {
4312 case SVM_EXITINTINFO_TYPE_NMI:
4313 svm->vcpu.arch.nmi_injected = true;
4314 break;
4315 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01004316 /*
4317 * In case of software exceptions, do not reinject the vector,
4318 * but re-execute the instruction instead. Rewind RIP first
4319 * if we emulated INT3 before.
4320 */
4321 if (kvm_exception_is_soft(vector)) {
4322 if (vector == BP_VECTOR && int3_injected &&
4323 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
4324 kvm_rip_write(&svm->vcpu,
4325 kvm_rip_read(&svm->vcpu) -
4326 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02004327 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01004328 }
Gleb Natapov9222be12009-04-23 17:14:37 +03004329 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
4330 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02004331 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03004332
4333 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02004334 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03004335 break;
4336 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004337 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03004338 break;
4339 default:
4340 break;
4341 }
4342}
4343
Avi Kivityb463a6f2010-07-20 15:06:17 +03004344static void svm_cancel_injection(struct kvm_vcpu *vcpu)
4345{
4346 struct vcpu_svm *svm = to_svm(vcpu);
4347 struct vmcb_control_area *control = &svm->vmcb->control;
4348
4349 control->exit_int_info = control->event_inj;
4350 control->exit_int_info_err = control->event_inj_err;
4351 control->event_inj = 0;
4352 svm_complete_interrupts(svm);
4353}
4354
Avi Kivity851ba692009-08-24 11:10:17 +03004355static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004356{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004357 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03004358
Joerg Roedel2041a062010-04-22 12:33:08 +02004359 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4360 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4361 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4362
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004363 /*
4364 * A vmexit emulation is required before the vcpu can be executed
4365 * again.
4366 */
4367 if (unlikely(svm->nested.exit_required))
4368 return;
4369
Rusty Russelle756fc62007-07-30 20:07:08 +10004370 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004371
Joerg Roedel649d6862008-04-16 16:51:15 +02004372 sync_lapic_to_cr8(vcpu);
4373
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02004374 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004375
Avi Kivity04d2cc72007-09-10 18:10:54 +03004376 clgi();
4377
4378 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08004379
Avi Kivity6aa8b732006-12-10 02:21:36 -08004380 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03004381 "push %%" _ASM_BP "; \n\t"
4382 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
4383 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
4384 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
4385 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
4386 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
4387 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004388#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004389 "mov %c[r8](%[svm]), %%r8 \n\t"
4390 "mov %c[r9](%[svm]), %%r9 \n\t"
4391 "mov %c[r10](%[svm]), %%r10 \n\t"
4392 "mov %c[r11](%[svm]), %%r11 \n\t"
4393 "mov %c[r12](%[svm]), %%r12 \n\t"
4394 "mov %c[r13](%[svm]), %%r13 \n\t"
4395 "mov %c[r14](%[svm]), %%r14 \n\t"
4396 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397#endif
4398
Avi Kivity6aa8b732006-12-10 02:21:36 -08004399 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03004400 "push %%" _ASM_AX " \n\t"
4401 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004402 __ex(SVM_VMLOAD) "\n\t"
4403 __ex(SVM_VMRUN) "\n\t"
4404 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03004405 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004406
4407 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03004408 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
4409 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
4410 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
4411 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
4412 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
4413 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004414#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004415 "mov %%r8, %c[r8](%[svm]) \n\t"
4416 "mov %%r9, %c[r9](%[svm]) \n\t"
4417 "mov %%r10, %c[r10](%[svm]) \n\t"
4418 "mov %%r11, %c[r11](%[svm]) \n\t"
4419 "mov %%r12, %c[r12](%[svm]) \n\t"
4420 "mov %%r13, %c[r13](%[svm]) \n\t"
4421 "mov %%r14, %c[r14](%[svm]) \n\t"
4422 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004423#endif
Avi Kivity74547662012-09-16 15:10:59 +03004424 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08004425 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004426 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08004427 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004428 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
4429 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
4430 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
4431 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
4432 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
4433 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004434#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004435 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
4436 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
4437 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
4438 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
4439 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
4440 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
4441 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
4442 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004443#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02004444 : "cc", "memory"
4445#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03004446 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02004447 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03004448#else
4449 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02004450#endif
4451 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08004452
Avi Kivity82ca2d12010-10-21 12:20:34 +02004453#ifdef CONFIG_X86_64
4454 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
4455#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02004456 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02004457#ifndef CONFIG_X86_32_LAZY_GS
4458 loadsegment(gs, svm->host.gs);
4459#endif
Avi Kivity9581d442010-10-19 16:46:55 +02004460#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004461
4462 reload_tss(vcpu);
4463
Avi Kivity56ba47d2007-11-07 17:14:18 +02004464 local_irq_disable();
4465
Avi Kivity13c34e02010-10-21 12:20:31 +02004466 vcpu->arch.cr2 = svm->vmcb->save.cr2;
4467 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
4468 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
4469 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
4470
Joerg Roedel3781c012011-01-14 16:45:02 +01004471 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
4472 kvm_before_handle_nmi(&svm->vcpu);
4473
4474 stgi();
4475
4476 /* Any pending NMI will happen here */
4477
4478 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
4479 kvm_after_handle_nmi(&svm->vcpu);
4480
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004481 sync_cr8_to_lapic(vcpu);
4482
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004483 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03004484
Joerg Roedel38e5e922010-12-03 15:25:16 +01004485 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
4486
Gleb Natapov631bc482010-10-14 11:22:52 +02004487 /* if exit due to PF check for async PF */
4488 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
4489 svm->apf_reason = kvm_read_and_reset_pf_reason();
4490
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004491 if (npt_enabled) {
4492 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
4493 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
4494 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02004495
4496 /*
4497 * We need to handle MC intercepts here before the vcpu has a chance to
4498 * change the physical cpu
4499 */
4500 if (unlikely(svm->vmcb->control.exit_code ==
4501 SVM_EXIT_EXCP_BASE + MC_VECTOR))
4502 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01004503
4504 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004505}
4506
Avi Kivity6aa8b732006-12-10 02:21:36 -08004507static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
4508{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004509 struct vcpu_svm *svm = to_svm(vcpu);
4510
4511 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01004512 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01004513 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004514}
4515
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004516static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
4517{
4518 struct vcpu_svm *svm = to_svm(vcpu);
4519
4520 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01004521 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004522
4523 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02004524 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01004525 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004526
Joerg Roedelf40f6a42010-12-03 15:25:15 +01004527 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004528}
4529
Avi Kivity6aa8b732006-12-10 02:21:36 -08004530static int is_disabled(void)
4531{
Joerg Roedel6031a612007-06-22 12:29:50 +03004532 u64 vm_cr;
4533
4534 rdmsrl(MSR_VM_CR, vm_cr);
4535 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
4536 return 1;
4537
Avi Kivity6aa8b732006-12-10 02:21:36 -08004538 return 0;
4539}
4540
Ingo Molnar102d8322007-02-19 14:37:47 +02004541static void
4542svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4543{
4544 /*
4545 * Patch in the VMMCALL instruction:
4546 */
4547 hypercall[0] = 0x0f;
4548 hypercall[1] = 0x01;
4549 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02004550}
4551
Yang, Sheng002c7f72007-07-31 14:23:01 +03004552static void svm_check_processor_compat(void *rtn)
4553{
4554 *(int *)rtn = 0;
4555}
4556
Avi Kivity774ead32007-12-26 13:57:04 +02004557static bool svm_cpu_has_accelerated_tpr(void)
4558{
4559 return false;
4560}
4561
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004562static bool svm_has_high_real_mode_segbase(void)
4563{
4564 return true;
4565}
4566
Paolo Bonzinifc07e762015-10-01 13:20:22 +02004567static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
4568{
4569 return 0;
4570}
4571
Sheng Yang0e851882009-12-18 16:48:46 +08004572static void svm_cpuid_update(struct kvm_vcpu *vcpu)
4573{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004574 struct vcpu_svm *svm = to_svm(vcpu);
4575
4576 /* Update nrips enabled cache */
4577 svm->nrips_enabled = !!guest_cpuid_has_nrips(&svm->vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08004578}
4579
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004580static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
4581{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004582 switch (func) {
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02004583 case 0x80000001:
4584 if (nested)
4585 entry->ecx |= (1 << 2); /* Set SVM bit */
4586 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004587 case 0x8000000A:
4588 entry->eax = 1; /* SVM revision 1 */
4589 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
4590 ASID emulation to nested SVM */
4591 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02004592 entry->edx = 0; /* Per default do not support any
4593 additional features */
4594
4595 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004596 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02004597 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004598
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02004599 /* Support NPT for the guest if enabled */
4600 if (npt_enabled)
4601 entry->edx |= SVM_FEATURE_NPT;
4602
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004603 break;
4604 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004605}
4606
Sheng Yang17cc3932010-01-05 19:02:27 +08004607static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004608{
Sheng Yang17cc3932010-01-05 19:02:27 +08004609 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004610}
4611
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004612static bool svm_rdtscp_supported(void)
4613{
Paolo Bonzini46896c72015-11-12 14:49:16 +01004614 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004615}
4616
Mao, Junjiead756a12012-07-02 01:18:48 +00004617static bool svm_invpcid_supported(void)
4618{
4619 return false;
4620}
4621
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004622static bool svm_mpx_supported(void)
4623{
4624 return false;
4625}
4626
Wanpeng Li55412b22014-12-02 19:21:30 +08004627static bool svm_xsaves_supported(void)
4628{
4629 return false;
4630}
4631
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004632static bool svm_has_wbinvd_exit(void)
4633{
4634 return true;
4635}
4636
Avi Kivity02daab22009-12-30 12:40:26 +02004637static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
4638{
4639 struct vcpu_svm *svm = to_svm(vcpu);
4640
Joerg Roedel18c918c2010-11-30 18:03:59 +01004641 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01004642 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02004643}
4644
Joerg Roedel80612522011-04-04 12:39:33 +02004645#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004646 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004647#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004648 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004649#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004650 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004651
Mathias Krause09941fb2012-08-30 01:30:20 +02004652static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004653 u32 exit_code;
4654 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004655} x86_intercept_map[] = {
4656 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4657 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4658 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4659 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4660 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004661 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4662 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004663 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4664 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4665 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4666 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4667 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4668 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4669 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4670 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004671 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4672 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4673 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4674 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4675 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4676 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4677 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4678 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004679 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4680 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4681 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004682 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4683 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4684 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4685 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4686 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4687 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4688 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4689 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4690 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004691 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4692 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4693 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4694 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4695 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4696 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4697 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004698 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4699 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4700 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4701 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004702};
4703
Joerg Roedel80612522011-04-04 12:39:33 +02004704#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004705#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004706#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004707
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004708static int svm_check_intercept(struct kvm_vcpu *vcpu,
4709 struct x86_instruction_info *info,
4710 enum x86_intercept_stage stage)
4711{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004712 struct vcpu_svm *svm = to_svm(vcpu);
4713 int vmexit, ret = X86EMUL_CONTINUE;
4714 struct __x86_intercept icpt_info;
4715 struct vmcb *vmcb = svm->vmcb;
4716
4717 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4718 goto out;
4719
4720 icpt_info = x86_intercept_map[info->intercept];
4721
Avi Kivity40e19b52011-04-21 12:35:41 +03004722 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004723 goto out;
4724
4725 switch (icpt_info.exit_code) {
4726 case SVM_EXIT_READ_CR0:
4727 if (info->intercept == x86_intercept_cr_read)
4728 icpt_info.exit_code += info->modrm_reg;
4729 break;
4730 case SVM_EXIT_WRITE_CR0: {
4731 unsigned long cr0, val;
4732 u64 intercept;
4733
4734 if (info->intercept == x86_intercept_cr_write)
4735 icpt_info.exit_code += info->modrm_reg;
4736
Jan Kiszka62baf442014-06-29 21:55:53 +02004737 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4738 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004739 break;
4740
4741 intercept = svm->nested.intercept;
4742
4743 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
4744 break;
4745
4746 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4747 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4748
4749 if (info->intercept == x86_intercept_lmsw) {
4750 cr0 &= 0xfUL;
4751 val &= 0xfUL;
4752 /* lmsw can't clear PE - catch this here */
4753 if (cr0 & X86_CR0_PE)
4754 val |= X86_CR0_PE;
4755 }
4756
4757 if (cr0 ^ val)
4758 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4759
4760 break;
4761 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004762 case SVM_EXIT_READ_DR0:
4763 case SVM_EXIT_WRITE_DR0:
4764 icpt_info.exit_code += info->modrm_reg;
4765 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004766 case SVM_EXIT_MSR:
4767 if (info->intercept == x86_intercept_wrmsr)
4768 vmcb->control.exit_info_1 = 1;
4769 else
4770 vmcb->control.exit_info_1 = 0;
4771 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004772 case SVM_EXIT_PAUSE:
4773 /*
4774 * We get this for NOP only, but pause
4775 * is rep not, check this here
4776 */
4777 if (info->rep_prefix != REPE_PREFIX)
4778 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02004779 case SVM_EXIT_IOIO: {
4780 u64 exit_info;
4781 u32 bytes;
4782
Joerg Roedelf6511932011-04-04 12:39:35 +02004783 if (info->intercept == x86_intercept_in ||
4784 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004785 exit_info = ((info->src_val & 0xffff) << 16) |
4786 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02004787 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02004788 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004789 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02004790 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02004791 }
4792
4793 if (info->intercept == x86_intercept_outs ||
4794 info->intercept == x86_intercept_ins)
4795 exit_info |= SVM_IOIO_STR_MASK;
4796
4797 if (info->rep_prefix)
4798 exit_info |= SVM_IOIO_REP_MASK;
4799
4800 bytes = min(bytes, 4u);
4801
4802 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4803
4804 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4805
4806 vmcb->control.exit_info_1 = exit_info;
4807 vmcb->control.exit_info_2 = info->next_rip;
4808
4809 break;
4810 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004811 default:
4812 break;
4813 }
4814
Bandan Dasf1047652015-06-11 02:05:33 -04004815 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4816 if (static_cpu_has(X86_FEATURE_NRIPS))
4817 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004818 vmcb->control.exit_code = icpt_info.exit_code;
4819 vmexit = nested_svm_exit_handled(svm);
4820
4821 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4822 : X86EMUL_CONTINUE;
4823
4824out:
4825 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004826}
4827
Yang Zhanga547c6d2013-04-11 19:25:10 +08004828static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
4829{
4830 local_irq_enable();
4831}
4832
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004833static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4834{
4835}
4836
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004837static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004838 .cpu_has_kvm_support = has_svm,
4839 .disabled_by_bios = is_disabled,
4840 .hardware_setup = svm_hardware_setup,
4841 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004842 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004843 .hardware_enable = svm_hardware_enable,
4844 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004845 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004846 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004847
4848 .vcpu_create = svm_create_vcpu,
4849 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004850 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004851
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004852 .vm_init = avic_vm_init,
4853 .vm_destroy = avic_vm_destroy,
4854
Avi Kivity04d2cc72007-09-10 18:10:54 +03004855 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004856 .vcpu_load = svm_vcpu_load,
4857 .vcpu_put = svm_vcpu_put,
4858
Paolo Bonzinia96036b2015-11-10 11:55:36 +01004859 .update_bp_intercept = update_bp_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860 .get_msr = svm_get_msr,
4861 .set_msr = svm_set_msr,
4862 .get_segment_base = svm_get_segment_base,
4863 .get_segment = svm_get_segment,
4864 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004865 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004866 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004867 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004868 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004869 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004871 .set_cr3 = svm_set_cr3,
4872 .set_cr4 = svm_set_cr4,
4873 .set_efer = svm_set_efer,
4874 .get_idt = svm_get_idt,
4875 .set_idt = svm_set_idt,
4876 .get_gdt = svm_get_gdt,
4877 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004878 .get_dr6 = svm_get_dr6,
4879 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03004880 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01004881 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004882 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004883 .get_rflags = svm_get_rflags,
4884 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08004885
4886 .get_pkru = svm_get_pkru,
4887
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +02004888 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004889 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004890
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004892
Avi Kivity6aa8b732006-12-10 02:21:36 -08004893 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004894 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004895 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004896 .set_interrupt_shadow = svm_set_interrupt_shadow,
4897 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004898 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004899 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004900 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004901 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004902 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004903 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004904 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004905 .get_nmi_mask = svm_get_nmi_mask,
4906 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004907 .enable_nmi_window = enable_nmi_window,
4908 .enable_irq_window = enable_irq_window,
4909 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08004910 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03004911 .get_enable_apicv = svm_get_enable_apicv,
4912 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08004913 .load_eoi_exitmap = svm_load_eoi_exitmap,
Yang Zhanga20ed542013-04-11 19:25:15 +08004914 .sync_pir_to_irr = svm_sync_pir_to_irr,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004915 .hwapic_irr_update = svm_hwapic_irr_update,
4916 .hwapic_isr_update = svm_hwapic_isr_update,
Izik Eiduscbc94022007-10-25 00:29:55 +02004917
4918 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004919 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004920 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004921
Avi Kivity586f9602010-11-18 13:09:54 +02004922 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004923
Sheng Yang17cc3932010-01-05 19:02:27 +08004924 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004925
4926 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004927
4928 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00004929 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004930 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08004931 .xsaves_supported = svm_xsaves_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004932
4933 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004934
4935 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004936
Will Auldba904632012-11-29 12:42:50 -08004937 .read_tsc_offset = svm_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004938 .write_tsc_offset = svm_write_tsc_offset,
Haozhong Zhang58ea6762015-10-20 15:39:06 +08004939 .adjust_tsc_offset_guest = svm_adjust_tsc_offset_guest,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03004940 .read_l1_tsc = svm_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004941
4942 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004943
4944 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08004945 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004946
4947 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02004948
4949 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004950 .deliver_posted_interrupt = svm_deliver_avic_intr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004951};
4952
4953static int __init svm_init(void)
4954{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004955 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004956 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004957}
4958
4959static void __exit svm_exit(void)
4960{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004961 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004962}
4963
4964module_init(svm_init)
4965module_exit(svm_exit)