blob: 3e848f952b4fde6692f5ebe970dde8c17603fe49 [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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080052
Eduardo Habkost63d11422008-11-17 19:03:20 -020053#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030054#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020055
Avi Kivity4ecac3f2008-05-13 13:23:38 +030056#define __ex(x) __kvm_handle_fault_on_reboot(x)
57
Avi Kivity6aa8b732006-12-10 02:21:36 -080058MODULE_AUTHOR("Qumranet");
59MODULE_LICENSE("GPL");
60
Josh Triplettae759542012-03-28 11:32:28 -070061static const struct x86_cpu_id svm_cpu_id[] = {
62 X86_FEATURE_MATCH(X86_FEATURE_SVM),
63 {}
64};
65MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
66
Avi Kivity6aa8b732006-12-10 02:21:36 -080067#define IOPM_ALLOC_ORDER 2
68#define MSRPM_ALLOC_ORDER 1
69
Avi Kivity6aa8b732006-12-10 02:21:36 -080070#define SEG_TYPE_LDT 2
71#define SEG_TYPE_BUSY_TSS16 3
72
Andre Przywara6bc31bd2010-04-11 23:07:28 +020073#define SVM_FEATURE_NPT (1 << 0)
74#define SVM_FEATURE_LBRV (1 << 1)
75#define SVM_FEATURE_SVML (1 << 2)
76#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010077#define SVM_FEATURE_TSC_RATE (1 << 4)
78#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
79#define SVM_FEATURE_FLUSH_ASID (1 << 6)
80#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020081#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030082
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -050083#define SVM_AVIC_DOORBELL 0xc001011b
84
Joerg Roedel410e4d52009-08-07 11:49:44 +020085#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
86#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
87#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
88
Joerg Roedel24e09cb2008-02-13 18:58:47 +010089#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
90
Joerg Roedelfbc0db72011-03-25 09:44:46 +010091#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010092#define TSC_RATIO_MIN 0x0000000000000001ULL
93#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010094
Dan Carpenter5446a972016-05-23 13:20:10 +030095#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050096
97/*
98 * 0xff is broadcast, so the max index allowed for physical APIC ID
99 * table is 0xfe. APIC IDs above 0xff are reserved.
100 */
101#define AVIC_MAX_PHYSICAL_ID_COUNT 255
102
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500103#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
104#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
105#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
106
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500107/* AVIC GATAG is encoded using VM and VCPU IDs */
108#define AVIC_VCPU_ID_BITS 8
109#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
110
111#define AVIC_VM_ID_BITS 24
112#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
113#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
114
115#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
116 (y & AVIC_VCPU_ID_MASK))
117#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
118#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
119
Joerg Roedel67ec6602010-05-17 14:43:35 +0200120static bool erratum_383_found __read_mostly;
121
Avi Kivity6c8166a2009-05-31 18:15:37 +0300122static const u32 host_save_user_msrs[] = {
123#ifdef CONFIG_X86_64
124 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
125 MSR_FS_BASE,
126#endif
127 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Paolo Bonzini46896c72015-11-12 14:49:16 +0100128 MSR_TSC_AUX,
Avi Kivity6c8166a2009-05-31 18:15:37 +0300129};
130
131#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
132
133struct kvm_vcpu;
134
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200135struct nested_state {
136 struct vmcb *hsave;
137 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +0100138 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200139 u64 vmcb;
140
141 /* These are the merged vectors */
142 u32 *msrpm;
143
144 /* gpa pointers to the real vectors */
145 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100146 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200147
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200148 /* A VMEXIT is required but not yet emulated */
149 bool exit_required;
150
Joerg Roedelaad42c62009-08-07 11:49:34 +0200151 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100152 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100153 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200154 u32 intercept_exceptions;
155 u64 intercept;
156
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200157 /* Nested Paging related state */
158 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200159};
160
Joerg Roedel323c3d82010-03-01 15:34:37 +0100161#define MSRPM_OFFSETS 16
162static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
163
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500164/*
165 * Set osvw_len to higher value when updated Revision Guides
166 * are published and we know what the new status bits are
167 */
168static uint64_t osvw_len = 4, osvw_status;
169
Avi Kivity6c8166a2009-05-31 18:15:37 +0300170struct vcpu_svm {
171 struct kvm_vcpu vcpu;
172 struct vmcb *vmcb;
173 unsigned long vmcb_pa;
174 struct svm_cpu_data *svm_data;
175 uint64_t asid_generation;
176 uint64_t sysenter_esp;
177 uint64_t sysenter_eip;
Paolo Bonzini46896c72015-11-12 14:49:16 +0100178 uint64_t tsc_aux;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300179
180 u64 next_rip;
181
182 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200183 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200184 u16 fs;
185 u16 gs;
186 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200187 u64 gs_base;
188 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300189
190 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300191
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200192 ulong nmi_iret_rip;
193
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200194 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200195
196 bool nmi_singlestep;
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200197 u64 nmi_singlestep_guest_rflags;
Jan Kiszka66b71382010-02-23 17:47:56 +0100198
199 unsigned int3_injected;
200 unsigned long int3_rip;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100201
Joerg Roedel6092d3d2015-10-14 15:10:54 +0200202 /* cached guest cpuid flags for faster access */
203 bool nrips_enabled : 1;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500204
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500205 u32 ldr_reg;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500206 struct page *avic_backing_page;
207 u64 *avic_physical_id_cache;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -0500208 bool avic_is_running;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500209
210 /*
211 * Per-vcpu list of struct amd_svm_iommu_ir:
212 * This is used mainly to store interrupt remapping information used
213 * when update the vcpu affinity. This avoids the need to scan for
214 * IRTE and try to match ga_tag in the IOMMU driver.
215 */
216 struct list_head ir_list;
217 spinlock_t ir_list_lock;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600218
219 /* which host CPU was used for running this vcpu */
220 unsigned int last_cpu;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500221};
222
223/*
224 * This is a wrapper of struct amd_iommu_ir_data.
225 */
226struct amd_svm_iommu_ir {
227 struct list_head node; /* Used by SVM for per-vcpu ir_list */
228 void *data; /* Storing pointer to struct amd_ir_data */
Avi Kivity6c8166a2009-05-31 18:15:37 +0300229};
230
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500231#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
232#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
233
234#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
235#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
236#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
237#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
238
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100239static DEFINE_PER_CPU(u64, current_tsc_ratio);
240#define TSC_RATIO_DEFAULT 0x0100000000ULL
241
Joerg Roedel455716f2010-03-01 15:34:35 +0100242#define MSR_INVALID 0xffffffffU
243
Mathias Krause09941fb2012-08-30 01:30:20 +0200244static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100245 u32 index; /* Index of the MSR */
246 bool always; /* True if intercept is always on */
247} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400248 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100249 { .index = MSR_IA32_SYSENTER_CS, .always = true },
250#ifdef CONFIG_X86_64
251 { .index = MSR_GS_BASE, .always = true },
252 { .index = MSR_FS_BASE, .always = true },
253 { .index = MSR_KERNEL_GS_BASE, .always = true },
254 { .index = MSR_LSTAR, .always = true },
255 { .index = MSR_CSTAR, .always = true },
256 { .index = MSR_SYSCALL_MASK, .always = true },
257#endif
258 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
259 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
260 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
261 { .index = MSR_IA32_LASTINTTOIP, .always = false },
262 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263};
264
265/* enable NPT for AMD64 and X86 with PAE */
266#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
267static bool npt_enabled = true;
268#else
Joerg Roedele0231712010-02-24 18:59:10 +0100269static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270#endif
271
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100272/* allow nested paging (virtualized MMU) for all guests */
273static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274module_param(npt, int, S_IRUGO);
275
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100276/* allow nested virtualization in KVM/SVM */
277static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278module_param(nested, int, S_IRUGO);
279
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500280/* enable / disable AVIC */
281static int avic;
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500282#ifdef CONFIG_X86_LOCAL_APIC
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500283module_param(avic, int, S_IRUGO);
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500284#endif
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500285
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -0500286/* enable/disable Virtual VMLOAD VMSAVE */
287static int vls = true;
288module_param(vls, int, 0444);
289
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500290/* enable/disable Virtual GIF */
291static int vgif = true;
292module_param(vgif, int, 0444);
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500293
Brijesh Singhe9df0942017-12-04 10:57:33 -0600294/* enable/disable SEV support */
295static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
296module_param(sev, int, 0444);
297
Paolo Bonzini79a80592015-09-21 07:46:55 +0200298static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800299static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200300static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301
Joerg Roedel410e4d52009-08-07 11:49:44 +0200302static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100303static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
306 bool has_error_code, u32 error_code);
307
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100308enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100309 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
310 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100311 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100312 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100313 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100314 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100315 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100316 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100317 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100318 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100319 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100320 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500321 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
322 * AVIC PHYSICAL_TABLE pointer,
323 * AVIC LOGICAL_TABLE pointer
324 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100325 VMCB_DIRTY_MAX,
326};
327
Joerg Roedel0574dec2010-12-03 11:45:58 +0100328/* TPR and CR2 are always written before VMRUN */
329#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100330
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500331#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
332
Brijesh Singhed3cd232017-12-04 10:57:32 -0600333static unsigned int max_sev_asid;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600334static unsigned int min_sev_asid;
335static unsigned long *sev_asid_bitmap;
Brijesh Singh89c50582017-12-04 10:57:35 -0600336#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
Brijesh Singh1654efc2017-12-04 10:57:34 -0600337
Brijesh Singh1e80fdc2017-12-04 10:57:38 -0600338struct enc_region {
339 struct list_head list;
340 unsigned long npages;
341 struct page **pages;
342 unsigned long uaddr;
343 unsigned long size;
344};
345
Brijesh Singh1654efc2017-12-04 10:57:34 -0600346static inline bool svm_sev_enabled(void)
347{
348 return max_sev_asid;
349}
350
351static inline bool sev_guest(struct kvm *kvm)
352{
353 struct kvm_sev_info *sev = &kvm->arch.sev_info;
354
355 return sev->active;
356}
Brijesh Singhed3cd232017-12-04 10:57:32 -0600357
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600358static inline int sev_get_asid(struct kvm *kvm)
359{
360 struct kvm_sev_info *sev = &kvm->arch.sev_info;
361
362 return sev->asid;
363}
364
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100365static inline void mark_all_dirty(struct vmcb *vmcb)
366{
367 vmcb->control.clean = 0;
368}
369
370static inline void mark_all_clean(struct vmcb *vmcb)
371{
372 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
373 & ~VMCB_ALWAYS_DIRTY_MASK;
374}
375
376static inline void mark_dirty(struct vmcb *vmcb, int bit)
377{
378 vmcb->control.clean &= ~(1 << bit);
379}
380
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
382{
383 return container_of(vcpu, struct vcpu_svm, vcpu);
384}
385
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500386static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
387{
388 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
389 mark_dirty(svm->vmcb, VMCB_AVIC);
390}
391
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -0500392static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
393{
394 struct vcpu_svm *svm = to_svm(vcpu);
395 u64 *entry = svm->avic_physical_id_cache;
396
397 if (!entry)
398 return false;
399
400 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
401}
402
Joerg Roedel384c6362010-11-30 18:03:56 +0100403static void recalc_intercepts(struct vcpu_svm *svm)
404{
405 struct vmcb_control_area *c, *h;
406 struct nested_state *g;
407
Joerg Roedel116a0a22010-12-03 11:45:49 +0100408 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
409
Joerg Roedel384c6362010-11-30 18:03:56 +0100410 if (!is_guest_mode(&svm->vcpu))
411 return;
412
413 c = &svm->vmcb->control;
414 h = &svm->nested.hsave->control;
415 g = &svm->nested;
416
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100417 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100418 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100419 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
420 c->intercept = h->intercept | g->intercept;
421}
422
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100423static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
424{
425 if (is_guest_mode(&svm->vcpu))
426 return svm->nested.hsave;
427 else
428 return svm->vmcb;
429}
430
431static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
432{
433 struct vmcb *vmcb = get_host_vmcb(svm);
434
435 vmcb->control.intercept_cr |= (1U << bit);
436
437 recalc_intercepts(svm);
438}
439
440static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
441{
442 struct vmcb *vmcb = get_host_vmcb(svm);
443
444 vmcb->control.intercept_cr &= ~(1U << bit);
445
446 recalc_intercepts(svm);
447}
448
449static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
450{
451 struct vmcb *vmcb = get_host_vmcb(svm);
452
453 return vmcb->control.intercept_cr & (1U << bit);
454}
455
Paolo Bonzini5315c712014-03-03 13:08:29 +0100456static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100457{
458 struct vmcb *vmcb = get_host_vmcb(svm);
459
Paolo Bonzini5315c712014-03-03 13:08:29 +0100460 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
461 | (1 << INTERCEPT_DR1_READ)
462 | (1 << INTERCEPT_DR2_READ)
463 | (1 << INTERCEPT_DR3_READ)
464 | (1 << INTERCEPT_DR4_READ)
465 | (1 << INTERCEPT_DR5_READ)
466 | (1 << INTERCEPT_DR6_READ)
467 | (1 << INTERCEPT_DR7_READ)
468 | (1 << INTERCEPT_DR0_WRITE)
469 | (1 << INTERCEPT_DR1_WRITE)
470 | (1 << INTERCEPT_DR2_WRITE)
471 | (1 << INTERCEPT_DR3_WRITE)
472 | (1 << INTERCEPT_DR4_WRITE)
473 | (1 << INTERCEPT_DR5_WRITE)
474 | (1 << INTERCEPT_DR6_WRITE)
475 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100476
477 recalc_intercepts(svm);
478}
479
Paolo Bonzini5315c712014-03-03 13:08:29 +0100480static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100481{
482 struct vmcb *vmcb = get_host_vmcb(svm);
483
Paolo Bonzini5315c712014-03-03 13:08:29 +0100484 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100485
486 recalc_intercepts(svm);
487}
488
Joerg Roedel18c918c2010-11-30 18:03:59 +0100489static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
490{
491 struct vmcb *vmcb = get_host_vmcb(svm);
492
493 vmcb->control.intercept_exceptions |= (1U << bit);
494
495 recalc_intercepts(svm);
496}
497
498static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
499{
500 struct vmcb *vmcb = get_host_vmcb(svm);
501
502 vmcb->control.intercept_exceptions &= ~(1U << bit);
503
504 recalc_intercepts(svm);
505}
506
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100507static inline void set_intercept(struct vcpu_svm *svm, int bit)
508{
509 struct vmcb *vmcb = get_host_vmcb(svm);
510
511 vmcb->control.intercept |= (1ULL << bit);
512
513 recalc_intercepts(svm);
514}
515
516static inline void clr_intercept(struct vcpu_svm *svm, int bit)
517{
518 struct vmcb *vmcb = get_host_vmcb(svm);
519
520 vmcb->control.intercept &= ~(1ULL << bit);
521
522 recalc_intercepts(svm);
523}
524
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500525static inline bool vgif_enabled(struct vcpu_svm *svm)
526{
527 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
528}
529
Joerg Roedel2af91942009-08-07 11:49:28 +0200530static inline void enable_gif(struct vcpu_svm *svm)
531{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500532 if (vgif_enabled(svm))
533 svm->vmcb->control.int_ctl |= V_GIF_MASK;
534 else
535 svm->vcpu.arch.hflags |= HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200536}
537
538static inline void disable_gif(struct vcpu_svm *svm)
539{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500540 if (vgif_enabled(svm))
541 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
542 else
543 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200544}
545
546static inline bool gif_set(struct vcpu_svm *svm)
547{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500548 if (vgif_enabled(svm))
549 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
550 else
551 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
Joerg Roedel2af91942009-08-07 11:49:28 +0200552}
553
Avi Kivity6aa8b732006-12-10 02:21:36 -0800554static unsigned long iopm_base;
555
556struct kvm_ldttss_desc {
557 u16 limit0;
558 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100559 unsigned base1:8, type:5, dpl:2, p:1;
560 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561 u32 base3;
562 u32 zero1;
563} __attribute__((packed));
564
565struct svm_cpu_data {
566 int cpu;
567
Avi Kivity5008fdf2007-04-02 13:05:50 +0300568 u64 asid_generation;
569 u32 max_asid;
570 u32 next_asid;
Brijesh Singh4faefff2017-12-04 10:57:25 -0600571 u32 min_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572 struct kvm_ldttss_desc *tss_desc;
573
574 struct page *save_area;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600575
576 /* index = sev_asid, value = vmcb pointer */
577 struct vmcb **sev_vmcbs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578};
579
580static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
581
582struct svm_init_data {
583 int cpu;
584 int r;
585};
586
Mathias Krause09941fb2012-08-30 01:30:20 +0200587static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200589#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590#define MSRS_RANGE_SIZE 2048
591#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
592
Joerg Roedel455716f2010-03-01 15:34:35 +0100593static u32 svm_msrpm_offset(u32 msr)
594{
595 u32 offset;
596 int i;
597
598 for (i = 0; i < NUM_MSR_MAPS; i++) {
599 if (msr < msrpm_ranges[i] ||
600 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
601 continue;
602
603 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
604 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
605
606 /* Now we have the u8 offset - but need the u32 offset */
607 return offset / 4;
608 }
609
610 /* MSR not in any range */
611 return MSR_INVALID;
612}
613
Avi Kivity6aa8b732006-12-10 02:21:36 -0800614#define MAX_INST_SIZE 15
615
Avi Kivity6aa8b732006-12-10 02:21:36 -0800616static inline void clgi(void)
617{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300618 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619}
620
621static inline void stgi(void)
622{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300623 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624}
625
626static inline void invlpga(unsigned long addr, u32 asid)
627{
Joerg Roedele0231712010-02-24 18:59:10 +0100628 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629}
630
Yu Zhang855feb62017-08-24 20:27:55 +0800631static int get_npt_level(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +0200632{
633#ifdef CONFIG_X86_64
Yu Zhang2a7266a2017-08-24 20:27:54 +0800634 return PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200635#else
636 return PT32E_ROOT_LEVEL;
637#endif
638}
639
Avi Kivity6aa8b732006-12-10 02:21:36 -0800640static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
641{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000642 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100643 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600644 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645
Alexander Graf9962d032008-11-25 20:17:02 +0100646 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100647 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648}
649
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650static int is_external_interrupt(u32 info)
651{
652 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
653 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
654}
655
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200656static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400657{
658 struct vcpu_svm *svm = to_svm(vcpu);
659 u32 ret = 0;
660
661 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200662 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
663 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400664}
665
666static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
667{
668 struct vcpu_svm *svm = to_svm(vcpu);
669
670 if (mask == 0)
671 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
672 else
673 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
674
675}
676
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
678{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400679 struct vcpu_svm *svm = to_svm(vcpu);
680
Bandan Dasf1047652015-06-11 02:05:33 -0400681 if (svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200682 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200683 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400684 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200685
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400686 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100687 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300688 EMULATE_DONE)
689 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690 return;
691 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300692 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
693 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
694 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300696 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400697 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698}
699
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700700static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100701{
702 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700703 unsigned nr = vcpu->arch.exception.nr;
704 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700705 bool reinject = vcpu->arch.exception.injected;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700706 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100707
Joerg Roedele0231712010-02-24 18:59:10 +0100708 /*
709 * If we are within a nested VM we'd better #VMEXIT and let the guest
710 * handle the exception
711 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200712 if (!reinject &&
713 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100714 return;
715
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200716 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100717 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
718
719 /*
720 * For guest debugging where we have to reinject #BP if some
721 * INT3 is guest-owned:
722 * Emulate nRIP by moving RIP forward. Will fail if injection
723 * raises a fault that is not intercepted. Still better than
724 * failing in all cases.
725 */
726 skip_emulated_instruction(&svm->vcpu);
727 rip = kvm_rip_read(&svm->vcpu);
728 svm->int3_rip = rip + svm->vmcb->save.cs.base;
729 svm->int3_injected = rip - old_rip;
730 }
731
Jan Kiszka116a4752010-02-23 17:47:54 +0100732 svm->vmcb->control.event_inj = nr
733 | SVM_EVTINJ_VALID
734 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
735 | SVM_EVTINJ_TYPE_EXEPT;
736 svm->vmcb->control.event_inj_err = error_code;
737}
738
Joerg Roedel67ec6602010-05-17 14:43:35 +0200739static void svm_init_erratum_383(void)
740{
741 u32 low, high;
742 int err;
743 u64 val;
744
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100745 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200746 return;
747
748 /* Use _safe variants to not break nested virtualization */
749 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
750 if (err)
751 return;
752
753 val |= (1ULL << 47);
754
755 low = lower_32_bits(val);
756 high = upper_32_bits(val);
757
758 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
759
760 erratum_383_found = true;
761}
762
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500763static void svm_init_osvw(struct kvm_vcpu *vcpu)
764{
765 /*
766 * Guests should see errata 400 and 415 as fixed (assuming that
767 * HLT and IO instructions are intercepted).
768 */
769 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
770 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
771
772 /*
773 * By increasing VCPU's osvw.length to 3 we are telling the guest that
774 * all osvw.status bits inside that length, including bit 0 (which is
775 * reserved for erratum 298), are valid. However, if host processor's
776 * osvw_len is 0 then osvw_status[0] carries no information. We need to
777 * be conservative here and therefore we tell the guest that erratum 298
778 * is present (because we really don't know).
779 */
780 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
781 vcpu->arch.osvw.status |= 1;
782}
783
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784static int has_svm(void)
785{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200786 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787
Eduardo Habkost63d11422008-11-17 19:03:20 -0200788 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800789 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 return 0;
791 }
792
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 return 1;
794}
795
Radim Krčmář13a34e02014-08-28 15:13:03 +0200796static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100798 /* Make sure we clean up behind us */
799 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
800 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
801
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200802 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100803
804 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805}
806
Radim Krčmář13a34e02014-08-28 15:13:03 +0200807static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808{
809
Tejun Heo0fe1e002009-10-29 22:34:14 +0900810 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812 struct desc_struct *gdt;
813 int me = raw_smp_processor_id();
814
Alexander Graf10474ae2009-09-15 11:37:46 +0200815 rdmsrl(MSR_EFER, efer);
816 if (efer & EFER_SVME)
817 return -EBUSY;
818
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200820 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200821 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900823 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900824 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200825 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200826 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827 }
828
Tejun Heo0fe1e002009-10-29 22:34:14 +0900829 sd->asid_generation = 1;
830 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
831 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600832 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833
Thomas Garnier45fc8752017-03-14 10:05:08 -0700834 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900835 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836
Alexander Graf9962d032008-11-25 20:17:02 +0100837 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838
Linus Torvaldsd0316552009-12-14 09:58:24 -0800839 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200840
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100841 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
842 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500843 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100844 }
845
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500846
847 /*
848 * Get OSVW bits.
849 *
850 * Note that it is possible to have a system with mixed processor
851 * revisions and therefore different OSVW bits. If bits are not the same
852 * on different processors then choose the worst case (i.e. if erratum
853 * is present on one processor and not on another then assume that the
854 * erratum is present everywhere).
855 */
856 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
857 uint64_t len, status = 0;
858 int err;
859
860 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
861 if (!err)
862 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
863 &err);
864
865 if (err)
866 osvw_status = osvw_len = 0;
867 else {
868 if (len < osvw_len)
869 osvw_len = len;
870 osvw_status |= status;
871 osvw_status &= (1ULL << osvw_len) - 1;
872 }
873 } else
874 osvw_status = osvw_len = 0;
875
Joerg Roedel67ec6602010-05-17 14:43:35 +0200876 svm_init_erratum_383();
877
Joerg Roedel1018faa2012-02-29 14:57:32 +0100878 amd_pmu_enable_virt();
879
Alexander Graf10474ae2009-09-15 11:37:46 +0200880 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881}
882
Joerg Roedel0da1db752008-07-02 16:02:11 +0200883static void svm_cpu_uninit(int cpu)
884{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900885 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200886
Tejun Heo0fe1e002009-10-29 22:34:14 +0900887 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200888 return;
889
890 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600891 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900892 __free_page(sd->save_area);
893 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200894}
895
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896static int svm_cpu_init(int cpu)
897{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900898 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 int r;
900
Tejun Heo0fe1e002009-10-29 22:34:14 +0900901 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
902 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900904 sd->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 r = -ENOMEM;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600906 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900907 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 goto err_1;
909
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600910 if (svm_sev_enabled()) {
911 r = -ENOMEM;
912 sd->sev_vmcbs = kmalloc((max_sev_asid + 1) * sizeof(void *), GFP_KERNEL);
913 if (!sd->sev_vmcbs)
914 goto err_1;
915 }
916
Tejun Heo0fe1e002009-10-29 22:34:14 +0900917 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918
919 return 0;
920
921err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900922 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923 return r;
924
925}
926
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100927static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928{
929 int i;
930
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100931 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
932 if (direct_access_msrs[i].index == index)
933 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100935 return false;
936}
937
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938static void set_msr_interception(u32 *msrpm, unsigned msr,
939 int read, int write)
940{
Joerg Roedel455716f2010-03-01 15:34:35 +0100941 u8 bit_read, bit_write;
942 unsigned long tmp;
943 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100945 /*
946 * If this warning triggers extend the direct_access_msrs list at the
947 * beginning of the file
948 */
949 WARN_ON(!valid_msr_intercept(msr));
950
Joerg Roedel455716f2010-03-01 15:34:35 +0100951 offset = svm_msrpm_offset(msr);
952 bit_read = 2 * (msr & 0x0f);
953 bit_write = 2 * (msr & 0x0f) + 1;
954 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955
Joerg Roedel455716f2010-03-01 15:34:35 +0100956 BUG_ON(offset == MSR_INVALID);
957
958 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
959 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
960
961 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962}
963
Joerg Roedelf65c2292008-02-13 18:58:46 +0100964static void svm_vcpu_init_msrpm(u32 *msrpm)
965{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100966 int i;
967
Joerg Roedelf65c2292008-02-13 18:58:46 +0100968 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
969
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100970 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
971 if (!direct_access_msrs[i].always)
972 continue;
973
974 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
975 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100976}
977
Joerg Roedel323c3d82010-03-01 15:34:37 +0100978static void add_msr_offset(u32 offset)
979{
980 int i;
981
982 for (i = 0; i < MSRPM_OFFSETS; ++i) {
983
984 /* Offset already in list? */
985 if (msrpm_offsets[i] == offset)
986 return;
987
988 /* Slot used by another offset? */
989 if (msrpm_offsets[i] != MSR_INVALID)
990 continue;
991
992 /* Add offset to list */
993 msrpm_offsets[i] = offset;
994
995 return;
996 }
997
998 /*
999 * If this BUG triggers the msrpm_offsets table has an overflow. Just
1000 * increase MSRPM_OFFSETS in this case.
1001 */
1002 BUG();
1003}
1004
1005static void init_msrpm_offsets(void)
1006{
1007 int i;
1008
1009 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
1010
1011 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1012 u32 offset;
1013
1014 offset = svm_msrpm_offset(direct_access_msrs[i].index);
1015 BUG_ON(offset == MSR_INVALID);
1016
1017 add_msr_offset(offset);
1018 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019}
1020
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001021static void svm_enable_lbrv(struct vcpu_svm *svm)
1022{
1023 u32 *msrpm = svm->msrpm;
1024
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001025 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001026 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
1027 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
1028 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
1029 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
1030}
1031
1032static void svm_disable_lbrv(struct vcpu_svm *svm)
1033{
1034 u32 *msrpm = svm->msrpm;
1035
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001036 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001037 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
1038 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
1039 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
1040 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
1041}
1042
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001043static void disable_nmi_singlestep(struct vcpu_svm *svm)
1044{
1045 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001046
Ladi Prosekab2f4d732017-06-21 09:06:58 +02001047 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
1048 /* Clear our flags if they were not set by the guest */
1049 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1050 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
1051 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1052 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
1053 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001054}
1055
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001056/* Note:
1057 * This hash table is used to map VM_ID to a struct kvm_arch,
1058 * when handling AMD IOMMU GALOG notification to schedule in
1059 * a particular vCPU.
1060 */
1061#define SVM_VM_DATA_HASH_BITS 8
David Hildenbrand681bcea2017-01-24 22:21:16 +01001062static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001063static u32 next_vm_id = 0;
1064static bool next_vm_id_wrapped = 0;
David Hildenbrand681bcea2017-01-24 22:21:16 +01001065static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001066
1067/* Note:
1068 * This function is called from IOMMU driver to notify
1069 * SVM to schedule in a particular vCPU of a particular VM.
1070 */
1071static int avic_ga_log_notifier(u32 ga_tag)
1072{
1073 unsigned long flags;
1074 struct kvm_arch *ka = NULL;
1075 struct kvm_vcpu *vcpu = NULL;
1076 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
1077 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
1078
1079 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
1080
1081 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1082 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1083 struct kvm *kvm = container_of(ka, struct kvm, arch);
1084 struct kvm_arch *vm_data = &kvm->arch;
1085
1086 if (vm_data->avic_vm_id != vm_id)
1087 continue;
1088 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
1089 break;
1090 }
1091 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1092
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001093 /* Note:
1094 * At this point, the IOMMU should have already set the pending
1095 * bit in the vAPIC backing page. So, we just need to schedule
1096 * in the vcpu.
1097 */
Paolo Bonzini1cf53582017-10-10 12:51:56 +02001098 if (vcpu)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001099 kvm_vcpu_wake_up(vcpu);
1100
1101 return 0;
1102}
1103
Brijesh Singhe9df0942017-12-04 10:57:33 -06001104static __init int sev_hardware_setup(void)
1105{
1106 struct sev_user_data_status *status;
1107 int rc;
1108
1109 /* Maximum number of encrypted guests supported simultaneously */
1110 max_sev_asid = cpuid_ecx(0x8000001F);
1111
1112 if (!max_sev_asid)
1113 return 1;
1114
Brijesh Singh1654efc2017-12-04 10:57:34 -06001115 /* Minimum ASID value that should be used for SEV guest */
1116 min_sev_asid = cpuid_edx(0x8000001F);
1117
1118 /* Initialize SEV ASID bitmap */
1119 sev_asid_bitmap = kcalloc(BITS_TO_LONGS(max_sev_asid),
1120 sizeof(unsigned long), GFP_KERNEL);
1121 if (!sev_asid_bitmap)
1122 return 1;
1123
Brijesh Singhe9df0942017-12-04 10:57:33 -06001124 status = kmalloc(sizeof(*status), GFP_KERNEL);
1125 if (!status)
1126 return 1;
1127
1128 /*
1129 * Check SEV platform status.
1130 *
1131 * PLATFORM_STATUS can be called in any state, if we failed to query
1132 * the PLATFORM status then either PSP firmware does not support SEV
1133 * feature or SEV firmware is dead.
1134 */
1135 rc = sev_platform_status(status, NULL);
1136 if (rc)
1137 goto err;
1138
1139 pr_info("SEV supported\n");
1140
1141err:
1142 kfree(status);
1143 return rc;
1144}
1145
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146static __init int svm_hardware_setup(void)
1147{
1148 int cpu;
1149 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001150 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 int r;
1152
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
1154
1155 if (!iopm_pages)
1156 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +03001157
1158 iopm_va = page_address(iopm_pages);
1159 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
1161
Joerg Roedel323c3d82010-03-01 15:34:37 +01001162 init_msrpm_offsets();
1163
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001164 if (boot_cpu_has(X86_FEATURE_NX))
1165 kvm_enable_efer_bits(EFER_NX);
1166
Alexander Graf1b2fd702009-02-02 16:23:51 +01001167 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
1168 kvm_enable_efer_bits(EFER_FFXSR);
1169
Joerg Roedel92a1f122011-03-25 09:44:51 +01001170 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +01001171 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +08001172 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1173 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +01001174 }
1175
Alexander Graf236de052008-11-25 20:17:10 +01001176 if (nested) {
1177 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +02001178 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +01001179 }
1180
Brijesh Singhe9df0942017-12-04 10:57:33 -06001181 if (sev) {
1182 if (boot_cpu_has(X86_FEATURE_SEV) &&
1183 IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
1184 r = sev_hardware_setup();
1185 if (r)
1186 sev = false;
1187 } else {
1188 sev = false;
1189 }
1190 }
1191
Zachary Amsden3230bb42009-09-29 11:38:37 -10001192 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193 r = svm_cpu_init(cpu);
1194 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +01001195 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +01001197
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001198 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001199 npt_enabled = false;
1200
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001201 if (npt_enabled && !npt) {
1202 printk(KERN_INFO "kvm: Nested Paging disabled\n");
1203 npt_enabled = false;
1204 }
1205
Joerg Roedel18552672008-02-07 13:47:41 +01001206 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001207 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +01001208 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +02001209 } else
1210 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001211
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001212 if (avic) {
1213 if (!npt_enabled ||
1214 !boot_cpu_has(X86_FEATURE_AVIC) ||
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001215 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001216 avic = false;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001217 } else {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001218 pr_info("AVIC enabled\n");
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001219
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001220 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1221 }
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001222 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001223
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001224 if (vls) {
1225 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001226 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001227 !IS_ENABLED(CONFIG_X86_64)) {
1228 vls = false;
1229 } else {
1230 pr_info("Virtual VMLOAD VMSAVE supported\n");
1231 }
1232 }
1233
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001234 if (vgif) {
1235 if (!boot_cpu_has(X86_FEATURE_VGIF))
1236 vgif = false;
1237 else
1238 pr_info("Virtual GIF supported\n");
1239 }
1240
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241 return 0;
1242
Joerg Roedelf65c2292008-02-13 18:58:46 +01001243err:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1245 iopm_base = 0;
1246 return r;
1247}
1248
1249static __exit void svm_hardware_unsetup(void)
1250{
Joerg Roedel0da1db752008-07-02 16:02:11 +02001251 int cpu;
1252
Brijesh Singh1654efc2017-12-04 10:57:34 -06001253 if (svm_sev_enabled())
1254 kfree(sev_asid_bitmap);
1255
Zachary Amsden3230bb42009-09-29 11:38:37 -10001256 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +02001257 svm_cpu_uninit(cpu);
1258
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001260 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001261}
1262
1263static void init_seg(struct vmcb_seg *seg)
1264{
1265 seg->selector = 0;
1266 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001267 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001268 seg->limit = 0xffff;
1269 seg->base = 0;
1270}
1271
1272static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1273{
1274 seg->selector = 0;
1275 seg->attrib = SVM_SELECTOR_P_MASK | type;
1276 seg->limit = 0xffff;
1277 seg->base = 0;
1278}
1279
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001280static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1281{
1282 struct vcpu_svm *svm = to_svm(vcpu);
1283 u64 g_tsc_offset = 0;
1284
Joerg Roedel20307532010-11-29 17:51:48 +01001285 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001286 g_tsc_offset = svm->vmcb->control.tsc_offset -
1287 svm->nested.hsave->control.tsc_offset;
1288 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001289 } else
1290 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1291 svm->vmcb->control.tsc_offset,
1292 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001293
1294 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001295
1296 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001297}
1298
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001299static void avic_init_vmcb(struct vcpu_svm *svm)
1300{
1301 struct vmcb *vmcb = svm->vmcb;
1302 struct kvm_arch *vm_data = &svm->vcpu.kvm->arch;
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001303 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
1304 phys_addr_t lpa = __sme_set(page_to_phys(vm_data->avic_logical_id_table_page));
1305 phys_addr_t ppa = __sme_set(page_to_phys(vm_data->avic_physical_id_table_page));
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001306
1307 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1308 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1309 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1310 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1311 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001312}
1313
Paolo Bonzini56908912015-10-19 11:30:19 +02001314static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315{
Joerg Roedele6101a92008-02-13 18:58:45 +01001316 struct vmcb_control_area *control = &svm->vmcb->control;
1317 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001319 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001320
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001321 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1322 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1323 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1324 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1325 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1326 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001327 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1328 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329
Paolo Bonzini5315c712014-03-03 13:08:29 +01001330 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331
Joerg Roedel18c918c2010-11-30 18:03:59 +01001332 set_exception_intercept(svm, PF_VECTOR);
1333 set_exception_intercept(svm, UD_VECTOR);
1334 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001335 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001336 set_exception_intercept(svm, DB_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001338 set_intercept(svm, INTERCEPT_INTR);
1339 set_intercept(svm, INTERCEPT_NMI);
1340 set_intercept(svm, INTERCEPT_SMI);
1341 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001342 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001343 set_intercept(svm, INTERCEPT_CPUID);
1344 set_intercept(svm, INTERCEPT_INVD);
1345 set_intercept(svm, INTERCEPT_HLT);
1346 set_intercept(svm, INTERCEPT_INVLPG);
1347 set_intercept(svm, INTERCEPT_INVLPGA);
1348 set_intercept(svm, INTERCEPT_IOIO_PROT);
1349 set_intercept(svm, INTERCEPT_MSR_PROT);
1350 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1351 set_intercept(svm, INTERCEPT_SHUTDOWN);
1352 set_intercept(svm, INTERCEPT_VMRUN);
1353 set_intercept(svm, INTERCEPT_VMMCALL);
1354 set_intercept(svm, INTERCEPT_VMLOAD);
1355 set_intercept(svm, INTERCEPT_VMSAVE);
1356 set_intercept(svm, INTERCEPT_STGI);
1357 set_intercept(svm, INTERCEPT_CLGI);
1358 set_intercept(svm, INTERCEPT_SKINIT);
1359 set_intercept(svm, INTERCEPT_WBINVD);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001360 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001362 if (!kvm_mwait_in_guest()) {
1363 set_intercept(svm, INTERCEPT_MONITOR);
1364 set_intercept(svm, INTERCEPT_MWAIT);
1365 }
1366
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001367 control->iopm_base_pa = __sme_set(iopm_base);
1368 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369 control->int_ctl = V_INTR_MASKING_MASK;
1370
1371 init_seg(&save->es);
1372 init_seg(&save->ss);
1373 init_seg(&save->ds);
1374 init_seg(&save->fs);
1375 init_seg(&save->gs);
1376
1377 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001378 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379 /* Executable/Readable Code Segment */
1380 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1381 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1382 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383
1384 save->gdtr.limit = 0xffff;
1385 save->idtr.limit = 0xffff;
1386
1387 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1388 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1389
Paolo Bonzini56908912015-10-19 11:30:19 +02001390 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001391 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001392 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001394 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395
Joerg Roedele0231712010-02-24 18:59:10 +01001396 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001397 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001398 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001400 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001401 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001402
Rusty Russell66aee912007-07-17 23:34:16 +10001403 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001405
1406 if (npt_enabled) {
1407 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001408 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001409 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001410 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001411 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1412 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001413 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001414 save->cr3 = 0;
1415 save->cr4 = 0;
1416 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001417 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001418
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001419 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001420 svm->vcpu.arch.hflags = 0;
1421
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001422 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001423 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001424 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001425 }
1426
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001427 if (kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001428 avic_init_vmcb(svm);
1429
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001430 /*
1431 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1432 * in VMCB and clear intercepts to avoid #VMEXIT.
1433 */
1434 if (vls) {
1435 clr_intercept(svm, INTERCEPT_VMLOAD);
1436 clr_intercept(svm, INTERCEPT_VMSAVE);
1437 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1438 }
1439
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001440 if (vgif) {
1441 clr_intercept(svm, INTERCEPT_STGI);
1442 clr_intercept(svm, INTERCEPT_CLGI);
1443 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1444 }
1445
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001446 if (sev_guest(svm->vcpu.kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001447 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001448 clr_exception_intercept(svm, UD_VECTOR);
1449 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001450
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001451 mark_all_dirty(svm->vmcb);
1452
Joerg Roedel2af91942009-08-07 11:49:28 +02001453 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001454
1455}
1456
Dan Carpenterd3e7dec2017-05-18 10:38:53 +03001457static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1458 unsigned int index)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001459{
1460 u64 *avic_physical_id_table;
1461 struct kvm_arch *vm_data = &vcpu->kvm->arch;
1462
1463 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1464 return NULL;
1465
1466 avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page);
1467
1468 return &avic_physical_id_table[index];
1469}
1470
1471/**
1472 * Note:
1473 * AVIC hardware walks the nested page table to check permissions,
1474 * but does not use the SPA address specified in the leaf page
1475 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1476 * field of the VMCB. Therefore, we set up the
1477 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1478 */
1479static int avic_init_access_page(struct kvm_vcpu *vcpu)
1480{
1481 struct kvm *kvm = vcpu->kvm;
1482 int ret;
1483
1484 if (kvm->arch.apic_access_page_done)
1485 return 0;
1486
1487 ret = x86_set_memory_region(kvm,
1488 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1489 APIC_DEFAULT_PHYS_BASE,
1490 PAGE_SIZE);
1491 if (ret)
1492 return ret;
1493
1494 kvm->arch.apic_access_page_done = true;
1495 return 0;
1496}
1497
1498static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1499{
1500 int ret;
1501 u64 *entry, new_entry;
1502 int id = vcpu->vcpu_id;
1503 struct vcpu_svm *svm = to_svm(vcpu);
1504
1505 ret = avic_init_access_page(vcpu);
1506 if (ret)
1507 return ret;
1508
1509 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1510 return -EINVAL;
1511
1512 if (!svm->vcpu.arch.apic->regs)
1513 return -EINVAL;
1514
1515 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1516
1517 /* Setting AVIC backing page address in the phy APIC ID table */
1518 entry = avic_get_physical_id_entry(vcpu, id);
1519 if (!entry)
1520 return -EINVAL;
1521
1522 new_entry = READ_ONCE(*entry);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001523 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
1524 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1525 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001526 WRITE_ONCE(*entry, new_entry);
1527
1528 svm->avic_physical_id_cache = entry;
1529
1530 return 0;
1531}
1532
Brijesh Singh1654efc2017-12-04 10:57:34 -06001533static void __sev_asid_free(int asid)
1534{
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001535 struct svm_cpu_data *sd;
1536 int cpu, pos;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001537
1538 pos = asid - 1;
1539 clear_bit(pos, sev_asid_bitmap);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001540
1541 for_each_possible_cpu(cpu) {
1542 sd = per_cpu(svm_data, cpu);
1543 sd->sev_vmcbs[pos] = NULL;
1544 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001545}
1546
1547static void sev_asid_free(struct kvm *kvm)
1548{
1549 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1550
1551 __sev_asid_free(sev->asid);
1552}
1553
Brijesh Singh59414c92017-12-04 10:57:35 -06001554static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
1555{
1556 struct sev_data_decommission *decommission;
1557 struct sev_data_deactivate *data;
1558
1559 if (!handle)
1560 return;
1561
1562 data = kzalloc(sizeof(*data), GFP_KERNEL);
1563 if (!data)
1564 return;
1565
1566 /* deactivate handle */
1567 data->handle = handle;
1568 sev_guest_deactivate(data, NULL);
1569
1570 wbinvd_on_all_cpus();
1571 sev_guest_df_flush(NULL);
1572 kfree(data);
1573
1574 decommission = kzalloc(sizeof(*decommission), GFP_KERNEL);
1575 if (!decommission)
1576 return;
1577
1578 /* decommission handle */
1579 decommission->handle = handle;
1580 sev_guest_decommission(decommission, NULL);
1581
1582 kfree(decommission);
1583}
1584
Brijesh Singh89c50582017-12-04 10:57:35 -06001585static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
1586 unsigned long ulen, unsigned long *n,
1587 int write)
1588{
1589 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1590 unsigned long npages, npinned, size;
1591 unsigned long locked, lock_limit;
1592 struct page **pages;
1593 int first, last;
1594
1595 /* Calculate number of pages. */
1596 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
1597 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
1598 npages = (last - first + 1);
1599
1600 locked = sev->pages_locked + npages;
1601 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1602 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
1603 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
1604 return NULL;
1605 }
1606
1607 /* Avoid using vmalloc for smaller buffers. */
1608 size = npages * sizeof(struct page *);
1609 if (size > PAGE_SIZE)
1610 pages = vmalloc(size);
1611 else
1612 pages = kmalloc(size, GFP_KERNEL);
1613
1614 if (!pages)
1615 return NULL;
1616
1617 /* Pin the user virtual address. */
1618 npinned = get_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
1619 if (npinned != npages) {
1620 pr_err("SEV: Failure locking %lu pages.\n", npages);
1621 goto err;
1622 }
1623
1624 *n = npages;
1625 sev->pages_locked = locked;
1626
1627 return pages;
1628
1629err:
1630 if (npinned > 0)
1631 release_pages(pages, npinned);
1632
1633 kvfree(pages);
1634 return NULL;
1635}
1636
1637static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
1638 unsigned long npages)
1639{
1640 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1641
1642 release_pages(pages, npages);
1643 kvfree(pages);
1644 sev->pages_locked -= npages;
1645}
1646
1647static void sev_clflush_pages(struct page *pages[], unsigned long npages)
1648{
1649 uint8_t *page_virtual;
1650 unsigned long i;
1651
1652 if (npages == 0 || pages == NULL)
1653 return;
1654
1655 for (i = 0; i < npages; i++) {
1656 page_virtual = kmap_atomic(pages[i]);
1657 clflush_cache_range(page_virtual, PAGE_SIZE);
1658 kunmap_atomic(page_virtual);
1659 }
1660}
1661
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001662static void __unregister_enc_region_locked(struct kvm *kvm,
1663 struct enc_region *region)
1664{
1665 /*
1666 * The guest may change the memory encryption attribute from C=0 -> C=1
1667 * or vice versa for this memory range. Lets make sure caches are
1668 * flushed to ensure that guest data gets written into memory with
1669 * correct C-bit.
1670 */
1671 sev_clflush_pages(region->pages, region->npages);
1672
1673 sev_unpin_memory(kvm, region->pages, region->npages);
1674 list_del(&region->list);
1675 kfree(region);
1676}
1677
Brijesh Singh1654efc2017-12-04 10:57:34 -06001678static void sev_vm_destroy(struct kvm *kvm)
1679{
Brijesh Singh59414c92017-12-04 10:57:35 -06001680 struct kvm_sev_info *sev = &kvm->arch.sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001681 struct list_head *head = &sev->regions_list;
1682 struct list_head *pos, *q;
Brijesh Singh59414c92017-12-04 10:57:35 -06001683
Brijesh Singh1654efc2017-12-04 10:57:34 -06001684 if (!sev_guest(kvm))
1685 return;
1686
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001687 mutex_lock(&kvm->lock);
1688
1689 /*
1690 * if userspace was terminated before unregistering the memory regions
1691 * then lets unpin all the registered memory.
1692 */
1693 if (!list_empty(head)) {
1694 list_for_each_safe(pos, q, head) {
1695 __unregister_enc_region_locked(kvm,
1696 list_entry(pos, struct enc_region, list));
1697 }
1698 }
1699
1700 mutex_unlock(&kvm->lock);
1701
Brijesh Singh59414c92017-12-04 10:57:35 -06001702 sev_unbind_asid(kvm, sev->handle);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001703 sev_asid_free(kvm);
1704}
1705
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001706static void avic_vm_destroy(struct kvm *kvm)
1707{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001708 unsigned long flags;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001709 struct kvm_arch *vm_data = &kvm->arch;
1710
Dmitry Vyukov3863dff2017-01-24 14:06:48 +01001711 if (!avic)
1712 return;
1713
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001714 if (vm_data->avic_logical_id_table_page)
1715 __free_page(vm_data->avic_logical_id_table_page);
1716 if (vm_data->avic_physical_id_table_page)
1717 __free_page(vm_data->avic_physical_id_table_page);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001718
1719 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1720 hash_del(&vm_data->hnode);
1721 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001722}
1723
Brijesh Singh1654efc2017-12-04 10:57:34 -06001724static void svm_vm_destroy(struct kvm *kvm)
1725{
1726 avic_vm_destroy(kvm);
1727 sev_vm_destroy(kvm);
1728}
1729
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001730static int avic_vm_init(struct kvm *kvm)
1731{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001732 unsigned long flags;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001733 int err = -ENOMEM;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001734 struct kvm_arch *vm_data = &kvm->arch;
1735 struct page *p_page;
1736 struct page *l_page;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001737 struct kvm_arch *ka;
1738 u32 vm_id;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001739
1740 if (!avic)
1741 return 0;
1742
1743 /* Allocating physical APIC ID table (4KB) */
1744 p_page = alloc_page(GFP_KERNEL);
1745 if (!p_page)
1746 goto free_avic;
1747
1748 vm_data->avic_physical_id_table_page = p_page;
1749 clear_page(page_address(p_page));
1750
1751 /* Allocating logical APIC ID table (4KB) */
1752 l_page = alloc_page(GFP_KERNEL);
1753 if (!l_page)
1754 goto free_avic;
1755
1756 vm_data->avic_logical_id_table_page = l_page;
1757 clear_page(page_address(l_page));
1758
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001759 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001760 again:
1761 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
1762 if (vm_id == 0) { /* id is 1-based, zero is not okay */
1763 next_vm_id_wrapped = 1;
1764 goto again;
1765 }
1766 /* Is it still in use? Only possible if wrapped at least once */
1767 if (next_vm_id_wrapped) {
1768 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1769 struct kvm *k2 = container_of(ka, struct kvm, arch);
1770 struct kvm_arch *vd2 = &k2->arch;
1771 if (vd2->avic_vm_id == vm_id)
1772 goto again;
1773 }
1774 }
1775 vm_data->avic_vm_id = vm_id;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001776 hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id);
1777 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1778
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001779 return 0;
1780
1781free_avic:
1782 avic_vm_destroy(kvm);
1783 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001784}
1785
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001786static inline int
1787avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001788{
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001789 int ret = 0;
1790 unsigned long flags;
1791 struct amd_svm_iommu_ir *ir;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001792 struct vcpu_svm *svm = to_svm(vcpu);
1793
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001794 if (!kvm_arch_has_assigned_device(vcpu->kvm))
1795 return 0;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001796
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001797 /*
1798 * Here, we go through the per-vcpu ir_list to update all existing
1799 * interrupt remapping table entry targeting this vcpu.
1800 */
1801 spin_lock_irqsave(&svm->ir_list_lock, flags);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001802
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001803 if (list_empty(&svm->ir_list))
1804 goto out;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001805
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001806 list_for_each_entry(ir, &svm->ir_list, node) {
1807 ret = amd_iommu_update_ga(cpu, r, ir->data);
1808 if (ret)
1809 break;
1810 }
1811out:
1812 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
1813 return ret;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001814}
1815
1816static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1817{
1818 u64 entry;
1819 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05001820 int h_physical_id = kvm_cpu_get_apicid(cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001821 struct vcpu_svm *svm = to_svm(vcpu);
1822
1823 if (!kvm_vcpu_apicv_active(vcpu))
1824 return;
1825
1826 if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
1827 return;
1828
1829 entry = READ_ONCE(*(svm->avic_physical_id_cache));
1830 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
1831
1832 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
1833 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
1834
1835 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1836 if (svm->avic_is_running)
1837 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1838
1839 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001840 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
1841 svm->avic_is_running);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001842}
1843
1844static void avic_vcpu_put(struct kvm_vcpu *vcpu)
1845{
1846 u64 entry;
1847 struct vcpu_svm *svm = to_svm(vcpu);
1848
1849 if (!kvm_vcpu_apicv_active(vcpu))
1850 return;
1851
1852 entry = READ_ONCE(*(svm->avic_physical_id_cache));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001853 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
1854 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
1855
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001856 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1857 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858}
1859
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001860/**
1861 * This function is called during VCPU halt/unhalt.
1862 */
1863static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
1864{
1865 struct vcpu_svm *svm = to_svm(vcpu);
1866
1867 svm->avic_is_running = is_run;
1868 if (is_run)
1869 avic_vcpu_load(vcpu, vcpu->cpu);
1870 else
1871 avic_vcpu_put(vcpu);
1872}
1873
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001874static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001875{
1876 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001877 u32 dummy;
1878 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001879
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001880 if (!init_event) {
1881 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1882 MSR_IA32_APICBASE_ENABLE;
1883 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1884 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1885 }
Paolo Bonzini56908912015-10-19 11:30:19 +02001886 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001887
Yu Zhange911eb32017-08-24 20:27:52 +08001888 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001889 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001890
1891 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1892 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001893}
1894
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001895static int avic_init_vcpu(struct vcpu_svm *svm)
1896{
1897 int ret;
1898
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001899 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001900 return 0;
1901
1902 ret = avic_init_backing_page(&svm->vcpu);
1903 if (ret)
1904 return ret;
1905
1906 INIT_LIST_HEAD(&svm->ir_list);
1907 spin_lock_init(&svm->ir_list_lock);
1908
1909 return ret;
1910}
1911
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001912static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001914 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001916 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001917 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001918 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001919 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920
Rusty Russellc16f8622007-07-30 21:12:19 +10001921 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001922 if (!svm) {
1923 err = -ENOMEM;
1924 goto out;
1925 }
1926
1927 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1928 if (err)
1929 goto free_svm;
1930
Joerg Roedelf65c2292008-02-13 18:58:46 +01001931 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001932 page = alloc_page(GFP_KERNEL);
1933 if (!page)
1934 goto uninit;
1935
Joerg Roedelf65c2292008-02-13 18:58:46 +01001936 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1937 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001938 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001939
1940 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1941 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001942 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001943
Alexander Grafb286d5d2008-11-25 20:17:05 +01001944 hsave_page = alloc_page(GFP_KERNEL);
1945 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001946 goto free_page3;
1947
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001948 err = avic_init_vcpu(svm);
1949 if (err)
1950 goto free_page4;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001951
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001952 /* We initialize this flag to true to make sure that the is_running
1953 * bit would be set the first time the vcpu is loaded.
1954 */
1955 svm->avic_is_running = true;
1956
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001957 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001958
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001959 svm->msrpm = page_address(msrpm_pages);
1960 svm_vcpu_init_msrpm(svm->msrpm);
1961
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001962 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001963 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001964
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001965 svm->vmcb = page_address(page);
1966 clear_page(svm->vmcb);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001967 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001968 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02001969 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001971 svm_init_osvw(&svm->vcpu);
1972
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001973 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001974
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001975free_page4:
1976 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001977free_page3:
1978 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1979free_page2:
1980 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1981free_page1:
1982 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001983uninit:
1984 kvm_vcpu_uninit(&svm->vcpu);
1985free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001986 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001987out:
1988 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989}
1990
1991static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1992{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001993 struct vcpu_svm *svm = to_svm(vcpu);
1994
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001995 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001996 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001997 __free_page(virt_to_page(svm->nested.hsave));
1998 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001999 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002000 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002001}
2002
Avi Kivity15ad7142007-07-11 18:17:21 +03002003static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002005 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002006 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02002007
Avi Kivity0cc50642007-03-25 12:07:27 +02002008 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002009 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002010 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02002011 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002012
Avi Kivity82ca2d12010-10-21 12:20:34 +02002013#ifdef CONFIG_X86_64
2014 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
2015#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02002016 savesegment(fs, svm->host.fs);
2017 savesegment(gs, svm->host.gs);
2018 svm->host.ldt = kvm_read_ldt();
2019
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002020 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002021 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002022
Haozhong Zhangad7218832015-10-20 15:39:02 +08002023 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
2024 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2025 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
2026 __this_cpu_write(current_tsc_ratio, tsc_ratio);
2027 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
2028 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002029 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01002030 /* This assumes that the kernel never uses MSR_TSC_AUX */
2031 if (static_cpu_has(X86_FEATURE_RDTSCP))
2032 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002033
2034 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002035}
2036
2037static void svm_vcpu_put(struct kvm_vcpu *vcpu)
2038{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002039 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002040 int i;
2041
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002042 avic_vcpu_put(vcpu);
2043
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002044 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02002045 kvm_load_ldt(svm->host.ldt);
2046#ifdef CONFIG_X86_64
2047 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07002048 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01002049 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02002050#else
Avi Kivity831ca602011-03-08 16:09:51 +02002051#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02002052 loadsegment(gs, svm->host.gs);
2053#endif
Avi Kivity831ca602011-03-08 16:09:51 +02002054#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002055 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002056 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002057}
2058
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002059static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
2060{
2061 avic_set_running(vcpu, false);
2062}
2063
2064static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
2065{
2066 avic_set_running(vcpu, true);
2067}
2068
Avi Kivity6aa8b732006-12-10 02:21:36 -08002069static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
2070{
Ladi Prosek9b611742017-06-21 09:06:59 +02002071 struct vcpu_svm *svm = to_svm(vcpu);
2072 unsigned long rflags = svm->vmcb->save.rflags;
2073
2074 if (svm->nmi_singlestep) {
2075 /* Hide our flags if they were not set by the guest */
2076 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
2077 rflags &= ~X86_EFLAGS_TF;
2078 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
2079 rflags &= ~X86_EFLAGS_RF;
2080 }
2081 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082}
2083
2084static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2085{
Ladi Prosek9b611742017-06-21 09:06:59 +02002086 if (to_svm(vcpu)->nmi_singlestep)
2087 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2088
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002089 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02002090 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002091 * (caused by either a task switch or an inter-privilege IRET),
2092 * so we do not need to update the CPL here.
2093 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002094 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002095}
2096
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002097static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
2098{
2099 switch (reg) {
2100 case VCPU_EXREG_PDPTR:
2101 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002102 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002103 break;
2104 default:
2105 BUG();
2106 }
2107}
2108
Alexander Graff0b85052008-11-25 20:17:01 +01002109static void svm_set_vintr(struct vcpu_svm *svm)
2110{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002111 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002112}
2113
2114static void svm_clear_vintr(struct vcpu_svm *svm)
2115{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002116 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002117}
2118
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
2120{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002121 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122
2123 switch (seg) {
2124 case VCPU_SREG_CS: return &save->cs;
2125 case VCPU_SREG_DS: return &save->ds;
2126 case VCPU_SREG_ES: return &save->es;
2127 case VCPU_SREG_FS: return &save->fs;
2128 case VCPU_SREG_GS: return &save->gs;
2129 case VCPU_SREG_SS: return &save->ss;
2130 case VCPU_SREG_TR: return &save->tr;
2131 case VCPU_SREG_LDTR: return &save->ldtr;
2132 }
2133 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00002134 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135}
2136
2137static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2138{
2139 struct vmcb_seg *s = svm_seg(vcpu, seg);
2140
2141 return s->base;
2142}
2143
2144static void svm_get_segment(struct kvm_vcpu *vcpu,
2145 struct kvm_segment *var, int seg)
2146{
2147 struct vmcb_seg *s = svm_seg(vcpu, seg);
2148
2149 var->base = s->base;
2150 var->limit = s->limit;
2151 var->selector = s->selector;
2152 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
2153 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
2154 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
2155 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
2156 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
2157 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
2158 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05302159
2160 /*
2161 * AMD CPUs circa 2014 track the G bit for all segments except CS.
2162 * However, the SVM spec states that the G bit is not observed by the
2163 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
2164 * So let's synthesize a legal G bit for all segments, this helps
2165 * running KVM nested. It also helps cross-vendor migration, because
2166 * Intel's vmentry has a check on the 'G' bit.
2167 */
2168 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00002169
Joerg Roedele0231712010-02-24 18:59:10 +01002170 /*
2171 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02002172 * for cross vendor migration purposes by "not present"
2173 */
Gioh Kim8eae9572017-05-30 15:24:45 +02002174 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02002175
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002176 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002177 case VCPU_SREG_TR:
2178 /*
2179 * Work around a bug where the busy flag in the tr selector
2180 * isn't exposed
2181 */
Amit Shahc0d09822008-10-27 09:04:18 +00002182 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002183 break;
2184 case VCPU_SREG_DS:
2185 case VCPU_SREG_ES:
2186 case VCPU_SREG_FS:
2187 case VCPU_SREG_GS:
2188 /*
2189 * The accessed bit must always be set in the segment
2190 * descriptor cache, although it can be cleared in the
2191 * descriptor, the cached bit always remains at 1. Since
2192 * Intel has a check on this, set it here to support
2193 * cross-vendor migration.
2194 */
2195 if (!var->unusable)
2196 var->type |= 0x1;
2197 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02002198 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01002199 /*
2200 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02002201 * descriptor is left as 1, although the whole segment has
2202 * been made unusable. Clear it here to pass an Intel VMX
2203 * entry check when cross vendor migrating.
2204 */
2205 if (var->unusable)
2206 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02002207 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02002208 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02002209 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002210 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002211}
2212
Izik Eidus2e4d2652008-03-24 19:38:34 +02002213static int svm_get_cpl(struct kvm_vcpu *vcpu)
2214{
2215 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
2216
2217 return save->cpl;
2218}
2219
Gleb Natapov89a27f42010-02-16 10:51:48 +02002220static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002222 struct vcpu_svm *svm = to_svm(vcpu);
2223
Gleb Natapov89a27f42010-02-16 10:51:48 +02002224 dt->size = svm->vmcb->save.idtr.limit;
2225 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226}
2227
Gleb Natapov89a27f42010-02-16 10:51:48 +02002228static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002230 struct vcpu_svm *svm = to_svm(vcpu);
2231
Gleb Natapov89a27f42010-02-16 10:51:48 +02002232 svm->vmcb->save.idtr.limit = dt->size;
2233 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002234 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235}
2236
Gleb Natapov89a27f42010-02-16 10:51:48 +02002237static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002239 struct vcpu_svm *svm = to_svm(vcpu);
2240
Gleb Natapov89a27f42010-02-16 10:51:48 +02002241 dt->size = svm->vmcb->save.gdtr.limit;
2242 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243}
2244
Gleb Natapov89a27f42010-02-16 10:51:48 +02002245static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002247 struct vcpu_svm *svm = to_svm(vcpu);
2248
Gleb Natapov89a27f42010-02-16 10:51:48 +02002249 svm->vmcb->save.gdtr.limit = dt->size;
2250 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002251 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252}
2253
Avi Kivitye8467fd2009-12-29 18:43:06 +02002254static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2255{
2256}
2257
Avi Kivityaff48ba2010-12-05 18:56:11 +02002258static void svm_decache_cr3(struct kvm_vcpu *vcpu)
2259{
2260}
2261
Anthony Liguori25c4c272007-04-27 09:29:21 +03002262static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002263{
2264}
2265
Avi Kivityd2251572010-01-06 10:55:27 +02002266static void update_cr0_intercept(struct vcpu_svm *svm)
2267{
2268 ulong gcr0 = svm->vcpu.arch.cr0;
2269 u64 *hcr0 = &svm->vmcb->save.cr0;
2270
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002271 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
2272 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
Avi Kivityd2251572010-01-06 10:55:27 +02002273
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002274 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002275
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002276 if (gcr0 == *hcr0) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002277 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
2278 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002279 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002280 set_cr_intercept(svm, INTERCEPT_CR0_READ);
2281 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002282 }
2283}
2284
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2286{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002287 struct vcpu_svm *svm = to_svm(vcpu);
2288
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002289#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002290 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002291 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002292 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002293 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294 }
2295
Mike Dayd77c26f2007-10-08 09:02:08 -04002296 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002297 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002298 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299 }
2300 }
2301#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002302 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002303
2304 if (!npt_enabled)
2305 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02002306
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02002307 /*
2308 * re-enable caching here because the QEMU bios
2309 * does not do it - this results in some delay at
2310 * reboot
2311 */
2312 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
2313 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002314 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002315 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002316 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317}
2318
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002319static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002321 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002322 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
2323
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002324 if (cr4 & X86_CR4_VMXE)
2325 return 1;
2326
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002327 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002328 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02002329
Joerg Roedelec077262008-04-09 14:15:28 +02002330 vcpu->arch.cr4 = cr4;
2331 if (!npt_enabled)
2332 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02002333 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02002334 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002335 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002336 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337}
2338
2339static void svm_set_segment(struct kvm_vcpu *vcpu,
2340 struct kvm_segment *var, int seg)
2341{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002342 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343 struct vmcb_seg *s = svm_seg(vcpu, seg);
2344
2345 s->base = var->base;
2346 s->limit = var->limit;
2347 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02002348 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2349 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2350 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2351 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2352 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2353 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2354 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2355 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002356
2357 /*
2358 * This is always accurate, except if SYSRET returned to a segment
2359 * with SS.DPL != 3. Intel does not have this quirk, and always
2360 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2361 * would entail passing the CPL to userspace and back.
2362 */
2363 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02002364 /* This is symmetric with svm_get_segment() */
2365 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366
Joerg Roedel060d0c92010-12-03 11:45:57 +01002367 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368}
2369
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01002370static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002372 struct vcpu_svm *svm = to_svm(vcpu);
2373
Joerg Roedel18c918c2010-11-30 18:03:59 +01002374 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03002375
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002376 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002377 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01002378 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002379 } else
2380 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03002381}
2382
Tejun Heo0fe1e002009-10-29 22:34:14 +09002383static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384{
Tejun Heo0fe1e002009-10-29 22:34:14 +09002385 if (sd->next_asid > sd->max_asid) {
2386 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06002387 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002388 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389 }
2390
Tejun Heo0fe1e002009-10-29 22:34:14 +09002391 svm->asid_generation = sd->asid_generation;
2392 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01002393
2394 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395}
2396
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002397static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2398{
2399 return to_svm(vcpu)->vmcb->save.dr6;
2400}
2401
2402static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2403{
2404 struct vcpu_svm *svm = to_svm(vcpu);
2405
2406 svm->vmcb->save.dr6 = value;
2407 mark_dirty(svm->vmcb, VMCB_DR);
2408}
2409
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002410static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2411{
2412 struct vcpu_svm *svm = to_svm(vcpu);
2413
2414 get_debugreg(vcpu->arch.db[0], 0);
2415 get_debugreg(vcpu->arch.db[1], 1);
2416 get_debugreg(vcpu->arch.db[2], 2);
2417 get_debugreg(vcpu->arch.db[3], 3);
2418 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2419 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2420
2421 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2422 set_dr_intercepts(svm);
2423}
2424
Gleb Natapov020df072010-04-13 10:05:23 +03002425static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002427 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002428
Gleb Natapov020df072010-04-13 10:05:23 +03002429 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01002430 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431}
2432
Avi Kivity851ba692009-08-24 11:10:17 +03002433static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002435 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002436 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002438 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
Andre Przywaradc25e892010-12-21 11:12:07 +01002439 svm->vmcb->control.insn_bytes,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002440 svm->vmcb->control.insn_len);
2441}
2442
2443static int npf_interception(struct vcpu_svm *svm)
2444{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002445 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Paolo Bonzinid0006532017-08-11 18:36:43 +02002446 u64 error_code = svm->vmcb->control.exit_info_1;
2447
2448 trace_kvm_page_fault(fault_address, error_code);
2449 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
2450 svm->vmcb->control.insn_bytes,
2451 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452}
2453
Avi Kivity851ba692009-08-24 11:10:17 +03002454static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002455{
Avi Kivity851ba692009-08-24 11:10:17 +03002456 struct kvm_run *kvm_run = svm->vcpu.run;
2457
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002458 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03002459 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02002460 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002461 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2462 return 1;
2463 }
Gleb Natapov44c11432009-05-11 13:35:52 +03002464
Jan Kiszka6be7d302009-10-18 13:24:54 +02002465 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02002466 disable_nmi_singlestep(svm);
Gleb Natapov44c11432009-05-11 13:35:52 +03002467 }
2468
2469 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002470 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002471 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2472 kvm_run->debug.arch.pc =
2473 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2474 kvm_run->debug.arch.exception = DB_VECTOR;
2475 return 0;
2476 }
2477
2478 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002479}
2480
Avi Kivity851ba692009-08-24 11:10:17 +03002481static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002482{
Avi Kivity851ba692009-08-24 11:10:17 +03002483 struct kvm_run *kvm_run = svm->vcpu.run;
2484
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002485 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2486 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2487 kvm_run->debug.arch.exception = BP_VECTOR;
2488 return 0;
2489}
2490
Avi Kivity851ba692009-08-24 11:10:17 +03002491static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002492{
2493 int er;
2494
Andre Przywara51d8b662010-12-21 11:12:02 +01002495 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002496 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002497 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002498 return 1;
2499}
2500
Eric Northup54a20552015-11-03 18:03:53 +01002501static int ac_interception(struct vcpu_svm *svm)
2502{
2503 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2504 return 1;
2505}
2506
Joerg Roedel67ec6602010-05-17 14:43:35 +02002507static bool is_erratum_383(void)
2508{
2509 int err, i;
2510 u64 value;
2511
2512 if (!erratum_383_found)
2513 return false;
2514
2515 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2516 if (err)
2517 return false;
2518
2519 /* Bit 62 may or may not be set for this mce */
2520 value &= ~(1ULL << 62);
2521
2522 if (value != 0xb600000000010015ULL)
2523 return false;
2524
2525 /* Clear MCi_STATUS registers */
2526 for (i = 0; i < 6; ++i)
2527 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2528
2529 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2530 if (!err) {
2531 u32 low, high;
2532
2533 value &= ~(1ULL << 2);
2534 low = lower_32_bits(value);
2535 high = upper_32_bits(value);
2536
2537 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2538 }
2539
2540 /* Flush tlb to evict multi-match entries */
2541 __flush_tlb_all();
2542
2543 return true;
2544}
2545
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002546static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002547{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002548 if (is_erratum_383()) {
2549 /*
2550 * Erratum 383 triggered. Guest state is corrupt so kill the
2551 * guest.
2552 */
2553 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2554
Avi Kivitya8eeb042010-05-10 12:34:53 +03002555 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002556
2557 return;
2558 }
2559
Joerg Roedel53371b52008-04-09 14:15:30 +02002560 /*
2561 * On an #MC intercept the MCE handler is not called automatically in
2562 * the host. So do it by hand here.
2563 */
2564 asm volatile (
2565 "int $0x12\n");
2566 /* not sure if we ever come back to this point */
2567
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002568 return;
2569}
2570
2571static int mc_interception(struct vcpu_svm *svm)
2572{
Joerg Roedel53371b52008-04-09 14:15:30 +02002573 return 1;
2574}
2575
Avi Kivity851ba692009-08-24 11:10:17 +03002576static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002577{
Avi Kivity851ba692009-08-24 11:10:17 +03002578 struct kvm_run *kvm_run = svm->vcpu.run;
2579
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002580 /*
2581 * VMCB is undefined after a SHUTDOWN intercept
2582 * so reinitialize it.
2583 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002584 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002585 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002586
2587 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2588 return 0;
2589}
2590
Avi Kivity851ba692009-08-24 11:10:17 +03002591static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002593 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002594 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002595 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002596 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597
Rusty Russelle756fc62007-07-30 20:07:08 +10002598 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002599 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002600 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002601 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01002602 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002603
Avi Kivity039576c2007-03-20 12:46:50 +02002604 port = io_info >> 16;
2605 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002606 svm->next_rip = svm->vmcb->control.exit_info_2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002607 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002608
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002609 /*
2610 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
2611 * KVM_EXIT_DEBUG here.
2612 */
2613 if (in)
2614 return kvm_fast_pio_in(vcpu, size, port) && ret;
2615 else
2616 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002617}
2618
Avi Kivity851ba692009-08-24 11:10:17 +03002619static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002620{
2621 return 1;
2622}
2623
Avi Kivity851ba692009-08-24 11:10:17 +03002624static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002625{
2626 ++svm->vcpu.stat.irq_exits;
2627 return 1;
2628}
2629
Avi Kivity851ba692009-08-24 11:10:17 +03002630static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631{
2632 return 1;
2633}
2634
Avi Kivity851ba692009-08-24 11:10:17 +03002635static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002637 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002638 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639}
2640
Avi Kivity851ba692009-08-24 11:10:17 +03002641static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002642{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002643 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002644 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002645}
2646
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002647static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2648{
2649 struct vcpu_svm *svm = to_svm(vcpu);
2650
2651 return svm->nested.nested_cr3;
2652}
2653
Avi Kivitye4e517b2011-07-28 11:36:17 +03002654static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2655{
2656 struct vcpu_svm *svm = to_svm(vcpu);
2657 u64 cr3 = svm->nested.nested_cr3;
2658 u64 pdpte;
2659 int ret;
2660
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002661 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002662 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002663 if (ret)
2664 return 0;
2665 return pdpte;
2666}
2667
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002668static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2669 unsigned long root)
2670{
2671 struct vcpu_svm *svm = to_svm(vcpu);
2672
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002673 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01002674 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002675 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002676}
2677
Avi Kivity6389ee92010-11-29 16:12:30 +02002678static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2679 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002680{
2681 struct vcpu_svm *svm = to_svm(vcpu);
2682
Paolo Bonzini5e352512014-09-02 13:18:37 +02002683 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2684 /*
2685 * TODO: track the cause of the nested page fault, and
2686 * correctly fill in the high bits of exit_info_1.
2687 */
2688 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2689 svm->vmcb->control.exit_code_hi = 0;
2690 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2691 svm->vmcb->control.exit_info_2 = fault->address;
2692 }
2693
2694 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2695 svm->vmcb->control.exit_info_1 |= fault->error_code;
2696
2697 /*
2698 * The present bit is always zero for page structure faults on real
2699 * hardware.
2700 */
2701 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2702 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002703
2704 nested_svm_vmexit(svm);
2705}
2706
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002707static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002708{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002709 WARN_ON(mmu_is_nested(vcpu));
2710 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002711 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2712 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002713 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002714 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
Yu Zhang855feb62017-08-24 20:27:55 +08002715 vcpu->arch.mmu.shadow_root_level = get_npt_level(vcpu);
Xiao Guangrongc258b622015-08-05 12:04:24 +08002716 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002717 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002718}
2719
2720static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2721{
2722 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2723}
2724
Alexander Grafc0725422008-11-25 20:17:03 +01002725static int nested_svm_check_permissions(struct vcpu_svm *svm)
2726{
Dan Carpentere9196ce2017-05-18 10:39:53 +03002727 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
2728 !is_paging(&svm->vcpu)) {
Alexander Grafc0725422008-11-25 20:17:03 +01002729 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2730 return 1;
2731 }
2732
2733 if (svm->vmcb->save.cpl) {
2734 kvm_inject_gp(&svm->vcpu, 0);
2735 return 1;
2736 }
2737
Dan Carpentere9196ce2017-05-18 10:39:53 +03002738 return 0;
Alexander Grafc0725422008-11-25 20:17:03 +01002739}
2740
Alexander Grafcf74a782008-11-25 20:17:08 +01002741static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2742 bool has_error_code, u32 error_code)
2743{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002744 int vmexit;
2745
Joerg Roedel20307532010-11-29 17:51:48 +01002746 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002747 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002748
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002749 vmexit = nested_svm_intercept(svm);
2750 if (vmexit != NESTED_EXIT_DONE)
2751 return 0;
2752
Joerg Roedel0295ad72009-08-07 11:49:37 +02002753 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2754 svm->vmcb->control.exit_code_hi = 0;
2755 svm->vmcb->control.exit_info_1 = error_code;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002756
2757 /*
2758 * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception.
2759 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2760 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be
2761 * written only when inject_pending_event runs (DR6 would written here
2762 * too). This should be conditional on a new capability---if the
2763 * capability is disabled, kvm_multiple_exception would write the
2764 * ancillary information to CR2 or DR6, for backwards ABI-compatibility.
2765 */
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002766 if (svm->vcpu.arch.exception.nested_apf)
2767 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
2768 else
2769 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
Joerg Roedel0295ad72009-08-07 11:49:37 +02002770
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002771 svm->nested.exit_required = true;
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002772 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002773}
2774
Joerg Roedel8fe54652010-02-19 16:23:01 +01002775/* This function returns true if it is save to enable the irq window */
2776static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002777{
Joerg Roedel20307532010-11-29 17:51:48 +01002778 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002779 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002780
Joerg Roedel26666952009-08-07 11:49:46 +02002781 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002782 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002783
Joerg Roedel26666952009-08-07 11:49:46 +02002784 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002785 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002786
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002787 /*
2788 * if vmexit was already requested (by intercepted exception
2789 * for instance) do not overwrite it with "external interrupt"
2790 * vmexit.
2791 */
2792 if (svm->nested.exit_required)
2793 return false;
2794
Joerg Roedel197717d2010-02-24 18:59:19 +01002795 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2796 svm->vmcb->control.exit_info_1 = 0;
2797 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002798
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002799 if (svm->nested.intercept & 1ULL) {
2800 /*
2801 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002802 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002803 * #vmexit emulation might sleep. Only signal request for
2804 * the #vmexit here.
2805 */
2806 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002807 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002808 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002809 }
2810
Joerg Roedel8fe54652010-02-19 16:23:01 +01002811 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002812}
2813
Joerg Roedel887f5002010-02-24 18:59:12 +01002814/* This function returns true if it is save to enable the nmi window */
2815static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2816{
Joerg Roedel20307532010-11-29 17:51:48 +01002817 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002818 return true;
2819
2820 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2821 return true;
2822
2823 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2824 svm->nested.exit_required = true;
2825
2826 return false;
2827}
2828
Joerg Roedel7597f122010-02-19 16:23:00 +01002829static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002830{
2831 struct page *page;
2832
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002833 might_sleep();
2834
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002835 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002836 if (is_error_page(page))
2837 goto error;
2838
Joerg Roedel7597f122010-02-19 16:23:00 +01002839 *_page = page;
2840
2841 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002842
2843error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002844 kvm_inject_gp(&svm->vcpu, 0);
2845
2846 return NULL;
2847}
2848
Joerg Roedel7597f122010-02-19 16:23:00 +01002849static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002850{
Joerg Roedel7597f122010-02-19 16:23:00 +01002851 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002852 kvm_release_page_dirty(page);
2853}
2854
Joerg Roedelce2ac082010-03-01 15:34:39 +01002855static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002856{
Jan Kiszka9bf41832014-06-30 10:54:17 +02002857 unsigned port, size, iopm_len;
2858 u16 val, mask;
2859 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002860 u64 gpa;
2861
2862 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2863 return NESTED_EXIT_HOST;
2864
2865 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02002866 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2867 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002868 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02002869 start_bit = port % 8;
2870 iopm_len = (start_bit + size > 8) ? 2 : 1;
2871 mask = (0xf >> (4 - size)) << start_bit;
2872 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002873
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002874 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02002875 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002876
Jan Kiszka9bf41832014-06-30 10:54:17 +02002877 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002878}
2879
Joerg Roedeld2477822010-03-01 15:34:34 +01002880static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002881{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002882 u32 offset, msr, value;
2883 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002884
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002885 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002886 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002887
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002888 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2889 offset = svm_msrpm_offset(msr);
2890 write = svm->vmcb->control.exit_info_1 & 1;
2891 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002892
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002893 if (offset == MSR_INVALID)
2894 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002895
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002896 /* Offset is in 32 bit units but need in 8 bit units */
2897 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002898
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002899 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002900 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002901
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002902 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002903}
2904
Ladi Prosekab2f4d732017-06-21 09:06:58 +02002905/* DB exceptions for our internal use must not cause vmexit */
2906static int nested_svm_intercept_db(struct vcpu_svm *svm)
2907{
2908 unsigned long dr6;
2909
2910 /* if we're not singlestepping, it's not ours */
2911 if (!svm->nmi_singlestep)
2912 return NESTED_EXIT_DONE;
2913
2914 /* if it's not a singlestep exception, it's not ours */
2915 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
2916 return NESTED_EXIT_DONE;
2917 if (!(dr6 & DR6_BS))
2918 return NESTED_EXIT_DONE;
2919
2920 /* if the guest is singlestepping, it should get the vmexit */
2921 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
2922 disable_nmi_singlestep(svm);
2923 return NESTED_EXIT_DONE;
2924 }
2925
2926 /* it's ours, the nested hypervisor must not see this one */
2927 return NESTED_EXIT_HOST;
2928}
2929
Joerg Roedel410e4d52009-08-07 11:49:44 +02002930static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002931{
Alexander Grafcf74a782008-11-25 20:17:08 +01002932 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002933
Joerg Roedel410e4d52009-08-07 11:49:44 +02002934 switch (exit_code) {
2935 case SVM_EXIT_INTR:
2936 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002937 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002938 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002939 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002940 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002941 if (npt_enabled)
2942 return NESTED_EXIT_HOST;
2943 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002944 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002945 /* When we're shadowing, trap PFs, but not async PF */
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002946 if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002947 return NESTED_EXIT_HOST;
2948 break;
2949 default:
2950 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002951 }
2952
Joerg Roedel410e4d52009-08-07 11:49:44 +02002953 return NESTED_EXIT_CONTINUE;
2954}
2955
2956/*
2957 * If this function returns true, this #vmexit was already handled
2958 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002959static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002960{
2961 u32 exit_code = svm->vmcb->control.exit_code;
2962 int vmexit = NESTED_EXIT_HOST;
2963
Alexander Grafcf74a782008-11-25 20:17:08 +01002964 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002965 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002966 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002967 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002968 case SVM_EXIT_IOIO:
2969 vmexit = nested_svm_intercept_ioio(svm);
2970 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002971 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2972 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2973 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002974 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002975 break;
2976 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002977 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2978 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2979 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002980 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002981 break;
2982 }
2983 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2984 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Ladi Prosekab2f4d732017-06-21 09:06:58 +02002985 if (svm->nested.intercept_exceptions & excp_bits) {
2986 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
2987 vmexit = nested_svm_intercept_db(svm);
2988 else
2989 vmexit = NESTED_EXIT_DONE;
2990 }
Gleb Natapov631bc482010-10-14 11:22:52 +02002991 /* async page fault always cause vmexit */
2992 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002993 svm->vcpu.arch.exception.nested_apf != 0)
Gleb Natapov631bc482010-10-14 11:22:52 +02002994 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002995 break;
2996 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002997 case SVM_EXIT_ERR: {
2998 vmexit = NESTED_EXIT_DONE;
2999 break;
3000 }
Alexander Grafcf74a782008-11-25 20:17:08 +01003001 default: {
3002 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02003003 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003004 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003005 }
3006 }
3007
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003008 return vmexit;
3009}
3010
3011static int nested_svm_exit_handled(struct vcpu_svm *svm)
3012{
3013 int vmexit;
3014
3015 vmexit = nested_svm_intercept(svm);
3016
3017 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003018 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003019
3020 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01003021}
3022
Joerg Roedel0460a972009-08-07 11:49:31 +02003023static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
3024{
3025 struct vmcb_control_area *dst = &dst_vmcb->control;
3026 struct vmcb_control_area *from = &from_vmcb->control;
3027
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003028 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003029 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02003030 dst->intercept_exceptions = from->intercept_exceptions;
3031 dst->intercept = from->intercept;
3032 dst->iopm_base_pa = from->iopm_base_pa;
3033 dst->msrpm_base_pa = from->msrpm_base_pa;
3034 dst->tsc_offset = from->tsc_offset;
3035 dst->asid = from->asid;
3036 dst->tlb_ctl = from->tlb_ctl;
3037 dst->int_ctl = from->int_ctl;
3038 dst->int_vector = from->int_vector;
3039 dst->int_state = from->int_state;
3040 dst->exit_code = from->exit_code;
3041 dst->exit_code_hi = from->exit_code_hi;
3042 dst->exit_info_1 = from->exit_info_1;
3043 dst->exit_info_2 = from->exit_info_2;
3044 dst->exit_int_info = from->exit_int_info;
3045 dst->exit_int_info_err = from->exit_int_info_err;
3046 dst->nested_ctl = from->nested_ctl;
3047 dst->event_inj = from->event_inj;
3048 dst->event_inj_err = from->event_inj_err;
3049 dst->nested_cr3 = from->nested_cr3;
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003050 dst->virt_ext = from->virt_ext;
Joerg Roedel0460a972009-08-07 11:49:31 +02003051}
3052
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003053static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003054{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003055 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003056 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02003057 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003058 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01003059
Joerg Roedel17897f32009-10-09 16:08:29 +02003060 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
3061 vmcb->control.exit_info_1,
3062 vmcb->control.exit_info_2,
3063 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003064 vmcb->control.exit_int_info_err,
3065 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02003066
Joerg Roedel7597f122010-02-19 16:23:00 +01003067 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003068 if (!nested_vmcb)
3069 return 1;
3070
Joerg Roedel20307532010-11-29 17:51:48 +01003071 /* Exit Guest-Mode */
3072 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01003073 svm->nested.vmcb = 0;
3074
Alexander Grafcf74a782008-11-25 20:17:08 +01003075 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02003076 disable_gif(svm);
3077
3078 nested_vmcb->save.es = vmcb->save.es;
3079 nested_vmcb->save.cs = vmcb->save.cs;
3080 nested_vmcb->save.ss = vmcb->save.ss;
3081 nested_vmcb->save.ds = vmcb->save.ds;
3082 nested_vmcb->save.gdtr = vmcb->save.gdtr;
3083 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02003084 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003085 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003086 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003087 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003088 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03003089 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003090 nested_vmcb->save.rip = vmcb->save.rip;
3091 nested_vmcb->save.rsp = vmcb->save.rsp;
3092 nested_vmcb->save.rax = vmcb->save.rax;
3093 nested_vmcb->save.dr7 = vmcb->save.dr7;
3094 nested_vmcb->save.dr6 = vmcb->save.dr6;
3095 nested_vmcb->save.cpl = vmcb->save.cpl;
3096
3097 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
3098 nested_vmcb->control.int_vector = vmcb->control.int_vector;
3099 nested_vmcb->control.int_state = vmcb->control.int_state;
3100 nested_vmcb->control.exit_code = vmcb->control.exit_code;
3101 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
3102 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
3103 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
3104 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
3105 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003106
3107 if (svm->nrips_enabled)
3108 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02003109
3110 /*
3111 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
3112 * to make sure that we do not lose injected events. So check event_inj
3113 * here and copy it to exit_int_info if it is valid.
3114 * Exit_int_info and event_inj can't be both valid because the case
3115 * below only happens on a VMRUN instruction intercept which has
3116 * no valid exit_int_info set.
3117 */
3118 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
3119 struct vmcb_control_area *nc = &nested_vmcb->control;
3120
3121 nc->exit_int_info = vmcb->control.event_inj;
3122 nc->exit_int_info_err = vmcb->control.event_inj_err;
3123 }
3124
Joerg Roedel33740e42009-08-07 11:49:29 +02003125 nested_vmcb->control.tlb_ctl = 0;
3126 nested_vmcb->control.event_inj = 0;
3127 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01003128
3129 /* We always set V_INTR_MASKING and remember the old value in hflags */
3130 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
3131 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
3132
Alexander Grafcf74a782008-11-25 20:17:08 +01003133 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02003134 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01003135
Alexander Graf219b65d2009-06-15 15:21:25 +02003136 kvm_clear_exception_queue(&svm->vcpu);
3137 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003138
Joerg Roedel4b161842010-09-10 17:31:03 +02003139 svm->nested.nested_cr3 = 0;
3140
Alexander Grafcf74a782008-11-25 20:17:08 +01003141 /* Restore selected save entries */
3142 svm->vmcb->save.es = hsave->save.es;
3143 svm->vmcb->save.cs = hsave->save.cs;
3144 svm->vmcb->save.ss = hsave->save.ss;
3145 svm->vmcb->save.ds = hsave->save.ds;
3146 svm->vmcb->save.gdtr = hsave->save.gdtr;
3147 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003148 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01003149 svm_set_efer(&svm->vcpu, hsave->save.efer);
3150 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
3151 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
3152 if (npt_enabled) {
3153 svm->vmcb->save.cr3 = hsave->save.cr3;
3154 svm->vcpu.arch.cr3 = hsave->save.cr3;
3155 } else {
Avi Kivity23902182010-06-10 17:02:16 +03003156 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01003157 }
3158 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
3159 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
3160 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
3161 svm->vmcb->save.dr7 = 0;
3162 svm->vmcb->save.cpl = 0;
3163 svm->vmcb->control.exit_int_info = 0;
3164
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003165 mark_all_dirty(svm->vmcb);
3166
Joerg Roedel7597f122010-02-19 16:23:00 +01003167 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01003168
Joerg Roedel4b161842010-09-10 17:31:03 +02003169 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003170 kvm_mmu_reset_context(&svm->vcpu);
3171 kvm_mmu_load(&svm->vcpu);
3172
3173 return 0;
3174}
Alexander Graf3d6368e2008-11-25 20:17:07 +01003175
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003176static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003177{
Joerg Roedel323c3d82010-03-01 15:34:37 +01003178 /*
3179 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003180 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01003181 * the kvm msr permission bitmap may contain zero bits
3182 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01003183 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003184
Joerg Roedel323c3d82010-03-01 15:34:37 +01003185 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3186 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003187
Joerg Roedel323c3d82010-03-01 15:34:37 +01003188 for (i = 0; i < MSRPM_OFFSETS; i++) {
3189 u32 value, p;
3190 u64 offset;
3191
3192 if (msrpm_offsets[i] == 0xffffffff)
3193 break;
3194
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003195 p = msrpm_offsets[i];
3196 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01003197
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003198 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01003199 return false;
3200
3201 svm->nested.msrpm[p] = svm->msrpm[p] | value;
3202 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003203
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05003204 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
Alexander Graf3d6368e2008-11-25 20:17:07 +01003205
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003206 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003207}
3208
Joerg Roedel52c65a302010-08-02 16:46:44 +02003209static bool nested_vmcb_checks(struct vmcb *vmcb)
3210{
3211 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
3212 return false;
3213
Joerg Roedeldbe77582010-08-02 16:46:45 +02003214 if (vmcb->control.asid == 0)
3215 return false;
3216
Tom Lendackycea3a192017-12-04 10:57:24 -06003217 if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
3218 !npt_enabled)
Joerg Roedel4b161842010-09-10 17:31:03 +02003219 return false;
3220
Joerg Roedel52c65a302010-08-02 16:46:44 +02003221 return true;
3222}
3223
Ladi Prosekc2634062017-10-11 16:54:44 +02003224static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
3225 struct vmcb *nested_vmcb, struct page *page)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003226{
Avi Kivityf6e78472010-08-02 15:30:20 +03003227 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003228 svm->vcpu.arch.hflags |= HF_HIF_MASK;
3229 else
3230 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
3231
Tom Lendackycea3a192017-12-04 10:57:24 -06003232 if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
Joerg Roedel4b161842010-09-10 17:31:03 +02003233 kvm_mmu_unload(&svm->vcpu);
3234 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
3235 nested_svm_init_mmu_context(&svm->vcpu);
3236 }
3237
Alexander Graf3d6368e2008-11-25 20:17:07 +01003238 /* Load the nested guest state */
3239 svm->vmcb->save.es = nested_vmcb->save.es;
3240 svm->vmcb->save.cs = nested_vmcb->save.cs;
3241 svm->vmcb->save.ss = nested_vmcb->save.ss;
3242 svm->vmcb->save.ds = nested_vmcb->save.ds;
3243 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
3244 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003245 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003246 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
3247 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
3248 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
3249 if (npt_enabled) {
3250 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
3251 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003252 } else
Avi Kivity23902182010-06-10 17:02:16 +03003253 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003254
3255 /* Guest paging mode is active - reset mmu */
3256 kvm_mmu_reset_context(&svm->vcpu);
3257
Joerg Roedeldefbba52009-08-07 11:49:30 +02003258 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003259 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
3260 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
3261 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01003262
Alexander Graf3d6368e2008-11-25 20:17:07 +01003263 /* In case we don't even reach vcpu_run, the fields are not updated */
3264 svm->vmcb->save.rax = nested_vmcb->save.rax;
3265 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3266 svm->vmcb->save.rip = nested_vmcb->save.rip;
3267 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3268 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3269 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3270
Joerg Roedelf7138532010-03-01 15:34:40 +01003271 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003272 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003273
Joerg Roedelaad42c62009-08-07 11:49:34 +02003274 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003275 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003276 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02003277 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3278 svm->nested.intercept = nested_vmcb->control.intercept;
3279
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003280 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003281 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003282 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3283 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3284 else
3285 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3286
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003287 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3288 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003289 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3290 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003291 }
3292
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003293 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003294 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003295
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003296 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003297 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3298 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
3299 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003300 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3301 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3302
Joerg Roedel7597f122010-02-19 16:23:00 +01003303 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003304
Joerg Roedel20307532010-11-29 17:51:48 +01003305 /* Enter Guest-Mode */
3306 enter_guest_mode(&svm->vcpu);
3307
Joerg Roedel384c6362010-11-30 18:03:56 +01003308 /*
3309 * Merge guest and host intercepts - must be called with vcpu in
3310 * guest-mode to take affect here
3311 */
3312 recalc_intercepts(svm);
3313
Joerg Roedel06fc77722010-02-19 16:23:07 +01003314 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003315
Joerg Roedel2af91942009-08-07 11:49:28 +02003316 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003317
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003318 mark_all_dirty(svm->vmcb);
Ladi Prosekc2634062017-10-11 16:54:44 +02003319}
3320
3321static bool nested_svm_vmrun(struct vcpu_svm *svm)
3322{
3323 struct vmcb *nested_vmcb;
3324 struct vmcb *hsave = svm->nested.hsave;
3325 struct vmcb *vmcb = svm->vmcb;
3326 struct page *page;
3327 u64 vmcb_gpa;
3328
3329 vmcb_gpa = svm->vmcb->save.rax;
3330
3331 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
3332 if (!nested_vmcb)
3333 return false;
3334
3335 if (!nested_vmcb_checks(nested_vmcb)) {
3336 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3337 nested_vmcb->control.exit_code_hi = 0;
3338 nested_vmcb->control.exit_info_1 = 0;
3339 nested_vmcb->control.exit_info_2 = 0;
3340
3341 nested_svm_unmap(page);
3342
3343 return false;
3344 }
3345
3346 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3347 nested_vmcb->save.rip,
3348 nested_vmcb->control.int_ctl,
3349 nested_vmcb->control.event_inj,
3350 nested_vmcb->control.nested_ctl);
3351
3352 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3353 nested_vmcb->control.intercept_cr >> 16,
3354 nested_vmcb->control.intercept_exceptions,
3355 nested_vmcb->control.intercept);
3356
3357 /* Clear internal status */
3358 kvm_clear_exception_queue(&svm->vcpu);
3359 kvm_clear_interrupt_queue(&svm->vcpu);
3360
3361 /*
3362 * Save the old vmcb, so we don't need to pick what we save, but can
3363 * restore everything when a VMEXIT occurs
3364 */
3365 hsave->save.es = vmcb->save.es;
3366 hsave->save.cs = vmcb->save.cs;
3367 hsave->save.ss = vmcb->save.ss;
3368 hsave->save.ds = vmcb->save.ds;
3369 hsave->save.gdtr = vmcb->save.gdtr;
3370 hsave->save.idtr = vmcb->save.idtr;
3371 hsave->save.efer = svm->vcpu.arch.efer;
3372 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3373 hsave->save.cr4 = svm->vcpu.arch.cr4;
3374 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3375 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3376 hsave->save.rsp = vmcb->save.rsp;
3377 hsave->save.rax = vmcb->save.rax;
3378 if (npt_enabled)
3379 hsave->save.cr3 = vmcb->save.cr3;
3380 else
3381 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3382
3383 copy_vmcb_control_area(hsave, vmcb);
3384
3385 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003386
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003387 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003388}
3389
Joerg Roedel9966bf62009-08-07 11:49:40 +02003390static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01003391{
3392 to_vmcb->save.fs = from_vmcb->save.fs;
3393 to_vmcb->save.gs = from_vmcb->save.gs;
3394 to_vmcb->save.tr = from_vmcb->save.tr;
3395 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3396 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3397 to_vmcb->save.star = from_vmcb->save.star;
3398 to_vmcb->save.lstar = from_vmcb->save.lstar;
3399 to_vmcb->save.cstar = from_vmcb->save.cstar;
3400 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3401 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3402 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3403 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01003404}
3405
Avi Kivity851ba692009-08-24 11:10:17 +03003406static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003407{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003408 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003409 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003410 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003411
Alexander Graf55426752008-11-25 20:17:06 +01003412 if (nested_svm_check_permissions(svm))
3413 return 1;
3414
Joerg Roedel7597f122010-02-19 16:23:00 +01003415 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003416 if (!nested_vmcb)
3417 return 1;
3418
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003419 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003420 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003421
Joerg Roedel9966bf62009-08-07 11:49:40 +02003422 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003423 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003424
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003425 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003426}
3427
Avi Kivity851ba692009-08-24 11:10:17 +03003428static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003429{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003430 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003431 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003432 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003433
Alexander Graf55426752008-11-25 20:17:06 +01003434 if (nested_svm_check_permissions(svm))
3435 return 1;
3436
Joerg Roedel7597f122010-02-19 16:23:00 +01003437 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003438 if (!nested_vmcb)
3439 return 1;
3440
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003441 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003442 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003443
Joerg Roedel9966bf62009-08-07 11:49:40 +02003444 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003445 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003446
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003447 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003448}
3449
Avi Kivity851ba692009-08-24 11:10:17 +03003450static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003451{
Alexander Graf3d6368e2008-11-25 20:17:07 +01003452 if (nested_svm_check_permissions(svm))
3453 return 1;
3454
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02003455 /* Save rip after vmrun instruction */
3456 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003457
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003458 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01003459 return 1;
3460
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003461 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02003462 goto failed;
3463
3464 return 1;
3465
3466failed:
3467
3468 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3469 svm->vmcb->control.exit_code_hi = 0;
3470 svm->vmcb->control.exit_info_1 = 0;
3471 svm->vmcb->control.exit_info_2 = 0;
3472
3473 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003474
3475 return 1;
3476}
3477
Avi Kivity851ba692009-08-24 11:10:17 +03003478static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003479{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003480 int ret;
3481
Alexander Graf1371d902008-11-25 20:17:04 +01003482 if (nested_svm_check_permissions(svm))
3483 return 1;
3484
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003485 /*
3486 * If VGIF is enabled, the STGI intercept is only added to
Ladi Prosekcc3d9672017-10-17 16:02:39 +02003487 * detect the opening of the SMI/NMI window; remove it now.
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003488 */
3489 if (vgif_enabled(svm))
3490 clr_intercept(svm, INTERCEPT_STGI);
3491
Alexander Graf1371d902008-11-25 20:17:04 +01003492 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003493 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003494 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003495
Joerg Roedel2af91942009-08-07 11:49:28 +02003496 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003497
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003498 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003499}
3500
Avi Kivity851ba692009-08-24 11:10:17 +03003501static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003502{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003503 int ret;
3504
Alexander Graf1371d902008-11-25 20:17:04 +01003505 if (nested_svm_check_permissions(svm))
3506 return 1;
3507
3508 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003509 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003510
Joerg Roedel2af91942009-08-07 11:49:28 +02003511 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003512
3513 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05003514 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3515 svm_clear_vintr(svm);
3516 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3517 mark_dirty(svm->vmcb, VMCB_INTR);
3518 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003519
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003520 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003521}
3522
Avi Kivity851ba692009-08-24 11:10:17 +03003523static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02003524{
3525 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02003526
David Kaplan668f1982015-02-20 16:02:10 -06003527 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
3528 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02003529
Alexander Grafff092382009-06-15 15:21:24 +02003530 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06003531 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02003532
3533 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003534 return kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02003535}
3536
Joerg Roedel532a46b2009-10-09 16:08:32 +02003537static int skinit_interception(struct vcpu_svm *svm)
3538{
David Kaplan668f1982015-02-20 16:02:10 -06003539 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003540
3541 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3542 return 1;
3543}
3544
David Kaplandab429a2015-03-02 13:43:37 -06003545static int wbinvd_interception(struct vcpu_svm *svm)
3546{
Kyle Huey6affcbe2016-11-29 12:40:40 -08003547 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06003548}
3549
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003550static int xsetbv_interception(struct vcpu_svm *svm)
3551{
3552 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
3553 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3554
3555 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3556 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003557 return kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003558 }
3559
3560 return 1;
3561}
3562
Avi Kivity851ba692009-08-24 11:10:17 +03003563static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003564{
Izik Eidus37817f22008-03-24 23:14:53 +02003565 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003566 int reason;
3567 int int_type = svm->vmcb->control.exit_int_info &
3568 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003569 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003570 uint32_t type =
3571 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3572 uint32_t idt_v =
3573 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003574 bool has_error_code = false;
3575 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003576
3577 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003578
Izik Eidus37817f22008-03-24 23:14:53 +02003579 if (svm->vmcb->control.exit_info_2 &
3580 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003581 reason = TASK_SWITCH_IRET;
3582 else if (svm->vmcb->control.exit_info_2 &
3583 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3584 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003585 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003586 reason = TASK_SWITCH_GATE;
3587 else
3588 reason = TASK_SWITCH_CALL;
3589
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003590 if (reason == TASK_SWITCH_GATE) {
3591 switch (type) {
3592 case SVM_EXITINTINFO_TYPE_NMI:
3593 svm->vcpu.arch.nmi_injected = false;
3594 break;
3595 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003596 if (svm->vmcb->control.exit_info_2 &
3597 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3598 has_error_code = true;
3599 error_code =
3600 (u32)svm->vmcb->control.exit_info_2;
3601 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003602 kvm_clear_exception_queue(&svm->vcpu);
3603 break;
3604 case SVM_EXITINTINFO_TYPE_INTR:
3605 kvm_clear_interrupt_queue(&svm->vcpu);
3606 break;
3607 default:
3608 break;
3609 }
3610 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003611
Gleb Natapov8317c292009-04-12 13:37:02 +03003612 if (reason != TASK_SWITCH_GATE ||
3613 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3614 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03003615 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3616 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003617
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003618 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3619 int_vec = -1;
3620
3621 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03003622 has_error_code, error_code) == EMULATE_FAIL) {
3623 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3624 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3625 svm->vcpu.run->internal.ndata = 0;
3626 return 0;
3627 }
3628 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003629}
3630
Avi Kivity851ba692009-08-24 11:10:17 +03003631static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003632{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003633 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Kyle Huey6a908b62016-11-29 12:40:37 -08003634 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635}
3636
Avi Kivity851ba692009-08-24 11:10:17 +03003637static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003638{
3639 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003640 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003641 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003642 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003643 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003644 return 1;
3645}
3646
Avi Kivity851ba692009-08-24 11:10:17 +03003647static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003648{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003649 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3650 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3651
3652 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003653 return kvm_skip_emulated_instruction(&svm->vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003654}
3655
Avi Kivity851ba692009-08-24 11:10:17 +03003656static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003657{
Andre Przywara51d8b662010-12-21 11:12:02 +01003658 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003659}
3660
Avi Kivity332b56e2011-11-10 14:57:24 +02003661static int rdpmc_interception(struct vcpu_svm *svm)
3662{
3663 int err;
3664
3665 if (!static_cpu_has(X86_FEATURE_NRIPS))
3666 return emulate_on_interception(svm);
3667
3668 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08003669 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02003670}
3671
Xiubo Li52eb5a62015-03-13 17:39:45 +08003672static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3673 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003674{
3675 unsigned long cr0 = svm->vcpu.arch.cr0;
3676 bool ret = false;
3677 u64 intercept;
3678
3679 intercept = svm->nested.intercept;
3680
3681 if (!is_guest_mode(&svm->vcpu) ||
3682 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3683 return false;
3684
3685 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3686 val &= ~SVM_CR0_SELECTIVE_MASK;
3687
3688 if (cr0 ^ val) {
3689 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3690 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3691 }
3692
3693 return ret;
3694}
3695
Andre Przywara7ff76d52010-12-21 11:12:04 +01003696#define CR_VALID (1ULL << 63)
3697
3698static int cr_interception(struct vcpu_svm *svm)
3699{
3700 int reg, cr;
3701 unsigned long val;
3702 int err;
3703
3704 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3705 return emulate_on_interception(svm);
3706
3707 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3708 return emulate_on_interception(svm);
3709
3710 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003711 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3712 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3713 else
3714 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003715
3716 err = 0;
3717 if (cr >= 16) { /* mov to cr */
3718 cr -= 16;
3719 val = kvm_register_read(&svm->vcpu, reg);
3720 switch (cr) {
3721 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003722 if (!check_selective_cr0_intercepted(svm, val))
3723 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003724 else
3725 return 1;
3726
Andre Przywara7ff76d52010-12-21 11:12:04 +01003727 break;
3728 case 3:
3729 err = kvm_set_cr3(&svm->vcpu, val);
3730 break;
3731 case 4:
3732 err = kvm_set_cr4(&svm->vcpu, val);
3733 break;
3734 case 8:
3735 err = kvm_set_cr8(&svm->vcpu, val);
3736 break;
3737 default:
3738 WARN(1, "unhandled write to CR%d", cr);
3739 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3740 return 1;
3741 }
3742 } else { /* mov from cr */
3743 switch (cr) {
3744 case 0:
3745 val = kvm_read_cr0(&svm->vcpu);
3746 break;
3747 case 2:
3748 val = svm->vcpu.arch.cr2;
3749 break;
3750 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003751 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003752 break;
3753 case 4:
3754 val = kvm_read_cr4(&svm->vcpu);
3755 break;
3756 case 8:
3757 val = kvm_get_cr8(&svm->vcpu);
3758 break;
3759 default:
3760 WARN(1, "unhandled read from CR%d", cr);
3761 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3762 return 1;
3763 }
3764 kvm_register_write(&svm->vcpu, reg, val);
3765 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08003766 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003767}
3768
Andre Przywaracae37972010-12-21 11:12:05 +01003769static int dr_interception(struct vcpu_svm *svm)
3770{
3771 int reg, dr;
3772 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003773
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003774 if (svm->vcpu.guest_debug == 0) {
3775 /*
3776 * No more DR vmexits; force a reload of the debug registers
3777 * and reenter on this instruction. The next vmexit will
3778 * retrieve the full state of the debug registers.
3779 */
3780 clr_dr_intercepts(svm);
3781 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3782 return 1;
3783 }
3784
Andre Przywaracae37972010-12-21 11:12:05 +01003785 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3786 return emulate_on_interception(svm);
3787
3788 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3789 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3790
3791 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003792 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3793 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003794 val = kvm_register_read(&svm->vcpu, reg);
3795 kvm_set_dr(&svm->vcpu, dr - 16, val);
3796 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003797 if (!kvm_require_dr(&svm->vcpu, dr))
3798 return 1;
3799 kvm_get_dr(&svm->vcpu, dr, &val);
3800 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003801 }
3802
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003803 return kvm_skip_emulated_instruction(&svm->vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01003804}
3805
Avi Kivity851ba692009-08-24 11:10:17 +03003806static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003807{
Avi Kivity851ba692009-08-24 11:10:17 +03003808 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003809 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003810
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003811 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3812 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003813 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003814 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003815 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003816 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003817 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003818 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3819 return 0;
3820}
3821
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003822static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003823{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003824 struct vcpu_svm *svm = to_svm(vcpu);
3825
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003826 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303827 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003828 msr_info->data = svm->vmcb->control.tsc_offset +
Haozhong Zhang35181e82015-10-20 15:39:03 +08003829 kvm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003830
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831 break;
3832 }
Brian Gerst8c065852010-07-17 09:03:26 -04003833 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003834 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003835 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003836#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003837 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003838 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003839 break;
3840 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003841 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003842 break;
3843 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003844 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003845 break;
3846 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003847 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003848 break;
3849#endif
3850 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003851 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003852 break;
3853 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003854 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003855 break;
3856 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003857 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003859 case MSR_TSC_AUX:
3860 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3861 return 1;
3862 msr_info->data = svm->tsc_aux;
3863 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003864 /*
3865 * Nobody will change the following 5 values in the VMCB so we can
3866 * safely return them on rdmsr. They will always be 0 until LBRV is
3867 * implemented.
3868 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003869 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003870 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01003871 break;
3872 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003873 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003874 break;
3875 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003876 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003877 break;
3878 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003879 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003880 break;
3881 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003882 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003883 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003884 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003885 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003886 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003887 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003888 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003889 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003890 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003891 msr_info->data = 0x01000065;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003892 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01003893 case MSR_F15H_IC_CFG: {
3894
3895 int family, model;
3896
3897 family = guest_cpuid_family(vcpu);
3898 model = guest_cpuid_model(vcpu);
3899
3900 if (family < 0 || model < 0)
3901 return kvm_get_msr_common(vcpu, msr_info);
3902
3903 msr_info->data = 0;
3904
3905 if (family == 0x15 &&
3906 (model >= 0x2 && model < 0x20))
3907 msr_info->data = 0x1E;
3908 }
3909 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003911 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003912 }
3913 return 0;
3914}
3915
Avi Kivity851ba692009-08-24 11:10:17 +03003916static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003917{
David Kaplan668f1982015-02-20 16:02:10 -06003918 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003919 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003921 msr_info.index = ecx;
3922 msr_info.host_initiated = false;
3923 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02003924 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003925 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003926 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02003927 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003928 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003929
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003930 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
3931 msr_info.data & 0xffffffff);
3932 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
3933 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003934 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003935 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003937}
3938
Joerg Roedel4a810182010-02-24 18:59:15 +01003939static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3940{
3941 struct vcpu_svm *svm = to_svm(vcpu);
3942 int svm_dis, chg_mask;
3943
3944 if (data & ~SVM_VM_CR_VALID_MASK)
3945 return 1;
3946
3947 chg_mask = SVM_VM_CR_VALID_MASK;
3948
3949 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3950 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3951
3952 svm->nested.vm_cr_msr &= ~chg_mask;
3953 svm->nested.vm_cr_msr |= (data & chg_mask);
3954
3955 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3956
3957 /* check for svm_disable while efer.svme is set */
3958 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3959 return 1;
3960
3961 return 0;
3962}
3963
Will Auld8fe8ab42012-11-29 12:42:12 -08003964static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003965{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003966 struct vcpu_svm *svm = to_svm(vcpu);
3967
Will Auld8fe8ab42012-11-29 12:42:12 -08003968 u32 ecx = msr->index;
3969 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003970 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02003971 case MSR_IA32_CR_PAT:
3972 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3973 return 1;
3974 vcpu->arch.pat = data;
3975 svm->vmcb->save.g_pat = data;
3976 mark_dirty(svm->vmcb, VMCB_NPT);
3977 break;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003978 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003979 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003980 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003981 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003982 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003983 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003984#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003985 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003986 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003987 break;
3988 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003989 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003990 break;
3991 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003992 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003993 break;
3994 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003995 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003996 break;
3997#endif
3998 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003999 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004000 break;
4001 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02004002 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004003 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004004 break;
4005 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02004006 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004007 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004008 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004009 case MSR_TSC_AUX:
4010 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4011 return 1;
4012
4013 /*
4014 * This is rare, so we update the MSR here instead of using
4015 * direct_access_msrs. Doing that would require a rdmsr in
4016 * svm_vcpu_put.
4017 */
4018 svm->tsc_aux = data;
4019 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
4020 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01004021 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004022 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03004023 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
4024 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004025 break;
4026 }
4027 if (data & DEBUGCTL_RESERVED_BITS)
4028 return 1;
4029
4030 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01004031 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004032 if (data & (1ULL<<0))
4033 svm_enable_lbrv(svm);
4034 else
4035 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01004036 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004037 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02004038 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004039 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004040 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01004041 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004042 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03004043 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004044 break;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004045 case MSR_IA32_APICBASE:
4046 if (kvm_vcpu_apicv_active(vcpu))
4047 avic_update_vapic_bar(to_svm(vcpu), data);
4048 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004049 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08004050 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004051 }
4052 return 0;
4053}
4054
Avi Kivity851ba692009-08-24 11:10:17 +03004055static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056{
Will Auld8fe8ab42012-11-29 12:42:12 -08004057 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06004058 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
4059 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004060
Will Auld8fe8ab42012-11-29 12:42:12 -08004061 msr.data = data;
4062 msr.index = ecx;
4063 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004064
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004065 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03004066 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02004067 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004068 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004069 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004070 } else {
4071 trace_kvm_msr_write(ecx, data);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004072 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02004073 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074}
4075
Avi Kivity851ba692009-08-24 11:10:17 +03004076static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004077{
Rusty Russelle756fc62007-07-30 20:07:08 +10004078 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03004079 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004080 else
Avi Kivity851ba692009-08-24 11:10:17 +03004081 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004082}
4083
Avi Kivity851ba692009-08-24 11:10:17 +03004084static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08004085{
Avi Kivity3842d132010-07-27 12:30:24 +03004086 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01004087 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03004088 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004089 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08004090 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08004091 return 1;
4092}
4093
Mark Langsdorf565d0992009-10-06 14:25:02 -05004094static int pause_interception(struct vcpu_svm *svm)
4095{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004096 struct kvm_vcpu *vcpu = &svm->vcpu;
4097 bool in_kernel = (svm_get_cpl(vcpu) == 0);
4098
4099 kvm_vcpu_on_spin(vcpu, in_kernel);
Mark Langsdorf565d0992009-10-06 14:25:02 -05004100 return 1;
4101}
4102
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004103static int nop_interception(struct vcpu_svm *svm)
4104{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004105 return kvm_skip_emulated_instruction(&(svm->vcpu));
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004106}
4107
4108static int monitor_interception(struct vcpu_svm *svm)
4109{
4110 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
4111 return nop_interception(svm);
4112}
4113
4114static int mwait_interception(struct vcpu_svm *svm)
4115{
4116 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4117 return nop_interception(svm);
4118}
4119
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004120enum avic_ipi_failure_cause {
4121 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
4122 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
4123 AVIC_IPI_FAILURE_INVALID_TARGET,
4124 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
4125};
4126
4127static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
4128{
4129 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
4130 u32 icrl = svm->vmcb->control.exit_info_1;
4131 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03004132 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004133 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4134
4135 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
4136
4137 switch (id) {
4138 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
4139 /*
4140 * AVIC hardware handles the generation of
4141 * IPIs when the specified Message Type is Fixed
4142 * (also known as fixed delivery mode) and
4143 * the Trigger Mode is edge-triggered. The hardware
4144 * also supports self and broadcast delivery modes
4145 * specified via the Destination Shorthand(DSH)
4146 * field of the ICRL. Logical and physical APIC ID
4147 * formats are supported. All other IPI types cause
4148 * a #VMEXIT, which needs to emulated.
4149 */
4150 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
4151 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
4152 break;
4153 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
4154 int i;
4155 struct kvm_vcpu *vcpu;
4156 struct kvm *kvm = svm->vcpu.kvm;
4157 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4158
4159 /*
4160 * At this point, we expect that the AVIC HW has already
4161 * set the appropriate IRR bits on the valid target
4162 * vcpus. So, we just need to kick the appropriate vcpu.
4163 */
4164 kvm_for_each_vcpu(i, vcpu, kvm) {
4165 bool m = kvm_apic_match_dest(vcpu, apic,
4166 icrl & KVM_APIC_SHORT_MASK,
4167 GET_APIC_DEST_FIELD(icrh),
4168 icrl & KVM_APIC_DEST_MASK);
4169
4170 if (m && !avic_vcpu_is_running(vcpu))
4171 kvm_vcpu_wake_up(vcpu);
4172 }
4173 break;
4174 }
4175 case AVIC_IPI_FAILURE_INVALID_TARGET:
4176 break;
4177 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
4178 WARN_ONCE(1, "Invalid backing page\n");
4179 break;
4180 default:
4181 pr_err("Unknown IPI interception\n");
4182 }
4183
4184 return 1;
4185}
4186
4187static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
4188{
4189 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4190 int index;
4191 u32 *logical_apic_id_table;
4192 int dlid = GET_APIC_LOGICAL_ID(ldr);
4193
4194 if (!dlid)
4195 return NULL;
4196
4197 if (flat) { /* flat */
4198 index = ffs(dlid) - 1;
4199 if (index > 7)
4200 return NULL;
4201 } else { /* cluster */
4202 int cluster = (dlid & 0xf0) >> 4;
4203 int apic = ffs(dlid & 0x0f) - 1;
4204
4205 if ((apic < 0) || (apic > 7) ||
4206 (cluster >= 0xf))
4207 return NULL;
4208 index = (cluster << 2) + apic;
4209 }
4210
4211 logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page);
4212
4213 return &logical_apic_id_table[index];
4214}
4215
4216static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
4217 bool valid)
4218{
4219 bool flat;
4220 u32 *entry, new_entry;
4221
4222 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4223 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4224 if (!entry)
4225 return -EINVAL;
4226
4227 new_entry = READ_ONCE(*entry);
4228 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4229 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
4230 if (valid)
4231 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4232 else
4233 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4234 WRITE_ONCE(*entry, new_entry);
4235
4236 return 0;
4237}
4238
4239static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4240{
4241 int ret;
4242 struct vcpu_svm *svm = to_svm(vcpu);
4243 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
4244
4245 if (!ldr)
4246 return 1;
4247
4248 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
4249 if (ret && svm->ldr_reg) {
4250 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
4251 svm->ldr_reg = 0;
4252 } else {
4253 svm->ldr_reg = ldr;
4254 }
4255 return ret;
4256}
4257
4258static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4259{
4260 u64 *old, *new;
4261 struct vcpu_svm *svm = to_svm(vcpu);
4262 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
4263 u32 id = (apic_id_reg >> 24) & 0xff;
4264
4265 if (vcpu->vcpu_id == id)
4266 return 0;
4267
4268 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4269 new = avic_get_physical_id_entry(vcpu, id);
4270 if (!new || !old)
4271 return 1;
4272
4273 /* We need to move physical_id_entry to new offset */
4274 *new = *old;
4275 *old = 0ULL;
4276 to_svm(vcpu)->avic_physical_id_cache = new;
4277
4278 /*
4279 * Also update the guest physical APIC ID in the logical
4280 * APIC ID table entry if already setup the LDR.
4281 */
4282 if (svm->ldr_reg)
4283 avic_handle_ldr_update(vcpu);
4284
4285 return 0;
4286}
4287
4288static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
4289{
4290 struct vcpu_svm *svm = to_svm(vcpu);
4291 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4292 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
4293 u32 mod = (dfr >> 28) & 0xf;
4294
4295 /*
4296 * We assume that all local APICs are using the same type.
4297 * If this changes, we need to flush the AVIC logical
4298 * APID id table.
4299 */
4300 if (vm_data->ldr_mode == mod)
4301 return 0;
4302
4303 clear_page(page_address(vm_data->avic_logical_id_table_page));
4304 vm_data->ldr_mode = mod;
4305
4306 if (svm->ldr_reg)
4307 avic_handle_ldr_update(vcpu);
4308 return 0;
4309}
4310
4311static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4312{
4313 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4314 u32 offset = svm->vmcb->control.exit_info_1 &
4315 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4316
4317 switch (offset) {
4318 case APIC_ID:
4319 if (avic_handle_apic_id_update(&svm->vcpu))
4320 return 0;
4321 break;
4322 case APIC_LDR:
4323 if (avic_handle_ldr_update(&svm->vcpu))
4324 return 0;
4325 break;
4326 case APIC_DFR:
4327 avic_handle_dfr_update(&svm->vcpu);
4328 break;
4329 default:
4330 break;
4331 }
4332
4333 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4334
4335 return 1;
4336}
4337
4338static bool is_avic_unaccelerated_access_trap(u32 offset)
4339{
4340 bool ret = false;
4341
4342 switch (offset) {
4343 case APIC_ID:
4344 case APIC_EOI:
4345 case APIC_RRR:
4346 case APIC_LDR:
4347 case APIC_DFR:
4348 case APIC_SPIV:
4349 case APIC_ESR:
4350 case APIC_ICR:
4351 case APIC_LVTT:
4352 case APIC_LVTTHMR:
4353 case APIC_LVTPC:
4354 case APIC_LVT0:
4355 case APIC_LVT1:
4356 case APIC_LVTERR:
4357 case APIC_TMICT:
4358 case APIC_TDCR:
4359 ret = true;
4360 break;
4361 default:
4362 break;
4363 }
4364 return ret;
4365}
4366
4367static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4368{
4369 int ret = 0;
4370 u32 offset = svm->vmcb->control.exit_info_1 &
4371 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4372 u32 vector = svm->vmcb->control.exit_info_2 &
4373 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4374 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4375 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4376 bool trap = is_avic_unaccelerated_access_trap(offset);
4377
4378 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4379 trap, write, vector);
4380 if (trap) {
4381 /* Handling Trap */
4382 WARN_ONCE(!write, "svm: Handling trap read.\n");
4383 ret = avic_unaccel_trap_write(svm);
4384 } else {
4385 /* Handling Fault */
4386 ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
4387 }
4388
4389 return ret;
4390}
4391
Mathias Krause09941fb2012-08-30 01:30:20 +02004392static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01004393 [SVM_EXIT_READ_CR0] = cr_interception,
4394 [SVM_EXIT_READ_CR3] = cr_interception,
4395 [SVM_EXIT_READ_CR4] = cr_interception,
4396 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06004397 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02004398 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01004399 [SVM_EXIT_WRITE_CR3] = cr_interception,
4400 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004401 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01004402 [SVM_EXIT_READ_DR0] = dr_interception,
4403 [SVM_EXIT_READ_DR1] = dr_interception,
4404 [SVM_EXIT_READ_DR2] = dr_interception,
4405 [SVM_EXIT_READ_DR3] = dr_interception,
4406 [SVM_EXIT_READ_DR4] = dr_interception,
4407 [SVM_EXIT_READ_DR5] = dr_interception,
4408 [SVM_EXIT_READ_DR6] = dr_interception,
4409 [SVM_EXIT_READ_DR7] = dr_interception,
4410 [SVM_EXIT_WRITE_DR0] = dr_interception,
4411 [SVM_EXIT_WRITE_DR1] = dr_interception,
4412 [SVM_EXIT_WRITE_DR2] = dr_interception,
4413 [SVM_EXIT_WRITE_DR3] = dr_interception,
4414 [SVM_EXIT_WRITE_DR4] = dr_interception,
4415 [SVM_EXIT_WRITE_DR5] = dr_interception,
4416 [SVM_EXIT_WRITE_DR6] = dr_interception,
4417 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004418 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4419 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004420 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004421 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004422 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01004423 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004424 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02004425 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004426 [SVM_EXIT_SMI] = nop_on_interception,
4427 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08004428 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02004429 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004430 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004431 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02004432 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05004433 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004434 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004435 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02004436 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004437 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004438 [SVM_EXIT_MSR] = msr_interception,
4439 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08004440 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01004441 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02004442 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01004443 [SVM_EXIT_VMLOAD] = vmload_interception,
4444 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01004445 [SVM_EXIT_STGI] = stgi_interception,
4446 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02004447 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06004448 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004449 [SVM_EXIT_MONITOR] = monitor_interception,
4450 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01004451 [SVM_EXIT_XSETBV] = xsetbv_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02004452 [SVM_EXIT_NPF] = npf_interception,
Paolo Bonzini64d60672015-05-07 11:36:11 +02004453 [SVM_EXIT_RSM] = emulate_on_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004454 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4455 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004456};
4457
Joe Perchesae8cc052011-04-24 22:00:50 -07004458static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02004459{
4460 struct vcpu_svm *svm = to_svm(vcpu);
4461 struct vmcb_control_area *control = &svm->vmcb->control;
4462 struct vmcb_save_area *save = &svm->vmcb->save;
4463
4464 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004465 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4466 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4467 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4468 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4469 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4470 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4471 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
4472 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4473 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4474 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4475 pr_err("%-20s%d\n", "asid:", control->asid);
4476 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4477 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4478 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4479 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4480 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4481 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4482 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4483 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4484 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4485 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4486 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004487 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07004488 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4489 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05004490 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07004491 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004492 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4493 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4494 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004495 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004496 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4497 "es:",
4498 save->es.selector, save->es.attrib,
4499 save->es.limit, save->es.base);
4500 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4501 "cs:",
4502 save->cs.selector, save->cs.attrib,
4503 save->cs.limit, save->cs.base);
4504 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4505 "ss:",
4506 save->ss.selector, save->ss.attrib,
4507 save->ss.limit, save->ss.base);
4508 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4509 "ds:",
4510 save->ds.selector, save->ds.attrib,
4511 save->ds.limit, save->ds.base);
4512 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4513 "fs:",
4514 save->fs.selector, save->fs.attrib,
4515 save->fs.limit, save->fs.base);
4516 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4517 "gs:",
4518 save->gs.selector, save->gs.attrib,
4519 save->gs.limit, save->gs.base);
4520 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4521 "gdtr:",
4522 save->gdtr.selector, save->gdtr.attrib,
4523 save->gdtr.limit, save->gdtr.base);
4524 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4525 "ldtr:",
4526 save->ldtr.selector, save->ldtr.attrib,
4527 save->ldtr.limit, save->ldtr.base);
4528 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4529 "idtr:",
4530 save->idtr.selector, save->idtr.attrib,
4531 save->idtr.limit, save->idtr.base);
4532 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4533 "tr:",
4534 save->tr.selector, save->tr.attrib,
4535 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004536 pr_err("cpl: %d efer: %016llx\n",
4537 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004538 pr_err("%-15s %016llx %-13s %016llx\n",
4539 "cr0:", save->cr0, "cr2:", save->cr2);
4540 pr_err("%-15s %016llx %-13s %016llx\n",
4541 "cr3:", save->cr3, "cr4:", save->cr4);
4542 pr_err("%-15s %016llx %-13s %016llx\n",
4543 "dr6:", save->dr6, "dr7:", save->dr7);
4544 pr_err("%-15s %016llx %-13s %016llx\n",
4545 "rip:", save->rip, "rflags:", save->rflags);
4546 pr_err("%-15s %016llx %-13s %016llx\n",
4547 "rsp:", save->rsp, "rax:", save->rax);
4548 pr_err("%-15s %016llx %-13s %016llx\n",
4549 "star:", save->star, "lstar:", save->lstar);
4550 pr_err("%-15s %016llx %-13s %016llx\n",
4551 "cstar:", save->cstar, "sfmask:", save->sfmask);
4552 pr_err("%-15s %016llx %-13s %016llx\n",
4553 "kernel_gs_base:", save->kernel_gs_base,
4554 "sysenter_cs:", save->sysenter_cs);
4555 pr_err("%-15s %016llx %-13s %016llx\n",
4556 "sysenter_esp:", save->sysenter_esp,
4557 "sysenter_eip:", save->sysenter_eip);
4558 pr_err("%-15s %016llx %-13s %016llx\n",
4559 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4560 pr_err("%-15s %016llx %-13s %016llx\n",
4561 "br_from:", save->br_from, "br_to:", save->br_to);
4562 pr_err("%-15s %016llx %-13s %016llx\n",
4563 "excp_from:", save->last_excp_from,
4564 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004565}
4566
Avi Kivity586f9602010-11-18 13:09:54 +02004567static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4568{
4569 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4570
4571 *info1 = control->exit_info_1;
4572 *info2 = control->exit_info_2;
4573}
4574
Avi Kivity851ba692009-08-24 11:10:17 +03004575static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004576{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004577 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004578 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004579 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004580
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004581 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4582
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004583 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004584 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4585 if (npt_enabled)
4586 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004587
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004588 if (unlikely(svm->nested.exit_required)) {
4589 nested_svm_vmexit(svm);
4590 svm->nested.exit_required = false;
4591
4592 return 1;
4593 }
4594
Joerg Roedel20307532010-11-29 17:51:48 +01004595 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004596 int vmexit;
4597
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004598 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4599 svm->vmcb->control.exit_info_1,
4600 svm->vmcb->control.exit_info_2,
4601 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004602 svm->vmcb->control.exit_int_info_err,
4603 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004604
Joerg Roedel410e4d52009-08-07 11:49:44 +02004605 vmexit = nested_svm_exit_special(svm);
4606
4607 if (vmexit == NESTED_EXIT_CONTINUE)
4608 vmexit = nested_svm_exit_handled(svm);
4609
4610 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004611 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004612 }
4613
Joerg Roedela5c38322009-08-07 11:49:32 +02004614 svm_complete_interrupts(svm);
4615
Avi Kivity04d2cc72007-09-10 18:10:54 +03004616 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4617 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4618 kvm_run->fail_entry.hardware_entry_failure_reason
4619 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004620 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4621 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004622 return 0;
4623 }
4624
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004625 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004626 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004627 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4628 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004629 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004630 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004631 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004632 exit_code);
4633
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004634 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004635 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04004636 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03004637 kvm_queue_exception(vcpu, UD_VECTOR);
4638 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004639 }
4640
Avi Kivity851ba692009-08-24 11:10:17 +03004641 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004642}
4643
4644static void reload_tss(struct kvm_vcpu *vcpu)
4645{
4646 int cpu = raw_smp_processor_id();
4647
Tejun Heo0fe1e002009-10-29 22:34:14 +09004648 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4649 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004650 load_TR_desc();
4651}
4652
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004653static void pre_sev_run(struct vcpu_svm *svm, int cpu)
4654{
4655 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4656 int asid = sev_get_asid(svm->vcpu.kvm);
4657
4658 /* Assign the asid allocated with this SEV guest */
4659 svm->vmcb->control.asid = asid;
4660
4661 /*
4662 * Flush guest TLB:
4663 *
4664 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
4665 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
4666 */
4667 if (sd->sev_vmcbs[asid] == svm->vmcb &&
4668 svm->last_cpu == cpu)
4669 return;
4670
4671 svm->last_cpu = cpu;
4672 sd->sev_vmcbs[asid] = svm->vmcb;
4673 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4674 mark_dirty(svm->vmcb, VMCB_ASID);
4675}
4676
Rusty Russelle756fc62007-07-30 20:07:08 +10004677static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004678{
4679 int cpu = raw_smp_processor_id();
4680
Tejun Heo0fe1e002009-10-29 22:34:14 +09004681 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004682
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004683 if (sev_guest(svm->vcpu.kvm))
4684 return pre_sev_run(svm, cpu);
4685
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03004686 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09004687 if (svm->asid_generation != sd->asid_generation)
4688 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004689}
4690
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004691static void svm_inject_nmi(struct kvm_vcpu *vcpu)
4692{
4693 struct vcpu_svm *svm = to_svm(vcpu);
4694
4695 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
4696 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004697 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004698 ++vcpu->stat.nmi_injections;
4699}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004700
Eddie Dong85f455f2007-07-06 12:20:49 +03004701static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004702{
4703 struct vmcb_control_area *control;
4704
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004705 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10004706 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03004707 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708 control->int_ctl &= ~V_INTR_PRIO_MASK;
4709 control->int_ctl |= V_IRQ_MASK |
4710 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004711 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004712}
4713
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004714static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03004715{
4716 struct vcpu_svm *svm = to_svm(vcpu);
4717
Joerg Roedel2af91942009-08-07 11:49:28 +02004718 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01004719
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03004720 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
4721 ++vcpu->stat.irq_injections;
4722
Alexander Graf219b65d2009-06-15 15:21:25 +02004723 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
4724 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03004725}
4726
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004727static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
4728{
4729 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
4730}
4731
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004732static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
4733{
4734 struct vcpu_svm *svm = to_svm(vcpu);
4735
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004736 if (svm_nested_virtualize_tpr(vcpu) ||
4737 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004738 return;
4739
Radim Krčmář596f3142014-03-11 19:11:18 +01004740 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
4741
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004742 if (irr == -1)
4743 return;
4744
4745 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004746 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004747}
4748
Yang Zhang8d146952013-01-25 10:18:50 +08004749static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
4750{
4751 return;
4752}
4753
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05004754static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004755{
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05004756 return avic && irqchip_split(vcpu->kvm);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004757}
4758
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004759static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
4760{
4761}
4762
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02004763static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004764{
4765}
4766
4767/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03004768static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4769{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004770 struct vcpu_svm *svm = to_svm(vcpu);
4771 struct vmcb *vmcb = svm->vmcb;
4772
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05004773 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004774 return;
4775
4776 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
4777 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004778}
4779
Andrey Smetanin63086302015-11-10 15:36:32 +03004780static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004781{
4782 return;
4783}
4784
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004785static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
4786{
4787 kvm_lapic_set_irr(vec, vcpu->arch.apic);
4788 smp_mb__after_atomic();
4789
4790 if (avic_vcpu_is_running(vcpu))
4791 wrmsrl(SVM_AVIC_DOORBELL,
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05004792 kvm_cpu_get_apicid(vcpu->cpu));
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004793 else
4794 kvm_vcpu_wake_up(vcpu);
4795}
4796
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004797static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4798{
4799 unsigned long flags;
4800 struct amd_svm_iommu_ir *cur;
4801
4802 spin_lock_irqsave(&svm->ir_list_lock, flags);
4803 list_for_each_entry(cur, &svm->ir_list, node) {
4804 if (cur->data != pi->ir_data)
4805 continue;
4806 list_del(&cur->node);
4807 kfree(cur);
4808 break;
4809 }
4810 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4811}
4812
4813static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4814{
4815 int ret = 0;
4816 unsigned long flags;
4817 struct amd_svm_iommu_ir *ir;
4818
4819 /**
4820 * In some cases, the existing irte is updaed and re-set,
4821 * so we need to check here if it's already been * added
4822 * to the ir_list.
4823 */
4824 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
4825 struct kvm *kvm = svm->vcpu.kvm;
4826 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
4827 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
4828 struct vcpu_svm *prev_svm;
4829
4830 if (!prev_vcpu) {
4831 ret = -EINVAL;
4832 goto out;
4833 }
4834
4835 prev_svm = to_svm(prev_vcpu);
4836 svm_ir_list_del(prev_svm, pi);
4837 }
4838
4839 /**
4840 * Allocating new amd_iommu_pi_data, which will get
4841 * add to the per-vcpu ir_list.
4842 */
4843 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
4844 if (!ir) {
4845 ret = -ENOMEM;
4846 goto out;
4847 }
4848 ir->data = pi->ir_data;
4849
4850 spin_lock_irqsave(&svm->ir_list_lock, flags);
4851 list_add(&ir->node, &svm->ir_list);
4852 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4853out:
4854 return ret;
4855}
4856
4857/**
4858 * Note:
4859 * The HW cannot support posting multicast/broadcast
4860 * interrupts to a vCPU. So, we still use legacy interrupt
4861 * remapping for these kind of interrupts.
4862 *
4863 * For lowest-priority interrupts, we only support
4864 * those with single CPU as the destination, e.g. user
4865 * configures the interrupts via /proc/irq or uses
4866 * irqbalance to make the interrupts single-CPU.
4867 */
4868static int
4869get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
4870 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
4871{
4872 struct kvm_lapic_irq irq;
4873 struct kvm_vcpu *vcpu = NULL;
4874
4875 kvm_set_msi_irq(kvm, e, &irq);
4876
4877 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
4878 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
4879 __func__, irq.vector);
4880 return -1;
4881 }
4882
4883 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
4884 irq.vector);
4885 *svm = to_svm(vcpu);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05004886 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004887 vcpu_info->vector = irq.vector;
4888
4889 return 0;
4890}
4891
4892/*
4893 * svm_update_pi_irte - set IRTE for Posted-Interrupts
4894 *
4895 * @kvm: kvm
4896 * @host_irq: host irq of the interrupt
4897 * @guest_irq: gsi of the interrupt
4898 * @set: set or unset PI
4899 * returns 0 on success, < 0 on failure
4900 */
4901static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
4902 uint32_t guest_irq, bool set)
4903{
4904 struct kvm_kernel_irq_routing_entry *e;
4905 struct kvm_irq_routing_table *irq_rt;
4906 int idx, ret = -EINVAL;
4907
4908 if (!kvm_arch_has_assigned_device(kvm) ||
4909 !irq_remapping_cap(IRQ_POSTING_CAP))
4910 return 0;
4911
4912 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
4913 __func__, host_irq, guest_irq, set);
4914
4915 idx = srcu_read_lock(&kvm->irq_srcu);
4916 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
4917 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
4918
4919 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
4920 struct vcpu_data vcpu_info;
4921 struct vcpu_svm *svm = NULL;
4922
4923 if (e->type != KVM_IRQ_ROUTING_MSI)
4924 continue;
4925
4926 /**
4927 * Here, we setup with legacy mode in the following cases:
4928 * 1. When cannot target interrupt to a specific vcpu.
4929 * 2. Unsetting posted interrupt.
4930 * 3. APIC virtialization is disabled for the vcpu.
4931 */
4932 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
4933 kvm_vcpu_apicv_active(&svm->vcpu)) {
4934 struct amd_iommu_pi_data pi;
4935
4936 /* Try to enable guest_mode in IRTE */
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05004937 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
4938 AVIC_HPA_MASK);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004939 pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id,
4940 svm->vcpu.vcpu_id);
4941 pi.is_guest_mode = true;
4942 pi.vcpu_data = &vcpu_info;
4943 ret = irq_set_vcpu_affinity(host_irq, &pi);
4944
4945 /**
4946 * Here, we successfully setting up vcpu affinity in
4947 * IOMMU guest mode. Now, we need to store the posted
4948 * interrupt information in a per-vcpu ir_list so that
4949 * we can reference to them directly when we update vcpu
4950 * scheduling information in IOMMU irte.
4951 */
4952 if (!ret && pi.is_guest_mode)
4953 svm_ir_list_add(svm, &pi);
4954 } else {
4955 /* Use legacy mode in IRTE */
4956 struct amd_iommu_pi_data pi;
4957
4958 /**
4959 * Here, pi is used to:
4960 * - Tell IOMMU to use legacy mode for this interrupt.
4961 * - Retrieve ga_tag of prior interrupt remapping data.
4962 */
4963 pi.is_guest_mode = false;
4964 ret = irq_set_vcpu_affinity(host_irq, &pi);
4965
4966 /**
4967 * Check if the posted interrupt was previously
4968 * setup with the guest_mode by checking if the ga_tag
4969 * was cached. If so, we need to clean up the per-vcpu
4970 * ir_list.
4971 */
4972 if (!ret && pi.prev_ga_tag) {
4973 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
4974 struct kvm_vcpu *vcpu;
4975
4976 vcpu = kvm_get_vcpu_by_id(kvm, id);
4977 if (vcpu)
4978 svm_ir_list_del(to_svm(vcpu), &pi);
4979 }
4980 }
4981
4982 if (!ret && svm) {
4983 trace_kvm_pi_irte_update(svm->vcpu.vcpu_id,
4984 host_irq, e->gsi,
4985 vcpu_info.vector,
4986 vcpu_info.pi_desc_addr, set);
4987 }
4988
4989 if (ret < 0) {
4990 pr_err("%s: failed to update PI IRTE\n", __func__);
4991 goto out;
4992 }
4993 }
4994
4995 ret = 0;
4996out:
4997 srcu_read_unlock(&kvm->irq_srcu, idx);
4998 return ret;
4999}
5000
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005001static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005002{
5003 struct vcpu_svm *svm = to_svm(vcpu);
5004 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02005005 int ret;
5006 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
5007 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
5008 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
5009
5010 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005011}
5012
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005013static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
5014{
5015 struct vcpu_svm *svm = to_svm(vcpu);
5016
5017 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
5018}
5019
5020static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5021{
5022 struct vcpu_svm *svm = to_svm(vcpu);
5023
5024 if (masked) {
5025 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005026 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005027 } else {
5028 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005029 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005030 }
5031}
5032
Gleb Natapov78646122009-03-23 12:12:11 +02005033static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
5034{
5035 struct vcpu_svm *svm = to_svm(vcpu);
5036 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005037 int ret;
5038
5039 if (!gif_set(svm) ||
5040 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
5041 return 0;
5042
Avi Kivityf6e78472010-08-02 15:30:20 +03005043 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005044
Joerg Roedel20307532010-11-29 17:51:48 +01005045 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005046 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
5047
5048 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02005049}
5050
Jan Kiszkac9a79532014-03-07 20:03:15 +01005051static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03005052{
Alexander Graf219b65d2009-06-15 15:21:25 +02005053 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02005054
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005055 if (kvm_vcpu_apicv_active(vcpu))
5056 return;
5057
Joerg Roedele0231712010-02-24 18:59:10 +01005058 /*
5059 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
5060 * 1, because that's a separate STGI/VMRUN intercept. The next time we
5061 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005062 * we'll get the vintr intercept. However, if the vGIF feature is
5063 * enabled, the STGI interception will not occur. Enable the irq
5064 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01005065 */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005066 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02005067 svm_set_vintr(svm);
5068 svm_inject_irq(svm, 0x0);
5069 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005070}
5071
Jan Kiszkac9a79532014-03-07 20:03:15 +01005072static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005073{
Avi Kivity04d2cc72007-09-10 18:10:54 +03005074 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005075
Gleb Natapov44c11432009-05-11 13:35:52 +03005076 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
5077 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01005078 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03005079
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005080 if (!gif_set(svm)) {
5081 if (vgif_enabled(svm))
5082 set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005083 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005084 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005085
5086 if (svm->nested.exit_required)
5087 return; /* we're not going to run the guest yet */
5088
Joerg Roedele0231712010-02-24 18:59:10 +01005089 /*
5090 * Something prevents NMI from been injected. Single step over possible
5091 * problem (IRET or exception injection or interrupt shadow)
5092 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02005093 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02005094 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03005095 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03005096}
5097
Izik Eiduscbc94022007-10-25 00:29:55 +02005098static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
5099{
5100 return 0;
5101}
5102
Avi Kivityd9e368d2007-06-07 19:18:30 +03005103static void svm_flush_tlb(struct kvm_vcpu *vcpu)
5104{
Joerg Roedel38e5e922010-12-03 15:25:16 +01005105 struct vcpu_svm *svm = to_svm(vcpu);
5106
5107 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
5108 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5109 else
5110 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03005111}
5112
Avi Kivity04d2cc72007-09-10 18:10:54 +03005113static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
5114{
5115}
5116
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005117static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
5118{
5119 struct vcpu_svm *svm = to_svm(vcpu);
5120
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005121 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005122 return;
5123
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005124 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005125 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03005126 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005127 }
5128}
5129
Joerg Roedel649d6862008-04-16 16:51:15 +02005130static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
5131{
5132 struct vcpu_svm *svm = to_svm(vcpu);
5133 u64 cr8;
5134
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005135 if (svm_nested_virtualize_tpr(vcpu) ||
5136 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005137 return;
5138
Joerg Roedel649d6862008-04-16 16:51:15 +02005139 cr8 = kvm_get_cr8(vcpu);
5140 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
5141 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
5142}
5143
Gleb Natapov9222be12009-04-23 17:14:37 +03005144static void svm_complete_interrupts(struct vcpu_svm *svm)
5145{
5146 u8 vector;
5147 int type;
5148 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01005149 unsigned int3_injected = svm->int3_injected;
5150
5151 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005152
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02005153 /*
5154 * If we've made progress since setting HF_IRET_MASK, we've
5155 * executed an IRET and can allow NMI injection.
5156 */
5157 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
5158 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03005159 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03005160 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5161 }
Gleb Natapov44c11432009-05-11 13:35:52 +03005162
Gleb Natapov9222be12009-04-23 17:14:37 +03005163 svm->vcpu.arch.nmi_injected = false;
5164 kvm_clear_exception_queue(&svm->vcpu);
5165 kvm_clear_interrupt_queue(&svm->vcpu);
5166
5167 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
5168 return;
5169
Avi Kivity3842d132010-07-27 12:30:24 +03005170 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5171
Gleb Natapov9222be12009-04-23 17:14:37 +03005172 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
5173 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
5174
5175 switch (type) {
5176 case SVM_EXITINTINFO_TYPE_NMI:
5177 svm->vcpu.arch.nmi_injected = true;
5178 break;
5179 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01005180 /*
5181 * In case of software exceptions, do not reinject the vector,
5182 * but re-execute the instruction instead. Rewind RIP first
5183 * if we emulated INT3 before.
5184 */
5185 if (kvm_exception_is_soft(vector)) {
5186 if (vector == BP_VECTOR && int3_injected &&
5187 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
5188 kvm_rip_write(&svm->vcpu,
5189 kvm_rip_read(&svm->vcpu) -
5190 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02005191 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01005192 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005193 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
5194 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005195 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03005196
5197 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005198 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03005199 break;
5200 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005201 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03005202 break;
5203 default:
5204 break;
5205 }
5206}
5207
Avi Kivityb463a6f2010-07-20 15:06:17 +03005208static void svm_cancel_injection(struct kvm_vcpu *vcpu)
5209{
5210 struct vcpu_svm *svm = to_svm(vcpu);
5211 struct vmcb_control_area *control = &svm->vmcb->control;
5212
5213 control->exit_int_info = control->event_inj;
5214 control->exit_int_info_err = control->event_inj_err;
5215 control->event_inj = 0;
5216 svm_complete_interrupts(svm);
5217}
5218
Avi Kivity851ba692009-08-24 11:10:17 +03005219static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005220{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005221 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03005222
Joerg Roedel2041a062010-04-22 12:33:08 +02005223 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5224 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5225 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5226
Joerg Roedelcd3ff652009-10-09 16:08:26 +02005227 /*
5228 * A vmexit emulation is required before the vcpu can be executed
5229 * again.
5230 */
5231 if (unlikely(svm->nested.exit_required))
5232 return;
5233
Ladi Proseka12713c2017-06-21 09:07:00 +02005234 /*
5235 * Disable singlestep if we're injecting an interrupt/exception.
5236 * We don't want our modified rflags to be pushed on the stack where
5237 * we might not be able to easily reset them if we disabled NMI
5238 * singlestep later.
5239 */
5240 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5241 /*
5242 * Event injection happens before external interrupts cause a
5243 * vmexit and interrupts are disabled here, so smp_send_reschedule
5244 * is enough to force an immediate vmexit.
5245 */
5246 disable_nmi_singlestep(svm);
5247 smp_send_reschedule(vcpu->cpu);
5248 }
5249
Rusty Russelle756fc62007-07-30 20:07:08 +10005250 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005251
Joerg Roedel649d6862008-04-16 16:51:15 +02005252 sync_lapic_to_cr8(vcpu);
5253
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02005254 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005255
Avi Kivity04d2cc72007-09-10 18:10:54 +03005256 clgi();
5257
5258 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08005259
Avi Kivity6aa8b732006-12-10 02:21:36 -08005260 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03005261 "push %%" _ASM_BP "; \n\t"
5262 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5263 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5264 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5265 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5266 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5267 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005268#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005269 "mov %c[r8](%[svm]), %%r8 \n\t"
5270 "mov %c[r9](%[svm]), %%r9 \n\t"
5271 "mov %c[r10](%[svm]), %%r10 \n\t"
5272 "mov %c[r11](%[svm]), %%r11 \n\t"
5273 "mov %c[r12](%[svm]), %%r12 \n\t"
5274 "mov %c[r13](%[svm]), %%r13 \n\t"
5275 "mov %c[r14](%[svm]), %%r14 \n\t"
5276 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005277#endif
5278
Avi Kivity6aa8b732006-12-10 02:21:36 -08005279 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03005280 "push %%" _ASM_AX " \n\t"
5281 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03005282 __ex(SVM_VMLOAD) "\n\t"
5283 __ex(SVM_VMRUN) "\n\t"
5284 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005285 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005286
5287 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03005288 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5289 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5290 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5291 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5292 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5293 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005294#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005295 "mov %%r8, %c[r8](%[svm]) \n\t"
5296 "mov %%r9, %c[r9](%[svm]) \n\t"
5297 "mov %%r10, %c[r10](%[svm]) \n\t"
5298 "mov %%r11, %c[r11](%[svm]) \n\t"
5299 "mov %%r12, %c[r12](%[svm]) \n\t"
5300 "mov %%r13, %c[r13](%[svm]) \n\t"
5301 "mov %%r14, %c[r14](%[svm]) \n\t"
5302 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005303#endif
Avi Kivity74547662012-09-16 15:10:59 +03005304 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08005305 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005306 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08005307 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005308 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5309 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5310 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5311 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5312 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5313 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005314#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005315 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5316 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5317 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5318 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5319 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5320 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5321 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5322 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005323#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02005324 : "cc", "memory"
5325#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03005326 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005327 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03005328#else
5329 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005330#endif
5331 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08005332
Avi Kivity82ca2d12010-10-21 12:20:34 +02005333#ifdef CONFIG_X86_64
5334 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5335#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02005336 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02005337#ifndef CONFIG_X86_32_LAZY_GS
5338 loadsegment(gs, svm->host.gs);
5339#endif
Avi Kivity9581d442010-10-19 16:46:55 +02005340#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005341
5342 reload_tss(vcpu);
5343
Avi Kivity56ba47d2007-11-07 17:14:18 +02005344 local_irq_disable();
5345
Avi Kivity13c34e02010-10-21 12:20:31 +02005346 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5347 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5348 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5349 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5350
Joerg Roedel3781c012011-01-14 16:45:02 +01005351 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5352 kvm_before_handle_nmi(&svm->vcpu);
5353
5354 stgi();
5355
5356 /* Any pending NMI will happen here */
5357
5358 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5359 kvm_after_handle_nmi(&svm->vcpu);
5360
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005361 sync_cr8_to_lapic(vcpu);
5362
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005363 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005364
Joerg Roedel38e5e922010-12-03 15:25:16 +01005365 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5366
Gleb Natapov631bc482010-10-14 11:22:52 +02005367 /* if exit due to PF check for async PF */
5368 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005369 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
Gleb Natapov631bc482010-10-14 11:22:52 +02005370
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005371 if (npt_enabled) {
5372 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5373 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5374 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02005375
5376 /*
5377 * We need to handle MC intercepts here before the vcpu has a chance to
5378 * change the physical cpu
5379 */
5380 if (unlikely(svm->vmcb->control.exit_code ==
5381 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5382 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01005383
5384 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005385}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005386STACK_FRAME_NON_STANDARD(svm_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005387
Avi Kivity6aa8b732006-12-10 02:21:36 -08005388static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5389{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005390 struct vcpu_svm *svm = to_svm(vcpu);
5391
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005392 svm->vmcb->save.cr3 = __sme_set(root);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005393 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01005394 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005395}
5396
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005397static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5398{
5399 struct vcpu_svm *svm = to_svm(vcpu);
5400
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005401 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01005402 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005403
5404 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02005405 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005406 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005407
Joerg Roedelf40f6a42010-12-03 15:25:15 +01005408 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005409}
5410
Avi Kivity6aa8b732006-12-10 02:21:36 -08005411static int is_disabled(void)
5412{
Joerg Roedel6031a612007-06-22 12:29:50 +03005413 u64 vm_cr;
5414
5415 rdmsrl(MSR_VM_CR, vm_cr);
5416 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5417 return 1;
5418
Avi Kivity6aa8b732006-12-10 02:21:36 -08005419 return 0;
5420}
5421
Ingo Molnar102d8322007-02-19 14:37:47 +02005422static void
5423svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5424{
5425 /*
5426 * Patch in the VMMCALL instruction:
5427 */
5428 hypercall[0] = 0x0f;
5429 hypercall[1] = 0x01;
5430 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02005431}
5432
Yang, Sheng002c7f72007-07-31 14:23:01 +03005433static void svm_check_processor_compat(void *rtn)
5434{
5435 *(int *)rtn = 0;
5436}
5437
Avi Kivity774ead32007-12-26 13:57:04 +02005438static bool svm_cpu_has_accelerated_tpr(void)
5439{
5440 return false;
5441}
5442
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005443static bool svm_has_high_real_mode_segbase(void)
5444{
5445 return true;
5446}
5447
Paolo Bonzinifc07e762015-10-01 13:20:22 +02005448static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5449{
5450 return 0;
5451}
5452
Sheng Yang0e851882009-12-18 16:48:46 +08005453static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5454{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005455 struct vcpu_svm *svm = to_svm(vcpu);
5456
5457 /* Update nrips enabled cache */
Radim Krčmářd6321d42017-08-05 00:12:49 +02005458 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005459
5460 if (!kvm_vcpu_apicv_active(vcpu))
5461 return;
5462
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02005463 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08005464}
5465
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005466static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5467{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005468 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005469 case 0x1:
5470 if (avic)
5471 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5472 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02005473 case 0x80000001:
5474 if (nested)
5475 entry->ecx |= (1 << 2); /* Set SVM bit */
5476 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005477 case 0x8000000A:
5478 entry->eax = 1; /* SVM revision 1 */
5479 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5480 ASID emulation to nested SVM */
5481 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02005482 entry->edx = 0; /* Per default do not support any
5483 additional features */
5484
5485 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02005486 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02005487 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005488
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005489 /* Support NPT for the guest if enabled */
5490 if (npt_enabled)
5491 entry->edx |= SVM_FEATURE_NPT;
5492
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005493 break;
Brijesh Singh8765d752017-12-04 10:57:25 -06005494 case 0x8000001F:
5495 /* Support memory encryption cpuid if host supports it */
5496 if (boot_cpu_has(X86_FEATURE_SEV))
5497 cpuid(0x8000001f, &entry->eax, &entry->ebx,
5498 &entry->ecx, &entry->edx);
5499
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005500 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005501}
5502
Sheng Yang17cc3932010-01-05 19:02:27 +08005503static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005504{
Sheng Yang17cc3932010-01-05 19:02:27 +08005505 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005506}
5507
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005508static bool svm_rdtscp_supported(void)
5509{
Paolo Bonzini46896c72015-11-12 14:49:16 +01005510 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005511}
5512
Mao, Junjiead756a12012-07-02 01:18:48 +00005513static bool svm_invpcid_supported(void)
5514{
5515 return false;
5516}
5517
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005518static bool svm_mpx_supported(void)
5519{
5520 return false;
5521}
5522
Wanpeng Li55412b22014-12-02 19:21:30 +08005523static bool svm_xsaves_supported(void)
5524{
5525 return false;
5526}
5527
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005528static bool svm_has_wbinvd_exit(void)
5529{
5530 return true;
5531}
5532
Joerg Roedel80612522011-04-04 12:39:33 +02005533#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005534 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005535#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005536 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005537#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005538 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005539
Mathias Krause09941fb2012-08-30 01:30:20 +02005540static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005541 u32 exit_code;
5542 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005543} x86_intercept_map[] = {
5544 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
5545 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
5546 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
5547 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
5548 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02005549 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
5550 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02005551 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
5552 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
5553 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
5554 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
5555 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
5556 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
5557 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
5558 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02005559 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
5560 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
5561 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
5562 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
5563 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
5564 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
5565 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
5566 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005567 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
5568 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
5569 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02005570 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
5571 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
5572 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
5573 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
5574 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
5575 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
5576 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
5577 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
5578 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02005579 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
5580 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
5581 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
5582 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
5583 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
5584 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
5585 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02005586 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
5587 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
5588 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
5589 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005590};
5591
Joerg Roedel80612522011-04-04 12:39:33 +02005592#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005593#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005594#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005595
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005596static int svm_check_intercept(struct kvm_vcpu *vcpu,
5597 struct x86_instruction_info *info,
5598 enum x86_intercept_stage stage)
5599{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005600 struct vcpu_svm *svm = to_svm(vcpu);
5601 int vmexit, ret = X86EMUL_CONTINUE;
5602 struct __x86_intercept icpt_info;
5603 struct vmcb *vmcb = svm->vmcb;
5604
5605 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
5606 goto out;
5607
5608 icpt_info = x86_intercept_map[info->intercept];
5609
Avi Kivity40e19b52011-04-21 12:35:41 +03005610 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005611 goto out;
5612
5613 switch (icpt_info.exit_code) {
5614 case SVM_EXIT_READ_CR0:
5615 if (info->intercept == x86_intercept_cr_read)
5616 icpt_info.exit_code += info->modrm_reg;
5617 break;
5618 case SVM_EXIT_WRITE_CR0: {
5619 unsigned long cr0, val;
5620 u64 intercept;
5621
5622 if (info->intercept == x86_intercept_cr_write)
5623 icpt_info.exit_code += info->modrm_reg;
5624
Jan Kiszka62baf442014-06-29 21:55:53 +02005625 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
5626 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005627 break;
5628
5629 intercept = svm->nested.intercept;
5630
5631 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
5632 break;
5633
5634 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
5635 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
5636
5637 if (info->intercept == x86_intercept_lmsw) {
5638 cr0 &= 0xfUL;
5639 val &= 0xfUL;
5640 /* lmsw can't clear PE - catch this here */
5641 if (cr0 & X86_CR0_PE)
5642 val |= X86_CR0_PE;
5643 }
5644
5645 if (cr0 ^ val)
5646 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
5647
5648 break;
5649 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02005650 case SVM_EXIT_READ_DR0:
5651 case SVM_EXIT_WRITE_DR0:
5652 icpt_info.exit_code += info->modrm_reg;
5653 break;
Joerg Roedel80612522011-04-04 12:39:33 +02005654 case SVM_EXIT_MSR:
5655 if (info->intercept == x86_intercept_wrmsr)
5656 vmcb->control.exit_info_1 = 1;
5657 else
5658 vmcb->control.exit_info_1 = 0;
5659 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02005660 case SVM_EXIT_PAUSE:
5661 /*
5662 * We get this for NOP only, but pause
5663 * is rep not, check this here
5664 */
5665 if (info->rep_prefix != REPE_PREFIX)
5666 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02005667 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02005668 case SVM_EXIT_IOIO: {
5669 u64 exit_info;
5670 u32 bytes;
5671
Joerg Roedelf6511932011-04-04 12:39:35 +02005672 if (info->intercept == x86_intercept_in ||
5673 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005674 exit_info = ((info->src_val & 0xffff) << 16) |
5675 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02005676 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02005677 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005678 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02005679 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02005680 }
5681
5682 if (info->intercept == x86_intercept_outs ||
5683 info->intercept == x86_intercept_ins)
5684 exit_info |= SVM_IOIO_STR_MASK;
5685
5686 if (info->rep_prefix)
5687 exit_info |= SVM_IOIO_REP_MASK;
5688
5689 bytes = min(bytes, 4u);
5690
5691 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
5692
5693 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
5694
5695 vmcb->control.exit_info_1 = exit_info;
5696 vmcb->control.exit_info_2 = info->next_rip;
5697
5698 break;
5699 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005700 default:
5701 break;
5702 }
5703
Bandan Dasf1047652015-06-11 02:05:33 -04005704 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
5705 if (static_cpu_has(X86_FEATURE_NRIPS))
5706 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005707 vmcb->control.exit_code = icpt_info.exit_code;
5708 vmexit = nested_svm_exit_handled(svm);
5709
5710 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
5711 : X86EMUL_CONTINUE;
5712
5713out:
5714 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005715}
5716
Yang Zhanga547c6d2013-04-11 19:25:10 +08005717static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
5718{
5719 local_irq_enable();
Paolo Bonzinif2485b32016-06-15 15:23:11 +02005720 /*
5721 * We must have an instruction with interrupts enabled, so
5722 * the timer interrupt isn't delayed by the interrupt shadow.
5723 */
5724 asm("nop");
5725 local_irq_disable();
Yang Zhanga547c6d2013-04-11 19:25:10 +08005726}
5727
Radim Krčmářae97a3b2014-08-21 18:08:06 +02005728static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
5729{
5730}
5731
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05005732static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
5733{
5734 if (avic_handle_apic_id_update(vcpu) != 0)
5735 return;
5736 if (avic_handle_dfr_update(vcpu) != 0)
5737 return;
5738 avic_handle_ldr_update(vcpu);
5739}
5740
Borislav Petkov74f16902017-03-26 23:51:24 +02005741static void svm_setup_mce(struct kvm_vcpu *vcpu)
5742{
5743 /* [63:9] are reserved. */
5744 vcpu->arch.mcg_cap &= 0x1ff;
5745}
5746
Ladi Prosek72d7b372017-10-11 16:54:41 +02005747static int svm_smi_allowed(struct kvm_vcpu *vcpu)
5748{
Ladi Prosek05cade72017-10-11 16:54:45 +02005749 struct vcpu_svm *svm = to_svm(vcpu);
5750
5751 /* Per APM Vol.2 15.22.2 "Response to SMI" */
5752 if (!gif_set(svm))
5753 return 0;
5754
5755 if (is_guest_mode(&svm->vcpu) &&
5756 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
5757 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
5758 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
5759 svm->nested.exit_required = true;
5760 return 0;
5761 }
5762
Ladi Prosek72d7b372017-10-11 16:54:41 +02005763 return 1;
5764}
5765
Ladi Prosek0234bf82017-10-11 16:54:40 +02005766static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
5767{
Ladi Prosek05cade72017-10-11 16:54:45 +02005768 struct vcpu_svm *svm = to_svm(vcpu);
5769 int ret;
5770
5771 if (is_guest_mode(vcpu)) {
5772 /* FED8h - SVM Guest */
5773 put_smstate(u64, smstate, 0x7ed8, 1);
5774 /* FEE0h - SVM Guest VMCB Physical Address */
5775 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
5776
5777 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5778 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5779 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5780
5781 ret = nested_svm_vmexit(svm);
5782 if (ret)
5783 return ret;
5784 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02005785 return 0;
5786}
5787
5788static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
5789{
Ladi Prosek05cade72017-10-11 16:54:45 +02005790 struct vcpu_svm *svm = to_svm(vcpu);
5791 struct vmcb *nested_vmcb;
5792 struct page *page;
5793 struct {
5794 u64 guest;
5795 u64 vmcb;
5796 } svm_state_save;
5797 int ret;
5798
5799 ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save,
5800 sizeof(svm_state_save));
5801 if (ret)
5802 return ret;
5803
5804 if (svm_state_save.guest) {
5805 vcpu->arch.hflags &= ~HF_SMM_MASK;
5806 nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page);
5807 if (nested_vmcb)
5808 enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page);
5809 else
5810 ret = 1;
5811 vcpu->arch.hflags |= HF_SMM_MASK;
5812 }
5813 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02005814}
5815
Ladi Prosekcc3d9672017-10-17 16:02:39 +02005816static int enable_smi_window(struct kvm_vcpu *vcpu)
5817{
5818 struct vcpu_svm *svm = to_svm(vcpu);
5819
5820 if (!gif_set(svm)) {
5821 if (vgif_enabled(svm))
5822 set_intercept(svm, INTERCEPT_STGI);
5823 /* STGI will cause a vm exit */
5824 return 1;
5825 }
5826 return 0;
5827}
5828
Brijesh Singh1654efc2017-12-04 10:57:34 -06005829static int sev_asid_new(void)
5830{
5831 int pos;
5832
5833 /*
5834 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
5835 */
5836 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
5837 if (pos >= max_sev_asid)
5838 return -EBUSY;
5839
5840 set_bit(pos, sev_asid_bitmap);
5841 return pos + 1;
5842}
5843
5844static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
5845{
5846 struct kvm_sev_info *sev = &kvm->arch.sev_info;
5847 int asid, ret;
5848
5849 ret = -EBUSY;
5850 asid = sev_asid_new();
5851 if (asid < 0)
5852 return ret;
5853
5854 ret = sev_platform_init(&argp->error);
5855 if (ret)
5856 goto e_free;
5857
5858 sev->active = true;
5859 sev->asid = asid;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06005860 INIT_LIST_HEAD(&sev->regions_list);
Brijesh Singh1654efc2017-12-04 10:57:34 -06005861
5862 return 0;
5863
5864e_free:
5865 __sev_asid_free(asid);
5866 return ret;
5867}
5868
Brijesh Singh59414c92017-12-04 10:57:35 -06005869static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
5870{
5871 struct sev_data_activate *data;
5872 int asid = sev_get_asid(kvm);
5873 int ret;
5874
5875 wbinvd_on_all_cpus();
5876
5877 ret = sev_guest_df_flush(error);
5878 if (ret)
5879 return ret;
5880
5881 data = kzalloc(sizeof(*data), GFP_KERNEL);
5882 if (!data)
5883 return -ENOMEM;
5884
5885 /* activate ASID on the given handle */
5886 data->handle = handle;
5887 data->asid = asid;
5888 ret = sev_guest_activate(data, error);
5889 kfree(data);
5890
5891 return ret;
5892}
5893
Brijesh Singh89c50582017-12-04 10:57:35 -06005894static int __sev_issue_cmd(int fd, int id, void *data, int *error)
Brijesh Singh59414c92017-12-04 10:57:35 -06005895{
5896 struct fd f;
5897 int ret;
5898
5899 f = fdget(fd);
5900 if (!f.file)
5901 return -EBADF;
5902
5903 ret = sev_issue_cmd_external_user(f.file, id, data, error);
5904
5905 fdput(f);
5906 return ret;
5907}
5908
Brijesh Singh89c50582017-12-04 10:57:35 -06005909static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
5910{
5911 struct kvm_sev_info *sev = &kvm->arch.sev_info;
5912
5913 return __sev_issue_cmd(sev->fd, id, data, error);
5914}
5915
Brijesh Singh59414c92017-12-04 10:57:35 -06005916static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
5917{
5918 struct kvm_sev_info *sev = &kvm->arch.sev_info;
5919 struct sev_data_launch_start *start;
5920 struct kvm_sev_launch_start params;
5921 void *dh_blob, *session_blob;
5922 int *error = &argp->error;
5923 int ret;
5924
5925 if (!sev_guest(kvm))
5926 return -ENOTTY;
5927
5928 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
5929 return -EFAULT;
5930
5931 start = kzalloc(sizeof(*start), GFP_KERNEL);
5932 if (!start)
5933 return -ENOMEM;
5934
5935 dh_blob = NULL;
5936 if (params.dh_uaddr) {
5937 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
5938 if (IS_ERR(dh_blob)) {
5939 ret = PTR_ERR(dh_blob);
5940 goto e_free;
5941 }
5942
5943 start->dh_cert_address = __sme_set(__pa(dh_blob));
5944 start->dh_cert_len = params.dh_len;
5945 }
5946
5947 session_blob = NULL;
5948 if (params.session_uaddr) {
5949 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
5950 if (IS_ERR(session_blob)) {
5951 ret = PTR_ERR(session_blob);
5952 goto e_free_dh;
5953 }
5954
5955 start->session_address = __sme_set(__pa(session_blob));
5956 start->session_len = params.session_len;
5957 }
5958
5959 start->handle = params.handle;
5960 start->policy = params.policy;
5961
5962 /* create memory encryption context */
Brijesh Singh89c50582017-12-04 10:57:35 -06005963 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
Brijesh Singh59414c92017-12-04 10:57:35 -06005964 if (ret)
5965 goto e_free_session;
5966
5967 /* Bind ASID to this guest */
5968 ret = sev_bind_asid(kvm, start->handle, error);
5969 if (ret)
5970 goto e_free_session;
5971
5972 /* return handle to userspace */
5973 params.handle = start->handle;
5974 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
5975 sev_unbind_asid(kvm, start->handle);
5976 ret = -EFAULT;
5977 goto e_free_session;
5978 }
5979
5980 sev->handle = start->handle;
5981 sev->fd = argp->sev_fd;
5982
5983e_free_session:
5984 kfree(session_blob);
5985e_free_dh:
5986 kfree(dh_blob);
5987e_free:
5988 kfree(start);
5989 return ret;
5990}
5991
Brijesh Singh89c50582017-12-04 10:57:35 -06005992static int get_num_contig_pages(int idx, struct page **inpages,
5993 unsigned long npages)
5994{
5995 unsigned long paddr, next_paddr;
5996 int i = idx + 1, pages = 1;
5997
5998 /* find the number of contiguous pages starting from idx */
5999 paddr = __sme_page_pa(inpages[idx]);
6000 while (i < npages) {
6001 next_paddr = __sme_page_pa(inpages[i++]);
6002 if ((paddr + PAGE_SIZE) == next_paddr) {
6003 pages++;
6004 paddr = next_paddr;
6005 continue;
6006 }
6007 break;
6008 }
6009
6010 return pages;
6011}
6012
6013static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
6014{
6015 unsigned long vaddr, vaddr_end, next_vaddr, npages, size;
6016 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6017 struct kvm_sev_launch_update_data params;
6018 struct sev_data_launch_update_data *data;
6019 struct page **inpages;
6020 int i, ret, pages;
6021
6022 if (!sev_guest(kvm))
6023 return -ENOTTY;
6024
6025 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6026 return -EFAULT;
6027
6028 data = kzalloc(sizeof(*data), GFP_KERNEL);
6029 if (!data)
6030 return -ENOMEM;
6031
6032 vaddr = params.uaddr;
6033 size = params.len;
6034 vaddr_end = vaddr + size;
6035
6036 /* Lock the user memory. */
6037 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
6038 if (!inpages) {
6039 ret = -ENOMEM;
6040 goto e_free;
6041 }
6042
6043 /*
6044 * The LAUNCH_UPDATE command will perform in-place encryption of the
6045 * memory content (i.e it will write the same memory region with C=1).
6046 * It's possible that the cache may contain the data with C=0, i.e.,
6047 * unencrypted so invalidate it first.
6048 */
6049 sev_clflush_pages(inpages, npages);
6050
6051 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
6052 int offset, len;
6053
6054 /*
6055 * If the user buffer is not page-aligned, calculate the offset
6056 * within the page.
6057 */
6058 offset = vaddr & (PAGE_SIZE - 1);
6059
6060 /* Calculate the number of pages that can be encrypted in one go. */
6061 pages = get_num_contig_pages(i, inpages, npages);
6062
6063 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
6064
6065 data->handle = sev->handle;
6066 data->len = len;
6067 data->address = __sme_page_pa(inpages[i]) + offset;
6068 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
6069 if (ret)
6070 goto e_unpin;
6071
6072 size -= len;
6073 next_vaddr = vaddr + len;
6074 }
6075
6076e_unpin:
6077 /* content of memory is updated, mark pages dirty */
6078 for (i = 0; i < npages; i++) {
6079 set_page_dirty_lock(inpages[i]);
6080 mark_page_accessed(inpages[i]);
6081 }
6082 /* unlock the user pages */
6083 sev_unpin_memory(kvm, inpages, npages);
6084e_free:
6085 kfree(data);
6086 return ret;
6087}
6088
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006089static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
6090{
6091 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6092 struct sev_data_launch_measure *data;
6093 struct kvm_sev_launch_measure params;
6094 void *blob = NULL;
6095 int ret;
6096
6097 if (!sev_guest(kvm))
6098 return -ENOTTY;
6099
6100 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6101 return -EFAULT;
6102
6103 data = kzalloc(sizeof(*data), GFP_KERNEL);
6104 if (!data)
6105 return -ENOMEM;
6106
6107 /* User wants to query the blob length */
6108 if (!params.len)
6109 goto cmd;
6110
6111 if (params.uaddr) {
6112 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
6113 ret = -EINVAL;
6114 goto e_free;
6115 }
6116
6117 if (!access_ok(VERIFY_WRITE, params.uaddr, params.len)) {
6118 ret = -EFAULT;
6119 goto e_free;
6120 }
6121
6122 ret = -ENOMEM;
6123 blob = kmalloc(params.len, GFP_KERNEL);
6124 if (!blob)
6125 goto e_free;
6126
6127 data->address = __psp_pa(blob);
6128 data->len = params.len;
6129 }
6130
6131cmd:
6132 data->handle = sev->handle;
6133 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
6134
6135 /*
6136 * If we query the session length, FW responded with expected data.
6137 */
6138 if (!params.len)
6139 goto done;
6140
6141 if (ret)
6142 goto e_free_blob;
6143
6144 if (blob) {
6145 if (copy_to_user((void __user *)(uintptr_t)params.uaddr, blob, params.len))
6146 ret = -EFAULT;
6147 }
6148
6149done:
6150 params.len = data->len;
6151 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6152 ret = -EFAULT;
6153e_free_blob:
6154 kfree(blob);
6155e_free:
6156 kfree(data);
6157 return ret;
6158}
6159
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006160static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
6161{
6162 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6163 struct sev_data_launch_finish *data;
6164 int ret;
6165
6166 if (!sev_guest(kvm))
6167 return -ENOTTY;
6168
6169 data = kzalloc(sizeof(*data), GFP_KERNEL);
6170 if (!data)
6171 return -ENOMEM;
6172
6173 data->handle = sev->handle;
6174 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
6175
6176 kfree(data);
6177 return ret;
6178}
6179
Brijesh Singh255d9e72017-12-04 10:57:37 -06006180static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
6181{
6182 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6183 struct kvm_sev_guest_status params;
6184 struct sev_data_guest_status *data;
6185 int ret;
6186
6187 if (!sev_guest(kvm))
6188 return -ENOTTY;
6189
6190 data = kzalloc(sizeof(*data), GFP_KERNEL);
6191 if (!data)
6192 return -ENOMEM;
6193
6194 data->handle = sev->handle;
6195 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
6196 if (ret)
6197 goto e_free;
6198
6199 params.policy = data->policy;
6200 params.state = data->state;
6201 params.handle = data->handle;
6202
6203 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6204 ret = -EFAULT;
6205e_free:
6206 kfree(data);
6207 return ret;
6208}
6209
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006210static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
6211 unsigned long dst, int size,
6212 int *error, bool enc)
6213{
6214 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6215 struct sev_data_dbg *data;
6216 int ret;
6217
6218 data = kzalloc(sizeof(*data), GFP_KERNEL);
6219 if (!data)
6220 return -ENOMEM;
6221
6222 data->handle = sev->handle;
6223 data->dst_addr = dst;
6224 data->src_addr = src;
6225 data->len = size;
6226
6227 ret = sev_issue_cmd(kvm,
6228 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
6229 data, error);
6230 kfree(data);
6231 return ret;
6232}
6233
6234static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
6235 unsigned long dst_paddr, int sz, int *err)
6236{
6237 int offset;
6238
6239 /*
6240 * Its safe to read more than we are asked, caller should ensure that
6241 * destination has enough space.
6242 */
6243 src_paddr = round_down(src_paddr, 16);
6244 offset = src_paddr & 15;
6245 sz = round_up(sz + offset, 16);
6246
6247 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
6248}
6249
6250static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
6251 unsigned long __user dst_uaddr,
6252 unsigned long dst_paddr,
6253 int size, int *err)
6254{
6255 struct page *tpage = NULL;
6256 int ret, offset;
6257
6258 /* if inputs are not 16-byte then use intermediate buffer */
6259 if (!IS_ALIGNED(dst_paddr, 16) ||
6260 !IS_ALIGNED(paddr, 16) ||
6261 !IS_ALIGNED(size, 16)) {
6262 tpage = (void *)alloc_page(GFP_KERNEL);
6263 if (!tpage)
6264 return -ENOMEM;
6265
6266 dst_paddr = __sme_page_pa(tpage);
6267 }
6268
6269 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
6270 if (ret)
6271 goto e_free;
6272
6273 if (tpage) {
6274 offset = paddr & 15;
6275 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
6276 page_address(tpage) + offset, size))
6277 ret = -EFAULT;
6278 }
6279
6280e_free:
6281 if (tpage)
6282 __free_page(tpage);
6283
6284 return ret;
6285}
6286
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006287static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
6288 unsigned long __user vaddr,
6289 unsigned long dst_paddr,
6290 unsigned long __user dst_vaddr,
6291 int size, int *error)
6292{
6293 struct page *src_tpage = NULL;
6294 struct page *dst_tpage = NULL;
6295 int ret, len = size;
6296
6297 /* If source buffer is not aligned then use an intermediate buffer */
6298 if (!IS_ALIGNED(vaddr, 16)) {
6299 src_tpage = alloc_page(GFP_KERNEL);
6300 if (!src_tpage)
6301 return -ENOMEM;
6302
6303 if (copy_from_user(page_address(src_tpage),
6304 (void __user *)(uintptr_t)vaddr, size)) {
6305 __free_page(src_tpage);
6306 return -EFAULT;
6307 }
6308
6309 paddr = __sme_page_pa(src_tpage);
6310 }
6311
6312 /*
6313 * If destination buffer or length is not aligned then do read-modify-write:
6314 * - decrypt destination in an intermediate buffer
6315 * - copy the source buffer in an intermediate buffer
6316 * - use the intermediate buffer as source buffer
6317 */
6318 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
6319 int dst_offset;
6320
6321 dst_tpage = alloc_page(GFP_KERNEL);
6322 if (!dst_tpage) {
6323 ret = -ENOMEM;
6324 goto e_free;
6325 }
6326
6327 ret = __sev_dbg_decrypt(kvm, dst_paddr,
6328 __sme_page_pa(dst_tpage), size, error);
6329 if (ret)
6330 goto e_free;
6331
6332 /*
6333 * If source is kernel buffer then use memcpy() otherwise
6334 * copy_from_user().
6335 */
6336 dst_offset = dst_paddr & 15;
6337
6338 if (src_tpage)
6339 memcpy(page_address(dst_tpage) + dst_offset,
6340 page_address(src_tpage), size);
6341 else {
6342 if (copy_from_user(page_address(dst_tpage) + dst_offset,
6343 (void __user *)(uintptr_t)vaddr, size)) {
6344 ret = -EFAULT;
6345 goto e_free;
6346 }
6347 }
6348
6349 paddr = __sme_page_pa(dst_tpage);
6350 dst_paddr = round_down(dst_paddr, 16);
6351 len = round_up(size, 16);
6352 }
6353
6354 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
6355
6356e_free:
6357 if (src_tpage)
6358 __free_page(src_tpage);
6359 if (dst_tpage)
6360 __free_page(dst_tpage);
6361 return ret;
6362}
6363
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006364static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
6365{
6366 unsigned long vaddr, vaddr_end, next_vaddr;
6367 unsigned long dst_vaddr, dst_vaddr_end;
6368 struct page **src_p, **dst_p;
6369 struct kvm_sev_dbg debug;
6370 unsigned long n;
6371 int ret, size;
6372
6373 if (!sev_guest(kvm))
6374 return -ENOTTY;
6375
6376 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
6377 return -EFAULT;
6378
6379 vaddr = debug.src_uaddr;
6380 size = debug.len;
6381 vaddr_end = vaddr + size;
6382 dst_vaddr = debug.dst_uaddr;
6383 dst_vaddr_end = dst_vaddr + size;
6384
6385 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
6386 int len, s_off, d_off;
6387
6388 /* lock userspace source and destination page */
6389 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
6390 if (!src_p)
6391 return -EFAULT;
6392
6393 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
6394 if (!dst_p) {
6395 sev_unpin_memory(kvm, src_p, n);
6396 return -EFAULT;
6397 }
6398
6399 /*
6400 * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
6401 * memory content (i.e it will write the same memory region with C=1).
6402 * It's possible that the cache may contain the data with C=0, i.e.,
6403 * unencrypted so invalidate it first.
6404 */
6405 sev_clflush_pages(src_p, 1);
6406 sev_clflush_pages(dst_p, 1);
6407
6408 /*
6409 * Since user buffer may not be page aligned, calculate the
6410 * offset within the page.
6411 */
6412 s_off = vaddr & ~PAGE_MASK;
6413 d_off = dst_vaddr & ~PAGE_MASK;
6414 len = min_t(size_t, (PAGE_SIZE - s_off), size);
6415
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006416 if (dec)
6417 ret = __sev_dbg_decrypt_user(kvm,
6418 __sme_page_pa(src_p[0]) + s_off,
6419 dst_vaddr,
6420 __sme_page_pa(dst_p[0]) + d_off,
6421 len, &argp->error);
6422 else
6423 ret = __sev_dbg_encrypt_user(kvm,
6424 __sme_page_pa(src_p[0]) + s_off,
6425 vaddr,
6426 __sme_page_pa(dst_p[0]) + d_off,
6427 dst_vaddr,
6428 len, &argp->error);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006429
6430 sev_unpin_memory(kvm, src_p, 1);
6431 sev_unpin_memory(kvm, dst_p, 1);
6432
6433 if (ret)
6434 goto err;
6435
6436 next_vaddr = vaddr + len;
6437 dst_vaddr = dst_vaddr + len;
6438 size -= len;
6439 }
6440err:
6441 return ret;
6442}
6443
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006444static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
6445{
6446 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6447 struct sev_data_launch_secret *data;
6448 struct kvm_sev_launch_secret params;
6449 struct page **pages;
6450 void *blob, *hdr;
6451 unsigned long n;
6452 int ret;
6453
6454 if (!sev_guest(kvm))
6455 return -ENOTTY;
6456
6457 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6458 return -EFAULT;
6459
6460 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
6461 if (!pages)
6462 return -ENOMEM;
6463
6464 /*
6465 * The secret must be copied into contiguous memory region, lets verify
6466 * that userspace memory pages are contiguous before we issue command.
6467 */
6468 if (get_num_contig_pages(0, pages, n) != n) {
6469 ret = -EINVAL;
6470 goto e_unpin_memory;
6471 }
6472
6473 ret = -ENOMEM;
6474 data = kzalloc(sizeof(*data), GFP_KERNEL);
6475 if (!data)
6476 goto e_unpin_memory;
6477
6478 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
6479 if (IS_ERR(blob)) {
6480 ret = PTR_ERR(blob);
6481 goto e_free;
6482 }
6483
6484 data->trans_address = __psp_pa(blob);
6485 data->trans_len = params.trans_len;
6486
6487 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
6488 if (IS_ERR(hdr)) {
6489 ret = PTR_ERR(hdr);
6490 goto e_free_blob;
6491 }
6492 data->trans_address = __psp_pa(blob);
6493 data->trans_len = params.trans_len;
6494
6495 data->handle = sev->handle;
6496 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
6497
6498 kfree(hdr);
6499
6500e_free_blob:
6501 kfree(blob);
6502e_free:
6503 kfree(data);
6504e_unpin_memory:
6505 sev_unpin_memory(kvm, pages, n);
6506 return ret;
6507}
6508
Brijesh Singh1654efc2017-12-04 10:57:34 -06006509static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
6510{
6511 struct kvm_sev_cmd sev_cmd;
6512 int r;
6513
6514 if (!svm_sev_enabled())
6515 return -ENOTTY;
6516
6517 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
6518 return -EFAULT;
6519
6520 mutex_lock(&kvm->lock);
6521
6522 switch (sev_cmd.id) {
6523 case KVM_SEV_INIT:
6524 r = sev_guest_init(kvm, &sev_cmd);
6525 break;
Brijesh Singh59414c92017-12-04 10:57:35 -06006526 case KVM_SEV_LAUNCH_START:
6527 r = sev_launch_start(kvm, &sev_cmd);
6528 break;
Brijesh Singh89c50582017-12-04 10:57:35 -06006529 case KVM_SEV_LAUNCH_UPDATE_DATA:
6530 r = sev_launch_update_data(kvm, &sev_cmd);
6531 break;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006532 case KVM_SEV_LAUNCH_MEASURE:
6533 r = sev_launch_measure(kvm, &sev_cmd);
6534 break;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006535 case KVM_SEV_LAUNCH_FINISH:
6536 r = sev_launch_finish(kvm, &sev_cmd);
6537 break;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006538 case KVM_SEV_GUEST_STATUS:
6539 r = sev_guest_status(kvm, &sev_cmd);
6540 break;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006541 case KVM_SEV_DBG_DECRYPT:
6542 r = sev_dbg_crypt(kvm, &sev_cmd, true);
6543 break;
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006544 case KVM_SEV_DBG_ENCRYPT:
6545 r = sev_dbg_crypt(kvm, &sev_cmd, false);
6546 break;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006547 case KVM_SEV_LAUNCH_SECRET:
6548 r = sev_launch_secret(kvm, &sev_cmd);
6549 break;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006550 default:
6551 r = -EINVAL;
6552 goto out;
6553 }
6554
6555 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
6556 r = -EFAULT;
6557
6558out:
6559 mutex_unlock(&kvm->lock);
6560 return r;
6561}
6562
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006563static int svm_register_enc_region(struct kvm *kvm,
6564 struct kvm_enc_region *range)
6565{
6566 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6567 struct enc_region *region;
6568 int ret = 0;
6569
6570 if (!sev_guest(kvm))
6571 return -ENOTTY;
6572
6573 region = kzalloc(sizeof(*region), GFP_KERNEL);
6574 if (!region)
6575 return -ENOMEM;
6576
6577 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
6578 if (!region->pages) {
6579 ret = -ENOMEM;
6580 goto e_free;
6581 }
6582
6583 /*
6584 * The guest may change the memory encryption attribute from C=0 -> C=1
6585 * or vice versa for this memory range. Lets make sure caches are
6586 * flushed to ensure that guest data gets written into memory with
6587 * correct C-bit.
6588 */
6589 sev_clflush_pages(region->pages, region->npages);
6590
6591 region->uaddr = range->addr;
6592 region->size = range->size;
6593
6594 mutex_lock(&kvm->lock);
6595 list_add_tail(&region->list, &sev->regions_list);
6596 mutex_unlock(&kvm->lock);
6597
6598 return ret;
6599
6600e_free:
6601 kfree(region);
6602 return ret;
6603}
6604
6605static struct enc_region *
6606find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
6607{
6608 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6609 struct list_head *head = &sev->regions_list;
6610 struct enc_region *i;
6611
6612 list_for_each_entry(i, head, list) {
6613 if (i->uaddr == range->addr &&
6614 i->size == range->size)
6615 return i;
6616 }
6617
6618 return NULL;
6619}
6620
6621
6622static int svm_unregister_enc_region(struct kvm *kvm,
6623 struct kvm_enc_region *range)
6624{
6625 struct enc_region *region;
6626 int ret;
6627
6628 mutex_lock(&kvm->lock);
6629
6630 if (!sev_guest(kvm)) {
6631 ret = -ENOTTY;
6632 goto failed;
6633 }
6634
6635 region = find_enc_region(kvm, range);
6636 if (!region) {
6637 ret = -EINVAL;
6638 goto failed;
6639 }
6640
6641 __unregister_enc_region_locked(kvm, region);
6642
6643 mutex_unlock(&kvm->lock);
6644 return 0;
6645
6646failed:
6647 mutex_unlock(&kvm->lock);
6648 return ret;
6649}
6650
Kees Cook404f6aa2016-08-08 16:29:06 -07006651static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006652 .cpu_has_kvm_support = has_svm,
6653 .disabled_by_bios = is_disabled,
6654 .hardware_setup = svm_hardware_setup,
6655 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03006656 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006657 .hardware_enable = svm_hardware_enable,
6658 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02006659 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006660 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006661
6662 .vcpu_create = svm_create_vcpu,
6663 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03006664 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006665
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006666 .vm_init = avic_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06006667 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006668
Avi Kivity04d2cc72007-09-10 18:10:54 +03006669 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006670 .vcpu_load = svm_vcpu_load,
6671 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05006672 .vcpu_blocking = svm_vcpu_blocking,
6673 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006674
Paolo Bonzinia96036b2015-11-10 11:55:36 +01006675 .update_bp_intercept = update_bp_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006676 .get_msr = svm_get_msr,
6677 .set_msr = svm_set_msr,
6678 .get_segment_base = svm_get_segment_base,
6679 .get_segment = svm_get_segment,
6680 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02006681 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10006682 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02006683 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02006684 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03006685 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006686 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006687 .set_cr3 = svm_set_cr3,
6688 .set_cr4 = svm_set_cr4,
6689 .set_efer = svm_set_efer,
6690 .get_idt = svm_get_idt,
6691 .set_idt = svm_set_idt,
6692 .get_gdt = svm_get_gdt,
6693 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006694 .get_dr6 = svm_get_dr6,
6695 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03006696 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01006697 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03006698 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006699 .get_rflags = svm_get_rflags,
6700 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08006701
Avi Kivity6aa8b732006-12-10 02:21:36 -08006702 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006703
Avi Kivity6aa8b732006-12-10 02:21:36 -08006704 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03006705 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006706 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04006707 .set_interrupt_shadow = svm_set_interrupt_shadow,
6708 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02006709 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03006710 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006711 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02006712 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006713 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02006714 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006715 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006716 .get_nmi_mask = svm_get_nmi_mask,
6717 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006718 .enable_nmi_window = enable_nmi_window,
6719 .enable_irq_window = enable_irq_window,
6720 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08006721 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03006722 .get_enable_apicv = svm_get_enable_apicv,
6723 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08006724 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006725 .hwapic_irr_update = svm_hwapic_irr_update,
6726 .hwapic_isr_update = svm_hwapic_isr_update,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05006727 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02006728
6729 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08006730 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006731 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006732
Avi Kivity586f9602010-11-18 13:09:54 +02006733 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02006734
Sheng Yang17cc3932010-01-05 19:02:27 +08006735 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08006736
6737 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006738
6739 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00006740 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006741 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08006742 .xsaves_supported = svm_xsaves_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006743
6744 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006745
6746 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10006747
6748 .write_tsc_offset = svm_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02006749
6750 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006751
6752 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08006753 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006754
6755 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02006756
6757 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05006758 .deliver_posted_interrupt = svm_deliver_avic_intr,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05006759 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02006760 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006761
Ladi Prosek72d7b372017-10-11 16:54:41 +02006762 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006763 .pre_enter_smm = svm_pre_enter_smm,
6764 .pre_leave_smm = svm_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006765 .enable_smi_window = enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06006766
6767 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006768 .mem_enc_reg_region = svm_register_enc_region,
6769 .mem_enc_unreg_region = svm_unregister_enc_region,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006770};
6771
6772static int __init svm_init(void)
6773{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08006774 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03006775 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006776}
6777
6778static void __exit svm_exit(void)
6779{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08006780 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08006781}
6782
6783module_init(svm_init)
6784module_exit(svm_exit)