blob: 597337987a167f38938f21ac7ad4b7c7c3a9faa1 [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>
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -050037#include <linux/amd-iommu.h>
38#include <linux/hashtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080039
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050040#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010041#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020042#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040043#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010044#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020045#include <asm/kvm_para.h>
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -050046#include <asm/irq_remapping.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080047
Eduardo Habkost63d11422008-11-17 19:03:20 -020048#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030049#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020050
Avi Kivity4ecac3f2008-05-13 13:23:38 +030051#define __ex(x) __kvm_handle_fault_on_reboot(x)
52
Avi Kivity6aa8b732006-12-10 02:21:36 -080053MODULE_AUTHOR("Qumranet");
54MODULE_LICENSE("GPL");
55
Josh Triplettae759542012-03-28 11:32:28 -070056static const struct x86_cpu_id svm_cpu_id[] = {
57 X86_FEATURE_MATCH(X86_FEATURE_SVM),
58 {}
59};
60MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
61
Avi Kivity6aa8b732006-12-10 02:21:36 -080062#define IOPM_ALLOC_ORDER 2
63#define MSRPM_ALLOC_ORDER 1
64
Avi Kivity6aa8b732006-12-10 02:21:36 -080065#define SEG_TYPE_LDT 2
66#define SEG_TYPE_BUSY_TSS16 3
67
Andre Przywara6bc31bd2010-04-11 23:07:28 +020068#define SVM_FEATURE_NPT (1 << 0)
69#define SVM_FEATURE_LBRV (1 << 1)
70#define SVM_FEATURE_SVML (1 << 2)
71#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010072#define SVM_FEATURE_TSC_RATE (1 << 4)
73#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
74#define SVM_FEATURE_FLUSH_ASID (1 << 6)
75#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020076#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030077
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -050078#define SVM_AVIC_DOORBELL 0xc001011b
79
Joerg Roedel410e4d52009-08-07 11:49:44 +020080#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
81#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
82#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
83
Joerg Roedel24e09cb2008-02-13 18:58:47 +010084#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
85
Joerg Roedelfbc0db72011-03-25 09:44:46 +010086#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010087#define TSC_RATIO_MIN 0x0000000000000001ULL
88#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010089
Dan Carpenter5446a972016-05-23 13:20:10 +030090#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050091
92/*
93 * 0xff is broadcast, so the max index allowed for physical APIC ID
94 * table is 0xfe. APIC IDs above 0xff are reserved.
95 */
96#define AVIC_MAX_PHYSICAL_ID_COUNT 255
97
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050098#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
99#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
100#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
101
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500102/* AVIC GATAG is encoded using VM and VCPU IDs */
103#define AVIC_VCPU_ID_BITS 8
104#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
105
106#define AVIC_VM_ID_BITS 24
107#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
108#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
109
110#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
111 (y & AVIC_VCPU_ID_MASK))
112#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
113#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
114
Joerg Roedel67ec6602010-05-17 14:43:35 +0200115static bool erratum_383_found __read_mostly;
116
Avi Kivity6c8166a2009-05-31 18:15:37 +0300117static const u32 host_save_user_msrs[] = {
118#ifdef CONFIG_X86_64
119 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
120 MSR_FS_BASE,
121#endif
122 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Paolo Bonzini46896c72015-11-12 14:49:16 +0100123 MSR_TSC_AUX,
Avi Kivity6c8166a2009-05-31 18:15:37 +0300124};
125
126#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
127
128struct kvm_vcpu;
129
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200130struct nested_state {
131 struct vmcb *hsave;
132 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +0100133 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200134 u64 vmcb;
135
136 /* These are the merged vectors */
137 u32 *msrpm;
138
139 /* gpa pointers to the real vectors */
140 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100141 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200142
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200143 /* A VMEXIT is required but not yet emulated */
144 bool exit_required;
145
Joerg Roedelaad42c62009-08-07 11:49:34 +0200146 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100147 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100148 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200149 u32 intercept_exceptions;
150 u64 intercept;
151
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200152 /* Nested Paging related state */
153 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200154};
155
Joerg Roedel323c3d82010-03-01 15:34:37 +0100156#define MSRPM_OFFSETS 16
157static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
158
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500159/*
160 * Set osvw_len to higher value when updated Revision Guides
161 * are published and we know what the new status bits are
162 */
163static uint64_t osvw_len = 4, osvw_status;
164
Avi Kivity6c8166a2009-05-31 18:15:37 +0300165struct vcpu_svm {
166 struct kvm_vcpu vcpu;
167 struct vmcb *vmcb;
168 unsigned long vmcb_pa;
169 struct svm_cpu_data *svm_data;
170 uint64_t asid_generation;
171 uint64_t sysenter_esp;
172 uint64_t sysenter_eip;
Paolo Bonzini46896c72015-11-12 14:49:16 +0100173 uint64_t tsc_aux;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300174
175 u64 next_rip;
176
177 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200178 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200179 u16 fs;
180 u16 gs;
181 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200182 u64 gs_base;
183 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300184
185 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300186
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200187 ulong nmi_iret_rip;
188
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200189 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200190
191 bool nmi_singlestep;
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200192 u64 nmi_singlestep_guest_rflags;
Jan Kiszka66b71382010-02-23 17:47:56 +0100193
194 unsigned int3_injected;
195 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200196 u32 apf_reason;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100197
Joerg Roedel6092d3d2015-10-14 15:10:54 +0200198 /* cached guest cpuid flags for faster access */
199 bool nrips_enabled : 1;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500200
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500201 u32 ldr_reg;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500202 struct page *avic_backing_page;
203 u64 *avic_physical_id_cache;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -0500204 bool avic_is_running;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500205
206 /*
207 * Per-vcpu list of struct amd_svm_iommu_ir:
208 * This is used mainly to store interrupt remapping information used
209 * when update the vcpu affinity. This avoids the need to scan for
210 * IRTE and try to match ga_tag in the IOMMU driver.
211 */
212 struct list_head ir_list;
213 spinlock_t ir_list_lock;
214};
215
216/*
217 * This is a wrapper of struct amd_iommu_ir_data.
218 */
219struct amd_svm_iommu_ir {
220 struct list_head node; /* Used by SVM for per-vcpu ir_list */
221 void *data; /* Storing pointer to struct amd_ir_data */
Avi Kivity6c8166a2009-05-31 18:15:37 +0300222};
223
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500224#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
225#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
226
227#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
228#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
229#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
230#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
231
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100232static DEFINE_PER_CPU(u64, current_tsc_ratio);
233#define TSC_RATIO_DEFAULT 0x0100000000ULL
234
Joerg Roedel455716f2010-03-01 15:34:35 +0100235#define MSR_INVALID 0xffffffffU
236
Mathias Krause09941fb2012-08-30 01:30:20 +0200237static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100238 u32 index; /* Index of the MSR */
239 bool always; /* True if intercept is always on */
240} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400241 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100242 { .index = MSR_IA32_SYSENTER_CS, .always = true },
243#ifdef CONFIG_X86_64
244 { .index = MSR_GS_BASE, .always = true },
245 { .index = MSR_FS_BASE, .always = true },
246 { .index = MSR_KERNEL_GS_BASE, .always = true },
247 { .index = MSR_LSTAR, .always = true },
248 { .index = MSR_CSTAR, .always = true },
249 { .index = MSR_SYSCALL_MASK, .always = true },
250#endif
251 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
252 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
253 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
254 { .index = MSR_IA32_LASTINTTOIP, .always = false },
255 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256};
257
258/* enable NPT for AMD64 and X86 with PAE */
259#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
260static bool npt_enabled = true;
261#else
Joerg Roedele0231712010-02-24 18:59:10 +0100262static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263#endif
264
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100265/* allow nested paging (virtualized MMU) for all guests */
266static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800267module_param(npt, int, S_IRUGO);
268
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100269/* allow nested virtualization in KVM/SVM */
270static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271module_param(nested, int, S_IRUGO);
272
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500273/* enable / disable AVIC */
274static int avic;
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500275#ifdef CONFIG_X86_LOCAL_APIC
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500276module_param(avic, int, S_IRUGO);
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500277#endif
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500278
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500279/* AVIC VM ID bit masks and lock */
280static DECLARE_BITMAP(avic_vm_id_bitmap, AVIC_VM_ID_NR);
281static DEFINE_SPINLOCK(avic_vm_id_lock);
282
Paolo Bonzini79a80592015-09-21 07:46:55 +0200283static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200285static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286
Joerg Roedel410e4d52009-08-07 11:49:44 +0200287static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100288static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800289static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
291 bool has_error_code, u32 error_code);
292
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100293enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100294 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
295 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100296 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100297 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100298 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100299 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100300 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100301 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100302 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100303 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100304 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100305 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500306 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
307 * AVIC PHYSICAL_TABLE pointer,
308 * AVIC LOGICAL_TABLE pointer
309 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100310 VMCB_DIRTY_MAX,
311};
312
Joerg Roedel0574dec2010-12-03 11:45:58 +0100313/* TPR and CR2 are always written before VMRUN */
314#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100315
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500316#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
317
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100318static inline void mark_all_dirty(struct vmcb *vmcb)
319{
320 vmcb->control.clean = 0;
321}
322
323static inline void mark_all_clean(struct vmcb *vmcb)
324{
325 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
326 & ~VMCB_ALWAYS_DIRTY_MASK;
327}
328
329static inline void mark_dirty(struct vmcb *vmcb, int bit)
330{
331 vmcb->control.clean &= ~(1 << bit);
332}
333
Avi Kivity6aa8b732006-12-10 02:21:36 -0800334static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
335{
336 return container_of(vcpu, struct vcpu_svm, vcpu);
337}
338
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500339static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
340{
341 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
342 mark_dirty(svm->vmcb, VMCB_AVIC);
343}
344
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -0500345static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
346{
347 struct vcpu_svm *svm = to_svm(vcpu);
348 u64 *entry = svm->avic_physical_id_cache;
349
350 if (!entry)
351 return false;
352
353 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
354}
355
Joerg Roedel384c6362010-11-30 18:03:56 +0100356static void recalc_intercepts(struct vcpu_svm *svm)
357{
358 struct vmcb_control_area *c, *h;
359 struct nested_state *g;
360
Joerg Roedel116a0a22010-12-03 11:45:49 +0100361 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
362
Joerg Roedel384c6362010-11-30 18:03:56 +0100363 if (!is_guest_mode(&svm->vcpu))
364 return;
365
366 c = &svm->vmcb->control;
367 h = &svm->nested.hsave->control;
368 g = &svm->nested;
369
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100370 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100371 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100372 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
373 c->intercept = h->intercept | g->intercept;
374}
375
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100376static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
377{
378 if (is_guest_mode(&svm->vcpu))
379 return svm->nested.hsave;
380 else
381 return svm->vmcb;
382}
383
384static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
385{
386 struct vmcb *vmcb = get_host_vmcb(svm);
387
388 vmcb->control.intercept_cr |= (1U << bit);
389
390 recalc_intercepts(svm);
391}
392
393static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
394{
395 struct vmcb *vmcb = get_host_vmcb(svm);
396
397 vmcb->control.intercept_cr &= ~(1U << bit);
398
399 recalc_intercepts(svm);
400}
401
402static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
403{
404 struct vmcb *vmcb = get_host_vmcb(svm);
405
406 return vmcb->control.intercept_cr & (1U << bit);
407}
408
Paolo Bonzini5315c712014-03-03 13:08:29 +0100409static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100410{
411 struct vmcb *vmcb = get_host_vmcb(svm);
412
Paolo Bonzini5315c712014-03-03 13:08:29 +0100413 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
414 | (1 << INTERCEPT_DR1_READ)
415 | (1 << INTERCEPT_DR2_READ)
416 | (1 << INTERCEPT_DR3_READ)
417 | (1 << INTERCEPT_DR4_READ)
418 | (1 << INTERCEPT_DR5_READ)
419 | (1 << INTERCEPT_DR6_READ)
420 | (1 << INTERCEPT_DR7_READ)
421 | (1 << INTERCEPT_DR0_WRITE)
422 | (1 << INTERCEPT_DR1_WRITE)
423 | (1 << INTERCEPT_DR2_WRITE)
424 | (1 << INTERCEPT_DR3_WRITE)
425 | (1 << INTERCEPT_DR4_WRITE)
426 | (1 << INTERCEPT_DR5_WRITE)
427 | (1 << INTERCEPT_DR6_WRITE)
428 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100429
430 recalc_intercepts(svm);
431}
432
Paolo Bonzini5315c712014-03-03 13:08:29 +0100433static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100434{
435 struct vmcb *vmcb = get_host_vmcb(svm);
436
Paolo Bonzini5315c712014-03-03 13:08:29 +0100437 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100438
439 recalc_intercepts(svm);
440}
441
Joerg Roedel18c918c2010-11-30 18:03:59 +0100442static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
443{
444 struct vmcb *vmcb = get_host_vmcb(svm);
445
446 vmcb->control.intercept_exceptions |= (1U << bit);
447
448 recalc_intercepts(svm);
449}
450
451static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
452{
453 struct vmcb *vmcb = get_host_vmcb(svm);
454
455 vmcb->control.intercept_exceptions &= ~(1U << bit);
456
457 recalc_intercepts(svm);
458}
459
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100460static inline void set_intercept(struct vcpu_svm *svm, int bit)
461{
462 struct vmcb *vmcb = get_host_vmcb(svm);
463
464 vmcb->control.intercept |= (1ULL << bit);
465
466 recalc_intercepts(svm);
467}
468
469static inline void clr_intercept(struct vcpu_svm *svm, int bit)
470{
471 struct vmcb *vmcb = get_host_vmcb(svm);
472
473 vmcb->control.intercept &= ~(1ULL << bit);
474
475 recalc_intercepts(svm);
476}
477
Joerg Roedel2af91942009-08-07 11:49:28 +0200478static inline void enable_gif(struct vcpu_svm *svm)
479{
480 svm->vcpu.arch.hflags |= HF_GIF_MASK;
481}
482
483static inline void disable_gif(struct vcpu_svm *svm)
484{
485 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
486}
487
488static inline bool gif_set(struct vcpu_svm *svm)
489{
490 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
491}
492
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493static unsigned long iopm_base;
494
495struct kvm_ldttss_desc {
496 u16 limit0;
497 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100498 unsigned base1:8, type:5, dpl:2, p:1;
499 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500 u32 base3;
501 u32 zero1;
502} __attribute__((packed));
503
504struct svm_cpu_data {
505 int cpu;
506
Avi Kivity5008fdf2007-04-02 13:05:50 +0300507 u64 asid_generation;
508 u32 max_asid;
509 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510 struct kvm_ldttss_desc *tss_desc;
511
512 struct page *save_area;
513};
514
515static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
516
517struct svm_init_data {
518 int cpu;
519 int r;
520};
521
Mathias Krause09941fb2012-08-30 01:30:20 +0200522static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800523
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200524#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525#define MSRS_RANGE_SIZE 2048
526#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
527
Joerg Roedel455716f2010-03-01 15:34:35 +0100528static u32 svm_msrpm_offset(u32 msr)
529{
530 u32 offset;
531 int i;
532
533 for (i = 0; i < NUM_MSR_MAPS; i++) {
534 if (msr < msrpm_ranges[i] ||
535 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
536 continue;
537
538 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
539 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
540
541 /* Now we have the u8 offset - but need the u32 offset */
542 return offset / 4;
543 }
544
545 /* MSR not in any range */
546 return MSR_INVALID;
547}
548
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549#define MAX_INST_SIZE 15
550
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551static inline void clgi(void)
552{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300553 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800554}
555
556static inline void stgi(void)
557{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300558 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559}
560
561static inline void invlpga(unsigned long addr, u32 asid)
562{
Joerg Roedele0231712010-02-24 18:59:10 +0100563 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564}
565
Joerg Roedel4b161842010-09-10 17:31:03 +0200566static int get_npt_level(void)
567{
568#ifdef CONFIG_X86_64
569 return PT64_ROOT_LEVEL;
570#else
571 return PT32E_ROOT_LEVEL;
572#endif
573}
574
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
576{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000577 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100578 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600579 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580
Alexander Graf9962d032008-11-25 20:17:02 +0100581 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100582 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583}
584
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585static int is_external_interrupt(u32 info)
586{
587 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
588 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
589}
590
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200591static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400592{
593 struct vcpu_svm *svm = to_svm(vcpu);
594 u32 ret = 0;
595
596 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200597 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
598 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400599}
600
601static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
602{
603 struct vcpu_svm *svm = to_svm(vcpu);
604
605 if (mask == 0)
606 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
607 else
608 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
609
610}
611
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
613{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400614 struct vcpu_svm *svm = to_svm(vcpu);
615
Bandan Dasf1047652015-06-11 02:05:33 -0400616 if (svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200617 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200618 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400619 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200620
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400621 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100622 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300623 EMULATE_DONE)
624 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625 return;
626 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300627 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
628 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
629 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300631 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400632 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633}
634
Jan Kiszka116a4752010-02-23 17:47:54 +0100635static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200636 bool has_error_code, u32 error_code,
637 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100638{
639 struct vcpu_svm *svm = to_svm(vcpu);
640
Joerg Roedele0231712010-02-24 18:59:10 +0100641 /*
642 * If we are within a nested VM we'd better #VMEXIT and let the guest
643 * handle the exception
644 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200645 if (!reinject &&
646 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100647 return;
648
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200649 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100650 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
651
652 /*
653 * For guest debugging where we have to reinject #BP if some
654 * INT3 is guest-owned:
655 * Emulate nRIP by moving RIP forward. Will fail if injection
656 * raises a fault that is not intercepted. Still better than
657 * failing in all cases.
658 */
659 skip_emulated_instruction(&svm->vcpu);
660 rip = kvm_rip_read(&svm->vcpu);
661 svm->int3_rip = rip + svm->vmcb->save.cs.base;
662 svm->int3_injected = rip - old_rip;
663 }
664
Jan Kiszka116a4752010-02-23 17:47:54 +0100665 svm->vmcb->control.event_inj = nr
666 | SVM_EVTINJ_VALID
667 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
668 | SVM_EVTINJ_TYPE_EXEPT;
669 svm->vmcb->control.event_inj_err = error_code;
670}
671
Joerg Roedel67ec6602010-05-17 14:43:35 +0200672static void svm_init_erratum_383(void)
673{
674 u32 low, high;
675 int err;
676 u64 val;
677
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100678 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200679 return;
680
681 /* Use _safe variants to not break nested virtualization */
682 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
683 if (err)
684 return;
685
686 val |= (1ULL << 47);
687
688 low = lower_32_bits(val);
689 high = upper_32_bits(val);
690
691 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
692
693 erratum_383_found = true;
694}
695
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500696static void svm_init_osvw(struct kvm_vcpu *vcpu)
697{
698 /*
699 * Guests should see errata 400 and 415 as fixed (assuming that
700 * HLT and IO instructions are intercepted).
701 */
702 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
703 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
704
705 /*
706 * By increasing VCPU's osvw.length to 3 we are telling the guest that
707 * all osvw.status bits inside that length, including bit 0 (which is
708 * reserved for erratum 298), are valid. However, if host processor's
709 * osvw_len is 0 then osvw_status[0] carries no information. We need to
710 * be conservative here and therefore we tell the guest that erratum 298
711 * is present (because we really don't know).
712 */
713 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
714 vcpu->arch.osvw.status |= 1;
715}
716
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717static int has_svm(void)
718{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200719 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720
Eduardo Habkost63d11422008-11-17 19:03:20 -0200721 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800722 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723 return 0;
724 }
725
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726 return 1;
727}
728
Radim Krčmář13a34e02014-08-28 15:13:03 +0200729static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100731 /* Make sure we clean up behind us */
732 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
733 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
734
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200735 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100736
737 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800738}
739
Radim Krčmář13a34e02014-08-28 15:13:03 +0200740static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741{
742
Tejun Heo0fe1e002009-10-29 22:34:14 +0900743 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745 struct desc_struct *gdt;
746 int me = raw_smp_processor_id();
747
Alexander Graf10474ae2009-09-15 11:37:46 +0200748 rdmsrl(MSR_EFER, efer);
749 if (efer & EFER_SVME)
750 return -EBUSY;
751
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200753 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200754 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900756 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900757 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200758 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200759 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 }
761
Tejun Heo0fe1e002009-10-29 22:34:14 +0900762 sd->asid_generation = 1;
763 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
764 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765
Thomas Garnier45fc8752017-03-14 10:05:08 -0700766 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900767 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768
Alexander Graf9962d032008-11-25 20:17:02 +0100769 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770
Linus Torvaldsd0316552009-12-14 09:58:24 -0800771 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200772
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100773 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
774 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500775 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100776 }
777
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500778
779 /*
780 * Get OSVW bits.
781 *
782 * Note that it is possible to have a system with mixed processor
783 * revisions and therefore different OSVW bits. If bits are not the same
784 * on different processors then choose the worst case (i.e. if erratum
785 * is present on one processor and not on another then assume that the
786 * erratum is present everywhere).
787 */
788 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
789 uint64_t len, status = 0;
790 int err;
791
792 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
793 if (!err)
794 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
795 &err);
796
797 if (err)
798 osvw_status = osvw_len = 0;
799 else {
800 if (len < osvw_len)
801 osvw_len = len;
802 osvw_status |= status;
803 osvw_status &= (1ULL << osvw_len) - 1;
804 }
805 } else
806 osvw_status = osvw_len = 0;
807
Joerg Roedel67ec6602010-05-17 14:43:35 +0200808 svm_init_erratum_383();
809
Joerg Roedel1018faa2012-02-29 14:57:32 +0100810 amd_pmu_enable_virt();
811
Alexander Graf10474ae2009-09-15 11:37:46 +0200812 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813}
814
Joerg Roedel0da1db752008-07-02 16:02:11 +0200815static void svm_cpu_uninit(int cpu)
816{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900817 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200818
Tejun Heo0fe1e002009-10-29 22:34:14 +0900819 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200820 return;
821
822 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900823 __free_page(sd->save_area);
824 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200825}
826
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827static int svm_cpu_init(int cpu)
828{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900829 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 int r;
831
Tejun Heo0fe1e002009-10-29 22:34:14 +0900832 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
833 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900835 sd->cpu = cpu;
836 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900838 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 goto err_1;
840
Tejun Heo0fe1e002009-10-29 22:34:14 +0900841 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842
843 return 0;
844
845err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900846 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847 return r;
848
849}
850
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100851static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852{
853 int i;
854
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100855 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
856 if (direct_access_msrs[i].index == index)
857 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100859 return false;
860}
861
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862static void set_msr_interception(u32 *msrpm, unsigned msr,
863 int read, int write)
864{
Joerg Roedel455716f2010-03-01 15:34:35 +0100865 u8 bit_read, bit_write;
866 unsigned long tmp;
867 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100869 /*
870 * If this warning triggers extend the direct_access_msrs list at the
871 * beginning of the file
872 */
873 WARN_ON(!valid_msr_intercept(msr));
874
Joerg Roedel455716f2010-03-01 15:34:35 +0100875 offset = svm_msrpm_offset(msr);
876 bit_read = 2 * (msr & 0x0f);
877 bit_write = 2 * (msr & 0x0f) + 1;
878 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879
Joerg Roedel455716f2010-03-01 15:34:35 +0100880 BUG_ON(offset == MSR_INVALID);
881
882 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
883 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
884
885 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886}
887
Joerg Roedelf65c2292008-02-13 18:58:46 +0100888static void svm_vcpu_init_msrpm(u32 *msrpm)
889{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100890 int i;
891
Joerg Roedelf65c2292008-02-13 18:58:46 +0100892 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
893
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100894 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
895 if (!direct_access_msrs[i].always)
896 continue;
897
898 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
899 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100900}
901
Joerg Roedel323c3d82010-03-01 15:34:37 +0100902static void add_msr_offset(u32 offset)
903{
904 int i;
905
906 for (i = 0; i < MSRPM_OFFSETS; ++i) {
907
908 /* Offset already in list? */
909 if (msrpm_offsets[i] == offset)
910 return;
911
912 /* Slot used by another offset? */
913 if (msrpm_offsets[i] != MSR_INVALID)
914 continue;
915
916 /* Add offset to list */
917 msrpm_offsets[i] = offset;
918
919 return;
920 }
921
922 /*
923 * If this BUG triggers the msrpm_offsets table has an overflow. Just
924 * increase MSRPM_OFFSETS in this case.
925 */
926 BUG();
927}
928
929static void init_msrpm_offsets(void)
930{
931 int i;
932
933 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
934
935 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
936 u32 offset;
937
938 offset = svm_msrpm_offset(direct_access_msrs[i].index);
939 BUG_ON(offset == MSR_INVALID);
940
941 add_msr_offset(offset);
942 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943}
944
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100945static void svm_enable_lbrv(struct vcpu_svm *svm)
946{
947 u32 *msrpm = svm->msrpm;
948
949 svm->vmcb->control.lbr_ctl = 1;
950 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
951 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
952 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
953 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
954}
955
956static void svm_disable_lbrv(struct vcpu_svm *svm)
957{
958 u32 *msrpm = svm->msrpm;
959
960 svm->vmcb->control.lbr_ctl = 0;
961 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
962 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
963 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
964 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
965}
966
Ladi Prosek4aebd0e2017-06-21 09:06:57 +0200967static void disable_nmi_singlestep(struct vcpu_svm *svm)
968{
969 svm->nmi_singlestep = false;
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200970 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
971 /* Clear our flags if they were not set by the guest */
972 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
973 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
974 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
975 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
976 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +0200977}
978
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -0500979/* Note:
980 * This hash table is used to map VM_ID to a struct kvm_arch,
981 * when handling AMD IOMMU GALOG notification to schedule in
982 * a particular vCPU.
983 */
984#define SVM_VM_DATA_HASH_BITS 8
David Hildenbrand681bcea2017-01-24 22:21:16 +0100985static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
986static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -0500987
988/* Note:
989 * This function is called from IOMMU driver to notify
990 * SVM to schedule in a particular vCPU of a particular VM.
991 */
992static int avic_ga_log_notifier(u32 ga_tag)
993{
994 unsigned long flags;
995 struct kvm_arch *ka = NULL;
996 struct kvm_vcpu *vcpu = NULL;
997 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
998 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
999
1000 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
1001
1002 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1003 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1004 struct kvm *kvm = container_of(ka, struct kvm, arch);
1005 struct kvm_arch *vm_data = &kvm->arch;
1006
1007 if (vm_data->avic_vm_id != vm_id)
1008 continue;
1009 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
1010 break;
1011 }
1012 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1013
1014 if (!vcpu)
1015 return 0;
1016
1017 /* Note:
1018 * At this point, the IOMMU should have already set the pending
1019 * bit in the vAPIC backing page. So, we just need to schedule
1020 * in the vcpu.
1021 */
1022 if (vcpu->mode == OUTSIDE_GUEST_MODE)
1023 kvm_vcpu_wake_up(vcpu);
1024
1025 return 0;
1026}
1027
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028static __init int svm_hardware_setup(void)
1029{
1030 int cpu;
1031 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001032 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033 int r;
1034
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
1036
1037 if (!iopm_pages)
1038 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +03001039
1040 iopm_va = page_address(iopm_pages);
1041 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
1043
Joerg Roedel323c3d82010-03-01 15:34:37 +01001044 init_msrpm_offsets();
1045
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001046 if (boot_cpu_has(X86_FEATURE_NX))
1047 kvm_enable_efer_bits(EFER_NX);
1048
Alexander Graf1b2fd702009-02-02 16:23:51 +01001049 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
1050 kvm_enable_efer_bits(EFER_FFXSR);
1051
Joerg Roedel92a1f122011-03-25 09:44:51 +01001052 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +01001053 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +08001054 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1055 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +01001056 }
1057
Alexander Graf236de052008-11-25 20:17:10 +01001058 if (nested) {
1059 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +02001060 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +01001061 }
1062
Zachary Amsden3230bb42009-09-29 11:38:37 -10001063 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 r = svm_cpu_init(cpu);
1065 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +01001066 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +01001068
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001069 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001070 npt_enabled = false;
1071
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001072 if (npt_enabled && !npt) {
1073 printk(KERN_INFO "kvm: Nested Paging disabled\n");
1074 npt_enabled = false;
1075 }
1076
Joerg Roedel18552672008-02-07 13:47:41 +01001077 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001078 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +01001079 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +02001080 } else
1081 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001082
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001083 if (avic) {
1084 if (!npt_enabled ||
1085 !boot_cpu_has(X86_FEATURE_AVIC) ||
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001086 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001087 avic = false;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001088 } else {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001089 pr_info("AVIC enabled\n");
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001090
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001091 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1092 }
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001093 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001094
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095 return 0;
1096
Joerg Roedelf65c2292008-02-13 18:58:46 +01001097err:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1099 iopm_base = 0;
1100 return r;
1101}
1102
1103static __exit void svm_hardware_unsetup(void)
1104{
Joerg Roedel0da1db752008-07-02 16:02:11 +02001105 int cpu;
1106
Zachary Amsden3230bb42009-09-29 11:38:37 -10001107 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +02001108 svm_cpu_uninit(cpu);
1109
Avi Kivity6aa8b732006-12-10 02:21:36 -08001110 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001111 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112}
1113
1114static void init_seg(struct vmcb_seg *seg)
1115{
1116 seg->selector = 0;
1117 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001118 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 seg->limit = 0xffff;
1120 seg->base = 0;
1121}
1122
1123static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1124{
1125 seg->selector = 0;
1126 seg->attrib = SVM_SELECTOR_P_MASK | type;
1127 seg->limit = 0xffff;
1128 seg->base = 0;
1129}
1130
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001131static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1132{
1133 struct vcpu_svm *svm = to_svm(vcpu);
1134 u64 g_tsc_offset = 0;
1135
Joerg Roedel20307532010-11-29 17:51:48 +01001136 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001137 g_tsc_offset = svm->vmcb->control.tsc_offset -
1138 svm->nested.hsave->control.tsc_offset;
1139 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001140 } else
1141 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1142 svm->vmcb->control.tsc_offset,
1143 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001144
1145 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001146
1147 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001148}
1149
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001150static void avic_init_vmcb(struct vcpu_svm *svm)
1151{
1152 struct vmcb *vmcb = svm->vmcb;
1153 struct kvm_arch *vm_data = &svm->vcpu.kvm->arch;
1154 phys_addr_t bpa = page_to_phys(svm->avic_backing_page);
1155 phys_addr_t lpa = page_to_phys(vm_data->avic_logical_id_table_page);
1156 phys_addr_t ppa = page_to_phys(vm_data->avic_physical_id_table_page);
1157
1158 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1159 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1160 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1161 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1162 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
1163 svm->vcpu.arch.apicv_active = true;
1164}
1165
Paolo Bonzini56908912015-10-19 11:30:19 +02001166static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167{
Joerg Roedele6101a92008-02-13 18:58:45 +01001168 struct vmcb_control_area *control = &svm->vmcb->control;
1169 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001171 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001172
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001173 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1174 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1175 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1176 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1177 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1178 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001179 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1180 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181
Paolo Bonzini5315c712014-03-03 13:08:29 +01001182 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183
Joerg Roedel18c918c2010-11-30 18:03:59 +01001184 set_exception_intercept(svm, PF_VECTOR);
1185 set_exception_intercept(svm, UD_VECTOR);
1186 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001187 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001188 set_exception_intercept(svm, DB_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001190 set_intercept(svm, INTERCEPT_INTR);
1191 set_intercept(svm, INTERCEPT_NMI);
1192 set_intercept(svm, INTERCEPT_SMI);
1193 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001194 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001195 set_intercept(svm, INTERCEPT_CPUID);
1196 set_intercept(svm, INTERCEPT_INVD);
1197 set_intercept(svm, INTERCEPT_HLT);
1198 set_intercept(svm, INTERCEPT_INVLPG);
1199 set_intercept(svm, INTERCEPT_INVLPGA);
1200 set_intercept(svm, INTERCEPT_IOIO_PROT);
1201 set_intercept(svm, INTERCEPT_MSR_PROT);
1202 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1203 set_intercept(svm, INTERCEPT_SHUTDOWN);
1204 set_intercept(svm, INTERCEPT_VMRUN);
1205 set_intercept(svm, INTERCEPT_VMMCALL);
1206 set_intercept(svm, INTERCEPT_VMLOAD);
1207 set_intercept(svm, INTERCEPT_VMSAVE);
1208 set_intercept(svm, INTERCEPT_STGI);
1209 set_intercept(svm, INTERCEPT_CLGI);
1210 set_intercept(svm, INTERCEPT_SKINIT);
1211 set_intercept(svm, INTERCEPT_WBINVD);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001212 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001214 if (!kvm_mwait_in_guest()) {
1215 set_intercept(svm, INTERCEPT_MONITOR);
1216 set_intercept(svm, INTERCEPT_MWAIT);
1217 }
1218
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001220 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221 control->int_ctl = V_INTR_MASKING_MASK;
1222
1223 init_seg(&save->es);
1224 init_seg(&save->ss);
1225 init_seg(&save->ds);
1226 init_seg(&save->fs);
1227 init_seg(&save->gs);
1228
1229 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001230 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231 /* Executable/Readable Code Segment */
1232 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1233 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1234 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235
1236 save->gdtr.limit = 0xffff;
1237 save->idtr.limit = 0xffff;
1238
1239 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1240 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1241
Paolo Bonzini56908912015-10-19 11:30:19 +02001242 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001243 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001244 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001246 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001247
Joerg Roedele0231712010-02-24 18:59:10 +01001248 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001249 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001250 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001252 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001253 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001254
Rusty Russell66aee912007-07-17 23:34:16 +10001255 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001257
1258 if (npt_enabled) {
1259 /* Setup VMCB for Nested Paging */
1260 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001261 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001262 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001263 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1264 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001265 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001266 save->cr3 = 0;
1267 save->cr4 = 0;
1268 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001269 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001270
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001271 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001272 svm->vcpu.arch.hflags = 0;
1273
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001274 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001275 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001276 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001277 }
1278
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001279 if (avic)
1280 avic_init_vmcb(svm);
1281
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001282 mark_all_dirty(svm->vmcb);
1283
Joerg Roedel2af91942009-08-07 11:49:28 +02001284 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001285
1286}
1287
Dan Carpenterd3e7dec2017-05-18 10:38:53 +03001288static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1289 unsigned int index)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001290{
1291 u64 *avic_physical_id_table;
1292 struct kvm_arch *vm_data = &vcpu->kvm->arch;
1293
1294 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1295 return NULL;
1296
1297 avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page);
1298
1299 return &avic_physical_id_table[index];
1300}
1301
1302/**
1303 * Note:
1304 * AVIC hardware walks the nested page table to check permissions,
1305 * but does not use the SPA address specified in the leaf page
1306 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1307 * field of the VMCB. Therefore, we set up the
1308 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1309 */
1310static int avic_init_access_page(struct kvm_vcpu *vcpu)
1311{
1312 struct kvm *kvm = vcpu->kvm;
1313 int ret;
1314
1315 if (kvm->arch.apic_access_page_done)
1316 return 0;
1317
1318 ret = x86_set_memory_region(kvm,
1319 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1320 APIC_DEFAULT_PHYS_BASE,
1321 PAGE_SIZE);
1322 if (ret)
1323 return ret;
1324
1325 kvm->arch.apic_access_page_done = true;
1326 return 0;
1327}
1328
1329static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1330{
1331 int ret;
1332 u64 *entry, new_entry;
1333 int id = vcpu->vcpu_id;
1334 struct vcpu_svm *svm = to_svm(vcpu);
1335
1336 ret = avic_init_access_page(vcpu);
1337 if (ret)
1338 return ret;
1339
1340 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1341 return -EINVAL;
1342
1343 if (!svm->vcpu.arch.apic->regs)
1344 return -EINVAL;
1345
1346 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1347
1348 /* Setting AVIC backing page address in the phy APIC ID table */
1349 entry = avic_get_physical_id_entry(vcpu, id);
1350 if (!entry)
1351 return -EINVAL;
1352
1353 new_entry = READ_ONCE(*entry);
1354 new_entry = (page_to_phys(svm->avic_backing_page) &
1355 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1356 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK;
1357 WRITE_ONCE(*entry, new_entry);
1358
1359 svm->avic_physical_id_cache = entry;
1360
1361 return 0;
1362}
1363
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -05001364static inline int avic_get_next_vm_id(void)
1365{
1366 int id;
1367
1368 spin_lock(&avic_vm_id_lock);
1369
1370 /* AVIC VM ID is one-based. */
1371 id = find_next_zero_bit(avic_vm_id_bitmap, AVIC_VM_ID_NR, 1);
1372 if (id <= AVIC_VM_ID_MASK)
1373 __set_bit(id, avic_vm_id_bitmap);
1374 else
1375 id = -EAGAIN;
1376
1377 spin_unlock(&avic_vm_id_lock);
1378 return id;
1379}
1380
1381static inline int avic_free_vm_id(int id)
1382{
1383 if (id <= 0 || id > AVIC_VM_ID_MASK)
1384 return -EINVAL;
1385
1386 spin_lock(&avic_vm_id_lock);
1387 __clear_bit(id, avic_vm_id_bitmap);
1388 spin_unlock(&avic_vm_id_lock);
1389 return 0;
1390}
1391
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001392static void avic_vm_destroy(struct kvm *kvm)
1393{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001394 unsigned long flags;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001395 struct kvm_arch *vm_data = &kvm->arch;
1396
Dmitry Vyukov3863dff2017-01-24 14:06:48 +01001397 if (!avic)
1398 return;
1399
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -05001400 avic_free_vm_id(vm_data->avic_vm_id);
1401
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001402 if (vm_data->avic_logical_id_table_page)
1403 __free_page(vm_data->avic_logical_id_table_page);
1404 if (vm_data->avic_physical_id_table_page)
1405 __free_page(vm_data->avic_physical_id_table_page);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001406
1407 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1408 hash_del(&vm_data->hnode);
1409 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001410}
1411
1412static int avic_vm_init(struct kvm *kvm)
1413{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001414 unsigned long flags;
Colin Ian Kingadad0d02016-09-19 07:11:59 +01001415 int vm_id, err = -ENOMEM;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001416 struct kvm_arch *vm_data = &kvm->arch;
1417 struct page *p_page;
1418 struct page *l_page;
1419
1420 if (!avic)
1421 return 0;
1422
Colin Ian Kingadad0d02016-09-19 07:11:59 +01001423 vm_id = avic_get_next_vm_id();
1424 if (vm_id < 0)
1425 return vm_id;
1426 vm_data->avic_vm_id = (u32)vm_id;
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -05001427
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001428 /* Allocating physical APIC ID table (4KB) */
1429 p_page = alloc_page(GFP_KERNEL);
1430 if (!p_page)
1431 goto free_avic;
1432
1433 vm_data->avic_physical_id_table_page = p_page;
1434 clear_page(page_address(p_page));
1435
1436 /* Allocating logical APIC ID table (4KB) */
1437 l_page = alloc_page(GFP_KERNEL);
1438 if (!l_page)
1439 goto free_avic;
1440
1441 vm_data->avic_logical_id_table_page = l_page;
1442 clear_page(page_address(l_page));
1443
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001444 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1445 hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id);
1446 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1447
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001448 return 0;
1449
1450free_avic:
1451 avic_vm_destroy(kvm);
1452 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453}
1454
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001455static inline int
1456avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001457{
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001458 int ret = 0;
1459 unsigned long flags;
1460 struct amd_svm_iommu_ir *ir;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001461 struct vcpu_svm *svm = to_svm(vcpu);
1462
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001463 if (!kvm_arch_has_assigned_device(vcpu->kvm))
1464 return 0;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001465
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001466 /*
1467 * Here, we go through the per-vcpu ir_list to update all existing
1468 * interrupt remapping table entry targeting this vcpu.
1469 */
1470 spin_lock_irqsave(&svm->ir_list_lock, flags);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001471
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001472 if (list_empty(&svm->ir_list))
1473 goto out;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001474
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001475 list_for_each_entry(ir, &svm->ir_list, node) {
1476 ret = amd_iommu_update_ga(cpu, r, ir->data);
1477 if (ret)
1478 break;
1479 }
1480out:
1481 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
1482 return ret;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001483}
1484
1485static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1486{
1487 u64 entry;
1488 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05001489 int h_physical_id = kvm_cpu_get_apicid(cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001490 struct vcpu_svm *svm = to_svm(vcpu);
1491
1492 if (!kvm_vcpu_apicv_active(vcpu))
1493 return;
1494
1495 if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
1496 return;
1497
1498 entry = READ_ONCE(*(svm->avic_physical_id_cache));
1499 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
1500
1501 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
1502 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
1503
1504 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1505 if (svm->avic_is_running)
1506 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1507
1508 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001509 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
1510 svm->avic_is_running);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001511}
1512
1513static void avic_vcpu_put(struct kvm_vcpu *vcpu)
1514{
1515 u64 entry;
1516 struct vcpu_svm *svm = to_svm(vcpu);
1517
1518 if (!kvm_vcpu_apicv_active(vcpu))
1519 return;
1520
1521 entry = READ_ONCE(*(svm->avic_physical_id_cache));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001522 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
1523 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
1524
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001525 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1526 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527}
1528
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001529/**
1530 * This function is called during VCPU halt/unhalt.
1531 */
1532static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
1533{
1534 struct vcpu_svm *svm = to_svm(vcpu);
1535
1536 svm->avic_is_running = is_run;
1537 if (is_run)
1538 avic_vcpu_load(vcpu, vcpu->cpu);
1539 else
1540 avic_vcpu_put(vcpu);
1541}
1542
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001543static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001544{
1545 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001546 u32 dummy;
1547 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001548
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001549 if (!init_event) {
1550 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1551 MSR_IA32_APICBASE_ENABLE;
1552 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1553 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1554 }
Paolo Bonzini56908912015-10-19 11:30:19 +02001555 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001556
Julian Stecklina66f7b722012-12-05 15:26:19 +01001557 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy);
1558 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001559
1560 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1561 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001562}
1563
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001564static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001566 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001568 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001569 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001570 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001571 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572
Rusty Russellc16f8622007-07-30 21:12:19 +10001573 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001574 if (!svm) {
1575 err = -ENOMEM;
1576 goto out;
1577 }
1578
1579 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1580 if (err)
1581 goto free_svm;
1582
Joerg Roedelf65c2292008-02-13 18:58:46 +01001583 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001584 page = alloc_page(GFP_KERNEL);
1585 if (!page)
1586 goto uninit;
1587
Joerg Roedelf65c2292008-02-13 18:58:46 +01001588 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1589 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001590 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001591
1592 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1593 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001594 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001595
Alexander Grafb286d5d2008-11-25 20:17:05 +01001596 hsave_page = alloc_page(GFP_KERNEL);
1597 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001598 goto free_page3;
1599
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001600 if (avic) {
1601 err = avic_init_backing_page(&svm->vcpu);
1602 if (err)
1603 goto free_page4;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001604
1605 INIT_LIST_HEAD(&svm->ir_list);
1606 spin_lock_init(&svm->ir_list_lock);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001607 }
1608
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001609 /* We initialize this flag to true to make sure that the is_running
1610 * bit would be set the first time the vcpu is loaded.
1611 */
1612 svm->avic_is_running = true;
1613
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001614 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001615
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001616 svm->msrpm = page_address(msrpm_pages);
1617 svm_vcpu_init_msrpm(svm->msrpm);
1618
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001619 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001620 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001621
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001622 svm->vmcb = page_address(page);
1623 clear_page(svm->vmcb);
1624 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1625 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02001626 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001628 svm_init_osvw(&svm->vcpu);
1629
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001630 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001631
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001632free_page4:
1633 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001634free_page3:
1635 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1636free_page2:
1637 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1638free_page1:
1639 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001640uninit:
1641 kvm_vcpu_uninit(&svm->vcpu);
1642free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001643 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001644out:
1645 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646}
1647
1648static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1649{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001650 struct vcpu_svm *svm = to_svm(vcpu);
1651
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001652 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001653 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001654 __free_page(virt_to_page(svm->nested.hsave));
1655 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001656 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001657 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658}
1659
Avi Kivity15ad7142007-07-11 18:17:21 +03001660static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001662 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001663 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001664
Avi Kivity0cc50642007-03-25 12:07:27 +02001665 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001666 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001667 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001668 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001669
Avi Kivity82ca2d12010-10-21 12:20:34 +02001670#ifdef CONFIG_X86_64
1671 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1672#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001673 savesegment(fs, svm->host.fs);
1674 savesegment(gs, svm->host.gs);
1675 svm->host.ldt = kvm_read_ldt();
1676
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001677 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001678 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001679
Haozhong Zhangad7218832015-10-20 15:39:02 +08001680 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1681 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1682 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1683 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1684 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1685 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001686 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01001687 /* This assumes that the kernel never uses MSR_TSC_AUX */
1688 if (static_cpu_has(X86_FEATURE_RDTSCP))
1689 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001690
1691 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692}
1693
1694static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1695{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001696 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001697 int i;
1698
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001699 avic_vcpu_put(vcpu);
1700
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001701 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001702 kvm_load_ldt(svm->host.ldt);
1703#ifdef CONFIG_X86_64
1704 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07001705 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001706 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001707#else
Avi Kivity831ca602011-03-08 16:09:51 +02001708#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001709 loadsegment(gs, svm->host.gs);
1710#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001711#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001712 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001713 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714}
1715
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001716static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
1717{
1718 avic_set_running(vcpu, false);
1719}
1720
1721static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
1722{
1723 avic_set_running(vcpu, true);
1724}
1725
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1727{
Ladi Prosek9b611742017-06-21 09:06:59 +02001728 struct vcpu_svm *svm = to_svm(vcpu);
1729 unsigned long rflags = svm->vmcb->save.rflags;
1730
1731 if (svm->nmi_singlestep) {
1732 /* Hide our flags if they were not set by the guest */
1733 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1734 rflags &= ~X86_EFLAGS_TF;
1735 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1736 rflags &= ~X86_EFLAGS_RF;
1737 }
1738 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739}
1740
1741static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1742{
Ladi Prosek9b611742017-06-21 09:06:59 +02001743 if (to_svm(vcpu)->nmi_singlestep)
1744 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1745
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001746 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02001747 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001748 * (caused by either a task switch or an inter-privilege IRET),
1749 * so we do not need to update the CPL here.
1750 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001751 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001752}
1753
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08001754static u32 svm_get_pkru(struct kvm_vcpu *vcpu)
1755{
1756 return 0;
1757}
1758
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001759static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1760{
1761 switch (reg) {
1762 case VCPU_EXREG_PDPTR:
1763 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001764 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001765 break;
1766 default:
1767 BUG();
1768 }
1769}
1770
Alexander Graff0b85052008-11-25 20:17:01 +01001771static void svm_set_vintr(struct vcpu_svm *svm)
1772{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001773 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001774}
1775
1776static void svm_clear_vintr(struct vcpu_svm *svm)
1777{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001778 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001779}
1780
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1782{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001783 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784
1785 switch (seg) {
1786 case VCPU_SREG_CS: return &save->cs;
1787 case VCPU_SREG_DS: return &save->ds;
1788 case VCPU_SREG_ES: return &save->es;
1789 case VCPU_SREG_FS: return &save->fs;
1790 case VCPU_SREG_GS: return &save->gs;
1791 case VCPU_SREG_SS: return &save->ss;
1792 case VCPU_SREG_TR: return &save->tr;
1793 case VCPU_SREG_LDTR: return &save->ldtr;
1794 }
1795 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001796 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001797}
1798
1799static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1800{
1801 struct vmcb_seg *s = svm_seg(vcpu, seg);
1802
1803 return s->base;
1804}
1805
1806static void svm_get_segment(struct kvm_vcpu *vcpu,
1807 struct kvm_segment *var, int seg)
1808{
1809 struct vmcb_seg *s = svm_seg(vcpu, seg);
1810
1811 var->base = s->base;
1812 var->limit = s->limit;
1813 var->selector = s->selector;
1814 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1815 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1816 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1817 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1818 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1819 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1820 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301821
1822 /*
1823 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1824 * However, the SVM spec states that the G bit is not observed by the
1825 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1826 * So let's synthesize a legal G bit for all segments, this helps
1827 * running KVM nested. It also helps cross-vendor migration, because
1828 * Intel's vmentry has a check on the 'G' bit.
1829 */
1830 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001831
Joerg Roedele0231712010-02-24 18:59:10 +01001832 /*
1833 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001834 * for cross vendor migration purposes by "not present"
1835 */
Gioh Kim8eae9572017-05-30 15:24:45 +02001836 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02001837
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001838 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001839 case VCPU_SREG_TR:
1840 /*
1841 * Work around a bug where the busy flag in the tr selector
1842 * isn't exposed
1843 */
Amit Shahc0d09822008-10-27 09:04:18 +00001844 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001845 break;
1846 case VCPU_SREG_DS:
1847 case VCPU_SREG_ES:
1848 case VCPU_SREG_FS:
1849 case VCPU_SREG_GS:
1850 /*
1851 * The accessed bit must always be set in the segment
1852 * descriptor cache, although it can be cleared in the
1853 * descriptor, the cached bit always remains at 1. Since
1854 * Intel has a check on this, set it here to support
1855 * cross-vendor migration.
1856 */
1857 if (!var->unusable)
1858 var->type |= 0x1;
1859 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001860 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001861 /*
1862 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001863 * descriptor is left as 1, although the whole segment has
1864 * been made unusable. Clear it here to pass an Intel VMX
1865 * entry check when cross vendor migrating.
1866 */
1867 if (var->unusable)
1868 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02001869 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02001870 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02001871 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001872 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873}
1874
Izik Eidus2e4d2652008-03-24 19:38:34 +02001875static int svm_get_cpl(struct kvm_vcpu *vcpu)
1876{
1877 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1878
1879 return save->cpl;
1880}
1881
Gleb Natapov89a27f42010-02-16 10:51:48 +02001882static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001884 struct vcpu_svm *svm = to_svm(vcpu);
1885
Gleb Natapov89a27f42010-02-16 10:51:48 +02001886 dt->size = svm->vmcb->save.idtr.limit;
1887 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001888}
1889
Gleb Natapov89a27f42010-02-16 10:51:48 +02001890static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001892 struct vcpu_svm *svm = to_svm(vcpu);
1893
Gleb Natapov89a27f42010-02-16 10:51:48 +02001894 svm->vmcb->save.idtr.limit = dt->size;
1895 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001896 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001897}
1898
Gleb Natapov89a27f42010-02-16 10:51:48 +02001899static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001901 struct vcpu_svm *svm = to_svm(vcpu);
1902
Gleb Natapov89a27f42010-02-16 10:51:48 +02001903 dt->size = svm->vmcb->save.gdtr.limit;
1904 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905}
1906
Gleb Natapov89a27f42010-02-16 10:51:48 +02001907static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001909 struct vcpu_svm *svm = to_svm(vcpu);
1910
Gleb Natapov89a27f42010-02-16 10:51:48 +02001911 svm->vmcb->save.gdtr.limit = dt->size;
1912 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001913 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001914}
1915
Avi Kivitye8467fd2009-12-29 18:43:06 +02001916static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1917{
1918}
1919
Avi Kivityaff48ba2010-12-05 18:56:11 +02001920static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1921{
1922}
1923
Anthony Liguori25c4c272007-04-27 09:29:21 +03001924static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001925{
1926}
1927
Avi Kivityd2251572010-01-06 10:55:27 +02001928static void update_cr0_intercept(struct vcpu_svm *svm)
1929{
1930 ulong gcr0 = svm->vcpu.arch.cr0;
1931 u64 *hcr0 = &svm->vmcb->save.cr0;
1932
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001933 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1934 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
Avi Kivityd2251572010-01-06 10:55:27 +02001935
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001936 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001937
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001938 if (gcr0 == *hcr0) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001939 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1940 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001941 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001942 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1943 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001944 }
1945}
1946
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1948{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001949 struct vcpu_svm *svm = to_svm(vcpu);
1950
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001951#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001952 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001953 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001954 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001955 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956 }
1957
Mike Dayd77c26f2007-10-08 09:02:08 -04001958 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001959 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001960 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 }
1962 }
1963#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001964 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001965
1966 if (!npt_enabled)
1967 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001968
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02001969 /*
1970 * re-enable caching here because the QEMU bios
1971 * does not do it - this results in some delay at
1972 * reboot
1973 */
1974 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
1975 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001976 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001977 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001978 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001979}
1980
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001981static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001983 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001984 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1985
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001986 if (cr4 & X86_CR4_VMXE)
1987 return 1;
1988
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001989 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001990 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001991
Joerg Roedelec077262008-04-09 14:15:28 +02001992 vcpu->arch.cr4 = cr4;
1993 if (!npt_enabled)
1994 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001995 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001996 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001997 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001998 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001999}
2000
2001static void svm_set_segment(struct kvm_vcpu *vcpu,
2002 struct kvm_segment *var, int seg)
2003{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002004 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002005 struct vmcb_seg *s = svm_seg(vcpu, seg);
2006
2007 s->base = var->base;
2008 s->limit = var->limit;
2009 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02002010 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2011 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2012 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2013 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2014 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2015 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2016 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2017 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002018
2019 /*
2020 * This is always accurate, except if SYSRET returned to a segment
2021 * with SS.DPL != 3. Intel does not have this quirk, and always
2022 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2023 * would entail passing the CPL to userspace and back.
2024 */
2025 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02002026 /* This is symmetric with svm_get_segment() */
2027 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028
Joerg Roedel060d0c92010-12-03 11:45:57 +01002029 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002030}
2031
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01002032static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002033{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002034 struct vcpu_svm *svm = to_svm(vcpu);
2035
Joerg Roedel18c918c2010-11-30 18:03:59 +01002036 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03002037
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002038 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002039 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01002040 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002041 } else
2042 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03002043}
2044
Tejun Heo0fe1e002009-10-29 22:34:14 +09002045static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002046{
Tejun Heo0fe1e002009-10-29 22:34:14 +09002047 if (sd->next_asid > sd->max_asid) {
2048 ++sd->asid_generation;
2049 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002050 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051 }
2052
Tejun Heo0fe1e002009-10-29 22:34:14 +09002053 svm->asid_generation = sd->asid_generation;
2054 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01002055
2056 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002057}
2058
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002059static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2060{
2061 return to_svm(vcpu)->vmcb->save.dr6;
2062}
2063
2064static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2065{
2066 struct vcpu_svm *svm = to_svm(vcpu);
2067
2068 svm->vmcb->save.dr6 = value;
2069 mark_dirty(svm->vmcb, VMCB_DR);
2070}
2071
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002072static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2073{
2074 struct vcpu_svm *svm = to_svm(vcpu);
2075
2076 get_debugreg(vcpu->arch.db[0], 0);
2077 get_debugreg(vcpu->arch.db[1], 1);
2078 get_debugreg(vcpu->arch.db[2], 2);
2079 get_debugreg(vcpu->arch.db[3], 3);
2080 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2081 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2082
2083 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2084 set_dr_intercepts(svm);
2085}
2086
Gleb Natapov020df072010-04-13 10:05:23 +03002087static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002089 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002090
Gleb Natapov020df072010-04-13 10:05:23 +03002091 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01002092 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093}
2094
Avi Kivity851ba692009-08-24 11:10:17 +03002095static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096{
Gleb Natapov631bc482010-10-14 11:22:52 +02002097 u64 fault_address = svm->vmcb->control.exit_info_2;
Tom Lendacky14727752016-11-23 12:01:38 -05002098 u64 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02002099 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100
Gleb Natapov631bc482010-10-14 11:22:52 +02002101 switch (svm->apf_reason) {
2102 default:
2103 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002104
Gleb Natapov631bc482010-10-14 11:22:52 +02002105 trace_kvm_page_fault(fault_address, error_code);
2106 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
2107 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01002108 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
2109 svm->vmcb->control.insn_bytes,
2110 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02002111 break;
2112 case KVM_PV_REASON_PAGE_NOT_PRESENT:
2113 svm->apf_reason = 0;
2114 local_irq_disable();
2115 kvm_async_pf_task_wait(fault_address);
2116 local_irq_enable();
2117 break;
2118 case KVM_PV_REASON_PAGE_READY:
2119 svm->apf_reason = 0;
2120 local_irq_disable();
2121 kvm_async_pf_task_wake(fault_address);
2122 local_irq_enable();
2123 break;
2124 }
2125 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126}
2127
Avi Kivity851ba692009-08-24 11:10:17 +03002128static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002129{
Avi Kivity851ba692009-08-24 11:10:17 +03002130 struct kvm_run *kvm_run = svm->vcpu.run;
2131
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002132 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03002133 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02002134 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002135 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2136 return 1;
2137 }
Gleb Natapov44c11432009-05-11 13:35:52 +03002138
Jan Kiszka6be7d302009-10-18 13:24:54 +02002139 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02002140 disable_nmi_singlestep(svm);
Gleb Natapov44c11432009-05-11 13:35:52 +03002141 }
2142
2143 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002144 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002145 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2146 kvm_run->debug.arch.pc =
2147 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2148 kvm_run->debug.arch.exception = DB_VECTOR;
2149 return 0;
2150 }
2151
2152 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002153}
2154
Avi Kivity851ba692009-08-24 11:10:17 +03002155static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002156{
Avi Kivity851ba692009-08-24 11:10:17 +03002157 struct kvm_run *kvm_run = svm->vcpu.run;
2158
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002159 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2160 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2161 kvm_run->debug.arch.exception = BP_VECTOR;
2162 return 0;
2163}
2164
Avi Kivity851ba692009-08-24 11:10:17 +03002165static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002166{
2167 int er;
2168
Andre Przywara51d8b662010-12-21 11:12:02 +01002169 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002170 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002171 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002172 return 1;
2173}
2174
Eric Northup54a20552015-11-03 18:03:53 +01002175static int ac_interception(struct vcpu_svm *svm)
2176{
2177 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2178 return 1;
2179}
2180
Joerg Roedel67ec6602010-05-17 14:43:35 +02002181static bool is_erratum_383(void)
2182{
2183 int err, i;
2184 u64 value;
2185
2186 if (!erratum_383_found)
2187 return false;
2188
2189 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2190 if (err)
2191 return false;
2192
2193 /* Bit 62 may or may not be set for this mce */
2194 value &= ~(1ULL << 62);
2195
2196 if (value != 0xb600000000010015ULL)
2197 return false;
2198
2199 /* Clear MCi_STATUS registers */
2200 for (i = 0; i < 6; ++i)
2201 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2202
2203 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2204 if (!err) {
2205 u32 low, high;
2206
2207 value &= ~(1ULL << 2);
2208 low = lower_32_bits(value);
2209 high = upper_32_bits(value);
2210
2211 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2212 }
2213
2214 /* Flush tlb to evict multi-match entries */
2215 __flush_tlb_all();
2216
2217 return true;
2218}
2219
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002220static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002221{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002222 if (is_erratum_383()) {
2223 /*
2224 * Erratum 383 triggered. Guest state is corrupt so kill the
2225 * guest.
2226 */
2227 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2228
Avi Kivitya8eeb042010-05-10 12:34:53 +03002229 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002230
2231 return;
2232 }
2233
Joerg Roedel53371b52008-04-09 14:15:30 +02002234 /*
2235 * On an #MC intercept the MCE handler is not called automatically in
2236 * the host. So do it by hand here.
2237 */
2238 asm volatile (
2239 "int $0x12\n");
2240 /* not sure if we ever come back to this point */
2241
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002242 return;
2243}
2244
2245static int mc_interception(struct vcpu_svm *svm)
2246{
Joerg Roedel53371b52008-04-09 14:15:30 +02002247 return 1;
2248}
2249
Avi Kivity851ba692009-08-24 11:10:17 +03002250static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002251{
Avi Kivity851ba692009-08-24 11:10:17 +03002252 struct kvm_run *kvm_run = svm->vcpu.run;
2253
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002254 /*
2255 * VMCB is undefined after a SHUTDOWN intercept
2256 * so reinitialize it.
2257 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002258 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002259 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002260
2261 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2262 return 0;
2263}
2264
Avi Kivity851ba692009-08-24 11:10:17 +03002265static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002267 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002268 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01002269 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002270 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271
Rusty Russelle756fc62007-07-30 20:07:08 +10002272 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002273 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002274 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002275 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01002276 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002277
Avi Kivity039576c2007-03-20 12:46:50 +02002278 port = io_info >> 16;
2279 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002280 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002281 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002282
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002283 return in ? kvm_fast_pio_in(vcpu, size, port)
2284 : kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285}
2286
Avi Kivity851ba692009-08-24 11:10:17 +03002287static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002288{
2289 return 1;
2290}
2291
Avi Kivity851ba692009-08-24 11:10:17 +03002292static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002293{
2294 ++svm->vcpu.stat.irq_exits;
2295 return 1;
2296}
2297
Avi Kivity851ba692009-08-24 11:10:17 +03002298static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299{
2300 return 1;
2301}
2302
Avi Kivity851ba692009-08-24 11:10:17 +03002303static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002305 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002306 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307}
2308
Avi Kivity851ba692009-08-24 11:10:17 +03002309static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002310{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002311 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002312 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002313}
2314
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002315static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2316{
2317 struct vcpu_svm *svm = to_svm(vcpu);
2318
2319 return svm->nested.nested_cr3;
2320}
2321
Avi Kivitye4e517b2011-07-28 11:36:17 +03002322static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2323{
2324 struct vcpu_svm *svm = to_svm(vcpu);
2325 u64 cr3 = svm->nested.nested_cr3;
2326 u64 pdpte;
2327 int ret;
2328
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002329 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
2330 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002331 if (ret)
2332 return 0;
2333 return pdpte;
2334}
2335
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002336static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2337 unsigned long root)
2338{
2339 struct vcpu_svm *svm = to_svm(vcpu);
2340
2341 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01002342 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002343 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002344}
2345
Avi Kivity6389ee92010-11-29 16:12:30 +02002346static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2347 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002348{
2349 struct vcpu_svm *svm = to_svm(vcpu);
2350
Paolo Bonzini5e352512014-09-02 13:18:37 +02002351 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2352 /*
2353 * TODO: track the cause of the nested page fault, and
2354 * correctly fill in the high bits of exit_info_1.
2355 */
2356 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2357 svm->vmcb->control.exit_code_hi = 0;
2358 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2359 svm->vmcb->control.exit_info_2 = fault->address;
2360 }
2361
2362 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2363 svm->vmcb->control.exit_info_1 |= fault->error_code;
2364
2365 /*
2366 * The present bit is always zero for page structure faults on real
2367 * hardware.
2368 */
2369 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2370 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002371
2372 nested_svm_vmexit(svm);
2373}
2374
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002375static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002376{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002377 WARN_ON(mmu_is_nested(vcpu));
2378 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002379 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2380 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002381 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002382 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
2383 vcpu->arch.mmu.shadow_root_level = get_npt_level();
Xiao Guangrongc258b622015-08-05 12:04:24 +08002384 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002385 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002386}
2387
2388static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2389{
2390 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2391}
2392
Alexander Grafc0725422008-11-25 20:17:03 +01002393static int nested_svm_check_permissions(struct vcpu_svm *svm)
2394{
Dan Carpentere9196ce2017-05-18 10:39:53 +03002395 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
2396 !is_paging(&svm->vcpu)) {
Alexander Grafc0725422008-11-25 20:17:03 +01002397 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2398 return 1;
2399 }
2400
2401 if (svm->vmcb->save.cpl) {
2402 kvm_inject_gp(&svm->vcpu, 0);
2403 return 1;
2404 }
2405
Dan Carpentere9196ce2017-05-18 10:39:53 +03002406 return 0;
Alexander Grafc0725422008-11-25 20:17:03 +01002407}
2408
Alexander Grafcf74a782008-11-25 20:17:08 +01002409static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2410 bool has_error_code, u32 error_code)
2411{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002412 int vmexit;
2413
Joerg Roedel20307532010-11-29 17:51:48 +01002414 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002415 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002416
Joerg Roedel0295ad72009-08-07 11:49:37 +02002417 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2418 svm->vmcb->control.exit_code_hi = 0;
2419 svm->vmcb->control.exit_info_1 = error_code;
2420 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
2421
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002422 vmexit = nested_svm_intercept(svm);
2423 if (vmexit == NESTED_EXIT_DONE)
2424 svm->nested.exit_required = true;
2425
2426 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002427}
2428
Joerg Roedel8fe54652010-02-19 16:23:01 +01002429/* This function returns true if it is save to enable the irq window */
2430static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002431{
Joerg Roedel20307532010-11-29 17:51:48 +01002432 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002433 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002434
Joerg Roedel26666952009-08-07 11:49:46 +02002435 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002436 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002437
Joerg Roedel26666952009-08-07 11:49:46 +02002438 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002439 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002440
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002441 /*
2442 * if vmexit was already requested (by intercepted exception
2443 * for instance) do not overwrite it with "external interrupt"
2444 * vmexit.
2445 */
2446 if (svm->nested.exit_required)
2447 return false;
2448
Joerg Roedel197717d2010-02-24 18:59:19 +01002449 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2450 svm->vmcb->control.exit_info_1 = 0;
2451 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002452
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002453 if (svm->nested.intercept & 1ULL) {
2454 /*
2455 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002456 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002457 * #vmexit emulation might sleep. Only signal request for
2458 * the #vmexit here.
2459 */
2460 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002461 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002462 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002463 }
2464
Joerg Roedel8fe54652010-02-19 16:23:01 +01002465 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002466}
2467
Joerg Roedel887f5002010-02-24 18:59:12 +01002468/* This function returns true if it is save to enable the nmi window */
2469static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2470{
Joerg Roedel20307532010-11-29 17:51:48 +01002471 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002472 return true;
2473
2474 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2475 return true;
2476
2477 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2478 svm->nested.exit_required = true;
2479
2480 return false;
2481}
2482
Joerg Roedel7597f122010-02-19 16:23:00 +01002483static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002484{
2485 struct page *page;
2486
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002487 might_sleep();
2488
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002489 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002490 if (is_error_page(page))
2491 goto error;
2492
Joerg Roedel7597f122010-02-19 16:23:00 +01002493 *_page = page;
2494
2495 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002496
2497error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002498 kvm_inject_gp(&svm->vcpu, 0);
2499
2500 return NULL;
2501}
2502
Joerg Roedel7597f122010-02-19 16:23:00 +01002503static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002504{
Joerg Roedel7597f122010-02-19 16:23:00 +01002505 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002506 kvm_release_page_dirty(page);
2507}
2508
Joerg Roedelce2ac082010-03-01 15:34:39 +01002509static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002510{
Jan Kiszka9bf41832014-06-30 10:54:17 +02002511 unsigned port, size, iopm_len;
2512 u16 val, mask;
2513 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002514 u64 gpa;
2515
2516 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2517 return NESTED_EXIT_HOST;
2518
2519 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02002520 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2521 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002522 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02002523 start_bit = port % 8;
2524 iopm_len = (start_bit + size > 8) ? 2 : 1;
2525 mask = (0xf >> (4 - size)) << start_bit;
2526 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002527
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002528 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02002529 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002530
Jan Kiszka9bf41832014-06-30 10:54:17 +02002531 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002532}
2533
Joerg Roedeld2477822010-03-01 15:34:34 +01002534static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002535{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002536 u32 offset, msr, value;
2537 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002538
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002539 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002540 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002541
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002542 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2543 offset = svm_msrpm_offset(msr);
2544 write = svm->vmcb->control.exit_info_1 & 1;
2545 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002546
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002547 if (offset == MSR_INVALID)
2548 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002549
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002550 /* Offset is in 32 bit units but need in 8 bit units */
2551 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002552
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002553 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002554 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002555
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002556 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002557}
2558
Ladi Prosekab2f4d732017-06-21 09:06:58 +02002559/* DB exceptions for our internal use must not cause vmexit */
2560static int nested_svm_intercept_db(struct vcpu_svm *svm)
2561{
2562 unsigned long dr6;
2563
2564 /* if we're not singlestepping, it's not ours */
2565 if (!svm->nmi_singlestep)
2566 return NESTED_EXIT_DONE;
2567
2568 /* if it's not a singlestep exception, it's not ours */
2569 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
2570 return NESTED_EXIT_DONE;
2571 if (!(dr6 & DR6_BS))
2572 return NESTED_EXIT_DONE;
2573
2574 /* if the guest is singlestepping, it should get the vmexit */
2575 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
2576 disable_nmi_singlestep(svm);
2577 return NESTED_EXIT_DONE;
2578 }
2579
2580 /* it's ours, the nested hypervisor must not see this one */
2581 return NESTED_EXIT_HOST;
2582}
2583
Joerg Roedel410e4d52009-08-07 11:49:44 +02002584static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002585{
Alexander Grafcf74a782008-11-25 20:17:08 +01002586 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002587
Joerg Roedel410e4d52009-08-07 11:49:44 +02002588 switch (exit_code) {
2589 case SVM_EXIT_INTR:
2590 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002591 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002592 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002593 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002594 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002595 if (npt_enabled)
2596 return NESTED_EXIT_HOST;
2597 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002598 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002599 /* When we're shadowing, trap PFs, but not async PF */
2600 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002601 return NESTED_EXIT_HOST;
2602 break;
2603 default:
2604 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002605 }
2606
Joerg Roedel410e4d52009-08-07 11:49:44 +02002607 return NESTED_EXIT_CONTINUE;
2608}
2609
2610/*
2611 * If this function returns true, this #vmexit was already handled
2612 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002613static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002614{
2615 u32 exit_code = svm->vmcb->control.exit_code;
2616 int vmexit = NESTED_EXIT_HOST;
2617
Alexander Grafcf74a782008-11-25 20:17:08 +01002618 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002619 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002620 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002621 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002622 case SVM_EXIT_IOIO:
2623 vmexit = nested_svm_intercept_ioio(svm);
2624 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002625 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2626 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2627 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002628 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002629 break;
2630 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002631 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2632 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2633 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002634 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002635 break;
2636 }
2637 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2638 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Ladi Prosekab2f4d732017-06-21 09:06:58 +02002639 if (svm->nested.intercept_exceptions & excp_bits) {
2640 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
2641 vmexit = nested_svm_intercept_db(svm);
2642 else
2643 vmexit = NESTED_EXIT_DONE;
2644 }
Gleb Natapov631bc482010-10-14 11:22:52 +02002645 /* async page fault always cause vmexit */
2646 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2647 svm->apf_reason != 0)
2648 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002649 break;
2650 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002651 case SVM_EXIT_ERR: {
2652 vmexit = NESTED_EXIT_DONE;
2653 break;
2654 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002655 default: {
2656 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002657 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002658 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002659 }
2660 }
2661
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002662 return vmexit;
2663}
2664
2665static int nested_svm_exit_handled(struct vcpu_svm *svm)
2666{
2667 int vmexit;
2668
2669 vmexit = nested_svm_intercept(svm);
2670
2671 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002672 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002673
2674 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002675}
2676
Joerg Roedel0460a972009-08-07 11:49:31 +02002677static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2678{
2679 struct vmcb_control_area *dst = &dst_vmcb->control;
2680 struct vmcb_control_area *from = &from_vmcb->control;
2681
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002682 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002683 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002684 dst->intercept_exceptions = from->intercept_exceptions;
2685 dst->intercept = from->intercept;
2686 dst->iopm_base_pa = from->iopm_base_pa;
2687 dst->msrpm_base_pa = from->msrpm_base_pa;
2688 dst->tsc_offset = from->tsc_offset;
2689 dst->asid = from->asid;
2690 dst->tlb_ctl = from->tlb_ctl;
2691 dst->int_ctl = from->int_ctl;
2692 dst->int_vector = from->int_vector;
2693 dst->int_state = from->int_state;
2694 dst->exit_code = from->exit_code;
2695 dst->exit_code_hi = from->exit_code_hi;
2696 dst->exit_info_1 = from->exit_info_1;
2697 dst->exit_info_2 = from->exit_info_2;
2698 dst->exit_int_info = from->exit_int_info;
2699 dst->exit_int_info_err = from->exit_int_info_err;
2700 dst->nested_ctl = from->nested_ctl;
2701 dst->event_inj = from->event_inj;
2702 dst->event_inj_err = from->event_inj_err;
2703 dst->nested_cr3 = from->nested_cr3;
2704 dst->lbr_ctl = from->lbr_ctl;
2705}
2706
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002707static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002708{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002709 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002710 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002711 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002712 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002713
Joerg Roedel17897f32009-10-09 16:08:29 +02002714 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2715 vmcb->control.exit_info_1,
2716 vmcb->control.exit_info_2,
2717 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002718 vmcb->control.exit_int_info_err,
2719 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002720
Joerg Roedel7597f122010-02-19 16:23:00 +01002721 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002722 if (!nested_vmcb)
2723 return 1;
2724
Joerg Roedel20307532010-11-29 17:51:48 +01002725 /* Exit Guest-Mode */
2726 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002727 svm->nested.vmcb = 0;
2728
Alexander Grafcf74a782008-11-25 20:17:08 +01002729 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002730 disable_gif(svm);
2731
2732 nested_vmcb->save.es = vmcb->save.es;
2733 nested_vmcb->save.cs = vmcb->save.cs;
2734 nested_vmcb->save.ss = vmcb->save.ss;
2735 nested_vmcb->save.ds = vmcb->save.ds;
2736 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2737 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002738 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002739 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002740 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002741 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002742 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002743 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002744 nested_vmcb->save.rip = vmcb->save.rip;
2745 nested_vmcb->save.rsp = vmcb->save.rsp;
2746 nested_vmcb->save.rax = vmcb->save.rax;
2747 nested_vmcb->save.dr7 = vmcb->save.dr7;
2748 nested_vmcb->save.dr6 = vmcb->save.dr6;
2749 nested_vmcb->save.cpl = vmcb->save.cpl;
2750
2751 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2752 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2753 nested_vmcb->control.int_state = vmcb->control.int_state;
2754 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2755 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2756 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2757 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2758 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2759 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02002760
2761 if (svm->nrips_enabled)
2762 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002763
2764 /*
2765 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2766 * to make sure that we do not lose injected events. So check event_inj
2767 * here and copy it to exit_int_info if it is valid.
2768 * Exit_int_info and event_inj can't be both valid because the case
2769 * below only happens on a VMRUN instruction intercept which has
2770 * no valid exit_int_info set.
2771 */
2772 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2773 struct vmcb_control_area *nc = &nested_vmcb->control;
2774
2775 nc->exit_int_info = vmcb->control.event_inj;
2776 nc->exit_int_info_err = vmcb->control.event_inj_err;
2777 }
2778
Joerg Roedel33740e42009-08-07 11:49:29 +02002779 nested_vmcb->control.tlb_ctl = 0;
2780 nested_vmcb->control.event_inj = 0;
2781 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002782
2783 /* We always set V_INTR_MASKING and remember the old value in hflags */
2784 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2785 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2786
Alexander Grafcf74a782008-11-25 20:17:08 +01002787 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002788 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002789
Alexander Graf219b65d2009-06-15 15:21:25 +02002790 kvm_clear_exception_queue(&svm->vcpu);
2791 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002792
Joerg Roedel4b161842010-09-10 17:31:03 +02002793 svm->nested.nested_cr3 = 0;
2794
Alexander Grafcf74a782008-11-25 20:17:08 +01002795 /* Restore selected save entries */
2796 svm->vmcb->save.es = hsave->save.es;
2797 svm->vmcb->save.cs = hsave->save.cs;
2798 svm->vmcb->save.ss = hsave->save.ss;
2799 svm->vmcb->save.ds = hsave->save.ds;
2800 svm->vmcb->save.gdtr = hsave->save.gdtr;
2801 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002802 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002803 svm_set_efer(&svm->vcpu, hsave->save.efer);
2804 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2805 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2806 if (npt_enabled) {
2807 svm->vmcb->save.cr3 = hsave->save.cr3;
2808 svm->vcpu.arch.cr3 = hsave->save.cr3;
2809 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002810 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002811 }
2812 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2813 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2814 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2815 svm->vmcb->save.dr7 = 0;
2816 svm->vmcb->save.cpl = 0;
2817 svm->vmcb->control.exit_int_info = 0;
2818
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002819 mark_all_dirty(svm->vmcb);
2820
Joerg Roedel7597f122010-02-19 16:23:00 +01002821 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002822
Joerg Roedel4b161842010-09-10 17:31:03 +02002823 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002824 kvm_mmu_reset_context(&svm->vcpu);
2825 kvm_mmu_load(&svm->vcpu);
2826
2827 return 0;
2828}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002829
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002830static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002831{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002832 /*
2833 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002834 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01002835 * the kvm msr permission bitmap may contain zero bits
2836 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002837 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002838
Joerg Roedel323c3d82010-03-01 15:34:37 +01002839 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2840 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002841
Joerg Roedel323c3d82010-03-01 15:34:37 +01002842 for (i = 0; i < MSRPM_OFFSETS; i++) {
2843 u32 value, p;
2844 u64 offset;
2845
2846 if (msrpm_offsets[i] == 0xffffffff)
2847 break;
2848
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002849 p = msrpm_offsets[i];
2850 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002851
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002852 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01002853 return false;
2854
2855 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2856 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002857
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002858 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002859
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002860 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002861}
2862
Joerg Roedel52c65a302010-08-02 16:46:44 +02002863static bool nested_vmcb_checks(struct vmcb *vmcb)
2864{
2865 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2866 return false;
2867
Joerg Roedeldbe77582010-08-02 16:46:45 +02002868 if (vmcb->control.asid == 0)
2869 return false;
2870
Joerg Roedel4b161842010-09-10 17:31:03 +02002871 if (vmcb->control.nested_ctl && !npt_enabled)
2872 return false;
2873
Joerg Roedel52c65a302010-08-02 16:46:44 +02002874 return true;
2875}
2876
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002877static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002878{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002879 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002880 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002881 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002882 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002883 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002884
Joerg Roedel06fc77722010-02-19 16:23:07 +01002885 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002886
Joerg Roedel7597f122010-02-19 16:23:00 +01002887 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002888 if (!nested_vmcb)
2889 return false;
2890
Joerg Roedel52c65a302010-08-02 16:46:44 +02002891 if (!nested_vmcb_checks(nested_vmcb)) {
2892 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2893 nested_vmcb->control.exit_code_hi = 0;
2894 nested_vmcb->control.exit_info_1 = 0;
2895 nested_vmcb->control.exit_info_2 = 0;
2896
2897 nested_svm_unmap(page);
2898
2899 return false;
2900 }
2901
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002902 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002903 nested_vmcb->save.rip,
2904 nested_vmcb->control.int_ctl,
2905 nested_vmcb->control.event_inj,
2906 nested_vmcb->control.nested_ctl);
2907
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002908 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2909 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002910 nested_vmcb->control.intercept_exceptions,
2911 nested_vmcb->control.intercept);
2912
Alexander Graf3d6368e2008-11-25 20:17:07 +01002913 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002914 kvm_clear_exception_queue(&svm->vcpu);
2915 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002916
Joerg Roedele0231712010-02-24 18:59:10 +01002917 /*
2918 * Save the old vmcb, so we don't need to pick what we save, but can
2919 * restore everything when a VMEXIT occurs
2920 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002921 hsave->save.es = vmcb->save.es;
2922 hsave->save.cs = vmcb->save.cs;
2923 hsave->save.ss = vmcb->save.ss;
2924 hsave->save.ds = vmcb->save.ds;
2925 hsave->save.gdtr = vmcb->save.gdtr;
2926 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002927 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002928 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002929 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002930 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002931 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002932 hsave->save.rsp = vmcb->save.rsp;
2933 hsave->save.rax = vmcb->save.rax;
2934 if (npt_enabled)
2935 hsave->save.cr3 = vmcb->save.cr3;
2936 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002937 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002938
Joerg Roedel0460a972009-08-07 11:49:31 +02002939 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002940
Avi Kivityf6e78472010-08-02 15:30:20 +03002941 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002942 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2943 else
2944 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2945
Joerg Roedel4b161842010-09-10 17:31:03 +02002946 if (nested_vmcb->control.nested_ctl) {
2947 kvm_mmu_unload(&svm->vcpu);
2948 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2949 nested_svm_init_mmu_context(&svm->vcpu);
2950 }
2951
Alexander Graf3d6368e2008-11-25 20:17:07 +01002952 /* Load the nested guest state */
2953 svm->vmcb->save.es = nested_vmcb->save.es;
2954 svm->vmcb->save.cs = nested_vmcb->save.cs;
2955 svm->vmcb->save.ss = nested_vmcb->save.ss;
2956 svm->vmcb->save.ds = nested_vmcb->save.ds;
2957 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2958 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002959 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002960 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2961 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2962 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2963 if (npt_enabled) {
2964 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2965 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002966 } else
Avi Kivity23902182010-06-10 17:02:16 +03002967 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002968
2969 /* Guest paging mode is active - reset mmu */
2970 kvm_mmu_reset_context(&svm->vcpu);
2971
Joerg Roedeldefbba52009-08-07 11:49:30 +02002972 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002973 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2974 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2975 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002976
Alexander Graf3d6368e2008-11-25 20:17:07 +01002977 /* In case we don't even reach vcpu_run, the fields are not updated */
2978 svm->vmcb->save.rax = nested_vmcb->save.rax;
2979 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2980 svm->vmcb->save.rip = nested_vmcb->save.rip;
2981 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2982 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2983 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2984
Joerg Roedelf7138532010-03-01 15:34:40 +01002985 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002986 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002987
Joerg Roedelaad42c62009-08-07 11:49:34 +02002988 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002989 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002990 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002991 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2992 svm->nested.intercept = nested_vmcb->control.intercept;
2993
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002994 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002995 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002996 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2997 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2998 else
2999 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3000
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003001 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3002 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003003 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3004 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003005 }
3006
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003007 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003008 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003009
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003010 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003011 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3012 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
3013 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003014 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3015 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3016
Joerg Roedel7597f122010-02-19 16:23:00 +01003017 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003018
Joerg Roedel20307532010-11-29 17:51:48 +01003019 /* Enter Guest-Mode */
3020 enter_guest_mode(&svm->vcpu);
3021
Joerg Roedel384c6362010-11-30 18:03:56 +01003022 /*
3023 * Merge guest and host intercepts - must be called with vcpu in
3024 * guest-mode to take affect here
3025 */
3026 recalc_intercepts(svm);
3027
Joerg Roedel06fc77722010-02-19 16:23:07 +01003028 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003029
Joerg Roedel2af91942009-08-07 11:49:28 +02003030 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003031
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003032 mark_all_dirty(svm->vmcb);
3033
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003034 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003035}
3036
Joerg Roedel9966bf62009-08-07 11:49:40 +02003037static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01003038{
3039 to_vmcb->save.fs = from_vmcb->save.fs;
3040 to_vmcb->save.gs = from_vmcb->save.gs;
3041 to_vmcb->save.tr = from_vmcb->save.tr;
3042 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3043 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3044 to_vmcb->save.star = from_vmcb->save.star;
3045 to_vmcb->save.lstar = from_vmcb->save.lstar;
3046 to_vmcb->save.cstar = from_vmcb->save.cstar;
3047 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3048 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3049 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3050 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01003051}
3052
Avi Kivity851ba692009-08-24 11:10:17 +03003053static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003054{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003055 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003056 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003057
Alexander Graf55426752008-11-25 20:17:06 +01003058 if (nested_svm_check_permissions(svm))
3059 return 1;
3060
Joerg Roedel7597f122010-02-19 16:23:00 +01003061 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003062 if (!nested_vmcb)
3063 return 1;
3064
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003065 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
3066 skip_emulated_instruction(&svm->vcpu);
3067
Joerg Roedel9966bf62009-08-07 11:49:40 +02003068 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003069 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003070
3071 return 1;
3072}
3073
Avi Kivity851ba692009-08-24 11:10:17 +03003074static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003075{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003076 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003077 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003078
Alexander Graf55426752008-11-25 20:17:06 +01003079 if (nested_svm_check_permissions(svm))
3080 return 1;
3081
Joerg Roedel7597f122010-02-19 16:23:00 +01003082 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003083 if (!nested_vmcb)
3084 return 1;
3085
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003086 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
3087 skip_emulated_instruction(&svm->vcpu);
3088
Joerg Roedel9966bf62009-08-07 11:49:40 +02003089 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003090 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003091
3092 return 1;
3093}
3094
Avi Kivity851ba692009-08-24 11:10:17 +03003095static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003096{
Alexander Graf3d6368e2008-11-25 20:17:07 +01003097 if (nested_svm_check_permissions(svm))
3098 return 1;
3099
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02003100 /* Save rip after vmrun instruction */
3101 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003102
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003103 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01003104 return 1;
3105
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003106 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02003107 goto failed;
3108
3109 return 1;
3110
3111failed:
3112
3113 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3114 svm->vmcb->control.exit_code_hi = 0;
3115 svm->vmcb->control.exit_info_1 = 0;
3116 svm->vmcb->control.exit_info_2 = 0;
3117
3118 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003119
3120 return 1;
3121}
3122
Avi Kivity851ba692009-08-24 11:10:17 +03003123static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003124{
3125 if (nested_svm_check_permissions(svm))
3126 return 1;
3127
3128 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
3129 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003130 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003131
Joerg Roedel2af91942009-08-07 11:49:28 +02003132 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003133
3134 return 1;
3135}
3136
Avi Kivity851ba692009-08-24 11:10:17 +03003137static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003138{
3139 if (nested_svm_check_permissions(svm))
3140 return 1;
3141
3142 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
3143 skip_emulated_instruction(&svm->vcpu);
3144
Joerg Roedel2af91942009-08-07 11:49:28 +02003145 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003146
3147 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05003148 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3149 svm_clear_vintr(svm);
3150 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3151 mark_dirty(svm->vmcb, VMCB_INTR);
3152 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003153
Alexander Graf1371d902008-11-25 20:17:04 +01003154 return 1;
3155}
3156
Avi Kivity851ba692009-08-24 11:10:17 +03003157static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02003158{
3159 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02003160
David Kaplan668f1982015-02-20 16:02:10 -06003161 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
3162 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02003163
Alexander Grafff092382009-06-15 15:21:24 +02003164 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06003165 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02003166
3167 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
3168 skip_emulated_instruction(&svm->vcpu);
3169 return 1;
3170}
3171
Joerg Roedel532a46b2009-10-09 16:08:32 +02003172static int skinit_interception(struct vcpu_svm *svm)
3173{
David Kaplan668f1982015-02-20 16:02:10 -06003174 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003175
3176 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3177 return 1;
3178}
3179
David Kaplandab429a2015-03-02 13:43:37 -06003180static int wbinvd_interception(struct vcpu_svm *svm)
3181{
Kyle Huey6affcbe2016-11-29 12:40:40 -08003182 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06003183}
3184
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003185static int xsetbv_interception(struct vcpu_svm *svm)
3186{
3187 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
3188 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3189
3190 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3191 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
3192 skip_emulated_instruction(&svm->vcpu);
3193 }
3194
3195 return 1;
3196}
3197
Avi Kivity851ba692009-08-24 11:10:17 +03003198static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199{
Izik Eidus37817f22008-03-24 23:14:53 +02003200 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003201 int reason;
3202 int int_type = svm->vmcb->control.exit_int_info &
3203 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003204 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003205 uint32_t type =
3206 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3207 uint32_t idt_v =
3208 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003209 bool has_error_code = false;
3210 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003211
3212 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003213
Izik Eidus37817f22008-03-24 23:14:53 +02003214 if (svm->vmcb->control.exit_info_2 &
3215 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003216 reason = TASK_SWITCH_IRET;
3217 else if (svm->vmcb->control.exit_info_2 &
3218 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3219 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003220 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003221 reason = TASK_SWITCH_GATE;
3222 else
3223 reason = TASK_SWITCH_CALL;
3224
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003225 if (reason == TASK_SWITCH_GATE) {
3226 switch (type) {
3227 case SVM_EXITINTINFO_TYPE_NMI:
3228 svm->vcpu.arch.nmi_injected = false;
3229 break;
3230 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003231 if (svm->vmcb->control.exit_info_2 &
3232 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3233 has_error_code = true;
3234 error_code =
3235 (u32)svm->vmcb->control.exit_info_2;
3236 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003237 kvm_clear_exception_queue(&svm->vcpu);
3238 break;
3239 case SVM_EXITINTINFO_TYPE_INTR:
3240 kvm_clear_interrupt_queue(&svm->vcpu);
3241 break;
3242 default:
3243 break;
3244 }
3245 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003246
Gleb Natapov8317c292009-04-12 13:37:02 +03003247 if (reason != TASK_SWITCH_GATE ||
3248 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3249 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03003250 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3251 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003252
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003253 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3254 int_vec = -1;
3255
3256 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03003257 has_error_code, error_code) == EMULATE_FAIL) {
3258 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3259 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3260 svm->vcpu.run->internal.ndata = 0;
3261 return 0;
3262 }
3263 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003264}
3265
Avi Kivity851ba692009-08-24 11:10:17 +03003266static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003268 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Kyle Huey6a908b62016-11-29 12:40:37 -08003269 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003270}
3271
Avi Kivity851ba692009-08-24 11:10:17 +03003272static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003273{
3274 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003275 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003276 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003277 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003278 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003279 return 1;
3280}
3281
Avi Kivity851ba692009-08-24 11:10:17 +03003282static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003283{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003284 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3285 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3286
3287 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
3288 skip_emulated_instruction(&svm->vcpu);
3289 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003290}
3291
Avi Kivity851ba692009-08-24 11:10:17 +03003292static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293{
Andre Przywara51d8b662010-12-21 11:12:02 +01003294 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003295}
3296
Avi Kivity332b56e2011-11-10 14:57:24 +02003297static int rdpmc_interception(struct vcpu_svm *svm)
3298{
3299 int err;
3300
3301 if (!static_cpu_has(X86_FEATURE_NRIPS))
3302 return emulate_on_interception(svm);
3303
3304 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08003305 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02003306}
3307
Xiubo Li52eb5a62015-03-13 17:39:45 +08003308static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3309 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003310{
3311 unsigned long cr0 = svm->vcpu.arch.cr0;
3312 bool ret = false;
3313 u64 intercept;
3314
3315 intercept = svm->nested.intercept;
3316
3317 if (!is_guest_mode(&svm->vcpu) ||
3318 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3319 return false;
3320
3321 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3322 val &= ~SVM_CR0_SELECTIVE_MASK;
3323
3324 if (cr0 ^ val) {
3325 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3326 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3327 }
3328
3329 return ret;
3330}
3331
Andre Przywara7ff76d52010-12-21 11:12:04 +01003332#define CR_VALID (1ULL << 63)
3333
3334static int cr_interception(struct vcpu_svm *svm)
3335{
3336 int reg, cr;
3337 unsigned long val;
3338 int err;
3339
3340 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3341 return emulate_on_interception(svm);
3342
3343 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3344 return emulate_on_interception(svm);
3345
3346 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003347 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3348 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3349 else
3350 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003351
3352 err = 0;
3353 if (cr >= 16) { /* mov to cr */
3354 cr -= 16;
3355 val = kvm_register_read(&svm->vcpu, reg);
3356 switch (cr) {
3357 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003358 if (!check_selective_cr0_intercepted(svm, val))
3359 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003360 else
3361 return 1;
3362
Andre Przywara7ff76d52010-12-21 11:12:04 +01003363 break;
3364 case 3:
3365 err = kvm_set_cr3(&svm->vcpu, val);
3366 break;
3367 case 4:
3368 err = kvm_set_cr4(&svm->vcpu, val);
3369 break;
3370 case 8:
3371 err = kvm_set_cr8(&svm->vcpu, val);
3372 break;
3373 default:
3374 WARN(1, "unhandled write to CR%d", cr);
3375 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3376 return 1;
3377 }
3378 } else { /* mov from cr */
3379 switch (cr) {
3380 case 0:
3381 val = kvm_read_cr0(&svm->vcpu);
3382 break;
3383 case 2:
3384 val = svm->vcpu.arch.cr2;
3385 break;
3386 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003387 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003388 break;
3389 case 4:
3390 val = kvm_read_cr4(&svm->vcpu);
3391 break;
3392 case 8:
3393 val = kvm_get_cr8(&svm->vcpu);
3394 break;
3395 default:
3396 WARN(1, "unhandled read from CR%d", cr);
3397 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3398 return 1;
3399 }
3400 kvm_register_write(&svm->vcpu, reg, val);
3401 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08003402 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003403}
3404
Andre Przywaracae37972010-12-21 11:12:05 +01003405static int dr_interception(struct vcpu_svm *svm)
3406{
3407 int reg, dr;
3408 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003409
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003410 if (svm->vcpu.guest_debug == 0) {
3411 /*
3412 * No more DR vmexits; force a reload of the debug registers
3413 * and reenter on this instruction. The next vmexit will
3414 * retrieve the full state of the debug registers.
3415 */
3416 clr_dr_intercepts(svm);
3417 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3418 return 1;
3419 }
3420
Andre Przywaracae37972010-12-21 11:12:05 +01003421 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3422 return emulate_on_interception(svm);
3423
3424 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3425 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3426
3427 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003428 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3429 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003430 val = kvm_register_read(&svm->vcpu, reg);
3431 kvm_set_dr(&svm->vcpu, dr - 16, val);
3432 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003433 if (!kvm_require_dr(&svm->vcpu, dr))
3434 return 1;
3435 kvm_get_dr(&svm->vcpu, dr, &val);
3436 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003437 }
3438
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01003439 skip_emulated_instruction(&svm->vcpu);
3440
Andre Przywaracae37972010-12-21 11:12:05 +01003441 return 1;
3442}
3443
Avi Kivity851ba692009-08-24 11:10:17 +03003444static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003445{
Avi Kivity851ba692009-08-24 11:10:17 +03003446 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003447 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003448
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003449 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3450 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003451 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003452 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003453 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003454 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003455 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003456 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3457 return 0;
3458}
3459
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003460static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003462 struct vcpu_svm *svm = to_svm(vcpu);
3463
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003464 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303465 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003466 msr_info->data = svm->vmcb->control.tsc_offset +
Haozhong Zhang35181e82015-10-20 15:39:03 +08003467 kvm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003468
Avi Kivity6aa8b732006-12-10 02:21:36 -08003469 break;
3470 }
Brian Gerst8c065852010-07-17 09:03:26 -04003471 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003472 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003473 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003474#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003475 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003476 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477 break;
3478 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003479 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003480 break;
3481 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003482 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003483 break;
3484 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003485 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003486 break;
3487#endif
3488 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003489 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490 break;
3491 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003492 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003493 break;
3494 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003495 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003496 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003497 case MSR_TSC_AUX:
3498 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3499 return 1;
3500 msr_info->data = svm->tsc_aux;
3501 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003502 /*
3503 * Nobody will change the following 5 values in the VMCB so we can
3504 * safely return them on rdmsr. They will always be 0 until LBRV is
3505 * implemented.
3506 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003507 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003508 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01003509 break;
3510 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003511 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003512 break;
3513 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003514 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003515 break;
3516 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003517 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003518 break;
3519 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003520 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003521 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003522 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003523 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003524 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003525 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003526 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003527 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003528 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003529 msr_info->data = 0x01000065;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003530 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01003531 case MSR_F15H_IC_CFG: {
3532
3533 int family, model;
3534
3535 family = guest_cpuid_family(vcpu);
3536 model = guest_cpuid_model(vcpu);
3537
3538 if (family < 0 || model < 0)
3539 return kvm_get_msr_common(vcpu, msr_info);
3540
3541 msr_info->data = 0;
3542
3543 if (family == 0x15 &&
3544 (model >= 0x2 && model < 0x20))
3545 msr_info->data = 0x1E;
3546 }
3547 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003548 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003549 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003550 }
3551 return 0;
3552}
3553
Avi Kivity851ba692009-08-24 11:10:17 +03003554static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003555{
David Kaplan668f1982015-02-20 16:02:10 -06003556 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003557 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003558
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003559 msr_info.index = ecx;
3560 msr_info.host_initiated = false;
3561 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02003562 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003563 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003564 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003565 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003566
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003567 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
3568 msr_info.data & 0xffffffff);
3569 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
3570 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003571 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003572 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003573 }
3574 return 1;
3575}
3576
Joerg Roedel4a810182010-02-24 18:59:15 +01003577static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3578{
3579 struct vcpu_svm *svm = to_svm(vcpu);
3580 int svm_dis, chg_mask;
3581
3582 if (data & ~SVM_VM_CR_VALID_MASK)
3583 return 1;
3584
3585 chg_mask = SVM_VM_CR_VALID_MASK;
3586
3587 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3588 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3589
3590 svm->nested.vm_cr_msr &= ~chg_mask;
3591 svm->nested.vm_cr_msr |= (data & chg_mask);
3592
3593 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3594
3595 /* check for svm_disable while efer.svme is set */
3596 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3597 return 1;
3598
3599 return 0;
3600}
3601
Will Auld8fe8ab42012-11-29 12:42:12 -08003602static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003603{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003604 struct vcpu_svm *svm = to_svm(vcpu);
3605
Will Auld8fe8ab42012-11-29 12:42:12 -08003606 u32 ecx = msr->index;
3607 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003608 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003609 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003610 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003611 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003612 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003613 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003614 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003615#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003616 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003617 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003618 break;
3619 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003620 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003621 break;
3622 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003623 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003624 break;
3625 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003626 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003627 break;
3628#endif
3629 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003630 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003631 break;
3632 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003633 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003634 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635 break;
3636 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003637 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003638 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003639 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003640 case MSR_TSC_AUX:
3641 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3642 return 1;
3643
3644 /*
3645 * This is rare, so we update the MSR here instead of using
3646 * direct_access_msrs. Doing that would require a rdmsr in
3647 * svm_vcpu_put.
3648 */
3649 svm->tsc_aux = data;
3650 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
3651 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003652 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003653 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003654 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
3655 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003656 break;
3657 }
3658 if (data & DEBUGCTL_RESERVED_BITS)
3659 return 1;
3660
3661 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003662 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003663 if (data & (1ULL<<0))
3664 svm_enable_lbrv(svm);
3665 else
3666 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003667 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003668 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003669 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003670 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003671 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003672 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003673 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03003674 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003675 break;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003676 case MSR_IA32_APICBASE:
3677 if (kvm_vcpu_apicv_active(vcpu))
3678 avic_update_vapic_bar(to_svm(vcpu), data);
3679 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003680 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08003681 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003682 }
3683 return 0;
3684}
3685
Avi Kivity851ba692009-08-24 11:10:17 +03003686static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003687{
Will Auld8fe8ab42012-11-29 12:42:12 -08003688 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06003689 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3690 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003691
Will Auld8fe8ab42012-11-29 12:42:12 -08003692 msr.data = data;
3693 msr.index = ecx;
3694 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003695
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003696 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03003697 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02003698 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003699 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003700 } else {
3701 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10003702 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003703 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003704 return 1;
3705}
3706
Avi Kivity851ba692009-08-24 11:10:17 +03003707static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003708{
Rusty Russelle756fc62007-07-30 20:07:08 +10003709 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003710 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003711 else
Avi Kivity851ba692009-08-24 11:10:17 +03003712 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003713}
3714
Avi Kivity851ba692009-08-24 11:10:17 +03003715static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003716{
Avi Kivity3842d132010-07-27 12:30:24 +03003717 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003718 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003719 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003720 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08003721 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003722 return 1;
3723}
3724
Mark Langsdorf565d0992009-10-06 14:25:02 -05003725static int pause_interception(struct vcpu_svm *svm)
3726{
3727 kvm_vcpu_on_spin(&(svm->vcpu));
3728 return 1;
3729}
3730
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003731static int nop_interception(struct vcpu_svm *svm)
3732{
3733 skip_emulated_instruction(&(svm->vcpu));
3734 return 1;
3735}
3736
3737static int monitor_interception(struct vcpu_svm *svm)
3738{
3739 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
3740 return nop_interception(svm);
3741}
3742
3743static int mwait_interception(struct vcpu_svm *svm)
3744{
3745 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
3746 return nop_interception(svm);
3747}
3748
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003749enum avic_ipi_failure_cause {
3750 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
3751 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
3752 AVIC_IPI_FAILURE_INVALID_TARGET,
3753 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
3754};
3755
3756static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
3757{
3758 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
3759 u32 icrl = svm->vmcb->control.exit_info_1;
3760 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03003761 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003762 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3763
3764 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
3765
3766 switch (id) {
3767 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
3768 /*
3769 * AVIC hardware handles the generation of
3770 * IPIs when the specified Message Type is Fixed
3771 * (also known as fixed delivery mode) and
3772 * the Trigger Mode is edge-triggered. The hardware
3773 * also supports self and broadcast delivery modes
3774 * specified via the Destination Shorthand(DSH)
3775 * field of the ICRL. Logical and physical APIC ID
3776 * formats are supported. All other IPI types cause
3777 * a #VMEXIT, which needs to emulated.
3778 */
3779 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
3780 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
3781 break;
3782 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
3783 int i;
3784 struct kvm_vcpu *vcpu;
3785 struct kvm *kvm = svm->vcpu.kvm;
3786 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3787
3788 /*
3789 * At this point, we expect that the AVIC HW has already
3790 * set the appropriate IRR bits on the valid target
3791 * vcpus. So, we just need to kick the appropriate vcpu.
3792 */
3793 kvm_for_each_vcpu(i, vcpu, kvm) {
3794 bool m = kvm_apic_match_dest(vcpu, apic,
3795 icrl & KVM_APIC_SHORT_MASK,
3796 GET_APIC_DEST_FIELD(icrh),
3797 icrl & KVM_APIC_DEST_MASK);
3798
3799 if (m && !avic_vcpu_is_running(vcpu))
3800 kvm_vcpu_wake_up(vcpu);
3801 }
3802 break;
3803 }
3804 case AVIC_IPI_FAILURE_INVALID_TARGET:
3805 break;
3806 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
3807 WARN_ONCE(1, "Invalid backing page\n");
3808 break;
3809 default:
3810 pr_err("Unknown IPI interception\n");
3811 }
3812
3813 return 1;
3814}
3815
3816static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
3817{
3818 struct kvm_arch *vm_data = &vcpu->kvm->arch;
3819 int index;
3820 u32 *logical_apic_id_table;
3821 int dlid = GET_APIC_LOGICAL_ID(ldr);
3822
3823 if (!dlid)
3824 return NULL;
3825
3826 if (flat) { /* flat */
3827 index = ffs(dlid) - 1;
3828 if (index > 7)
3829 return NULL;
3830 } else { /* cluster */
3831 int cluster = (dlid & 0xf0) >> 4;
3832 int apic = ffs(dlid & 0x0f) - 1;
3833
3834 if ((apic < 0) || (apic > 7) ||
3835 (cluster >= 0xf))
3836 return NULL;
3837 index = (cluster << 2) + apic;
3838 }
3839
3840 logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page);
3841
3842 return &logical_apic_id_table[index];
3843}
3844
3845static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
3846 bool valid)
3847{
3848 bool flat;
3849 u32 *entry, new_entry;
3850
3851 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
3852 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
3853 if (!entry)
3854 return -EINVAL;
3855
3856 new_entry = READ_ONCE(*entry);
3857 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
3858 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
3859 if (valid)
3860 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
3861 else
3862 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
3863 WRITE_ONCE(*entry, new_entry);
3864
3865 return 0;
3866}
3867
3868static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
3869{
3870 int ret;
3871 struct vcpu_svm *svm = to_svm(vcpu);
3872 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
3873
3874 if (!ldr)
3875 return 1;
3876
3877 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
3878 if (ret && svm->ldr_reg) {
3879 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
3880 svm->ldr_reg = 0;
3881 } else {
3882 svm->ldr_reg = ldr;
3883 }
3884 return ret;
3885}
3886
3887static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
3888{
3889 u64 *old, *new;
3890 struct vcpu_svm *svm = to_svm(vcpu);
3891 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
3892 u32 id = (apic_id_reg >> 24) & 0xff;
3893
3894 if (vcpu->vcpu_id == id)
3895 return 0;
3896
3897 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
3898 new = avic_get_physical_id_entry(vcpu, id);
3899 if (!new || !old)
3900 return 1;
3901
3902 /* We need to move physical_id_entry to new offset */
3903 *new = *old;
3904 *old = 0ULL;
3905 to_svm(vcpu)->avic_physical_id_cache = new;
3906
3907 /*
3908 * Also update the guest physical APIC ID in the logical
3909 * APIC ID table entry if already setup the LDR.
3910 */
3911 if (svm->ldr_reg)
3912 avic_handle_ldr_update(vcpu);
3913
3914 return 0;
3915}
3916
3917static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
3918{
3919 struct vcpu_svm *svm = to_svm(vcpu);
3920 struct kvm_arch *vm_data = &vcpu->kvm->arch;
3921 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
3922 u32 mod = (dfr >> 28) & 0xf;
3923
3924 /*
3925 * We assume that all local APICs are using the same type.
3926 * If this changes, we need to flush the AVIC logical
3927 * APID id table.
3928 */
3929 if (vm_data->ldr_mode == mod)
3930 return 0;
3931
3932 clear_page(page_address(vm_data->avic_logical_id_table_page));
3933 vm_data->ldr_mode = mod;
3934
3935 if (svm->ldr_reg)
3936 avic_handle_ldr_update(vcpu);
3937 return 0;
3938}
3939
3940static int avic_unaccel_trap_write(struct vcpu_svm *svm)
3941{
3942 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3943 u32 offset = svm->vmcb->control.exit_info_1 &
3944 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
3945
3946 switch (offset) {
3947 case APIC_ID:
3948 if (avic_handle_apic_id_update(&svm->vcpu))
3949 return 0;
3950 break;
3951 case APIC_LDR:
3952 if (avic_handle_ldr_update(&svm->vcpu))
3953 return 0;
3954 break;
3955 case APIC_DFR:
3956 avic_handle_dfr_update(&svm->vcpu);
3957 break;
3958 default:
3959 break;
3960 }
3961
3962 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
3963
3964 return 1;
3965}
3966
3967static bool is_avic_unaccelerated_access_trap(u32 offset)
3968{
3969 bool ret = false;
3970
3971 switch (offset) {
3972 case APIC_ID:
3973 case APIC_EOI:
3974 case APIC_RRR:
3975 case APIC_LDR:
3976 case APIC_DFR:
3977 case APIC_SPIV:
3978 case APIC_ESR:
3979 case APIC_ICR:
3980 case APIC_LVTT:
3981 case APIC_LVTTHMR:
3982 case APIC_LVTPC:
3983 case APIC_LVT0:
3984 case APIC_LVT1:
3985 case APIC_LVTERR:
3986 case APIC_TMICT:
3987 case APIC_TDCR:
3988 ret = true;
3989 break;
3990 default:
3991 break;
3992 }
3993 return ret;
3994}
3995
3996static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
3997{
3998 int ret = 0;
3999 u32 offset = svm->vmcb->control.exit_info_1 &
4000 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4001 u32 vector = svm->vmcb->control.exit_info_2 &
4002 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4003 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4004 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4005 bool trap = is_avic_unaccelerated_access_trap(offset);
4006
4007 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4008 trap, write, vector);
4009 if (trap) {
4010 /* Handling Trap */
4011 WARN_ONCE(!write, "svm: Handling trap read.\n");
4012 ret = avic_unaccel_trap_write(svm);
4013 } else {
4014 /* Handling Fault */
4015 ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
4016 }
4017
4018 return ret;
4019}
4020
Mathias Krause09941fb2012-08-30 01:30:20 +02004021static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01004022 [SVM_EXIT_READ_CR0] = cr_interception,
4023 [SVM_EXIT_READ_CR3] = cr_interception,
4024 [SVM_EXIT_READ_CR4] = cr_interception,
4025 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06004026 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02004027 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01004028 [SVM_EXIT_WRITE_CR3] = cr_interception,
4029 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004030 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01004031 [SVM_EXIT_READ_DR0] = dr_interception,
4032 [SVM_EXIT_READ_DR1] = dr_interception,
4033 [SVM_EXIT_READ_DR2] = dr_interception,
4034 [SVM_EXIT_READ_DR3] = dr_interception,
4035 [SVM_EXIT_READ_DR4] = dr_interception,
4036 [SVM_EXIT_READ_DR5] = dr_interception,
4037 [SVM_EXIT_READ_DR6] = dr_interception,
4038 [SVM_EXIT_READ_DR7] = dr_interception,
4039 [SVM_EXIT_WRITE_DR0] = dr_interception,
4040 [SVM_EXIT_WRITE_DR1] = dr_interception,
4041 [SVM_EXIT_WRITE_DR2] = dr_interception,
4042 [SVM_EXIT_WRITE_DR3] = dr_interception,
4043 [SVM_EXIT_WRITE_DR4] = dr_interception,
4044 [SVM_EXIT_WRITE_DR5] = dr_interception,
4045 [SVM_EXIT_WRITE_DR6] = dr_interception,
4046 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004047 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4048 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004049 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004050 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004051 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01004052 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004053 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02004054 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004055 [SVM_EXIT_SMI] = nop_on_interception,
4056 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08004057 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02004058 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004060 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02004061 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05004062 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004063 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004064 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02004065 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004066 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067 [SVM_EXIT_MSR] = msr_interception,
4068 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08004069 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01004070 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02004071 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01004072 [SVM_EXIT_VMLOAD] = vmload_interception,
4073 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01004074 [SVM_EXIT_STGI] = stgi_interception,
4075 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02004076 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06004077 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004078 [SVM_EXIT_MONITOR] = monitor_interception,
4079 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01004080 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004081 [SVM_EXIT_NPF] = pf_interception,
Paolo Bonzini64d60672015-05-07 11:36:11 +02004082 [SVM_EXIT_RSM] = emulate_on_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004083 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4084 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004085};
4086
Joe Perchesae8cc052011-04-24 22:00:50 -07004087static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02004088{
4089 struct vcpu_svm *svm = to_svm(vcpu);
4090 struct vmcb_control_area *control = &svm->vmcb->control;
4091 struct vmcb_save_area *save = &svm->vmcb->save;
4092
4093 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004094 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4095 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4096 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4097 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4098 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4099 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4100 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
4101 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4102 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4103 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4104 pr_err("%-20s%d\n", "asid:", control->asid);
4105 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4106 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4107 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4108 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4109 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4110 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4111 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4112 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4113 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4114 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4115 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004116 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07004117 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4118 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
4119 pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
4120 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004121 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4122 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4123 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004124 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004125 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4126 "es:",
4127 save->es.selector, save->es.attrib,
4128 save->es.limit, save->es.base);
4129 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4130 "cs:",
4131 save->cs.selector, save->cs.attrib,
4132 save->cs.limit, save->cs.base);
4133 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4134 "ss:",
4135 save->ss.selector, save->ss.attrib,
4136 save->ss.limit, save->ss.base);
4137 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4138 "ds:",
4139 save->ds.selector, save->ds.attrib,
4140 save->ds.limit, save->ds.base);
4141 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4142 "fs:",
4143 save->fs.selector, save->fs.attrib,
4144 save->fs.limit, save->fs.base);
4145 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4146 "gs:",
4147 save->gs.selector, save->gs.attrib,
4148 save->gs.limit, save->gs.base);
4149 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4150 "gdtr:",
4151 save->gdtr.selector, save->gdtr.attrib,
4152 save->gdtr.limit, save->gdtr.base);
4153 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4154 "ldtr:",
4155 save->ldtr.selector, save->ldtr.attrib,
4156 save->ldtr.limit, save->ldtr.base);
4157 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4158 "idtr:",
4159 save->idtr.selector, save->idtr.attrib,
4160 save->idtr.limit, save->idtr.base);
4161 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4162 "tr:",
4163 save->tr.selector, save->tr.attrib,
4164 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004165 pr_err("cpl: %d efer: %016llx\n",
4166 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004167 pr_err("%-15s %016llx %-13s %016llx\n",
4168 "cr0:", save->cr0, "cr2:", save->cr2);
4169 pr_err("%-15s %016llx %-13s %016llx\n",
4170 "cr3:", save->cr3, "cr4:", save->cr4);
4171 pr_err("%-15s %016llx %-13s %016llx\n",
4172 "dr6:", save->dr6, "dr7:", save->dr7);
4173 pr_err("%-15s %016llx %-13s %016llx\n",
4174 "rip:", save->rip, "rflags:", save->rflags);
4175 pr_err("%-15s %016llx %-13s %016llx\n",
4176 "rsp:", save->rsp, "rax:", save->rax);
4177 pr_err("%-15s %016llx %-13s %016llx\n",
4178 "star:", save->star, "lstar:", save->lstar);
4179 pr_err("%-15s %016llx %-13s %016llx\n",
4180 "cstar:", save->cstar, "sfmask:", save->sfmask);
4181 pr_err("%-15s %016llx %-13s %016llx\n",
4182 "kernel_gs_base:", save->kernel_gs_base,
4183 "sysenter_cs:", save->sysenter_cs);
4184 pr_err("%-15s %016llx %-13s %016llx\n",
4185 "sysenter_esp:", save->sysenter_esp,
4186 "sysenter_eip:", save->sysenter_eip);
4187 pr_err("%-15s %016llx %-13s %016llx\n",
4188 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4189 pr_err("%-15s %016llx %-13s %016llx\n",
4190 "br_from:", save->br_from, "br_to:", save->br_to);
4191 pr_err("%-15s %016llx %-13s %016llx\n",
4192 "excp_from:", save->last_excp_from,
4193 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004194}
4195
Avi Kivity586f9602010-11-18 13:09:54 +02004196static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4197{
4198 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4199
4200 *info1 = control->exit_info_1;
4201 *info2 = control->exit_info_2;
4202}
4203
Avi Kivity851ba692009-08-24 11:10:17 +03004204static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004205{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004206 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004207 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004208 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004209
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004210 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4211
Tom Lendacky0f89b202016-12-14 14:59:23 -05004212 vcpu->arch.gpa_available = (exit_code == SVM_EXIT_NPF);
4213
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004214 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004215 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4216 if (npt_enabled)
4217 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004218
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004219 if (unlikely(svm->nested.exit_required)) {
4220 nested_svm_vmexit(svm);
4221 svm->nested.exit_required = false;
4222
4223 return 1;
4224 }
4225
Joerg Roedel20307532010-11-29 17:51:48 +01004226 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004227 int vmexit;
4228
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004229 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4230 svm->vmcb->control.exit_info_1,
4231 svm->vmcb->control.exit_info_2,
4232 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004233 svm->vmcb->control.exit_int_info_err,
4234 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004235
Joerg Roedel410e4d52009-08-07 11:49:44 +02004236 vmexit = nested_svm_exit_special(svm);
4237
4238 if (vmexit == NESTED_EXIT_CONTINUE)
4239 vmexit = nested_svm_exit_handled(svm);
4240
4241 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004242 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004243 }
4244
Joerg Roedela5c38322009-08-07 11:49:32 +02004245 svm_complete_interrupts(svm);
4246
Avi Kivity04d2cc72007-09-10 18:10:54 +03004247 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4248 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4249 kvm_run->fail_entry.hardware_entry_failure_reason
4250 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004251 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4252 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004253 return 0;
4254 }
4255
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004256 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004257 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004258 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4259 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004260 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004261 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004262 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004263 exit_code);
4264
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004265 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004266 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04004267 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03004268 kvm_queue_exception(vcpu, UD_VECTOR);
4269 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004270 }
4271
Avi Kivity851ba692009-08-24 11:10:17 +03004272 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004273}
4274
4275static void reload_tss(struct kvm_vcpu *vcpu)
4276{
4277 int cpu = raw_smp_processor_id();
4278
Tejun Heo0fe1e002009-10-29 22:34:14 +09004279 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4280 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004281 load_TR_desc();
4282}
4283
Rusty Russelle756fc62007-07-30 20:07:08 +10004284static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004285{
4286 int cpu = raw_smp_processor_id();
4287
Tejun Heo0fe1e002009-10-29 22:34:14 +09004288 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004289
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03004290 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09004291 if (svm->asid_generation != sd->asid_generation)
4292 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004293}
4294
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004295static void svm_inject_nmi(struct kvm_vcpu *vcpu)
4296{
4297 struct vcpu_svm *svm = to_svm(vcpu);
4298
4299 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
4300 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004301 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004302 ++vcpu->stat.nmi_injections;
4303}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004304
Eddie Dong85f455f2007-07-06 12:20:49 +03004305static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004306{
4307 struct vmcb_control_area *control;
4308
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004309 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10004310 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03004311 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004312 control->int_ctl &= ~V_INTR_PRIO_MASK;
4313 control->int_ctl |= V_IRQ_MASK |
4314 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004315 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004316}
4317
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004318static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03004319{
4320 struct vcpu_svm *svm = to_svm(vcpu);
4321
Joerg Roedel2af91942009-08-07 11:49:28 +02004322 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01004323
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03004324 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
4325 ++vcpu->stat.irq_injections;
4326
Alexander Graf219b65d2009-06-15 15:21:25 +02004327 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
4328 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03004329}
4330
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004331static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
4332{
4333 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
4334}
4335
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004336static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
4337{
4338 struct vcpu_svm *svm = to_svm(vcpu);
4339
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004340 if (svm_nested_virtualize_tpr(vcpu) ||
4341 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004342 return;
4343
Radim Krčmář596f3142014-03-11 19:11:18 +01004344 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
4345
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004346 if (irr == -1)
4347 return;
4348
4349 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004350 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004351}
4352
Yang Zhang8d146952013-01-25 10:18:50 +08004353static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
4354{
4355 return;
4356}
4357
Andrey Smetanind62caab2015-11-10 15:36:33 +03004358static bool svm_get_enable_apicv(void)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004359{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004360 return avic;
Yang Zhangc7c9c562013-01-25 10:18:51 +08004361}
4362
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004363static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
4364{
4365}
4366
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02004367static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004368{
4369}
4370
4371/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03004372static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4373{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004374 struct vcpu_svm *svm = to_svm(vcpu);
4375 struct vmcb *vmcb = svm->vmcb;
4376
4377 if (!avic)
4378 return;
4379
4380 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
4381 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004382}
4383
Andrey Smetanin63086302015-11-10 15:36:32 +03004384static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004385{
4386 return;
4387}
4388
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004389static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
4390{
4391 kvm_lapic_set_irr(vec, vcpu->arch.apic);
4392 smp_mb__after_atomic();
4393
4394 if (avic_vcpu_is_running(vcpu))
4395 wrmsrl(SVM_AVIC_DOORBELL,
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05004396 kvm_cpu_get_apicid(vcpu->cpu));
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004397 else
4398 kvm_vcpu_wake_up(vcpu);
4399}
4400
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004401static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4402{
4403 unsigned long flags;
4404 struct amd_svm_iommu_ir *cur;
4405
4406 spin_lock_irqsave(&svm->ir_list_lock, flags);
4407 list_for_each_entry(cur, &svm->ir_list, node) {
4408 if (cur->data != pi->ir_data)
4409 continue;
4410 list_del(&cur->node);
4411 kfree(cur);
4412 break;
4413 }
4414 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4415}
4416
4417static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4418{
4419 int ret = 0;
4420 unsigned long flags;
4421 struct amd_svm_iommu_ir *ir;
4422
4423 /**
4424 * In some cases, the existing irte is updaed and re-set,
4425 * so we need to check here if it's already been * added
4426 * to the ir_list.
4427 */
4428 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
4429 struct kvm *kvm = svm->vcpu.kvm;
4430 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
4431 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
4432 struct vcpu_svm *prev_svm;
4433
4434 if (!prev_vcpu) {
4435 ret = -EINVAL;
4436 goto out;
4437 }
4438
4439 prev_svm = to_svm(prev_vcpu);
4440 svm_ir_list_del(prev_svm, pi);
4441 }
4442
4443 /**
4444 * Allocating new amd_iommu_pi_data, which will get
4445 * add to the per-vcpu ir_list.
4446 */
4447 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
4448 if (!ir) {
4449 ret = -ENOMEM;
4450 goto out;
4451 }
4452 ir->data = pi->ir_data;
4453
4454 spin_lock_irqsave(&svm->ir_list_lock, flags);
4455 list_add(&ir->node, &svm->ir_list);
4456 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4457out:
4458 return ret;
4459}
4460
4461/**
4462 * Note:
4463 * The HW cannot support posting multicast/broadcast
4464 * interrupts to a vCPU. So, we still use legacy interrupt
4465 * remapping for these kind of interrupts.
4466 *
4467 * For lowest-priority interrupts, we only support
4468 * those with single CPU as the destination, e.g. user
4469 * configures the interrupts via /proc/irq or uses
4470 * irqbalance to make the interrupts single-CPU.
4471 */
4472static int
4473get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
4474 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
4475{
4476 struct kvm_lapic_irq irq;
4477 struct kvm_vcpu *vcpu = NULL;
4478
4479 kvm_set_msi_irq(kvm, e, &irq);
4480
4481 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
4482 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
4483 __func__, irq.vector);
4484 return -1;
4485 }
4486
4487 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
4488 irq.vector);
4489 *svm = to_svm(vcpu);
4490 vcpu_info->pi_desc_addr = page_to_phys((*svm)->avic_backing_page);
4491 vcpu_info->vector = irq.vector;
4492
4493 return 0;
4494}
4495
4496/*
4497 * svm_update_pi_irte - set IRTE for Posted-Interrupts
4498 *
4499 * @kvm: kvm
4500 * @host_irq: host irq of the interrupt
4501 * @guest_irq: gsi of the interrupt
4502 * @set: set or unset PI
4503 * returns 0 on success, < 0 on failure
4504 */
4505static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
4506 uint32_t guest_irq, bool set)
4507{
4508 struct kvm_kernel_irq_routing_entry *e;
4509 struct kvm_irq_routing_table *irq_rt;
4510 int idx, ret = -EINVAL;
4511
4512 if (!kvm_arch_has_assigned_device(kvm) ||
4513 !irq_remapping_cap(IRQ_POSTING_CAP))
4514 return 0;
4515
4516 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
4517 __func__, host_irq, guest_irq, set);
4518
4519 idx = srcu_read_lock(&kvm->irq_srcu);
4520 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
4521 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
4522
4523 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
4524 struct vcpu_data vcpu_info;
4525 struct vcpu_svm *svm = NULL;
4526
4527 if (e->type != KVM_IRQ_ROUTING_MSI)
4528 continue;
4529
4530 /**
4531 * Here, we setup with legacy mode in the following cases:
4532 * 1. When cannot target interrupt to a specific vcpu.
4533 * 2. Unsetting posted interrupt.
4534 * 3. APIC virtialization is disabled for the vcpu.
4535 */
4536 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
4537 kvm_vcpu_apicv_active(&svm->vcpu)) {
4538 struct amd_iommu_pi_data pi;
4539
4540 /* Try to enable guest_mode in IRTE */
4541 pi.base = page_to_phys(svm->avic_backing_page) & AVIC_HPA_MASK;
4542 pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id,
4543 svm->vcpu.vcpu_id);
4544 pi.is_guest_mode = true;
4545 pi.vcpu_data = &vcpu_info;
4546 ret = irq_set_vcpu_affinity(host_irq, &pi);
4547
4548 /**
4549 * Here, we successfully setting up vcpu affinity in
4550 * IOMMU guest mode. Now, we need to store the posted
4551 * interrupt information in a per-vcpu ir_list so that
4552 * we can reference to them directly when we update vcpu
4553 * scheduling information in IOMMU irte.
4554 */
4555 if (!ret && pi.is_guest_mode)
4556 svm_ir_list_add(svm, &pi);
4557 } else {
4558 /* Use legacy mode in IRTE */
4559 struct amd_iommu_pi_data pi;
4560
4561 /**
4562 * Here, pi is used to:
4563 * - Tell IOMMU to use legacy mode for this interrupt.
4564 * - Retrieve ga_tag of prior interrupt remapping data.
4565 */
4566 pi.is_guest_mode = false;
4567 ret = irq_set_vcpu_affinity(host_irq, &pi);
4568
4569 /**
4570 * Check if the posted interrupt was previously
4571 * setup with the guest_mode by checking if the ga_tag
4572 * was cached. If so, we need to clean up the per-vcpu
4573 * ir_list.
4574 */
4575 if (!ret && pi.prev_ga_tag) {
4576 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
4577 struct kvm_vcpu *vcpu;
4578
4579 vcpu = kvm_get_vcpu_by_id(kvm, id);
4580 if (vcpu)
4581 svm_ir_list_del(to_svm(vcpu), &pi);
4582 }
4583 }
4584
4585 if (!ret && svm) {
4586 trace_kvm_pi_irte_update(svm->vcpu.vcpu_id,
4587 host_irq, e->gsi,
4588 vcpu_info.vector,
4589 vcpu_info.pi_desc_addr, set);
4590 }
4591
4592 if (ret < 0) {
4593 pr_err("%s: failed to update PI IRTE\n", __func__);
4594 goto out;
4595 }
4596 }
4597
4598 ret = 0;
4599out:
4600 srcu_read_unlock(&kvm->irq_srcu, idx);
4601 return ret;
4602}
4603
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004604static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02004605{
4606 struct vcpu_svm *svm = to_svm(vcpu);
4607 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02004608 int ret;
4609 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
4610 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
4611 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
4612
4613 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02004614}
4615
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004616static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
4617{
4618 struct vcpu_svm *svm = to_svm(vcpu);
4619
4620 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
4621}
4622
4623static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4624{
4625 struct vcpu_svm *svm = to_svm(vcpu);
4626
4627 if (masked) {
4628 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004629 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004630 } else {
4631 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004632 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004633 }
4634}
4635
Gleb Natapov78646122009-03-23 12:12:11 +02004636static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
4637{
4638 struct vcpu_svm *svm = to_svm(vcpu);
4639 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02004640 int ret;
4641
4642 if (!gif_set(svm) ||
4643 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
4644 return 0;
4645
Avi Kivityf6e78472010-08-02 15:30:20 +03004646 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02004647
Joerg Roedel20307532010-11-29 17:51:48 +01004648 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02004649 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
4650
4651 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02004652}
4653
Jan Kiszkac9a79532014-03-07 20:03:15 +01004654static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03004655{
Alexander Graf219b65d2009-06-15 15:21:25 +02004656 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02004657
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004658 if (kvm_vcpu_apicv_active(vcpu))
4659 return;
4660
Joerg Roedele0231712010-02-24 18:59:10 +01004661 /*
4662 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
4663 * 1, because that's a separate STGI/VMRUN intercept. The next time we
4664 * get that intercept, this function will be called again though and
4665 * we'll get the vintr intercept.
4666 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01004667 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02004668 svm_set_vintr(svm);
4669 svm_inject_irq(svm, 0x0);
4670 }
Gleb Natapov9222be12009-04-23 17:14:37 +03004671}
4672
Jan Kiszkac9a79532014-03-07 20:03:15 +01004673static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004674{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004675 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004676
Gleb Natapov44c11432009-05-11 13:35:52 +03004677 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
4678 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01004679 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03004680
Joerg Roedele0231712010-02-24 18:59:10 +01004681 /*
4682 * Something prevents NMI from been injected. Single step over possible
4683 * problem (IRET or exception injection or interrupt shadow)
4684 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02004685 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02004686 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03004687 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03004688}
4689
Izik Eiduscbc94022007-10-25 00:29:55 +02004690static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
4691{
4692 return 0;
4693}
4694
Avi Kivityd9e368d2007-06-07 19:18:30 +03004695static void svm_flush_tlb(struct kvm_vcpu *vcpu)
4696{
Joerg Roedel38e5e922010-12-03 15:25:16 +01004697 struct vcpu_svm *svm = to_svm(vcpu);
4698
4699 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
4700 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4701 else
4702 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03004703}
4704
Avi Kivity04d2cc72007-09-10 18:10:54 +03004705static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
4706{
4707}
4708
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004709static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
4710{
4711 struct vcpu_svm *svm = to_svm(vcpu);
4712
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004713 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004714 return;
4715
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004716 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004717 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03004718 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004719 }
4720}
4721
Joerg Roedel649d6862008-04-16 16:51:15 +02004722static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
4723{
4724 struct vcpu_svm *svm = to_svm(vcpu);
4725 u64 cr8;
4726
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004727 if (svm_nested_virtualize_tpr(vcpu) ||
4728 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004729 return;
4730
Joerg Roedel649d6862008-04-16 16:51:15 +02004731 cr8 = kvm_get_cr8(vcpu);
4732 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
4733 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
4734}
4735
Gleb Natapov9222be12009-04-23 17:14:37 +03004736static void svm_complete_interrupts(struct vcpu_svm *svm)
4737{
4738 u8 vector;
4739 int type;
4740 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01004741 unsigned int3_injected = svm->int3_injected;
4742
4743 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03004744
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02004745 /*
4746 * If we've made progress since setting HF_IRET_MASK, we've
4747 * executed an IRET and can allow NMI injection.
4748 */
4749 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
4750 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03004751 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03004752 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
4753 }
Gleb Natapov44c11432009-05-11 13:35:52 +03004754
Gleb Natapov9222be12009-04-23 17:14:37 +03004755 svm->vcpu.arch.nmi_injected = false;
4756 kvm_clear_exception_queue(&svm->vcpu);
4757 kvm_clear_interrupt_queue(&svm->vcpu);
4758
4759 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
4760 return;
4761
Avi Kivity3842d132010-07-27 12:30:24 +03004762 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
4763
Gleb Natapov9222be12009-04-23 17:14:37 +03004764 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
4765 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
4766
4767 switch (type) {
4768 case SVM_EXITINTINFO_TYPE_NMI:
4769 svm->vcpu.arch.nmi_injected = true;
4770 break;
4771 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01004772 /*
4773 * In case of software exceptions, do not reinject the vector,
4774 * but re-execute the instruction instead. Rewind RIP first
4775 * if we emulated INT3 before.
4776 */
4777 if (kvm_exception_is_soft(vector)) {
4778 if (vector == BP_VECTOR && int3_injected &&
4779 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
4780 kvm_rip_write(&svm->vcpu,
4781 kvm_rip_read(&svm->vcpu) -
4782 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02004783 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01004784 }
Gleb Natapov9222be12009-04-23 17:14:37 +03004785 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
4786 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02004787 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03004788
4789 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02004790 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03004791 break;
4792 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004793 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03004794 break;
4795 default:
4796 break;
4797 }
4798}
4799
Avi Kivityb463a6f2010-07-20 15:06:17 +03004800static void svm_cancel_injection(struct kvm_vcpu *vcpu)
4801{
4802 struct vcpu_svm *svm = to_svm(vcpu);
4803 struct vmcb_control_area *control = &svm->vmcb->control;
4804
4805 control->exit_int_info = control->event_inj;
4806 control->exit_int_info_err = control->event_inj_err;
4807 control->event_inj = 0;
4808 svm_complete_interrupts(svm);
4809}
4810
Avi Kivity851ba692009-08-24 11:10:17 +03004811static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004812{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004813 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03004814
Joerg Roedel2041a062010-04-22 12:33:08 +02004815 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4816 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4817 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4818
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004819 /*
4820 * A vmexit emulation is required before the vcpu can be executed
4821 * again.
4822 */
4823 if (unlikely(svm->nested.exit_required))
4824 return;
4825
Ladi Proseka12713c2017-06-21 09:07:00 +02004826 /*
4827 * Disable singlestep if we're injecting an interrupt/exception.
4828 * We don't want our modified rflags to be pushed on the stack where
4829 * we might not be able to easily reset them if we disabled NMI
4830 * singlestep later.
4831 */
4832 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
4833 /*
4834 * Event injection happens before external interrupts cause a
4835 * vmexit and interrupts are disabled here, so smp_send_reschedule
4836 * is enough to force an immediate vmexit.
4837 */
4838 disable_nmi_singlestep(svm);
4839 smp_send_reschedule(vcpu->cpu);
4840 }
4841
Rusty Russelle756fc62007-07-30 20:07:08 +10004842 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004843
Joerg Roedel649d6862008-04-16 16:51:15 +02004844 sync_lapic_to_cr8(vcpu);
4845
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02004846 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004847
Avi Kivity04d2cc72007-09-10 18:10:54 +03004848 clgi();
4849
4850 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08004851
Avi Kivity6aa8b732006-12-10 02:21:36 -08004852 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03004853 "push %%" _ASM_BP "; \n\t"
4854 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
4855 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
4856 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
4857 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
4858 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
4859 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004860#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004861 "mov %c[r8](%[svm]), %%r8 \n\t"
4862 "mov %c[r9](%[svm]), %%r9 \n\t"
4863 "mov %c[r10](%[svm]), %%r10 \n\t"
4864 "mov %c[r11](%[svm]), %%r11 \n\t"
4865 "mov %c[r12](%[svm]), %%r12 \n\t"
4866 "mov %c[r13](%[svm]), %%r13 \n\t"
4867 "mov %c[r14](%[svm]), %%r14 \n\t"
4868 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869#endif
4870
Avi Kivity6aa8b732006-12-10 02:21:36 -08004871 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03004872 "push %%" _ASM_AX " \n\t"
4873 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004874 __ex(SVM_VMLOAD) "\n\t"
4875 __ex(SVM_VMRUN) "\n\t"
4876 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03004877 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004878
4879 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03004880 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
4881 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
4882 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
4883 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
4884 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
4885 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004886#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004887 "mov %%r8, %c[r8](%[svm]) \n\t"
4888 "mov %%r9, %c[r9](%[svm]) \n\t"
4889 "mov %%r10, %c[r10](%[svm]) \n\t"
4890 "mov %%r11, %c[r11](%[svm]) \n\t"
4891 "mov %%r12, %c[r12](%[svm]) \n\t"
4892 "mov %%r13, %c[r13](%[svm]) \n\t"
4893 "mov %%r14, %c[r14](%[svm]) \n\t"
4894 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004895#endif
Avi Kivity74547662012-09-16 15:10:59 +03004896 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08004897 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004898 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08004899 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004900 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
4901 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
4902 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
4903 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
4904 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
4905 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004906#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004907 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
4908 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
4909 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
4910 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
4911 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
4912 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
4913 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
4914 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004915#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02004916 : "cc", "memory"
4917#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03004918 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02004919 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03004920#else
4921 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02004922#endif
4923 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08004924
Avi Kivity82ca2d12010-10-21 12:20:34 +02004925#ifdef CONFIG_X86_64
4926 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
4927#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02004928 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02004929#ifndef CONFIG_X86_32_LAZY_GS
4930 loadsegment(gs, svm->host.gs);
4931#endif
Avi Kivity9581d442010-10-19 16:46:55 +02004932#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004933
4934 reload_tss(vcpu);
4935
Avi Kivity56ba47d2007-11-07 17:14:18 +02004936 local_irq_disable();
4937
Avi Kivity13c34e02010-10-21 12:20:31 +02004938 vcpu->arch.cr2 = svm->vmcb->save.cr2;
4939 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
4940 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
4941 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
4942
Joerg Roedel3781c012011-01-14 16:45:02 +01004943 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
4944 kvm_before_handle_nmi(&svm->vcpu);
4945
4946 stgi();
4947
4948 /* Any pending NMI will happen here */
4949
4950 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
4951 kvm_after_handle_nmi(&svm->vcpu);
4952
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004953 sync_cr8_to_lapic(vcpu);
4954
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004955 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03004956
Joerg Roedel38e5e922010-12-03 15:25:16 +01004957 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
4958
Gleb Natapov631bc482010-10-14 11:22:52 +02004959 /* if exit due to PF check for async PF */
4960 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
4961 svm->apf_reason = kvm_read_and_reset_pf_reason();
4962
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004963 if (npt_enabled) {
4964 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
4965 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
4966 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02004967
4968 /*
4969 * We need to handle MC intercepts here before the vcpu has a chance to
4970 * change the physical cpu
4971 */
4972 if (unlikely(svm->vmcb->control.exit_code ==
4973 SVM_EXIT_EXCP_BASE + MC_VECTOR))
4974 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01004975
4976 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977}
4978
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
4980{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004981 struct vcpu_svm *svm = to_svm(vcpu);
4982
4983 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01004984 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01004985 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004986}
4987
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004988static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
4989{
4990 struct vcpu_svm *svm = to_svm(vcpu);
4991
4992 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01004993 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004994
4995 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02004996 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01004997 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004998
Joerg Roedelf40f6a42010-12-03 15:25:15 +01004999 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005000}
5001
Avi Kivity6aa8b732006-12-10 02:21:36 -08005002static int is_disabled(void)
5003{
Joerg Roedel6031a612007-06-22 12:29:50 +03005004 u64 vm_cr;
5005
5006 rdmsrl(MSR_VM_CR, vm_cr);
5007 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5008 return 1;
5009
Avi Kivity6aa8b732006-12-10 02:21:36 -08005010 return 0;
5011}
5012
Ingo Molnar102d8322007-02-19 14:37:47 +02005013static void
5014svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5015{
5016 /*
5017 * Patch in the VMMCALL instruction:
5018 */
5019 hypercall[0] = 0x0f;
5020 hypercall[1] = 0x01;
5021 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02005022}
5023
Yang, Sheng002c7f72007-07-31 14:23:01 +03005024static void svm_check_processor_compat(void *rtn)
5025{
5026 *(int *)rtn = 0;
5027}
5028
Avi Kivity774ead32007-12-26 13:57:04 +02005029static bool svm_cpu_has_accelerated_tpr(void)
5030{
5031 return false;
5032}
5033
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005034static bool svm_has_high_real_mode_segbase(void)
5035{
5036 return true;
5037}
5038
Paolo Bonzinifc07e762015-10-01 13:20:22 +02005039static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5040{
5041 return 0;
5042}
5043
Sheng Yang0e851882009-12-18 16:48:46 +08005044static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5045{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005046 struct vcpu_svm *svm = to_svm(vcpu);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005047 struct kvm_cpuid_entry2 *entry;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005048
5049 /* Update nrips enabled cache */
5050 svm->nrips_enabled = !!guest_cpuid_has_nrips(&svm->vcpu);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005051
5052 if (!kvm_vcpu_apicv_active(vcpu))
5053 return;
5054
5055 entry = kvm_find_cpuid_entry(vcpu, 1, 0);
5056 if (entry)
5057 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08005058}
5059
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005060static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5061{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005062 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005063 case 0x1:
5064 if (avic)
5065 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5066 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02005067 case 0x80000001:
5068 if (nested)
5069 entry->ecx |= (1 << 2); /* Set SVM bit */
5070 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005071 case 0x8000000A:
5072 entry->eax = 1; /* SVM revision 1 */
5073 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5074 ASID emulation to nested SVM */
5075 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02005076 entry->edx = 0; /* Per default do not support any
5077 additional features */
5078
5079 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02005080 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02005081 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005082
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005083 /* Support NPT for the guest if enabled */
5084 if (npt_enabled)
5085 entry->edx |= SVM_FEATURE_NPT;
5086
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005087 break;
5088 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005089}
5090
Sheng Yang17cc3932010-01-05 19:02:27 +08005091static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005092{
Sheng Yang17cc3932010-01-05 19:02:27 +08005093 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005094}
5095
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005096static bool svm_rdtscp_supported(void)
5097{
Paolo Bonzini46896c72015-11-12 14:49:16 +01005098 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005099}
5100
Mao, Junjiead756a12012-07-02 01:18:48 +00005101static bool svm_invpcid_supported(void)
5102{
5103 return false;
5104}
5105
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005106static bool svm_mpx_supported(void)
5107{
5108 return false;
5109}
5110
Wanpeng Li55412b22014-12-02 19:21:30 +08005111static bool svm_xsaves_supported(void)
5112{
5113 return false;
5114}
5115
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005116static bool svm_has_wbinvd_exit(void)
5117{
5118 return true;
5119}
5120
Joerg Roedel80612522011-04-04 12:39:33 +02005121#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005122 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005123#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005124 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005125#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005126 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005127
Mathias Krause09941fb2012-08-30 01:30:20 +02005128static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005129 u32 exit_code;
5130 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005131} x86_intercept_map[] = {
5132 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
5133 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
5134 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
5135 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
5136 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02005137 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
5138 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02005139 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
5140 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
5141 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
5142 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
5143 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
5144 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
5145 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
5146 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02005147 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
5148 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
5149 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
5150 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
5151 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
5152 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
5153 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
5154 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005155 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
5156 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
5157 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02005158 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
5159 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
5160 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
5161 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
5162 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
5163 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
5164 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
5165 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
5166 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02005167 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
5168 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
5169 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
5170 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
5171 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
5172 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
5173 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02005174 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
5175 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
5176 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
5177 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005178};
5179
Joerg Roedel80612522011-04-04 12:39:33 +02005180#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005181#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005182#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005183
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005184static int svm_check_intercept(struct kvm_vcpu *vcpu,
5185 struct x86_instruction_info *info,
5186 enum x86_intercept_stage stage)
5187{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005188 struct vcpu_svm *svm = to_svm(vcpu);
5189 int vmexit, ret = X86EMUL_CONTINUE;
5190 struct __x86_intercept icpt_info;
5191 struct vmcb *vmcb = svm->vmcb;
5192
5193 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
5194 goto out;
5195
5196 icpt_info = x86_intercept_map[info->intercept];
5197
Avi Kivity40e19b52011-04-21 12:35:41 +03005198 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005199 goto out;
5200
5201 switch (icpt_info.exit_code) {
5202 case SVM_EXIT_READ_CR0:
5203 if (info->intercept == x86_intercept_cr_read)
5204 icpt_info.exit_code += info->modrm_reg;
5205 break;
5206 case SVM_EXIT_WRITE_CR0: {
5207 unsigned long cr0, val;
5208 u64 intercept;
5209
5210 if (info->intercept == x86_intercept_cr_write)
5211 icpt_info.exit_code += info->modrm_reg;
5212
Jan Kiszka62baf442014-06-29 21:55:53 +02005213 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
5214 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005215 break;
5216
5217 intercept = svm->nested.intercept;
5218
5219 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
5220 break;
5221
5222 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
5223 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
5224
5225 if (info->intercept == x86_intercept_lmsw) {
5226 cr0 &= 0xfUL;
5227 val &= 0xfUL;
5228 /* lmsw can't clear PE - catch this here */
5229 if (cr0 & X86_CR0_PE)
5230 val |= X86_CR0_PE;
5231 }
5232
5233 if (cr0 ^ val)
5234 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
5235
5236 break;
5237 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02005238 case SVM_EXIT_READ_DR0:
5239 case SVM_EXIT_WRITE_DR0:
5240 icpt_info.exit_code += info->modrm_reg;
5241 break;
Joerg Roedel80612522011-04-04 12:39:33 +02005242 case SVM_EXIT_MSR:
5243 if (info->intercept == x86_intercept_wrmsr)
5244 vmcb->control.exit_info_1 = 1;
5245 else
5246 vmcb->control.exit_info_1 = 0;
5247 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02005248 case SVM_EXIT_PAUSE:
5249 /*
5250 * We get this for NOP only, but pause
5251 * is rep not, check this here
5252 */
5253 if (info->rep_prefix != REPE_PREFIX)
5254 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02005255 case SVM_EXIT_IOIO: {
5256 u64 exit_info;
5257 u32 bytes;
5258
Joerg Roedelf6511932011-04-04 12:39:35 +02005259 if (info->intercept == x86_intercept_in ||
5260 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005261 exit_info = ((info->src_val & 0xffff) << 16) |
5262 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02005263 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02005264 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005265 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02005266 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02005267 }
5268
5269 if (info->intercept == x86_intercept_outs ||
5270 info->intercept == x86_intercept_ins)
5271 exit_info |= SVM_IOIO_STR_MASK;
5272
5273 if (info->rep_prefix)
5274 exit_info |= SVM_IOIO_REP_MASK;
5275
5276 bytes = min(bytes, 4u);
5277
5278 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
5279
5280 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
5281
5282 vmcb->control.exit_info_1 = exit_info;
5283 vmcb->control.exit_info_2 = info->next_rip;
5284
5285 break;
5286 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005287 default:
5288 break;
5289 }
5290
Bandan Dasf1047652015-06-11 02:05:33 -04005291 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
5292 if (static_cpu_has(X86_FEATURE_NRIPS))
5293 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005294 vmcb->control.exit_code = icpt_info.exit_code;
5295 vmexit = nested_svm_exit_handled(svm);
5296
5297 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
5298 : X86EMUL_CONTINUE;
5299
5300out:
5301 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005302}
5303
Yang Zhanga547c6d2013-04-11 19:25:10 +08005304static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
5305{
5306 local_irq_enable();
Paolo Bonzinif2485b32016-06-15 15:23:11 +02005307 /*
5308 * We must have an instruction with interrupts enabled, so
5309 * the timer interrupt isn't delayed by the interrupt shadow.
5310 */
5311 asm("nop");
5312 local_irq_disable();
Yang Zhanga547c6d2013-04-11 19:25:10 +08005313}
5314
Radim Krčmářae97a3b2014-08-21 18:08:06 +02005315static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
5316{
5317}
5318
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05005319static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
5320{
5321 if (avic_handle_apic_id_update(vcpu) != 0)
5322 return;
5323 if (avic_handle_dfr_update(vcpu) != 0)
5324 return;
5325 avic_handle_ldr_update(vcpu);
5326}
5327
Borislav Petkov74f16902017-03-26 23:51:24 +02005328static void svm_setup_mce(struct kvm_vcpu *vcpu)
5329{
5330 /* [63:9] are reserved. */
5331 vcpu->arch.mcg_cap &= 0x1ff;
5332}
5333
Kees Cook404f6aa2016-08-08 16:29:06 -07005334static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005335 .cpu_has_kvm_support = has_svm,
5336 .disabled_by_bios = is_disabled,
5337 .hardware_setup = svm_hardware_setup,
5338 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03005339 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005340 .hardware_enable = svm_hardware_enable,
5341 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02005342 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005343 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005344
5345 .vcpu_create = svm_create_vcpu,
5346 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03005347 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005348
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005349 .vm_init = avic_vm_init,
5350 .vm_destroy = avic_vm_destroy,
5351
Avi Kivity04d2cc72007-09-10 18:10:54 +03005352 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005353 .vcpu_load = svm_vcpu_load,
5354 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05005355 .vcpu_blocking = svm_vcpu_blocking,
5356 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005357
Paolo Bonzinia96036b2015-11-10 11:55:36 +01005358 .update_bp_intercept = update_bp_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005359 .get_msr = svm_get_msr,
5360 .set_msr = svm_set_msr,
5361 .get_segment_base = svm_get_segment_base,
5362 .get_segment = svm_get_segment,
5363 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02005364 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10005365 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02005366 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02005367 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03005368 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005369 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005370 .set_cr3 = svm_set_cr3,
5371 .set_cr4 = svm_set_cr4,
5372 .set_efer = svm_set_efer,
5373 .get_idt = svm_get_idt,
5374 .set_idt = svm_set_idt,
5375 .get_gdt = svm_get_gdt,
5376 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005377 .get_dr6 = svm_get_dr6,
5378 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03005379 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01005380 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005381 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005382 .get_rflags = svm_get_rflags,
5383 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005384
5385 .get_pkru = svm_get_pkru,
5386
Avi Kivity6aa8b732006-12-10 02:21:36 -08005387 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005388
Avi Kivity6aa8b732006-12-10 02:21:36 -08005389 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03005390 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005391 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04005392 .set_interrupt_shadow = svm_set_interrupt_shadow,
5393 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02005394 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03005395 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005396 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02005397 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03005398 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02005399 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005400 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005401 .get_nmi_mask = svm_get_nmi_mask,
5402 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005403 .enable_nmi_window = enable_nmi_window,
5404 .enable_irq_window = enable_irq_window,
5405 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08005406 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03005407 .get_enable_apicv = svm_get_enable_apicv,
5408 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005409 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005410 .hwapic_irr_update = svm_hwapic_irr_update,
5411 .hwapic_isr_update = svm_hwapic_isr_update,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05005412 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02005413
5414 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08005415 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005416 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005417
Avi Kivity586f9602010-11-18 13:09:54 +02005418 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02005419
Sheng Yang17cc3932010-01-05 19:02:27 +08005420 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08005421
5422 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005423
5424 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00005425 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005426 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08005427 .xsaves_supported = svm_xsaves_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005428
5429 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005430
5431 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10005432
5433 .write_tsc_offset = svm_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005434
5435 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005436
5437 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08005438 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02005439
5440 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02005441
5442 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005443 .deliver_posted_interrupt = svm_deliver_avic_intr,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005444 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02005445 .setup_mce = svm_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005446};
5447
5448static int __init svm_init(void)
5449{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08005450 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03005451 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005452}
5453
5454static void __exit svm_exit(void)
5455{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08005456 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08005457}
5458
5459module_init(svm_init)
5460module_exit(svm_exit)