blob: e77a536d0b7cdcf33b548d23a6574edef2d9d9b8 [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
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001427static u64 svm_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
1428{
1429 struct vcpu_svm *svm = to_svm(vcpu);
1430
1431 if (is_guest_mode(vcpu))
1432 return svm->nested.hsave->control.tsc_offset;
1433
1434 return vcpu->arch.tsc_offset;
1435}
1436
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001437static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1438{
1439 struct vcpu_svm *svm = to_svm(vcpu);
1440 u64 g_tsc_offset = 0;
1441
Joerg Roedel20307532010-11-29 17:51:48 +01001442 if (is_guest_mode(vcpu)) {
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001443 /* Write L1's TSC offset. */
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001444 g_tsc_offset = svm->vmcb->control.tsc_offset -
1445 svm->nested.hsave->control.tsc_offset;
1446 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001447 } else
1448 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1449 svm->vmcb->control.tsc_offset,
1450 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001451
1452 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001453
1454 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001455}
1456
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001457static void avic_init_vmcb(struct vcpu_svm *svm)
1458{
1459 struct vmcb *vmcb = svm->vmcb;
Sean Christopherson81811c12018-03-20 12:17:21 -07001460 struct kvm_svm *kvm_svm = to_kvm_svm(svm->vcpu.kvm);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001461 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
Sean Christopherson81811c12018-03-20 12:17:21 -07001462 phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page));
1463 phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page));
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001464
1465 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1466 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1467 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1468 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1469 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001470}
1471
Paolo Bonzini56908912015-10-19 11:30:19 +02001472static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473{
Joerg Roedele6101a92008-02-13 18:58:45 +01001474 struct vmcb_control_area *control = &svm->vmcb->control;
1475 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001477 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001478
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001479 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1480 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1481 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1482 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1483 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1484 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001485 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1486 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487
Paolo Bonzini5315c712014-03-03 13:08:29 +01001488 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489
Joerg Roedel18c918c2010-11-30 18:03:59 +01001490 set_exception_intercept(svm, PF_VECTOR);
1491 set_exception_intercept(svm, UD_VECTOR);
1492 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001493 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001494 set_exception_intercept(svm, DB_VECTOR);
Liran Alon97184202018-03-12 13:12:52 +02001495 /*
1496 * Guest access to VMware backdoor ports could legitimately
1497 * trigger #GP because of TSS I/O permission bitmap.
1498 * We intercept those #GP and allow access to them anyway
1499 * as VMware does.
1500 */
1501 if (enable_vmware_backdoor)
1502 set_exception_intercept(svm, GP_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001504 set_intercept(svm, INTERCEPT_INTR);
1505 set_intercept(svm, INTERCEPT_NMI);
1506 set_intercept(svm, INTERCEPT_SMI);
1507 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001508 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001509 set_intercept(svm, INTERCEPT_CPUID);
1510 set_intercept(svm, INTERCEPT_INVD);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001511 set_intercept(svm, INTERCEPT_INVLPG);
1512 set_intercept(svm, INTERCEPT_INVLPGA);
1513 set_intercept(svm, INTERCEPT_IOIO_PROT);
1514 set_intercept(svm, INTERCEPT_MSR_PROT);
1515 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1516 set_intercept(svm, INTERCEPT_SHUTDOWN);
1517 set_intercept(svm, INTERCEPT_VMRUN);
1518 set_intercept(svm, INTERCEPT_VMMCALL);
1519 set_intercept(svm, INTERCEPT_VMLOAD);
1520 set_intercept(svm, INTERCEPT_VMSAVE);
1521 set_intercept(svm, INTERCEPT_STGI);
1522 set_intercept(svm, INTERCEPT_CLGI);
1523 set_intercept(svm, INTERCEPT_SKINIT);
1524 set_intercept(svm, INTERCEPT_WBINVD);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001525 set_intercept(svm, INTERCEPT_XSETBV);
Brijesh Singh7607b712018-02-19 10:14:44 -06001526 set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527
Wanpeng Li4d5422c2018-03-12 04:53:02 -07001528 if (!kvm_mwait_in_guest(svm->vcpu.kvm)) {
Michael S. Tsirkin668fffa32017-04-21 12:27:17 +02001529 set_intercept(svm, INTERCEPT_MONITOR);
1530 set_intercept(svm, INTERCEPT_MWAIT);
1531 }
1532
Wanpeng Licaa057a2018-03-12 04:53:03 -07001533 if (!kvm_hlt_in_guest(svm->vcpu.kvm))
1534 set_intercept(svm, INTERCEPT_HLT);
1535
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001536 control->iopm_base_pa = __sme_set(iopm_base);
1537 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538 control->int_ctl = V_INTR_MASKING_MASK;
1539
1540 init_seg(&save->es);
1541 init_seg(&save->ss);
1542 init_seg(&save->ds);
1543 init_seg(&save->fs);
1544 init_seg(&save->gs);
1545
1546 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001547 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548 /* Executable/Readable Code Segment */
1549 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1550 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1551 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001552
1553 save->gdtr.limit = 0xffff;
1554 save->idtr.limit = 0xffff;
1555
1556 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1557 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1558
Paolo Bonzini56908912015-10-19 11:30:19 +02001559 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001560 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001561 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001562 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001563 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001564
Joerg Roedele0231712010-02-24 18:59:10 +01001565 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001566 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001567 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001569 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001570 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001571
Rusty Russell66aee912007-07-17 23:34:16 +10001572 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001574
1575 if (npt_enabled) {
1576 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001577 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001578 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001579 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001580 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1581 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001582 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001583 save->cr3 = 0;
1584 save->cr4 = 0;
1585 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001586 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001587
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001588 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001589 svm->vcpu.arch.hflags = 0;
1590
Babu Moger8566ac82018-03-16 16:37:26 -04001591 if (pause_filter_count) {
1592 control->pause_filter_count = pause_filter_count;
1593 if (pause_filter_thresh)
1594 control->pause_filter_thresh = pause_filter_thresh;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001595 set_intercept(svm, INTERCEPT_PAUSE);
Babu Moger8566ac82018-03-16 16:37:26 -04001596 } else {
1597 clr_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001598 }
1599
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001600 if (kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001601 avic_init_vmcb(svm);
1602
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001603 /*
1604 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1605 * in VMCB and clear intercepts to avoid #VMEXIT.
1606 */
1607 if (vls) {
1608 clr_intercept(svm, INTERCEPT_VMLOAD);
1609 clr_intercept(svm, INTERCEPT_VMSAVE);
1610 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1611 }
1612
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001613 if (vgif) {
1614 clr_intercept(svm, INTERCEPT_STGI);
1615 clr_intercept(svm, INTERCEPT_CLGI);
1616 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1617 }
1618
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001619 if (sev_guest(svm->vcpu.kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001620 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001621 clr_exception_intercept(svm, UD_VECTOR);
1622 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001623
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001624 mark_all_dirty(svm->vmcb);
1625
Joerg Roedel2af91942009-08-07 11:49:28 +02001626 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001627
1628}
1629
Dan Carpenterd3e7dec2017-05-18 10:38:53 +03001630static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1631 unsigned int index)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001632{
1633 u64 *avic_physical_id_table;
Sean Christopherson81811c12018-03-20 12:17:21 -07001634 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001635
1636 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1637 return NULL;
1638
Sean Christopherson81811c12018-03-20 12:17:21 -07001639 avic_physical_id_table = page_address(kvm_svm->avic_physical_id_table_page);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001640
1641 return &avic_physical_id_table[index];
1642}
1643
1644/**
1645 * Note:
1646 * AVIC hardware walks the nested page table to check permissions,
1647 * but does not use the SPA address specified in the leaf page
1648 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1649 * field of the VMCB. Therefore, we set up the
1650 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1651 */
1652static int avic_init_access_page(struct kvm_vcpu *vcpu)
1653{
1654 struct kvm *kvm = vcpu->kvm;
1655 int ret;
1656
1657 if (kvm->arch.apic_access_page_done)
1658 return 0;
1659
1660 ret = x86_set_memory_region(kvm,
1661 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1662 APIC_DEFAULT_PHYS_BASE,
1663 PAGE_SIZE);
1664 if (ret)
1665 return ret;
1666
1667 kvm->arch.apic_access_page_done = true;
1668 return 0;
1669}
1670
1671static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1672{
1673 int ret;
1674 u64 *entry, new_entry;
1675 int id = vcpu->vcpu_id;
1676 struct vcpu_svm *svm = to_svm(vcpu);
1677
1678 ret = avic_init_access_page(vcpu);
1679 if (ret)
1680 return ret;
1681
1682 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1683 return -EINVAL;
1684
1685 if (!svm->vcpu.arch.apic->regs)
1686 return -EINVAL;
1687
1688 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1689
1690 /* Setting AVIC backing page address in the phy APIC ID table */
1691 entry = avic_get_physical_id_entry(vcpu, id);
1692 if (!entry)
1693 return -EINVAL;
1694
1695 new_entry = READ_ONCE(*entry);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001696 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
1697 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1698 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001699 WRITE_ONCE(*entry, new_entry);
1700
1701 svm->avic_physical_id_cache = entry;
1702
1703 return 0;
1704}
1705
Brijesh Singh1654efc2017-12-04 10:57:34 -06001706static void __sev_asid_free(int asid)
1707{
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001708 struct svm_cpu_data *sd;
1709 int cpu, pos;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001710
1711 pos = asid - 1;
1712 clear_bit(pos, sev_asid_bitmap);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001713
1714 for_each_possible_cpu(cpu) {
1715 sd = per_cpu(svm_data, cpu);
1716 sd->sev_vmcbs[pos] = NULL;
1717 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001718}
1719
1720static void sev_asid_free(struct kvm *kvm)
1721{
Sean Christopherson81811c12018-03-20 12:17:21 -07001722 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001723
1724 __sev_asid_free(sev->asid);
1725}
1726
Brijesh Singh59414c92017-12-04 10:57:35 -06001727static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
1728{
1729 struct sev_data_decommission *decommission;
1730 struct sev_data_deactivate *data;
1731
1732 if (!handle)
1733 return;
1734
1735 data = kzalloc(sizeof(*data), GFP_KERNEL);
1736 if (!data)
1737 return;
1738
1739 /* deactivate handle */
1740 data->handle = handle;
1741 sev_guest_deactivate(data, NULL);
1742
1743 wbinvd_on_all_cpus();
1744 sev_guest_df_flush(NULL);
1745 kfree(data);
1746
1747 decommission = kzalloc(sizeof(*decommission), GFP_KERNEL);
1748 if (!decommission)
1749 return;
1750
1751 /* decommission handle */
1752 decommission->handle = handle;
1753 sev_guest_decommission(decommission, NULL);
1754
1755 kfree(decommission);
1756}
1757
Brijesh Singh89c50582017-12-04 10:57:35 -06001758static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
1759 unsigned long ulen, unsigned long *n,
1760 int write)
1761{
Sean Christopherson81811c12018-03-20 12:17:21 -07001762 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06001763 unsigned long npages, npinned, size;
1764 unsigned long locked, lock_limit;
1765 struct page **pages;
1766 int first, last;
1767
1768 /* Calculate number of pages. */
1769 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
1770 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
1771 npages = (last - first + 1);
1772
1773 locked = sev->pages_locked + npages;
1774 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1775 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
1776 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
1777 return NULL;
1778 }
1779
1780 /* Avoid using vmalloc for smaller buffers. */
1781 size = npages * sizeof(struct page *);
1782 if (size > PAGE_SIZE)
1783 pages = vmalloc(size);
1784 else
1785 pages = kmalloc(size, GFP_KERNEL);
1786
1787 if (!pages)
1788 return NULL;
1789
1790 /* Pin the user virtual address. */
1791 npinned = get_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
1792 if (npinned != npages) {
1793 pr_err("SEV: Failure locking %lu pages.\n", npages);
1794 goto err;
1795 }
1796
1797 *n = npages;
1798 sev->pages_locked = locked;
1799
1800 return pages;
1801
1802err:
1803 if (npinned > 0)
1804 release_pages(pages, npinned);
1805
1806 kvfree(pages);
1807 return NULL;
1808}
1809
1810static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
1811 unsigned long npages)
1812{
Sean Christopherson81811c12018-03-20 12:17:21 -07001813 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06001814
1815 release_pages(pages, npages);
1816 kvfree(pages);
1817 sev->pages_locked -= npages;
1818}
1819
1820static void sev_clflush_pages(struct page *pages[], unsigned long npages)
1821{
1822 uint8_t *page_virtual;
1823 unsigned long i;
1824
1825 if (npages == 0 || pages == NULL)
1826 return;
1827
1828 for (i = 0; i < npages; i++) {
1829 page_virtual = kmap_atomic(pages[i]);
1830 clflush_cache_range(page_virtual, PAGE_SIZE);
1831 kunmap_atomic(page_virtual);
1832 }
1833}
1834
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001835static void __unregister_enc_region_locked(struct kvm *kvm,
1836 struct enc_region *region)
1837{
1838 /*
1839 * The guest may change the memory encryption attribute from C=0 -> C=1
1840 * or vice versa for this memory range. Lets make sure caches are
1841 * flushed to ensure that guest data gets written into memory with
1842 * correct C-bit.
1843 */
1844 sev_clflush_pages(region->pages, region->npages);
1845
1846 sev_unpin_memory(kvm, region->pages, region->npages);
1847 list_del(&region->list);
1848 kfree(region);
1849}
1850
Sean Christopherson434a1e92018-03-20 12:17:18 -07001851static struct kvm *svm_vm_alloc(void)
1852{
Sean Christopherson81811c12018-03-20 12:17:21 -07001853 struct kvm_svm *kvm_svm = kzalloc(sizeof(struct kvm_svm), GFP_KERNEL);
1854 return &kvm_svm->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07001855}
1856
1857static void svm_vm_free(struct kvm *kvm)
1858{
Sean Christopherson81811c12018-03-20 12:17:21 -07001859 kfree(to_kvm_svm(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07001860}
1861
Brijesh Singh1654efc2017-12-04 10:57:34 -06001862static void sev_vm_destroy(struct kvm *kvm)
1863{
Sean Christopherson81811c12018-03-20 12:17:21 -07001864 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001865 struct list_head *head = &sev->regions_list;
1866 struct list_head *pos, *q;
Brijesh Singh59414c92017-12-04 10:57:35 -06001867
Brijesh Singh1654efc2017-12-04 10:57:34 -06001868 if (!sev_guest(kvm))
1869 return;
1870
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001871 mutex_lock(&kvm->lock);
1872
1873 /*
1874 * if userspace was terminated before unregistering the memory regions
1875 * then lets unpin all the registered memory.
1876 */
1877 if (!list_empty(head)) {
1878 list_for_each_safe(pos, q, head) {
1879 __unregister_enc_region_locked(kvm,
1880 list_entry(pos, struct enc_region, list));
1881 }
1882 }
1883
1884 mutex_unlock(&kvm->lock);
1885
Brijesh Singh59414c92017-12-04 10:57:35 -06001886 sev_unbind_asid(kvm, sev->handle);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001887 sev_asid_free(kvm);
1888}
1889
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001890static void avic_vm_destroy(struct kvm *kvm)
1891{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001892 unsigned long flags;
Sean Christopherson81811c12018-03-20 12:17:21 -07001893 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001894
Dmitry Vyukov3863dff2017-01-24 14:06:48 +01001895 if (!avic)
1896 return;
1897
Sean Christopherson81811c12018-03-20 12:17:21 -07001898 if (kvm_svm->avic_logical_id_table_page)
1899 __free_page(kvm_svm->avic_logical_id_table_page);
1900 if (kvm_svm->avic_physical_id_table_page)
1901 __free_page(kvm_svm->avic_physical_id_table_page);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001902
1903 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Sean Christopherson81811c12018-03-20 12:17:21 -07001904 hash_del(&kvm_svm->hnode);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001905 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001906}
1907
Brijesh Singh1654efc2017-12-04 10:57:34 -06001908static void svm_vm_destroy(struct kvm *kvm)
1909{
1910 avic_vm_destroy(kvm);
1911 sev_vm_destroy(kvm);
1912}
1913
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001914static int avic_vm_init(struct kvm *kvm)
1915{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001916 unsigned long flags;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001917 int err = -ENOMEM;
Sean Christopherson81811c12018-03-20 12:17:21 -07001918 struct kvm_svm *kvm_svm = to_kvm_svm(kvm);
1919 struct kvm_svm *k2;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001920 struct page *p_page;
1921 struct page *l_page;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001922 u32 vm_id;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001923
1924 if (!avic)
1925 return 0;
1926
1927 /* Allocating physical APIC ID table (4KB) */
1928 p_page = alloc_page(GFP_KERNEL);
1929 if (!p_page)
1930 goto free_avic;
1931
Sean Christopherson81811c12018-03-20 12:17:21 -07001932 kvm_svm->avic_physical_id_table_page = p_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001933 clear_page(page_address(p_page));
1934
1935 /* Allocating logical APIC ID table (4KB) */
1936 l_page = alloc_page(GFP_KERNEL);
1937 if (!l_page)
1938 goto free_avic;
1939
Sean Christopherson81811c12018-03-20 12:17:21 -07001940 kvm_svm->avic_logical_id_table_page = l_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001941 clear_page(page_address(l_page));
1942
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001943 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001944 again:
1945 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
1946 if (vm_id == 0) { /* id is 1-based, zero is not okay */
1947 next_vm_id_wrapped = 1;
1948 goto again;
1949 }
1950 /* Is it still in use? Only possible if wrapped at least once */
1951 if (next_vm_id_wrapped) {
Sean Christopherson81811c12018-03-20 12:17:21 -07001952 hash_for_each_possible(svm_vm_data_hash, k2, hnode, vm_id) {
1953 if (k2->avic_vm_id == vm_id)
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001954 goto again;
1955 }
1956 }
Sean Christopherson81811c12018-03-20 12:17:21 -07001957 kvm_svm->avic_vm_id = vm_id;
1958 hash_add(svm_vm_data_hash, &kvm_svm->hnode, kvm_svm->avic_vm_id);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001959 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1960
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001961 return 0;
1962
1963free_avic:
1964 avic_vm_destroy(kvm);
1965 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966}
1967
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001968static inline int
1969avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001970{
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001971 int ret = 0;
1972 unsigned long flags;
1973 struct amd_svm_iommu_ir *ir;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001974 struct vcpu_svm *svm = to_svm(vcpu);
1975
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001976 if (!kvm_arch_has_assigned_device(vcpu->kvm))
1977 return 0;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001978
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001979 /*
1980 * Here, we go through the per-vcpu ir_list to update all existing
1981 * interrupt remapping table entry targeting this vcpu.
1982 */
1983 spin_lock_irqsave(&svm->ir_list_lock, flags);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001984
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001985 if (list_empty(&svm->ir_list))
1986 goto out;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001987
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001988 list_for_each_entry(ir, &svm->ir_list, node) {
1989 ret = amd_iommu_update_ga(cpu, r, ir->data);
1990 if (ret)
1991 break;
1992 }
1993out:
1994 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
1995 return ret;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001996}
1997
1998static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1999{
2000 u64 entry;
2001 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05002002 int h_physical_id = kvm_cpu_get_apicid(cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002003 struct vcpu_svm *svm = to_svm(vcpu);
2004
2005 if (!kvm_vcpu_apicv_active(vcpu))
2006 return;
2007
2008 if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
2009 return;
2010
2011 entry = READ_ONCE(*(svm->avic_physical_id_cache));
2012 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
2013
2014 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
2015 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
2016
2017 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2018 if (svm->avic_is_running)
2019 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2020
2021 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002022 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
2023 svm->avic_is_running);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002024}
2025
2026static void avic_vcpu_put(struct kvm_vcpu *vcpu)
2027{
2028 u64 entry;
2029 struct vcpu_svm *svm = to_svm(vcpu);
2030
2031 if (!kvm_vcpu_apicv_active(vcpu))
2032 return;
2033
2034 entry = READ_ONCE(*(svm->avic_physical_id_cache));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002035 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
2036 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
2037
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002038 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
2039 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040}
2041
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05002042/**
2043 * This function is called during VCPU halt/unhalt.
2044 */
2045static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
2046{
2047 struct vcpu_svm *svm = to_svm(vcpu);
2048
2049 svm->avic_is_running = is_run;
2050 if (is_run)
2051 avic_vcpu_load(vcpu, vcpu->cpu);
2052 else
2053 avic_vcpu_put(vcpu);
2054}
2055
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002056static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03002057{
2058 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01002059 u32 dummy;
2060 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002061
Wanpeng Li518e7b92018-02-28 14:03:31 +08002062 vcpu->arch.microcode_version = 0x01000065;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002063 svm->spec_ctrl = 0;
2064
Nadav Amitd28bc9d2015-04-13 14:34:08 +03002065 if (!init_event) {
2066 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
2067 MSR_IA32_APICBASE_ENABLE;
2068 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
2069 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
2070 }
Paolo Bonzini56908912015-10-19 11:30:19 +02002071 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02002072
Yu Zhange911eb32017-08-24 20:27:52 +08002073 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
Julian Stecklina66f7b722012-12-05 15:26:19 +01002074 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002075
2076 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
2077 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03002078}
2079
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002080static int avic_init_vcpu(struct vcpu_svm *svm)
2081{
2082 int ret;
2083
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05002084 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002085 return 0;
2086
2087 ret = avic_init_backing_page(&svm->vcpu);
2088 if (ret)
2089 return ret;
2090
2091 INIT_LIST_HEAD(&svm->ir_list);
2092 spin_lock_init(&svm->ir_list_lock);
2093
2094 return ret;
2095}
2096
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002097static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002099 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01002101 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002102 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002103 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002104 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105
Rusty Russellc16f8622007-07-30 21:12:19 +10002106 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002107 if (!svm) {
2108 err = -ENOMEM;
2109 goto out;
2110 }
2111
2112 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
2113 if (err)
2114 goto free_svm;
2115
Joerg Roedelf65c2292008-02-13 18:58:46 +01002116 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002117 page = alloc_page(GFP_KERNEL);
2118 if (!page)
2119 goto uninit;
2120
Joerg Roedelf65c2292008-02-13 18:58:46 +01002121 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
2122 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002123 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002124
2125 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
2126 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002127 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01002128
Alexander Grafb286d5d2008-11-25 20:17:05 +01002129 hsave_page = alloc_page(GFP_KERNEL);
2130 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002131 goto free_page3;
2132
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05002133 err = avic_init_vcpu(svm);
2134 if (err)
2135 goto free_page4;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002136
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002137 /* We initialize this flag to true to make sure that the is_running
2138 * bit would be set the first time the vcpu is loaded.
2139 */
2140 svm->avic_is_running = true;
2141
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002142 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01002143
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002144 svm->msrpm = page_address(msrpm_pages);
2145 svm_vcpu_init_msrpm(svm->msrpm);
2146
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002147 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002148 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002149
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002150 svm->vmcb = page_address(page);
2151 clear_page(svm->vmcb);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002152 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002153 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02002154 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002156 svm_init_osvw(&svm->vcpu);
2157
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002158 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08002159
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002160free_page4:
2161 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002162free_page3:
2163 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
2164free_page2:
2165 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
2166free_page1:
2167 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002168uninit:
2169 kvm_vcpu_uninit(&svm->vcpu);
2170free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10002171 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002172out:
2173 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002174}
2175
2176static void svm_free_vcpu(struct kvm_vcpu *vcpu)
2177{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002178 struct vcpu_svm *svm = to_svm(vcpu);
2179
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002180 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01002181 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002182 __free_page(virt_to_page(svm->nested.hsave));
2183 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002184 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002185 kmem_cache_free(kvm_vcpu_cache, svm);
Ashok Raj15d45072018-02-01 22:59:43 +01002186 /*
2187 * The vmcb page can be recycled, causing a false negative in
2188 * svm_vcpu_load(). So do a full IBPB now.
2189 */
2190 indirect_branch_prediction_barrier();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191}
2192
Avi Kivity15ad7142007-07-11 18:17:21 +03002193static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002195 struct vcpu_svm *svm = to_svm(vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01002196 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002197 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02002198
Avi Kivity0cc50642007-03-25 12:07:27 +02002199 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002200 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002201 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02002202 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002203
Avi Kivity82ca2d12010-10-21 12:20:34 +02002204#ifdef CONFIG_X86_64
2205 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
2206#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02002207 savesegment(fs, svm->host.fs);
2208 savesegment(gs, svm->host.gs);
2209 svm->host.ldt = kvm_read_ldt();
2210
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002211 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002212 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002213
Haozhong Zhangad7218832015-10-20 15:39:02 +08002214 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
2215 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2216 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
2217 __this_cpu_write(current_tsc_ratio, tsc_ratio);
2218 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
2219 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002220 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01002221 /* This assumes that the kernel never uses MSR_TSC_AUX */
2222 if (static_cpu_has(X86_FEATURE_RDTSCP))
2223 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002224
Ashok Raj15d45072018-02-01 22:59:43 +01002225 if (sd->current_vmcb != svm->vmcb) {
2226 sd->current_vmcb = svm->vmcb;
2227 indirect_branch_prediction_barrier();
2228 }
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002229 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230}
2231
2232static void svm_vcpu_put(struct kvm_vcpu *vcpu)
2233{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002234 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002235 int i;
2236
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002237 avic_vcpu_put(vcpu);
2238
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002239 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02002240 kvm_load_ldt(svm->host.ldt);
2241#ifdef CONFIG_X86_64
2242 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07002243 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01002244 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02002245#else
Avi Kivity831ca602011-03-08 16:09:51 +02002246#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02002247 loadsegment(gs, svm->host.gs);
2248#endif
Avi Kivity831ca602011-03-08 16:09:51 +02002249#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002250 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002251 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252}
2253
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002254static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
2255{
2256 avic_set_running(vcpu, false);
2257}
2258
2259static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
2260{
2261 avic_set_running(vcpu, true);
2262}
2263
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
2265{
Ladi Prosek9b611742017-06-21 09:06:59 +02002266 struct vcpu_svm *svm = to_svm(vcpu);
2267 unsigned long rflags = svm->vmcb->save.rflags;
2268
2269 if (svm->nmi_singlestep) {
2270 /* Hide our flags if they were not set by the guest */
2271 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
2272 rflags &= ~X86_EFLAGS_TF;
2273 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
2274 rflags &= ~X86_EFLAGS_RF;
2275 }
2276 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277}
2278
2279static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2280{
Ladi Prosek9b611742017-06-21 09:06:59 +02002281 if (to_svm(vcpu)->nmi_singlestep)
2282 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2283
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002284 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02002285 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002286 * (caused by either a task switch or an inter-privilege IRET),
2287 * so we do not need to update the CPL here.
2288 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002289 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290}
2291
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002292static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
2293{
2294 switch (reg) {
2295 case VCPU_EXREG_PDPTR:
2296 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002297 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002298 break;
2299 default:
2300 BUG();
2301 }
2302}
2303
Alexander Graff0b85052008-11-25 20:17:01 +01002304static void svm_set_vintr(struct vcpu_svm *svm)
2305{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002306 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002307}
2308
2309static void svm_clear_vintr(struct vcpu_svm *svm)
2310{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002311 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002312}
2313
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
2315{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002316 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317
2318 switch (seg) {
2319 case VCPU_SREG_CS: return &save->cs;
2320 case VCPU_SREG_DS: return &save->ds;
2321 case VCPU_SREG_ES: return &save->es;
2322 case VCPU_SREG_FS: return &save->fs;
2323 case VCPU_SREG_GS: return &save->gs;
2324 case VCPU_SREG_SS: return &save->ss;
2325 case VCPU_SREG_TR: return &save->tr;
2326 case VCPU_SREG_LDTR: return &save->ldtr;
2327 }
2328 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00002329 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330}
2331
2332static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2333{
2334 struct vmcb_seg *s = svm_seg(vcpu, seg);
2335
2336 return s->base;
2337}
2338
2339static void svm_get_segment(struct kvm_vcpu *vcpu,
2340 struct kvm_segment *var, int seg)
2341{
2342 struct vmcb_seg *s = svm_seg(vcpu, seg);
2343
2344 var->base = s->base;
2345 var->limit = s->limit;
2346 var->selector = s->selector;
2347 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
2348 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
2349 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
2350 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
2351 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
2352 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
2353 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05302354
2355 /*
2356 * AMD CPUs circa 2014 track the G bit for all segments except CS.
2357 * However, the SVM spec states that the G bit is not observed by the
2358 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
2359 * So let's synthesize a legal G bit for all segments, this helps
2360 * running KVM nested. It also helps cross-vendor migration, because
2361 * Intel's vmentry has a check on the 'G' bit.
2362 */
2363 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00002364
Joerg Roedele0231712010-02-24 18:59:10 +01002365 /*
2366 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02002367 * for cross vendor migration purposes by "not present"
2368 */
Gioh Kim8eae9572017-05-30 15:24:45 +02002369 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02002370
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002371 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002372 case VCPU_SREG_TR:
2373 /*
2374 * Work around a bug where the busy flag in the tr selector
2375 * isn't exposed
2376 */
Amit Shahc0d09822008-10-27 09:04:18 +00002377 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002378 break;
2379 case VCPU_SREG_DS:
2380 case VCPU_SREG_ES:
2381 case VCPU_SREG_FS:
2382 case VCPU_SREG_GS:
2383 /*
2384 * The accessed bit must always be set in the segment
2385 * descriptor cache, although it can be cleared in the
2386 * descriptor, the cached bit always remains at 1. Since
2387 * Intel has a check on this, set it here to support
2388 * cross-vendor migration.
2389 */
2390 if (!var->unusable)
2391 var->type |= 0x1;
2392 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02002393 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01002394 /*
2395 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02002396 * descriptor is left as 1, although the whole segment has
2397 * been made unusable. Clear it here to pass an Intel VMX
2398 * entry check when cross vendor migrating.
2399 */
2400 if (var->unusable)
2401 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02002402 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02002403 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02002404 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002405 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406}
2407
Izik Eidus2e4d2652008-03-24 19:38:34 +02002408static int svm_get_cpl(struct kvm_vcpu *vcpu)
2409{
2410 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
2411
2412 return save->cpl;
2413}
2414
Gleb Natapov89a27f42010-02-16 10:51:48 +02002415static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002417 struct vcpu_svm *svm = to_svm(vcpu);
2418
Gleb Natapov89a27f42010-02-16 10:51:48 +02002419 dt->size = svm->vmcb->save.idtr.limit;
2420 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421}
2422
Gleb Natapov89a27f42010-02-16 10:51:48 +02002423static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002425 struct vcpu_svm *svm = to_svm(vcpu);
2426
Gleb Natapov89a27f42010-02-16 10:51:48 +02002427 svm->vmcb->save.idtr.limit = dt->size;
2428 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002429 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430}
2431
Gleb Natapov89a27f42010-02-16 10:51:48 +02002432static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002434 struct vcpu_svm *svm = to_svm(vcpu);
2435
Gleb Natapov89a27f42010-02-16 10:51:48 +02002436 dt->size = svm->vmcb->save.gdtr.limit;
2437 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438}
2439
Gleb Natapov89a27f42010-02-16 10:51:48 +02002440static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002442 struct vcpu_svm *svm = to_svm(vcpu);
2443
Gleb Natapov89a27f42010-02-16 10:51:48 +02002444 svm->vmcb->save.gdtr.limit = dt->size;
2445 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002446 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447}
2448
Avi Kivitye8467fd2009-12-29 18:43:06 +02002449static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2450{
2451}
2452
Avi Kivityaff48ba2010-12-05 18:56:11 +02002453static void svm_decache_cr3(struct kvm_vcpu *vcpu)
2454{
2455}
2456
Anthony Liguori25c4c272007-04-27 09:29:21 +03002457static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002458{
2459}
2460
Avi Kivityd2251572010-01-06 10:55:27 +02002461static void update_cr0_intercept(struct vcpu_svm *svm)
2462{
2463 ulong gcr0 = svm->vcpu.arch.cr0;
2464 u64 *hcr0 = &svm->vmcb->save.cr0;
2465
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002466 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
2467 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
Avi Kivityd2251572010-01-06 10:55:27 +02002468
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002469 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002470
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002471 if (gcr0 == *hcr0) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002472 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
2473 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002474 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002475 set_cr_intercept(svm, INTERCEPT_CR0_READ);
2476 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002477 }
2478}
2479
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2481{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002482 struct vcpu_svm *svm = to_svm(vcpu);
2483
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002484#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002485 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002486 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002487 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002488 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002489 }
2490
Mike Dayd77c26f2007-10-08 09:02:08 -04002491 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002492 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002493 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494 }
2495 }
2496#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002497 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002498
2499 if (!npt_enabled)
2500 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02002501
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02002502 /*
2503 * re-enable caching here because the QEMU bios
2504 * does not do it - this results in some delay at
2505 * reboot
2506 */
2507 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
2508 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002509 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002510 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002511 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512}
2513
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002514static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002516 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002517 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
2518
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002519 if (cr4 & X86_CR4_VMXE)
2520 return 1;
2521
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002522 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002523 svm_flush_tlb(vcpu, true);
Joerg Roedel6394b642008-04-09 14:15:29 +02002524
Joerg Roedelec077262008-04-09 14:15:28 +02002525 vcpu->arch.cr4 = cr4;
2526 if (!npt_enabled)
2527 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02002528 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02002529 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002530 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002531 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532}
2533
2534static void svm_set_segment(struct kvm_vcpu *vcpu,
2535 struct kvm_segment *var, int seg)
2536{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002537 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538 struct vmcb_seg *s = svm_seg(vcpu, seg);
2539
2540 s->base = var->base;
2541 s->limit = var->limit;
2542 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02002543 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2544 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2545 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2546 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2547 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2548 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2549 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2550 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002551
2552 /*
2553 * This is always accurate, except if SYSRET returned to a segment
2554 * with SS.DPL != 3. Intel does not have this quirk, and always
2555 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2556 * would entail passing the CPL to userspace and back.
2557 */
2558 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02002559 /* This is symmetric with svm_get_segment() */
2560 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561
Joerg Roedel060d0c92010-12-03 11:45:57 +01002562 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002563}
2564
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01002565static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002567 struct vcpu_svm *svm = to_svm(vcpu);
2568
Joerg Roedel18c918c2010-11-30 18:03:59 +01002569 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03002570
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002571 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002572 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01002573 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002574 } else
2575 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03002576}
2577
Tejun Heo0fe1e002009-10-29 22:34:14 +09002578static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002579{
Tejun Heo0fe1e002009-10-29 22:34:14 +09002580 if (sd->next_asid > sd->max_asid) {
2581 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06002582 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002583 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584 }
2585
Tejun Heo0fe1e002009-10-29 22:34:14 +09002586 svm->asid_generation = sd->asid_generation;
2587 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01002588
2589 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590}
2591
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002592static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2593{
2594 return to_svm(vcpu)->vmcb->save.dr6;
2595}
2596
2597static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2598{
2599 struct vcpu_svm *svm = to_svm(vcpu);
2600
2601 svm->vmcb->save.dr6 = value;
2602 mark_dirty(svm->vmcb, VMCB_DR);
2603}
2604
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002605static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2606{
2607 struct vcpu_svm *svm = to_svm(vcpu);
2608
2609 get_debugreg(vcpu->arch.db[0], 0);
2610 get_debugreg(vcpu->arch.db[1], 1);
2611 get_debugreg(vcpu->arch.db[2], 2);
2612 get_debugreg(vcpu->arch.db[3], 3);
2613 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2614 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2615
2616 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2617 set_dr_intercepts(svm);
2618}
2619
Gleb Natapov020df072010-04-13 10:05:23 +03002620static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002622 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002623
Gleb Natapov020df072010-04-13 10:05:23 +03002624 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01002625 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626}
2627
Avi Kivity851ba692009-08-24 11:10:17 +03002628static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002629{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002630 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002631 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002633 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002634 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2635 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002636 svm->vmcb->control.insn_len);
2637}
2638
2639static int npf_interception(struct vcpu_svm *svm)
2640{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002641 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Paolo Bonzinid0006532017-08-11 18:36:43 +02002642 u64 error_code = svm->vmcb->control.exit_info_1;
2643
2644 trace_kvm_page_fault(fault_address, error_code);
2645 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002646 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2647 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002648 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002649}
2650
Avi Kivity851ba692009-08-24 11:10:17 +03002651static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002652{
Avi Kivity851ba692009-08-24 11:10:17 +03002653 struct kvm_run *kvm_run = svm->vcpu.run;
2654
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002655 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03002656 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02002657 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002658 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2659 return 1;
2660 }
Gleb Natapov44c11432009-05-11 13:35:52 +03002661
Jan Kiszka6be7d302009-10-18 13:24:54 +02002662 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02002663 disable_nmi_singlestep(svm);
Gleb Natapov44c11432009-05-11 13:35:52 +03002664 }
2665
2666 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002667 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002668 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2669 kvm_run->debug.arch.pc =
2670 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2671 kvm_run->debug.arch.exception = DB_VECTOR;
2672 return 0;
2673 }
2674
2675 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002676}
2677
Avi Kivity851ba692009-08-24 11:10:17 +03002678static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002679{
Avi Kivity851ba692009-08-24 11:10:17 +03002680 struct kvm_run *kvm_run = svm->vcpu.run;
2681
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002682 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2683 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2684 kvm_run->debug.arch.exception = BP_VECTOR;
2685 return 0;
2686}
2687
Avi Kivity851ba692009-08-24 11:10:17 +03002688static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002689{
Wanpeng Li082d06e2018-04-03 16:28:48 -07002690 return handle_ud(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002691}
2692
Eric Northup54a20552015-11-03 18:03:53 +01002693static int ac_interception(struct vcpu_svm *svm)
2694{
2695 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2696 return 1;
2697}
2698
Liran Alon97184202018-03-12 13:12:52 +02002699static int gp_interception(struct vcpu_svm *svm)
2700{
2701 struct kvm_vcpu *vcpu = &svm->vcpu;
2702 u32 error_code = svm->vmcb->control.exit_info_1;
2703 int er;
2704
2705 WARN_ON_ONCE(!enable_vmware_backdoor);
2706
2707 er = emulate_instruction(vcpu,
2708 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
2709 if (er == EMULATE_USER_EXIT)
2710 return 0;
2711 else if (er != EMULATE_DONE)
2712 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2713 return 1;
2714}
2715
Joerg Roedel67ec6602010-05-17 14:43:35 +02002716static bool is_erratum_383(void)
2717{
2718 int err, i;
2719 u64 value;
2720
2721 if (!erratum_383_found)
2722 return false;
2723
2724 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2725 if (err)
2726 return false;
2727
2728 /* Bit 62 may or may not be set for this mce */
2729 value &= ~(1ULL << 62);
2730
2731 if (value != 0xb600000000010015ULL)
2732 return false;
2733
2734 /* Clear MCi_STATUS registers */
2735 for (i = 0; i < 6; ++i)
2736 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2737
2738 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2739 if (!err) {
2740 u32 low, high;
2741
2742 value &= ~(1ULL << 2);
2743 low = lower_32_bits(value);
2744 high = upper_32_bits(value);
2745
2746 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2747 }
2748
2749 /* Flush tlb to evict multi-match entries */
2750 __flush_tlb_all();
2751
2752 return true;
2753}
2754
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002755static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002756{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002757 if (is_erratum_383()) {
2758 /*
2759 * Erratum 383 triggered. Guest state is corrupt so kill the
2760 * guest.
2761 */
2762 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2763
Avi Kivitya8eeb042010-05-10 12:34:53 +03002764 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002765
2766 return;
2767 }
2768
Joerg Roedel53371b52008-04-09 14:15:30 +02002769 /*
2770 * On an #MC intercept the MCE handler is not called automatically in
2771 * the host. So do it by hand here.
2772 */
2773 asm volatile (
2774 "int $0x12\n");
2775 /* not sure if we ever come back to this point */
2776
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002777 return;
2778}
2779
2780static int mc_interception(struct vcpu_svm *svm)
2781{
Joerg Roedel53371b52008-04-09 14:15:30 +02002782 return 1;
2783}
2784
Avi Kivity851ba692009-08-24 11:10:17 +03002785static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002786{
Avi Kivity851ba692009-08-24 11:10:17 +03002787 struct kvm_run *kvm_run = svm->vcpu.run;
2788
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002789 /*
2790 * VMCB is undefined after a SHUTDOWN intercept
2791 * so reinitialize it.
2792 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002793 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002794 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002795
2796 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2797 return 0;
2798}
2799
Avi Kivity851ba692009-08-24 11:10:17 +03002800static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002802 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002803 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Sean Christophersondca7f122018-03-08 08:57:27 -08002804 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002805 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806
Rusty Russelle756fc62007-07-30 20:07:08 +10002807 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002808 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002809 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002810 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01002811 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002812
Avi Kivity039576c2007-03-20 12:46:50 +02002813 port = io_info >> 16;
2814 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002815 svm->next_rip = svm->vmcb->control.exit_info_2;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002816
Sean Christophersondca7f122018-03-08 08:57:27 -08002817 return kvm_fast_pio(&svm->vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818}
2819
Avi Kivity851ba692009-08-24 11:10:17 +03002820static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002821{
2822 return 1;
2823}
2824
Avi Kivity851ba692009-08-24 11:10:17 +03002825static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002826{
2827 ++svm->vcpu.stat.irq_exits;
2828 return 1;
2829}
2830
Avi Kivity851ba692009-08-24 11:10:17 +03002831static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832{
2833 return 1;
2834}
2835
Avi Kivity851ba692009-08-24 11:10:17 +03002836static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002837{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002838 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002839 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840}
2841
Avi Kivity851ba692009-08-24 11:10:17 +03002842static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002843{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002844 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002845 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002846}
2847
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002848static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2849{
2850 struct vcpu_svm *svm = to_svm(vcpu);
2851
2852 return svm->nested.nested_cr3;
2853}
2854
Avi Kivitye4e517b2011-07-28 11:36:17 +03002855static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2856{
2857 struct vcpu_svm *svm = to_svm(vcpu);
2858 u64 cr3 = svm->nested.nested_cr3;
2859 u64 pdpte;
2860 int ret;
2861
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002862 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002863 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002864 if (ret)
2865 return 0;
2866 return pdpte;
2867}
2868
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002869static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2870 unsigned long root)
2871{
2872 struct vcpu_svm *svm = to_svm(vcpu);
2873
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002874 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01002875 mark_dirty(svm->vmcb, VMCB_NPT);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002876 svm_flush_tlb(vcpu, true);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002877}
2878
Avi Kivity6389ee92010-11-29 16:12:30 +02002879static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2880 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002881{
2882 struct vcpu_svm *svm = to_svm(vcpu);
2883
Paolo Bonzini5e352512014-09-02 13:18:37 +02002884 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2885 /*
2886 * TODO: track the cause of the nested page fault, and
2887 * correctly fill in the high bits of exit_info_1.
2888 */
2889 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2890 svm->vmcb->control.exit_code_hi = 0;
2891 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2892 svm->vmcb->control.exit_info_2 = fault->address;
2893 }
2894
2895 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2896 svm->vmcb->control.exit_info_1 |= fault->error_code;
2897
2898 /*
2899 * The present bit is always zero for page structure faults on real
2900 * hardware.
2901 */
2902 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2903 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002904
2905 nested_svm_vmexit(svm);
2906}
2907
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002908static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002909{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002910 WARN_ON(mmu_is_nested(vcpu));
2911 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002912 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2913 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002914 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002915 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
Yu Zhang855feb62017-08-24 20:27:55 +08002916 vcpu->arch.mmu.shadow_root_level = get_npt_level(vcpu);
Xiao Guangrongc258b622015-08-05 12:04:24 +08002917 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002918 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002919}
2920
2921static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2922{
2923 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2924}
2925
Alexander Grafc0725422008-11-25 20:17:03 +01002926static int nested_svm_check_permissions(struct vcpu_svm *svm)
2927{
Dan Carpentere9196ce2017-05-18 10:39:53 +03002928 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
2929 !is_paging(&svm->vcpu)) {
Alexander Grafc0725422008-11-25 20:17:03 +01002930 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2931 return 1;
2932 }
2933
2934 if (svm->vmcb->save.cpl) {
2935 kvm_inject_gp(&svm->vcpu, 0);
2936 return 1;
2937 }
2938
Dan Carpentere9196ce2017-05-18 10:39:53 +03002939 return 0;
Alexander Grafc0725422008-11-25 20:17:03 +01002940}
2941
Alexander Grafcf74a782008-11-25 20:17:08 +01002942static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2943 bool has_error_code, u32 error_code)
2944{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002945 int vmexit;
2946
Joerg Roedel20307532010-11-29 17:51:48 +01002947 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002948 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002949
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002950 vmexit = nested_svm_intercept(svm);
2951 if (vmexit != NESTED_EXIT_DONE)
2952 return 0;
2953
Joerg Roedel0295ad72009-08-07 11:49:37 +02002954 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2955 svm->vmcb->control.exit_code_hi = 0;
2956 svm->vmcb->control.exit_info_1 = error_code;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002957
2958 /*
2959 * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception.
2960 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2961 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be
2962 * written only when inject_pending_event runs (DR6 would written here
2963 * too). This should be conditional on a new capability---if the
2964 * capability is disabled, kvm_multiple_exception would write the
2965 * ancillary information to CR2 or DR6, for backwards ABI-compatibility.
2966 */
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002967 if (svm->vcpu.arch.exception.nested_apf)
2968 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
2969 else
2970 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
Joerg Roedel0295ad72009-08-07 11:49:37 +02002971
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002972 svm->nested.exit_required = true;
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002973 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002974}
2975
Joerg Roedel8fe54652010-02-19 16:23:01 +01002976/* This function returns true if it is save to enable the irq window */
2977static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002978{
Joerg Roedel20307532010-11-29 17:51:48 +01002979 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002980 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002981
Joerg Roedel26666952009-08-07 11:49:46 +02002982 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002983 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002984
Joerg Roedel26666952009-08-07 11:49:46 +02002985 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002986 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002987
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002988 /*
2989 * if vmexit was already requested (by intercepted exception
2990 * for instance) do not overwrite it with "external interrupt"
2991 * vmexit.
2992 */
2993 if (svm->nested.exit_required)
2994 return false;
2995
Joerg Roedel197717d2010-02-24 18:59:19 +01002996 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2997 svm->vmcb->control.exit_info_1 = 0;
2998 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002999
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003000 if (svm->nested.intercept & 1ULL) {
3001 /*
3002 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003003 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003004 * #vmexit emulation might sleep. Only signal request for
3005 * the #vmexit here.
3006 */
3007 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02003008 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01003009 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01003010 }
3011
Joerg Roedel8fe54652010-02-19 16:23:01 +01003012 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01003013}
3014
Joerg Roedel887f5002010-02-24 18:59:12 +01003015/* This function returns true if it is save to enable the nmi window */
3016static inline bool nested_svm_nmi(struct vcpu_svm *svm)
3017{
Joerg Roedel20307532010-11-29 17:51:48 +01003018 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01003019 return true;
3020
3021 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
3022 return true;
3023
3024 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
3025 svm->nested.exit_required = true;
3026
3027 return false;
3028}
3029
Joerg Roedel7597f122010-02-19 16:23:00 +01003030static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003031{
3032 struct page *page;
3033
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01003034 might_sleep();
3035
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003036 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003037 if (is_error_page(page))
3038 goto error;
3039
Joerg Roedel7597f122010-02-19 16:23:00 +01003040 *_page = page;
3041
3042 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003043
3044error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003045 kvm_inject_gp(&svm->vcpu, 0);
3046
3047 return NULL;
3048}
3049
Joerg Roedel7597f122010-02-19 16:23:00 +01003050static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003051{
Joerg Roedel7597f122010-02-19 16:23:00 +01003052 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003053 kvm_release_page_dirty(page);
3054}
3055
Joerg Roedelce2ac082010-03-01 15:34:39 +01003056static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003057{
Jan Kiszka9bf41832014-06-30 10:54:17 +02003058 unsigned port, size, iopm_len;
3059 u16 val, mask;
3060 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003061 u64 gpa;
3062
3063 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
3064 return NESTED_EXIT_HOST;
3065
3066 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02003067 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
3068 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003069 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02003070 start_bit = port % 8;
3071 iopm_len = (start_bit + size > 8) ? 2 : 1;
3072 mask = (0xf >> (4 - size)) << start_bit;
3073 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003074
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003075 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02003076 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003077
Jan Kiszka9bf41832014-06-30 10:54:17 +02003078 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003079}
3080
Joerg Roedeld2477822010-03-01 15:34:34 +01003081static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003082{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003083 u32 offset, msr, value;
3084 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003085
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003086 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01003087 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003088
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003089 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
3090 offset = svm_msrpm_offset(msr);
3091 write = svm->vmcb->control.exit_info_1 & 1;
3092 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003093
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003094 if (offset == MSR_INVALID)
3095 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003096
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003097 /* Offset is in 32 bit units but need in 8 bit units */
3098 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003099
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003100 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003101 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003102
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003103 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003104}
3105
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003106/* DB exceptions for our internal use must not cause vmexit */
3107static int nested_svm_intercept_db(struct vcpu_svm *svm)
3108{
3109 unsigned long dr6;
3110
3111 /* if we're not singlestepping, it's not ours */
3112 if (!svm->nmi_singlestep)
3113 return NESTED_EXIT_DONE;
3114
3115 /* if it's not a singlestep exception, it's not ours */
3116 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
3117 return NESTED_EXIT_DONE;
3118 if (!(dr6 & DR6_BS))
3119 return NESTED_EXIT_DONE;
3120
3121 /* if the guest is singlestepping, it should get the vmexit */
3122 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
3123 disable_nmi_singlestep(svm);
3124 return NESTED_EXIT_DONE;
3125 }
3126
3127 /* it's ours, the nested hypervisor must not see this one */
3128 return NESTED_EXIT_HOST;
3129}
3130
Joerg Roedel410e4d52009-08-07 11:49:44 +02003131static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003132{
Alexander Grafcf74a782008-11-25 20:17:08 +01003133 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02003134
Joerg Roedel410e4d52009-08-07 11:49:44 +02003135 switch (exit_code) {
3136 case SVM_EXIT_INTR:
3137 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02003138 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02003139 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02003140 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01003141 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02003142 if (npt_enabled)
3143 return NESTED_EXIT_HOST;
3144 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02003145 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02003146 /* When we're shadowing, trap PFs, but not async PF */
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003147 if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003148 return NESTED_EXIT_HOST;
3149 break;
3150 default:
3151 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01003152 }
3153
Joerg Roedel410e4d52009-08-07 11:49:44 +02003154 return NESTED_EXIT_CONTINUE;
3155}
3156
3157/*
3158 * If this function returns true, this #vmexit was already handled
3159 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003160static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003161{
3162 u32 exit_code = svm->vmcb->control.exit_code;
3163 int vmexit = NESTED_EXIT_HOST;
3164
Alexander Grafcf74a782008-11-25 20:17:08 +01003165 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003166 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003167 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003168 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003169 case SVM_EXIT_IOIO:
3170 vmexit = nested_svm_intercept_ioio(svm);
3171 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003172 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
3173 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
3174 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003175 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003176 break;
3177 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01003178 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
3179 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
3180 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003181 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003182 break;
3183 }
3184 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
3185 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003186 if (svm->nested.intercept_exceptions & excp_bits) {
3187 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
3188 vmexit = nested_svm_intercept_db(svm);
3189 else
3190 vmexit = NESTED_EXIT_DONE;
3191 }
Gleb Natapov631bc482010-10-14 11:22:52 +02003192 /* async page fault always cause vmexit */
3193 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003194 svm->vcpu.arch.exception.nested_apf != 0)
Gleb Natapov631bc482010-10-14 11:22:52 +02003195 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003196 break;
3197 }
Joerg Roedel228070b2010-04-22 12:33:10 +02003198 case SVM_EXIT_ERR: {
3199 vmexit = NESTED_EXIT_DONE;
3200 break;
3201 }
Alexander Grafcf74a782008-11-25 20:17:08 +01003202 default: {
3203 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02003204 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003205 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003206 }
3207 }
3208
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003209 return vmexit;
3210}
3211
3212static int nested_svm_exit_handled(struct vcpu_svm *svm)
3213{
3214 int vmexit;
3215
3216 vmexit = nested_svm_intercept(svm);
3217
3218 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003219 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003220
3221 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01003222}
3223
Joerg Roedel0460a972009-08-07 11:49:31 +02003224static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
3225{
3226 struct vmcb_control_area *dst = &dst_vmcb->control;
3227 struct vmcb_control_area *from = &from_vmcb->control;
3228
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003229 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003230 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02003231 dst->intercept_exceptions = from->intercept_exceptions;
3232 dst->intercept = from->intercept;
3233 dst->iopm_base_pa = from->iopm_base_pa;
3234 dst->msrpm_base_pa = from->msrpm_base_pa;
3235 dst->tsc_offset = from->tsc_offset;
3236 dst->asid = from->asid;
3237 dst->tlb_ctl = from->tlb_ctl;
3238 dst->int_ctl = from->int_ctl;
3239 dst->int_vector = from->int_vector;
3240 dst->int_state = from->int_state;
3241 dst->exit_code = from->exit_code;
3242 dst->exit_code_hi = from->exit_code_hi;
3243 dst->exit_info_1 = from->exit_info_1;
3244 dst->exit_info_2 = from->exit_info_2;
3245 dst->exit_int_info = from->exit_int_info;
3246 dst->exit_int_info_err = from->exit_int_info_err;
3247 dst->nested_ctl = from->nested_ctl;
3248 dst->event_inj = from->event_inj;
3249 dst->event_inj_err = from->event_inj_err;
3250 dst->nested_cr3 = from->nested_cr3;
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003251 dst->virt_ext = from->virt_ext;
Joerg Roedel0460a972009-08-07 11:49:31 +02003252}
3253
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003254static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003255{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003256 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003257 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02003258 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003259 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01003260
Joerg Roedel17897f32009-10-09 16:08:29 +02003261 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
3262 vmcb->control.exit_info_1,
3263 vmcb->control.exit_info_2,
3264 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003265 vmcb->control.exit_int_info_err,
3266 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02003267
Joerg Roedel7597f122010-02-19 16:23:00 +01003268 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003269 if (!nested_vmcb)
3270 return 1;
3271
Joerg Roedel20307532010-11-29 17:51:48 +01003272 /* Exit Guest-Mode */
3273 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01003274 svm->nested.vmcb = 0;
3275
Alexander Grafcf74a782008-11-25 20:17:08 +01003276 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02003277 disable_gif(svm);
3278
3279 nested_vmcb->save.es = vmcb->save.es;
3280 nested_vmcb->save.cs = vmcb->save.cs;
3281 nested_vmcb->save.ss = vmcb->save.ss;
3282 nested_vmcb->save.ds = vmcb->save.ds;
3283 nested_vmcb->save.gdtr = vmcb->save.gdtr;
3284 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02003285 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003286 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003287 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003288 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003289 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03003290 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003291 nested_vmcb->save.rip = vmcb->save.rip;
3292 nested_vmcb->save.rsp = vmcb->save.rsp;
3293 nested_vmcb->save.rax = vmcb->save.rax;
3294 nested_vmcb->save.dr7 = vmcb->save.dr7;
3295 nested_vmcb->save.dr6 = vmcb->save.dr6;
3296 nested_vmcb->save.cpl = vmcb->save.cpl;
3297
3298 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
3299 nested_vmcb->control.int_vector = vmcb->control.int_vector;
3300 nested_vmcb->control.int_state = vmcb->control.int_state;
3301 nested_vmcb->control.exit_code = vmcb->control.exit_code;
3302 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
3303 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
3304 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
3305 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
3306 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003307
3308 if (svm->nrips_enabled)
3309 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02003310
3311 /*
3312 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
3313 * to make sure that we do not lose injected events. So check event_inj
3314 * here and copy it to exit_int_info if it is valid.
3315 * Exit_int_info and event_inj can't be both valid because the case
3316 * below only happens on a VMRUN instruction intercept which has
3317 * no valid exit_int_info set.
3318 */
3319 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
3320 struct vmcb_control_area *nc = &nested_vmcb->control;
3321
3322 nc->exit_int_info = vmcb->control.event_inj;
3323 nc->exit_int_info_err = vmcb->control.event_inj_err;
3324 }
3325
Joerg Roedel33740e42009-08-07 11:49:29 +02003326 nested_vmcb->control.tlb_ctl = 0;
3327 nested_vmcb->control.event_inj = 0;
3328 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01003329
3330 /* We always set V_INTR_MASKING and remember the old value in hflags */
3331 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
3332 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
3333
Alexander Grafcf74a782008-11-25 20:17:08 +01003334 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02003335 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01003336
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003337 svm->vcpu.arch.tsc_offset = svm->vmcb->control.tsc_offset;
Alexander Graf219b65d2009-06-15 15:21:25 +02003338 kvm_clear_exception_queue(&svm->vcpu);
3339 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003340
Joerg Roedel4b161842010-09-10 17:31:03 +02003341 svm->nested.nested_cr3 = 0;
3342
Alexander Grafcf74a782008-11-25 20:17:08 +01003343 /* Restore selected save entries */
3344 svm->vmcb->save.es = hsave->save.es;
3345 svm->vmcb->save.cs = hsave->save.cs;
3346 svm->vmcb->save.ss = hsave->save.ss;
3347 svm->vmcb->save.ds = hsave->save.ds;
3348 svm->vmcb->save.gdtr = hsave->save.gdtr;
3349 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003350 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01003351 svm_set_efer(&svm->vcpu, hsave->save.efer);
3352 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
3353 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
3354 if (npt_enabled) {
3355 svm->vmcb->save.cr3 = hsave->save.cr3;
3356 svm->vcpu.arch.cr3 = hsave->save.cr3;
3357 } else {
Avi Kivity23902182010-06-10 17:02:16 +03003358 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01003359 }
3360 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
3361 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
3362 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
3363 svm->vmcb->save.dr7 = 0;
3364 svm->vmcb->save.cpl = 0;
3365 svm->vmcb->control.exit_int_info = 0;
3366
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003367 mark_all_dirty(svm->vmcb);
3368
Joerg Roedel7597f122010-02-19 16:23:00 +01003369 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01003370
Joerg Roedel4b161842010-09-10 17:31:03 +02003371 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003372 kvm_mmu_reset_context(&svm->vcpu);
3373 kvm_mmu_load(&svm->vcpu);
3374
3375 return 0;
3376}
Alexander Graf3d6368e2008-11-25 20:17:07 +01003377
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003378static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003379{
Joerg Roedel323c3d82010-03-01 15:34:37 +01003380 /*
3381 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003382 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01003383 * the kvm msr permission bitmap may contain zero bits
3384 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01003385 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003386
Joerg Roedel323c3d82010-03-01 15:34:37 +01003387 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3388 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003389
Joerg Roedel323c3d82010-03-01 15:34:37 +01003390 for (i = 0; i < MSRPM_OFFSETS; i++) {
3391 u32 value, p;
3392 u64 offset;
3393
3394 if (msrpm_offsets[i] == 0xffffffff)
3395 break;
3396
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003397 p = msrpm_offsets[i];
3398 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01003399
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003400 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01003401 return false;
3402
3403 svm->nested.msrpm[p] = svm->msrpm[p] | value;
3404 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003405
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05003406 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
Alexander Graf3d6368e2008-11-25 20:17:07 +01003407
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003408 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003409}
3410
Joerg Roedel52c65a302010-08-02 16:46:44 +02003411static bool nested_vmcb_checks(struct vmcb *vmcb)
3412{
3413 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
3414 return false;
3415
Joerg Roedeldbe77582010-08-02 16:46:45 +02003416 if (vmcb->control.asid == 0)
3417 return false;
3418
Tom Lendackycea3a192017-12-04 10:57:24 -06003419 if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
3420 !npt_enabled)
Joerg Roedel4b161842010-09-10 17:31:03 +02003421 return false;
3422
Joerg Roedel52c65a302010-08-02 16:46:44 +02003423 return true;
3424}
3425
Ladi Prosekc2634062017-10-11 16:54:44 +02003426static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
3427 struct vmcb *nested_vmcb, struct page *page)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003428{
Avi Kivityf6e78472010-08-02 15:30:20 +03003429 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003430 svm->vcpu.arch.hflags |= HF_HIF_MASK;
3431 else
3432 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
3433
Tom Lendackycea3a192017-12-04 10:57:24 -06003434 if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
Joerg Roedel4b161842010-09-10 17:31:03 +02003435 kvm_mmu_unload(&svm->vcpu);
3436 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
3437 nested_svm_init_mmu_context(&svm->vcpu);
3438 }
3439
Alexander Graf3d6368e2008-11-25 20:17:07 +01003440 /* Load the nested guest state */
3441 svm->vmcb->save.es = nested_vmcb->save.es;
3442 svm->vmcb->save.cs = nested_vmcb->save.cs;
3443 svm->vmcb->save.ss = nested_vmcb->save.ss;
3444 svm->vmcb->save.ds = nested_vmcb->save.ds;
3445 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
3446 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003447 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003448 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
3449 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
3450 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
3451 if (npt_enabled) {
3452 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
3453 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003454 } else
Avi Kivity23902182010-06-10 17:02:16 +03003455 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003456
3457 /* Guest paging mode is active - reset mmu */
3458 kvm_mmu_reset_context(&svm->vcpu);
3459
Joerg Roedeldefbba52009-08-07 11:49:30 +02003460 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003461 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
3462 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
3463 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01003464
Alexander Graf3d6368e2008-11-25 20:17:07 +01003465 /* In case we don't even reach vcpu_run, the fields are not updated */
3466 svm->vmcb->save.rax = nested_vmcb->save.rax;
3467 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3468 svm->vmcb->save.rip = nested_vmcb->save.rip;
3469 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3470 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3471 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3472
Joerg Roedelf7138532010-03-01 15:34:40 +01003473 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003474 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003475
Joerg Roedelaad42c62009-08-07 11:49:34 +02003476 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003477 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003478 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02003479 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3480 svm->nested.intercept = nested_vmcb->control.intercept;
3481
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08003482 svm_flush_tlb(&svm->vcpu, true);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003483 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003484 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3485 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3486 else
3487 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3488
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003489 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3490 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003491 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3492 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003493 }
3494
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003495 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003496 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003497
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003498 svm->vcpu.arch.tsc_offset += nested_vmcb->control.tsc_offset;
3499 svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset;
3500
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003501 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003502 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3503 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003504 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3505 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3506
Joerg Roedel7597f122010-02-19 16:23:00 +01003507 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003508
Joerg Roedel20307532010-11-29 17:51:48 +01003509 /* Enter Guest-Mode */
3510 enter_guest_mode(&svm->vcpu);
3511
Joerg Roedel384c6362010-11-30 18:03:56 +01003512 /*
3513 * Merge guest and host intercepts - must be called with vcpu in
3514 * guest-mode to take affect here
3515 */
3516 recalc_intercepts(svm);
3517
Joerg Roedel06fc77722010-02-19 16:23:07 +01003518 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003519
Joerg Roedel2af91942009-08-07 11:49:28 +02003520 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003521
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003522 mark_all_dirty(svm->vmcb);
Ladi Prosekc2634062017-10-11 16:54:44 +02003523}
3524
3525static bool nested_svm_vmrun(struct vcpu_svm *svm)
3526{
3527 struct vmcb *nested_vmcb;
3528 struct vmcb *hsave = svm->nested.hsave;
3529 struct vmcb *vmcb = svm->vmcb;
3530 struct page *page;
3531 u64 vmcb_gpa;
3532
3533 vmcb_gpa = svm->vmcb->save.rax;
3534
3535 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
3536 if (!nested_vmcb)
3537 return false;
3538
3539 if (!nested_vmcb_checks(nested_vmcb)) {
3540 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3541 nested_vmcb->control.exit_code_hi = 0;
3542 nested_vmcb->control.exit_info_1 = 0;
3543 nested_vmcb->control.exit_info_2 = 0;
3544
3545 nested_svm_unmap(page);
3546
3547 return false;
3548 }
3549
3550 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3551 nested_vmcb->save.rip,
3552 nested_vmcb->control.int_ctl,
3553 nested_vmcb->control.event_inj,
3554 nested_vmcb->control.nested_ctl);
3555
3556 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3557 nested_vmcb->control.intercept_cr >> 16,
3558 nested_vmcb->control.intercept_exceptions,
3559 nested_vmcb->control.intercept);
3560
3561 /* Clear internal status */
3562 kvm_clear_exception_queue(&svm->vcpu);
3563 kvm_clear_interrupt_queue(&svm->vcpu);
3564
3565 /*
3566 * Save the old vmcb, so we don't need to pick what we save, but can
3567 * restore everything when a VMEXIT occurs
3568 */
3569 hsave->save.es = vmcb->save.es;
3570 hsave->save.cs = vmcb->save.cs;
3571 hsave->save.ss = vmcb->save.ss;
3572 hsave->save.ds = vmcb->save.ds;
3573 hsave->save.gdtr = vmcb->save.gdtr;
3574 hsave->save.idtr = vmcb->save.idtr;
3575 hsave->save.efer = svm->vcpu.arch.efer;
3576 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3577 hsave->save.cr4 = svm->vcpu.arch.cr4;
3578 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3579 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3580 hsave->save.rsp = vmcb->save.rsp;
3581 hsave->save.rax = vmcb->save.rax;
3582 if (npt_enabled)
3583 hsave->save.cr3 = vmcb->save.cr3;
3584 else
3585 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3586
3587 copy_vmcb_control_area(hsave, vmcb);
3588
3589 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003590
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003591 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003592}
3593
Joerg Roedel9966bf62009-08-07 11:49:40 +02003594static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01003595{
3596 to_vmcb->save.fs = from_vmcb->save.fs;
3597 to_vmcb->save.gs = from_vmcb->save.gs;
3598 to_vmcb->save.tr = from_vmcb->save.tr;
3599 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3600 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3601 to_vmcb->save.star = from_vmcb->save.star;
3602 to_vmcb->save.lstar = from_vmcb->save.lstar;
3603 to_vmcb->save.cstar = from_vmcb->save.cstar;
3604 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3605 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3606 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3607 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01003608}
3609
Avi Kivity851ba692009-08-24 11:10:17 +03003610static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003611{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003612 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003613 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003614 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003615
Alexander Graf55426752008-11-25 20:17:06 +01003616 if (nested_svm_check_permissions(svm))
3617 return 1;
3618
Joerg Roedel7597f122010-02-19 16:23:00 +01003619 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003620 if (!nested_vmcb)
3621 return 1;
3622
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003623 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003624 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003625
Joerg Roedel9966bf62009-08-07 11:49:40 +02003626 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003627 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003628
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003629 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003630}
3631
Avi Kivity851ba692009-08-24 11:10:17 +03003632static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003633{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003634 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003635 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003636 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003637
Alexander Graf55426752008-11-25 20:17:06 +01003638 if (nested_svm_check_permissions(svm))
3639 return 1;
3640
Joerg Roedel7597f122010-02-19 16:23:00 +01003641 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003642 if (!nested_vmcb)
3643 return 1;
3644
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003645 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003646 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003647
Joerg Roedel9966bf62009-08-07 11:49:40 +02003648 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003649 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003650
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003651 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003652}
3653
Avi Kivity851ba692009-08-24 11:10:17 +03003654static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003655{
Alexander Graf3d6368e2008-11-25 20:17:07 +01003656 if (nested_svm_check_permissions(svm))
3657 return 1;
3658
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02003659 /* Save rip after vmrun instruction */
3660 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003661
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003662 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01003663 return 1;
3664
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003665 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02003666 goto failed;
3667
3668 return 1;
3669
3670failed:
3671
3672 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3673 svm->vmcb->control.exit_code_hi = 0;
3674 svm->vmcb->control.exit_info_1 = 0;
3675 svm->vmcb->control.exit_info_2 = 0;
3676
3677 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003678
3679 return 1;
3680}
3681
Avi Kivity851ba692009-08-24 11:10:17 +03003682static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003683{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003684 int ret;
3685
Alexander Graf1371d902008-11-25 20:17:04 +01003686 if (nested_svm_check_permissions(svm))
3687 return 1;
3688
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003689 /*
3690 * If VGIF is enabled, the STGI intercept is only added to
Ladi Prosekcc3d9672017-10-17 16:02:39 +02003691 * detect the opening of the SMI/NMI window; remove it now.
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003692 */
3693 if (vgif_enabled(svm))
3694 clr_intercept(svm, INTERCEPT_STGI);
3695
Alexander Graf1371d902008-11-25 20:17:04 +01003696 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003697 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003698 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003699
Joerg Roedel2af91942009-08-07 11:49:28 +02003700 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003701
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003702 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003703}
3704
Avi Kivity851ba692009-08-24 11:10:17 +03003705static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003706{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003707 int ret;
3708
Alexander Graf1371d902008-11-25 20:17:04 +01003709 if (nested_svm_check_permissions(svm))
3710 return 1;
3711
3712 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003713 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003714
Joerg Roedel2af91942009-08-07 11:49:28 +02003715 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003716
3717 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05003718 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3719 svm_clear_vintr(svm);
3720 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3721 mark_dirty(svm->vmcb, VMCB_INTR);
3722 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003723
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003724 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003725}
3726
Avi Kivity851ba692009-08-24 11:10:17 +03003727static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02003728{
3729 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02003730
David Kaplan668f1982015-02-20 16:02:10 -06003731 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
3732 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02003733
Alexander Grafff092382009-06-15 15:21:24 +02003734 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06003735 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02003736
3737 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003738 return kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02003739}
3740
Joerg Roedel532a46b2009-10-09 16:08:32 +02003741static int skinit_interception(struct vcpu_svm *svm)
3742{
David Kaplan668f1982015-02-20 16:02:10 -06003743 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003744
3745 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3746 return 1;
3747}
3748
David Kaplandab429a2015-03-02 13:43:37 -06003749static int wbinvd_interception(struct vcpu_svm *svm)
3750{
Kyle Huey6affcbe2016-11-29 12:40:40 -08003751 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06003752}
3753
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003754static int xsetbv_interception(struct vcpu_svm *svm)
3755{
3756 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
3757 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3758
3759 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3760 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003761 return kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003762 }
3763
3764 return 1;
3765}
3766
Avi Kivity851ba692009-08-24 11:10:17 +03003767static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003768{
Izik Eidus37817f22008-03-24 23:14:53 +02003769 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003770 int reason;
3771 int int_type = svm->vmcb->control.exit_int_info &
3772 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003773 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003774 uint32_t type =
3775 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3776 uint32_t idt_v =
3777 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003778 bool has_error_code = false;
3779 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003780
3781 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003782
Izik Eidus37817f22008-03-24 23:14:53 +02003783 if (svm->vmcb->control.exit_info_2 &
3784 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003785 reason = TASK_SWITCH_IRET;
3786 else if (svm->vmcb->control.exit_info_2 &
3787 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3788 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003789 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003790 reason = TASK_SWITCH_GATE;
3791 else
3792 reason = TASK_SWITCH_CALL;
3793
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003794 if (reason == TASK_SWITCH_GATE) {
3795 switch (type) {
3796 case SVM_EXITINTINFO_TYPE_NMI:
3797 svm->vcpu.arch.nmi_injected = false;
3798 break;
3799 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003800 if (svm->vmcb->control.exit_info_2 &
3801 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3802 has_error_code = true;
3803 error_code =
3804 (u32)svm->vmcb->control.exit_info_2;
3805 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003806 kvm_clear_exception_queue(&svm->vcpu);
3807 break;
3808 case SVM_EXITINTINFO_TYPE_INTR:
3809 kvm_clear_interrupt_queue(&svm->vcpu);
3810 break;
3811 default:
3812 break;
3813 }
3814 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003815
Gleb Natapov8317c292009-04-12 13:37:02 +03003816 if (reason != TASK_SWITCH_GATE ||
3817 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3818 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03003819 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3820 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003821
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003822 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3823 int_vec = -1;
3824
3825 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03003826 has_error_code, error_code) == EMULATE_FAIL) {
3827 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3828 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3829 svm->vcpu.run->internal.ndata = 0;
3830 return 0;
3831 }
3832 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003833}
3834
Avi Kivity851ba692009-08-24 11:10:17 +03003835static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003836{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003837 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Kyle Huey6a908b62016-11-29 12:40:37 -08003838 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003839}
3840
Avi Kivity851ba692009-08-24 11:10:17 +03003841static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003842{
3843 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003844 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003845 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003846 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003847 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003848 return 1;
3849}
3850
Avi Kivity851ba692009-08-24 11:10:17 +03003851static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003852{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003853 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3854 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3855
3856 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003857 return kvm_skip_emulated_instruction(&svm->vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003858}
3859
Avi Kivity851ba692009-08-24 11:10:17 +03003860static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861{
Andre Przywara51d8b662010-12-21 11:12:02 +01003862 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003863}
3864
Brijesh Singh7607b712018-02-19 10:14:44 -06003865static int rsm_interception(struct vcpu_svm *svm)
3866{
3867 return x86_emulate_instruction(&svm->vcpu, 0, 0,
3868 rsm_ins_bytes, 2) == EMULATE_DONE;
3869}
3870
Avi Kivity332b56e2011-11-10 14:57:24 +02003871static int rdpmc_interception(struct vcpu_svm *svm)
3872{
3873 int err;
3874
3875 if (!static_cpu_has(X86_FEATURE_NRIPS))
3876 return emulate_on_interception(svm);
3877
3878 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08003879 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02003880}
3881
Xiubo Li52eb5a62015-03-13 17:39:45 +08003882static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3883 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003884{
3885 unsigned long cr0 = svm->vcpu.arch.cr0;
3886 bool ret = false;
3887 u64 intercept;
3888
3889 intercept = svm->nested.intercept;
3890
3891 if (!is_guest_mode(&svm->vcpu) ||
3892 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3893 return false;
3894
3895 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3896 val &= ~SVM_CR0_SELECTIVE_MASK;
3897
3898 if (cr0 ^ val) {
3899 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3900 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3901 }
3902
3903 return ret;
3904}
3905
Andre Przywara7ff76d52010-12-21 11:12:04 +01003906#define CR_VALID (1ULL << 63)
3907
3908static int cr_interception(struct vcpu_svm *svm)
3909{
3910 int reg, cr;
3911 unsigned long val;
3912 int err;
3913
3914 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3915 return emulate_on_interception(svm);
3916
3917 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3918 return emulate_on_interception(svm);
3919
3920 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003921 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3922 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3923 else
3924 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003925
3926 err = 0;
3927 if (cr >= 16) { /* mov to cr */
3928 cr -= 16;
3929 val = kvm_register_read(&svm->vcpu, reg);
3930 switch (cr) {
3931 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003932 if (!check_selective_cr0_intercepted(svm, val))
3933 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003934 else
3935 return 1;
3936
Andre Przywara7ff76d52010-12-21 11:12:04 +01003937 break;
3938 case 3:
3939 err = kvm_set_cr3(&svm->vcpu, val);
3940 break;
3941 case 4:
3942 err = kvm_set_cr4(&svm->vcpu, val);
3943 break;
3944 case 8:
3945 err = kvm_set_cr8(&svm->vcpu, val);
3946 break;
3947 default:
3948 WARN(1, "unhandled write to CR%d", cr);
3949 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3950 return 1;
3951 }
3952 } else { /* mov from cr */
3953 switch (cr) {
3954 case 0:
3955 val = kvm_read_cr0(&svm->vcpu);
3956 break;
3957 case 2:
3958 val = svm->vcpu.arch.cr2;
3959 break;
3960 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003961 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003962 break;
3963 case 4:
3964 val = kvm_read_cr4(&svm->vcpu);
3965 break;
3966 case 8:
3967 val = kvm_get_cr8(&svm->vcpu);
3968 break;
3969 default:
3970 WARN(1, "unhandled read from CR%d", cr);
3971 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3972 return 1;
3973 }
3974 kvm_register_write(&svm->vcpu, reg, val);
3975 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08003976 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003977}
3978
Andre Przywaracae37972010-12-21 11:12:05 +01003979static int dr_interception(struct vcpu_svm *svm)
3980{
3981 int reg, dr;
3982 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003983
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003984 if (svm->vcpu.guest_debug == 0) {
3985 /*
3986 * No more DR vmexits; force a reload of the debug registers
3987 * and reenter on this instruction. The next vmexit will
3988 * retrieve the full state of the debug registers.
3989 */
3990 clr_dr_intercepts(svm);
3991 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3992 return 1;
3993 }
3994
Andre Przywaracae37972010-12-21 11:12:05 +01003995 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3996 return emulate_on_interception(svm);
3997
3998 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3999 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
4000
4001 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004002 if (!kvm_require_dr(&svm->vcpu, dr - 16))
4003 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01004004 val = kvm_register_read(&svm->vcpu, reg);
4005 kvm_set_dr(&svm->vcpu, dr - 16, val);
4006 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03004007 if (!kvm_require_dr(&svm->vcpu, dr))
4008 return 1;
4009 kvm_get_dr(&svm->vcpu, dr, &val);
4010 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01004011 }
4012
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004013 return kvm_skip_emulated_instruction(&svm->vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01004014}
4015
Avi Kivity851ba692009-08-24 11:10:17 +03004016static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01004017{
Avi Kivity851ba692009-08-24 11:10:17 +03004018 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004019 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03004020
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004021 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
4022 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01004023 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004024 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01004025 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004026 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01004027 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01004028 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
4029 return 0;
4030}
4031
Tom Lendacky801e4592018-02-21 13:39:51 -06004032static int svm_get_msr_feature(struct kvm_msr_entry *msr)
4033{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004034 msr->data = 0;
4035
4036 switch (msr->index) {
4037 case MSR_F10H_DECFG:
4038 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
4039 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
4040 break;
4041 default:
4042 return 1;
4043 }
4044
4045 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004046}
4047
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004048static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004049{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004050 struct vcpu_svm *svm = to_svm(vcpu);
4051
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004052 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05304053 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004054 msr_info->data = svm->vmcb->control.tsc_offset +
Haozhong Zhang35181e82015-10-20 15:39:03 +08004055 kvm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01004056
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057 break;
4058 }
Brian Gerst8c065852010-07-17 09:03:26 -04004059 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004060 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004061 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08004062#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004063 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004064 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065 break;
4066 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004067 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068 break;
4069 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004070 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004071 break;
4072 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004073 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074 break;
4075#endif
4076 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004077 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004078 break;
4079 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004080 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004081 break;
4082 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004083 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004085 case MSR_TSC_AUX:
4086 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4087 return 1;
4088 msr_info->data = svm->tsc_aux;
4089 break;
Joerg Roedele0231712010-02-24 18:59:10 +01004090 /*
4091 * Nobody will change the following 5 values in the VMCB so we can
4092 * safely return them on rdmsr. They will always be 0 until LBRV is
4093 * implemented.
4094 */
Joerg Roedela2938c82008-02-13 16:30:28 +01004095 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004096 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01004097 break;
4098 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004099 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01004100 break;
4101 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004102 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01004103 break;
4104 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004105 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01004106 break;
4107 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004108 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01004109 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004110 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004111 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004112 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01004113 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004114 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01004115 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004116 case MSR_IA32_SPEC_CTRL:
4117 if (!msr_info->host_initiated &&
4118 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
4119 return 1;
4120
4121 msr_info->data = svm->spec_ctrl;
4122 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01004123 case MSR_F15H_IC_CFG: {
4124
4125 int family, model;
4126
4127 family = guest_cpuid_family(vcpu);
4128 model = guest_cpuid_model(vcpu);
4129
4130 if (family < 0 || model < 0)
4131 return kvm_get_msr_common(vcpu, msr_info);
4132
4133 msr_info->data = 0;
4134
4135 if (family == 0x15 &&
4136 (model >= 0x2 && model < 0x20))
4137 msr_info->data = 0x1E;
4138 }
4139 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004140 case MSR_F10H_DECFG:
4141 msr_info->data = svm->msr_decfg;
4142 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004143 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004144 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145 }
4146 return 0;
4147}
4148
Avi Kivity851ba692009-08-24 11:10:17 +03004149static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004150{
David Kaplan668f1982015-02-20 16:02:10 -06004151 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004152 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004153
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004154 msr_info.index = ecx;
4155 msr_info.host_initiated = false;
4156 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02004157 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004158 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004159 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004160 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004161 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004162
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004163 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
4164 msr_info.data & 0xffffffff);
4165 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
4166 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004167 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004168 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004170}
4171
Joerg Roedel4a810182010-02-24 18:59:15 +01004172static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
4173{
4174 struct vcpu_svm *svm = to_svm(vcpu);
4175 int svm_dis, chg_mask;
4176
4177 if (data & ~SVM_VM_CR_VALID_MASK)
4178 return 1;
4179
4180 chg_mask = SVM_VM_CR_VALID_MASK;
4181
4182 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
4183 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
4184
4185 svm->nested.vm_cr_msr &= ~chg_mask;
4186 svm->nested.vm_cr_msr |= (data & chg_mask);
4187
4188 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
4189
4190 /* check for svm_disable while efer.svme is set */
4191 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
4192 return 1;
4193
4194 return 0;
4195}
4196
Will Auld8fe8ab42012-11-29 12:42:12 -08004197static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004198{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004199 struct vcpu_svm *svm = to_svm(vcpu);
4200
Will Auld8fe8ab42012-11-29 12:42:12 -08004201 u32 ecx = msr->index;
4202 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004203 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02004204 case MSR_IA32_CR_PAT:
4205 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4206 return 1;
4207 vcpu->arch.pat = data;
4208 svm->vmcb->save.g_pat = data;
4209 mark_dirty(svm->vmcb, VMCB_NPT);
4210 break;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10004211 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08004212 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004213 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004214 case MSR_IA32_SPEC_CTRL:
4215 if (!msr->host_initiated &&
4216 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
4217 return 1;
4218
4219 /* The STIBP bit doesn't fault even if it's not advertised */
4220 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
4221 return 1;
4222
4223 svm->spec_ctrl = data;
4224
4225 if (!data)
4226 break;
4227
4228 /*
4229 * For non-nested:
4230 * When it's written (to non-zero) for the first time, pass
4231 * it through.
4232 *
4233 * For nested:
4234 * The handling of the MSR bitmap for L2 guests is done in
4235 * nested_svm_vmrun_msrpm.
4236 * We update the L1 MSR bit as well since it will end up
4237 * touching the MSR anyway now.
4238 */
4239 set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
4240 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004241 case MSR_IA32_PRED_CMD:
4242 if (!msr->host_initiated &&
4243 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB))
4244 return 1;
4245
4246 if (data & ~PRED_CMD_IBPB)
4247 return 1;
4248
4249 if (!data)
4250 break;
4251
4252 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4253 if (is_guest_mode(vcpu))
4254 break;
4255 set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
4256 break;
Brian Gerst8c065852010-07-17 09:03:26 -04004257 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004258 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004259 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08004260#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004261 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004262 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004263 break;
4264 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004265 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004266 break;
4267 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004268 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004269 break;
4270 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004271 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272 break;
4273#endif
4274 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004275 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004276 break;
4277 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02004278 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004279 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004280 break;
4281 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02004282 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004283 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004284 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004285 case MSR_TSC_AUX:
4286 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4287 return 1;
4288
4289 /*
4290 * This is rare, so we update the MSR here instead of using
4291 * direct_access_msrs. Doing that would require a rdmsr in
4292 * svm_vcpu_put.
4293 */
4294 svm->tsc_aux = data;
4295 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
4296 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01004297 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004298 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03004299 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
4300 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004301 break;
4302 }
4303 if (data & DEBUGCTL_RESERVED_BITS)
4304 return 1;
4305
4306 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01004307 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004308 if (data & (1ULL<<0))
4309 svm_enable_lbrv(svm);
4310 else
4311 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01004312 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004313 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02004314 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004315 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004316 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01004317 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004318 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03004319 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004320 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004321 case MSR_F10H_DECFG: {
4322 struct kvm_msr_entry msr_entry;
4323
4324 msr_entry.index = msr->index;
4325 if (svm_get_msr_feature(&msr_entry))
4326 return 1;
4327
4328 /* Check the supported bits */
4329 if (data & ~msr_entry.data)
4330 return 1;
4331
4332 /* Don't allow the guest to change a bit, #GP */
4333 if (!msr->host_initiated && (data ^ msr_entry.data))
4334 return 1;
4335
4336 svm->msr_decfg = data;
4337 break;
4338 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004339 case MSR_IA32_APICBASE:
4340 if (kvm_vcpu_apicv_active(vcpu))
4341 avic_update_vapic_bar(to_svm(vcpu), data);
4342 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004343 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08004344 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004345 }
4346 return 0;
4347}
4348
Avi Kivity851ba692009-08-24 11:10:17 +03004349static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004350{
Will Auld8fe8ab42012-11-29 12:42:12 -08004351 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06004352 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
4353 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004354
Will Auld8fe8ab42012-11-29 12:42:12 -08004355 msr.data = data;
4356 msr.index = ecx;
4357 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004358
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004359 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03004360 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02004361 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004362 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004363 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004364 } else {
4365 trace_kvm_msr_write(ecx, data);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004366 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02004367 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004368}
4369
Avi Kivity851ba692009-08-24 11:10:17 +03004370static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004371{
Rusty Russelle756fc62007-07-30 20:07:08 +10004372 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03004373 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004374 else
Avi Kivity851ba692009-08-24 11:10:17 +03004375 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004376}
4377
Avi Kivity851ba692009-08-24 11:10:17 +03004378static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08004379{
Avi Kivity3842d132010-07-27 12:30:24 +03004380 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01004381 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03004382 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004383 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08004384 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08004385 return 1;
4386}
4387
Mark Langsdorf565d0992009-10-06 14:25:02 -05004388static int pause_interception(struct vcpu_svm *svm)
4389{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004390 struct kvm_vcpu *vcpu = &svm->vcpu;
4391 bool in_kernel = (svm_get_cpl(vcpu) == 0);
4392
Babu Moger8566ac82018-03-16 16:37:26 -04004393 if (pause_filter_thresh)
4394 grow_ple_window(vcpu);
4395
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004396 kvm_vcpu_on_spin(vcpu, in_kernel);
Mark Langsdorf565d0992009-10-06 14:25:02 -05004397 return 1;
4398}
4399
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004400static int nop_interception(struct vcpu_svm *svm)
4401{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004402 return kvm_skip_emulated_instruction(&(svm->vcpu));
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004403}
4404
4405static int monitor_interception(struct vcpu_svm *svm)
4406{
4407 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
4408 return nop_interception(svm);
4409}
4410
4411static int mwait_interception(struct vcpu_svm *svm)
4412{
4413 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4414 return nop_interception(svm);
4415}
4416
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004417enum avic_ipi_failure_cause {
4418 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
4419 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
4420 AVIC_IPI_FAILURE_INVALID_TARGET,
4421 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
4422};
4423
4424static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
4425{
4426 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
4427 u32 icrl = svm->vmcb->control.exit_info_1;
4428 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03004429 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004430 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4431
4432 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
4433
4434 switch (id) {
4435 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
4436 /*
4437 * AVIC hardware handles the generation of
4438 * IPIs when the specified Message Type is Fixed
4439 * (also known as fixed delivery mode) and
4440 * the Trigger Mode is edge-triggered. The hardware
4441 * also supports self and broadcast delivery modes
4442 * specified via the Destination Shorthand(DSH)
4443 * field of the ICRL. Logical and physical APIC ID
4444 * formats are supported. All other IPI types cause
4445 * a #VMEXIT, which needs to emulated.
4446 */
4447 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
4448 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
4449 break;
4450 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
4451 int i;
4452 struct kvm_vcpu *vcpu;
4453 struct kvm *kvm = svm->vcpu.kvm;
4454 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4455
4456 /*
4457 * At this point, we expect that the AVIC HW has already
4458 * set the appropriate IRR bits on the valid target
4459 * vcpus. So, we just need to kick the appropriate vcpu.
4460 */
4461 kvm_for_each_vcpu(i, vcpu, kvm) {
4462 bool m = kvm_apic_match_dest(vcpu, apic,
4463 icrl & KVM_APIC_SHORT_MASK,
4464 GET_APIC_DEST_FIELD(icrh),
4465 icrl & KVM_APIC_DEST_MASK);
4466
4467 if (m && !avic_vcpu_is_running(vcpu))
4468 kvm_vcpu_wake_up(vcpu);
4469 }
4470 break;
4471 }
4472 case AVIC_IPI_FAILURE_INVALID_TARGET:
4473 break;
4474 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
4475 WARN_ONCE(1, "Invalid backing page\n");
4476 break;
4477 default:
4478 pr_err("Unknown IPI interception\n");
4479 }
4480
4481 return 1;
4482}
4483
4484static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
4485{
Sean Christopherson81811c12018-03-20 12:17:21 -07004486 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004487 int index;
4488 u32 *logical_apic_id_table;
4489 int dlid = GET_APIC_LOGICAL_ID(ldr);
4490
4491 if (!dlid)
4492 return NULL;
4493
4494 if (flat) { /* flat */
4495 index = ffs(dlid) - 1;
4496 if (index > 7)
4497 return NULL;
4498 } else { /* cluster */
4499 int cluster = (dlid & 0xf0) >> 4;
4500 int apic = ffs(dlid & 0x0f) - 1;
4501
4502 if ((apic < 0) || (apic > 7) ||
4503 (cluster >= 0xf))
4504 return NULL;
4505 index = (cluster << 2) + apic;
4506 }
4507
Sean Christopherson81811c12018-03-20 12:17:21 -07004508 logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004509
4510 return &logical_apic_id_table[index];
4511}
4512
4513static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
4514 bool valid)
4515{
4516 bool flat;
4517 u32 *entry, new_entry;
4518
4519 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4520 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4521 if (!entry)
4522 return -EINVAL;
4523
4524 new_entry = READ_ONCE(*entry);
4525 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4526 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
4527 if (valid)
4528 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4529 else
4530 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4531 WRITE_ONCE(*entry, new_entry);
4532
4533 return 0;
4534}
4535
4536static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4537{
4538 int ret;
4539 struct vcpu_svm *svm = to_svm(vcpu);
4540 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
4541
4542 if (!ldr)
4543 return 1;
4544
4545 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
4546 if (ret && svm->ldr_reg) {
4547 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
4548 svm->ldr_reg = 0;
4549 } else {
4550 svm->ldr_reg = ldr;
4551 }
4552 return ret;
4553}
4554
4555static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4556{
4557 u64 *old, *new;
4558 struct vcpu_svm *svm = to_svm(vcpu);
4559 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
4560 u32 id = (apic_id_reg >> 24) & 0xff;
4561
4562 if (vcpu->vcpu_id == id)
4563 return 0;
4564
4565 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4566 new = avic_get_physical_id_entry(vcpu, id);
4567 if (!new || !old)
4568 return 1;
4569
4570 /* We need to move physical_id_entry to new offset */
4571 *new = *old;
4572 *old = 0ULL;
4573 to_svm(vcpu)->avic_physical_id_cache = new;
4574
4575 /*
4576 * Also update the guest physical APIC ID in the logical
4577 * APIC ID table entry if already setup the LDR.
4578 */
4579 if (svm->ldr_reg)
4580 avic_handle_ldr_update(vcpu);
4581
4582 return 0;
4583}
4584
4585static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
4586{
4587 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christopherson81811c12018-03-20 12:17:21 -07004588 struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm);
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004589 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
4590 u32 mod = (dfr >> 28) & 0xf;
4591
4592 /*
4593 * We assume that all local APICs are using the same type.
4594 * If this changes, we need to flush the AVIC logical
4595 * APID id table.
4596 */
Sean Christopherson81811c12018-03-20 12:17:21 -07004597 if (kvm_svm->ldr_mode == mod)
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004598 return 0;
4599
Sean Christopherson81811c12018-03-20 12:17:21 -07004600 clear_page(page_address(kvm_svm->avic_logical_id_table_page));
4601 kvm_svm->ldr_mode = mod;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004602
4603 if (svm->ldr_reg)
4604 avic_handle_ldr_update(vcpu);
4605 return 0;
4606}
4607
4608static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4609{
4610 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4611 u32 offset = svm->vmcb->control.exit_info_1 &
4612 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4613
4614 switch (offset) {
4615 case APIC_ID:
4616 if (avic_handle_apic_id_update(&svm->vcpu))
4617 return 0;
4618 break;
4619 case APIC_LDR:
4620 if (avic_handle_ldr_update(&svm->vcpu))
4621 return 0;
4622 break;
4623 case APIC_DFR:
4624 avic_handle_dfr_update(&svm->vcpu);
4625 break;
4626 default:
4627 break;
4628 }
4629
4630 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4631
4632 return 1;
4633}
4634
4635static bool is_avic_unaccelerated_access_trap(u32 offset)
4636{
4637 bool ret = false;
4638
4639 switch (offset) {
4640 case APIC_ID:
4641 case APIC_EOI:
4642 case APIC_RRR:
4643 case APIC_LDR:
4644 case APIC_DFR:
4645 case APIC_SPIV:
4646 case APIC_ESR:
4647 case APIC_ICR:
4648 case APIC_LVTT:
4649 case APIC_LVTTHMR:
4650 case APIC_LVTPC:
4651 case APIC_LVT0:
4652 case APIC_LVT1:
4653 case APIC_LVTERR:
4654 case APIC_TMICT:
4655 case APIC_TDCR:
4656 ret = true;
4657 break;
4658 default:
4659 break;
4660 }
4661 return ret;
4662}
4663
4664static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4665{
4666 int ret = 0;
4667 u32 offset = svm->vmcb->control.exit_info_1 &
4668 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4669 u32 vector = svm->vmcb->control.exit_info_2 &
4670 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4671 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4672 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4673 bool trap = is_avic_unaccelerated_access_trap(offset);
4674
4675 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4676 trap, write, vector);
4677 if (trap) {
4678 /* Handling Trap */
4679 WARN_ONCE(!write, "svm: Handling trap read.\n");
4680 ret = avic_unaccel_trap_write(svm);
4681 } else {
4682 /* Handling Fault */
4683 ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
4684 }
4685
4686 return ret;
4687}
4688
Mathias Krause09941fb2012-08-30 01:30:20 +02004689static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01004690 [SVM_EXIT_READ_CR0] = cr_interception,
4691 [SVM_EXIT_READ_CR3] = cr_interception,
4692 [SVM_EXIT_READ_CR4] = cr_interception,
4693 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06004694 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02004695 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01004696 [SVM_EXIT_WRITE_CR3] = cr_interception,
4697 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004698 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01004699 [SVM_EXIT_READ_DR0] = dr_interception,
4700 [SVM_EXIT_READ_DR1] = dr_interception,
4701 [SVM_EXIT_READ_DR2] = dr_interception,
4702 [SVM_EXIT_READ_DR3] = dr_interception,
4703 [SVM_EXIT_READ_DR4] = dr_interception,
4704 [SVM_EXIT_READ_DR5] = dr_interception,
4705 [SVM_EXIT_READ_DR6] = dr_interception,
4706 [SVM_EXIT_READ_DR7] = dr_interception,
4707 [SVM_EXIT_WRITE_DR0] = dr_interception,
4708 [SVM_EXIT_WRITE_DR1] = dr_interception,
4709 [SVM_EXIT_WRITE_DR2] = dr_interception,
4710 [SVM_EXIT_WRITE_DR3] = dr_interception,
4711 [SVM_EXIT_WRITE_DR4] = dr_interception,
4712 [SVM_EXIT_WRITE_DR5] = dr_interception,
4713 [SVM_EXIT_WRITE_DR6] = dr_interception,
4714 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004715 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4716 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004717 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004718 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004719 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01004720 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Liran Alon97184202018-03-12 13:12:52 +02004721 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004722 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02004723 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004724 [SVM_EXIT_SMI] = nop_on_interception,
4725 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08004726 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02004727 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004728 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004729 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02004730 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05004731 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004732 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004733 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02004734 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004735 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004736 [SVM_EXIT_MSR] = msr_interception,
4737 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08004738 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01004739 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02004740 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01004741 [SVM_EXIT_VMLOAD] = vmload_interception,
4742 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01004743 [SVM_EXIT_STGI] = stgi_interception,
4744 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02004745 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06004746 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004747 [SVM_EXIT_MONITOR] = monitor_interception,
4748 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01004749 [SVM_EXIT_XSETBV] = xsetbv_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02004750 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06004751 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004752 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4753 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004754};
4755
Joe Perchesae8cc052011-04-24 22:00:50 -07004756static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02004757{
4758 struct vcpu_svm *svm = to_svm(vcpu);
4759 struct vmcb_control_area *control = &svm->vmcb->control;
4760 struct vmcb_save_area *save = &svm->vmcb->save;
4761
4762 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004763 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4764 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4765 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4766 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4767 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4768 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4769 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
Babu Moger1d8fb442018-03-16 16:37:25 -04004770 pr_err("%-20s%d\n", "pause filter threshold:",
4771 control->pause_filter_thresh);
Joe Perchesae8cc052011-04-24 22:00:50 -07004772 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4773 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4774 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4775 pr_err("%-20s%d\n", "asid:", control->asid);
4776 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4777 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4778 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4779 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4780 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4781 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4782 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4783 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4784 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4785 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4786 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004787 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07004788 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4789 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05004790 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07004791 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004792 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4793 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4794 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004795 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004796 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4797 "es:",
4798 save->es.selector, save->es.attrib,
4799 save->es.limit, save->es.base);
4800 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4801 "cs:",
4802 save->cs.selector, save->cs.attrib,
4803 save->cs.limit, save->cs.base);
4804 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4805 "ss:",
4806 save->ss.selector, save->ss.attrib,
4807 save->ss.limit, save->ss.base);
4808 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4809 "ds:",
4810 save->ds.selector, save->ds.attrib,
4811 save->ds.limit, save->ds.base);
4812 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4813 "fs:",
4814 save->fs.selector, save->fs.attrib,
4815 save->fs.limit, save->fs.base);
4816 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4817 "gs:",
4818 save->gs.selector, save->gs.attrib,
4819 save->gs.limit, save->gs.base);
4820 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4821 "gdtr:",
4822 save->gdtr.selector, save->gdtr.attrib,
4823 save->gdtr.limit, save->gdtr.base);
4824 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4825 "ldtr:",
4826 save->ldtr.selector, save->ldtr.attrib,
4827 save->ldtr.limit, save->ldtr.base);
4828 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4829 "idtr:",
4830 save->idtr.selector, save->idtr.attrib,
4831 save->idtr.limit, save->idtr.base);
4832 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4833 "tr:",
4834 save->tr.selector, save->tr.attrib,
4835 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004836 pr_err("cpl: %d efer: %016llx\n",
4837 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004838 pr_err("%-15s %016llx %-13s %016llx\n",
4839 "cr0:", save->cr0, "cr2:", save->cr2);
4840 pr_err("%-15s %016llx %-13s %016llx\n",
4841 "cr3:", save->cr3, "cr4:", save->cr4);
4842 pr_err("%-15s %016llx %-13s %016llx\n",
4843 "dr6:", save->dr6, "dr7:", save->dr7);
4844 pr_err("%-15s %016llx %-13s %016llx\n",
4845 "rip:", save->rip, "rflags:", save->rflags);
4846 pr_err("%-15s %016llx %-13s %016llx\n",
4847 "rsp:", save->rsp, "rax:", save->rax);
4848 pr_err("%-15s %016llx %-13s %016llx\n",
4849 "star:", save->star, "lstar:", save->lstar);
4850 pr_err("%-15s %016llx %-13s %016llx\n",
4851 "cstar:", save->cstar, "sfmask:", save->sfmask);
4852 pr_err("%-15s %016llx %-13s %016llx\n",
4853 "kernel_gs_base:", save->kernel_gs_base,
4854 "sysenter_cs:", save->sysenter_cs);
4855 pr_err("%-15s %016llx %-13s %016llx\n",
4856 "sysenter_esp:", save->sysenter_esp,
4857 "sysenter_eip:", save->sysenter_eip);
4858 pr_err("%-15s %016llx %-13s %016llx\n",
4859 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4860 pr_err("%-15s %016llx %-13s %016llx\n",
4861 "br_from:", save->br_from, "br_to:", save->br_to);
4862 pr_err("%-15s %016llx %-13s %016llx\n",
4863 "excp_from:", save->last_excp_from,
4864 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004865}
4866
Avi Kivity586f9602010-11-18 13:09:54 +02004867static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4868{
4869 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4870
4871 *info1 = control->exit_info_1;
4872 *info2 = control->exit_info_2;
4873}
4874
Avi Kivity851ba692009-08-24 11:10:17 +03004875static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004876{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004877 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004878 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004879 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004880
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004881 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4882
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004883 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004884 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4885 if (npt_enabled)
4886 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004887
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004888 if (unlikely(svm->nested.exit_required)) {
4889 nested_svm_vmexit(svm);
4890 svm->nested.exit_required = false;
4891
4892 return 1;
4893 }
4894
Joerg Roedel20307532010-11-29 17:51:48 +01004895 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004896 int vmexit;
4897
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004898 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4899 svm->vmcb->control.exit_info_1,
4900 svm->vmcb->control.exit_info_2,
4901 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004902 svm->vmcb->control.exit_int_info_err,
4903 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004904
Joerg Roedel410e4d52009-08-07 11:49:44 +02004905 vmexit = nested_svm_exit_special(svm);
4906
4907 if (vmexit == NESTED_EXIT_CONTINUE)
4908 vmexit = nested_svm_exit_handled(svm);
4909
4910 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004911 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004912 }
4913
Joerg Roedela5c38322009-08-07 11:49:32 +02004914 svm_complete_interrupts(svm);
4915
Avi Kivity04d2cc72007-09-10 18:10:54 +03004916 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4917 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4918 kvm_run->fail_entry.hardware_entry_failure_reason
4919 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004920 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4921 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004922 return 0;
4923 }
4924
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004925 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004926 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004927 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4928 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004929 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004930 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004931 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004932 exit_code);
4933
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004934 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004935 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04004936 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03004937 kvm_queue_exception(vcpu, UD_VECTOR);
4938 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004939 }
4940
Avi Kivity851ba692009-08-24 11:10:17 +03004941 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004942}
4943
4944static void reload_tss(struct kvm_vcpu *vcpu)
4945{
4946 int cpu = raw_smp_processor_id();
4947
Tejun Heo0fe1e002009-10-29 22:34:14 +09004948 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4949 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004950 load_TR_desc();
4951}
4952
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004953static void pre_sev_run(struct vcpu_svm *svm, int cpu)
4954{
4955 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4956 int asid = sev_get_asid(svm->vcpu.kvm);
4957
4958 /* Assign the asid allocated with this SEV guest */
4959 svm->vmcb->control.asid = asid;
4960
4961 /*
4962 * Flush guest TLB:
4963 *
4964 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
4965 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
4966 */
4967 if (sd->sev_vmcbs[asid] == svm->vmcb &&
4968 svm->last_cpu == cpu)
4969 return;
4970
4971 svm->last_cpu = cpu;
4972 sd->sev_vmcbs[asid] = svm->vmcb;
4973 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4974 mark_dirty(svm->vmcb, VMCB_ASID);
4975}
4976
Rusty Russelle756fc62007-07-30 20:07:08 +10004977static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978{
4979 int cpu = raw_smp_processor_id();
4980
Tejun Heo0fe1e002009-10-29 22:34:14 +09004981 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004983 if (sev_guest(svm->vcpu.kvm))
4984 return pre_sev_run(svm, cpu);
4985
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03004986 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09004987 if (svm->asid_generation != sd->asid_generation)
4988 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004989}
4990
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004991static void svm_inject_nmi(struct kvm_vcpu *vcpu)
4992{
4993 struct vcpu_svm *svm = to_svm(vcpu);
4994
4995 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
4996 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004997 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004998 ++vcpu->stat.nmi_injections;
4999}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000
Eddie Dong85f455f2007-07-06 12:20:49 +03005001static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005002{
5003 struct vmcb_control_area *control;
5004
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005005 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10005006 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03005007 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005008 control->int_ctl &= ~V_INTR_PRIO_MASK;
5009 control->int_ctl |= V_IRQ_MASK |
5010 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01005011 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005012}
5013
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005014static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03005015{
5016 struct vcpu_svm *svm = to_svm(vcpu);
5017
Joerg Roedel2af91942009-08-07 11:49:28 +02005018 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01005019
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03005020 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
5021 ++vcpu->stat.irq_injections;
5022
Alexander Graf219b65d2009-06-15 15:21:25 +02005023 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
5024 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03005025}
5026
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005027static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
5028{
5029 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
5030}
5031
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005032static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
5033{
5034 struct vcpu_svm *svm = to_svm(vcpu);
5035
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005036 if (svm_nested_virtualize_tpr(vcpu) ||
5037 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005038 return;
5039
Radim Krčmář596f3142014-03-11 19:11:18 +01005040 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
5041
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005042 if (irr == -1)
5043 return;
5044
5045 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005046 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005047}
5048
Yang Zhang8d146952013-01-25 10:18:50 +08005049static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
5050{
5051 return;
5052}
5053
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005054static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005055{
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05005056 return avic && irqchip_split(vcpu->kvm);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005057}
5058
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005059static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
5060{
5061}
5062
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02005063static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005064{
5065}
5066
5067/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03005068static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5069{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005070 struct vcpu_svm *svm = to_svm(vcpu);
5071 struct vmcb *vmcb = svm->vmcb;
5072
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05005073 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005074 return;
5075
5076 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
5077 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005078}
5079
Andrey Smetanin63086302015-11-10 15:36:32 +03005080static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08005081{
5082 return;
5083}
5084
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005085static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
5086{
5087 kvm_lapic_set_irr(vec, vcpu->arch.apic);
5088 smp_mb__after_atomic();
5089
5090 if (avic_vcpu_is_running(vcpu))
5091 wrmsrl(SVM_AVIC_DOORBELL,
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05005092 kvm_cpu_get_apicid(vcpu->cpu));
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005093 else
5094 kvm_vcpu_wake_up(vcpu);
5095}
5096
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005097static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
5098{
5099 unsigned long flags;
5100 struct amd_svm_iommu_ir *cur;
5101
5102 spin_lock_irqsave(&svm->ir_list_lock, flags);
5103 list_for_each_entry(cur, &svm->ir_list, node) {
5104 if (cur->data != pi->ir_data)
5105 continue;
5106 list_del(&cur->node);
5107 kfree(cur);
5108 break;
5109 }
5110 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
5111}
5112
5113static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
5114{
5115 int ret = 0;
5116 unsigned long flags;
5117 struct amd_svm_iommu_ir *ir;
5118
5119 /**
5120 * In some cases, the existing irte is updaed and re-set,
5121 * so we need to check here if it's already been * added
5122 * to the ir_list.
5123 */
5124 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
5125 struct kvm *kvm = svm->vcpu.kvm;
5126 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
5127 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
5128 struct vcpu_svm *prev_svm;
5129
5130 if (!prev_vcpu) {
5131 ret = -EINVAL;
5132 goto out;
5133 }
5134
5135 prev_svm = to_svm(prev_vcpu);
5136 svm_ir_list_del(prev_svm, pi);
5137 }
5138
5139 /**
5140 * Allocating new amd_iommu_pi_data, which will get
5141 * add to the per-vcpu ir_list.
5142 */
5143 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
5144 if (!ir) {
5145 ret = -ENOMEM;
5146 goto out;
5147 }
5148 ir->data = pi->ir_data;
5149
5150 spin_lock_irqsave(&svm->ir_list_lock, flags);
5151 list_add(&ir->node, &svm->ir_list);
5152 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
5153out:
5154 return ret;
5155}
5156
5157/**
5158 * Note:
5159 * The HW cannot support posting multicast/broadcast
5160 * interrupts to a vCPU. So, we still use legacy interrupt
5161 * remapping for these kind of interrupts.
5162 *
5163 * For lowest-priority interrupts, we only support
5164 * those with single CPU as the destination, e.g. user
5165 * configures the interrupts via /proc/irq or uses
5166 * irqbalance to make the interrupts single-CPU.
5167 */
5168static int
5169get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
5170 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
5171{
5172 struct kvm_lapic_irq irq;
5173 struct kvm_vcpu *vcpu = NULL;
5174
5175 kvm_set_msi_irq(kvm, e, &irq);
5176
5177 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
5178 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
5179 __func__, irq.vector);
5180 return -1;
5181 }
5182
5183 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
5184 irq.vector);
5185 *svm = to_svm(vcpu);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005186 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005187 vcpu_info->vector = irq.vector;
5188
5189 return 0;
5190}
5191
5192/*
5193 * svm_update_pi_irte - set IRTE for Posted-Interrupts
5194 *
5195 * @kvm: kvm
5196 * @host_irq: host irq of the interrupt
5197 * @guest_irq: gsi of the interrupt
5198 * @set: set or unset PI
5199 * returns 0 on success, < 0 on failure
5200 */
5201static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
5202 uint32_t guest_irq, bool set)
5203{
5204 struct kvm_kernel_irq_routing_entry *e;
5205 struct kvm_irq_routing_table *irq_rt;
5206 int idx, ret = -EINVAL;
5207
5208 if (!kvm_arch_has_assigned_device(kvm) ||
5209 !irq_remapping_cap(IRQ_POSTING_CAP))
5210 return 0;
5211
5212 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
5213 __func__, host_irq, guest_irq, set);
5214
5215 idx = srcu_read_lock(&kvm->irq_srcu);
5216 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
5217 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
5218
5219 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
5220 struct vcpu_data vcpu_info;
5221 struct vcpu_svm *svm = NULL;
5222
5223 if (e->type != KVM_IRQ_ROUTING_MSI)
5224 continue;
5225
5226 /**
5227 * Here, we setup with legacy mode in the following cases:
5228 * 1. When cannot target interrupt to a specific vcpu.
5229 * 2. Unsetting posted interrupt.
5230 * 3. APIC virtialization is disabled for the vcpu.
5231 */
5232 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
5233 kvm_vcpu_apicv_active(&svm->vcpu)) {
5234 struct amd_iommu_pi_data pi;
5235
5236 /* Try to enable guest_mode in IRTE */
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005237 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
5238 AVIC_HPA_MASK);
Sean Christopherson81811c12018-03-20 12:17:21 -07005239 pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005240 svm->vcpu.vcpu_id);
5241 pi.is_guest_mode = true;
5242 pi.vcpu_data = &vcpu_info;
5243 ret = irq_set_vcpu_affinity(host_irq, &pi);
5244
5245 /**
5246 * Here, we successfully setting up vcpu affinity in
5247 * IOMMU guest mode. Now, we need to store the posted
5248 * interrupt information in a per-vcpu ir_list so that
5249 * we can reference to them directly when we update vcpu
5250 * scheduling information in IOMMU irte.
5251 */
5252 if (!ret && pi.is_guest_mode)
5253 svm_ir_list_add(svm, &pi);
5254 } else {
5255 /* Use legacy mode in IRTE */
5256 struct amd_iommu_pi_data pi;
5257
5258 /**
5259 * Here, pi is used to:
5260 * - Tell IOMMU to use legacy mode for this interrupt.
5261 * - Retrieve ga_tag of prior interrupt remapping data.
5262 */
5263 pi.is_guest_mode = false;
5264 ret = irq_set_vcpu_affinity(host_irq, &pi);
5265
5266 /**
5267 * Check if the posted interrupt was previously
5268 * setup with the guest_mode by checking if the ga_tag
5269 * was cached. If so, we need to clean up the per-vcpu
5270 * ir_list.
5271 */
5272 if (!ret && pi.prev_ga_tag) {
5273 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
5274 struct kvm_vcpu *vcpu;
5275
5276 vcpu = kvm_get_vcpu_by_id(kvm, id);
5277 if (vcpu)
5278 svm_ir_list_del(to_svm(vcpu), &pi);
5279 }
5280 }
5281
5282 if (!ret && svm) {
hu huajun2698d822018-04-11 15:16:40 +08005283 trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id,
5284 e->gsi, vcpu_info.vector,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005285 vcpu_info.pi_desc_addr, set);
5286 }
5287
5288 if (ret < 0) {
5289 pr_err("%s: failed to update PI IRTE\n", __func__);
5290 goto out;
5291 }
5292 }
5293
5294 ret = 0;
5295out:
5296 srcu_read_unlock(&kvm->irq_srcu, idx);
5297 return ret;
5298}
5299
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005300static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005301{
5302 struct vcpu_svm *svm = to_svm(vcpu);
5303 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02005304 int ret;
5305 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
5306 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
5307 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
5308
5309 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005310}
5311
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005312static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
5313{
5314 struct vcpu_svm *svm = to_svm(vcpu);
5315
5316 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
5317}
5318
5319static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5320{
5321 struct vcpu_svm *svm = to_svm(vcpu);
5322
5323 if (masked) {
5324 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005325 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005326 } else {
5327 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005328 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005329 }
5330}
5331
Gleb Natapov78646122009-03-23 12:12:11 +02005332static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
5333{
5334 struct vcpu_svm *svm = to_svm(vcpu);
5335 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005336 int ret;
5337
5338 if (!gif_set(svm) ||
5339 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
5340 return 0;
5341
Avi Kivityf6e78472010-08-02 15:30:20 +03005342 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005343
Joerg Roedel20307532010-11-29 17:51:48 +01005344 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005345 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
5346
5347 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02005348}
5349
Jan Kiszkac9a79532014-03-07 20:03:15 +01005350static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03005351{
Alexander Graf219b65d2009-06-15 15:21:25 +02005352 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02005353
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005354 if (kvm_vcpu_apicv_active(vcpu))
5355 return;
5356
Joerg Roedele0231712010-02-24 18:59:10 +01005357 /*
5358 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
5359 * 1, because that's a separate STGI/VMRUN intercept. The next time we
5360 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005361 * we'll get the vintr intercept. However, if the vGIF feature is
5362 * enabled, the STGI interception will not occur. Enable the irq
5363 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01005364 */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005365 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02005366 svm_set_vintr(svm);
5367 svm_inject_irq(svm, 0x0);
5368 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005369}
5370
Jan Kiszkac9a79532014-03-07 20:03:15 +01005371static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005372{
Avi Kivity04d2cc72007-09-10 18:10:54 +03005373 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005374
Gleb Natapov44c11432009-05-11 13:35:52 +03005375 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
5376 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01005377 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03005378
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005379 if (!gif_set(svm)) {
5380 if (vgif_enabled(svm))
5381 set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005382 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005383 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005384
5385 if (svm->nested.exit_required)
5386 return; /* we're not going to run the guest yet */
5387
Joerg Roedele0231712010-02-24 18:59:10 +01005388 /*
5389 * Something prevents NMI from been injected. Single step over possible
5390 * problem (IRET or exception injection or interrupt shadow)
5391 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02005392 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02005393 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03005394 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03005395}
5396
Izik Eiduscbc94022007-10-25 00:29:55 +02005397static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
5398{
5399 return 0;
5400}
5401
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07005402static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
5403{
5404 return 0;
5405}
5406
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005407static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Avi Kivityd9e368d2007-06-07 19:18:30 +03005408{
Joerg Roedel38e5e922010-12-03 15:25:16 +01005409 struct vcpu_svm *svm = to_svm(vcpu);
5410
5411 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
5412 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5413 else
5414 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03005415}
5416
Avi Kivity04d2cc72007-09-10 18:10:54 +03005417static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
5418{
5419}
5420
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005421static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
5422{
5423 struct vcpu_svm *svm = to_svm(vcpu);
5424
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005425 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005426 return;
5427
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005428 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005429 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03005430 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005431 }
5432}
5433
Joerg Roedel649d6862008-04-16 16:51:15 +02005434static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
5435{
5436 struct vcpu_svm *svm = to_svm(vcpu);
5437 u64 cr8;
5438
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005439 if (svm_nested_virtualize_tpr(vcpu) ||
5440 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005441 return;
5442
Joerg Roedel649d6862008-04-16 16:51:15 +02005443 cr8 = kvm_get_cr8(vcpu);
5444 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
5445 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
5446}
5447
Gleb Natapov9222be12009-04-23 17:14:37 +03005448static void svm_complete_interrupts(struct vcpu_svm *svm)
5449{
5450 u8 vector;
5451 int type;
5452 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01005453 unsigned int3_injected = svm->int3_injected;
5454
5455 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005456
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02005457 /*
5458 * If we've made progress since setting HF_IRET_MASK, we've
5459 * executed an IRET and can allow NMI injection.
5460 */
5461 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
5462 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03005463 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03005464 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5465 }
Gleb Natapov44c11432009-05-11 13:35:52 +03005466
Gleb Natapov9222be12009-04-23 17:14:37 +03005467 svm->vcpu.arch.nmi_injected = false;
5468 kvm_clear_exception_queue(&svm->vcpu);
5469 kvm_clear_interrupt_queue(&svm->vcpu);
5470
5471 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
5472 return;
5473
Avi Kivity3842d132010-07-27 12:30:24 +03005474 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5475
Gleb Natapov9222be12009-04-23 17:14:37 +03005476 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
5477 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
5478
5479 switch (type) {
5480 case SVM_EXITINTINFO_TYPE_NMI:
5481 svm->vcpu.arch.nmi_injected = true;
5482 break;
5483 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01005484 /*
5485 * In case of software exceptions, do not reinject the vector,
5486 * but re-execute the instruction instead. Rewind RIP first
5487 * if we emulated INT3 before.
5488 */
5489 if (kvm_exception_is_soft(vector)) {
5490 if (vector == BP_VECTOR && int3_injected &&
5491 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
5492 kvm_rip_write(&svm->vcpu,
5493 kvm_rip_read(&svm->vcpu) -
5494 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02005495 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01005496 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005497 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
5498 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005499 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03005500
5501 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005502 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03005503 break;
5504 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005505 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03005506 break;
5507 default:
5508 break;
5509 }
5510}
5511
Avi Kivityb463a6f2010-07-20 15:06:17 +03005512static void svm_cancel_injection(struct kvm_vcpu *vcpu)
5513{
5514 struct vcpu_svm *svm = to_svm(vcpu);
5515 struct vmcb_control_area *control = &svm->vmcb->control;
5516
5517 control->exit_int_info = control->event_inj;
5518 control->exit_int_info_err = control->event_inj_err;
5519 control->event_inj = 0;
5520 svm_complete_interrupts(svm);
5521}
5522
Avi Kivity851ba692009-08-24 11:10:17 +03005523static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005524{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005525 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03005526
Joerg Roedel2041a062010-04-22 12:33:08 +02005527 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5528 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5529 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5530
Joerg Roedelcd3ff652009-10-09 16:08:26 +02005531 /*
5532 * A vmexit emulation is required before the vcpu can be executed
5533 * again.
5534 */
5535 if (unlikely(svm->nested.exit_required))
5536 return;
5537
Ladi Proseka12713c2017-06-21 09:07:00 +02005538 /*
5539 * Disable singlestep if we're injecting an interrupt/exception.
5540 * We don't want our modified rflags to be pushed on the stack where
5541 * we might not be able to easily reset them if we disabled NMI
5542 * singlestep later.
5543 */
5544 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5545 /*
5546 * Event injection happens before external interrupts cause a
5547 * vmexit and interrupts are disabled here, so smp_send_reschedule
5548 * is enough to force an immediate vmexit.
5549 */
5550 disable_nmi_singlestep(svm);
5551 smp_send_reschedule(vcpu->cpu);
5552 }
5553
Rusty Russelle756fc62007-07-30 20:07:08 +10005554 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005555
Joerg Roedel649d6862008-04-16 16:51:15 +02005556 sync_lapic_to_cr8(vcpu);
5557
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02005558 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005559
Avi Kivity04d2cc72007-09-10 18:10:54 +03005560 clgi();
5561
5562 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08005563
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005564 /*
5565 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
5566 * it's non-zero. Since vmentry is serialising on affected CPUs, there
5567 * is no need to worry about the conditional branch over the wrmsr
5568 * being speculatively taken.
5569 */
5570 if (svm->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005571 native_wrmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005572
Avi Kivity6aa8b732006-12-10 02:21:36 -08005573 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03005574 "push %%" _ASM_BP "; \n\t"
5575 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5576 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5577 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5578 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5579 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5580 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005581#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005582 "mov %c[r8](%[svm]), %%r8 \n\t"
5583 "mov %c[r9](%[svm]), %%r9 \n\t"
5584 "mov %c[r10](%[svm]), %%r10 \n\t"
5585 "mov %c[r11](%[svm]), %%r11 \n\t"
5586 "mov %c[r12](%[svm]), %%r12 \n\t"
5587 "mov %c[r13](%[svm]), %%r13 \n\t"
5588 "mov %c[r14](%[svm]), %%r14 \n\t"
5589 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005590#endif
5591
Avi Kivity6aa8b732006-12-10 02:21:36 -08005592 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03005593 "push %%" _ASM_AX " \n\t"
5594 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03005595 __ex(SVM_VMLOAD) "\n\t"
5596 __ex(SVM_VMRUN) "\n\t"
5597 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005598 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005599
5600 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03005601 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5602 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5603 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5604 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5605 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5606 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005607#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005608 "mov %%r8, %c[r8](%[svm]) \n\t"
5609 "mov %%r9, %c[r9](%[svm]) \n\t"
5610 "mov %%r10, %c[r10](%[svm]) \n\t"
5611 "mov %%r11, %c[r11](%[svm]) \n\t"
5612 "mov %%r12, %c[r12](%[svm]) \n\t"
5613 "mov %%r13, %c[r13](%[svm]) \n\t"
5614 "mov %%r14, %c[r14](%[svm]) \n\t"
5615 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005616#endif
Jim Mattson0cb5b302018-01-03 14:31:38 -08005617 /*
5618 * Clear host registers marked as clobbered to prevent
5619 * speculative use.
5620 */
5621 "xor %%" _ASM_BX ", %%" _ASM_BX " \n\t"
5622 "xor %%" _ASM_CX ", %%" _ASM_CX " \n\t"
5623 "xor %%" _ASM_DX ", %%" _ASM_DX " \n\t"
5624 "xor %%" _ASM_SI ", %%" _ASM_SI " \n\t"
5625 "xor %%" _ASM_DI ", %%" _ASM_DI " \n\t"
5626#ifdef CONFIG_X86_64
5627 "xor %%r8, %%r8 \n\t"
5628 "xor %%r9, %%r9 \n\t"
5629 "xor %%r10, %%r10 \n\t"
5630 "xor %%r11, %%r11 \n\t"
5631 "xor %%r12, %%r12 \n\t"
5632 "xor %%r13, %%r13 \n\t"
5633 "xor %%r14, %%r14 \n\t"
5634 "xor %%r15, %%r15 \n\t"
5635#endif
Avi Kivity74547662012-09-16 15:10:59 +03005636 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08005637 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005638 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08005639 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005640 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5641 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5642 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5643 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5644 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5645 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005646#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005647 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5648 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5649 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5650 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5651 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5652 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5653 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5654 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005655#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02005656 : "cc", "memory"
5657#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03005658 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005659 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03005660#else
5661 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005662#endif
5663 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08005664
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005665 /*
5666 * We do not use IBRS in the kernel. If this vCPU has used the
5667 * SPEC_CTRL MSR it may have left it on; save the value and
5668 * turn it off. This is much more efficient than blindly adding
5669 * it to the atomic save/restore list. Especially as the former
5670 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
5671 *
5672 * For non-nested case:
5673 * If the L01 MSR bitmap does not intercept the MSR, then we need to
5674 * save it.
5675 *
5676 * For nested case:
5677 * If the L02 MSR bitmap does not intercept the MSR, then we need to
5678 * save it.
5679 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01005680 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005681 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005682
5683 if (svm->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005684 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005685
David Woodhouse117cc7a2018-01-12 11:11:27 +00005686 /* Eliminate branch target predictions from guest mode */
5687 vmexit_fill_RSB();
5688
Avi Kivity82ca2d12010-10-21 12:20:34 +02005689#ifdef CONFIG_X86_64
5690 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5691#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02005692 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02005693#ifndef CONFIG_X86_32_LAZY_GS
5694 loadsegment(gs, svm->host.gs);
5695#endif
Avi Kivity9581d442010-10-19 16:46:55 +02005696#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005697
5698 reload_tss(vcpu);
5699
Avi Kivity56ba47d2007-11-07 17:14:18 +02005700 local_irq_disable();
5701
Avi Kivity13c34e02010-10-21 12:20:31 +02005702 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5703 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5704 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5705 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5706
Joerg Roedel3781c012011-01-14 16:45:02 +01005707 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Andi Kleendd60d212017-07-25 17:20:32 -07005708 kvm_before_interrupt(&svm->vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01005709
5710 stgi();
5711
5712 /* Any pending NMI will happen here */
5713
5714 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Andi Kleendd60d212017-07-25 17:20:32 -07005715 kvm_after_interrupt(&svm->vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01005716
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005717 sync_cr8_to_lapic(vcpu);
5718
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005719 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005720
Joerg Roedel38e5e922010-12-03 15:25:16 +01005721 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5722
Gleb Natapov631bc482010-10-14 11:22:52 +02005723 /* if exit due to PF check for async PF */
5724 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005725 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
Gleb Natapov631bc482010-10-14 11:22:52 +02005726
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005727 if (npt_enabled) {
5728 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5729 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5730 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02005731
5732 /*
5733 * We need to handle MC intercepts here before the vcpu has a chance to
5734 * change the physical cpu
5735 */
5736 if (unlikely(svm->vmcb->control.exit_code ==
5737 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5738 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01005739
5740 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005741}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005742STACK_FRAME_NON_STANDARD(svm_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005743
Avi Kivity6aa8b732006-12-10 02:21:36 -08005744static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5745{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005746 struct vcpu_svm *svm = to_svm(vcpu);
5747
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005748 svm->vmcb->save.cr3 = __sme_set(root);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005749 mark_dirty(svm->vmcb, VMCB_CR);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005750 svm_flush_tlb(vcpu, true);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005751}
5752
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005753static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5754{
5755 struct vcpu_svm *svm = to_svm(vcpu);
5756
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005757 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01005758 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005759
5760 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02005761 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005762 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005763
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005764 svm_flush_tlb(vcpu, true);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005765}
5766
Avi Kivity6aa8b732006-12-10 02:21:36 -08005767static int is_disabled(void)
5768{
Joerg Roedel6031a612007-06-22 12:29:50 +03005769 u64 vm_cr;
5770
5771 rdmsrl(MSR_VM_CR, vm_cr);
5772 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5773 return 1;
5774
Avi Kivity6aa8b732006-12-10 02:21:36 -08005775 return 0;
5776}
5777
Ingo Molnar102d8322007-02-19 14:37:47 +02005778static void
5779svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5780{
5781 /*
5782 * Patch in the VMMCALL instruction:
5783 */
5784 hypercall[0] = 0x0f;
5785 hypercall[1] = 0x01;
5786 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02005787}
5788
Yang, Sheng002c7f72007-07-31 14:23:01 +03005789static void svm_check_processor_compat(void *rtn)
5790{
5791 *(int *)rtn = 0;
5792}
5793
Avi Kivity774ead32007-12-26 13:57:04 +02005794static bool svm_cpu_has_accelerated_tpr(void)
5795{
5796 return false;
5797}
5798
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005799static bool svm_has_high_real_mode_segbase(void)
5800{
5801 return true;
5802}
5803
Paolo Bonzinifc07e762015-10-01 13:20:22 +02005804static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5805{
5806 return 0;
5807}
5808
Sheng Yang0e851882009-12-18 16:48:46 +08005809static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5810{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005811 struct vcpu_svm *svm = to_svm(vcpu);
5812
5813 /* Update nrips enabled cache */
Radim Krčmářd6321d42017-08-05 00:12:49 +02005814 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005815
5816 if (!kvm_vcpu_apicv_active(vcpu))
5817 return;
5818
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02005819 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08005820}
5821
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005822static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5823{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005824 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005825 case 0x1:
5826 if (avic)
5827 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5828 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02005829 case 0x80000001:
5830 if (nested)
5831 entry->ecx |= (1 << 2); /* Set SVM bit */
5832 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005833 case 0x8000000A:
5834 entry->eax = 1; /* SVM revision 1 */
5835 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5836 ASID emulation to nested SVM */
5837 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02005838 entry->edx = 0; /* Per default do not support any
5839 additional features */
5840
5841 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02005842 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02005843 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005844
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005845 /* Support NPT for the guest if enabled */
5846 if (npt_enabled)
5847 entry->edx |= SVM_FEATURE_NPT;
5848
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005849 break;
Brijesh Singh8765d752017-12-04 10:57:25 -06005850 case 0x8000001F:
5851 /* Support memory encryption cpuid if host supports it */
5852 if (boot_cpu_has(X86_FEATURE_SEV))
5853 cpuid(0x8000001f, &entry->eax, &entry->ebx,
5854 &entry->ecx, &entry->edx);
5855
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005856 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005857}
5858
Sheng Yang17cc3932010-01-05 19:02:27 +08005859static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005860{
Sheng Yang17cc3932010-01-05 19:02:27 +08005861 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005862}
5863
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005864static bool svm_rdtscp_supported(void)
5865{
Paolo Bonzini46896c72015-11-12 14:49:16 +01005866 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005867}
5868
Mao, Junjiead756a12012-07-02 01:18:48 +00005869static bool svm_invpcid_supported(void)
5870{
5871 return false;
5872}
5873
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005874static bool svm_mpx_supported(void)
5875{
5876 return false;
5877}
5878
Wanpeng Li55412b22014-12-02 19:21:30 +08005879static bool svm_xsaves_supported(void)
5880{
5881 return false;
5882}
5883
Paolo Bonzini66336ca2016-07-12 10:36:41 +02005884static bool svm_umip_emulated(void)
5885{
5886 return false;
5887}
5888
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005889static bool svm_has_wbinvd_exit(void)
5890{
5891 return true;
5892}
5893
Joerg Roedel80612522011-04-04 12:39:33 +02005894#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005895 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005896#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005897 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005898#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005899 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005900
Mathias Krause09941fb2012-08-30 01:30:20 +02005901static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005902 u32 exit_code;
5903 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005904} x86_intercept_map[] = {
5905 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
5906 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
5907 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
5908 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
5909 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02005910 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
5911 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02005912 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
5913 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
5914 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
5915 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
5916 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
5917 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
5918 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
5919 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02005920 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
5921 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
5922 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
5923 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
5924 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
5925 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
5926 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
5927 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005928 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
5929 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
5930 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02005931 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
5932 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
5933 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
5934 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
5935 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
5936 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
5937 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
5938 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
5939 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02005940 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
5941 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
5942 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
5943 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
5944 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
5945 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
5946 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02005947 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
5948 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
5949 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
5950 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005951};
5952
Joerg Roedel80612522011-04-04 12:39:33 +02005953#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005954#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005955#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005956
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005957static int svm_check_intercept(struct kvm_vcpu *vcpu,
5958 struct x86_instruction_info *info,
5959 enum x86_intercept_stage stage)
5960{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005961 struct vcpu_svm *svm = to_svm(vcpu);
5962 int vmexit, ret = X86EMUL_CONTINUE;
5963 struct __x86_intercept icpt_info;
5964 struct vmcb *vmcb = svm->vmcb;
5965
5966 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
5967 goto out;
5968
5969 icpt_info = x86_intercept_map[info->intercept];
5970
Avi Kivity40e19b52011-04-21 12:35:41 +03005971 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005972 goto out;
5973
5974 switch (icpt_info.exit_code) {
5975 case SVM_EXIT_READ_CR0:
5976 if (info->intercept == x86_intercept_cr_read)
5977 icpt_info.exit_code += info->modrm_reg;
5978 break;
5979 case SVM_EXIT_WRITE_CR0: {
5980 unsigned long cr0, val;
5981 u64 intercept;
5982
5983 if (info->intercept == x86_intercept_cr_write)
5984 icpt_info.exit_code += info->modrm_reg;
5985
Jan Kiszka62baf442014-06-29 21:55:53 +02005986 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
5987 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005988 break;
5989
5990 intercept = svm->nested.intercept;
5991
5992 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
5993 break;
5994
5995 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
5996 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
5997
5998 if (info->intercept == x86_intercept_lmsw) {
5999 cr0 &= 0xfUL;
6000 val &= 0xfUL;
6001 /* lmsw can't clear PE - catch this here */
6002 if (cr0 & X86_CR0_PE)
6003 val |= X86_CR0_PE;
6004 }
6005
6006 if (cr0 ^ val)
6007 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
6008
6009 break;
6010 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02006011 case SVM_EXIT_READ_DR0:
6012 case SVM_EXIT_WRITE_DR0:
6013 icpt_info.exit_code += info->modrm_reg;
6014 break;
Joerg Roedel80612522011-04-04 12:39:33 +02006015 case SVM_EXIT_MSR:
6016 if (info->intercept == x86_intercept_wrmsr)
6017 vmcb->control.exit_info_1 = 1;
6018 else
6019 vmcb->control.exit_info_1 = 0;
6020 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02006021 case SVM_EXIT_PAUSE:
6022 /*
6023 * We get this for NOP only, but pause
6024 * is rep not, check this here
6025 */
6026 if (info->rep_prefix != REPE_PREFIX)
6027 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02006028 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02006029 case SVM_EXIT_IOIO: {
6030 u64 exit_info;
6031 u32 bytes;
6032
Joerg Roedelf6511932011-04-04 12:39:35 +02006033 if (info->intercept == x86_intercept_in ||
6034 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02006035 exit_info = ((info->src_val & 0xffff) << 16) |
6036 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02006037 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02006038 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02006039 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02006040 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02006041 }
6042
6043 if (info->intercept == x86_intercept_outs ||
6044 info->intercept == x86_intercept_ins)
6045 exit_info |= SVM_IOIO_STR_MASK;
6046
6047 if (info->rep_prefix)
6048 exit_info |= SVM_IOIO_REP_MASK;
6049
6050 bytes = min(bytes, 4u);
6051
6052 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
6053
6054 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
6055
6056 vmcb->control.exit_info_1 = exit_info;
6057 vmcb->control.exit_info_2 = info->next_rip;
6058
6059 break;
6060 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006061 default:
6062 break;
6063 }
6064
Bandan Dasf1047652015-06-11 02:05:33 -04006065 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
6066 if (static_cpu_has(X86_FEATURE_NRIPS))
6067 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02006068 vmcb->control.exit_code = icpt_info.exit_code;
6069 vmexit = nested_svm_exit_handled(svm);
6070
6071 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
6072 : X86EMUL_CONTINUE;
6073
6074out:
6075 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006076}
6077
Yang Zhanga547c6d2013-04-11 19:25:10 +08006078static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
6079{
6080 local_irq_enable();
Paolo Bonzinif2485b32016-06-15 15:23:11 +02006081 /*
6082 * We must have an instruction with interrupts enabled, so
6083 * the timer interrupt isn't delayed by the interrupt shadow.
6084 */
6085 asm("nop");
6086 local_irq_disable();
Yang Zhanga547c6d2013-04-11 19:25:10 +08006087}
6088
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006089static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
6090{
Babu Moger8566ac82018-03-16 16:37:26 -04006091 if (pause_filter_thresh)
6092 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006093}
6094
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05006095static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
6096{
6097 if (avic_handle_apic_id_update(vcpu) != 0)
6098 return;
6099 if (avic_handle_dfr_update(vcpu) != 0)
6100 return;
6101 avic_handle_ldr_update(vcpu);
6102}
6103
Borislav Petkov74f16902017-03-26 23:51:24 +02006104static void svm_setup_mce(struct kvm_vcpu *vcpu)
6105{
6106 /* [63:9] are reserved. */
6107 vcpu->arch.mcg_cap &= 0x1ff;
6108}
6109
Ladi Prosek72d7b372017-10-11 16:54:41 +02006110static int svm_smi_allowed(struct kvm_vcpu *vcpu)
6111{
Ladi Prosek05cade72017-10-11 16:54:45 +02006112 struct vcpu_svm *svm = to_svm(vcpu);
6113
6114 /* Per APM Vol.2 15.22.2 "Response to SMI" */
6115 if (!gif_set(svm))
6116 return 0;
6117
6118 if (is_guest_mode(&svm->vcpu) &&
6119 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
6120 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
6121 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
6122 svm->nested.exit_required = true;
6123 return 0;
6124 }
6125
Ladi Prosek72d7b372017-10-11 16:54:41 +02006126 return 1;
6127}
6128
Ladi Prosek0234bf82017-10-11 16:54:40 +02006129static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
6130{
Ladi Prosek05cade72017-10-11 16:54:45 +02006131 struct vcpu_svm *svm = to_svm(vcpu);
6132 int ret;
6133
6134 if (is_guest_mode(vcpu)) {
6135 /* FED8h - SVM Guest */
6136 put_smstate(u64, smstate, 0x7ed8, 1);
6137 /* FEE0h - SVM Guest VMCB Physical Address */
6138 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
6139
6140 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
6141 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
6142 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
6143
6144 ret = nested_svm_vmexit(svm);
6145 if (ret)
6146 return ret;
6147 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02006148 return 0;
6149}
6150
6151static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
6152{
Ladi Prosek05cade72017-10-11 16:54:45 +02006153 struct vcpu_svm *svm = to_svm(vcpu);
6154 struct vmcb *nested_vmcb;
6155 struct page *page;
6156 struct {
6157 u64 guest;
6158 u64 vmcb;
6159 } svm_state_save;
6160 int ret;
6161
6162 ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save,
6163 sizeof(svm_state_save));
6164 if (ret)
6165 return ret;
6166
6167 if (svm_state_save.guest) {
6168 vcpu->arch.hflags &= ~HF_SMM_MASK;
6169 nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page);
6170 if (nested_vmcb)
6171 enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page);
6172 else
6173 ret = 1;
6174 vcpu->arch.hflags |= HF_SMM_MASK;
6175 }
6176 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02006177}
6178
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006179static int enable_smi_window(struct kvm_vcpu *vcpu)
6180{
6181 struct vcpu_svm *svm = to_svm(vcpu);
6182
6183 if (!gif_set(svm)) {
6184 if (vgif_enabled(svm))
6185 set_intercept(svm, INTERCEPT_STGI);
6186 /* STGI will cause a vm exit */
6187 return 1;
6188 }
6189 return 0;
6190}
6191
Brijesh Singh1654efc2017-12-04 10:57:34 -06006192static int sev_asid_new(void)
6193{
6194 int pos;
6195
6196 /*
6197 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
6198 */
6199 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
6200 if (pos >= max_sev_asid)
6201 return -EBUSY;
6202
6203 set_bit(pos, sev_asid_bitmap);
6204 return pos + 1;
6205}
6206
6207static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
6208{
Sean Christopherson81811c12018-03-20 12:17:21 -07006209 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006210 int asid, ret;
6211
6212 ret = -EBUSY;
6213 asid = sev_asid_new();
6214 if (asid < 0)
6215 return ret;
6216
6217 ret = sev_platform_init(&argp->error);
6218 if (ret)
6219 goto e_free;
6220
6221 sev->active = true;
6222 sev->asid = asid;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006223 INIT_LIST_HEAD(&sev->regions_list);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006224
6225 return 0;
6226
6227e_free:
6228 __sev_asid_free(asid);
6229 return ret;
6230}
6231
Brijesh Singh59414c92017-12-04 10:57:35 -06006232static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
6233{
6234 struct sev_data_activate *data;
6235 int asid = sev_get_asid(kvm);
6236 int ret;
6237
6238 wbinvd_on_all_cpus();
6239
6240 ret = sev_guest_df_flush(error);
6241 if (ret)
6242 return ret;
6243
6244 data = kzalloc(sizeof(*data), GFP_KERNEL);
6245 if (!data)
6246 return -ENOMEM;
6247
6248 /* activate ASID on the given handle */
6249 data->handle = handle;
6250 data->asid = asid;
6251 ret = sev_guest_activate(data, error);
6252 kfree(data);
6253
6254 return ret;
6255}
6256
Brijesh Singh89c50582017-12-04 10:57:35 -06006257static int __sev_issue_cmd(int fd, int id, void *data, int *error)
Brijesh Singh59414c92017-12-04 10:57:35 -06006258{
6259 struct fd f;
6260 int ret;
6261
6262 f = fdget(fd);
6263 if (!f.file)
6264 return -EBADF;
6265
6266 ret = sev_issue_cmd_external_user(f.file, id, data, error);
6267
6268 fdput(f);
6269 return ret;
6270}
6271
Brijesh Singh89c50582017-12-04 10:57:35 -06006272static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
6273{
Sean Christopherson81811c12018-03-20 12:17:21 -07006274 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06006275
6276 return __sev_issue_cmd(sev->fd, id, data, error);
6277}
6278
Brijesh Singh59414c92017-12-04 10:57:35 -06006279static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
6280{
Sean Christopherson81811c12018-03-20 12:17:21 -07006281 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh59414c92017-12-04 10:57:35 -06006282 struct sev_data_launch_start *start;
6283 struct kvm_sev_launch_start params;
6284 void *dh_blob, *session_blob;
6285 int *error = &argp->error;
6286 int ret;
6287
6288 if (!sev_guest(kvm))
6289 return -ENOTTY;
6290
6291 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6292 return -EFAULT;
6293
6294 start = kzalloc(sizeof(*start), GFP_KERNEL);
6295 if (!start)
6296 return -ENOMEM;
6297
6298 dh_blob = NULL;
6299 if (params.dh_uaddr) {
6300 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
6301 if (IS_ERR(dh_blob)) {
6302 ret = PTR_ERR(dh_blob);
6303 goto e_free;
6304 }
6305
6306 start->dh_cert_address = __sme_set(__pa(dh_blob));
6307 start->dh_cert_len = params.dh_len;
6308 }
6309
6310 session_blob = NULL;
6311 if (params.session_uaddr) {
6312 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
6313 if (IS_ERR(session_blob)) {
6314 ret = PTR_ERR(session_blob);
6315 goto e_free_dh;
6316 }
6317
6318 start->session_address = __sme_set(__pa(session_blob));
6319 start->session_len = params.session_len;
6320 }
6321
6322 start->handle = params.handle;
6323 start->policy = params.policy;
6324
6325 /* create memory encryption context */
Brijesh Singh89c50582017-12-04 10:57:35 -06006326 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
Brijesh Singh59414c92017-12-04 10:57:35 -06006327 if (ret)
6328 goto e_free_session;
6329
6330 /* Bind ASID to this guest */
6331 ret = sev_bind_asid(kvm, start->handle, error);
6332 if (ret)
6333 goto e_free_session;
6334
6335 /* return handle to userspace */
6336 params.handle = start->handle;
6337 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
6338 sev_unbind_asid(kvm, start->handle);
6339 ret = -EFAULT;
6340 goto e_free_session;
6341 }
6342
6343 sev->handle = start->handle;
6344 sev->fd = argp->sev_fd;
6345
6346e_free_session:
6347 kfree(session_blob);
6348e_free_dh:
6349 kfree(dh_blob);
6350e_free:
6351 kfree(start);
6352 return ret;
6353}
6354
Brijesh Singh89c50582017-12-04 10:57:35 -06006355static int get_num_contig_pages(int idx, struct page **inpages,
6356 unsigned long npages)
6357{
6358 unsigned long paddr, next_paddr;
6359 int i = idx + 1, pages = 1;
6360
6361 /* find the number of contiguous pages starting from idx */
6362 paddr = __sme_page_pa(inpages[idx]);
6363 while (i < npages) {
6364 next_paddr = __sme_page_pa(inpages[i++]);
6365 if ((paddr + PAGE_SIZE) == next_paddr) {
6366 pages++;
6367 paddr = next_paddr;
6368 continue;
6369 }
6370 break;
6371 }
6372
6373 return pages;
6374}
6375
6376static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
6377{
6378 unsigned long vaddr, vaddr_end, next_vaddr, npages, size;
Sean Christopherson81811c12018-03-20 12:17:21 -07006379 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06006380 struct kvm_sev_launch_update_data params;
6381 struct sev_data_launch_update_data *data;
6382 struct page **inpages;
6383 int i, ret, pages;
6384
6385 if (!sev_guest(kvm))
6386 return -ENOTTY;
6387
6388 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6389 return -EFAULT;
6390
6391 data = kzalloc(sizeof(*data), GFP_KERNEL);
6392 if (!data)
6393 return -ENOMEM;
6394
6395 vaddr = params.uaddr;
6396 size = params.len;
6397 vaddr_end = vaddr + size;
6398
6399 /* Lock the user memory. */
6400 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
6401 if (!inpages) {
6402 ret = -ENOMEM;
6403 goto e_free;
6404 }
6405
6406 /*
6407 * The LAUNCH_UPDATE command will perform in-place encryption of the
6408 * memory content (i.e it will write the same memory region with C=1).
6409 * It's possible that the cache may contain the data with C=0, i.e.,
6410 * unencrypted so invalidate it first.
6411 */
6412 sev_clflush_pages(inpages, npages);
6413
6414 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
6415 int offset, len;
6416
6417 /*
6418 * If the user buffer is not page-aligned, calculate the offset
6419 * within the page.
6420 */
6421 offset = vaddr & (PAGE_SIZE - 1);
6422
6423 /* Calculate the number of pages that can be encrypted in one go. */
6424 pages = get_num_contig_pages(i, inpages, npages);
6425
6426 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
6427
6428 data->handle = sev->handle;
6429 data->len = len;
6430 data->address = __sme_page_pa(inpages[i]) + offset;
6431 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
6432 if (ret)
6433 goto e_unpin;
6434
6435 size -= len;
6436 next_vaddr = vaddr + len;
6437 }
6438
6439e_unpin:
6440 /* content of memory is updated, mark pages dirty */
6441 for (i = 0; i < npages; i++) {
6442 set_page_dirty_lock(inpages[i]);
6443 mark_page_accessed(inpages[i]);
6444 }
6445 /* unlock the user pages */
6446 sev_unpin_memory(kvm, inpages, npages);
6447e_free:
6448 kfree(data);
6449 return ret;
6450}
6451
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006452static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
6453{
Brijesh Singh3e233382018-02-23 12:36:50 -06006454 void __user *measure = (void __user *)(uintptr_t)argp->data;
Sean Christopherson81811c12018-03-20 12:17:21 -07006455 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006456 struct sev_data_launch_measure *data;
6457 struct kvm_sev_launch_measure params;
Brijesh Singh3e233382018-02-23 12:36:50 -06006458 void __user *p = NULL;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006459 void *blob = NULL;
6460 int ret;
6461
6462 if (!sev_guest(kvm))
6463 return -ENOTTY;
6464
Brijesh Singh3e233382018-02-23 12:36:50 -06006465 if (copy_from_user(&params, measure, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006466 return -EFAULT;
6467
6468 data = kzalloc(sizeof(*data), GFP_KERNEL);
6469 if (!data)
6470 return -ENOMEM;
6471
6472 /* User wants to query the blob length */
6473 if (!params.len)
6474 goto cmd;
6475
Brijesh Singh3e233382018-02-23 12:36:50 -06006476 p = (void __user *)(uintptr_t)params.uaddr;
6477 if (p) {
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006478 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
6479 ret = -EINVAL;
6480 goto e_free;
6481 }
6482
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006483 ret = -ENOMEM;
6484 blob = kmalloc(params.len, GFP_KERNEL);
6485 if (!blob)
6486 goto e_free;
6487
6488 data->address = __psp_pa(blob);
6489 data->len = params.len;
6490 }
6491
6492cmd:
6493 data->handle = sev->handle;
6494 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
6495
6496 /*
6497 * If we query the session length, FW responded with expected data.
6498 */
6499 if (!params.len)
6500 goto done;
6501
6502 if (ret)
6503 goto e_free_blob;
6504
6505 if (blob) {
Brijesh Singh3e233382018-02-23 12:36:50 -06006506 if (copy_to_user(p, blob, params.len))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006507 ret = -EFAULT;
6508 }
6509
6510done:
6511 params.len = data->len;
Brijesh Singh3e233382018-02-23 12:36:50 -06006512 if (copy_to_user(measure, &params, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006513 ret = -EFAULT;
6514e_free_blob:
6515 kfree(blob);
6516e_free:
6517 kfree(data);
6518 return ret;
6519}
6520
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006521static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
6522{
Sean Christopherson81811c12018-03-20 12:17:21 -07006523 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006524 struct sev_data_launch_finish *data;
6525 int ret;
6526
6527 if (!sev_guest(kvm))
6528 return -ENOTTY;
6529
6530 data = kzalloc(sizeof(*data), GFP_KERNEL);
6531 if (!data)
6532 return -ENOMEM;
6533
6534 data->handle = sev->handle;
6535 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
6536
6537 kfree(data);
6538 return ret;
6539}
6540
Brijesh Singh255d9e72017-12-04 10:57:37 -06006541static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
6542{
Sean Christopherson81811c12018-03-20 12:17:21 -07006543 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006544 struct kvm_sev_guest_status params;
6545 struct sev_data_guest_status *data;
6546 int ret;
6547
6548 if (!sev_guest(kvm))
6549 return -ENOTTY;
6550
6551 data = kzalloc(sizeof(*data), GFP_KERNEL);
6552 if (!data)
6553 return -ENOMEM;
6554
6555 data->handle = sev->handle;
6556 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
6557 if (ret)
6558 goto e_free;
6559
6560 params.policy = data->policy;
6561 params.state = data->state;
6562 params.handle = data->handle;
6563
6564 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6565 ret = -EFAULT;
6566e_free:
6567 kfree(data);
6568 return ret;
6569}
6570
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006571static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
6572 unsigned long dst, int size,
6573 int *error, bool enc)
6574{
Sean Christopherson81811c12018-03-20 12:17:21 -07006575 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006576 struct sev_data_dbg *data;
6577 int ret;
6578
6579 data = kzalloc(sizeof(*data), GFP_KERNEL);
6580 if (!data)
6581 return -ENOMEM;
6582
6583 data->handle = sev->handle;
6584 data->dst_addr = dst;
6585 data->src_addr = src;
6586 data->len = size;
6587
6588 ret = sev_issue_cmd(kvm,
6589 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
6590 data, error);
6591 kfree(data);
6592 return ret;
6593}
6594
6595static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
6596 unsigned long dst_paddr, int sz, int *err)
6597{
6598 int offset;
6599
6600 /*
6601 * Its safe to read more than we are asked, caller should ensure that
6602 * destination has enough space.
6603 */
6604 src_paddr = round_down(src_paddr, 16);
6605 offset = src_paddr & 15;
6606 sz = round_up(sz + offset, 16);
6607
6608 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
6609}
6610
6611static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
6612 unsigned long __user dst_uaddr,
6613 unsigned long dst_paddr,
6614 int size, int *err)
6615{
6616 struct page *tpage = NULL;
6617 int ret, offset;
6618
6619 /* if inputs are not 16-byte then use intermediate buffer */
6620 if (!IS_ALIGNED(dst_paddr, 16) ||
6621 !IS_ALIGNED(paddr, 16) ||
6622 !IS_ALIGNED(size, 16)) {
6623 tpage = (void *)alloc_page(GFP_KERNEL);
6624 if (!tpage)
6625 return -ENOMEM;
6626
6627 dst_paddr = __sme_page_pa(tpage);
6628 }
6629
6630 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
6631 if (ret)
6632 goto e_free;
6633
6634 if (tpage) {
6635 offset = paddr & 15;
6636 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
6637 page_address(tpage) + offset, size))
6638 ret = -EFAULT;
6639 }
6640
6641e_free:
6642 if (tpage)
6643 __free_page(tpage);
6644
6645 return ret;
6646}
6647
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006648static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
6649 unsigned long __user vaddr,
6650 unsigned long dst_paddr,
6651 unsigned long __user dst_vaddr,
6652 int size, int *error)
6653{
6654 struct page *src_tpage = NULL;
6655 struct page *dst_tpage = NULL;
6656 int ret, len = size;
6657
6658 /* If source buffer is not aligned then use an intermediate buffer */
6659 if (!IS_ALIGNED(vaddr, 16)) {
6660 src_tpage = alloc_page(GFP_KERNEL);
6661 if (!src_tpage)
6662 return -ENOMEM;
6663
6664 if (copy_from_user(page_address(src_tpage),
6665 (void __user *)(uintptr_t)vaddr, size)) {
6666 __free_page(src_tpage);
6667 return -EFAULT;
6668 }
6669
6670 paddr = __sme_page_pa(src_tpage);
6671 }
6672
6673 /*
6674 * If destination buffer or length is not aligned then do read-modify-write:
6675 * - decrypt destination in an intermediate buffer
6676 * - copy the source buffer in an intermediate buffer
6677 * - use the intermediate buffer as source buffer
6678 */
6679 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
6680 int dst_offset;
6681
6682 dst_tpage = alloc_page(GFP_KERNEL);
6683 if (!dst_tpage) {
6684 ret = -ENOMEM;
6685 goto e_free;
6686 }
6687
6688 ret = __sev_dbg_decrypt(kvm, dst_paddr,
6689 __sme_page_pa(dst_tpage), size, error);
6690 if (ret)
6691 goto e_free;
6692
6693 /*
6694 * If source is kernel buffer then use memcpy() otherwise
6695 * copy_from_user().
6696 */
6697 dst_offset = dst_paddr & 15;
6698
6699 if (src_tpage)
6700 memcpy(page_address(dst_tpage) + dst_offset,
6701 page_address(src_tpage), size);
6702 else {
6703 if (copy_from_user(page_address(dst_tpage) + dst_offset,
6704 (void __user *)(uintptr_t)vaddr, size)) {
6705 ret = -EFAULT;
6706 goto e_free;
6707 }
6708 }
6709
6710 paddr = __sme_page_pa(dst_tpage);
6711 dst_paddr = round_down(dst_paddr, 16);
6712 len = round_up(size, 16);
6713 }
6714
6715 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
6716
6717e_free:
6718 if (src_tpage)
6719 __free_page(src_tpage);
6720 if (dst_tpage)
6721 __free_page(dst_tpage);
6722 return ret;
6723}
6724
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006725static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
6726{
6727 unsigned long vaddr, vaddr_end, next_vaddr;
6728 unsigned long dst_vaddr, dst_vaddr_end;
6729 struct page **src_p, **dst_p;
6730 struct kvm_sev_dbg debug;
6731 unsigned long n;
6732 int ret, size;
6733
6734 if (!sev_guest(kvm))
6735 return -ENOTTY;
6736
6737 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
6738 return -EFAULT;
6739
6740 vaddr = debug.src_uaddr;
6741 size = debug.len;
6742 vaddr_end = vaddr + size;
6743 dst_vaddr = debug.dst_uaddr;
6744 dst_vaddr_end = dst_vaddr + size;
6745
6746 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
6747 int len, s_off, d_off;
6748
6749 /* lock userspace source and destination page */
6750 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
6751 if (!src_p)
6752 return -EFAULT;
6753
6754 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
6755 if (!dst_p) {
6756 sev_unpin_memory(kvm, src_p, n);
6757 return -EFAULT;
6758 }
6759
6760 /*
6761 * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
6762 * memory content (i.e it will write the same memory region with C=1).
6763 * It's possible that the cache may contain the data with C=0, i.e.,
6764 * unencrypted so invalidate it first.
6765 */
6766 sev_clflush_pages(src_p, 1);
6767 sev_clflush_pages(dst_p, 1);
6768
6769 /*
6770 * Since user buffer may not be page aligned, calculate the
6771 * offset within the page.
6772 */
6773 s_off = vaddr & ~PAGE_MASK;
6774 d_off = dst_vaddr & ~PAGE_MASK;
6775 len = min_t(size_t, (PAGE_SIZE - s_off), size);
6776
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006777 if (dec)
6778 ret = __sev_dbg_decrypt_user(kvm,
6779 __sme_page_pa(src_p[0]) + s_off,
6780 dst_vaddr,
6781 __sme_page_pa(dst_p[0]) + d_off,
6782 len, &argp->error);
6783 else
6784 ret = __sev_dbg_encrypt_user(kvm,
6785 __sme_page_pa(src_p[0]) + s_off,
6786 vaddr,
6787 __sme_page_pa(dst_p[0]) + d_off,
6788 dst_vaddr,
6789 len, &argp->error);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006790
6791 sev_unpin_memory(kvm, src_p, 1);
6792 sev_unpin_memory(kvm, dst_p, 1);
6793
6794 if (ret)
6795 goto err;
6796
6797 next_vaddr = vaddr + len;
6798 dst_vaddr = dst_vaddr + len;
6799 size -= len;
6800 }
6801err:
6802 return ret;
6803}
6804
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006805static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
6806{
Sean Christopherson81811c12018-03-20 12:17:21 -07006807 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006808 struct sev_data_launch_secret *data;
6809 struct kvm_sev_launch_secret params;
6810 struct page **pages;
6811 void *blob, *hdr;
6812 unsigned long n;
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006813 int ret, offset;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006814
6815 if (!sev_guest(kvm))
6816 return -ENOTTY;
6817
6818 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6819 return -EFAULT;
6820
6821 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
6822 if (!pages)
6823 return -ENOMEM;
6824
6825 /*
6826 * The secret must be copied into contiguous memory region, lets verify
6827 * that userspace memory pages are contiguous before we issue command.
6828 */
6829 if (get_num_contig_pages(0, pages, n) != n) {
6830 ret = -EINVAL;
6831 goto e_unpin_memory;
6832 }
6833
6834 ret = -ENOMEM;
6835 data = kzalloc(sizeof(*data), GFP_KERNEL);
6836 if (!data)
6837 goto e_unpin_memory;
6838
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006839 offset = params.guest_uaddr & (PAGE_SIZE - 1);
6840 data->guest_address = __sme_page_pa(pages[0]) + offset;
6841 data->guest_len = params.guest_len;
6842
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006843 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
6844 if (IS_ERR(blob)) {
6845 ret = PTR_ERR(blob);
6846 goto e_free;
6847 }
6848
6849 data->trans_address = __psp_pa(blob);
6850 data->trans_len = params.trans_len;
6851
6852 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
6853 if (IS_ERR(hdr)) {
6854 ret = PTR_ERR(hdr);
6855 goto e_free_blob;
6856 }
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006857 data->hdr_address = __psp_pa(hdr);
6858 data->hdr_len = params.hdr_len;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006859
6860 data->handle = sev->handle;
6861 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
6862
6863 kfree(hdr);
6864
6865e_free_blob:
6866 kfree(blob);
6867e_free:
6868 kfree(data);
6869e_unpin_memory:
6870 sev_unpin_memory(kvm, pages, n);
6871 return ret;
6872}
6873
Brijesh Singh1654efc2017-12-04 10:57:34 -06006874static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
6875{
6876 struct kvm_sev_cmd sev_cmd;
6877 int r;
6878
6879 if (!svm_sev_enabled())
6880 return -ENOTTY;
6881
6882 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
6883 return -EFAULT;
6884
6885 mutex_lock(&kvm->lock);
6886
6887 switch (sev_cmd.id) {
6888 case KVM_SEV_INIT:
6889 r = sev_guest_init(kvm, &sev_cmd);
6890 break;
Brijesh Singh59414c92017-12-04 10:57:35 -06006891 case KVM_SEV_LAUNCH_START:
6892 r = sev_launch_start(kvm, &sev_cmd);
6893 break;
Brijesh Singh89c50582017-12-04 10:57:35 -06006894 case KVM_SEV_LAUNCH_UPDATE_DATA:
6895 r = sev_launch_update_data(kvm, &sev_cmd);
6896 break;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006897 case KVM_SEV_LAUNCH_MEASURE:
6898 r = sev_launch_measure(kvm, &sev_cmd);
6899 break;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006900 case KVM_SEV_LAUNCH_FINISH:
6901 r = sev_launch_finish(kvm, &sev_cmd);
6902 break;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006903 case KVM_SEV_GUEST_STATUS:
6904 r = sev_guest_status(kvm, &sev_cmd);
6905 break;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006906 case KVM_SEV_DBG_DECRYPT:
6907 r = sev_dbg_crypt(kvm, &sev_cmd, true);
6908 break;
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006909 case KVM_SEV_DBG_ENCRYPT:
6910 r = sev_dbg_crypt(kvm, &sev_cmd, false);
6911 break;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006912 case KVM_SEV_LAUNCH_SECRET:
6913 r = sev_launch_secret(kvm, &sev_cmd);
6914 break;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006915 default:
6916 r = -EINVAL;
6917 goto out;
6918 }
6919
6920 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
6921 r = -EFAULT;
6922
6923out:
6924 mutex_unlock(&kvm->lock);
6925 return r;
6926}
6927
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006928static int svm_register_enc_region(struct kvm *kvm,
6929 struct kvm_enc_region *range)
6930{
Sean Christopherson81811c12018-03-20 12:17:21 -07006931 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006932 struct enc_region *region;
6933 int ret = 0;
6934
6935 if (!sev_guest(kvm))
6936 return -ENOTTY;
6937
6938 region = kzalloc(sizeof(*region), GFP_KERNEL);
6939 if (!region)
6940 return -ENOMEM;
6941
6942 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
6943 if (!region->pages) {
6944 ret = -ENOMEM;
6945 goto e_free;
6946 }
6947
6948 /*
6949 * The guest may change the memory encryption attribute from C=0 -> C=1
6950 * or vice versa for this memory range. Lets make sure caches are
6951 * flushed to ensure that guest data gets written into memory with
6952 * correct C-bit.
6953 */
6954 sev_clflush_pages(region->pages, region->npages);
6955
6956 region->uaddr = range->addr;
6957 region->size = range->size;
6958
6959 mutex_lock(&kvm->lock);
6960 list_add_tail(&region->list, &sev->regions_list);
6961 mutex_unlock(&kvm->lock);
6962
6963 return ret;
6964
6965e_free:
6966 kfree(region);
6967 return ret;
6968}
6969
6970static struct enc_region *
6971find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
6972{
Sean Christopherson81811c12018-03-20 12:17:21 -07006973 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006974 struct list_head *head = &sev->regions_list;
6975 struct enc_region *i;
6976
6977 list_for_each_entry(i, head, list) {
6978 if (i->uaddr == range->addr &&
6979 i->size == range->size)
6980 return i;
6981 }
6982
6983 return NULL;
6984}
6985
6986
6987static int svm_unregister_enc_region(struct kvm *kvm,
6988 struct kvm_enc_region *range)
6989{
6990 struct enc_region *region;
6991 int ret;
6992
6993 mutex_lock(&kvm->lock);
6994
6995 if (!sev_guest(kvm)) {
6996 ret = -ENOTTY;
6997 goto failed;
6998 }
6999
7000 region = find_enc_region(kvm, range);
7001 if (!region) {
7002 ret = -EINVAL;
7003 goto failed;
7004 }
7005
7006 __unregister_enc_region_locked(kvm, region);
7007
7008 mutex_unlock(&kvm->lock);
7009 return 0;
7010
7011failed:
7012 mutex_unlock(&kvm->lock);
7013 return ret;
7014}
7015
Kees Cook404f6aa2016-08-08 16:29:06 -07007016static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007017 .cpu_has_kvm_support = has_svm,
7018 .disabled_by_bios = is_disabled,
7019 .hardware_setup = svm_hardware_setup,
7020 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03007021 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007022 .hardware_enable = svm_hardware_enable,
7023 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02007024 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02007025 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007026
7027 .vcpu_create = svm_create_vcpu,
7028 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007029 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007030
Sean Christopherson434a1e92018-03-20 12:17:18 -07007031 .vm_alloc = svm_vm_alloc,
7032 .vm_free = svm_vm_free,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007033 .vm_init = avic_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06007034 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007035
Avi Kivity04d2cc72007-09-10 18:10:54 +03007036 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007037 .vcpu_load = svm_vcpu_load,
7038 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05007039 .vcpu_blocking = svm_vcpu_blocking,
7040 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007041
Paolo Bonzinia96036b2015-11-10 11:55:36 +01007042 .update_bp_intercept = update_bp_intercept,
Tom Lendacky801e4592018-02-21 13:39:51 -06007043 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007044 .get_msr = svm_get_msr,
7045 .set_msr = svm_set_msr,
7046 .get_segment_base = svm_get_segment_base,
7047 .get_segment = svm_get_segment,
7048 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007049 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10007050 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02007051 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02007052 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03007053 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007054 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007055 .set_cr3 = svm_set_cr3,
7056 .set_cr4 = svm_set_cr4,
7057 .set_efer = svm_set_efer,
7058 .get_idt = svm_get_idt,
7059 .set_idt = svm_set_idt,
7060 .get_gdt = svm_get_gdt,
7061 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007062 .get_dr6 = svm_get_dr6,
7063 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03007064 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01007065 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03007066 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007067 .get_rflags = svm_get_rflags,
7068 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007069
Avi Kivity6aa8b732006-12-10 02:21:36 -08007070 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007071
Avi Kivity6aa8b732006-12-10 02:21:36 -08007072 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007073 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007074 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007075 .set_interrupt_shadow = svm_set_interrupt_shadow,
7076 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007077 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007078 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007079 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007080 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007081 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007082 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007083 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007084 .get_nmi_mask = svm_get_nmi_mask,
7085 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007086 .enable_nmi_window = enable_nmi_window,
7087 .enable_irq_window = enable_irq_window,
7088 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08007089 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007090 .get_enable_apicv = svm_get_enable_apicv,
7091 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007092 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05007093 .hwapic_irr_update = svm_hwapic_irr_update,
7094 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02007095 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05007096 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02007097
7098 .set_tss_addr = svm_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007099 .set_identity_map_addr = svm_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08007100 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007101 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007102
Avi Kivity586f9602010-11-18 13:09:54 +02007103 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007104
Sheng Yang17cc3932010-01-05 19:02:27 +08007105 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08007106
7107 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007108
7109 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00007110 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01007111 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08007112 .xsaves_supported = svm_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +02007113 .umip_emulated = svm_umip_emulated,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007114
7115 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007116
7117 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007118
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02007119 .read_l1_tsc_offset = svm_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007120 .write_tsc_offset = svm_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007121
7122 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007123
7124 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08007125 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007126
7127 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02007128
7129 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05007130 .deliver_posted_interrupt = svm_deliver_avic_intr,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05007131 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02007132 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007133
Ladi Prosek72d7b372017-10-11 16:54:41 +02007134 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007135 .pre_enter_smm = svm_pre_enter_smm,
7136 .pre_leave_smm = svm_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007137 .enable_smi_window = enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06007138
7139 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06007140 .mem_enc_reg_region = svm_register_enc_region,
7141 .mem_enc_unreg_region = svm_unregister_enc_region,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007142};
7143
7144static int __init svm_init(void)
7145{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007146 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03007147 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007148}
7149
7150static void __exit svm_exit(void)
7151{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007152 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08007153}
7154
7155module_init(svm_init)
7156module_exit(svm_exit)