blob: a7b358f20aca0767110127ed5a4c9924575a306d [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;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100742 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600743 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744
Alexander Graf9962d032008-11-25 20:17:02 +0100745 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100746 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747}
748
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749static int is_external_interrupt(u32 info)
750{
751 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
752 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
753}
754
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200755static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400756{
757 struct vcpu_svm *svm = to_svm(vcpu);
758 u32 ret = 0;
759
760 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200761 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
762 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400763}
764
765static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
766{
767 struct vcpu_svm *svm = to_svm(vcpu);
768
769 if (mask == 0)
770 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
771 else
772 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
773
774}
775
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200776static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400778 struct vcpu_svm *svm = to_svm(vcpu);
779
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200780 if (nrips && svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200781 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200782 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400783 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200784
Sean Christopherson1957aa62019-08-27 14:40:39 -0700785 if (!svm->next_rip) {
786 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
787 return 0;
788 } else {
789 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
790 pr_err("%s: ip 0x%lx next 0x%llx\n",
791 __func__, kvm_rip_read(vcpu), svm->next_rip);
792 kvm_rip_write(vcpu, svm->next_rip);
793 }
Glauber Costa2809f5d2009-05-12 16:21:05 -0400794 svm_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200795
Sean Christopherson60fc3d02019-08-27 14:40:38 -0700796 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797}
798
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700799static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100800{
801 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700802 unsigned nr = vcpu->arch.exception.nr;
803 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700804 bool reinject = vcpu->arch.exception.injected;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700805 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100806
Joerg Roedele0231712010-02-24 18:59:10 +0100807 /*
808 * If we are within a nested VM we'd better #VMEXIT and let the guest
809 * handle the exception
810 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200811 if (!reinject &&
812 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100813 return;
814
Jim Mattsonda998b42018-10-16 14:29:22 -0700815 kvm_deliver_exception_payload(&svm->vcpu);
816
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200817 if (nr == BP_VECTOR && !nrips) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100818 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
819
820 /*
821 * For guest debugging where we have to reinject #BP if some
822 * INT3 is guest-owned:
823 * Emulate nRIP by moving RIP forward. Will fail if injection
824 * raises a fault that is not intercepted. Still better than
825 * failing in all cases.
826 */
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200827 (void)skip_emulated_instruction(&svm->vcpu);
Jan Kiszka66b71382010-02-23 17:47:56 +0100828 rip = kvm_rip_read(&svm->vcpu);
829 svm->int3_rip = rip + svm->vmcb->save.cs.base;
830 svm->int3_injected = rip - old_rip;
831 }
832
Jan Kiszka116a4752010-02-23 17:47:54 +0100833 svm->vmcb->control.event_inj = nr
834 | SVM_EVTINJ_VALID
835 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
836 | SVM_EVTINJ_TYPE_EXEPT;
837 svm->vmcb->control.event_inj_err = error_code;
838}
839
Joerg Roedel67ec6602010-05-17 14:43:35 +0200840static void svm_init_erratum_383(void)
841{
842 u32 low, high;
843 int err;
844 u64 val;
845
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100846 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200847 return;
848
849 /* Use _safe variants to not break nested virtualization */
850 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
851 if (err)
852 return;
853
854 val |= (1ULL << 47);
855
856 low = lower_32_bits(val);
857 high = upper_32_bits(val);
858
859 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
860
861 erratum_383_found = true;
862}
863
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500864static void svm_init_osvw(struct kvm_vcpu *vcpu)
865{
866 /*
867 * Guests should see errata 400 and 415 as fixed (assuming that
868 * HLT and IO instructions are intercepted).
869 */
870 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
871 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
872
873 /*
874 * By increasing VCPU's osvw.length to 3 we are telling the guest that
875 * all osvw.status bits inside that length, including bit 0 (which is
876 * reserved for erratum 298), are valid. However, if host processor's
877 * osvw_len is 0 then osvw_status[0] carries no information. We need to
878 * be conservative here and therefore we tell the guest that erratum 298
879 * is present (because we really don't know).
880 */
881 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
882 vcpu->arch.osvw.status |= 1;
883}
884
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885static int has_svm(void)
886{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200887 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888
Eduardo Habkost63d11422008-11-17 19:03:20 -0200889 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800890 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 return 0;
892 }
893
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894 return 1;
895}
896
Radim Krčmář13a34e02014-08-28 15:13:03 +0200897static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100899 /* Make sure we clean up behind us */
900 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
901 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
902
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200903 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100904
905 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906}
907
Radim Krčmář13a34e02014-08-28 15:13:03 +0200908static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909{
910
Tejun Heo0fe1e002009-10-29 22:34:14 +0900911 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 struct desc_struct *gdt;
914 int me = raw_smp_processor_id();
915
Alexander Graf10474ae2009-09-15 11:37:46 +0200916 rdmsrl(MSR_EFER, efer);
917 if (efer & EFER_SVME)
918 return -EBUSY;
919
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200921 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200922 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900924 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900925 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200926 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200927 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928 }
929
Tejun Heo0fe1e002009-10-29 22:34:14 +0900930 sd->asid_generation = 1;
931 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
932 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600933 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
Thomas Garnier45fc8752017-03-14 10:05:08 -0700935 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900936 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937
Alexander Graf9962d032008-11-25 20:17:02 +0100938 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939
Linus Torvaldsd0316552009-12-14 09:58:24 -0800940 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200941
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100942 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
943 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500944 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100945 }
946
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500947
948 /*
949 * Get OSVW bits.
950 *
951 * Note that it is possible to have a system with mixed processor
952 * revisions and therefore different OSVW bits. If bits are not the same
953 * on different processors then choose the worst case (i.e. if erratum
954 * is present on one processor and not on another then assume that the
955 * erratum is present everywhere).
956 */
957 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
958 uint64_t len, status = 0;
959 int err;
960
961 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
962 if (!err)
963 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
964 &err);
965
966 if (err)
967 osvw_status = osvw_len = 0;
968 else {
969 if (len < osvw_len)
970 osvw_len = len;
971 osvw_status |= status;
972 osvw_status &= (1ULL << osvw_len) - 1;
973 }
974 } else
975 osvw_status = osvw_len = 0;
976
Joerg Roedel67ec6602010-05-17 14:43:35 +0200977 svm_init_erratum_383();
978
Joerg Roedel1018faa2012-02-29 14:57:32 +0100979 amd_pmu_enable_virt();
980
Alexander Graf10474ae2009-09-15 11:37:46 +0200981 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982}
983
Joerg Roedel0da1db752008-07-02 16:02:11 +0200984static void svm_cpu_uninit(int cpu)
985{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900986 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200987
Tejun Heo0fe1e002009-10-29 22:34:14 +0900988 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200989 return;
990
991 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600992 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900993 __free_page(sd->save_area);
994 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200995}
996
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997static int svm_cpu_init(int cpu)
998{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900999 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000 int r;
1001
Tejun Heo0fe1e002009-10-29 22:34:14 +09001002 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
1003 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +09001005 sd->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006 r = -ENOMEM;
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001007 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +09001008 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 goto err_1;
1010
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001011 if (svm_sev_enabled()) {
1012 r = -ENOMEM;
Kees Cook6da2ec52018-06-12 13:55:00 -07001013 sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1,
1014 sizeof(void *),
1015 GFP_KERNEL);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001016 if (!sd->sev_vmcbs)
1017 goto err_1;
1018 }
1019
Tejun Heo0fe1e002009-10-29 22:34:14 +09001020 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021
1022 return 0;
1023
1024err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +09001025 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026 return r;
1027
1028}
1029
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001030static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031{
1032 int i;
1033
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001034 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
1035 if (direct_access_msrs[i].index == index)
1036 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001038 return false;
1039}
1040
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001041static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr)
1042{
1043 u8 bit_write;
1044 unsigned long tmp;
1045 u32 offset;
1046 u32 *msrpm;
1047
1048 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
1049 to_svm(vcpu)->msrpm;
1050
1051 offset = svm_msrpm_offset(msr);
1052 bit_write = 2 * (msr & 0x0f) + 1;
1053 tmp = msrpm[offset];
1054
1055 BUG_ON(offset == MSR_INVALID);
1056
1057 return !!test_bit(bit_write, &tmp);
1058}
1059
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060static void set_msr_interception(u32 *msrpm, unsigned msr,
1061 int read, int write)
1062{
Joerg Roedel455716f2010-03-01 15:34:35 +01001063 u8 bit_read, bit_write;
1064 unsigned long tmp;
1065 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001067 /*
1068 * If this warning triggers extend the direct_access_msrs list at the
1069 * beginning of the file
1070 */
1071 WARN_ON(!valid_msr_intercept(msr));
1072
Joerg Roedel455716f2010-03-01 15:34:35 +01001073 offset = svm_msrpm_offset(msr);
1074 bit_read = 2 * (msr & 0x0f);
1075 bit_write = 2 * (msr & 0x0f) + 1;
1076 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077
Joerg Roedel455716f2010-03-01 15:34:35 +01001078 BUG_ON(offset == MSR_INVALID);
1079
1080 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
1081 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
1082
1083 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001084}
1085
Joerg Roedelf65c2292008-02-13 18:58:46 +01001086static void svm_vcpu_init_msrpm(u32 *msrpm)
1087{
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001088 int i;
1089
Joerg Roedelf65c2292008-02-13 18:58:46 +01001090 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
1091
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001092 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1093 if (!direct_access_msrs[i].always)
1094 continue;
1095
1096 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
1097 }
Joerg Roedelf65c2292008-02-13 18:58:46 +01001098}
1099
Joerg Roedel323c3d82010-03-01 15:34:37 +01001100static void add_msr_offset(u32 offset)
1101{
1102 int i;
1103
1104 for (i = 0; i < MSRPM_OFFSETS; ++i) {
1105
1106 /* Offset already in list? */
1107 if (msrpm_offsets[i] == offset)
1108 return;
1109
1110 /* Slot used by another offset? */
1111 if (msrpm_offsets[i] != MSR_INVALID)
1112 continue;
1113
1114 /* Add offset to list */
1115 msrpm_offsets[i] = offset;
1116
1117 return;
1118 }
1119
1120 /*
1121 * If this BUG triggers the msrpm_offsets table has an overflow. Just
1122 * increase MSRPM_OFFSETS in this case.
1123 */
1124 BUG();
1125}
1126
1127static void init_msrpm_offsets(void)
1128{
1129 int i;
1130
1131 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
1132
1133 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1134 u32 offset;
1135
1136 offset = svm_msrpm_offset(direct_access_msrs[i].index);
1137 BUG_ON(offset == MSR_INVALID);
1138
1139 add_msr_offset(offset);
1140 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141}
1142
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001143static void svm_enable_lbrv(struct vcpu_svm *svm)
1144{
1145 u32 *msrpm = svm->msrpm;
1146
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001147 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001148 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
1149 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
1150 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
1151 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
1152}
1153
1154static void svm_disable_lbrv(struct vcpu_svm *svm)
1155{
1156 u32 *msrpm = svm->msrpm;
1157
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001158 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001159 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
1160 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
1161 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
1162 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
1163}
1164
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001165static void disable_nmi_singlestep(struct vcpu_svm *svm)
1166{
1167 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001168
Ladi Prosekab2f4d732017-06-21 09:06:58 +02001169 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
1170 /* Clear our flags if they were not set by the guest */
1171 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1172 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
1173 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1174 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
1175 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001176}
1177
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001178/* Note:
Sean Christopherson81811c12018-03-20 12:17:21 -07001179 * This hash table is used to map VM_ID to a struct kvm_svm,
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001180 * when handling AMD IOMMU GALOG notification to schedule in
1181 * a particular vCPU.
1182 */
1183#define SVM_VM_DATA_HASH_BITS 8
David Hildenbrand681bcea2017-01-24 22:21:16 +01001184static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001185static u32 next_vm_id = 0;
1186static bool next_vm_id_wrapped = 0;
David Hildenbrand681bcea2017-01-24 22:21:16 +01001187static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001188
1189/* Note:
1190 * This function is called from IOMMU driver to notify
1191 * SVM to schedule in a particular vCPU of a particular VM.
1192 */
1193static int avic_ga_log_notifier(u32 ga_tag)
1194{
1195 unsigned long flags;
Sean Christopherson81811c12018-03-20 12:17:21 -07001196 struct kvm_svm *kvm_svm;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001197 struct kvm_vcpu *vcpu = NULL;
1198 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
1199 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
1200
1201 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
1202
1203 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Sean Christopherson81811c12018-03-20 12:17:21 -07001204 hash_for_each_possible(svm_vm_data_hash, kvm_svm, hnode, vm_id) {
1205 if (kvm_svm->avic_vm_id != vm_id)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001206 continue;
Sean Christopherson81811c12018-03-20 12:17:21 -07001207 vcpu = kvm_get_vcpu_by_id(&kvm_svm->kvm, vcpu_id);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001208 break;
1209 }
1210 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1211
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001212 /* Note:
1213 * At this point, the IOMMU should have already set the pending
1214 * bit in the vAPIC backing page. So, we just need to schedule
1215 * in the vcpu.
1216 */
Paolo Bonzini1cf53582017-10-10 12:51:56 +02001217 if (vcpu)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001218 kvm_vcpu_wake_up(vcpu);
1219
1220 return 0;
1221}
1222
Brijesh Singhe9df0942017-12-04 10:57:33 -06001223static __init int sev_hardware_setup(void)
1224{
1225 struct sev_user_data_status *status;
1226 int rc;
1227
1228 /* Maximum number of encrypted guests supported simultaneously */
1229 max_sev_asid = cpuid_ecx(0x8000001F);
1230
1231 if (!max_sev_asid)
1232 return 1;
1233
Brijesh Singh1654efc2017-12-04 10:57:34 -06001234 /* Minimum ASID value that should be used for SEV guest */
1235 min_sev_asid = cpuid_edx(0x8000001F);
1236
Tom Lendacky33af3a72019-10-03 21:17:48 +00001237 /* Initialize SEV ASID bitmaps */
Andy Shevchenkoa101c9d632018-08-30 14:49:59 +03001238 sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001239 if (!sev_asid_bitmap)
1240 return 1;
1241
Tom Lendacky33af3a72019-10-03 21:17:48 +00001242 sev_reclaim_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
1243 if (!sev_reclaim_asid_bitmap)
1244 return 1;
1245
Brijesh Singhe9df0942017-12-04 10:57:33 -06001246 status = kmalloc(sizeof(*status), GFP_KERNEL);
1247 if (!status)
1248 return 1;
1249
1250 /*
1251 * Check SEV platform status.
1252 *
1253 * PLATFORM_STATUS can be called in any state, if we failed to query
1254 * the PLATFORM status then either PSP firmware does not support SEV
1255 * feature or SEV firmware is dead.
1256 */
1257 rc = sev_platform_status(status, NULL);
1258 if (rc)
1259 goto err;
1260
1261 pr_info("SEV supported\n");
1262
1263err:
1264 kfree(status);
1265 return rc;
1266}
1267
Babu Moger8566ac82018-03-16 16:37:26 -04001268static void grow_ple_window(struct kvm_vcpu *vcpu)
1269{
1270 struct vcpu_svm *svm = to_svm(vcpu);
1271 struct vmcb_control_area *control = &svm->vmcb->control;
1272 int old = control->pause_filter_count;
1273
1274 control->pause_filter_count = __grow_ple_window(old,
1275 pause_filter_count,
1276 pause_filter_count_grow,
1277 pause_filter_count_max);
1278
Peter Xu4f75bcc2019-09-06 10:17:22 +08001279 if (control->pause_filter_count != old) {
Babu Moger8566ac82018-03-16 16:37:26 -04001280 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +08001281 trace_kvm_ple_window_update(vcpu->vcpu_id,
1282 control->pause_filter_count, old);
1283 }
Babu Moger8566ac82018-03-16 16:37:26 -04001284}
1285
1286static void shrink_ple_window(struct kvm_vcpu *vcpu)
1287{
1288 struct vcpu_svm *svm = to_svm(vcpu);
1289 struct vmcb_control_area *control = &svm->vmcb->control;
1290 int old = control->pause_filter_count;
1291
1292 control->pause_filter_count =
1293 __shrink_ple_window(old,
1294 pause_filter_count,
1295 pause_filter_count_shrink,
1296 pause_filter_count);
Peter Xu4f75bcc2019-09-06 10:17:22 +08001297 if (control->pause_filter_count != old) {
Babu Moger8566ac82018-03-16 16:37:26 -04001298 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +08001299 trace_kvm_ple_window_update(vcpu->vcpu_id,
1300 control->pause_filter_count, old);
1301 }
Babu Moger8566ac82018-03-16 16:37:26 -04001302}
1303
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304static __init int svm_hardware_setup(void)
1305{
1306 int cpu;
1307 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001308 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309 int r;
1310
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
1312
1313 if (!iopm_pages)
1314 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +03001315
1316 iopm_va = page_address(iopm_pages);
1317 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
1319
Joerg Roedel323c3d82010-03-01 15:34:37 +01001320 init_msrpm_offsets();
1321
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001322 if (boot_cpu_has(X86_FEATURE_NX))
1323 kvm_enable_efer_bits(EFER_NX);
1324
Alexander Graf1b2fd702009-02-02 16:23:51 +01001325 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
1326 kvm_enable_efer_bits(EFER_FFXSR);
1327
Joerg Roedel92a1f122011-03-25 09:44:51 +01001328 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +01001329 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +08001330 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1331 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +01001332 }
1333
Babu Moger8566ac82018-03-16 16:37:26 -04001334 /* Check for pause filtering support */
1335 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
1336 pause_filter_count = 0;
1337 pause_filter_thresh = 0;
1338 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
1339 pause_filter_thresh = 0;
1340 }
1341
Alexander Graf236de052008-11-25 20:17:10 +01001342 if (nested) {
1343 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +02001344 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +01001345 }
1346
Brijesh Singhe9df0942017-12-04 10:57:33 -06001347 if (sev) {
1348 if (boot_cpu_has(X86_FEATURE_SEV) &&
1349 IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
1350 r = sev_hardware_setup();
1351 if (r)
1352 sev = false;
1353 } else {
1354 sev = false;
1355 }
1356 }
1357
Zachary Amsden3230bb42009-09-29 11:38:37 -10001358 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359 r = svm_cpu_init(cpu);
1360 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +01001361 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +01001363
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001364 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001365 npt_enabled = false;
1366
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001367 if (npt_enabled && !npt) {
1368 printk(KERN_INFO "kvm: Nested Paging disabled\n");
1369 npt_enabled = false;
1370 }
1371
Joerg Roedel18552672008-02-07 13:47:41 +01001372 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001373 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +01001374 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +02001375 } else
1376 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001377
Paolo Bonzinid647eb62019-06-20 14:13:33 +02001378 if (nrips) {
1379 if (!boot_cpu_has(X86_FEATURE_NRIPS))
1380 nrips = false;
1381 }
1382
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001383 if (avic) {
1384 if (!npt_enabled ||
1385 !boot_cpu_has(X86_FEATURE_AVIC) ||
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001386 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001387 avic = false;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001388 } else {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001389 pr_info("AVIC enabled\n");
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001390
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001391 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1392 }
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001393 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001394
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001395 if (vls) {
1396 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001397 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001398 !IS_ENABLED(CONFIG_X86_64)) {
1399 vls = false;
1400 } else {
1401 pr_info("Virtual VMLOAD VMSAVE supported\n");
1402 }
1403 }
1404
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001405 if (vgif) {
1406 if (!boot_cpu_has(X86_FEATURE_VGIF))
1407 vgif = false;
1408 else
1409 pr_info("Virtual GIF supported\n");
1410 }
1411
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 return 0;
1413
Joerg Roedelf65c2292008-02-13 18:58:46 +01001414err:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1416 iopm_base = 0;
1417 return r;
1418}
1419
1420static __exit void svm_hardware_unsetup(void)
1421{
Joerg Roedel0da1db752008-07-02 16:02:11 +02001422 int cpu;
1423
Tom Lendacky33af3a72019-10-03 21:17:48 +00001424 if (svm_sev_enabled()) {
Andy Shevchenkoa101c9d632018-08-30 14:49:59 +03001425 bitmap_free(sev_asid_bitmap);
Tom Lendacky33af3a72019-10-03 21:17:48 +00001426 bitmap_free(sev_reclaim_asid_bitmap);
1427
1428 sev_flush_asids();
1429 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001430
Zachary Amsden3230bb42009-09-29 11:38:37 -10001431 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +02001432 svm_cpu_uninit(cpu);
1433
Avi Kivity6aa8b732006-12-10 02:21:36 -08001434 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001435 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436}
1437
1438static void init_seg(struct vmcb_seg *seg)
1439{
1440 seg->selector = 0;
1441 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001442 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443 seg->limit = 0xffff;
1444 seg->base = 0;
1445}
1446
1447static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1448{
1449 seg->selector = 0;
1450 seg->attrib = SVM_SELECTOR_P_MASK | type;
1451 seg->limit = 0xffff;
1452 seg->base = 0;
1453}
1454
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001455static u64 svm_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
1456{
1457 struct vcpu_svm *svm = to_svm(vcpu);
1458
1459 if (is_guest_mode(vcpu))
1460 return svm->nested.hsave->control.tsc_offset;
1461
1462 return vcpu->arch.tsc_offset;
1463}
1464
Leonid Shatz326e7422018-11-06 12:14:25 +02001465static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001466{
1467 struct vcpu_svm *svm = to_svm(vcpu);
1468 u64 g_tsc_offset = 0;
1469
Joerg Roedel20307532010-11-29 17:51:48 +01001470 if (is_guest_mode(vcpu)) {
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001471 /* Write L1's TSC offset. */
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001472 g_tsc_offset = svm->vmcb->control.tsc_offset -
1473 svm->nested.hsave->control.tsc_offset;
1474 svm->nested.hsave->control.tsc_offset = offset;
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001475 }
1476
1477 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1478 svm->vmcb->control.tsc_offset - g_tsc_offset,
1479 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001480
1481 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001482
1483 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Leonid Shatz326e7422018-11-06 12:14:25 +02001484 return svm->vmcb->control.tsc_offset;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001485}
1486
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001487static void avic_init_vmcb(struct vcpu_svm *svm)
1488{
1489 struct vmcb *vmcb = svm->vmcb;
Sean Christopherson81811c12018-03-20 12:17:21 -07001490 struct kvm_svm *kvm_svm = to_kvm_svm(svm->vcpu.kvm);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001491 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
Sean Christopherson81811c12018-03-20 12:17:21 -07001492 phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page));
1493 phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page));
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001494
1495 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1496 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1497 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1498 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1499 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001500}
1501
Paolo Bonzini56908912015-10-19 11:30:19 +02001502static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503{
Joerg Roedele6101a92008-02-13 18:58:45 +01001504 struct vmcb_control_area *control = &svm->vmcb->control;
1505 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001507 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001508
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001509 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1510 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1511 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1512 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1513 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1514 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001515 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1516 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517
Paolo Bonzini5315c712014-03-03 13:08:29 +01001518 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519
Joerg Roedel18c918c2010-11-30 18:03:59 +01001520 set_exception_intercept(svm, PF_VECTOR);
1521 set_exception_intercept(svm, UD_VECTOR);
1522 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001523 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001524 set_exception_intercept(svm, DB_VECTOR);
Liran Alon97184202018-03-12 13:12:52 +02001525 /*
1526 * Guest access to VMware backdoor ports could legitimately
1527 * trigger #GP because of TSS I/O permission bitmap.
1528 * We intercept those #GP and allow access to them anyway
1529 * as VMware does.
1530 */
1531 if (enable_vmware_backdoor)
1532 set_exception_intercept(svm, GP_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001534 set_intercept(svm, INTERCEPT_INTR);
1535 set_intercept(svm, INTERCEPT_NMI);
1536 set_intercept(svm, INTERCEPT_SMI);
1537 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001538 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001539 set_intercept(svm, INTERCEPT_CPUID);
1540 set_intercept(svm, INTERCEPT_INVD);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001541 set_intercept(svm, INTERCEPT_INVLPG);
1542 set_intercept(svm, INTERCEPT_INVLPGA);
1543 set_intercept(svm, INTERCEPT_IOIO_PROT);
1544 set_intercept(svm, INTERCEPT_MSR_PROT);
1545 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1546 set_intercept(svm, INTERCEPT_SHUTDOWN);
1547 set_intercept(svm, INTERCEPT_VMRUN);
1548 set_intercept(svm, INTERCEPT_VMMCALL);
1549 set_intercept(svm, INTERCEPT_VMLOAD);
1550 set_intercept(svm, INTERCEPT_VMSAVE);
1551 set_intercept(svm, INTERCEPT_STGI);
1552 set_intercept(svm, INTERCEPT_CLGI);
1553 set_intercept(svm, INTERCEPT_SKINIT);
1554 set_intercept(svm, INTERCEPT_WBINVD);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001555 set_intercept(svm, INTERCEPT_XSETBV);
Jim Mattson0cb84102019-09-19 15:59:17 -07001556 set_intercept(svm, INTERCEPT_RDPRU);
Brijesh Singh7607b712018-02-19 10:14:44 -06001557 set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558
Wanpeng Li4d5422c2018-03-12 04:53:02 -07001559 if (!kvm_mwait_in_guest(svm->vcpu.kvm)) {
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001560 set_intercept(svm, INTERCEPT_MONITOR);
1561 set_intercept(svm, INTERCEPT_MWAIT);
1562 }
1563
Wanpeng Licaa057a2018-03-12 04:53:03 -07001564 if (!kvm_hlt_in_guest(svm->vcpu.kvm))
1565 set_intercept(svm, INTERCEPT_HLT);
1566
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001567 control->iopm_base_pa = __sme_set(iopm_base);
1568 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569 control->int_ctl = V_INTR_MASKING_MASK;
1570
1571 init_seg(&save->es);
1572 init_seg(&save->ss);
1573 init_seg(&save->ds);
1574 init_seg(&save->fs);
1575 init_seg(&save->gs);
1576
1577 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001578 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579 /* Executable/Readable Code Segment */
1580 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1581 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1582 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583
1584 save->gdtr.limit = 0xffff;
1585 save->idtr.limit = 0xffff;
1586
1587 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1588 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1589
Paolo Bonzini56908912015-10-19 11:30:19 +02001590 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001591 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001592 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001594 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595
Joerg Roedele0231712010-02-24 18:59:10 +01001596 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001597 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001598 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001600 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001601 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001602
Rusty Russell66aee912007-07-17 23:34:16 +10001603 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001605
1606 if (npt_enabled) {
1607 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001608 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001609 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001610 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001611 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1612 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001613 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001614 save->cr3 = 0;
1615 save->cr4 = 0;
1616 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001617 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001618
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001619 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001620 svm->vcpu.arch.hflags = 0;
1621
Babu Moger8566ac82018-03-16 16:37:26 -04001622 if (pause_filter_count) {
1623 control->pause_filter_count = pause_filter_count;
1624 if (pause_filter_thresh)
1625 control->pause_filter_thresh = pause_filter_thresh;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001626 set_intercept(svm, INTERCEPT_PAUSE);
Babu Moger8566ac82018-03-16 16:37:26 -04001627 } else {
1628 clr_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001629 }
1630
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001631 if (kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001632 avic_init_vmcb(svm);
1633
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001634 /*
1635 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1636 * in VMCB and clear intercepts to avoid #VMEXIT.
1637 */
1638 if (vls) {
1639 clr_intercept(svm, INTERCEPT_VMLOAD);
1640 clr_intercept(svm, INTERCEPT_VMSAVE);
1641 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1642 }
1643
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001644 if (vgif) {
1645 clr_intercept(svm, INTERCEPT_STGI);
1646 clr_intercept(svm, INTERCEPT_CLGI);
1647 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1648 }
1649
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001650 if (sev_guest(svm->vcpu.kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001651 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001652 clr_exception_intercept(svm, UD_VECTOR);
1653 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001654
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001655 mark_all_dirty(svm->vmcb);
1656
Joerg Roedel2af91942009-08-07 11:49:28 +02001657 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001658
1659}
1660
Dan Carpenterd3e7dec2017-05-18 10:38:53 +03001661static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1662 unsigned int index)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001663{
1664 u64 *avic_physical_id_table;
Sean Christopherson81811c12018-03-20 12:17:21 -07001665 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001666
1667 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1668 return NULL;
1669
Sean Christopherson81811c12018-03-20 12:17:21 -07001670 avic_physical_id_table = page_address(kvm_svm->avic_physical_id_table_page);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001671
1672 return &avic_physical_id_table[index];
1673}
1674
1675/**
1676 * Note:
1677 * AVIC hardware walks the nested page table to check permissions,
1678 * but does not use the SPA address specified in the leaf page
1679 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1680 * field of the VMCB. Therefore, we set up the
1681 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1682 */
1683static int avic_init_access_page(struct kvm_vcpu *vcpu)
1684{
1685 struct kvm *kvm = vcpu->kvm;
Wei Wang30510382018-11-12 12:23:14 +00001686 int ret = 0;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001687
Wei Wang30510382018-11-12 12:23:14 +00001688 mutex_lock(&kvm->slots_lock);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001689 if (kvm->arch.apic_access_page_done)
Wei Wang30510382018-11-12 12:23:14 +00001690 goto out;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001691
Wei Wang30510382018-11-12 12:23:14 +00001692 ret = __x86_set_memory_region(kvm,
1693 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1694 APIC_DEFAULT_PHYS_BASE,
1695 PAGE_SIZE);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001696 if (ret)
Wei Wang30510382018-11-12 12:23:14 +00001697 goto out;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001698
1699 kvm->arch.apic_access_page_done = true;
Wei Wang30510382018-11-12 12:23:14 +00001700out:
1701 mutex_unlock(&kvm->slots_lock);
1702 return ret;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001703}
1704
1705static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1706{
1707 int ret;
1708 u64 *entry, new_entry;
1709 int id = vcpu->vcpu_id;
1710 struct vcpu_svm *svm = to_svm(vcpu);
1711
1712 ret = avic_init_access_page(vcpu);
1713 if (ret)
1714 return ret;
1715
1716 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1717 return -EINVAL;
1718
1719 if (!svm->vcpu.arch.apic->regs)
1720 return -EINVAL;
1721
1722 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1723
1724 /* Setting AVIC backing page address in the phy APIC ID table */
1725 entry = avic_get_physical_id_entry(vcpu, id);
1726 if (!entry)
1727 return -EINVAL;
1728
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001729 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
1730 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1731 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001732 WRITE_ONCE(*entry, new_entry);
1733
1734 svm->avic_physical_id_cache = entry;
1735
1736 return 0;
1737}
1738
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00001739static void sev_asid_free(int asid)
Brijesh Singh1654efc2017-12-04 10:57:34 -06001740{
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001741 struct svm_cpu_data *sd;
1742 int cpu, pos;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001743
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00001744 mutex_lock(&sev_bitmap_lock);
1745
Brijesh Singh1654efc2017-12-04 10:57:34 -06001746 pos = asid - 1;
Tom Lendacky33af3a72019-10-03 21:17:48 +00001747 __set_bit(pos, sev_reclaim_asid_bitmap);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001748
1749 for_each_possible_cpu(cpu) {
1750 sd = per_cpu(svm_data, cpu);
1751 sd->sev_vmcbs[pos] = NULL;
1752 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001753
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00001754 mutex_unlock(&sev_bitmap_lock);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001755}
1756
Brijesh Singh59414c92017-12-04 10:57:35 -06001757static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
1758{
1759 struct sev_data_decommission *decommission;
1760 struct sev_data_deactivate *data;
1761
1762 if (!handle)
1763 return;
1764
1765 data = kzalloc(sizeof(*data), GFP_KERNEL);
1766 if (!data)
1767 return;
1768
1769 /* deactivate handle */
1770 data->handle = handle;
Tom Lendacky83af5e62019-10-03 21:17:45 +00001771
Tom Lendacky33af3a72019-10-03 21:17:48 +00001772 /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */
1773 down_read(&sev_deactivate_lock);
Brijesh Singh59414c92017-12-04 10:57:35 -06001774 sev_guest_deactivate(data, NULL);
Tom Lendacky33af3a72019-10-03 21:17:48 +00001775 up_read(&sev_deactivate_lock);
Tom Lendacky83af5e62019-10-03 21:17:45 +00001776
Brijesh Singh59414c92017-12-04 10:57:35 -06001777 kfree(data);
1778
1779 decommission = kzalloc(sizeof(*decommission), GFP_KERNEL);
1780 if (!decommission)
1781 return;
1782
1783 /* decommission handle */
1784 decommission->handle = handle;
1785 sev_guest_decommission(decommission, NULL);
1786
1787 kfree(decommission);
1788}
1789
Brijesh Singh89c50582017-12-04 10:57:35 -06001790static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
1791 unsigned long ulen, unsigned long *n,
1792 int write)
1793{
Sean Christopherson81811c12018-03-20 12:17:21 -07001794 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06001795 unsigned long npages, npinned, size;
1796 unsigned long locked, lock_limit;
1797 struct page **pages;
Dan Carpenter86bf20c2018-05-19 09:01:36 +03001798 unsigned long first, last;
1799
1800 if (ulen == 0 || uaddr + ulen < uaddr)
1801 return NULL;
Brijesh Singh89c50582017-12-04 10:57:35 -06001802
1803 /* Calculate number of pages. */
1804 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
1805 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
1806 npages = (last - first + 1);
1807
1808 locked = sev->pages_locked + npages;
1809 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1810 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
1811 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
1812 return NULL;
1813 }
1814
1815 /* Avoid using vmalloc for smaller buffers. */
1816 size = npages * sizeof(struct page *);
1817 if (size > PAGE_SIZE)
Ben Gardon1ec69642019-02-11 11:02:51 -08001818 pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO,
1819 PAGE_KERNEL);
Brijesh Singh89c50582017-12-04 10:57:35 -06001820 else
Ben Gardon1ec69642019-02-11 11:02:51 -08001821 pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
Brijesh Singh89c50582017-12-04 10:57:35 -06001822
1823 if (!pages)
1824 return NULL;
1825
1826 /* Pin the user virtual address. */
Ira Weiny73b01402019-05-13 17:17:11 -07001827 npinned = get_user_pages_fast(uaddr, npages, FOLL_WRITE, pages);
Brijesh Singh89c50582017-12-04 10:57:35 -06001828 if (npinned != npages) {
1829 pr_err("SEV: Failure locking %lu pages.\n", npages);
1830 goto err;
1831 }
1832
1833 *n = npages;
1834 sev->pages_locked = locked;
1835
1836 return pages;
1837
1838err:
1839 if (npinned > 0)
1840 release_pages(pages, npinned);
1841
1842 kvfree(pages);
1843 return NULL;
1844}
1845
1846static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
1847 unsigned long npages)
1848{
Sean Christopherson81811c12018-03-20 12:17:21 -07001849 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06001850
1851 release_pages(pages, npages);
1852 kvfree(pages);
1853 sev->pages_locked -= npages;
1854}
1855
1856static void sev_clflush_pages(struct page *pages[], unsigned long npages)
1857{
1858 uint8_t *page_virtual;
1859 unsigned long i;
1860
1861 if (npages == 0 || pages == NULL)
1862 return;
1863
1864 for (i = 0; i < npages; i++) {
1865 page_virtual = kmap_atomic(pages[i]);
1866 clflush_cache_range(page_virtual, PAGE_SIZE);
1867 kunmap_atomic(page_virtual);
1868 }
1869}
1870
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001871static void __unregister_enc_region_locked(struct kvm *kvm,
1872 struct enc_region *region)
1873{
1874 /*
1875 * The guest may change the memory encryption attribute from C=0 -> C=1
1876 * or vice versa for this memory range. Lets make sure caches are
1877 * flushed to ensure that guest data gets written into memory with
1878 * correct C-bit.
1879 */
1880 sev_clflush_pages(region->pages, region->npages);
1881
1882 sev_unpin_memory(kvm, region->pages, region->npages);
1883 list_del(&region->list);
1884 kfree(region);
1885}
1886
Sean Christopherson434a1e92018-03-20 12:17:18 -07001887static struct kvm *svm_vm_alloc(void)
1888{
Ben Gardon1ec69642019-02-11 11:02:51 -08001889 struct kvm_svm *kvm_svm = __vmalloc(sizeof(struct kvm_svm),
1890 GFP_KERNEL_ACCOUNT | __GFP_ZERO,
1891 PAGE_KERNEL);
Sean Christopherson81811c12018-03-20 12:17:21 -07001892 return &kvm_svm->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07001893}
1894
1895static void svm_vm_free(struct kvm *kvm)
1896{
Marc Orrd1e5b0e2018-05-15 04:37:37 -07001897 vfree(to_kvm_svm(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07001898}
1899
Brijesh Singh1654efc2017-12-04 10:57:34 -06001900static void sev_vm_destroy(struct kvm *kvm)
1901{
Sean Christopherson81811c12018-03-20 12:17:21 -07001902 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001903 struct list_head *head = &sev->regions_list;
1904 struct list_head *pos, *q;
Brijesh Singh59414c92017-12-04 10:57:35 -06001905
Brijesh Singh1654efc2017-12-04 10:57:34 -06001906 if (!sev_guest(kvm))
1907 return;
1908
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001909 mutex_lock(&kvm->lock);
1910
1911 /*
1912 * if userspace was terminated before unregistering the memory regions
1913 * then lets unpin all the registered memory.
1914 */
1915 if (!list_empty(head)) {
1916 list_for_each_safe(pos, q, head) {
1917 __unregister_enc_region_locked(kvm,
1918 list_entry(pos, struct enc_region, list));
1919 }
1920 }
1921
1922 mutex_unlock(&kvm->lock);
1923
Brijesh Singh59414c92017-12-04 10:57:35 -06001924 sev_unbind_asid(kvm, sev->handle);
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00001925 sev_asid_free(sev->asid);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001926}
1927
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001928static void avic_vm_destroy(struct kvm *kvm)
1929{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001930 unsigned long flags;
Sean Christopherson81811c12018-03-20 12:17:21 -07001931 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001932
Dmitry Vyukov3863dff2017-01-24 14:06:48 +01001933 if (!avic)
1934 return;
1935
Sean Christopherson81811c12018-03-20 12:17:21 -07001936 if (kvm_svm->avic_logical_id_table_page)
1937 __free_page(kvm_svm->avic_logical_id_table_page);
1938 if (kvm_svm->avic_physical_id_table_page)
1939 __free_page(kvm_svm->avic_physical_id_table_page);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001940
1941 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Sean Christopherson81811c12018-03-20 12:17:21 -07001942 hash_del(&kvm_svm->hnode);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001943 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001944}
1945
Brijesh Singh1654efc2017-12-04 10:57:34 -06001946static void svm_vm_destroy(struct kvm *kvm)
1947{
1948 avic_vm_destroy(kvm);
1949 sev_vm_destroy(kvm);
1950}
1951
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001952static int avic_vm_init(struct kvm *kvm)
1953{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001954 unsigned long flags;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001955 int err = -ENOMEM;
Sean Christopherson81811c12018-03-20 12:17:21 -07001956 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
1957 struct kvm_svm *k2;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001958 struct page *p_page;
1959 struct page *l_page;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001960 u32 vm_id;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001961
1962 if (!avic)
1963 return 0;
1964
1965 /* Allocating physical APIC ID table (4KB) */
Ben Gardon1ec69642019-02-11 11:02:51 -08001966 p_page = alloc_page(GFP_KERNEL_ACCOUNT);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001967 if (!p_page)
1968 goto free_avic;
1969
Sean Christopherson81811c12018-03-20 12:17:21 -07001970 kvm_svm->avic_physical_id_table_page = p_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001971 clear_page(page_address(p_page));
1972
1973 /* Allocating logical APIC ID table (4KB) */
Ben Gardon1ec69642019-02-11 11:02:51 -08001974 l_page = alloc_page(GFP_KERNEL_ACCOUNT);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001975 if (!l_page)
1976 goto free_avic;
1977
Sean Christopherson81811c12018-03-20 12:17:21 -07001978 kvm_svm->avic_logical_id_table_page = l_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001979 clear_page(page_address(l_page));
1980
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001981 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001982 again:
1983 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
1984 if (vm_id == 0) { /* id is 1-based, zero is not okay */
1985 next_vm_id_wrapped = 1;
1986 goto again;
1987 }
1988 /* Is it still in use? Only possible if wrapped at least once */
1989 if (next_vm_id_wrapped) {
Sean Christopherson81811c12018-03-20 12:17:21 -07001990 hash_for_each_possible(svm_vm_data_hash, k2, hnode, vm_id) {
1991 if (k2->avic_vm_id == vm_id)
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001992 goto again;
1993 }
1994 }
Sean Christopherson81811c12018-03-20 12:17:21 -07001995 kvm_svm->avic_vm_id = vm_id;
1996 hash_add(svm_vm_data_hash, &kvm_svm->hnode, kvm_svm->avic_vm_id);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001997 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1998
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001999 return 0;
2000
2001free_avic:
2002 avic_vm_destroy(kvm);
2003 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004}
2005
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002006static inline int
2007avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002008{
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002009 int ret = 0;
2010 unsigned long flags;
2011 struct amd_svm_iommu_ir *ir;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002012 struct vcpu_svm *svm = to_svm(vcpu);
2013
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002014 if (!kvm_arch_has_assigned_device(vcpu->kvm))
2015 return 0;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002016
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002017 /*
2018 * Here, we go through the per-vcpu ir_list to update all existing
2019 * interrupt remapping table entry targeting this vcpu.
2020 */
2021 spin_lock_irqsave(&svm->ir_list_lock, flags);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002022
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002023 if (list_empty(&svm->ir_list))
2024 goto out;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002025
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002026 list_for_each_entry(ir, &svm->ir_list, node) {
2027 ret = amd_iommu_update_ga(cpu, r, ir->data);
2028 if (ret)
2029 break;
2030 }
2031out:
2032 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
2033 return ret;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002034}
2035
2036static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2037{
2038 u64 entry;
2039 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05002040 int h_physical_id = kvm_cpu_get_apicid(cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002041 struct vcpu_svm *svm = to_svm(vcpu);
2042
2043 if (!kvm_vcpu_apicv_active(vcpu))
2044 return;
2045
Suthikulpanit, Suraveec9bcd3e2019-05-14 15:49:52 +00002046 /*
2047 * Since the host physical APIC id is 8 bits,
2048 * we can support host APIC ID upto 255.
2049 */
2050 if (WARN_ON(h_physical_id > AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK))
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002051 return;
2052
2053 entry = READ_ONCE(*(svm->avic_physical_id_cache));
2054 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
2055
2056 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
2057 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
2058
2059 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2060 if (svm->avic_is_running)
2061 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2062
2063 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002064 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
2065 svm->avic_is_running);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002066}
2067
2068static void avic_vcpu_put(struct kvm_vcpu *vcpu)
2069{
2070 u64 entry;
2071 struct vcpu_svm *svm = to_svm(vcpu);
2072
2073 if (!kvm_vcpu_apicv_active(vcpu))
2074 return;
2075
2076 entry = READ_ONCE(*(svm->avic_physical_id_cache));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002077 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
2078 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
2079
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002080 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2081 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082}
2083
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002084/**
2085 * This function is called during VCPU halt/unhalt.
2086 */
2087static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
2088{
2089 struct vcpu_svm *svm = to_svm(vcpu);
2090
2091 svm->avic_is_running = is_run;
2092 if (is_run)
2093 avic_vcpu_load(vcpu, vcpu->cpu);
2094 else
2095 avic_vcpu_put(vcpu);
2096}
2097
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002098static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03002099{
2100 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01002101 u32 dummy;
2102 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002103
Wanpeng Li518e7b92018-02-28 14:03:31 +08002104 vcpu->arch.microcode_version = 0x01000065;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002105 svm->spec_ctrl = 0;
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02002106 svm->virt_spec_ctrl = 0;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002107
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002108 if (!init_event) {
2109 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
2110 MSR_IA32_APICBASE_ENABLE;
2111 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
2112 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
2113 }
Paolo Bonzini56908912015-10-19 11:30:19 +02002114 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02002115
Yu Zhange911eb32017-08-24 20:27:52 +08002116 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
Sean Christophersonde3cd112019-04-30 10:36:17 -07002117 kvm_rdx_write(vcpu, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002118
2119 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
2120 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03002121}
2122
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002123static int avic_init_vcpu(struct vcpu_svm *svm)
2124{
2125 int ret;
2126
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05002127 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002128 return 0;
2129
2130 ret = avic_init_backing_page(&svm->vcpu);
2131 if (ret)
2132 return ret;
2133
2134 INIT_LIST_HEAD(&svm->ir_list);
2135 spin_lock_init(&svm->ir_list_lock);
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00002136 svm->dfr_reg = APIC_DFR_FLAT;
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002137
2138 return ret;
2139}
2140
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002141static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002143 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01002145 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002146 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002147 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002148 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149
Sean Christopherson12b58f42019-08-15 10:22:37 -07002150 BUILD_BUG_ON_MSG(offsetof(struct vcpu_svm, vcpu) != 0,
2151 "struct kvm_vcpu must be at offset 0 for arch usercopy region");
2152
Ben Gardon1ec69642019-02-11 11:02:51 -08002153 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002154 if (!svm) {
2155 err = -ENOMEM;
2156 goto out;
2157 }
2158
Wanpeng Lid9a710e2019-07-22 12:26:21 +08002159 svm->vcpu.arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
2160 GFP_KERNEL_ACCOUNT);
2161 if (!svm->vcpu.arch.user_fpu) {
2162 printk(KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n");
2163 err = -ENOMEM;
2164 goto free_partial_svm;
2165 }
2166
Ben Gardon1ec69642019-02-11 11:02:51 -08002167 svm->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
2168 GFP_KERNEL_ACCOUNT);
Marc Orrb666a4b2018-11-06 14:53:56 -08002169 if (!svm->vcpu.arch.guest_fpu) {
2170 printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n");
2171 err = -ENOMEM;
Wanpeng Lid9a710e2019-07-22 12:26:21 +08002172 goto free_user_fpu;
Marc Orrb666a4b2018-11-06 14:53:56 -08002173 }
2174
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002175 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
2176 if (err)
2177 goto free_svm;
2178
Joerg Roedelf65c2292008-02-13 18:58:46 +01002179 err = -ENOMEM;
Ben Gardon1ec69642019-02-11 11:02:51 -08002180 page = alloc_page(GFP_KERNEL_ACCOUNT);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002181 if (!page)
2182 goto uninit;
2183
Ben Gardon1ec69642019-02-11 11:02:51 -08002184 msrpm_pages = alloc_pages(GFP_KERNEL_ACCOUNT, MSRPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01002185 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002186 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002187
Ben Gardon1ec69642019-02-11 11:02:51 -08002188 nested_msrpm_pages = alloc_pages(GFP_KERNEL_ACCOUNT, MSRPM_ALLOC_ORDER);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002189 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002190 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01002191
Ben Gardon1ec69642019-02-11 11:02:51 -08002192 hsave_page = alloc_page(GFP_KERNEL_ACCOUNT);
Alexander Grafb286d5d2008-11-25 20:17:05 +01002193 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002194 goto free_page3;
2195
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002196 err = avic_init_vcpu(svm);
2197 if (err)
2198 goto free_page4;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002199
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002200 /* We initialize this flag to true to make sure that the is_running
2201 * bit would be set the first time the vcpu is loaded.
2202 */
2203 svm->avic_is_running = true;
2204
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002205 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01002206
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002207 svm->msrpm = page_address(msrpm_pages);
2208 svm_vcpu_init_msrpm(svm->msrpm);
2209
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002210 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002211 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002212
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002213 svm->vmcb = page_address(page);
2214 clear_page(svm->vmcb);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002215 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002216 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02002217 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002219 svm_init_osvw(&svm->vcpu);
2220
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002221 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08002222
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002223free_page4:
2224 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002225free_page3:
2226 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
2227free_page2:
2228 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
2229free_page1:
2230 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002231uninit:
2232 kvm_vcpu_uninit(&svm->vcpu);
2233free_svm:
Marc Orrb666a4b2018-11-06 14:53:56 -08002234 kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.guest_fpu);
Wanpeng Lid9a710e2019-07-22 12:26:21 +08002235free_user_fpu:
2236 kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.user_fpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08002237free_partial_svm:
Rusty Russella4770342007-08-01 14:46:11 +10002238 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002239out:
2240 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241}
2242
Jim Mattsonfd65d312018-05-22 09:54:20 -07002243static void svm_clear_current_vmcb(struct vmcb *vmcb)
2244{
2245 int i;
2246
2247 for_each_online_cpu(i)
2248 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
2249}
2250
Avi Kivity6aa8b732006-12-10 02:21:36 -08002251static void svm_free_vcpu(struct kvm_vcpu *vcpu)
2252{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002253 struct vcpu_svm *svm = to_svm(vcpu);
2254
Jim Mattsonfd65d312018-05-22 09:54:20 -07002255 /*
2256 * The vmcb page can be recycled, causing a false negative in
2257 * svm_vcpu_load(). So, ensure that no logical CPU has this
2258 * vmcb page recorded as its current vmcb.
2259 */
2260 svm_clear_current_vmcb(svm->vmcb);
2261
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002262 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01002263 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002264 __free_page(virt_to_page(svm->nested.hsave));
2265 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002266 kvm_vcpu_uninit(vcpu);
Wanpeng Lid9a710e2019-07-22 12:26:21 +08002267 kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.user_fpu);
Marc Orrb666a4b2018-11-06 14:53:56 -08002268 kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.guest_fpu);
Rusty Russella4770342007-08-01 14:46:11 +10002269 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270}
2271
Avi Kivity15ad7142007-07-11 18:17:21 +03002272static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002274 struct vcpu_svm *svm = to_svm(vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01002275 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002276 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02002277
Avi Kivity0cc50642007-03-25 12:07:27 +02002278 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002279 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002280 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02002281 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002282
Avi Kivity82ca2d12010-10-21 12:20:34 +02002283#ifdef CONFIG_X86_64
2284 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
2285#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02002286 savesegment(fs, svm->host.fs);
2287 savesegment(gs, svm->host.gs);
2288 svm->host.ldt = kvm_read_ldt();
2289
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002290 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002291 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002292
Haozhong Zhangad7218832015-10-20 15:39:02 +08002293 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
2294 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2295 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
2296 __this_cpu_write(current_tsc_ratio, tsc_ratio);
2297 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
2298 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002299 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01002300 /* This assumes that the kernel never uses MSR_TSC_AUX */
2301 if (static_cpu_has(X86_FEATURE_RDTSCP))
2302 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002303
Ashok Raj15d45072018-02-01 22:59:43 +01002304 if (sd->current_vmcb != svm->vmcb) {
2305 sd->current_vmcb = svm->vmcb;
2306 indirect_branch_prediction_barrier();
2307 }
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002308 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309}
2310
2311static void svm_vcpu_put(struct kvm_vcpu *vcpu)
2312{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002313 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002314 int i;
2315
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002316 avic_vcpu_put(vcpu);
2317
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002318 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02002319 kvm_load_ldt(svm->host.ldt);
2320#ifdef CONFIG_X86_64
2321 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07002322 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01002323 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02002324#else
Avi Kivity831ca602011-03-08 16:09:51 +02002325#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02002326 loadsegment(gs, svm->host.gs);
2327#endif
Avi Kivity831ca602011-03-08 16:09:51 +02002328#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002329 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002330 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331}
2332
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002333static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
2334{
2335 avic_set_running(vcpu, false);
2336}
2337
2338static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
2339{
2340 avic_set_running(vcpu, true);
2341}
2342
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
2344{
Ladi Prosek9b611742017-06-21 09:06:59 +02002345 struct vcpu_svm *svm = to_svm(vcpu);
2346 unsigned long rflags = svm->vmcb->save.rflags;
2347
2348 if (svm->nmi_singlestep) {
2349 /* Hide our flags if they were not set by the guest */
2350 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
2351 rflags &= ~X86_EFLAGS_TF;
2352 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
2353 rflags &= ~X86_EFLAGS_RF;
2354 }
2355 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356}
2357
2358static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2359{
Ladi Prosek9b611742017-06-21 09:06:59 +02002360 if (to_svm(vcpu)->nmi_singlestep)
2361 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2362
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002363 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02002364 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002365 * (caused by either a task switch or an inter-privilege IRET),
2366 * so we do not need to update the CPL here.
2367 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002368 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369}
2370
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002371static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
2372{
2373 switch (reg) {
2374 case VCPU_EXREG_PDPTR:
2375 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002376 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002377 break;
2378 default:
Sean Christopherson34059c22019-09-27 14:45:23 -07002379 WARN_ON_ONCE(1);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002380 }
2381}
2382
Alexander Graff0b85052008-11-25 20:17:01 +01002383static void svm_set_vintr(struct vcpu_svm *svm)
2384{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002385 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002386}
2387
2388static void svm_clear_vintr(struct vcpu_svm *svm)
2389{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002390 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002391}
2392
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
2394{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002395 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396
2397 switch (seg) {
2398 case VCPU_SREG_CS: return &save->cs;
2399 case VCPU_SREG_DS: return &save->ds;
2400 case VCPU_SREG_ES: return &save->es;
2401 case VCPU_SREG_FS: return &save->fs;
2402 case VCPU_SREG_GS: return &save->gs;
2403 case VCPU_SREG_SS: return &save->ss;
2404 case VCPU_SREG_TR: return &save->tr;
2405 case VCPU_SREG_LDTR: return &save->ldtr;
2406 }
2407 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00002408 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002409}
2410
2411static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2412{
2413 struct vmcb_seg *s = svm_seg(vcpu, seg);
2414
2415 return s->base;
2416}
2417
2418static void svm_get_segment(struct kvm_vcpu *vcpu,
2419 struct kvm_segment *var, int seg)
2420{
2421 struct vmcb_seg *s = svm_seg(vcpu, seg);
2422
2423 var->base = s->base;
2424 var->limit = s->limit;
2425 var->selector = s->selector;
2426 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
2427 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
2428 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
2429 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
2430 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
2431 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
2432 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05302433
2434 /*
2435 * AMD CPUs circa 2014 track the G bit for all segments except CS.
2436 * However, the SVM spec states that the G bit is not observed by the
2437 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
2438 * So let's synthesize a legal G bit for all segments, this helps
2439 * running KVM nested. It also helps cross-vendor migration, because
2440 * Intel's vmentry has a check on the 'G' bit.
2441 */
2442 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00002443
Joerg Roedele0231712010-02-24 18:59:10 +01002444 /*
2445 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02002446 * for cross vendor migration purposes by "not present"
2447 */
Gioh Kim8eae9572017-05-30 15:24:45 +02002448 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02002449
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002450 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002451 case VCPU_SREG_TR:
2452 /*
2453 * Work around a bug where the busy flag in the tr selector
2454 * isn't exposed
2455 */
Amit Shahc0d09822008-10-27 09:04:18 +00002456 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002457 break;
2458 case VCPU_SREG_DS:
2459 case VCPU_SREG_ES:
2460 case VCPU_SREG_FS:
2461 case VCPU_SREG_GS:
2462 /*
2463 * The accessed bit must always be set in the segment
2464 * descriptor cache, although it can be cleared in the
2465 * descriptor, the cached bit always remains at 1. Since
2466 * Intel has a check on this, set it here to support
2467 * cross-vendor migration.
2468 */
2469 if (!var->unusable)
2470 var->type |= 0x1;
2471 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02002472 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01002473 /*
2474 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02002475 * descriptor is left as 1, although the whole segment has
2476 * been made unusable. Clear it here to pass an Intel VMX
2477 * entry check when cross vendor migrating.
2478 */
2479 if (var->unusable)
2480 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02002481 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02002482 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02002483 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002484 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485}
2486
Izik Eidus2e4d2652008-03-24 19:38:34 +02002487static int svm_get_cpl(struct kvm_vcpu *vcpu)
2488{
2489 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
2490
2491 return save->cpl;
2492}
2493
Gleb Natapov89a27f42010-02-16 10:51:48 +02002494static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002495{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002496 struct vcpu_svm *svm = to_svm(vcpu);
2497
Gleb Natapov89a27f42010-02-16 10:51:48 +02002498 dt->size = svm->vmcb->save.idtr.limit;
2499 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500}
2501
Gleb Natapov89a27f42010-02-16 10:51:48 +02002502static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002503{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002504 struct vcpu_svm *svm = to_svm(vcpu);
2505
Gleb Natapov89a27f42010-02-16 10:51:48 +02002506 svm->vmcb->save.idtr.limit = dt->size;
2507 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002508 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509}
2510
Gleb Natapov89a27f42010-02-16 10:51:48 +02002511static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002513 struct vcpu_svm *svm = to_svm(vcpu);
2514
Gleb Natapov89a27f42010-02-16 10:51:48 +02002515 dt->size = svm->vmcb->save.gdtr.limit;
2516 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517}
2518
Gleb Natapov89a27f42010-02-16 10:51:48 +02002519static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002521 struct vcpu_svm *svm = to_svm(vcpu);
2522
Gleb Natapov89a27f42010-02-16 10:51:48 +02002523 svm->vmcb->save.gdtr.limit = dt->size;
2524 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002525 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526}
2527
Avi Kivitye8467fd2009-12-29 18:43:06 +02002528static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2529{
2530}
2531
Anthony Liguori25c4c272007-04-27 09:29:21 +03002532static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002533{
2534}
2535
Avi Kivityd2251572010-01-06 10:55:27 +02002536static void update_cr0_intercept(struct vcpu_svm *svm)
2537{
2538 ulong gcr0 = svm->vcpu.arch.cr0;
2539 u64 *hcr0 = &svm->vmcb->save.cr0;
2540
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002541 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
2542 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
Avi Kivityd2251572010-01-06 10:55:27 +02002543
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002544 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002545
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002546 if (gcr0 == *hcr0) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002547 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
2548 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002549 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002550 set_cr_intercept(svm, INTERCEPT_CR0_READ);
2551 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002552 }
2553}
2554
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2556{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002557 struct vcpu_svm *svm = to_svm(vcpu);
2558
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002559#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002560 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002561 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002562 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002563 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564 }
2565
Mike Dayd77c26f2007-10-08 09:02:08 -04002566 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002567 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002568 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569 }
2570 }
2571#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002572 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002573
2574 if (!npt_enabled)
2575 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02002576
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02002577 /*
2578 * re-enable caching here because the QEMU bios
2579 * does not do it - this results in some delay at
2580 * reboot
2581 */
2582 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
2583 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002584 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002585 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002586 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587}
2588
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002589static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002591 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002592 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
2593
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002594 if (cr4 & X86_CR4_VMXE)
2595 return 1;
2596
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002597 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002598 svm_flush_tlb(vcpu, true);
Joerg Roedel6394b642008-04-09 14:15:29 +02002599
Joerg Roedelec077262008-04-09 14:15:28 +02002600 vcpu->arch.cr4 = cr4;
2601 if (!npt_enabled)
2602 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02002603 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02002604 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002605 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002606 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607}
2608
2609static void svm_set_segment(struct kvm_vcpu *vcpu,
2610 struct kvm_segment *var, int seg)
2611{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002612 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613 struct vmcb_seg *s = svm_seg(vcpu, seg);
2614
2615 s->base = var->base;
2616 s->limit = var->limit;
2617 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02002618 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2619 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2620 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2621 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2622 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2623 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2624 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2625 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002626
2627 /*
2628 * This is always accurate, except if SYSRET returned to a segment
2629 * with SS.DPL != 3. Intel does not have this quirk, and always
2630 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2631 * would entail passing the CPL to userspace and back.
2632 */
2633 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02002634 /* This is symmetric with svm_get_segment() */
2635 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636
Joerg Roedel060d0c92010-12-03 11:45:57 +01002637 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638}
2639
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01002640static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002642 struct vcpu_svm *svm = to_svm(vcpu);
2643
Joerg Roedel18c918c2010-11-30 18:03:59 +01002644 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03002645
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002646 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002647 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01002648 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002649 } else
2650 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03002651}
2652
Tejun Heo0fe1e002009-10-29 22:34:14 +09002653static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654{
Tejun Heo0fe1e002009-10-29 22:34:14 +09002655 if (sd->next_asid > sd->max_asid) {
2656 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06002657 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002658 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 }
2660
Tejun Heo0fe1e002009-10-29 22:34:14 +09002661 svm->asid_generation = sd->asid_generation;
2662 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01002663
2664 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665}
2666
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002667static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2668{
2669 return to_svm(vcpu)->vmcb->save.dr6;
2670}
2671
2672static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2673{
2674 struct vcpu_svm *svm = to_svm(vcpu);
2675
2676 svm->vmcb->save.dr6 = value;
2677 mark_dirty(svm->vmcb, VMCB_DR);
2678}
2679
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002680static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2681{
2682 struct vcpu_svm *svm = to_svm(vcpu);
2683
2684 get_debugreg(vcpu->arch.db[0], 0);
2685 get_debugreg(vcpu->arch.db[1], 1);
2686 get_debugreg(vcpu->arch.db[2], 2);
2687 get_debugreg(vcpu->arch.db[3], 3);
2688 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2689 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2690
2691 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2692 set_dr_intercepts(svm);
2693}
2694
Gleb Natapov020df072010-04-13 10:05:23 +03002695static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002696{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002697 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002698
Gleb Natapov020df072010-04-13 10:05:23 +03002699 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01002700 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701}
2702
Avi Kivity851ba692009-08-24 11:10:17 +03002703static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002705 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002706 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002708 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002709 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2710 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002711 svm->vmcb->control.insn_len);
2712}
2713
2714static int npf_interception(struct vcpu_svm *svm)
2715{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002716 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Paolo Bonzinid0006532017-08-11 18:36:43 +02002717 u64 error_code = svm->vmcb->control.exit_info_1;
2718
2719 trace_kvm_page_fault(fault_address, error_code);
2720 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002721 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2722 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002723 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724}
2725
Avi Kivity851ba692009-08-24 11:10:17 +03002726static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002727{
Avi Kivity851ba692009-08-24 11:10:17 +03002728 struct kvm_run *kvm_run = svm->vcpu.run;
Vitaly Kuznetsov99c22172019-04-03 16:06:42 +02002729 struct kvm_vcpu *vcpu = &svm->vcpu;
Avi Kivity851ba692009-08-24 11:10:17 +03002730
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002731 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03002732 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02002733 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002734 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2735 return 1;
2736 }
Gleb Natapov44c11432009-05-11 13:35:52 +03002737
Jan Kiszka6be7d302009-10-18 13:24:54 +02002738 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02002739 disable_nmi_singlestep(svm);
Vitaly Kuznetsov99c22172019-04-03 16:06:42 +02002740 /* Make sure we check for pending NMIs upon entry */
2741 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov44c11432009-05-11 13:35:52 +03002742 }
2743
2744 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002745 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002746 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2747 kvm_run->debug.arch.pc =
2748 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2749 kvm_run->debug.arch.exception = DB_VECTOR;
2750 return 0;
2751 }
2752
2753 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002754}
2755
Avi Kivity851ba692009-08-24 11:10:17 +03002756static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002757{
Avi Kivity851ba692009-08-24 11:10:17 +03002758 struct kvm_run *kvm_run = svm->vcpu.run;
2759
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002760 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2761 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2762 kvm_run->debug.arch.exception = BP_VECTOR;
2763 return 0;
2764}
2765
Avi Kivity851ba692009-08-24 11:10:17 +03002766static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002767{
Wanpeng Li082d06e2018-04-03 16:28:48 -07002768 return handle_ud(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002769}
2770
Eric Northup54a20552015-11-03 18:03:53 +01002771static int ac_interception(struct vcpu_svm *svm)
2772{
2773 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2774 return 1;
2775}
2776
Liran Alon97184202018-03-12 13:12:52 +02002777static int gp_interception(struct vcpu_svm *svm)
2778{
2779 struct kvm_vcpu *vcpu = &svm->vcpu;
2780 u32 error_code = svm->vmcb->control.exit_info_1;
Liran Alon97184202018-03-12 13:12:52 +02002781
2782 WARN_ON_ONCE(!enable_vmware_backdoor);
2783
Sean Christophersona6c6ed12019-08-27 14:40:30 -07002784 /*
2785 * VMware backdoor emulation on #GP interception only handles IN{S},
2786 * OUT{S}, and RDPMC, none of which generate a non-zero error code.
2787 */
2788 if (error_code) {
2789 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2790 return 1;
2791 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002792 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
Liran Alon97184202018-03-12 13:12:52 +02002793}
2794
Joerg Roedel67ec6602010-05-17 14:43:35 +02002795static bool is_erratum_383(void)
2796{
2797 int err, i;
2798 u64 value;
2799
2800 if (!erratum_383_found)
2801 return false;
2802
2803 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2804 if (err)
2805 return false;
2806
2807 /* Bit 62 may or may not be set for this mce */
2808 value &= ~(1ULL << 62);
2809
2810 if (value != 0xb600000000010015ULL)
2811 return false;
2812
2813 /* Clear MCi_STATUS registers */
2814 for (i = 0; i < 6; ++i)
2815 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2816
2817 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2818 if (!err) {
2819 u32 low, high;
2820
2821 value &= ~(1ULL << 2);
2822 low = lower_32_bits(value);
2823 high = upper_32_bits(value);
2824
2825 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2826 }
2827
2828 /* Flush tlb to evict multi-match entries */
2829 __flush_tlb_all();
2830
2831 return true;
2832}
2833
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002834static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002835{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002836 if (is_erratum_383()) {
2837 /*
2838 * Erratum 383 triggered. Guest state is corrupt so kill the
2839 * guest.
2840 */
2841 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2842
Avi Kivitya8eeb042010-05-10 12:34:53 +03002843 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002844
2845 return;
2846 }
2847
Joerg Roedel53371b52008-04-09 14:15:30 +02002848 /*
2849 * On an #MC intercept the MCE handler is not called automatically in
2850 * the host. So do it by hand here.
2851 */
2852 asm volatile (
2853 "int $0x12\n");
2854 /* not sure if we ever come back to this point */
2855
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002856 return;
2857}
2858
2859static int mc_interception(struct vcpu_svm *svm)
2860{
Joerg Roedel53371b52008-04-09 14:15:30 +02002861 return 1;
2862}
2863
Avi Kivity851ba692009-08-24 11:10:17 +03002864static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002865{
Avi Kivity851ba692009-08-24 11:10:17 +03002866 struct kvm_run *kvm_run = svm->vcpu.run;
2867
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002868 /*
2869 * VMCB is undefined after a SHUTDOWN intercept
2870 * so reinitialize it.
2871 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002872 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002873 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002874
2875 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2876 return 0;
2877}
2878
Avi Kivity851ba692009-08-24 11:10:17 +03002879static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002881 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002882 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Sean Christophersondca7f122018-03-08 08:57:27 -08002883 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002884 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885
Rusty Russelle756fc62007-07-30 20:07:08 +10002886 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002887 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002888 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002889 if (string)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002890 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002891
Avi Kivity039576c2007-03-20 12:46:50 +02002892 port = io_info >> 16;
2893 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002894 svm->next_rip = svm->vmcb->control.exit_info_2;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002895
Sean Christophersondca7f122018-03-08 08:57:27 -08002896 return kvm_fast_pio(&svm->vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897}
2898
Avi Kivity851ba692009-08-24 11:10:17 +03002899static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002900{
2901 return 1;
2902}
2903
Avi Kivity851ba692009-08-24 11:10:17 +03002904static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002905{
2906 ++svm->vcpu.stat.irq_exits;
2907 return 1;
2908}
2909
Avi Kivity851ba692009-08-24 11:10:17 +03002910static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002911{
2912 return 1;
2913}
2914
Avi Kivity851ba692009-08-24 11:10:17 +03002915static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916{
Rusty Russelle756fc62007-07-30 20:07:08 +10002917 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918}
2919
Avi Kivity851ba692009-08-24 11:10:17 +03002920static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002921{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002922 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002923}
2924
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002925static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2926{
2927 struct vcpu_svm *svm = to_svm(vcpu);
2928
2929 return svm->nested.nested_cr3;
2930}
2931
Avi Kivitye4e517b2011-07-28 11:36:17 +03002932static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2933{
2934 struct vcpu_svm *svm = to_svm(vcpu);
2935 u64 cr3 = svm->nested.nested_cr3;
2936 u64 pdpte;
2937 int ret;
2938
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002939 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002940 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002941 if (ret)
2942 return 0;
2943 return pdpte;
2944}
2945
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002946static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2947 unsigned long root)
2948{
2949 struct vcpu_svm *svm = to_svm(vcpu);
2950
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002951 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01002952 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002953}
2954
Avi Kivity6389ee92010-11-29 16:12:30 +02002955static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2956 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002957{
2958 struct vcpu_svm *svm = to_svm(vcpu);
2959
Paolo Bonzini5e352512014-09-02 13:18:37 +02002960 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2961 /*
2962 * TODO: track the cause of the nested page fault, and
2963 * correctly fill in the high bits of exit_info_1.
2964 */
2965 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2966 svm->vmcb->control.exit_code_hi = 0;
2967 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2968 svm->vmcb->control.exit_info_2 = fault->address;
2969 }
2970
2971 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2972 svm->vmcb->control.exit_info_1 |= fault->error_code;
2973
2974 /*
2975 * The present bit is always zero for page structure faults on real
2976 * hardware.
2977 */
2978 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2979 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002980
2981 nested_svm_vmexit(svm);
2982}
2983
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002984static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002985{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002986 WARN_ON(mmu_is_nested(vcpu));
Vitaly Kuznetsov3cf85f92018-12-19 17:25:14 +01002987
2988 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Paolo Bonziniad896af2013-10-02 16:56:14 +02002989 kvm_init_shadow_mmu(vcpu);
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02002990 vcpu->arch.mmu->set_cr3 = nested_svm_set_tdp_cr3;
2991 vcpu->arch.mmu->get_cr3 = nested_svm_get_tdp_cr3;
2992 vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr;
2993 vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit;
2994 vcpu->arch.mmu->shadow_root_level = get_npt_level(vcpu);
2995 reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002996 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002997}
2998
2999static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
3000{
Vitaly Kuznetsov3cf85f92018-12-19 17:25:14 +01003001 vcpu->arch.mmu = &vcpu->arch.root_mmu;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02003002 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02003003}
3004
Alexander Grafc0725422008-11-25 20:17:03 +01003005static int nested_svm_check_permissions(struct vcpu_svm *svm)
3006{
Dan Carpentere9196ce2017-05-18 10:39:53 +03003007 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
3008 !is_paging(&svm->vcpu)) {
Alexander Grafc0725422008-11-25 20:17:03 +01003009 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3010 return 1;
3011 }
3012
3013 if (svm->vmcb->save.cpl) {
3014 kvm_inject_gp(&svm->vcpu, 0);
3015 return 1;
3016 }
3017
Dan Carpentere9196ce2017-05-18 10:39:53 +03003018 return 0;
Alexander Grafc0725422008-11-25 20:17:03 +01003019}
3020
Alexander Grafcf74a782008-11-25 20:17:08 +01003021static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
3022 bool has_error_code, u32 error_code)
3023{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003024 int vmexit;
3025
Joerg Roedel20307532010-11-29 17:51:48 +01003026 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02003027 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01003028
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003029 vmexit = nested_svm_intercept(svm);
3030 if (vmexit != NESTED_EXIT_DONE)
3031 return 0;
3032
Joerg Roedel0295ad72009-08-07 11:49:37 +02003033 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
3034 svm->vmcb->control.exit_code_hi = 0;
3035 svm->vmcb->control.exit_info_1 = error_code;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003036
3037 /*
Jim Mattsonda998b42018-10-16 14:29:22 -07003038 * EXITINFO2 is undefined for all exception intercepts other
3039 * than #PF.
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003040 */
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003041 if (svm->vcpu.arch.exception.nested_apf)
3042 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
Jim Mattsonda998b42018-10-16 14:29:22 -07003043 else if (svm->vcpu.arch.exception.has_payload)
3044 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003045 else
3046 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
Joerg Roedel0295ad72009-08-07 11:49:37 +02003047
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003048 svm->nested.exit_required = true;
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003049 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01003050}
3051
Joerg Roedel8fe54652010-02-19 16:23:01 +01003052/* This function returns true if it is save to enable the irq window */
3053static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003054{
Joerg Roedel20307532010-11-29 17:51:48 +01003055 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01003056 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01003057
Joerg Roedel26666952009-08-07 11:49:46 +02003058 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01003059 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01003060
Joerg Roedel26666952009-08-07 11:49:46 +02003061 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01003062 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01003063
Gleb Natapova0a07cd2010-09-20 10:15:32 +02003064 /*
3065 * if vmexit was already requested (by intercepted exception
3066 * for instance) do not overwrite it with "external interrupt"
3067 * vmexit.
3068 */
3069 if (svm->nested.exit_required)
3070 return false;
3071
Joerg Roedel197717d2010-02-24 18:59:19 +01003072 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
3073 svm->vmcb->control.exit_info_1 = 0;
3074 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02003075
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003076 if (svm->nested.intercept & 1ULL) {
3077 /*
3078 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003079 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003080 * #vmexit emulation might sleep. Only signal request for
3081 * the #vmexit here.
3082 */
3083 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02003084 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01003085 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01003086 }
3087
Joerg Roedel8fe54652010-02-19 16:23:01 +01003088 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01003089}
3090
Joerg Roedel887f5002010-02-24 18:59:12 +01003091/* This function returns true if it is save to enable the nmi window */
3092static inline bool nested_svm_nmi(struct vcpu_svm *svm)
3093{
Joerg Roedel20307532010-11-29 17:51:48 +01003094 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01003095 return true;
3096
3097 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
3098 return true;
3099
3100 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
3101 svm->nested.exit_required = true;
3102
3103 return false;
3104}
3105
Joerg Roedelce2ac082010-03-01 15:34:39 +01003106static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003107{
Jan Kiszka9bf41832014-06-30 10:54:17 +02003108 unsigned port, size, iopm_len;
3109 u16 val, mask;
3110 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003111 u64 gpa;
3112
3113 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
3114 return NESTED_EXIT_HOST;
3115
3116 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02003117 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
3118 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003119 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02003120 start_bit = port % 8;
3121 iopm_len = (start_bit + size > 8) ? 2 : 1;
3122 mask = (0xf >> (4 - size)) << start_bit;
3123 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003124
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003125 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02003126 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003127
Jan Kiszka9bf41832014-06-30 10:54:17 +02003128 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003129}
3130
Joerg Roedeld2477822010-03-01 15:34:34 +01003131static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003132{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003133 u32 offset, msr, value;
3134 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003135
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003136 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01003137 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003138
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003139 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
3140 offset = svm_msrpm_offset(msr);
3141 write = svm->vmcb->control.exit_info_1 & 1;
3142 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003143
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003144 if (offset == MSR_INVALID)
3145 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003146
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003147 /* Offset is in 32 bit units but need in 8 bit units */
3148 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003149
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003150 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003151 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003152
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003153 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003154}
3155
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003156/* DB exceptions for our internal use must not cause vmexit */
3157static int nested_svm_intercept_db(struct vcpu_svm *svm)
3158{
3159 unsigned long dr6;
3160
3161 /* if we're not singlestepping, it's not ours */
3162 if (!svm->nmi_singlestep)
3163 return NESTED_EXIT_DONE;
3164
3165 /* if it's not a singlestep exception, it's not ours */
3166 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
3167 return NESTED_EXIT_DONE;
3168 if (!(dr6 & DR6_BS))
3169 return NESTED_EXIT_DONE;
3170
3171 /* if the guest is singlestepping, it should get the vmexit */
3172 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
3173 disable_nmi_singlestep(svm);
3174 return NESTED_EXIT_DONE;
3175 }
3176
3177 /* it's ours, the nested hypervisor must not see this one */
3178 return NESTED_EXIT_HOST;
3179}
3180
Joerg Roedel410e4d52009-08-07 11:49:44 +02003181static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003182{
Alexander Grafcf74a782008-11-25 20:17:08 +01003183 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003184
Joerg Roedel410e4d52009-08-07 11:49:44 +02003185 switch (exit_code) {
3186 case SVM_EXIT_INTR:
3187 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02003188 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02003189 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02003190 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01003191 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02003192 if (npt_enabled)
3193 return NESTED_EXIT_HOST;
3194 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02003195 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02003196 /* When we're shadowing, trap PFs, but not async PF */
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003197 if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003198 return NESTED_EXIT_HOST;
3199 break;
3200 default:
3201 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01003202 }
3203
Joerg Roedel410e4d52009-08-07 11:49:44 +02003204 return NESTED_EXIT_CONTINUE;
3205}
3206
3207/*
3208 * If this function returns true, this #vmexit was already handled
3209 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003210static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003211{
3212 u32 exit_code = svm->vmcb->control.exit_code;
3213 int vmexit = NESTED_EXIT_HOST;
3214
Alexander Grafcf74a782008-11-25 20:17:08 +01003215 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003216 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003217 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003218 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003219 case SVM_EXIT_IOIO:
3220 vmexit = nested_svm_intercept_ioio(svm);
3221 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003222 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
3223 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
3224 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003225 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003226 break;
3227 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01003228 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
3229 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
3230 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003231 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003232 break;
3233 }
3234 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
3235 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003236 if (svm->nested.intercept_exceptions & excp_bits) {
3237 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
3238 vmexit = nested_svm_intercept_db(svm);
3239 else
3240 vmexit = NESTED_EXIT_DONE;
3241 }
Gleb Natapov631bc482010-10-14 11:22:52 +02003242 /* async page fault always cause vmexit */
3243 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003244 svm->vcpu.arch.exception.nested_apf != 0)
Gleb Natapov631bc482010-10-14 11:22:52 +02003245 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003246 break;
3247 }
Joerg Roedel228070b2010-04-22 12:33:10 +02003248 case SVM_EXIT_ERR: {
3249 vmexit = NESTED_EXIT_DONE;
3250 break;
3251 }
Alexander Grafcf74a782008-11-25 20:17:08 +01003252 default: {
3253 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02003254 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003255 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003256 }
3257 }
3258
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003259 return vmexit;
3260}
3261
3262static int nested_svm_exit_handled(struct vcpu_svm *svm)
3263{
3264 int vmexit;
3265
3266 vmexit = nested_svm_intercept(svm);
3267
3268 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003269 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003270
3271 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01003272}
3273
Joerg Roedel0460a972009-08-07 11:49:31 +02003274static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
3275{
3276 struct vmcb_control_area *dst = &dst_vmcb->control;
3277 struct vmcb_control_area *from = &from_vmcb->control;
3278
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003279 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003280 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02003281 dst->intercept_exceptions = from->intercept_exceptions;
3282 dst->intercept = from->intercept;
3283 dst->iopm_base_pa = from->iopm_base_pa;
3284 dst->msrpm_base_pa = from->msrpm_base_pa;
3285 dst->tsc_offset = from->tsc_offset;
3286 dst->asid = from->asid;
3287 dst->tlb_ctl = from->tlb_ctl;
3288 dst->int_ctl = from->int_ctl;
3289 dst->int_vector = from->int_vector;
3290 dst->int_state = from->int_state;
3291 dst->exit_code = from->exit_code;
3292 dst->exit_code_hi = from->exit_code_hi;
3293 dst->exit_info_1 = from->exit_info_1;
3294 dst->exit_info_2 = from->exit_info_2;
3295 dst->exit_int_info = from->exit_int_info;
3296 dst->exit_int_info_err = from->exit_int_info_err;
3297 dst->nested_ctl = from->nested_ctl;
3298 dst->event_inj = from->event_inj;
3299 dst->event_inj_err = from->event_inj_err;
3300 dst->nested_cr3 = from->nested_cr3;
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003301 dst->virt_ext = from->virt_ext;
Tambe, Williame0813542018-11-13 16:51:20 +00003302 dst->pause_filter_count = from->pause_filter_count;
3303 dst->pause_filter_thresh = from->pause_filter_thresh;
Joerg Roedel0460a972009-08-07 11:49:31 +02003304}
3305
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003306static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003307{
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003308 int rc;
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003309 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003310 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02003311 struct vmcb *vmcb = svm->vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003312 struct kvm_host_map map;
Alexander Grafcf74a782008-11-25 20:17:08 +01003313
Joerg Roedel17897f32009-10-09 16:08:29 +02003314 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
3315 vmcb->control.exit_info_1,
3316 vmcb->control.exit_info_2,
3317 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003318 vmcb->control.exit_int_info_err,
3319 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02003320
Vitaly Kuznetsov8f383022019-06-04 18:09:39 +02003321 rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb), &map);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003322 if (rc) {
3323 if (rc == -EINVAL)
3324 kvm_inject_gp(&svm->vcpu, 0);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003325 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003326 }
3327
3328 nested_vmcb = map.hva;
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003329
Joerg Roedel20307532010-11-29 17:51:48 +01003330 /* Exit Guest-Mode */
3331 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01003332 svm->nested.vmcb = 0;
3333
Alexander Grafcf74a782008-11-25 20:17:08 +01003334 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02003335 disable_gif(svm);
3336
3337 nested_vmcb->save.es = vmcb->save.es;
3338 nested_vmcb->save.cs = vmcb->save.cs;
3339 nested_vmcb->save.ss = vmcb->save.ss;
3340 nested_vmcb->save.ds = vmcb->save.ds;
3341 nested_vmcb->save.gdtr = vmcb->save.gdtr;
3342 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02003343 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003344 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003345 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003346 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003347 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03003348 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003349 nested_vmcb->save.rip = vmcb->save.rip;
3350 nested_vmcb->save.rsp = vmcb->save.rsp;
3351 nested_vmcb->save.rax = vmcb->save.rax;
3352 nested_vmcb->save.dr7 = vmcb->save.dr7;
3353 nested_vmcb->save.dr6 = vmcb->save.dr6;
3354 nested_vmcb->save.cpl = vmcb->save.cpl;
3355
3356 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
3357 nested_vmcb->control.int_vector = vmcb->control.int_vector;
3358 nested_vmcb->control.int_state = vmcb->control.int_state;
3359 nested_vmcb->control.exit_code = vmcb->control.exit_code;
3360 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
3361 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
3362 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
3363 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
3364 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003365
3366 if (svm->nrips_enabled)
3367 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02003368
3369 /*
3370 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
3371 * to make sure that we do not lose injected events. So check event_inj
3372 * here and copy it to exit_int_info if it is valid.
3373 * Exit_int_info and event_inj can't be both valid because the case
3374 * below only happens on a VMRUN instruction intercept which has
3375 * no valid exit_int_info set.
3376 */
3377 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
3378 struct vmcb_control_area *nc = &nested_vmcb->control;
3379
3380 nc->exit_int_info = vmcb->control.event_inj;
3381 nc->exit_int_info_err = vmcb->control.event_inj_err;
3382 }
3383
Joerg Roedel33740e42009-08-07 11:49:29 +02003384 nested_vmcb->control.tlb_ctl = 0;
3385 nested_vmcb->control.event_inj = 0;
3386 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01003387
Tambe, Williame0813542018-11-13 16:51:20 +00003388 nested_vmcb->control.pause_filter_count =
3389 svm->vmcb->control.pause_filter_count;
3390 nested_vmcb->control.pause_filter_thresh =
3391 svm->vmcb->control.pause_filter_thresh;
3392
Alexander Grafcf74a782008-11-25 20:17:08 +01003393 /* We always set V_INTR_MASKING and remember the old value in hflags */
3394 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
3395 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
3396
Alexander Grafcf74a782008-11-25 20:17:08 +01003397 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02003398 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01003399
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003400 svm->vcpu.arch.tsc_offset = svm->vmcb->control.tsc_offset;
Alexander Graf219b65d2009-06-15 15:21:25 +02003401 kvm_clear_exception_queue(&svm->vcpu);
3402 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003403
Joerg Roedel4b161842010-09-10 17:31:03 +02003404 svm->nested.nested_cr3 = 0;
3405
Alexander Grafcf74a782008-11-25 20:17:08 +01003406 /* Restore selected save entries */
3407 svm->vmcb->save.es = hsave->save.es;
3408 svm->vmcb->save.cs = hsave->save.cs;
3409 svm->vmcb->save.ss = hsave->save.ss;
3410 svm->vmcb->save.ds = hsave->save.ds;
3411 svm->vmcb->save.gdtr = hsave->save.gdtr;
3412 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003413 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01003414 svm_set_efer(&svm->vcpu, hsave->save.efer);
3415 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
3416 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
3417 if (npt_enabled) {
3418 svm->vmcb->save.cr3 = hsave->save.cr3;
3419 svm->vcpu.arch.cr3 = hsave->save.cr3;
3420 } else {
Avi Kivity23902182010-06-10 17:02:16 +03003421 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01003422 }
Sean Christophersonde3cd112019-04-30 10:36:17 -07003423 kvm_rax_write(&svm->vcpu, hsave->save.rax);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02003424 kvm_rsp_write(&svm->vcpu, hsave->save.rsp);
3425 kvm_rip_write(&svm->vcpu, hsave->save.rip);
Alexander Grafcf74a782008-11-25 20:17:08 +01003426 svm->vmcb->save.dr7 = 0;
3427 svm->vmcb->save.cpl = 0;
3428 svm->vmcb->control.exit_int_info = 0;
3429
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003430 mark_all_dirty(svm->vmcb);
3431
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003432 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Alexander Grafcf74a782008-11-25 20:17:08 +01003433
Joerg Roedel4b161842010-09-10 17:31:03 +02003434 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003435 kvm_mmu_reset_context(&svm->vcpu);
3436 kvm_mmu_load(&svm->vcpu);
3437
Vitaly Kuznetsov619ad842019-01-07 19:44:51 +01003438 /*
3439 * Drop what we picked up for L2 via svm_complete_interrupts() so it
3440 * doesn't end up in L1.
3441 */
3442 svm->vcpu.arch.nmi_injected = false;
3443 kvm_clear_exception_queue(&svm->vcpu);
3444 kvm_clear_interrupt_queue(&svm->vcpu);
3445
Alexander Grafcf74a782008-11-25 20:17:08 +01003446 return 0;
3447}
Alexander Graf3d6368e2008-11-25 20:17:07 +01003448
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003449static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003450{
Joerg Roedel323c3d82010-03-01 15:34:37 +01003451 /*
3452 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003453 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01003454 * the kvm msr permission bitmap may contain zero bits
3455 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01003456 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003457
Joerg Roedel323c3d82010-03-01 15:34:37 +01003458 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3459 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003460
Joerg Roedel323c3d82010-03-01 15:34:37 +01003461 for (i = 0; i < MSRPM_OFFSETS; i++) {
3462 u32 value, p;
3463 u64 offset;
3464
3465 if (msrpm_offsets[i] == 0xffffffff)
3466 break;
3467
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003468 p = msrpm_offsets[i];
3469 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01003470
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003471 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01003472 return false;
3473
3474 svm->nested.msrpm[p] = svm->msrpm[p] | value;
3475 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003476
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05003477 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
Alexander Graf3d6368e2008-11-25 20:17:07 +01003478
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003479 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003480}
3481
Joerg Roedel52c65a302010-08-02 16:46:44 +02003482static bool nested_vmcb_checks(struct vmcb *vmcb)
3483{
3484 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
3485 return false;
3486
Joerg Roedeldbe77582010-08-02 16:46:45 +02003487 if (vmcb->control.asid == 0)
3488 return false;
3489
Tom Lendackycea3a192017-12-04 10:57:24 -06003490 if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
3491 !npt_enabled)
Joerg Roedel4b161842010-09-10 17:31:03 +02003492 return false;
3493
Joerg Roedel52c65a302010-08-02 16:46:44 +02003494 return true;
3495}
3496
Ladi Prosekc2634062017-10-11 16:54:44 +02003497static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003498 struct vmcb *nested_vmcb, struct kvm_host_map *map)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003499{
Avi Kivityf6e78472010-08-02 15:30:20 +03003500 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003501 svm->vcpu.arch.hflags |= HF_HIF_MASK;
3502 else
3503 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
3504
Tom Lendackycea3a192017-12-04 10:57:24 -06003505 if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
Joerg Roedel4b161842010-09-10 17:31:03 +02003506 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
3507 nested_svm_init_mmu_context(&svm->vcpu);
3508 }
3509
Alexander Graf3d6368e2008-11-25 20:17:07 +01003510 /* Load the nested guest state */
3511 svm->vmcb->save.es = nested_vmcb->save.es;
3512 svm->vmcb->save.cs = nested_vmcb->save.cs;
3513 svm->vmcb->save.ss = nested_vmcb->save.ss;
3514 svm->vmcb->save.ds = nested_vmcb->save.ds;
3515 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
3516 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003517 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003518 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
3519 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
3520 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
3521 if (npt_enabled) {
3522 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
3523 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003524 } else
Avi Kivity23902182010-06-10 17:02:16 +03003525 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003526
3527 /* Guest paging mode is active - reset mmu */
3528 kvm_mmu_reset_context(&svm->vcpu);
3529
Joerg Roedeldefbba52009-08-07 11:49:30 +02003530 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Sean Christophersonde3cd112019-04-30 10:36:17 -07003531 kvm_rax_write(&svm->vcpu, nested_vmcb->save.rax);
Paolo Bonzinie9c16c72019-04-30 22:07:26 +02003532 kvm_rsp_write(&svm->vcpu, nested_vmcb->save.rsp);
3533 kvm_rip_write(&svm->vcpu, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01003534
Alexander Graf3d6368e2008-11-25 20:17:07 +01003535 /* In case we don't even reach vcpu_run, the fields are not updated */
3536 svm->vmcb->save.rax = nested_vmcb->save.rax;
3537 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3538 svm->vmcb->save.rip = nested_vmcb->save.rip;
3539 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3540 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3541 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3542
Joerg Roedelf7138532010-03-01 15:34:40 +01003543 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003544 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003545
Joerg Roedelaad42c62009-08-07 11:49:34 +02003546 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003547 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003548 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02003549 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3550 svm->nested.intercept = nested_vmcb->control.intercept;
3551
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08003552 svm_flush_tlb(&svm->vcpu, true);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003553 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003554 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3555 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3556 else
3557 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3558
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003559 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3560 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003561 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3562 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003563 }
3564
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003565 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003566 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003567
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003568 svm->vcpu.arch.tsc_offset += nested_vmcb->control.tsc_offset;
3569 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
3570
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003571 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003572 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3573 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003574 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3575 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3576
Tambe, Williame0813542018-11-13 16:51:20 +00003577 svm->vmcb->control.pause_filter_count =
3578 nested_vmcb->control.pause_filter_count;
3579 svm->vmcb->control.pause_filter_thresh =
3580 nested_vmcb->control.pause_filter_thresh;
3581
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003582 kvm_vcpu_unmap(&svm->vcpu, map, true);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003583
Joerg Roedel20307532010-11-29 17:51:48 +01003584 /* Enter Guest-Mode */
3585 enter_guest_mode(&svm->vcpu);
3586
Joerg Roedel384c6362010-11-30 18:03:56 +01003587 /*
3588 * Merge guest and host intercepts - must be called with vcpu in
3589 * guest-mode to take affect here
3590 */
3591 recalc_intercepts(svm);
3592
Joerg Roedel06fc77722010-02-19 16:23:07 +01003593 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003594
Joerg Roedel2af91942009-08-07 11:49:28 +02003595 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003596
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003597 mark_all_dirty(svm->vmcb);
Ladi Prosekc2634062017-10-11 16:54:44 +02003598}
3599
Vitaly Kuznetsove7134c12019-08-13 15:53:34 +02003600static int nested_svm_vmrun(struct vcpu_svm *svm)
Ladi Prosekc2634062017-10-11 16:54:44 +02003601{
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003602 int ret;
Ladi Prosekc2634062017-10-11 16:54:44 +02003603 struct vmcb *nested_vmcb;
3604 struct vmcb *hsave = svm->nested.hsave;
3605 struct vmcb *vmcb = svm->vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003606 struct kvm_host_map map;
Ladi Prosekc2634062017-10-11 16:54:44 +02003607 u64 vmcb_gpa;
3608
3609 vmcb_gpa = svm->vmcb->save.rax;
3610
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003611 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb_gpa), &map);
Dan Carpentera0619852019-08-27 12:38:52 +03003612 if (ret == -EINVAL) {
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003613 kvm_inject_gp(&svm->vcpu, 0);
Vitaly Kuznetsove7134c12019-08-13 15:53:34 +02003614 return 1;
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003615 } else if (ret) {
3616 return kvm_skip_emulated_instruction(&svm->vcpu);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003617 }
3618
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003619 ret = kvm_skip_emulated_instruction(&svm->vcpu);
3620
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003621 nested_vmcb = map.hva;
Ladi Prosekc2634062017-10-11 16:54:44 +02003622
3623 if (!nested_vmcb_checks(nested_vmcb)) {
3624 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3625 nested_vmcb->control.exit_code_hi = 0;
3626 nested_vmcb->control.exit_info_1 = 0;
3627 nested_vmcb->control.exit_info_2 = 0;
3628
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003629 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Ladi Prosekc2634062017-10-11 16:54:44 +02003630
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003631 return ret;
Ladi Prosekc2634062017-10-11 16:54:44 +02003632 }
3633
3634 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3635 nested_vmcb->save.rip,
3636 nested_vmcb->control.int_ctl,
3637 nested_vmcb->control.event_inj,
3638 nested_vmcb->control.nested_ctl);
3639
3640 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3641 nested_vmcb->control.intercept_cr >> 16,
3642 nested_vmcb->control.intercept_exceptions,
3643 nested_vmcb->control.intercept);
3644
3645 /* Clear internal status */
3646 kvm_clear_exception_queue(&svm->vcpu);
3647 kvm_clear_interrupt_queue(&svm->vcpu);
3648
3649 /*
3650 * Save the old vmcb, so we don't need to pick what we save, but can
3651 * restore everything when a VMEXIT occurs
3652 */
3653 hsave->save.es = vmcb->save.es;
3654 hsave->save.cs = vmcb->save.cs;
3655 hsave->save.ss = vmcb->save.ss;
3656 hsave->save.ds = vmcb->save.ds;
3657 hsave->save.gdtr = vmcb->save.gdtr;
3658 hsave->save.idtr = vmcb->save.idtr;
3659 hsave->save.efer = svm->vcpu.arch.efer;
3660 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3661 hsave->save.cr4 = svm->vcpu.arch.cr4;
3662 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3663 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3664 hsave->save.rsp = vmcb->save.rsp;
3665 hsave->save.rax = vmcb->save.rax;
3666 if (npt_enabled)
3667 hsave->save.cr3 = vmcb->save.cr3;
3668 else
3669 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3670
3671 copy_vmcb_control_area(hsave, vmcb);
3672
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003673 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, &map);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003674
Vitaly Kuznetsove7134c12019-08-13 15:53:34 +02003675 if (!nested_svm_vmrun_msrpm(svm)) {
3676 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3677 svm->vmcb->control.exit_code_hi = 0;
3678 svm->vmcb->control.exit_info_1 = 0;
3679 svm->vmcb->control.exit_info_2 = 0;
3680
3681 nested_svm_vmexit(svm);
3682 }
3683
Vitaly Kuznetsovc8e16b72019-08-13 15:53:35 +02003684 return ret;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003685}
3686
Joerg Roedel9966bf62009-08-07 11:49:40 +02003687static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01003688{
3689 to_vmcb->save.fs = from_vmcb->save.fs;
3690 to_vmcb->save.gs = from_vmcb->save.gs;
3691 to_vmcb->save.tr = from_vmcb->save.tr;
3692 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3693 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3694 to_vmcb->save.star = from_vmcb->save.star;
3695 to_vmcb->save.lstar = from_vmcb->save.lstar;
3696 to_vmcb->save.cstar = from_vmcb->save.cstar;
3697 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3698 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3699 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3700 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01003701}
3702
Avi Kivity851ba692009-08-24 11:10:17 +03003703static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003704{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003705 struct vmcb *nested_vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003706 struct kvm_host_map map;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003707 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003708
Alexander Graf55426752008-11-25 20:17:06 +01003709 if (nested_svm_check_permissions(svm))
3710 return 1;
3711
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003712 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
3713 if (ret) {
3714 if (ret == -EINVAL)
3715 kvm_inject_gp(&svm->vcpu, 0);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003716 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003717 }
3718
3719 nested_vmcb = map.hva;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003720
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003721 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003722
Joerg Roedel9966bf62009-08-07 11:49:40 +02003723 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003724 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Alexander Graf55426752008-11-25 20:17:06 +01003725
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003726 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003727}
3728
Avi Kivity851ba692009-08-24 11:10:17 +03003729static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003730{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003731 struct vmcb *nested_vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003732 struct kvm_host_map map;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003733 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003734
Alexander Graf55426752008-11-25 20:17:06 +01003735 if (nested_svm_check_permissions(svm))
3736 return 1;
3737
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003738 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
3739 if (ret) {
3740 if (ret == -EINVAL)
3741 kvm_inject_gp(&svm->vcpu, 0);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003742 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003743 }
3744
3745 nested_vmcb = map.hva;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003746
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003747 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003748
Joerg Roedel9966bf62009-08-07 11:49:40 +02003749 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01003750 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Alexander Graf55426752008-11-25 20:17:06 +01003751
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003752 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003753}
3754
Avi Kivity851ba692009-08-24 11:10:17 +03003755static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003756{
Alexander Graf3d6368e2008-11-25 20:17:07 +01003757 if (nested_svm_check_permissions(svm))
3758 return 1;
3759
Vitaly Kuznetsove7134c12019-08-13 15:53:34 +02003760 return nested_svm_vmrun(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003761}
3762
Avi Kivity851ba692009-08-24 11:10:17 +03003763static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003764{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003765 int ret;
3766
Alexander Graf1371d902008-11-25 20:17:04 +01003767 if (nested_svm_check_permissions(svm))
3768 return 1;
3769
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003770 /*
3771 * If VGIF is enabled, the STGI intercept is only added to
Ladi Prosekcc3d9672017-10-17 16:02:39 +02003772 * detect the opening of the SMI/NMI window; remove it now.
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003773 */
3774 if (vgif_enabled(svm))
3775 clr_intercept(svm, INTERCEPT_STGI);
3776
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003777 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003778 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003779
Joerg Roedel2af91942009-08-07 11:49:28 +02003780 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003781
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003782 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003783}
3784
Avi Kivity851ba692009-08-24 11:10:17 +03003785static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003786{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003787 int ret;
3788
Alexander Graf1371d902008-11-25 20:17:04 +01003789 if (nested_svm_check_permissions(svm))
3790 return 1;
3791
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003792 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003793
Joerg Roedel2af91942009-08-07 11:49:28 +02003794 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003795
3796 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05003797 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3798 svm_clear_vintr(svm);
3799 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3800 mark_dirty(svm->vmcb, VMCB_INTR);
3801 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003802
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003803 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003804}
3805
Avi Kivity851ba692009-08-24 11:10:17 +03003806static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02003807{
3808 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02003809
Sean Christophersonde3cd112019-04-30 10:36:17 -07003810 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_rcx_read(&svm->vcpu),
3811 kvm_rax_read(&svm->vcpu));
Joerg Roedelec1ff792009-10-09 16:08:31 +02003812
Alexander Grafff092382009-06-15 15:21:24 +02003813 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
Sean Christophersonde3cd112019-04-30 10:36:17 -07003814 kvm_mmu_invlpg(vcpu, kvm_rax_read(&svm->vcpu));
Alexander Grafff092382009-06-15 15:21:24 +02003815
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003816 return kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02003817}
3818
Joerg Roedel532a46b2009-10-09 16:08:32 +02003819static int skinit_interception(struct vcpu_svm *svm)
3820{
Sean Christophersonde3cd112019-04-30 10:36:17 -07003821 trace_kvm_skinit(svm->vmcb->save.rip, kvm_rax_read(&svm->vcpu));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003822
3823 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3824 return 1;
3825}
3826
David Kaplandab429a2015-03-02 13:43:37 -06003827static int wbinvd_interception(struct vcpu_svm *svm)
3828{
Kyle Huey6affcbe2016-11-29 12:40:40 -08003829 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06003830}
3831
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003832static int xsetbv_interception(struct vcpu_svm *svm)
3833{
3834 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07003835 u32 index = kvm_rcx_read(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003836
3837 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003838 return kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003839 }
3840
3841 return 1;
3842}
3843
Jim Mattson0cb84102019-09-19 15:59:17 -07003844static int rdpru_interception(struct vcpu_svm *svm)
3845{
3846 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3847 return 1;
3848}
3849
Avi Kivity851ba692009-08-24 11:10:17 +03003850static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003851{
Izik Eidus37817f22008-03-24 23:14:53 +02003852 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003853 int reason;
3854 int int_type = svm->vmcb->control.exit_int_info &
3855 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003856 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003857 uint32_t type =
3858 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3859 uint32_t idt_v =
3860 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003861 bool has_error_code = false;
3862 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003863
3864 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003865
Izik Eidus37817f22008-03-24 23:14:53 +02003866 if (svm->vmcb->control.exit_info_2 &
3867 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003868 reason = TASK_SWITCH_IRET;
3869 else if (svm->vmcb->control.exit_info_2 &
3870 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3871 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003872 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003873 reason = TASK_SWITCH_GATE;
3874 else
3875 reason = TASK_SWITCH_CALL;
3876
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003877 if (reason == TASK_SWITCH_GATE) {
3878 switch (type) {
3879 case SVM_EXITINTINFO_TYPE_NMI:
3880 svm->vcpu.arch.nmi_injected = false;
3881 break;
3882 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003883 if (svm->vmcb->control.exit_info_2 &
3884 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3885 has_error_code = true;
3886 error_code =
3887 (u32)svm->vmcb->control.exit_info_2;
3888 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003889 kvm_clear_exception_queue(&svm->vcpu);
3890 break;
3891 case SVM_EXITINTINFO_TYPE_INTR:
3892 kvm_clear_interrupt_queue(&svm->vcpu);
3893 break;
3894 default:
3895 break;
3896 }
3897 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003898
Gleb Natapov8317c292009-04-12 13:37:02 +03003899 if (reason != TASK_SWITCH_GATE ||
3900 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3901 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02003902 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07003903 if (!skip_emulated_instruction(&svm->vcpu))
Sean Christopherson738fece2019-08-27 14:40:34 -07003904 return 0;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02003905 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003906
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003907 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3908 int_vec = -1;
3909
Sean Christopherson10517782019-08-27 14:40:35 -07003910 return kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07003911 has_error_code, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003912}
3913
Avi Kivity851ba692009-08-24 11:10:17 +03003914static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003915{
Kyle Huey6a908b62016-11-29 12:40:37 -08003916 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003917}
3918
Avi Kivity851ba692009-08-24 11:10:17 +03003919static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003920{
3921 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003922 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003923 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003924 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003925 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003926 return 1;
3927}
3928
Avi Kivity851ba692009-08-24 11:10:17 +03003929static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003930{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003931 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07003932 return kvm_emulate_instruction(&svm->vcpu, 0);
Andre Przywaradf4f31082010-12-21 11:12:06 +01003933
3934 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003935 return kvm_skip_emulated_instruction(&svm->vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003936}
3937
Avi Kivity851ba692009-08-24 11:10:17 +03003938static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003939{
Sean Christopherson60fc3d02019-08-27 14:40:38 -07003940 return kvm_emulate_instruction(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003941}
3942
Brijesh Singh7607b712018-02-19 10:14:44 -06003943static int rsm_interception(struct vcpu_svm *svm)
3944{
Sean Christopherson60fc3d02019-08-27 14:40:38 -07003945 return kvm_emulate_instruction_from_buffer(&svm->vcpu, rsm_ins_bytes, 2);
Brijesh Singh7607b712018-02-19 10:14:44 -06003946}
3947
Avi Kivity332b56e2011-11-10 14:57:24 +02003948static int rdpmc_interception(struct vcpu_svm *svm)
3949{
3950 int err;
3951
Paolo Bonzinid647eb62019-06-20 14:13:33 +02003952 if (!nrips)
Avi Kivity332b56e2011-11-10 14:57:24 +02003953 return emulate_on_interception(svm);
3954
3955 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08003956 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02003957}
3958
Xiubo Li52eb5a62015-03-13 17:39:45 +08003959static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3960 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003961{
3962 unsigned long cr0 = svm->vcpu.arch.cr0;
3963 bool ret = false;
3964 u64 intercept;
3965
3966 intercept = svm->nested.intercept;
3967
3968 if (!is_guest_mode(&svm->vcpu) ||
3969 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3970 return false;
3971
3972 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3973 val &= ~SVM_CR0_SELECTIVE_MASK;
3974
3975 if (cr0 ^ val) {
3976 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3977 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3978 }
3979
3980 return ret;
3981}
3982
Andre Przywara7ff76d52010-12-21 11:12:04 +01003983#define CR_VALID (1ULL << 63)
3984
3985static int cr_interception(struct vcpu_svm *svm)
3986{
3987 int reg, cr;
3988 unsigned long val;
3989 int err;
3990
3991 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3992 return emulate_on_interception(svm);
3993
3994 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3995 return emulate_on_interception(svm);
3996
3997 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003998 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3999 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
4000 else
4001 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01004002
4003 err = 0;
4004 if (cr >= 16) { /* mov to cr */
4005 cr -= 16;
4006 val = kvm_register_read(&svm->vcpu, reg);
4007 switch (cr) {
4008 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02004009 if (!check_selective_cr0_intercepted(svm, val))
4010 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02004011 else
4012 return 1;
4013
Andre Przywara7ff76d52010-12-21 11:12:04 +01004014 break;
4015 case 3:
4016 err = kvm_set_cr3(&svm->vcpu, val);
4017 break;
4018 case 4:
4019 err = kvm_set_cr4(&svm->vcpu, val);
4020 break;
4021 case 8:
4022 err = kvm_set_cr8(&svm->vcpu, val);
4023 break;
4024 default:
4025 WARN(1, "unhandled write to CR%d", cr);
4026 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
4027 return 1;
4028 }
4029 } else { /* mov from cr */
4030 switch (cr) {
4031 case 0:
4032 val = kvm_read_cr0(&svm->vcpu);
4033 break;
4034 case 2:
4035 val = svm->vcpu.arch.cr2;
4036 break;
4037 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02004038 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01004039 break;
4040 case 4:
4041 val = kvm_read_cr4(&svm->vcpu);
4042 break;
4043 case 8:
4044 val = kvm_get_cr8(&svm->vcpu);
4045 break;
4046 default:
4047 WARN(1, "unhandled read from CR%d", cr);
4048 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
4049 return 1;
4050 }
4051 kvm_register_write(&svm->vcpu, reg, val);
4052 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08004053 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01004054}
4055
Andre Przywaracae37972010-12-21 11:12:05 +01004056static int dr_interception(struct vcpu_svm *svm)
4057{
4058 int reg, dr;
4059 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01004060
Paolo Bonzinifacb0132014-02-21 10:32:27 +01004061 if (svm->vcpu.guest_debug == 0) {
4062 /*
4063 * No more DR vmexits; force a reload of the debug registers
4064 * and reenter on this instruction. The next vmexit will
4065 * retrieve the full state of the debug registers.
4066 */
4067 clr_dr_intercepts(svm);
4068 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
4069 return 1;
4070 }
4071
Andre Przywaracae37972010-12-21 11:12:05 +01004072 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
4073 return emulate_on_interception(svm);
4074
4075 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
4076 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
4077
4078 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004079 if (!kvm_require_dr(&svm->vcpu, dr - 16))
4080 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01004081 val = kvm_register_read(&svm->vcpu, reg);
4082 kvm_set_dr(&svm->vcpu, dr - 16, val);
4083 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004084 if (!kvm_require_dr(&svm->vcpu, dr))
4085 return 1;
4086 kvm_get_dr(&svm->vcpu, dr, &val);
4087 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01004088 }
4089
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004090 return kvm_skip_emulated_instruction(&svm->vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01004091}
4092
Avi Kivity851ba692009-08-24 11:10:17 +03004093static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01004094{
Avi Kivity851ba692009-08-24 11:10:17 +03004095 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004096 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03004097
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004098 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
4099 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01004100 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004101 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01004102 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004103 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01004104 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01004105 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
4106 return 0;
4107}
4108
Tom Lendacky801e4592018-02-21 13:39:51 -06004109static int svm_get_msr_feature(struct kvm_msr_entry *msr)
4110{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004111 msr->data = 0;
4112
4113 switch (msr->index) {
4114 case MSR_F10H_DECFG:
4115 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
4116 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
4117 break;
4118 default:
4119 return 1;
4120 }
4121
4122 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004123}
4124
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004125static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004127 struct vcpu_svm *svm = to_svm(vcpu);
4128
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004129 switch (msr_info->index) {
Brian Gerst8c065852010-07-17 09:03:26 -04004130 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004131 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004132 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08004133#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004135 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136 break;
4137 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004138 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004139 break;
4140 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004141 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004142 break;
4143 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004144 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145 break;
4146#endif
4147 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004148 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149 break;
4150 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004151 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152 break;
4153 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004154 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004156 case MSR_TSC_AUX:
4157 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4158 return 1;
4159 msr_info->data = svm->tsc_aux;
4160 break;
Joerg Roedele0231712010-02-24 18:59:10 +01004161 /*
4162 * Nobody will change the following 5 values in the VMCB so we can
4163 * safely return them on rdmsr. They will always be 0 until LBRV is
4164 * implemented.
4165 */
Joerg Roedela2938c82008-02-13 16:30:28 +01004166 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004167 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01004168 break;
4169 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004170 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01004171 break;
4172 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004173 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01004174 break;
4175 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004176 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01004177 break;
4178 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004179 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01004180 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004181 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004182 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004183 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01004184 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004185 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01004186 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004187 case MSR_IA32_SPEC_CTRL:
4188 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -04004189 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
4190 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004191 return 1;
4192
4193 msr_info->data = svm->spec_ctrl;
4194 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02004195 case MSR_AMD64_VIRT_SPEC_CTRL:
4196 if (!msr_info->host_initiated &&
4197 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
4198 return 1;
4199
4200 msr_info->data = svm->virt_spec_ctrl;
4201 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01004202 case MSR_F15H_IC_CFG: {
4203
4204 int family, model;
4205
4206 family = guest_cpuid_family(vcpu);
4207 model = guest_cpuid_model(vcpu);
4208
4209 if (family < 0 || model < 0)
4210 return kvm_get_msr_common(vcpu, msr_info);
4211
4212 msr_info->data = 0;
4213
4214 if (family == 0x15 &&
4215 (model >= 0x2 && model < 0x20))
4216 msr_info->data = 0x1E;
4217 }
4218 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004219 case MSR_F10H_DECFG:
4220 msr_info->data = svm->msr_decfg;
4221 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004222 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004223 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004224 }
4225 return 0;
4226}
4227
Avi Kivity851ba692009-08-24 11:10:17 +03004228static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004229{
Sean Christopherson1edce0a2019-09-05 14:22:55 -07004230 return kvm_emulate_rdmsr(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004231}
4232
Joerg Roedel4a810182010-02-24 18:59:15 +01004233static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
4234{
4235 struct vcpu_svm *svm = to_svm(vcpu);
4236 int svm_dis, chg_mask;
4237
4238 if (data & ~SVM_VM_CR_VALID_MASK)
4239 return 1;
4240
4241 chg_mask = SVM_VM_CR_VALID_MASK;
4242
4243 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
4244 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
4245
4246 svm->nested.vm_cr_msr &= ~chg_mask;
4247 svm->nested.vm_cr_msr |= (data & chg_mask);
4248
4249 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
4250
4251 /* check for svm_disable while efer.svme is set */
4252 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
4253 return 1;
4254
4255 return 0;
4256}
4257
Will Auld8fe8ab42012-11-29 12:42:12 -08004258static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004259{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004260 struct vcpu_svm *svm = to_svm(vcpu);
4261
Will Auld8fe8ab42012-11-29 12:42:12 -08004262 u32 ecx = msr->index;
4263 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004264 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02004265 case MSR_IA32_CR_PAT:
4266 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4267 return 1;
4268 vcpu->arch.pat = data;
4269 svm->vmcb->save.g_pat = data;
4270 mark_dirty(svm->vmcb, VMCB_NPT);
4271 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004272 case MSR_IA32_SPEC_CTRL:
4273 if (!msr->host_initiated &&
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -04004274 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
4275 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004276 return 1;
4277
4278 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -04004279 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004280 return 1;
4281
4282 svm->spec_ctrl = data;
4283
4284 if (!data)
4285 break;
4286
4287 /*
4288 * For non-nested:
4289 * When it's written (to non-zero) for the first time, pass
4290 * it through.
4291 *
4292 * For nested:
4293 * The handling of the MSR bitmap for L2 guests is done in
4294 * nested_svm_vmrun_msrpm.
4295 * We update the L1 MSR bit as well since it will end up
4296 * touching the MSR anyway now.
4297 */
4298 set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
4299 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004300 case MSR_IA32_PRED_CMD:
4301 if (!msr->host_initiated &&
Borislav Petkove7c587d2018-05-02 18:15:14 +02004302 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB))
Ashok Raj15d45072018-02-01 22:59:43 +01004303 return 1;
4304
4305 if (data & ~PRED_CMD_IBPB)
4306 return 1;
4307
4308 if (!data)
4309 break;
4310
4311 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4312 if (is_guest_mode(vcpu))
4313 break;
4314 set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
4315 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02004316 case MSR_AMD64_VIRT_SPEC_CTRL:
4317 if (!msr->host_initiated &&
4318 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
4319 return 1;
4320
4321 if (data & ~SPEC_CTRL_SSBD)
4322 return 1;
4323
4324 svm->virt_spec_ctrl = data;
4325 break;
Brian Gerst8c065852010-07-17 09:03:26 -04004326 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004327 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004328 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08004329#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004330 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004331 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004332 break;
4333 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004334 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004335 break;
4336 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004337 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004338 break;
4339 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004340 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341 break;
4342#endif
4343 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004344 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004345 break;
4346 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02004347 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004348 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004349 break;
4350 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02004351 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004352 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004353 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004354 case MSR_TSC_AUX:
4355 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4356 return 1;
4357
4358 /*
4359 * This is rare, so we update the MSR here instead of using
4360 * direct_access_msrs. Doing that would require a rdmsr in
4361 * svm_vcpu_put.
4362 */
4363 svm->tsc_aux = data;
4364 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
4365 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01004366 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004367 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03004368 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
4369 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004370 break;
4371 }
4372 if (data & DEBUGCTL_RESERVED_BITS)
4373 return 1;
4374
4375 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01004376 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004377 if (data & (1ULL<<0))
4378 svm_enable_lbrv(svm);
4379 else
4380 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01004381 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004382 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02004383 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004384 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004385 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01004386 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004387 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03004388 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004389 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004390 case MSR_F10H_DECFG: {
4391 struct kvm_msr_entry msr_entry;
4392
4393 msr_entry.index = msr->index;
4394 if (svm_get_msr_feature(&msr_entry))
4395 return 1;
4396
4397 /* Check the supported bits */
4398 if (data & ~msr_entry.data)
4399 return 1;
4400
4401 /* Don't allow the guest to change a bit, #GP */
4402 if (!msr->host_initiated && (data ^ msr_entry.data))
4403 return 1;
4404
4405 svm->msr_decfg = data;
4406 break;
4407 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004408 case MSR_IA32_APICBASE:
4409 if (kvm_vcpu_apicv_active(vcpu))
4410 avic_update_vapic_bar(to_svm(vcpu), data);
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06004411 /* Fall through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004412 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08004413 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004414 }
4415 return 0;
4416}
4417
Avi Kivity851ba692009-08-24 11:10:17 +03004418static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004419{
Sean Christopherson1edce0a2019-09-05 14:22:55 -07004420 return kvm_emulate_wrmsr(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004421}
4422
Avi Kivity851ba692009-08-24 11:10:17 +03004423static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004424{
Rusty Russelle756fc62007-07-30 20:07:08 +10004425 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03004426 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004427 else
Avi Kivity851ba692009-08-24 11:10:17 +03004428 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004429}
4430
Avi Kivity851ba692009-08-24 11:10:17 +03004431static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08004432{
Avi Kivity3842d132010-07-27 12:30:24 +03004433 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01004434 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03004435 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004436 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08004437 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08004438 return 1;
4439}
4440
Mark Langsdorf565d0992009-10-06 14:25:02 -05004441static int pause_interception(struct vcpu_svm *svm)
4442{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004443 struct kvm_vcpu *vcpu = &svm->vcpu;
4444 bool in_kernel = (svm_get_cpl(vcpu) == 0);
4445
Babu Moger8566ac82018-03-16 16:37:26 -04004446 if (pause_filter_thresh)
4447 grow_ple_window(vcpu);
4448
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004449 kvm_vcpu_on_spin(vcpu, in_kernel);
Mark Langsdorf565d0992009-10-06 14:25:02 -05004450 return 1;
4451}
4452
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004453static int nop_interception(struct vcpu_svm *svm)
4454{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004455 return kvm_skip_emulated_instruction(&(svm->vcpu));
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004456}
4457
4458static int monitor_interception(struct vcpu_svm *svm)
4459{
4460 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
4461 return nop_interception(svm);
4462}
4463
4464static int mwait_interception(struct vcpu_svm *svm)
4465{
4466 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4467 return nop_interception(svm);
4468}
4469
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004470enum avic_ipi_failure_cause {
4471 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
4472 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
4473 AVIC_IPI_FAILURE_INVALID_TARGET,
4474 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
4475};
4476
4477static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
4478{
4479 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
4480 u32 icrl = svm->vmcb->control.exit_info_1;
4481 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03004482 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004483 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4484
4485 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
4486
4487 switch (id) {
4488 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
4489 /*
4490 * AVIC hardware handles the generation of
4491 * IPIs when the specified Message Type is Fixed
4492 * (also known as fixed delivery mode) and
4493 * the Trigger Mode is edge-triggered. The hardware
4494 * also supports self and broadcast delivery modes
4495 * specified via the Destination Shorthand(DSH)
4496 * field of the ICRL. Logical and physical APIC ID
4497 * formats are supported. All other IPI types cause
4498 * a #VMEXIT, which needs to emulated.
4499 */
4500 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
4501 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
4502 break;
4503 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
Suthikulpanit, Suravee4a580382019-03-20 08:12:28 +00004504 int i;
4505 struct kvm_vcpu *vcpu;
4506 struct kvm *kvm = svm->vcpu.kvm;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004507 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4508
4509 /*
Suthikulpanit, Suravee4a580382019-03-20 08:12:28 +00004510 * At this point, we expect that the AVIC HW has already
4511 * set the appropriate IRR bits on the valid target
4512 * vcpus. So, we just need to kick the appropriate vcpu.
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004513 */
Suthikulpanit, Suravee4a580382019-03-20 08:12:28 +00004514 kvm_for_each_vcpu(i, vcpu, kvm) {
4515 bool m = kvm_apic_match_dest(vcpu, apic,
4516 icrl & KVM_APIC_SHORT_MASK,
4517 GET_APIC_DEST_FIELD(icrh),
4518 icrl & KVM_APIC_DEST_MASK);
4519
4520 if (m && !avic_vcpu_is_running(vcpu))
4521 kvm_vcpu_wake_up(vcpu);
4522 }
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004523 break;
4524 }
4525 case AVIC_IPI_FAILURE_INVALID_TARGET:
Suravee Suthikulpanit37ef0c42019-01-22 10:24:19 +00004526 WARN_ONCE(1, "Invalid IPI target: index=%u, vcpu=%d, icr=%#0x:%#0x\n",
4527 index, svm->vcpu.vcpu_id, icrh, icrl);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004528 break;
4529 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
4530 WARN_ONCE(1, "Invalid backing page\n");
4531 break;
4532 default:
4533 pr_err("Unknown IPI interception\n");
4534 }
4535
4536 return 1;
4537}
4538
4539static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
4540{
Sean Christopherson81811c12018-03-20 12:17:21 -07004541 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004542 int index;
4543 u32 *logical_apic_id_table;
4544 int dlid = GET_APIC_LOGICAL_ID(ldr);
4545
4546 if (!dlid)
4547 return NULL;
4548
4549 if (flat) { /* flat */
4550 index = ffs(dlid) - 1;
4551 if (index > 7)
4552 return NULL;
4553 } else { /* cluster */
4554 int cluster = (dlid & 0xf0) >> 4;
4555 int apic = ffs(dlid & 0x0f) - 1;
4556
4557 if ((apic < 0) || (apic > 7) ||
4558 (cluster >= 0xf))
4559 return NULL;
4560 index = (cluster << 2) + apic;
4561 }
4562
Sean Christopherson81811c12018-03-20 12:17:21 -07004563 logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004564
4565 return &logical_apic_id_table[index];
4566}
4567
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004568static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr)
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004569{
4570 bool flat;
4571 u32 *entry, new_entry;
4572
4573 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4574 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4575 if (!entry)
4576 return -EINVAL;
4577
4578 new_entry = READ_ONCE(*entry);
4579 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4580 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004581 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004582 WRITE_ONCE(*entry, new_entry);
4583
4584 return 0;
4585}
4586
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004587static void avic_invalidate_logical_id_entry(struct kvm_vcpu *vcpu)
4588{
4589 struct vcpu_svm *svm = to_svm(vcpu);
4590 bool flat = svm->dfr_reg == APIC_DFR_FLAT;
4591 u32 *entry = avic_get_logical_id_entry(vcpu, svm->ldr_reg, flat);
4592
4593 if (entry)
Suthikulpanit, Suraveee44e3ea2019-03-26 03:57:37 +00004594 clear_bit(AVIC_LOGICAL_ID_ENTRY_VALID_BIT, (unsigned long *)entry);
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004595}
4596
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004597static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4598{
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004599 int ret = 0;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004600 struct vcpu_svm *svm = to_svm(vcpu);
4601 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
4602
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004603 if (ldr == svm->ldr_reg)
4604 return 0;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004605
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004606 avic_invalidate_logical_id_entry(vcpu);
4607
4608 if (ldr)
4609 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr);
4610
4611 if (!ret)
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004612 svm->ldr_reg = ldr;
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004613
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004614 return ret;
4615}
4616
4617static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4618{
4619 u64 *old, *new;
4620 struct vcpu_svm *svm = to_svm(vcpu);
4621 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
4622 u32 id = (apic_id_reg >> 24) & 0xff;
4623
4624 if (vcpu->vcpu_id == id)
4625 return 0;
4626
4627 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4628 new = avic_get_physical_id_entry(vcpu, id);
4629 if (!new || !old)
4630 return 1;
4631
4632 /* We need to move physical_id_entry to new offset */
4633 *new = *old;
4634 *old = 0ULL;
4635 to_svm(vcpu)->avic_physical_id_cache = new;
4636
4637 /*
4638 * Also update the guest physical APIC ID in the logical
4639 * APIC ID table entry if already setup the LDR.
4640 */
4641 if (svm->ldr_reg)
4642 avic_handle_ldr_update(vcpu);
4643
4644 return 0;
4645}
4646
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004647static void avic_handle_dfr_update(struct kvm_vcpu *vcpu)
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004648{
4649 struct vcpu_svm *svm = to_svm(vcpu);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004650 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004651
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004652 if (svm->dfr_reg == dfr)
4653 return;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004654
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00004655 avic_invalidate_logical_id_entry(vcpu);
4656 svm->dfr_reg = dfr;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004657}
4658
4659static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4660{
4661 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4662 u32 offset = svm->vmcb->control.exit_info_1 &
4663 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4664
4665 switch (offset) {
4666 case APIC_ID:
4667 if (avic_handle_apic_id_update(&svm->vcpu))
4668 return 0;
4669 break;
4670 case APIC_LDR:
4671 if (avic_handle_ldr_update(&svm->vcpu))
4672 return 0;
4673 break;
4674 case APIC_DFR:
4675 avic_handle_dfr_update(&svm->vcpu);
4676 break;
4677 default:
4678 break;
4679 }
4680
4681 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4682
4683 return 1;
4684}
4685
4686static bool is_avic_unaccelerated_access_trap(u32 offset)
4687{
4688 bool ret = false;
4689
4690 switch (offset) {
4691 case APIC_ID:
4692 case APIC_EOI:
4693 case APIC_RRR:
4694 case APIC_LDR:
4695 case APIC_DFR:
4696 case APIC_SPIV:
4697 case APIC_ESR:
4698 case APIC_ICR:
4699 case APIC_LVTT:
4700 case APIC_LVTTHMR:
4701 case APIC_LVTPC:
4702 case APIC_LVT0:
4703 case APIC_LVT1:
4704 case APIC_LVTERR:
4705 case APIC_TMICT:
4706 case APIC_TDCR:
4707 ret = true;
4708 break;
4709 default:
4710 break;
4711 }
4712 return ret;
4713}
4714
4715static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4716{
4717 int ret = 0;
4718 u32 offset = svm->vmcb->control.exit_info_1 &
4719 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4720 u32 vector = svm->vmcb->control.exit_info_2 &
4721 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4722 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4723 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4724 bool trap = is_avic_unaccelerated_access_trap(offset);
4725
4726 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4727 trap, write, vector);
4728 if (trap) {
4729 /* Handling Trap */
4730 WARN_ONCE(!write, "svm: Handling trap read.\n");
4731 ret = avic_unaccel_trap_write(svm);
4732 } else {
4733 /* Handling Fault */
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004734 ret = kvm_emulate_instruction(&svm->vcpu, 0);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004735 }
4736
4737 return ret;
4738}
4739
Mathias Krause09941fb2012-08-30 01:30:20 +02004740static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01004741 [SVM_EXIT_READ_CR0] = cr_interception,
4742 [SVM_EXIT_READ_CR3] = cr_interception,
4743 [SVM_EXIT_READ_CR4] = cr_interception,
4744 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06004745 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02004746 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01004747 [SVM_EXIT_WRITE_CR3] = cr_interception,
4748 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004749 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01004750 [SVM_EXIT_READ_DR0] = dr_interception,
4751 [SVM_EXIT_READ_DR1] = dr_interception,
4752 [SVM_EXIT_READ_DR2] = dr_interception,
4753 [SVM_EXIT_READ_DR3] = dr_interception,
4754 [SVM_EXIT_READ_DR4] = dr_interception,
4755 [SVM_EXIT_READ_DR5] = dr_interception,
4756 [SVM_EXIT_READ_DR6] = dr_interception,
4757 [SVM_EXIT_READ_DR7] = dr_interception,
4758 [SVM_EXIT_WRITE_DR0] = dr_interception,
4759 [SVM_EXIT_WRITE_DR1] = dr_interception,
4760 [SVM_EXIT_WRITE_DR2] = dr_interception,
4761 [SVM_EXIT_WRITE_DR3] = dr_interception,
4762 [SVM_EXIT_WRITE_DR4] = dr_interception,
4763 [SVM_EXIT_WRITE_DR5] = dr_interception,
4764 [SVM_EXIT_WRITE_DR6] = dr_interception,
4765 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004766 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4767 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004768 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004769 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004770 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01004771 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Liran Alon97184202018-03-12 13:12:52 +02004772 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004773 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02004774 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004775 [SVM_EXIT_SMI] = nop_on_interception,
4776 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08004777 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02004778 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004779 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004780 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02004781 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05004782 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004783 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004784 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02004785 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004786 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004787 [SVM_EXIT_MSR] = msr_interception,
4788 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08004789 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01004790 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02004791 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01004792 [SVM_EXIT_VMLOAD] = vmload_interception,
4793 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01004794 [SVM_EXIT_STGI] = stgi_interception,
4795 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02004796 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06004797 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004798 [SVM_EXIT_MONITOR] = monitor_interception,
4799 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01004800 [SVM_EXIT_XSETBV] = xsetbv_interception,
Jim Mattson0cb84102019-09-19 15:59:17 -07004801 [SVM_EXIT_RDPRU] = rdpru_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02004802 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06004803 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004804 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4805 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004806};
4807
Joe Perchesae8cc052011-04-24 22:00:50 -07004808static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02004809{
4810 struct vcpu_svm *svm = to_svm(vcpu);
4811 struct vmcb_control_area *control = &svm->vmcb->control;
4812 struct vmcb_save_area *save = &svm->vmcb->save;
4813
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02004814 if (!dump_invalid_vmcb) {
4815 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
4816 return;
4817 }
4818
Joerg Roedel3f10c842010-05-05 16:04:42 +02004819 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004820 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4821 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4822 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4823 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4824 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4825 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4826 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
Babu Moger1d8fb442018-03-16 16:37:25 -04004827 pr_err("%-20s%d\n", "pause filter threshold:",
4828 control->pause_filter_thresh);
Joe Perchesae8cc052011-04-24 22:00:50 -07004829 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4830 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4831 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4832 pr_err("%-20s%d\n", "asid:", control->asid);
4833 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4834 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4835 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4836 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4837 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4838 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4839 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4840 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4841 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4842 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4843 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004844 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07004845 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4846 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05004847 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07004848 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004849 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4850 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4851 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004852 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004853 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4854 "es:",
4855 save->es.selector, save->es.attrib,
4856 save->es.limit, save->es.base);
4857 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4858 "cs:",
4859 save->cs.selector, save->cs.attrib,
4860 save->cs.limit, save->cs.base);
4861 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4862 "ss:",
4863 save->ss.selector, save->ss.attrib,
4864 save->ss.limit, save->ss.base);
4865 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4866 "ds:",
4867 save->ds.selector, save->ds.attrib,
4868 save->ds.limit, save->ds.base);
4869 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4870 "fs:",
4871 save->fs.selector, save->fs.attrib,
4872 save->fs.limit, save->fs.base);
4873 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4874 "gs:",
4875 save->gs.selector, save->gs.attrib,
4876 save->gs.limit, save->gs.base);
4877 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4878 "gdtr:",
4879 save->gdtr.selector, save->gdtr.attrib,
4880 save->gdtr.limit, save->gdtr.base);
4881 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4882 "ldtr:",
4883 save->ldtr.selector, save->ldtr.attrib,
4884 save->ldtr.limit, save->ldtr.base);
4885 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4886 "idtr:",
4887 save->idtr.selector, save->idtr.attrib,
4888 save->idtr.limit, save->idtr.base);
4889 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4890 "tr:",
4891 save->tr.selector, save->tr.attrib,
4892 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004893 pr_err("cpl: %d efer: %016llx\n",
4894 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004895 pr_err("%-15s %016llx %-13s %016llx\n",
4896 "cr0:", save->cr0, "cr2:", save->cr2);
4897 pr_err("%-15s %016llx %-13s %016llx\n",
4898 "cr3:", save->cr3, "cr4:", save->cr4);
4899 pr_err("%-15s %016llx %-13s %016llx\n",
4900 "dr6:", save->dr6, "dr7:", save->dr7);
4901 pr_err("%-15s %016llx %-13s %016llx\n",
4902 "rip:", save->rip, "rflags:", save->rflags);
4903 pr_err("%-15s %016llx %-13s %016llx\n",
4904 "rsp:", save->rsp, "rax:", save->rax);
4905 pr_err("%-15s %016llx %-13s %016llx\n",
4906 "star:", save->star, "lstar:", save->lstar);
4907 pr_err("%-15s %016llx %-13s %016llx\n",
4908 "cstar:", save->cstar, "sfmask:", save->sfmask);
4909 pr_err("%-15s %016llx %-13s %016llx\n",
4910 "kernel_gs_base:", save->kernel_gs_base,
4911 "sysenter_cs:", save->sysenter_cs);
4912 pr_err("%-15s %016llx %-13s %016llx\n",
4913 "sysenter_esp:", save->sysenter_esp,
4914 "sysenter_eip:", save->sysenter_eip);
4915 pr_err("%-15s %016llx %-13s %016llx\n",
4916 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4917 pr_err("%-15s %016llx %-13s %016llx\n",
4918 "br_from:", save->br_from, "br_to:", save->br_to);
4919 pr_err("%-15s %016llx %-13s %016llx\n",
4920 "excp_from:", save->last_excp_from,
4921 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004922}
4923
Avi Kivity586f9602010-11-18 13:09:54 +02004924static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4925{
4926 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4927
4928 *info1 = control->exit_info_1;
4929 *info2 = control->exit_info_2;
4930}
4931
Avi Kivity851ba692009-08-24 11:10:17 +03004932static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004933{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004934 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004935 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004936 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004937
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004938 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4939
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004940 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004941 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4942 if (npt_enabled)
4943 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004944
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004945 if (unlikely(svm->nested.exit_required)) {
4946 nested_svm_vmexit(svm);
4947 svm->nested.exit_required = false;
4948
4949 return 1;
4950 }
4951
Joerg Roedel20307532010-11-29 17:51:48 +01004952 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004953 int vmexit;
4954
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004955 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4956 svm->vmcb->control.exit_info_1,
4957 svm->vmcb->control.exit_info_2,
4958 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004959 svm->vmcb->control.exit_int_info_err,
4960 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004961
Joerg Roedel410e4d52009-08-07 11:49:44 +02004962 vmexit = nested_svm_exit_special(svm);
4963
4964 if (vmexit == NESTED_EXIT_CONTINUE)
4965 vmexit = nested_svm_exit_handled(svm);
4966
4967 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004968 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004969 }
4970
Joerg Roedela5c38322009-08-07 11:49:32 +02004971 svm_complete_interrupts(svm);
4972
Avi Kivity04d2cc72007-09-10 18:10:54 +03004973 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4974 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4975 kvm_run->fail_entry.hardware_entry_failure_reason
4976 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004977 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004978 return 0;
4979 }
4980
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004981 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004982 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004983 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4984 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004985 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004986 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004987 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004988 exit_code);
4989
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004990 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004991 || !svm_exit_handlers[exit_code]) {
Liran Alon7396d332019-08-26 13:16:43 +03004992 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%x\n", exit_code);
4993 dump_vmcb(vcpu);
4994 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4995 vcpu->run->internal.suberror =
4996 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
4997 vcpu->run->internal.ndata = 1;
4998 vcpu->run->internal.data[0] = exit_code;
4999 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000 }
5001
Andrea Arcangeli3dcb2a32019-11-04 18:00:00 -05005002#ifdef CONFIG_RETPOLINE
5003 if (exit_code == SVM_EXIT_MSR)
5004 return msr_interception(svm);
5005 else if (exit_code == SVM_EXIT_VINTR)
5006 return interrupt_window_interception(svm);
5007 else if (exit_code == SVM_EXIT_INTR)
5008 return intr_interception(svm);
5009 else if (exit_code == SVM_EXIT_HLT)
5010 return halt_interception(svm);
5011 else if (exit_code == SVM_EXIT_NPF)
5012 return npf_interception(svm);
5013#endif
Avi Kivity851ba692009-08-24 11:10:17 +03005014 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005015}
5016
5017static void reload_tss(struct kvm_vcpu *vcpu)
5018{
5019 int cpu = raw_smp_processor_id();
5020
Tejun Heo0fe1e002009-10-29 22:34:14 +09005021 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
5022 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08005023 load_TR_desc();
5024}
5025
Brijesh Singh70cd94e2017-12-04 10:57:34 -06005026static void pre_sev_run(struct vcpu_svm *svm, int cpu)
5027{
5028 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
5029 int asid = sev_get_asid(svm->vcpu.kvm);
5030
5031 /* Assign the asid allocated with this SEV guest */
5032 svm->vmcb->control.asid = asid;
5033
5034 /*
5035 * Flush guest TLB:
5036 *
5037 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
5038 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
5039 */
5040 if (sd->sev_vmcbs[asid] == svm->vmcb &&
5041 svm->last_cpu == cpu)
5042 return;
5043
5044 svm->last_cpu = cpu;
5045 sd->sev_vmcbs[asid] = svm->vmcb;
5046 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5047 mark_dirty(svm->vmcb, VMCB_ASID);
5048}
5049
Rusty Russelle756fc62007-07-30 20:07:08 +10005050static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051{
5052 int cpu = raw_smp_processor_id();
5053
Tejun Heo0fe1e002009-10-29 22:34:14 +09005054 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005055
Brijesh Singh70cd94e2017-12-04 10:57:34 -06005056 if (sev_guest(svm->vcpu.kvm))
5057 return pre_sev_run(svm, cpu);
5058
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03005059 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09005060 if (svm->asid_generation != sd->asid_generation)
5061 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005062}
5063
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005064static void svm_inject_nmi(struct kvm_vcpu *vcpu)
5065{
5066 struct vcpu_svm *svm = to_svm(vcpu);
5067
5068 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
5069 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005070 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005071 ++vcpu->stat.nmi_injections;
5072}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005073
Eddie Dong85f455f2007-07-06 12:20:49 +03005074static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005075{
5076 struct vmcb_control_area *control;
5077
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005078 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10005079 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03005080 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005081 control->int_ctl &= ~V_INTR_PRIO_MASK;
5082 control->int_ctl |= V_IRQ_MASK |
5083 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01005084 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005085}
5086
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005087static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03005088{
5089 struct vcpu_svm *svm = to_svm(vcpu);
5090
Joerg Roedel2af91942009-08-07 11:49:28 +02005091 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01005092
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03005093 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
5094 ++vcpu->stat.irq_injections;
5095
Alexander Graf219b65d2009-06-15 15:21:25 +02005096 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
5097 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03005098}
5099
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005100static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
5101{
5102 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
5103}
5104
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005105static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
5106{
5107 struct vcpu_svm *svm = to_svm(vcpu);
5108
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005109 if (svm_nested_virtualize_tpr(vcpu) ||
5110 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005111 return;
5112
Radim Krčmář596f3142014-03-11 19:11:18 +01005113 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
5114
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005115 if (irr == -1)
5116 return;
5117
5118 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005119 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005120}
5121
Jim Mattson8d860bb2018-05-09 16:56:05 -04005122static void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08005123{
5124 return;
5125}
5126
Suthikulpanit, Suravee2cf9af02019-09-13 19:00:49 +00005127static bool svm_get_enable_apicv(struct kvm *kvm)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005128{
Suthikulpanit, Suravee2cf9af02019-09-13 19:00:49 +00005129 return avic && irqchip_split(kvm);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005130}
5131
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005132static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
5133{
5134}
5135
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005136static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005137{
5138}
5139
5140/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03005141static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5142{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005143 struct vcpu_svm *svm = to_svm(vcpu);
5144 struct vmcb *vmcb = svm->vmcb;
5145
Suthikulpanit, Suraveec57cd3c2019-01-29 08:09:46 +00005146 if (kvm_vcpu_apicv_active(vcpu))
5147 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
5148 else
5149 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
5150 mark_dirty(vmcb, VMCB_AVIC);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005151}
5152
Andrey Smetanin63086302015-11-10 15:36:32 +03005153static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005154{
5155 return;
5156}
5157
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005158static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
5159{
5160 kvm_lapic_set_irr(vec, vcpu->arch.apic);
5161 smp_mb__after_atomic();
5162
Suthikulpanit, Suravee0532dd52019-05-03 06:38:53 -07005163 if (avic_vcpu_is_running(vcpu)) {
5164 int cpuid = vcpu->cpu;
5165
5166 if (cpuid != get_cpu())
5167 wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpuid));
5168 put_cpu();
5169 } else
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005170 kvm_vcpu_wake_up(vcpu);
5171}
5172
Wanpeng Li17e433b2019-08-05 10:03:19 +08005173static bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
5174{
5175 return false;
5176}
5177
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005178static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
5179{
5180 unsigned long flags;
5181 struct amd_svm_iommu_ir *cur;
5182
5183 spin_lock_irqsave(&svm->ir_list_lock, flags);
5184 list_for_each_entry(cur, &svm->ir_list, node) {
5185 if (cur->data != pi->ir_data)
5186 continue;
5187 list_del(&cur->node);
5188 kfree(cur);
5189 break;
5190 }
5191 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
5192}
5193
5194static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
5195{
5196 int ret = 0;
5197 unsigned long flags;
5198 struct amd_svm_iommu_ir *ir;
5199
5200 /**
5201 * In some cases, the existing irte is updaed and re-set,
5202 * so we need to check here if it's already been * added
5203 * to the ir_list.
5204 */
5205 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
5206 struct kvm *kvm = svm->vcpu.kvm;
5207 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
5208 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
5209 struct vcpu_svm *prev_svm;
5210
5211 if (!prev_vcpu) {
5212 ret = -EINVAL;
5213 goto out;
5214 }
5215
5216 prev_svm = to_svm(prev_vcpu);
5217 svm_ir_list_del(prev_svm, pi);
5218 }
5219
5220 /**
5221 * Allocating new amd_iommu_pi_data, which will get
5222 * add to the per-vcpu ir_list.
5223 */
Ben Gardon1ec69642019-02-11 11:02:51 -08005224 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL_ACCOUNT);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005225 if (!ir) {
5226 ret = -ENOMEM;
5227 goto out;
5228 }
5229 ir->data = pi->ir_data;
5230
5231 spin_lock_irqsave(&svm->ir_list_lock, flags);
5232 list_add(&ir->node, &svm->ir_list);
5233 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
5234out:
5235 return ret;
5236}
5237
5238/**
5239 * Note:
5240 * The HW cannot support posting multicast/broadcast
5241 * interrupts to a vCPU. So, we still use legacy interrupt
5242 * remapping for these kind of interrupts.
5243 *
5244 * For lowest-priority interrupts, we only support
5245 * those with single CPU as the destination, e.g. user
5246 * configures the interrupts via /proc/irq or uses
5247 * irqbalance to make the interrupts single-CPU.
5248 */
5249static int
5250get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
5251 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
5252{
5253 struct kvm_lapic_irq irq;
5254 struct kvm_vcpu *vcpu = NULL;
5255
5256 kvm_set_msi_irq(kvm, e, &irq);
5257
Alexander Graffdcf7562019-09-05 14:58:18 +02005258 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
5259 !kvm_irq_is_postable(&irq)) {
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005260 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
5261 __func__, irq.vector);
5262 return -1;
5263 }
5264
5265 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
5266 irq.vector);
5267 *svm = to_svm(vcpu);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005268 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005269 vcpu_info->vector = irq.vector;
5270
5271 return 0;
5272}
5273
5274/*
5275 * svm_update_pi_irte - set IRTE for Posted-Interrupts
5276 *
5277 * @kvm: kvm
5278 * @host_irq: host irq of the interrupt
5279 * @guest_irq: gsi of the interrupt
5280 * @set: set or unset PI
5281 * returns 0 on success, < 0 on failure
5282 */
5283static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
5284 uint32_t guest_irq, bool set)
5285{
5286 struct kvm_kernel_irq_routing_entry *e;
5287 struct kvm_irq_routing_table *irq_rt;
5288 int idx, ret = -EINVAL;
5289
5290 if (!kvm_arch_has_assigned_device(kvm) ||
5291 !irq_remapping_cap(IRQ_POSTING_CAP))
5292 return 0;
5293
5294 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
5295 __func__, host_irq, guest_irq, set);
5296
5297 idx = srcu_read_lock(&kvm->irq_srcu);
5298 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
5299 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
5300
5301 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
5302 struct vcpu_data vcpu_info;
5303 struct vcpu_svm *svm = NULL;
5304
5305 if (e->type != KVM_IRQ_ROUTING_MSI)
5306 continue;
5307
5308 /**
5309 * Here, we setup with legacy mode in the following cases:
5310 * 1. When cannot target interrupt to a specific vcpu.
5311 * 2. Unsetting posted interrupt.
5312 * 3. APIC virtialization is disabled for the vcpu.
Alexander Graffdcf7562019-09-05 14:58:18 +02005313 * 4. IRQ has incompatible delivery mode (SMI, INIT, etc)
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005314 */
5315 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
5316 kvm_vcpu_apicv_active(&svm->vcpu)) {
5317 struct amd_iommu_pi_data pi;
5318
5319 /* Try to enable guest_mode in IRTE */
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005320 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
5321 AVIC_HPA_MASK);
Sean Christopherson81811c12018-03-20 12:17:21 -07005322 pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005323 svm->vcpu.vcpu_id);
5324 pi.is_guest_mode = true;
5325 pi.vcpu_data = &vcpu_info;
5326 ret = irq_set_vcpu_affinity(host_irq, &pi);
5327
5328 /**
5329 * Here, we successfully setting up vcpu affinity in
5330 * IOMMU guest mode. Now, we need to store the posted
5331 * interrupt information in a per-vcpu ir_list so that
5332 * we can reference to them directly when we update vcpu
5333 * scheduling information in IOMMU irte.
5334 */
5335 if (!ret && pi.is_guest_mode)
5336 svm_ir_list_add(svm, &pi);
5337 } else {
5338 /* Use legacy mode in IRTE */
5339 struct amd_iommu_pi_data pi;
5340
5341 /**
5342 * Here, pi is used to:
5343 * - Tell IOMMU to use legacy mode for this interrupt.
5344 * - Retrieve ga_tag of prior interrupt remapping data.
5345 */
5346 pi.is_guest_mode = false;
5347 ret = irq_set_vcpu_affinity(host_irq, &pi);
5348
5349 /**
5350 * Check if the posted interrupt was previously
5351 * setup with the guest_mode by checking if the ga_tag
5352 * was cached. If so, we need to clean up the per-vcpu
5353 * ir_list.
5354 */
5355 if (!ret && pi.prev_ga_tag) {
5356 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
5357 struct kvm_vcpu *vcpu;
5358
5359 vcpu = kvm_get_vcpu_by_id(kvm, id);
5360 if (vcpu)
5361 svm_ir_list_del(to_svm(vcpu), &pi);
5362 }
5363 }
5364
5365 if (!ret && svm) {
hu huajun2698d822018-04-11 15:16:40 +08005366 trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id,
5367 e->gsi, vcpu_info.vector,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005368 vcpu_info.pi_desc_addr, set);
5369 }
5370
5371 if (ret < 0) {
5372 pr_err("%s: failed to update PI IRTE\n", __func__);
5373 goto out;
5374 }
5375 }
5376
5377 ret = 0;
5378out:
5379 srcu_read_unlock(&kvm->irq_srcu, idx);
5380 return ret;
5381}
5382
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005383static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005384{
5385 struct vcpu_svm *svm = to_svm(vcpu);
5386 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02005387 int ret;
5388 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
5389 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
5390 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
5391
5392 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005393}
5394
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005395static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
5396{
5397 struct vcpu_svm *svm = to_svm(vcpu);
5398
5399 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
5400}
5401
5402static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5403{
5404 struct vcpu_svm *svm = to_svm(vcpu);
5405
5406 if (masked) {
5407 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005408 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005409 } else {
5410 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005411 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005412 }
5413}
5414
Gleb Natapov78646122009-03-23 12:12:11 +02005415static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
5416{
5417 struct vcpu_svm *svm = to_svm(vcpu);
5418 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005419 int ret;
5420
5421 if (!gif_set(svm) ||
5422 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
5423 return 0;
5424
Avi Kivityf6e78472010-08-02 15:30:20 +03005425 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005426
Joerg Roedel20307532010-11-29 17:51:48 +01005427 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005428 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
5429
5430 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02005431}
5432
Jan Kiszkac9a79532014-03-07 20:03:15 +01005433static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03005434{
Alexander Graf219b65d2009-06-15 15:21:25 +02005435 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02005436
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005437 if (kvm_vcpu_apicv_active(vcpu))
5438 return;
5439
Joerg Roedele0231712010-02-24 18:59:10 +01005440 /*
5441 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
5442 * 1, because that's a separate STGI/VMRUN intercept. The next time we
5443 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005444 * we'll get the vintr intercept. However, if the vGIF feature is
5445 * enabled, the STGI interception will not occur. Enable the irq
5446 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01005447 */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005448 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02005449 svm_set_vintr(svm);
5450 svm_inject_irq(svm, 0x0);
5451 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005452}
5453
Jan Kiszkac9a79532014-03-07 20:03:15 +01005454static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005455{
Avi Kivity04d2cc72007-09-10 18:10:54 +03005456 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005457
Gleb Natapov44c11432009-05-11 13:35:52 +03005458 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
5459 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01005460 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03005461
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005462 if (!gif_set(svm)) {
5463 if (vgif_enabled(svm))
5464 set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005465 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005466 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005467
5468 if (svm->nested.exit_required)
5469 return; /* we're not going to run the guest yet */
5470
Joerg Roedele0231712010-02-24 18:59:10 +01005471 /*
5472 * Something prevents NMI from been injected. Single step over possible
5473 * problem (IRET or exception injection or interrupt shadow)
5474 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02005475 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02005476 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03005477 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03005478}
5479
Izik Eiduscbc94022007-10-25 00:29:55 +02005480static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
5481{
5482 return 0;
5483}
5484
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07005485static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
5486{
5487 return 0;
5488}
5489
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005490static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Avi Kivityd9e368d2007-06-07 19:18:30 +03005491{
Joerg Roedel38e5e922010-12-03 15:25:16 +01005492 struct vcpu_svm *svm = to_svm(vcpu);
5493
5494 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
5495 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5496 else
5497 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03005498}
5499
Junaid Shahidfaff8752018-06-29 13:10:05 -07005500static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
5501{
5502 struct vcpu_svm *svm = to_svm(vcpu);
5503
5504 invlpga(gva, svm->vmcb->control.asid);
5505}
5506
Avi Kivity04d2cc72007-09-10 18:10:54 +03005507static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
5508{
5509}
5510
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005511static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
5512{
5513 struct vcpu_svm *svm = to_svm(vcpu);
5514
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005515 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005516 return;
5517
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005518 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005519 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03005520 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005521 }
5522}
5523
Joerg Roedel649d6862008-04-16 16:51:15 +02005524static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
5525{
5526 struct vcpu_svm *svm = to_svm(vcpu);
5527 u64 cr8;
5528
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005529 if (svm_nested_virtualize_tpr(vcpu) ||
5530 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005531 return;
5532
Joerg Roedel649d6862008-04-16 16:51:15 +02005533 cr8 = kvm_get_cr8(vcpu);
5534 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
5535 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
5536}
5537
Gleb Natapov9222be12009-04-23 17:14:37 +03005538static void svm_complete_interrupts(struct vcpu_svm *svm)
5539{
5540 u8 vector;
5541 int type;
5542 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01005543 unsigned int3_injected = svm->int3_injected;
5544
5545 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005546
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02005547 /*
5548 * If we've made progress since setting HF_IRET_MASK, we've
5549 * executed an IRET and can allow NMI injection.
5550 */
5551 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
5552 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03005553 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03005554 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5555 }
Gleb Natapov44c11432009-05-11 13:35:52 +03005556
Gleb Natapov9222be12009-04-23 17:14:37 +03005557 svm->vcpu.arch.nmi_injected = false;
5558 kvm_clear_exception_queue(&svm->vcpu);
5559 kvm_clear_interrupt_queue(&svm->vcpu);
5560
5561 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
5562 return;
5563
Avi Kivity3842d132010-07-27 12:30:24 +03005564 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5565
Gleb Natapov9222be12009-04-23 17:14:37 +03005566 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
5567 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
5568
5569 switch (type) {
5570 case SVM_EXITINTINFO_TYPE_NMI:
5571 svm->vcpu.arch.nmi_injected = true;
5572 break;
5573 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01005574 /*
5575 * In case of software exceptions, do not reinject the vector,
5576 * but re-execute the instruction instead. Rewind RIP first
5577 * if we emulated INT3 before.
5578 */
5579 if (kvm_exception_is_soft(vector)) {
5580 if (vector == BP_VECTOR && int3_injected &&
5581 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
5582 kvm_rip_write(&svm->vcpu,
5583 kvm_rip_read(&svm->vcpu) -
5584 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02005585 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01005586 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005587 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
5588 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005589 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03005590
5591 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005592 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03005593 break;
5594 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005595 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03005596 break;
5597 default:
5598 break;
5599 }
5600}
5601
Avi Kivityb463a6f2010-07-20 15:06:17 +03005602static void svm_cancel_injection(struct kvm_vcpu *vcpu)
5603{
5604 struct vcpu_svm *svm = to_svm(vcpu);
5605 struct vmcb_control_area *control = &svm->vmcb->control;
5606
5607 control->exit_int_info = control->event_inj;
5608 control->exit_int_info_err = control->event_inj_err;
5609 control->event_inj = 0;
5610 svm_complete_interrupts(svm);
5611}
5612
Avi Kivity851ba692009-08-24 11:10:17 +03005613static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005614{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005615 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03005616
Joerg Roedel2041a062010-04-22 12:33:08 +02005617 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5618 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5619 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5620
Joerg Roedelcd3ff652009-10-09 16:08:26 +02005621 /*
5622 * A vmexit emulation is required before the vcpu can be executed
5623 * again.
5624 */
5625 if (unlikely(svm->nested.exit_required))
5626 return;
5627
Ladi Proseka12713c2017-06-21 09:07:00 +02005628 /*
5629 * Disable singlestep if we're injecting an interrupt/exception.
5630 * We don't want our modified rflags to be pushed on the stack where
5631 * we might not be able to easily reset them if we disabled NMI
5632 * singlestep later.
5633 */
5634 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5635 /*
5636 * Event injection happens before external interrupts cause a
5637 * vmexit and interrupts are disabled here, so smp_send_reschedule
5638 * is enough to force an immediate vmexit.
5639 */
5640 disable_nmi_singlestep(svm);
5641 smp_send_reschedule(vcpu->cpu);
5642 }
5643
Rusty Russelle756fc62007-07-30 20:07:08 +10005644 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005645
Joerg Roedel649d6862008-04-16 16:51:15 +02005646 sync_lapic_to_cr8(vcpu);
5647
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02005648 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005649
Avi Kivity04d2cc72007-09-10 18:10:54 +03005650 clgi();
Aaron Lewis139a12c2019-10-21 16:30:25 -07005651 kvm_load_guest_xsave_state(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03005652
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08005653 if (lapic_in_kernel(vcpu) &&
5654 vcpu->arch.apic->lapic_timer.timer_advance_ns)
5655 kvm_wait_lapic_expire(vcpu);
5656
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005657 /*
5658 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
5659 * it's non-zero. Since vmentry is serialising on affected CPUs, there
5660 * is no need to worry about the conditional branch over the wrmsr
5661 * being speculatively taken.
5662 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02005663 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005664
Thomas Gleixner024d83c2018-08-12 20:41:45 +02005665 local_irq_enable();
5666
Avi Kivity6aa8b732006-12-10 02:21:36 -08005667 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03005668 "push %%" _ASM_BP "; \n\t"
5669 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5670 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5671 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5672 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5673 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5674 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005675#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005676 "mov %c[r8](%[svm]), %%r8 \n\t"
5677 "mov %c[r9](%[svm]), %%r9 \n\t"
5678 "mov %c[r10](%[svm]), %%r10 \n\t"
5679 "mov %c[r11](%[svm]), %%r11 \n\t"
5680 "mov %c[r12](%[svm]), %%r12 \n\t"
5681 "mov %c[r13](%[svm]), %%r13 \n\t"
5682 "mov %c[r14](%[svm]), %%r14 \n\t"
5683 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005684#endif
5685
Avi Kivity6aa8b732006-12-10 02:21:36 -08005686 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03005687 "push %%" _ASM_AX " \n\t"
5688 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Uros Bizjakac5ffda22018-11-26 17:00:08 +01005689 __ex("vmload %%" _ASM_AX) "\n\t"
5690 __ex("vmrun %%" _ASM_AX) "\n\t"
5691 __ex("vmsave %%" _ASM_AX) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005692 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005693
5694 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03005695 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5696 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5697 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5698 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5699 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5700 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005701#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005702 "mov %%r8, %c[r8](%[svm]) \n\t"
5703 "mov %%r9, %c[r9](%[svm]) \n\t"
5704 "mov %%r10, %c[r10](%[svm]) \n\t"
5705 "mov %%r11, %c[r11](%[svm]) \n\t"
5706 "mov %%r12, %c[r12](%[svm]) \n\t"
5707 "mov %%r13, %c[r13](%[svm]) \n\t"
5708 "mov %%r14, %c[r14](%[svm]) \n\t"
5709 "mov %%r15, %c[r15](%[svm]) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08005710 /*
5711 * Clear host registers marked as clobbered to prevent
5712 * speculative use.
5713 */
Uros Bizjak43ce76c2018-10-17 16:46:57 +02005714 "xor %%r8d, %%r8d \n\t"
5715 "xor %%r9d, %%r9d \n\t"
5716 "xor %%r10d, %%r10d \n\t"
5717 "xor %%r11d, %%r11d \n\t"
5718 "xor %%r12d, %%r12d \n\t"
5719 "xor %%r13d, %%r13d \n\t"
5720 "xor %%r14d, %%r14d \n\t"
5721 "xor %%r15d, %%r15d \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08005722#endif
Uros Bizjak43ce76c2018-10-17 16:46:57 +02005723 "xor %%ebx, %%ebx \n\t"
5724 "xor %%ecx, %%ecx \n\t"
5725 "xor %%edx, %%edx \n\t"
5726 "xor %%esi, %%esi \n\t"
5727 "xor %%edi, %%edi \n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005728 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08005729 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005730 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08005731 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005732 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5733 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5734 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5735 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5736 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5737 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005738#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005739 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5740 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5741 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5742 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5743 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5744 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5745 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5746 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005747#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02005748 : "cc", "memory"
5749#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03005750 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005751 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03005752#else
5753 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005754#endif
5755 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08005756
Thomas Gleixner15e6c222018-05-11 15:21:01 +02005757 /* Eliminate branch target predictions from guest mode */
5758 vmexit_fill_RSB();
5759
5760#ifdef CONFIG_X86_64
5761 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5762#else
5763 loadsegment(fs, svm->host.fs);
5764#ifndef CONFIG_X86_32_LAZY_GS
5765 loadsegment(gs, svm->host.gs);
5766#endif
5767#endif
5768
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005769 /*
5770 * We do not use IBRS in the kernel. If this vCPU has used the
5771 * SPEC_CTRL MSR it may have left it on; save the value and
5772 * turn it off. This is much more efficient than blindly adding
5773 * it to the atomic save/restore list. Especially as the former
5774 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
5775 *
5776 * For non-nested case:
5777 * If the L01 MSR bitmap does not intercept the MSR, then we need to
5778 * save it.
5779 *
5780 * For nested case:
5781 * If the L02 MSR bitmap does not intercept the MSR, then we need to
5782 * save it.
5783 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01005784 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005785 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005786
Avi Kivity6aa8b732006-12-10 02:21:36 -08005787 reload_tss(vcpu);
5788
Avi Kivity56ba47d2007-11-07 17:14:18 +02005789 local_irq_disable();
5790
Thomas Gleixner024d83c2018-08-12 20:41:45 +02005791 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
5792
Avi Kivity13c34e02010-10-21 12:20:31 +02005793 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5794 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5795 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5796 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5797
Joerg Roedel3781c012011-01-14 16:45:02 +01005798 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Andi Kleendd60d212017-07-25 17:20:32 -07005799 kvm_before_interrupt(&svm->vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01005800
Aaron Lewis139a12c2019-10-21 16:30:25 -07005801 kvm_load_host_xsave_state(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01005802 stgi();
5803
5804 /* Any pending NMI will happen here */
5805
5806 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Andi Kleendd60d212017-07-25 17:20:32 -07005807 kvm_after_interrupt(&svm->vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01005808
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005809 sync_cr8_to_lapic(vcpu);
5810
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005811 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005812
Joerg Roedel38e5e922010-12-03 15:25:16 +01005813 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5814
Gleb Natapov631bc482010-10-14 11:22:52 +02005815 /* if exit due to PF check for async PF */
5816 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005817 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
Gleb Natapov631bc482010-10-14 11:22:52 +02005818
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005819 if (npt_enabled) {
5820 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5821 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5822 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02005823
5824 /*
5825 * We need to handle MC intercepts here before the vcpu has a chance to
5826 * change the physical cpu
5827 */
5828 if (unlikely(svm->vmcb->control.exit_code ==
5829 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5830 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01005831
5832 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005833}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005834STACK_FRAME_NON_STANDARD(svm_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005835
Avi Kivity6aa8b732006-12-10 02:21:36 -08005836static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5837{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005838 struct vcpu_svm *svm = to_svm(vcpu);
5839
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005840 svm->vmcb->save.cr3 = __sme_set(root);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005841 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005842}
5843
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005844static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5845{
5846 struct vcpu_svm *svm = to_svm(vcpu);
5847
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005848 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01005849 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005850
5851 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02005852 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005853 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005854}
5855
Avi Kivity6aa8b732006-12-10 02:21:36 -08005856static int is_disabled(void)
5857{
Joerg Roedel6031a612007-06-22 12:29:50 +03005858 u64 vm_cr;
5859
5860 rdmsrl(MSR_VM_CR, vm_cr);
5861 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5862 return 1;
5863
Avi Kivity6aa8b732006-12-10 02:21:36 -08005864 return 0;
5865}
5866
Ingo Molnar102d8322007-02-19 14:37:47 +02005867static void
5868svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5869{
5870 /*
5871 * Patch in the VMMCALL instruction:
5872 */
5873 hypercall[0] = 0x0f;
5874 hypercall[1] = 0x01;
5875 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02005876}
5877
Sean Christophersonf257d6d2019-04-19 22:18:17 -07005878static int __init svm_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03005879{
Sean Christophersonf257d6d2019-04-19 22:18:17 -07005880 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03005881}
5882
Avi Kivity774ead32007-12-26 13:57:04 +02005883static bool svm_cpu_has_accelerated_tpr(void)
5884{
5885 return false;
5886}
5887
Tom Lendackybc226f02018-05-10 22:06:39 +02005888static bool svm_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005889{
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01005890 switch (index) {
5891 case MSR_IA32_MCG_EXT_CTL:
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02005892 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01005893 return false;
5894 default:
5895 break;
5896 }
5897
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005898 return true;
5899}
5900
Paolo Bonzinifc07e762015-10-01 13:20:22 +02005901static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5902{
5903 return 0;
5904}
5905
Sheng Yang0e851882009-12-18 16:48:46 +08005906static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5907{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005908 struct vcpu_svm *svm = to_svm(vcpu);
5909
Aaron Lewis72041602019-10-21 16:30:20 -07005910 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5911 boot_cpu_has(X86_FEATURE_XSAVES);
5912
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005913 /* Update nrips enabled cache */
Radim Krčmářd6321d42017-08-05 00:12:49 +02005914 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005915
5916 if (!kvm_vcpu_apicv_active(vcpu))
5917 return;
5918
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02005919 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08005920}
5921
Paolo Bonzini50896de2019-08-15 09:17:31 +02005922#define F(x) bit(X86_FEATURE_##x)
5923
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005924static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5925{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005926 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005927 case 0x1:
5928 if (avic)
5929 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5930 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02005931 case 0x80000001:
5932 if (nested)
5933 entry->ecx |= (1 << 2); /* Set SVM bit */
5934 break;
Paolo Bonzini50896de2019-08-15 09:17:31 +02005935 case 0x80000008:
5936 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
5937 boot_cpu_has(X86_FEATURE_AMD_SSBD))
5938 entry->ebx |= F(VIRT_SSBD);
5939 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005940 case 0x8000000A:
5941 entry->eax = 1; /* SVM revision 1 */
5942 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5943 ASID emulation to nested SVM */
5944 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02005945 entry->edx = 0; /* Per default do not support any
5946 additional features */
5947
5948 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02005949 if (boot_cpu_has(X86_FEATURE_NRIPS))
Paolo Bonzini50896de2019-08-15 09:17:31 +02005950 entry->edx |= F(NRIPS);
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005951
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005952 /* Support NPT for the guest if enabled */
5953 if (npt_enabled)
Paolo Bonzini50896de2019-08-15 09:17:31 +02005954 entry->edx |= F(NPT);
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005955
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005956 break;
Brijesh Singh8765d752017-12-04 10:57:25 -06005957 case 0x8000001F:
5958 /* Support memory encryption cpuid if host supports it */
5959 if (boot_cpu_has(X86_FEATURE_SEV))
5960 cpuid(0x8000001f, &entry->eax, &entry->ebx,
5961 &entry->ecx, &entry->edx);
5962
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005963 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005964}
5965
Sheng Yang17cc3932010-01-05 19:02:27 +08005966static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005967{
Sheng Yang17cc3932010-01-05 19:02:27 +08005968 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005969}
5970
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005971static bool svm_rdtscp_supported(void)
5972{
Paolo Bonzini46896c72015-11-12 14:49:16 +01005973 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005974}
5975
Mao, Junjiead756a12012-07-02 01:18:48 +00005976static bool svm_invpcid_supported(void)
5977{
5978 return false;
5979}
5980
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005981static bool svm_mpx_supported(void)
5982{
5983 return false;
5984}
5985
Wanpeng Li55412b22014-12-02 19:21:30 +08005986static bool svm_xsaves_supported(void)
5987{
Aaron Lewis52297432019-10-21 16:30:27 -07005988 return boot_cpu_has(X86_FEATURE_XSAVES);
Wanpeng Li55412b22014-12-02 19:21:30 +08005989}
5990
Paolo Bonzini66336ca2016-07-12 10:36:41 +02005991static bool svm_umip_emulated(void)
5992{
5993 return false;
5994}
5995
Chao Peng86f52012018-10-24 16:05:11 +08005996static bool svm_pt_supported(void)
5997{
5998 return false;
5999}
6000
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006001static bool svm_has_wbinvd_exit(void)
6002{
6003 return true;
6004}
6005
Joerg Roedel80612522011-04-04 12:39:33 +02006006#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03006007 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006008#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03006009 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02006010#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03006011 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006012
Mathias Krause09941fb2012-08-30 01:30:20 +02006013static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006014 u32 exit_code;
6015 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006016} x86_intercept_map[] = {
6017 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
6018 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
6019 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
6020 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
6021 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02006022 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
6023 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02006024 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
6025 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
6026 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
6027 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
6028 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
6029 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
6030 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
6031 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02006032 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
6033 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
6034 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
6035 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
6036 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
6037 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
6038 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
6039 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02006040 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
6041 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
6042 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02006043 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
6044 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
6045 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
6046 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
6047 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
6048 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
6049 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
6050 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
6051 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02006052 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
6053 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
6054 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
6055 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
6056 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
6057 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
6058 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02006059 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
6060 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
6061 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
6062 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02006063 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006064};
6065
Joerg Roedel80612522011-04-04 12:39:33 +02006066#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006067#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02006068#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006069
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006070static int svm_check_intercept(struct kvm_vcpu *vcpu,
6071 struct x86_instruction_info *info,
6072 enum x86_intercept_stage stage)
6073{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006074 struct vcpu_svm *svm = to_svm(vcpu);
6075 int vmexit, ret = X86EMUL_CONTINUE;
6076 struct __x86_intercept icpt_info;
6077 struct vmcb *vmcb = svm->vmcb;
6078
6079 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
6080 goto out;
6081
6082 icpt_info = x86_intercept_map[info->intercept];
6083
Avi Kivity40e19b52011-04-21 12:35:41 +03006084 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006085 goto out;
6086
6087 switch (icpt_info.exit_code) {
6088 case SVM_EXIT_READ_CR0:
6089 if (info->intercept == x86_intercept_cr_read)
6090 icpt_info.exit_code += info->modrm_reg;
6091 break;
6092 case SVM_EXIT_WRITE_CR0: {
6093 unsigned long cr0, val;
6094 u64 intercept;
6095
6096 if (info->intercept == x86_intercept_cr_write)
6097 icpt_info.exit_code += info->modrm_reg;
6098
Jan Kiszka62baf442014-06-29 21:55:53 +02006099 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
6100 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006101 break;
6102
6103 intercept = svm->nested.intercept;
6104
6105 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
6106 break;
6107
6108 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
6109 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
6110
6111 if (info->intercept == x86_intercept_lmsw) {
6112 cr0 &= 0xfUL;
6113 val &= 0xfUL;
6114 /* lmsw can't clear PE - catch this here */
6115 if (cr0 & X86_CR0_PE)
6116 val |= X86_CR0_PE;
6117 }
6118
6119 if (cr0 ^ val)
6120 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
6121
6122 break;
6123 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02006124 case SVM_EXIT_READ_DR0:
6125 case SVM_EXIT_WRITE_DR0:
6126 icpt_info.exit_code += info->modrm_reg;
6127 break;
Joerg Roedel80612522011-04-04 12:39:33 +02006128 case SVM_EXIT_MSR:
6129 if (info->intercept == x86_intercept_wrmsr)
6130 vmcb->control.exit_info_1 = 1;
6131 else
6132 vmcb->control.exit_info_1 = 0;
6133 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02006134 case SVM_EXIT_PAUSE:
6135 /*
6136 * We get this for NOP only, but pause
6137 * is rep not, check this here
6138 */
6139 if (info->rep_prefix != REPE_PREFIX)
6140 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02006141 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02006142 case SVM_EXIT_IOIO: {
6143 u64 exit_info;
6144 u32 bytes;
6145
Joerg Roedelf6511932011-04-04 12:39:35 +02006146 if (info->intercept == x86_intercept_in ||
6147 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02006148 exit_info = ((info->src_val & 0xffff) << 16) |
6149 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02006150 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02006151 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02006152 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02006153 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02006154 }
6155
6156 if (info->intercept == x86_intercept_outs ||
6157 info->intercept == x86_intercept_ins)
6158 exit_info |= SVM_IOIO_STR_MASK;
6159
6160 if (info->rep_prefix)
6161 exit_info |= SVM_IOIO_REP_MASK;
6162
6163 bytes = min(bytes, 4u);
6164
6165 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
6166
6167 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
6168
6169 vmcb->control.exit_info_1 = exit_info;
6170 vmcb->control.exit_info_2 = info->next_rip;
6171
6172 break;
6173 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006174 default:
6175 break;
6176 }
6177
Bandan Dasf1047652015-06-11 02:05:33 -04006178 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
6179 if (static_cpu_has(X86_FEATURE_NRIPS))
6180 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006181 vmcb->control.exit_code = icpt_info.exit_code;
6182 vmexit = nested_svm_exit_handled(svm);
6183
6184 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
6185 : X86EMUL_CONTINUE;
6186
6187out:
6188 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006189}
6190
Sean Christopherson95b5a482019-04-19 22:50:59 -07006191static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08006192{
Sean Christophersond7a08882019-07-10 09:07:34 -07006193
Yang Zhanga547c6d2013-04-11 19:25:10 +08006194}
6195
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006196static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
6197{
Babu Moger8566ac82018-03-16 16:37:26 -04006198 if (pause_filter_thresh)
6199 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006200}
6201
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05006202static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
6203{
6204 if (avic_handle_apic_id_update(vcpu) != 0)
6205 return;
Suthikulpanit, Suravee98d90582019-01-29 08:08:42 +00006206 avic_handle_dfr_update(vcpu);
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05006207 avic_handle_ldr_update(vcpu);
6208}
6209
Borislav Petkov74f16902017-03-26 23:51:24 +02006210static void svm_setup_mce(struct kvm_vcpu *vcpu)
6211{
6212 /* [63:9] are reserved. */
6213 vcpu->arch.mcg_cap &= 0x1ff;
6214}
6215
Ladi Prosek72d7b372017-10-11 16:54:41 +02006216static int svm_smi_allowed(struct kvm_vcpu *vcpu)
6217{
Ladi Prosek05cade72017-10-11 16:54:45 +02006218 struct vcpu_svm *svm = to_svm(vcpu);
6219
6220 /* Per APM Vol.2 15.22.2 "Response to SMI" */
6221 if (!gif_set(svm))
6222 return 0;
6223
6224 if (is_guest_mode(&svm->vcpu) &&
6225 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
6226 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
6227 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
6228 svm->nested.exit_required = true;
6229 return 0;
6230 }
6231
Ladi Prosek72d7b372017-10-11 16:54:41 +02006232 return 1;
6233}
6234
Ladi Prosek0234bf82017-10-11 16:54:40 +02006235static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
6236{
Ladi Prosek05cade72017-10-11 16:54:45 +02006237 struct vcpu_svm *svm = to_svm(vcpu);
6238 int ret;
6239
6240 if (is_guest_mode(vcpu)) {
6241 /* FED8h - SVM Guest */
6242 put_smstate(u64, smstate, 0x7ed8, 1);
6243 /* FEE0h - SVM Guest VMCB Physical Address */
6244 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
6245
6246 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
6247 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
6248 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
6249
6250 ret = nested_svm_vmexit(svm);
6251 if (ret)
6252 return ret;
6253 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02006254 return 0;
6255}
6256
Sean Christophersoned193212019-04-02 08:03:09 -07006257static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02006258{
Ladi Prosek05cade72017-10-11 16:54:45 +02006259 struct vcpu_svm *svm = to_svm(vcpu);
6260 struct vmcb *nested_vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01006261 struct kvm_host_map map;
Sean Christophersoned193212019-04-02 08:03:09 -07006262 u64 guest;
6263 u64 vmcb;
Ladi Prosek05cade72017-10-11 16:54:45 +02006264
Sean Christophersoned193212019-04-02 08:03:09 -07006265 guest = GET_SMSTATE(u64, smstate, 0x7ed8);
6266 vmcb = GET_SMSTATE(u64, smstate, 0x7ee0);
Ladi Prosek05cade72017-10-11 16:54:45 +02006267
Sean Christophersoned193212019-04-02 08:03:09 -07006268 if (guest) {
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01006269 if (kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb), &map) == -EINVAL)
Sean Christopherson9ec19492019-04-02 08:03:11 -07006270 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01006271 nested_vmcb = map.hva;
6272 enter_svm_guest_mode(svm, vmcb, nested_vmcb, &map);
Ladi Prosek05cade72017-10-11 16:54:45 +02006273 }
Sean Christopherson9ec19492019-04-02 08:03:11 -07006274 return 0;
Ladi Prosek0234bf82017-10-11 16:54:40 +02006275}
6276
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006277static int enable_smi_window(struct kvm_vcpu *vcpu)
6278{
6279 struct vcpu_svm *svm = to_svm(vcpu);
6280
6281 if (!gif_set(svm)) {
6282 if (vgif_enabled(svm))
6283 set_intercept(svm, INTERCEPT_STGI);
6284 /* STGI will cause a vm exit */
6285 return 1;
6286 }
6287 return 0;
6288}
6289
Tom Lendacky33af3a72019-10-03 21:17:48 +00006290static int sev_flush_asids(void)
6291{
6292 int ret, error;
6293
6294 /*
6295 * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail,
6296 * so it must be guarded.
6297 */
6298 down_write(&sev_deactivate_lock);
6299
6300 wbinvd_on_all_cpus();
6301 ret = sev_guest_df_flush(&error);
6302
6303 up_write(&sev_deactivate_lock);
6304
6305 if (ret)
6306 pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error);
6307
6308 return ret;
6309}
6310
6311/* Must be called with the sev_bitmap_lock held */
6312static bool __sev_recycle_asids(void)
6313{
6314 int pos;
6315
6316 /* Check if there are any ASIDs to reclaim before performing a flush */
6317 pos = find_next_bit(sev_reclaim_asid_bitmap,
6318 max_sev_asid, min_sev_asid - 1);
6319 if (pos >= max_sev_asid)
6320 return false;
6321
6322 if (sev_flush_asids())
6323 return false;
6324
6325 bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap,
6326 max_sev_asid);
6327 bitmap_zero(sev_reclaim_asid_bitmap, max_sev_asid);
6328
6329 return true;
6330}
6331
Brijesh Singh1654efc2017-12-04 10:57:34 -06006332static int sev_asid_new(void)
6333{
Tom Lendacky33af3a72019-10-03 21:17:48 +00006334 bool retry = true;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006335 int pos;
6336
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00006337 mutex_lock(&sev_bitmap_lock);
6338
Brijesh Singh1654efc2017-12-04 10:57:34 -06006339 /*
6340 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
6341 */
Tom Lendacky33af3a72019-10-03 21:17:48 +00006342again:
Brijesh Singh1654efc2017-12-04 10:57:34 -06006343 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00006344 if (pos >= max_sev_asid) {
Tom Lendacky33af3a72019-10-03 21:17:48 +00006345 if (retry && __sev_recycle_asids()) {
6346 retry = false;
6347 goto again;
6348 }
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00006349 mutex_unlock(&sev_bitmap_lock);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006350 return -EBUSY;
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00006351 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06006352
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00006353 __set_bit(pos, sev_asid_bitmap);
6354
6355 mutex_unlock(&sev_bitmap_lock);
6356
Brijesh Singh1654efc2017-12-04 10:57:34 -06006357 return pos + 1;
6358}
6359
6360static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
6361{
Sean Christopherson81811c12018-03-20 12:17:21 -07006362 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006363 int asid, ret;
6364
6365 ret = -EBUSY;
David Rientjes3f14a892019-01-02 12:56:33 -08006366 if (unlikely(sev->active))
6367 return ret;
6368
Brijesh Singh1654efc2017-12-04 10:57:34 -06006369 asid = sev_asid_new();
6370 if (asid < 0)
6371 return ret;
6372
6373 ret = sev_platform_init(&argp->error);
6374 if (ret)
6375 goto e_free;
6376
6377 sev->active = true;
6378 sev->asid = asid;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006379 INIT_LIST_HEAD(&sev->regions_list);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006380
6381 return 0;
6382
6383e_free:
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00006384 sev_asid_free(asid);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006385 return ret;
6386}
6387
Brijesh Singh59414c92017-12-04 10:57:35 -06006388static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
6389{
6390 struct sev_data_activate *data;
6391 int asid = sev_get_asid(kvm);
6392 int ret;
6393
Ben Gardon1ec69642019-02-11 11:02:51 -08006394 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh59414c92017-12-04 10:57:35 -06006395 if (!data)
6396 return -ENOMEM;
6397
6398 /* activate ASID on the given handle */
6399 data->handle = handle;
6400 data->asid = asid;
6401 ret = sev_guest_activate(data, error);
6402 kfree(data);
6403
6404 return ret;
6405}
6406
Brijesh Singh89c50582017-12-04 10:57:35 -06006407static int __sev_issue_cmd(int fd, int id, void *data, int *error)
Brijesh Singh59414c92017-12-04 10:57:35 -06006408{
6409 struct fd f;
6410 int ret;
6411
6412 f = fdget(fd);
6413 if (!f.file)
6414 return -EBADF;
6415
6416 ret = sev_issue_cmd_external_user(f.file, id, data, error);
6417
6418 fdput(f);
6419 return ret;
6420}
6421
Brijesh Singh89c50582017-12-04 10:57:35 -06006422static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
6423{
Sean Christopherson81811c12018-03-20 12:17:21 -07006424 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06006425
6426 return __sev_issue_cmd(sev->fd, id, data, error);
6427}
6428
Brijesh Singh59414c92017-12-04 10:57:35 -06006429static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
6430{
Sean Christopherson81811c12018-03-20 12:17:21 -07006431 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh59414c92017-12-04 10:57:35 -06006432 struct sev_data_launch_start *start;
6433 struct kvm_sev_launch_start params;
6434 void *dh_blob, *session_blob;
6435 int *error = &argp->error;
6436 int ret;
6437
6438 if (!sev_guest(kvm))
6439 return -ENOTTY;
6440
6441 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6442 return -EFAULT;
6443
Ben Gardon1ec69642019-02-11 11:02:51 -08006444 start = kzalloc(sizeof(*start), GFP_KERNEL_ACCOUNT);
Brijesh Singh59414c92017-12-04 10:57:35 -06006445 if (!start)
6446 return -ENOMEM;
6447
6448 dh_blob = NULL;
6449 if (params.dh_uaddr) {
6450 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
6451 if (IS_ERR(dh_blob)) {
6452 ret = PTR_ERR(dh_blob);
6453 goto e_free;
6454 }
6455
6456 start->dh_cert_address = __sme_set(__pa(dh_blob));
6457 start->dh_cert_len = params.dh_len;
6458 }
6459
6460 session_blob = NULL;
6461 if (params.session_uaddr) {
6462 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
6463 if (IS_ERR(session_blob)) {
6464 ret = PTR_ERR(session_blob);
6465 goto e_free_dh;
6466 }
6467
6468 start->session_address = __sme_set(__pa(session_blob));
6469 start->session_len = params.session_len;
6470 }
6471
6472 start->handle = params.handle;
6473 start->policy = params.policy;
6474
6475 /* create memory encryption context */
Brijesh Singh89c50582017-12-04 10:57:35 -06006476 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
Brijesh Singh59414c92017-12-04 10:57:35 -06006477 if (ret)
6478 goto e_free_session;
6479
6480 /* Bind ASID to this guest */
6481 ret = sev_bind_asid(kvm, start->handle, error);
6482 if (ret)
6483 goto e_free_session;
6484
6485 /* return handle to userspace */
6486 params.handle = start->handle;
6487 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
6488 sev_unbind_asid(kvm, start->handle);
6489 ret = -EFAULT;
6490 goto e_free_session;
6491 }
6492
6493 sev->handle = start->handle;
6494 sev->fd = argp->sev_fd;
6495
6496e_free_session:
6497 kfree(session_blob);
6498e_free_dh:
6499 kfree(dh_blob);
6500e_free:
6501 kfree(start);
6502 return ret;
6503}
6504
David Rientjesede885e2019-03-19 15:19:56 -07006505static unsigned long get_num_contig_pages(unsigned long idx,
6506 struct page **inpages, unsigned long npages)
Brijesh Singh89c50582017-12-04 10:57:35 -06006507{
6508 unsigned long paddr, next_paddr;
David Rientjesede885e2019-03-19 15:19:56 -07006509 unsigned long i = idx + 1, pages = 1;
Brijesh Singh89c50582017-12-04 10:57:35 -06006510
6511 /* find the number of contiguous pages starting from idx */
6512 paddr = __sme_page_pa(inpages[idx]);
6513 while (i < npages) {
6514 next_paddr = __sme_page_pa(inpages[i++]);
6515 if ((paddr + PAGE_SIZE) == next_paddr) {
6516 pages++;
6517 paddr = next_paddr;
6518 continue;
6519 }
6520 break;
6521 }
6522
6523 return pages;
6524}
6525
6526static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
6527{
David Rientjesede885e2019-03-19 15:19:56 -07006528 unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i;
Sean Christopherson81811c12018-03-20 12:17:21 -07006529 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06006530 struct kvm_sev_launch_update_data params;
6531 struct sev_data_launch_update_data *data;
6532 struct page **inpages;
David Rientjesede885e2019-03-19 15:19:56 -07006533 int ret;
Brijesh Singh89c50582017-12-04 10:57:35 -06006534
6535 if (!sev_guest(kvm))
6536 return -ENOTTY;
6537
6538 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6539 return -EFAULT;
6540
Ben Gardon1ec69642019-02-11 11:02:51 -08006541 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh89c50582017-12-04 10:57:35 -06006542 if (!data)
6543 return -ENOMEM;
6544
6545 vaddr = params.uaddr;
6546 size = params.len;
6547 vaddr_end = vaddr + size;
6548
6549 /* Lock the user memory. */
6550 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
6551 if (!inpages) {
6552 ret = -ENOMEM;
6553 goto e_free;
6554 }
6555
6556 /*
6557 * The LAUNCH_UPDATE command will perform in-place encryption of the
6558 * memory content (i.e it will write the same memory region with C=1).
6559 * It's possible that the cache may contain the data with C=0, i.e.,
6560 * unencrypted so invalidate it first.
6561 */
6562 sev_clflush_pages(inpages, npages);
6563
6564 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
6565 int offset, len;
6566
6567 /*
6568 * If the user buffer is not page-aligned, calculate the offset
6569 * within the page.
6570 */
6571 offset = vaddr & (PAGE_SIZE - 1);
6572
6573 /* Calculate the number of pages that can be encrypted in one go. */
6574 pages = get_num_contig_pages(i, inpages, npages);
6575
6576 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
6577
6578 data->handle = sev->handle;
6579 data->len = len;
6580 data->address = __sme_page_pa(inpages[i]) + offset;
6581 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
6582 if (ret)
6583 goto e_unpin;
6584
6585 size -= len;
6586 next_vaddr = vaddr + len;
6587 }
6588
6589e_unpin:
6590 /* content of memory is updated, mark pages dirty */
6591 for (i = 0; i < npages; i++) {
6592 set_page_dirty_lock(inpages[i]);
6593 mark_page_accessed(inpages[i]);
6594 }
6595 /* unlock the user pages */
6596 sev_unpin_memory(kvm, inpages, npages);
6597e_free:
6598 kfree(data);
6599 return ret;
6600}
6601
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006602static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
6603{
Brijesh Singh3e233382018-02-23 12:36:50 -06006604 void __user *measure = (void __user *)(uintptr_t)argp->data;
Sean Christopherson81811c12018-03-20 12:17:21 -07006605 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006606 struct sev_data_launch_measure *data;
6607 struct kvm_sev_launch_measure params;
Brijesh Singh3e233382018-02-23 12:36:50 -06006608 void __user *p = NULL;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006609 void *blob = NULL;
6610 int ret;
6611
6612 if (!sev_guest(kvm))
6613 return -ENOTTY;
6614
Brijesh Singh3e233382018-02-23 12:36:50 -06006615 if (copy_from_user(&params, measure, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006616 return -EFAULT;
6617
Ben Gardon1ec69642019-02-11 11:02:51 -08006618 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006619 if (!data)
6620 return -ENOMEM;
6621
6622 /* User wants to query the blob length */
6623 if (!params.len)
6624 goto cmd;
6625
Brijesh Singh3e233382018-02-23 12:36:50 -06006626 p = (void __user *)(uintptr_t)params.uaddr;
6627 if (p) {
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006628 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
6629 ret = -EINVAL;
6630 goto e_free;
6631 }
6632
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006633 ret = -ENOMEM;
6634 blob = kmalloc(params.len, GFP_KERNEL);
6635 if (!blob)
6636 goto e_free;
6637
6638 data->address = __psp_pa(blob);
6639 data->len = params.len;
6640 }
6641
6642cmd:
6643 data->handle = sev->handle;
6644 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
6645
6646 /*
6647 * If we query the session length, FW responded with expected data.
6648 */
6649 if (!params.len)
6650 goto done;
6651
6652 if (ret)
6653 goto e_free_blob;
6654
6655 if (blob) {
Brijesh Singh3e233382018-02-23 12:36:50 -06006656 if (copy_to_user(p, blob, params.len))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006657 ret = -EFAULT;
6658 }
6659
6660done:
6661 params.len = data->len;
Brijesh Singh3e233382018-02-23 12:36:50 -06006662 if (copy_to_user(measure, &params, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006663 ret = -EFAULT;
6664e_free_blob:
6665 kfree(blob);
6666e_free:
6667 kfree(data);
6668 return ret;
6669}
6670
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006671static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
6672{
Sean Christopherson81811c12018-03-20 12:17:21 -07006673 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006674 struct sev_data_launch_finish *data;
6675 int ret;
6676
6677 if (!sev_guest(kvm))
6678 return -ENOTTY;
6679
Ben Gardon1ec69642019-02-11 11:02:51 -08006680 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006681 if (!data)
6682 return -ENOMEM;
6683
6684 data->handle = sev->handle;
6685 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
6686
6687 kfree(data);
6688 return ret;
6689}
6690
Brijesh Singh255d9e72017-12-04 10:57:37 -06006691static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
6692{
Sean Christopherson81811c12018-03-20 12:17:21 -07006693 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006694 struct kvm_sev_guest_status params;
6695 struct sev_data_guest_status *data;
6696 int ret;
6697
6698 if (!sev_guest(kvm))
6699 return -ENOTTY;
6700
Ben Gardon1ec69642019-02-11 11:02:51 -08006701 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh255d9e72017-12-04 10:57:37 -06006702 if (!data)
6703 return -ENOMEM;
6704
6705 data->handle = sev->handle;
6706 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
6707 if (ret)
6708 goto e_free;
6709
6710 params.policy = data->policy;
6711 params.state = data->state;
6712 params.handle = data->handle;
6713
6714 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6715 ret = -EFAULT;
6716e_free:
6717 kfree(data);
6718 return ret;
6719}
6720
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006721static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
6722 unsigned long dst, int size,
6723 int *error, bool enc)
6724{
Sean Christopherson81811c12018-03-20 12:17:21 -07006725 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006726 struct sev_data_dbg *data;
6727 int ret;
6728
Ben Gardon1ec69642019-02-11 11:02:51 -08006729 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006730 if (!data)
6731 return -ENOMEM;
6732
6733 data->handle = sev->handle;
6734 data->dst_addr = dst;
6735 data->src_addr = src;
6736 data->len = size;
6737
6738 ret = sev_issue_cmd(kvm,
6739 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
6740 data, error);
6741 kfree(data);
6742 return ret;
6743}
6744
6745static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
6746 unsigned long dst_paddr, int sz, int *err)
6747{
6748 int offset;
6749
6750 /*
6751 * Its safe to read more than we are asked, caller should ensure that
6752 * destination has enough space.
6753 */
6754 src_paddr = round_down(src_paddr, 16);
6755 offset = src_paddr & 15;
6756 sz = round_up(sz + offset, 16);
6757
6758 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
6759}
6760
6761static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
6762 unsigned long __user dst_uaddr,
6763 unsigned long dst_paddr,
6764 int size, int *err)
6765{
6766 struct page *tpage = NULL;
6767 int ret, offset;
6768
6769 /* if inputs are not 16-byte then use intermediate buffer */
6770 if (!IS_ALIGNED(dst_paddr, 16) ||
6771 !IS_ALIGNED(paddr, 16) ||
6772 !IS_ALIGNED(size, 16)) {
6773 tpage = (void *)alloc_page(GFP_KERNEL);
6774 if (!tpage)
6775 return -ENOMEM;
6776
6777 dst_paddr = __sme_page_pa(tpage);
6778 }
6779
6780 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
6781 if (ret)
6782 goto e_free;
6783
6784 if (tpage) {
6785 offset = paddr & 15;
6786 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
6787 page_address(tpage) + offset, size))
6788 ret = -EFAULT;
6789 }
6790
6791e_free:
6792 if (tpage)
6793 __free_page(tpage);
6794
6795 return ret;
6796}
6797
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006798static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
6799 unsigned long __user vaddr,
6800 unsigned long dst_paddr,
6801 unsigned long __user dst_vaddr,
6802 int size, int *error)
6803{
6804 struct page *src_tpage = NULL;
6805 struct page *dst_tpage = NULL;
6806 int ret, len = size;
6807
6808 /* If source buffer is not aligned then use an intermediate buffer */
6809 if (!IS_ALIGNED(vaddr, 16)) {
6810 src_tpage = alloc_page(GFP_KERNEL);
6811 if (!src_tpage)
6812 return -ENOMEM;
6813
6814 if (copy_from_user(page_address(src_tpage),
6815 (void __user *)(uintptr_t)vaddr, size)) {
6816 __free_page(src_tpage);
6817 return -EFAULT;
6818 }
6819
6820 paddr = __sme_page_pa(src_tpage);
6821 }
6822
6823 /*
6824 * If destination buffer or length is not aligned then do read-modify-write:
6825 * - decrypt destination in an intermediate buffer
6826 * - copy the source buffer in an intermediate buffer
6827 * - use the intermediate buffer as source buffer
6828 */
6829 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
6830 int dst_offset;
6831
6832 dst_tpage = alloc_page(GFP_KERNEL);
6833 if (!dst_tpage) {
6834 ret = -ENOMEM;
6835 goto e_free;
6836 }
6837
6838 ret = __sev_dbg_decrypt(kvm, dst_paddr,
6839 __sme_page_pa(dst_tpage), size, error);
6840 if (ret)
6841 goto e_free;
6842
6843 /*
6844 * If source is kernel buffer then use memcpy() otherwise
6845 * copy_from_user().
6846 */
6847 dst_offset = dst_paddr & 15;
6848
6849 if (src_tpage)
6850 memcpy(page_address(dst_tpage) + dst_offset,
6851 page_address(src_tpage), size);
6852 else {
6853 if (copy_from_user(page_address(dst_tpage) + dst_offset,
6854 (void __user *)(uintptr_t)vaddr, size)) {
6855 ret = -EFAULT;
6856 goto e_free;
6857 }
6858 }
6859
6860 paddr = __sme_page_pa(dst_tpage);
6861 dst_paddr = round_down(dst_paddr, 16);
6862 len = round_up(size, 16);
6863 }
6864
6865 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
6866
6867e_free:
6868 if (src_tpage)
6869 __free_page(src_tpage);
6870 if (dst_tpage)
6871 __free_page(dst_tpage);
6872 return ret;
6873}
6874
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006875static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
6876{
6877 unsigned long vaddr, vaddr_end, next_vaddr;
Colin Ian King0186ec82018-08-28 16:22:28 +01006878 unsigned long dst_vaddr;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006879 struct page **src_p, **dst_p;
6880 struct kvm_sev_dbg debug;
6881 unsigned long n;
David Rientjesb86bc282019-03-25 11:47:31 -07006882 unsigned int size;
6883 int ret;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006884
6885 if (!sev_guest(kvm))
6886 return -ENOTTY;
6887
6888 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
6889 return -EFAULT;
6890
David Rientjesb86bc282019-03-25 11:47:31 -07006891 if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr)
6892 return -EINVAL;
6893 if (!debug.dst_uaddr)
6894 return -EINVAL;
6895
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006896 vaddr = debug.src_uaddr;
6897 size = debug.len;
6898 vaddr_end = vaddr + size;
6899 dst_vaddr = debug.dst_uaddr;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006900
6901 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
6902 int len, s_off, d_off;
6903
6904 /* lock userspace source and destination page */
6905 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
6906 if (!src_p)
6907 return -EFAULT;
6908
6909 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
6910 if (!dst_p) {
6911 sev_unpin_memory(kvm, src_p, n);
6912 return -EFAULT;
6913 }
6914
6915 /*
6916 * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
6917 * memory content (i.e it will write the same memory region with C=1).
6918 * It's possible that the cache may contain the data with C=0, i.e.,
6919 * unencrypted so invalidate it first.
6920 */
6921 sev_clflush_pages(src_p, 1);
6922 sev_clflush_pages(dst_p, 1);
6923
6924 /*
6925 * Since user buffer may not be page aligned, calculate the
6926 * offset within the page.
6927 */
6928 s_off = vaddr & ~PAGE_MASK;
6929 d_off = dst_vaddr & ~PAGE_MASK;
6930 len = min_t(size_t, (PAGE_SIZE - s_off), size);
6931
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006932 if (dec)
6933 ret = __sev_dbg_decrypt_user(kvm,
6934 __sme_page_pa(src_p[0]) + s_off,
6935 dst_vaddr,
6936 __sme_page_pa(dst_p[0]) + d_off,
6937 len, &argp->error);
6938 else
6939 ret = __sev_dbg_encrypt_user(kvm,
6940 __sme_page_pa(src_p[0]) + s_off,
6941 vaddr,
6942 __sme_page_pa(dst_p[0]) + d_off,
6943 dst_vaddr,
6944 len, &argp->error);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006945
David Rientjesb86bc282019-03-25 11:47:31 -07006946 sev_unpin_memory(kvm, src_p, n);
6947 sev_unpin_memory(kvm, dst_p, n);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006948
6949 if (ret)
6950 goto err;
6951
6952 next_vaddr = vaddr + len;
6953 dst_vaddr = dst_vaddr + len;
6954 size -= len;
6955 }
6956err:
6957 return ret;
6958}
6959
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006960static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
6961{
Sean Christopherson81811c12018-03-20 12:17:21 -07006962 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006963 struct sev_data_launch_secret *data;
6964 struct kvm_sev_launch_secret params;
6965 struct page **pages;
6966 void *blob, *hdr;
6967 unsigned long n;
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006968 int ret, offset;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006969
6970 if (!sev_guest(kvm))
6971 return -ENOTTY;
6972
6973 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6974 return -EFAULT;
6975
6976 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
6977 if (!pages)
6978 return -ENOMEM;
6979
6980 /*
6981 * The secret must be copied into contiguous memory region, lets verify
6982 * that userspace memory pages are contiguous before we issue command.
6983 */
6984 if (get_num_contig_pages(0, pages, n) != n) {
6985 ret = -EINVAL;
6986 goto e_unpin_memory;
6987 }
6988
6989 ret = -ENOMEM;
Ben Gardon1ec69642019-02-11 11:02:51 -08006990 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006991 if (!data)
6992 goto e_unpin_memory;
6993
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006994 offset = params.guest_uaddr & (PAGE_SIZE - 1);
6995 data->guest_address = __sme_page_pa(pages[0]) + offset;
6996 data->guest_len = params.guest_len;
6997
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006998 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
6999 if (IS_ERR(blob)) {
7000 ret = PTR_ERR(blob);
7001 goto e_free;
7002 }
7003
7004 data->trans_address = __psp_pa(blob);
7005 data->trans_len = params.trans_len;
7006
7007 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
7008 if (IS_ERR(hdr)) {
7009 ret = PTR_ERR(hdr);
7010 goto e_free_blob;
7011 }
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06007012 data->hdr_address = __psp_pa(hdr);
7013 data->hdr_len = params.hdr_len;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06007014
7015 data->handle = sev->handle;
7016 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
7017
7018 kfree(hdr);
7019
7020e_free_blob:
7021 kfree(blob);
7022e_free:
7023 kfree(data);
7024e_unpin_memory:
7025 sev_unpin_memory(kvm, pages, n);
7026 return ret;
7027}
7028
Brijesh Singh1654efc2017-12-04 10:57:34 -06007029static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
7030{
7031 struct kvm_sev_cmd sev_cmd;
7032 int r;
7033
7034 if (!svm_sev_enabled())
7035 return -ENOTTY;
7036
7037 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
7038 return -EFAULT;
7039
7040 mutex_lock(&kvm->lock);
7041
7042 switch (sev_cmd.id) {
7043 case KVM_SEV_INIT:
7044 r = sev_guest_init(kvm, &sev_cmd);
7045 break;
Brijesh Singh59414c92017-12-04 10:57:35 -06007046 case KVM_SEV_LAUNCH_START:
7047 r = sev_launch_start(kvm, &sev_cmd);
7048 break;
Brijesh Singh89c50582017-12-04 10:57:35 -06007049 case KVM_SEV_LAUNCH_UPDATE_DATA:
7050 r = sev_launch_update_data(kvm, &sev_cmd);
7051 break;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06007052 case KVM_SEV_LAUNCH_MEASURE:
7053 r = sev_launch_measure(kvm, &sev_cmd);
7054 break;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06007055 case KVM_SEV_LAUNCH_FINISH:
7056 r = sev_launch_finish(kvm, &sev_cmd);
7057 break;
Brijesh Singh255d9e72017-12-04 10:57:37 -06007058 case KVM_SEV_GUEST_STATUS:
7059 r = sev_guest_status(kvm, &sev_cmd);
7060 break;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06007061 case KVM_SEV_DBG_DECRYPT:
7062 r = sev_dbg_crypt(kvm, &sev_cmd, true);
7063 break;
Brijesh Singh7d1594f2017-12-04 10:57:37 -06007064 case KVM_SEV_DBG_ENCRYPT:
7065 r = sev_dbg_crypt(kvm, &sev_cmd, false);
7066 break;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06007067 case KVM_SEV_LAUNCH_SECRET:
7068 r = sev_launch_secret(kvm, &sev_cmd);
7069 break;
Brijesh Singh1654efc2017-12-04 10:57:34 -06007070 default:
7071 r = -EINVAL;
7072 goto out;
7073 }
7074
7075 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
7076 r = -EFAULT;
7077
7078out:
7079 mutex_unlock(&kvm->lock);
7080 return r;
7081}
7082
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007083static int svm_register_enc_region(struct kvm *kvm,
7084 struct kvm_enc_region *range)
7085{
Sean Christopherson81811c12018-03-20 12:17:21 -07007086 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007087 struct enc_region *region;
7088 int ret = 0;
7089
7090 if (!sev_guest(kvm))
7091 return -ENOTTY;
7092
Dan Carpenter86bf20c2018-05-19 09:01:36 +03007093 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
7094 return -EINVAL;
7095
Ben Gardon1ec69642019-02-11 11:02:51 -08007096 region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT);
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007097 if (!region)
7098 return -ENOMEM;
7099
7100 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
7101 if (!region->pages) {
7102 ret = -ENOMEM;
7103 goto e_free;
7104 }
7105
7106 /*
7107 * The guest may change the memory encryption attribute from C=0 -> C=1
7108 * or vice versa for this memory range. Lets make sure caches are
7109 * flushed to ensure that guest data gets written into memory with
7110 * correct C-bit.
7111 */
7112 sev_clflush_pages(region->pages, region->npages);
7113
7114 region->uaddr = range->addr;
7115 region->size = range->size;
7116
7117 mutex_lock(&kvm->lock);
7118 list_add_tail(&region->list, &sev->regions_list);
7119 mutex_unlock(&kvm->lock);
7120
7121 return ret;
7122
7123e_free:
7124 kfree(region);
7125 return ret;
7126}
7127
7128static struct enc_region *
7129find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
7130{
Sean Christopherson81811c12018-03-20 12:17:21 -07007131 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007132 struct list_head *head = &sev->regions_list;
7133 struct enc_region *i;
7134
7135 list_for_each_entry(i, head, list) {
7136 if (i->uaddr == range->addr &&
7137 i->size == range->size)
7138 return i;
7139 }
7140
7141 return NULL;
7142}
7143
7144
7145static int svm_unregister_enc_region(struct kvm *kvm,
7146 struct kvm_enc_region *range)
7147{
7148 struct enc_region *region;
7149 int ret;
7150
7151 mutex_lock(&kvm->lock);
7152
7153 if (!sev_guest(kvm)) {
7154 ret = -ENOTTY;
7155 goto failed;
7156 }
7157
7158 region = find_enc_region(kvm, range);
7159 if (!region) {
7160 ret = -EINVAL;
7161 goto failed;
7162 }
7163
7164 __unregister_enc_region_locked(kvm, region);
7165
7166 mutex_unlock(&kvm->lock);
7167 return 0;
7168
7169failed:
7170 mutex_unlock(&kvm->lock);
7171 return ret;
7172}
7173
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007174static bool svm_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
7175{
Liran Alon118154b2019-07-17 02:56:58 +03007176 unsigned long cr4 = kvm_read_cr4(vcpu);
7177 bool smep = cr4 & X86_CR4_SMEP;
7178 bool smap = cr4 & X86_CR4_SMAP;
7179 bool is_user = svm_get_cpl(vcpu) == 3;
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007180
7181 /*
Liran Alon118154b2019-07-17 02:56:58 +03007182 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
7183 *
7184 * Errata:
7185 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is
7186 * possible that CPU microcode implementing DecodeAssist will fail
7187 * to read bytes of instruction which caused #NPF. In this case,
7188 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly
7189 * return 0 instead of the correct guest instruction bytes.
7190 *
7191 * This happens because CPU microcode reading instruction bytes
7192 * uses a special opcode which attempts to read data using CPL=0
7193 * priviledges. The microcode reads CS:RIP and if it hits a SMAP
7194 * fault, it gives up and returns no instruction bytes.
7195 *
7196 * Detection:
7197 * We reach here in case CPU supports DecodeAssist, raised #NPF and
7198 * returned 0 in GuestIntrBytes field of the VMCB.
7199 * First, errata can only be triggered in case vCPU CR4.SMAP=1.
7200 * Second, if vCPU CR4.SMEP=1, errata could only be triggered
7201 * in case vCPU CPL==3 (Because otherwise guest would have triggered
7202 * a SMEP fault instead of #NPF).
7203 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL.
7204 * As most guests enable SMAP if they have also enabled SMEP, use above
7205 * logic in order to attempt minimize false-positive of detecting errata
7206 * while still preserving all cases semantic correctness.
7207 *
7208 * Workaround:
7209 * To determine what instruction the guest was executing, the hypervisor
7210 * will have to decode the instruction at the instruction pointer.
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007211 *
7212 * In non SEV guest, hypervisor will be able to read the guest
7213 * memory to decode the instruction pointer when insn_len is zero
7214 * so we return true to indicate that decoding is possible.
7215 *
7216 * But in the SEV guest, the guest memory is encrypted with the
7217 * guest specific key and hypervisor will not be able to decode the
7218 * instruction pointer so we will not able to workaround it. Lets
7219 * print the error and request to kill the guest.
7220 */
Liran Alon118154b2019-07-17 02:56:58 +03007221 if (smap && (!smep || is_user)) {
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007222 if (!sev_guest(vcpu->kvm))
7223 return true;
7224
Liran Alon118154b2019-07-17 02:56:58 +03007225 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007226 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7227 }
7228
7229 return false;
7230}
7231
Liran Alon4b9852f2019-08-26 13:24:49 +03007232static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7233{
7234 struct vcpu_svm *svm = to_svm(vcpu);
7235
7236 /*
7237 * TODO: Last condition latch INIT signals on vCPU when
7238 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
7239 * To properly emulate the INIT intercept, SVM should implement
7240 * kvm_x86_ops->check_nested_events() and call nested_svm_vmexit()
7241 * there if an INIT signal is pending.
7242 */
7243 return !gif_set(svm) ||
7244 (svm->vmcb->control.intercept & (1ULL << INTERCEPT_INIT));
7245}
7246
Kees Cook404f6aa2016-08-08 16:29:06 -07007247static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007248 .cpu_has_kvm_support = has_svm,
7249 .disabled_by_bios = is_disabled,
7250 .hardware_setup = svm_hardware_setup,
7251 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03007252 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007253 .hardware_enable = svm_hardware_enable,
7254 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02007255 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Tom Lendackybc226f02018-05-10 22:06:39 +02007256 .has_emulated_msr = svm_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007257
7258 .vcpu_create = svm_create_vcpu,
7259 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007260 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007261
Sean Christopherson434a1e92018-03-20 12:17:18 -07007262 .vm_alloc = svm_vm_alloc,
7263 .vm_free = svm_vm_free,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007264 .vm_init = avic_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06007265 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007266
Avi Kivity04d2cc72007-09-10 18:10:54 +03007267 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007268 .vcpu_load = svm_vcpu_load,
7269 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05007270 .vcpu_blocking = svm_vcpu_blocking,
7271 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007272
Paolo Bonzinia96036b2015-11-10 11:55:36 +01007273 .update_bp_intercept = update_bp_intercept,
Tom Lendacky801e4592018-02-21 13:39:51 -06007274 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007275 .get_msr = svm_get_msr,
7276 .set_msr = svm_set_msr,
7277 .get_segment_base = svm_get_segment_base,
7278 .get_segment = svm_get_segment,
7279 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007280 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10007281 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02007282 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03007283 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007284 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007285 .set_cr3 = svm_set_cr3,
7286 .set_cr4 = svm_set_cr4,
7287 .set_efer = svm_set_efer,
7288 .get_idt = svm_get_idt,
7289 .set_idt = svm_set_idt,
7290 .get_gdt = svm_get_gdt,
7291 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007292 .get_dr6 = svm_get_dr6,
7293 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03007294 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01007295 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03007296 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007297 .get_rflags = svm_get_rflags,
7298 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007299
Avi Kivity6aa8b732006-12-10 02:21:36 -08007300 .tlb_flush = svm_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007301 .tlb_flush_gva = svm_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007302
Avi Kivity6aa8b732006-12-10 02:21:36 -08007303 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007304 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007305 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007306 .set_interrupt_shadow = svm_set_interrupt_shadow,
7307 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007308 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007309 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007310 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007311 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007312 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007313 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007314 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007315 .get_nmi_mask = svm_get_nmi_mask,
7316 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007317 .enable_nmi_window = enable_nmi_window,
7318 .enable_irq_window = enable_irq_window,
7319 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007320 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007321 .get_enable_apicv = svm_get_enable_apicv,
7322 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007323 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007324 .hwapic_irr_update = svm_hwapic_irr_update,
7325 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02007326 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05007327 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02007328
7329 .set_tss_addr = svm_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007330 .set_identity_map_addr = svm_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08007331 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007332 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007333
Avi Kivity586f9602010-11-18 13:09:54 +02007334 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007335
Sheng Yang17cc3932010-01-05 19:02:27 +08007336 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08007337
7338 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007339
7340 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00007341 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01007342 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08007343 .xsaves_supported = svm_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +02007344 .umip_emulated = svm_umip_emulated,
Chao Peng86f52012018-10-24 16:05:11 +08007345 .pt_supported = svm_pt_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007346
7347 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007348
7349 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007350
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02007351 .read_l1_tsc_offset = svm_read_l1_tsc_offset,
Leonid Shatz326e7422018-11-06 12:14:25 +02007352 .write_l1_tsc_offset = svm_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007353
7354 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007355
7356 .check_intercept = svm_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07007357 .handle_exit_irqoff = svm_handle_exit_irqoff,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007358
Sean Christophersond264ee02018-08-27 15:21:12 -07007359 .request_immediate_exit = __kvm_request_immediate_exit,
7360
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007361 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02007362
7363 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05007364 .deliver_posted_interrupt = svm_deliver_avic_intr,
Wanpeng Li17e433b2019-08-05 10:03:19 +08007365 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05007366 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02007367 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007368
Ladi Prosek72d7b372017-10-11 16:54:41 +02007369 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007370 .pre_enter_smm = svm_pre_enter_smm,
7371 .pre_leave_smm = svm_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007372 .enable_smi_window = enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06007373
7374 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007375 .mem_enc_reg_region = svm_register_enc_region,
7376 .mem_enc_unreg_region = svm_unregister_enc_region,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007377
Vitaly Kuznetsov956e2552019-08-28 09:59:04 +02007378 .nested_enable_evmcs = NULL,
Vitaly Kuznetsovea152982019-08-27 18:04:02 +02007379 .nested_get_evmcs_version = NULL,
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007380
7381 .need_emulation_on_page_fault = svm_need_emulation_on_page_fault,
Liran Alon4b9852f2019-08-26 13:24:49 +03007382
7383 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007384};
7385
7386static int __init svm_init(void)
7387{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007388 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03007389 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007390}
7391
7392static void __exit svm_exit(void)
7393{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007394 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08007395}
7396
7397module_init(svm_init)
7398module_exit(svm_exit)