blob: e0a3f56a791d579da61fe325f0515d02b8c2348a [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * AMD SVM support
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02007 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08008 *
9 * Authors:
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050017
18#define pr_fmt(fmt) "SVM: " fmt
19
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
21
Eddie Dong85f455f2007-07-06 12:20:49 +030022#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080023#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020025#include "x86.h"
Julian Stecklina66f7b722012-12-05 15:26:19 +010026#include "cpuid.h"
Wei Huang25462f72015-06-19 15:45:05 +020027#include "pmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040028
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/module.h>
Josh Triplettae759542012-03-28 11:32:28 -070030#include <linux/mod_devicetable.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020031#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/vmalloc.h>
33#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040034#include <linux/sched.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040035#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -050037#include <linux/amd-iommu.h>
38#include <linux/hashtable.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050039#include <linux/frame.h>
Brijesh Singhe9df0942017-12-04 10:57:33 -060040#include <linux/psp-sev.h>
Brijesh Singh1654efc2017-12-04 10:57:34 -060041#include <linux/file.h>
Brijesh Singh89c50582017-12-04 10:57:35 -060042#include <linux/pagemap.h>
43#include <linux/swap.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050045#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010046#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020047#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040048#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020050#include <asm/kvm_para.h>
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -050051#include <asm/irq_remapping.h>
Paolo Bonziniecb586b2018-02-22 16:43:17 +010052#include <asm/microcode.h>
David Woodhouse117cc7a2018-01-12 11:11:27 +000053#include <asm/nospec-branch.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080054
Eduardo Habkost63d11422008-11-17 19:03:20 -020055#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030056#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020057
Avi Kivity4ecac3f2008-05-13 13:23:38 +030058#define __ex(x) __kvm_handle_fault_on_reboot(x)
59
Avi Kivity6aa8b732006-12-10 02:21:36 -080060MODULE_AUTHOR("Qumranet");
61MODULE_LICENSE("GPL");
62
Josh Triplettae759542012-03-28 11:32:28 -070063static const struct x86_cpu_id svm_cpu_id[] = {
64 X86_FEATURE_MATCH(X86_FEATURE_SVM),
65 {}
66};
67MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
68
Avi Kivity6aa8b732006-12-10 02:21:36 -080069#define IOPM_ALLOC_ORDER 2
70#define MSRPM_ALLOC_ORDER 1
71
Avi Kivity6aa8b732006-12-10 02:21:36 -080072#define SEG_TYPE_LDT 2
73#define SEG_TYPE_BUSY_TSS16 3
74
Andre Przywara6bc31bd2010-04-11 23:07:28 +020075#define SVM_FEATURE_NPT (1 << 0)
76#define SVM_FEATURE_LBRV (1 << 1)
77#define SVM_FEATURE_SVML (1 << 2)
78#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010079#define SVM_FEATURE_TSC_RATE (1 << 4)
80#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
81#define SVM_FEATURE_FLUSH_ASID (1 << 6)
82#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020083#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030084
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -050085#define SVM_AVIC_DOORBELL 0xc001011b
86
Joerg Roedel410e4d52009-08-07 11:49:44 +020087#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
88#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
89#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
90
Joerg Roedel24e09cb2008-02-13 18:58:47 +010091#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
92
Joerg Roedelfbc0db72011-03-25 09:44:46 +010093#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010094#define TSC_RATIO_MIN 0x0000000000000001ULL
95#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010096
Dan Carpenter5446a972016-05-23 13:20:10 +030097#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050098
99/*
100 * 0xff is broadcast, so the max index allowed for physical APIC ID
101 * table is 0xfe. APIC IDs above 0xff are reserved.
102 */
103#define AVIC_MAX_PHYSICAL_ID_COUNT 255
104
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500105#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
106#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
107#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
108
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500109/* AVIC GATAG is encoded using VM and VCPU IDs */
110#define AVIC_VCPU_ID_BITS 8
111#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
112
113#define AVIC_VM_ID_BITS 24
114#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
115#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
116
117#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
118 (y & AVIC_VCPU_ID_MASK))
119#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
120#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
121
Joerg Roedel67ec6602010-05-17 14:43:35 +0200122static bool erratum_383_found __read_mostly;
123
Avi Kivity6c8166a2009-05-31 18:15:37 +0300124static const u32 host_save_user_msrs[] = {
125#ifdef CONFIG_X86_64
126 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
127 MSR_FS_BASE,
128#endif
129 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Paolo Bonzini46896c72015-11-12 14:49:16 +0100130 MSR_TSC_AUX,
Avi Kivity6c8166a2009-05-31 18:15:37 +0300131};
132
133#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
134
Sean Christopherson81811c12018-03-20 12:17:21 -0700135struct kvm_sev_info {
136 bool active; /* SEV enabled guest */
137 unsigned int asid; /* ASID used for this guest */
138 unsigned int handle; /* SEV firmware handle */
139 int fd; /* SEV device fd */
140 unsigned long pages_locked; /* Number of pages locked */
141 struct list_head regions_list; /* List of registered regions */
142};
143
144struct kvm_svm {
145 struct kvm kvm;
146
147 /* Struct members for AVIC */
148 u32 avic_vm_id;
149 u32 ldr_mode;
150 struct page *avic_logical_id_table_page;
151 struct page *avic_physical_id_table_page;
152 struct hlist_node hnode;
153
154 struct kvm_sev_info sev_info;
155};
156
Avi Kivity6c8166a2009-05-31 18:15:37 +0300157struct kvm_vcpu;
158
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200159struct nested_state {
160 struct vmcb *hsave;
161 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +0100162 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200163 u64 vmcb;
164
165 /* These are the merged vectors */
166 u32 *msrpm;
167
168 /* gpa pointers to the real vectors */
169 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100170 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200171
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200172 /* A VMEXIT is required but not yet emulated */
173 bool exit_required;
174
Joerg Roedelaad42c62009-08-07 11:49:34 +0200175 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100176 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100177 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200178 u32 intercept_exceptions;
179 u64 intercept;
180
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200181 /* Nested Paging related state */
182 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200183};
184
Joerg Roedel323c3d82010-03-01 15:34:37 +0100185#define MSRPM_OFFSETS 16
186static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
187
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500188/*
189 * Set osvw_len to higher value when updated Revision Guides
190 * are published and we know what the new status bits are
191 */
192static uint64_t osvw_len = 4, osvw_status;
193
Avi Kivity6c8166a2009-05-31 18:15:37 +0300194struct vcpu_svm {
195 struct kvm_vcpu vcpu;
196 struct vmcb *vmcb;
197 unsigned long vmcb_pa;
198 struct svm_cpu_data *svm_data;
199 uint64_t asid_generation;
200 uint64_t sysenter_esp;
201 uint64_t sysenter_eip;
Paolo Bonzini46896c72015-11-12 14:49:16 +0100202 uint64_t tsc_aux;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300203
Tom Lendackyd1d93fa2018-02-24 00:18:20 +0100204 u64 msr_decfg;
205
Avi Kivity6c8166a2009-05-31 18:15:37 +0300206 u64 next_rip;
207
208 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200209 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200210 u16 fs;
211 u16 gs;
212 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200213 u64 gs_base;
214 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300215
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100216 u64 spec_ctrl;
217
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;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500234 struct page *avic_backing_page;
235 u64 *avic_physical_id_cache;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -0500236 bool avic_is_running;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500237
238 /*
239 * Per-vcpu list of struct amd_svm_iommu_ir:
240 * This is used mainly to store interrupt remapping information used
241 * when update the vcpu affinity. This avoids the need to scan for
242 * IRTE and try to match ga_tag in the IOMMU driver.
243 */
244 struct list_head ir_list;
245 spinlock_t ir_list_lock;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600246
247 /* which host CPU was used for running this vcpu */
248 unsigned int last_cpu;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500249};
250
251/*
252 * This is a wrapper of struct amd_iommu_ir_data.
253 */
254struct amd_svm_iommu_ir {
255 struct list_head node; /* Used by SVM for per-vcpu ir_list */
256 void *data; /* Storing pointer to struct amd_ir_data */
Avi Kivity6c8166a2009-05-31 18:15:37 +0300257};
258
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500259#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
260#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
261
262#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
263#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
264#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
265#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
266
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100267static DEFINE_PER_CPU(u64, current_tsc_ratio);
268#define TSC_RATIO_DEFAULT 0x0100000000ULL
269
Joerg Roedel455716f2010-03-01 15:34:35 +0100270#define MSR_INVALID 0xffffffffU
271
Mathias Krause09941fb2012-08-30 01:30:20 +0200272static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100273 u32 index; /* Index of the MSR */
274 bool always; /* True if intercept is always on */
275} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400276 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100277 { .index = MSR_IA32_SYSENTER_CS, .always = true },
278#ifdef CONFIG_X86_64
279 { .index = MSR_GS_BASE, .always = true },
280 { .index = MSR_FS_BASE, .always = true },
281 { .index = MSR_KERNEL_GS_BASE, .always = true },
282 { .index = MSR_LSTAR, .always = true },
283 { .index = MSR_CSTAR, .always = true },
284 { .index = MSR_SYSCALL_MASK, .always = true },
285#endif
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100286 { .index = MSR_IA32_SPEC_CTRL, .always = false },
Ashok Raj15d45072018-02-01 22:59:43 +0100287 { .index = MSR_IA32_PRED_CMD, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100288 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
289 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
290 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
291 { .index = MSR_IA32_LASTINTTOIP, .always = false },
292 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293};
294
295/* enable NPT for AMD64 and X86 with PAE */
296#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
297static bool npt_enabled = true;
298#else
Joerg Roedele0231712010-02-24 18:59:10 +0100299static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300#endif
301
Babu Moger8566ac82018-03-16 16:37:26 -0400302/*
303 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
304 * pause_filter_count: On processors that support Pause filtering(indicated
305 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
306 * count value. On VMRUN this value is loaded into an internal counter.
307 * Each time a pause instruction is executed, this counter is decremented
308 * until it reaches zero at which time a #VMEXIT is generated if pause
309 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
310 * Intercept Filtering for more details.
311 * This also indicate if ple logic enabled.
312 *
313 * pause_filter_thresh: In addition, some processor families support advanced
314 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
315 * the amount of time a guest is allowed to execute in a pause loop.
316 * In this mode, a 16-bit pause filter threshold field is added in the
317 * VMCB. The threshold value is a cycle count that is used to reset the
318 * pause counter. As with simple pause filtering, VMRUN loads the pause
319 * count value from VMCB into an internal counter. Then, on each pause
320 * instruction the hardware checks the elapsed number of cycles since
321 * the most recent pause instruction against the pause filter threshold.
322 * If the elapsed cycle count is greater than the pause filter threshold,
323 * then the internal pause count is reloaded from the VMCB and execution
324 * continues. If the elapsed cycle count is less than the pause filter
325 * threshold, then the internal pause count is decremented. If the count
326 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
327 * triggered. If advanced pause filtering is supported and pause filter
328 * threshold field is set to zero, the filter will operate in the simpler,
329 * count only mode.
330 */
331
332static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
333module_param(pause_filter_thresh, ushort, 0444);
334
335static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
336module_param(pause_filter_count, ushort, 0444);
337
338/* Default doubles per-vcpu window every exit. */
339static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
340module_param(pause_filter_count_grow, ushort, 0444);
341
342/* Default resets per-vcpu window every exit to pause_filter_count. */
343static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
344module_param(pause_filter_count_shrink, ushort, 0444);
345
346/* Default is to compute the maximum so we can never overflow. */
347static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
348module_param(pause_filter_count_max, ushort, 0444);
349
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100350/* allow nested paging (virtualized MMU) for all guests */
351static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352module_param(npt, int, S_IRUGO);
353
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100354/* allow nested virtualization in KVM/SVM */
355static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800356module_param(nested, int, S_IRUGO);
357
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500358/* enable / disable AVIC */
359static int avic;
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500360#ifdef CONFIG_X86_LOCAL_APIC
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500361module_param(avic, int, S_IRUGO);
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500362#endif
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500363
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -0500364/* enable/disable Virtual VMLOAD VMSAVE */
365static int vls = true;
366module_param(vls, int, 0444);
367
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500368/* enable/disable Virtual GIF */
369static int vgif = true;
370module_param(vgif, int, 0444);
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500371
Brijesh Singhe9df0942017-12-04 10:57:33 -0600372/* enable/disable SEV support */
373static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
374module_param(sev, int, 0444);
375
Brijesh Singh7607b712018-02-19 10:14:44 -0600376static u8 rsm_ins_bytes[] = "\x0f\xaa";
377
Paolo Bonzini79a80592015-09-21 07:46:55 +0200378static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -0800379static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa);
Joerg Roedela5c38322009-08-07 11:49:32 +0200380static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381
Joerg Roedel410e4d52009-08-07 11:49:44 +0200382static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100383static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800384static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800385static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
386 bool has_error_code, u32 error_code);
387
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100388enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100389 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
390 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100391 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100392 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100393 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100394 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100395 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100396 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100397 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100398 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100399 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100400 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500401 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
402 * AVIC PHYSICAL_TABLE pointer,
403 * AVIC LOGICAL_TABLE pointer
404 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100405 VMCB_DIRTY_MAX,
406};
407
Joerg Roedel0574dec2010-12-03 11:45:58 +0100408/* TPR and CR2 are always written before VMRUN */
409#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100410
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500411#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
412
Brijesh Singhed3cd232017-12-04 10:57:32 -0600413static unsigned int max_sev_asid;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600414static unsigned int min_sev_asid;
415static unsigned long *sev_asid_bitmap;
Brijesh Singh89c50582017-12-04 10:57:35 -0600416#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
Brijesh Singh1654efc2017-12-04 10:57:34 -0600417
Brijesh Singh1e80fdc2017-12-04 10:57:38 -0600418struct enc_region {
419 struct list_head list;
420 unsigned long npages;
421 struct page **pages;
422 unsigned long uaddr;
423 unsigned long size;
424};
425
Sean Christopherson81811c12018-03-20 12:17:21 -0700426
427static inline struct kvm_svm *to_kvm_svm(struct kvm *kvm)
428{
429 return container_of(kvm, struct kvm_svm, kvm);
430}
431
Brijesh Singh1654efc2017-12-04 10:57:34 -0600432static inline bool svm_sev_enabled(void)
433{
434 return max_sev_asid;
435}
436
437static inline bool sev_guest(struct kvm *kvm)
438{
Sean Christopherson81811c12018-03-20 12:17:21 -0700439 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600440
441 return sev->active;
442}
Brijesh Singhed3cd232017-12-04 10:57:32 -0600443
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600444static inline int sev_get_asid(struct kvm *kvm)
445{
Sean Christopherson81811c12018-03-20 12:17:21 -0700446 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600447
448 return sev->asid;
449}
450
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100451static inline void mark_all_dirty(struct vmcb *vmcb)
452{
453 vmcb->control.clean = 0;
454}
455
456static inline void mark_all_clean(struct vmcb *vmcb)
457{
458 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
459 & ~VMCB_ALWAYS_DIRTY_MASK;
460}
461
462static inline void mark_dirty(struct vmcb *vmcb, int bit)
463{
464 vmcb->control.clean &= ~(1 << bit);
465}
466
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
468{
469 return container_of(vcpu, struct vcpu_svm, vcpu);
470}
471
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500472static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
473{
474 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
475 mark_dirty(svm->vmcb, VMCB_AVIC);
476}
477
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -0500478static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
479{
480 struct vcpu_svm *svm = to_svm(vcpu);
481 u64 *entry = svm->avic_physical_id_cache;
482
483 if (!entry)
484 return false;
485
486 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
487}
488
Joerg Roedel384c6362010-11-30 18:03:56 +0100489static void recalc_intercepts(struct vcpu_svm *svm)
490{
491 struct vmcb_control_area *c, *h;
492 struct nested_state *g;
493
Joerg Roedel116a0a22010-12-03 11:45:49 +0100494 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
495
Joerg Roedel384c6362010-11-30 18:03:56 +0100496 if (!is_guest_mode(&svm->vcpu))
497 return;
498
499 c = &svm->vmcb->control;
500 h = &svm->nested.hsave->control;
501 g = &svm->nested;
502
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100503 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100504 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Paolo Bonzinibd895252018-01-11 16:55:24 +0100505 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
Joerg Roedel384c6362010-11-30 18:03:56 +0100506 c->intercept = h->intercept | g->intercept;
507}
508
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100509static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
510{
511 if (is_guest_mode(&svm->vcpu))
512 return svm->nested.hsave;
513 else
514 return svm->vmcb;
515}
516
517static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
518{
519 struct vmcb *vmcb = get_host_vmcb(svm);
520
521 vmcb->control.intercept_cr |= (1U << bit);
522
523 recalc_intercepts(svm);
524}
525
526static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
527{
528 struct vmcb *vmcb = get_host_vmcb(svm);
529
530 vmcb->control.intercept_cr &= ~(1U << bit);
531
532 recalc_intercepts(svm);
533}
534
535static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
536{
537 struct vmcb *vmcb = get_host_vmcb(svm);
538
539 return vmcb->control.intercept_cr & (1U << bit);
540}
541
Paolo Bonzini5315c712014-03-03 13:08:29 +0100542static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100543{
544 struct vmcb *vmcb = get_host_vmcb(svm);
545
Paolo Bonzini5315c712014-03-03 13:08:29 +0100546 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
547 | (1 << INTERCEPT_DR1_READ)
548 | (1 << INTERCEPT_DR2_READ)
549 | (1 << INTERCEPT_DR3_READ)
550 | (1 << INTERCEPT_DR4_READ)
551 | (1 << INTERCEPT_DR5_READ)
552 | (1 << INTERCEPT_DR6_READ)
553 | (1 << INTERCEPT_DR7_READ)
554 | (1 << INTERCEPT_DR0_WRITE)
555 | (1 << INTERCEPT_DR1_WRITE)
556 | (1 << INTERCEPT_DR2_WRITE)
557 | (1 << INTERCEPT_DR3_WRITE)
558 | (1 << INTERCEPT_DR4_WRITE)
559 | (1 << INTERCEPT_DR5_WRITE)
560 | (1 << INTERCEPT_DR6_WRITE)
561 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100562
563 recalc_intercepts(svm);
564}
565
Paolo Bonzini5315c712014-03-03 13:08:29 +0100566static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100567{
568 struct vmcb *vmcb = get_host_vmcb(svm);
569
Paolo Bonzini5315c712014-03-03 13:08:29 +0100570 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100571
572 recalc_intercepts(svm);
573}
574
Joerg Roedel18c918c2010-11-30 18:03:59 +0100575static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
576{
577 struct vmcb *vmcb = get_host_vmcb(svm);
578
579 vmcb->control.intercept_exceptions |= (1U << bit);
580
581 recalc_intercepts(svm);
582}
583
584static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
585{
586 struct vmcb *vmcb = get_host_vmcb(svm);
587
588 vmcb->control.intercept_exceptions &= ~(1U << bit);
589
590 recalc_intercepts(svm);
591}
592
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100593static inline void set_intercept(struct vcpu_svm *svm, int bit)
594{
595 struct vmcb *vmcb = get_host_vmcb(svm);
596
597 vmcb->control.intercept |= (1ULL << bit);
598
599 recalc_intercepts(svm);
600}
601
602static inline void clr_intercept(struct vcpu_svm *svm, int bit)
603{
604 struct vmcb *vmcb = get_host_vmcb(svm);
605
606 vmcb->control.intercept &= ~(1ULL << bit);
607
608 recalc_intercepts(svm);
609}
610
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500611static inline bool vgif_enabled(struct vcpu_svm *svm)
612{
613 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
614}
615
Joerg Roedel2af91942009-08-07 11:49:28 +0200616static inline void enable_gif(struct vcpu_svm *svm)
617{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500618 if (vgif_enabled(svm))
619 svm->vmcb->control.int_ctl |= V_GIF_MASK;
620 else
621 svm->vcpu.arch.hflags |= HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200622}
623
624static inline void disable_gif(struct vcpu_svm *svm)
625{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500626 if (vgif_enabled(svm))
627 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
628 else
629 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200630}
631
632static inline bool gif_set(struct vcpu_svm *svm)
633{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500634 if (vgif_enabled(svm))
635 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
636 else
637 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
Joerg Roedel2af91942009-08-07 11:49:28 +0200638}
639
Avi Kivity6aa8b732006-12-10 02:21:36 -0800640static unsigned long iopm_base;
641
642struct kvm_ldttss_desc {
643 u16 limit0;
644 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100645 unsigned base1:8, type:5, dpl:2, p:1;
646 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647 u32 base3;
648 u32 zero1;
649} __attribute__((packed));
650
651struct svm_cpu_data {
652 int cpu;
653
Avi Kivity5008fdf2007-04-02 13:05:50 +0300654 u64 asid_generation;
655 u32 max_asid;
656 u32 next_asid;
Brijesh Singh4faefff2017-12-04 10:57:25 -0600657 u32 min_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658 struct kvm_ldttss_desc *tss_desc;
659
660 struct page *save_area;
Ashok Raj15d45072018-02-01 22:59:43 +0100661 struct vmcb *current_vmcb;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600662
663 /* index = sev_asid, value = vmcb pointer */
664 struct vmcb **sev_vmcbs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665};
666
667static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
668
669struct svm_init_data {
670 int cpu;
671 int r;
672};
673
Mathias Krause09941fb2012-08-30 01:30:20 +0200674static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200676#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677#define MSRS_RANGE_SIZE 2048
678#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
679
Joerg Roedel455716f2010-03-01 15:34:35 +0100680static u32 svm_msrpm_offset(u32 msr)
681{
682 u32 offset;
683 int i;
684
685 for (i = 0; i < NUM_MSR_MAPS; i++) {
686 if (msr < msrpm_ranges[i] ||
687 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
688 continue;
689
690 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
691 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
692
693 /* Now we have the u8 offset - but need the u32 offset */
694 return offset / 4;
695 }
696
697 /* MSR not in any range */
698 return MSR_INVALID;
699}
700
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701#define MAX_INST_SIZE 15
702
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703static inline void clgi(void)
704{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300705 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706}
707
708static inline void stgi(void)
709{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300710 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711}
712
713static inline void invlpga(unsigned long addr, u32 asid)
714{
Joerg Roedele0231712010-02-24 18:59:10 +0100715 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716}
717
Yu Zhang855feb62017-08-24 20:27:55 +0800718static int get_npt_level(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +0200719{
720#ifdef CONFIG_X86_64
Yu Zhang2a7266a2017-08-24 20:27:54 +0800721 return PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200722#else
723 return PT32E_ROOT_LEVEL;
724#endif
725}
726
Avi Kivity6aa8b732006-12-10 02:21:36 -0800727static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
728{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000729 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100730 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600731 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732
Alexander Graf9962d032008-11-25 20:17:02 +0100733 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100734 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735}
736
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737static int is_external_interrupt(u32 info)
738{
739 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
740 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
741}
742
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200743static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400744{
745 struct vcpu_svm *svm = to_svm(vcpu);
746 u32 ret = 0;
747
748 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200749 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
750 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400751}
752
753static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
754{
755 struct vcpu_svm *svm = to_svm(vcpu);
756
757 if (mask == 0)
758 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
759 else
760 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
761
762}
763
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
765{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400766 struct vcpu_svm *svm = to_svm(vcpu);
767
Bandan Dasf1047652015-06-11 02:05:33 -0400768 if (svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200769 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200770 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400771 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200772
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400773 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100774 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300775 EMULATE_DONE)
776 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777 return;
778 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300779 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
780 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
781 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300783 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400784 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785}
786
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700787static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100788{
789 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700790 unsigned nr = vcpu->arch.exception.nr;
791 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700792 bool reinject = vcpu->arch.exception.injected;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700793 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100794
Joerg Roedele0231712010-02-24 18:59:10 +0100795 /*
796 * If we are within a nested VM we'd better #VMEXIT and let the guest
797 * handle the exception
798 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200799 if (!reinject &&
800 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100801 return;
802
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200803 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100804 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
805
806 /*
807 * For guest debugging where we have to reinject #BP if some
808 * INT3 is guest-owned:
809 * Emulate nRIP by moving RIP forward. Will fail if injection
810 * raises a fault that is not intercepted. Still better than
811 * failing in all cases.
812 */
813 skip_emulated_instruction(&svm->vcpu);
814 rip = kvm_rip_read(&svm->vcpu);
815 svm->int3_rip = rip + svm->vmcb->save.cs.base;
816 svm->int3_injected = rip - old_rip;
817 }
818
Jan Kiszka116a4752010-02-23 17:47:54 +0100819 svm->vmcb->control.event_inj = nr
820 | SVM_EVTINJ_VALID
821 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
822 | SVM_EVTINJ_TYPE_EXEPT;
823 svm->vmcb->control.event_inj_err = error_code;
824}
825
Joerg Roedel67ec6602010-05-17 14:43:35 +0200826static void svm_init_erratum_383(void)
827{
828 u32 low, high;
829 int err;
830 u64 val;
831
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100832 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200833 return;
834
835 /* Use _safe variants to not break nested virtualization */
836 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
837 if (err)
838 return;
839
840 val |= (1ULL << 47);
841
842 low = lower_32_bits(val);
843 high = upper_32_bits(val);
844
845 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
846
847 erratum_383_found = true;
848}
849
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500850static void svm_init_osvw(struct kvm_vcpu *vcpu)
851{
852 /*
853 * Guests should see errata 400 and 415 as fixed (assuming that
854 * HLT and IO instructions are intercepted).
855 */
856 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
857 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
858
859 /*
860 * By increasing VCPU's osvw.length to 3 we are telling the guest that
861 * all osvw.status bits inside that length, including bit 0 (which is
862 * reserved for erratum 298), are valid. However, if host processor's
863 * osvw_len is 0 then osvw_status[0] carries no information. We need to
864 * be conservative here and therefore we tell the guest that erratum 298
865 * is present (because we really don't know).
866 */
867 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
868 vcpu->arch.osvw.status |= 1;
869}
870
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871static int has_svm(void)
872{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200873 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874
Eduardo Habkost63d11422008-11-17 19:03:20 -0200875 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800876 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877 return 0;
878 }
879
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 return 1;
881}
882
Radim Krčmář13a34e02014-08-28 15:13:03 +0200883static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800884{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100885 /* Make sure we clean up behind us */
886 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
887 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
888
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200889 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100890
891 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892}
893
Radim Krčmář13a34e02014-08-28 15:13:03 +0200894static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895{
896
Tejun Heo0fe1e002009-10-29 22:34:14 +0900897 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 struct desc_struct *gdt;
900 int me = raw_smp_processor_id();
901
Alexander Graf10474ae2009-09-15 11:37:46 +0200902 rdmsrl(MSR_EFER, efer);
903 if (efer & EFER_SVME)
904 return -EBUSY;
905
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200907 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200908 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900910 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900911 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200912 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200913 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 }
915
Tejun Heo0fe1e002009-10-29 22:34:14 +0900916 sd->asid_generation = 1;
917 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
918 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600919 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920
Thomas Garnier45fc8752017-03-14 10:05:08 -0700921 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900922 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923
Alexander Graf9962d032008-11-25 20:17:02 +0100924 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925
Linus Torvaldsd0316552009-12-14 09:58:24 -0800926 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200927
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100928 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
929 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500930 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100931 }
932
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500933
934 /*
935 * Get OSVW bits.
936 *
937 * Note that it is possible to have a system with mixed processor
938 * revisions and therefore different OSVW bits. If bits are not the same
939 * on different processors then choose the worst case (i.e. if erratum
940 * is present on one processor and not on another then assume that the
941 * erratum is present everywhere).
942 */
943 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
944 uint64_t len, status = 0;
945 int err;
946
947 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
948 if (!err)
949 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
950 &err);
951
952 if (err)
953 osvw_status = osvw_len = 0;
954 else {
955 if (len < osvw_len)
956 osvw_len = len;
957 osvw_status |= status;
958 osvw_status &= (1ULL << osvw_len) - 1;
959 }
960 } else
961 osvw_status = osvw_len = 0;
962
Joerg Roedel67ec6602010-05-17 14:43:35 +0200963 svm_init_erratum_383();
964
Joerg Roedel1018faa2012-02-29 14:57:32 +0100965 amd_pmu_enable_virt();
966
Alexander Graf10474ae2009-09-15 11:37:46 +0200967 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968}
969
Joerg Roedel0da1db752008-07-02 16:02:11 +0200970static void svm_cpu_uninit(int cpu)
971{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900972 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200973
Tejun Heo0fe1e002009-10-29 22:34:14 +0900974 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200975 return;
976
977 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600978 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900979 __free_page(sd->save_area);
980 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200981}
982
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983static int svm_cpu_init(int cpu)
984{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900985 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 int r;
987
Tejun Heo0fe1e002009-10-29 22:34:14 +0900988 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
989 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900991 sd->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 r = -ENOMEM;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600993 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900994 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 goto err_1;
996
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600997 if (svm_sev_enabled()) {
998 r = -ENOMEM;
999 sd->sev_vmcbs = kmalloc((max_sev_asid + 1) * sizeof(void *), GFP_KERNEL);
1000 if (!sd->sev_vmcbs)
1001 goto err_1;
1002 }
1003
Tejun Heo0fe1e002009-10-29 22:34:14 +09001004 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005
1006 return 0;
1007
1008err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +09001009 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 return r;
1011
1012}
1013
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001014static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015{
1016 int i;
1017
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001018 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
1019 if (direct_access_msrs[i].index == index)
1020 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001022 return false;
1023}
1024
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001025static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr)
1026{
1027 u8 bit_write;
1028 unsigned long tmp;
1029 u32 offset;
1030 u32 *msrpm;
1031
1032 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
1033 to_svm(vcpu)->msrpm;
1034
1035 offset = svm_msrpm_offset(msr);
1036 bit_write = 2 * (msr & 0x0f) + 1;
1037 tmp = msrpm[offset];
1038
1039 BUG_ON(offset == MSR_INVALID);
1040
1041 return !!test_bit(bit_write, &tmp);
1042}
1043
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044static void set_msr_interception(u32 *msrpm, unsigned msr,
1045 int read, int write)
1046{
Joerg Roedel455716f2010-03-01 15:34:35 +01001047 u8 bit_read, bit_write;
1048 unsigned long tmp;
1049 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001051 /*
1052 * If this warning triggers extend the direct_access_msrs list at the
1053 * beginning of the file
1054 */
1055 WARN_ON(!valid_msr_intercept(msr));
1056
Joerg Roedel455716f2010-03-01 15:34:35 +01001057 offset = svm_msrpm_offset(msr);
1058 bit_read = 2 * (msr & 0x0f);
1059 bit_write = 2 * (msr & 0x0f) + 1;
1060 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061
Joerg Roedel455716f2010-03-01 15:34:35 +01001062 BUG_ON(offset == MSR_INVALID);
1063
1064 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
1065 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
1066
1067 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068}
1069
Joerg Roedelf65c2292008-02-13 18:58:46 +01001070static void svm_vcpu_init_msrpm(u32 *msrpm)
1071{
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001072 int i;
1073
Joerg Roedelf65c2292008-02-13 18:58:46 +01001074 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
1075
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001076 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1077 if (!direct_access_msrs[i].always)
1078 continue;
1079
1080 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
1081 }
Joerg Roedelf65c2292008-02-13 18:58:46 +01001082}
1083
Joerg Roedel323c3d82010-03-01 15:34:37 +01001084static void add_msr_offset(u32 offset)
1085{
1086 int i;
1087
1088 for (i = 0; i < MSRPM_OFFSETS; ++i) {
1089
1090 /* Offset already in list? */
1091 if (msrpm_offsets[i] == offset)
1092 return;
1093
1094 /* Slot used by another offset? */
1095 if (msrpm_offsets[i] != MSR_INVALID)
1096 continue;
1097
1098 /* Add offset to list */
1099 msrpm_offsets[i] = offset;
1100
1101 return;
1102 }
1103
1104 /*
1105 * If this BUG triggers the msrpm_offsets table has an overflow. Just
1106 * increase MSRPM_OFFSETS in this case.
1107 */
1108 BUG();
1109}
1110
1111static void init_msrpm_offsets(void)
1112{
1113 int i;
1114
1115 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
1116
1117 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1118 u32 offset;
1119
1120 offset = svm_msrpm_offset(direct_access_msrs[i].index);
1121 BUG_ON(offset == MSR_INVALID);
1122
1123 add_msr_offset(offset);
1124 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125}
1126
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001127static void svm_enable_lbrv(struct vcpu_svm *svm)
1128{
1129 u32 *msrpm = svm->msrpm;
1130
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001131 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001132 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
1133 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
1134 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
1135 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
1136}
1137
1138static void svm_disable_lbrv(struct vcpu_svm *svm)
1139{
1140 u32 *msrpm = svm->msrpm;
1141
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001142 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001143 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
1144 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
1145 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
1146 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
1147}
1148
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001149static void disable_nmi_singlestep(struct vcpu_svm *svm)
1150{
1151 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001152
Ladi Prosekab2f4d732017-06-21 09:06:58 +02001153 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
1154 /* Clear our flags if they were not set by the guest */
1155 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1156 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
1157 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1158 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
1159 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001160}
1161
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001162/* Note:
Sean Christopherson81811c12018-03-20 12:17:21 -07001163 * This hash table is used to map VM_ID to a struct kvm_svm,
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001164 * when handling AMD IOMMU GALOG notification to schedule in
1165 * a particular vCPU.
1166 */
1167#define SVM_VM_DATA_HASH_BITS 8
David Hildenbrand681bcea2017-01-24 22:21:16 +01001168static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001169static u32 next_vm_id = 0;
1170static bool next_vm_id_wrapped = 0;
David Hildenbrand681bcea2017-01-24 22:21:16 +01001171static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001172
1173/* Note:
1174 * This function is called from IOMMU driver to notify
1175 * SVM to schedule in a particular vCPU of a particular VM.
1176 */
1177static int avic_ga_log_notifier(u32 ga_tag)
1178{
1179 unsigned long flags;
Sean Christopherson81811c12018-03-20 12:17:21 -07001180 struct kvm_svm *kvm_svm;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001181 struct kvm_vcpu *vcpu = NULL;
1182 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
1183 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
1184
1185 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
1186
1187 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Sean Christopherson81811c12018-03-20 12:17:21 -07001188 hash_for_each_possible(svm_vm_data_hash, kvm_svm, hnode, vm_id) {
1189 if (kvm_svm->avic_vm_id != vm_id)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001190 continue;
Sean Christopherson81811c12018-03-20 12:17:21 -07001191 vcpu = kvm_get_vcpu_by_id(&kvm_svm->kvm, vcpu_id);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001192 break;
1193 }
1194 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1195
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001196 /* Note:
1197 * At this point, the IOMMU should have already set the pending
1198 * bit in the vAPIC backing page. So, we just need to schedule
1199 * in the vcpu.
1200 */
Paolo Bonzini1cf53582017-10-10 12:51:56 +02001201 if (vcpu)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001202 kvm_vcpu_wake_up(vcpu);
1203
1204 return 0;
1205}
1206
Brijesh Singhe9df0942017-12-04 10:57:33 -06001207static __init int sev_hardware_setup(void)
1208{
1209 struct sev_user_data_status *status;
1210 int rc;
1211
1212 /* Maximum number of encrypted guests supported simultaneously */
1213 max_sev_asid = cpuid_ecx(0x8000001F);
1214
1215 if (!max_sev_asid)
1216 return 1;
1217
Brijesh Singh1654efc2017-12-04 10:57:34 -06001218 /* Minimum ASID value that should be used for SEV guest */
1219 min_sev_asid = cpuid_edx(0x8000001F);
1220
1221 /* Initialize SEV ASID bitmap */
1222 sev_asid_bitmap = kcalloc(BITS_TO_LONGS(max_sev_asid),
1223 sizeof(unsigned long), GFP_KERNEL);
1224 if (!sev_asid_bitmap)
1225 return 1;
1226
Brijesh Singhe9df0942017-12-04 10:57:33 -06001227 status = kmalloc(sizeof(*status), GFP_KERNEL);
1228 if (!status)
1229 return 1;
1230
1231 /*
1232 * Check SEV platform status.
1233 *
1234 * PLATFORM_STATUS can be called in any state, if we failed to query
1235 * the PLATFORM status then either PSP firmware does not support SEV
1236 * feature or SEV firmware is dead.
1237 */
1238 rc = sev_platform_status(status, NULL);
1239 if (rc)
1240 goto err;
1241
1242 pr_info("SEV supported\n");
1243
1244err:
1245 kfree(status);
1246 return rc;
1247}
1248
Babu Moger8566ac82018-03-16 16:37:26 -04001249static void grow_ple_window(struct kvm_vcpu *vcpu)
1250{
1251 struct vcpu_svm *svm = to_svm(vcpu);
1252 struct vmcb_control_area *control = &svm->vmcb->control;
1253 int old = control->pause_filter_count;
1254
1255 control->pause_filter_count = __grow_ple_window(old,
1256 pause_filter_count,
1257 pause_filter_count_grow,
1258 pause_filter_count_max);
1259
1260 if (control->pause_filter_count != old)
1261 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
1262
1263 trace_kvm_ple_window_grow(vcpu->vcpu_id,
1264 control->pause_filter_count, old);
1265}
1266
1267static void shrink_ple_window(struct kvm_vcpu *vcpu)
1268{
1269 struct vcpu_svm *svm = to_svm(vcpu);
1270 struct vmcb_control_area *control = &svm->vmcb->control;
1271 int old = control->pause_filter_count;
1272
1273 control->pause_filter_count =
1274 __shrink_ple_window(old,
1275 pause_filter_count,
1276 pause_filter_count_shrink,
1277 pause_filter_count);
1278 if (control->pause_filter_count != old)
1279 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
1280
1281 trace_kvm_ple_window_shrink(vcpu->vcpu_id,
1282 control->pause_filter_count, old);
1283}
1284
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285static __init int svm_hardware_setup(void)
1286{
1287 int cpu;
1288 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001289 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001290 int r;
1291
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
1293
1294 if (!iopm_pages)
1295 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +03001296
1297 iopm_va = page_address(iopm_pages);
1298 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
1300
Joerg Roedel323c3d82010-03-01 15:34:37 +01001301 init_msrpm_offsets();
1302
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001303 if (boot_cpu_has(X86_FEATURE_NX))
1304 kvm_enable_efer_bits(EFER_NX);
1305
Alexander Graf1b2fd702009-02-02 16:23:51 +01001306 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
1307 kvm_enable_efer_bits(EFER_FFXSR);
1308
Joerg Roedel92a1f122011-03-25 09:44:51 +01001309 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +01001310 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +08001311 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1312 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +01001313 }
1314
Babu Moger8566ac82018-03-16 16:37:26 -04001315 /* Check for pause filtering support */
1316 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
1317 pause_filter_count = 0;
1318 pause_filter_thresh = 0;
1319 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
1320 pause_filter_thresh = 0;
1321 }
1322
Alexander Graf236de052008-11-25 20:17:10 +01001323 if (nested) {
1324 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +02001325 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +01001326 }
1327
Brijesh Singhe9df0942017-12-04 10:57:33 -06001328 if (sev) {
1329 if (boot_cpu_has(X86_FEATURE_SEV) &&
1330 IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
1331 r = sev_hardware_setup();
1332 if (r)
1333 sev = false;
1334 } else {
1335 sev = false;
1336 }
1337 }
1338
Zachary Amsden3230bb42009-09-29 11:38:37 -10001339 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340 r = svm_cpu_init(cpu);
1341 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +01001342 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +01001344
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001345 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001346 npt_enabled = false;
1347
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001348 if (npt_enabled && !npt) {
1349 printk(KERN_INFO "kvm: Nested Paging disabled\n");
1350 npt_enabled = false;
1351 }
1352
Joerg Roedel18552672008-02-07 13:47:41 +01001353 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001354 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +01001355 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +02001356 } else
1357 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001358
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001359 if (avic) {
1360 if (!npt_enabled ||
1361 !boot_cpu_has(X86_FEATURE_AVIC) ||
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001362 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001363 avic = false;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001364 } else {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001365 pr_info("AVIC enabled\n");
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001366
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001367 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1368 }
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001369 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001370
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001371 if (vls) {
1372 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001373 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001374 !IS_ENABLED(CONFIG_X86_64)) {
1375 vls = false;
1376 } else {
1377 pr_info("Virtual VMLOAD VMSAVE supported\n");
1378 }
1379 }
1380
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001381 if (vgif) {
1382 if (!boot_cpu_has(X86_FEATURE_VGIF))
1383 vgif = false;
1384 else
1385 pr_info("Virtual GIF supported\n");
1386 }
1387
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 return 0;
1389
Joerg Roedelf65c2292008-02-13 18:58:46 +01001390err:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1392 iopm_base = 0;
1393 return r;
1394}
1395
1396static __exit void svm_hardware_unsetup(void)
1397{
Joerg Roedel0da1db752008-07-02 16:02:11 +02001398 int cpu;
1399
Brijesh Singh1654efc2017-12-04 10:57:34 -06001400 if (svm_sev_enabled())
1401 kfree(sev_asid_bitmap);
1402
Zachary Amsden3230bb42009-09-29 11:38:37 -10001403 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +02001404 svm_cpu_uninit(cpu);
1405
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001407 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408}
1409
1410static void init_seg(struct vmcb_seg *seg)
1411{
1412 seg->selector = 0;
1413 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001414 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415 seg->limit = 0xffff;
1416 seg->base = 0;
1417}
1418
1419static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1420{
1421 seg->selector = 0;
1422 seg->attrib = SVM_SELECTOR_P_MASK | type;
1423 seg->limit = 0xffff;
1424 seg->base = 0;
1425}
1426
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001427static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1428{
1429 struct vcpu_svm *svm = to_svm(vcpu);
1430 u64 g_tsc_offset = 0;
1431
Joerg Roedel20307532010-11-29 17:51:48 +01001432 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001433 g_tsc_offset = svm->vmcb->control.tsc_offset -
1434 svm->nested.hsave->control.tsc_offset;
1435 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001436 } else
1437 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1438 svm->vmcb->control.tsc_offset,
1439 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001440
1441 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001442
1443 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001444}
1445
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001446static void avic_init_vmcb(struct vcpu_svm *svm)
1447{
1448 struct vmcb *vmcb = svm->vmcb;
Sean Christopherson81811c12018-03-20 12:17:21 -07001449 struct kvm_svm *kvm_svm = to_kvm_svm(svm->vcpu.kvm);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001450 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
Sean Christopherson81811c12018-03-20 12:17:21 -07001451 phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page));
1452 phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page));
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001453
1454 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1455 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1456 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1457 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1458 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001459}
1460
Paolo Bonzini56908912015-10-19 11:30:19 +02001461static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462{
Joerg Roedele6101a92008-02-13 18:58:45 +01001463 struct vmcb_control_area *control = &svm->vmcb->control;
1464 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001466 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001467
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001468 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1469 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1470 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1471 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1472 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1473 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001474 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1475 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476
Paolo Bonzini5315c712014-03-03 13:08:29 +01001477 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478
Joerg Roedel18c918c2010-11-30 18:03:59 +01001479 set_exception_intercept(svm, PF_VECTOR);
1480 set_exception_intercept(svm, UD_VECTOR);
1481 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001482 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001483 set_exception_intercept(svm, DB_VECTOR);
Liran Alon97184202018-03-12 13:12:52 +02001484 /*
1485 * Guest access to VMware backdoor ports could legitimately
1486 * trigger #GP because of TSS I/O permission bitmap.
1487 * We intercept those #GP and allow access to them anyway
1488 * as VMware does.
1489 */
1490 if (enable_vmware_backdoor)
1491 set_exception_intercept(svm, GP_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001493 set_intercept(svm, INTERCEPT_INTR);
1494 set_intercept(svm, INTERCEPT_NMI);
1495 set_intercept(svm, INTERCEPT_SMI);
1496 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001497 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001498 set_intercept(svm, INTERCEPT_CPUID);
1499 set_intercept(svm, INTERCEPT_INVD);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001500 set_intercept(svm, INTERCEPT_INVLPG);
1501 set_intercept(svm, INTERCEPT_INVLPGA);
1502 set_intercept(svm, INTERCEPT_IOIO_PROT);
1503 set_intercept(svm, INTERCEPT_MSR_PROT);
1504 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1505 set_intercept(svm, INTERCEPT_SHUTDOWN);
1506 set_intercept(svm, INTERCEPT_VMRUN);
1507 set_intercept(svm, INTERCEPT_VMMCALL);
1508 set_intercept(svm, INTERCEPT_VMLOAD);
1509 set_intercept(svm, INTERCEPT_VMSAVE);
1510 set_intercept(svm, INTERCEPT_STGI);
1511 set_intercept(svm, INTERCEPT_CLGI);
1512 set_intercept(svm, INTERCEPT_SKINIT);
1513 set_intercept(svm, INTERCEPT_WBINVD);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001514 set_intercept(svm, INTERCEPT_XSETBV);
Brijesh Singh7607b712018-02-19 10:14:44 -06001515 set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516
Wanpeng Li4d5422c2018-03-12 04:53:02 -07001517 if (!kvm_mwait_in_guest(svm->vcpu.kvm)) {
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001518 set_intercept(svm, INTERCEPT_MONITOR);
1519 set_intercept(svm, INTERCEPT_MWAIT);
1520 }
1521
Wanpeng Licaa057a2018-03-12 04:53:03 -07001522 if (!kvm_hlt_in_guest(svm->vcpu.kvm))
1523 set_intercept(svm, INTERCEPT_HLT);
1524
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001525 control->iopm_base_pa = __sme_set(iopm_base);
1526 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527 control->int_ctl = V_INTR_MASKING_MASK;
1528
1529 init_seg(&save->es);
1530 init_seg(&save->ss);
1531 init_seg(&save->ds);
1532 init_seg(&save->fs);
1533 init_seg(&save->gs);
1534
1535 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001536 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537 /* Executable/Readable Code Segment */
1538 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1539 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1540 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001541
1542 save->gdtr.limit = 0xffff;
1543 save->idtr.limit = 0xffff;
1544
1545 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1546 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1547
Paolo Bonzini56908912015-10-19 11:30:19 +02001548 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001549 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001550 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001552 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553
Joerg Roedele0231712010-02-24 18:59:10 +01001554 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001555 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001556 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001558 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001559 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001560
Rusty Russell66aee912007-07-17 23:34:16 +10001561 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001562 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001563
1564 if (npt_enabled) {
1565 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001566 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001567 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001568 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001569 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1570 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001571 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001572 save->cr3 = 0;
1573 save->cr4 = 0;
1574 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001575 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001576
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001577 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001578 svm->vcpu.arch.hflags = 0;
1579
Babu Moger8566ac82018-03-16 16:37:26 -04001580 if (pause_filter_count) {
1581 control->pause_filter_count = pause_filter_count;
1582 if (pause_filter_thresh)
1583 control->pause_filter_thresh = pause_filter_thresh;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001584 set_intercept(svm, INTERCEPT_PAUSE);
Babu Moger8566ac82018-03-16 16:37:26 -04001585 } else {
1586 clr_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001587 }
1588
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001589 if (kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001590 avic_init_vmcb(svm);
1591
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001592 /*
1593 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1594 * in VMCB and clear intercepts to avoid #VMEXIT.
1595 */
1596 if (vls) {
1597 clr_intercept(svm, INTERCEPT_VMLOAD);
1598 clr_intercept(svm, INTERCEPT_VMSAVE);
1599 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1600 }
1601
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001602 if (vgif) {
1603 clr_intercept(svm, INTERCEPT_STGI);
1604 clr_intercept(svm, INTERCEPT_CLGI);
1605 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1606 }
1607
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001608 if (sev_guest(svm->vcpu.kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001609 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001610 clr_exception_intercept(svm, UD_VECTOR);
1611 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001612
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001613 mark_all_dirty(svm->vmcb);
1614
Joerg Roedel2af91942009-08-07 11:49:28 +02001615 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001616
1617}
1618
Dan Carpenterd3e7dec2017-05-18 10:38:53 +03001619static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1620 unsigned int index)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001621{
1622 u64 *avic_physical_id_table;
Sean Christopherson81811c12018-03-20 12:17:21 -07001623 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001624
1625 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1626 return NULL;
1627
Sean Christopherson81811c12018-03-20 12:17:21 -07001628 avic_physical_id_table = page_address(kvm_svm->avic_physical_id_table_page);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001629
1630 return &avic_physical_id_table[index];
1631}
1632
1633/**
1634 * Note:
1635 * AVIC hardware walks the nested page table to check permissions,
1636 * but does not use the SPA address specified in the leaf page
1637 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1638 * field of the VMCB. Therefore, we set up the
1639 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1640 */
1641static int avic_init_access_page(struct kvm_vcpu *vcpu)
1642{
1643 struct kvm *kvm = vcpu->kvm;
1644 int ret;
1645
1646 if (kvm->arch.apic_access_page_done)
1647 return 0;
1648
1649 ret = x86_set_memory_region(kvm,
1650 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1651 APIC_DEFAULT_PHYS_BASE,
1652 PAGE_SIZE);
1653 if (ret)
1654 return ret;
1655
1656 kvm->arch.apic_access_page_done = true;
1657 return 0;
1658}
1659
1660static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1661{
1662 int ret;
1663 u64 *entry, new_entry;
1664 int id = vcpu->vcpu_id;
1665 struct vcpu_svm *svm = to_svm(vcpu);
1666
1667 ret = avic_init_access_page(vcpu);
1668 if (ret)
1669 return ret;
1670
1671 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1672 return -EINVAL;
1673
1674 if (!svm->vcpu.arch.apic->regs)
1675 return -EINVAL;
1676
1677 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1678
1679 /* Setting AVIC backing page address in the phy APIC ID table */
1680 entry = avic_get_physical_id_entry(vcpu, id);
1681 if (!entry)
1682 return -EINVAL;
1683
1684 new_entry = READ_ONCE(*entry);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001685 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
1686 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1687 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001688 WRITE_ONCE(*entry, new_entry);
1689
1690 svm->avic_physical_id_cache = entry;
1691
1692 return 0;
1693}
1694
Brijesh Singh1654efc2017-12-04 10:57:34 -06001695static void __sev_asid_free(int asid)
1696{
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001697 struct svm_cpu_data *sd;
1698 int cpu, pos;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001699
1700 pos = asid - 1;
1701 clear_bit(pos, sev_asid_bitmap);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001702
1703 for_each_possible_cpu(cpu) {
1704 sd = per_cpu(svm_data, cpu);
1705 sd->sev_vmcbs[pos] = NULL;
1706 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001707}
1708
1709static void sev_asid_free(struct kvm *kvm)
1710{
Sean Christopherson81811c12018-03-20 12:17:21 -07001711 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001712
1713 __sev_asid_free(sev->asid);
1714}
1715
Brijesh Singh59414c92017-12-04 10:57:35 -06001716static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
1717{
1718 struct sev_data_decommission *decommission;
1719 struct sev_data_deactivate *data;
1720
1721 if (!handle)
1722 return;
1723
1724 data = kzalloc(sizeof(*data), GFP_KERNEL);
1725 if (!data)
1726 return;
1727
1728 /* deactivate handle */
1729 data->handle = handle;
1730 sev_guest_deactivate(data, NULL);
1731
1732 wbinvd_on_all_cpus();
1733 sev_guest_df_flush(NULL);
1734 kfree(data);
1735
1736 decommission = kzalloc(sizeof(*decommission), GFP_KERNEL);
1737 if (!decommission)
1738 return;
1739
1740 /* decommission handle */
1741 decommission->handle = handle;
1742 sev_guest_decommission(decommission, NULL);
1743
1744 kfree(decommission);
1745}
1746
Brijesh Singh89c50582017-12-04 10:57:35 -06001747static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
1748 unsigned long ulen, unsigned long *n,
1749 int write)
1750{
Sean Christopherson81811c12018-03-20 12:17:21 -07001751 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06001752 unsigned long npages, npinned, size;
1753 unsigned long locked, lock_limit;
1754 struct page **pages;
1755 int first, last;
1756
1757 /* Calculate number of pages. */
1758 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
1759 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
1760 npages = (last - first + 1);
1761
1762 locked = sev->pages_locked + npages;
1763 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1764 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
1765 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
1766 return NULL;
1767 }
1768
1769 /* Avoid using vmalloc for smaller buffers. */
1770 size = npages * sizeof(struct page *);
1771 if (size > PAGE_SIZE)
1772 pages = vmalloc(size);
1773 else
1774 pages = kmalloc(size, GFP_KERNEL);
1775
1776 if (!pages)
1777 return NULL;
1778
1779 /* Pin the user virtual address. */
1780 npinned = get_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
1781 if (npinned != npages) {
1782 pr_err("SEV: Failure locking %lu pages.\n", npages);
1783 goto err;
1784 }
1785
1786 *n = npages;
1787 sev->pages_locked = locked;
1788
1789 return pages;
1790
1791err:
1792 if (npinned > 0)
1793 release_pages(pages, npinned);
1794
1795 kvfree(pages);
1796 return NULL;
1797}
1798
1799static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
1800 unsigned long npages)
1801{
Sean Christopherson81811c12018-03-20 12:17:21 -07001802 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06001803
1804 release_pages(pages, npages);
1805 kvfree(pages);
1806 sev->pages_locked -= npages;
1807}
1808
1809static void sev_clflush_pages(struct page *pages[], unsigned long npages)
1810{
1811 uint8_t *page_virtual;
1812 unsigned long i;
1813
1814 if (npages == 0 || pages == NULL)
1815 return;
1816
1817 for (i = 0; i < npages; i++) {
1818 page_virtual = kmap_atomic(pages[i]);
1819 clflush_cache_range(page_virtual, PAGE_SIZE);
1820 kunmap_atomic(page_virtual);
1821 }
1822}
1823
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001824static void __unregister_enc_region_locked(struct kvm *kvm,
1825 struct enc_region *region)
1826{
1827 /*
1828 * The guest may change the memory encryption attribute from C=0 -> C=1
1829 * or vice versa for this memory range. Lets make sure caches are
1830 * flushed to ensure that guest data gets written into memory with
1831 * correct C-bit.
1832 */
1833 sev_clflush_pages(region->pages, region->npages);
1834
1835 sev_unpin_memory(kvm, region->pages, region->npages);
1836 list_del(&region->list);
1837 kfree(region);
1838}
1839
Sean Christopherson434a1e92018-03-20 12:17:18 -07001840static struct kvm *svm_vm_alloc(void)
1841{
Sean Christopherson81811c12018-03-20 12:17:21 -07001842 struct kvm_svm *kvm_svm = kzalloc(sizeof(struct kvm_svm), GFP_KERNEL);
1843 return &kvm_svm->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07001844}
1845
1846static void svm_vm_free(struct kvm *kvm)
1847{
Sean Christopherson81811c12018-03-20 12:17:21 -07001848 kfree(to_kvm_svm(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07001849}
1850
Brijesh Singh1654efc2017-12-04 10:57:34 -06001851static void sev_vm_destroy(struct kvm *kvm)
1852{
Sean Christopherson81811c12018-03-20 12:17:21 -07001853 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001854 struct list_head *head = &sev->regions_list;
1855 struct list_head *pos, *q;
Brijesh Singh59414c92017-12-04 10:57:35 -06001856
Brijesh Singh1654efc2017-12-04 10:57:34 -06001857 if (!sev_guest(kvm))
1858 return;
1859
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001860 mutex_lock(&kvm->lock);
1861
1862 /*
1863 * if userspace was terminated before unregistering the memory regions
1864 * then lets unpin all the registered memory.
1865 */
1866 if (!list_empty(head)) {
1867 list_for_each_safe(pos, q, head) {
1868 __unregister_enc_region_locked(kvm,
1869 list_entry(pos, struct enc_region, list));
1870 }
1871 }
1872
1873 mutex_unlock(&kvm->lock);
1874
Brijesh Singh59414c92017-12-04 10:57:35 -06001875 sev_unbind_asid(kvm, sev->handle);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001876 sev_asid_free(kvm);
1877}
1878
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001879static void avic_vm_destroy(struct kvm *kvm)
1880{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001881 unsigned long flags;
Sean Christopherson81811c12018-03-20 12:17:21 -07001882 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001883
Dmitry Vyukov3863dff2017-01-24 14:06:48 +01001884 if (!avic)
1885 return;
1886
Sean Christopherson81811c12018-03-20 12:17:21 -07001887 if (kvm_svm->avic_logical_id_table_page)
1888 __free_page(kvm_svm->avic_logical_id_table_page);
1889 if (kvm_svm->avic_physical_id_table_page)
1890 __free_page(kvm_svm->avic_physical_id_table_page);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001891
1892 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Sean Christopherson81811c12018-03-20 12:17:21 -07001893 hash_del(&kvm_svm->hnode);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001894 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001895}
1896
Brijesh Singh1654efc2017-12-04 10:57:34 -06001897static void svm_vm_destroy(struct kvm *kvm)
1898{
1899 avic_vm_destroy(kvm);
1900 sev_vm_destroy(kvm);
1901}
1902
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001903static int avic_vm_init(struct kvm *kvm)
1904{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001905 unsigned long flags;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001906 int err = -ENOMEM;
Sean Christopherson81811c12018-03-20 12:17:21 -07001907 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
1908 struct kvm_svm *k2;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001909 struct page *p_page;
1910 struct page *l_page;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001911 u32 vm_id;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001912
1913 if (!avic)
1914 return 0;
1915
1916 /* Allocating physical APIC ID table (4KB) */
1917 p_page = alloc_page(GFP_KERNEL);
1918 if (!p_page)
1919 goto free_avic;
1920
Sean Christopherson81811c12018-03-20 12:17:21 -07001921 kvm_svm->avic_physical_id_table_page = p_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001922 clear_page(page_address(p_page));
1923
1924 /* Allocating logical APIC ID table (4KB) */
1925 l_page = alloc_page(GFP_KERNEL);
1926 if (!l_page)
1927 goto free_avic;
1928
Sean Christopherson81811c12018-03-20 12:17:21 -07001929 kvm_svm->avic_logical_id_table_page = l_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001930 clear_page(page_address(l_page));
1931
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001932 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001933 again:
1934 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
1935 if (vm_id == 0) { /* id is 1-based, zero is not okay */
1936 next_vm_id_wrapped = 1;
1937 goto again;
1938 }
1939 /* Is it still in use? Only possible if wrapped at least once */
1940 if (next_vm_id_wrapped) {
Sean Christopherson81811c12018-03-20 12:17:21 -07001941 hash_for_each_possible(svm_vm_data_hash, k2, hnode, vm_id) {
1942 if (k2->avic_vm_id == vm_id)
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001943 goto again;
1944 }
1945 }
Sean Christopherson81811c12018-03-20 12:17:21 -07001946 kvm_svm->avic_vm_id = vm_id;
1947 hash_add(svm_vm_data_hash, &kvm_svm->hnode, kvm_svm->avic_vm_id);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001948 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1949
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001950 return 0;
1951
1952free_avic:
1953 avic_vm_destroy(kvm);
1954 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001955}
1956
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001957static inline int
1958avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001959{
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001960 int ret = 0;
1961 unsigned long flags;
1962 struct amd_svm_iommu_ir *ir;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001963 struct vcpu_svm *svm = to_svm(vcpu);
1964
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001965 if (!kvm_arch_has_assigned_device(vcpu->kvm))
1966 return 0;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001967
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001968 /*
1969 * Here, we go through the per-vcpu ir_list to update all existing
1970 * interrupt remapping table entry targeting this vcpu.
1971 */
1972 spin_lock_irqsave(&svm->ir_list_lock, flags);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001973
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001974 if (list_empty(&svm->ir_list))
1975 goto out;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001976
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001977 list_for_each_entry(ir, &svm->ir_list, node) {
1978 ret = amd_iommu_update_ga(cpu, r, ir->data);
1979 if (ret)
1980 break;
1981 }
1982out:
1983 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
1984 return ret;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001985}
1986
1987static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1988{
1989 u64 entry;
1990 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05001991 int h_physical_id = kvm_cpu_get_apicid(cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001992 struct vcpu_svm *svm = to_svm(vcpu);
1993
1994 if (!kvm_vcpu_apicv_active(vcpu))
1995 return;
1996
1997 if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
1998 return;
1999
2000 entry = READ_ONCE(*(svm->avic_physical_id_cache));
2001 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
2002
2003 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
2004 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
2005
2006 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2007 if (svm->avic_is_running)
2008 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2009
2010 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002011 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
2012 svm->avic_is_running);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002013}
2014
2015static void avic_vcpu_put(struct kvm_vcpu *vcpu)
2016{
2017 u64 entry;
2018 struct vcpu_svm *svm = to_svm(vcpu);
2019
2020 if (!kvm_vcpu_apicv_active(vcpu))
2021 return;
2022
2023 entry = READ_ONCE(*(svm->avic_physical_id_cache));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002024 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
2025 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
2026
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002027 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2028 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002029}
2030
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002031/**
2032 * This function is called during VCPU halt/unhalt.
2033 */
2034static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
2035{
2036 struct vcpu_svm *svm = to_svm(vcpu);
2037
2038 svm->avic_is_running = is_run;
2039 if (is_run)
2040 avic_vcpu_load(vcpu, vcpu->cpu);
2041 else
2042 avic_vcpu_put(vcpu);
2043}
2044
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002045static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03002046{
2047 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01002048 u32 dummy;
2049 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002050
Wanpeng Li518e7b92018-02-28 14:03:31 +08002051 vcpu->arch.microcode_version = 0x01000065;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002052 svm->spec_ctrl = 0;
2053
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002054 if (!init_event) {
2055 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
2056 MSR_IA32_APICBASE_ENABLE;
2057 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
2058 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
2059 }
Paolo Bonzini56908912015-10-19 11:30:19 +02002060 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02002061
Yu Zhange911eb32017-08-24 20:27:52 +08002062 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
Julian Stecklina66f7b722012-12-05 15:26:19 +01002063 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002064
2065 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
2066 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03002067}
2068
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002069static int avic_init_vcpu(struct vcpu_svm *svm)
2070{
2071 int ret;
2072
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05002073 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002074 return 0;
2075
2076 ret = avic_init_backing_page(&svm->vcpu);
2077 if (ret)
2078 return ret;
2079
2080 INIT_LIST_HEAD(&svm->ir_list);
2081 spin_lock_init(&svm->ir_list_lock);
2082
2083 return ret;
2084}
2085
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002086static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002088 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002089 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01002090 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002091 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002092 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002093 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094
Rusty Russellc16f8622007-07-30 21:12:19 +10002095 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002096 if (!svm) {
2097 err = -ENOMEM;
2098 goto out;
2099 }
2100
2101 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
2102 if (err)
2103 goto free_svm;
2104
Joerg Roedelf65c2292008-02-13 18:58:46 +01002105 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002106 page = alloc_page(GFP_KERNEL);
2107 if (!page)
2108 goto uninit;
2109
Joerg Roedelf65c2292008-02-13 18:58:46 +01002110 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
2111 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002112 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002113
2114 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
2115 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002116 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01002117
Alexander Grafb286d5d2008-11-25 20:17:05 +01002118 hsave_page = alloc_page(GFP_KERNEL);
2119 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002120 goto free_page3;
2121
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002122 err = avic_init_vcpu(svm);
2123 if (err)
2124 goto free_page4;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002125
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002126 /* We initialize this flag to true to make sure that the is_running
2127 * bit would be set the first time the vcpu is loaded.
2128 */
2129 svm->avic_is_running = true;
2130
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002131 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01002132
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002133 svm->msrpm = page_address(msrpm_pages);
2134 svm_vcpu_init_msrpm(svm->msrpm);
2135
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002136 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002137 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002138
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002139 svm->vmcb = page_address(page);
2140 clear_page(svm->vmcb);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002141 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002142 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02002143 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002145 svm_init_osvw(&svm->vcpu);
2146
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002147 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08002148
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002149free_page4:
2150 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002151free_page3:
2152 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
2153free_page2:
2154 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
2155free_page1:
2156 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002157uninit:
2158 kvm_vcpu_uninit(&svm->vcpu);
2159free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10002160 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002161out:
2162 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163}
2164
2165static void svm_free_vcpu(struct kvm_vcpu *vcpu)
2166{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002167 struct vcpu_svm *svm = to_svm(vcpu);
2168
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002169 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01002170 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002171 __free_page(virt_to_page(svm->nested.hsave));
2172 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002173 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002174 kmem_cache_free(kvm_vcpu_cache, svm);
Ashok Raj15d45072018-02-01 22:59:43 +01002175 /*
2176 * The vmcb page can be recycled, causing a false negative in
2177 * svm_vcpu_load(). So do a full IBPB now.
2178 */
2179 indirect_branch_prediction_barrier();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180}
2181
Avi Kivity15ad7142007-07-11 18:17:21 +03002182static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002184 struct vcpu_svm *svm = to_svm(vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01002185 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002186 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02002187
Avi Kivity0cc50642007-03-25 12:07:27 +02002188 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002189 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002190 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02002191 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002192
Avi Kivity82ca2d12010-10-21 12:20:34 +02002193#ifdef CONFIG_X86_64
2194 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
2195#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02002196 savesegment(fs, svm->host.fs);
2197 savesegment(gs, svm->host.gs);
2198 svm->host.ldt = kvm_read_ldt();
2199
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002200 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002201 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002202
Haozhong Zhangad7218832015-10-20 15:39:02 +08002203 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
2204 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2205 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
2206 __this_cpu_write(current_tsc_ratio, tsc_ratio);
2207 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
2208 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002209 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01002210 /* This assumes that the kernel never uses MSR_TSC_AUX */
2211 if (static_cpu_has(X86_FEATURE_RDTSCP))
2212 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002213
Ashok Raj15d45072018-02-01 22:59:43 +01002214 if (sd->current_vmcb != svm->vmcb) {
2215 sd->current_vmcb = svm->vmcb;
2216 indirect_branch_prediction_barrier();
2217 }
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002218 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219}
2220
2221static void svm_vcpu_put(struct kvm_vcpu *vcpu)
2222{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002223 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002224 int i;
2225
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002226 avic_vcpu_put(vcpu);
2227
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002228 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02002229 kvm_load_ldt(svm->host.ldt);
2230#ifdef CONFIG_X86_64
2231 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07002232 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01002233 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02002234#else
Avi Kivity831ca602011-03-08 16:09:51 +02002235#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02002236 loadsegment(gs, svm->host.gs);
2237#endif
Avi Kivity831ca602011-03-08 16:09:51 +02002238#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002239 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002240 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241}
2242
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002243static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
2244{
2245 avic_set_running(vcpu, false);
2246}
2247
2248static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
2249{
2250 avic_set_running(vcpu, true);
2251}
2252
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
2254{
Ladi Prosek9b611742017-06-21 09:06:59 +02002255 struct vcpu_svm *svm = to_svm(vcpu);
2256 unsigned long rflags = svm->vmcb->save.rflags;
2257
2258 if (svm->nmi_singlestep) {
2259 /* Hide our flags if they were not set by the guest */
2260 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
2261 rflags &= ~X86_EFLAGS_TF;
2262 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
2263 rflags &= ~X86_EFLAGS_RF;
2264 }
2265 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266}
2267
2268static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2269{
Ladi Prosek9b611742017-06-21 09:06:59 +02002270 if (to_svm(vcpu)->nmi_singlestep)
2271 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2272
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002273 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02002274 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002275 * (caused by either a task switch or an inter-privilege IRET),
2276 * so we do not need to update the CPL here.
2277 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002278 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279}
2280
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002281static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
2282{
2283 switch (reg) {
2284 case VCPU_EXREG_PDPTR:
2285 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002286 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002287 break;
2288 default:
2289 BUG();
2290 }
2291}
2292
Alexander Graff0b85052008-11-25 20:17:01 +01002293static void svm_set_vintr(struct vcpu_svm *svm)
2294{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002295 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002296}
2297
2298static void svm_clear_vintr(struct vcpu_svm *svm)
2299{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002300 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002301}
2302
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
2304{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002305 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306
2307 switch (seg) {
2308 case VCPU_SREG_CS: return &save->cs;
2309 case VCPU_SREG_DS: return &save->ds;
2310 case VCPU_SREG_ES: return &save->es;
2311 case VCPU_SREG_FS: return &save->fs;
2312 case VCPU_SREG_GS: return &save->gs;
2313 case VCPU_SREG_SS: return &save->ss;
2314 case VCPU_SREG_TR: return &save->tr;
2315 case VCPU_SREG_LDTR: return &save->ldtr;
2316 }
2317 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00002318 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319}
2320
2321static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2322{
2323 struct vmcb_seg *s = svm_seg(vcpu, seg);
2324
2325 return s->base;
2326}
2327
2328static void svm_get_segment(struct kvm_vcpu *vcpu,
2329 struct kvm_segment *var, int seg)
2330{
2331 struct vmcb_seg *s = svm_seg(vcpu, seg);
2332
2333 var->base = s->base;
2334 var->limit = s->limit;
2335 var->selector = s->selector;
2336 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
2337 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
2338 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
2339 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
2340 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
2341 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
2342 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05302343
2344 /*
2345 * AMD CPUs circa 2014 track the G bit for all segments except CS.
2346 * However, the SVM spec states that the G bit is not observed by the
2347 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
2348 * So let's synthesize a legal G bit for all segments, this helps
2349 * running KVM nested. It also helps cross-vendor migration, because
2350 * Intel's vmentry has a check on the 'G' bit.
2351 */
2352 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00002353
Joerg Roedele0231712010-02-24 18:59:10 +01002354 /*
2355 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02002356 * for cross vendor migration purposes by "not present"
2357 */
Gioh Kim8eae9572017-05-30 15:24:45 +02002358 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02002359
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002360 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002361 case VCPU_SREG_TR:
2362 /*
2363 * Work around a bug where the busy flag in the tr selector
2364 * isn't exposed
2365 */
Amit Shahc0d09822008-10-27 09:04:18 +00002366 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002367 break;
2368 case VCPU_SREG_DS:
2369 case VCPU_SREG_ES:
2370 case VCPU_SREG_FS:
2371 case VCPU_SREG_GS:
2372 /*
2373 * The accessed bit must always be set in the segment
2374 * descriptor cache, although it can be cleared in the
2375 * descriptor, the cached bit always remains at 1. Since
2376 * Intel has a check on this, set it here to support
2377 * cross-vendor migration.
2378 */
2379 if (!var->unusable)
2380 var->type |= 0x1;
2381 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02002382 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01002383 /*
2384 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02002385 * descriptor is left as 1, although the whole segment has
2386 * been made unusable. Clear it here to pass an Intel VMX
2387 * entry check when cross vendor migrating.
2388 */
2389 if (var->unusable)
2390 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02002391 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02002392 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02002393 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002394 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395}
2396
Izik Eidus2e4d2652008-03-24 19:38:34 +02002397static int svm_get_cpl(struct kvm_vcpu *vcpu)
2398{
2399 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
2400
2401 return save->cpl;
2402}
2403
Gleb Natapov89a27f42010-02-16 10:51:48 +02002404static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002406 struct vcpu_svm *svm = to_svm(vcpu);
2407
Gleb Natapov89a27f42010-02-16 10:51:48 +02002408 dt->size = svm->vmcb->save.idtr.limit;
2409 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410}
2411
Gleb Natapov89a27f42010-02-16 10:51:48 +02002412static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002414 struct vcpu_svm *svm = to_svm(vcpu);
2415
Gleb Natapov89a27f42010-02-16 10:51:48 +02002416 svm->vmcb->save.idtr.limit = dt->size;
2417 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002418 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419}
2420
Gleb Natapov89a27f42010-02-16 10:51:48 +02002421static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002423 struct vcpu_svm *svm = to_svm(vcpu);
2424
Gleb Natapov89a27f42010-02-16 10:51:48 +02002425 dt->size = svm->vmcb->save.gdtr.limit;
2426 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427}
2428
Gleb Natapov89a27f42010-02-16 10:51:48 +02002429static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002431 struct vcpu_svm *svm = to_svm(vcpu);
2432
Gleb Natapov89a27f42010-02-16 10:51:48 +02002433 svm->vmcb->save.gdtr.limit = dt->size;
2434 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002435 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436}
2437
Avi Kivitye8467fd2009-12-29 18:43:06 +02002438static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2439{
2440}
2441
Avi Kivityaff48ba2010-12-05 18:56:11 +02002442static void svm_decache_cr3(struct kvm_vcpu *vcpu)
2443{
2444}
2445
Anthony Liguori25c4c272007-04-27 09:29:21 +03002446static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002447{
2448}
2449
Avi Kivityd2251572010-01-06 10:55:27 +02002450static void update_cr0_intercept(struct vcpu_svm *svm)
2451{
2452 ulong gcr0 = svm->vcpu.arch.cr0;
2453 u64 *hcr0 = &svm->vmcb->save.cr0;
2454
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002455 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
2456 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
Avi Kivityd2251572010-01-06 10:55:27 +02002457
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002458 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002459
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002460 if (gcr0 == *hcr0) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002461 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
2462 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002463 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002464 set_cr_intercept(svm, INTERCEPT_CR0_READ);
2465 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002466 }
2467}
2468
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2470{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002471 struct vcpu_svm *svm = to_svm(vcpu);
2472
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002473#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002474 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002475 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002476 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002477 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478 }
2479
Mike Dayd77c26f2007-10-08 09:02:08 -04002480 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002481 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002482 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002483 }
2484 }
2485#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002486 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002487
2488 if (!npt_enabled)
2489 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02002490
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02002491 /*
2492 * re-enable caching here because the QEMU bios
2493 * does not do it - this results in some delay at
2494 * reboot
2495 */
2496 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
2497 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002498 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002499 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002500 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501}
2502
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002503static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002505 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002506 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
2507
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002508 if (cr4 & X86_CR4_VMXE)
2509 return 1;
2510
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002511 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002512 svm_flush_tlb(vcpu, true);
Joerg Roedel6394b642008-04-09 14:15:29 +02002513
Joerg Roedelec077262008-04-09 14:15:28 +02002514 vcpu->arch.cr4 = cr4;
2515 if (!npt_enabled)
2516 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02002517 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02002518 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002519 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002520 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521}
2522
2523static void svm_set_segment(struct kvm_vcpu *vcpu,
2524 struct kvm_segment *var, int seg)
2525{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002526 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527 struct vmcb_seg *s = svm_seg(vcpu, seg);
2528
2529 s->base = var->base;
2530 s->limit = var->limit;
2531 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02002532 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2533 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2534 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2535 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2536 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2537 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2538 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2539 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002540
2541 /*
2542 * This is always accurate, except if SYSRET returned to a segment
2543 * with SS.DPL != 3. Intel does not have this quirk, and always
2544 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2545 * would entail passing the CPL to userspace and back.
2546 */
2547 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02002548 /* This is symmetric with svm_get_segment() */
2549 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002550
Joerg Roedel060d0c92010-12-03 11:45:57 +01002551 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552}
2553
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01002554static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002556 struct vcpu_svm *svm = to_svm(vcpu);
2557
Joerg Roedel18c918c2010-11-30 18:03:59 +01002558 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03002559
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002560 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002561 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01002562 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002563 } else
2564 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03002565}
2566
Tejun Heo0fe1e002009-10-29 22:34:14 +09002567static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568{
Tejun Heo0fe1e002009-10-29 22:34:14 +09002569 if (sd->next_asid > sd->max_asid) {
2570 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06002571 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002572 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002573 }
2574
Tejun Heo0fe1e002009-10-29 22:34:14 +09002575 svm->asid_generation = sd->asid_generation;
2576 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01002577
2578 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002579}
2580
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002581static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2582{
2583 return to_svm(vcpu)->vmcb->save.dr6;
2584}
2585
2586static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2587{
2588 struct vcpu_svm *svm = to_svm(vcpu);
2589
2590 svm->vmcb->save.dr6 = value;
2591 mark_dirty(svm->vmcb, VMCB_DR);
2592}
2593
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002594static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2595{
2596 struct vcpu_svm *svm = to_svm(vcpu);
2597
2598 get_debugreg(vcpu->arch.db[0], 0);
2599 get_debugreg(vcpu->arch.db[1], 1);
2600 get_debugreg(vcpu->arch.db[2], 2);
2601 get_debugreg(vcpu->arch.db[3], 3);
2602 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2603 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2604
2605 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2606 set_dr_intercepts(svm);
2607}
2608
Gleb Natapov020df072010-04-13 10:05:23 +03002609static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002611 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002612
Gleb Natapov020df072010-04-13 10:05:23 +03002613 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01002614 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615}
2616
Avi Kivity851ba692009-08-24 11:10:17 +03002617static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002618{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002619 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002620 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002622 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002623 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2624 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002625 svm->vmcb->control.insn_len);
2626}
2627
2628static int npf_interception(struct vcpu_svm *svm)
2629{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002630 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Paolo Bonzinid0006532017-08-11 18:36:43 +02002631 u64 error_code = svm->vmcb->control.exit_info_1;
2632
2633 trace_kvm_page_fault(fault_address, error_code);
2634 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002635 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2636 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002637 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638}
2639
Avi Kivity851ba692009-08-24 11:10:17 +03002640static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002641{
Avi Kivity851ba692009-08-24 11:10:17 +03002642 struct kvm_run *kvm_run = svm->vcpu.run;
2643
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002644 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03002645 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02002646 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002647 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2648 return 1;
2649 }
Gleb Natapov44c11432009-05-11 13:35:52 +03002650
Jan Kiszka6be7d302009-10-18 13:24:54 +02002651 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02002652 disable_nmi_singlestep(svm);
Gleb Natapov44c11432009-05-11 13:35:52 +03002653 }
2654
2655 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002656 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002657 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2658 kvm_run->debug.arch.pc =
2659 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2660 kvm_run->debug.arch.exception = DB_VECTOR;
2661 return 0;
2662 }
2663
2664 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002665}
2666
Avi Kivity851ba692009-08-24 11:10:17 +03002667static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002668{
Avi Kivity851ba692009-08-24 11:10:17 +03002669 struct kvm_run *kvm_run = svm->vcpu.run;
2670
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002671 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2672 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2673 kvm_run->debug.arch.exception = BP_VECTOR;
2674 return 0;
2675}
2676
Avi Kivity851ba692009-08-24 11:10:17 +03002677static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002678{
Wanpeng Li082d06e2018-04-03 16:28:48 -07002679 return handle_ud(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002680}
2681
Eric Northup54a20552015-11-03 18:03:53 +01002682static int ac_interception(struct vcpu_svm *svm)
2683{
2684 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2685 return 1;
2686}
2687
Liran Alon97184202018-03-12 13:12:52 +02002688static int gp_interception(struct vcpu_svm *svm)
2689{
2690 struct kvm_vcpu *vcpu = &svm->vcpu;
2691 u32 error_code = svm->vmcb->control.exit_info_1;
2692 int er;
2693
2694 WARN_ON_ONCE(!enable_vmware_backdoor);
2695
2696 er = emulate_instruction(vcpu,
2697 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
2698 if (er == EMULATE_USER_EXIT)
2699 return 0;
2700 else if (er != EMULATE_DONE)
2701 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2702 return 1;
2703}
2704
Joerg Roedel67ec6602010-05-17 14:43:35 +02002705static bool is_erratum_383(void)
2706{
2707 int err, i;
2708 u64 value;
2709
2710 if (!erratum_383_found)
2711 return false;
2712
2713 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2714 if (err)
2715 return false;
2716
2717 /* Bit 62 may or may not be set for this mce */
2718 value &= ~(1ULL << 62);
2719
2720 if (value != 0xb600000000010015ULL)
2721 return false;
2722
2723 /* Clear MCi_STATUS registers */
2724 for (i = 0; i < 6; ++i)
2725 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2726
2727 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2728 if (!err) {
2729 u32 low, high;
2730
2731 value &= ~(1ULL << 2);
2732 low = lower_32_bits(value);
2733 high = upper_32_bits(value);
2734
2735 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2736 }
2737
2738 /* Flush tlb to evict multi-match entries */
2739 __flush_tlb_all();
2740
2741 return true;
2742}
2743
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002744static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002745{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002746 if (is_erratum_383()) {
2747 /*
2748 * Erratum 383 triggered. Guest state is corrupt so kill the
2749 * guest.
2750 */
2751 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2752
Avi Kivitya8eeb042010-05-10 12:34:53 +03002753 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002754
2755 return;
2756 }
2757
Joerg Roedel53371b52008-04-09 14:15:30 +02002758 /*
2759 * On an #MC intercept the MCE handler is not called automatically in
2760 * the host. So do it by hand here.
2761 */
2762 asm volatile (
2763 "int $0x12\n");
2764 /* not sure if we ever come back to this point */
2765
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002766 return;
2767}
2768
2769static int mc_interception(struct vcpu_svm *svm)
2770{
Joerg Roedel53371b52008-04-09 14:15:30 +02002771 return 1;
2772}
2773
Avi Kivity851ba692009-08-24 11:10:17 +03002774static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002775{
Avi Kivity851ba692009-08-24 11:10:17 +03002776 struct kvm_run *kvm_run = svm->vcpu.run;
2777
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002778 /*
2779 * VMCB is undefined after a SHUTDOWN intercept
2780 * so reinitialize it.
2781 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002782 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002783 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002784
2785 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2786 return 0;
2787}
2788
Avi Kivity851ba692009-08-24 11:10:17 +03002789static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002791 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002792 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Sean Christophersondca7f122018-03-08 08:57:27 -08002793 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002794 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002795
Rusty Russelle756fc62007-07-30 20:07:08 +10002796 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002797 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002798 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002799 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01002800 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002801
Avi Kivity039576c2007-03-20 12:46:50 +02002802 port = io_info >> 16;
2803 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002804 svm->next_rip = svm->vmcb->control.exit_info_2;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002805
Sean Christophersondca7f122018-03-08 08:57:27 -08002806 return kvm_fast_pio(&svm->vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807}
2808
Avi Kivity851ba692009-08-24 11:10:17 +03002809static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002810{
2811 return 1;
2812}
2813
Avi Kivity851ba692009-08-24 11:10:17 +03002814static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002815{
2816 ++svm->vcpu.stat.irq_exits;
2817 return 1;
2818}
2819
Avi Kivity851ba692009-08-24 11:10:17 +03002820static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821{
2822 return 1;
2823}
2824
Avi Kivity851ba692009-08-24 11:10:17 +03002825static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002827 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002828 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829}
2830
Avi Kivity851ba692009-08-24 11:10:17 +03002831static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002832{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002833 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002834 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002835}
2836
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002837static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2838{
2839 struct vcpu_svm *svm = to_svm(vcpu);
2840
2841 return svm->nested.nested_cr3;
2842}
2843
Avi Kivitye4e517b2011-07-28 11:36:17 +03002844static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2845{
2846 struct vcpu_svm *svm = to_svm(vcpu);
2847 u64 cr3 = svm->nested.nested_cr3;
2848 u64 pdpte;
2849 int ret;
2850
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002851 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002852 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002853 if (ret)
2854 return 0;
2855 return pdpte;
2856}
2857
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002858static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2859 unsigned long root)
2860{
2861 struct vcpu_svm *svm = to_svm(vcpu);
2862
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002863 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01002864 mark_dirty(svm->vmcb, VMCB_NPT);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002865 svm_flush_tlb(vcpu, true);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002866}
2867
Avi Kivity6389ee92010-11-29 16:12:30 +02002868static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2869 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002870{
2871 struct vcpu_svm *svm = to_svm(vcpu);
2872
Paolo Bonzini5e352512014-09-02 13:18:37 +02002873 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2874 /*
2875 * TODO: track the cause of the nested page fault, and
2876 * correctly fill in the high bits of exit_info_1.
2877 */
2878 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2879 svm->vmcb->control.exit_code_hi = 0;
2880 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2881 svm->vmcb->control.exit_info_2 = fault->address;
2882 }
2883
2884 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2885 svm->vmcb->control.exit_info_1 |= fault->error_code;
2886
2887 /*
2888 * The present bit is always zero for page structure faults on real
2889 * hardware.
2890 */
2891 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2892 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002893
2894 nested_svm_vmexit(svm);
2895}
2896
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002897static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002898{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002899 WARN_ON(mmu_is_nested(vcpu));
2900 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002901 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2902 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002903 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002904 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
Yu Zhang855feb62017-08-24 20:27:55 +08002905 vcpu->arch.mmu.shadow_root_level = get_npt_level(vcpu);
Xiao Guangrongc258b622015-08-05 12:04:24 +08002906 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002907 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002908}
2909
2910static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2911{
2912 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2913}
2914
Alexander Grafc0725422008-11-25 20:17:03 +01002915static int nested_svm_check_permissions(struct vcpu_svm *svm)
2916{
Dan Carpentere9196ce2017-05-18 10:39:53 +03002917 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
2918 !is_paging(&svm->vcpu)) {
Alexander Grafc0725422008-11-25 20:17:03 +01002919 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2920 return 1;
2921 }
2922
2923 if (svm->vmcb->save.cpl) {
2924 kvm_inject_gp(&svm->vcpu, 0);
2925 return 1;
2926 }
2927
Dan Carpentere9196ce2017-05-18 10:39:53 +03002928 return 0;
Alexander Grafc0725422008-11-25 20:17:03 +01002929}
2930
Alexander Grafcf74a782008-11-25 20:17:08 +01002931static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2932 bool has_error_code, u32 error_code)
2933{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002934 int vmexit;
2935
Joerg Roedel20307532010-11-29 17:51:48 +01002936 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002937 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002938
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002939 vmexit = nested_svm_intercept(svm);
2940 if (vmexit != NESTED_EXIT_DONE)
2941 return 0;
2942
Joerg Roedel0295ad72009-08-07 11:49:37 +02002943 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2944 svm->vmcb->control.exit_code_hi = 0;
2945 svm->vmcb->control.exit_info_1 = error_code;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002946
2947 /*
2948 * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception.
2949 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2950 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be
2951 * written only when inject_pending_event runs (DR6 would written here
2952 * too). This should be conditional on a new capability---if the
2953 * capability is disabled, kvm_multiple_exception would write the
2954 * ancillary information to CR2 or DR6, for backwards ABI-compatibility.
2955 */
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002956 if (svm->vcpu.arch.exception.nested_apf)
2957 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
2958 else
2959 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
Joerg Roedel0295ad72009-08-07 11:49:37 +02002960
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002961 svm->nested.exit_required = true;
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002962 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002963}
2964
Joerg Roedel8fe54652010-02-19 16:23:01 +01002965/* This function returns true if it is save to enable the irq window */
2966static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002967{
Joerg Roedel20307532010-11-29 17:51:48 +01002968 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002969 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002970
Joerg Roedel26666952009-08-07 11:49:46 +02002971 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002972 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002973
Joerg Roedel26666952009-08-07 11:49:46 +02002974 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002975 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002976
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002977 /*
2978 * if vmexit was already requested (by intercepted exception
2979 * for instance) do not overwrite it with "external interrupt"
2980 * vmexit.
2981 */
2982 if (svm->nested.exit_required)
2983 return false;
2984
Joerg Roedel197717d2010-02-24 18:59:19 +01002985 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2986 svm->vmcb->control.exit_info_1 = 0;
2987 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002988
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002989 if (svm->nested.intercept & 1ULL) {
2990 /*
2991 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002992 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002993 * #vmexit emulation might sleep. Only signal request for
2994 * the #vmexit here.
2995 */
2996 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002997 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002998 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002999 }
3000
Joerg Roedel8fe54652010-02-19 16:23:01 +01003001 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01003002}
3003
Joerg Roedel887f5002010-02-24 18:59:12 +01003004/* This function returns true if it is save to enable the nmi window */
3005static inline bool nested_svm_nmi(struct vcpu_svm *svm)
3006{
Joerg Roedel20307532010-11-29 17:51:48 +01003007 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01003008 return true;
3009
3010 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
3011 return true;
3012
3013 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
3014 svm->nested.exit_required = true;
3015
3016 return false;
3017}
3018
Joerg Roedel7597f122010-02-19 16:23:00 +01003019static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003020{
3021 struct page *page;
3022
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01003023 might_sleep();
3024
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003025 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003026 if (is_error_page(page))
3027 goto error;
3028
Joerg Roedel7597f122010-02-19 16:23:00 +01003029 *_page = page;
3030
3031 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003032
3033error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003034 kvm_inject_gp(&svm->vcpu, 0);
3035
3036 return NULL;
3037}
3038
Joerg Roedel7597f122010-02-19 16:23:00 +01003039static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003040{
Joerg Roedel7597f122010-02-19 16:23:00 +01003041 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003042 kvm_release_page_dirty(page);
3043}
3044
Joerg Roedelce2ac082010-03-01 15:34:39 +01003045static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003046{
Jan Kiszka9bf41832014-06-30 10:54:17 +02003047 unsigned port, size, iopm_len;
3048 u16 val, mask;
3049 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003050 u64 gpa;
3051
3052 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
3053 return NESTED_EXIT_HOST;
3054
3055 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02003056 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
3057 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003058 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02003059 start_bit = port % 8;
3060 iopm_len = (start_bit + size > 8) ? 2 : 1;
3061 mask = (0xf >> (4 - size)) << start_bit;
3062 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003063
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003064 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02003065 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003066
Jan Kiszka9bf41832014-06-30 10:54:17 +02003067 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003068}
3069
Joerg Roedeld2477822010-03-01 15:34:34 +01003070static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003071{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003072 u32 offset, msr, value;
3073 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003074
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003075 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01003076 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003077
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003078 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
3079 offset = svm_msrpm_offset(msr);
3080 write = svm->vmcb->control.exit_info_1 & 1;
3081 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003082
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003083 if (offset == MSR_INVALID)
3084 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003085
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003086 /* Offset is in 32 bit units but need in 8 bit units */
3087 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003088
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003089 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003090 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003091
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003092 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003093}
3094
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003095/* DB exceptions for our internal use must not cause vmexit */
3096static int nested_svm_intercept_db(struct vcpu_svm *svm)
3097{
3098 unsigned long dr6;
3099
3100 /* if we're not singlestepping, it's not ours */
3101 if (!svm->nmi_singlestep)
3102 return NESTED_EXIT_DONE;
3103
3104 /* if it's not a singlestep exception, it's not ours */
3105 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
3106 return NESTED_EXIT_DONE;
3107 if (!(dr6 & DR6_BS))
3108 return NESTED_EXIT_DONE;
3109
3110 /* if the guest is singlestepping, it should get the vmexit */
3111 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
3112 disable_nmi_singlestep(svm);
3113 return NESTED_EXIT_DONE;
3114 }
3115
3116 /* it's ours, the nested hypervisor must not see this one */
3117 return NESTED_EXIT_HOST;
3118}
3119
Joerg Roedel410e4d52009-08-07 11:49:44 +02003120static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003121{
Alexander Grafcf74a782008-11-25 20:17:08 +01003122 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003123
Joerg Roedel410e4d52009-08-07 11:49:44 +02003124 switch (exit_code) {
3125 case SVM_EXIT_INTR:
3126 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02003127 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02003128 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02003129 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01003130 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02003131 if (npt_enabled)
3132 return NESTED_EXIT_HOST;
3133 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02003134 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02003135 /* When we're shadowing, trap PFs, but not async PF */
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003136 if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003137 return NESTED_EXIT_HOST;
3138 break;
3139 default:
3140 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01003141 }
3142
Joerg Roedel410e4d52009-08-07 11:49:44 +02003143 return NESTED_EXIT_CONTINUE;
3144}
3145
3146/*
3147 * If this function returns true, this #vmexit was already handled
3148 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003149static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003150{
3151 u32 exit_code = svm->vmcb->control.exit_code;
3152 int vmexit = NESTED_EXIT_HOST;
3153
Alexander Grafcf74a782008-11-25 20:17:08 +01003154 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003155 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003156 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003157 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003158 case SVM_EXIT_IOIO:
3159 vmexit = nested_svm_intercept_ioio(svm);
3160 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003161 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
3162 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
3163 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003164 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003165 break;
3166 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01003167 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
3168 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
3169 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003170 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003171 break;
3172 }
3173 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
3174 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003175 if (svm->nested.intercept_exceptions & excp_bits) {
3176 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
3177 vmexit = nested_svm_intercept_db(svm);
3178 else
3179 vmexit = NESTED_EXIT_DONE;
3180 }
Gleb Natapov631bc482010-10-14 11:22:52 +02003181 /* async page fault always cause vmexit */
3182 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003183 svm->vcpu.arch.exception.nested_apf != 0)
Gleb Natapov631bc482010-10-14 11:22:52 +02003184 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003185 break;
3186 }
Joerg Roedel228070b2010-04-22 12:33:10 +02003187 case SVM_EXIT_ERR: {
3188 vmexit = NESTED_EXIT_DONE;
3189 break;
3190 }
Alexander Grafcf74a782008-11-25 20:17:08 +01003191 default: {
3192 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02003193 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003194 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003195 }
3196 }
3197
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003198 return vmexit;
3199}
3200
3201static int nested_svm_exit_handled(struct vcpu_svm *svm)
3202{
3203 int vmexit;
3204
3205 vmexit = nested_svm_intercept(svm);
3206
3207 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003208 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003209
3210 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01003211}
3212
Joerg Roedel0460a972009-08-07 11:49:31 +02003213static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
3214{
3215 struct vmcb_control_area *dst = &dst_vmcb->control;
3216 struct vmcb_control_area *from = &from_vmcb->control;
3217
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003218 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003219 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02003220 dst->intercept_exceptions = from->intercept_exceptions;
3221 dst->intercept = from->intercept;
3222 dst->iopm_base_pa = from->iopm_base_pa;
3223 dst->msrpm_base_pa = from->msrpm_base_pa;
3224 dst->tsc_offset = from->tsc_offset;
3225 dst->asid = from->asid;
3226 dst->tlb_ctl = from->tlb_ctl;
3227 dst->int_ctl = from->int_ctl;
3228 dst->int_vector = from->int_vector;
3229 dst->int_state = from->int_state;
3230 dst->exit_code = from->exit_code;
3231 dst->exit_code_hi = from->exit_code_hi;
3232 dst->exit_info_1 = from->exit_info_1;
3233 dst->exit_info_2 = from->exit_info_2;
3234 dst->exit_int_info = from->exit_int_info;
3235 dst->exit_int_info_err = from->exit_int_info_err;
3236 dst->nested_ctl = from->nested_ctl;
3237 dst->event_inj = from->event_inj;
3238 dst->event_inj_err = from->event_inj_err;
3239 dst->nested_cr3 = from->nested_cr3;
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003240 dst->virt_ext = from->virt_ext;
Joerg Roedel0460a972009-08-07 11:49:31 +02003241}
3242
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003243static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003244{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003245 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003246 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02003247 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003248 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01003249
Joerg Roedel17897f32009-10-09 16:08:29 +02003250 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
3251 vmcb->control.exit_info_1,
3252 vmcb->control.exit_info_2,
3253 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003254 vmcb->control.exit_int_info_err,
3255 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02003256
Joerg Roedel7597f122010-02-19 16:23:00 +01003257 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003258 if (!nested_vmcb)
3259 return 1;
3260
Joerg Roedel20307532010-11-29 17:51:48 +01003261 /* Exit Guest-Mode */
3262 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01003263 svm->nested.vmcb = 0;
3264
Alexander Grafcf74a782008-11-25 20:17:08 +01003265 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02003266 disable_gif(svm);
3267
3268 nested_vmcb->save.es = vmcb->save.es;
3269 nested_vmcb->save.cs = vmcb->save.cs;
3270 nested_vmcb->save.ss = vmcb->save.ss;
3271 nested_vmcb->save.ds = vmcb->save.ds;
3272 nested_vmcb->save.gdtr = vmcb->save.gdtr;
3273 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02003274 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003275 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003276 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003277 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003278 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03003279 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003280 nested_vmcb->save.rip = vmcb->save.rip;
3281 nested_vmcb->save.rsp = vmcb->save.rsp;
3282 nested_vmcb->save.rax = vmcb->save.rax;
3283 nested_vmcb->save.dr7 = vmcb->save.dr7;
3284 nested_vmcb->save.dr6 = vmcb->save.dr6;
3285 nested_vmcb->save.cpl = vmcb->save.cpl;
3286
3287 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
3288 nested_vmcb->control.int_vector = vmcb->control.int_vector;
3289 nested_vmcb->control.int_state = vmcb->control.int_state;
3290 nested_vmcb->control.exit_code = vmcb->control.exit_code;
3291 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
3292 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
3293 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
3294 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
3295 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003296
3297 if (svm->nrips_enabled)
3298 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02003299
3300 /*
3301 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
3302 * to make sure that we do not lose injected events. So check event_inj
3303 * here and copy it to exit_int_info if it is valid.
3304 * Exit_int_info and event_inj can't be both valid because the case
3305 * below only happens on a VMRUN instruction intercept which has
3306 * no valid exit_int_info set.
3307 */
3308 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
3309 struct vmcb_control_area *nc = &nested_vmcb->control;
3310
3311 nc->exit_int_info = vmcb->control.event_inj;
3312 nc->exit_int_info_err = vmcb->control.event_inj_err;
3313 }
3314
Joerg Roedel33740e42009-08-07 11:49:29 +02003315 nested_vmcb->control.tlb_ctl = 0;
3316 nested_vmcb->control.event_inj = 0;
3317 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01003318
3319 /* We always set V_INTR_MASKING and remember the old value in hflags */
3320 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
3321 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
3322
Alexander Grafcf74a782008-11-25 20:17:08 +01003323 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02003324 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01003325
Alexander Graf219b65d2009-06-15 15:21:25 +02003326 kvm_clear_exception_queue(&svm->vcpu);
3327 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003328
Joerg Roedel4b161842010-09-10 17:31:03 +02003329 svm->nested.nested_cr3 = 0;
3330
Alexander Grafcf74a782008-11-25 20:17:08 +01003331 /* Restore selected save entries */
3332 svm->vmcb->save.es = hsave->save.es;
3333 svm->vmcb->save.cs = hsave->save.cs;
3334 svm->vmcb->save.ss = hsave->save.ss;
3335 svm->vmcb->save.ds = hsave->save.ds;
3336 svm->vmcb->save.gdtr = hsave->save.gdtr;
3337 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003338 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01003339 svm_set_efer(&svm->vcpu, hsave->save.efer);
3340 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
3341 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
3342 if (npt_enabled) {
3343 svm->vmcb->save.cr3 = hsave->save.cr3;
3344 svm->vcpu.arch.cr3 = hsave->save.cr3;
3345 } else {
Avi Kivity23902182010-06-10 17:02:16 +03003346 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01003347 }
3348 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
3349 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
3350 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
3351 svm->vmcb->save.dr7 = 0;
3352 svm->vmcb->save.cpl = 0;
3353 svm->vmcb->control.exit_int_info = 0;
3354
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003355 mark_all_dirty(svm->vmcb);
3356
Joerg Roedel7597f122010-02-19 16:23:00 +01003357 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01003358
Joerg Roedel4b161842010-09-10 17:31:03 +02003359 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003360 kvm_mmu_reset_context(&svm->vcpu);
3361 kvm_mmu_load(&svm->vcpu);
3362
3363 return 0;
3364}
Alexander Graf3d6368e2008-11-25 20:17:07 +01003365
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003366static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003367{
Joerg Roedel323c3d82010-03-01 15:34:37 +01003368 /*
3369 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003370 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01003371 * the kvm msr permission bitmap may contain zero bits
3372 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01003373 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003374
Joerg Roedel323c3d82010-03-01 15:34:37 +01003375 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3376 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003377
Joerg Roedel323c3d82010-03-01 15:34:37 +01003378 for (i = 0; i < MSRPM_OFFSETS; i++) {
3379 u32 value, p;
3380 u64 offset;
3381
3382 if (msrpm_offsets[i] == 0xffffffff)
3383 break;
3384
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003385 p = msrpm_offsets[i];
3386 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01003387
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003388 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01003389 return false;
3390
3391 svm->nested.msrpm[p] = svm->msrpm[p] | value;
3392 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003393
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05003394 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
Alexander Graf3d6368e2008-11-25 20:17:07 +01003395
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003396 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003397}
3398
Joerg Roedel52c65a302010-08-02 16:46:44 +02003399static bool nested_vmcb_checks(struct vmcb *vmcb)
3400{
3401 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
3402 return false;
3403
Joerg Roedeldbe77582010-08-02 16:46:45 +02003404 if (vmcb->control.asid == 0)
3405 return false;
3406
Tom Lendackycea3a192017-12-04 10:57:24 -06003407 if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
3408 !npt_enabled)
Joerg Roedel4b161842010-09-10 17:31:03 +02003409 return false;
3410
Joerg Roedel52c65a302010-08-02 16:46:44 +02003411 return true;
3412}
3413
Ladi Prosekc2634062017-10-11 16:54:44 +02003414static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
3415 struct vmcb *nested_vmcb, struct page *page)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003416{
Avi Kivityf6e78472010-08-02 15:30:20 +03003417 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003418 svm->vcpu.arch.hflags |= HF_HIF_MASK;
3419 else
3420 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
3421
Tom Lendackycea3a192017-12-04 10:57:24 -06003422 if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
Joerg Roedel4b161842010-09-10 17:31:03 +02003423 kvm_mmu_unload(&svm->vcpu);
3424 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
3425 nested_svm_init_mmu_context(&svm->vcpu);
3426 }
3427
Alexander Graf3d6368e2008-11-25 20:17:07 +01003428 /* Load the nested guest state */
3429 svm->vmcb->save.es = nested_vmcb->save.es;
3430 svm->vmcb->save.cs = nested_vmcb->save.cs;
3431 svm->vmcb->save.ss = nested_vmcb->save.ss;
3432 svm->vmcb->save.ds = nested_vmcb->save.ds;
3433 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
3434 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003435 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003436 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
3437 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
3438 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
3439 if (npt_enabled) {
3440 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
3441 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003442 } else
Avi Kivity23902182010-06-10 17:02:16 +03003443 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003444
3445 /* Guest paging mode is active - reset mmu */
3446 kvm_mmu_reset_context(&svm->vcpu);
3447
Joerg Roedeldefbba52009-08-07 11:49:30 +02003448 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003449 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
3450 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
3451 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01003452
Alexander Graf3d6368e2008-11-25 20:17:07 +01003453 /* In case we don't even reach vcpu_run, the fields are not updated */
3454 svm->vmcb->save.rax = nested_vmcb->save.rax;
3455 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3456 svm->vmcb->save.rip = nested_vmcb->save.rip;
3457 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3458 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3459 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3460
Joerg Roedelf7138532010-03-01 15:34:40 +01003461 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003462 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003463
Joerg Roedelaad42c62009-08-07 11:49:34 +02003464 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003465 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003466 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02003467 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3468 svm->nested.intercept = nested_vmcb->control.intercept;
3469
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08003470 svm_flush_tlb(&svm->vcpu, true);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003471 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003472 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3473 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3474 else
3475 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3476
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003477 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3478 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003479 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3480 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003481 }
3482
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003483 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003484 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003485
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003486 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003487 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3488 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
3489 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003490 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3491 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3492
Joerg Roedel7597f122010-02-19 16:23:00 +01003493 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003494
Joerg Roedel20307532010-11-29 17:51:48 +01003495 /* Enter Guest-Mode */
3496 enter_guest_mode(&svm->vcpu);
3497
Joerg Roedel384c6362010-11-30 18:03:56 +01003498 /*
3499 * Merge guest and host intercepts - must be called with vcpu in
3500 * guest-mode to take affect here
3501 */
3502 recalc_intercepts(svm);
3503
Joerg Roedel06fc77722010-02-19 16:23:07 +01003504 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003505
Joerg Roedel2af91942009-08-07 11:49:28 +02003506 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003507
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003508 mark_all_dirty(svm->vmcb);
Ladi Prosekc2634062017-10-11 16:54:44 +02003509}
3510
3511static bool nested_svm_vmrun(struct vcpu_svm *svm)
3512{
3513 struct vmcb *nested_vmcb;
3514 struct vmcb *hsave = svm->nested.hsave;
3515 struct vmcb *vmcb = svm->vmcb;
3516 struct page *page;
3517 u64 vmcb_gpa;
3518
3519 vmcb_gpa = svm->vmcb->save.rax;
3520
3521 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
3522 if (!nested_vmcb)
3523 return false;
3524
3525 if (!nested_vmcb_checks(nested_vmcb)) {
3526 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3527 nested_vmcb->control.exit_code_hi = 0;
3528 nested_vmcb->control.exit_info_1 = 0;
3529 nested_vmcb->control.exit_info_2 = 0;
3530
3531 nested_svm_unmap(page);
3532
3533 return false;
3534 }
3535
3536 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3537 nested_vmcb->save.rip,
3538 nested_vmcb->control.int_ctl,
3539 nested_vmcb->control.event_inj,
3540 nested_vmcb->control.nested_ctl);
3541
3542 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3543 nested_vmcb->control.intercept_cr >> 16,
3544 nested_vmcb->control.intercept_exceptions,
3545 nested_vmcb->control.intercept);
3546
3547 /* Clear internal status */
3548 kvm_clear_exception_queue(&svm->vcpu);
3549 kvm_clear_interrupt_queue(&svm->vcpu);
3550
3551 /*
3552 * Save the old vmcb, so we don't need to pick what we save, but can
3553 * restore everything when a VMEXIT occurs
3554 */
3555 hsave->save.es = vmcb->save.es;
3556 hsave->save.cs = vmcb->save.cs;
3557 hsave->save.ss = vmcb->save.ss;
3558 hsave->save.ds = vmcb->save.ds;
3559 hsave->save.gdtr = vmcb->save.gdtr;
3560 hsave->save.idtr = vmcb->save.idtr;
3561 hsave->save.efer = svm->vcpu.arch.efer;
3562 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3563 hsave->save.cr4 = svm->vcpu.arch.cr4;
3564 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3565 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3566 hsave->save.rsp = vmcb->save.rsp;
3567 hsave->save.rax = vmcb->save.rax;
3568 if (npt_enabled)
3569 hsave->save.cr3 = vmcb->save.cr3;
3570 else
3571 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3572
3573 copy_vmcb_control_area(hsave, vmcb);
3574
3575 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003576
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003577 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003578}
3579
Joerg Roedel9966bf62009-08-07 11:49:40 +02003580static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01003581{
3582 to_vmcb->save.fs = from_vmcb->save.fs;
3583 to_vmcb->save.gs = from_vmcb->save.gs;
3584 to_vmcb->save.tr = from_vmcb->save.tr;
3585 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3586 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3587 to_vmcb->save.star = from_vmcb->save.star;
3588 to_vmcb->save.lstar = from_vmcb->save.lstar;
3589 to_vmcb->save.cstar = from_vmcb->save.cstar;
3590 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3591 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3592 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3593 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01003594}
3595
Avi Kivity851ba692009-08-24 11:10:17 +03003596static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003597{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003598 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003599 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003600 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003601
Alexander Graf55426752008-11-25 20:17:06 +01003602 if (nested_svm_check_permissions(svm))
3603 return 1;
3604
Joerg Roedel7597f122010-02-19 16:23:00 +01003605 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003606 if (!nested_vmcb)
3607 return 1;
3608
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003609 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003610 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003611
Joerg Roedel9966bf62009-08-07 11:49:40 +02003612 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003613 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003614
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003615 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003616}
3617
Avi Kivity851ba692009-08-24 11:10:17 +03003618static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003619{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003620 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003621 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003622 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003623
Alexander Graf55426752008-11-25 20:17:06 +01003624 if (nested_svm_check_permissions(svm))
3625 return 1;
3626
Joerg Roedel7597f122010-02-19 16:23:00 +01003627 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003628 if (!nested_vmcb)
3629 return 1;
3630
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003631 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003632 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003633
Joerg Roedel9966bf62009-08-07 11:49:40 +02003634 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003635 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003636
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003637 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003638}
3639
Avi Kivity851ba692009-08-24 11:10:17 +03003640static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003641{
Alexander Graf3d6368e2008-11-25 20:17:07 +01003642 if (nested_svm_check_permissions(svm))
3643 return 1;
3644
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02003645 /* Save rip after vmrun instruction */
3646 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003647
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003648 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01003649 return 1;
3650
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003651 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02003652 goto failed;
3653
3654 return 1;
3655
3656failed:
3657
3658 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3659 svm->vmcb->control.exit_code_hi = 0;
3660 svm->vmcb->control.exit_info_1 = 0;
3661 svm->vmcb->control.exit_info_2 = 0;
3662
3663 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003664
3665 return 1;
3666}
3667
Avi Kivity851ba692009-08-24 11:10:17 +03003668static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003669{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003670 int ret;
3671
Alexander Graf1371d902008-11-25 20:17:04 +01003672 if (nested_svm_check_permissions(svm))
3673 return 1;
3674
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003675 /*
3676 * If VGIF is enabled, the STGI intercept is only added to
Ladi Prosekcc3d9672017-10-17 16:02:39 +02003677 * detect the opening of the SMI/NMI window; remove it now.
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003678 */
3679 if (vgif_enabled(svm))
3680 clr_intercept(svm, INTERCEPT_STGI);
3681
Alexander Graf1371d902008-11-25 20:17:04 +01003682 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003683 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003684 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003685
Joerg Roedel2af91942009-08-07 11:49:28 +02003686 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003687
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003688 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003689}
3690
Avi Kivity851ba692009-08-24 11:10:17 +03003691static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003692{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003693 int ret;
3694
Alexander Graf1371d902008-11-25 20:17:04 +01003695 if (nested_svm_check_permissions(svm))
3696 return 1;
3697
3698 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003699 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003700
Joerg Roedel2af91942009-08-07 11:49:28 +02003701 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003702
3703 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05003704 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3705 svm_clear_vintr(svm);
3706 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3707 mark_dirty(svm->vmcb, VMCB_INTR);
3708 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003709
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003710 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003711}
3712
Avi Kivity851ba692009-08-24 11:10:17 +03003713static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02003714{
3715 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02003716
David Kaplan668f1982015-02-20 16:02:10 -06003717 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
3718 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02003719
Alexander Grafff092382009-06-15 15:21:24 +02003720 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06003721 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02003722
3723 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003724 return kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02003725}
3726
Joerg Roedel532a46b2009-10-09 16:08:32 +02003727static int skinit_interception(struct vcpu_svm *svm)
3728{
David Kaplan668f1982015-02-20 16:02:10 -06003729 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003730
3731 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3732 return 1;
3733}
3734
David Kaplandab429a2015-03-02 13:43:37 -06003735static int wbinvd_interception(struct vcpu_svm *svm)
3736{
Kyle Huey6affcbe2016-11-29 12:40:40 -08003737 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06003738}
3739
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003740static int xsetbv_interception(struct vcpu_svm *svm)
3741{
3742 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
3743 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3744
3745 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3746 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003747 return kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003748 }
3749
3750 return 1;
3751}
3752
Avi Kivity851ba692009-08-24 11:10:17 +03003753static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003754{
Izik Eidus37817f22008-03-24 23:14:53 +02003755 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003756 int reason;
3757 int int_type = svm->vmcb->control.exit_int_info &
3758 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003759 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003760 uint32_t type =
3761 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3762 uint32_t idt_v =
3763 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003764 bool has_error_code = false;
3765 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003766
3767 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003768
Izik Eidus37817f22008-03-24 23:14:53 +02003769 if (svm->vmcb->control.exit_info_2 &
3770 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003771 reason = TASK_SWITCH_IRET;
3772 else if (svm->vmcb->control.exit_info_2 &
3773 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3774 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003775 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003776 reason = TASK_SWITCH_GATE;
3777 else
3778 reason = TASK_SWITCH_CALL;
3779
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003780 if (reason == TASK_SWITCH_GATE) {
3781 switch (type) {
3782 case SVM_EXITINTINFO_TYPE_NMI:
3783 svm->vcpu.arch.nmi_injected = false;
3784 break;
3785 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003786 if (svm->vmcb->control.exit_info_2 &
3787 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3788 has_error_code = true;
3789 error_code =
3790 (u32)svm->vmcb->control.exit_info_2;
3791 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003792 kvm_clear_exception_queue(&svm->vcpu);
3793 break;
3794 case SVM_EXITINTINFO_TYPE_INTR:
3795 kvm_clear_interrupt_queue(&svm->vcpu);
3796 break;
3797 default:
3798 break;
3799 }
3800 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003801
Gleb Natapov8317c292009-04-12 13:37:02 +03003802 if (reason != TASK_SWITCH_GATE ||
3803 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3804 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03003805 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3806 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003807
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003808 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3809 int_vec = -1;
3810
3811 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03003812 has_error_code, error_code) == EMULATE_FAIL) {
3813 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3814 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3815 svm->vcpu.run->internal.ndata = 0;
3816 return 0;
3817 }
3818 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003819}
3820
Avi Kivity851ba692009-08-24 11:10:17 +03003821static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003822{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003823 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Kyle Huey6a908b62016-11-29 12:40:37 -08003824 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003825}
3826
Avi Kivity851ba692009-08-24 11:10:17 +03003827static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003828{
3829 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003830 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003831 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003832 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003833 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003834 return 1;
3835}
3836
Avi Kivity851ba692009-08-24 11:10:17 +03003837static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003838{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003839 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3840 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3841
3842 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003843 return kvm_skip_emulated_instruction(&svm->vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003844}
3845
Avi Kivity851ba692009-08-24 11:10:17 +03003846static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847{
Andre Przywara51d8b662010-12-21 11:12:02 +01003848 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003849}
3850
Brijesh Singh7607b712018-02-19 10:14:44 -06003851static int rsm_interception(struct vcpu_svm *svm)
3852{
3853 return x86_emulate_instruction(&svm->vcpu, 0, 0,
3854 rsm_ins_bytes, 2) == EMULATE_DONE;
3855}
3856
Avi Kivity332b56e2011-11-10 14:57:24 +02003857static int rdpmc_interception(struct vcpu_svm *svm)
3858{
3859 int err;
3860
3861 if (!static_cpu_has(X86_FEATURE_NRIPS))
3862 return emulate_on_interception(svm);
3863
3864 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08003865 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02003866}
3867
Xiubo Li52eb5a62015-03-13 17:39:45 +08003868static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3869 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003870{
3871 unsigned long cr0 = svm->vcpu.arch.cr0;
3872 bool ret = false;
3873 u64 intercept;
3874
3875 intercept = svm->nested.intercept;
3876
3877 if (!is_guest_mode(&svm->vcpu) ||
3878 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3879 return false;
3880
3881 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3882 val &= ~SVM_CR0_SELECTIVE_MASK;
3883
3884 if (cr0 ^ val) {
3885 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3886 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3887 }
3888
3889 return ret;
3890}
3891
Andre Przywara7ff76d52010-12-21 11:12:04 +01003892#define CR_VALID (1ULL << 63)
3893
3894static int cr_interception(struct vcpu_svm *svm)
3895{
3896 int reg, cr;
3897 unsigned long val;
3898 int err;
3899
3900 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3901 return emulate_on_interception(svm);
3902
3903 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3904 return emulate_on_interception(svm);
3905
3906 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003907 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3908 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3909 else
3910 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003911
3912 err = 0;
3913 if (cr >= 16) { /* mov to cr */
3914 cr -= 16;
3915 val = kvm_register_read(&svm->vcpu, reg);
3916 switch (cr) {
3917 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003918 if (!check_selective_cr0_intercepted(svm, val))
3919 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003920 else
3921 return 1;
3922
Andre Przywara7ff76d52010-12-21 11:12:04 +01003923 break;
3924 case 3:
3925 err = kvm_set_cr3(&svm->vcpu, val);
3926 break;
3927 case 4:
3928 err = kvm_set_cr4(&svm->vcpu, val);
3929 break;
3930 case 8:
3931 err = kvm_set_cr8(&svm->vcpu, val);
3932 break;
3933 default:
3934 WARN(1, "unhandled write to CR%d", cr);
3935 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3936 return 1;
3937 }
3938 } else { /* mov from cr */
3939 switch (cr) {
3940 case 0:
3941 val = kvm_read_cr0(&svm->vcpu);
3942 break;
3943 case 2:
3944 val = svm->vcpu.arch.cr2;
3945 break;
3946 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003947 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003948 break;
3949 case 4:
3950 val = kvm_read_cr4(&svm->vcpu);
3951 break;
3952 case 8:
3953 val = kvm_get_cr8(&svm->vcpu);
3954 break;
3955 default:
3956 WARN(1, "unhandled read from CR%d", cr);
3957 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3958 return 1;
3959 }
3960 kvm_register_write(&svm->vcpu, reg, val);
3961 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08003962 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003963}
3964
Andre Przywaracae37972010-12-21 11:12:05 +01003965static int dr_interception(struct vcpu_svm *svm)
3966{
3967 int reg, dr;
3968 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003969
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003970 if (svm->vcpu.guest_debug == 0) {
3971 /*
3972 * No more DR vmexits; force a reload of the debug registers
3973 * and reenter on this instruction. The next vmexit will
3974 * retrieve the full state of the debug registers.
3975 */
3976 clr_dr_intercepts(svm);
3977 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3978 return 1;
3979 }
3980
Andre Przywaracae37972010-12-21 11:12:05 +01003981 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3982 return emulate_on_interception(svm);
3983
3984 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3985 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3986
3987 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003988 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3989 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003990 val = kvm_register_read(&svm->vcpu, reg);
3991 kvm_set_dr(&svm->vcpu, dr - 16, val);
3992 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003993 if (!kvm_require_dr(&svm->vcpu, dr))
3994 return 1;
3995 kvm_get_dr(&svm->vcpu, dr, &val);
3996 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003997 }
3998
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003999 return kvm_skip_emulated_instruction(&svm->vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01004000}
4001
Avi Kivity851ba692009-08-24 11:10:17 +03004002static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01004003{
Avi Kivity851ba692009-08-24 11:10:17 +03004004 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004005 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03004006
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004007 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
4008 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01004009 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004010 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01004011 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004012 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01004013 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01004014 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
4015 return 0;
4016}
4017
Tom Lendacky801e4592018-02-21 13:39:51 -06004018static int svm_get_msr_feature(struct kvm_msr_entry *msr)
4019{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004020 msr->data = 0;
4021
4022 switch (msr->index) {
4023 case MSR_F10H_DECFG:
4024 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
4025 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
4026 break;
4027 default:
4028 return 1;
4029 }
4030
4031 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004032}
4033
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004034static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004035{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004036 struct vcpu_svm *svm = to_svm(vcpu);
4037
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004038 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05304039 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004040 msr_info->data = svm->vmcb->control.tsc_offset +
Haozhong Zhang35181e82015-10-20 15:39:03 +08004041 kvm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01004042
Avi Kivity6aa8b732006-12-10 02:21:36 -08004043 break;
4044 }
Brian Gerst8c065852010-07-17 09:03:26 -04004045 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004046 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004047 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08004048#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004049 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004050 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004051 break;
4052 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004053 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004054 break;
4055 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004056 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057 break;
4058 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004059 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004060 break;
4061#endif
4062 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004063 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064 break;
4065 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004066 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067 break;
4068 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004069 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004071 case MSR_TSC_AUX:
4072 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4073 return 1;
4074 msr_info->data = svm->tsc_aux;
4075 break;
Joerg Roedele0231712010-02-24 18:59:10 +01004076 /*
4077 * Nobody will change the following 5 values in the VMCB so we can
4078 * safely return them on rdmsr. They will always be 0 until LBRV is
4079 * implemented.
4080 */
Joerg Roedela2938c82008-02-13 16:30:28 +01004081 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004082 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01004083 break;
4084 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004085 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01004086 break;
4087 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004088 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01004089 break;
4090 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004091 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01004092 break;
4093 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004094 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01004095 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004096 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004097 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004098 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01004099 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004100 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01004101 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004102 case MSR_IA32_SPEC_CTRL:
4103 if (!msr_info->host_initiated &&
4104 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
4105 return 1;
4106
4107 msr_info->data = svm->spec_ctrl;
4108 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01004109 case MSR_F15H_IC_CFG: {
4110
4111 int family, model;
4112
4113 family = guest_cpuid_family(vcpu);
4114 model = guest_cpuid_model(vcpu);
4115
4116 if (family < 0 || model < 0)
4117 return kvm_get_msr_common(vcpu, msr_info);
4118
4119 msr_info->data = 0;
4120
4121 if (family == 0x15 &&
4122 (model >= 0x2 && model < 0x20))
4123 msr_info->data = 0x1E;
4124 }
4125 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004126 case MSR_F10H_DECFG:
4127 msr_info->data = svm->msr_decfg;
4128 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004129 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004130 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004131 }
4132 return 0;
4133}
4134
Avi Kivity851ba692009-08-24 11:10:17 +03004135static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136{
David Kaplan668f1982015-02-20 16:02:10 -06004137 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004138 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004139
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004140 msr_info.index = ecx;
4141 msr_info.host_initiated = false;
4142 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02004143 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004144 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004145 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004146 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004147 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004148
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004149 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
4150 msr_info.data & 0xffffffff);
4151 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
4152 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004153 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004154 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004156}
4157
Joerg Roedel4a810182010-02-24 18:59:15 +01004158static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
4159{
4160 struct vcpu_svm *svm = to_svm(vcpu);
4161 int svm_dis, chg_mask;
4162
4163 if (data & ~SVM_VM_CR_VALID_MASK)
4164 return 1;
4165
4166 chg_mask = SVM_VM_CR_VALID_MASK;
4167
4168 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
4169 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
4170
4171 svm->nested.vm_cr_msr &= ~chg_mask;
4172 svm->nested.vm_cr_msr |= (data & chg_mask);
4173
4174 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
4175
4176 /* check for svm_disable while efer.svme is set */
4177 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
4178 return 1;
4179
4180 return 0;
4181}
4182
Will Auld8fe8ab42012-11-29 12:42:12 -08004183static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004184{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004185 struct vcpu_svm *svm = to_svm(vcpu);
4186
Will Auld8fe8ab42012-11-29 12:42:12 -08004187 u32 ecx = msr->index;
4188 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02004190 case MSR_IA32_CR_PAT:
4191 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4192 return 1;
4193 vcpu->arch.pat = data;
4194 svm->vmcb->save.g_pat = data;
4195 mark_dirty(svm->vmcb, VMCB_NPT);
4196 break;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10004197 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08004198 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004199 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004200 case MSR_IA32_SPEC_CTRL:
4201 if (!msr->host_initiated &&
4202 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
4203 return 1;
4204
4205 /* The STIBP bit doesn't fault even if it's not advertised */
4206 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
4207 return 1;
4208
4209 svm->spec_ctrl = data;
4210
4211 if (!data)
4212 break;
4213
4214 /*
4215 * For non-nested:
4216 * When it's written (to non-zero) for the first time, pass
4217 * it through.
4218 *
4219 * For nested:
4220 * The handling of the MSR bitmap for L2 guests is done in
4221 * nested_svm_vmrun_msrpm.
4222 * We update the L1 MSR bit as well since it will end up
4223 * touching the MSR anyway now.
4224 */
4225 set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
4226 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004227 case MSR_IA32_PRED_CMD:
4228 if (!msr->host_initiated &&
4229 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB))
4230 return 1;
4231
4232 if (data & ~PRED_CMD_IBPB)
4233 return 1;
4234
4235 if (!data)
4236 break;
4237
4238 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4239 if (is_guest_mode(vcpu))
4240 break;
4241 set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
4242 break;
Brian Gerst8c065852010-07-17 09:03:26 -04004243 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004244 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004245 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08004246#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004247 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004248 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004249 break;
4250 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004251 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004252 break;
4253 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004254 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004255 break;
4256 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004257 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004258 break;
4259#endif
4260 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004261 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004262 break;
4263 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02004264 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004265 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004266 break;
4267 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02004268 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004269 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004270 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004271 case MSR_TSC_AUX:
4272 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4273 return 1;
4274
4275 /*
4276 * This is rare, so we update the MSR here instead of using
4277 * direct_access_msrs. Doing that would require a rdmsr in
4278 * svm_vcpu_put.
4279 */
4280 svm->tsc_aux = data;
4281 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
4282 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01004283 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004284 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03004285 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
4286 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004287 break;
4288 }
4289 if (data & DEBUGCTL_RESERVED_BITS)
4290 return 1;
4291
4292 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01004293 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004294 if (data & (1ULL<<0))
4295 svm_enable_lbrv(svm);
4296 else
4297 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01004298 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004299 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02004300 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004301 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004302 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01004303 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004304 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03004305 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004306 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004307 case MSR_F10H_DECFG: {
4308 struct kvm_msr_entry msr_entry;
4309
4310 msr_entry.index = msr->index;
4311 if (svm_get_msr_feature(&msr_entry))
4312 return 1;
4313
4314 /* Check the supported bits */
4315 if (data & ~msr_entry.data)
4316 return 1;
4317
4318 /* Don't allow the guest to change a bit, #GP */
4319 if (!msr->host_initiated && (data ^ msr_entry.data))
4320 return 1;
4321
4322 svm->msr_decfg = data;
4323 break;
4324 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004325 case MSR_IA32_APICBASE:
4326 if (kvm_vcpu_apicv_active(vcpu))
4327 avic_update_vapic_bar(to_svm(vcpu), data);
4328 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004329 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08004330 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004331 }
4332 return 0;
4333}
4334
Avi Kivity851ba692009-08-24 11:10:17 +03004335static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336{
Will Auld8fe8ab42012-11-29 12:42:12 -08004337 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06004338 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
4339 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004340
Will Auld8fe8ab42012-11-29 12:42:12 -08004341 msr.data = data;
4342 msr.index = ecx;
4343 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004344
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004345 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03004346 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02004347 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004348 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004349 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004350 } else {
4351 trace_kvm_msr_write(ecx, data);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004352 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02004353 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004354}
4355
Avi Kivity851ba692009-08-24 11:10:17 +03004356static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004357{
Rusty Russelle756fc62007-07-30 20:07:08 +10004358 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03004359 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004360 else
Avi Kivity851ba692009-08-24 11:10:17 +03004361 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004362}
4363
Avi Kivity851ba692009-08-24 11:10:17 +03004364static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08004365{
Avi Kivity3842d132010-07-27 12:30:24 +03004366 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01004367 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03004368 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004369 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08004370 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08004371 return 1;
4372}
4373
Mark Langsdorf565d0992009-10-06 14:25:02 -05004374static int pause_interception(struct vcpu_svm *svm)
4375{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004376 struct kvm_vcpu *vcpu = &svm->vcpu;
4377 bool in_kernel = (svm_get_cpl(vcpu) == 0);
4378
Babu Moger8566ac82018-03-16 16:37:26 -04004379 if (pause_filter_thresh)
4380 grow_ple_window(vcpu);
4381
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004382 kvm_vcpu_on_spin(vcpu, in_kernel);
Mark Langsdorf565d0992009-10-06 14:25:02 -05004383 return 1;
4384}
4385
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004386static int nop_interception(struct vcpu_svm *svm)
4387{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004388 return kvm_skip_emulated_instruction(&(svm->vcpu));
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004389}
4390
4391static int monitor_interception(struct vcpu_svm *svm)
4392{
4393 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
4394 return nop_interception(svm);
4395}
4396
4397static int mwait_interception(struct vcpu_svm *svm)
4398{
4399 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4400 return nop_interception(svm);
4401}
4402
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004403enum avic_ipi_failure_cause {
4404 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
4405 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
4406 AVIC_IPI_FAILURE_INVALID_TARGET,
4407 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
4408};
4409
4410static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
4411{
4412 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
4413 u32 icrl = svm->vmcb->control.exit_info_1;
4414 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03004415 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004416 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4417
4418 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
4419
4420 switch (id) {
4421 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
4422 /*
4423 * AVIC hardware handles the generation of
4424 * IPIs when the specified Message Type is Fixed
4425 * (also known as fixed delivery mode) and
4426 * the Trigger Mode is edge-triggered. The hardware
4427 * also supports self and broadcast delivery modes
4428 * specified via the Destination Shorthand(DSH)
4429 * field of the ICRL. Logical and physical APIC ID
4430 * formats are supported. All other IPI types cause
4431 * a #VMEXIT, which needs to emulated.
4432 */
4433 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
4434 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
4435 break;
4436 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
4437 int i;
4438 struct kvm_vcpu *vcpu;
4439 struct kvm *kvm = svm->vcpu.kvm;
4440 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4441
4442 /*
4443 * At this point, we expect that the AVIC HW has already
4444 * set the appropriate IRR bits on the valid target
4445 * vcpus. So, we just need to kick the appropriate vcpu.
4446 */
4447 kvm_for_each_vcpu(i, vcpu, kvm) {
4448 bool m = kvm_apic_match_dest(vcpu, apic,
4449 icrl & KVM_APIC_SHORT_MASK,
4450 GET_APIC_DEST_FIELD(icrh),
4451 icrl & KVM_APIC_DEST_MASK);
4452
4453 if (m && !avic_vcpu_is_running(vcpu))
4454 kvm_vcpu_wake_up(vcpu);
4455 }
4456 break;
4457 }
4458 case AVIC_IPI_FAILURE_INVALID_TARGET:
4459 break;
4460 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
4461 WARN_ONCE(1, "Invalid backing page\n");
4462 break;
4463 default:
4464 pr_err("Unknown IPI interception\n");
4465 }
4466
4467 return 1;
4468}
4469
4470static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
4471{
Sean Christopherson81811c12018-03-20 12:17:21 -07004472 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004473 int index;
4474 u32 *logical_apic_id_table;
4475 int dlid = GET_APIC_LOGICAL_ID(ldr);
4476
4477 if (!dlid)
4478 return NULL;
4479
4480 if (flat) { /* flat */
4481 index = ffs(dlid) - 1;
4482 if (index > 7)
4483 return NULL;
4484 } else { /* cluster */
4485 int cluster = (dlid & 0xf0) >> 4;
4486 int apic = ffs(dlid & 0x0f) - 1;
4487
4488 if ((apic < 0) || (apic > 7) ||
4489 (cluster >= 0xf))
4490 return NULL;
4491 index = (cluster << 2) + apic;
4492 }
4493
Sean Christopherson81811c12018-03-20 12:17:21 -07004494 logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004495
4496 return &logical_apic_id_table[index];
4497}
4498
4499static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
4500 bool valid)
4501{
4502 bool flat;
4503 u32 *entry, new_entry;
4504
4505 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4506 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4507 if (!entry)
4508 return -EINVAL;
4509
4510 new_entry = READ_ONCE(*entry);
4511 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4512 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
4513 if (valid)
4514 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4515 else
4516 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4517 WRITE_ONCE(*entry, new_entry);
4518
4519 return 0;
4520}
4521
4522static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4523{
4524 int ret;
4525 struct vcpu_svm *svm = to_svm(vcpu);
4526 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
4527
4528 if (!ldr)
4529 return 1;
4530
4531 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
4532 if (ret && svm->ldr_reg) {
4533 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
4534 svm->ldr_reg = 0;
4535 } else {
4536 svm->ldr_reg = ldr;
4537 }
4538 return ret;
4539}
4540
4541static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4542{
4543 u64 *old, *new;
4544 struct vcpu_svm *svm = to_svm(vcpu);
4545 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
4546 u32 id = (apic_id_reg >> 24) & 0xff;
4547
4548 if (vcpu->vcpu_id == id)
4549 return 0;
4550
4551 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4552 new = avic_get_physical_id_entry(vcpu, id);
4553 if (!new || !old)
4554 return 1;
4555
4556 /* We need to move physical_id_entry to new offset */
4557 *new = *old;
4558 *old = 0ULL;
4559 to_svm(vcpu)->avic_physical_id_cache = new;
4560
4561 /*
4562 * Also update the guest physical APIC ID in the logical
4563 * APIC ID table entry if already setup the LDR.
4564 */
4565 if (svm->ldr_reg)
4566 avic_handle_ldr_update(vcpu);
4567
4568 return 0;
4569}
4570
4571static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
4572{
4573 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christopherson81811c12018-03-20 12:17:21 -07004574 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004575 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
4576 u32 mod = (dfr >> 28) & 0xf;
4577
4578 /*
4579 * We assume that all local APICs are using the same type.
4580 * If this changes, we need to flush the AVIC logical
4581 * APID id table.
4582 */
Sean Christopherson81811c12018-03-20 12:17:21 -07004583 if (kvm_svm->ldr_mode == mod)
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004584 return 0;
4585
Sean Christopherson81811c12018-03-20 12:17:21 -07004586 clear_page(page_address(kvm_svm->avic_logical_id_table_page));
4587 kvm_svm->ldr_mode = mod;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004588
4589 if (svm->ldr_reg)
4590 avic_handle_ldr_update(vcpu);
4591 return 0;
4592}
4593
4594static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4595{
4596 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4597 u32 offset = svm->vmcb->control.exit_info_1 &
4598 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4599
4600 switch (offset) {
4601 case APIC_ID:
4602 if (avic_handle_apic_id_update(&svm->vcpu))
4603 return 0;
4604 break;
4605 case APIC_LDR:
4606 if (avic_handle_ldr_update(&svm->vcpu))
4607 return 0;
4608 break;
4609 case APIC_DFR:
4610 avic_handle_dfr_update(&svm->vcpu);
4611 break;
4612 default:
4613 break;
4614 }
4615
4616 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4617
4618 return 1;
4619}
4620
4621static bool is_avic_unaccelerated_access_trap(u32 offset)
4622{
4623 bool ret = false;
4624
4625 switch (offset) {
4626 case APIC_ID:
4627 case APIC_EOI:
4628 case APIC_RRR:
4629 case APIC_LDR:
4630 case APIC_DFR:
4631 case APIC_SPIV:
4632 case APIC_ESR:
4633 case APIC_ICR:
4634 case APIC_LVTT:
4635 case APIC_LVTTHMR:
4636 case APIC_LVTPC:
4637 case APIC_LVT0:
4638 case APIC_LVT1:
4639 case APIC_LVTERR:
4640 case APIC_TMICT:
4641 case APIC_TDCR:
4642 ret = true;
4643 break;
4644 default:
4645 break;
4646 }
4647 return ret;
4648}
4649
4650static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4651{
4652 int ret = 0;
4653 u32 offset = svm->vmcb->control.exit_info_1 &
4654 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4655 u32 vector = svm->vmcb->control.exit_info_2 &
4656 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4657 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4658 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4659 bool trap = is_avic_unaccelerated_access_trap(offset);
4660
4661 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4662 trap, write, vector);
4663 if (trap) {
4664 /* Handling Trap */
4665 WARN_ONCE(!write, "svm: Handling trap read.\n");
4666 ret = avic_unaccel_trap_write(svm);
4667 } else {
4668 /* Handling Fault */
4669 ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
4670 }
4671
4672 return ret;
4673}
4674
Mathias Krause09941fb2012-08-30 01:30:20 +02004675static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01004676 [SVM_EXIT_READ_CR0] = cr_interception,
4677 [SVM_EXIT_READ_CR3] = cr_interception,
4678 [SVM_EXIT_READ_CR4] = cr_interception,
4679 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06004680 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02004681 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01004682 [SVM_EXIT_WRITE_CR3] = cr_interception,
4683 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004684 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01004685 [SVM_EXIT_READ_DR0] = dr_interception,
4686 [SVM_EXIT_READ_DR1] = dr_interception,
4687 [SVM_EXIT_READ_DR2] = dr_interception,
4688 [SVM_EXIT_READ_DR3] = dr_interception,
4689 [SVM_EXIT_READ_DR4] = dr_interception,
4690 [SVM_EXIT_READ_DR5] = dr_interception,
4691 [SVM_EXIT_READ_DR6] = dr_interception,
4692 [SVM_EXIT_READ_DR7] = dr_interception,
4693 [SVM_EXIT_WRITE_DR0] = dr_interception,
4694 [SVM_EXIT_WRITE_DR1] = dr_interception,
4695 [SVM_EXIT_WRITE_DR2] = dr_interception,
4696 [SVM_EXIT_WRITE_DR3] = dr_interception,
4697 [SVM_EXIT_WRITE_DR4] = dr_interception,
4698 [SVM_EXIT_WRITE_DR5] = dr_interception,
4699 [SVM_EXIT_WRITE_DR6] = dr_interception,
4700 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004701 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4702 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004703 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004704 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004705 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01004706 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Liran Alon97184202018-03-12 13:12:52 +02004707 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004708 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02004709 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710 [SVM_EXIT_SMI] = nop_on_interception,
4711 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08004712 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02004713 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004714 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004715 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02004716 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05004717 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004718 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004719 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02004720 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004721 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004722 [SVM_EXIT_MSR] = msr_interception,
4723 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08004724 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01004725 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02004726 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01004727 [SVM_EXIT_VMLOAD] = vmload_interception,
4728 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01004729 [SVM_EXIT_STGI] = stgi_interception,
4730 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02004731 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06004732 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004733 [SVM_EXIT_MONITOR] = monitor_interception,
4734 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01004735 [SVM_EXIT_XSETBV] = xsetbv_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02004736 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06004737 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004738 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4739 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004740};
4741
Joe Perchesae8cc052011-04-24 22:00:50 -07004742static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02004743{
4744 struct vcpu_svm *svm = to_svm(vcpu);
4745 struct vmcb_control_area *control = &svm->vmcb->control;
4746 struct vmcb_save_area *save = &svm->vmcb->save;
4747
4748 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004749 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4750 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4751 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4752 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4753 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4754 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4755 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
Babu Moger1d8fb442018-03-16 16:37:25 -04004756 pr_err("%-20s%d\n", "pause filter threshold:",
4757 control->pause_filter_thresh);
Joe Perchesae8cc052011-04-24 22:00:50 -07004758 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4759 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4760 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4761 pr_err("%-20s%d\n", "asid:", control->asid);
4762 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4763 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4764 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4765 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4766 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4767 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4768 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4769 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4770 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4771 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4772 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004773 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07004774 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4775 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05004776 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07004777 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004778 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4779 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4780 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004781 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004782 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4783 "es:",
4784 save->es.selector, save->es.attrib,
4785 save->es.limit, save->es.base);
4786 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4787 "cs:",
4788 save->cs.selector, save->cs.attrib,
4789 save->cs.limit, save->cs.base);
4790 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4791 "ss:",
4792 save->ss.selector, save->ss.attrib,
4793 save->ss.limit, save->ss.base);
4794 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4795 "ds:",
4796 save->ds.selector, save->ds.attrib,
4797 save->ds.limit, save->ds.base);
4798 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4799 "fs:",
4800 save->fs.selector, save->fs.attrib,
4801 save->fs.limit, save->fs.base);
4802 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4803 "gs:",
4804 save->gs.selector, save->gs.attrib,
4805 save->gs.limit, save->gs.base);
4806 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4807 "gdtr:",
4808 save->gdtr.selector, save->gdtr.attrib,
4809 save->gdtr.limit, save->gdtr.base);
4810 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4811 "ldtr:",
4812 save->ldtr.selector, save->ldtr.attrib,
4813 save->ldtr.limit, save->ldtr.base);
4814 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4815 "idtr:",
4816 save->idtr.selector, save->idtr.attrib,
4817 save->idtr.limit, save->idtr.base);
4818 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4819 "tr:",
4820 save->tr.selector, save->tr.attrib,
4821 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004822 pr_err("cpl: %d efer: %016llx\n",
4823 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004824 pr_err("%-15s %016llx %-13s %016llx\n",
4825 "cr0:", save->cr0, "cr2:", save->cr2);
4826 pr_err("%-15s %016llx %-13s %016llx\n",
4827 "cr3:", save->cr3, "cr4:", save->cr4);
4828 pr_err("%-15s %016llx %-13s %016llx\n",
4829 "dr6:", save->dr6, "dr7:", save->dr7);
4830 pr_err("%-15s %016llx %-13s %016llx\n",
4831 "rip:", save->rip, "rflags:", save->rflags);
4832 pr_err("%-15s %016llx %-13s %016llx\n",
4833 "rsp:", save->rsp, "rax:", save->rax);
4834 pr_err("%-15s %016llx %-13s %016llx\n",
4835 "star:", save->star, "lstar:", save->lstar);
4836 pr_err("%-15s %016llx %-13s %016llx\n",
4837 "cstar:", save->cstar, "sfmask:", save->sfmask);
4838 pr_err("%-15s %016llx %-13s %016llx\n",
4839 "kernel_gs_base:", save->kernel_gs_base,
4840 "sysenter_cs:", save->sysenter_cs);
4841 pr_err("%-15s %016llx %-13s %016llx\n",
4842 "sysenter_esp:", save->sysenter_esp,
4843 "sysenter_eip:", save->sysenter_eip);
4844 pr_err("%-15s %016llx %-13s %016llx\n",
4845 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4846 pr_err("%-15s %016llx %-13s %016llx\n",
4847 "br_from:", save->br_from, "br_to:", save->br_to);
4848 pr_err("%-15s %016llx %-13s %016llx\n",
4849 "excp_from:", save->last_excp_from,
4850 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004851}
4852
Avi Kivity586f9602010-11-18 13:09:54 +02004853static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4854{
4855 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4856
4857 *info1 = control->exit_info_1;
4858 *info2 = control->exit_info_2;
4859}
4860
Avi Kivity851ba692009-08-24 11:10:17 +03004861static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004862{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004863 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004864 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004865 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004867 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4868
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004869 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004870 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4871 if (npt_enabled)
4872 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004873
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004874 if (unlikely(svm->nested.exit_required)) {
4875 nested_svm_vmexit(svm);
4876 svm->nested.exit_required = false;
4877
4878 return 1;
4879 }
4880
Joerg Roedel20307532010-11-29 17:51:48 +01004881 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004882 int vmexit;
4883
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004884 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4885 svm->vmcb->control.exit_info_1,
4886 svm->vmcb->control.exit_info_2,
4887 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004888 svm->vmcb->control.exit_int_info_err,
4889 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004890
Joerg Roedel410e4d52009-08-07 11:49:44 +02004891 vmexit = nested_svm_exit_special(svm);
4892
4893 if (vmexit == NESTED_EXIT_CONTINUE)
4894 vmexit = nested_svm_exit_handled(svm);
4895
4896 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004897 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004898 }
4899
Joerg Roedela5c38322009-08-07 11:49:32 +02004900 svm_complete_interrupts(svm);
4901
Avi Kivity04d2cc72007-09-10 18:10:54 +03004902 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4903 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4904 kvm_run->fail_entry.hardware_entry_failure_reason
4905 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004906 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4907 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004908 return 0;
4909 }
4910
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004911 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004912 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004913 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4914 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004915 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004916 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004917 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004918 exit_code);
4919
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004920 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004921 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04004922 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03004923 kvm_queue_exception(vcpu, UD_VECTOR);
4924 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004925 }
4926
Avi Kivity851ba692009-08-24 11:10:17 +03004927 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004928}
4929
4930static void reload_tss(struct kvm_vcpu *vcpu)
4931{
4932 int cpu = raw_smp_processor_id();
4933
Tejun Heo0fe1e002009-10-29 22:34:14 +09004934 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4935 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004936 load_TR_desc();
4937}
4938
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004939static void pre_sev_run(struct vcpu_svm *svm, int cpu)
4940{
4941 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4942 int asid = sev_get_asid(svm->vcpu.kvm);
4943
4944 /* Assign the asid allocated with this SEV guest */
4945 svm->vmcb->control.asid = asid;
4946
4947 /*
4948 * Flush guest TLB:
4949 *
4950 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
4951 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
4952 */
4953 if (sd->sev_vmcbs[asid] == svm->vmcb &&
4954 svm->last_cpu == cpu)
4955 return;
4956
4957 svm->last_cpu = cpu;
4958 sd->sev_vmcbs[asid] = svm->vmcb;
4959 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4960 mark_dirty(svm->vmcb, VMCB_ASID);
4961}
4962
Rusty Russelle756fc62007-07-30 20:07:08 +10004963static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004964{
4965 int cpu = raw_smp_processor_id();
4966
Tejun Heo0fe1e002009-10-29 22:34:14 +09004967 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004968
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004969 if (sev_guest(svm->vcpu.kvm))
4970 return pre_sev_run(svm, cpu);
4971
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03004972 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09004973 if (svm->asid_generation != sd->asid_generation)
4974 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004975}
4976
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004977static void svm_inject_nmi(struct kvm_vcpu *vcpu)
4978{
4979 struct vcpu_svm *svm = to_svm(vcpu);
4980
4981 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
4982 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004983 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004984 ++vcpu->stat.nmi_injections;
4985}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004986
Eddie Dong85f455f2007-07-06 12:20:49 +03004987static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004988{
4989 struct vmcb_control_area *control;
4990
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004991 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10004992 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03004993 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004994 control->int_ctl &= ~V_INTR_PRIO_MASK;
4995 control->int_ctl |= V_IRQ_MASK |
4996 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004997 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004998}
4999
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005000static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03005001{
5002 struct vcpu_svm *svm = to_svm(vcpu);
5003
Joerg Roedel2af91942009-08-07 11:49:28 +02005004 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01005005
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03005006 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
5007 ++vcpu->stat.irq_injections;
5008
Alexander Graf219b65d2009-06-15 15:21:25 +02005009 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
5010 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03005011}
5012
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005013static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
5014{
5015 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
5016}
5017
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005018static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
5019{
5020 struct vcpu_svm *svm = to_svm(vcpu);
5021
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005022 if (svm_nested_virtualize_tpr(vcpu) ||
5023 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005024 return;
5025
Radim Krčmář596f3142014-03-11 19:11:18 +01005026 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
5027
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005028 if (irr == -1)
5029 return;
5030
5031 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005032 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005033}
5034
Yang Zhang8d146952013-01-25 10:18:50 +08005035static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
5036{
5037 return;
5038}
5039
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005040static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005041{
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05005042 return avic && irqchip_split(vcpu->kvm);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005043}
5044
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005045static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
5046{
5047}
5048
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005049static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005050{
5051}
5052
5053/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03005054static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5055{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005056 struct vcpu_svm *svm = to_svm(vcpu);
5057 struct vmcb *vmcb = svm->vmcb;
5058
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05005059 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005060 return;
5061
5062 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
5063 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005064}
5065
Andrey Smetanin63086302015-11-10 15:36:32 +03005066static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005067{
5068 return;
5069}
5070
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005071static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
5072{
5073 kvm_lapic_set_irr(vec, vcpu->arch.apic);
5074 smp_mb__after_atomic();
5075
5076 if (avic_vcpu_is_running(vcpu))
5077 wrmsrl(SVM_AVIC_DOORBELL,
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05005078 kvm_cpu_get_apicid(vcpu->cpu));
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005079 else
5080 kvm_vcpu_wake_up(vcpu);
5081}
5082
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005083static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
5084{
5085 unsigned long flags;
5086 struct amd_svm_iommu_ir *cur;
5087
5088 spin_lock_irqsave(&svm->ir_list_lock, flags);
5089 list_for_each_entry(cur, &svm->ir_list, node) {
5090 if (cur->data != pi->ir_data)
5091 continue;
5092 list_del(&cur->node);
5093 kfree(cur);
5094 break;
5095 }
5096 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
5097}
5098
5099static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
5100{
5101 int ret = 0;
5102 unsigned long flags;
5103 struct amd_svm_iommu_ir *ir;
5104
5105 /**
5106 * In some cases, the existing irte is updaed and re-set,
5107 * so we need to check here if it's already been * added
5108 * to the ir_list.
5109 */
5110 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
5111 struct kvm *kvm = svm->vcpu.kvm;
5112 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
5113 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
5114 struct vcpu_svm *prev_svm;
5115
5116 if (!prev_vcpu) {
5117 ret = -EINVAL;
5118 goto out;
5119 }
5120
5121 prev_svm = to_svm(prev_vcpu);
5122 svm_ir_list_del(prev_svm, pi);
5123 }
5124
5125 /**
5126 * Allocating new amd_iommu_pi_data, which will get
5127 * add to the per-vcpu ir_list.
5128 */
5129 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
5130 if (!ir) {
5131 ret = -ENOMEM;
5132 goto out;
5133 }
5134 ir->data = pi->ir_data;
5135
5136 spin_lock_irqsave(&svm->ir_list_lock, flags);
5137 list_add(&ir->node, &svm->ir_list);
5138 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
5139out:
5140 return ret;
5141}
5142
5143/**
5144 * Note:
5145 * The HW cannot support posting multicast/broadcast
5146 * interrupts to a vCPU. So, we still use legacy interrupt
5147 * remapping for these kind of interrupts.
5148 *
5149 * For lowest-priority interrupts, we only support
5150 * those with single CPU as the destination, e.g. user
5151 * configures the interrupts via /proc/irq or uses
5152 * irqbalance to make the interrupts single-CPU.
5153 */
5154static int
5155get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
5156 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
5157{
5158 struct kvm_lapic_irq irq;
5159 struct kvm_vcpu *vcpu = NULL;
5160
5161 kvm_set_msi_irq(kvm, e, &irq);
5162
5163 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
5164 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
5165 __func__, irq.vector);
5166 return -1;
5167 }
5168
5169 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
5170 irq.vector);
5171 *svm = to_svm(vcpu);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005172 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005173 vcpu_info->vector = irq.vector;
5174
5175 return 0;
5176}
5177
5178/*
5179 * svm_update_pi_irte - set IRTE for Posted-Interrupts
5180 *
5181 * @kvm: kvm
5182 * @host_irq: host irq of the interrupt
5183 * @guest_irq: gsi of the interrupt
5184 * @set: set or unset PI
5185 * returns 0 on success, < 0 on failure
5186 */
5187static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
5188 uint32_t guest_irq, bool set)
5189{
5190 struct kvm_kernel_irq_routing_entry *e;
5191 struct kvm_irq_routing_table *irq_rt;
5192 int idx, ret = -EINVAL;
5193
5194 if (!kvm_arch_has_assigned_device(kvm) ||
5195 !irq_remapping_cap(IRQ_POSTING_CAP))
5196 return 0;
5197
5198 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
5199 __func__, host_irq, guest_irq, set);
5200
5201 idx = srcu_read_lock(&kvm->irq_srcu);
5202 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
5203 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
5204
5205 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
5206 struct vcpu_data vcpu_info;
5207 struct vcpu_svm *svm = NULL;
5208
5209 if (e->type != KVM_IRQ_ROUTING_MSI)
5210 continue;
5211
5212 /**
5213 * Here, we setup with legacy mode in the following cases:
5214 * 1. When cannot target interrupt to a specific vcpu.
5215 * 2. Unsetting posted interrupt.
5216 * 3. APIC virtialization is disabled for the vcpu.
5217 */
5218 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
5219 kvm_vcpu_apicv_active(&svm->vcpu)) {
5220 struct amd_iommu_pi_data pi;
5221
5222 /* Try to enable guest_mode in IRTE */
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005223 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
5224 AVIC_HPA_MASK);
Sean Christopherson81811c12018-03-20 12:17:21 -07005225 pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005226 svm->vcpu.vcpu_id);
5227 pi.is_guest_mode = true;
5228 pi.vcpu_data = &vcpu_info;
5229 ret = irq_set_vcpu_affinity(host_irq, &pi);
5230
5231 /**
5232 * Here, we successfully setting up vcpu affinity in
5233 * IOMMU guest mode. Now, we need to store the posted
5234 * interrupt information in a per-vcpu ir_list so that
5235 * we can reference to them directly when we update vcpu
5236 * scheduling information in IOMMU irte.
5237 */
5238 if (!ret && pi.is_guest_mode)
5239 svm_ir_list_add(svm, &pi);
5240 } else {
5241 /* Use legacy mode in IRTE */
5242 struct amd_iommu_pi_data pi;
5243
5244 /**
5245 * Here, pi is used to:
5246 * - Tell IOMMU to use legacy mode for this interrupt.
5247 * - Retrieve ga_tag of prior interrupt remapping data.
5248 */
5249 pi.is_guest_mode = false;
5250 ret = irq_set_vcpu_affinity(host_irq, &pi);
5251
5252 /**
5253 * Check if the posted interrupt was previously
5254 * setup with the guest_mode by checking if the ga_tag
5255 * was cached. If so, we need to clean up the per-vcpu
5256 * ir_list.
5257 */
5258 if (!ret && pi.prev_ga_tag) {
5259 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
5260 struct kvm_vcpu *vcpu;
5261
5262 vcpu = kvm_get_vcpu_by_id(kvm, id);
5263 if (vcpu)
5264 svm_ir_list_del(to_svm(vcpu), &pi);
5265 }
5266 }
5267
5268 if (!ret && svm) {
5269 trace_kvm_pi_irte_update(svm->vcpu.vcpu_id,
5270 host_irq, e->gsi,
5271 vcpu_info.vector,
5272 vcpu_info.pi_desc_addr, set);
5273 }
5274
5275 if (ret < 0) {
5276 pr_err("%s: failed to update PI IRTE\n", __func__);
5277 goto out;
5278 }
5279 }
5280
5281 ret = 0;
5282out:
5283 srcu_read_unlock(&kvm->irq_srcu, idx);
5284 return ret;
5285}
5286
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005287static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005288{
5289 struct vcpu_svm *svm = to_svm(vcpu);
5290 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02005291 int ret;
5292 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
5293 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
5294 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
5295
5296 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005297}
5298
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005299static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
5300{
5301 struct vcpu_svm *svm = to_svm(vcpu);
5302
5303 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
5304}
5305
5306static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5307{
5308 struct vcpu_svm *svm = to_svm(vcpu);
5309
5310 if (masked) {
5311 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005312 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005313 } else {
5314 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005315 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005316 }
5317}
5318
Gleb Natapov78646122009-03-23 12:12:11 +02005319static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
5320{
5321 struct vcpu_svm *svm = to_svm(vcpu);
5322 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005323 int ret;
5324
5325 if (!gif_set(svm) ||
5326 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
5327 return 0;
5328
Avi Kivityf6e78472010-08-02 15:30:20 +03005329 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005330
Joerg Roedel20307532010-11-29 17:51:48 +01005331 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005332 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
5333
5334 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02005335}
5336
Jan Kiszkac9a79532014-03-07 20:03:15 +01005337static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03005338{
Alexander Graf219b65d2009-06-15 15:21:25 +02005339 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02005340
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005341 if (kvm_vcpu_apicv_active(vcpu))
5342 return;
5343
Joerg Roedele0231712010-02-24 18:59:10 +01005344 /*
5345 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
5346 * 1, because that's a separate STGI/VMRUN intercept. The next time we
5347 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005348 * we'll get the vintr intercept. However, if the vGIF feature is
5349 * enabled, the STGI interception will not occur. Enable the irq
5350 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01005351 */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005352 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02005353 svm_set_vintr(svm);
5354 svm_inject_irq(svm, 0x0);
5355 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005356}
5357
Jan Kiszkac9a79532014-03-07 20:03:15 +01005358static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005359{
Avi Kivity04d2cc72007-09-10 18:10:54 +03005360 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005361
Gleb Natapov44c11432009-05-11 13:35:52 +03005362 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
5363 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01005364 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03005365
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005366 if (!gif_set(svm)) {
5367 if (vgif_enabled(svm))
5368 set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005369 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005370 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005371
5372 if (svm->nested.exit_required)
5373 return; /* we're not going to run the guest yet */
5374
Joerg Roedele0231712010-02-24 18:59:10 +01005375 /*
5376 * Something prevents NMI from been injected. Single step over possible
5377 * problem (IRET or exception injection or interrupt shadow)
5378 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02005379 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02005380 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03005381 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03005382}
5383
Izik Eiduscbc94022007-10-25 00:29:55 +02005384static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
5385{
5386 return 0;
5387}
5388
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07005389static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
5390{
5391 return 0;
5392}
5393
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005394static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Avi Kivityd9e368d2007-06-07 19:18:30 +03005395{
Joerg Roedel38e5e922010-12-03 15:25:16 +01005396 struct vcpu_svm *svm = to_svm(vcpu);
5397
5398 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
5399 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5400 else
5401 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03005402}
5403
Avi Kivity04d2cc72007-09-10 18:10:54 +03005404static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
5405{
5406}
5407
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005408static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
5409{
5410 struct vcpu_svm *svm = to_svm(vcpu);
5411
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005412 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005413 return;
5414
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005415 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005416 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03005417 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005418 }
5419}
5420
Joerg Roedel649d6862008-04-16 16:51:15 +02005421static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
5422{
5423 struct vcpu_svm *svm = to_svm(vcpu);
5424 u64 cr8;
5425
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005426 if (svm_nested_virtualize_tpr(vcpu) ||
5427 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005428 return;
5429
Joerg Roedel649d6862008-04-16 16:51:15 +02005430 cr8 = kvm_get_cr8(vcpu);
5431 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
5432 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
5433}
5434
Gleb Natapov9222be12009-04-23 17:14:37 +03005435static void svm_complete_interrupts(struct vcpu_svm *svm)
5436{
5437 u8 vector;
5438 int type;
5439 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01005440 unsigned int3_injected = svm->int3_injected;
5441
5442 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005443
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02005444 /*
5445 * If we've made progress since setting HF_IRET_MASK, we've
5446 * executed an IRET and can allow NMI injection.
5447 */
5448 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
5449 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03005450 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03005451 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5452 }
Gleb Natapov44c11432009-05-11 13:35:52 +03005453
Gleb Natapov9222be12009-04-23 17:14:37 +03005454 svm->vcpu.arch.nmi_injected = false;
5455 kvm_clear_exception_queue(&svm->vcpu);
5456 kvm_clear_interrupt_queue(&svm->vcpu);
5457
5458 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
5459 return;
5460
Avi Kivity3842d132010-07-27 12:30:24 +03005461 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5462
Gleb Natapov9222be12009-04-23 17:14:37 +03005463 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
5464 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
5465
5466 switch (type) {
5467 case SVM_EXITINTINFO_TYPE_NMI:
5468 svm->vcpu.arch.nmi_injected = true;
5469 break;
5470 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01005471 /*
5472 * In case of software exceptions, do not reinject the vector,
5473 * but re-execute the instruction instead. Rewind RIP first
5474 * if we emulated INT3 before.
5475 */
5476 if (kvm_exception_is_soft(vector)) {
5477 if (vector == BP_VECTOR && int3_injected &&
5478 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
5479 kvm_rip_write(&svm->vcpu,
5480 kvm_rip_read(&svm->vcpu) -
5481 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02005482 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01005483 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005484 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
5485 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005486 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03005487
5488 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005489 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03005490 break;
5491 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005492 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03005493 break;
5494 default:
5495 break;
5496 }
5497}
5498
Avi Kivityb463a6f2010-07-20 15:06:17 +03005499static void svm_cancel_injection(struct kvm_vcpu *vcpu)
5500{
5501 struct vcpu_svm *svm = to_svm(vcpu);
5502 struct vmcb_control_area *control = &svm->vmcb->control;
5503
5504 control->exit_int_info = control->event_inj;
5505 control->exit_int_info_err = control->event_inj_err;
5506 control->event_inj = 0;
5507 svm_complete_interrupts(svm);
5508}
5509
Avi Kivity851ba692009-08-24 11:10:17 +03005510static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005511{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005512 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03005513
Joerg Roedel2041a062010-04-22 12:33:08 +02005514 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5515 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5516 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5517
Joerg Roedelcd3ff652009-10-09 16:08:26 +02005518 /*
5519 * A vmexit emulation is required before the vcpu can be executed
5520 * again.
5521 */
5522 if (unlikely(svm->nested.exit_required))
5523 return;
5524
Ladi Proseka12713c2017-06-21 09:07:00 +02005525 /*
5526 * Disable singlestep if we're injecting an interrupt/exception.
5527 * We don't want our modified rflags to be pushed on the stack where
5528 * we might not be able to easily reset them if we disabled NMI
5529 * singlestep later.
5530 */
5531 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5532 /*
5533 * Event injection happens before external interrupts cause a
5534 * vmexit and interrupts are disabled here, so smp_send_reschedule
5535 * is enough to force an immediate vmexit.
5536 */
5537 disable_nmi_singlestep(svm);
5538 smp_send_reschedule(vcpu->cpu);
5539 }
5540
Rusty Russelle756fc62007-07-30 20:07:08 +10005541 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005542
Joerg Roedel649d6862008-04-16 16:51:15 +02005543 sync_lapic_to_cr8(vcpu);
5544
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02005545 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005546
Avi Kivity04d2cc72007-09-10 18:10:54 +03005547 clgi();
5548
5549 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08005550
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005551 /*
5552 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
5553 * it's non-zero. Since vmentry is serialising on affected CPUs, there
5554 * is no need to worry about the conditional branch over the wrmsr
5555 * being speculatively taken.
5556 */
5557 if (svm->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005558 native_wrmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005559
Avi Kivity6aa8b732006-12-10 02:21:36 -08005560 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03005561 "push %%" _ASM_BP "; \n\t"
5562 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5563 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5564 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5565 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5566 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5567 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005568#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005569 "mov %c[r8](%[svm]), %%r8 \n\t"
5570 "mov %c[r9](%[svm]), %%r9 \n\t"
5571 "mov %c[r10](%[svm]), %%r10 \n\t"
5572 "mov %c[r11](%[svm]), %%r11 \n\t"
5573 "mov %c[r12](%[svm]), %%r12 \n\t"
5574 "mov %c[r13](%[svm]), %%r13 \n\t"
5575 "mov %c[r14](%[svm]), %%r14 \n\t"
5576 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005577#endif
5578
Avi Kivity6aa8b732006-12-10 02:21:36 -08005579 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03005580 "push %%" _ASM_AX " \n\t"
5581 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03005582 __ex(SVM_VMLOAD) "\n\t"
5583 __ex(SVM_VMRUN) "\n\t"
5584 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005585 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005586
5587 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03005588 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5589 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5590 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5591 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5592 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5593 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005594#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005595 "mov %%r8, %c[r8](%[svm]) \n\t"
5596 "mov %%r9, %c[r9](%[svm]) \n\t"
5597 "mov %%r10, %c[r10](%[svm]) \n\t"
5598 "mov %%r11, %c[r11](%[svm]) \n\t"
5599 "mov %%r12, %c[r12](%[svm]) \n\t"
5600 "mov %%r13, %c[r13](%[svm]) \n\t"
5601 "mov %%r14, %c[r14](%[svm]) \n\t"
5602 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005603#endif
Jim Mattson0cb5b302018-01-03 14:31:38 -08005604 /*
5605 * Clear host registers marked as clobbered to prevent
5606 * speculative use.
5607 */
5608 "xor %%" _ASM_BX ", %%" _ASM_BX " \n\t"
5609 "xor %%" _ASM_CX ", %%" _ASM_CX " \n\t"
5610 "xor %%" _ASM_DX ", %%" _ASM_DX " \n\t"
5611 "xor %%" _ASM_SI ", %%" _ASM_SI " \n\t"
5612 "xor %%" _ASM_DI ", %%" _ASM_DI " \n\t"
5613#ifdef CONFIG_X86_64
5614 "xor %%r8, %%r8 \n\t"
5615 "xor %%r9, %%r9 \n\t"
5616 "xor %%r10, %%r10 \n\t"
5617 "xor %%r11, %%r11 \n\t"
5618 "xor %%r12, %%r12 \n\t"
5619 "xor %%r13, %%r13 \n\t"
5620 "xor %%r14, %%r14 \n\t"
5621 "xor %%r15, %%r15 \n\t"
5622#endif
Avi Kivity74547662012-09-16 15:10:59 +03005623 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08005624 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005625 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08005626 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005627 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5628 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5629 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5630 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5631 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5632 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005633#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005634 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5635 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5636 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5637 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5638 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5639 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5640 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5641 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005642#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02005643 : "cc", "memory"
5644#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03005645 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005646 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03005647#else
5648 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005649#endif
5650 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08005651
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005652 /*
5653 * We do not use IBRS in the kernel. If this vCPU has used the
5654 * SPEC_CTRL MSR it may have left it on; save the value and
5655 * turn it off. This is much more efficient than blindly adding
5656 * it to the atomic save/restore list. Especially as the former
5657 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
5658 *
5659 * For non-nested case:
5660 * If the L01 MSR bitmap does not intercept the MSR, then we need to
5661 * save it.
5662 *
5663 * For nested case:
5664 * If the L02 MSR bitmap does not intercept the MSR, then we need to
5665 * save it.
5666 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01005667 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005668 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005669
5670 if (svm->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005671 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005672
David Woodhouse117cc7a2018-01-12 11:11:27 +00005673 /* Eliminate branch target predictions from guest mode */
5674 vmexit_fill_RSB();
5675
Avi Kivity82ca2d12010-10-21 12:20:34 +02005676#ifdef CONFIG_X86_64
5677 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5678#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02005679 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02005680#ifndef CONFIG_X86_32_LAZY_GS
5681 loadsegment(gs, svm->host.gs);
5682#endif
Avi Kivity9581d442010-10-19 16:46:55 +02005683#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005684
5685 reload_tss(vcpu);
5686
Avi Kivity56ba47d2007-11-07 17:14:18 +02005687 local_irq_disable();
5688
Avi Kivity13c34e02010-10-21 12:20:31 +02005689 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5690 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5691 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5692 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5693
Joerg Roedel3781c012011-01-14 16:45:02 +01005694 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Andi Kleendd60d212017-07-25 17:20:32 -07005695 kvm_before_interrupt(&svm->vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01005696
5697 stgi();
5698
5699 /* Any pending NMI will happen here */
5700
5701 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Andi Kleendd60d212017-07-25 17:20:32 -07005702 kvm_after_interrupt(&svm->vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01005703
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005704 sync_cr8_to_lapic(vcpu);
5705
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005706 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005707
Joerg Roedel38e5e922010-12-03 15:25:16 +01005708 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5709
Gleb Natapov631bc482010-10-14 11:22:52 +02005710 /* if exit due to PF check for async PF */
5711 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005712 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
Gleb Natapov631bc482010-10-14 11:22:52 +02005713
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005714 if (npt_enabled) {
5715 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5716 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5717 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02005718
5719 /*
5720 * We need to handle MC intercepts here before the vcpu has a chance to
5721 * change the physical cpu
5722 */
5723 if (unlikely(svm->vmcb->control.exit_code ==
5724 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5725 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01005726
5727 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005728}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005729STACK_FRAME_NON_STANDARD(svm_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005730
Avi Kivity6aa8b732006-12-10 02:21:36 -08005731static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5732{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005733 struct vcpu_svm *svm = to_svm(vcpu);
5734
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005735 svm->vmcb->save.cr3 = __sme_set(root);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005736 mark_dirty(svm->vmcb, VMCB_CR);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005737 svm_flush_tlb(vcpu, true);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005738}
5739
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005740static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5741{
5742 struct vcpu_svm *svm = to_svm(vcpu);
5743
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005744 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01005745 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005746
5747 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02005748 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005749 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005750
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005751 svm_flush_tlb(vcpu, true);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005752}
5753
Avi Kivity6aa8b732006-12-10 02:21:36 -08005754static int is_disabled(void)
5755{
Joerg Roedel6031a612007-06-22 12:29:50 +03005756 u64 vm_cr;
5757
5758 rdmsrl(MSR_VM_CR, vm_cr);
5759 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5760 return 1;
5761
Avi Kivity6aa8b732006-12-10 02:21:36 -08005762 return 0;
5763}
5764
Ingo Molnar102d8322007-02-19 14:37:47 +02005765static void
5766svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5767{
5768 /*
5769 * Patch in the VMMCALL instruction:
5770 */
5771 hypercall[0] = 0x0f;
5772 hypercall[1] = 0x01;
5773 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02005774}
5775
Yang, Sheng002c7f72007-07-31 14:23:01 +03005776static void svm_check_processor_compat(void *rtn)
5777{
5778 *(int *)rtn = 0;
5779}
5780
Avi Kivity774ead32007-12-26 13:57:04 +02005781static bool svm_cpu_has_accelerated_tpr(void)
5782{
5783 return false;
5784}
5785
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005786static bool svm_has_high_real_mode_segbase(void)
5787{
5788 return true;
5789}
5790
Paolo Bonzinifc07e762015-10-01 13:20:22 +02005791static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5792{
5793 return 0;
5794}
5795
Sheng Yang0e851882009-12-18 16:48:46 +08005796static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5797{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005798 struct vcpu_svm *svm = to_svm(vcpu);
5799
5800 /* Update nrips enabled cache */
Radim Krčmářd6321d42017-08-05 00:12:49 +02005801 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005802
5803 if (!kvm_vcpu_apicv_active(vcpu))
5804 return;
5805
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02005806 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08005807}
5808
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005809static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5810{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005811 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005812 case 0x1:
5813 if (avic)
5814 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5815 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02005816 case 0x80000001:
5817 if (nested)
5818 entry->ecx |= (1 << 2); /* Set SVM bit */
5819 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005820 case 0x8000000A:
5821 entry->eax = 1; /* SVM revision 1 */
5822 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5823 ASID emulation to nested SVM */
5824 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02005825 entry->edx = 0; /* Per default do not support any
5826 additional features */
5827
5828 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02005829 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02005830 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005831
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005832 /* Support NPT for the guest if enabled */
5833 if (npt_enabled)
5834 entry->edx |= SVM_FEATURE_NPT;
5835
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005836 break;
Brijesh Singh8765d752017-12-04 10:57:25 -06005837 case 0x8000001F:
5838 /* Support memory encryption cpuid if host supports it */
5839 if (boot_cpu_has(X86_FEATURE_SEV))
5840 cpuid(0x8000001f, &entry->eax, &entry->ebx,
5841 &entry->ecx, &entry->edx);
5842
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005843 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005844}
5845
Sheng Yang17cc3932010-01-05 19:02:27 +08005846static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005847{
Sheng Yang17cc3932010-01-05 19:02:27 +08005848 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005849}
5850
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005851static bool svm_rdtscp_supported(void)
5852{
Paolo Bonzini46896c72015-11-12 14:49:16 +01005853 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005854}
5855
Mao, Junjiead756a12012-07-02 01:18:48 +00005856static bool svm_invpcid_supported(void)
5857{
5858 return false;
5859}
5860
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005861static bool svm_mpx_supported(void)
5862{
5863 return false;
5864}
5865
Wanpeng Li55412b22014-12-02 19:21:30 +08005866static bool svm_xsaves_supported(void)
5867{
5868 return false;
5869}
5870
Paolo Bonzini66336ca2016-07-12 10:36:41 +02005871static bool svm_umip_emulated(void)
5872{
5873 return false;
5874}
5875
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005876static bool svm_has_wbinvd_exit(void)
5877{
5878 return true;
5879}
5880
Joerg Roedel80612522011-04-04 12:39:33 +02005881#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005882 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005883#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005884 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005885#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005886 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005887
Mathias Krause09941fb2012-08-30 01:30:20 +02005888static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005889 u32 exit_code;
5890 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005891} x86_intercept_map[] = {
5892 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
5893 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
5894 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
5895 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
5896 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02005897 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
5898 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02005899 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
5900 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
5901 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
5902 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
5903 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
5904 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
5905 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
5906 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02005907 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
5908 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
5909 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
5910 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
5911 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
5912 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
5913 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
5914 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005915 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
5916 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
5917 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02005918 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
5919 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
5920 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
5921 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
5922 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
5923 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
5924 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
5925 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
5926 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02005927 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
5928 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
5929 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
5930 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
5931 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
5932 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
5933 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02005934 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
5935 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
5936 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
5937 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005938};
5939
Joerg Roedel80612522011-04-04 12:39:33 +02005940#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005941#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005942#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005943
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005944static int svm_check_intercept(struct kvm_vcpu *vcpu,
5945 struct x86_instruction_info *info,
5946 enum x86_intercept_stage stage)
5947{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005948 struct vcpu_svm *svm = to_svm(vcpu);
5949 int vmexit, ret = X86EMUL_CONTINUE;
5950 struct __x86_intercept icpt_info;
5951 struct vmcb *vmcb = svm->vmcb;
5952
5953 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
5954 goto out;
5955
5956 icpt_info = x86_intercept_map[info->intercept];
5957
Avi Kivity40e19b52011-04-21 12:35:41 +03005958 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005959 goto out;
5960
5961 switch (icpt_info.exit_code) {
5962 case SVM_EXIT_READ_CR0:
5963 if (info->intercept == x86_intercept_cr_read)
5964 icpt_info.exit_code += info->modrm_reg;
5965 break;
5966 case SVM_EXIT_WRITE_CR0: {
5967 unsigned long cr0, val;
5968 u64 intercept;
5969
5970 if (info->intercept == x86_intercept_cr_write)
5971 icpt_info.exit_code += info->modrm_reg;
5972
Jan Kiszka62baf442014-06-29 21:55:53 +02005973 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
5974 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005975 break;
5976
5977 intercept = svm->nested.intercept;
5978
5979 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
5980 break;
5981
5982 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
5983 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
5984
5985 if (info->intercept == x86_intercept_lmsw) {
5986 cr0 &= 0xfUL;
5987 val &= 0xfUL;
5988 /* lmsw can't clear PE - catch this here */
5989 if (cr0 & X86_CR0_PE)
5990 val |= X86_CR0_PE;
5991 }
5992
5993 if (cr0 ^ val)
5994 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
5995
5996 break;
5997 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02005998 case SVM_EXIT_READ_DR0:
5999 case SVM_EXIT_WRITE_DR0:
6000 icpt_info.exit_code += info->modrm_reg;
6001 break;
Joerg Roedel80612522011-04-04 12:39:33 +02006002 case SVM_EXIT_MSR:
6003 if (info->intercept == x86_intercept_wrmsr)
6004 vmcb->control.exit_info_1 = 1;
6005 else
6006 vmcb->control.exit_info_1 = 0;
6007 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02006008 case SVM_EXIT_PAUSE:
6009 /*
6010 * We get this for NOP only, but pause
6011 * is rep not, check this here
6012 */
6013 if (info->rep_prefix != REPE_PREFIX)
6014 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02006015 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02006016 case SVM_EXIT_IOIO: {
6017 u64 exit_info;
6018 u32 bytes;
6019
Joerg Roedelf6511932011-04-04 12:39:35 +02006020 if (info->intercept == x86_intercept_in ||
6021 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02006022 exit_info = ((info->src_val & 0xffff) << 16) |
6023 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02006024 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02006025 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02006026 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02006027 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02006028 }
6029
6030 if (info->intercept == x86_intercept_outs ||
6031 info->intercept == x86_intercept_ins)
6032 exit_info |= SVM_IOIO_STR_MASK;
6033
6034 if (info->rep_prefix)
6035 exit_info |= SVM_IOIO_REP_MASK;
6036
6037 bytes = min(bytes, 4u);
6038
6039 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
6040
6041 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
6042
6043 vmcb->control.exit_info_1 = exit_info;
6044 vmcb->control.exit_info_2 = info->next_rip;
6045
6046 break;
6047 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006048 default:
6049 break;
6050 }
6051
Bandan Dasf1047652015-06-11 02:05:33 -04006052 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
6053 if (static_cpu_has(X86_FEATURE_NRIPS))
6054 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006055 vmcb->control.exit_code = icpt_info.exit_code;
6056 vmexit = nested_svm_exit_handled(svm);
6057
6058 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
6059 : X86EMUL_CONTINUE;
6060
6061out:
6062 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006063}
6064
Yang Zhanga547c6d2013-04-11 19:25:10 +08006065static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
6066{
6067 local_irq_enable();
Paolo Bonzinif2485b32016-06-15 15:23:11 +02006068 /*
6069 * We must have an instruction with interrupts enabled, so
6070 * the timer interrupt isn't delayed by the interrupt shadow.
6071 */
6072 asm("nop");
6073 local_irq_disable();
Yang Zhanga547c6d2013-04-11 19:25:10 +08006074}
6075
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006076static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
6077{
Babu Moger8566ac82018-03-16 16:37:26 -04006078 if (pause_filter_thresh)
6079 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006080}
6081
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05006082static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
6083{
6084 if (avic_handle_apic_id_update(vcpu) != 0)
6085 return;
6086 if (avic_handle_dfr_update(vcpu) != 0)
6087 return;
6088 avic_handle_ldr_update(vcpu);
6089}
6090
Borislav Petkov74f16902017-03-26 23:51:24 +02006091static void svm_setup_mce(struct kvm_vcpu *vcpu)
6092{
6093 /* [63:9] are reserved. */
6094 vcpu->arch.mcg_cap &= 0x1ff;
6095}
6096
Ladi Prosek72d7b372017-10-11 16:54:41 +02006097static int svm_smi_allowed(struct kvm_vcpu *vcpu)
6098{
Ladi Prosek05cade72017-10-11 16:54:45 +02006099 struct vcpu_svm *svm = to_svm(vcpu);
6100
6101 /* Per APM Vol.2 15.22.2 "Response to SMI" */
6102 if (!gif_set(svm))
6103 return 0;
6104
6105 if (is_guest_mode(&svm->vcpu) &&
6106 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
6107 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
6108 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
6109 svm->nested.exit_required = true;
6110 return 0;
6111 }
6112
Ladi Prosek72d7b372017-10-11 16:54:41 +02006113 return 1;
6114}
6115
Ladi Prosek0234bf82017-10-11 16:54:40 +02006116static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
6117{
Ladi Prosek05cade72017-10-11 16:54:45 +02006118 struct vcpu_svm *svm = to_svm(vcpu);
6119 int ret;
6120
6121 if (is_guest_mode(vcpu)) {
6122 /* FED8h - SVM Guest */
6123 put_smstate(u64, smstate, 0x7ed8, 1);
6124 /* FEE0h - SVM Guest VMCB Physical Address */
6125 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
6126
6127 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
6128 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
6129 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
6130
6131 ret = nested_svm_vmexit(svm);
6132 if (ret)
6133 return ret;
6134 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02006135 return 0;
6136}
6137
6138static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
6139{
Ladi Prosek05cade72017-10-11 16:54:45 +02006140 struct vcpu_svm *svm = to_svm(vcpu);
6141 struct vmcb *nested_vmcb;
6142 struct page *page;
6143 struct {
6144 u64 guest;
6145 u64 vmcb;
6146 } svm_state_save;
6147 int ret;
6148
6149 ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save,
6150 sizeof(svm_state_save));
6151 if (ret)
6152 return ret;
6153
6154 if (svm_state_save.guest) {
6155 vcpu->arch.hflags &= ~HF_SMM_MASK;
6156 nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page);
6157 if (nested_vmcb)
6158 enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page);
6159 else
6160 ret = 1;
6161 vcpu->arch.hflags |= HF_SMM_MASK;
6162 }
6163 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02006164}
6165
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006166static int enable_smi_window(struct kvm_vcpu *vcpu)
6167{
6168 struct vcpu_svm *svm = to_svm(vcpu);
6169
6170 if (!gif_set(svm)) {
6171 if (vgif_enabled(svm))
6172 set_intercept(svm, INTERCEPT_STGI);
6173 /* STGI will cause a vm exit */
6174 return 1;
6175 }
6176 return 0;
6177}
6178
Brijesh Singh1654efc2017-12-04 10:57:34 -06006179static int sev_asid_new(void)
6180{
6181 int pos;
6182
6183 /*
6184 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
6185 */
6186 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
6187 if (pos >= max_sev_asid)
6188 return -EBUSY;
6189
6190 set_bit(pos, sev_asid_bitmap);
6191 return pos + 1;
6192}
6193
6194static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
6195{
Sean Christopherson81811c12018-03-20 12:17:21 -07006196 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006197 int asid, ret;
6198
6199 ret = -EBUSY;
6200 asid = sev_asid_new();
6201 if (asid < 0)
6202 return ret;
6203
6204 ret = sev_platform_init(&argp->error);
6205 if (ret)
6206 goto e_free;
6207
6208 sev->active = true;
6209 sev->asid = asid;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006210 INIT_LIST_HEAD(&sev->regions_list);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006211
6212 return 0;
6213
6214e_free:
6215 __sev_asid_free(asid);
6216 return ret;
6217}
6218
Brijesh Singh59414c92017-12-04 10:57:35 -06006219static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
6220{
6221 struct sev_data_activate *data;
6222 int asid = sev_get_asid(kvm);
6223 int ret;
6224
6225 wbinvd_on_all_cpus();
6226
6227 ret = sev_guest_df_flush(error);
6228 if (ret)
6229 return ret;
6230
6231 data = kzalloc(sizeof(*data), GFP_KERNEL);
6232 if (!data)
6233 return -ENOMEM;
6234
6235 /* activate ASID on the given handle */
6236 data->handle = handle;
6237 data->asid = asid;
6238 ret = sev_guest_activate(data, error);
6239 kfree(data);
6240
6241 return ret;
6242}
6243
Brijesh Singh89c50582017-12-04 10:57:35 -06006244static int __sev_issue_cmd(int fd, int id, void *data, int *error)
Brijesh Singh59414c92017-12-04 10:57:35 -06006245{
6246 struct fd f;
6247 int ret;
6248
6249 f = fdget(fd);
6250 if (!f.file)
6251 return -EBADF;
6252
6253 ret = sev_issue_cmd_external_user(f.file, id, data, error);
6254
6255 fdput(f);
6256 return ret;
6257}
6258
Brijesh Singh89c50582017-12-04 10:57:35 -06006259static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
6260{
Sean Christopherson81811c12018-03-20 12:17:21 -07006261 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06006262
6263 return __sev_issue_cmd(sev->fd, id, data, error);
6264}
6265
Brijesh Singh59414c92017-12-04 10:57:35 -06006266static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
6267{
Sean Christopherson81811c12018-03-20 12:17:21 -07006268 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh59414c92017-12-04 10:57:35 -06006269 struct sev_data_launch_start *start;
6270 struct kvm_sev_launch_start params;
6271 void *dh_blob, *session_blob;
6272 int *error = &argp->error;
6273 int ret;
6274
6275 if (!sev_guest(kvm))
6276 return -ENOTTY;
6277
6278 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6279 return -EFAULT;
6280
6281 start = kzalloc(sizeof(*start), GFP_KERNEL);
6282 if (!start)
6283 return -ENOMEM;
6284
6285 dh_blob = NULL;
6286 if (params.dh_uaddr) {
6287 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
6288 if (IS_ERR(dh_blob)) {
6289 ret = PTR_ERR(dh_blob);
6290 goto e_free;
6291 }
6292
6293 start->dh_cert_address = __sme_set(__pa(dh_blob));
6294 start->dh_cert_len = params.dh_len;
6295 }
6296
6297 session_blob = NULL;
6298 if (params.session_uaddr) {
6299 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
6300 if (IS_ERR(session_blob)) {
6301 ret = PTR_ERR(session_blob);
6302 goto e_free_dh;
6303 }
6304
6305 start->session_address = __sme_set(__pa(session_blob));
6306 start->session_len = params.session_len;
6307 }
6308
6309 start->handle = params.handle;
6310 start->policy = params.policy;
6311
6312 /* create memory encryption context */
Brijesh Singh89c50582017-12-04 10:57:35 -06006313 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
Brijesh Singh59414c92017-12-04 10:57:35 -06006314 if (ret)
6315 goto e_free_session;
6316
6317 /* Bind ASID to this guest */
6318 ret = sev_bind_asid(kvm, start->handle, error);
6319 if (ret)
6320 goto e_free_session;
6321
6322 /* return handle to userspace */
6323 params.handle = start->handle;
6324 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
6325 sev_unbind_asid(kvm, start->handle);
6326 ret = -EFAULT;
6327 goto e_free_session;
6328 }
6329
6330 sev->handle = start->handle;
6331 sev->fd = argp->sev_fd;
6332
6333e_free_session:
6334 kfree(session_blob);
6335e_free_dh:
6336 kfree(dh_blob);
6337e_free:
6338 kfree(start);
6339 return ret;
6340}
6341
Brijesh Singh89c50582017-12-04 10:57:35 -06006342static int get_num_contig_pages(int idx, struct page **inpages,
6343 unsigned long npages)
6344{
6345 unsigned long paddr, next_paddr;
6346 int i = idx + 1, pages = 1;
6347
6348 /* find the number of contiguous pages starting from idx */
6349 paddr = __sme_page_pa(inpages[idx]);
6350 while (i < npages) {
6351 next_paddr = __sme_page_pa(inpages[i++]);
6352 if ((paddr + PAGE_SIZE) == next_paddr) {
6353 pages++;
6354 paddr = next_paddr;
6355 continue;
6356 }
6357 break;
6358 }
6359
6360 return pages;
6361}
6362
6363static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
6364{
6365 unsigned long vaddr, vaddr_end, next_vaddr, npages, size;
Sean Christopherson81811c12018-03-20 12:17:21 -07006366 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06006367 struct kvm_sev_launch_update_data params;
6368 struct sev_data_launch_update_data *data;
6369 struct page **inpages;
6370 int i, ret, pages;
6371
6372 if (!sev_guest(kvm))
6373 return -ENOTTY;
6374
6375 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6376 return -EFAULT;
6377
6378 data = kzalloc(sizeof(*data), GFP_KERNEL);
6379 if (!data)
6380 return -ENOMEM;
6381
6382 vaddr = params.uaddr;
6383 size = params.len;
6384 vaddr_end = vaddr + size;
6385
6386 /* Lock the user memory. */
6387 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
6388 if (!inpages) {
6389 ret = -ENOMEM;
6390 goto e_free;
6391 }
6392
6393 /*
6394 * The LAUNCH_UPDATE command will perform in-place encryption of the
6395 * memory content (i.e it will write the same memory region with C=1).
6396 * It's possible that the cache may contain the data with C=0, i.e.,
6397 * unencrypted so invalidate it first.
6398 */
6399 sev_clflush_pages(inpages, npages);
6400
6401 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
6402 int offset, len;
6403
6404 /*
6405 * If the user buffer is not page-aligned, calculate the offset
6406 * within the page.
6407 */
6408 offset = vaddr & (PAGE_SIZE - 1);
6409
6410 /* Calculate the number of pages that can be encrypted in one go. */
6411 pages = get_num_contig_pages(i, inpages, npages);
6412
6413 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
6414
6415 data->handle = sev->handle;
6416 data->len = len;
6417 data->address = __sme_page_pa(inpages[i]) + offset;
6418 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
6419 if (ret)
6420 goto e_unpin;
6421
6422 size -= len;
6423 next_vaddr = vaddr + len;
6424 }
6425
6426e_unpin:
6427 /* content of memory is updated, mark pages dirty */
6428 for (i = 0; i < npages; i++) {
6429 set_page_dirty_lock(inpages[i]);
6430 mark_page_accessed(inpages[i]);
6431 }
6432 /* unlock the user pages */
6433 sev_unpin_memory(kvm, inpages, npages);
6434e_free:
6435 kfree(data);
6436 return ret;
6437}
6438
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006439static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
6440{
Brijesh Singh3e233382018-02-23 12:36:50 -06006441 void __user *measure = (void __user *)(uintptr_t)argp->data;
Sean Christopherson81811c12018-03-20 12:17:21 -07006442 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006443 struct sev_data_launch_measure *data;
6444 struct kvm_sev_launch_measure params;
Brijesh Singh3e233382018-02-23 12:36:50 -06006445 void __user *p = NULL;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006446 void *blob = NULL;
6447 int ret;
6448
6449 if (!sev_guest(kvm))
6450 return -ENOTTY;
6451
Brijesh Singh3e233382018-02-23 12:36:50 -06006452 if (copy_from_user(&params, measure, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006453 return -EFAULT;
6454
6455 data = kzalloc(sizeof(*data), GFP_KERNEL);
6456 if (!data)
6457 return -ENOMEM;
6458
6459 /* User wants to query the blob length */
6460 if (!params.len)
6461 goto cmd;
6462
Brijesh Singh3e233382018-02-23 12:36:50 -06006463 p = (void __user *)(uintptr_t)params.uaddr;
6464 if (p) {
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006465 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
6466 ret = -EINVAL;
6467 goto e_free;
6468 }
6469
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006470 ret = -ENOMEM;
6471 blob = kmalloc(params.len, GFP_KERNEL);
6472 if (!blob)
6473 goto e_free;
6474
6475 data->address = __psp_pa(blob);
6476 data->len = params.len;
6477 }
6478
6479cmd:
6480 data->handle = sev->handle;
6481 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
6482
6483 /*
6484 * If we query the session length, FW responded with expected data.
6485 */
6486 if (!params.len)
6487 goto done;
6488
6489 if (ret)
6490 goto e_free_blob;
6491
6492 if (blob) {
Brijesh Singh3e233382018-02-23 12:36:50 -06006493 if (copy_to_user(p, blob, params.len))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006494 ret = -EFAULT;
6495 }
6496
6497done:
6498 params.len = data->len;
Brijesh Singh3e233382018-02-23 12:36:50 -06006499 if (copy_to_user(measure, &params, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006500 ret = -EFAULT;
6501e_free_blob:
6502 kfree(blob);
6503e_free:
6504 kfree(data);
6505 return ret;
6506}
6507
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006508static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
6509{
Sean Christopherson81811c12018-03-20 12:17:21 -07006510 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006511 struct sev_data_launch_finish *data;
6512 int ret;
6513
6514 if (!sev_guest(kvm))
6515 return -ENOTTY;
6516
6517 data = kzalloc(sizeof(*data), GFP_KERNEL);
6518 if (!data)
6519 return -ENOMEM;
6520
6521 data->handle = sev->handle;
6522 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
6523
6524 kfree(data);
6525 return ret;
6526}
6527
Brijesh Singh255d9e72017-12-04 10:57:37 -06006528static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
6529{
Sean Christopherson81811c12018-03-20 12:17:21 -07006530 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006531 struct kvm_sev_guest_status params;
6532 struct sev_data_guest_status *data;
6533 int ret;
6534
6535 if (!sev_guest(kvm))
6536 return -ENOTTY;
6537
6538 data = kzalloc(sizeof(*data), GFP_KERNEL);
6539 if (!data)
6540 return -ENOMEM;
6541
6542 data->handle = sev->handle;
6543 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
6544 if (ret)
6545 goto e_free;
6546
6547 params.policy = data->policy;
6548 params.state = data->state;
6549 params.handle = data->handle;
6550
6551 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6552 ret = -EFAULT;
6553e_free:
6554 kfree(data);
6555 return ret;
6556}
6557
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006558static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
6559 unsigned long dst, int size,
6560 int *error, bool enc)
6561{
Sean Christopherson81811c12018-03-20 12:17:21 -07006562 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006563 struct sev_data_dbg *data;
6564 int ret;
6565
6566 data = kzalloc(sizeof(*data), GFP_KERNEL);
6567 if (!data)
6568 return -ENOMEM;
6569
6570 data->handle = sev->handle;
6571 data->dst_addr = dst;
6572 data->src_addr = src;
6573 data->len = size;
6574
6575 ret = sev_issue_cmd(kvm,
6576 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
6577 data, error);
6578 kfree(data);
6579 return ret;
6580}
6581
6582static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
6583 unsigned long dst_paddr, int sz, int *err)
6584{
6585 int offset;
6586
6587 /*
6588 * Its safe to read more than we are asked, caller should ensure that
6589 * destination has enough space.
6590 */
6591 src_paddr = round_down(src_paddr, 16);
6592 offset = src_paddr & 15;
6593 sz = round_up(sz + offset, 16);
6594
6595 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
6596}
6597
6598static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
6599 unsigned long __user dst_uaddr,
6600 unsigned long dst_paddr,
6601 int size, int *err)
6602{
6603 struct page *tpage = NULL;
6604 int ret, offset;
6605
6606 /* if inputs are not 16-byte then use intermediate buffer */
6607 if (!IS_ALIGNED(dst_paddr, 16) ||
6608 !IS_ALIGNED(paddr, 16) ||
6609 !IS_ALIGNED(size, 16)) {
6610 tpage = (void *)alloc_page(GFP_KERNEL);
6611 if (!tpage)
6612 return -ENOMEM;
6613
6614 dst_paddr = __sme_page_pa(tpage);
6615 }
6616
6617 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
6618 if (ret)
6619 goto e_free;
6620
6621 if (tpage) {
6622 offset = paddr & 15;
6623 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
6624 page_address(tpage) + offset, size))
6625 ret = -EFAULT;
6626 }
6627
6628e_free:
6629 if (tpage)
6630 __free_page(tpage);
6631
6632 return ret;
6633}
6634
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006635static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
6636 unsigned long __user vaddr,
6637 unsigned long dst_paddr,
6638 unsigned long __user dst_vaddr,
6639 int size, int *error)
6640{
6641 struct page *src_tpage = NULL;
6642 struct page *dst_tpage = NULL;
6643 int ret, len = size;
6644
6645 /* If source buffer is not aligned then use an intermediate buffer */
6646 if (!IS_ALIGNED(vaddr, 16)) {
6647 src_tpage = alloc_page(GFP_KERNEL);
6648 if (!src_tpage)
6649 return -ENOMEM;
6650
6651 if (copy_from_user(page_address(src_tpage),
6652 (void __user *)(uintptr_t)vaddr, size)) {
6653 __free_page(src_tpage);
6654 return -EFAULT;
6655 }
6656
6657 paddr = __sme_page_pa(src_tpage);
6658 }
6659
6660 /*
6661 * If destination buffer or length is not aligned then do read-modify-write:
6662 * - decrypt destination in an intermediate buffer
6663 * - copy the source buffer in an intermediate buffer
6664 * - use the intermediate buffer as source buffer
6665 */
6666 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
6667 int dst_offset;
6668
6669 dst_tpage = alloc_page(GFP_KERNEL);
6670 if (!dst_tpage) {
6671 ret = -ENOMEM;
6672 goto e_free;
6673 }
6674
6675 ret = __sev_dbg_decrypt(kvm, dst_paddr,
6676 __sme_page_pa(dst_tpage), size, error);
6677 if (ret)
6678 goto e_free;
6679
6680 /*
6681 * If source is kernel buffer then use memcpy() otherwise
6682 * copy_from_user().
6683 */
6684 dst_offset = dst_paddr & 15;
6685
6686 if (src_tpage)
6687 memcpy(page_address(dst_tpage) + dst_offset,
6688 page_address(src_tpage), size);
6689 else {
6690 if (copy_from_user(page_address(dst_tpage) + dst_offset,
6691 (void __user *)(uintptr_t)vaddr, size)) {
6692 ret = -EFAULT;
6693 goto e_free;
6694 }
6695 }
6696
6697 paddr = __sme_page_pa(dst_tpage);
6698 dst_paddr = round_down(dst_paddr, 16);
6699 len = round_up(size, 16);
6700 }
6701
6702 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
6703
6704e_free:
6705 if (src_tpage)
6706 __free_page(src_tpage);
6707 if (dst_tpage)
6708 __free_page(dst_tpage);
6709 return ret;
6710}
6711
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006712static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
6713{
6714 unsigned long vaddr, vaddr_end, next_vaddr;
6715 unsigned long dst_vaddr, dst_vaddr_end;
6716 struct page **src_p, **dst_p;
6717 struct kvm_sev_dbg debug;
6718 unsigned long n;
6719 int ret, size;
6720
6721 if (!sev_guest(kvm))
6722 return -ENOTTY;
6723
6724 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
6725 return -EFAULT;
6726
6727 vaddr = debug.src_uaddr;
6728 size = debug.len;
6729 vaddr_end = vaddr + size;
6730 dst_vaddr = debug.dst_uaddr;
6731 dst_vaddr_end = dst_vaddr + size;
6732
6733 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
6734 int len, s_off, d_off;
6735
6736 /* lock userspace source and destination page */
6737 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
6738 if (!src_p)
6739 return -EFAULT;
6740
6741 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
6742 if (!dst_p) {
6743 sev_unpin_memory(kvm, src_p, n);
6744 return -EFAULT;
6745 }
6746
6747 /*
6748 * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
6749 * memory content (i.e it will write the same memory region with C=1).
6750 * It's possible that the cache may contain the data with C=0, i.e.,
6751 * unencrypted so invalidate it first.
6752 */
6753 sev_clflush_pages(src_p, 1);
6754 sev_clflush_pages(dst_p, 1);
6755
6756 /*
6757 * Since user buffer may not be page aligned, calculate the
6758 * offset within the page.
6759 */
6760 s_off = vaddr & ~PAGE_MASK;
6761 d_off = dst_vaddr & ~PAGE_MASK;
6762 len = min_t(size_t, (PAGE_SIZE - s_off), size);
6763
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006764 if (dec)
6765 ret = __sev_dbg_decrypt_user(kvm,
6766 __sme_page_pa(src_p[0]) + s_off,
6767 dst_vaddr,
6768 __sme_page_pa(dst_p[0]) + d_off,
6769 len, &argp->error);
6770 else
6771 ret = __sev_dbg_encrypt_user(kvm,
6772 __sme_page_pa(src_p[0]) + s_off,
6773 vaddr,
6774 __sme_page_pa(dst_p[0]) + d_off,
6775 dst_vaddr,
6776 len, &argp->error);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006777
6778 sev_unpin_memory(kvm, src_p, 1);
6779 sev_unpin_memory(kvm, dst_p, 1);
6780
6781 if (ret)
6782 goto err;
6783
6784 next_vaddr = vaddr + len;
6785 dst_vaddr = dst_vaddr + len;
6786 size -= len;
6787 }
6788err:
6789 return ret;
6790}
6791
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006792static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
6793{
Sean Christopherson81811c12018-03-20 12:17:21 -07006794 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006795 struct sev_data_launch_secret *data;
6796 struct kvm_sev_launch_secret params;
6797 struct page **pages;
6798 void *blob, *hdr;
6799 unsigned long n;
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006800 int ret, offset;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006801
6802 if (!sev_guest(kvm))
6803 return -ENOTTY;
6804
6805 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6806 return -EFAULT;
6807
6808 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
6809 if (!pages)
6810 return -ENOMEM;
6811
6812 /*
6813 * The secret must be copied into contiguous memory region, lets verify
6814 * that userspace memory pages are contiguous before we issue command.
6815 */
6816 if (get_num_contig_pages(0, pages, n) != n) {
6817 ret = -EINVAL;
6818 goto e_unpin_memory;
6819 }
6820
6821 ret = -ENOMEM;
6822 data = kzalloc(sizeof(*data), GFP_KERNEL);
6823 if (!data)
6824 goto e_unpin_memory;
6825
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006826 offset = params.guest_uaddr & (PAGE_SIZE - 1);
6827 data->guest_address = __sme_page_pa(pages[0]) + offset;
6828 data->guest_len = params.guest_len;
6829
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006830 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
6831 if (IS_ERR(blob)) {
6832 ret = PTR_ERR(blob);
6833 goto e_free;
6834 }
6835
6836 data->trans_address = __psp_pa(blob);
6837 data->trans_len = params.trans_len;
6838
6839 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
6840 if (IS_ERR(hdr)) {
6841 ret = PTR_ERR(hdr);
6842 goto e_free_blob;
6843 }
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006844 data->hdr_address = __psp_pa(hdr);
6845 data->hdr_len = params.hdr_len;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006846
6847 data->handle = sev->handle;
6848 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
6849
6850 kfree(hdr);
6851
6852e_free_blob:
6853 kfree(blob);
6854e_free:
6855 kfree(data);
6856e_unpin_memory:
6857 sev_unpin_memory(kvm, pages, n);
6858 return ret;
6859}
6860
Brijesh Singh1654efc2017-12-04 10:57:34 -06006861static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
6862{
6863 struct kvm_sev_cmd sev_cmd;
6864 int r;
6865
6866 if (!svm_sev_enabled())
6867 return -ENOTTY;
6868
6869 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
6870 return -EFAULT;
6871
6872 mutex_lock(&kvm->lock);
6873
6874 switch (sev_cmd.id) {
6875 case KVM_SEV_INIT:
6876 r = sev_guest_init(kvm, &sev_cmd);
6877 break;
Brijesh Singh59414c92017-12-04 10:57:35 -06006878 case KVM_SEV_LAUNCH_START:
6879 r = sev_launch_start(kvm, &sev_cmd);
6880 break;
Brijesh Singh89c50582017-12-04 10:57:35 -06006881 case KVM_SEV_LAUNCH_UPDATE_DATA:
6882 r = sev_launch_update_data(kvm, &sev_cmd);
6883 break;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006884 case KVM_SEV_LAUNCH_MEASURE:
6885 r = sev_launch_measure(kvm, &sev_cmd);
6886 break;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006887 case KVM_SEV_LAUNCH_FINISH:
6888 r = sev_launch_finish(kvm, &sev_cmd);
6889 break;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006890 case KVM_SEV_GUEST_STATUS:
6891 r = sev_guest_status(kvm, &sev_cmd);
6892 break;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006893 case KVM_SEV_DBG_DECRYPT:
6894 r = sev_dbg_crypt(kvm, &sev_cmd, true);
6895 break;
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006896 case KVM_SEV_DBG_ENCRYPT:
6897 r = sev_dbg_crypt(kvm, &sev_cmd, false);
6898 break;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006899 case KVM_SEV_LAUNCH_SECRET:
6900 r = sev_launch_secret(kvm, &sev_cmd);
6901 break;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006902 default:
6903 r = -EINVAL;
6904 goto out;
6905 }
6906
6907 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
6908 r = -EFAULT;
6909
6910out:
6911 mutex_unlock(&kvm->lock);
6912 return r;
6913}
6914
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006915static int svm_register_enc_region(struct kvm *kvm,
6916 struct kvm_enc_region *range)
6917{
Sean Christopherson81811c12018-03-20 12:17:21 -07006918 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006919 struct enc_region *region;
6920 int ret = 0;
6921
6922 if (!sev_guest(kvm))
6923 return -ENOTTY;
6924
6925 region = kzalloc(sizeof(*region), GFP_KERNEL);
6926 if (!region)
6927 return -ENOMEM;
6928
6929 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
6930 if (!region->pages) {
6931 ret = -ENOMEM;
6932 goto e_free;
6933 }
6934
6935 /*
6936 * The guest may change the memory encryption attribute from C=0 -> C=1
6937 * or vice versa for this memory range. Lets make sure caches are
6938 * flushed to ensure that guest data gets written into memory with
6939 * correct C-bit.
6940 */
6941 sev_clflush_pages(region->pages, region->npages);
6942
6943 region->uaddr = range->addr;
6944 region->size = range->size;
6945
6946 mutex_lock(&kvm->lock);
6947 list_add_tail(&region->list, &sev->regions_list);
6948 mutex_unlock(&kvm->lock);
6949
6950 return ret;
6951
6952e_free:
6953 kfree(region);
6954 return ret;
6955}
6956
6957static struct enc_region *
6958find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
6959{
Sean Christopherson81811c12018-03-20 12:17:21 -07006960 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006961 struct list_head *head = &sev->regions_list;
6962 struct enc_region *i;
6963
6964 list_for_each_entry(i, head, list) {
6965 if (i->uaddr == range->addr &&
6966 i->size == range->size)
6967 return i;
6968 }
6969
6970 return NULL;
6971}
6972
6973
6974static int svm_unregister_enc_region(struct kvm *kvm,
6975 struct kvm_enc_region *range)
6976{
6977 struct enc_region *region;
6978 int ret;
6979
6980 mutex_lock(&kvm->lock);
6981
6982 if (!sev_guest(kvm)) {
6983 ret = -ENOTTY;
6984 goto failed;
6985 }
6986
6987 region = find_enc_region(kvm, range);
6988 if (!region) {
6989 ret = -EINVAL;
6990 goto failed;
6991 }
6992
6993 __unregister_enc_region_locked(kvm, region);
6994
6995 mutex_unlock(&kvm->lock);
6996 return 0;
6997
6998failed:
6999 mutex_unlock(&kvm->lock);
7000 return ret;
7001}
7002
Kees Cook404f6aa2016-08-08 16:29:06 -07007003static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007004 .cpu_has_kvm_support = has_svm,
7005 .disabled_by_bios = is_disabled,
7006 .hardware_setup = svm_hardware_setup,
7007 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03007008 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007009 .hardware_enable = svm_hardware_enable,
7010 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02007011 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02007012 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007013
7014 .vcpu_create = svm_create_vcpu,
7015 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007016 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007017
Sean Christopherson434a1e92018-03-20 12:17:18 -07007018 .vm_alloc = svm_vm_alloc,
7019 .vm_free = svm_vm_free,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007020 .vm_init = avic_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06007021 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007022
Avi Kivity04d2cc72007-09-10 18:10:54 +03007023 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007024 .vcpu_load = svm_vcpu_load,
7025 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05007026 .vcpu_blocking = svm_vcpu_blocking,
7027 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007028
Paolo Bonzinia96036b2015-11-10 11:55:36 +01007029 .update_bp_intercept = update_bp_intercept,
Tom Lendacky801e4592018-02-21 13:39:51 -06007030 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007031 .get_msr = svm_get_msr,
7032 .set_msr = svm_set_msr,
7033 .get_segment_base = svm_get_segment_base,
7034 .get_segment = svm_get_segment,
7035 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007036 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10007037 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02007038 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02007039 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03007040 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007041 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007042 .set_cr3 = svm_set_cr3,
7043 .set_cr4 = svm_set_cr4,
7044 .set_efer = svm_set_efer,
7045 .get_idt = svm_get_idt,
7046 .set_idt = svm_set_idt,
7047 .get_gdt = svm_get_gdt,
7048 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007049 .get_dr6 = svm_get_dr6,
7050 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03007051 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01007052 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03007053 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007054 .get_rflags = svm_get_rflags,
7055 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007056
Avi Kivity6aa8b732006-12-10 02:21:36 -08007057 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007058
Avi Kivity6aa8b732006-12-10 02:21:36 -08007059 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007060 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007061 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007062 .set_interrupt_shadow = svm_set_interrupt_shadow,
7063 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007064 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007065 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007066 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007067 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007068 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007069 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007070 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007071 .get_nmi_mask = svm_get_nmi_mask,
7072 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007073 .enable_nmi_window = enable_nmi_window,
7074 .enable_irq_window = enable_irq_window,
7075 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08007076 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007077 .get_enable_apicv = svm_get_enable_apicv,
7078 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007079 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007080 .hwapic_irr_update = svm_hwapic_irr_update,
7081 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02007082 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05007083 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02007084
7085 .set_tss_addr = svm_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007086 .set_identity_map_addr = svm_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08007087 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007088 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007089
Avi Kivity586f9602010-11-18 13:09:54 +02007090 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007091
Sheng Yang17cc3932010-01-05 19:02:27 +08007092 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08007093
7094 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007095
7096 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00007097 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01007098 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08007099 .xsaves_supported = svm_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +02007100 .umip_emulated = svm_umip_emulated,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007101
7102 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007103
7104 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007105
7106 .write_tsc_offset = svm_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007107
7108 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007109
7110 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08007111 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007112
7113 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02007114
7115 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05007116 .deliver_posted_interrupt = svm_deliver_avic_intr,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05007117 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02007118 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007119
Ladi Prosek72d7b372017-10-11 16:54:41 +02007120 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007121 .pre_enter_smm = svm_pre_enter_smm,
7122 .pre_leave_smm = svm_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007123 .enable_smi_window = enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06007124
7125 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007126 .mem_enc_reg_region = svm_register_enc_region,
7127 .mem_enc_unreg_region = svm_unregister_enc_region,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007128};
7129
7130static int __init svm_init(void)
7131{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007132 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03007133 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007134}
7135
7136static void __exit svm_exit(void)
7137{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007138 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08007139}
7140
7141module_init(svm_init)
7142module_exit(svm_exit)