blob: 1086bebaeea3fb9f1137683e16f5b58078bca82c [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>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050039#include <linux/frame.h>
Brijesh Singhe9df0942017-12-04 10:57:33 -060040#include <linux/psp-sev.h>
Brijesh Singh1654efc2017-12-04 10:57:34 -060041#include <linux/file.h>
Brijesh Singh89c50582017-12-04 10:57:35 -060042#include <linux/pagemap.h>
43#include <linux/swap.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050045#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010046#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020047#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040048#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020050#include <asm/kvm_para.h>
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -050051#include <asm/irq_remapping.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020052#include <asm/spec-ctrl.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080053
Eduardo Habkost63d11422008-11-17 19:03:20 -020054#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030055#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020056
Avi Kivity4ecac3f2008-05-13 13:23:38 +030057#define __ex(x) __kvm_handle_fault_on_reboot(x)
58
Avi Kivity6aa8b732006-12-10 02:21:36 -080059MODULE_AUTHOR("Qumranet");
60MODULE_LICENSE("GPL");
61
Josh Triplettae759542012-03-28 11:32:28 -070062static const struct x86_cpu_id svm_cpu_id[] = {
63 X86_FEATURE_MATCH(X86_FEATURE_SVM),
64 {}
65};
66MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
67
Avi Kivity6aa8b732006-12-10 02:21:36 -080068#define IOPM_ALLOC_ORDER 2
69#define MSRPM_ALLOC_ORDER 1
70
Avi Kivity6aa8b732006-12-10 02:21:36 -080071#define SEG_TYPE_LDT 2
72#define SEG_TYPE_BUSY_TSS16 3
73
Andre Przywara6bc31bd2010-04-11 23:07:28 +020074#define SVM_FEATURE_NPT (1 << 0)
75#define SVM_FEATURE_LBRV (1 << 1)
76#define SVM_FEATURE_SVML (1 << 2)
77#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010078#define SVM_FEATURE_TSC_RATE (1 << 4)
79#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
80#define SVM_FEATURE_FLUSH_ASID (1 << 6)
81#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020082#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030083
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -050084#define SVM_AVIC_DOORBELL 0xc001011b
85
Joerg Roedel410e4d52009-08-07 11:49:44 +020086#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
87#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
88#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
89
Joerg Roedel24e09cb2008-02-13 18:58:47 +010090#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
91
Joerg Roedelfbc0db72011-03-25 09:44:46 +010092#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010093#define TSC_RATIO_MIN 0x0000000000000001ULL
94#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010095
Dan Carpenter5446a972016-05-23 13:20:10 +030096#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050097
98/*
99 * 0xff is broadcast, so the max index allowed for physical APIC ID
100 * table is 0xfe. APIC IDs above 0xff are reserved.
101 */
102#define AVIC_MAX_PHYSICAL_ID_COUNT 255
103
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500104#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
105#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
106#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
107
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500108/* AVIC GATAG is encoded using VM and VCPU IDs */
109#define AVIC_VCPU_ID_BITS 8
110#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
111
112#define AVIC_VM_ID_BITS 24
113#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
114#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
115
116#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
117 (y & AVIC_VCPU_ID_MASK))
118#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
119#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
120
Joerg Roedel67ec6602010-05-17 14:43:35 +0200121static bool erratum_383_found __read_mostly;
122
Avi Kivity6c8166a2009-05-31 18:15:37 +0300123static const u32 host_save_user_msrs[] = {
124#ifdef CONFIG_X86_64
125 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
126 MSR_FS_BASE,
127#endif
128 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Paolo Bonzini46896c72015-11-12 14:49:16 +0100129 MSR_TSC_AUX,
Avi Kivity6c8166a2009-05-31 18:15:37 +0300130};
131
132#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
133
Sean Christopherson81811c12018-03-20 12:17:21 -0700134struct kvm_sev_info {
135 bool active; /* SEV enabled guest */
136 unsigned int asid; /* ASID used for this guest */
137 unsigned int handle; /* SEV firmware handle */
138 int fd; /* SEV device fd */
139 unsigned long pages_locked; /* Number of pages locked */
140 struct list_head regions_list; /* List of registered regions */
141};
142
143struct kvm_svm {
144 struct kvm kvm;
145
146 /* Struct members for AVIC */
147 u32 avic_vm_id;
148 u32 ldr_mode;
149 struct page *avic_logical_id_table_page;
150 struct page *avic_physical_id_table_page;
151 struct hlist_node hnode;
152
153 struct kvm_sev_info sev_info;
154};
155
Avi Kivity6c8166a2009-05-31 18:15:37 +0300156struct kvm_vcpu;
157
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200158struct nested_state {
159 struct vmcb *hsave;
160 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +0100161 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200162 u64 vmcb;
163
164 /* These are the merged vectors */
165 u32 *msrpm;
166
167 /* gpa pointers to the real vectors */
168 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100169 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200170
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200171 /* A VMEXIT is required but not yet emulated */
172 bool exit_required;
173
Joerg Roedelaad42c62009-08-07 11:49:34 +0200174 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100175 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100176 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200177 u32 intercept_exceptions;
178 u64 intercept;
179
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200180 /* Nested Paging related state */
181 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200182};
183
Joerg Roedel323c3d82010-03-01 15:34:37 +0100184#define MSRPM_OFFSETS 16
185static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
186
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500187/*
188 * Set osvw_len to higher value when updated Revision Guides
189 * are published and we know what the new status bits are
190 */
191static uint64_t osvw_len = 4, osvw_status;
192
Avi Kivity6c8166a2009-05-31 18:15:37 +0300193struct vcpu_svm {
194 struct kvm_vcpu vcpu;
195 struct vmcb *vmcb;
196 unsigned long vmcb_pa;
197 struct svm_cpu_data *svm_data;
198 uint64_t asid_generation;
199 uint64_t sysenter_esp;
200 uint64_t sysenter_eip;
Paolo Bonzini46896c72015-11-12 14:49:16 +0100201 uint64_t tsc_aux;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300202
Tom Lendackyd1d93fa2018-02-24 00:18:20 +0100203 u64 msr_decfg;
204
Avi Kivity6c8166a2009-05-31 18:15:37 +0300205 u64 next_rip;
206
207 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200208 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200209 u16 fs;
210 u16 gs;
211 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200212 u64 gs_base;
213 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300214
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100215 u64 spec_ctrl;
Thomas Gleixnerccbcd262018-05-09 23:01:01 +0200216 /*
217 * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
218 * translated into the appropriate L2_CFG bits on the host to
219 * perform speculative control.
220 */
221 u64 virt_spec_ctrl;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100222
Avi Kivity6c8166a2009-05-31 18:15:37 +0300223 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300224
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200225 ulong nmi_iret_rip;
226
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200227 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200228
229 bool nmi_singlestep;
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200230 u64 nmi_singlestep_guest_rflags;
Jan Kiszka66b71382010-02-23 17:47:56 +0100231
232 unsigned int3_injected;
233 unsigned long int3_rip;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100234
Joerg Roedel6092d3d2015-10-14 15:10:54 +0200235 /* cached guest cpuid flags for faster access */
236 bool nrips_enabled : 1;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500237
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500238 u32 ldr_reg;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500239 struct page *avic_backing_page;
240 u64 *avic_physical_id_cache;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -0500241 bool avic_is_running;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500242
243 /*
244 * Per-vcpu list of struct amd_svm_iommu_ir:
245 * This is used mainly to store interrupt remapping information used
246 * when update the vcpu affinity. This avoids the need to scan for
247 * IRTE and try to match ga_tag in the IOMMU driver.
248 */
249 struct list_head ir_list;
250 spinlock_t ir_list_lock;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600251
252 /* which host CPU was used for running this vcpu */
253 unsigned int last_cpu;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500254};
255
256/*
257 * This is a wrapper of struct amd_iommu_ir_data.
258 */
259struct amd_svm_iommu_ir {
260 struct list_head node; /* Used by SVM for per-vcpu ir_list */
261 void *data; /* Storing pointer to struct amd_ir_data */
Avi Kivity6c8166a2009-05-31 18:15:37 +0300262};
263
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500264#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
265#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
266
267#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
268#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
269#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
270#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
271
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100272static DEFINE_PER_CPU(u64, current_tsc_ratio);
273#define TSC_RATIO_DEFAULT 0x0100000000ULL
274
Joerg Roedel455716f2010-03-01 15:34:35 +0100275#define MSR_INVALID 0xffffffffU
276
Mathias Krause09941fb2012-08-30 01:30:20 +0200277static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100278 u32 index; /* Index of the MSR */
279 bool always; /* True if intercept is always on */
280} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400281 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100282 { .index = MSR_IA32_SYSENTER_CS, .always = true },
283#ifdef CONFIG_X86_64
284 { .index = MSR_GS_BASE, .always = true },
285 { .index = MSR_FS_BASE, .always = true },
286 { .index = MSR_KERNEL_GS_BASE, .always = true },
287 { .index = MSR_LSTAR, .always = true },
288 { .index = MSR_CSTAR, .always = true },
289 { .index = MSR_SYSCALL_MASK, .always = true },
290#endif
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100291 { .index = MSR_IA32_SPEC_CTRL, .always = false },
Ashok Raj15d45072018-02-01 22:59:43 +0100292 { .index = MSR_IA32_PRED_CMD, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100293 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
294 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
295 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
296 { .index = MSR_IA32_LASTINTTOIP, .always = false },
297 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298};
299
300/* enable NPT for AMD64 and X86 with PAE */
301#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
302static bool npt_enabled = true;
303#else
Joerg Roedele0231712010-02-24 18:59:10 +0100304static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305#endif
306
Babu Moger8566ac82018-03-16 16:37:26 -0400307/*
308 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
309 * pause_filter_count: On processors that support Pause filtering(indicated
310 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
311 * count value. On VMRUN this value is loaded into an internal counter.
312 * Each time a pause instruction is executed, this counter is decremented
313 * until it reaches zero at which time a #VMEXIT is generated if pause
314 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
315 * Intercept Filtering for more details.
316 * This also indicate if ple logic enabled.
317 *
318 * pause_filter_thresh: In addition, some processor families support advanced
319 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
320 * the amount of time a guest is allowed to execute in a pause loop.
321 * In this mode, a 16-bit pause filter threshold field is added in the
322 * VMCB. The threshold value is a cycle count that is used to reset the
323 * pause counter. As with simple pause filtering, VMRUN loads the pause
324 * count value from VMCB into an internal counter. Then, on each pause
325 * instruction the hardware checks the elapsed number of cycles since
326 * the most recent pause instruction against the pause filter threshold.
327 * If the elapsed cycle count is greater than the pause filter threshold,
328 * then the internal pause count is reloaded from the VMCB and execution
329 * continues. If the elapsed cycle count is less than the pause filter
330 * threshold, then the internal pause count is decremented. If the count
331 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
332 * triggered. If advanced pause filtering is supported and pause filter
333 * threshold field is set to zero, the filter will operate in the simpler,
334 * count only mode.
335 */
336
337static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
338module_param(pause_filter_thresh, ushort, 0444);
339
340static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
341module_param(pause_filter_count, ushort, 0444);
342
343/* Default doubles per-vcpu window every exit. */
344static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
345module_param(pause_filter_count_grow, ushort, 0444);
346
347/* Default resets per-vcpu window every exit to pause_filter_count. */
348static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
349module_param(pause_filter_count_shrink, ushort, 0444);
350
351/* Default is to compute the maximum so we can never overflow. */
352static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
353module_param(pause_filter_count_max, ushort, 0444);
354
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100355/* allow nested paging (virtualized MMU) for all guests */
356static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357module_param(npt, int, S_IRUGO);
358
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100359/* allow nested virtualization in KVM/SVM */
360static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361module_param(nested, int, S_IRUGO);
362
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500363/* enable / disable AVIC */
364static int avic;
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500365#ifdef CONFIG_X86_LOCAL_APIC
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500366module_param(avic, int, S_IRUGO);
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500367#endif
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500368
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -0500369/* enable/disable Virtual VMLOAD VMSAVE */
370static int vls = true;
371module_param(vls, int, 0444);
372
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500373/* enable/disable Virtual GIF */
374static int vgif = true;
375module_param(vgif, int, 0444);
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500376
Brijesh Singhe9df0942017-12-04 10:57:33 -0600377/* enable/disable SEV support */
378static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
379module_param(sev, int, 0444);
380
Brijesh Singh7607b712018-02-19 10:14:44 -0600381static u8 rsm_ins_bytes[] = "\x0f\xaa";
382
Paolo Bonzini79a80592015-09-21 07:46:55 +0200383static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -0800384static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa);
Joerg Roedela5c38322009-08-07 11:49:32 +0200385static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386
Joerg Roedel410e4d52009-08-07 11:49:44 +0200387static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100388static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800389static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
391 bool has_error_code, u32 error_code);
392
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100393enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100394 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
395 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100396 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100397 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100398 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100399 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100400 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100401 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100402 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100403 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100404 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100405 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500406 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
407 * AVIC PHYSICAL_TABLE pointer,
408 * AVIC LOGICAL_TABLE pointer
409 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100410 VMCB_DIRTY_MAX,
411};
412
Joerg Roedel0574dec2010-12-03 11:45:58 +0100413/* TPR and CR2 are always written before VMRUN */
414#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100415
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500416#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
417
Brijesh Singhed3cd232017-12-04 10:57:32 -0600418static unsigned int max_sev_asid;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600419static unsigned int min_sev_asid;
420static unsigned long *sev_asid_bitmap;
Brijesh Singh89c50582017-12-04 10:57:35 -0600421#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
Brijesh Singh1654efc2017-12-04 10:57:34 -0600422
Brijesh Singh1e80fdc2017-12-04 10:57:38 -0600423struct enc_region {
424 struct list_head list;
425 unsigned long npages;
426 struct page **pages;
427 unsigned long uaddr;
428 unsigned long size;
429};
430
Sean Christopherson81811c12018-03-20 12:17:21 -0700431
432static inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
433{
434 return container_of(kvm, struct kvm_svm, kvm);
435}
436
Brijesh Singh1654efc2017-12-04 10:57:34 -0600437static inline bool svm_sev_enabled(void)
438{
439 return max_sev_asid;
440}
441
442static inline bool sev_guest(struct kvm *kvm)
443{
Sean Christopherson81811c12018-03-20 12:17:21 -0700444 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600445
446 return sev->active;
447}
Brijesh Singhed3cd232017-12-04 10:57:32 -0600448
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600449static inline int sev_get_asid(struct kvm *kvm)
450{
Sean Christopherson81811c12018-03-20 12:17:21 -0700451 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600452
453 return sev->asid;
454}
455
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100456static inline void mark_all_dirty(struct vmcb *vmcb)
457{
458 vmcb->control.clean = 0;
459}
460
461static inline void mark_all_clean(struct vmcb *vmcb)
462{
463 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
464 & ~VMCB_ALWAYS_DIRTY_MASK;
465}
466
467static inline void mark_dirty(struct vmcb *vmcb, int bit)
468{
469 vmcb->control.clean &= ~(1 << bit);
470}
471
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
473{
474 return container_of(vcpu, struct vcpu_svm, vcpu);
475}
476
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500477static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
478{
479 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
480 mark_dirty(svm->vmcb, VMCB_AVIC);
481}
482
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -0500483static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
484{
485 struct vcpu_svm *svm = to_svm(vcpu);
486 u64 *entry = svm->avic_physical_id_cache;
487
488 if (!entry)
489 return false;
490
491 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
492}
493
Joerg Roedel384c6362010-11-30 18:03:56 +0100494static void recalc_intercepts(struct vcpu_svm *svm)
495{
496 struct vmcb_control_area *c, *h;
497 struct nested_state *g;
498
Joerg Roedel116a0a22010-12-03 11:45:49 +0100499 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
500
Joerg Roedel384c6362010-11-30 18:03:56 +0100501 if (!is_guest_mode(&svm->vcpu))
502 return;
503
504 c = &svm->vmcb->control;
505 h = &svm->nested.hsave->control;
506 g = &svm->nested;
507
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100508 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100509 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Paolo Bonzinibd895252018-01-11 16:55:24 +0100510 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
Joerg Roedel384c6362010-11-30 18:03:56 +0100511 c->intercept = h->intercept | g->intercept;
512}
513
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100514static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
515{
516 if (is_guest_mode(&svm->vcpu))
517 return svm->nested.hsave;
518 else
519 return svm->vmcb;
520}
521
522static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
523{
524 struct vmcb *vmcb = get_host_vmcb(svm);
525
526 vmcb->control.intercept_cr |= (1U << bit);
527
528 recalc_intercepts(svm);
529}
530
531static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
532{
533 struct vmcb *vmcb = get_host_vmcb(svm);
534
535 vmcb->control.intercept_cr &= ~(1U << bit);
536
537 recalc_intercepts(svm);
538}
539
540static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
541{
542 struct vmcb *vmcb = get_host_vmcb(svm);
543
544 return vmcb->control.intercept_cr & (1U << bit);
545}
546
Paolo Bonzini5315c712014-03-03 13:08:29 +0100547static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100548{
549 struct vmcb *vmcb = get_host_vmcb(svm);
550
Paolo Bonzini5315c712014-03-03 13:08:29 +0100551 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
552 | (1 << INTERCEPT_DR1_READ)
553 | (1 << INTERCEPT_DR2_READ)
554 | (1 << INTERCEPT_DR3_READ)
555 | (1 << INTERCEPT_DR4_READ)
556 | (1 << INTERCEPT_DR5_READ)
557 | (1 << INTERCEPT_DR6_READ)
558 | (1 << INTERCEPT_DR7_READ)
559 | (1 << INTERCEPT_DR0_WRITE)
560 | (1 << INTERCEPT_DR1_WRITE)
561 | (1 << INTERCEPT_DR2_WRITE)
562 | (1 << INTERCEPT_DR3_WRITE)
563 | (1 << INTERCEPT_DR4_WRITE)
564 | (1 << INTERCEPT_DR5_WRITE)
565 | (1 << INTERCEPT_DR6_WRITE)
566 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100567
568 recalc_intercepts(svm);
569}
570
Paolo Bonzini5315c712014-03-03 13:08:29 +0100571static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100572{
573 struct vmcb *vmcb = get_host_vmcb(svm);
574
Paolo Bonzini5315c712014-03-03 13:08:29 +0100575 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100576
577 recalc_intercepts(svm);
578}
579
Joerg Roedel18c918c2010-11-30 18:03:59 +0100580static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
581{
582 struct vmcb *vmcb = get_host_vmcb(svm);
583
584 vmcb->control.intercept_exceptions |= (1U << bit);
585
586 recalc_intercepts(svm);
587}
588
589static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
590{
591 struct vmcb *vmcb = get_host_vmcb(svm);
592
593 vmcb->control.intercept_exceptions &= ~(1U << bit);
594
595 recalc_intercepts(svm);
596}
597
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100598static inline void set_intercept(struct vcpu_svm *svm, int bit)
599{
600 struct vmcb *vmcb = get_host_vmcb(svm);
601
602 vmcb->control.intercept |= (1ULL << bit);
603
604 recalc_intercepts(svm);
605}
606
607static inline void clr_intercept(struct vcpu_svm *svm, int bit)
608{
609 struct vmcb *vmcb = get_host_vmcb(svm);
610
611 vmcb->control.intercept &= ~(1ULL << bit);
612
613 recalc_intercepts(svm);
614}
615
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500616static inline bool vgif_enabled(struct vcpu_svm *svm)
617{
618 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
619}
620
Joerg Roedel2af91942009-08-07 11:49:28 +0200621static inline void enable_gif(struct vcpu_svm *svm)
622{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500623 if (vgif_enabled(svm))
624 svm->vmcb->control.int_ctl |= V_GIF_MASK;
625 else
626 svm->vcpu.arch.hflags |= HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200627}
628
629static inline void disable_gif(struct vcpu_svm *svm)
630{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500631 if (vgif_enabled(svm))
632 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
633 else
634 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200635}
636
637static inline bool gif_set(struct vcpu_svm *svm)
638{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500639 if (vgif_enabled(svm))
640 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
641 else
642 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
Joerg Roedel2af91942009-08-07 11:49:28 +0200643}
644
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645static unsigned long iopm_base;
646
647struct kvm_ldttss_desc {
648 u16 limit0;
649 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100650 unsigned base1:8, type:5, dpl:2, p:1;
651 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800652 u32 base3;
653 u32 zero1;
654} __attribute__((packed));
655
656struct svm_cpu_data {
657 int cpu;
658
Avi Kivity5008fdf2007-04-02 13:05:50 +0300659 u64 asid_generation;
660 u32 max_asid;
661 u32 next_asid;
Brijesh Singh4faefff2017-12-04 10:57:25 -0600662 u32 min_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663 struct kvm_ldttss_desc *tss_desc;
664
665 struct page *save_area;
Ashok Raj15d45072018-02-01 22:59:43 +0100666 struct vmcb *current_vmcb;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600667
668 /* index = sev_asid, value = vmcb pointer */
669 struct vmcb **sev_vmcbs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800670};
671
672static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
673
674struct svm_init_data {
675 int cpu;
676 int r;
677};
678
Mathias Krause09941fb2012-08-30 01:30:20 +0200679static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200681#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682#define MSRS_RANGE_SIZE 2048
683#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
684
Joerg Roedel455716f2010-03-01 15:34:35 +0100685static u32 svm_msrpm_offset(u32 msr)
686{
687 u32 offset;
688 int i;
689
690 for (i = 0; i < NUM_MSR_MAPS; i++) {
691 if (msr < msrpm_ranges[i] ||
692 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
693 continue;
694
695 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
696 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
697
698 /* Now we have the u8 offset - but need the u32 offset */
699 return offset / 4;
700 }
701
702 /* MSR not in any range */
703 return MSR_INVALID;
704}
705
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706#define MAX_INST_SIZE 15
707
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708static inline void clgi(void)
709{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300710 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711}
712
713static inline void stgi(void)
714{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300715 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716}
717
718static inline void invlpga(unsigned long addr, u32 asid)
719{
Joerg Roedele0231712010-02-24 18:59:10 +0100720 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721}
722
Yu Zhang855feb62017-08-24 20:27:55 +0800723static int get_npt_level(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +0200724{
725#ifdef CONFIG_X86_64
Yu Zhang2a7266a2017-08-24 20:27:54 +0800726 return PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200727#else
728 return PT32E_ROOT_LEVEL;
729#endif
730}
731
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
733{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000734 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100735 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600736 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737
Alexander Graf9962d032008-11-25 20:17:02 +0100738 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100739 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800740}
741
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742static int is_external_interrupt(u32 info)
743{
744 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
745 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
746}
747
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200748static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400749{
750 struct vcpu_svm *svm = to_svm(vcpu);
751 u32 ret = 0;
752
753 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200754 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
755 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400756}
757
758static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
759{
760 struct vcpu_svm *svm = to_svm(vcpu);
761
762 if (mask == 0)
763 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
764 else
765 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
766
767}
768
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
770{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400771 struct vcpu_svm *svm = to_svm(vcpu);
772
Bandan Dasf1047652015-06-11 02:05:33 -0400773 if (svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200774 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200775 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400776 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200777
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400778 if (!svm->next_rip) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -0700779 if (kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300780 EMULATE_DONE)
781 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782 return;
783 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300784 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
785 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
786 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300788 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400789 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790}
791
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700792static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100793{
794 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700795 unsigned nr = vcpu->arch.exception.nr;
796 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700797 bool reinject = vcpu->arch.exception.injected;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700798 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100799
Joerg Roedele0231712010-02-24 18:59:10 +0100800 /*
801 * If we are within a nested VM we'd better #VMEXIT and let the guest
802 * handle the exception
803 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200804 if (!reinject &&
805 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100806 return;
807
Jim Mattsonda998b42018-10-16 14:29:22 -0700808 kvm_deliver_exception_payload(&svm->vcpu);
809
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200810 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100811 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
812
813 /*
814 * For guest debugging where we have to reinject #BP if some
815 * INT3 is guest-owned:
816 * Emulate nRIP by moving RIP forward. Will fail if injection
817 * raises a fault that is not intercepted. Still better than
818 * failing in all cases.
819 */
820 skip_emulated_instruction(&svm->vcpu);
821 rip = kvm_rip_read(&svm->vcpu);
822 svm->int3_rip = rip + svm->vmcb->save.cs.base;
823 svm->int3_injected = rip - old_rip;
824 }
825
Jan Kiszka116a4752010-02-23 17:47:54 +0100826 svm->vmcb->control.event_inj = nr
827 | SVM_EVTINJ_VALID
828 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
829 | SVM_EVTINJ_TYPE_EXEPT;
830 svm->vmcb->control.event_inj_err = error_code;
831}
832
Joerg Roedel67ec6602010-05-17 14:43:35 +0200833static void svm_init_erratum_383(void)
834{
835 u32 low, high;
836 int err;
837 u64 val;
838
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100839 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200840 return;
841
842 /* Use _safe variants to not break nested virtualization */
843 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
844 if (err)
845 return;
846
847 val |= (1ULL << 47);
848
849 low = lower_32_bits(val);
850 high = upper_32_bits(val);
851
852 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
853
854 erratum_383_found = true;
855}
856
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500857static void svm_init_osvw(struct kvm_vcpu *vcpu)
858{
859 /*
860 * Guests should see errata 400 and 415 as fixed (assuming that
861 * HLT and IO instructions are intercepted).
862 */
863 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
864 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
865
866 /*
867 * By increasing VCPU's osvw.length to 3 we are telling the guest that
868 * all osvw.status bits inside that length, including bit 0 (which is
869 * reserved for erratum 298), are valid. However, if host processor's
870 * osvw_len is 0 then osvw_status[0] carries no information. We need to
871 * be conservative here and therefore we tell the guest that erratum 298
872 * is present (because we really don't know).
873 */
874 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
875 vcpu->arch.osvw.status |= 1;
876}
877
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878static int has_svm(void)
879{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200880 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881
Eduardo Habkost63d11422008-11-17 19:03:20 -0200882 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800883 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884 return 0;
885 }
886
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887 return 1;
888}
889
Radim Krčmář13a34e02014-08-28 15:13:03 +0200890static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100892 /* Make sure we clean up behind us */
893 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
894 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
895
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200896 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100897
898 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899}
900
Radim Krčmář13a34e02014-08-28 15:13:03 +0200901static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902{
903
Tejun Heo0fe1e002009-10-29 22:34:14 +0900904 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 struct desc_struct *gdt;
907 int me = raw_smp_processor_id();
908
Alexander Graf10474ae2009-09-15 11:37:46 +0200909 rdmsrl(MSR_EFER, efer);
910 if (efer & EFER_SVME)
911 return -EBUSY;
912
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200914 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200915 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900917 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900918 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200919 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200920 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800921 }
922
Tejun Heo0fe1e002009-10-29 22:34:14 +0900923 sd->asid_generation = 1;
924 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
925 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600926 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927
Thomas Garnier45fc8752017-03-14 10:05:08 -0700928 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900929 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930
Alexander Graf9962d032008-11-25 20:17:02 +0100931 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800932
Linus Torvaldsd0316552009-12-14 09:58:24 -0800933 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200934
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100935 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
936 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500937 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100938 }
939
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500940
941 /*
942 * Get OSVW bits.
943 *
944 * Note that it is possible to have a system with mixed processor
945 * revisions and therefore different OSVW bits. If bits are not the same
946 * on different processors then choose the worst case (i.e. if erratum
947 * is present on one processor and not on another then assume that the
948 * erratum is present everywhere).
949 */
950 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
951 uint64_t len, status = 0;
952 int err;
953
954 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
955 if (!err)
956 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
957 &err);
958
959 if (err)
960 osvw_status = osvw_len = 0;
961 else {
962 if (len < osvw_len)
963 osvw_len = len;
964 osvw_status |= status;
965 osvw_status &= (1ULL << osvw_len) - 1;
966 }
967 } else
968 osvw_status = osvw_len = 0;
969
Joerg Roedel67ec6602010-05-17 14:43:35 +0200970 svm_init_erratum_383();
971
Joerg Roedel1018faa2012-02-29 14:57:32 +0100972 amd_pmu_enable_virt();
973
Alexander Graf10474ae2009-09-15 11:37:46 +0200974 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975}
976
Joerg Roedel0da1db752008-07-02 16:02:11 +0200977static void svm_cpu_uninit(int cpu)
978{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900979 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200980
Tejun Heo0fe1e002009-10-29 22:34:14 +0900981 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200982 return;
983
984 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600985 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900986 __free_page(sd->save_area);
987 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200988}
989
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990static int svm_cpu_init(int cpu)
991{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900992 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993 int r;
994
Tejun Heo0fe1e002009-10-29 22:34:14 +0900995 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
996 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900998 sd->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999 r = -ENOMEM;
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001000 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +09001001 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 goto err_1;
1003
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001004 if (svm_sev_enabled()) {
1005 r = -ENOMEM;
Kees Cook6da2ec52018-06-12 13:55:00 -07001006 sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1,
1007 sizeof(void *),
1008 GFP_KERNEL);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001009 if (!sd->sev_vmcbs)
1010 goto err_1;
1011 }
1012
Tejun Heo0fe1e002009-10-29 22:34:14 +09001013 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014
1015 return 0;
1016
1017err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +09001018 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019 return r;
1020
1021}
1022
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001023static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024{
1025 int i;
1026
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001027 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
1028 if (direct_access_msrs[i].index == index)
1029 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001031 return false;
1032}
1033
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001034static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr)
1035{
1036 u8 bit_write;
1037 unsigned long tmp;
1038 u32 offset;
1039 u32 *msrpm;
1040
1041 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
1042 to_svm(vcpu)->msrpm;
1043
1044 offset = svm_msrpm_offset(msr);
1045 bit_write = 2 * (msr & 0x0f) + 1;
1046 tmp = msrpm[offset];
1047
1048 BUG_ON(offset == MSR_INVALID);
1049
1050 return !!test_bit(bit_write, &tmp);
1051}
1052
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053static void set_msr_interception(u32 *msrpm, unsigned msr,
1054 int read, int write)
1055{
Joerg Roedel455716f2010-03-01 15:34:35 +01001056 u8 bit_read, bit_write;
1057 unsigned long tmp;
1058 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001060 /*
1061 * If this warning triggers extend the direct_access_msrs list at the
1062 * beginning of the file
1063 */
1064 WARN_ON(!valid_msr_intercept(msr));
1065
Joerg Roedel455716f2010-03-01 15:34:35 +01001066 offset = svm_msrpm_offset(msr);
1067 bit_read = 2 * (msr & 0x0f);
1068 bit_write = 2 * (msr & 0x0f) + 1;
1069 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070
Joerg Roedel455716f2010-03-01 15:34:35 +01001071 BUG_ON(offset == MSR_INVALID);
1072
1073 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
1074 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
1075
1076 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077}
1078
Joerg Roedelf65c2292008-02-13 18:58:46 +01001079static void svm_vcpu_init_msrpm(u32 *msrpm)
1080{
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001081 int i;
1082
Joerg Roedelf65c2292008-02-13 18:58:46 +01001083 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
1084
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001085 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1086 if (!direct_access_msrs[i].always)
1087 continue;
1088
1089 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
1090 }
Joerg Roedelf65c2292008-02-13 18:58:46 +01001091}
1092
Joerg Roedel323c3d82010-03-01 15:34:37 +01001093static void add_msr_offset(u32 offset)
1094{
1095 int i;
1096
1097 for (i = 0; i < MSRPM_OFFSETS; ++i) {
1098
1099 /* Offset already in list? */
1100 if (msrpm_offsets[i] == offset)
1101 return;
1102
1103 /* Slot used by another offset? */
1104 if (msrpm_offsets[i] != MSR_INVALID)
1105 continue;
1106
1107 /* Add offset to list */
1108 msrpm_offsets[i] = offset;
1109
1110 return;
1111 }
1112
1113 /*
1114 * If this BUG triggers the msrpm_offsets table has an overflow. Just
1115 * increase MSRPM_OFFSETS in this case.
1116 */
1117 BUG();
1118}
1119
1120static void init_msrpm_offsets(void)
1121{
1122 int i;
1123
1124 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
1125
1126 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1127 u32 offset;
1128
1129 offset = svm_msrpm_offset(direct_access_msrs[i].index);
1130 BUG_ON(offset == MSR_INVALID);
1131
1132 add_msr_offset(offset);
1133 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134}
1135
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001136static void svm_enable_lbrv(struct vcpu_svm *svm)
1137{
1138 u32 *msrpm = svm->msrpm;
1139
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001140 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001141 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
1142 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
1143 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
1144 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
1145}
1146
1147static void svm_disable_lbrv(struct vcpu_svm *svm)
1148{
1149 u32 *msrpm = svm->msrpm;
1150
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001151 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001152 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
1153 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
1154 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
1155 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
1156}
1157
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001158static void disable_nmi_singlestep(struct vcpu_svm *svm)
1159{
1160 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001161
Ladi Prosekab2f4d732017-06-21 09:06:58 +02001162 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
1163 /* Clear our flags if they were not set by the guest */
1164 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1165 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
1166 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1167 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
1168 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001169}
1170
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001171/* Note:
Sean Christopherson81811c12018-03-20 12:17:21 -07001172 * This hash table is used to map VM_ID to a struct kvm_svm,
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001173 * when handling AMD IOMMU GALOG notification to schedule in
1174 * a particular vCPU.
1175 */
1176#define SVM_VM_DATA_HASH_BITS 8
David Hildenbrand681bcea2017-01-24 22:21:16 +01001177static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001178static u32 next_vm_id = 0;
1179static bool next_vm_id_wrapped = 0;
David Hildenbrand681bcea2017-01-24 22:21:16 +01001180static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001181
1182/* Note:
1183 * This function is called from IOMMU driver to notify
1184 * SVM to schedule in a particular vCPU of a particular VM.
1185 */
1186static int avic_ga_log_notifier(u32 ga_tag)
1187{
1188 unsigned long flags;
Sean Christopherson81811c12018-03-20 12:17:21 -07001189 struct kvm_svm *kvm_svm;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001190 struct kvm_vcpu *vcpu = NULL;
1191 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
1192 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
1193
1194 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
1195
1196 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Sean Christopherson81811c12018-03-20 12:17:21 -07001197 hash_for_each_possible(svm_vm_data_hash, kvm_svm, hnode, vm_id) {
1198 if (kvm_svm->avic_vm_id != vm_id)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001199 continue;
Sean Christopherson81811c12018-03-20 12:17:21 -07001200 vcpu = kvm_get_vcpu_by_id(&kvm_svm->kvm, vcpu_id);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001201 break;
1202 }
1203 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1204
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001205 /* Note:
1206 * At this point, the IOMMU should have already set the pending
1207 * bit in the vAPIC backing page. So, we just need to schedule
1208 * in the vcpu.
1209 */
Paolo Bonzini1cf53582017-10-10 12:51:56 +02001210 if (vcpu)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001211 kvm_vcpu_wake_up(vcpu);
1212
1213 return 0;
1214}
1215
Brijesh Singhe9df0942017-12-04 10:57:33 -06001216static __init int sev_hardware_setup(void)
1217{
1218 struct sev_user_data_status *status;
1219 int rc;
1220
1221 /* Maximum number of encrypted guests supported simultaneously */
1222 max_sev_asid = cpuid_ecx(0x8000001F);
1223
1224 if (!max_sev_asid)
1225 return 1;
1226
Brijesh Singh1654efc2017-12-04 10:57:34 -06001227 /* Minimum ASID value that should be used for SEV guest */
1228 min_sev_asid = cpuid_edx(0x8000001F);
1229
1230 /* Initialize SEV ASID bitmap */
Andy Shevchenkoa101c9d632018-08-30 14:49:59 +03001231 sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001232 if (!sev_asid_bitmap)
1233 return 1;
1234
Brijesh Singhe9df0942017-12-04 10:57:33 -06001235 status = kmalloc(sizeof(*status), GFP_KERNEL);
1236 if (!status)
1237 return 1;
1238
1239 /*
1240 * Check SEV platform status.
1241 *
1242 * PLATFORM_STATUS can be called in any state, if we failed to query
1243 * the PLATFORM status then either PSP firmware does not support SEV
1244 * feature or SEV firmware is dead.
1245 */
1246 rc = sev_platform_status(status, NULL);
1247 if (rc)
1248 goto err;
1249
1250 pr_info("SEV supported\n");
1251
1252err:
1253 kfree(status);
1254 return rc;
1255}
1256
Babu Moger8566ac82018-03-16 16:37:26 -04001257static void grow_ple_window(struct kvm_vcpu *vcpu)
1258{
1259 struct vcpu_svm *svm = to_svm(vcpu);
1260 struct vmcb_control_area *control = &svm->vmcb->control;
1261 int old = control->pause_filter_count;
1262
1263 control->pause_filter_count = __grow_ple_window(old,
1264 pause_filter_count,
1265 pause_filter_count_grow,
1266 pause_filter_count_max);
1267
1268 if (control->pause_filter_count != old)
1269 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
1270
1271 trace_kvm_ple_window_grow(vcpu->vcpu_id,
1272 control->pause_filter_count, old);
1273}
1274
1275static void shrink_ple_window(struct kvm_vcpu *vcpu)
1276{
1277 struct vcpu_svm *svm = to_svm(vcpu);
1278 struct vmcb_control_area *control = &svm->vmcb->control;
1279 int old = control->pause_filter_count;
1280
1281 control->pause_filter_count =
1282 __shrink_ple_window(old,
1283 pause_filter_count,
1284 pause_filter_count_shrink,
1285 pause_filter_count);
1286 if (control->pause_filter_count != old)
1287 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
1288
1289 trace_kvm_ple_window_shrink(vcpu->vcpu_id,
1290 control->pause_filter_count, old);
1291}
1292
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293static __init int svm_hardware_setup(void)
1294{
1295 int cpu;
1296 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001297 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298 int r;
1299
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
1301
1302 if (!iopm_pages)
1303 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +03001304
1305 iopm_va = page_address(iopm_pages);
1306 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
1308
Joerg Roedel323c3d82010-03-01 15:34:37 +01001309 init_msrpm_offsets();
1310
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001311 if (boot_cpu_has(X86_FEATURE_NX))
1312 kvm_enable_efer_bits(EFER_NX);
1313
Alexander Graf1b2fd702009-02-02 16:23:51 +01001314 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
1315 kvm_enable_efer_bits(EFER_FFXSR);
1316
Joerg Roedel92a1f122011-03-25 09:44:51 +01001317 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +01001318 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +08001319 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1320 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +01001321 }
1322
Babu Moger8566ac82018-03-16 16:37:26 -04001323 /* Check for pause filtering support */
1324 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
1325 pause_filter_count = 0;
1326 pause_filter_thresh = 0;
1327 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
1328 pause_filter_thresh = 0;
1329 }
1330
Alexander Graf236de052008-11-25 20:17:10 +01001331 if (nested) {
1332 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +02001333 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +01001334 }
1335
Brijesh Singhe9df0942017-12-04 10:57:33 -06001336 if (sev) {
1337 if (boot_cpu_has(X86_FEATURE_SEV) &&
1338 IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
1339 r = sev_hardware_setup();
1340 if (r)
1341 sev = false;
1342 } else {
1343 sev = false;
1344 }
1345 }
1346
Zachary Amsden3230bb42009-09-29 11:38:37 -10001347 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 r = svm_cpu_init(cpu);
1349 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +01001350 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +01001352
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001353 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001354 npt_enabled = false;
1355
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001356 if (npt_enabled && !npt) {
1357 printk(KERN_INFO "kvm: Nested Paging disabled\n");
1358 npt_enabled = false;
1359 }
1360
Joerg Roedel18552672008-02-07 13:47:41 +01001361 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001362 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +01001363 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +02001364 } else
1365 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001366
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001367 if (avic) {
1368 if (!npt_enabled ||
1369 !boot_cpu_has(X86_FEATURE_AVIC) ||
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001370 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001371 avic = false;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001372 } else {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001373 pr_info("AVIC enabled\n");
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001374
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001375 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1376 }
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001377 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001378
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001379 if (vls) {
1380 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001381 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001382 !IS_ENABLED(CONFIG_X86_64)) {
1383 vls = false;
1384 } else {
1385 pr_info("Virtual VMLOAD VMSAVE supported\n");
1386 }
1387 }
1388
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001389 if (vgif) {
1390 if (!boot_cpu_has(X86_FEATURE_VGIF))
1391 vgif = false;
1392 else
1393 pr_info("Virtual GIF supported\n");
1394 }
1395
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396 return 0;
1397
Joerg Roedelf65c2292008-02-13 18:58:46 +01001398err:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1400 iopm_base = 0;
1401 return r;
1402}
1403
1404static __exit void svm_hardware_unsetup(void)
1405{
Joerg Roedel0da1db752008-07-02 16:02:11 +02001406 int cpu;
1407
Brijesh Singh1654efc2017-12-04 10:57:34 -06001408 if (svm_sev_enabled())
Andy Shevchenkoa101c9d632018-08-30 14:49:59 +03001409 bitmap_free(sev_asid_bitmap);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001410
Zachary Amsden3230bb42009-09-29 11:38:37 -10001411 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +02001412 svm_cpu_uninit(cpu);
1413
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001415 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416}
1417
1418static void init_seg(struct vmcb_seg *seg)
1419{
1420 seg->selector = 0;
1421 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001422 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423 seg->limit = 0xffff;
1424 seg->base = 0;
1425}
1426
1427static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1428{
1429 seg->selector = 0;
1430 seg->attrib = SVM_SELECTOR_P_MASK | type;
1431 seg->limit = 0xffff;
1432 seg->base = 0;
1433}
1434
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001435static u64 svm_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
1436{
1437 struct vcpu_svm *svm = to_svm(vcpu);
1438
1439 if (is_guest_mode(vcpu))
1440 return svm->nested.hsave->control.tsc_offset;
1441
1442 return vcpu->arch.tsc_offset;
1443}
1444
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001445static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1446{
1447 struct vcpu_svm *svm = to_svm(vcpu);
1448 u64 g_tsc_offset = 0;
1449
Joerg Roedel20307532010-11-29 17:51:48 +01001450 if (is_guest_mode(vcpu)) {
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001451 /* Write L1's TSC offset. */
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001452 g_tsc_offset = svm->vmcb->control.tsc_offset -
1453 svm->nested.hsave->control.tsc_offset;
1454 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001455 } else
1456 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1457 svm->vmcb->control.tsc_offset,
1458 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001459
1460 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001461
1462 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001463}
1464
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001465static void avic_init_vmcb(struct vcpu_svm *svm)
1466{
1467 struct vmcb *vmcb = svm->vmcb;
Sean Christopherson81811c12018-03-20 12:17:21 -07001468 struct kvm_svm *kvm_svm = to_kvm_svm(svm->vcpu.kvm);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001469 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
Sean Christopherson81811c12018-03-20 12:17:21 -07001470 phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page));
1471 phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page));
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001472
1473 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1474 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1475 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1476 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1477 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001478}
1479
Paolo Bonzini56908912015-10-19 11:30:19 +02001480static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481{
Joerg Roedele6101a92008-02-13 18:58:45 +01001482 struct vmcb_control_area *control = &svm->vmcb->control;
1483 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001485 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001486
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001487 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1488 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1489 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1490 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1491 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1492 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001493 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1494 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495
Paolo Bonzini5315c712014-03-03 13:08:29 +01001496 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497
Joerg Roedel18c918c2010-11-30 18:03:59 +01001498 set_exception_intercept(svm, PF_VECTOR);
1499 set_exception_intercept(svm, UD_VECTOR);
1500 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001501 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001502 set_exception_intercept(svm, DB_VECTOR);
Liran Alon97184202018-03-12 13:12:52 +02001503 /*
1504 * Guest access to VMware backdoor ports could legitimately
1505 * trigger #GP because of TSS I/O permission bitmap.
1506 * We intercept those #GP and allow access to them anyway
1507 * as VMware does.
1508 */
1509 if (enable_vmware_backdoor)
1510 set_exception_intercept(svm, GP_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001512 set_intercept(svm, INTERCEPT_INTR);
1513 set_intercept(svm, INTERCEPT_NMI);
1514 set_intercept(svm, INTERCEPT_SMI);
1515 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001516 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001517 set_intercept(svm, INTERCEPT_CPUID);
1518 set_intercept(svm, INTERCEPT_INVD);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001519 set_intercept(svm, INTERCEPT_INVLPG);
1520 set_intercept(svm, INTERCEPT_INVLPGA);
1521 set_intercept(svm, INTERCEPT_IOIO_PROT);
1522 set_intercept(svm, INTERCEPT_MSR_PROT);
1523 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1524 set_intercept(svm, INTERCEPT_SHUTDOWN);
1525 set_intercept(svm, INTERCEPT_VMRUN);
1526 set_intercept(svm, INTERCEPT_VMMCALL);
1527 set_intercept(svm, INTERCEPT_VMLOAD);
1528 set_intercept(svm, INTERCEPT_VMSAVE);
1529 set_intercept(svm, INTERCEPT_STGI);
1530 set_intercept(svm, INTERCEPT_CLGI);
1531 set_intercept(svm, INTERCEPT_SKINIT);
1532 set_intercept(svm, INTERCEPT_WBINVD);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001533 set_intercept(svm, INTERCEPT_XSETBV);
Brijesh Singh7607b712018-02-19 10:14:44 -06001534 set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535
Wanpeng Li4d5422c2018-03-12 04:53:02 -07001536 if (!kvm_mwait_in_guest(svm->vcpu.kvm)) {
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001537 set_intercept(svm, INTERCEPT_MONITOR);
1538 set_intercept(svm, INTERCEPT_MWAIT);
1539 }
1540
Wanpeng Licaa057a2018-03-12 04:53:03 -07001541 if (!kvm_hlt_in_guest(svm->vcpu.kvm))
1542 set_intercept(svm, INTERCEPT_HLT);
1543
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001544 control->iopm_base_pa = __sme_set(iopm_base);
1545 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001546 control->int_ctl = V_INTR_MASKING_MASK;
1547
1548 init_seg(&save->es);
1549 init_seg(&save->ss);
1550 init_seg(&save->ds);
1551 init_seg(&save->fs);
1552 init_seg(&save->gs);
1553
1554 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001555 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001556 /* Executable/Readable Code Segment */
1557 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1558 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1559 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560
1561 save->gdtr.limit = 0xffff;
1562 save->idtr.limit = 0xffff;
1563
1564 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1565 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1566
Paolo Bonzini56908912015-10-19 11:30:19 +02001567 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001568 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001569 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001571 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572
Joerg Roedele0231712010-02-24 18:59:10 +01001573 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001574 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001575 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001576 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001577 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001578 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001579
Rusty Russell66aee912007-07-17 23:34:16 +10001580 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001582
1583 if (npt_enabled) {
1584 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001585 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001586 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001587 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001588 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1589 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001590 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001591 save->cr3 = 0;
1592 save->cr4 = 0;
1593 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001594 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001595
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001596 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001597 svm->vcpu.arch.hflags = 0;
1598
Babu Moger8566ac82018-03-16 16:37:26 -04001599 if (pause_filter_count) {
1600 control->pause_filter_count = pause_filter_count;
1601 if (pause_filter_thresh)
1602 control->pause_filter_thresh = pause_filter_thresh;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001603 set_intercept(svm, INTERCEPT_PAUSE);
Babu Moger8566ac82018-03-16 16:37:26 -04001604 } else {
1605 clr_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001606 }
1607
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001608 if (kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001609 avic_init_vmcb(svm);
1610
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001611 /*
1612 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1613 * in VMCB and clear intercepts to avoid #VMEXIT.
1614 */
1615 if (vls) {
1616 clr_intercept(svm, INTERCEPT_VMLOAD);
1617 clr_intercept(svm, INTERCEPT_VMSAVE);
1618 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1619 }
1620
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001621 if (vgif) {
1622 clr_intercept(svm, INTERCEPT_STGI);
1623 clr_intercept(svm, INTERCEPT_CLGI);
1624 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1625 }
1626
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001627 if (sev_guest(svm->vcpu.kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001628 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001629 clr_exception_intercept(svm, UD_VECTOR);
1630 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001631
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001632 mark_all_dirty(svm->vmcb);
1633
Joerg Roedel2af91942009-08-07 11:49:28 +02001634 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001635
1636}
1637
Dan Carpenterd3e7dec2017-05-18 10:38:53 +03001638static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1639 unsigned int index)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001640{
1641 u64 *avic_physical_id_table;
Sean Christopherson81811c12018-03-20 12:17:21 -07001642 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001643
1644 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1645 return NULL;
1646
Sean Christopherson81811c12018-03-20 12:17:21 -07001647 avic_physical_id_table = page_address(kvm_svm->avic_physical_id_table_page);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001648
1649 return &avic_physical_id_table[index];
1650}
1651
1652/**
1653 * Note:
1654 * AVIC hardware walks the nested page table to check permissions,
1655 * but does not use the SPA address specified in the leaf page
1656 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1657 * field of the VMCB. Therefore, we set up the
1658 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1659 */
1660static int avic_init_access_page(struct kvm_vcpu *vcpu)
1661{
1662 struct kvm *kvm = vcpu->kvm;
1663 int ret;
1664
1665 if (kvm->arch.apic_access_page_done)
1666 return 0;
1667
1668 ret = x86_set_memory_region(kvm,
1669 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1670 APIC_DEFAULT_PHYS_BASE,
1671 PAGE_SIZE);
1672 if (ret)
1673 return ret;
1674
1675 kvm->arch.apic_access_page_done = true;
1676 return 0;
1677}
1678
1679static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1680{
1681 int ret;
1682 u64 *entry, new_entry;
1683 int id = vcpu->vcpu_id;
1684 struct vcpu_svm *svm = to_svm(vcpu);
1685
1686 ret = avic_init_access_page(vcpu);
1687 if (ret)
1688 return ret;
1689
1690 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1691 return -EINVAL;
1692
1693 if (!svm->vcpu.arch.apic->regs)
1694 return -EINVAL;
1695
1696 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1697
1698 /* Setting AVIC backing page address in the phy APIC ID table */
1699 entry = avic_get_physical_id_entry(vcpu, id);
1700 if (!entry)
1701 return -EINVAL;
1702
1703 new_entry = READ_ONCE(*entry);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001704 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
1705 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1706 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001707 WRITE_ONCE(*entry, new_entry);
1708
1709 svm->avic_physical_id_cache = entry;
1710
1711 return 0;
1712}
1713
Brijesh Singh1654efc2017-12-04 10:57:34 -06001714static void __sev_asid_free(int asid)
1715{
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001716 struct svm_cpu_data *sd;
1717 int cpu, pos;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001718
1719 pos = asid - 1;
1720 clear_bit(pos, sev_asid_bitmap);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001721
1722 for_each_possible_cpu(cpu) {
1723 sd = per_cpu(svm_data, cpu);
1724 sd->sev_vmcbs[pos] = NULL;
1725 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001726}
1727
1728static void sev_asid_free(struct kvm *kvm)
1729{
Sean Christopherson81811c12018-03-20 12:17:21 -07001730 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001731
1732 __sev_asid_free(sev->asid);
1733}
1734
Brijesh Singh59414c92017-12-04 10:57:35 -06001735static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
1736{
1737 struct sev_data_decommission *decommission;
1738 struct sev_data_deactivate *data;
1739
1740 if (!handle)
1741 return;
1742
1743 data = kzalloc(sizeof(*data), GFP_KERNEL);
1744 if (!data)
1745 return;
1746
1747 /* deactivate handle */
1748 data->handle = handle;
1749 sev_guest_deactivate(data, NULL);
1750
1751 wbinvd_on_all_cpus();
1752 sev_guest_df_flush(NULL);
1753 kfree(data);
1754
1755 decommission = kzalloc(sizeof(*decommission), GFP_KERNEL);
1756 if (!decommission)
1757 return;
1758
1759 /* decommission handle */
1760 decommission->handle = handle;
1761 sev_guest_decommission(decommission, NULL);
1762
1763 kfree(decommission);
1764}
1765
Brijesh Singh89c50582017-12-04 10:57:35 -06001766static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
1767 unsigned long ulen, unsigned long *n,
1768 int write)
1769{
Sean Christopherson81811c12018-03-20 12:17:21 -07001770 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06001771 unsigned long npages, npinned, size;
1772 unsigned long locked, lock_limit;
1773 struct page **pages;
Dan Carpenter86bf20c2018-05-19 09:01:36 +03001774 unsigned long first, last;
1775
1776 if (ulen == 0 || uaddr + ulen < uaddr)
1777 return NULL;
Brijesh Singh89c50582017-12-04 10:57:35 -06001778
1779 /* Calculate number of pages. */
1780 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
1781 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
1782 npages = (last - first + 1);
1783
1784 locked = sev->pages_locked + npages;
1785 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1786 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
1787 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
1788 return NULL;
1789 }
1790
1791 /* Avoid using vmalloc for smaller buffers. */
1792 size = npages * sizeof(struct page *);
1793 if (size > PAGE_SIZE)
1794 pages = vmalloc(size);
1795 else
1796 pages = kmalloc(size, GFP_KERNEL);
1797
1798 if (!pages)
1799 return NULL;
1800
1801 /* Pin the user virtual address. */
1802 npinned = get_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
1803 if (npinned != npages) {
1804 pr_err("SEV: Failure locking %lu pages.\n", npages);
1805 goto err;
1806 }
1807
1808 *n = npages;
1809 sev->pages_locked = locked;
1810
1811 return pages;
1812
1813err:
1814 if (npinned > 0)
1815 release_pages(pages, npinned);
1816
1817 kvfree(pages);
1818 return NULL;
1819}
1820
1821static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
1822 unsigned long npages)
1823{
Sean Christopherson81811c12018-03-20 12:17:21 -07001824 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06001825
1826 release_pages(pages, npages);
1827 kvfree(pages);
1828 sev->pages_locked -= npages;
1829}
1830
1831static void sev_clflush_pages(struct page *pages[], unsigned long npages)
1832{
1833 uint8_t *page_virtual;
1834 unsigned long i;
1835
1836 if (npages == 0 || pages == NULL)
1837 return;
1838
1839 for (i = 0; i < npages; i++) {
1840 page_virtual = kmap_atomic(pages[i]);
1841 clflush_cache_range(page_virtual, PAGE_SIZE);
1842 kunmap_atomic(page_virtual);
1843 }
1844}
1845
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001846static void __unregister_enc_region_locked(struct kvm *kvm,
1847 struct enc_region *region)
1848{
1849 /*
1850 * The guest may change the memory encryption attribute from C=0 -> C=1
1851 * or vice versa for this memory range. Lets make sure caches are
1852 * flushed to ensure that guest data gets written into memory with
1853 * correct C-bit.
1854 */
1855 sev_clflush_pages(region->pages, region->npages);
1856
1857 sev_unpin_memory(kvm, region->pages, region->npages);
1858 list_del(&region->list);
1859 kfree(region);
1860}
1861
Sean Christopherson434a1e92018-03-20 12:17:18 -07001862static struct kvm *svm_vm_alloc(void)
1863{
Marc Orrd1e5b0e2018-05-15 04:37:37 -07001864 struct kvm_svm *kvm_svm = vzalloc(sizeof(struct kvm_svm));
Sean Christopherson81811c12018-03-20 12:17:21 -07001865 return &kvm_svm->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07001866}
1867
1868static void svm_vm_free(struct kvm *kvm)
1869{
Marc Orrd1e5b0e2018-05-15 04:37:37 -07001870 vfree(to_kvm_svm(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07001871}
1872
Brijesh Singh1654efc2017-12-04 10:57:34 -06001873static void sev_vm_destroy(struct kvm *kvm)
1874{
Sean Christopherson81811c12018-03-20 12:17:21 -07001875 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001876 struct list_head *head = &sev->regions_list;
1877 struct list_head *pos, *q;
Brijesh Singh59414c92017-12-04 10:57:35 -06001878
Brijesh Singh1654efc2017-12-04 10:57:34 -06001879 if (!sev_guest(kvm))
1880 return;
1881
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001882 mutex_lock(&kvm->lock);
1883
1884 /*
1885 * if userspace was terminated before unregistering the memory regions
1886 * then lets unpin all the registered memory.
1887 */
1888 if (!list_empty(head)) {
1889 list_for_each_safe(pos, q, head) {
1890 __unregister_enc_region_locked(kvm,
1891 list_entry(pos, struct enc_region, list));
1892 }
1893 }
1894
1895 mutex_unlock(&kvm->lock);
1896
Brijesh Singh59414c92017-12-04 10:57:35 -06001897 sev_unbind_asid(kvm, sev->handle);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001898 sev_asid_free(kvm);
1899}
1900
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001901static void avic_vm_destroy(struct kvm *kvm)
1902{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001903 unsigned long flags;
Sean Christopherson81811c12018-03-20 12:17:21 -07001904 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001905
Dmitry Vyukov3863dff2017-01-24 14:06:48 +01001906 if (!avic)
1907 return;
1908
Sean Christopherson81811c12018-03-20 12:17:21 -07001909 if (kvm_svm->avic_logical_id_table_page)
1910 __free_page(kvm_svm->avic_logical_id_table_page);
1911 if (kvm_svm->avic_physical_id_table_page)
1912 __free_page(kvm_svm->avic_physical_id_table_page);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001913
1914 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Sean Christopherson81811c12018-03-20 12:17:21 -07001915 hash_del(&kvm_svm->hnode);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001916 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001917}
1918
Brijesh Singh1654efc2017-12-04 10:57:34 -06001919static void svm_vm_destroy(struct kvm *kvm)
1920{
1921 avic_vm_destroy(kvm);
1922 sev_vm_destroy(kvm);
1923}
1924
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001925static int avic_vm_init(struct kvm *kvm)
1926{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001927 unsigned long flags;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001928 int err = -ENOMEM;
Sean Christopherson81811c12018-03-20 12:17:21 -07001929 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
1930 struct kvm_svm *k2;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001931 struct page *p_page;
1932 struct page *l_page;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001933 u32 vm_id;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001934
1935 if (!avic)
1936 return 0;
1937
1938 /* Allocating physical APIC ID table (4KB) */
1939 p_page = alloc_page(GFP_KERNEL);
1940 if (!p_page)
1941 goto free_avic;
1942
Sean Christopherson81811c12018-03-20 12:17:21 -07001943 kvm_svm->avic_physical_id_table_page = p_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001944 clear_page(page_address(p_page));
1945
1946 /* Allocating logical APIC ID table (4KB) */
1947 l_page = alloc_page(GFP_KERNEL);
1948 if (!l_page)
1949 goto free_avic;
1950
Sean Christopherson81811c12018-03-20 12:17:21 -07001951 kvm_svm->avic_logical_id_table_page = l_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001952 clear_page(page_address(l_page));
1953
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001954 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001955 again:
1956 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
1957 if (vm_id == 0) { /* id is 1-based, zero is not okay */
1958 next_vm_id_wrapped = 1;
1959 goto again;
1960 }
1961 /* Is it still in use? Only possible if wrapped at least once */
1962 if (next_vm_id_wrapped) {
Sean Christopherson81811c12018-03-20 12:17:21 -07001963 hash_for_each_possible(svm_vm_data_hash, k2, hnode, vm_id) {
1964 if (k2->avic_vm_id == vm_id)
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001965 goto again;
1966 }
1967 }
Sean Christopherson81811c12018-03-20 12:17:21 -07001968 kvm_svm->avic_vm_id = vm_id;
1969 hash_add(svm_vm_data_hash, &kvm_svm->hnode, kvm_svm->avic_vm_id);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001970 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1971
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001972 return 0;
1973
1974free_avic:
1975 avic_vm_destroy(kvm);
1976 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001977}
1978
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001979static inline int
1980avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001981{
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001982 int ret = 0;
1983 unsigned long flags;
1984 struct amd_svm_iommu_ir *ir;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001985 struct vcpu_svm *svm = to_svm(vcpu);
1986
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001987 if (!kvm_arch_has_assigned_device(vcpu->kvm))
1988 return 0;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001989
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001990 /*
1991 * Here, we go through the per-vcpu ir_list to update all existing
1992 * interrupt remapping table entry targeting this vcpu.
1993 */
1994 spin_lock_irqsave(&svm->ir_list_lock, flags);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001995
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001996 if (list_empty(&svm->ir_list))
1997 goto out;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001998
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001999 list_for_each_entry(ir, &svm->ir_list, node) {
2000 ret = amd_iommu_update_ga(cpu, r, ir->data);
2001 if (ret)
2002 break;
2003 }
2004out:
2005 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
2006 return ret;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002007}
2008
2009static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2010{
2011 u64 entry;
2012 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05002013 int h_physical_id = kvm_cpu_get_apicid(cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002014 struct vcpu_svm *svm = to_svm(vcpu);
2015
2016 if (!kvm_vcpu_apicv_active(vcpu))
2017 return;
2018
2019 if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
2020 return;
2021
2022 entry = READ_ONCE(*(svm->avic_physical_id_cache));
2023 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
2024
2025 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
2026 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
2027
2028 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2029 if (svm->avic_is_running)
2030 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2031
2032 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002033 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
2034 svm->avic_is_running);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002035}
2036
2037static void avic_vcpu_put(struct kvm_vcpu *vcpu)
2038{
2039 u64 entry;
2040 struct vcpu_svm *svm = to_svm(vcpu);
2041
2042 if (!kvm_vcpu_apicv_active(vcpu))
2043 return;
2044
2045 entry = READ_ONCE(*(svm->avic_physical_id_cache));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002046 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
2047 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
2048
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002049 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2050 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051}
2052
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002053/**
2054 * This function is called during VCPU halt/unhalt.
2055 */
2056static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
2057{
2058 struct vcpu_svm *svm = to_svm(vcpu);
2059
2060 svm->avic_is_running = is_run;
2061 if (is_run)
2062 avic_vcpu_load(vcpu, vcpu->cpu);
2063 else
2064 avic_vcpu_put(vcpu);
2065}
2066
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002067static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03002068{
2069 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01002070 u32 dummy;
2071 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002072
Wanpeng Li518e7b92018-02-28 14:03:31 +08002073 vcpu->arch.microcode_version = 0x01000065;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002074 svm->spec_ctrl = 0;
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02002075 svm->virt_spec_ctrl = 0;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002076
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002077 if (!init_event) {
2078 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
2079 MSR_IA32_APICBASE_ENABLE;
2080 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
2081 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
2082 }
Paolo Bonzini56908912015-10-19 11:30:19 +02002083 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02002084
Yu Zhange911eb32017-08-24 20:27:52 +08002085 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
Julian Stecklina66f7b722012-12-05 15:26:19 +01002086 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002087
2088 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
2089 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03002090}
2091
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002092static int avic_init_vcpu(struct vcpu_svm *svm)
2093{
2094 int ret;
2095
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05002096 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002097 return 0;
2098
2099 ret = avic_init_backing_page(&svm->vcpu);
2100 if (ret)
2101 return ret;
2102
2103 INIT_LIST_HEAD(&svm->ir_list);
2104 spin_lock_init(&svm->ir_list_lock);
2105
2106 return ret;
2107}
2108
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002109static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002111 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01002113 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002114 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002115 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002116 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117
Rusty Russellc16f8622007-07-30 21:12:19 +10002118 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002119 if (!svm) {
2120 err = -ENOMEM;
2121 goto out;
2122 }
2123
2124 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
2125 if (err)
2126 goto free_svm;
2127
Joerg Roedelf65c2292008-02-13 18:58:46 +01002128 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002129 page = alloc_page(GFP_KERNEL);
2130 if (!page)
2131 goto uninit;
2132
Joerg Roedelf65c2292008-02-13 18:58:46 +01002133 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
2134 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002135 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002136
2137 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
2138 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002139 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01002140
Alexander Grafb286d5d2008-11-25 20:17:05 +01002141 hsave_page = alloc_page(GFP_KERNEL);
2142 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002143 goto free_page3;
2144
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002145 err = avic_init_vcpu(svm);
2146 if (err)
2147 goto free_page4;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002148
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002149 /* We initialize this flag to true to make sure that the is_running
2150 * bit would be set the first time the vcpu is loaded.
2151 */
2152 svm->avic_is_running = true;
2153
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002154 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01002155
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002156 svm->msrpm = page_address(msrpm_pages);
2157 svm_vcpu_init_msrpm(svm->msrpm);
2158
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002159 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002160 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002161
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002162 svm->vmcb = page_address(page);
2163 clear_page(svm->vmcb);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002164 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002165 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02002166 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002168 svm_init_osvw(&svm->vcpu);
2169
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002170 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08002171
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002172free_page4:
2173 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002174free_page3:
2175 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
2176free_page2:
2177 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
2178free_page1:
2179 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002180uninit:
2181 kvm_vcpu_uninit(&svm->vcpu);
2182free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10002183 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002184out:
2185 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186}
2187
2188static void svm_free_vcpu(struct kvm_vcpu *vcpu)
2189{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002190 struct vcpu_svm *svm = to_svm(vcpu);
2191
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002192 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01002193 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002194 __free_page(virt_to_page(svm->nested.hsave));
2195 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002196 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002197 kmem_cache_free(kvm_vcpu_cache, svm);
Ashok Raj15d45072018-02-01 22:59:43 +01002198 /*
2199 * The vmcb page can be recycled, causing a false negative in
2200 * svm_vcpu_load(). So do a full IBPB now.
2201 */
2202 indirect_branch_prediction_barrier();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203}
2204
Avi Kivity15ad7142007-07-11 18:17:21 +03002205static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002207 struct vcpu_svm *svm = to_svm(vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01002208 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002209 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02002210
Avi Kivity0cc50642007-03-25 12:07:27 +02002211 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002212 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002213 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02002214 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002215
Avi Kivity82ca2d12010-10-21 12:20:34 +02002216#ifdef CONFIG_X86_64
2217 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
2218#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02002219 savesegment(fs, svm->host.fs);
2220 savesegment(gs, svm->host.gs);
2221 svm->host.ldt = kvm_read_ldt();
2222
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002223 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002224 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002225
Haozhong Zhangad7218832015-10-20 15:39:02 +08002226 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
2227 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2228 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
2229 __this_cpu_write(current_tsc_ratio, tsc_ratio);
2230 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
2231 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002232 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01002233 /* This assumes that the kernel never uses MSR_TSC_AUX */
2234 if (static_cpu_has(X86_FEATURE_RDTSCP))
2235 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002236
Ashok Raj15d45072018-02-01 22:59:43 +01002237 if (sd->current_vmcb != svm->vmcb) {
2238 sd->current_vmcb = svm->vmcb;
2239 indirect_branch_prediction_barrier();
2240 }
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002241 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242}
2243
2244static void svm_vcpu_put(struct kvm_vcpu *vcpu)
2245{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002246 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002247 int i;
2248
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002249 avic_vcpu_put(vcpu);
2250
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002251 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02002252 kvm_load_ldt(svm->host.ldt);
2253#ifdef CONFIG_X86_64
2254 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07002255 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01002256 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02002257#else
Avi Kivity831ca602011-03-08 16:09:51 +02002258#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02002259 loadsegment(gs, svm->host.gs);
2260#endif
Avi Kivity831ca602011-03-08 16:09:51 +02002261#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002262 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002263 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264}
2265
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002266static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
2267{
2268 avic_set_running(vcpu, false);
2269}
2270
2271static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
2272{
2273 avic_set_running(vcpu, true);
2274}
2275
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
2277{
Ladi Prosek9b611742017-06-21 09:06:59 +02002278 struct vcpu_svm *svm = to_svm(vcpu);
2279 unsigned long rflags = svm->vmcb->save.rflags;
2280
2281 if (svm->nmi_singlestep) {
2282 /* Hide our flags if they were not set by the guest */
2283 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
2284 rflags &= ~X86_EFLAGS_TF;
2285 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
2286 rflags &= ~X86_EFLAGS_RF;
2287 }
2288 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289}
2290
2291static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2292{
Ladi Prosek9b611742017-06-21 09:06:59 +02002293 if (to_svm(vcpu)->nmi_singlestep)
2294 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2295
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002296 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02002297 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002298 * (caused by either a task switch or an inter-privilege IRET),
2299 * so we do not need to update the CPL here.
2300 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002301 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302}
2303
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002304static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
2305{
2306 switch (reg) {
2307 case VCPU_EXREG_PDPTR:
2308 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002309 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002310 break;
2311 default:
2312 BUG();
2313 }
2314}
2315
Alexander Graff0b85052008-11-25 20:17:01 +01002316static void svm_set_vintr(struct vcpu_svm *svm)
2317{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002318 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002319}
2320
2321static void svm_clear_vintr(struct vcpu_svm *svm)
2322{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002323 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002324}
2325
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
2327{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002328 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329
2330 switch (seg) {
2331 case VCPU_SREG_CS: return &save->cs;
2332 case VCPU_SREG_DS: return &save->ds;
2333 case VCPU_SREG_ES: return &save->es;
2334 case VCPU_SREG_FS: return &save->fs;
2335 case VCPU_SREG_GS: return &save->gs;
2336 case VCPU_SREG_SS: return &save->ss;
2337 case VCPU_SREG_TR: return &save->tr;
2338 case VCPU_SREG_LDTR: return &save->ldtr;
2339 }
2340 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00002341 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342}
2343
2344static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2345{
2346 struct vmcb_seg *s = svm_seg(vcpu, seg);
2347
2348 return s->base;
2349}
2350
2351static void svm_get_segment(struct kvm_vcpu *vcpu,
2352 struct kvm_segment *var, int seg)
2353{
2354 struct vmcb_seg *s = svm_seg(vcpu, seg);
2355
2356 var->base = s->base;
2357 var->limit = s->limit;
2358 var->selector = s->selector;
2359 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
2360 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
2361 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
2362 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
2363 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
2364 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
2365 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05302366
2367 /*
2368 * AMD CPUs circa 2014 track the G bit for all segments except CS.
2369 * However, the SVM spec states that the G bit is not observed by the
2370 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
2371 * So let's synthesize a legal G bit for all segments, this helps
2372 * running KVM nested. It also helps cross-vendor migration, because
2373 * Intel's vmentry has a check on the 'G' bit.
2374 */
2375 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00002376
Joerg Roedele0231712010-02-24 18:59:10 +01002377 /*
2378 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02002379 * for cross vendor migration purposes by "not present"
2380 */
Gioh Kim8eae9572017-05-30 15:24:45 +02002381 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02002382
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002383 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002384 case VCPU_SREG_TR:
2385 /*
2386 * Work around a bug where the busy flag in the tr selector
2387 * isn't exposed
2388 */
Amit Shahc0d09822008-10-27 09:04:18 +00002389 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002390 break;
2391 case VCPU_SREG_DS:
2392 case VCPU_SREG_ES:
2393 case VCPU_SREG_FS:
2394 case VCPU_SREG_GS:
2395 /*
2396 * The accessed bit must always be set in the segment
2397 * descriptor cache, although it can be cleared in the
2398 * descriptor, the cached bit always remains at 1. Since
2399 * Intel has a check on this, set it here to support
2400 * cross-vendor migration.
2401 */
2402 if (!var->unusable)
2403 var->type |= 0x1;
2404 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02002405 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01002406 /*
2407 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02002408 * descriptor is left as 1, although the whole segment has
2409 * been made unusable. Clear it here to pass an Intel VMX
2410 * entry check when cross vendor migrating.
2411 */
2412 if (var->unusable)
2413 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02002414 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02002415 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02002416 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002417 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002418}
2419
Izik Eidus2e4d2652008-03-24 19:38:34 +02002420static int svm_get_cpl(struct kvm_vcpu *vcpu)
2421{
2422 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
2423
2424 return save->cpl;
2425}
2426
Gleb Natapov89a27f42010-02-16 10:51:48 +02002427static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002429 struct vcpu_svm *svm = to_svm(vcpu);
2430
Gleb Natapov89a27f42010-02-16 10:51:48 +02002431 dt->size = svm->vmcb->save.idtr.limit;
2432 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433}
2434
Gleb Natapov89a27f42010-02-16 10:51:48 +02002435static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002437 struct vcpu_svm *svm = to_svm(vcpu);
2438
Gleb Natapov89a27f42010-02-16 10:51:48 +02002439 svm->vmcb->save.idtr.limit = dt->size;
2440 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002441 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442}
2443
Gleb Natapov89a27f42010-02-16 10:51:48 +02002444static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002446 struct vcpu_svm *svm = to_svm(vcpu);
2447
Gleb Natapov89a27f42010-02-16 10:51:48 +02002448 dt->size = svm->vmcb->save.gdtr.limit;
2449 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450}
2451
Gleb Natapov89a27f42010-02-16 10:51:48 +02002452static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002454 struct vcpu_svm *svm = to_svm(vcpu);
2455
Gleb Natapov89a27f42010-02-16 10:51:48 +02002456 svm->vmcb->save.gdtr.limit = dt->size;
2457 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002458 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459}
2460
Avi Kivitye8467fd2009-12-29 18:43:06 +02002461static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2462{
2463}
2464
Avi Kivityaff48ba2010-12-05 18:56:11 +02002465static void svm_decache_cr3(struct kvm_vcpu *vcpu)
2466{
2467}
2468
Anthony Liguori25c4c272007-04-27 09:29:21 +03002469static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002470{
2471}
2472
Avi Kivityd2251572010-01-06 10:55:27 +02002473static void update_cr0_intercept(struct vcpu_svm *svm)
2474{
2475 ulong gcr0 = svm->vcpu.arch.cr0;
2476 u64 *hcr0 = &svm->vmcb->save.cr0;
2477
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002478 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
2479 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
Avi Kivityd2251572010-01-06 10:55:27 +02002480
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002481 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002482
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002483 if (gcr0 == *hcr0) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002484 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
2485 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002486 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002487 set_cr_intercept(svm, INTERCEPT_CR0_READ);
2488 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002489 }
2490}
2491
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2493{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002494 struct vcpu_svm *svm = to_svm(vcpu);
2495
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002496#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002497 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002498 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002499 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002500 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501 }
2502
Mike Dayd77c26f2007-10-08 09:02:08 -04002503 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002504 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002505 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506 }
2507 }
2508#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002509 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002510
2511 if (!npt_enabled)
2512 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02002513
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02002514 /*
2515 * re-enable caching here because the QEMU bios
2516 * does not do it - this results in some delay at
2517 * reboot
2518 */
2519 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
2520 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002521 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002522 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002523 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524}
2525
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002526static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002528 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002529 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
2530
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002531 if (cr4 & X86_CR4_VMXE)
2532 return 1;
2533
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002534 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002535 svm_flush_tlb(vcpu, true);
Joerg Roedel6394b642008-04-09 14:15:29 +02002536
Joerg Roedelec077262008-04-09 14:15:28 +02002537 vcpu->arch.cr4 = cr4;
2538 if (!npt_enabled)
2539 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02002540 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02002541 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002542 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002543 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544}
2545
2546static void svm_set_segment(struct kvm_vcpu *vcpu,
2547 struct kvm_segment *var, int seg)
2548{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002549 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550 struct vmcb_seg *s = svm_seg(vcpu, seg);
2551
2552 s->base = var->base;
2553 s->limit = var->limit;
2554 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02002555 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2556 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2557 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2558 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2559 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2560 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2561 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2562 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002563
2564 /*
2565 * This is always accurate, except if SYSRET returned to a segment
2566 * with SS.DPL != 3. Intel does not have this quirk, and always
2567 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2568 * would entail passing the CPL to userspace and back.
2569 */
2570 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02002571 /* This is symmetric with svm_get_segment() */
2572 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002573
Joerg Roedel060d0c92010-12-03 11:45:57 +01002574 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575}
2576
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01002577static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002579 struct vcpu_svm *svm = to_svm(vcpu);
2580
Joerg Roedel18c918c2010-11-30 18:03:59 +01002581 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03002582
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002583 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002584 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01002585 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002586 } else
2587 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03002588}
2589
Tejun Heo0fe1e002009-10-29 22:34:14 +09002590static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002591{
Tejun Heo0fe1e002009-10-29 22:34:14 +09002592 if (sd->next_asid > sd->max_asid) {
2593 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06002594 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002595 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596 }
2597
Tejun Heo0fe1e002009-10-29 22:34:14 +09002598 svm->asid_generation = sd->asid_generation;
2599 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01002600
2601 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602}
2603
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002604static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2605{
2606 return to_svm(vcpu)->vmcb->save.dr6;
2607}
2608
2609static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2610{
2611 struct vcpu_svm *svm = to_svm(vcpu);
2612
2613 svm->vmcb->save.dr6 = value;
2614 mark_dirty(svm->vmcb, VMCB_DR);
2615}
2616
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002617static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2618{
2619 struct vcpu_svm *svm = to_svm(vcpu);
2620
2621 get_debugreg(vcpu->arch.db[0], 0);
2622 get_debugreg(vcpu->arch.db[1], 1);
2623 get_debugreg(vcpu->arch.db[2], 2);
2624 get_debugreg(vcpu->arch.db[3], 3);
2625 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2626 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2627
2628 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2629 set_dr_intercepts(svm);
2630}
2631
Gleb Natapov020df072010-04-13 10:05:23 +03002632static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002634 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002635
Gleb Natapov020df072010-04-13 10:05:23 +03002636 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01002637 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638}
2639
Avi Kivity851ba692009-08-24 11:10:17 +03002640static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002642 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002643 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002645 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002646 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2647 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002648 svm->vmcb->control.insn_len);
2649}
2650
2651static int npf_interception(struct vcpu_svm *svm)
2652{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002653 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Paolo Bonzinid0006532017-08-11 18:36:43 +02002654 u64 error_code = svm->vmcb->control.exit_info_1;
2655
2656 trace_kvm_page_fault(fault_address, error_code);
2657 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002658 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2659 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002660 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661}
2662
Avi Kivity851ba692009-08-24 11:10:17 +03002663static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002664{
Avi Kivity851ba692009-08-24 11:10:17 +03002665 struct kvm_run *kvm_run = svm->vcpu.run;
2666
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002667 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03002668 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02002669 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002670 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2671 return 1;
2672 }
Gleb Natapov44c11432009-05-11 13:35:52 +03002673
Jan Kiszka6be7d302009-10-18 13:24:54 +02002674 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02002675 disable_nmi_singlestep(svm);
Gleb Natapov44c11432009-05-11 13:35:52 +03002676 }
2677
2678 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002679 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002680 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2681 kvm_run->debug.arch.pc =
2682 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2683 kvm_run->debug.arch.exception = DB_VECTOR;
2684 return 0;
2685 }
2686
2687 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002688}
2689
Avi Kivity851ba692009-08-24 11:10:17 +03002690static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002691{
Avi Kivity851ba692009-08-24 11:10:17 +03002692 struct kvm_run *kvm_run = svm->vcpu.run;
2693
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002694 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2695 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2696 kvm_run->debug.arch.exception = BP_VECTOR;
2697 return 0;
2698}
2699
Avi Kivity851ba692009-08-24 11:10:17 +03002700static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002701{
Wanpeng Li082d06e2018-04-03 16:28:48 -07002702 return handle_ud(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002703}
2704
Eric Northup54a20552015-11-03 18:03:53 +01002705static int ac_interception(struct vcpu_svm *svm)
2706{
2707 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2708 return 1;
2709}
2710
Liran Alon97184202018-03-12 13:12:52 +02002711static int gp_interception(struct vcpu_svm *svm)
2712{
2713 struct kvm_vcpu *vcpu = &svm->vcpu;
2714 u32 error_code = svm->vmcb->control.exit_info_1;
2715 int er;
2716
2717 WARN_ON_ONCE(!enable_vmware_backdoor);
2718
Sean Christopherson0ce97a22018-08-23 13:56:52 -07002719 er = kvm_emulate_instruction(vcpu,
Liran Alon97184202018-03-12 13:12:52 +02002720 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
2721 if (er == EMULATE_USER_EXIT)
2722 return 0;
2723 else if (er != EMULATE_DONE)
2724 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2725 return 1;
2726}
2727
Joerg Roedel67ec6602010-05-17 14:43:35 +02002728static bool is_erratum_383(void)
2729{
2730 int err, i;
2731 u64 value;
2732
2733 if (!erratum_383_found)
2734 return false;
2735
2736 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2737 if (err)
2738 return false;
2739
2740 /* Bit 62 may or may not be set for this mce */
2741 value &= ~(1ULL << 62);
2742
2743 if (value != 0xb600000000010015ULL)
2744 return false;
2745
2746 /* Clear MCi_STATUS registers */
2747 for (i = 0; i < 6; ++i)
2748 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2749
2750 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2751 if (!err) {
2752 u32 low, high;
2753
2754 value &= ~(1ULL << 2);
2755 low = lower_32_bits(value);
2756 high = upper_32_bits(value);
2757
2758 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2759 }
2760
2761 /* Flush tlb to evict multi-match entries */
2762 __flush_tlb_all();
2763
2764 return true;
2765}
2766
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002767static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002768{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002769 if (is_erratum_383()) {
2770 /*
2771 * Erratum 383 triggered. Guest state is corrupt so kill the
2772 * guest.
2773 */
2774 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2775
Avi Kivitya8eeb042010-05-10 12:34:53 +03002776 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002777
2778 return;
2779 }
2780
Joerg Roedel53371b52008-04-09 14:15:30 +02002781 /*
2782 * On an #MC intercept the MCE handler is not called automatically in
2783 * the host. So do it by hand here.
2784 */
2785 asm volatile (
2786 "int $0x12\n");
2787 /* not sure if we ever come back to this point */
2788
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002789 return;
2790}
2791
2792static int mc_interception(struct vcpu_svm *svm)
2793{
Joerg Roedel53371b52008-04-09 14:15:30 +02002794 return 1;
2795}
2796
Avi Kivity851ba692009-08-24 11:10:17 +03002797static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002798{
Avi Kivity851ba692009-08-24 11:10:17 +03002799 struct kvm_run *kvm_run = svm->vcpu.run;
2800
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002801 /*
2802 * VMCB is undefined after a SHUTDOWN intercept
2803 * so reinitialize it.
2804 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002805 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002806 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002807
2808 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2809 return 0;
2810}
2811
Avi Kivity851ba692009-08-24 11:10:17 +03002812static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002814 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002815 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Sean Christophersondca7f122018-03-08 08:57:27 -08002816 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002817 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818
Rusty Russelle756fc62007-07-30 20:07:08 +10002819 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002820 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002821 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002822 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07002823 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002824
Avi Kivity039576c2007-03-20 12:46:50 +02002825 port = io_info >> 16;
2826 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002827 svm->next_rip = svm->vmcb->control.exit_info_2;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002828
Sean Christophersondca7f122018-03-08 08:57:27 -08002829 return kvm_fast_pio(&svm->vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830}
2831
Avi Kivity851ba692009-08-24 11:10:17 +03002832static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002833{
2834 return 1;
2835}
2836
Avi Kivity851ba692009-08-24 11:10:17 +03002837static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002838{
2839 ++svm->vcpu.stat.irq_exits;
2840 return 1;
2841}
2842
Avi Kivity851ba692009-08-24 11:10:17 +03002843static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844{
2845 return 1;
2846}
2847
Avi Kivity851ba692009-08-24 11:10:17 +03002848static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002849{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002850 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002851 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852}
2853
Avi Kivity851ba692009-08-24 11:10:17 +03002854static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002855{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002856 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002857 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002858}
2859
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002860static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2861{
2862 struct vcpu_svm *svm = to_svm(vcpu);
2863
2864 return svm->nested.nested_cr3;
2865}
2866
Avi Kivitye4e517b2011-07-28 11:36:17 +03002867static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2868{
2869 struct vcpu_svm *svm = to_svm(vcpu);
2870 u64 cr3 = svm->nested.nested_cr3;
2871 u64 pdpte;
2872 int ret;
2873
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002874 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002875 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002876 if (ret)
2877 return 0;
2878 return pdpte;
2879}
2880
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002881static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2882 unsigned long root)
2883{
2884 struct vcpu_svm *svm = to_svm(vcpu);
2885
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002886 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01002887 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002888}
2889
Avi Kivity6389ee92010-11-29 16:12:30 +02002890static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2891 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002892{
2893 struct vcpu_svm *svm = to_svm(vcpu);
2894
Paolo Bonzini5e352512014-09-02 13:18:37 +02002895 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2896 /*
2897 * TODO: track the cause of the nested page fault, and
2898 * correctly fill in the high bits of exit_info_1.
2899 */
2900 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2901 svm->vmcb->control.exit_code_hi = 0;
2902 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2903 svm->vmcb->control.exit_info_2 = fault->address;
2904 }
2905
2906 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2907 svm->vmcb->control.exit_info_1 |= fault->error_code;
2908
2909 /*
2910 * The present bit is always zero for page structure faults on real
2911 * hardware.
2912 */
2913 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2914 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002915
2916 nested_svm_vmexit(svm);
2917}
2918
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002919static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002920{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002921 WARN_ON(mmu_is_nested(vcpu));
2922 kvm_init_shadow_mmu(vcpu);
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002923 vcpu->arch.mmu->set_cr3 = nested_svm_set_tdp_cr3;
2924 vcpu->arch.mmu->get_cr3 = nested_svm_get_tdp_cr3;
2925 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
2926 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
2927 vcpu->arch.mmu->shadow_root_level = get_npt_level(vcpu);
2928 reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002929 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002930}
2931
2932static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2933{
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002934 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002935}
2936
Alexander Grafc0725422008-11-25 20:17:03 +01002937static int nested_svm_check_permissions(struct vcpu_svm *svm)
2938{
Dan Carpentere9196ce2017-05-18 10:39:53 +03002939 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
2940 !is_paging(&svm->vcpu)) {
Alexander Grafc0725422008-11-25 20:17:03 +01002941 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2942 return 1;
2943 }
2944
2945 if (svm->vmcb->save.cpl) {
2946 kvm_inject_gp(&svm->vcpu, 0);
2947 return 1;
2948 }
2949
Dan Carpentere9196ce2017-05-18 10:39:53 +03002950 return 0;
Alexander Grafc0725422008-11-25 20:17:03 +01002951}
2952
Alexander Grafcf74a782008-11-25 20:17:08 +01002953static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2954 bool has_error_code, u32 error_code)
2955{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002956 int vmexit;
2957
Joerg Roedel20307532010-11-29 17:51:48 +01002958 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002959 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002960
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002961 vmexit = nested_svm_intercept(svm);
2962 if (vmexit != NESTED_EXIT_DONE)
2963 return 0;
2964
Joerg Roedel0295ad72009-08-07 11:49:37 +02002965 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2966 svm->vmcb->control.exit_code_hi = 0;
2967 svm->vmcb->control.exit_info_1 = error_code;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002968
2969 /*
Jim Mattsonda998b42018-10-16 14:29:22 -07002970 * EXITINFO2 is undefined for all exception intercepts other
2971 * than #PF.
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002972 */
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002973 if (svm->vcpu.arch.exception.nested_apf)
2974 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
Jim Mattsonda998b42018-10-16 14:29:22 -07002975 else if (svm->vcpu.arch.exception.has_payload)
2976 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002977 else
2978 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
Joerg Roedel0295ad72009-08-07 11:49:37 +02002979
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002980 svm->nested.exit_required = true;
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002981 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002982}
2983
Joerg Roedel8fe54652010-02-19 16:23:01 +01002984/* This function returns true if it is save to enable the irq window */
2985static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002986{
Joerg Roedel20307532010-11-29 17:51:48 +01002987 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002988 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002989
Joerg Roedel26666952009-08-07 11:49:46 +02002990 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002991 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002992
Joerg Roedel26666952009-08-07 11:49:46 +02002993 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002994 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002995
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002996 /*
2997 * if vmexit was already requested (by intercepted exception
2998 * for instance) do not overwrite it with "external interrupt"
2999 * vmexit.
3000 */
3001 if (svm->nested.exit_required)
3002 return false;
3003
Joerg Roedel197717d2010-02-24 18:59:19 +01003004 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
3005 svm->vmcb->control.exit_info_1 = 0;
3006 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02003007
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003008 if (svm->nested.intercept & 1ULL) {
3009 /*
3010 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003011 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003012 * #vmexit emulation might sleep. Only signal request for
3013 * the #vmexit here.
3014 */
3015 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02003016 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01003017 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01003018 }
3019
Joerg Roedel8fe54652010-02-19 16:23:01 +01003020 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01003021}
3022
Joerg Roedel887f5002010-02-24 18:59:12 +01003023/* This function returns true if it is save to enable the nmi window */
3024static inline bool nested_svm_nmi(struct vcpu_svm *svm)
3025{
Joerg Roedel20307532010-11-29 17:51:48 +01003026 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01003027 return true;
3028
3029 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
3030 return true;
3031
3032 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
3033 svm->nested.exit_required = true;
3034
3035 return false;
3036}
3037
Joerg Roedel7597f122010-02-19 16:23:00 +01003038static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003039{
3040 struct page *page;
3041
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01003042 might_sleep();
3043
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003044 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003045 if (is_error_page(page))
3046 goto error;
3047
Joerg Roedel7597f122010-02-19 16:23:00 +01003048 *_page = page;
3049
3050 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003051
3052error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003053 kvm_inject_gp(&svm->vcpu, 0);
3054
3055 return NULL;
3056}
3057
Joerg Roedel7597f122010-02-19 16:23:00 +01003058static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003059{
Joerg Roedel7597f122010-02-19 16:23:00 +01003060 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003061 kvm_release_page_dirty(page);
3062}
3063
Joerg Roedelce2ac082010-03-01 15:34:39 +01003064static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003065{
Jan Kiszka9bf41832014-06-30 10:54:17 +02003066 unsigned port, size, iopm_len;
3067 u16 val, mask;
3068 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003069 u64 gpa;
3070
3071 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
3072 return NESTED_EXIT_HOST;
3073
3074 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02003075 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
3076 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003077 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02003078 start_bit = port % 8;
3079 iopm_len = (start_bit + size > 8) ? 2 : 1;
3080 mask = (0xf >> (4 - size)) << start_bit;
3081 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003082
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003083 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02003084 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003085
Jan Kiszka9bf41832014-06-30 10:54:17 +02003086 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003087}
3088
Joerg Roedeld2477822010-03-01 15:34:34 +01003089static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003090{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003091 u32 offset, msr, value;
3092 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003093
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003094 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01003095 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003096
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003097 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
3098 offset = svm_msrpm_offset(msr);
3099 write = svm->vmcb->control.exit_info_1 & 1;
3100 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003101
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003102 if (offset == MSR_INVALID)
3103 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003104
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003105 /* Offset is in 32 bit units but need in 8 bit units */
3106 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003107
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003108 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003109 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003110
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003111 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003112}
3113
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003114/* DB exceptions for our internal use must not cause vmexit */
3115static int nested_svm_intercept_db(struct vcpu_svm *svm)
3116{
3117 unsigned long dr6;
3118
3119 /* if we're not singlestepping, it's not ours */
3120 if (!svm->nmi_singlestep)
3121 return NESTED_EXIT_DONE;
3122
3123 /* if it's not a singlestep exception, it's not ours */
3124 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
3125 return NESTED_EXIT_DONE;
3126 if (!(dr6 & DR6_BS))
3127 return NESTED_EXIT_DONE;
3128
3129 /* if the guest is singlestepping, it should get the vmexit */
3130 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
3131 disable_nmi_singlestep(svm);
3132 return NESTED_EXIT_DONE;
3133 }
3134
3135 /* it's ours, the nested hypervisor must not see this one */
3136 return NESTED_EXIT_HOST;
3137}
3138
Joerg Roedel410e4d52009-08-07 11:49:44 +02003139static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003140{
Alexander Grafcf74a782008-11-25 20:17:08 +01003141 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003142
Joerg Roedel410e4d52009-08-07 11:49:44 +02003143 switch (exit_code) {
3144 case SVM_EXIT_INTR:
3145 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02003146 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02003147 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02003148 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01003149 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02003150 if (npt_enabled)
3151 return NESTED_EXIT_HOST;
3152 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02003153 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02003154 /* When we're shadowing, trap PFs, but not async PF */
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003155 if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003156 return NESTED_EXIT_HOST;
3157 break;
3158 default:
3159 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01003160 }
3161
Joerg Roedel410e4d52009-08-07 11:49:44 +02003162 return NESTED_EXIT_CONTINUE;
3163}
3164
3165/*
3166 * If this function returns true, this #vmexit was already handled
3167 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003168static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003169{
3170 u32 exit_code = svm->vmcb->control.exit_code;
3171 int vmexit = NESTED_EXIT_HOST;
3172
Alexander Grafcf74a782008-11-25 20:17:08 +01003173 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003174 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003175 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003176 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003177 case SVM_EXIT_IOIO:
3178 vmexit = nested_svm_intercept_ioio(svm);
3179 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003180 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
3181 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
3182 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003183 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003184 break;
3185 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01003186 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
3187 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
3188 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003189 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003190 break;
3191 }
3192 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
3193 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003194 if (svm->nested.intercept_exceptions & excp_bits) {
3195 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
3196 vmexit = nested_svm_intercept_db(svm);
3197 else
3198 vmexit = NESTED_EXIT_DONE;
3199 }
Gleb Natapov631bc482010-10-14 11:22:52 +02003200 /* async page fault always cause vmexit */
3201 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003202 svm->vcpu.arch.exception.nested_apf != 0)
Gleb Natapov631bc482010-10-14 11:22:52 +02003203 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003204 break;
3205 }
Joerg Roedel228070b2010-04-22 12:33:10 +02003206 case SVM_EXIT_ERR: {
3207 vmexit = NESTED_EXIT_DONE;
3208 break;
3209 }
Alexander Grafcf74a782008-11-25 20:17:08 +01003210 default: {
3211 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02003212 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003213 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003214 }
3215 }
3216
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003217 return vmexit;
3218}
3219
3220static int nested_svm_exit_handled(struct vcpu_svm *svm)
3221{
3222 int vmexit;
3223
3224 vmexit = nested_svm_intercept(svm);
3225
3226 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003227 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003228
3229 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01003230}
3231
Joerg Roedel0460a972009-08-07 11:49:31 +02003232static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
3233{
3234 struct vmcb_control_area *dst = &dst_vmcb->control;
3235 struct vmcb_control_area *from = &from_vmcb->control;
3236
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003237 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003238 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02003239 dst->intercept_exceptions = from->intercept_exceptions;
3240 dst->intercept = from->intercept;
3241 dst->iopm_base_pa = from->iopm_base_pa;
3242 dst->msrpm_base_pa = from->msrpm_base_pa;
3243 dst->tsc_offset = from->tsc_offset;
3244 dst->asid = from->asid;
3245 dst->tlb_ctl = from->tlb_ctl;
3246 dst->int_ctl = from->int_ctl;
3247 dst->int_vector = from->int_vector;
3248 dst->int_state = from->int_state;
3249 dst->exit_code = from->exit_code;
3250 dst->exit_code_hi = from->exit_code_hi;
3251 dst->exit_info_1 = from->exit_info_1;
3252 dst->exit_info_2 = from->exit_info_2;
3253 dst->exit_int_info = from->exit_int_info;
3254 dst->exit_int_info_err = from->exit_int_info_err;
3255 dst->nested_ctl = from->nested_ctl;
3256 dst->event_inj = from->event_inj;
3257 dst->event_inj_err = from->event_inj_err;
3258 dst->nested_cr3 = from->nested_cr3;
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003259 dst->virt_ext = from->virt_ext;
Joerg Roedel0460a972009-08-07 11:49:31 +02003260}
3261
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003262static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003263{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003264 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003265 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02003266 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003267 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01003268
Joerg Roedel17897f32009-10-09 16:08:29 +02003269 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
3270 vmcb->control.exit_info_1,
3271 vmcb->control.exit_info_2,
3272 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003273 vmcb->control.exit_int_info_err,
3274 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02003275
Joerg Roedel7597f122010-02-19 16:23:00 +01003276 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003277 if (!nested_vmcb)
3278 return 1;
3279
Joerg Roedel20307532010-11-29 17:51:48 +01003280 /* Exit Guest-Mode */
3281 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01003282 svm->nested.vmcb = 0;
3283
Alexander Grafcf74a782008-11-25 20:17:08 +01003284 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02003285 disable_gif(svm);
3286
3287 nested_vmcb->save.es = vmcb->save.es;
3288 nested_vmcb->save.cs = vmcb->save.cs;
3289 nested_vmcb->save.ss = vmcb->save.ss;
3290 nested_vmcb->save.ds = vmcb->save.ds;
3291 nested_vmcb->save.gdtr = vmcb->save.gdtr;
3292 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02003293 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003294 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003295 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003296 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003297 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03003298 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003299 nested_vmcb->save.rip = vmcb->save.rip;
3300 nested_vmcb->save.rsp = vmcb->save.rsp;
3301 nested_vmcb->save.rax = vmcb->save.rax;
3302 nested_vmcb->save.dr7 = vmcb->save.dr7;
3303 nested_vmcb->save.dr6 = vmcb->save.dr6;
3304 nested_vmcb->save.cpl = vmcb->save.cpl;
3305
3306 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
3307 nested_vmcb->control.int_vector = vmcb->control.int_vector;
3308 nested_vmcb->control.int_state = vmcb->control.int_state;
3309 nested_vmcb->control.exit_code = vmcb->control.exit_code;
3310 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
3311 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
3312 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
3313 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
3314 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003315
3316 if (svm->nrips_enabled)
3317 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02003318
3319 /*
3320 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
3321 * to make sure that we do not lose injected events. So check event_inj
3322 * here and copy it to exit_int_info if it is valid.
3323 * Exit_int_info and event_inj can't be both valid because the case
3324 * below only happens on a VMRUN instruction intercept which has
3325 * no valid exit_int_info set.
3326 */
3327 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
3328 struct vmcb_control_area *nc = &nested_vmcb->control;
3329
3330 nc->exit_int_info = vmcb->control.event_inj;
3331 nc->exit_int_info_err = vmcb->control.event_inj_err;
3332 }
3333
Joerg Roedel33740e42009-08-07 11:49:29 +02003334 nested_vmcb->control.tlb_ctl = 0;
3335 nested_vmcb->control.event_inj = 0;
3336 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01003337
3338 /* We always set V_INTR_MASKING and remember the old value in hflags */
3339 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
3340 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
3341
Alexander Grafcf74a782008-11-25 20:17:08 +01003342 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02003343 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01003344
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003345 svm->vcpu.arch.tsc_offset = svm->vmcb->control.tsc_offset;
Alexander Graf219b65d2009-06-15 15:21:25 +02003346 kvm_clear_exception_queue(&svm->vcpu);
3347 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003348
Joerg Roedel4b161842010-09-10 17:31:03 +02003349 svm->nested.nested_cr3 = 0;
3350
Alexander Grafcf74a782008-11-25 20:17:08 +01003351 /* Restore selected save entries */
3352 svm->vmcb->save.es = hsave->save.es;
3353 svm->vmcb->save.cs = hsave->save.cs;
3354 svm->vmcb->save.ss = hsave->save.ss;
3355 svm->vmcb->save.ds = hsave->save.ds;
3356 svm->vmcb->save.gdtr = hsave->save.gdtr;
3357 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003358 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01003359 svm_set_efer(&svm->vcpu, hsave->save.efer);
3360 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
3361 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
3362 if (npt_enabled) {
3363 svm->vmcb->save.cr3 = hsave->save.cr3;
3364 svm->vcpu.arch.cr3 = hsave->save.cr3;
3365 } else {
Avi Kivity23902182010-06-10 17:02:16 +03003366 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01003367 }
3368 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
3369 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
3370 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
3371 svm->vmcb->save.dr7 = 0;
3372 svm->vmcb->save.cpl = 0;
3373 svm->vmcb->control.exit_int_info = 0;
3374
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003375 mark_all_dirty(svm->vmcb);
3376
Joerg Roedel7597f122010-02-19 16:23:00 +01003377 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01003378
Joerg Roedel4b161842010-09-10 17:31:03 +02003379 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003380 kvm_mmu_reset_context(&svm->vcpu);
3381 kvm_mmu_load(&svm->vcpu);
3382
3383 return 0;
3384}
Alexander Graf3d6368e2008-11-25 20:17:07 +01003385
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003386static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003387{
Joerg Roedel323c3d82010-03-01 15:34:37 +01003388 /*
3389 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003390 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01003391 * the kvm msr permission bitmap may contain zero bits
3392 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01003393 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003394
Joerg Roedel323c3d82010-03-01 15:34:37 +01003395 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3396 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003397
Joerg Roedel323c3d82010-03-01 15:34:37 +01003398 for (i = 0; i < MSRPM_OFFSETS; i++) {
3399 u32 value, p;
3400 u64 offset;
3401
3402 if (msrpm_offsets[i] == 0xffffffff)
3403 break;
3404
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003405 p = msrpm_offsets[i];
3406 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01003407
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003408 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01003409 return false;
3410
3411 svm->nested.msrpm[p] = svm->msrpm[p] | value;
3412 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003413
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05003414 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
Alexander Graf3d6368e2008-11-25 20:17:07 +01003415
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003416 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003417}
3418
Joerg Roedel52c65a302010-08-02 16:46:44 +02003419static bool nested_vmcb_checks(struct vmcb *vmcb)
3420{
3421 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
3422 return false;
3423
Joerg Roedeldbe77582010-08-02 16:46:45 +02003424 if (vmcb->control.asid == 0)
3425 return false;
3426
Tom Lendackycea3a192017-12-04 10:57:24 -06003427 if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
3428 !npt_enabled)
Joerg Roedel4b161842010-09-10 17:31:03 +02003429 return false;
3430
Joerg Roedel52c65a302010-08-02 16:46:44 +02003431 return true;
3432}
3433
Ladi Prosekc2634062017-10-11 16:54:44 +02003434static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
3435 struct vmcb *nested_vmcb, struct page *page)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003436{
Avi Kivityf6e78472010-08-02 15:30:20 +03003437 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003438 svm->vcpu.arch.hflags |= HF_HIF_MASK;
3439 else
3440 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
3441
Tom Lendackycea3a192017-12-04 10:57:24 -06003442 if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
Joerg Roedel4b161842010-09-10 17:31:03 +02003443 kvm_mmu_unload(&svm->vcpu);
3444 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
3445 nested_svm_init_mmu_context(&svm->vcpu);
3446 }
3447
Alexander Graf3d6368e2008-11-25 20:17:07 +01003448 /* Load the nested guest state */
3449 svm->vmcb->save.es = nested_vmcb->save.es;
3450 svm->vmcb->save.cs = nested_vmcb->save.cs;
3451 svm->vmcb->save.ss = nested_vmcb->save.ss;
3452 svm->vmcb->save.ds = nested_vmcb->save.ds;
3453 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
3454 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003455 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003456 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
3457 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
3458 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
3459 if (npt_enabled) {
3460 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
3461 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003462 } else
Avi Kivity23902182010-06-10 17:02:16 +03003463 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003464
3465 /* Guest paging mode is active - reset mmu */
3466 kvm_mmu_reset_context(&svm->vcpu);
3467
Joerg Roedeldefbba52009-08-07 11:49:30 +02003468 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003469 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
3470 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
3471 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01003472
Alexander Graf3d6368e2008-11-25 20:17:07 +01003473 /* In case we don't even reach vcpu_run, the fields are not updated */
3474 svm->vmcb->save.rax = nested_vmcb->save.rax;
3475 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3476 svm->vmcb->save.rip = nested_vmcb->save.rip;
3477 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3478 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3479 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3480
Joerg Roedelf7138532010-03-01 15:34:40 +01003481 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003482 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003483
Joerg Roedelaad42c62009-08-07 11:49:34 +02003484 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003485 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003486 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02003487 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3488 svm->nested.intercept = nested_vmcb->control.intercept;
3489
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08003490 svm_flush_tlb(&svm->vcpu, true);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003491 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003492 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3493 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3494 else
3495 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3496
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003497 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3498 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003499 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3500 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003501 }
3502
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003503 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003504 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003505
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003506 svm->vcpu.arch.tsc_offset += nested_vmcb->control.tsc_offset;
3507 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
3508
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003509 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003510 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3511 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003512 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3513 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3514
Joerg Roedel7597f122010-02-19 16:23:00 +01003515 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003516
Joerg Roedel20307532010-11-29 17:51:48 +01003517 /* Enter Guest-Mode */
3518 enter_guest_mode(&svm->vcpu);
3519
Joerg Roedel384c6362010-11-30 18:03:56 +01003520 /*
3521 * Merge guest and host intercepts - must be called with vcpu in
3522 * guest-mode to take affect here
3523 */
3524 recalc_intercepts(svm);
3525
Joerg Roedel06fc77722010-02-19 16:23:07 +01003526 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003527
Joerg Roedel2af91942009-08-07 11:49:28 +02003528 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003529
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003530 mark_all_dirty(svm->vmcb);
Ladi Prosekc2634062017-10-11 16:54:44 +02003531}
3532
3533static bool nested_svm_vmrun(struct vcpu_svm *svm)
3534{
3535 struct vmcb *nested_vmcb;
3536 struct vmcb *hsave = svm->nested.hsave;
3537 struct vmcb *vmcb = svm->vmcb;
3538 struct page *page;
3539 u64 vmcb_gpa;
3540
3541 vmcb_gpa = svm->vmcb->save.rax;
3542
3543 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
3544 if (!nested_vmcb)
3545 return false;
3546
3547 if (!nested_vmcb_checks(nested_vmcb)) {
3548 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3549 nested_vmcb->control.exit_code_hi = 0;
3550 nested_vmcb->control.exit_info_1 = 0;
3551 nested_vmcb->control.exit_info_2 = 0;
3552
3553 nested_svm_unmap(page);
3554
3555 return false;
3556 }
3557
3558 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3559 nested_vmcb->save.rip,
3560 nested_vmcb->control.int_ctl,
3561 nested_vmcb->control.event_inj,
3562 nested_vmcb->control.nested_ctl);
3563
3564 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3565 nested_vmcb->control.intercept_cr >> 16,
3566 nested_vmcb->control.intercept_exceptions,
3567 nested_vmcb->control.intercept);
3568
3569 /* Clear internal status */
3570 kvm_clear_exception_queue(&svm->vcpu);
3571 kvm_clear_interrupt_queue(&svm->vcpu);
3572
3573 /*
3574 * Save the old vmcb, so we don't need to pick what we save, but can
3575 * restore everything when a VMEXIT occurs
3576 */
3577 hsave->save.es = vmcb->save.es;
3578 hsave->save.cs = vmcb->save.cs;
3579 hsave->save.ss = vmcb->save.ss;
3580 hsave->save.ds = vmcb->save.ds;
3581 hsave->save.gdtr = vmcb->save.gdtr;
3582 hsave->save.idtr = vmcb->save.idtr;
3583 hsave->save.efer = svm->vcpu.arch.efer;
3584 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3585 hsave->save.cr4 = svm->vcpu.arch.cr4;
3586 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3587 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3588 hsave->save.rsp = vmcb->save.rsp;
3589 hsave->save.rax = vmcb->save.rax;
3590 if (npt_enabled)
3591 hsave->save.cr3 = vmcb->save.cr3;
3592 else
3593 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3594
3595 copy_vmcb_control_area(hsave, vmcb);
3596
3597 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003598
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003599 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003600}
3601
Joerg Roedel9966bf62009-08-07 11:49:40 +02003602static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01003603{
3604 to_vmcb->save.fs = from_vmcb->save.fs;
3605 to_vmcb->save.gs = from_vmcb->save.gs;
3606 to_vmcb->save.tr = from_vmcb->save.tr;
3607 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3608 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3609 to_vmcb->save.star = from_vmcb->save.star;
3610 to_vmcb->save.lstar = from_vmcb->save.lstar;
3611 to_vmcb->save.cstar = from_vmcb->save.cstar;
3612 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3613 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3614 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3615 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01003616}
3617
Avi Kivity851ba692009-08-24 11:10:17 +03003618static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003619{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003620 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003621 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003622 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003623
Alexander Graf55426752008-11-25 20:17:06 +01003624 if (nested_svm_check_permissions(svm))
3625 return 1;
3626
Joerg Roedel7597f122010-02-19 16:23:00 +01003627 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003628 if (!nested_vmcb)
3629 return 1;
3630
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003631 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003632 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003633
Joerg Roedel9966bf62009-08-07 11:49:40 +02003634 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003635 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003636
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003637 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003638}
3639
Avi Kivity851ba692009-08-24 11:10:17 +03003640static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003641{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003642 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003643 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003644 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003645
Alexander Graf55426752008-11-25 20:17:06 +01003646 if (nested_svm_check_permissions(svm))
3647 return 1;
3648
Joerg Roedel7597f122010-02-19 16:23:00 +01003649 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003650 if (!nested_vmcb)
3651 return 1;
3652
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003653 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003654 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003655
Joerg Roedel9966bf62009-08-07 11:49:40 +02003656 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003657 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003658
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003659 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003660}
3661
Avi Kivity851ba692009-08-24 11:10:17 +03003662static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003663{
Alexander Graf3d6368e2008-11-25 20:17:07 +01003664 if (nested_svm_check_permissions(svm))
3665 return 1;
3666
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02003667 /* Save rip after vmrun instruction */
3668 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003669
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003670 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01003671 return 1;
3672
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003673 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02003674 goto failed;
3675
3676 return 1;
3677
3678failed:
3679
3680 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3681 svm->vmcb->control.exit_code_hi = 0;
3682 svm->vmcb->control.exit_info_1 = 0;
3683 svm->vmcb->control.exit_info_2 = 0;
3684
3685 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003686
3687 return 1;
3688}
3689
Avi Kivity851ba692009-08-24 11:10:17 +03003690static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003691{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003692 int ret;
3693
Alexander Graf1371d902008-11-25 20:17:04 +01003694 if (nested_svm_check_permissions(svm))
3695 return 1;
3696
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003697 /*
3698 * If VGIF is enabled, the STGI intercept is only added to
Ladi Prosekcc3d9672017-10-17 16:02:39 +02003699 * detect the opening of the SMI/NMI window; remove it now.
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003700 */
3701 if (vgif_enabled(svm))
3702 clr_intercept(svm, INTERCEPT_STGI);
3703
Alexander Graf1371d902008-11-25 20:17:04 +01003704 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003705 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003706 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003707
Joerg Roedel2af91942009-08-07 11:49:28 +02003708 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003709
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003710 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003711}
3712
Avi Kivity851ba692009-08-24 11:10:17 +03003713static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003714{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003715 int ret;
3716
Alexander Graf1371d902008-11-25 20:17:04 +01003717 if (nested_svm_check_permissions(svm))
3718 return 1;
3719
3720 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003721 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003722
Joerg Roedel2af91942009-08-07 11:49:28 +02003723 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003724
3725 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05003726 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3727 svm_clear_vintr(svm);
3728 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3729 mark_dirty(svm->vmcb, VMCB_INTR);
3730 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003731
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003732 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003733}
3734
Avi Kivity851ba692009-08-24 11:10:17 +03003735static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02003736{
3737 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02003738
David Kaplan668f1982015-02-20 16:02:10 -06003739 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
3740 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02003741
Alexander Grafff092382009-06-15 15:21:24 +02003742 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06003743 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02003744
3745 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003746 return kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02003747}
3748
Joerg Roedel532a46b2009-10-09 16:08:32 +02003749static int skinit_interception(struct vcpu_svm *svm)
3750{
David Kaplan668f1982015-02-20 16:02:10 -06003751 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003752
3753 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3754 return 1;
3755}
3756
David Kaplandab429a2015-03-02 13:43:37 -06003757static int wbinvd_interception(struct vcpu_svm *svm)
3758{
Kyle Huey6affcbe2016-11-29 12:40:40 -08003759 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06003760}
3761
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003762static int xsetbv_interception(struct vcpu_svm *svm)
3763{
3764 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
3765 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3766
3767 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3768 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003769 return kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003770 }
3771
3772 return 1;
3773}
3774
Avi Kivity851ba692009-08-24 11:10:17 +03003775static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003776{
Izik Eidus37817f22008-03-24 23:14:53 +02003777 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003778 int reason;
3779 int int_type = svm->vmcb->control.exit_int_info &
3780 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003781 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003782 uint32_t type =
3783 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3784 uint32_t idt_v =
3785 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003786 bool has_error_code = false;
3787 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003788
3789 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003790
Izik Eidus37817f22008-03-24 23:14:53 +02003791 if (svm->vmcb->control.exit_info_2 &
3792 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003793 reason = TASK_SWITCH_IRET;
3794 else if (svm->vmcb->control.exit_info_2 &
3795 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3796 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003797 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003798 reason = TASK_SWITCH_GATE;
3799 else
3800 reason = TASK_SWITCH_CALL;
3801
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003802 if (reason == TASK_SWITCH_GATE) {
3803 switch (type) {
3804 case SVM_EXITINTINFO_TYPE_NMI:
3805 svm->vcpu.arch.nmi_injected = false;
3806 break;
3807 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003808 if (svm->vmcb->control.exit_info_2 &
3809 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3810 has_error_code = true;
3811 error_code =
3812 (u32)svm->vmcb->control.exit_info_2;
3813 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003814 kvm_clear_exception_queue(&svm->vcpu);
3815 break;
3816 case SVM_EXITINTINFO_TYPE_INTR:
3817 kvm_clear_interrupt_queue(&svm->vcpu);
3818 break;
3819 default:
3820 break;
3821 }
3822 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003823
Gleb Natapov8317c292009-04-12 13:37:02 +03003824 if (reason != TASK_SWITCH_GATE ||
3825 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3826 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03003827 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3828 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003829
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003830 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3831 int_vec = -1;
3832
3833 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03003834 has_error_code, error_code) == EMULATE_FAIL) {
3835 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3836 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3837 svm->vcpu.run->internal.ndata = 0;
3838 return 0;
3839 }
3840 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003841}
3842
Avi Kivity851ba692009-08-24 11:10:17 +03003843static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003845 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Kyle Huey6a908b62016-11-29 12:40:37 -08003846 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847}
3848
Avi Kivity851ba692009-08-24 11:10:17 +03003849static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003850{
3851 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003852 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003853 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003854 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003855 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003856 return 1;
3857}
3858
Avi Kivity851ba692009-08-24 11:10:17 +03003859static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003860{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003861 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Sean Christopherson0ce97a22018-08-23 13:56:52 -07003862 return kvm_emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Andre Przywaradf4f31082010-12-21 11:12:06 +01003863
3864 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003865 return kvm_skip_emulated_instruction(&svm->vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003866}
3867
Avi Kivity851ba692009-08-24 11:10:17 +03003868static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003869{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07003870 return kvm_emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871}
3872
Brijesh Singh7607b712018-02-19 10:14:44 -06003873static int rsm_interception(struct vcpu_svm *svm)
3874{
Sean Christopherson35be0ad2018-08-23 13:56:47 -07003875 return kvm_emulate_instruction_from_buffer(&svm->vcpu,
3876 rsm_ins_bytes, 2) == EMULATE_DONE;
Brijesh Singh7607b712018-02-19 10:14:44 -06003877}
3878
Avi Kivity332b56e2011-11-10 14:57:24 +02003879static int rdpmc_interception(struct vcpu_svm *svm)
3880{
3881 int err;
3882
3883 if (!static_cpu_has(X86_FEATURE_NRIPS))
3884 return emulate_on_interception(svm);
3885
3886 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08003887 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02003888}
3889
Xiubo Li52eb5a62015-03-13 17:39:45 +08003890static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3891 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003892{
3893 unsigned long cr0 = svm->vcpu.arch.cr0;
3894 bool ret = false;
3895 u64 intercept;
3896
3897 intercept = svm->nested.intercept;
3898
3899 if (!is_guest_mode(&svm->vcpu) ||
3900 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3901 return false;
3902
3903 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3904 val &= ~SVM_CR0_SELECTIVE_MASK;
3905
3906 if (cr0 ^ val) {
3907 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3908 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3909 }
3910
3911 return ret;
3912}
3913
Andre Przywara7ff76d52010-12-21 11:12:04 +01003914#define CR_VALID (1ULL << 63)
3915
3916static int cr_interception(struct vcpu_svm *svm)
3917{
3918 int reg, cr;
3919 unsigned long val;
3920 int err;
3921
3922 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3923 return emulate_on_interception(svm);
3924
3925 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3926 return emulate_on_interception(svm);
3927
3928 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003929 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3930 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3931 else
3932 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003933
3934 err = 0;
3935 if (cr >= 16) { /* mov to cr */
3936 cr -= 16;
3937 val = kvm_register_read(&svm->vcpu, reg);
3938 switch (cr) {
3939 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003940 if (!check_selective_cr0_intercepted(svm, val))
3941 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003942 else
3943 return 1;
3944
Andre Przywara7ff76d52010-12-21 11:12:04 +01003945 break;
3946 case 3:
3947 err = kvm_set_cr3(&svm->vcpu, val);
3948 break;
3949 case 4:
3950 err = kvm_set_cr4(&svm->vcpu, val);
3951 break;
3952 case 8:
3953 err = kvm_set_cr8(&svm->vcpu, val);
3954 break;
3955 default:
3956 WARN(1, "unhandled write to CR%d", cr);
3957 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3958 return 1;
3959 }
3960 } else { /* mov from cr */
3961 switch (cr) {
3962 case 0:
3963 val = kvm_read_cr0(&svm->vcpu);
3964 break;
3965 case 2:
3966 val = svm->vcpu.arch.cr2;
3967 break;
3968 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003969 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003970 break;
3971 case 4:
3972 val = kvm_read_cr4(&svm->vcpu);
3973 break;
3974 case 8:
3975 val = kvm_get_cr8(&svm->vcpu);
3976 break;
3977 default:
3978 WARN(1, "unhandled read from CR%d", cr);
3979 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3980 return 1;
3981 }
3982 kvm_register_write(&svm->vcpu, reg, val);
3983 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08003984 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003985}
3986
Andre Przywaracae37972010-12-21 11:12:05 +01003987static int dr_interception(struct vcpu_svm *svm)
3988{
3989 int reg, dr;
3990 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003991
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003992 if (svm->vcpu.guest_debug == 0) {
3993 /*
3994 * No more DR vmexits; force a reload of the debug registers
3995 * and reenter on this instruction. The next vmexit will
3996 * retrieve the full state of the debug registers.
3997 */
3998 clr_dr_intercepts(svm);
3999 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
4000 return 1;
4001 }
4002
Andre Przywaracae37972010-12-21 11:12:05 +01004003 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
4004 return emulate_on_interception(svm);
4005
4006 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
4007 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
4008
4009 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004010 if (!kvm_require_dr(&svm->vcpu, dr - 16))
4011 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01004012 val = kvm_register_read(&svm->vcpu, reg);
4013 kvm_set_dr(&svm->vcpu, dr - 16, val);
4014 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004015 if (!kvm_require_dr(&svm->vcpu, dr))
4016 return 1;
4017 kvm_get_dr(&svm->vcpu, dr, &val);
4018 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01004019 }
4020
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004021 return kvm_skip_emulated_instruction(&svm->vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01004022}
4023
Avi Kivity851ba692009-08-24 11:10:17 +03004024static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01004025{
Avi Kivity851ba692009-08-24 11:10:17 +03004026 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004027 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03004028
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004029 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
4030 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01004031 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004032 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01004033 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004034 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01004035 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01004036 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
4037 return 0;
4038}
4039
Tom Lendacky801e4592018-02-21 13:39:51 -06004040static int svm_get_msr_feature(struct kvm_msr_entry *msr)
4041{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004042 msr->data = 0;
4043
4044 switch (msr->index) {
4045 case MSR_F10H_DECFG:
4046 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
4047 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
4048 break;
4049 default:
4050 return 1;
4051 }
4052
4053 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004054}
4055
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004056static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004058 struct vcpu_svm *svm = to_svm(vcpu);
4059
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004060 switch (msr_info->index) {
Brian Gerst8c065852010-07-17 09:03:26 -04004061 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004062 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004063 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08004064#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004066 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067 break;
4068 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004069 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070 break;
4071 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004072 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073 break;
4074 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004075 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004076 break;
4077#endif
4078 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004079 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004080 break;
4081 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004082 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004083 break;
4084 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004085 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004086 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004087 case MSR_TSC_AUX:
4088 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4089 return 1;
4090 msr_info->data = svm->tsc_aux;
4091 break;
Joerg Roedele0231712010-02-24 18:59:10 +01004092 /*
4093 * Nobody will change the following 5 values in the VMCB so we can
4094 * safely return them on rdmsr. They will always be 0 until LBRV is
4095 * implemented.
4096 */
Joerg Roedela2938c82008-02-13 16:30:28 +01004097 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004098 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01004099 break;
4100 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004101 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01004102 break;
4103 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004104 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01004105 break;
4106 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004107 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01004108 break;
4109 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004110 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01004111 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004112 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004113 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004114 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01004115 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004116 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01004117 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004118 case MSR_IA32_SPEC_CTRL:
4119 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -04004120 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
4121 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004122 return 1;
4123
4124 msr_info->data = svm->spec_ctrl;
4125 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02004126 case MSR_AMD64_VIRT_SPEC_CTRL:
4127 if (!msr_info->host_initiated &&
4128 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
4129 return 1;
4130
4131 msr_info->data = svm->virt_spec_ctrl;
4132 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01004133 case MSR_F15H_IC_CFG: {
4134
4135 int family, model;
4136
4137 family = guest_cpuid_family(vcpu);
4138 model = guest_cpuid_model(vcpu);
4139
4140 if (family < 0 || model < 0)
4141 return kvm_get_msr_common(vcpu, msr_info);
4142
4143 msr_info->data = 0;
4144
4145 if (family == 0x15 &&
4146 (model >= 0x2 && model < 0x20))
4147 msr_info->data = 0x1E;
4148 }
4149 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004150 case MSR_F10H_DECFG:
4151 msr_info->data = svm->msr_decfg;
4152 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004153 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004154 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 }
4156 return 0;
4157}
4158
Avi Kivity851ba692009-08-24 11:10:17 +03004159static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004160{
David Kaplan668f1982015-02-20 16:02:10 -06004161 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004162 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004164 msr_info.index = ecx;
4165 msr_info.host_initiated = false;
4166 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02004167 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004168 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004169 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004170 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004171 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004172
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004173 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
4174 msr_info.data & 0xffffffff);
4175 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
4176 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004177 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004178 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004179 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180}
4181
Joerg Roedel4a810182010-02-24 18:59:15 +01004182static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
4183{
4184 struct vcpu_svm *svm = to_svm(vcpu);
4185 int svm_dis, chg_mask;
4186
4187 if (data & ~SVM_VM_CR_VALID_MASK)
4188 return 1;
4189
4190 chg_mask = SVM_VM_CR_VALID_MASK;
4191
4192 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
4193 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
4194
4195 svm->nested.vm_cr_msr &= ~chg_mask;
4196 svm->nested.vm_cr_msr |= (data & chg_mask);
4197
4198 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
4199
4200 /* check for svm_disable while efer.svme is set */
4201 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
4202 return 1;
4203
4204 return 0;
4205}
4206
Will Auld8fe8ab42012-11-29 12:42:12 -08004207static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004208{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004209 struct vcpu_svm *svm = to_svm(vcpu);
4210
Will Auld8fe8ab42012-11-29 12:42:12 -08004211 u32 ecx = msr->index;
4212 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004213 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02004214 case MSR_IA32_CR_PAT:
4215 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4216 return 1;
4217 vcpu->arch.pat = data;
4218 svm->vmcb->save.g_pat = data;
4219 mark_dirty(svm->vmcb, VMCB_NPT);
4220 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004221 case MSR_IA32_SPEC_CTRL:
4222 if (!msr->host_initiated &&
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -04004223 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
4224 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004225 return 1;
4226
4227 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -04004228 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004229 return 1;
4230
4231 svm->spec_ctrl = data;
4232
4233 if (!data)
4234 break;
4235
4236 /*
4237 * For non-nested:
4238 * When it's written (to non-zero) for the first time, pass
4239 * it through.
4240 *
4241 * For nested:
4242 * The handling of the MSR bitmap for L2 guests is done in
4243 * nested_svm_vmrun_msrpm.
4244 * We update the L1 MSR bit as well since it will end up
4245 * touching the MSR anyway now.
4246 */
4247 set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
4248 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004249 case MSR_IA32_PRED_CMD:
4250 if (!msr->host_initiated &&
Borislav Petkove7c587d2018-05-02 18:15:14 +02004251 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB))
Ashok Raj15d45072018-02-01 22:59:43 +01004252 return 1;
4253
4254 if (data & ~PRED_CMD_IBPB)
4255 return 1;
4256
4257 if (!data)
4258 break;
4259
4260 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4261 if (is_guest_mode(vcpu))
4262 break;
4263 set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
4264 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02004265 case MSR_AMD64_VIRT_SPEC_CTRL:
4266 if (!msr->host_initiated &&
4267 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
4268 return 1;
4269
4270 if (data & ~SPEC_CTRL_SSBD)
4271 return 1;
4272
4273 svm->virt_spec_ctrl = data;
4274 break;
Brian Gerst8c065852010-07-17 09:03:26 -04004275 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004276 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004277 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08004278#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004279 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004280 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004281 break;
4282 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004283 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004284 break;
4285 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004286 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004287 break;
4288 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004289 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004290 break;
4291#endif
4292 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004293 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004294 break;
4295 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02004296 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004297 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004298 break;
4299 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02004300 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004301 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004302 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004303 case MSR_TSC_AUX:
4304 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4305 return 1;
4306
4307 /*
4308 * This is rare, so we update the MSR here instead of using
4309 * direct_access_msrs. Doing that would require a rdmsr in
4310 * svm_vcpu_put.
4311 */
4312 svm->tsc_aux = data;
4313 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
4314 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01004315 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004316 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03004317 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
4318 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004319 break;
4320 }
4321 if (data & DEBUGCTL_RESERVED_BITS)
4322 return 1;
4323
4324 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01004325 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004326 if (data & (1ULL<<0))
4327 svm_enable_lbrv(svm);
4328 else
4329 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01004330 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004331 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02004332 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004333 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004334 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01004335 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004336 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03004337 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004338 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004339 case MSR_F10H_DECFG: {
4340 struct kvm_msr_entry msr_entry;
4341
4342 msr_entry.index = msr->index;
4343 if (svm_get_msr_feature(&msr_entry))
4344 return 1;
4345
4346 /* Check the supported bits */
4347 if (data & ~msr_entry.data)
4348 return 1;
4349
4350 /* Don't allow the guest to change a bit, #GP */
4351 if (!msr->host_initiated && (data ^ msr_entry.data))
4352 return 1;
4353
4354 svm->msr_decfg = data;
4355 break;
4356 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004357 case MSR_IA32_APICBASE:
4358 if (kvm_vcpu_apicv_active(vcpu))
4359 avic_update_vapic_bar(to_svm(vcpu), data);
4360 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004361 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08004362 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004363 }
4364 return 0;
4365}
4366
Avi Kivity851ba692009-08-24 11:10:17 +03004367static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004368{
Will Auld8fe8ab42012-11-29 12:42:12 -08004369 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06004370 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
4371 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004372
Will Auld8fe8ab42012-11-29 12:42:12 -08004373 msr.data = data;
4374 msr.index = ecx;
4375 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004376
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004377 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03004378 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02004379 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004380 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004381 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004382 } else {
4383 trace_kvm_msr_write(ecx, data);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004384 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02004385 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004386}
4387
Avi Kivity851ba692009-08-24 11:10:17 +03004388static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004389{
Rusty Russelle756fc62007-07-30 20:07:08 +10004390 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03004391 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392 else
Avi Kivity851ba692009-08-24 11:10:17 +03004393 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004394}
4395
Avi Kivity851ba692009-08-24 11:10:17 +03004396static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08004397{
Avi Kivity3842d132010-07-27 12:30:24 +03004398 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01004399 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03004400 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004401 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08004402 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08004403 return 1;
4404}
4405
Mark Langsdorf565d0992009-10-06 14:25:02 -05004406static int pause_interception(struct vcpu_svm *svm)
4407{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004408 struct kvm_vcpu *vcpu = &svm->vcpu;
4409 bool in_kernel = (svm_get_cpl(vcpu) == 0);
4410
Babu Moger8566ac82018-03-16 16:37:26 -04004411 if (pause_filter_thresh)
4412 grow_ple_window(vcpu);
4413
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004414 kvm_vcpu_on_spin(vcpu, in_kernel);
Mark Langsdorf565d0992009-10-06 14:25:02 -05004415 return 1;
4416}
4417
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004418static int nop_interception(struct vcpu_svm *svm)
4419{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004420 return kvm_skip_emulated_instruction(&(svm->vcpu));
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004421}
4422
4423static int monitor_interception(struct vcpu_svm *svm)
4424{
4425 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
4426 return nop_interception(svm);
4427}
4428
4429static int mwait_interception(struct vcpu_svm *svm)
4430{
4431 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4432 return nop_interception(svm);
4433}
4434
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004435enum avic_ipi_failure_cause {
4436 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
4437 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
4438 AVIC_IPI_FAILURE_INVALID_TARGET,
4439 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
4440};
4441
4442static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
4443{
4444 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
4445 u32 icrl = svm->vmcb->control.exit_info_1;
4446 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03004447 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004448 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4449
4450 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
4451
4452 switch (id) {
4453 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
4454 /*
4455 * AVIC hardware handles the generation of
4456 * IPIs when the specified Message Type is Fixed
4457 * (also known as fixed delivery mode) and
4458 * the Trigger Mode is edge-triggered. The hardware
4459 * also supports self and broadcast delivery modes
4460 * specified via the Destination Shorthand(DSH)
4461 * field of the ICRL. Logical and physical APIC ID
4462 * formats are supported. All other IPI types cause
4463 * a #VMEXIT, which needs to emulated.
4464 */
4465 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
4466 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
4467 break;
4468 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
4469 int i;
4470 struct kvm_vcpu *vcpu;
4471 struct kvm *kvm = svm->vcpu.kvm;
4472 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4473
4474 /*
4475 * At this point, we expect that the AVIC HW has already
4476 * set the appropriate IRR bits on the valid target
4477 * vcpus. So, we just need to kick the appropriate vcpu.
4478 */
4479 kvm_for_each_vcpu(i, vcpu, kvm) {
4480 bool m = kvm_apic_match_dest(vcpu, apic,
4481 icrl & KVM_APIC_SHORT_MASK,
4482 GET_APIC_DEST_FIELD(icrh),
4483 icrl & KVM_APIC_DEST_MASK);
4484
4485 if (m && !avic_vcpu_is_running(vcpu))
4486 kvm_vcpu_wake_up(vcpu);
4487 }
4488 break;
4489 }
4490 case AVIC_IPI_FAILURE_INVALID_TARGET:
4491 break;
4492 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
4493 WARN_ONCE(1, "Invalid backing page\n");
4494 break;
4495 default:
4496 pr_err("Unknown IPI interception\n");
4497 }
4498
4499 return 1;
4500}
4501
4502static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
4503{
Sean Christopherson81811c12018-03-20 12:17:21 -07004504 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004505 int index;
4506 u32 *logical_apic_id_table;
4507 int dlid = GET_APIC_LOGICAL_ID(ldr);
4508
4509 if (!dlid)
4510 return NULL;
4511
4512 if (flat) { /* flat */
4513 index = ffs(dlid) - 1;
4514 if (index > 7)
4515 return NULL;
4516 } else { /* cluster */
4517 int cluster = (dlid & 0xf0) >> 4;
4518 int apic = ffs(dlid & 0x0f) - 1;
4519
4520 if ((apic < 0) || (apic > 7) ||
4521 (cluster >= 0xf))
4522 return NULL;
4523 index = (cluster << 2) + apic;
4524 }
4525
Sean Christopherson81811c12018-03-20 12:17:21 -07004526 logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004527
4528 return &logical_apic_id_table[index];
4529}
4530
4531static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
4532 bool valid)
4533{
4534 bool flat;
4535 u32 *entry, new_entry;
4536
4537 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4538 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4539 if (!entry)
4540 return -EINVAL;
4541
4542 new_entry = READ_ONCE(*entry);
4543 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4544 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
4545 if (valid)
4546 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4547 else
4548 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4549 WRITE_ONCE(*entry, new_entry);
4550
4551 return 0;
4552}
4553
4554static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4555{
4556 int ret;
4557 struct vcpu_svm *svm = to_svm(vcpu);
4558 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
4559
4560 if (!ldr)
4561 return 1;
4562
4563 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
4564 if (ret && svm->ldr_reg) {
4565 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
4566 svm->ldr_reg = 0;
4567 } else {
4568 svm->ldr_reg = ldr;
4569 }
4570 return ret;
4571}
4572
4573static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4574{
4575 u64 *old, *new;
4576 struct vcpu_svm *svm = to_svm(vcpu);
4577 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
4578 u32 id = (apic_id_reg >> 24) & 0xff;
4579
4580 if (vcpu->vcpu_id == id)
4581 return 0;
4582
4583 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4584 new = avic_get_physical_id_entry(vcpu, id);
4585 if (!new || !old)
4586 return 1;
4587
4588 /* We need to move physical_id_entry to new offset */
4589 *new = *old;
4590 *old = 0ULL;
4591 to_svm(vcpu)->avic_physical_id_cache = new;
4592
4593 /*
4594 * Also update the guest physical APIC ID in the logical
4595 * APIC ID table entry if already setup the LDR.
4596 */
4597 if (svm->ldr_reg)
4598 avic_handle_ldr_update(vcpu);
4599
4600 return 0;
4601}
4602
4603static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
4604{
4605 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christopherson81811c12018-03-20 12:17:21 -07004606 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004607 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
4608 u32 mod = (dfr >> 28) & 0xf;
4609
4610 /*
4611 * We assume that all local APICs are using the same type.
4612 * If this changes, we need to flush the AVIC logical
4613 * APID id table.
4614 */
Sean Christopherson81811c12018-03-20 12:17:21 -07004615 if (kvm_svm->ldr_mode == mod)
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004616 return 0;
4617
Sean Christopherson81811c12018-03-20 12:17:21 -07004618 clear_page(page_address(kvm_svm->avic_logical_id_table_page));
4619 kvm_svm->ldr_mode = mod;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004620
4621 if (svm->ldr_reg)
4622 avic_handle_ldr_update(vcpu);
4623 return 0;
4624}
4625
4626static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4627{
4628 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4629 u32 offset = svm->vmcb->control.exit_info_1 &
4630 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4631
4632 switch (offset) {
4633 case APIC_ID:
4634 if (avic_handle_apic_id_update(&svm->vcpu))
4635 return 0;
4636 break;
4637 case APIC_LDR:
4638 if (avic_handle_ldr_update(&svm->vcpu))
4639 return 0;
4640 break;
4641 case APIC_DFR:
4642 avic_handle_dfr_update(&svm->vcpu);
4643 break;
4644 default:
4645 break;
4646 }
4647
4648 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4649
4650 return 1;
4651}
4652
4653static bool is_avic_unaccelerated_access_trap(u32 offset)
4654{
4655 bool ret = false;
4656
4657 switch (offset) {
4658 case APIC_ID:
4659 case APIC_EOI:
4660 case APIC_RRR:
4661 case APIC_LDR:
4662 case APIC_DFR:
4663 case APIC_SPIV:
4664 case APIC_ESR:
4665 case APIC_ICR:
4666 case APIC_LVTT:
4667 case APIC_LVTTHMR:
4668 case APIC_LVTPC:
4669 case APIC_LVT0:
4670 case APIC_LVT1:
4671 case APIC_LVTERR:
4672 case APIC_TMICT:
4673 case APIC_TDCR:
4674 ret = true;
4675 break;
4676 default:
4677 break;
4678 }
4679 return ret;
4680}
4681
4682static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4683{
4684 int ret = 0;
4685 u32 offset = svm->vmcb->control.exit_info_1 &
4686 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4687 u32 vector = svm->vmcb->control.exit_info_2 &
4688 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4689 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4690 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4691 bool trap = is_avic_unaccelerated_access_trap(offset);
4692
4693 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4694 trap, write, vector);
4695 if (trap) {
4696 /* Handling Trap */
4697 WARN_ONCE(!write, "svm: Handling trap read.\n");
4698 ret = avic_unaccel_trap_write(svm);
4699 } else {
4700 /* Handling Fault */
Sean Christopherson0ce97a22018-08-23 13:56:52 -07004701 ret = (kvm_emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004702 }
4703
4704 return ret;
4705}
4706
Mathias Krause09941fb2012-08-30 01:30:20 +02004707static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01004708 [SVM_EXIT_READ_CR0] = cr_interception,
4709 [SVM_EXIT_READ_CR3] = cr_interception,
4710 [SVM_EXIT_READ_CR4] = cr_interception,
4711 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06004712 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02004713 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01004714 [SVM_EXIT_WRITE_CR3] = cr_interception,
4715 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004716 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01004717 [SVM_EXIT_READ_DR0] = dr_interception,
4718 [SVM_EXIT_READ_DR1] = dr_interception,
4719 [SVM_EXIT_READ_DR2] = dr_interception,
4720 [SVM_EXIT_READ_DR3] = dr_interception,
4721 [SVM_EXIT_READ_DR4] = dr_interception,
4722 [SVM_EXIT_READ_DR5] = dr_interception,
4723 [SVM_EXIT_READ_DR6] = dr_interception,
4724 [SVM_EXIT_READ_DR7] = dr_interception,
4725 [SVM_EXIT_WRITE_DR0] = dr_interception,
4726 [SVM_EXIT_WRITE_DR1] = dr_interception,
4727 [SVM_EXIT_WRITE_DR2] = dr_interception,
4728 [SVM_EXIT_WRITE_DR3] = dr_interception,
4729 [SVM_EXIT_WRITE_DR4] = dr_interception,
4730 [SVM_EXIT_WRITE_DR5] = dr_interception,
4731 [SVM_EXIT_WRITE_DR6] = dr_interception,
4732 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004733 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4734 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004735 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004736 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004737 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01004738 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Liran Alon97184202018-03-12 13:12:52 +02004739 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004740 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02004741 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742 [SVM_EXIT_SMI] = nop_on_interception,
4743 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08004744 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02004745 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004747 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02004748 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05004749 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004750 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004751 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02004752 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004753 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004754 [SVM_EXIT_MSR] = msr_interception,
4755 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08004756 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01004757 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02004758 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01004759 [SVM_EXIT_VMLOAD] = vmload_interception,
4760 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01004761 [SVM_EXIT_STGI] = stgi_interception,
4762 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02004763 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06004764 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004765 [SVM_EXIT_MONITOR] = monitor_interception,
4766 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01004767 [SVM_EXIT_XSETBV] = xsetbv_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02004768 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06004769 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004770 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4771 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004772};
4773
Joe Perchesae8cc052011-04-24 22:00:50 -07004774static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02004775{
4776 struct vcpu_svm *svm = to_svm(vcpu);
4777 struct vmcb_control_area *control = &svm->vmcb->control;
4778 struct vmcb_save_area *save = &svm->vmcb->save;
4779
4780 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004781 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4782 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4783 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4784 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4785 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4786 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4787 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
Babu Moger1d8fb442018-03-16 16:37:25 -04004788 pr_err("%-20s%d\n", "pause filter threshold:",
4789 control->pause_filter_thresh);
Joe Perchesae8cc052011-04-24 22:00:50 -07004790 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4791 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4792 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4793 pr_err("%-20s%d\n", "asid:", control->asid);
4794 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4795 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4796 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4797 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4798 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4799 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4800 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4801 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4802 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4803 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4804 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004805 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07004806 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4807 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05004808 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07004809 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004810 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4811 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4812 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004813 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004814 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4815 "es:",
4816 save->es.selector, save->es.attrib,
4817 save->es.limit, save->es.base);
4818 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4819 "cs:",
4820 save->cs.selector, save->cs.attrib,
4821 save->cs.limit, save->cs.base);
4822 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4823 "ss:",
4824 save->ss.selector, save->ss.attrib,
4825 save->ss.limit, save->ss.base);
4826 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4827 "ds:",
4828 save->ds.selector, save->ds.attrib,
4829 save->ds.limit, save->ds.base);
4830 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4831 "fs:",
4832 save->fs.selector, save->fs.attrib,
4833 save->fs.limit, save->fs.base);
4834 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4835 "gs:",
4836 save->gs.selector, save->gs.attrib,
4837 save->gs.limit, save->gs.base);
4838 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4839 "gdtr:",
4840 save->gdtr.selector, save->gdtr.attrib,
4841 save->gdtr.limit, save->gdtr.base);
4842 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4843 "ldtr:",
4844 save->ldtr.selector, save->ldtr.attrib,
4845 save->ldtr.limit, save->ldtr.base);
4846 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4847 "idtr:",
4848 save->idtr.selector, save->idtr.attrib,
4849 save->idtr.limit, save->idtr.base);
4850 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4851 "tr:",
4852 save->tr.selector, save->tr.attrib,
4853 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004854 pr_err("cpl: %d efer: %016llx\n",
4855 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004856 pr_err("%-15s %016llx %-13s %016llx\n",
4857 "cr0:", save->cr0, "cr2:", save->cr2);
4858 pr_err("%-15s %016llx %-13s %016llx\n",
4859 "cr3:", save->cr3, "cr4:", save->cr4);
4860 pr_err("%-15s %016llx %-13s %016llx\n",
4861 "dr6:", save->dr6, "dr7:", save->dr7);
4862 pr_err("%-15s %016llx %-13s %016llx\n",
4863 "rip:", save->rip, "rflags:", save->rflags);
4864 pr_err("%-15s %016llx %-13s %016llx\n",
4865 "rsp:", save->rsp, "rax:", save->rax);
4866 pr_err("%-15s %016llx %-13s %016llx\n",
4867 "star:", save->star, "lstar:", save->lstar);
4868 pr_err("%-15s %016llx %-13s %016llx\n",
4869 "cstar:", save->cstar, "sfmask:", save->sfmask);
4870 pr_err("%-15s %016llx %-13s %016llx\n",
4871 "kernel_gs_base:", save->kernel_gs_base,
4872 "sysenter_cs:", save->sysenter_cs);
4873 pr_err("%-15s %016llx %-13s %016llx\n",
4874 "sysenter_esp:", save->sysenter_esp,
4875 "sysenter_eip:", save->sysenter_eip);
4876 pr_err("%-15s %016llx %-13s %016llx\n",
4877 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4878 pr_err("%-15s %016llx %-13s %016llx\n",
4879 "br_from:", save->br_from, "br_to:", save->br_to);
4880 pr_err("%-15s %016llx %-13s %016llx\n",
4881 "excp_from:", save->last_excp_from,
4882 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004883}
4884
Avi Kivity586f9602010-11-18 13:09:54 +02004885static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4886{
4887 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4888
4889 *info1 = control->exit_info_1;
4890 *info2 = control->exit_info_2;
4891}
4892
Avi Kivity851ba692009-08-24 11:10:17 +03004893static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004895 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004896 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004897 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004898
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004899 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4900
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004901 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004902 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4903 if (npt_enabled)
4904 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004905
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004906 if (unlikely(svm->nested.exit_required)) {
4907 nested_svm_vmexit(svm);
4908 svm->nested.exit_required = false;
4909
4910 return 1;
4911 }
4912
Joerg Roedel20307532010-11-29 17:51:48 +01004913 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004914 int vmexit;
4915
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004916 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4917 svm->vmcb->control.exit_info_1,
4918 svm->vmcb->control.exit_info_2,
4919 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004920 svm->vmcb->control.exit_int_info_err,
4921 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004922
Joerg Roedel410e4d52009-08-07 11:49:44 +02004923 vmexit = nested_svm_exit_special(svm);
4924
4925 if (vmexit == NESTED_EXIT_CONTINUE)
4926 vmexit = nested_svm_exit_handled(svm);
4927
4928 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004929 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004930 }
4931
Joerg Roedela5c38322009-08-07 11:49:32 +02004932 svm_complete_interrupts(svm);
4933
Avi Kivity04d2cc72007-09-10 18:10:54 +03004934 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4935 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4936 kvm_run->fail_entry.hardware_entry_failure_reason
4937 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004938 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4939 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004940 return 0;
4941 }
4942
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004943 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004944 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004945 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4946 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004947 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004948 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004949 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004950 exit_code);
4951
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004952 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004953 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04004954 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03004955 kvm_queue_exception(vcpu, UD_VECTOR);
4956 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004957 }
4958
Avi Kivity851ba692009-08-24 11:10:17 +03004959 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004960}
4961
4962static void reload_tss(struct kvm_vcpu *vcpu)
4963{
4964 int cpu = raw_smp_processor_id();
4965
Tejun Heo0fe1e002009-10-29 22:34:14 +09004966 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4967 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004968 load_TR_desc();
4969}
4970
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004971static void pre_sev_run(struct vcpu_svm *svm, int cpu)
4972{
4973 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4974 int asid = sev_get_asid(svm->vcpu.kvm);
4975
4976 /* Assign the asid allocated with this SEV guest */
4977 svm->vmcb->control.asid = asid;
4978
4979 /*
4980 * Flush guest TLB:
4981 *
4982 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
4983 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
4984 */
4985 if (sd->sev_vmcbs[asid] == svm->vmcb &&
4986 svm->last_cpu == cpu)
4987 return;
4988
4989 svm->last_cpu = cpu;
4990 sd->sev_vmcbs[asid] = svm->vmcb;
4991 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4992 mark_dirty(svm->vmcb, VMCB_ASID);
4993}
4994
Rusty Russelle756fc62007-07-30 20:07:08 +10004995static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996{
4997 int cpu = raw_smp_processor_id();
4998
Tejun Heo0fe1e002009-10-29 22:34:14 +09004999 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000
Brijesh Singh70cd94e2017-12-04 10:57:34 -06005001 if (sev_guest(svm->vcpu.kvm))
5002 return pre_sev_run(svm, cpu);
5003
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03005004 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09005005 if (svm->asid_generation != sd->asid_generation)
5006 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005007}
5008
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005009static void svm_inject_nmi(struct kvm_vcpu *vcpu)
5010{
5011 struct vcpu_svm *svm = to_svm(vcpu);
5012
5013 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
5014 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005015 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005016 ++vcpu->stat.nmi_injections;
5017}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005018
Eddie Dong85f455f2007-07-06 12:20:49 +03005019static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005020{
5021 struct vmcb_control_area *control;
5022
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005023 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10005024 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03005025 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005026 control->int_ctl &= ~V_INTR_PRIO_MASK;
5027 control->int_ctl |= V_IRQ_MASK |
5028 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01005029 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005030}
5031
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005032static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03005033{
5034 struct vcpu_svm *svm = to_svm(vcpu);
5035
Joerg Roedel2af91942009-08-07 11:49:28 +02005036 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01005037
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03005038 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
5039 ++vcpu->stat.irq_injections;
5040
Alexander Graf219b65d2009-06-15 15:21:25 +02005041 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
5042 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03005043}
5044
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005045static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
5046{
5047 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
5048}
5049
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005050static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
5051{
5052 struct vcpu_svm *svm = to_svm(vcpu);
5053
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005054 if (svm_nested_virtualize_tpr(vcpu) ||
5055 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005056 return;
5057
Radim Krčmář596f3142014-03-11 19:11:18 +01005058 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
5059
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005060 if (irr == -1)
5061 return;
5062
5063 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005064 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005065}
5066
Jim Mattson8d860bb2018-05-09 16:56:05 -04005067static void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08005068{
5069 return;
5070}
5071
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005072static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005073{
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05005074 return avic && irqchip_split(vcpu->kvm);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005075}
5076
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005077static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
5078{
5079}
5080
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005081static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005082{
5083}
5084
5085/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03005086static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5087{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005088 struct vcpu_svm *svm = to_svm(vcpu);
5089 struct vmcb *vmcb = svm->vmcb;
5090
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05005091 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005092 return;
5093
5094 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
5095 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005096}
5097
Andrey Smetanin63086302015-11-10 15:36:32 +03005098static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005099{
5100 return;
5101}
5102
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005103static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
5104{
5105 kvm_lapic_set_irr(vec, vcpu->arch.apic);
5106 smp_mb__after_atomic();
5107
5108 if (avic_vcpu_is_running(vcpu))
5109 wrmsrl(SVM_AVIC_DOORBELL,
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05005110 kvm_cpu_get_apicid(vcpu->cpu));
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005111 else
5112 kvm_vcpu_wake_up(vcpu);
5113}
5114
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005115static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
5116{
5117 unsigned long flags;
5118 struct amd_svm_iommu_ir *cur;
5119
5120 spin_lock_irqsave(&svm->ir_list_lock, flags);
5121 list_for_each_entry(cur, &svm->ir_list, node) {
5122 if (cur->data != pi->ir_data)
5123 continue;
5124 list_del(&cur->node);
5125 kfree(cur);
5126 break;
5127 }
5128 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
5129}
5130
5131static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
5132{
5133 int ret = 0;
5134 unsigned long flags;
5135 struct amd_svm_iommu_ir *ir;
5136
5137 /**
5138 * In some cases, the existing irte is updaed and re-set,
5139 * so we need to check here if it's already been * added
5140 * to the ir_list.
5141 */
5142 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
5143 struct kvm *kvm = svm->vcpu.kvm;
5144 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
5145 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
5146 struct vcpu_svm *prev_svm;
5147
5148 if (!prev_vcpu) {
5149 ret = -EINVAL;
5150 goto out;
5151 }
5152
5153 prev_svm = to_svm(prev_vcpu);
5154 svm_ir_list_del(prev_svm, pi);
5155 }
5156
5157 /**
5158 * Allocating new amd_iommu_pi_data, which will get
5159 * add to the per-vcpu ir_list.
5160 */
5161 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
5162 if (!ir) {
5163 ret = -ENOMEM;
5164 goto out;
5165 }
5166 ir->data = pi->ir_data;
5167
5168 spin_lock_irqsave(&svm->ir_list_lock, flags);
5169 list_add(&ir->node, &svm->ir_list);
5170 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
5171out:
5172 return ret;
5173}
5174
5175/**
5176 * Note:
5177 * The HW cannot support posting multicast/broadcast
5178 * interrupts to a vCPU. So, we still use legacy interrupt
5179 * remapping for these kind of interrupts.
5180 *
5181 * For lowest-priority interrupts, we only support
5182 * those with single CPU as the destination, e.g. user
5183 * configures the interrupts via /proc/irq or uses
5184 * irqbalance to make the interrupts single-CPU.
5185 */
5186static int
5187get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
5188 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
5189{
5190 struct kvm_lapic_irq irq;
5191 struct kvm_vcpu *vcpu = NULL;
5192
5193 kvm_set_msi_irq(kvm, e, &irq);
5194
5195 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
5196 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
5197 __func__, irq.vector);
5198 return -1;
5199 }
5200
5201 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
5202 irq.vector);
5203 *svm = to_svm(vcpu);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005204 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005205 vcpu_info->vector = irq.vector;
5206
5207 return 0;
5208}
5209
5210/*
5211 * svm_update_pi_irte - set IRTE for Posted-Interrupts
5212 *
5213 * @kvm: kvm
5214 * @host_irq: host irq of the interrupt
5215 * @guest_irq: gsi of the interrupt
5216 * @set: set or unset PI
5217 * returns 0 on success, < 0 on failure
5218 */
5219static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
5220 uint32_t guest_irq, bool set)
5221{
5222 struct kvm_kernel_irq_routing_entry *e;
5223 struct kvm_irq_routing_table *irq_rt;
5224 int idx, ret = -EINVAL;
5225
5226 if (!kvm_arch_has_assigned_device(kvm) ||
5227 !irq_remapping_cap(IRQ_POSTING_CAP))
5228 return 0;
5229
5230 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
5231 __func__, host_irq, guest_irq, set);
5232
5233 idx = srcu_read_lock(&kvm->irq_srcu);
5234 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
5235 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
5236
5237 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
5238 struct vcpu_data vcpu_info;
5239 struct vcpu_svm *svm = NULL;
5240
5241 if (e->type != KVM_IRQ_ROUTING_MSI)
5242 continue;
5243
5244 /**
5245 * Here, we setup with legacy mode in the following cases:
5246 * 1. When cannot target interrupt to a specific vcpu.
5247 * 2. Unsetting posted interrupt.
5248 * 3. APIC virtialization is disabled for the vcpu.
5249 */
5250 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
5251 kvm_vcpu_apicv_active(&svm->vcpu)) {
5252 struct amd_iommu_pi_data pi;
5253
5254 /* Try to enable guest_mode in IRTE */
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005255 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
5256 AVIC_HPA_MASK);
Sean Christopherson81811c12018-03-20 12:17:21 -07005257 pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005258 svm->vcpu.vcpu_id);
5259 pi.is_guest_mode = true;
5260 pi.vcpu_data = &vcpu_info;
5261 ret = irq_set_vcpu_affinity(host_irq, &pi);
5262
5263 /**
5264 * Here, we successfully setting up vcpu affinity in
5265 * IOMMU guest mode. Now, we need to store the posted
5266 * interrupt information in a per-vcpu ir_list so that
5267 * we can reference to them directly when we update vcpu
5268 * scheduling information in IOMMU irte.
5269 */
5270 if (!ret && pi.is_guest_mode)
5271 svm_ir_list_add(svm, &pi);
5272 } else {
5273 /* Use legacy mode in IRTE */
5274 struct amd_iommu_pi_data pi;
5275
5276 /**
5277 * Here, pi is used to:
5278 * - Tell IOMMU to use legacy mode for this interrupt.
5279 * - Retrieve ga_tag of prior interrupt remapping data.
5280 */
5281 pi.is_guest_mode = false;
5282 ret = irq_set_vcpu_affinity(host_irq, &pi);
5283
5284 /**
5285 * Check if the posted interrupt was previously
5286 * setup with the guest_mode by checking if the ga_tag
5287 * was cached. If so, we need to clean up the per-vcpu
5288 * ir_list.
5289 */
5290 if (!ret && pi.prev_ga_tag) {
5291 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
5292 struct kvm_vcpu *vcpu;
5293
5294 vcpu = kvm_get_vcpu_by_id(kvm, id);
5295 if (vcpu)
5296 svm_ir_list_del(to_svm(vcpu), &pi);
5297 }
5298 }
5299
5300 if (!ret && svm) {
hu huajun2698d822018-04-11 15:16:40 +08005301 trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id,
5302 e->gsi, vcpu_info.vector,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005303 vcpu_info.pi_desc_addr, set);
5304 }
5305
5306 if (ret < 0) {
5307 pr_err("%s: failed to update PI IRTE\n", __func__);
5308 goto out;
5309 }
5310 }
5311
5312 ret = 0;
5313out:
5314 srcu_read_unlock(&kvm->irq_srcu, idx);
5315 return ret;
5316}
5317
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005318static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005319{
5320 struct vcpu_svm *svm = to_svm(vcpu);
5321 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02005322 int ret;
5323 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
5324 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
5325 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
5326
5327 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005328}
5329
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005330static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
5331{
5332 struct vcpu_svm *svm = to_svm(vcpu);
5333
5334 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
5335}
5336
5337static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5338{
5339 struct vcpu_svm *svm = to_svm(vcpu);
5340
5341 if (masked) {
5342 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005343 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005344 } else {
5345 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005346 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005347 }
5348}
5349
Gleb Natapov78646122009-03-23 12:12:11 +02005350static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
5351{
5352 struct vcpu_svm *svm = to_svm(vcpu);
5353 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005354 int ret;
5355
5356 if (!gif_set(svm) ||
5357 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
5358 return 0;
5359
Avi Kivityf6e78472010-08-02 15:30:20 +03005360 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005361
Joerg Roedel20307532010-11-29 17:51:48 +01005362 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005363 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
5364
5365 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02005366}
5367
Jan Kiszkac9a79532014-03-07 20:03:15 +01005368static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03005369{
Alexander Graf219b65d2009-06-15 15:21:25 +02005370 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02005371
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005372 if (kvm_vcpu_apicv_active(vcpu))
5373 return;
5374
Joerg Roedele0231712010-02-24 18:59:10 +01005375 /*
5376 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
5377 * 1, because that's a separate STGI/VMRUN intercept. The next time we
5378 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005379 * we'll get the vintr intercept. However, if the vGIF feature is
5380 * enabled, the STGI interception will not occur. Enable the irq
5381 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01005382 */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005383 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02005384 svm_set_vintr(svm);
5385 svm_inject_irq(svm, 0x0);
5386 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005387}
5388
Jan Kiszkac9a79532014-03-07 20:03:15 +01005389static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005390{
Avi Kivity04d2cc72007-09-10 18:10:54 +03005391 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005392
Gleb Natapov44c11432009-05-11 13:35:52 +03005393 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
5394 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01005395 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03005396
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005397 if (!gif_set(svm)) {
5398 if (vgif_enabled(svm))
5399 set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005400 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005401 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005402
5403 if (svm->nested.exit_required)
5404 return; /* we're not going to run the guest yet */
5405
Joerg Roedele0231712010-02-24 18:59:10 +01005406 /*
5407 * Something prevents NMI from been injected. Single step over possible
5408 * problem (IRET or exception injection or interrupt shadow)
5409 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02005410 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02005411 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03005412 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03005413}
5414
Izik Eiduscbc94022007-10-25 00:29:55 +02005415static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
5416{
5417 return 0;
5418}
5419
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07005420static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
5421{
5422 return 0;
5423}
5424
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005425static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Avi Kivityd9e368d2007-06-07 19:18:30 +03005426{
Joerg Roedel38e5e922010-12-03 15:25:16 +01005427 struct vcpu_svm *svm = to_svm(vcpu);
5428
5429 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
5430 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5431 else
5432 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03005433}
5434
Junaid Shahidfaff8752018-06-29 13:10:05 -07005435static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
5436{
5437 struct vcpu_svm *svm = to_svm(vcpu);
5438
5439 invlpga(gva, svm->vmcb->control.asid);
5440}
5441
Avi Kivity04d2cc72007-09-10 18:10:54 +03005442static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
5443{
5444}
5445
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005446static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
5447{
5448 struct vcpu_svm *svm = to_svm(vcpu);
5449
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005450 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005451 return;
5452
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005453 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005454 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03005455 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005456 }
5457}
5458
Joerg Roedel649d6862008-04-16 16:51:15 +02005459static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
5460{
5461 struct vcpu_svm *svm = to_svm(vcpu);
5462 u64 cr8;
5463
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005464 if (svm_nested_virtualize_tpr(vcpu) ||
5465 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005466 return;
5467
Joerg Roedel649d6862008-04-16 16:51:15 +02005468 cr8 = kvm_get_cr8(vcpu);
5469 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
5470 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
5471}
5472
Gleb Natapov9222be12009-04-23 17:14:37 +03005473static void svm_complete_interrupts(struct vcpu_svm *svm)
5474{
5475 u8 vector;
5476 int type;
5477 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01005478 unsigned int3_injected = svm->int3_injected;
5479
5480 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005481
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02005482 /*
5483 * If we've made progress since setting HF_IRET_MASK, we've
5484 * executed an IRET and can allow NMI injection.
5485 */
5486 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
5487 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03005488 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03005489 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5490 }
Gleb Natapov44c11432009-05-11 13:35:52 +03005491
Gleb Natapov9222be12009-04-23 17:14:37 +03005492 svm->vcpu.arch.nmi_injected = false;
5493 kvm_clear_exception_queue(&svm->vcpu);
5494 kvm_clear_interrupt_queue(&svm->vcpu);
5495
5496 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
5497 return;
5498
Avi Kivity3842d132010-07-27 12:30:24 +03005499 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5500
Gleb Natapov9222be12009-04-23 17:14:37 +03005501 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
5502 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
5503
5504 switch (type) {
5505 case SVM_EXITINTINFO_TYPE_NMI:
5506 svm->vcpu.arch.nmi_injected = true;
5507 break;
5508 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01005509 /*
5510 * In case of software exceptions, do not reinject the vector,
5511 * but re-execute the instruction instead. Rewind RIP first
5512 * if we emulated INT3 before.
5513 */
5514 if (kvm_exception_is_soft(vector)) {
5515 if (vector == BP_VECTOR && int3_injected &&
5516 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
5517 kvm_rip_write(&svm->vcpu,
5518 kvm_rip_read(&svm->vcpu) -
5519 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02005520 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01005521 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005522 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
5523 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005524 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03005525
5526 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005527 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03005528 break;
5529 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005530 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03005531 break;
5532 default:
5533 break;
5534 }
5535}
5536
Avi Kivityb463a6f2010-07-20 15:06:17 +03005537static void svm_cancel_injection(struct kvm_vcpu *vcpu)
5538{
5539 struct vcpu_svm *svm = to_svm(vcpu);
5540 struct vmcb_control_area *control = &svm->vmcb->control;
5541
5542 control->exit_int_info = control->event_inj;
5543 control->exit_int_info_err = control->event_inj_err;
5544 control->event_inj = 0;
5545 svm_complete_interrupts(svm);
5546}
5547
Avi Kivity851ba692009-08-24 11:10:17 +03005548static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005549{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005550 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03005551
Joerg Roedel2041a062010-04-22 12:33:08 +02005552 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5553 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5554 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5555
Joerg Roedelcd3ff652009-10-09 16:08:26 +02005556 /*
5557 * A vmexit emulation is required before the vcpu can be executed
5558 * again.
5559 */
5560 if (unlikely(svm->nested.exit_required))
5561 return;
5562
Ladi Proseka12713c2017-06-21 09:07:00 +02005563 /*
5564 * Disable singlestep if we're injecting an interrupt/exception.
5565 * We don't want our modified rflags to be pushed on the stack where
5566 * we might not be able to easily reset them if we disabled NMI
5567 * singlestep later.
5568 */
5569 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5570 /*
5571 * Event injection happens before external interrupts cause a
5572 * vmexit and interrupts are disabled here, so smp_send_reschedule
5573 * is enough to force an immediate vmexit.
5574 */
5575 disable_nmi_singlestep(svm);
5576 smp_send_reschedule(vcpu->cpu);
5577 }
5578
Rusty Russelle756fc62007-07-30 20:07:08 +10005579 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005580
Joerg Roedel649d6862008-04-16 16:51:15 +02005581 sync_lapic_to_cr8(vcpu);
5582
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02005583 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005584
Avi Kivity04d2cc72007-09-10 18:10:54 +03005585 clgi();
5586
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005587 /*
5588 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
5589 * it's non-zero. Since vmentry is serialising on affected CPUs, there
5590 * is no need to worry about the conditional branch over the wrmsr
5591 * being speculatively taken.
5592 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02005593 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005594
Thomas Gleixner024d83c2018-08-12 20:41:45 +02005595 local_irq_enable();
5596
Avi Kivity6aa8b732006-12-10 02:21:36 -08005597 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03005598 "push %%" _ASM_BP "; \n\t"
5599 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5600 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5601 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5602 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5603 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5604 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005605#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005606 "mov %c[r8](%[svm]), %%r8 \n\t"
5607 "mov %c[r9](%[svm]), %%r9 \n\t"
5608 "mov %c[r10](%[svm]), %%r10 \n\t"
5609 "mov %c[r11](%[svm]), %%r11 \n\t"
5610 "mov %c[r12](%[svm]), %%r12 \n\t"
5611 "mov %c[r13](%[svm]), %%r13 \n\t"
5612 "mov %c[r14](%[svm]), %%r14 \n\t"
5613 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005614#endif
5615
Avi Kivity6aa8b732006-12-10 02:21:36 -08005616 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03005617 "push %%" _ASM_AX " \n\t"
5618 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03005619 __ex(SVM_VMLOAD) "\n\t"
5620 __ex(SVM_VMRUN) "\n\t"
5621 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005622 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005623
5624 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03005625 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5626 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5627 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5628 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5629 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5630 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005631#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005632 "mov %%r8, %c[r8](%[svm]) \n\t"
5633 "mov %%r9, %c[r9](%[svm]) \n\t"
5634 "mov %%r10, %c[r10](%[svm]) \n\t"
5635 "mov %%r11, %c[r11](%[svm]) \n\t"
5636 "mov %%r12, %c[r12](%[svm]) \n\t"
5637 "mov %%r13, %c[r13](%[svm]) \n\t"
5638 "mov %%r14, %c[r14](%[svm]) \n\t"
5639 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005640#endif
Jim Mattson0cb5b302018-01-03 14:31:38 -08005641 /*
5642 * Clear host registers marked as clobbered to prevent
5643 * speculative use.
5644 */
5645 "xor %%" _ASM_BX ", %%" _ASM_BX " \n\t"
5646 "xor %%" _ASM_CX ", %%" _ASM_CX " \n\t"
5647 "xor %%" _ASM_DX ", %%" _ASM_DX " \n\t"
5648 "xor %%" _ASM_SI ", %%" _ASM_SI " \n\t"
5649 "xor %%" _ASM_DI ", %%" _ASM_DI " \n\t"
5650#ifdef CONFIG_X86_64
5651 "xor %%r8, %%r8 \n\t"
5652 "xor %%r9, %%r9 \n\t"
5653 "xor %%r10, %%r10 \n\t"
5654 "xor %%r11, %%r11 \n\t"
5655 "xor %%r12, %%r12 \n\t"
5656 "xor %%r13, %%r13 \n\t"
5657 "xor %%r14, %%r14 \n\t"
5658 "xor %%r15, %%r15 \n\t"
5659#endif
Avi Kivity74547662012-09-16 15:10:59 +03005660 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08005661 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005662 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08005663 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005664 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5665 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5666 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5667 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5668 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5669 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005670#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005671 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5672 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5673 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5674 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5675 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5676 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5677 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5678 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005679#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02005680 : "cc", "memory"
5681#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03005682 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005683 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03005684#else
5685 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005686#endif
5687 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08005688
Thomas Gleixner15e6c222018-05-11 15:21:01 +02005689 /* Eliminate branch target predictions from guest mode */
5690 vmexit_fill_RSB();
5691
5692#ifdef CONFIG_X86_64
5693 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5694#else
5695 loadsegment(fs, svm->host.fs);
5696#ifndef CONFIG_X86_32_LAZY_GS
5697 loadsegment(gs, svm->host.gs);
5698#endif
5699#endif
5700
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005701 /*
5702 * We do not use IBRS in the kernel. If this vCPU has used the
5703 * SPEC_CTRL MSR it may have left it on; save the value and
5704 * turn it off. This is much more efficient than blindly adding
5705 * it to the atomic save/restore list. Especially as the former
5706 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
5707 *
5708 * For non-nested case:
5709 * If the L01 MSR bitmap does not intercept the MSR, then we need to
5710 * save it.
5711 *
5712 * For nested case:
5713 * If the L02 MSR bitmap does not intercept the MSR, then we need to
5714 * save it.
5715 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01005716 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005717 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005718
Avi Kivity6aa8b732006-12-10 02:21:36 -08005719 reload_tss(vcpu);
5720
Avi Kivity56ba47d2007-11-07 17:14:18 +02005721 local_irq_disable();
5722
Thomas Gleixner024d83c2018-08-12 20:41:45 +02005723 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
5724
Avi Kivity13c34e02010-10-21 12:20:31 +02005725 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5726 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5727 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5728 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5729
Joerg Roedel3781c012011-01-14 16:45:02 +01005730 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Andi Kleendd60d212017-07-25 17:20:32 -07005731 kvm_before_interrupt(&svm->vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01005732
5733 stgi();
5734
5735 /* Any pending NMI will happen here */
5736
5737 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Andi Kleendd60d212017-07-25 17:20:32 -07005738 kvm_after_interrupt(&svm->vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01005739
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005740 sync_cr8_to_lapic(vcpu);
5741
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005742 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005743
Joerg Roedel38e5e922010-12-03 15:25:16 +01005744 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5745
Gleb Natapov631bc482010-10-14 11:22:52 +02005746 /* if exit due to PF check for async PF */
5747 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005748 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
Gleb Natapov631bc482010-10-14 11:22:52 +02005749
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005750 if (npt_enabled) {
5751 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5752 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5753 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02005754
5755 /*
5756 * We need to handle MC intercepts here before the vcpu has a chance to
5757 * change the physical cpu
5758 */
5759 if (unlikely(svm->vmcb->control.exit_code ==
5760 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5761 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01005762
5763 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005764}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005765STACK_FRAME_NON_STANDARD(svm_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005766
Avi Kivity6aa8b732006-12-10 02:21:36 -08005767static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5768{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005769 struct vcpu_svm *svm = to_svm(vcpu);
5770
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005771 svm->vmcb->save.cr3 = __sme_set(root);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005772 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005773}
5774
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005775static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5776{
5777 struct vcpu_svm *svm = to_svm(vcpu);
5778
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005779 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01005780 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005781
5782 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02005783 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005784 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005785}
5786
Avi Kivity6aa8b732006-12-10 02:21:36 -08005787static int is_disabled(void)
5788{
Joerg Roedel6031a612007-06-22 12:29:50 +03005789 u64 vm_cr;
5790
5791 rdmsrl(MSR_VM_CR, vm_cr);
5792 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5793 return 1;
5794
Avi Kivity6aa8b732006-12-10 02:21:36 -08005795 return 0;
5796}
5797
Ingo Molnar102d8322007-02-19 14:37:47 +02005798static void
5799svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5800{
5801 /*
5802 * Patch in the VMMCALL instruction:
5803 */
5804 hypercall[0] = 0x0f;
5805 hypercall[1] = 0x01;
5806 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02005807}
5808
Yang, Sheng002c7f72007-07-31 14:23:01 +03005809static void svm_check_processor_compat(void *rtn)
5810{
5811 *(int *)rtn = 0;
5812}
5813
Avi Kivity774ead32007-12-26 13:57:04 +02005814static bool svm_cpu_has_accelerated_tpr(void)
5815{
5816 return false;
5817}
5818
Tom Lendackybc226f02018-05-10 22:06:39 +02005819static bool svm_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005820{
5821 return true;
5822}
5823
Paolo Bonzinifc07e762015-10-01 13:20:22 +02005824static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5825{
5826 return 0;
5827}
5828
Sheng Yang0e851882009-12-18 16:48:46 +08005829static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5830{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005831 struct vcpu_svm *svm = to_svm(vcpu);
5832
5833 /* Update nrips enabled cache */
Radim Krčmářd6321d42017-08-05 00:12:49 +02005834 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005835
5836 if (!kvm_vcpu_apicv_active(vcpu))
5837 return;
5838
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02005839 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08005840}
5841
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005842static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5843{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005844 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005845 case 0x1:
5846 if (avic)
5847 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5848 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02005849 case 0x80000001:
5850 if (nested)
5851 entry->ecx |= (1 << 2); /* Set SVM bit */
5852 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005853 case 0x8000000A:
5854 entry->eax = 1; /* SVM revision 1 */
5855 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5856 ASID emulation to nested SVM */
5857 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02005858 entry->edx = 0; /* Per default do not support any
5859 additional features */
5860
5861 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02005862 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02005863 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005864
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005865 /* Support NPT for the guest if enabled */
5866 if (npt_enabled)
5867 entry->edx |= SVM_FEATURE_NPT;
5868
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005869 break;
Brijesh Singh8765d752017-12-04 10:57:25 -06005870 case 0x8000001F:
5871 /* Support memory encryption cpuid if host supports it */
5872 if (boot_cpu_has(X86_FEATURE_SEV))
5873 cpuid(0x8000001f, &entry->eax, &entry->ebx,
5874 &entry->ecx, &entry->edx);
5875
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005876 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005877}
5878
Sheng Yang17cc3932010-01-05 19:02:27 +08005879static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005880{
Sheng Yang17cc3932010-01-05 19:02:27 +08005881 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005882}
5883
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005884static bool svm_rdtscp_supported(void)
5885{
Paolo Bonzini46896c72015-11-12 14:49:16 +01005886 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005887}
5888
Mao, Junjiead756a12012-07-02 01:18:48 +00005889static bool svm_invpcid_supported(void)
5890{
5891 return false;
5892}
5893
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005894static bool svm_mpx_supported(void)
5895{
5896 return false;
5897}
5898
Wanpeng Li55412b22014-12-02 19:21:30 +08005899static bool svm_xsaves_supported(void)
5900{
5901 return false;
5902}
5903
Paolo Bonzini66336ca2016-07-12 10:36:41 +02005904static bool svm_umip_emulated(void)
5905{
5906 return false;
5907}
5908
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005909static bool svm_has_wbinvd_exit(void)
5910{
5911 return true;
5912}
5913
Joerg Roedel80612522011-04-04 12:39:33 +02005914#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005915 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005916#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005917 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005918#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005919 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005920
Mathias Krause09941fb2012-08-30 01:30:20 +02005921static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005922 u32 exit_code;
5923 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005924} x86_intercept_map[] = {
5925 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
5926 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
5927 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
5928 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
5929 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02005930 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
5931 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02005932 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
5933 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
5934 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
5935 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
5936 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
5937 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
5938 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
5939 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02005940 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
5941 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
5942 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
5943 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
5944 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
5945 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
5946 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
5947 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005948 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
5949 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
5950 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02005951 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
5952 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
5953 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
5954 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
5955 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
5956 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
5957 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
5958 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
5959 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02005960 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
5961 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
5962 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
5963 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
5964 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
5965 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
5966 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02005967 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
5968 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
5969 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
5970 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005971};
5972
Joerg Roedel80612522011-04-04 12:39:33 +02005973#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005974#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005975#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005976
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005977static int svm_check_intercept(struct kvm_vcpu *vcpu,
5978 struct x86_instruction_info *info,
5979 enum x86_intercept_stage stage)
5980{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005981 struct vcpu_svm *svm = to_svm(vcpu);
5982 int vmexit, ret = X86EMUL_CONTINUE;
5983 struct __x86_intercept icpt_info;
5984 struct vmcb *vmcb = svm->vmcb;
5985
5986 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
5987 goto out;
5988
5989 icpt_info = x86_intercept_map[info->intercept];
5990
Avi Kivity40e19b52011-04-21 12:35:41 +03005991 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005992 goto out;
5993
5994 switch (icpt_info.exit_code) {
5995 case SVM_EXIT_READ_CR0:
5996 if (info->intercept == x86_intercept_cr_read)
5997 icpt_info.exit_code += info->modrm_reg;
5998 break;
5999 case SVM_EXIT_WRITE_CR0: {
6000 unsigned long cr0, val;
6001 u64 intercept;
6002
6003 if (info->intercept == x86_intercept_cr_write)
6004 icpt_info.exit_code += info->modrm_reg;
6005
Jan Kiszka62baf442014-06-29 21:55:53 +02006006 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
6007 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006008 break;
6009
6010 intercept = svm->nested.intercept;
6011
6012 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
6013 break;
6014
6015 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
6016 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
6017
6018 if (info->intercept == x86_intercept_lmsw) {
6019 cr0 &= 0xfUL;
6020 val &= 0xfUL;
6021 /* lmsw can't clear PE - catch this here */
6022 if (cr0 & X86_CR0_PE)
6023 val |= X86_CR0_PE;
6024 }
6025
6026 if (cr0 ^ val)
6027 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
6028
6029 break;
6030 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02006031 case SVM_EXIT_READ_DR0:
6032 case SVM_EXIT_WRITE_DR0:
6033 icpt_info.exit_code += info->modrm_reg;
6034 break;
Joerg Roedel80612522011-04-04 12:39:33 +02006035 case SVM_EXIT_MSR:
6036 if (info->intercept == x86_intercept_wrmsr)
6037 vmcb->control.exit_info_1 = 1;
6038 else
6039 vmcb->control.exit_info_1 = 0;
6040 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02006041 case SVM_EXIT_PAUSE:
6042 /*
6043 * We get this for NOP only, but pause
6044 * is rep not, check this here
6045 */
6046 if (info->rep_prefix != REPE_PREFIX)
6047 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02006048 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02006049 case SVM_EXIT_IOIO: {
6050 u64 exit_info;
6051 u32 bytes;
6052
Joerg Roedelf6511932011-04-04 12:39:35 +02006053 if (info->intercept == x86_intercept_in ||
6054 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02006055 exit_info = ((info->src_val & 0xffff) << 16) |
6056 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02006057 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02006058 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02006059 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02006060 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02006061 }
6062
6063 if (info->intercept == x86_intercept_outs ||
6064 info->intercept == x86_intercept_ins)
6065 exit_info |= SVM_IOIO_STR_MASK;
6066
6067 if (info->rep_prefix)
6068 exit_info |= SVM_IOIO_REP_MASK;
6069
6070 bytes = min(bytes, 4u);
6071
6072 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
6073
6074 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
6075
6076 vmcb->control.exit_info_1 = exit_info;
6077 vmcb->control.exit_info_2 = info->next_rip;
6078
6079 break;
6080 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006081 default:
6082 break;
6083 }
6084
Bandan Dasf1047652015-06-11 02:05:33 -04006085 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
6086 if (static_cpu_has(X86_FEATURE_NRIPS))
6087 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006088 vmcb->control.exit_code = icpt_info.exit_code;
6089 vmexit = nested_svm_exit_handled(svm);
6090
6091 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
6092 : X86EMUL_CONTINUE;
6093
6094out:
6095 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006096}
6097
Yang Zhanga547c6d2013-04-11 19:25:10 +08006098static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
6099{
6100 local_irq_enable();
Paolo Bonzinif2485b32016-06-15 15:23:11 +02006101 /*
6102 * We must have an instruction with interrupts enabled, so
6103 * the timer interrupt isn't delayed by the interrupt shadow.
6104 */
6105 asm("nop");
6106 local_irq_disable();
Yang Zhanga547c6d2013-04-11 19:25:10 +08006107}
6108
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006109static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
6110{
Babu Moger8566ac82018-03-16 16:37:26 -04006111 if (pause_filter_thresh)
6112 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006113}
6114
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05006115static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
6116{
6117 if (avic_handle_apic_id_update(vcpu) != 0)
6118 return;
6119 if (avic_handle_dfr_update(vcpu) != 0)
6120 return;
6121 avic_handle_ldr_update(vcpu);
6122}
6123
Borislav Petkov74f16902017-03-26 23:51:24 +02006124static void svm_setup_mce(struct kvm_vcpu *vcpu)
6125{
6126 /* [63:9] are reserved. */
6127 vcpu->arch.mcg_cap &= 0x1ff;
6128}
6129
Ladi Prosek72d7b372017-10-11 16:54:41 +02006130static int svm_smi_allowed(struct kvm_vcpu *vcpu)
6131{
Ladi Prosek05cade72017-10-11 16:54:45 +02006132 struct vcpu_svm *svm = to_svm(vcpu);
6133
6134 /* Per APM Vol.2 15.22.2 "Response to SMI" */
6135 if (!gif_set(svm))
6136 return 0;
6137
6138 if (is_guest_mode(&svm->vcpu) &&
6139 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
6140 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
6141 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
6142 svm->nested.exit_required = true;
6143 return 0;
6144 }
6145
Ladi Prosek72d7b372017-10-11 16:54:41 +02006146 return 1;
6147}
6148
Ladi Prosek0234bf82017-10-11 16:54:40 +02006149static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
6150{
Ladi Prosek05cade72017-10-11 16:54:45 +02006151 struct vcpu_svm *svm = to_svm(vcpu);
6152 int ret;
6153
6154 if (is_guest_mode(vcpu)) {
6155 /* FED8h - SVM Guest */
6156 put_smstate(u64, smstate, 0x7ed8, 1);
6157 /* FEE0h - SVM Guest VMCB Physical Address */
6158 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
6159
6160 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
6161 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
6162 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
6163
6164 ret = nested_svm_vmexit(svm);
6165 if (ret)
6166 return ret;
6167 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02006168 return 0;
6169}
6170
6171static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
6172{
Ladi Prosek05cade72017-10-11 16:54:45 +02006173 struct vcpu_svm *svm = to_svm(vcpu);
6174 struct vmcb *nested_vmcb;
6175 struct page *page;
6176 struct {
6177 u64 guest;
6178 u64 vmcb;
6179 } svm_state_save;
6180 int ret;
6181
6182 ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save,
6183 sizeof(svm_state_save));
6184 if (ret)
6185 return ret;
6186
6187 if (svm_state_save.guest) {
6188 vcpu->arch.hflags &= ~HF_SMM_MASK;
6189 nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page);
6190 if (nested_vmcb)
6191 enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page);
6192 else
6193 ret = 1;
6194 vcpu->arch.hflags |= HF_SMM_MASK;
6195 }
6196 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02006197}
6198
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006199static int enable_smi_window(struct kvm_vcpu *vcpu)
6200{
6201 struct vcpu_svm *svm = to_svm(vcpu);
6202
6203 if (!gif_set(svm)) {
6204 if (vgif_enabled(svm))
6205 set_intercept(svm, INTERCEPT_STGI);
6206 /* STGI will cause a vm exit */
6207 return 1;
6208 }
6209 return 0;
6210}
6211
Brijesh Singh1654efc2017-12-04 10:57:34 -06006212static int sev_asid_new(void)
6213{
6214 int pos;
6215
6216 /*
6217 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
6218 */
6219 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
6220 if (pos >= max_sev_asid)
6221 return -EBUSY;
6222
6223 set_bit(pos, sev_asid_bitmap);
6224 return pos + 1;
6225}
6226
6227static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
6228{
Sean Christopherson81811c12018-03-20 12:17:21 -07006229 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006230 int asid, ret;
6231
6232 ret = -EBUSY;
6233 asid = sev_asid_new();
6234 if (asid < 0)
6235 return ret;
6236
6237 ret = sev_platform_init(&argp->error);
6238 if (ret)
6239 goto e_free;
6240
6241 sev->active = true;
6242 sev->asid = asid;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006243 INIT_LIST_HEAD(&sev->regions_list);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006244
6245 return 0;
6246
6247e_free:
6248 __sev_asid_free(asid);
6249 return ret;
6250}
6251
Brijesh Singh59414c92017-12-04 10:57:35 -06006252static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
6253{
6254 struct sev_data_activate *data;
6255 int asid = sev_get_asid(kvm);
6256 int ret;
6257
6258 wbinvd_on_all_cpus();
6259
6260 ret = sev_guest_df_flush(error);
6261 if (ret)
6262 return ret;
6263
6264 data = kzalloc(sizeof(*data), GFP_KERNEL);
6265 if (!data)
6266 return -ENOMEM;
6267
6268 /* activate ASID on the given handle */
6269 data->handle = handle;
6270 data->asid = asid;
6271 ret = sev_guest_activate(data, error);
6272 kfree(data);
6273
6274 return ret;
6275}
6276
Brijesh Singh89c50582017-12-04 10:57:35 -06006277static int __sev_issue_cmd(int fd, int id, void *data, int *error)
Brijesh Singh59414c92017-12-04 10:57:35 -06006278{
6279 struct fd f;
6280 int ret;
6281
6282 f = fdget(fd);
6283 if (!f.file)
6284 return -EBADF;
6285
6286 ret = sev_issue_cmd_external_user(f.file, id, data, error);
6287
6288 fdput(f);
6289 return ret;
6290}
6291
Brijesh Singh89c50582017-12-04 10:57:35 -06006292static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
6293{
Sean Christopherson81811c12018-03-20 12:17:21 -07006294 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06006295
6296 return __sev_issue_cmd(sev->fd, id, data, error);
6297}
6298
Brijesh Singh59414c92017-12-04 10:57:35 -06006299static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
6300{
Sean Christopherson81811c12018-03-20 12:17:21 -07006301 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh59414c92017-12-04 10:57:35 -06006302 struct sev_data_launch_start *start;
6303 struct kvm_sev_launch_start params;
6304 void *dh_blob, *session_blob;
6305 int *error = &argp->error;
6306 int ret;
6307
6308 if (!sev_guest(kvm))
6309 return -ENOTTY;
6310
6311 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6312 return -EFAULT;
6313
6314 start = kzalloc(sizeof(*start), GFP_KERNEL);
6315 if (!start)
6316 return -ENOMEM;
6317
6318 dh_blob = NULL;
6319 if (params.dh_uaddr) {
6320 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
6321 if (IS_ERR(dh_blob)) {
6322 ret = PTR_ERR(dh_blob);
6323 goto e_free;
6324 }
6325
6326 start->dh_cert_address = __sme_set(__pa(dh_blob));
6327 start->dh_cert_len = params.dh_len;
6328 }
6329
6330 session_blob = NULL;
6331 if (params.session_uaddr) {
6332 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
6333 if (IS_ERR(session_blob)) {
6334 ret = PTR_ERR(session_blob);
6335 goto e_free_dh;
6336 }
6337
6338 start->session_address = __sme_set(__pa(session_blob));
6339 start->session_len = params.session_len;
6340 }
6341
6342 start->handle = params.handle;
6343 start->policy = params.policy;
6344
6345 /* create memory encryption context */
Brijesh Singh89c50582017-12-04 10:57:35 -06006346 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
Brijesh Singh59414c92017-12-04 10:57:35 -06006347 if (ret)
6348 goto e_free_session;
6349
6350 /* Bind ASID to this guest */
6351 ret = sev_bind_asid(kvm, start->handle, error);
6352 if (ret)
6353 goto e_free_session;
6354
6355 /* return handle to userspace */
6356 params.handle = start->handle;
6357 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
6358 sev_unbind_asid(kvm, start->handle);
6359 ret = -EFAULT;
6360 goto e_free_session;
6361 }
6362
6363 sev->handle = start->handle;
6364 sev->fd = argp->sev_fd;
6365
6366e_free_session:
6367 kfree(session_blob);
6368e_free_dh:
6369 kfree(dh_blob);
6370e_free:
6371 kfree(start);
6372 return ret;
6373}
6374
Brijesh Singh89c50582017-12-04 10:57:35 -06006375static int get_num_contig_pages(int idx, struct page **inpages,
6376 unsigned long npages)
6377{
6378 unsigned long paddr, next_paddr;
6379 int i = idx + 1, pages = 1;
6380
6381 /* find the number of contiguous pages starting from idx */
6382 paddr = __sme_page_pa(inpages[idx]);
6383 while (i < npages) {
6384 next_paddr = __sme_page_pa(inpages[i++]);
6385 if ((paddr + PAGE_SIZE) == next_paddr) {
6386 pages++;
6387 paddr = next_paddr;
6388 continue;
6389 }
6390 break;
6391 }
6392
6393 return pages;
6394}
6395
6396static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
6397{
6398 unsigned long vaddr, vaddr_end, next_vaddr, npages, size;
Sean Christopherson81811c12018-03-20 12:17:21 -07006399 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06006400 struct kvm_sev_launch_update_data params;
6401 struct sev_data_launch_update_data *data;
6402 struct page **inpages;
6403 int i, ret, pages;
6404
6405 if (!sev_guest(kvm))
6406 return -ENOTTY;
6407
6408 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6409 return -EFAULT;
6410
6411 data = kzalloc(sizeof(*data), GFP_KERNEL);
6412 if (!data)
6413 return -ENOMEM;
6414
6415 vaddr = params.uaddr;
6416 size = params.len;
6417 vaddr_end = vaddr + size;
6418
6419 /* Lock the user memory. */
6420 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
6421 if (!inpages) {
6422 ret = -ENOMEM;
6423 goto e_free;
6424 }
6425
6426 /*
6427 * The LAUNCH_UPDATE command will perform in-place encryption of the
6428 * memory content (i.e it will write the same memory region with C=1).
6429 * It's possible that the cache may contain the data with C=0, i.e.,
6430 * unencrypted so invalidate it first.
6431 */
6432 sev_clflush_pages(inpages, npages);
6433
6434 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
6435 int offset, len;
6436
6437 /*
6438 * If the user buffer is not page-aligned, calculate the offset
6439 * within the page.
6440 */
6441 offset = vaddr & (PAGE_SIZE - 1);
6442
6443 /* Calculate the number of pages that can be encrypted in one go. */
6444 pages = get_num_contig_pages(i, inpages, npages);
6445
6446 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
6447
6448 data->handle = sev->handle;
6449 data->len = len;
6450 data->address = __sme_page_pa(inpages[i]) + offset;
6451 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
6452 if (ret)
6453 goto e_unpin;
6454
6455 size -= len;
6456 next_vaddr = vaddr + len;
6457 }
6458
6459e_unpin:
6460 /* content of memory is updated, mark pages dirty */
6461 for (i = 0; i < npages; i++) {
6462 set_page_dirty_lock(inpages[i]);
6463 mark_page_accessed(inpages[i]);
6464 }
6465 /* unlock the user pages */
6466 sev_unpin_memory(kvm, inpages, npages);
6467e_free:
6468 kfree(data);
6469 return ret;
6470}
6471
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006472static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
6473{
Brijesh Singh3e233382018-02-23 12:36:50 -06006474 void __user *measure = (void __user *)(uintptr_t)argp->data;
Sean Christopherson81811c12018-03-20 12:17:21 -07006475 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006476 struct sev_data_launch_measure *data;
6477 struct kvm_sev_launch_measure params;
Brijesh Singh3e233382018-02-23 12:36:50 -06006478 void __user *p = NULL;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006479 void *blob = NULL;
6480 int ret;
6481
6482 if (!sev_guest(kvm))
6483 return -ENOTTY;
6484
Brijesh Singh3e233382018-02-23 12:36:50 -06006485 if (copy_from_user(&params, measure, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006486 return -EFAULT;
6487
6488 data = kzalloc(sizeof(*data), GFP_KERNEL);
6489 if (!data)
6490 return -ENOMEM;
6491
6492 /* User wants to query the blob length */
6493 if (!params.len)
6494 goto cmd;
6495
Brijesh Singh3e233382018-02-23 12:36:50 -06006496 p = (void __user *)(uintptr_t)params.uaddr;
6497 if (p) {
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006498 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
6499 ret = -EINVAL;
6500 goto e_free;
6501 }
6502
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006503 ret = -ENOMEM;
6504 blob = kmalloc(params.len, GFP_KERNEL);
6505 if (!blob)
6506 goto e_free;
6507
6508 data->address = __psp_pa(blob);
6509 data->len = params.len;
6510 }
6511
6512cmd:
6513 data->handle = sev->handle;
6514 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
6515
6516 /*
6517 * If we query the session length, FW responded with expected data.
6518 */
6519 if (!params.len)
6520 goto done;
6521
6522 if (ret)
6523 goto e_free_blob;
6524
6525 if (blob) {
Brijesh Singh3e233382018-02-23 12:36:50 -06006526 if (copy_to_user(p, blob, params.len))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006527 ret = -EFAULT;
6528 }
6529
6530done:
6531 params.len = data->len;
Brijesh Singh3e233382018-02-23 12:36:50 -06006532 if (copy_to_user(measure, &params, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006533 ret = -EFAULT;
6534e_free_blob:
6535 kfree(blob);
6536e_free:
6537 kfree(data);
6538 return ret;
6539}
6540
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006541static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
6542{
Sean Christopherson81811c12018-03-20 12:17:21 -07006543 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006544 struct sev_data_launch_finish *data;
6545 int ret;
6546
6547 if (!sev_guest(kvm))
6548 return -ENOTTY;
6549
6550 data = kzalloc(sizeof(*data), GFP_KERNEL);
6551 if (!data)
6552 return -ENOMEM;
6553
6554 data->handle = sev->handle;
6555 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
6556
6557 kfree(data);
6558 return ret;
6559}
6560
Brijesh Singh255d9e72017-12-04 10:57:37 -06006561static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
6562{
Sean Christopherson81811c12018-03-20 12:17:21 -07006563 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006564 struct kvm_sev_guest_status params;
6565 struct sev_data_guest_status *data;
6566 int ret;
6567
6568 if (!sev_guest(kvm))
6569 return -ENOTTY;
6570
6571 data = kzalloc(sizeof(*data), GFP_KERNEL);
6572 if (!data)
6573 return -ENOMEM;
6574
6575 data->handle = sev->handle;
6576 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
6577 if (ret)
6578 goto e_free;
6579
6580 params.policy = data->policy;
6581 params.state = data->state;
6582 params.handle = data->handle;
6583
6584 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6585 ret = -EFAULT;
6586e_free:
6587 kfree(data);
6588 return ret;
6589}
6590
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006591static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
6592 unsigned long dst, int size,
6593 int *error, bool enc)
6594{
Sean Christopherson81811c12018-03-20 12:17:21 -07006595 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006596 struct sev_data_dbg *data;
6597 int ret;
6598
6599 data = kzalloc(sizeof(*data), GFP_KERNEL);
6600 if (!data)
6601 return -ENOMEM;
6602
6603 data->handle = sev->handle;
6604 data->dst_addr = dst;
6605 data->src_addr = src;
6606 data->len = size;
6607
6608 ret = sev_issue_cmd(kvm,
6609 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
6610 data, error);
6611 kfree(data);
6612 return ret;
6613}
6614
6615static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
6616 unsigned long dst_paddr, int sz, int *err)
6617{
6618 int offset;
6619
6620 /*
6621 * Its safe to read more than we are asked, caller should ensure that
6622 * destination has enough space.
6623 */
6624 src_paddr = round_down(src_paddr, 16);
6625 offset = src_paddr & 15;
6626 sz = round_up(sz + offset, 16);
6627
6628 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
6629}
6630
6631static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
6632 unsigned long __user dst_uaddr,
6633 unsigned long dst_paddr,
6634 int size, int *err)
6635{
6636 struct page *tpage = NULL;
6637 int ret, offset;
6638
6639 /* if inputs are not 16-byte then use intermediate buffer */
6640 if (!IS_ALIGNED(dst_paddr, 16) ||
6641 !IS_ALIGNED(paddr, 16) ||
6642 !IS_ALIGNED(size, 16)) {
6643 tpage = (void *)alloc_page(GFP_KERNEL);
6644 if (!tpage)
6645 return -ENOMEM;
6646
6647 dst_paddr = __sme_page_pa(tpage);
6648 }
6649
6650 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
6651 if (ret)
6652 goto e_free;
6653
6654 if (tpage) {
6655 offset = paddr & 15;
6656 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
6657 page_address(tpage) + offset, size))
6658 ret = -EFAULT;
6659 }
6660
6661e_free:
6662 if (tpage)
6663 __free_page(tpage);
6664
6665 return ret;
6666}
6667
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006668static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
6669 unsigned long __user vaddr,
6670 unsigned long dst_paddr,
6671 unsigned long __user dst_vaddr,
6672 int size, int *error)
6673{
6674 struct page *src_tpage = NULL;
6675 struct page *dst_tpage = NULL;
6676 int ret, len = size;
6677
6678 /* If source buffer is not aligned then use an intermediate buffer */
6679 if (!IS_ALIGNED(vaddr, 16)) {
6680 src_tpage = alloc_page(GFP_KERNEL);
6681 if (!src_tpage)
6682 return -ENOMEM;
6683
6684 if (copy_from_user(page_address(src_tpage),
6685 (void __user *)(uintptr_t)vaddr, size)) {
6686 __free_page(src_tpage);
6687 return -EFAULT;
6688 }
6689
6690 paddr = __sme_page_pa(src_tpage);
6691 }
6692
6693 /*
6694 * If destination buffer or length is not aligned then do read-modify-write:
6695 * - decrypt destination in an intermediate buffer
6696 * - copy the source buffer in an intermediate buffer
6697 * - use the intermediate buffer as source buffer
6698 */
6699 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
6700 int dst_offset;
6701
6702 dst_tpage = alloc_page(GFP_KERNEL);
6703 if (!dst_tpage) {
6704 ret = -ENOMEM;
6705 goto e_free;
6706 }
6707
6708 ret = __sev_dbg_decrypt(kvm, dst_paddr,
6709 __sme_page_pa(dst_tpage), size, error);
6710 if (ret)
6711 goto e_free;
6712
6713 /*
6714 * If source is kernel buffer then use memcpy() otherwise
6715 * copy_from_user().
6716 */
6717 dst_offset = dst_paddr & 15;
6718
6719 if (src_tpage)
6720 memcpy(page_address(dst_tpage) + dst_offset,
6721 page_address(src_tpage), size);
6722 else {
6723 if (copy_from_user(page_address(dst_tpage) + dst_offset,
6724 (void __user *)(uintptr_t)vaddr, size)) {
6725 ret = -EFAULT;
6726 goto e_free;
6727 }
6728 }
6729
6730 paddr = __sme_page_pa(dst_tpage);
6731 dst_paddr = round_down(dst_paddr, 16);
6732 len = round_up(size, 16);
6733 }
6734
6735 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
6736
6737e_free:
6738 if (src_tpage)
6739 __free_page(src_tpage);
6740 if (dst_tpage)
6741 __free_page(dst_tpage);
6742 return ret;
6743}
6744
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006745static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
6746{
6747 unsigned long vaddr, vaddr_end, next_vaddr;
Colin Ian King0186ec82018-08-28 16:22:28 +01006748 unsigned long dst_vaddr;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006749 struct page **src_p, **dst_p;
6750 struct kvm_sev_dbg debug;
6751 unsigned long n;
6752 int ret, size;
6753
6754 if (!sev_guest(kvm))
6755 return -ENOTTY;
6756
6757 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
6758 return -EFAULT;
6759
6760 vaddr = debug.src_uaddr;
6761 size = debug.len;
6762 vaddr_end = vaddr + size;
6763 dst_vaddr = debug.dst_uaddr;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006764
6765 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
6766 int len, s_off, d_off;
6767
6768 /* lock userspace source and destination page */
6769 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
6770 if (!src_p)
6771 return -EFAULT;
6772
6773 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
6774 if (!dst_p) {
6775 sev_unpin_memory(kvm, src_p, n);
6776 return -EFAULT;
6777 }
6778
6779 /*
6780 * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
6781 * memory content (i.e it will write the same memory region with C=1).
6782 * It's possible that the cache may contain the data with C=0, i.e.,
6783 * unencrypted so invalidate it first.
6784 */
6785 sev_clflush_pages(src_p, 1);
6786 sev_clflush_pages(dst_p, 1);
6787
6788 /*
6789 * Since user buffer may not be page aligned, calculate the
6790 * offset within the page.
6791 */
6792 s_off = vaddr & ~PAGE_MASK;
6793 d_off = dst_vaddr & ~PAGE_MASK;
6794 len = min_t(size_t, (PAGE_SIZE - s_off), size);
6795
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006796 if (dec)
6797 ret = __sev_dbg_decrypt_user(kvm,
6798 __sme_page_pa(src_p[0]) + s_off,
6799 dst_vaddr,
6800 __sme_page_pa(dst_p[0]) + d_off,
6801 len, &argp->error);
6802 else
6803 ret = __sev_dbg_encrypt_user(kvm,
6804 __sme_page_pa(src_p[0]) + s_off,
6805 vaddr,
6806 __sme_page_pa(dst_p[0]) + d_off,
6807 dst_vaddr,
6808 len, &argp->error);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006809
6810 sev_unpin_memory(kvm, src_p, 1);
6811 sev_unpin_memory(kvm, dst_p, 1);
6812
6813 if (ret)
6814 goto err;
6815
6816 next_vaddr = vaddr + len;
6817 dst_vaddr = dst_vaddr + len;
6818 size -= len;
6819 }
6820err:
6821 return ret;
6822}
6823
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006824static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
6825{
Sean Christopherson81811c12018-03-20 12:17:21 -07006826 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006827 struct sev_data_launch_secret *data;
6828 struct kvm_sev_launch_secret params;
6829 struct page **pages;
6830 void *blob, *hdr;
6831 unsigned long n;
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006832 int ret, offset;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006833
6834 if (!sev_guest(kvm))
6835 return -ENOTTY;
6836
6837 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6838 return -EFAULT;
6839
6840 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
6841 if (!pages)
6842 return -ENOMEM;
6843
6844 /*
6845 * The secret must be copied into contiguous memory region, lets verify
6846 * that userspace memory pages are contiguous before we issue command.
6847 */
6848 if (get_num_contig_pages(0, pages, n) != n) {
6849 ret = -EINVAL;
6850 goto e_unpin_memory;
6851 }
6852
6853 ret = -ENOMEM;
6854 data = kzalloc(sizeof(*data), GFP_KERNEL);
6855 if (!data)
6856 goto e_unpin_memory;
6857
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006858 offset = params.guest_uaddr & (PAGE_SIZE - 1);
6859 data->guest_address = __sme_page_pa(pages[0]) + offset;
6860 data->guest_len = params.guest_len;
6861
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006862 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
6863 if (IS_ERR(blob)) {
6864 ret = PTR_ERR(blob);
6865 goto e_free;
6866 }
6867
6868 data->trans_address = __psp_pa(blob);
6869 data->trans_len = params.trans_len;
6870
6871 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
6872 if (IS_ERR(hdr)) {
6873 ret = PTR_ERR(hdr);
6874 goto e_free_blob;
6875 }
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006876 data->hdr_address = __psp_pa(hdr);
6877 data->hdr_len = params.hdr_len;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006878
6879 data->handle = sev->handle;
6880 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
6881
6882 kfree(hdr);
6883
6884e_free_blob:
6885 kfree(blob);
6886e_free:
6887 kfree(data);
6888e_unpin_memory:
6889 sev_unpin_memory(kvm, pages, n);
6890 return ret;
6891}
6892
Brijesh Singh1654efc2017-12-04 10:57:34 -06006893static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
6894{
6895 struct kvm_sev_cmd sev_cmd;
6896 int r;
6897
6898 if (!svm_sev_enabled())
6899 return -ENOTTY;
6900
6901 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
6902 return -EFAULT;
6903
6904 mutex_lock(&kvm->lock);
6905
6906 switch (sev_cmd.id) {
6907 case KVM_SEV_INIT:
6908 r = sev_guest_init(kvm, &sev_cmd);
6909 break;
Brijesh Singh59414c92017-12-04 10:57:35 -06006910 case KVM_SEV_LAUNCH_START:
6911 r = sev_launch_start(kvm, &sev_cmd);
6912 break;
Brijesh Singh89c50582017-12-04 10:57:35 -06006913 case KVM_SEV_LAUNCH_UPDATE_DATA:
6914 r = sev_launch_update_data(kvm, &sev_cmd);
6915 break;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006916 case KVM_SEV_LAUNCH_MEASURE:
6917 r = sev_launch_measure(kvm, &sev_cmd);
6918 break;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006919 case KVM_SEV_LAUNCH_FINISH:
6920 r = sev_launch_finish(kvm, &sev_cmd);
6921 break;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006922 case KVM_SEV_GUEST_STATUS:
6923 r = sev_guest_status(kvm, &sev_cmd);
6924 break;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006925 case KVM_SEV_DBG_DECRYPT:
6926 r = sev_dbg_crypt(kvm, &sev_cmd, true);
6927 break;
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006928 case KVM_SEV_DBG_ENCRYPT:
6929 r = sev_dbg_crypt(kvm, &sev_cmd, false);
6930 break;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006931 case KVM_SEV_LAUNCH_SECRET:
6932 r = sev_launch_secret(kvm, &sev_cmd);
6933 break;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006934 default:
6935 r = -EINVAL;
6936 goto out;
6937 }
6938
6939 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
6940 r = -EFAULT;
6941
6942out:
6943 mutex_unlock(&kvm->lock);
6944 return r;
6945}
6946
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006947static int svm_register_enc_region(struct kvm *kvm,
6948 struct kvm_enc_region *range)
6949{
Sean Christopherson81811c12018-03-20 12:17:21 -07006950 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006951 struct enc_region *region;
6952 int ret = 0;
6953
6954 if (!sev_guest(kvm))
6955 return -ENOTTY;
6956
Dan Carpenter86bf20c2018-05-19 09:01:36 +03006957 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
6958 return -EINVAL;
6959
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006960 region = kzalloc(sizeof(*region), GFP_KERNEL);
6961 if (!region)
6962 return -ENOMEM;
6963
6964 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
6965 if (!region->pages) {
6966 ret = -ENOMEM;
6967 goto e_free;
6968 }
6969
6970 /*
6971 * The guest may change the memory encryption attribute from C=0 -> C=1
6972 * or vice versa for this memory range. Lets make sure caches are
6973 * flushed to ensure that guest data gets written into memory with
6974 * correct C-bit.
6975 */
6976 sev_clflush_pages(region->pages, region->npages);
6977
6978 region->uaddr = range->addr;
6979 region->size = range->size;
6980
6981 mutex_lock(&kvm->lock);
6982 list_add_tail(&region->list, &sev->regions_list);
6983 mutex_unlock(&kvm->lock);
6984
6985 return ret;
6986
6987e_free:
6988 kfree(region);
6989 return ret;
6990}
6991
6992static struct enc_region *
6993find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
6994{
Sean Christopherson81811c12018-03-20 12:17:21 -07006995 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006996 struct list_head *head = &sev->regions_list;
6997 struct enc_region *i;
6998
6999 list_for_each_entry(i, head, list) {
7000 if (i->uaddr == range->addr &&
7001 i->size == range->size)
7002 return i;
7003 }
7004
7005 return NULL;
7006}
7007
7008
7009static int svm_unregister_enc_region(struct kvm *kvm,
7010 struct kvm_enc_region *range)
7011{
7012 struct enc_region *region;
7013 int ret;
7014
7015 mutex_lock(&kvm->lock);
7016
7017 if (!sev_guest(kvm)) {
7018 ret = -ENOTTY;
7019 goto failed;
7020 }
7021
7022 region = find_enc_region(kvm, range);
7023 if (!region) {
7024 ret = -EINVAL;
7025 goto failed;
7026 }
7027
7028 __unregister_enc_region_locked(kvm, region);
7029
7030 mutex_unlock(&kvm->lock);
7031 return 0;
7032
7033failed:
7034 mutex_unlock(&kvm->lock);
7035 return ret;
7036}
7037
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007038static int nested_enable_evmcs(struct kvm_vcpu *vcpu,
7039 uint16_t *vmcs_version)
7040{
7041 /* Intel-only feature */
7042 return -ENODEV;
7043}
7044
Kees Cook404f6aa2016-08-08 16:29:06 -07007045static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007046 .cpu_has_kvm_support = has_svm,
7047 .disabled_by_bios = is_disabled,
7048 .hardware_setup = svm_hardware_setup,
7049 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03007050 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007051 .hardware_enable = svm_hardware_enable,
7052 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02007053 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Tom Lendackybc226f02018-05-10 22:06:39 +02007054 .has_emulated_msr = svm_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007055
7056 .vcpu_create = svm_create_vcpu,
7057 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007058 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007059
Sean Christopherson434a1e92018-03-20 12:17:18 -07007060 .vm_alloc = svm_vm_alloc,
7061 .vm_free = svm_vm_free,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007062 .vm_init = avic_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06007063 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007064
Avi Kivity04d2cc72007-09-10 18:10:54 +03007065 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007066 .vcpu_load = svm_vcpu_load,
7067 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05007068 .vcpu_blocking = svm_vcpu_blocking,
7069 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007070
Paolo Bonzinia96036b2015-11-10 11:55:36 +01007071 .update_bp_intercept = update_bp_intercept,
Tom Lendacky801e4592018-02-21 13:39:51 -06007072 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007073 .get_msr = svm_get_msr,
7074 .set_msr = svm_set_msr,
7075 .get_segment_base = svm_get_segment_base,
7076 .get_segment = svm_get_segment,
7077 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007078 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10007079 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02007080 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02007081 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03007082 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007083 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007084 .set_cr3 = svm_set_cr3,
7085 .set_cr4 = svm_set_cr4,
7086 .set_efer = svm_set_efer,
7087 .get_idt = svm_get_idt,
7088 .set_idt = svm_set_idt,
7089 .get_gdt = svm_get_gdt,
7090 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007091 .get_dr6 = svm_get_dr6,
7092 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03007093 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01007094 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03007095 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007096 .get_rflags = svm_get_rflags,
7097 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007098
Avi Kivity6aa8b732006-12-10 02:21:36 -08007099 .tlb_flush = svm_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007100 .tlb_flush_gva = svm_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007101
Avi Kivity6aa8b732006-12-10 02:21:36 -08007102 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007103 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007104 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007105 .set_interrupt_shadow = svm_set_interrupt_shadow,
7106 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007107 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007108 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007109 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007110 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007111 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007112 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007113 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007114 .get_nmi_mask = svm_get_nmi_mask,
7115 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007116 .enable_nmi_window = enable_nmi_window,
7117 .enable_irq_window = enable_irq_window,
7118 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007119 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007120 .get_enable_apicv = svm_get_enable_apicv,
7121 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007122 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007123 .hwapic_irr_update = svm_hwapic_irr_update,
7124 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02007125 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05007126 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02007127
7128 .set_tss_addr = svm_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007129 .set_identity_map_addr = svm_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08007130 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007131 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007132
Avi Kivity586f9602010-11-18 13:09:54 +02007133 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007134
Sheng Yang17cc3932010-01-05 19:02:27 +08007135 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08007136
7137 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007138
7139 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00007140 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01007141 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08007142 .xsaves_supported = svm_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +02007143 .umip_emulated = svm_umip_emulated,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007144
7145 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007146
7147 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007148
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02007149 .read_l1_tsc_offset = svm_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007150 .write_tsc_offset = svm_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007151
7152 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007153
7154 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08007155 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007156
Sean Christophersond264ee02018-08-27 15:21:12 -07007157 .request_immediate_exit = __kvm_request_immediate_exit,
7158
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007159 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02007160
7161 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05007162 .deliver_posted_interrupt = svm_deliver_avic_intr,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05007163 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02007164 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007165
Ladi Prosek72d7b372017-10-11 16:54:41 +02007166 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007167 .pre_enter_smm = svm_pre_enter_smm,
7168 .pre_leave_smm = svm_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007169 .enable_smi_window = enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06007170
7171 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007172 .mem_enc_reg_region = svm_register_enc_region,
7173 .mem_enc_unreg_region = svm_unregister_enc_region,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007174
7175 .nested_enable_evmcs = nested_enable_evmcs,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007176};
7177
7178static int __init svm_init(void)
7179{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007180 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03007181 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007182}
7183
7184static void __exit svm_exit(void)
7185{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007186 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08007187}
7188
7189module_init(svm_init)
7190module_exit(svm_exit)