blob: 9583ae7ae218fcb7f19be3f78ebe544c204e2b97 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Avi Kivity6aa8b732006-12-10 02:21:36 -08002/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * AMD SVM support
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Yaniv Kamay <yaniv@qumranet.com>
12 * Avi Kivity <avi@qumranet.com>
Avi Kivity6aa8b732006-12-10 02:21:36 -080013 */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050014
15#define pr_fmt(fmt) "SVM: " fmt
16
Avi Kivityedf88412007-12-16 11:02:48 +020017#include <linux/kvm_host.h>
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030021#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020022#include "x86.h"
Julian Stecklina66f7b722012-12-05 15:26:19 +010023#include "cpuid.h"
Wei Huang25462f72015-06-19 15:45:05 +020024#include "pmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040025
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/module.h>
Josh Triplettae759542012-03-28 11:32:28 -070027#include <linux/mod_devicetable.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020028#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/vmalloc.h>
30#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040031#include <linux/sched.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -050034#include <linux/amd-iommu.h>
35#include <linux/hashtable.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Brijesh Singhe9df0942017-12-04 10:57:33 -060037#include <linux/psp-sev.h>
Brijesh Singh1654efc2017-12-04 10:57:34 -060038#include <linux/file.h>
Brijesh Singh89c50582017-12-04 10:57:35 -060039#include <linux/pagemap.h>
40#include <linux/swap.h>
Tom Lendacky33af3a72019-10-03 21:17:48 +000041#include <linux/rwsem.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050043#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010044#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020045#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010047#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020048#include <asm/kvm_para.h>
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -050049#include <asm/irq_remapping.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020050#include <asm/spec-ctrl.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
Eduardo Habkost63d11422008-11-17 19:03:20 -020052#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030053#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020054
Avi Kivity4ecac3f2008-05-13 13:23:38 +030055#define __ex(x) __kvm_handle_fault_on_reboot(x)
56
Avi Kivity6aa8b732006-12-10 02:21:36 -080057MODULE_AUTHOR("Qumranet");
58MODULE_LICENSE("GPL");
59
Josh Triplettae759542012-03-28 11:32:28 -070060static const struct x86_cpu_id svm_cpu_id[] = {
61 X86_FEATURE_MATCH(X86_FEATURE_SVM),
62 {}
63};
64MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
65
Avi Kivity6aa8b732006-12-10 02:21:36 -080066#define IOPM_ALLOC_ORDER 2
67#define MSRPM_ALLOC_ORDER 1
68
Avi Kivity6aa8b732006-12-10 02:21:36 -080069#define SEG_TYPE_LDT 2
70#define SEG_TYPE_BUSY_TSS16 3
71
Andre Przywara6bc31bd2010-04-11 23:07:28 +020072#define SVM_FEATURE_LBRV (1 << 1)
73#define SVM_FEATURE_SVML (1 << 2)
Andre Przywaraddce97a2010-12-21 11:12:03 +010074#define SVM_FEATURE_TSC_RATE (1 << 4)
75#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
76#define SVM_FEATURE_FLUSH_ASID (1 << 6)
77#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020078#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030079
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -050080#define SVM_AVIC_DOORBELL 0xc001011b
81
Joerg Roedel410e4d52009-08-07 11:49:44 +020082#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
83#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
84#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
85
Joerg Roedel24e09cb2008-02-13 18:58:47 +010086#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
87
Joerg Roedelfbc0db72011-03-25 09:44:46 +010088#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010089#define TSC_RATIO_MIN 0x0000000000000001ULL
90#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010091
Dan Carpenter5446a972016-05-23 13:20:10 +030092#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050093
94/*
95 * 0xff is broadcast, so the max index allowed for physical APIC ID
96 * table is 0xfe. APIC IDs above 0xff are reserved.
97 */
98#define AVIC_MAX_PHYSICAL_ID_COUNT 255
99
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500100#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
101#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
102#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
103
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500104/* AVIC GATAG is encoded using VM and VCPU IDs */
105#define AVIC_VCPU_ID_BITS 8
106#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
107
108#define AVIC_VM_ID_BITS 24
109#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
110#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
111
112#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
113 (y & AVIC_VCPU_ID_MASK))
114#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
115#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
116
Joerg Roedel67ec6602010-05-17 14:43:35 +0200117static bool erratum_383_found __read_mostly;
118
Avi Kivity6c8166a2009-05-31 18:15:37 +0300119static const u32 host_save_user_msrs[] = {
120#ifdef CONFIG_X86_64
121 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
122 MSR_FS_BASE,
123#endif
124 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Paolo Bonzini46896c72015-11-12 14:49:16 +0100125 MSR_TSC_AUX,
Avi Kivity6c8166a2009-05-31 18:15:37 +0300126};
127
128#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
129
Sean Christopherson81811c12018-03-20 12:17:21 -0700130struct kvm_sev_info {
131 bool active; /* SEV enabled guest */
132 unsigned int asid; /* ASID used for this guest */
133 unsigned int handle; /* SEV firmware handle */
134 int fd; /* SEV device fd */
135 unsigned long pages_locked; /* Number of pages locked */
136 struct list_head regions_list; /* List of registered regions */
137};
138
139struct kvm_svm {
140 struct kvm kvm;
141
142 /* Struct members for AVIC */
143 u32 avic_vm_id;
Sean Christopherson81811c12018-03-20 12:17:21 -0700144 struct page *avic_logical_id_table_page;
145 struct page *avic_physical_id_table_page;
146 struct hlist_node hnode;
147
148 struct kvm_sev_info sev_info;
149};
150
Avi Kivity6c8166a2009-05-31 18:15:37 +0300151struct kvm_vcpu;
152
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200153struct nested_state {
154 struct vmcb *hsave;
155 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +0100156 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200157 u64 vmcb;
158
159 /* These are the merged vectors */
160 u32 *msrpm;
161
162 /* gpa pointers to the real vectors */
163 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100164 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200165
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200166 /* A VMEXIT is required but not yet emulated */
167 bool exit_required;
168
Joerg Roedelaad42c62009-08-07 11:49:34 +0200169 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100170 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100171 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200172 u32 intercept_exceptions;
173 u64 intercept;
174
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200175 /* Nested Paging related state */
176 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200177};
178
Joerg Roedel323c3d82010-03-01 15:34:37 +0100179#define MSRPM_OFFSETS 16
180static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
181
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500182/*
183 * Set osvw_len to higher value when updated Revision Guides
184 * are published and we know what the new status bits are
185 */
186static uint64_t osvw_len = 4, osvw_status;
187
Avi Kivity6c8166a2009-05-31 18:15:37 +0300188struct vcpu_svm {
189 struct kvm_vcpu vcpu;
190 struct vmcb *vmcb;
191 unsigned long vmcb_pa;
192 struct svm_cpu_data *svm_data;
193 uint64_t asid_generation;
194 uint64_t sysenter_esp;
195 uint64_t sysenter_eip;
Paolo Bonzini46896c72015-11-12 14:49:16 +0100196 uint64_t tsc_aux;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300197
Tom Lendackyd1d93fa2018-02-24 00:18:20 +0100198 u64 msr_decfg;
199
Avi Kivity6c8166a2009-05-31 18:15:37 +0300200 u64 next_rip;
201
202 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200203 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200204 u16 fs;
205 u16 gs;
206 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200207 u64 gs_base;
208 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300209
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100210 u64 spec_ctrl;
Thomas Gleixnerccbcd262018-05-09 23:01:01 +0200211 /*
212 * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
213 * translated into the appropriate L2_CFG bits on the host to
214 * perform speculative control.
215 */
216 u64 virt_spec_ctrl;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100217
Avi Kivity6c8166a2009-05-31 18:15:37 +0300218 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300219
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200220 ulong nmi_iret_rip;
221
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200222 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200223
224 bool nmi_singlestep;
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200225 u64 nmi_singlestep_guest_rflags;
Jan Kiszka66b71382010-02-23 17:47:56 +0100226
227 unsigned int3_injected;
228 unsigned long int3_rip;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100229
Joerg Roedel6092d3d2015-10-14 15:10:54 +0200230 /* cached guest cpuid flags for faster access */
231 bool nrips_enabled : 1;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500232
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500233 u32 ldr_reg;
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +0000234 u32 dfr_reg;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500235 struct page *avic_backing_page;
236 u64 *avic_physical_id_cache;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -0500237 bool avic_is_running;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500238
239 /*
240 * Per-vcpu list of struct amd_svm_iommu_ir:
241 * This is used mainly to store interrupt remapping information used
242 * when update the vcpu affinity. This avoids the need to scan for
243 * IRTE and try to match ga_tag in the IOMMU driver.
244 */
245 struct list_head ir_list;
246 spinlock_t ir_list_lock;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600247
248 /* which host CPU was used for running this vcpu */
249 unsigned int last_cpu;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500250};
251
252/*
253 * This is a wrapper of struct amd_iommu_ir_data.
254 */
255struct amd_svm_iommu_ir {
256 struct list_head node; /* Used by SVM for per-vcpu ir_list */
257 void *data; /* Storing pointer to struct amd_ir_data */
Avi Kivity6c8166a2009-05-31 18:15:37 +0300258};
259
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500260#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
Suthikulpanit, Suraveee44e3ea2019-03-26 03:57:37 +0000261#define AVIC_LOGICAL_ID_ENTRY_VALID_BIT 31
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500262#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
263
264#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
265#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
266#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
267#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
268
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100269static DEFINE_PER_CPU(u64, current_tsc_ratio);
270#define TSC_RATIO_DEFAULT 0x0100000000ULL
271
Joerg Roedel455716f2010-03-01 15:34:35 +0100272#define MSR_INVALID 0xffffffffU
273
Mathias Krause09941fb2012-08-30 01:30:20 +0200274static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100275 u32 index; /* Index of the MSR */
276 bool always; /* True if intercept is always on */
277} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400278 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100279 { .index = MSR_IA32_SYSENTER_CS, .always = true },
280#ifdef CONFIG_X86_64
281 { .index = MSR_GS_BASE, .always = true },
282 { .index = MSR_FS_BASE, .always = true },
283 { .index = MSR_KERNEL_GS_BASE, .always = true },
284 { .index = MSR_LSTAR, .always = true },
285 { .index = MSR_CSTAR, .always = true },
286 { .index = MSR_SYSCALL_MASK, .always = true },
287#endif
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100288 { .index = MSR_IA32_SPEC_CTRL, .always = false },
Ashok Raj15d45072018-02-01 22:59:43 +0100289 { .index = MSR_IA32_PRED_CMD, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100290 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
291 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
292 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
293 { .index = MSR_IA32_LASTINTTOIP, .always = false },
294 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295};
296
297/* enable NPT for AMD64 and X86 with PAE */
298#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
299static bool npt_enabled = true;
300#else
Joerg Roedele0231712010-02-24 18:59:10 +0100301static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302#endif
303
Babu Moger8566ac82018-03-16 16:37:26 -0400304/*
305 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
306 * pause_filter_count: On processors that support Pause filtering(indicated
307 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
308 * count value. On VMRUN this value is loaded into an internal counter.
309 * Each time a pause instruction is executed, this counter is decremented
310 * until it reaches zero at which time a #VMEXIT is generated if pause
311 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
312 * Intercept Filtering for more details.
313 * This also indicate if ple logic enabled.
314 *
315 * pause_filter_thresh: In addition, some processor families support advanced
316 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
317 * the amount of time a guest is allowed to execute in a pause loop.
318 * In this mode, a 16-bit pause filter threshold field is added in the
319 * VMCB. The threshold value is a cycle count that is used to reset the
320 * pause counter. As with simple pause filtering, VMRUN loads the pause
321 * count value from VMCB into an internal counter. Then, on each pause
322 * instruction the hardware checks the elapsed number of cycles since
323 * the most recent pause instruction against the pause filter threshold.
324 * If the elapsed cycle count is greater than the pause filter threshold,
325 * then the internal pause count is reloaded from the VMCB and execution
326 * continues. If the elapsed cycle count is less than the pause filter
327 * threshold, then the internal pause count is decremented. If the count
328 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
329 * triggered. If advanced pause filtering is supported and pause filter
330 * threshold field is set to zero, the filter will operate in the simpler,
331 * count only mode.
332 */
333
334static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
335module_param(pause_filter_thresh, ushort, 0444);
336
337static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
338module_param(pause_filter_count, ushort, 0444);
339
340/* Default doubles per-vcpu window every exit. */
341static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
342module_param(pause_filter_count_grow, ushort, 0444);
343
344/* Default resets per-vcpu window every exit to pause_filter_count. */
345static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
346module_param(pause_filter_count_shrink, ushort, 0444);
347
348/* Default is to compute the maximum so we can never overflow. */
349static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
350module_param(pause_filter_count_max, ushort, 0444);
351
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100352/* allow nested paging (virtualized MMU) for all guests */
353static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800354module_param(npt, int, S_IRUGO);
355
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100356/* allow nested virtualization in KVM/SVM */
357static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358module_param(nested, int, S_IRUGO);
359
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500360/* enable / disable AVIC */
361static int avic;
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500362#ifdef CONFIG_X86_LOCAL_APIC
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500363module_param(avic, int, S_IRUGO);
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500364#endif
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500365
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200366/* enable/disable Next RIP Save */
367static int nrips = true;
368module_param(nrips, int, 0444);
369
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -0500370/* enable/disable Virtual VMLOAD VMSAVE */
371static int vls = true;
372module_param(vls, int, 0444);
373
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500374/* enable/disable Virtual GIF */
375static int vgif = true;
376module_param(vgif, int, 0444);
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500377
Brijesh Singhe9df0942017-12-04 10:57:33 -0600378/* enable/disable SEV support */
379static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
380module_param(sev, int, 0444);
381
Paolo Bonzini6f2f8452019-05-20 15:34:35 +0200382static bool __read_mostly dump_invalid_vmcb = 0;
383module_param(dump_invalid_vmcb, bool, 0644);
384
Brijesh Singh7607b712018-02-19 10:14:44 -0600385static u8 rsm_ins_bytes[] = "\x0f\xaa";
386
Paolo Bonzini79a80592015-09-21 07:46:55 +0200387static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -0800388static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa);
Joerg Roedela5c38322009-08-07 11:49:32 +0200389static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390
Joerg Roedel410e4d52009-08-07 11:49:44 +0200391static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100392static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800393static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
395 bool has_error_code, u32 error_code);
396
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100397enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100398 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
399 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100400 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100401 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100402 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100403 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100404 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100405 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100406 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100407 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100408 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100409 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500410 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
411 * AVIC PHYSICAL_TABLE pointer,
412 * AVIC LOGICAL_TABLE pointer
413 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100414 VMCB_DIRTY_MAX,
415};
416
Joerg Roedel0574dec2010-12-03 11:45:58 +0100417/* TPR and CR2 are always written before VMRUN */
418#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100419
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500420#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
421
Tom Lendacky33af3a72019-10-03 21:17:48 +0000422static int sev_flush_asids(void);
423static DECLARE_RWSEM(sev_deactivate_lock);
Tom Lendackye3b9a9e2019-10-03 21:17:43 +0000424static DEFINE_MUTEX(sev_bitmap_lock);
Brijesh Singhed3cd232017-12-04 10:57:32 -0600425static unsigned int max_sev_asid;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600426static unsigned int min_sev_asid;
427static unsigned long *sev_asid_bitmap;
Tom Lendacky33af3a72019-10-03 21:17:48 +0000428static unsigned long *sev_reclaim_asid_bitmap;
Brijesh Singh89c50582017-12-04 10:57:35 -0600429#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
Brijesh Singh1654efc2017-12-04 10:57:34 -0600430
Brijesh Singh1e80fdc2017-12-04 10:57:38 -0600431struct enc_region {
432 struct list_head list;
433 unsigned long npages;
434 struct page **pages;
435 unsigned long uaddr;
436 unsigned long size;
437};
438
Sean Christopherson81811c12018-03-20 12:17:21 -0700439
440static inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
441{
442 return container_of(kvm, struct kvm_svm, kvm);
443}
444
Brijesh Singh1654efc2017-12-04 10:57:34 -0600445static inline bool svm_sev_enabled(void)
446{
Paolo Bonzini853c1102018-10-09 18:35:29 +0200447 return IS_ENABLED(CONFIG_KVM_AMD_SEV) ? max_sev_asid : 0;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600448}
449
450static inline bool sev_guest(struct kvm *kvm)
451{
Paolo Bonzini853c1102018-10-09 18:35:29 +0200452#ifdef CONFIG_KVM_AMD_SEV
Sean Christopherson81811c12018-03-20 12:17:21 -0700453 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600454
455 return sev->active;
Paolo Bonzini853c1102018-10-09 18:35:29 +0200456#else
457 return false;
458#endif
Brijesh Singh1654efc2017-12-04 10:57:34 -0600459}
Brijesh Singhed3cd232017-12-04 10:57:32 -0600460
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600461static inline int sev_get_asid(struct kvm *kvm)
462{
Sean Christopherson81811c12018-03-20 12:17:21 -0700463 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600464
465 return sev->asid;
466}
467
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100468static inline void mark_all_dirty(struct vmcb *vmcb)
469{
470 vmcb->control.clean = 0;
471}
472
473static inline void mark_all_clean(struct vmcb *vmcb)
474{
475 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
476 & ~VMCB_ALWAYS_DIRTY_MASK;
477}
478
479static inline void mark_dirty(struct vmcb *vmcb, int bit)
480{
481 vmcb->control.clean &= ~(1 << bit);
482}
483
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
485{
486 return container_of(vcpu, struct vcpu_svm, vcpu);
487}
488
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500489static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
490{
491 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
492 mark_dirty(svm->vmcb, VMCB_AVIC);
493}
494
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -0500495static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
496{
497 struct vcpu_svm *svm = to_svm(vcpu);
498 u64 *entry = svm->avic_physical_id_cache;
499
500 if (!entry)
501 return false;
502
503 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
504}
505
Joerg Roedel384c6362010-11-30 18:03:56 +0100506static void recalc_intercepts(struct vcpu_svm *svm)
507{
508 struct vmcb_control_area *c, *h;
509 struct nested_state *g;
510
Joerg Roedel116a0a22010-12-03 11:45:49 +0100511 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
512
Joerg Roedel384c6362010-11-30 18:03:56 +0100513 if (!is_guest_mode(&svm->vcpu))
514 return;
515
516 c = &svm->vmcb->control;
517 h = &svm->nested.hsave->control;
518 g = &svm->nested;
519
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100520 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100521 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Paolo Bonzinibd895252018-01-11 16:55:24 +0100522 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
Joerg Roedel384c6362010-11-30 18:03:56 +0100523 c->intercept = h->intercept | g->intercept;
524}
525
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100526static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
527{
528 if (is_guest_mode(&svm->vcpu))
529 return svm->nested.hsave;
530 else
531 return svm->vmcb;
532}
533
534static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
535{
536 struct vmcb *vmcb = get_host_vmcb(svm);
537
538 vmcb->control.intercept_cr |= (1U << bit);
539
540 recalc_intercepts(svm);
541}
542
543static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
544{
545 struct vmcb *vmcb = get_host_vmcb(svm);
546
547 vmcb->control.intercept_cr &= ~(1U << bit);
548
549 recalc_intercepts(svm);
550}
551
552static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
553{
554 struct vmcb *vmcb = get_host_vmcb(svm);
555
556 return vmcb->control.intercept_cr & (1U << bit);
557}
558
Paolo Bonzini5315c712014-03-03 13:08:29 +0100559static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100560{
561 struct vmcb *vmcb = get_host_vmcb(svm);
562
Paolo Bonzini5315c712014-03-03 13:08:29 +0100563 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
564 | (1 << INTERCEPT_DR1_READ)
565 | (1 << INTERCEPT_DR2_READ)
566 | (1 << INTERCEPT_DR3_READ)
567 | (1 << INTERCEPT_DR4_READ)
568 | (1 << INTERCEPT_DR5_READ)
569 | (1 << INTERCEPT_DR6_READ)
570 | (1 << INTERCEPT_DR7_READ)
571 | (1 << INTERCEPT_DR0_WRITE)
572 | (1 << INTERCEPT_DR1_WRITE)
573 | (1 << INTERCEPT_DR2_WRITE)
574 | (1 << INTERCEPT_DR3_WRITE)
575 | (1 << INTERCEPT_DR4_WRITE)
576 | (1 << INTERCEPT_DR5_WRITE)
577 | (1 << INTERCEPT_DR6_WRITE)
578 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100579
580 recalc_intercepts(svm);
581}
582
Paolo Bonzini5315c712014-03-03 13:08:29 +0100583static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100584{
585 struct vmcb *vmcb = get_host_vmcb(svm);
586
Paolo Bonzini5315c712014-03-03 13:08:29 +0100587 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100588
589 recalc_intercepts(svm);
590}
591
Joerg Roedel18c918c2010-11-30 18:03:59 +0100592static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
593{
594 struct vmcb *vmcb = get_host_vmcb(svm);
595
596 vmcb->control.intercept_exceptions |= (1U << bit);
597
598 recalc_intercepts(svm);
599}
600
601static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
602{
603 struct vmcb *vmcb = get_host_vmcb(svm);
604
605 vmcb->control.intercept_exceptions &= ~(1U << bit);
606
607 recalc_intercepts(svm);
608}
609
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100610static inline void set_intercept(struct vcpu_svm *svm, int bit)
611{
612 struct vmcb *vmcb = get_host_vmcb(svm);
613
614 vmcb->control.intercept |= (1ULL << bit);
615
616 recalc_intercepts(svm);
617}
618
619static inline void clr_intercept(struct vcpu_svm *svm, int bit)
620{
621 struct vmcb *vmcb = get_host_vmcb(svm);
622
623 vmcb->control.intercept &= ~(1ULL << bit);
624
625 recalc_intercepts(svm);
626}
627
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500628static inline bool vgif_enabled(struct vcpu_svm *svm)
629{
630 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
631}
632
Joerg Roedel2af91942009-08-07 11:49:28 +0200633static inline void enable_gif(struct vcpu_svm *svm)
634{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500635 if (vgif_enabled(svm))
636 svm->vmcb->control.int_ctl |= V_GIF_MASK;
637 else
638 svm->vcpu.arch.hflags |= HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200639}
640
641static inline void disable_gif(struct vcpu_svm *svm)
642{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500643 if (vgif_enabled(svm))
644 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
645 else
646 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200647}
648
649static inline bool gif_set(struct vcpu_svm *svm)
650{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500651 if (vgif_enabled(svm))
652 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
653 else
654 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
Joerg Roedel2af91942009-08-07 11:49:28 +0200655}
656
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657static unsigned long iopm_base;
658
659struct kvm_ldttss_desc {
660 u16 limit0;
661 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100662 unsigned base1:8, type:5, dpl:2, p:1;
663 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 u32 base3;
665 u32 zero1;
666} __attribute__((packed));
667
668struct svm_cpu_data {
669 int cpu;
670
Avi Kivity5008fdf2007-04-02 13:05:50 +0300671 u64 asid_generation;
672 u32 max_asid;
673 u32 next_asid;
Brijesh Singh4faefff2017-12-04 10:57:25 -0600674 u32 min_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675 struct kvm_ldttss_desc *tss_desc;
676
677 struct page *save_area;
Ashok Raj15d45072018-02-01 22:59:43 +0100678 struct vmcb *current_vmcb;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600679
680 /* index = sev_asid, value = vmcb pointer */
681 struct vmcb **sev_vmcbs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682};
683
684static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
685
Mathias Krause09941fb2012-08-30 01:30:20 +0200686static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200688#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800689#define MSRS_RANGE_SIZE 2048
690#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
691
Joerg Roedel455716f2010-03-01 15:34:35 +0100692static u32 svm_msrpm_offset(u32 msr)
693{
694 u32 offset;
695 int i;
696
697 for (i = 0; i < NUM_MSR_MAPS; i++) {
698 if (msr < msrpm_ranges[i] ||
699 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
700 continue;
701
702 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
703 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
704
705 /* Now we have the u8 offset - but need the u32 offset */
706 return offset / 4;
707 }
708
709 /* MSR not in any range */
710 return MSR_INVALID;
711}
712
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713#define MAX_INST_SIZE 15
714
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715static inline void clgi(void)
716{
Uros Bizjakac5ffda22018-11-26 17:00:08 +0100717 asm volatile (__ex("clgi"));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718}
719
720static inline void stgi(void)
721{
Uros Bizjakac5ffda22018-11-26 17:00:08 +0100722 asm volatile (__ex("stgi"));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723}
724
725static inline void invlpga(unsigned long addr, u32 asid)
726{
Uros Bizjakac5ffda22018-11-26 17:00:08 +0100727 asm volatile (__ex("invlpga %1, %0") : : "c"(asid), "a"(addr));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728}
729
Yu Zhang855feb62017-08-24 20:27:55 +0800730static int get_npt_level(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +0200731{
732#ifdef CONFIG_X86_64
Yu Zhang2a7266a2017-08-24 20:27:54 +0800733 return PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200734#else
735 return PT32E_ROOT_LEVEL;
736#endif
737}
738
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
740{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000741 vcpu->arch.efer = efer;
Paolo Bonzini9167ab72019-10-27 16:23:23 +0100742
743 if (!npt_enabled) {
744 /* Shadow paging assumes NX to be available. */
745 efer |= EFER_NX;
746
747 if (!(efer & EFER_LMA))
748 efer &= ~EFER_LME;
749 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800750
Alexander Graf9962d032008-11-25 20:17:02 +0100751 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100752 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753}
754
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755static int is_external_interrupt(u32 info)
756{
757 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
758 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
759}
760
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200761static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400762{
763 struct vcpu_svm *svm = to_svm(vcpu);
764 u32 ret = 0;
765
766 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200767 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
768 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400769}
770
771static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
772{
773 struct vcpu_svm *svm = to_svm(vcpu);
774
775 if (mask == 0)
776 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
777 else
778 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
779
780}
781
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200782static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400784 struct vcpu_svm *svm = to_svm(vcpu);
785
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200786 if (nrips && svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200787 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200788 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400789 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200790
Sean Christopherson1957aa62019-08-27 14:40:39 -0700791 if (!svm->next_rip) {
792 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
793 return 0;
794 } else {
795 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
796 pr_err("%s: ip 0x%lx next 0x%llx\n",
797 __func__, kvm_rip_read(vcpu), svm->next_rip);
798 kvm_rip_write(vcpu, svm->next_rip);
799 }
Glauber Costa2809f5d2009-05-12 16:21:05 -0400800 svm_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200801
Sean Christopherson60fc3d02019-08-27 14:40:38 -0700802 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803}
804
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700805static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100806{
807 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700808 unsigned nr = vcpu->arch.exception.nr;
809 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700810 bool reinject = vcpu->arch.exception.injected;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700811 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100812
Joerg Roedele0231712010-02-24 18:59:10 +0100813 /*
814 * If we are within a nested VM we'd better #VMEXIT and let the guest
815 * handle the exception
816 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200817 if (!reinject &&
818 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100819 return;
820
Jim Mattsonda998b42018-10-16 14:29:22 -0700821 kvm_deliver_exception_payload(&svm->vcpu);
822
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200823 if (nr == BP_VECTOR && !nrips) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100824 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
825
826 /*
827 * For guest debugging where we have to reinject #BP if some
828 * INT3 is guest-owned:
829 * Emulate nRIP by moving RIP forward. Will fail if injection
830 * raises a fault that is not intercepted. Still better than
831 * failing in all cases.
832 */
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200833 (void)skip_emulated_instruction(&svm->vcpu);
Jan Kiszka66b71382010-02-23 17:47:56 +0100834 rip = kvm_rip_read(&svm->vcpu);
835 svm->int3_rip = rip + svm->vmcb->save.cs.base;
836 svm->int3_injected = rip - old_rip;
837 }
838
Jan Kiszka116a4752010-02-23 17:47:54 +0100839 svm->vmcb->control.event_inj = nr
840 | SVM_EVTINJ_VALID
841 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
842 | SVM_EVTINJ_TYPE_EXEPT;
843 svm->vmcb->control.event_inj_err = error_code;
844}
845
Joerg Roedel67ec6602010-05-17 14:43:35 +0200846static void svm_init_erratum_383(void)
847{
848 u32 low, high;
849 int err;
850 u64 val;
851
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100852 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200853 return;
854
855 /* Use _safe variants to not break nested virtualization */
856 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
857 if (err)
858 return;
859
860 val |= (1ULL << 47);
861
862 low = lower_32_bits(val);
863 high = upper_32_bits(val);
864
865 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
866
867 erratum_383_found = true;
868}
869
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500870static void svm_init_osvw(struct kvm_vcpu *vcpu)
871{
872 /*
873 * Guests should see errata 400 and 415 as fixed (assuming that
874 * HLT and IO instructions are intercepted).
875 */
876 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
877 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
878
879 /*
880 * By increasing VCPU's osvw.length to 3 we are telling the guest that
881 * all osvw.status bits inside that length, including bit 0 (which is
882 * reserved for erratum 298), are valid. However, if host processor's
883 * osvw_len is 0 then osvw_status[0] carries no information. We need to
884 * be conservative here and therefore we tell the guest that erratum 298
885 * is present (because we really don't know).
886 */
887 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
888 vcpu->arch.osvw.status |= 1;
889}
890
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891static int has_svm(void)
892{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200893 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894
Eduardo Habkost63d11422008-11-17 19:03:20 -0200895 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800896 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 return 0;
898 }
899
Avi Kivity6aa8b732006-12-10 02:21:36 -0800900 return 1;
901}
902
Radim Krčmář13a34e02014-08-28 15:13:03 +0200903static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100905 /* Make sure we clean up behind us */
906 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
907 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
908
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200909 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100910
911 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912}
913
Radim Krčmář13a34e02014-08-28 15:13:03 +0200914static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915{
916
Tejun Heo0fe1e002009-10-29 22:34:14 +0900917 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 struct desc_struct *gdt;
920 int me = raw_smp_processor_id();
921
Alexander Graf10474ae2009-09-15 11:37:46 +0200922 rdmsrl(MSR_EFER, efer);
923 if (efer & EFER_SVME)
924 return -EBUSY;
925
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200927 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200928 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900930 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900931 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200932 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200933 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934 }
935
Tejun Heo0fe1e002009-10-29 22:34:14 +0900936 sd->asid_generation = 1;
937 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
938 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600939 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940
Thomas Garnier45fc8752017-03-14 10:05:08 -0700941 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900942 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943
Alexander Graf9962d032008-11-25 20:17:02 +0100944 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945
Linus Torvaldsd0316552009-12-14 09:58:24 -0800946 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200947
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100948 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
949 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500950 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100951 }
952
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500953
954 /*
955 * Get OSVW bits.
956 *
957 * Note that it is possible to have a system with mixed processor
958 * revisions and therefore different OSVW bits. If bits are not the same
959 * on different processors then choose the worst case (i.e. if erratum
960 * is present on one processor and not on another then assume that the
961 * erratum is present everywhere).
962 */
963 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
964 uint64_t len, status = 0;
965 int err;
966
967 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
968 if (!err)
969 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
970 &err);
971
972 if (err)
973 osvw_status = osvw_len = 0;
974 else {
975 if (len < osvw_len)
976 osvw_len = len;
977 osvw_status |= status;
978 osvw_status &= (1ULL << osvw_len) - 1;
979 }
980 } else
981 osvw_status = osvw_len = 0;
982
Joerg Roedel67ec6602010-05-17 14:43:35 +0200983 svm_init_erratum_383();
984
Joerg Roedel1018faa2012-02-29 14:57:32 +0100985 amd_pmu_enable_virt();
986
Alexander Graf10474ae2009-09-15 11:37:46 +0200987 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988}
989
Joerg Roedel0da1db752008-07-02 16:02:11 +0200990static void svm_cpu_uninit(int cpu)
991{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900992 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200993
Tejun Heo0fe1e002009-10-29 22:34:14 +0900994 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200995 return;
996
997 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600998 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900999 __free_page(sd->save_area);
1000 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +02001001}
1002
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003static int svm_cpu_init(int cpu)
1004{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001005 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 int r;
1007
Tejun Heo0fe1e002009-10-29 22:34:14 +09001008 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
1009 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +09001011 sd->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 r = -ENOMEM;
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001013 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +09001014 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 goto err_1;
1016
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001017 if (svm_sev_enabled()) {
1018 r = -ENOMEM;
Kees Cook6da2ec52018-06-12 13:55:00 -07001019 sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1,
1020 sizeof(void *),
1021 GFP_KERNEL);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001022 if (!sd->sev_vmcbs)
1023 goto err_1;
1024 }
1025
Tejun Heo0fe1e002009-10-29 22:34:14 +09001026 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027
1028 return 0;
1029
1030err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +09001031 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032 return r;
1033
1034}
1035
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001036static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037{
1038 int i;
1039
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001040 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
1041 if (direct_access_msrs[i].index == index)
1042 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001044 return false;
1045}
1046
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001047static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr)
1048{
1049 u8 bit_write;
1050 unsigned long tmp;
1051 u32 offset;
1052 u32 *msrpm;
1053
1054 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
1055 to_svm(vcpu)->msrpm;
1056
1057 offset = svm_msrpm_offset(msr);
1058 bit_write = 2 * (msr & 0x0f) + 1;
1059 tmp = msrpm[offset];
1060
1061 BUG_ON(offset == MSR_INVALID);
1062
1063 return !!test_bit(bit_write, &tmp);
1064}
1065
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066static void set_msr_interception(u32 *msrpm, unsigned msr,
1067 int read, int write)
1068{
Joerg Roedel455716f2010-03-01 15:34:35 +01001069 u8 bit_read, bit_write;
1070 unsigned long tmp;
1071 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001073 /*
1074 * If this warning triggers extend the direct_access_msrs list at the
1075 * beginning of the file
1076 */
1077 WARN_ON(!valid_msr_intercept(msr));
1078
Joerg Roedel455716f2010-03-01 15:34:35 +01001079 offset = svm_msrpm_offset(msr);
1080 bit_read = 2 * (msr & 0x0f);
1081 bit_write = 2 * (msr & 0x0f) + 1;
1082 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083
Joerg Roedel455716f2010-03-01 15:34:35 +01001084 BUG_ON(offset == MSR_INVALID);
1085
1086 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
1087 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
1088
1089 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090}
1091
Joerg Roedelf65c2292008-02-13 18:58:46 +01001092static void svm_vcpu_init_msrpm(u32 *msrpm)
1093{
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001094 int i;
1095
Joerg Roedelf65c2292008-02-13 18:58:46 +01001096 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
1097
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001098 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1099 if (!direct_access_msrs[i].always)
1100 continue;
1101
1102 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
1103 }
Joerg Roedelf65c2292008-02-13 18:58:46 +01001104}
1105
Joerg Roedel323c3d82010-03-01 15:34:37 +01001106static void add_msr_offset(u32 offset)
1107{
1108 int i;
1109
1110 for (i = 0; i < MSRPM_OFFSETS; ++i) {
1111
1112 /* Offset already in list? */
1113 if (msrpm_offsets[i] == offset)
1114 return;
1115
1116 /* Slot used by another offset? */
1117 if (msrpm_offsets[i] != MSR_INVALID)
1118 continue;
1119
1120 /* Add offset to list */
1121 msrpm_offsets[i] = offset;
1122
1123 return;
1124 }
1125
1126 /*
1127 * If this BUG triggers the msrpm_offsets table has an overflow. Just
1128 * increase MSRPM_OFFSETS in this case.
1129 */
1130 BUG();
1131}
1132
1133static void init_msrpm_offsets(void)
1134{
1135 int i;
1136
1137 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
1138
1139 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1140 u32 offset;
1141
1142 offset = svm_msrpm_offset(direct_access_msrs[i].index);
1143 BUG_ON(offset == MSR_INVALID);
1144
1145 add_msr_offset(offset);
1146 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147}
1148
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001149static void svm_enable_lbrv(struct vcpu_svm *svm)
1150{
1151 u32 *msrpm = svm->msrpm;
1152
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001153 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001154 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
1155 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
1156 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
1157 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
1158}
1159
1160static void svm_disable_lbrv(struct vcpu_svm *svm)
1161{
1162 u32 *msrpm = svm->msrpm;
1163
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001164 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001165 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
1166 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
1167 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
1168 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
1169}
1170
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001171static void disable_nmi_singlestep(struct vcpu_svm *svm)
1172{
1173 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001174
Ladi Prosekab2f4d732017-06-21 09:06:58 +02001175 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
1176 /* Clear our flags if they were not set by the guest */
1177 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1178 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
1179 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1180 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
1181 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001182}
1183
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001184/* Note:
Sean Christopherson81811c12018-03-20 12:17:21 -07001185 * This hash table is used to map VM_ID to a struct kvm_svm,
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001186 * when handling AMD IOMMU GALOG notification to schedule in
1187 * a particular vCPU.
1188 */
1189#define SVM_VM_DATA_HASH_BITS 8
David Hildenbrand681bcea2017-01-24 22:21:16 +01001190static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001191static u32 next_vm_id = 0;
1192static bool next_vm_id_wrapped = 0;
David Hildenbrand681bcea2017-01-24 22:21:16 +01001193static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001194
1195/* Note:
1196 * This function is called from IOMMU driver to notify
1197 * SVM to schedule in a particular vCPU of a particular VM.
1198 */
1199static int avic_ga_log_notifier(u32 ga_tag)
1200{
1201 unsigned long flags;
Sean Christopherson81811c12018-03-20 12:17:21 -07001202 struct kvm_svm *kvm_svm;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001203 struct kvm_vcpu *vcpu = NULL;
1204 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
1205 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
1206
1207 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
1208
1209 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Sean Christopherson81811c12018-03-20 12:17:21 -07001210 hash_for_each_possible(svm_vm_data_hash, kvm_svm, hnode, vm_id) {
1211 if (kvm_svm->avic_vm_id != vm_id)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001212 continue;
Sean Christopherson81811c12018-03-20 12:17:21 -07001213 vcpu = kvm_get_vcpu_by_id(&kvm_svm->kvm, vcpu_id);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001214 break;
1215 }
1216 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1217
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001218 /* Note:
1219 * At this point, the IOMMU should have already set the pending
1220 * bit in the vAPIC backing page. So, we just need to schedule
1221 * in the vcpu.
1222 */
Paolo Bonzini1cf53582017-10-10 12:51:56 +02001223 if (vcpu)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001224 kvm_vcpu_wake_up(vcpu);
1225
1226 return 0;
1227}
1228
Brijesh Singhe9df0942017-12-04 10:57:33 -06001229static __init int sev_hardware_setup(void)
1230{
1231 struct sev_user_data_status *status;
1232 int rc;
1233
1234 /* Maximum number of encrypted guests supported simultaneously */
1235 max_sev_asid = cpuid_ecx(0x8000001F);
1236
1237 if (!max_sev_asid)
1238 return 1;
1239
Brijesh Singh1654efc2017-12-04 10:57:34 -06001240 /* Minimum ASID value that should be used for SEV guest */
1241 min_sev_asid = cpuid_edx(0x8000001F);
1242
Tom Lendacky33af3a72019-10-03 21:17:48 +00001243 /* Initialize SEV ASID bitmaps */
Andy Shevchenkoa101c9d632018-08-30 14:49:59 +03001244 sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001245 if (!sev_asid_bitmap)
1246 return 1;
1247
Tom Lendacky33af3a72019-10-03 21:17:48 +00001248 sev_reclaim_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
1249 if (!sev_reclaim_asid_bitmap)
1250 return 1;
1251
Brijesh Singhe9df0942017-12-04 10:57:33 -06001252 status = kmalloc(sizeof(*status), GFP_KERNEL);
1253 if (!status)
1254 return 1;
1255
1256 /*
1257 * Check SEV platform status.
1258 *
1259 * PLATFORM_STATUS can be called in any state, if we failed to query
1260 * the PLATFORM status then either PSP firmware does not support SEV
1261 * feature or SEV firmware is dead.
1262 */
1263 rc = sev_platform_status(status, NULL);
1264 if (rc)
1265 goto err;
1266
1267 pr_info("SEV supported\n");
1268
1269err:
1270 kfree(status);
1271 return rc;
1272}
1273
Babu Moger8566ac82018-03-16 16:37:26 -04001274static void grow_ple_window(struct kvm_vcpu *vcpu)
1275{
1276 struct vcpu_svm *svm = to_svm(vcpu);
1277 struct vmcb_control_area *control = &svm->vmcb->control;
1278 int old = control->pause_filter_count;
1279
1280 control->pause_filter_count = __grow_ple_window(old,
1281 pause_filter_count,
1282 pause_filter_count_grow,
1283 pause_filter_count_max);
1284
Peter Xu4f75bcc2019-09-06 10:17:22 +08001285 if (control->pause_filter_count != old) {
Babu Moger8566ac82018-03-16 16:37:26 -04001286 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +08001287 trace_kvm_ple_window_update(vcpu->vcpu_id,
1288 control->pause_filter_count, old);
1289 }
Babu Moger8566ac82018-03-16 16:37:26 -04001290}
1291
1292static void shrink_ple_window(struct kvm_vcpu *vcpu)
1293{
1294 struct vcpu_svm *svm = to_svm(vcpu);
1295 struct vmcb_control_area *control = &svm->vmcb->control;
1296 int old = control->pause_filter_count;
1297
1298 control->pause_filter_count =
1299 __shrink_ple_window(old,
1300 pause_filter_count,
1301 pause_filter_count_shrink,
1302 pause_filter_count);
Peter Xu4f75bcc2019-09-06 10:17:22 +08001303 if (control->pause_filter_count != old) {
Babu Moger8566ac82018-03-16 16:37:26 -04001304 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +08001305 trace_kvm_ple_window_update(vcpu->vcpu_id,
1306 control->pause_filter_count, old);
1307 }
Babu Moger8566ac82018-03-16 16:37:26 -04001308}
1309
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310static __init int svm_hardware_setup(void)
1311{
1312 int cpu;
1313 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001314 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315 int r;
1316
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
1318
1319 if (!iopm_pages)
1320 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +03001321
1322 iopm_va = page_address(iopm_pages);
1323 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
1325
Joerg Roedel323c3d82010-03-01 15:34:37 +01001326 init_msrpm_offsets();
1327
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001328 if (boot_cpu_has(X86_FEATURE_NX))
1329 kvm_enable_efer_bits(EFER_NX);
1330
Alexander Graf1b2fd702009-02-02 16:23:51 +01001331 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
1332 kvm_enable_efer_bits(EFER_FFXSR);
1333
Joerg Roedel92a1f122011-03-25 09:44:51 +01001334 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +01001335 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +08001336 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1337 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +01001338 }
1339
Babu Moger8566ac82018-03-16 16:37:26 -04001340 /* Check for pause filtering support */
1341 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
1342 pause_filter_count = 0;
1343 pause_filter_thresh = 0;
1344 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
1345 pause_filter_thresh = 0;
1346 }
1347
Alexander Graf236de052008-11-25 20:17:10 +01001348 if (nested) {
1349 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +02001350 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +01001351 }
1352
Brijesh Singhe9df0942017-12-04 10:57:33 -06001353 if (sev) {
1354 if (boot_cpu_has(X86_FEATURE_SEV) &&
1355 IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
1356 r = sev_hardware_setup();
1357 if (r)
1358 sev = false;
1359 } else {
1360 sev = false;
1361 }
1362 }
1363
Zachary Amsden3230bb42009-09-29 11:38:37 -10001364 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365 r = svm_cpu_init(cpu);
1366 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +01001367 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +01001369
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001370 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001371 npt_enabled = false;
1372
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001373 if (npt_enabled && !npt) {
1374 printk(KERN_INFO "kvm: Nested Paging disabled\n");
1375 npt_enabled = false;
1376 }
1377
Joerg Roedel18552672008-02-07 13:47:41 +01001378 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001379 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +01001380 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +02001381 } else
1382 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001383
Paolo Bonzinid647eb62019-06-20 14:13:33 +02001384 if (nrips) {
1385 if (!boot_cpu_has(X86_FEATURE_NRIPS))
1386 nrips = false;
1387 }
1388
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001389 if (avic) {
1390 if (!npt_enabled ||
1391 !boot_cpu_has(X86_FEATURE_AVIC) ||
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001392 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001393 avic = false;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001394 } else {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001395 pr_info("AVIC enabled\n");
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001396
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001397 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1398 }
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001399 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001400
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001401 if (vls) {
1402 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001403 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001404 !IS_ENABLED(CONFIG_X86_64)) {
1405 vls = false;
1406 } else {
1407 pr_info("Virtual VMLOAD VMSAVE supported\n");
1408 }
1409 }
1410
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001411 if (vgif) {
1412 if (!boot_cpu_has(X86_FEATURE_VGIF))
1413 vgif = false;
1414 else
1415 pr_info("Virtual GIF supported\n");
1416 }
1417
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418 return 0;
1419
Joerg Roedelf65c2292008-02-13 18:58:46 +01001420err:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1422 iopm_base = 0;
1423 return r;
1424}
1425
1426static __exit void svm_hardware_unsetup(void)
1427{
Joerg Roedel0da1db752008-07-02 16:02:11 +02001428 int cpu;
1429
Tom Lendacky33af3a72019-10-03 21:17:48 +00001430 if (svm_sev_enabled()) {
Andy Shevchenkoa101c9d632018-08-30 14:49:59 +03001431 bitmap_free(sev_asid_bitmap);
Tom Lendacky33af3a72019-10-03 21:17:48 +00001432 bitmap_free(sev_reclaim_asid_bitmap);
1433
1434 sev_flush_asids();
1435 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001436
Zachary Amsden3230bb42009-09-29 11:38:37 -10001437 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +02001438 svm_cpu_uninit(cpu);
1439
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001441 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442}
1443
1444static void init_seg(struct vmcb_seg *seg)
1445{
1446 seg->selector = 0;
1447 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001448 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449 seg->limit = 0xffff;
1450 seg->base = 0;
1451}
1452
1453static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1454{
1455 seg->selector = 0;
1456 seg->attrib = SVM_SELECTOR_P_MASK | type;
1457 seg->limit = 0xffff;
1458 seg->base = 0;
1459}
1460
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001461static u64 svm_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
1462{
1463 struct vcpu_svm *svm = to_svm(vcpu);
1464
1465 if (is_guest_mode(vcpu))
1466 return svm->nested.hsave->control.tsc_offset;
1467
1468 return vcpu->arch.tsc_offset;
1469}
1470
Leonid Shatz326e7422018-11-06 12:14:25 +02001471static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001472{
1473 struct vcpu_svm *svm = to_svm(vcpu);
1474 u64 g_tsc_offset = 0;
1475
Joerg Roedel20307532010-11-29 17:51:48 +01001476 if (is_guest_mode(vcpu)) {
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001477 /* Write L1's TSC offset. */
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001478 g_tsc_offset = svm->vmcb->control.tsc_offset -
1479 svm->nested.hsave->control.tsc_offset;
1480 svm->nested.hsave->control.tsc_offset = offset;
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001481 }
1482
1483 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1484 svm->vmcb->control.tsc_offset - g_tsc_offset,
1485 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001486
1487 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001488
1489 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Leonid Shatz326e7422018-11-06 12:14:25 +02001490 return svm->vmcb->control.tsc_offset;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001491}
1492
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001493static void avic_init_vmcb(struct vcpu_svm *svm)
1494{
1495 struct vmcb *vmcb = svm->vmcb;
Sean Christopherson81811c12018-03-20 12:17:21 -07001496 struct kvm_svm *kvm_svm = to_kvm_svm(svm->vcpu.kvm);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001497 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
Sean Christopherson81811c12018-03-20 12:17:21 -07001498 phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page));
1499 phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page));
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001500
1501 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1502 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1503 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1504 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1505 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001506}
1507
Paolo Bonzini56908912015-10-19 11:30:19 +02001508static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509{
Joerg Roedele6101a92008-02-13 18:58:45 +01001510 struct vmcb_control_area *control = &svm->vmcb->control;
1511 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001513 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001514
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001515 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1516 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1517 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1518 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1519 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1520 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001521 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1522 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001523
Paolo Bonzini5315c712014-03-03 13:08:29 +01001524 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525
Joerg Roedel18c918c2010-11-30 18:03:59 +01001526 set_exception_intercept(svm, PF_VECTOR);
1527 set_exception_intercept(svm, UD_VECTOR);
1528 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001529 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001530 set_exception_intercept(svm, DB_VECTOR);
Liran Alon97184202018-03-12 13:12:52 +02001531 /*
1532 * Guest access to VMware backdoor ports could legitimately
1533 * trigger #GP because of TSS I/O permission bitmap.
1534 * We intercept those #GP and allow access to them anyway
1535 * as VMware does.
1536 */
1537 if (enable_vmware_backdoor)
1538 set_exception_intercept(svm, GP_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001539
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001540 set_intercept(svm, INTERCEPT_INTR);
1541 set_intercept(svm, INTERCEPT_NMI);
1542 set_intercept(svm, INTERCEPT_SMI);
1543 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001544 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001545 set_intercept(svm, INTERCEPT_CPUID);
1546 set_intercept(svm, INTERCEPT_INVD);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001547 set_intercept(svm, INTERCEPT_INVLPG);
1548 set_intercept(svm, INTERCEPT_INVLPGA);
1549 set_intercept(svm, INTERCEPT_IOIO_PROT);
1550 set_intercept(svm, INTERCEPT_MSR_PROT);
1551 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1552 set_intercept(svm, INTERCEPT_SHUTDOWN);
1553 set_intercept(svm, INTERCEPT_VMRUN);
1554 set_intercept(svm, INTERCEPT_VMMCALL);
1555 set_intercept(svm, INTERCEPT_VMLOAD);
1556 set_intercept(svm, INTERCEPT_VMSAVE);
1557 set_intercept(svm, INTERCEPT_STGI);
1558 set_intercept(svm, INTERCEPT_CLGI);
1559 set_intercept(svm, INTERCEPT_SKINIT);
1560 set_intercept(svm, INTERCEPT_WBINVD);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001561 set_intercept(svm, INTERCEPT_XSETBV);
Jim Mattson0cb84102019-09-19 15:59:17 -07001562 set_intercept(svm, INTERCEPT_RDPRU);
Brijesh Singh7607b712018-02-19 10:14:44 -06001563 set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001564
Wanpeng Li4d5422c2018-03-12 04:53:02 -07001565 if (!kvm_mwait_in_guest(svm->vcpu.kvm)) {
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001566 set_intercept(svm, INTERCEPT_MONITOR);
1567 set_intercept(svm, INTERCEPT_MWAIT);
1568 }
1569
Wanpeng Licaa057a2018-03-12 04:53:03 -07001570 if (!kvm_hlt_in_guest(svm->vcpu.kvm))
1571 set_intercept(svm, INTERCEPT_HLT);
1572
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001573 control->iopm_base_pa = __sme_set(iopm_base);
1574 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575 control->int_ctl = V_INTR_MASKING_MASK;
1576
1577 init_seg(&save->es);
1578 init_seg(&save->ss);
1579 init_seg(&save->ds);
1580 init_seg(&save->fs);
1581 init_seg(&save->gs);
1582
1583 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001584 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585 /* Executable/Readable Code Segment */
1586 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1587 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1588 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589
1590 save->gdtr.limit = 0xffff;
1591 save->idtr.limit = 0xffff;
1592
1593 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1594 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1595
Paolo Bonzini56908912015-10-19 11:30:19 +02001596 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001597 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001598 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001600 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601
Joerg Roedele0231712010-02-24 18:59:10 +01001602 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001603 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001604 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001606 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001607 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001608
Rusty Russell66aee912007-07-17 23:34:16 +10001609 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001611
1612 if (npt_enabled) {
1613 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001614 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001615 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001616 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001617 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1618 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001619 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001620 save->cr3 = 0;
1621 save->cr4 = 0;
1622 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001623 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001624
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001625 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001626 svm->vcpu.arch.hflags = 0;
1627
Babu Moger8566ac82018-03-16 16:37:26 -04001628 if (pause_filter_count) {
1629 control->pause_filter_count = pause_filter_count;
1630 if (pause_filter_thresh)
1631 control->pause_filter_thresh = pause_filter_thresh;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001632 set_intercept(svm, INTERCEPT_PAUSE);
Babu Moger8566ac82018-03-16 16:37:26 -04001633 } else {
1634 clr_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001635 }
1636
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001637 if (kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001638 avic_init_vmcb(svm);
1639
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001640 /*
1641 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1642 * in VMCB and clear intercepts to avoid #VMEXIT.
1643 */
1644 if (vls) {
1645 clr_intercept(svm, INTERCEPT_VMLOAD);
1646 clr_intercept(svm, INTERCEPT_VMSAVE);
1647 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1648 }
1649
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001650 if (vgif) {
1651 clr_intercept(svm, INTERCEPT_STGI);
1652 clr_intercept(svm, INTERCEPT_CLGI);
1653 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1654 }
1655
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001656 if (sev_guest(svm->vcpu.kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001657 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001658 clr_exception_intercept(svm, UD_VECTOR);
1659 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001660
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001661 mark_all_dirty(svm->vmcb);
1662
Joerg Roedel2af91942009-08-07 11:49:28 +02001663 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001664
1665}
1666
Dan Carpenterd3e7dec2017-05-18 10:38:53 +03001667static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1668 unsigned int index)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001669{
1670 u64 *avic_physical_id_table;
Sean Christopherson81811c12018-03-20 12:17:21 -07001671 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001672
1673 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1674 return NULL;
1675
Sean Christopherson81811c12018-03-20 12:17:21 -07001676 avic_physical_id_table = page_address(kvm_svm->avic_physical_id_table_page);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001677
1678 return &avic_physical_id_table[index];
1679}
1680
1681/**
1682 * Note:
1683 * AVIC hardware walks the nested page table to check permissions,
1684 * but does not use the SPA address specified in the leaf page
1685 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1686 * field of the VMCB. Therefore, we set up the
1687 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1688 */
1689static int avic_init_access_page(struct kvm_vcpu *vcpu)
1690{
1691 struct kvm *kvm = vcpu->kvm;
Wei Wang30510382018-11-12 12:23:14 +00001692 int ret = 0;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001693
Wei Wang30510382018-11-12 12:23:14 +00001694 mutex_lock(&kvm->slots_lock);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001695 if (kvm->arch.apic_access_page_done)
Wei Wang30510382018-11-12 12:23:14 +00001696 goto out;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001697
Wei Wang30510382018-11-12 12:23:14 +00001698 ret = __x86_set_memory_region(kvm,
1699 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1700 APIC_DEFAULT_PHYS_BASE,
1701 PAGE_SIZE);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001702 if (ret)
Wei Wang30510382018-11-12 12:23:14 +00001703 goto out;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001704
1705 kvm->arch.apic_access_page_done = true;
Wei Wang30510382018-11-12 12:23:14 +00001706out:
1707 mutex_unlock(&kvm->slots_lock);
1708 return ret;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001709}
1710
1711static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1712{
1713 int ret;
1714 u64 *entry, new_entry;
1715 int id = vcpu->vcpu_id;
1716 struct vcpu_svm *svm = to_svm(vcpu);
1717
1718 ret = avic_init_access_page(vcpu);
1719 if (ret)
1720 return ret;
1721
1722 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1723 return -EINVAL;
1724
1725 if (!svm->vcpu.arch.apic->regs)
1726 return -EINVAL;
1727
1728 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1729
1730 /* Setting AVIC backing page address in the phy APIC ID table */
1731 entry = avic_get_physical_id_entry(vcpu, id);
1732 if (!entry)
1733 return -EINVAL;
1734
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001735 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
1736 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1737 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001738 WRITE_ONCE(*entry, new_entry);
1739
1740 svm->avic_physical_id_cache = entry;
1741
1742 return 0;
1743}
1744
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00001745static void sev_asid_free(int asid)
Brijesh Singh1654efc2017-12-04 10:57:34 -06001746{
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001747 struct svm_cpu_data *sd;
1748 int cpu, pos;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001749
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00001750 mutex_lock(&sev_bitmap_lock);
1751
Brijesh Singh1654efc2017-12-04 10:57:34 -06001752 pos = asid - 1;
Tom Lendacky33af3a72019-10-03 21:17:48 +00001753 __set_bit(pos, sev_reclaim_asid_bitmap);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001754
1755 for_each_possible_cpu(cpu) {
1756 sd = per_cpu(svm_data, cpu);
1757 sd->sev_vmcbs[pos] = NULL;
1758 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001759
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00001760 mutex_unlock(&sev_bitmap_lock);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001761}
1762
Brijesh Singh59414c92017-12-04 10:57:35 -06001763static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
1764{
1765 struct sev_data_decommission *decommission;
1766 struct sev_data_deactivate *data;
1767
1768 if (!handle)
1769 return;
1770
1771 data = kzalloc(sizeof(*data), GFP_KERNEL);
1772 if (!data)
1773 return;
1774
1775 /* deactivate handle */
1776 data->handle = handle;
Tom Lendacky83af5e62019-10-03 21:17:45 +00001777
Tom Lendacky33af3a72019-10-03 21:17:48 +00001778 /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */
1779 down_read(&sev_deactivate_lock);
Brijesh Singh59414c92017-12-04 10:57:35 -06001780 sev_guest_deactivate(data, NULL);
Tom Lendacky33af3a72019-10-03 21:17:48 +00001781 up_read(&sev_deactivate_lock);
Tom Lendacky83af5e62019-10-03 21:17:45 +00001782
Brijesh Singh59414c92017-12-04 10:57:35 -06001783 kfree(data);
1784
1785 decommission = kzalloc(sizeof(*decommission), GFP_KERNEL);
1786 if (!decommission)
1787 return;
1788
1789 /* decommission handle */
1790 decommission->handle = handle;
1791 sev_guest_decommission(decommission, NULL);
1792
1793 kfree(decommission);
1794}
1795
Brijesh Singh89c50582017-12-04 10:57:35 -06001796static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
1797 unsigned long ulen, unsigned long *n,
1798 int write)
1799{
Sean Christopherson81811c12018-03-20 12:17:21 -07001800 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06001801 unsigned long npages, npinned, size;
1802 unsigned long locked, lock_limit;
1803 struct page **pages;
Dan Carpenter86bf20c2018-05-19 09:01:36 +03001804 unsigned long first, last;
1805
1806 if (ulen == 0 || uaddr + ulen < uaddr)
1807 return NULL;
Brijesh Singh89c50582017-12-04 10:57:35 -06001808
1809 /* Calculate number of pages. */
1810 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
1811 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
1812 npages = (last - first + 1);
1813
1814 locked = sev->pages_locked + npages;
1815 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1816 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
1817 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
1818 return NULL;
1819 }
1820
1821 /* Avoid using vmalloc for smaller buffers. */
1822 size = npages * sizeof(struct page *);
1823 if (size > PAGE_SIZE)
Ben Gardon1ec69642019-02-11 11:02:51 -08001824 pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO,
1825 PAGE_KERNEL);
Brijesh Singh89c50582017-12-04 10:57:35 -06001826 else
Ben Gardon1ec69642019-02-11 11:02:51 -08001827 pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
Brijesh Singh89c50582017-12-04 10:57:35 -06001828
1829 if (!pages)
1830 return NULL;
1831
1832 /* Pin the user virtual address. */
Ira Weiny73b01402019-05-13 17:17:11 -07001833 npinned = get_user_pages_fast(uaddr, npages, FOLL_WRITE, pages);
Brijesh Singh89c50582017-12-04 10:57:35 -06001834 if (npinned != npages) {
1835 pr_err("SEV: Failure locking %lu pages.\n", npages);
1836 goto err;
1837 }
1838
1839 *n = npages;
1840 sev->pages_locked = locked;
1841
1842 return pages;
1843
1844err:
1845 if (npinned > 0)
1846 release_pages(pages, npinned);
1847
1848 kvfree(pages);
1849 return NULL;
1850}
1851
1852static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
1853 unsigned long npages)
1854{
Sean Christopherson81811c12018-03-20 12:17:21 -07001855 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06001856
1857 release_pages(pages, npages);
1858 kvfree(pages);
1859 sev->pages_locked -= npages;
1860}
1861
1862static void sev_clflush_pages(struct page *pages[], unsigned long npages)
1863{
1864 uint8_t *page_virtual;
1865 unsigned long i;
1866
1867 if (npages == 0 || pages == NULL)
1868 return;
1869
1870 for (i = 0; i < npages; i++) {
1871 page_virtual = kmap_atomic(pages[i]);
1872 clflush_cache_range(page_virtual, PAGE_SIZE);
1873 kunmap_atomic(page_virtual);
1874 }
1875}
1876
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001877static void __unregister_enc_region_locked(struct kvm *kvm,
1878 struct enc_region *region)
1879{
1880 /*
1881 * The guest may change the memory encryption attribute from C=0 -> C=1
1882 * or vice versa for this memory range. Lets make sure caches are
1883 * flushed to ensure that guest data gets written into memory with
1884 * correct C-bit.
1885 */
1886 sev_clflush_pages(region->pages, region->npages);
1887
1888 sev_unpin_memory(kvm, region->pages, region->npages);
1889 list_del(&region->list);
1890 kfree(region);
1891}
1892
Sean Christopherson434a1e92018-03-20 12:17:18 -07001893static struct kvm *svm_vm_alloc(void)
1894{
Ben Gardon1ec69642019-02-11 11:02:51 -08001895 struct kvm_svm *kvm_svm = __vmalloc(sizeof(struct kvm_svm),
1896 GFP_KERNEL_ACCOUNT | __GFP_ZERO,
1897 PAGE_KERNEL);
Sean Christopherson81811c12018-03-20 12:17:21 -07001898 return &kvm_svm->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07001899}
1900
1901static void svm_vm_free(struct kvm *kvm)
1902{
Marc Orrd1e5b0e2018-05-15 04:37:37 -07001903 vfree(to_kvm_svm(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07001904}
1905
Brijesh Singh1654efc2017-12-04 10:57:34 -06001906static void sev_vm_destroy(struct kvm *kvm)
1907{
Sean Christopherson81811c12018-03-20 12:17:21 -07001908 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001909 struct list_head *head = &sev->regions_list;
1910 struct list_head *pos, *q;
Brijesh Singh59414c92017-12-04 10:57:35 -06001911
Brijesh Singh1654efc2017-12-04 10:57:34 -06001912 if (!sev_guest(kvm))
1913 return;
1914
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001915 mutex_lock(&kvm->lock);
1916
1917 /*
1918 * if userspace was terminated before unregistering the memory regions
1919 * then lets unpin all the registered memory.
1920 */
1921 if (!list_empty(head)) {
1922 list_for_each_safe(pos, q, head) {
1923 __unregister_enc_region_locked(kvm,
1924 list_entry(pos, struct enc_region, list));
1925 }
1926 }
1927
1928 mutex_unlock(&kvm->lock);
1929
Brijesh Singh59414c92017-12-04 10:57:35 -06001930 sev_unbind_asid(kvm, sev->handle);
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00001931 sev_asid_free(sev->asid);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001932}
1933
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001934static void avic_vm_destroy(struct kvm *kvm)
1935{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001936 unsigned long flags;
Sean Christopherson81811c12018-03-20 12:17:21 -07001937 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001938
Dmitry Vyukov3863dff2017-01-24 14:06:48 +01001939 if (!avic)
1940 return;
1941
Sean Christopherson81811c12018-03-20 12:17:21 -07001942 if (kvm_svm->avic_logical_id_table_page)
1943 __free_page(kvm_svm->avic_logical_id_table_page);
1944 if (kvm_svm->avic_physical_id_table_page)
1945 __free_page(kvm_svm->avic_physical_id_table_page);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001946
1947 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Sean Christopherson81811c12018-03-20 12:17:21 -07001948 hash_del(&kvm_svm->hnode);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001949 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001950}
1951
Brijesh Singh1654efc2017-12-04 10:57:34 -06001952static void svm_vm_destroy(struct kvm *kvm)
1953{
1954 avic_vm_destroy(kvm);
1955 sev_vm_destroy(kvm);
1956}
1957
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001958static int avic_vm_init(struct kvm *kvm)
1959{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001960 unsigned long flags;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001961 int err = -ENOMEM;
Sean Christopherson81811c12018-03-20 12:17:21 -07001962 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
1963 struct kvm_svm *k2;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001964 struct page *p_page;
1965 struct page *l_page;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001966 u32 vm_id;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001967
1968 if (!avic)
1969 return 0;
1970
1971 /* Allocating physical APIC ID table (4KB) */
Ben Gardon1ec69642019-02-11 11:02:51 -08001972 p_page = alloc_page(GFP_KERNEL_ACCOUNT);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001973 if (!p_page)
1974 goto free_avic;
1975
Sean Christopherson81811c12018-03-20 12:17:21 -07001976 kvm_svm->avic_physical_id_table_page = p_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001977 clear_page(page_address(p_page));
1978
1979 /* Allocating logical APIC ID table (4KB) */
Ben Gardon1ec69642019-02-11 11:02:51 -08001980 l_page = alloc_page(GFP_KERNEL_ACCOUNT);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001981 if (!l_page)
1982 goto free_avic;
1983
Sean Christopherson81811c12018-03-20 12:17:21 -07001984 kvm_svm->avic_logical_id_table_page = l_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001985 clear_page(page_address(l_page));
1986
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001987 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001988 again:
1989 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
1990 if (vm_id == 0) { /* id is 1-based, zero is not okay */
1991 next_vm_id_wrapped = 1;
1992 goto again;
1993 }
1994 /* Is it still in use? Only possible if wrapped at least once */
1995 if (next_vm_id_wrapped) {
Sean Christopherson81811c12018-03-20 12:17:21 -07001996 hash_for_each_possible(svm_vm_data_hash, k2, hnode, vm_id) {
1997 if (k2->avic_vm_id == vm_id)
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001998 goto again;
1999 }
2000 }
Sean Christopherson81811c12018-03-20 12:17:21 -07002001 kvm_svm->avic_vm_id = vm_id;
2002 hash_add(svm_vm_data_hash, &kvm_svm->hnode, kvm_svm->avic_vm_id);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05002003 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
2004
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002005 return 0;
2006
2007free_avic:
2008 avic_vm_destroy(kvm);
2009 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002010}
2011
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002012static inline int
2013avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002014{
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002015 int ret = 0;
2016 unsigned long flags;
2017 struct amd_svm_iommu_ir *ir;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002018 struct vcpu_svm *svm = to_svm(vcpu);
2019
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002020 if (!kvm_arch_has_assigned_device(vcpu->kvm))
2021 return 0;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002022
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002023 /*
2024 * Here, we go through the per-vcpu ir_list to update all existing
2025 * interrupt remapping table entry targeting this vcpu.
2026 */
2027 spin_lock_irqsave(&svm->ir_list_lock, flags);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002028
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002029 if (list_empty(&svm->ir_list))
2030 goto out;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002031
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002032 list_for_each_entry(ir, &svm->ir_list, node) {
2033 ret = amd_iommu_update_ga(cpu, r, ir->data);
2034 if (ret)
2035 break;
2036 }
2037out:
2038 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
2039 return ret;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002040}
2041
2042static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2043{
2044 u64 entry;
2045 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05002046 int h_physical_id = kvm_cpu_get_apicid(cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002047 struct vcpu_svm *svm = to_svm(vcpu);
2048
2049 if (!kvm_vcpu_apicv_active(vcpu))
2050 return;
2051
Suthikulpanit, Suraveec9bcd3e2019-05-14 15:49:52 +00002052 /*
2053 * Since the host physical APIC id is 8 bits,
2054 * we can support host APIC ID upto 255.
2055 */
2056 if (WARN_ON(h_physical_id > AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK))
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002057 return;
2058
2059 entry = READ_ONCE(*(svm->avic_physical_id_cache));
2060 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
2061
2062 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
2063 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
2064
2065 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2066 if (svm->avic_is_running)
2067 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2068
2069 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002070 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
2071 svm->avic_is_running);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002072}
2073
2074static void avic_vcpu_put(struct kvm_vcpu *vcpu)
2075{
2076 u64 entry;
2077 struct vcpu_svm *svm = to_svm(vcpu);
2078
2079 if (!kvm_vcpu_apicv_active(vcpu))
2080 return;
2081
2082 entry = READ_ONCE(*(svm->avic_physical_id_cache));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002083 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
2084 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
2085
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002086 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2087 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088}
2089
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002090/**
2091 * This function is called during VCPU halt/unhalt.
2092 */
2093static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
2094{
2095 struct vcpu_svm *svm = to_svm(vcpu);
2096
2097 svm->avic_is_running = is_run;
2098 if (is_run)
2099 avic_vcpu_load(vcpu, vcpu->cpu);
2100 else
2101 avic_vcpu_put(vcpu);
2102}
2103
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002104static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03002105{
2106 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01002107 u32 dummy;
2108 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002109
Wanpeng Li518e7b92018-02-28 14:03:31 +08002110 vcpu->arch.microcode_version = 0x01000065;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002111 svm->spec_ctrl = 0;
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02002112 svm->virt_spec_ctrl = 0;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002113
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002114 if (!init_event) {
2115 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
2116 MSR_IA32_APICBASE_ENABLE;
2117 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
2118 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
2119 }
Paolo Bonzini56908912015-10-19 11:30:19 +02002120 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02002121
Yu Zhange911eb32017-08-24 20:27:52 +08002122 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
Sean Christophersonde3cd112019-04-30 10:36:17 -07002123 kvm_rdx_write(vcpu, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002124
2125 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
2126 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03002127}
2128
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002129static int avic_init_vcpu(struct vcpu_svm *svm)
2130{
2131 int ret;
2132
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05002133 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002134 return 0;
2135
2136 ret = avic_init_backing_page(&svm->vcpu);
2137 if (ret)
2138 return ret;
2139
2140 INIT_LIST_HEAD(&svm->ir_list);
2141 spin_lock_init(&svm->ir_list_lock);
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00002142 svm->dfr_reg = APIC_DFR_FLAT;
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002143
2144 return ret;
2145}
2146
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002147static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002149 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01002151 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002152 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002153 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002154 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155
Sean Christopherson12b58f42019-08-15 10:22:37 -07002156 BUILD_BUG_ON_MSG(offsetof(struct vcpu_svm, vcpu) != 0,
2157 "struct kvm_vcpu must be at offset 0 for arch usercopy region");
2158
Ben Gardon1ec69642019-02-11 11:02:51 -08002159 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002160 if (!svm) {
2161 err = -ENOMEM;
2162 goto out;
2163 }
2164
Wanpeng Lid9a710e2019-07-22 12:26:21 +08002165 svm->vcpu.arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
2166 GFP_KERNEL_ACCOUNT);
2167 if (!svm->vcpu.arch.user_fpu) {
2168 printk(KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n");
2169 err = -ENOMEM;
2170 goto free_partial_svm;
2171 }
2172
Ben Gardon1ec69642019-02-11 11:02:51 -08002173 svm->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
2174 GFP_KERNEL_ACCOUNT);
Marc Orrb666a4b2018-11-06 14:53:56 -08002175 if (!svm->vcpu.arch.guest_fpu) {
2176 printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n");
2177 err = -ENOMEM;
Wanpeng Lid9a710e2019-07-22 12:26:21 +08002178 goto free_user_fpu;
Marc Orrb666a4b2018-11-06 14:53:56 -08002179 }
2180
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002181 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
2182 if (err)
2183 goto free_svm;
2184
Joerg Roedelf65c2292008-02-13 18:58:46 +01002185 err = -ENOMEM;
Ben Gardon1ec69642019-02-11 11:02:51 -08002186 page = alloc_page(GFP_KERNEL_ACCOUNT);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002187 if (!page)
2188 goto uninit;
2189
Ben Gardon1ec69642019-02-11 11:02:51 -08002190 msrpm_pages = alloc_pages(GFP_KERNEL_ACCOUNT, MSRPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01002191 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002192 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002193
Ben Gardon1ec69642019-02-11 11:02:51 -08002194 nested_msrpm_pages = alloc_pages(GFP_KERNEL_ACCOUNT, MSRPM_ALLOC_ORDER);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002195 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002196 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01002197
Ben Gardon1ec69642019-02-11 11:02:51 -08002198 hsave_page = alloc_page(GFP_KERNEL_ACCOUNT);
Alexander Grafb286d5d2008-11-25 20:17:05 +01002199 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002200 goto free_page3;
2201
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002202 err = avic_init_vcpu(svm);
2203 if (err)
2204 goto free_page4;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002205
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002206 /* We initialize this flag to true to make sure that the is_running
2207 * bit would be set the first time the vcpu is loaded.
2208 */
2209 svm->avic_is_running = true;
2210
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002211 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01002212
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002213 svm->msrpm = page_address(msrpm_pages);
2214 svm_vcpu_init_msrpm(svm->msrpm);
2215
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002216 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002217 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002218
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002219 svm->vmcb = page_address(page);
2220 clear_page(svm->vmcb);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002221 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002222 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02002223 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002225 svm_init_osvw(&svm->vcpu);
2226
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002227 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08002228
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002229free_page4:
2230 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002231free_page3:
2232 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
2233free_page2:
2234 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
2235free_page1:
2236 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002237uninit:
2238 kvm_vcpu_uninit(&svm->vcpu);
2239free_svm:
Marc Orrb666a4b2018-11-06 14:53:56 -08002240 kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.guest_fpu);
Wanpeng Lid9a710e2019-07-22 12:26:21 +08002241free_user_fpu:
2242 kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.user_fpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08002243free_partial_svm:
Rusty Russella4770342007-08-01 14:46:11 +10002244 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002245out:
2246 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247}
2248
Jim Mattsonfd65d312018-05-22 09:54:20 -07002249static void svm_clear_current_vmcb(struct vmcb *vmcb)
2250{
2251 int i;
2252
2253 for_each_online_cpu(i)
2254 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
2255}
2256
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257static void svm_free_vcpu(struct kvm_vcpu *vcpu)
2258{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002259 struct vcpu_svm *svm = to_svm(vcpu);
2260
Jim Mattsonfd65d312018-05-22 09:54:20 -07002261 /*
2262 * The vmcb page can be recycled, causing a false negative in
2263 * svm_vcpu_load(). So, ensure that no logical CPU has this
2264 * vmcb page recorded as its current vmcb.
2265 */
2266 svm_clear_current_vmcb(svm->vmcb);
2267
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002268 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01002269 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002270 __free_page(virt_to_page(svm->nested.hsave));
2271 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002272 kvm_vcpu_uninit(vcpu);
Wanpeng Lid9a710e2019-07-22 12:26:21 +08002273 kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.user_fpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08002274 kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.guest_fpu);
Rusty Russella4770342007-08-01 14:46:11 +10002275 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276}
2277
Avi Kivity15ad7142007-07-11 18:17:21 +03002278static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002280 struct vcpu_svm *svm = to_svm(vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01002281 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002282 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02002283
Avi Kivity0cc50642007-03-25 12:07:27 +02002284 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002285 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002286 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02002287 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002288
Avi Kivity82ca2d12010-10-21 12:20:34 +02002289#ifdef CONFIG_X86_64
2290 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
2291#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02002292 savesegment(fs, svm->host.fs);
2293 savesegment(gs, svm->host.gs);
2294 svm->host.ldt = kvm_read_ldt();
2295
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002296 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002297 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002298
Haozhong Zhangad7218832015-10-20 15:39:02 +08002299 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
2300 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2301 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
2302 __this_cpu_write(current_tsc_ratio, tsc_ratio);
2303 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
2304 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002305 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01002306 /* This assumes that the kernel never uses MSR_TSC_AUX */
2307 if (static_cpu_has(X86_FEATURE_RDTSCP))
2308 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002309
Ashok Raj15d45072018-02-01 22:59:43 +01002310 if (sd->current_vmcb != svm->vmcb) {
2311 sd->current_vmcb = svm->vmcb;
2312 indirect_branch_prediction_barrier();
2313 }
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002314 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315}
2316
2317static void svm_vcpu_put(struct kvm_vcpu *vcpu)
2318{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002319 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002320 int i;
2321
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002322 avic_vcpu_put(vcpu);
2323
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002324 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02002325 kvm_load_ldt(svm->host.ldt);
2326#ifdef CONFIG_X86_64
2327 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07002328 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01002329 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02002330#else
Avi Kivity831ca602011-03-08 16:09:51 +02002331#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02002332 loadsegment(gs, svm->host.gs);
2333#endif
Avi Kivity831ca602011-03-08 16:09:51 +02002334#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002335 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002336 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337}
2338
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002339static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
2340{
2341 avic_set_running(vcpu, false);
2342}
2343
2344static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
2345{
2346 avic_set_running(vcpu, true);
2347}
2348
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
2350{
Ladi Prosek9b611742017-06-21 09:06:59 +02002351 struct vcpu_svm *svm = to_svm(vcpu);
2352 unsigned long rflags = svm->vmcb->save.rflags;
2353
2354 if (svm->nmi_singlestep) {
2355 /* Hide our flags if they were not set by the guest */
2356 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
2357 rflags &= ~X86_EFLAGS_TF;
2358 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
2359 rflags &= ~X86_EFLAGS_RF;
2360 }
2361 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362}
2363
2364static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2365{
Ladi Prosek9b611742017-06-21 09:06:59 +02002366 if (to_svm(vcpu)->nmi_singlestep)
2367 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2368
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002369 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02002370 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002371 * (caused by either a task switch or an inter-privilege IRET),
2372 * so we do not need to update the CPL here.
2373 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002374 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375}
2376
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002377static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
2378{
2379 switch (reg) {
2380 case VCPU_EXREG_PDPTR:
2381 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002382 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002383 break;
2384 default:
Sean Christopherson34059c22019-09-27 14:45:23 -07002385 WARN_ON_ONCE(1);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002386 }
2387}
2388
Alexander Graff0b85052008-11-25 20:17:01 +01002389static void svm_set_vintr(struct vcpu_svm *svm)
2390{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002391 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002392}
2393
2394static void svm_clear_vintr(struct vcpu_svm *svm)
2395{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002396 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002397}
2398
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
2400{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002401 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402
2403 switch (seg) {
2404 case VCPU_SREG_CS: return &save->cs;
2405 case VCPU_SREG_DS: return &save->ds;
2406 case VCPU_SREG_ES: return &save->es;
2407 case VCPU_SREG_FS: return &save->fs;
2408 case VCPU_SREG_GS: return &save->gs;
2409 case VCPU_SREG_SS: return &save->ss;
2410 case VCPU_SREG_TR: return &save->tr;
2411 case VCPU_SREG_LDTR: return &save->ldtr;
2412 }
2413 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00002414 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415}
2416
2417static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2418{
2419 struct vmcb_seg *s = svm_seg(vcpu, seg);
2420
2421 return s->base;
2422}
2423
2424static void svm_get_segment(struct kvm_vcpu *vcpu,
2425 struct kvm_segment *var, int seg)
2426{
2427 struct vmcb_seg *s = svm_seg(vcpu, seg);
2428
2429 var->base = s->base;
2430 var->limit = s->limit;
2431 var->selector = s->selector;
2432 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
2433 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
2434 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
2435 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
2436 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
2437 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
2438 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05302439
2440 /*
2441 * AMD CPUs circa 2014 track the G bit for all segments except CS.
2442 * However, the SVM spec states that the G bit is not observed by the
2443 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
2444 * So let's synthesize a legal G bit for all segments, this helps
2445 * running KVM nested. It also helps cross-vendor migration, because
2446 * Intel's vmentry has a check on the 'G' bit.
2447 */
2448 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00002449
Joerg Roedele0231712010-02-24 18:59:10 +01002450 /*
2451 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02002452 * for cross vendor migration purposes by "not present"
2453 */
Gioh Kim8eae9572017-05-30 15:24:45 +02002454 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02002455
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002456 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002457 case VCPU_SREG_TR:
2458 /*
2459 * Work around a bug where the busy flag in the tr selector
2460 * isn't exposed
2461 */
Amit Shahc0d09822008-10-27 09:04:18 +00002462 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002463 break;
2464 case VCPU_SREG_DS:
2465 case VCPU_SREG_ES:
2466 case VCPU_SREG_FS:
2467 case VCPU_SREG_GS:
2468 /*
2469 * The accessed bit must always be set in the segment
2470 * descriptor cache, although it can be cleared in the
2471 * descriptor, the cached bit always remains at 1. Since
2472 * Intel has a check on this, set it here to support
2473 * cross-vendor migration.
2474 */
2475 if (!var->unusable)
2476 var->type |= 0x1;
2477 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02002478 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01002479 /*
2480 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02002481 * descriptor is left as 1, although the whole segment has
2482 * been made unusable. Clear it here to pass an Intel VMX
2483 * entry check when cross vendor migrating.
2484 */
2485 if (var->unusable)
2486 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02002487 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02002488 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02002489 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002490 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491}
2492
Izik Eidus2e4d2652008-03-24 19:38:34 +02002493static int svm_get_cpl(struct kvm_vcpu *vcpu)
2494{
2495 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
2496
2497 return save->cpl;
2498}
2499
Gleb Natapov89a27f42010-02-16 10:51:48 +02002500static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002502 struct vcpu_svm *svm = to_svm(vcpu);
2503
Gleb Natapov89a27f42010-02-16 10:51:48 +02002504 dt->size = svm->vmcb->save.idtr.limit;
2505 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506}
2507
Gleb Natapov89a27f42010-02-16 10:51:48 +02002508static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002510 struct vcpu_svm *svm = to_svm(vcpu);
2511
Gleb Natapov89a27f42010-02-16 10:51:48 +02002512 svm->vmcb->save.idtr.limit = dt->size;
2513 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002514 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515}
2516
Gleb Natapov89a27f42010-02-16 10:51:48 +02002517static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002519 struct vcpu_svm *svm = to_svm(vcpu);
2520
Gleb Natapov89a27f42010-02-16 10:51:48 +02002521 dt->size = svm->vmcb->save.gdtr.limit;
2522 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523}
2524
Gleb Natapov89a27f42010-02-16 10:51:48 +02002525static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002527 struct vcpu_svm *svm = to_svm(vcpu);
2528
Gleb Natapov89a27f42010-02-16 10:51:48 +02002529 svm->vmcb->save.gdtr.limit = dt->size;
2530 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002531 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532}
2533
Avi Kivitye8467fd2009-12-29 18:43:06 +02002534static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2535{
2536}
2537
Anthony Liguori25c4c272007-04-27 09:29:21 +03002538static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002539{
2540}
2541
Avi Kivityd2251572010-01-06 10:55:27 +02002542static void update_cr0_intercept(struct vcpu_svm *svm)
2543{
2544 ulong gcr0 = svm->vcpu.arch.cr0;
2545 u64 *hcr0 = &svm->vmcb->save.cr0;
2546
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002547 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
2548 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
Avi Kivityd2251572010-01-06 10:55:27 +02002549
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002550 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002551
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002552 if (gcr0 == *hcr0) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002553 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
2554 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002555 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002556 set_cr_intercept(svm, INTERCEPT_CR0_READ);
2557 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002558 }
2559}
2560
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2562{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002563 struct vcpu_svm *svm = to_svm(vcpu);
2564
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002565#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002566 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002567 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002568 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002569 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570 }
2571
Mike Dayd77c26f2007-10-08 09:02:08 -04002572 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002573 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002574 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575 }
2576 }
2577#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002578 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002579
2580 if (!npt_enabled)
2581 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02002582
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02002583 /*
2584 * re-enable caching here because the QEMU bios
2585 * does not do it - this results in some delay at
2586 * reboot
2587 */
2588 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
2589 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002590 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002591 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002592 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002593}
2594
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002595static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002597 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002598 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
2599
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002600 if (cr4 & X86_CR4_VMXE)
2601 return 1;
2602
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002603 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002604 svm_flush_tlb(vcpu, true);
Joerg Roedel6394b642008-04-09 14:15:29 +02002605
Joerg Roedelec077262008-04-09 14:15:28 +02002606 vcpu->arch.cr4 = cr4;
2607 if (!npt_enabled)
2608 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02002609 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02002610 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002611 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002612 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613}
2614
2615static void svm_set_segment(struct kvm_vcpu *vcpu,
2616 struct kvm_segment *var, int seg)
2617{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002618 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002619 struct vmcb_seg *s = svm_seg(vcpu, seg);
2620
2621 s->base = var->base;
2622 s->limit = var->limit;
2623 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02002624 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2625 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2626 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2627 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2628 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2629 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2630 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2631 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002632
2633 /*
2634 * This is always accurate, except if SYSRET returned to a segment
2635 * with SS.DPL != 3. Intel does not have this quirk, and always
2636 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2637 * would entail passing the CPL to userspace and back.
2638 */
2639 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02002640 /* This is symmetric with svm_get_segment() */
2641 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642
Joerg Roedel060d0c92010-12-03 11:45:57 +01002643 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644}
2645
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01002646static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002648 struct vcpu_svm *svm = to_svm(vcpu);
2649
Joerg Roedel18c918c2010-11-30 18:03:59 +01002650 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03002651
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002652 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002653 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01002654 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002655 } else
2656 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03002657}
2658
Tejun Heo0fe1e002009-10-29 22:34:14 +09002659static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660{
Tejun Heo0fe1e002009-10-29 22:34:14 +09002661 if (sd->next_asid > sd->max_asid) {
2662 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06002663 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002664 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665 }
2666
Tejun Heo0fe1e002009-10-29 22:34:14 +09002667 svm->asid_generation = sd->asid_generation;
2668 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01002669
2670 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671}
2672
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002673static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2674{
2675 return to_svm(vcpu)->vmcb->save.dr6;
2676}
2677
2678static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2679{
2680 struct vcpu_svm *svm = to_svm(vcpu);
2681
2682 svm->vmcb->save.dr6 = value;
2683 mark_dirty(svm->vmcb, VMCB_DR);
2684}
2685
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002686static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2687{
2688 struct vcpu_svm *svm = to_svm(vcpu);
2689
2690 get_debugreg(vcpu->arch.db[0], 0);
2691 get_debugreg(vcpu->arch.db[1], 1);
2692 get_debugreg(vcpu->arch.db[2], 2);
2693 get_debugreg(vcpu->arch.db[3], 3);
2694 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2695 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2696
2697 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2698 set_dr_intercepts(svm);
2699}
2700
Gleb Natapov020df072010-04-13 10:05:23 +03002701static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002703 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002704
Gleb Natapov020df072010-04-13 10:05:23 +03002705 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01002706 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707}
2708
Avi Kivity851ba692009-08-24 11:10:17 +03002709static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002711 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002712 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002714 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002715 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2716 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002717 svm->vmcb->control.insn_len);
2718}
2719
2720static int npf_interception(struct vcpu_svm *svm)
2721{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002722 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Paolo Bonzinid0006532017-08-11 18:36:43 +02002723 u64 error_code = svm->vmcb->control.exit_info_1;
2724
2725 trace_kvm_page_fault(fault_address, error_code);
2726 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002727 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2728 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002729 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730}
2731
Avi Kivity851ba692009-08-24 11:10:17 +03002732static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002733{
Avi Kivity851ba692009-08-24 11:10:17 +03002734 struct kvm_run *kvm_run = svm->vcpu.run;
Vitaly Kuznetsov99c22172019-04-03 16:06:42 +02002735 struct kvm_vcpu *vcpu = &svm->vcpu;
Avi Kivity851ba692009-08-24 11:10:17 +03002736
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002737 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03002738 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02002739 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002740 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2741 return 1;
2742 }
Gleb Natapov44c11432009-05-11 13:35:52 +03002743
Jan Kiszka6be7d302009-10-18 13:24:54 +02002744 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02002745 disable_nmi_singlestep(svm);
Vitaly Kuznetsov99c22172019-04-03 16:06:42 +02002746 /* Make sure we check for pending NMIs upon entry */
2747 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov44c11432009-05-11 13:35:52 +03002748 }
2749
2750 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002751 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002752 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2753 kvm_run->debug.arch.pc =
2754 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2755 kvm_run->debug.arch.exception = DB_VECTOR;
2756 return 0;
2757 }
2758
2759 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002760}
2761
Avi Kivity851ba692009-08-24 11:10:17 +03002762static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002763{
Avi Kivity851ba692009-08-24 11:10:17 +03002764 struct kvm_run *kvm_run = svm->vcpu.run;
2765
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002766 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2767 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2768 kvm_run->debug.arch.exception = BP_VECTOR;
2769 return 0;
2770}
2771
Avi Kivity851ba692009-08-24 11:10:17 +03002772static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002773{
Wanpeng Li082d06e2018-04-03 16:28:48 -07002774 return handle_ud(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002775}
2776
Eric Northup54a20552015-11-03 18:03:53 +01002777static int ac_interception(struct vcpu_svm *svm)
2778{
2779 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2780 return 1;
2781}
2782
Liran Alon97184202018-03-12 13:12:52 +02002783static int gp_interception(struct vcpu_svm *svm)
2784{
2785 struct kvm_vcpu *vcpu = &svm->vcpu;
2786 u32 error_code = svm->vmcb->control.exit_info_1;
Liran Alon97184202018-03-12 13:12:52 +02002787
2788 WARN_ON_ONCE(!enable_vmware_backdoor);
2789
Sean Christophersona6c6ed12019-08-27 14:40:30 -07002790 /*
2791 * VMware backdoor emulation on #GP interception only handles IN{S},
2792 * OUT{S}, and RDPMC, none of which generate a non-zero error code.
2793 */
2794 if (error_code) {
2795 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2796 return 1;
2797 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002798 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
Liran Alon97184202018-03-12 13:12:52 +02002799}
2800
Joerg Roedel67ec6602010-05-17 14:43:35 +02002801static bool is_erratum_383(void)
2802{
2803 int err, i;
2804 u64 value;
2805
2806 if (!erratum_383_found)
2807 return false;
2808
2809 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2810 if (err)
2811 return false;
2812
2813 /* Bit 62 may or may not be set for this mce */
2814 value &= ~(1ULL << 62);
2815
2816 if (value != 0xb600000000010015ULL)
2817 return false;
2818
2819 /* Clear MCi_STATUS registers */
2820 for (i = 0; i < 6; ++i)
2821 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2822
2823 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2824 if (!err) {
2825 u32 low, high;
2826
2827 value &= ~(1ULL << 2);
2828 low = lower_32_bits(value);
2829 high = upper_32_bits(value);
2830
2831 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2832 }
2833
2834 /* Flush tlb to evict multi-match entries */
2835 __flush_tlb_all();
2836
2837 return true;
2838}
2839
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002840static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002841{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002842 if (is_erratum_383()) {
2843 /*
2844 * Erratum 383 triggered. Guest state is corrupt so kill the
2845 * guest.
2846 */
2847 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2848
Avi Kivitya8eeb042010-05-10 12:34:53 +03002849 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002850
2851 return;
2852 }
2853
Joerg Roedel53371b52008-04-09 14:15:30 +02002854 /*
2855 * On an #MC intercept the MCE handler is not called automatically in
2856 * the host. So do it by hand here.
2857 */
2858 asm volatile (
2859 "int $0x12\n");
2860 /* not sure if we ever come back to this point */
2861
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002862 return;
2863}
2864
2865static int mc_interception(struct vcpu_svm *svm)
2866{
Joerg Roedel53371b52008-04-09 14:15:30 +02002867 return 1;
2868}
2869
Avi Kivity851ba692009-08-24 11:10:17 +03002870static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002871{
Avi Kivity851ba692009-08-24 11:10:17 +03002872 struct kvm_run *kvm_run = svm->vcpu.run;
2873
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002874 /*
2875 * VMCB is undefined after a SHUTDOWN intercept
2876 * so reinitialize it.
2877 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002878 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002879 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002880
2881 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2882 return 0;
2883}
2884
Avi Kivity851ba692009-08-24 11:10:17 +03002885static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002887 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002888 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Sean Christophersondca7f122018-03-08 08:57:27 -08002889 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002890 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891
Rusty Russelle756fc62007-07-30 20:07:08 +10002892 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002893 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002894 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002895 if (string)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002896 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002897
Avi Kivity039576c2007-03-20 12:46:50 +02002898 port = io_info >> 16;
2899 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002900 svm->next_rip = svm->vmcb->control.exit_info_2;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002901
Sean Christophersondca7f122018-03-08 08:57:27 -08002902 return kvm_fast_pio(&svm->vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903}
2904
Avi Kivity851ba692009-08-24 11:10:17 +03002905static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002906{
2907 return 1;
2908}
2909
Avi Kivity851ba692009-08-24 11:10:17 +03002910static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002911{
2912 ++svm->vcpu.stat.irq_exits;
2913 return 1;
2914}
2915
Avi Kivity851ba692009-08-24 11:10:17 +03002916static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917{
2918 return 1;
2919}
2920
Avi Kivity851ba692009-08-24 11:10:17 +03002921static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922{
Rusty Russelle756fc62007-07-30 20:07:08 +10002923 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924}
2925
Avi Kivity851ba692009-08-24 11:10:17 +03002926static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002927{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002928 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002929}
2930
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002931static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2932{
2933 struct vcpu_svm *svm = to_svm(vcpu);
2934
2935 return svm->nested.nested_cr3;
2936}
2937
Avi Kivitye4e517b2011-07-28 11:36:17 +03002938static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2939{
2940 struct vcpu_svm *svm = to_svm(vcpu);
2941 u64 cr3 = svm->nested.nested_cr3;
2942 u64 pdpte;
2943 int ret;
2944
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002945 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002946 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002947 if (ret)
2948 return 0;
2949 return pdpte;
2950}
2951
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002952static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2953 unsigned long root)
2954{
2955 struct vcpu_svm *svm = to_svm(vcpu);
2956
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002957 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01002958 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002959}
2960
Avi Kivity6389ee92010-11-29 16:12:30 +02002961static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2962 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002963{
2964 struct vcpu_svm *svm = to_svm(vcpu);
2965
Paolo Bonzini5e352512014-09-02 13:18:37 +02002966 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2967 /*
2968 * TODO: track the cause of the nested page fault, and
2969 * correctly fill in the high bits of exit_info_1.
2970 */
2971 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2972 svm->vmcb->control.exit_code_hi = 0;
2973 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2974 svm->vmcb->control.exit_info_2 = fault->address;
2975 }
2976
2977 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2978 svm->vmcb->control.exit_info_1 |= fault->error_code;
2979
2980 /*
2981 * The present bit is always zero for page structure faults on real
2982 * hardware.
2983 */
2984 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2985 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002986
2987 nested_svm_vmexit(svm);
2988}
2989
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002990static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002991{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002992 WARN_ON(mmu_is_nested(vcpu));
Vitaly Kuznetsov3cf85f92018-12-19 17:25:14 +01002993
2994 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Paolo Bonziniad896af2013-10-02 16:56:14 +02002995 kvm_init_shadow_mmu(vcpu);
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002996 vcpu->arch.mmu->set_cr3 = nested_svm_set_tdp_cr3;
2997 vcpu->arch.mmu->get_cr3 = nested_svm_get_tdp_cr3;
2998 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
2999 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
3000 vcpu->arch.mmu->shadow_root_level = get_npt_level(vcpu);
3001 reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02003002 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02003003}
3004
3005static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
3006{
Vitaly Kuznetsov3cf85f92018-12-19 17:25:14 +01003007 vcpu->arch.mmu = &vcpu->arch.root_mmu;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003008 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02003009}
3010
Alexander Grafc0725422008-11-25 20:17:03 +01003011static int nested_svm_check_permissions(struct vcpu_svm *svm)
3012{
Dan Carpentere9196ce2017-05-18 10:39:53 +03003013 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
3014 !is_paging(&svm->vcpu)) {
Alexander Grafc0725422008-11-25 20:17:03 +01003015 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3016 return 1;
3017 }
3018
3019 if (svm->vmcb->save.cpl) {
3020 kvm_inject_gp(&svm->vcpu, 0);
3021 return 1;
3022 }
3023
Dan Carpentere9196ce2017-05-18 10:39:53 +03003024 return 0;
Alexander Grafc0725422008-11-25 20:17:03 +01003025}
3026
Alexander Grafcf74a782008-11-25 20:17:08 +01003027static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
3028 bool has_error_code, u32 error_code)
3029{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003030 int vmexit;
3031
Joerg Roedel20307532010-11-29 17:51:48 +01003032 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02003033 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01003034
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003035 vmexit = nested_svm_intercept(svm);
3036 if (vmexit != NESTED_EXIT_DONE)
3037 return 0;
3038
Joerg Roedel0295ad72009-08-07 11:49:37 +02003039 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
3040 svm->vmcb->control.exit_code_hi = 0;
3041 svm->vmcb->control.exit_info_1 = error_code;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003042
3043 /*
Jim Mattsonda998b42018-10-16 14:29:22 -07003044 * EXITINFO2 is undefined for all exception intercepts other
3045 * than #PF.
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003046 */
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003047 if (svm->vcpu.arch.exception.nested_apf)
3048 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
Jim Mattsonda998b42018-10-16 14:29:22 -07003049 else if (svm->vcpu.arch.exception.has_payload)
3050 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003051 else
3052 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
Joerg Roedel0295ad72009-08-07 11:49:37 +02003053
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003054 svm->nested.exit_required = true;
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003055 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01003056}
3057
Joerg Roedel8fe54652010-02-19 16:23:01 +01003058/* This function returns true if it is save to enable the irq window */
3059static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003060{
Joerg Roedel20307532010-11-29 17:51:48 +01003061 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01003062 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01003063
Joerg Roedel26666952009-08-07 11:49:46 +02003064 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01003065 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01003066
Joerg Roedel26666952009-08-07 11:49:46 +02003067 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01003068 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01003069
Gleb Natapova0a07cd2010-09-20 10:15:32 +02003070 /*
3071 * if vmexit was already requested (by intercepted exception
3072 * for instance) do not overwrite it with "external interrupt"
3073 * vmexit.
3074 */
3075 if (svm->nested.exit_required)
3076 return false;
3077
Joerg Roedel197717d2010-02-24 18:59:19 +01003078 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
3079 svm->vmcb->control.exit_info_1 = 0;
3080 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02003081
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003082 if (svm->nested.intercept & 1ULL) {
3083 /*
3084 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003085 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003086 * #vmexit emulation might sleep. Only signal request for
3087 * the #vmexit here.
3088 */
3089 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02003090 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01003091 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01003092 }
3093
Joerg Roedel8fe54652010-02-19 16:23:01 +01003094 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01003095}
3096
Joerg Roedel887f5002010-02-24 18:59:12 +01003097/* This function returns true if it is save to enable the nmi window */
3098static inline bool nested_svm_nmi(struct vcpu_svm *svm)
3099{
Joerg Roedel20307532010-11-29 17:51:48 +01003100 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01003101 return true;
3102
3103 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
3104 return true;
3105
3106 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
3107 svm->nested.exit_required = true;
3108
3109 return false;
3110}
3111
Joerg Roedelce2ac082010-03-01 15:34:39 +01003112static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003113{
Jan Kiszka9bf41832014-06-30 10:54:17 +02003114 unsigned port, size, iopm_len;
3115 u16 val, mask;
3116 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003117 u64 gpa;
3118
3119 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
3120 return NESTED_EXIT_HOST;
3121
3122 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02003123 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
3124 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003125 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02003126 start_bit = port % 8;
3127 iopm_len = (start_bit + size > 8) ? 2 : 1;
3128 mask = (0xf >> (4 - size)) << start_bit;
3129 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003130
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003131 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02003132 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003133
Jan Kiszka9bf41832014-06-30 10:54:17 +02003134 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003135}
3136
Joerg Roedeld2477822010-03-01 15:34:34 +01003137static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003138{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003139 u32 offset, msr, value;
3140 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003141
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003142 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01003143 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003144
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003145 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
3146 offset = svm_msrpm_offset(msr);
3147 write = svm->vmcb->control.exit_info_1 & 1;
3148 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003149
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003150 if (offset == MSR_INVALID)
3151 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003152
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003153 /* Offset is in 32 bit units but need in 8 bit units */
3154 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003155
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003156 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003157 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003158
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003159 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003160}
3161
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003162/* DB exceptions for our internal use must not cause vmexit */
3163static int nested_svm_intercept_db(struct vcpu_svm *svm)
3164{
3165 unsigned long dr6;
3166
3167 /* if we're not singlestepping, it's not ours */
3168 if (!svm->nmi_singlestep)
3169 return NESTED_EXIT_DONE;
3170
3171 /* if it's not a singlestep exception, it's not ours */
3172 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
3173 return NESTED_EXIT_DONE;
3174 if (!(dr6 & DR6_BS))
3175 return NESTED_EXIT_DONE;
3176
3177 /* if the guest is singlestepping, it should get the vmexit */
3178 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
3179 disable_nmi_singlestep(svm);
3180 return NESTED_EXIT_DONE;
3181 }
3182
3183 /* it's ours, the nested hypervisor must not see this one */
3184 return NESTED_EXIT_HOST;
3185}
3186
Joerg Roedel410e4d52009-08-07 11:49:44 +02003187static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003188{
Alexander Grafcf74a782008-11-25 20:17:08 +01003189 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003190
Joerg Roedel410e4d52009-08-07 11:49:44 +02003191 switch (exit_code) {
3192 case SVM_EXIT_INTR:
3193 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02003194 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02003195 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02003196 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01003197 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02003198 if (npt_enabled)
3199 return NESTED_EXIT_HOST;
3200 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02003201 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02003202 /* When we're shadowing, trap PFs, but not async PF */
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003203 if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003204 return NESTED_EXIT_HOST;
3205 break;
3206 default:
3207 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01003208 }
3209
Joerg Roedel410e4d52009-08-07 11:49:44 +02003210 return NESTED_EXIT_CONTINUE;
3211}
3212
3213/*
3214 * If this function returns true, this #vmexit was already handled
3215 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003216static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003217{
3218 u32 exit_code = svm->vmcb->control.exit_code;
3219 int vmexit = NESTED_EXIT_HOST;
3220
Alexander Grafcf74a782008-11-25 20:17:08 +01003221 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003222 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003223 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003224 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003225 case SVM_EXIT_IOIO:
3226 vmexit = nested_svm_intercept_ioio(svm);
3227 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003228 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
3229 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
3230 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003231 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003232 break;
3233 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01003234 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
3235 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
3236 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003237 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003238 break;
3239 }
3240 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
3241 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003242 if (svm->nested.intercept_exceptions & excp_bits) {
3243 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
3244 vmexit = nested_svm_intercept_db(svm);
3245 else
3246 vmexit = NESTED_EXIT_DONE;
3247 }
Gleb Natapov631bc482010-10-14 11:22:52 +02003248 /* async page fault always cause vmexit */
3249 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003250 svm->vcpu.arch.exception.nested_apf != 0)
Gleb Natapov631bc482010-10-14 11:22:52 +02003251 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003252 break;
3253 }
Joerg Roedel228070b2010-04-22 12:33:10 +02003254 case SVM_EXIT_ERR: {
3255 vmexit = NESTED_EXIT_DONE;
3256 break;
3257 }
Alexander Grafcf74a782008-11-25 20:17:08 +01003258 default: {
3259 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02003260 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003261 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003262 }
3263 }
3264
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003265 return vmexit;
3266}
3267
3268static int nested_svm_exit_handled(struct vcpu_svm *svm)
3269{
3270 int vmexit;
3271
3272 vmexit = nested_svm_intercept(svm);
3273
3274 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003275 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003276
3277 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01003278}
3279
Joerg Roedel0460a972009-08-07 11:49:31 +02003280static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
3281{
3282 struct vmcb_control_area *dst = &dst_vmcb->control;
3283 struct vmcb_control_area *from = &from_vmcb->control;
3284
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003285 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003286 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02003287 dst->intercept_exceptions = from->intercept_exceptions;
3288 dst->intercept = from->intercept;
3289 dst->iopm_base_pa = from->iopm_base_pa;
3290 dst->msrpm_base_pa = from->msrpm_base_pa;
3291 dst->tsc_offset = from->tsc_offset;
3292 dst->asid = from->asid;
3293 dst->tlb_ctl = from->tlb_ctl;
3294 dst->int_ctl = from->int_ctl;
3295 dst->int_vector = from->int_vector;
3296 dst->int_state = from->int_state;
3297 dst->exit_code = from->exit_code;
3298 dst->exit_code_hi = from->exit_code_hi;
3299 dst->exit_info_1 = from->exit_info_1;
3300 dst->exit_info_2 = from->exit_info_2;
3301 dst->exit_int_info = from->exit_int_info;
3302 dst->exit_int_info_err = from->exit_int_info_err;
3303 dst->nested_ctl = from->nested_ctl;
3304 dst->event_inj = from->event_inj;
3305 dst->event_inj_err = from->event_inj_err;
3306 dst->nested_cr3 = from->nested_cr3;
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003307 dst->virt_ext = from->virt_ext;
Tambe, Williame0813542018-11-13 16:51:20 +00003308 dst->pause_filter_count = from->pause_filter_count;
3309 dst->pause_filter_thresh = from->pause_filter_thresh;
Joerg Roedel0460a972009-08-07 11:49:31 +02003310}
3311
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003312static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003313{
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003314 int rc;
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003315 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003316 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02003317 struct vmcb *vmcb = svm->vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003318 struct kvm_host_map map;
Alexander Grafcf74a782008-11-25 20:17:08 +01003319
Joerg Roedel17897f32009-10-09 16:08:29 +02003320 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
3321 vmcb->control.exit_info_1,
3322 vmcb->control.exit_info_2,
3323 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003324 vmcb->control.exit_int_info_err,
3325 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02003326
Vitaly Kuznetsov8f383022019-06-04 18:09:39 +02003327 rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb), &map);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003328 if (rc) {
3329 if (rc == -EINVAL)
3330 kvm_inject_gp(&svm->vcpu, 0);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003331 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003332 }
3333
3334 nested_vmcb = map.hva;
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003335
Joerg Roedel20307532010-11-29 17:51:48 +01003336 /* Exit Guest-Mode */
3337 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01003338 svm->nested.vmcb = 0;
3339
Alexander Grafcf74a782008-11-25 20:17:08 +01003340 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02003341 disable_gif(svm);
3342
3343 nested_vmcb->save.es = vmcb->save.es;
3344 nested_vmcb->save.cs = vmcb->save.cs;
3345 nested_vmcb->save.ss = vmcb->save.ss;
3346 nested_vmcb->save.ds = vmcb->save.ds;
3347 nested_vmcb->save.gdtr = vmcb->save.gdtr;
3348 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02003349 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003350 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003351 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003352 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003353 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03003354 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003355 nested_vmcb->save.rip = vmcb->save.rip;
3356 nested_vmcb->save.rsp = vmcb->save.rsp;
3357 nested_vmcb->save.rax = vmcb->save.rax;
3358 nested_vmcb->save.dr7 = vmcb->save.dr7;
3359 nested_vmcb->save.dr6 = vmcb->save.dr6;
3360 nested_vmcb->save.cpl = vmcb->save.cpl;
3361
3362 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
3363 nested_vmcb->control.int_vector = vmcb->control.int_vector;
3364 nested_vmcb->control.int_state = vmcb->control.int_state;
3365 nested_vmcb->control.exit_code = vmcb->control.exit_code;
3366 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
3367 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
3368 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
3369 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
3370 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003371
3372 if (svm->nrips_enabled)
3373 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02003374
3375 /*
3376 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
3377 * to make sure that we do not lose injected events. So check event_inj
3378 * here and copy it to exit_int_info if it is valid.
3379 * Exit_int_info and event_inj can't be both valid because the case
3380 * below only happens on a VMRUN instruction intercept which has
3381 * no valid exit_int_info set.
3382 */
3383 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
3384 struct vmcb_control_area *nc = &nested_vmcb->control;
3385
3386 nc->exit_int_info = vmcb->control.event_inj;
3387 nc->exit_int_info_err = vmcb->control.event_inj_err;
3388 }
3389
Joerg Roedel33740e42009-08-07 11:49:29 +02003390 nested_vmcb->control.tlb_ctl = 0;
3391 nested_vmcb->control.event_inj = 0;
3392 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01003393
Tambe, Williame0813542018-11-13 16:51:20 +00003394 nested_vmcb->control.pause_filter_count =
3395 svm->vmcb->control.pause_filter_count;
3396 nested_vmcb->control.pause_filter_thresh =
3397 svm->vmcb->control.pause_filter_thresh;
3398
Alexander Grafcf74a782008-11-25 20:17:08 +01003399 /* We always set V_INTR_MASKING and remember the old value in hflags */
3400 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
3401 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
3402
Alexander Grafcf74a782008-11-25 20:17:08 +01003403 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02003404 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01003405
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003406 svm->vcpu.arch.tsc_offset = svm->vmcb->control.tsc_offset;
Alexander Graf219b65d2009-06-15 15:21:25 +02003407 kvm_clear_exception_queue(&svm->vcpu);
3408 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003409
Joerg Roedel4b161842010-09-10 17:31:03 +02003410 svm->nested.nested_cr3 = 0;
3411
Alexander Grafcf74a782008-11-25 20:17:08 +01003412 /* Restore selected save entries */
3413 svm->vmcb->save.es = hsave->save.es;
3414 svm->vmcb->save.cs = hsave->save.cs;
3415 svm->vmcb->save.ss = hsave->save.ss;
3416 svm->vmcb->save.ds = hsave->save.ds;
3417 svm->vmcb->save.gdtr = hsave->save.gdtr;
3418 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003419 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01003420 svm_set_efer(&svm->vcpu, hsave->save.efer);
3421 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
3422 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
3423 if (npt_enabled) {
3424 svm->vmcb->save.cr3 = hsave->save.cr3;
3425 svm->vcpu.arch.cr3 = hsave->save.cr3;
3426 } else {
Avi Kivity23902182010-06-10 17:02:16 +03003427 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01003428 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07003429 kvm_rax_write(&svm->vcpu, hsave->save.rax);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02003430 kvm_rsp_write(&svm->vcpu, hsave->save.rsp);
3431 kvm_rip_write(&svm->vcpu, hsave->save.rip);
Alexander Grafcf74a782008-11-25 20:17:08 +01003432 svm->vmcb->save.dr7 = 0;
3433 svm->vmcb->save.cpl = 0;
3434 svm->vmcb->control.exit_int_info = 0;
3435
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003436 mark_all_dirty(svm->vmcb);
3437
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003438 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Alexander Grafcf74a782008-11-25 20:17:08 +01003439
Joerg Roedel4b161842010-09-10 17:31:03 +02003440 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003441 kvm_mmu_reset_context(&svm->vcpu);
3442 kvm_mmu_load(&svm->vcpu);
3443
Vitaly Kuznetsov619ad842019-01-07 19:44:51 +01003444 /*
3445 * Drop what we picked up for L2 via svm_complete_interrupts() so it
3446 * doesn't end up in L1.
3447 */
3448 svm->vcpu.arch.nmi_injected = false;
3449 kvm_clear_exception_queue(&svm->vcpu);
3450 kvm_clear_interrupt_queue(&svm->vcpu);
3451
Alexander Grafcf74a782008-11-25 20:17:08 +01003452 return 0;
3453}
Alexander Graf3d6368e2008-11-25 20:17:07 +01003454
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003455static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003456{
Joerg Roedel323c3d82010-03-01 15:34:37 +01003457 /*
3458 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003459 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01003460 * the kvm msr permission bitmap may contain zero bits
3461 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01003462 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003463
Joerg Roedel323c3d82010-03-01 15:34:37 +01003464 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3465 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003466
Joerg Roedel323c3d82010-03-01 15:34:37 +01003467 for (i = 0; i < MSRPM_OFFSETS; i++) {
3468 u32 value, p;
3469 u64 offset;
3470
3471 if (msrpm_offsets[i] == 0xffffffff)
3472 break;
3473
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003474 p = msrpm_offsets[i];
3475 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01003476
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003477 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01003478 return false;
3479
3480 svm->nested.msrpm[p] = svm->msrpm[p] | value;
3481 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003482
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05003483 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
Alexander Graf3d6368e2008-11-25 20:17:07 +01003484
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003485 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003486}
3487
Joerg Roedel52c65a302010-08-02 16:46:44 +02003488static bool nested_vmcb_checks(struct vmcb *vmcb)
3489{
3490 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
3491 return false;
3492
Joerg Roedeldbe77582010-08-02 16:46:45 +02003493 if (vmcb->control.asid == 0)
3494 return false;
3495
Tom Lendackycea3a192017-12-04 10:57:24 -06003496 if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
3497 !npt_enabled)
Joerg Roedel4b161842010-09-10 17:31:03 +02003498 return false;
3499
Joerg Roedel52c65a302010-08-02 16:46:44 +02003500 return true;
3501}
3502
Ladi Prosekc2634062017-10-11 16:54:44 +02003503static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003504 struct vmcb *nested_vmcb, struct kvm_host_map *map)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003505{
Avi Kivityf6e78472010-08-02 15:30:20 +03003506 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003507 svm->vcpu.arch.hflags |= HF_HIF_MASK;
3508 else
3509 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
3510
Tom Lendackycea3a192017-12-04 10:57:24 -06003511 if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
Joerg Roedel4b161842010-09-10 17:31:03 +02003512 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
3513 nested_svm_init_mmu_context(&svm->vcpu);
3514 }
3515
Alexander Graf3d6368e2008-11-25 20:17:07 +01003516 /* Load the nested guest state */
3517 svm->vmcb->save.es = nested_vmcb->save.es;
3518 svm->vmcb->save.cs = nested_vmcb->save.cs;
3519 svm->vmcb->save.ss = nested_vmcb->save.ss;
3520 svm->vmcb->save.ds = nested_vmcb->save.ds;
3521 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
3522 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003523 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003524 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
3525 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
3526 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
3527 if (npt_enabled) {
3528 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
3529 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003530 } else
Avi Kivity23902182010-06-10 17:02:16 +03003531 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003532
3533 /* Guest paging mode is active - reset mmu */
3534 kvm_mmu_reset_context(&svm->vcpu);
3535
Joerg Roedeldefbba52009-08-07 11:49:30 +02003536 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Sean Christophersonde3cd112019-04-30 10:36:17 -07003537 kvm_rax_write(&svm->vcpu, nested_vmcb->save.rax);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02003538 kvm_rsp_write(&svm->vcpu, nested_vmcb->save.rsp);
3539 kvm_rip_write(&svm->vcpu, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01003540
Alexander Graf3d6368e2008-11-25 20:17:07 +01003541 /* In case we don't even reach vcpu_run, the fields are not updated */
3542 svm->vmcb->save.rax = nested_vmcb->save.rax;
3543 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3544 svm->vmcb->save.rip = nested_vmcb->save.rip;
3545 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3546 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3547 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3548
Joerg Roedelf7138532010-03-01 15:34:40 +01003549 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003550 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003551
Joerg Roedelaad42c62009-08-07 11:49:34 +02003552 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003553 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003554 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02003555 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3556 svm->nested.intercept = nested_vmcb->control.intercept;
3557
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08003558 svm_flush_tlb(&svm->vcpu, true);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003559 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003560 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3561 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3562 else
3563 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3564
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003565 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3566 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003567 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3568 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003569 }
3570
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003571 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003572 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003573
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003574 svm->vcpu.arch.tsc_offset += nested_vmcb->control.tsc_offset;
3575 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
3576
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003577 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003578 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3579 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003580 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3581 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3582
Tambe, Williame0813542018-11-13 16:51:20 +00003583 svm->vmcb->control.pause_filter_count =
3584 nested_vmcb->control.pause_filter_count;
3585 svm->vmcb->control.pause_filter_thresh =
3586 nested_vmcb->control.pause_filter_thresh;
3587
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003588 kvm_vcpu_unmap(&svm->vcpu, map, true);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003589
Joerg Roedel20307532010-11-29 17:51:48 +01003590 /* Enter Guest-Mode */
3591 enter_guest_mode(&svm->vcpu);
3592
Joerg Roedel384c6362010-11-30 18:03:56 +01003593 /*
3594 * Merge guest and host intercepts - must be called with vcpu in
3595 * guest-mode to take affect here
3596 */
3597 recalc_intercepts(svm);
3598
Joerg Roedel06fc77722010-02-19 16:23:07 +01003599 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003600
Joerg Roedel2af91942009-08-07 11:49:28 +02003601 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003602
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003603 mark_all_dirty(svm->vmcb);
Ladi Prosekc2634062017-10-11 16:54:44 +02003604}
3605
Vitaly Kuznetsove7134c12019-08-13 15:53:34 +02003606static int nested_svm_vmrun(struct vcpu_svm *svm)
Ladi Prosekc2634062017-10-11 16:54:44 +02003607{
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003608 int ret;
Ladi Prosekc2634062017-10-11 16:54:44 +02003609 struct vmcb *nested_vmcb;
3610 struct vmcb *hsave = svm->nested.hsave;
3611 struct vmcb *vmcb = svm->vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003612 struct kvm_host_map map;
Ladi Prosekc2634062017-10-11 16:54:44 +02003613 u64 vmcb_gpa;
3614
3615 vmcb_gpa = svm->vmcb->save.rax;
3616
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003617 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb_gpa), &map);
Dan Carpentera0619852019-08-27 12:38:52 +03003618 if (ret == -EINVAL) {
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003619 kvm_inject_gp(&svm->vcpu, 0);
Vitaly Kuznetsove7134c12019-08-13 15:53:34 +02003620 return 1;
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003621 } else if (ret) {
3622 return kvm_skip_emulated_instruction(&svm->vcpu);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003623 }
3624
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003625 ret = kvm_skip_emulated_instruction(&svm->vcpu);
3626
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003627 nested_vmcb = map.hva;
Ladi Prosekc2634062017-10-11 16:54:44 +02003628
3629 if (!nested_vmcb_checks(nested_vmcb)) {
3630 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3631 nested_vmcb->control.exit_code_hi = 0;
3632 nested_vmcb->control.exit_info_1 = 0;
3633 nested_vmcb->control.exit_info_2 = 0;
3634
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003635 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Ladi Prosekc2634062017-10-11 16:54:44 +02003636
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003637 return ret;
Ladi Prosekc2634062017-10-11 16:54:44 +02003638 }
3639
3640 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3641 nested_vmcb->save.rip,
3642 nested_vmcb->control.int_ctl,
3643 nested_vmcb->control.event_inj,
3644 nested_vmcb->control.nested_ctl);
3645
3646 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3647 nested_vmcb->control.intercept_cr >> 16,
3648 nested_vmcb->control.intercept_exceptions,
3649 nested_vmcb->control.intercept);
3650
3651 /* Clear internal status */
3652 kvm_clear_exception_queue(&svm->vcpu);
3653 kvm_clear_interrupt_queue(&svm->vcpu);
3654
3655 /*
3656 * Save the old vmcb, so we don't need to pick what we save, but can
3657 * restore everything when a VMEXIT occurs
3658 */
3659 hsave->save.es = vmcb->save.es;
3660 hsave->save.cs = vmcb->save.cs;
3661 hsave->save.ss = vmcb->save.ss;
3662 hsave->save.ds = vmcb->save.ds;
3663 hsave->save.gdtr = vmcb->save.gdtr;
3664 hsave->save.idtr = vmcb->save.idtr;
3665 hsave->save.efer = svm->vcpu.arch.efer;
3666 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3667 hsave->save.cr4 = svm->vcpu.arch.cr4;
3668 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3669 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3670 hsave->save.rsp = vmcb->save.rsp;
3671 hsave->save.rax = vmcb->save.rax;
3672 if (npt_enabled)
3673 hsave->save.cr3 = vmcb->save.cr3;
3674 else
3675 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3676
3677 copy_vmcb_control_area(hsave, vmcb);
3678
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003679 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, &map);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003680
Vitaly Kuznetsove7134c12019-08-13 15:53:34 +02003681 if (!nested_svm_vmrun_msrpm(svm)) {
3682 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3683 svm->vmcb->control.exit_code_hi = 0;
3684 svm->vmcb->control.exit_info_1 = 0;
3685 svm->vmcb->control.exit_info_2 = 0;
3686
3687 nested_svm_vmexit(svm);
3688 }
3689
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003690 return ret;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003691}
3692
Joerg Roedel9966bf62009-08-07 11:49:40 +02003693static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01003694{
3695 to_vmcb->save.fs = from_vmcb->save.fs;
3696 to_vmcb->save.gs = from_vmcb->save.gs;
3697 to_vmcb->save.tr = from_vmcb->save.tr;
3698 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3699 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3700 to_vmcb->save.star = from_vmcb->save.star;
3701 to_vmcb->save.lstar = from_vmcb->save.lstar;
3702 to_vmcb->save.cstar = from_vmcb->save.cstar;
3703 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3704 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3705 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3706 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01003707}
3708
Avi Kivity851ba692009-08-24 11:10:17 +03003709static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003710{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003711 struct vmcb *nested_vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003712 struct kvm_host_map map;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003713 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003714
Alexander Graf55426752008-11-25 20:17:06 +01003715 if (nested_svm_check_permissions(svm))
3716 return 1;
3717
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003718 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
3719 if (ret) {
3720 if (ret == -EINVAL)
3721 kvm_inject_gp(&svm->vcpu, 0);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003722 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003723 }
3724
3725 nested_vmcb = map.hva;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003726
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003727 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003728
Joerg Roedel9966bf62009-08-07 11:49:40 +02003729 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003730 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Alexander Graf55426752008-11-25 20:17:06 +01003731
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003732 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003733}
3734
Avi Kivity851ba692009-08-24 11:10:17 +03003735static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003736{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003737 struct vmcb *nested_vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003738 struct kvm_host_map map;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003739 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003740
Alexander Graf55426752008-11-25 20:17:06 +01003741 if (nested_svm_check_permissions(svm))
3742 return 1;
3743
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003744 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
3745 if (ret) {
3746 if (ret == -EINVAL)
3747 kvm_inject_gp(&svm->vcpu, 0);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003748 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003749 }
3750
3751 nested_vmcb = map.hva;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003752
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003753 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003754
Joerg Roedel9966bf62009-08-07 11:49:40 +02003755 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003756 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Alexander Graf55426752008-11-25 20:17:06 +01003757
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003758 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003759}
3760
Avi Kivity851ba692009-08-24 11:10:17 +03003761static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003762{
Alexander Graf3d6368e2008-11-25 20:17:07 +01003763 if (nested_svm_check_permissions(svm))
3764 return 1;
3765
Vitaly Kuznetsove7134c12019-08-13 15:53:34 +02003766 return nested_svm_vmrun(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003767}
3768
Avi Kivity851ba692009-08-24 11:10:17 +03003769static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003770{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003771 int ret;
3772
Alexander Graf1371d902008-11-25 20:17:04 +01003773 if (nested_svm_check_permissions(svm))
3774 return 1;
3775
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003776 /*
3777 * If VGIF is enabled, the STGI intercept is only added to
Ladi Prosekcc3d9672017-10-17 16:02:39 +02003778 * detect the opening of the SMI/NMI window; remove it now.
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003779 */
3780 if (vgif_enabled(svm))
3781 clr_intercept(svm, INTERCEPT_STGI);
3782
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003783 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003784 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003785
Joerg Roedel2af91942009-08-07 11:49:28 +02003786 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003787
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003788 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003789}
3790
Avi Kivity851ba692009-08-24 11:10:17 +03003791static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003792{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003793 int ret;
3794
Alexander Graf1371d902008-11-25 20:17:04 +01003795 if (nested_svm_check_permissions(svm))
3796 return 1;
3797
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003798 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003799
Joerg Roedel2af91942009-08-07 11:49:28 +02003800 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003801
3802 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05003803 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3804 svm_clear_vintr(svm);
3805 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3806 mark_dirty(svm->vmcb, VMCB_INTR);
3807 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003808
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003809 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003810}
3811
Avi Kivity851ba692009-08-24 11:10:17 +03003812static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02003813{
3814 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02003815
Sean Christophersonde3cd112019-04-30 10:36:17 -07003816 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_rcx_read(&svm->vcpu),
3817 kvm_rax_read(&svm->vcpu));
Joerg Roedelec1ff792009-10-09 16:08:31 +02003818
Alexander Grafff092382009-06-15 15:21:24 +02003819 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
Sean Christophersonde3cd112019-04-30 10:36:17 -07003820 kvm_mmu_invlpg(vcpu, kvm_rax_read(&svm->vcpu));
Alexander Grafff092382009-06-15 15:21:24 +02003821
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003822 return kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02003823}
3824
Joerg Roedel532a46b2009-10-09 16:08:32 +02003825static int skinit_interception(struct vcpu_svm *svm)
3826{
Sean Christophersonde3cd112019-04-30 10:36:17 -07003827 trace_kvm_skinit(svm->vmcb->save.rip, kvm_rax_read(&svm->vcpu));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003828
3829 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3830 return 1;
3831}
3832
David Kaplandab429a2015-03-02 13:43:37 -06003833static int wbinvd_interception(struct vcpu_svm *svm)
3834{
Kyle Huey6affcbe2016-11-29 12:40:40 -08003835 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06003836}
3837
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003838static int xsetbv_interception(struct vcpu_svm *svm)
3839{
3840 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07003841 u32 index = kvm_rcx_read(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003842
3843 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003844 return kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003845 }
3846
3847 return 1;
3848}
3849
Jim Mattson0cb84102019-09-19 15:59:17 -07003850static int rdpru_interception(struct vcpu_svm *svm)
3851{
3852 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3853 return 1;
3854}
3855
Avi Kivity851ba692009-08-24 11:10:17 +03003856static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857{
Izik Eidus37817f22008-03-24 23:14:53 +02003858 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003859 int reason;
3860 int int_type = svm->vmcb->control.exit_int_info &
3861 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003862 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003863 uint32_t type =
3864 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3865 uint32_t idt_v =
3866 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003867 bool has_error_code = false;
3868 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003869
3870 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003871
Izik Eidus37817f22008-03-24 23:14:53 +02003872 if (svm->vmcb->control.exit_info_2 &
3873 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003874 reason = TASK_SWITCH_IRET;
3875 else if (svm->vmcb->control.exit_info_2 &
3876 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3877 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003878 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003879 reason = TASK_SWITCH_GATE;
3880 else
3881 reason = TASK_SWITCH_CALL;
3882
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003883 if (reason == TASK_SWITCH_GATE) {
3884 switch (type) {
3885 case SVM_EXITINTINFO_TYPE_NMI:
3886 svm->vcpu.arch.nmi_injected = false;
3887 break;
3888 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003889 if (svm->vmcb->control.exit_info_2 &
3890 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3891 has_error_code = true;
3892 error_code =
3893 (u32)svm->vmcb->control.exit_info_2;
3894 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003895 kvm_clear_exception_queue(&svm->vcpu);
3896 break;
3897 case SVM_EXITINTINFO_TYPE_INTR:
3898 kvm_clear_interrupt_queue(&svm->vcpu);
3899 break;
3900 default:
3901 break;
3902 }
3903 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003904
Gleb Natapov8317c292009-04-12 13:37:02 +03003905 if (reason != TASK_SWITCH_GATE ||
3906 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3907 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02003908 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07003909 if (!skip_emulated_instruction(&svm->vcpu))
Sean Christopherson738fece2019-08-27 14:40:34 -07003910 return 0;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02003911 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003912
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003913 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3914 int_vec = -1;
3915
Sean Christopherson10517782019-08-27 14:40:35 -07003916 return kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07003917 has_error_code, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918}
3919
Avi Kivity851ba692009-08-24 11:10:17 +03003920static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003921{
Kyle Huey6a908b62016-11-29 12:40:37 -08003922 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923}
3924
Avi Kivity851ba692009-08-24 11:10:17 +03003925static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003926{
3927 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003928 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003929 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003930 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003931 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003932 return 1;
3933}
3934
Avi Kivity851ba692009-08-24 11:10:17 +03003935static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003936{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003937 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07003938 return kvm_emulate_instruction(&svm->vcpu, 0);
Andre Przywaradf4f31082010-12-21 11:12:06 +01003939
3940 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003941 return kvm_skip_emulated_instruction(&svm->vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003942}
3943
Avi Kivity851ba692009-08-24 11:10:17 +03003944static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003945{
Sean Christopherson60fc3d02019-08-27 14:40:38 -07003946 return kvm_emulate_instruction(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003947}
3948
Brijesh Singh7607b712018-02-19 10:14:44 -06003949static int rsm_interception(struct vcpu_svm *svm)
3950{
Sean Christopherson60fc3d02019-08-27 14:40:38 -07003951 return kvm_emulate_instruction_from_buffer(&svm->vcpu, rsm_ins_bytes, 2);
Brijesh Singh7607b712018-02-19 10:14:44 -06003952}
3953
Avi Kivity332b56e2011-11-10 14:57:24 +02003954static int rdpmc_interception(struct vcpu_svm *svm)
3955{
3956 int err;
3957
Paolo Bonzinid647eb62019-06-20 14:13:33 +02003958 if (!nrips)
Avi Kivity332b56e2011-11-10 14:57:24 +02003959 return emulate_on_interception(svm);
3960
3961 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08003962 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02003963}
3964
Xiubo Li52eb5a62015-03-13 17:39:45 +08003965static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3966 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003967{
3968 unsigned long cr0 = svm->vcpu.arch.cr0;
3969 bool ret = false;
3970 u64 intercept;
3971
3972 intercept = svm->nested.intercept;
3973
3974 if (!is_guest_mode(&svm->vcpu) ||
3975 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3976 return false;
3977
3978 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3979 val &= ~SVM_CR0_SELECTIVE_MASK;
3980
3981 if (cr0 ^ val) {
3982 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3983 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3984 }
3985
3986 return ret;
3987}
3988
Andre Przywara7ff76d52010-12-21 11:12:04 +01003989#define CR_VALID (1ULL << 63)
3990
3991static int cr_interception(struct vcpu_svm *svm)
3992{
3993 int reg, cr;
3994 unsigned long val;
3995 int err;
3996
3997 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3998 return emulate_on_interception(svm);
3999
4000 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
4001 return emulate_on_interception(svm);
4002
4003 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06004004 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
4005 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
4006 else
4007 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01004008
4009 err = 0;
4010 if (cr >= 16) { /* mov to cr */
4011 cr -= 16;
4012 val = kvm_register_read(&svm->vcpu, reg);
4013 switch (cr) {
4014 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02004015 if (!check_selective_cr0_intercepted(svm, val))
4016 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02004017 else
4018 return 1;
4019
Andre Przywara7ff76d52010-12-21 11:12:04 +01004020 break;
4021 case 3:
4022 err = kvm_set_cr3(&svm->vcpu, val);
4023 break;
4024 case 4:
4025 err = kvm_set_cr4(&svm->vcpu, val);
4026 break;
4027 case 8:
4028 err = kvm_set_cr8(&svm->vcpu, val);
4029 break;
4030 default:
4031 WARN(1, "unhandled write to CR%d", cr);
4032 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
4033 return 1;
4034 }
4035 } else { /* mov from cr */
4036 switch (cr) {
4037 case 0:
4038 val = kvm_read_cr0(&svm->vcpu);
4039 break;
4040 case 2:
4041 val = svm->vcpu.arch.cr2;
4042 break;
4043 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02004044 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01004045 break;
4046 case 4:
4047 val = kvm_read_cr4(&svm->vcpu);
4048 break;
4049 case 8:
4050 val = kvm_get_cr8(&svm->vcpu);
4051 break;
4052 default:
4053 WARN(1, "unhandled read from CR%d", cr);
4054 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
4055 return 1;
4056 }
4057 kvm_register_write(&svm->vcpu, reg, val);
4058 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08004059 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01004060}
4061
Andre Przywaracae37972010-12-21 11:12:05 +01004062static int dr_interception(struct vcpu_svm *svm)
4063{
4064 int reg, dr;
4065 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01004066
Paolo Bonzinifacb0132014-02-21 10:32:27 +01004067 if (svm->vcpu.guest_debug == 0) {
4068 /*
4069 * No more DR vmexits; force a reload of the debug registers
4070 * and reenter on this instruction. The next vmexit will
4071 * retrieve the full state of the debug registers.
4072 */
4073 clr_dr_intercepts(svm);
4074 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
4075 return 1;
4076 }
4077
Andre Przywaracae37972010-12-21 11:12:05 +01004078 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
4079 return emulate_on_interception(svm);
4080
4081 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
4082 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
4083
4084 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004085 if (!kvm_require_dr(&svm->vcpu, dr - 16))
4086 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01004087 val = kvm_register_read(&svm->vcpu, reg);
4088 kvm_set_dr(&svm->vcpu, dr - 16, val);
4089 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004090 if (!kvm_require_dr(&svm->vcpu, dr))
4091 return 1;
4092 kvm_get_dr(&svm->vcpu, dr, &val);
4093 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01004094 }
4095
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004096 return kvm_skip_emulated_instruction(&svm->vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01004097}
4098
Avi Kivity851ba692009-08-24 11:10:17 +03004099static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01004100{
Avi Kivity851ba692009-08-24 11:10:17 +03004101 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004102 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03004103
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004104 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
4105 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01004106 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004107 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01004108 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004109 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01004110 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01004111 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
4112 return 0;
4113}
4114
Tom Lendacky801e4592018-02-21 13:39:51 -06004115static int svm_get_msr_feature(struct kvm_msr_entry *msr)
4116{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004117 msr->data = 0;
4118
4119 switch (msr->index) {
4120 case MSR_F10H_DECFG:
4121 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
4122 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
4123 break;
4124 default:
4125 return 1;
4126 }
4127
4128 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004129}
4130
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004131static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004132{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004133 struct vcpu_svm *svm = to_svm(vcpu);
4134
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004135 switch (msr_info->index) {
Brian Gerst8c065852010-07-17 09:03:26 -04004136 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004137 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08004139#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004140 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004141 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004142 break;
4143 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004144 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145 break;
4146 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004147 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004148 break;
4149 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004150 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151 break;
4152#endif
4153 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004154 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 break;
4156 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004157 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004158 break;
4159 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004160 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004162 case MSR_TSC_AUX:
4163 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4164 return 1;
4165 msr_info->data = svm->tsc_aux;
4166 break;
Joerg Roedele0231712010-02-24 18:59:10 +01004167 /*
4168 * Nobody will change the following 5 values in the VMCB so we can
4169 * safely return them on rdmsr. They will always be 0 until LBRV is
4170 * implemented.
4171 */
Joerg Roedela2938c82008-02-13 16:30:28 +01004172 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004173 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01004174 break;
4175 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004176 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01004177 break;
4178 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004179 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01004180 break;
4181 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004182 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01004183 break;
4184 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004185 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01004186 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004187 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004188 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004189 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01004190 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004191 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01004192 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004193 case MSR_IA32_SPEC_CTRL:
4194 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -04004195 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
4196 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004197 return 1;
4198
4199 msr_info->data = svm->spec_ctrl;
4200 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02004201 case MSR_AMD64_VIRT_SPEC_CTRL:
4202 if (!msr_info->host_initiated &&
4203 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
4204 return 1;
4205
4206 msr_info->data = svm->virt_spec_ctrl;
4207 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01004208 case MSR_F15H_IC_CFG: {
4209
4210 int family, model;
4211
4212 family = guest_cpuid_family(vcpu);
4213 model = guest_cpuid_model(vcpu);
4214
4215 if (family < 0 || model < 0)
4216 return kvm_get_msr_common(vcpu, msr_info);
4217
4218 msr_info->data = 0;
4219
4220 if (family == 0x15 &&
4221 (model >= 0x2 && model < 0x20))
4222 msr_info->data = 0x1E;
4223 }
4224 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004225 case MSR_F10H_DECFG:
4226 msr_info->data = svm->msr_decfg;
4227 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004228 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004229 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004230 }
4231 return 0;
4232}
4233
Avi Kivity851ba692009-08-24 11:10:17 +03004234static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004235{
Sean Christopherson1edce0a2019-09-05 14:22:55 -07004236 return kvm_emulate_rdmsr(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004237}
4238
Joerg Roedel4a810182010-02-24 18:59:15 +01004239static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
4240{
4241 struct vcpu_svm *svm = to_svm(vcpu);
4242 int svm_dis, chg_mask;
4243
4244 if (data & ~SVM_VM_CR_VALID_MASK)
4245 return 1;
4246
4247 chg_mask = SVM_VM_CR_VALID_MASK;
4248
4249 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
4250 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
4251
4252 svm->nested.vm_cr_msr &= ~chg_mask;
4253 svm->nested.vm_cr_msr |= (data & chg_mask);
4254
4255 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
4256
4257 /* check for svm_disable while efer.svme is set */
4258 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
4259 return 1;
4260
4261 return 0;
4262}
4263
Will Auld8fe8ab42012-11-29 12:42:12 -08004264static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004265{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004266 struct vcpu_svm *svm = to_svm(vcpu);
4267
Will Auld8fe8ab42012-11-29 12:42:12 -08004268 u32 ecx = msr->index;
4269 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004270 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02004271 case MSR_IA32_CR_PAT:
4272 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4273 return 1;
4274 vcpu->arch.pat = data;
4275 svm->vmcb->save.g_pat = data;
4276 mark_dirty(svm->vmcb, VMCB_NPT);
4277 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004278 case MSR_IA32_SPEC_CTRL:
4279 if (!msr->host_initiated &&
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -04004280 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
4281 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004282 return 1;
4283
4284 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -04004285 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004286 return 1;
4287
4288 svm->spec_ctrl = data;
4289
4290 if (!data)
4291 break;
4292
4293 /*
4294 * For non-nested:
4295 * When it's written (to non-zero) for the first time, pass
4296 * it through.
4297 *
4298 * For nested:
4299 * The handling of the MSR bitmap for L2 guests is done in
4300 * nested_svm_vmrun_msrpm.
4301 * We update the L1 MSR bit as well since it will end up
4302 * touching the MSR anyway now.
4303 */
4304 set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
4305 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004306 case MSR_IA32_PRED_CMD:
4307 if (!msr->host_initiated &&
Borislav Petkove7c587d2018-05-02 18:15:14 +02004308 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB))
Ashok Raj15d45072018-02-01 22:59:43 +01004309 return 1;
4310
4311 if (data & ~PRED_CMD_IBPB)
4312 return 1;
4313
4314 if (!data)
4315 break;
4316
4317 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4318 if (is_guest_mode(vcpu))
4319 break;
4320 set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
4321 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02004322 case MSR_AMD64_VIRT_SPEC_CTRL:
4323 if (!msr->host_initiated &&
4324 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
4325 return 1;
4326
4327 if (data & ~SPEC_CTRL_SSBD)
4328 return 1;
4329
4330 svm->virt_spec_ctrl = data;
4331 break;
Brian Gerst8c065852010-07-17 09:03:26 -04004332 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004333 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004334 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08004335#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004337 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004338 break;
4339 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004340 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341 break;
4342 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004343 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004344 break;
4345 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004346 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004347 break;
4348#endif
4349 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004350 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351 break;
4352 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02004353 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004354 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004355 break;
4356 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02004357 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004358 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004360 case MSR_TSC_AUX:
4361 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4362 return 1;
4363
4364 /*
4365 * This is rare, so we update the MSR here instead of using
4366 * direct_access_msrs. Doing that would require a rdmsr in
4367 * svm_vcpu_put.
4368 */
4369 svm->tsc_aux = data;
4370 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
4371 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01004372 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004373 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03004374 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
4375 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004376 break;
4377 }
4378 if (data & DEBUGCTL_RESERVED_BITS)
4379 return 1;
4380
4381 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01004382 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004383 if (data & (1ULL<<0))
4384 svm_enable_lbrv(svm);
4385 else
4386 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01004387 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004388 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02004389 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004390 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004391 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01004392 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004393 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03004394 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004395 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004396 case MSR_F10H_DECFG: {
4397 struct kvm_msr_entry msr_entry;
4398
4399 msr_entry.index = msr->index;
4400 if (svm_get_msr_feature(&msr_entry))
4401 return 1;
4402
4403 /* Check the supported bits */
4404 if (data & ~msr_entry.data)
4405 return 1;
4406
4407 /* Don't allow the guest to change a bit, #GP */
4408 if (!msr->host_initiated && (data ^ msr_entry.data))
4409 return 1;
4410
4411 svm->msr_decfg = data;
4412 break;
4413 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004414 case MSR_IA32_APICBASE:
4415 if (kvm_vcpu_apicv_active(vcpu))
4416 avic_update_vapic_bar(to_svm(vcpu), data);
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004417 /* Fall through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004418 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08004419 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004420 }
4421 return 0;
4422}
4423
Avi Kivity851ba692009-08-24 11:10:17 +03004424static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004425{
Sean Christopherson1edce0a2019-09-05 14:22:55 -07004426 return kvm_emulate_wrmsr(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004427}
4428
Avi Kivity851ba692009-08-24 11:10:17 +03004429static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004430{
Rusty Russelle756fc62007-07-30 20:07:08 +10004431 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03004432 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433 else
Avi Kivity851ba692009-08-24 11:10:17 +03004434 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004435}
4436
Avi Kivity851ba692009-08-24 11:10:17 +03004437static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08004438{
Avi Kivity3842d132010-07-27 12:30:24 +03004439 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01004440 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03004441 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004442 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08004443 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08004444 return 1;
4445}
4446
Mark Langsdorf565d0992009-10-06 14:25:02 -05004447static int pause_interception(struct vcpu_svm *svm)
4448{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004449 struct kvm_vcpu *vcpu = &svm->vcpu;
4450 bool in_kernel = (svm_get_cpl(vcpu) == 0);
4451
Babu Moger8566ac82018-03-16 16:37:26 -04004452 if (pause_filter_thresh)
4453 grow_ple_window(vcpu);
4454
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004455 kvm_vcpu_on_spin(vcpu, in_kernel);
Mark Langsdorf565d0992009-10-06 14:25:02 -05004456 return 1;
4457}
4458
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004459static int nop_interception(struct vcpu_svm *svm)
4460{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004461 return kvm_skip_emulated_instruction(&(svm->vcpu));
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004462}
4463
4464static int monitor_interception(struct vcpu_svm *svm)
4465{
4466 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
4467 return nop_interception(svm);
4468}
4469
4470static int mwait_interception(struct vcpu_svm *svm)
4471{
4472 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4473 return nop_interception(svm);
4474}
4475
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004476enum avic_ipi_failure_cause {
4477 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
4478 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
4479 AVIC_IPI_FAILURE_INVALID_TARGET,
4480 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
4481};
4482
4483static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
4484{
4485 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
4486 u32 icrl = svm->vmcb->control.exit_info_1;
4487 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03004488 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004489 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4490
4491 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
4492
4493 switch (id) {
4494 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
4495 /*
4496 * AVIC hardware handles the generation of
4497 * IPIs when the specified Message Type is Fixed
4498 * (also known as fixed delivery mode) and
4499 * the Trigger Mode is edge-triggered. The hardware
4500 * also supports self and broadcast delivery modes
4501 * specified via the Destination Shorthand(DSH)
4502 * field of the ICRL. Logical and physical APIC ID
4503 * formats are supported. All other IPI types cause
4504 * a #VMEXIT, which needs to emulated.
4505 */
4506 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
4507 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
4508 break;
4509 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
Suthikulpanit, Suravee4a580382019-03-20 08:12:28 +00004510 int i;
4511 struct kvm_vcpu *vcpu;
4512 struct kvm *kvm = svm->vcpu.kvm;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004513 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4514
4515 /*
Suthikulpanit, Suravee4a580382019-03-20 08:12:28 +00004516 * At this point, we expect that the AVIC HW has already
4517 * set the appropriate IRR bits on the valid target
4518 * vcpus. So, we just need to kick the appropriate vcpu.
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004519 */
Suthikulpanit, Suravee4a580382019-03-20 08:12:28 +00004520 kvm_for_each_vcpu(i, vcpu, kvm) {
4521 bool m = kvm_apic_match_dest(vcpu, apic,
Peter Xuac8ef992019-12-04 20:07:19 +01004522 icrl & APIC_SHORT_MASK,
Suthikulpanit, Suravee4a580382019-03-20 08:12:28 +00004523 GET_APIC_DEST_FIELD(icrh),
Peter Xuac8ef992019-12-04 20:07:19 +01004524 icrl & APIC_DEST_MASK);
Suthikulpanit, Suravee4a580382019-03-20 08:12:28 +00004525
4526 if (m && !avic_vcpu_is_running(vcpu))
4527 kvm_vcpu_wake_up(vcpu);
4528 }
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004529 break;
4530 }
4531 case AVIC_IPI_FAILURE_INVALID_TARGET:
Suravee Suthikulpanit37ef0c42019-01-22 10:24:19 +00004532 WARN_ONCE(1, "Invalid IPI target: index=%u, vcpu=%d, icr=%#0x:%#0x\n",
4533 index, svm->vcpu.vcpu_id, icrh, icrl);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004534 break;
4535 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
4536 WARN_ONCE(1, "Invalid backing page\n");
4537 break;
4538 default:
4539 pr_err("Unknown IPI interception\n");
4540 }
4541
4542 return 1;
4543}
4544
4545static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
4546{
Sean Christopherson81811c12018-03-20 12:17:21 -07004547 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004548 int index;
4549 u32 *logical_apic_id_table;
4550 int dlid = GET_APIC_LOGICAL_ID(ldr);
4551
4552 if (!dlid)
4553 return NULL;
4554
4555 if (flat) { /* flat */
4556 index = ffs(dlid) - 1;
4557 if (index > 7)
4558 return NULL;
4559 } else { /* cluster */
4560 int cluster = (dlid & 0xf0) >> 4;
4561 int apic = ffs(dlid & 0x0f) - 1;
4562
4563 if ((apic < 0) || (apic > 7) ||
4564 (cluster >= 0xf))
4565 return NULL;
4566 index = (cluster << 2) + apic;
4567 }
4568
Sean Christopherson81811c12018-03-20 12:17:21 -07004569 logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004570
4571 return &logical_apic_id_table[index];
4572}
4573
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004574static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr)
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004575{
4576 bool flat;
4577 u32 *entry, new_entry;
4578
4579 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4580 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4581 if (!entry)
4582 return -EINVAL;
4583
4584 new_entry = READ_ONCE(*entry);
4585 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4586 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004587 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004588 WRITE_ONCE(*entry, new_entry);
4589
4590 return 0;
4591}
4592
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004593static void avic_invalidate_logical_id_entry(struct kvm_vcpu *vcpu)
4594{
4595 struct vcpu_svm *svm = to_svm(vcpu);
4596 bool flat = svm->dfr_reg == APIC_DFR_FLAT;
4597 u32 *entry = avic_get_logical_id_entry(vcpu, svm->ldr_reg, flat);
4598
4599 if (entry)
Suthikulpanit, Suraveee44e3ea2019-03-26 03:57:37 +00004600 clear_bit(AVIC_LOGICAL_ID_ENTRY_VALID_BIT, (unsigned long *)entry);
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004601}
4602
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004603static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4604{
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004605 int ret = 0;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004606 struct vcpu_svm *svm = to_svm(vcpu);
4607 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
Miaohe Lin5c94ac52019-10-18 10:50:31 +08004608 u32 id = kvm_xapic_id(vcpu->arch.apic);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004609
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004610 if (ldr == svm->ldr_reg)
4611 return 0;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004612
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004613 avic_invalidate_logical_id_entry(vcpu);
4614
4615 if (ldr)
Miaohe Lin5c94ac52019-10-18 10:50:31 +08004616 ret = avic_ldr_write(vcpu, id, ldr);
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004617
4618 if (!ret)
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004619 svm->ldr_reg = ldr;
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004620
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004621 return ret;
4622}
4623
4624static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4625{
4626 u64 *old, *new;
4627 struct vcpu_svm *svm = to_svm(vcpu);
Miaohe Lin5c94ac52019-10-18 10:50:31 +08004628 u32 id = kvm_xapic_id(vcpu->arch.apic);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004629
4630 if (vcpu->vcpu_id == id)
4631 return 0;
4632
4633 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4634 new = avic_get_physical_id_entry(vcpu, id);
4635 if (!new || !old)
4636 return 1;
4637
4638 /* We need to move physical_id_entry to new offset */
4639 *new = *old;
4640 *old = 0ULL;
4641 to_svm(vcpu)->avic_physical_id_cache = new;
4642
4643 /*
4644 * Also update the guest physical APIC ID in the logical
4645 * APIC ID table entry if already setup the LDR.
4646 */
4647 if (svm->ldr_reg)
4648 avic_handle_ldr_update(vcpu);
4649
4650 return 0;
4651}
4652
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004653static void avic_handle_dfr_update(struct kvm_vcpu *vcpu)
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004654{
4655 struct vcpu_svm *svm = to_svm(vcpu);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004656 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004657
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004658 if (svm->dfr_reg == dfr)
4659 return;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004660
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004661 avic_invalidate_logical_id_entry(vcpu);
4662 svm->dfr_reg = dfr;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004663}
4664
4665static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4666{
4667 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4668 u32 offset = svm->vmcb->control.exit_info_1 &
4669 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4670
4671 switch (offset) {
4672 case APIC_ID:
4673 if (avic_handle_apic_id_update(&svm->vcpu))
4674 return 0;
4675 break;
4676 case APIC_LDR:
4677 if (avic_handle_ldr_update(&svm->vcpu))
4678 return 0;
4679 break;
4680 case APIC_DFR:
4681 avic_handle_dfr_update(&svm->vcpu);
4682 break;
4683 default:
4684 break;
4685 }
4686
4687 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4688
4689 return 1;
4690}
4691
4692static bool is_avic_unaccelerated_access_trap(u32 offset)
4693{
4694 bool ret = false;
4695
4696 switch (offset) {
4697 case APIC_ID:
4698 case APIC_EOI:
4699 case APIC_RRR:
4700 case APIC_LDR:
4701 case APIC_DFR:
4702 case APIC_SPIV:
4703 case APIC_ESR:
4704 case APIC_ICR:
4705 case APIC_LVTT:
4706 case APIC_LVTTHMR:
4707 case APIC_LVTPC:
4708 case APIC_LVT0:
4709 case APIC_LVT1:
4710 case APIC_LVTERR:
4711 case APIC_TMICT:
4712 case APIC_TDCR:
4713 ret = true;
4714 break;
4715 default:
4716 break;
4717 }
4718 return ret;
4719}
4720
4721static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4722{
4723 int ret = 0;
4724 u32 offset = svm->vmcb->control.exit_info_1 &
4725 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4726 u32 vector = svm->vmcb->control.exit_info_2 &
4727 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4728 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4729 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4730 bool trap = is_avic_unaccelerated_access_trap(offset);
4731
4732 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4733 trap, write, vector);
4734 if (trap) {
4735 /* Handling Trap */
4736 WARN_ONCE(!write, "svm: Handling trap read.\n");
4737 ret = avic_unaccel_trap_write(svm);
4738 } else {
4739 /* Handling Fault */
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004740 ret = kvm_emulate_instruction(&svm->vcpu, 0);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004741 }
4742
4743 return ret;
4744}
4745
Mathias Krause09941fb2012-08-30 01:30:20 +02004746static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01004747 [SVM_EXIT_READ_CR0] = cr_interception,
4748 [SVM_EXIT_READ_CR3] = cr_interception,
4749 [SVM_EXIT_READ_CR4] = cr_interception,
4750 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06004751 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02004752 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01004753 [SVM_EXIT_WRITE_CR3] = cr_interception,
4754 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004755 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01004756 [SVM_EXIT_READ_DR0] = dr_interception,
4757 [SVM_EXIT_READ_DR1] = dr_interception,
4758 [SVM_EXIT_READ_DR2] = dr_interception,
4759 [SVM_EXIT_READ_DR3] = dr_interception,
4760 [SVM_EXIT_READ_DR4] = dr_interception,
4761 [SVM_EXIT_READ_DR5] = dr_interception,
4762 [SVM_EXIT_READ_DR6] = dr_interception,
4763 [SVM_EXIT_READ_DR7] = dr_interception,
4764 [SVM_EXIT_WRITE_DR0] = dr_interception,
4765 [SVM_EXIT_WRITE_DR1] = dr_interception,
4766 [SVM_EXIT_WRITE_DR2] = dr_interception,
4767 [SVM_EXIT_WRITE_DR3] = dr_interception,
4768 [SVM_EXIT_WRITE_DR4] = dr_interception,
4769 [SVM_EXIT_WRITE_DR5] = dr_interception,
4770 [SVM_EXIT_WRITE_DR6] = dr_interception,
4771 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004772 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4773 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004774 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004775 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004776 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01004777 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Liran Alon97184202018-03-12 13:12:52 +02004778 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004779 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02004780 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004781 [SVM_EXIT_SMI] = nop_on_interception,
4782 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08004783 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02004784 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004785 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004786 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02004787 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05004788 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004789 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004790 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02004791 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004792 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004793 [SVM_EXIT_MSR] = msr_interception,
4794 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08004795 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01004796 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02004797 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01004798 [SVM_EXIT_VMLOAD] = vmload_interception,
4799 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01004800 [SVM_EXIT_STGI] = stgi_interception,
4801 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02004802 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06004803 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004804 [SVM_EXIT_MONITOR] = monitor_interception,
4805 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01004806 [SVM_EXIT_XSETBV] = xsetbv_interception,
Jim Mattson0cb84102019-09-19 15:59:17 -07004807 [SVM_EXIT_RDPRU] = rdpru_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02004808 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06004809 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004810 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4811 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004812};
4813
Joe Perchesae8cc052011-04-24 22:00:50 -07004814static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02004815{
4816 struct vcpu_svm *svm = to_svm(vcpu);
4817 struct vmcb_control_area *control = &svm->vmcb->control;
4818 struct vmcb_save_area *save = &svm->vmcb->save;
4819
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02004820 if (!dump_invalid_vmcb) {
4821 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
4822 return;
4823 }
4824
Joerg Roedel3f10c842010-05-05 16:04:42 +02004825 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004826 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4827 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4828 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4829 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4830 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4831 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4832 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
Babu Moger1d8fb442018-03-16 16:37:25 -04004833 pr_err("%-20s%d\n", "pause filter threshold:",
4834 control->pause_filter_thresh);
Joe Perchesae8cc052011-04-24 22:00:50 -07004835 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4836 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4837 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4838 pr_err("%-20s%d\n", "asid:", control->asid);
4839 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4840 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4841 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4842 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4843 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4844 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4845 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4846 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4847 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4848 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4849 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004850 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07004851 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4852 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05004853 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07004854 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004855 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4856 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4857 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004858 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004859 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4860 "es:",
4861 save->es.selector, save->es.attrib,
4862 save->es.limit, save->es.base);
4863 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4864 "cs:",
4865 save->cs.selector, save->cs.attrib,
4866 save->cs.limit, save->cs.base);
4867 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4868 "ss:",
4869 save->ss.selector, save->ss.attrib,
4870 save->ss.limit, save->ss.base);
4871 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4872 "ds:",
4873 save->ds.selector, save->ds.attrib,
4874 save->ds.limit, save->ds.base);
4875 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4876 "fs:",
4877 save->fs.selector, save->fs.attrib,
4878 save->fs.limit, save->fs.base);
4879 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4880 "gs:",
4881 save->gs.selector, save->gs.attrib,
4882 save->gs.limit, save->gs.base);
4883 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4884 "gdtr:",
4885 save->gdtr.selector, save->gdtr.attrib,
4886 save->gdtr.limit, save->gdtr.base);
4887 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4888 "ldtr:",
4889 save->ldtr.selector, save->ldtr.attrib,
4890 save->ldtr.limit, save->ldtr.base);
4891 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4892 "idtr:",
4893 save->idtr.selector, save->idtr.attrib,
4894 save->idtr.limit, save->idtr.base);
4895 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4896 "tr:",
4897 save->tr.selector, save->tr.attrib,
4898 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004899 pr_err("cpl: %d efer: %016llx\n",
4900 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004901 pr_err("%-15s %016llx %-13s %016llx\n",
4902 "cr0:", save->cr0, "cr2:", save->cr2);
4903 pr_err("%-15s %016llx %-13s %016llx\n",
4904 "cr3:", save->cr3, "cr4:", save->cr4);
4905 pr_err("%-15s %016llx %-13s %016llx\n",
4906 "dr6:", save->dr6, "dr7:", save->dr7);
4907 pr_err("%-15s %016llx %-13s %016llx\n",
4908 "rip:", save->rip, "rflags:", save->rflags);
4909 pr_err("%-15s %016llx %-13s %016llx\n",
4910 "rsp:", save->rsp, "rax:", save->rax);
4911 pr_err("%-15s %016llx %-13s %016llx\n",
4912 "star:", save->star, "lstar:", save->lstar);
4913 pr_err("%-15s %016llx %-13s %016llx\n",
4914 "cstar:", save->cstar, "sfmask:", save->sfmask);
4915 pr_err("%-15s %016llx %-13s %016llx\n",
4916 "kernel_gs_base:", save->kernel_gs_base,
4917 "sysenter_cs:", save->sysenter_cs);
4918 pr_err("%-15s %016llx %-13s %016llx\n",
4919 "sysenter_esp:", save->sysenter_esp,
4920 "sysenter_eip:", save->sysenter_eip);
4921 pr_err("%-15s %016llx %-13s %016llx\n",
4922 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4923 pr_err("%-15s %016llx %-13s %016llx\n",
4924 "br_from:", save->br_from, "br_to:", save->br_to);
4925 pr_err("%-15s %016llx %-13s %016llx\n",
4926 "excp_from:", save->last_excp_from,
4927 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004928}
4929
Avi Kivity586f9602010-11-18 13:09:54 +02004930static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4931{
4932 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4933
4934 *info1 = control->exit_info_1;
4935 *info2 = control->exit_info_2;
4936}
4937
Wanpeng Li1e9e2622019-11-21 11:17:11 +08004938static int handle_exit(struct kvm_vcpu *vcpu,
4939 enum exit_fastpath_completion exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004940{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004941 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004942 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004943 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004944
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004945 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4946
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004947 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004948 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4949 if (npt_enabled)
4950 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004951
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004952 if (unlikely(svm->nested.exit_required)) {
4953 nested_svm_vmexit(svm);
4954 svm->nested.exit_required = false;
4955
4956 return 1;
4957 }
4958
Joerg Roedel20307532010-11-29 17:51:48 +01004959 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004960 int vmexit;
4961
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004962 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4963 svm->vmcb->control.exit_info_1,
4964 svm->vmcb->control.exit_info_2,
4965 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004966 svm->vmcb->control.exit_int_info_err,
4967 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004968
Joerg Roedel410e4d52009-08-07 11:49:44 +02004969 vmexit = nested_svm_exit_special(svm);
4970
4971 if (vmexit == NESTED_EXIT_CONTINUE)
4972 vmexit = nested_svm_exit_handled(svm);
4973
4974 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004975 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004976 }
4977
Joerg Roedela5c38322009-08-07 11:49:32 +02004978 svm_complete_interrupts(svm);
4979
Avi Kivity04d2cc72007-09-10 18:10:54 +03004980 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4981 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4982 kvm_run->fail_entry.hardware_entry_failure_reason
4983 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004984 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004985 return 0;
4986 }
4987
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004988 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004989 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004990 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4991 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004992 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004993 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004994 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004995 exit_code);
4996
Wanpeng Li1e9e2622019-11-21 11:17:11 +08004997 if (exit_fastpath == EXIT_FASTPATH_SKIP_EMUL_INS) {
4998 kvm_skip_emulated_instruction(vcpu);
4999 return 1;
5000 } else if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08005001 || !svm_exit_handlers[exit_code]) {
Liran Alon7396d332019-08-26 13:16:43 +03005002 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%x\n", exit_code);
5003 dump_vmcb(vcpu);
5004 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5005 vcpu->run->internal.suberror =
5006 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
5007 vcpu->run->internal.ndata = 1;
5008 vcpu->run->internal.data[0] = exit_code;
5009 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005010 }
5011
Andrea Arcangeli3dcb2a32019-11-04 18:00:00 -05005012#ifdef CONFIG_RETPOLINE
5013 if (exit_code == SVM_EXIT_MSR)
5014 return msr_interception(svm);
5015 else if (exit_code == SVM_EXIT_VINTR)
5016 return interrupt_window_interception(svm);
5017 else if (exit_code == SVM_EXIT_INTR)
5018 return intr_interception(svm);
5019 else if (exit_code == SVM_EXIT_HLT)
5020 return halt_interception(svm);
5021 else if (exit_code == SVM_EXIT_NPF)
5022 return npf_interception(svm);
5023#endif
Avi Kivity851ba692009-08-24 11:10:17 +03005024 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005025}
5026
5027static void reload_tss(struct kvm_vcpu *vcpu)
5028{
5029 int cpu = raw_smp_processor_id();
5030
Tejun Heo0fe1e002009-10-29 22:34:14 +09005031 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
5032 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005033 load_TR_desc();
5034}
5035
Brijesh Singh70cd94e2017-12-04 10:57:34 -06005036static void pre_sev_run(struct vcpu_svm *svm, int cpu)
5037{
5038 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
5039 int asid = sev_get_asid(svm->vcpu.kvm);
5040
5041 /* Assign the asid allocated with this SEV guest */
5042 svm->vmcb->control.asid = asid;
5043
5044 /*
5045 * Flush guest TLB:
5046 *
5047 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
5048 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
5049 */
5050 if (sd->sev_vmcbs[asid] == svm->vmcb &&
5051 svm->last_cpu == cpu)
5052 return;
5053
5054 svm->last_cpu = cpu;
5055 sd->sev_vmcbs[asid] = svm->vmcb;
5056 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5057 mark_dirty(svm->vmcb, VMCB_ASID);
5058}
5059
Rusty Russelle756fc62007-07-30 20:07:08 +10005060static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005061{
5062 int cpu = raw_smp_processor_id();
5063
Tejun Heo0fe1e002009-10-29 22:34:14 +09005064 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005065
Brijesh Singh70cd94e2017-12-04 10:57:34 -06005066 if (sev_guest(svm->vcpu.kvm))
5067 return pre_sev_run(svm, cpu);
5068
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03005069 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09005070 if (svm->asid_generation != sd->asid_generation)
5071 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005072}
5073
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005074static void svm_inject_nmi(struct kvm_vcpu *vcpu)
5075{
5076 struct vcpu_svm *svm = to_svm(vcpu);
5077
5078 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
5079 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005080 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005081 ++vcpu->stat.nmi_injections;
5082}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005083
Eddie Dong85f455f2007-07-06 12:20:49 +03005084static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005085{
5086 struct vmcb_control_area *control;
5087
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005088 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10005089 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03005090 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005091 control->int_ctl &= ~V_INTR_PRIO_MASK;
5092 control->int_ctl |= V_IRQ_MASK |
5093 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01005094 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095}
5096
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005097static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03005098{
5099 struct vcpu_svm *svm = to_svm(vcpu);
5100
Joerg Roedel2af91942009-08-07 11:49:28 +02005101 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01005102
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03005103 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
5104 ++vcpu->stat.irq_injections;
5105
Alexander Graf219b65d2009-06-15 15:21:25 +02005106 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
5107 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03005108}
5109
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005110static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
5111{
5112 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
5113}
5114
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005115static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
5116{
5117 struct vcpu_svm *svm = to_svm(vcpu);
5118
Liran Alon49d654d2019-11-11 14:26:21 +02005119 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005120 return;
5121
Radim Krčmář596f3142014-03-11 19:11:18 +01005122 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
5123
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005124 if (irr == -1)
5125 return;
5126
5127 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005128 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005129}
5130
Jim Mattson8d860bb2018-05-09 16:56:05 -04005131static void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08005132{
5133 return;
5134}
5135
Suthikulpanit, Suravee2cf9af02019-09-13 19:00:49 +00005136static bool svm_get_enable_apicv(struct kvm *kvm)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005137{
Suthikulpanit, Suravee2cf9af02019-09-13 19:00:49 +00005138 return avic && irqchip_split(kvm);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005139}
5140
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005141static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
5142{
5143}
5144
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005145static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005146{
5147}
5148
5149/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03005150static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5151{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005152 struct vcpu_svm *svm = to_svm(vcpu);
5153 struct vmcb *vmcb = svm->vmcb;
5154
Suthikulpanit, Suraveec57cd3c2019-01-29 08:09:46 +00005155 if (kvm_vcpu_apicv_active(vcpu))
5156 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
5157 else
5158 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
5159 mark_dirty(vmcb, VMCB_AVIC);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005160}
5161
Andrey Smetanin63086302015-11-10 15:36:32 +03005162static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005163{
5164 return;
5165}
5166
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005167static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
5168{
5169 kvm_lapic_set_irr(vec, vcpu->arch.apic);
5170 smp_mb__after_atomic();
5171
Suthikulpanit, Suravee0532dd52019-05-03 06:38:53 -07005172 if (avic_vcpu_is_running(vcpu)) {
5173 int cpuid = vcpu->cpu;
5174
5175 if (cpuid != get_cpu())
5176 wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpuid));
5177 put_cpu();
5178 } else
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005179 kvm_vcpu_wake_up(vcpu);
5180}
5181
Wanpeng Li17e433b2019-08-05 10:03:19 +08005182static bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
5183{
5184 return false;
5185}
5186
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005187static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
5188{
5189 unsigned long flags;
5190 struct amd_svm_iommu_ir *cur;
5191
5192 spin_lock_irqsave(&svm->ir_list_lock, flags);
5193 list_for_each_entry(cur, &svm->ir_list, node) {
5194 if (cur->data != pi->ir_data)
5195 continue;
5196 list_del(&cur->node);
5197 kfree(cur);
5198 break;
5199 }
5200 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
5201}
5202
5203static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
5204{
5205 int ret = 0;
5206 unsigned long flags;
5207 struct amd_svm_iommu_ir *ir;
5208
5209 /**
5210 * In some cases, the existing irte is updaed and re-set,
5211 * so we need to check here if it's already been * added
5212 * to the ir_list.
5213 */
5214 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
5215 struct kvm *kvm = svm->vcpu.kvm;
5216 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
5217 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
5218 struct vcpu_svm *prev_svm;
5219
5220 if (!prev_vcpu) {
5221 ret = -EINVAL;
5222 goto out;
5223 }
5224
5225 prev_svm = to_svm(prev_vcpu);
5226 svm_ir_list_del(prev_svm, pi);
5227 }
5228
5229 /**
5230 * Allocating new amd_iommu_pi_data, which will get
5231 * add to the per-vcpu ir_list.
5232 */
Ben Gardon1ec69642019-02-11 11:02:51 -08005233 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL_ACCOUNT);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005234 if (!ir) {
5235 ret = -ENOMEM;
5236 goto out;
5237 }
5238 ir->data = pi->ir_data;
5239
5240 spin_lock_irqsave(&svm->ir_list_lock, flags);
5241 list_add(&ir->node, &svm->ir_list);
5242 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
5243out:
5244 return ret;
5245}
5246
5247/**
5248 * Note:
5249 * The HW cannot support posting multicast/broadcast
5250 * interrupts to a vCPU. So, we still use legacy interrupt
5251 * remapping for these kind of interrupts.
5252 *
5253 * For lowest-priority interrupts, we only support
5254 * those with single CPU as the destination, e.g. user
5255 * configures the interrupts via /proc/irq or uses
5256 * irqbalance to make the interrupts single-CPU.
5257 */
5258static int
5259get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
5260 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
5261{
5262 struct kvm_lapic_irq irq;
5263 struct kvm_vcpu *vcpu = NULL;
5264
5265 kvm_set_msi_irq(kvm, e, &irq);
5266
Alexander Graffdcf7562019-09-05 14:58:18 +02005267 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
5268 !kvm_irq_is_postable(&irq)) {
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005269 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
5270 __func__, irq.vector);
5271 return -1;
5272 }
5273
5274 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
5275 irq.vector);
5276 *svm = to_svm(vcpu);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005277 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005278 vcpu_info->vector = irq.vector;
5279
5280 return 0;
5281}
5282
5283/*
5284 * svm_update_pi_irte - set IRTE for Posted-Interrupts
5285 *
5286 * @kvm: kvm
5287 * @host_irq: host irq of the interrupt
5288 * @guest_irq: gsi of the interrupt
5289 * @set: set or unset PI
5290 * returns 0 on success, < 0 on failure
5291 */
5292static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
5293 uint32_t guest_irq, bool set)
5294{
5295 struct kvm_kernel_irq_routing_entry *e;
5296 struct kvm_irq_routing_table *irq_rt;
5297 int idx, ret = -EINVAL;
5298
5299 if (!kvm_arch_has_assigned_device(kvm) ||
5300 !irq_remapping_cap(IRQ_POSTING_CAP))
5301 return 0;
5302
5303 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
5304 __func__, host_irq, guest_irq, set);
5305
5306 idx = srcu_read_lock(&kvm->irq_srcu);
5307 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
5308 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
5309
5310 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
5311 struct vcpu_data vcpu_info;
5312 struct vcpu_svm *svm = NULL;
5313
5314 if (e->type != KVM_IRQ_ROUTING_MSI)
5315 continue;
5316
5317 /**
5318 * Here, we setup with legacy mode in the following cases:
5319 * 1. When cannot target interrupt to a specific vcpu.
5320 * 2. Unsetting posted interrupt.
5321 * 3. APIC virtialization is disabled for the vcpu.
Alexander Graffdcf7562019-09-05 14:58:18 +02005322 * 4. IRQ has incompatible delivery mode (SMI, INIT, etc)
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005323 */
5324 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
5325 kvm_vcpu_apicv_active(&svm->vcpu)) {
5326 struct amd_iommu_pi_data pi;
5327
5328 /* Try to enable guest_mode in IRTE */
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005329 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
5330 AVIC_HPA_MASK);
Sean Christopherson81811c12018-03-20 12:17:21 -07005331 pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005332 svm->vcpu.vcpu_id);
5333 pi.is_guest_mode = true;
5334 pi.vcpu_data = &vcpu_info;
5335 ret = irq_set_vcpu_affinity(host_irq, &pi);
5336
5337 /**
5338 * Here, we successfully setting up vcpu affinity in
5339 * IOMMU guest mode. Now, we need to store the posted
5340 * interrupt information in a per-vcpu ir_list so that
5341 * we can reference to them directly when we update vcpu
5342 * scheduling information in IOMMU irte.
5343 */
5344 if (!ret && pi.is_guest_mode)
5345 svm_ir_list_add(svm, &pi);
5346 } else {
5347 /* Use legacy mode in IRTE */
5348 struct amd_iommu_pi_data pi;
5349
5350 /**
5351 * Here, pi is used to:
5352 * - Tell IOMMU to use legacy mode for this interrupt.
5353 * - Retrieve ga_tag of prior interrupt remapping data.
5354 */
5355 pi.is_guest_mode = false;
5356 ret = irq_set_vcpu_affinity(host_irq, &pi);
5357
5358 /**
5359 * Check if the posted interrupt was previously
5360 * setup with the guest_mode by checking if the ga_tag
5361 * was cached. If so, we need to clean up the per-vcpu
5362 * ir_list.
5363 */
5364 if (!ret && pi.prev_ga_tag) {
5365 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
5366 struct kvm_vcpu *vcpu;
5367
5368 vcpu = kvm_get_vcpu_by_id(kvm, id);
5369 if (vcpu)
5370 svm_ir_list_del(to_svm(vcpu), &pi);
5371 }
5372 }
5373
5374 if (!ret && svm) {
hu huajun2698d822018-04-11 15:16:40 +08005375 trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id,
5376 e->gsi, vcpu_info.vector,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005377 vcpu_info.pi_desc_addr, set);
5378 }
5379
5380 if (ret < 0) {
5381 pr_err("%s: failed to update PI IRTE\n", __func__);
5382 goto out;
5383 }
5384 }
5385
5386 ret = 0;
5387out:
5388 srcu_read_unlock(&kvm->irq_srcu, idx);
5389 return ret;
5390}
5391
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005392static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005393{
5394 struct vcpu_svm *svm = to_svm(vcpu);
5395 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02005396 int ret;
5397 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
5398 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
5399 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
5400
5401 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005402}
5403
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005404static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
5405{
5406 struct vcpu_svm *svm = to_svm(vcpu);
5407
5408 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
5409}
5410
5411static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5412{
5413 struct vcpu_svm *svm = to_svm(vcpu);
5414
5415 if (masked) {
5416 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005417 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005418 } else {
5419 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005420 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005421 }
5422}
5423
Gleb Natapov78646122009-03-23 12:12:11 +02005424static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
5425{
5426 struct vcpu_svm *svm = to_svm(vcpu);
5427 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005428 int ret;
5429
5430 if (!gif_set(svm) ||
5431 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
5432 return 0;
5433
Avi Kivityf6e78472010-08-02 15:30:20 +03005434 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005435
Joerg Roedel20307532010-11-29 17:51:48 +01005436 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005437 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
5438
5439 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02005440}
5441
Jan Kiszkac9a79532014-03-07 20:03:15 +01005442static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03005443{
Alexander Graf219b65d2009-06-15 15:21:25 +02005444 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02005445
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005446 if (kvm_vcpu_apicv_active(vcpu))
5447 return;
5448
Joerg Roedele0231712010-02-24 18:59:10 +01005449 /*
5450 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
5451 * 1, because that's a separate STGI/VMRUN intercept. The next time we
5452 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005453 * we'll get the vintr intercept. However, if the vGIF feature is
5454 * enabled, the STGI interception will not occur. Enable the irq
5455 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01005456 */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005457 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02005458 svm_set_vintr(svm);
5459 svm_inject_irq(svm, 0x0);
5460 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005461}
5462
Jan Kiszkac9a79532014-03-07 20:03:15 +01005463static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005464{
Avi Kivity04d2cc72007-09-10 18:10:54 +03005465 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005466
Gleb Natapov44c11432009-05-11 13:35:52 +03005467 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
5468 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01005469 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03005470
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005471 if (!gif_set(svm)) {
5472 if (vgif_enabled(svm))
5473 set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005474 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005475 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005476
5477 if (svm->nested.exit_required)
5478 return; /* we're not going to run the guest yet */
5479
Joerg Roedele0231712010-02-24 18:59:10 +01005480 /*
5481 * Something prevents NMI from been injected. Single step over possible
5482 * problem (IRET or exception injection or interrupt shadow)
5483 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02005484 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02005485 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03005486 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03005487}
5488
Izik Eiduscbc94022007-10-25 00:29:55 +02005489static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
5490{
5491 return 0;
5492}
5493
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07005494static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
5495{
5496 return 0;
5497}
5498
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005499static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Avi Kivityd9e368d2007-06-07 19:18:30 +03005500{
Joerg Roedel38e5e922010-12-03 15:25:16 +01005501 struct vcpu_svm *svm = to_svm(vcpu);
5502
5503 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
5504 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5505 else
5506 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03005507}
5508
Junaid Shahidfaff8752018-06-29 13:10:05 -07005509static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
5510{
5511 struct vcpu_svm *svm = to_svm(vcpu);
5512
5513 invlpga(gva, svm->vmcb->control.asid);
5514}
5515
Avi Kivity04d2cc72007-09-10 18:10:54 +03005516static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
5517{
5518}
5519
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005520static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
5521{
5522 struct vcpu_svm *svm = to_svm(vcpu);
5523
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005524 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005525 return;
5526
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005527 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005528 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03005529 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005530 }
5531}
5532
Joerg Roedel649d6862008-04-16 16:51:15 +02005533static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
5534{
5535 struct vcpu_svm *svm = to_svm(vcpu);
5536 u64 cr8;
5537
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005538 if (svm_nested_virtualize_tpr(vcpu) ||
5539 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005540 return;
5541
Joerg Roedel649d6862008-04-16 16:51:15 +02005542 cr8 = kvm_get_cr8(vcpu);
5543 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
5544 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
5545}
5546
Gleb Natapov9222be12009-04-23 17:14:37 +03005547static void svm_complete_interrupts(struct vcpu_svm *svm)
5548{
5549 u8 vector;
5550 int type;
5551 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01005552 unsigned int3_injected = svm->int3_injected;
5553
5554 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005555
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02005556 /*
5557 * If we've made progress since setting HF_IRET_MASK, we've
5558 * executed an IRET and can allow NMI injection.
5559 */
5560 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
5561 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03005562 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03005563 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5564 }
Gleb Natapov44c11432009-05-11 13:35:52 +03005565
Gleb Natapov9222be12009-04-23 17:14:37 +03005566 svm->vcpu.arch.nmi_injected = false;
5567 kvm_clear_exception_queue(&svm->vcpu);
5568 kvm_clear_interrupt_queue(&svm->vcpu);
5569
5570 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
5571 return;
5572
Avi Kivity3842d132010-07-27 12:30:24 +03005573 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5574
Gleb Natapov9222be12009-04-23 17:14:37 +03005575 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
5576 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
5577
5578 switch (type) {
5579 case SVM_EXITINTINFO_TYPE_NMI:
5580 svm->vcpu.arch.nmi_injected = true;
5581 break;
5582 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01005583 /*
5584 * In case of software exceptions, do not reinject the vector,
5585 * but re-execute the instruction instead. Rewind RIP first
5586 * if we emulated INT3 before.
5587 */
5588 if (kvm_exception_is_soft(vector)) {
5589 if (vector == BP_VECTOR && int3_injected &&
5590 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
5591 kvm_rip_write(&svm->vcpu,
5592 kvm_rip_read(&svm->vcpu) -
5593 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02005594 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01005595 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005596 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
5597 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005598 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03005599
5600 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005601 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03005602 break;
5603 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005604 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03005605 break;
5606 default:
5607 break;
5608 }
5609}
5610
Avi Kivityb463a6f2010-07-20 15:06:17 +03005611static void svm_cancel_injection(struct kvm_vcpu *vcpu)
5612{
5613 struct vcpu_svm *svm = to_svm(vcpu);
5614 struct vmcb_control_area *control = &svm->vmcb->control;
5615
5616 control->exit_int_info = control->event_inj;
5617 control->exit_int_info_err = control->event_inj_err;
5618 control->event_inj = 0;
5619 svm_complete_interrupts(svm);
5620}
5621
Avi Kivity851ba692009-08-24 11:10:17 +03005622static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005623{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005624 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03005625
Joerg Roedel2041a062010-04-22 12:33:08 +02005626 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5627 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5628 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5629
Joerg Roedelcd3ff652009-10-09 16:08:26 +02005630 /*
5631 * A vmexit emulation is required before the vcpu can be executed
5632 * again.
5633 */
5634 if (unlikely(svm->nested.exit_required))
5635 return;
5636
Ladi Proseka12713c2017-06-21 09:07:00 +02005637 /*
5638 * Disable singlestep if we're injecting an interrupt/exception.
5639 * We don't want our modified rflags to be pushed on the stack where
5640 * we might not be able to easily reset them if we disabled NMI
5641 * singlestep later.
5642 */
5643 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5644 /*
5645 * Event injection happens before external interrupts cause a
5646 * vmexit and interrupts are disabled here, so smp_send_reschedule
5647 * is enough to force an immediate vmexit.
5648 */
5649 disable_nmi_singlestep(svm);
5650 smp_send_reschedule(vcpu->cpu);
5651 }
5652
Rusty Russelle756fc62007-07-30 20:07:08 +10005653 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005654
Joerg Roedel649d6862008-04-16 16:51:15 +02005655 sync_lapic_to_cr8(vcpu);
5656
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02005657 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005658
Avi Kivity04d2cc72007-09-10 18:10:54 +03005659 clgi();
Aaron Lewis139a12c2019-10-21 16:30:25 -07005660 kvm_load_guest_xsave_state(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03005661
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08005662 if (lapic_in_kernel(vcpu) &&
5663 vcpu->arch.apic->lapic_timer.timer_advance_ns)
5664 kvm_wait_lapic_expire(vcpu);
5665
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005666 /*
5667 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
5668 * it's non-zero. Since vmentry is serialising on affected CPUs, there
5669 * is no need to worry about the conditional branch over the wrmsr
5670 * being speculatively taken.
5671 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02005672 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005673
Thomas Gleixner024d83c2018-08-12 20:41:45 +02005674 local_irq_enable();
5675
Avi Kivity6aa8b732006-12-10 02:21:36 -08005676 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03005677 "push %%" _ASM_BP "; \n\t"
5678 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5679 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5680 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5681 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5682 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5683 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005684#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005685 "mov %c[r8](%[svm]), %%r8 \n\t"
5686 "mov %c[r9](%[svm]), %%r9 \n\t"
5687 "mov %c[r10](%[svm]), %%r10 \n\t"
5688 "mov %c[r11](%[svm]), %%r11 \n\t"
5689 "mov %c[r12](%[svm]), %%r12 \n\t"
5690 "mov %c[r13](%[svm]), %%r13 \n\t"
5691 "mov %c[r14](%[svm]), %%r14 \n\t"
5692 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005693#endif
5694
Avi Kivity6aa8b732006-12-10 02:21:36 -08005695 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03005696 "push %%" _ASM_AX " \n\t"
5697 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Uros Bizjakac5ffda22018-11-26 17:00:08 +01005698 __ex("vmload %%" _ASM_AX) "\n\t"
5699 __ex("vmrun %%" _ASM_AX) "\n\t"
5700 __ex("vmsave %%" _ASM_AX) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005701 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005702
5703 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03005704 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5705 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5706 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5707 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5708 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5709 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005710#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005711 "mov %%r8, %c[r8](%[svm]) \n\t"
5712 "mov %%r9, %c[r9](%[svm]) \n\t"
5713 "mov %%r10, %c[r10](%[svm]) \n\t"
5714 "mov %%r11, %c[r11](%[svm]) \n\t"
5715 "mov %%r12, %c[r12](%[svm]) \n\t"
5716 "mov %%r13, %c[r13](%[svm]) \n\t"
5717 "mov %%r14, %c[r14](%[svm]) \n\t"
5718 "mov %%r15, %c[r15](%[svm]) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08005719 /*
5720 * Clear host registers marked as clobbered to prevent
5721 * speculative use.
5722 */
Uros Bizjak43ce76c2018-10-17 16:46:57 +02005723 "xor %%r8d, %%r8d \n\t"
5724 "xor %%r9d, %%r9d \n\t"
5725 "xor %%r10d, %%r10d \n\t"
5726 "xor %%r11d, %%r11d \n\t"
5727 "xor %%r12d, %%r12d \n\t"
5728 "xor %%r13d, %%r13d \n\t"
5729 "xor %%r14d, %%r14d \n\t"
5730 "xor %%r15d, %%r15d \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08005731#endif
Uros Bizjak43ce76c2018-10-17 16:46:57 +02005732 "xor %%ebx, %%ebx \n\t"
5733 "xor %%ecx, %%ecx \n\t"
5734 "xor %%edx, %%edx \n\t"
5735 "xor %%esi, %%esi \n\t"
5736 "xor %%edi, %%edi \n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005737 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08005738 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005739 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08005740 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005741 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5742 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5743 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5744 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5745 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5746 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005747#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005748 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5749 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5750 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5751 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5752 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5753 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5754 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5755 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005756#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02005757 : "cc", "memory"
5758#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03005759 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005760 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03005761#else
5762 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005763#endif
5764 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08005765
Thomas Gleixner15e6c222018-05-11 15:21:01 +02005766 /* Eliminate branch target predictions from guest mode */
5767 vmexit_fill_RSB();
5768
5769#ifdef CONFIG_X86_64
5770 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5771#else
5772 loadsegment(fs, svm->host.fs);
5773#ifndef CONFIG_X86_32_LAZY_GS
5774 loadsegment(gs, svm->host.gs);
5775#endif
5776#endif
5777
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005778 /*
5779 * We do not use IBRS in the kernel. If this vCPU has used the
5780 * SPEC_CTRL MSR it may have left it on; save the value and
5781 * turn it off. This is much more efficient than blindly adding
5782 * it to the atomic save/restore list. Especially as the former
5783 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
5784 *
5785 * For non-nested case:
5786 * If the L01 MSR bitmap does not intercept the MSR, then we need to
5787 * save it.
5788 *
5789 * For nested case:
5790 * If the L02 MSR bitmap does not intercept the MSR, then we need to
5791 * save it.
5792 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01005793 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005794 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005795
Avi Kivity6aa8b732006-12-10 02:21:36 -08005796 reload_tss(vcpu);
5797
Avi Kivity56ba47d2007-11-07 17:14:18 +02005798 local_irq_disable();
5799
Thomas Gleixner024d83c2018-08-12 20:41:45 +02005800 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
5801
Avi Kivity13c34e02010-10-21 12:20:31 +02005802 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5803 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5804 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5805 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5806
Joerg Roedel3781c012011-01-14 16:45:02 +01005807 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Andi Kleendd60d212017-07-25 17:20:32 -07005808 kvm_before_interrupt(&svm->vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01005809
Aaron Lewis139a12c2019-10-21 16:30:25 -07005810 kvm_load_host_xsave_state(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01005811 stgi();
5812
5813 /* Any pending NMI will happen here */
5814
5815 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Andi Kleendd60d212017-07-25 17:20:32 -07005816 kvm_after_interrupt(&svm->vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01005817
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005818 sync_cr8_to_lapic(vcpu);
5819
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005820 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005821
Joerg Roedel38e5e922010-12-03 15:25:16 +01005822 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5823
Gleb Natapov631bc482010-10-14 11:22:52 +02005824 /* if exit due to PF check for async PF */
5825 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005826 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
Gleb Natapov631bc482010-10-14 11:22:52 +02005827
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005828 if (npt_enabled) {
5829 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5830 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5831 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02005832
5833 /*
5834 * We need to handle MC intercepts here before the vcpu has a chance to
5835 * change the physical cpu
5836 */
5837 if (unlikely(svm->vmcb->control.exit_code ==
5838 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5839 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01005840
5841 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005842}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005843STACK_FRAME_NON_STANDARD(svm_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005844
Avi Kivity6aa8b732006-12-10 02:21:36 -08005845static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5846{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005847 struct vcpu_svm *svm = to_svm(vcpu);
5848
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005849 svm->vmcb->save.cr3 = __sme_set(root);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005850 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005851}
5852
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005853static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5854{
5855 struct vcpu_svm *svm = to_svm(vcpu);
5856
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005857 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01005858 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005859
5860 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02005861 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005862 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005863}
5864
Avi Kivity6aa8b732006-12-10 02:21:36 -08005865static int is_disabled(void)
5866{
Joerg Roedel6031a612007-06-22 12:29:50 +03005867 u64 vm_cr;
5868
5869 rdmsrl(MSR_VM_CR, vm_cr);
5870 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5871 return 1;
5872
Avi Kivity6aa8b732006-12-10 02:21:36 -08005873 return 0;
5874}
5875
Ingo Molnar102d8322007-02-19 14:37:47 +02005876static void
5877svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5878{
5879 /*
5880 * Patch in the VMMCALL instruction:
5881 */
5882 hypercall[0] = 0x0f;
5883 hypercall[1] = 0x01;
5884 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02005885}
5886
Sean Christophersonf257d6d2019-04-19 22:18:17 -07005887static int __init svm_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03005888{
Sean Christophersonf257d6d2019-04-19 22:18:17 -07005889 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03005890}
5891
Avi Kivity774ead32007-12-26 13:57:04 +02005892static bool svm_cpu_has_accelerated_tpr(void)
5893{
5894 return false;
5895}
5896
Tom Lendackybc226f02018-05-10 22:06:39 +02005897static bool svm_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005898{
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01005899 switch (index) {
5900 case MSR_IA32_MCG_EXT_CTL:
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02005901 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01005902 return false;
5903 default:
5904 break;
5905 }
5906
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005907 return true;
5908}
5909
Paolo Bonzinifc07e762015-10-01 13:20:22 +02005910static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5911{
5912 return 0;
5913}
5914
Sheng Yang0e851882009-12-18 16:48:46 +08005915static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5916{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005917 struct vcpu_svm *svm = to_svm(vcpu);
5918
Aaron Lewis72041602019-10-21 16:30:20 -07005919 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5920 boot_cpu_has(X86_FEATURE_XSAVES);
5921
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005922 /* Update nrips enabled cache */
Radim Krčmářd6321d42017-08-05 00:12:49 +02005923 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005924
5925 if (!kvm_vcpu_apicv_active(vcpu))
5926 return;
5927
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02005928 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08005929}
5930
Paolo Bonzini50896de2019-08-15 09:17:31 +02005931#define F(x) bit(X86_FEATURE_##x)
5932
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005933static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5934{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005935 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005936 case 0x1:
5937 if (avic)
5938 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5939 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02005940 case 0x80000001:
5941 if (nested)
5942 entry->ecx |= (1 << 2); /* Set SVM bit */
5943 break;
Paolo Bonzini50896de2019-08-15 09:17:31 +02005944 case 0x80000008:
5945 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
5946 boot_cpu_has(X86_FEATURE_AMD_SSBD))
5947 entry->ebx |= F(VIRT_SSBD);
5948 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005949 case 0x8000000A:
5950 entry->eax = 1; /* SVM revision 1 */
5951 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5952 ASID emulation to nested SVM */
5953 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02005954 entry->edx = 0; /* Per default do not support any
5955 additional features */
5956
5957 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02005958 if (boot_cpu_has(X86_FEATURE_NRIPS))
Paolo Bonzini50896de2019-08-15 09:17:31 +02005959 entry->edx |= F(NRIPS);
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005960
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005961 /* Support NPT for the guest if enabled */
5962 if (npt_enabled)
Paolo Bonzini50896de2019-08-15 09:17:31 +02005963 entry->edx |= F(NPT);
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005964
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005965 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005966}
5967
Sheng Yang17cc3932010-01-05 19:02:27 +08005968static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005969{
Sheng Yang17cc3932010-01-05 19:02:27 +08005970 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005971}
5972
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005973static bool svm_rdtscp_supported(void)
5974{
Paolo Bonzini46896c72015-11-12 14:49:16 +01005975 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005976}
5977
Mao, Junjiead756a12012-07-02 01:18:48 +00005978static bool svm_invpcid_supported(void)
5979{
5980 return false;
5981}
5982
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005983static bool svm_mpx_supported(void)
5984{
5985 return false;
5986}
5987
Wanpeng Li55412b22014-12-02 19:21:30 +08005988static bool svm_xsaves_supported(void)
5989{
Aaron Lewis52297432019-10-21 16:30:27 -07005990 return boot_cpu_has(X86_FEATURE_XSAVES);
Wanpeng Li55412b22014-12-02 19:21:30 +08005991}
5992
Paolo Bonzini66336ca2016-07-12 10:36:41 +02005993static bool svm_umip_emulated(void)
5994{
5995 return false;
5996}
5997
Chao Peng86f52012018-10-24 16:05:11 +08005998static bool svm_pt_supported(void)
5999{
6000 return false;
6001}
6002
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006003static bool svm_has_wbinvd_exit(void)
6004{
6005 return true;
6006}
6007
Joerg Roedel80612522011-04-04 12:39:33 +02006008#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03006009 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006010#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03006011 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02006012#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03006013 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006014
Mathias Krause09941fb2012-08-30 01:30:20 +02006015static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006016 u32 exit_code;
6017 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006018} x86_intercept_map[] = {
6019 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
6020 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
6021 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
6022 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
6023 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02006024 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
6025 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02006026 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
6027 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
6028 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
6029 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
6030 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
6031 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
6032 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
6033 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02006034 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
6035 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
6036 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
6037 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
6038 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
6039 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
6040 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
6041 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02006042 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
6043 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
6044 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02006045 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
6046 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
6047 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
6048 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
6049 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
6050 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
6051 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
6052 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
6053 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02006054 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
6055 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
6056 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
6057 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
6058 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
6059 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
6060 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02006061 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
6062 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
6063 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
6064 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006065 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006066};
6067
Joerg Roedel80612522011-04-04 12:39:33 +02006068#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006069#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02006070#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006071
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006072static int svm_check_intercept(struct kvm_vcpu *vcpu,
6073 struct x86_instruction_info *info,
6074 enum x86_intercept_stage stage)
6075{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006076 struct vcpu_svm *svm = to_svm(vcpu);
6077 int vmexit, ret = X86EMUL_CONTINUE;
6078 struct __x86_intercept icpt_info;
6079 struct vmcb *vmcb = svm->vmcb;
6080
6081 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
6082 goto out;
6083
6084 icpt_info = x86_intercept_map[info->intercept];
6085
Avi Kivity40e19b52011-04-21 12:35:41 +03006086 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006087 goto out;
6088
6089 switch (icpt_info.exit_code) {
6090 case SVM_EXIT_READ_CR0:
6091 if (info->intercept == x86_intercept_cr_read)
6092 icpt_info.exit_code += info->modrm_reg;
6093 break;
6094 case SVM_EXIT_WRITE_CR0: {
6095 unsigned long cr0, val;
6096 u64 intercept;
6097
6098 if (info->intercept == x86_intercept_cr_write)
6099 icpt_info.exit_code += info->modrm_reg;
6100
Jan Kiszka62baf442014-06-29 21:55:53 +02006101 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
6102 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006103 break;
6104
6105 intercept = svm->nested.intercept;
6106
6107 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
6108 break;
6109
6110 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
6111 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
6112
6113 if (info->intercept == x86_intercept_lmsw) {
6114 cr0 &= 0xfUL;
6115 val &= 0xfUL;
6116 /* lmsw can't clear PE - catch this here */
6117 if (cr0 & X86_CR0_PE)
6118 val |= X86_CR0_PE;
6119 }
6120
6121 if (cr0 ^ val)
6122 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
6123
6124 break;
6125 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02006126 case SVM_EXIT_READ_DR0:
6127 case SVM_EXIT_WRITE_DR0:
6128 icpt_info.exit_code += info->modrm_reg;
6129 break;
Joerg Roedel80612522011-04-04 12:39:33 +02006130 case SVM_EXIT_MSR:
6131 if (info->intercept == x86_intercept_wrmsr)
6132 vmcb->control.exit_info_1 = 1;
6133 else
6134 vmcb->control.exit_info_1 = 0;
6135 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02006136 case SVM_EXIT_PAUSE:
6137 /*
6138 * We get this for NOP only, but pause
6139 * is rep not, check this here
6140 */
6141 if (info->rep_prefix != REPE_PREFIX)
6142 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02006143 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02006144 case SVM_EXIT_IOIO: {
6145 u64 exit_info;
6146 u32 bytes;
6147
Joerg Roedelf6511932011-04-04 12:39:35 +02006148 if (info->intercept == x86_intercept_in ||
6149 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02006150 exit_info = ((info->src_val & 0xffff) << 16) |
6151 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02006152 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02006153 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02006154 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02006155 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02006156 }
6157
6158 if (info->intercept == x86_intercept_outs ||
6159 info->intercept == x86_intercept_ins)
6160 exit_info |= SVM_IOIO_STR_MASK;
6161
6162 if (info->rep_prefix)
6163 exit_info |= SVM_IOIO_REP_MASK;
6164
6165 bytes = min(bytes, 4u);
6166
6167 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
6168
6169 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
6170
6171 vmcb->control.exit_info_1 = exit_info;
6172 vmcb->control.exit_info_2 = info->next_rip;
6173
6174 break;
6175 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006176 default:
6177 break;
6178 }
6179
Bandan Dasf1047652015-06-11 02:05:33 -04006180 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
6181 if (static_cpu_has(X86_FEATURE_NRIPS))
6182 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006183 vmcb->control.exit_code = icpt_info.exit_code;
6184 vmexit = nested_svm_exit_handled(svm);
6185
6186 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
6187 : X86EMUL_CONTINUE;
6188
6189out:
6190 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006191}
6192
Wanpeng Li1e9e2622019-11-21 11:17:11 +08006193static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu,
6194 enum exit_fastpath_completion *exit_fastpath)
Yang Zhanga547c6d2013-04-11 19:25:10 +08006195{
Wanpeng Li1e9e2622019-11-21 11:17:11 +08006196 if (!is_guest_mode(vcpu) &&
6197 to_svm(vcpu)->vmcb->control.exit_code == EXIT_REASON_MSR_WRITE)
6198 *exit_fastpath = handle_fastpath_set_msr_irqoff(vcpu);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006199}
6200
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006201static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
6202{
Babu Moger8566ac82018-03-16 16:37:26 -04006203 if (pause_filter_thresh)
6204 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006205}
6206
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05006207static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
6208{
6209 if (avic_handle_apic_id_update(vcpu) != 0)
6210 return;
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00006211 avic_handle_dfr_update(vcpu);
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05006212 avic_handle_ldr_update(vcpu);
6213}
6214
Borislav Petkov74f16902017-03-26 23:51:24 +02006215static void svm_setup_mce(struct kvm_vcpu *vcpu)
6216{
6217 /* [63:9] are reserved. */
6218 vcpu->arch.mcg_cap &= 0x1ff;
6219}
6220
Ladi Prosek72d7b372017-10-11 16:54:41 +02006221static int svm_smi_allowed(struct kvm_vcpu *vcpu)
6222{
Ladi Prosek05cade72017-10-11 16:54:45 +02006223 struct vcpu_svm *svm = to_svm(vcpu);
6224
6225 /* Per APM Vol.2 15.22.2 "Response to SMI" */
6226 if (!gif_set(svm))
6227 return 0;
6228
6229 if (is_guest_mode(&svm->vcpu) &&
6230 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
6231 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
6232 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
6233 svm->nested.exit_required = true;
6234 return 0;
6235 }
6236
Ladi Prosek72d7b372017-10-11 16:54:41 +02006237 return 1;
6238}
6239
Ladi Prosek0234bf82017-10-11 16:54:40 +02006240static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
6241{
Ladi Prosek05cade72017-10-11 16:54:45 +02006242 struct vcpu_svm *svm = to_svm(vcpu);
6243 int ret;
6244
6245 if (is_guest_mode(vcpu)) {
6246 /* FED8h - SVM Guest */
6247 put_smstate(u64, smstate, 0x7ed8, 1);
6248 /* FEE0h - SVM Guest VMCB Physical Address */
6249 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
6250
6251 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
6252 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
6253 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
6254
6255 ret = nested_svm_vmexit(svm);
6256 if (ret)
6257 return ret;
6258 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02006259 return 0;
6260}
6261
Sean Christophersoned193212019-04-02 08:03:09 -07006262static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006263{
Ladi Prosek05cade72017-10-11 16:54:45 +02006264 struct vcpu_svm *svm = to_svm(vcpu);
6265 struct vmcb *nested_vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01006266 struct kvm_host_map map;
Sean Christophersoned193212019-04-02 08:03:09 -07006267 u64 guest;
6268 u64 vmcb;
Ladi Prosek05cade72017-10-11 16:54:45 +02006269
Sean Christophersoned193212019-04-02 08:03:09 -07006270 guest = GET_SMSTATE(u64, smstate, 0x7ed8);
6271 vmcb = GET_SMSTATE(u64, smstate, 0x7ee0);
Ladi Prosek05cade72017-10-11 16:54:45 +02006272
Sean Christophersoned193212019-04-02 08:03:09 -07006273 if (guest) {
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01006274 if (kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb), &map) == -EINVAL)
Sean Christopherson9ec19492019-04-02 08:03:11 -07006275 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01006276 nested_vmcb = map.hva;
6277 enter_svm_guest_mode(svm, vmcb, nested_vmcb, &map);
Ladi Prosek05cade72017-10-11 16:54:45 +02006278 }
Sean Christopherson9ec19492019-04-02 08:03:11 -07006279 return 0;
Ladi Prosek0234bf82017-10-11 16:54:40 +02006280}
6281
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006282static int enable_smi_window(struct kvm_vcpu *vcpu)
6283{
6284 struct vcpu_svm *svm = to_svm(vcpu);
6285
6286 if (!gif_set(svm)) {
6287 if (vgif_enabled(svm))
6288 set_intercept(svm, INTERCEPT_STGI);
6289 /* STGI will cause a vm exit */
6290 return 1;
6291 }
6292 return 0;
6293}
6294
Tom Lendacky33af3a72019-10-03 21:17:48 +00006295static int sev_flush_asids(void)
6296{
6297 int ret, error;
6298
6299 /*
6300 * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail,
6301 * so it must be guarded.
6302 */
6303 down_write(&sev_deactivate_lock);
6304
6305 wbinvd_on_all_cpus();
6306 ret = sev_guest_df_flush(&error);
6307
6308 up_write(&sev_deactivate_lock);
6309
6310 if (ret)
6311 pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error);
6312
6313 return ret;
6314}
6315
6316/* Must be called with the sev_bitmap_lock held */
6317static bool __sev_recycle_asids(void)
6318{
6319 int pos;
6320
6321 /* Check if there are any ASIDs to reclaim before performing a flush */
6322 pos = find_next_bit(sev_reclaim_asid_bitmap,
6323 max_sev_asid, min_sev_asid - 1);
6324 if (pos >= max_sev_asid)
6325 return false;
6326
6327 if (sev_flush_asids())
6328 return false;
6329
6330 bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap,
6331 max_sev_asid);
6332 bitmap_zero(sev_reclaim_asid_bitmap, max_sev_asid);
6333
6334 return true;
6335}
6336
Brijesh Singh1654efc2017-12-04 10:57:34 -06006337static int sev_asid_new(void)
6338{
Tom Lendacky33af3a72019-10-03 21:17:48 +00006339 bool retry = true;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006340 int pos;
6341
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00006342 mutex_lock(&sev_bitmap_lock);
6343
Brijesh Singh1654efc2017-12-04 10:57:34 -06006344 /*
6345 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
6346 */
Tom Lendacky33af3a72019-10-03 21:17:48 +00006347again:
Brijesh Singh1654efc2017-12-04 10:57:34 -06006348 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00006349 if (pos >= max_sev_asid) {
Tom Lendacky33af3a72019-10-03 21:17:48 +00006350 if (retry && __sev_recycle_asids()) {
6351 retry = false;
6352 goto again;
6353 }
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00006354 mutex_unlock(&sev_bitmap_lock);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006355 return -EBUSY;
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00006356 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06006357
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00006358 __set_bit(pos, sev_asid_bitmap);
6359
6360 mutex_unlock(&sev_bitmap_lock);
6361
Brijesh Singh1654efc2017-12-04 10:57:34 -06006362 return pos + 1;
6363}
6364
6365static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
6366{
Sean Christopherson81811c12018-03-20 12:17:21 -07006367 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006368 int asid, ret;
6369
6370 ret = -EBUSY;
David Rientjes3f14a892019-01-02 12:56:33 -08006371 if (unlikely(sev->active))
6372 return ret;
6373
Brijesh Singh1654efc2017-12-04 10:57:34 -06006374 asid = sev_asid_new();
6375 if (asid < 0)
6376 return ret;
6377
6378 ret = sev_platform_init(&argp->error);
6379 if (ret)
6380 goto e_free;
6381
6382 sev->active = true;
6383 sev->asid = asid;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006384 INIT_LIST_HEAD(&sev->regions_list);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006385
6386 return 0;
6387
6388e_free:
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00006389 sev_asid_free(asid);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006390 return ret;
6391}
6392
Brijesh Singh59414c92017-12-04 10:57:35 -06006393static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
6394{
6395 struct sev_data_activate *data;
6396 int asid = sev_get_asid(kvm);
6397 int ret;
6398
Ben Gardon1ec69642019-02-11 11:02:51 -08006399 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh59414c92017-12-04 10:57:35 -06006400 if (!data)
6401 return -ENOMEM;
6402
6403 /* activate ASID on the given handle */
6404 data->handle = handle;
6405 data->asid = asid;
6406 ret = sev_guest_activate(data, error);
6407 kfree(data);
6408
6409 return ret;
6410}
6411
Brijesh Singh89c50582017-12-04 10:57:35 -06006412static int __sev_issue_cmd(int fd, int id, void *data, int *error)
Brijesh Singh59414c92017-12-04 10:57:35 -06006413{
6414 struct fd f;
6415 int ret;
6416
6417 f = fdget(fd);
6418 if (!f.file)
6419 return -EBADF;
6420
6421 ret = sev_issue_cmd_external_user(f.file, id, data, error);
6422
6423 fdput(f);
6424 return ret;
6425}
6426
Brijesh Singh89c50582017-12-04 10:57:35 -06006427static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
6428{
Sean Christopherson81811c12018-03-20 12:17:21 -07006429 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06006430
6431 return __sev_issue_cmd(sev->fd, id, data, error);
6432}
6433
Brijesh Singh59414c92017-12-04 10:57:35 -06006434static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
6435{
Sean Christopherson81811c12018-03-20 12:17:21 -07006436 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh59414c92017-12-04 10:57:35 -06006437 struct sev_data_launch_start *start;
6438 struct kvm_sev_launch_start params;
6439 void *dh_blob, *session_blob;
6440 int *error = &argp->error;
6441 int ret;
6442
6443 if (!sev_guest(kvm))
6444 return -ENOTTY;
6445
6446 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6447 return -EFAULT;
6448
Ben Gardon1ec69642019-02-11 11:02:51 -08006449 start = kzalloc(sizeof(*start), GFP_KERNEL_ACCOUNT);
Brijesh Singh59414c92017-12-04 10:57:35 -06006450 if (!start)
6451 return -ENOMEM;
6452
6453 dh_blob = NULL;
6454 if (params.dh_uaddr) {
6455 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
6456 if (IS_ERR(dh_blob)) {
6457 ret = PTR_ERR(dh_blob);
6458 goto e_free;
6459 }
6460
6461 start->dh_cert_address = __sme_set(__pa(dh_blob));
6462 start->dh_cert_len = params.dh_len;
6463 }
6464
6465 session_blob = NULL;
6466 if (params.session_uaddr) {
6467 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
6468 if (IS_ERR(session_blob)) {
6469 ret = PTR_ERR(session_blob);
6470 goto e_free_dh;
6471 }
6472
6473 start->session_address = __sme_set(__pa(session_blob));
6474 start->session_len = params.session_len;
6475 }
6476
6477 start->handle = params.handle;
6478 start->policy = params.policy;
6479
6480 /* create memory encryption context */
Brijesh Singh89c50582017-12-04 10:57:35 -06006481 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
Brijesh Singh59414c92017-12-04 10:57:35 -06006482 if (ret)
6483 goto e_free_session;
6484
6485 /* Bind ASID to this guest */
6486 ret = sev_bind_asid(kvm, start->handle, error);
6487 if (ret)
6488 goto e_free_session;
6489
6490 /* return handle to userspace */
6491 params.handle = start->handle;
6492 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
6493 sev_unbind_asid(kvm, start->handle);
6494 ret = -EFAULT;
6495 goto e_free_session;
6496 }
6497
6498 sev->handle = start->handle;
6499 sev->fd = argp->sev_fd;
6500
6501e_free_session:
6502 kfree(session_blob);
6503e_free_dh:
6504 kfree(dh_blob);
6505e_free:
6506 kfree(start);
6507 return ret;
6508}
6509
David Rientjesede885e2019-03-19 15:19:56 -07006510static unsigned long get_num_contig_pages(unsigned long idx,
6511 struct page **inpages, unsigned long npages)
Brijesh Singh89c50582017-12-04 10:57:35 -06006512{
6513 unsigned long paddr, next_paddr;
David Rientjesede885e2019-03-19 15:19:56 -07006514 unsigned long i = idx + 1, pages = 1;
Brijesh Singh89c50582017-12-04 10:57:35 -06006515
6516 /* find the number of contiguous pages starting from idx */
6517 paddr = __sme_page_pa(inpages[idx]);
6518 while (i < npages) {
6519 next_paddr = __sme_page_pa(inpages[i++]);
6520 if ((paddr + PAGE_SIZE) == next_paddr) {
6521 pages++;
6522 paddr = next_paddr;
6523 continue;
6524 }
6525 break;
6526 }
6527
6528 return pages;
6529}
6530
6531static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
6532{
David Rientjesede885e2019-03-19 15:19:56 -07006533 unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i;
Sean Christopherson81811c12018-03-20 12:17:21 -07006534 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06006535 struct kvm_sev_launch_update_data params;
6536 struct sev_data_launch_update_data *data;
6537 struct page **inpages;
David Rientjesede885e2019-03-19 15:19:56 -07006538 int ret;
Brijesh Singh89c50582017-12-04 10:57:35 -06006539
6540 if (!sev_guest(kvm))
6541 return -ENOTTY;
6542
6543 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6544 return -EFAULT;
6545
Ben Gardon1ec69642019-02-11 11:02:51 -08006546 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh89c50582017-12-04 10:57:35 -06006547 if (!data)
6548 return -ENOMEM;
6549
6550 vaddr = params.uaddr;
6551 size = params.len;
6552 vaddr_end = vaddr + size;
6553
6554 /* Lock the user memory. */
6555 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
6556 if (!inpages) {
6557 ret = -ENOMEM;
6558 goto e_free;
6559 }
6560
6561 /*
6562 * The LAUNCH_UPDATE command will perform in-place encryption of the
6563 * memory content (i.e it will write the same memory region with C=1).
6564 * It's possible that the cache may contain the data with C=0, i.e.,
6565 * unencrypted so invalidate it first.
6566 */
6567 sev_clflush_pages(inpages, npages);
6568
6569 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
6570 int offset, len;
6571
6572 /*
6573 * If the user buffer is not page-aligned, calculate the offset
6574 * within the page.
6575 */
6576 offset = vaddr & (PAGE_SIZE - 1);
6577
6578 /* Calculate the number of pages that can be encrypted in one go. */
6579 pages = get_num_contig_pages(i, inpages, npages);
6580
6581 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
6582
6583 data->handle = sev->handle;
6584 data->len = len;
6585 data->address = __sme_page_pa(inpages[i]) + offset;
6586 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
6587 if (ret)
6588 goto e_unpin;
6589
6590 size -= len;
6591 next_vaddr = vaddr + len;
6592 }
6593
6594e_unpin:
6595 /* content of memory is updated, mark pages dirty */
6596 for (i = 0; i < npages; i++) {
6597 set_page_dirty_lock(inpages[i]);
6598 mark_page_accessed(inpages[i]);
6599 }
6600 /* unlock the user pages */
6601 sev_unpin_memory(kvm, inpages, npages);
6602e_free:
6603 kfree(data);
6604 return ret;
6605}
6606
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006607static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
6608{
Brijesh Singh3e233382018-02-23 12:36:50 -06006609 void __user *measure = (void __user *)(uintptr_t)argp->data;
Sean Christopherson81811c12018-03-20 12:17:21 -07006610 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006611 struct sev_data_launch_measure *data;
6612 struct kvm_sev_launch_measure params;
Brijesh Singh3e233382018-02-23 12:36:50 -06006613 void __user *p = NULL;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006614 void *blob = NULL;
6615 int ret;
6616
6617 if (!sev_guest(kvm))
6618 return -ENOTTY;
6619
Brijesh Singh3e233382018-02-23 12:36:50 -06006620 if (copy_from_user(&params, measure, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006621 return -EFAULT;
6622
Ben Gardon1ec69642019-02-11 11:02:51 -08006623 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006624 if (!data)
6625 return -ENOMEM;
6626
6627 /* User wants to query the blob length */
6628 if (!params.len)
6629 goto cmd;
6630
Brijesh Singh3e233382018-02-23 12:36:50 -06006631 p = (void __user *)(uintptr_t)params.uaddr;
6632 if (p) {
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006633 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
6634 ret = -EINVAL;
6635 goto e_free;
6636 }
6637
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006638 ret = -ENOMEM;
6639 blob = kmalloc(params.len, GFP_KERNEL);
6640 if (!blob)
6641 goto e_free;
6642
6643 data->address = __psp_pa(blob);
6644 data->len = params.len;
6645 }
6646
6647cmd:
6648 data->handle = sev->handle;
6649 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
6650
6651 /*
6652 * If we query the session length, FW responded with expected data.
6653 */
6654 if (!params.len)
6655 goto done;
6656
6657 if (ret)
6658 goto e_free_blob;
6659
6660 if (blob) {
Brijesh Singh3e233382018-02-23 12:36:50 -06006661 if (copy_to_user(p, blob, params.len))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006662 ret = -EFAULT;
6663 }
6664
6665done:
6666 params.len = data->len;
Brijesh Singh3e233382018-02-23 12:36:50 -06006667 if (copy_to_user(measure, &params, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006668 ret = -EFAULT;
6669e_free_blob:
6670 kfree(blob);
6671e_free:
6672 kfree(data);
6673 return ret;
6674}
6675
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006676static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
6677{
Sean Christopherson81811c12018-03-20 12:17:21 -07006678 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006679 struct sev_data_launch_finish *data;
6680 int ret;
6681
6682 if (!sev_guest(kvm))
6683 return -ENOTTY;
6684
Ben Gardon1ec69642019-02-11 11:02:51 -08006685 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006686 if (!data)
6687 return -ENOMEM;
6688
6689 data->handle = sev->handle;
6690 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
6691
6692 kfree(data);
6693 return ret;
6694}
6695
Brijesh Singh255d9e72017-12-04 10:57:37 -06006696static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
6697{
Sean Christopherson81811c12018-03-20 12:17:21 -07006698 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006699 struct kvm_sev_guest_status params;
6700 struct sev_data_guest_status *data;
6701 int ret;
6702
6703 if (!sev_guest(kvm))
6704 return -ENOTTY;
6705
Ben Gardon1ec69642019-02-11 11:02:51 -08006706 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh255d9e72017-12-04 10:57:37 -06006707 if (!data)
6708 return -ENOMEM;
6709
6710 data->handle = sev->handle;
6711 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
6712 if (ret)
6713 goto e_free;
6714
6715 params.policy = data->policy;
6716 params.state = data->state;
6717 params.handle = data->handle;
6718
6719 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6720 ret = -EFAULT;
6721e_free:
6722 kfree(data);
6723 return ret;
6724}
6725
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006726static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
6727 unsigned long dst, int size,
6728 int *error, bool enc)
6729{
Sean Christopherson81811c12018-03-20 12:17:21 -07006730 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006731 struct sev_data_dbg *data;
6732 int ret;
6733
Ben Gardon1ec69642019-02-11 11:02:51 -08006734 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006735 if (!data)
6736 return -ENOMEM;
6737
6738 data->handle = sev->handle;
6739 data->dst_addr = dst;
6740 data->src_addr = src;
6741 data->len = size;
6742
6743 ret = sev_issue_cmd(kvm,
6744 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
6745 data, error);
6746 kfree(data);
6747 return ret;
6748}
6749
6750static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
6751 unsigned long dst_paddr, int sz, int *err)
6752{
6753 int offset;
6754
6755 /*
6756 * Its safe to read more than we are asked, caller should ensure that
6757 * destination has enough space.
6758 */
6759 src_paddr = round_down(src_paddr, 16);
6760 offset = src_paddr & 15;
6761 sz = round_up(sz + offset, 16);
6762
6763 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
6764}
6765
6766static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
6767 unsigned long __user dst_uaddr,
6768 unsigned long dst_paddr,
6769 int size, int *err)
6770{
6771 struct page *tpage = NULL;
6772 int ret, offset;
6773
6774 /* if inputs are not 16-byte then use intermediate buffer */
6775 if (!IS_ALIGNED(dst_paddr, 16) ||
6776 !IS_ALIGNED(paddr, 16) ||
6777 !IS_ALIGNED(size, 16)) {
6778 tpage = (void *)alloc_page(GFP_KERNEL);
6779 if (!tpage)
6780 return -ENOMEM;
6781
6782 dst_paddr = __sme_page_pa(tpage);
6783 }
6784
6785 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
6786 if (ret)
6787 goto e_free;
6788
6789 if (tpage) {
6790 offset = paddr & 15;
6791 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
6792 page_address(tpage) + offset, size))
6793 ret = -EFAULT;
6794 }
6795
6796e_free:
6797 if (tpage)
6798 __free_page(tpage);
6799
6800 return ret;
6801}
6802
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006803static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
6804 unsigned long __user vaddr,
6805 unsigned long dst_paddr,
6806 unsigned long __user dst_vaddr,
6807 int size, int *error)
6808{
6809 struct page *src_tpage = NULL;
6810 struct page *dst_tpage = NULL;
6811 int ret, len = size;
6812
6813 /* If source buffer is not aligned then use an intermediate buffer */
6814 if (!IS_ALIGNED(vaddr, 16)) {
6815 src_tpage = alloc_page(GFP_KERNEL);
6816 if (!src_tpage)
6817 return -ENOMEM;
6818
6819 if (copy_from_user(page_address(src_tpage),
6820 (void __user *)(uintptr_t)vaddr, size)) {
6821 __free_page(src_tpage);
6822 return -EFAULT;
6823 }
6824
6825 paddr = __sme_page_pa(src_tpage);
6826 }
6827
6828 /*
6829 * If destination buffer or length is not aligned then do read-modify-write:
6830 * - decrypt destination in an intermediate buffer
6831 * - copy the source buffer in an intermediate buffer
6832 * - use the intermediate buffer as source buffer
6833 */
6834 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
6835 int dst_offset;
6836
6837 dst_tpage = alloc_page(GFP_KERNEL);
6838 if (!dst_tpage) {
6839 ret = -ENOMEM;
6840 goto e_free;
6841 }
6842
6843 ret = __sev_dbg_decrypt(kvm, dst_paddr,
6844 __sme_page_pa(dst_tpage), size, error);
6845 if (ret)
6846 goto e_free;
6847
6848 /*
6849 * If source is kernel buffer then use memcpy() otherwise
6850 * copy_from_user().
6851 */
6852 dst_offset = dst_paddr & 15;
6853
6854 if (src_tpage)
6855 memcpy(page_address(dst_tpage) + dst_offset,
6856 page_address(src_tpage), size);
6857 else {
6858 if (copy_from_user(page_address(dst_tpage) + dst_offset,
6859 (void __user *)(uintptr_t)vaddr, size)) {
6860 ret = -EFAULT;
6861 goto e_free;
6862 }
6863 }
6864
6865 paddr = __sme_page_pa(dst_tpage);
6866 dst_paddr = round_down(dst_paddr, 16);
6867 len = round_up(size, 16);
6868 }
6869
6870 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
6871
6872e_free:
6873 if (src_tpage)
6874 __free_page(src_tpage);
6875 if (dst_tpage)
6876 __free_page(dst_tpage);
6877 return ret;
6878}
6879
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006880static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
6881{
6882 unsigned long vaddr, vaddr_end, next_vaddr;
Colin Ian King0186ec82018-08-28 16:22:28 +01006883 unsigned long dst_vaddr;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006884 struct page **src_p, **dst_p;
6885 struct kvm_sev_dbg debug;
6886 unsigned long n;
David Rientjesb86bc282019-03-25 11:47:31 -07006887 unsigned int size;
6888 int ret;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006889
6890 if (!sev_guest(kvm))
6891 return -ENOTTY;
6892
6893 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
6894 return -EFAULT;
6895
David Rientjesb86bc282019-03-25 11:47:31 -07006896 if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr)
6897 return -EINVAL;
6898 if (!debug.dst_uaddr)
6899 return -EINVAL;
6900
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006901 vaddr = debug.src_uaddr;
6902 size = debug.len;
6903 vaddr_end = vaddr + size;
6904 dst_vaddr = debug.dst_uaddr;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006905
6906 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
6907 int len, s_off, d_off;
6908
6909 /* lock userspace source and destination page */
6910 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
6911 if (!src_p)
6912 return -EFAULT;
6913
6914 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
6915 if (!dst_p) {
6916 sev_unpin_memory(kvm, src_p, n);
6917 return -EFAULT;
6918 }
6919
6920 /*
6921 * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
6922 * memory content (i.e it will write the same memory region with C=1).
6923 * It's possible that the cache may contain the data with C=0, i.e.,
6924 * unencrypted so invalidate it first.
6925 */
6926 sev_clflush_pages(src_p, 1);
6927 sev_clflush_pages(dst_p, 1);
6928
6929 /*
6930 * Since user buffer may not be page aligned, calculate the
6931 * offset within the page.
6932 */
6933 s_off = vaddr & ~PAGE_MASK;
6934 d_off = dst_vaddr & ~PAGE_MASK;
6935 len = min_t(size_t, (PAGE_SIZE - s_off), size);
6936
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006937 if (dec)
6938 ret = __sev_dbg_decrypt_user(kvm,
6939 __sme_page_pa(src_p[0]) + s_off,
6940 dst_vaddr,
6941 __sme_page_pa(dst_p[0]) + d_off,
6942 len, &argp->error);
6943 else
6944 ret = __sev_dbg_encrypt_user(kvm,
6945 __sme_page_pa(src_p[0]) + s_off,
6946 vaddr,
6947 __sme_page_pa(dst_p[0]) + d_off,
6948 dst_vaddr,
6949 len, &argp->error);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006950
David Rientjesb86bc282019-03-25 11:47:31 -07006951 sev_unpin_memory(kvm, src_p, n);
6952 sev_unpin_memory(kvm, dst_p, n);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006953
6954 if (ret)
6955 goto err;
6956
6957 next_vaddr = vaddr + len;
6958 dst_vaddr = dst_vaddr + len;
6959 size -= len;
6960 }
6961err:
6962 return ret;
6963}
6964
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006965static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
6966{
Sean Christopherson81811c12018-03-20 12:17:21 -07006967 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006968 struct sev_data_launch_secret *data;
6969 struct kvm_sev_launch_secret params;
6970 struct page **pages;
6971 void *blob, *hdr;
6972 unsigned long n;
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006973 int ret, offset;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006974
6975 if (!sev_guest(kvm))
6976 return -ENOTTY;
6977
6978 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6979 return -EFAULT;
6980
6981 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
6982 if (!pages)
6983 return -ENOMEM;
6984
6985 /*
6986 * The secret must be copied into contiguous memory region, lets verify
6987 * that userspace memory pages are contiguous before we issue command.
6988 */
6989 if (get_num_contig_pages(0, pages, n) != n) {
6990 ret = -EINVAL;
6991 goto e_unpin_memory;
6992 }
6993
6994 ret = -ENOMEM;
Ben Gardon1ec69642019-02-11 11:02:51 -08006995 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006996 if (!data)
6997 goto e_unpin_memory;
6998
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006999 offset = params.guest_uaddr & (PAGE_SIZE - 1);
7000 data->guest_address = __sme_page_pa(pages[0]) + offset;
7001 data->guest_len = params.guest_len;
7002
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06007003 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
7004 if (IS_ERR(blob)) {
7005 ret = PTR_ERR(blob);
7006 goto e_free;
7007 }
7008
7009 data->trans_address = __psp_pa(blob);
7010 data->trans_len = params.trans_len;
7011
7012 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
7013 if (IS_ERR(hdr)) {
7014 ret = PTR_ERR(hdr);
7015 goto e_free_blob;
7016 }
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06007017 data->hdr_address = __psp_pa(hdr);
7018 data->hdr_len = params.hdr_len;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06007019
7020 data->handle = sev->handle;
7021 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
7022
7023 kfree(hdr);
7024
7025e_free_blob:
7026 kfree(blob);
7027e_free:
7028 kfree(data);
7029e_unpin_memory:
7030 sev_unpin_memory(kvm, pages, n);
7031 return ret;
7032}
7033
Brijesh Singh1654efc2017-12-04 10:57:34 -06007034static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
7035{
7036 struct kvm_sev_cmd sev_cmd;
7037 int r;
7038
7039 if (!svm_sev_enabled())
7040 return -ENOTTY;
7041
7042 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
7043 return -EFAULT;
7044
7045 mutex_lock(&kvm->lock);
7046
7047 switch (sev_cmd.id) {
7048 case KVM_SEV_INIT:
7049 r = sev_guest_init(kvm, &sev_cmd);
7050 break;
Brijesh Singh59414c92017-12-04 10:57:35 -06007051 case KVM_SEV_LAUNCH_START:
7052 r = sev_launch_start(kvm, &sev_cmd);
7053 break;
Brijesh Singh89c50582017-12-04 10:57:35 -06007054 case KVM_SEV_LAUNCH_UPDATE_DATA:
7055 r = sev_launch_update_data(kvm, &sev_cmd);
7056 break;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06007057 case KVM_SEV_LAUNCH_MEASURE:
7058 r = sev_launch_measure(kvm, &sev_cmd);
7059 break;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06007060 case KVM_SEV_LAUNCH_FINISH:
7061 r = sev_launch_finish(kvm, &sev_cmd);
7062 break;
Brijesh Singh255d9e72017-12-04 10:57:37 -06007063 case KVM_SEV_GUEST_STATUS:
7064 r = sev_guest_status(kvm, &sev_cmd);
7065 break;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06007066 case KVM_SEV_DBG_DECRYPT:
7067 r = sev_dbg_crypt(kvm, &sev_cmd, true);
7068 break;
Brijesh Singh7d1594f2017-12-04 10:57:37 -06007069 case KVM_SEV_DBG_ENCRYPT:
7070 r = sev_dbg_crypt(kvm, &sev_cmd, false);
7071 break;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06007072 case KVM_SEV_LAUNCH_SECRET:
7073 r = sev_launch_secret(kvm, &sev_cmd);
7074 break;
Brijesh Singh1654efc2017-12-04 10:57:34 -06007075 default:
7076 r = -EINVAL;
7077 goto out;
7078 }
7079
7080 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
7081 r = -EFAULT;
7082
7083out:
7084 mutex_unlock(&kvm->lock);
7085 return r;
7086}
7087
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007088static int svm_register_enc_region(struct kvm *kvm,
7089 struct kvm_enc_region *range)
7090{
Sean Christopherson81811c12018-03-20 12:17:21 -07007091 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007092 struct enc_region *region;
7093 int ret = 0;
7094
7095 if (!sev_guest(kvm))
7096 return -ENOTTY;
7097
Dan Carpenter86bf20c2018-05-19 09:01:36 +03007098 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
7099 return -EINVAL;
7100
Ben Gardon1ec69642019-02-11 11:02:51 -08007101 region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT);
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007102 if (!region)
7103 return -ENOMEM;
7104
7105 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
7106 if (!region->pages) {
7107 ret = -ENOMEM;
7108 goto e_free;
7109 }
7110
7111 /*
7112 * The guest may change the memory encryption attribute from C=0 -> C=1
7113 * or vice versa for this memory range. Lets make sure caches are
7114 * flushed to ensure that guest data gets written into memory with
7115 * correct C-bit.
7116 */
7117 sev_clflush_pages(region->pages, region->npages);
7118
7119 region->uaddr = range->addr;
7120 region->size = range->size;
7121
7122 mutex_lock(&kvm->lock);
7123 list_add_tail(&region->list, &sev->regions_list);
7124 mutex_unlock(&kvm->lock);
7125
7126 return ret;
7127
7128e_free:
7129 kfree(region);
7130 return ret;
7131}
7132
7133static struct enc_region *
7134find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
7135{
Sean Christopherson81811c12018-03-20 12:17:21 -07007136 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007137 struct list_head *head = &sev->regions_list;
7138 struct enc_region *i;
7139
7140 list_for_each_entry(i, head, list) {
7141 if (i->uaddr == range->addr &&
7142 i->size == range->size)
7143 return i;
7144 }
7145
7146 return NULL;
7147}
7148
7149
7150static int svm_unregister_enc_region(struct kvm *kvm,
7151 struct kvm_enc_region *range)
7152{
7153 struct enc_region *region;
7154 int ret;
7155
7156 mutex_lock(&kvm->lock);
7157
7158 if (!sev_guest(kvm)) {
7159 ret = -ENOTTY;
7160 goto failed;
7161 }
7162
7163 region = find_enc_region(kvm, range);
7164 if (!region) {
7165 ret = -EINVAL;
7166 goto failed;
7167 }
7168
7169 __unregister_enc_region_locked(kvm, region);
7170
7171 mutex_unlock(&kvm->lock);
7172 return 0;
7173
7174failed:
7175 mutex_unlock(&kvm->lock);
7176 return ret;
7177}
7178
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007179static bool svm_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
7180{
Liran Alon118154b2019-07-17 02:56:58 +03007181 unsigned long cr4 = kvm_read_cr4(vcpu);
7182 bool smep = cr4 & X86_CR4_SMEP;
7183 bool smap = cr4 & X86_CR4_SMAP;
7184 bool is_user = svm_get_cpl(vcpu) == 3;
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007185
7186 /*
Liran Alon118154b2019-07-17 02:56:58 +03007187 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
7188 *
7189 * Errata:
7190 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is
7191 * possible that CPU microcode implementing DecodeAssist will fail
7192 * to read bytes of instruction which caused #NPF. In this case,
7193 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly
7194 * return 0 instead of the correct guest instruction bytes.
7195 *
7196 * This happens because CPU microcode reading instruction bytes
7197 * uses a special opcode which attempts to read data using CPL=0
7198 * priviledges. The microcode reads CS:RIP and if it hits a SMAP
7199 * fault, it gives up and returns no instruction bytes.
7200 *
7201 * Detection:
7202 * We reach here in case CPU supports DecodeAssist, raised #NPF and
7203 * returned 0 in GuestIntrBytes field of the VMCB.
7204 * First, errata can only be triggered in case vCPU CR4.SMAP=1.
7205 * Second, if vCPU CR4.SMEP=1, errata could only be triggered
7206 * in case vCPU CPL==3 (Because otherwise guest would have triggered
7207 * a SMEP fault instead of #NPF).
7208 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL.
7209 * As most guests enable SMAP if they have also enabled SMEP, use above
7210 * logic in order to attempt minimize false-positive of detecting errata
7211 * while still preserving all cases semantic correctness.
7212 *
7213 * Workaround:
7214 * To determine what instruction the guest was executing, the hypervisor
7215 * will have to decode the instruction at the instruction pointer.
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007216 *
7217 * In non SEV guest, hypervisor will be able to read the guest
7218 * memory to decode the instruction pointer when insn_len is zero
7219 * so we return true to indicate that decoding is possible.
7220 *
7221 * But in the SEV guest, the guest memory is encrypted with the
7222 * guest specific key and hypervisor will not be able to decode the
7223 * instruction pointer so we will not able to workaround it. Lets
7224 * print the error and request to kill the guest.
7225 */
Liran Alon118154b2019-07-17 02:56:58 +03007226 if (smap && (!smep || is_user)) {
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007227 if (!sev_guest(vcpu->kvm))
7228 return true;
7229
Liran Alon118154b2019-07-17 02:56:58 +03007230 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007231 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7232 }
7233
7234 return false;
7235}
7236
Liran Alon4b9852f2019-08-26 13:24:49 +03007237static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7238{
7239 struct vcpu_svm *svm = to_svm(vcpu);
7240
7241 /*
7242 * TODO: Last condition latch INIT signals on vCPU when
7243 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
7244 * To properly emulate the INIT intercept, SVM should implement
7245 * kvm_x86_ops->check_nested_events() and call nested_svm_vmexit()
7246 * there if an INIT signal is pending.
7247 */
7248 return !gif_set(svm) ||
7249 (svm->vmcb->control.intercept & (1ULL << INTERCEPT_INIT));
7250}
7251
Kees Cook404f6aa2016-08-08 16:29:06 -07007252static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007253 .cpu_has_kvm_support = has_svm,
7254 .disabled_by_bios = is_disabled,
7255 .hardware_setup = svm_hardware_setup,
7256 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03007257 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007258 .hardware_enable = svm_hardware_enable,
7259 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02007260 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Tom Lendackybc226f02018-05-10 22:06:39 +02007261 .has_emulated_msr = svm_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007262
7263 .vcpu_create = svm_create_vcpu,
7264 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007265 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007266
Sean Christopherson434a1e92018-03-20 12:17:18 -07007267 .vm_alloc = svm_vm_alloc,
7268 .vm_free = svm_vm_free,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007269 .vm_init = avic_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06007270 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007271
Avi Kivity04d2cc72007-09-10 18:10:54 +03007272 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007273 .vcpu_load = svm_vcpu_load,
7274 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05007275 .vcpu_blocking = svm_vcpu_blocking,
7276 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007277
Paolo Bonzinia96036b2015-11-10 11:55:36 +01007278 .update_bp_intercept = update_bp_intercept,
Tom Lendacky801e4592018-02-21 13:39:51 -06007279 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007280 .get_msr = svm_get_msr,
7281 .set_msr = svm_set_msr,
7282 .get_segment_base = svm_get_segment_base,
7283 .get_segment = svm_get_segment,
7284 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007285 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10007286 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02007287 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03007288 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007289 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007290 .set_cr3 = svm_set_cr3,
7291 .set_cr4 = svm_set_cr4,
7292 .set_efer = svm_set_efer,
7293 .get_idt = svm_get_idt,
7294 .set_idt = svm_set_idt,
7295 .get_gdt = svm_get_gdt,
7296 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007297 .get_dr6 = svm_get_dr6,
7298 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03007299 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01007300 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03007301 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007302 .get_rflags = svm_get_rflags,
7303 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007304
Avi Kivity6aa8b732006-12-10 02:21:36 -08007305 .tlb_flush = svm_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007306 .tlb_flush_gva = svm_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007307
Avi Kivity6aa8b732006-12-10 02:21:36 -08007308 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007309 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007310 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007311 .set_interrupt_shadow = svm_set_interrupt_shadow,
7312 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007313 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007314 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007315 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007316 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007317 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007318 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007319 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007320 .get_nmi_mask = svm_get_nmi_mask,
7321 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007322 .enable_nmi_window = enable_nmi_window,
7323 .enable_irq_window = enable_irq_window,
7324 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007325 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007326 .get_enable_apicv = svm_get_enable_apicv,
7327 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007328 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007329 .hwapic_irr_update = svm_hwapic_irr_update,
7330 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02007331 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05007332 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02007333
7334 .set_tss_addr = svm_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007335 .set_identity_map_addr = svm_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08007336 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007337 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007338
Avi Kivity586f9602010-11-18 13:09:54 +02007339 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007340
Sheng Yang17cc3932010-01-05 19:02:27 +08007341 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08007342
7343 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007344
7345 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00007346 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01007347 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08007348 .xsaves_supported = svm_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +02007349 .umip_emulated = svm_umip_emulated,
Chao Peng86f52012018-10-24 16:05:11 +08007350 .pt_supported = svm_pt_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007351
7352 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007353
7354 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007355
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02007356 .read_l1_tsc_offset = svm_read_l1_tsc_offset,
Leonid Shatz326e7422018-11-06 12:14:25 +02007357 .write_l1_tsc_offset = svm_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007358
7359 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007360
7361 .check_intercept = svm_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07007362 .handle_exit_irqoff = svm_handle_exit_irqoff,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007363
Sean Christophersond264ee02018-08-27 15:21:12 -07007364 .request_immediate_exit = __kvm_request_immediate_exit,
7365
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007366 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02007367
7368 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05007369 .deliver_posted_interrupt = svm_deliver_avic_intr,
Wanpeng Li17e433b2019-08-05 10:03:19 +08007370 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05007371 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02007372 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007373
Ladi Prosek72d7b372017-10-11 16:54:41 +02007374 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007375 .pre_enter_smm = svm_pre_enter_smm,
7376 .pre_leave_smm = svm_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007377 .enable_smi_window = enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06007378
7379 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007380 .mem_enc_reg_region = svm_register_enc_region,
7381 .mem_enc_unreg_region = svm_unregister_enc_region,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007382
Vitaly Kuznetsov956e2552019-08-28 09:59:04 +02007383 .nested_enable_evmcs = NULL,
Vitaly Kuznetsovea152982019-08-27 18:04:02 +02007384 .nested_get_evmcs_version = NULL,
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007385
7386 .need_emulation_on_page_fault = svm_need_emulation_on_page_fault,
Liran Alon4b9852f2019-08-26 13:24:49 +03007387
7388 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007389};
7390
7391static int __init svm_init(void)
7392{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007393 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03007394 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007395}
7396
7397static void __exit svm_exit(void)
7398{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007399 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08007400}
7401
7402module_init(svm_init)
7403module_exit(svm_exit)