blob: 51186107eb22accbcab8d922009a40026bde01cd [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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050043#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010044#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020045#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040046#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010047#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020048#include <asm/kvm_para.h>
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -050049#include <asm/irq_remapping.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
Eduardo Habkost63d11422008-11-17 19:03:20 -020051#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030052#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020053
Avi Kivity4ecac3f2008-05-13 13:23:38 +030054#define __ex(x) __kvm_handle_fault_on_reboot(x)
55
Avi Kivity6aa8b732006-12-10 02:21:36 -080056MODULE_AUTHOR("Qumranet");
57MODULE_LICENSE("GPL");
58
Josh Triplettae759542012-03-28 11:32:28 -070059static const struct x86_cpu_id svm_cpu_id[] = {
60 X86_FEATURE_MATCH(X86_FEATURE_SVM),
61 {}
62};
63MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
64
Avi Kivity6aa8b732006-12-10 02:21:36 -080065#define IOPM_ALLOC_ORDER 2
66#define MSRPM_ALLOC_ORDER 1
67
Avi Kivity6aa8b732006-12-10 02:21:36 -080068#define SEG_TYPE_LDT 2
69#define SEG_TYPE_BUSY_TSS16 3
70
Andre Przywara6bc31bd2010-04-11 23:07:28 +020071#define SVM_FEATURE_NPT (1 << 0)
72#define SVM_FEATURE_LBRV (1 << 1)
73#define SVM_FEATURE_SVML (1 << 2)
74#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010075#define SVM_FEATURE_TSC_RATE (1 << 4)
76#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
77#define SVM_FEATURE_FLUSH_ASID (1 << 6)
78#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020079#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030080
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -050081#define SVM_AVIC_DOORBELL 0xc001011b
82
Joerg Roedel410e4d52009-08-07 11:49:44 +020083#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
84#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
85#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
86
Joerg Roedel24e09cb2008-02-13 18:58:47 +010087#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
88
Joerg Roedelfbc0db72011-03-25 09:44:46 +010089#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010090#define TSC_RATIO_MIN 0x0000000000000001ULL
91#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010092
Dan Carpenter5446a972016-05-23 13:20:10 +030093#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050094
95/*
96 * 0xff is broadcast, so the max index allowed for physical APIC ID
97 * table is 0xfe. APIC IDs above 0xff are reserved.
98 */
99#define AVIC_MAX_PHYSICAL_ID_COUNT 255
100
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500101#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
102#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
103#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
104
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500105/* AVIC GATAG is encoded using VM and VCPU IDs */
106#define AVIC_VCPU_ID_BITS 8
107#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
108
109#define AVIC_VM_ID_BITS 24
110#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
111#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
112
113#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
114 (y & AVIC_VCPU_ID_MASK))
115#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
116#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
117
Joerg Roedel67ec6602010-05-17 14:43:35 +0200118static bool erratum_383_found __read_mostly;
119
Avi Kivity6c8166a2009-05-31 18:15:37 +0300120static const u32 host_save_user_msrs[] = {
121#ifdef CONFIG_X86_64
122 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
123 MSR_FS_BASE,
124#endif
125 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Paolo Bonzini46896c72015-11-12 14:49:16 +0100126 MSR_TSC_AUX,
Avi Kivity6c8166a2009-05-31 18:15:37 +0300127};
128
129#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
130
131struct kvm_vcpu;
132
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200133struct nested_state {
134 struct vmcb *hsave;
135 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +0100136 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200137 u64 vmcb;
138
139 /* These are the merged vectors */
140 u32 *msrpm;
141
142 /* gpa pointers to the real vectors */
143 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100144 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200145
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200146 /* A VMEXIT is required but not yet emulated */
147 bool exit_required;
148
Joerg Roedelaad42c62009-08-07 11:49:34 +0200149 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100150 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100151 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200152 u32 intercept_exceptions;
153 u64 intercept;
154
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200155 /* Nested Paging related state */
156 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200157};
158
Joerg Roedel323c3d82010-03-01 15:34:37 +0100159#define MSRPM_OFFSETS 16
160static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
161
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500162/*
163 * Set osvw_len to higher value when updated Revision Guides
164 * are published and we know what the new status bits are
165 */
166static uint64_t osvw_len = 4, osvw_status;
167
Avi Kivity6c8166a2009-05-31 18:15:37 +0300168struct vcpu_svm {
169 struct kvm_vcpu vcpu;
170 struct vmcb *vmcb;
171 unsigned long vmcb_pa;
172 struct svm_cpu_data *svm_data;
173 uint64_t asid_generation;
174 uint64_t sysenter_esp;
175 uint64_t sysenter_eip;
Paolo Bonzini46896c72015-11-12 14:49:16 +0100176 uint64_t tsc_aux;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300177
178 u64 next_rip;
179
180 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200181 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200182 u16 fs;
183 u16 gs;
184 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200185 u64 gs_base;
186 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300187
188 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300189
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200190 ulong nmi_iret_rip;
191
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200192 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200193
194 bool nmi_singlestep;
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200195 u64 nmi_singlestep_guest_rflags;
Jan Kiszka66b71382010-02-23 17:47:56 +0100196
197 unsigned int3_injected;
198 unsigned long int3_rip;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100199
Joerg Roedel6092d3d2015-10-14 15:10:54 +0200200 /* cached guest cpuid flags for faster access */
201 bool nrips_enabled : 1;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500202
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500203 u32 ldr_reg;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500204 struct page *avic_backing_page;
205 u64 *avic_physical_id_cache;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -0500206 bool avic_is_running;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500207
208 /*
209 * Per-vcpu list of struct amd_svm_iommu_ir:
210 * This is used mainly to store interrupt remapping information used
211 * when update the vcpu affinity. This avoids the need to scan for
212 * IRTE and try to match ga_tag in the IOMMU driver.
213 */
214 struct list_head ir_list;
215 spinlock_t ir_list_lock;
216};
217
218/*
219 * This is a wrapper of struct amd_iommu_ir_data.
220 */
221struct amd_svm_iommu_ir {
222 struct list_head node; /* Used by SVM for per-vcpu ir_list */
223 void *data; /* Storing pointer to struct amd_ir_data */
Avi Kivity6c8166a2009-05-31 18:15:37 +0300224};
225
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500226#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
227#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
228
229#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
230#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
231#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
232#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
233
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100234static DEFINE_PER_CPU(u64, current_tsc_ratio);
235#define TSC_RATIO_DEFAULT 0x0100000000ULL
236
Joerg Roedel455716f2010-03-01 15:34:35 +0100237#define MSR_INVALID 0xffffffffU
238
Mathias Krause09941fb2012-08-30 01:30:20 +0200239static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100240 u32 index; /* Index of the MSR */
241 bool always; /* True if intercept is always on */
242} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400243 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100244 { .index = MSR_IA32_SYSENTER_CS, .always = true },
245#ifdef CONFIG_X86_64
246 { .index = MSR_GS_BASE, .always = true },
247 { .index = MSR_FS_BASE, .always = true },
248 { .index = MSR_KERNEL_GS_BASE, .always = true },
249 { .index = MSR_LSTAR, .always = true },
250 { .index = MSR_CSTAR, .always = true },
251 { .index = MSR_SYSCALL_MASK, .always = true },
252#endif
253 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
254 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
255 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
256 { .index = MSR_IA32_LASTINTTOIP, .always = false },
257 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258};
259
260/* enable NPT for AMD64 and X86 with PAE */
261#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
262static bool npt_enabled = true;
263#else
Joerg Roedele0231712010-02-24 18:59:10 +0100264static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265#endif
266
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100267/* allow nested paging (virtualized MMU) for all guests */
268static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269module_param(npt, int, S_IRUGO);
270
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100271/* allow nested virtualization in KVM/SVM */
272static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273module_param(nested, int, S_IRUGO);
274
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500275/* enable / disable AVIC */
276static int avic;
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500277#ifdef CONFIG_X86_LOCAL_APIC
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500278module_param(avic, int, S_IRUGO);
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500279#endif
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500280
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -0500281/* enable/disable Virtual VMLOAD VMSAVE */
282static int vls = true;
283module_param(vls, int, 0444);
284
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500285/* enable/disable Virtual GIF */
286static int vgif = true;
287module_param(vgif, int, 0444);
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500288
Brijesh Singhe9df0942017-12-04 10:57:33 -0600289/* enable/disable SEV support */
290static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
291module_param(sev, int, 0444);
292
Paolo Bonzini79a80592015-09-21 07:46:55 +0200293static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200295static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296
Joerg Roedel410e4d52009-08-07 11:49:44 +0200297static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100298static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800299static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
301 bool has_error_code, u32 error_code);
302
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100303enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100304 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
305 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100306 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100307 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100308 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100309 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100310 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100311 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100312 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100313 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100314 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100315 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500316 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
317 * AVIC PHYSICAL_TABLE pointer,
318 * AVIC LOGICAL_TABLE pointer
319 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100320 VMCB_DIRTY_MAX,
321};
322
Joerg Roedel0574dec2010-12-03 11:45:58 +0100323/* TPR and CR2 are always written before VMRUN */
324#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100325
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500326#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
327
Brijesh Singhed3cd232017-12-04 10:57:32 -0600328static unsigned int max_sev_asid;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600329static unsigned int min_sev_asid;
330static unsigned long *sev_asid_bitmap;
331
332static inline bool svm_sev_enabled(void)
333{
334 return max_sev_asid;
335}
336
337static inline bool sev_guest(struct kvm *kvm)
338{
339 struct kvm_sev_info *sev = &kvm->arch.sev_info;
340
341 return sev->active;
342}
Brijesh Singhed3cd232017-12-04 10:57:32 -0600343
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100344static inline void mark_all_dirty(struct vmcb *vmcb)
345{
346 vmcb->control.clean = 0;
347}
348
349static inline void mark_all_clean(struct vmcb *vmcb)
350{
351 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
352 & ~VMCB_ALWAYS_DIRTY_MASK;
353}
354
355static inline void mark_dirty(struct vmcb *vmcb, int bit)
356{
357 vmcb->control.clean &= ~(1 << bit);
358}
359
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
361{
362 return container_of(vcpu, struct vcpu_svm, vcpu);
363}
364
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500365static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
366{
367 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
368 mark_dirty(svm->vmcb, VMCB_AVIC);
369}
370
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -0500371static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
372{
373 struct vcpu_svm *svm = to_svm(vcpu);
374 u64 *entry = svm->avic_physical_id_cache;
375
376 if (!entry)
377 return false;
378
379 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
380}
381
Joerg Roedel384c6362010-11-30 18:03:56 +0100382static void recalc_intercepts(struct vcpu_svm *svm)
383{
384 struct vmcb_control_area *c, *h;
385 struct nested_state *g;
386
Joerg Roedel116a0a22010-12-03 11:45:49 +0100387 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
388
Joerg Roedel384c6362010-11-30 18:03:56 +0100389 if (!is_guest_mode(&svm->vcpu))
390 return;
391
392 c = &svm->vmcb->control;
393 h = &svm->nested.hsave->control;
394 g = &svm->nested;
395
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100396 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100397 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100398 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
399 c->intercept = h->intercept | g->intercept;
400}
401
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100402static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
403{
404 if (is_guest_mode(&svm->vcpu))
405 return svm->nested.hsave;
406 else
407 return svm->vmcb;
408}
409
410static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
411{
412 struct vmcb *vmcb = get_host_vmcb(svm);
413
414 vmcb->control.intercept_cr |= (1U << bit);
415
416 recalc_intercepts(svm);
417}
418
419static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
420{
421 struct vmcb *vmcb = get_host_vmcb(svm);
422
423 vmcb->control.intercept_cr &= ~(1U << bit);
424
425 recalc_intercepts(svm);
426}
427
428static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
429{
430 struct vmcb *vmcb = get_host_vmcb(svm);
431
432 return vmcb->control.intercept_cr & (1U << bit);
433}
434
Paolo Bonzini5315c712014-03-03 13:08:29 +0100435static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100436{
437 struct vmcb *vmcb = get_host_vmcb(svm);
438
Paolo Bonzini5315c712014-03-03 13:08:29 +0100439 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
440 | (1 << INTERCEPT_DR1_READ)
441 | (1 << INTERCEPT_DR2_READ)
442 | (1 << INTERCEPT_DR3_READ)
443 | (1 << INTERCEPT_DR4_READ)
444 | (1 << INTERCEPT_DR5_READ)
445 | (1 << INTERCEPT_DR6_READ)
446 | (1 << INTERCEPT_DR7_READ)
447 | (1 << INTERCEPT_DR0_WRITE)
448 | (1 << INTERCEPT_DR1_WRITE)
449 | (1 << INTERCEPT_DR2_WRITE)
450 | (1 << INTERCEPT_DR3_WRITE)
451 | (1 << INTERCEPT_DR4_WRITE)
452 | (1 << INTERCEPT_DR5_WRITE)
453 | (1 << INTERCEPT_DR6_WRITE)
454 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100455
456 recalc_intercepts(svm);
457}
458
Paolo Bonzini5315c712014-03-03 13:08:29 +0100459static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100460{
461 struct vmcb *vmcb = get_host_vmcb(svm);
462
Paolo Bonzini5315c712014-03-03 13:08:29 +0100463 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100464
465 recalc_intercepts(svm);
466}
467
Joerg Roedel18c918c2010-11-30 18:03:59 +0100468static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
469{
470 struct vmcb *vmcb = get_host_vmcb(svm);
471
472 vmcb->control.intercept_exceptions |= (1U << bit);
473
474 recalc_intercepts(svm);
475}
476
477static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
478{
479 struct vmcb *vmcb = get_host_vmcb(svm);
480
481 vmcb->control.intercept_exceptions &= ~(1U << bit);
482
483 recalc_intercepts(svm);
484}
485
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100486static inline void set_intercept(struct vcpu_svm *svm, int bit)
487{
488 struct vmcb *vmcb = get_host_vmcb(svm);
489
490 vmcb->control.intercept |= (1ULL << bit);
491
492 recalc_intercepts(svm);
493}
494
495static inline void clr_intercept(struct vcpu_svm *svm, int bit)
496{
497 struct vmcb *vmcb = get_host_vmcb(svm);
498
499 vmcb->control.intercept &= ~(1ULL << bit);
500
501 recalc_intercepts(svm);
502}
503
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500504static inline bool vgif_enabled(struct vcpu_svm *svm)
505{
506 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
507}
508
Joerg Roedel2af91942009-08-07 11:49:28 +0200509static inline void enable_gif(struct vcpu_svm *svm)
510{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500511 if (vgif_enabled(svm))
512 svm->vmcb->control.int_ctl |= V_GIF_MASK;
513 else
514 svm->vcpu.arch.hflags |= HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200515}
516
517static inline void disable_gif(struct vcpu_svm *svm)
518{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500519 if (vgif_enabled(svm))
520 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
521 else
522 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200523}
524
525static inline bool gif_set(struct vcpu_svm *svm)
526{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500527 if (vgif_enabled(svm))
528 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
529 else
530 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
Joerg Roedel2af91942009-08-07 11:49:28 +0200531}
532
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533static unsigned long iopm_base;
534
535struct kvm_ldttss_desc {
536 u16 limit0;
537 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100538 unsigned base1:8, type:5, dpl:2, p:1;
539 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540 u32 base3;
541 u32 zero1;
542} __attribute__((packed));
543
544struct svm_cpu_data {
545 int cpu;
546
Avi Kivity5008fdf2007-04-02 13:05:50 +0300547 u64 asid_generation;
548 u32 max_asid;
549 u32 next_asid;
Brijesh Singh4faefff2017-12-04 10:57:25 -0600550 u32 min_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551 struct kvm_ldttss_desc *tss_desc;
552
553 struct page *save_area;
554};
555
556static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
557
558struct svm_init_data {
559 int cpu;
560 int r;
561};
562
Mathias Krause09941fb2012-08-30 01:30:20 +0200563static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200565#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566#define MSRS_RANGE_SIZE 2048
567#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
568
Joerg Roedel455716f2010-03-01 15:34:35 +0100569static u32 svm_msrpm_offset(u32 msr)
570{
571 u32 offset;
572 int i;
573
574 for (i = 0; i < NUM_MSR_MAPS; i++) {
575 if (msr < msrpm_ranges[i] ||
576 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
577 continue;
578
579 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
580 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
581
582 /* Now we have the u8 offset - but need the u32 offset */
583 return offset / 4;
584 }
585
586 /* MSR not in any range */
587 return MSR_INVALID;
588}
589
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590#define MAX_INST_SIZE 15
591
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592static inline void clgi(void)
593{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300594 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595}
596
597static inline void stgi(void)
598{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300599 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600}
601
602static inline void invlpga(unsigned long addr, u32 asid)
603{
Joerg Roedele0231712010-02-24 18:59:10 +0100604 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605}
606
Yu Zhang855feb62017-08-24 20:27:55 +0800607static int get_npt_level(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +0200608{
609#ifdef CONFIG_X86_64
Yu Zhang2a7266a2017-08-24 20:27:54 +0800610 return PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200611#else
612 return PT32E_ROOT_LEVEL;
613#endif
614}
615
Avi Kivity6aa8b732006-12-10 02:21:36 -0800616static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
617{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000618 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100619 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600620 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621
Alexander Graf9962d032008-11-25 20:17:02 +0100622 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100623 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624}
625
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626static int is_external_interrupt(u32 info)
627{
628 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
629 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
630}
631
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200632static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400633{
634 struct vcpu_svm *svm = to_svm(vcpu);
635 u32 ret = 0;
636
637 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200638 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
639 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400640}
641
642static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
643{
644 struct vcpu_svm *svm = to_svm(vcpu);
645
646 if (mask == 0)
647 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
648 else
649 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
650
651}
652
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
654{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400655 struct vcpu_svm *svm = to_svm(vcpu);
656
Bandan Dasf1047652015-06-11 02:05:33 -0400657 if (svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200658 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200659 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400660 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200661
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400662 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100663 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300664 EMULATE_DONE)
665 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 return;
667 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300668 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
669 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
670 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300672 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400673 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800674}
675
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700676static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100677{
678 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700679 unsigned nr = vcpu->arch.exception.nr;
680 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700681 bool reinject = vcpu->arch.exception.injected;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700682 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100683
Joerg Roedele0231712010-02-24 18:59:10 +0100684 /*
685 * If we are within a nested VM we'd better #VMEXIT and let the guest
686 * handle the exception
687 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200688 if (!reinject &&
689 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100690 return;
691
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200692 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100693 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
694
695 /*
696 * For guest debugging where we have to reinject #BP if some
697 * INT3 is guest-owned:
698 * Emulate nRIP by moving RIP forward. Will fail if injection
699 * raises a fault that is not intercepted. Still better than
700 * failing in all cases.
701 */
702 skip_emulated_instruction(&svm->vcpu);
703 rip = kvm_rip_read(&svm->vcpu);
704 svm->int3_rip = rip + svm->vmcb->save.cs.base;
705 svm->int3_injected = rip - old_rip;
706 }
707
Jan Kiszka116a4752010-02-23 17:47:54 +0100708 svm->vmcb->control.event_inj = nr
709 | SVM_EVTINJ_VALID
710 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
711 | SVM_EVTINJ_TYPE_EXEPT;
712 svm->vmcb->control.event_inj_err = error_code;
713}
714
Joerg Roedel67ec6602010-05-17 14:43:35 +0200715static void svm_init_erratum_383(void)
716{
717 u32 low, high;
718 int err;
719 u64 val;
720
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100721 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200722 return;
723
724 /* Use _safe variants to not break nested virtualization */
725 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
726 if (err)
727 return;
728
729 val |= (1ULL << 47);
730
731 low = lower_32_bits(val);
732 high = upper_32_bits(val);
733
734 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
735
736 erratum_383_found = true;
737}
738
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500739static void svm_init_osvw(struct kvm_vcpu *vcpu)
740{
741 /*
742 * Guests should see errata 400 and 415 as fixed (assuming that
743 * HLT and IO instructions are intercepted).
744 */
745 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
746 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
747
748 /*
749 * By increasing VCPU's osvw.length to 3 we are telling the guest that
750 * all osvw.status bits inside that length, including bit 0 (which is
751 * reserved for erratum 298), are valid. However, if host processor's
752 * osvw_len is 0 then osvw_status[0] carries no information. We need to
753 * be conservative here and therefore we tell the guest that erratum 298
754 * is present (because we really don't know).
755 */
756 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
757 vcpu->arch.osvw.status |= 1;
758}
759
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760static int has_svm(void)
761{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200762 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763
Eduardo Habkost63d11422008-11-17 19:03:20 -0200764 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800765 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766 return 0;
767 }
768
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 return 1;
770}
771
Radim Krčmář13a34e02014-08-28 15:13:03 +0200772static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100774 /* Make sure we clean up behind us */
775 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
776 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
777
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200778 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100779
780 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781}
782
Radim Krčmář13a34e02014-08-28 15:13:03 +0200783static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784{
785
Tejun Heo0fe1e002009-10-29 22:34:14 +0900786 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 struct desc_struct *gdt;
789 int me = raw_smp_processor_id();
790
Alexander Graf10474ae2009-09-15 11:37:46 +0200791 rdmsrl(MSR_EFER, efer);
792 if (efer & EFER_SVME)
793 return -EBUSY;
794
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200796 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200797 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900799 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900800 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200801 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200802 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803 }
804
Tejun Heo0fe1e002009-10-29 22:34:14 +0900805 sd->asid_generation = 1;
806 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
807 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600808 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809
Thomas Garnier45fc8752017-03-14 10:05:08 -0700810 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900811 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812
Alexander Graf9962d032008-11-25 20:17:02 +0100813 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814
Linus Torvaldsd0316552009-12-14 09:58:24 -0800815 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200816
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100817 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
818 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500819 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100820 }
821
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500822
823 /*
824 * Get OSVW bits.
825 *
826 * Note that it is possible to have a system with mixed processor
827 * revisions and therefore different OSVW bits. If bits are not the same
828 * on different processors then choose the worst case (i.e. if erratum
829 * is present on one processor and not on another then assume that the
830 * erratum is present everywhere).
831 */
832 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
833 uint64_t len, status = 0;
834 int err;
835
836 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
837 if (!err)
838 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
839 &err);
840
841 if (err)
842 osvw_status = osvw_len = 0;
843 else {
844 if (len < osvw_len)
845 osvw_len = len;
846 osvw_status |= status;
847 osvw_status &= (1ULL << osvw_len) - 1;
848 }
849 } else
850 osvw_status = osvw_len = 0;
851
Joerg Roedel67ec6602010-05-17 14:43:35 +0200852 svm_init_erratum_383();
853
Joerg Roedel1018faa2012-02-29 14:57:32 +0100854 amd_pmu_enable_virt();
855
Alexander Graf10474ae2009-09-15 11:37:46 +0200856 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857}
858
Joerg Roedel0da1db752008-07-02 16:02:11 +0200859static void svm_cpu_uninit(int cpu)
860{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900861 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200862
Tejun Heo0fe1e002009-10-29 22:34:14 +0900863 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200864 return;
865
866 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900867 __free_page(sd->save_area);
868 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200869}
870
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871static int svm_cpu_init(int cpu)
872{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900873 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 int r;
875
Tejun Heo0fe1e002009-10-29 22:34:14 +0900876 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
877 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900879 sd->cpu = cpu;
880 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900882 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883 goto err_1;
884
Tejun Heo0fe1e002009-10-29 22:34:14 +0900885 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886
887 return 0;
888
889err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900890 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 return r;
892
893}
894
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100895static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896{
897 int i;
898
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100899 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
900 if (direct_access_msrs[i].index == index)
901 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100903 return false;
904}
905
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906static void set_msr_interception(u32 *msrpm, unsigned msr,
907 int read, int write)
908{
Joerg Roedel455716f2010-03-01 15:34:35 +0100909 u8 bit_read, bit_write;
910 unsigned long tmp;
911 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100913 /*
914 * If this warning triggers extend the direct_access_msrs list at the
915 * beginning of the file
916 */
917 WARN_ON(!valid_msr_intercept(msr));
918
Joerg Roedel455716f2010-03-01 15:34:35 +0100919 offset = svm_msrpm_offset(msr);
920 bit_read = 2 * (msr & 0x0f);
921 bit_write = 2 * (msr & 0x0f) + 1;
922 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800923
Joerg Roedel455716f2010-03-01 15:34:35 +0100924 BUG_ON(offset == MSR_INVALID);
925
926 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
927 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
928
929 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930}
931
Joerg Roedelf65c2292008-02-13 18:58:46 +0100932static void svm_vcpu_init_msrpm(u32 *msrpm)
933{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100934 int i;
935
Joerg Roedelf65c2292008-02-13 18:58:46 +0100936 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
937
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100938 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
939 if (!direct_access_msrs[i].always)
940 continue;
941
942 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
943 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100944}
945
Joerg Roedel323c3d82010-03-01 15:34:37 +0100946static void add_msr_offset(u32 offset)
947{
948 int i;
949
950 for (i = 0; i < MSRPM_OFFSETS; ++i) {
951
952 /* Offset already in list? */
953 if (msrpm_offsets[i] == offset)
954 return;
955
956 /* Slot used by another offset? */
957 if (msrpm_offsets[i] != MSR_INVALID)
958 continue;
959
960 /* Add offset to list */
961 msrpm_offsets[i] = offset;
962
963 return;
964 }
965
966 /*
967 * If this BUG triggers the msrpm_offsets table has an overflow. Just
968 * increase MSRPM_OFFSETS in this case.
969 */
970 BUG();
971}
972
973static void init_msrpm_offsets(void)
974{
975 int i;
976
977 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
978
979 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
980 u32 offset;
981
982 offset = svm_msrpm_offset(direct_access_msrs[i].index);
983 BUG_ON(offset == MSR_INVALID);
984
985 add_msr_offset(offset);
986 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987}
988
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100989static void svm_enable_lbrv(struct vcpu_svm *svm)
990{
991 u32 *msrpm = svm->msrpm;
992
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -0500993 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100994 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
995 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
996 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
997 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
998}
999
1000static void svm_disable_lbrv(struct vcpu_svm *svm)
1001{
1002 u32 *msrpm = svm->msrpm;
1003
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001004 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001005 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
1006 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
1007 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
1008 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
1009}
1010
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001011static void disable_nmi_singlestep(struct vcpu_svm *svm)
1012{
1013 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001014
Ladi Prosekab2f4d732017-06-21 09:06:58 +02001015 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
1016 /* Clear our flags if they were not set by the guest */
1017 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1018 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
1019 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1020 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
1021 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001022}
1023
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001024/* Note:
1025 * This hash table is used to map VM_ID to a struct kvm_arch,
1026 * when handling AMD IOMMU GALOG notification to schedule in
1027 * a particular vCPU.
1028 */
1029#define SVM_VM_DATA_HASH_BITS 8
David Hildenbrand681bcea2017-01-24 22:21:16 +01001030static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001031static u32 next_vm_id = 0;
1032static bool next_vm_id_wrapped = 0;
David Hildenbrand681bcea2017-01-24 22:21:16 +01001033static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001034
1035/* Note:
1036 * This function is called from IOMMU driver to notify
1037 * SVM to schedule in a particular vCPU of a particular VM.
1038 */
1039static int avic_ga_log_notifier(u32 ga_tag)
1040{
1041 unsigned long flags;
1042 struct kvm_arch *ka = NULL;
1043 struct kvm_vcpu *vcpu = NULL;
1044 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
1045 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
1046
1047 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
1048
1049 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1050 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1051 struct kvm *kvm = container_of(ka, struct kvm, arch);
1052 struct kvm_arch *vm_data = &kvm->arch;
1053
1054 if (vm_data->avic_vm_id != vm_id)
1055 continue;
1056 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
1057 break;
1058 }
1059 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1060
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001061 /* Note:
1062 * At this point, the IOMMU should have already set the pending
1063 * bit in the vAPIC backing page. So, we just need to schedule
1064 * in the vcpu.
1065 */
Paolo Bonzini1cf53582017-10-10 12:51:56 +02001066 if (vcpu)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001067 kvm_vcpu_wake_up(vcpu);
1068
1069 return 0;
1070}
1071
Brijesh Singhe9df0942017-12-04 10:57:33 -06001072static __init int sev_hardware_setup(void)
1073{
1074 struct sev_user_data_status *status;
1075 int rc;
1076
1077 /* Maximum number of encrypted guests supported simultaneously */
1078 max_sev_asid = cpuid_ecx(0x8000001F);
1079
1080 if (!max_sev_asid)
1081 return 1;
1082
Brijesh Singh1654efc2017-12-04 10:57:34 -06001083 /* Minimum ASID value that should be used for SEV guest */
1084 min_sev_asid = cpuid_edx(0x8000001F);
1085
1086 /* Initialize SEV ASID bitmap */
1087 sev_asid_bitmap = kcalloc(BITS_TO_LONGS(max_sev_asid),
1088 sizeof(unsigned long), GFP_KERNEL);
1089 if (!sev_asid_bitmap)
1090 return 1;
1091
Brijesh Singhe9df0942017-12-04 10:57:33 -06001092 status = kmalloc(sizeof(*status), GFP_KERNEL);
1093 if (!status)
1094 return 1;
1095
1096 /*
1097 * Check SEV platform status.
1098 *
1099 * PLATFORM_STATUS can be called in any state, if we failed to query
1100 * the PLATFORM status then either PSP firmware does not support SEV
1101 * feature or SEV firmware is dead.
1102 */
1103 rc = sev_platform_status(status, NULL);
1104 if (rc)
1105 goto err;
1106
1107 pr_info("SEV supported\n");
1108
1109err:
1110 kfree(status);
1111 return rc;
1112}
1113
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114static __init int svm_hardware_setup(void)
1115{
1116 int cpu;
1117 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001118 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 int r;
1120
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
1122
1123 if (!iopm_pages)
1124 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +03001125
1126 iopm_va = page_address(iopm_pages);
1127 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
1129
Joerg Roedel323c3d82010-03-01 15:34:37 +01001130 init_msrpm_offsets();
1131
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001132 if (boot_cpu_has(X86_FEATURE_NX))
1133 kvm_enable_efer_bits(EFER_NX);
1134
Alexander Graf1b2fd702009-02-02 16:23:51 +01001135 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
1136 kvm_enable_efer_bits(EFER_FFXSR);
1137
Joerg Roedel92a1f122011-03-25 09:44:51 +01001138 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +01001139 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +08001140 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1141 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +01001142 }
1143
Alexander Graf236de052008-11-25 20:17:10 +01001144 if (nested) {
1145 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +02001146 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +01001147 }
1148
Brijesh Singhe9df0942017-12-04 10:57:33 -06001149 if (sev) {
1150 if (boot_cpu_has(X86_FEATURE_SEV) &&
1151 IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
1152 r = sev_hardware_setup();
1153 if (r)
1154 sev = false;
1155 } else {
1156 sev = false;
1157 }
1158 }
1159
Zachary Amsden3230bb42009-09-29 11:38:37 -10001160 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161 r = svm_cpu_init(cpu);
1162 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +01001163 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +01001165
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001166 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001167 npt_enabled = false;
1168
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001169 if (npt_enabled && !npt) {
1170 printk(KERN_INFO "kvm: Nested Paging disabled\n");
1171 npt_enabled = false;
1172 }
1173
Joerg Roedel18552672008-02-07 13:47:41 +01001174 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001175 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +01001176 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +02001177 } else
1178 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001179
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001180 if (avic) {
1181 if (!npt_enabled ||
1182 !boot_cpu_has(X86_FEATURE_AVIC) ||
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001183 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001184 avic = false;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001185 } else {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001186 pr_info("AVIC enabled\n");
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001187
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001188 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1189 }
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001190 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001191
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001192 if (vls) {
1193 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001194 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001195 !IS_ENABLED(CONFIG_X86_64)) {
1196 vls = false;
1197 } else {
1198 pr_info("Virtual VMLOAD VMSAVE supported\n");
1199 }
1200 }
1201
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001202 if (vgif) {
1203 if (!boot_cpu_has(X86_FEATURE_VGIF))
1204 vgif = false;
1205 else
1206 pr_info("Virtual GIF supported\n");
1207 }
1208
Avi Kivity6aa8b732006-12-10 02:21:36 -08001209 return 0;
1210
Joerg Roedelf65c2292008-02-13 18:58:46 +01001211err:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001212 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1213 iopm_base = 0;
1214 return r;
1215}
1216
1217static __exit void svm_hardware_unsetup(void)
1218{
Joerg Roedel0da1db752008-07-02 16:02:11 +02001219 int cpu;
1220
Brijesh Singh1654efc2017-12-04 10:57:34 -06001221 if (svm_sev_enabled())
1222 kfree(sev_asid_bitmap);
1223
Zachary Amsden3230bb42009-09-29 11:38:37 -10001224 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +02001225 svm_cpu_uninit(cpu);
1226
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001228 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229}
1230
1231static void init_seg(struct vmcb_seg *seg)
1232{
1233 seg->selector = 0;
1234 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001235 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236 seg->limit = 0xffff;
1237 seg->base = 0;
1238}
1239
1240static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1241{
1242 seg->selector = 0;
1243 seg->attrib = SVM_SELECTOR_P_MASK | type;
1244 seg->limit = 0xffff;
1245 seg->base = 0;
1246}
1247
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001248static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1249{
1250 struct vcpu_svm *svm = to_svm(vcpu);
1251 u64 g_tsc_offset = 0;
1252
Joerg Roedel20307532010-11-29 17:51:48 +01001253 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001254 g_tsc_offset = svm->vmcb->control.tsc_offset -
1255 svm->nested.hsave->control.tsc_offset;
1256 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001257 } else
1258 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1259 svm->vmcb->control.tsc_offset,
1260 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001261
1262 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001263
1264 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001265}
1266
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001267static void avic_init_vmcb(struct vcpu_svm *svm)
1268{
1269 struct vmcb *vmcb = svm->vmcb;
1270 struct kvm_arch *vm_data = &svm->vcpu.kvm->arch;
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001271 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
1272 phys_addr_t lpa = __sme_set(page_to_phys(vm_data->avic_logical_id_table_page));
1273 phys_addr_t ppa = __sme_set(page_to_phys(vm_data->avic_physical_id_table_page));
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001274
1275 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1276 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1277 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1278 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1279 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001280}
1281
Paolo Bonzini56908912015-10-19 11:30:19 +02001282static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283{
Joerg Roedele6101a92008-02-13 18:58:45 +01001284 struct vmcb_control_area *control = &svm->vmcb->control;
1285 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001287 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001288
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001289 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1290 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1291 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1292 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1293 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1294 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001295 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1296 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297
Paolo Bonzini5315c712014-03-03 13:08:29 +01001298 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299
Joerg Roedel18c918c2010-11-30 18:03:59 +01001300 set_exception_intercept(svm, PF_VECTOR);
1301 set_exception_intercept(svm, UD_VECTOR);
1302 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001303 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001304 set_exception_intercept(svm, DB_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001306 set_intercept(svm, INTERCEPT_INTR);
1307 set_intercept(svm, INTERCEPT_NMI);
1308 set_intercept(svm, INTERCEPT_SMI);
1309 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001310 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001311 set_intercept(svm, INTERCEPT_CPUID);
1312 set_intercept(svm, INTERCEPT_INVD);
1313 set_intercept(svm, INTERCEPT_HLT);
1314 set_intercept(svm, INTERCEPT_INVLPG);
1315 set_intercept(svm, INTERCEPT_INVLPGA);
1316 set_intercept(svm, INTERCEPT_IOIO_PROT);
1317 set_intercept(svm, INTERCEPT_MSR_PROT);
1318 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1319 set_intercept(svm, INTERCEPT_SHUTDOWN);
1320 set_intercept(svm, INTERCEPT_VMRUN);
1321 set_intercept(svm, INTERCEPT_VMMCALL);
1322 set_intercept(svm, INTERCEPT_VMLOAD);
1323 set_intercept(svm, INTERCEPT_VMSAVE);
1324 set_intercept(svm, INTERCEPT_STGI);
1325 set_intercept(svm, INTERCEPT_CLGI);
1326 set_intercept(svm, INTERCEPT_SKINIT);
1327 set_intercept(svm, INTERCEPT_WBINVD);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001328 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329
Michael S. Tsirkin668fffa32017-04-21 12:27:17 +02001330 if (!kvm_mwait_in_guest()) {
1331 set_intercept(svm, INTERCEPT_MONITOR);
1332 set_intercept(svm, INTERCEPT_MWAIT);
1333 }
1334
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001335 control->iopm_base_pa = __sme_set(iopm_base);
1336 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337 control->int_ctl = V_INTR_MASKING_MASK;
1338
1339 init_seg(&save->es);
1340 init_seg(&save->ss);
1341 init_seg(&save->ds);
1342 init_seg(&save->fs);
1343 init_seg(&save->gs);
1344
1345 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001346 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 /* Executable/Readable Code Segment */
1348 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1349 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1350 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351
1352 save->gdtr.limit = 0xffff;
1353 save->idtr.limit = 0xffff;
1354
1355 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1356 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1357
Paolo Bonzini56908912015-10-19 11:30:19 +02001358 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001359 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001360 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001362 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363
Joerg Roedele0231712010-02-24 18:59:10 +01001364 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001365 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001366 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001368 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001369 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001370
Rusty Russell66aee912007-07-17 23:34:16 +10001371 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001373
1374 if (npt_enabled) {
1375 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001376 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001377 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001378 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001379 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1380 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001381 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001382 save->cr3 = 0;
1383 save->cr4 = 0;
1384 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001385 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001386
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001387 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001388 svm->vcpu.arch.hflags = 0;
1389
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001390 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001391 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001392 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001393 }
1394
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001395 if (kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001396 avic_init_vmcb(svm);
1397
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001398 /*
1399 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1400 * in VMCB and clear intercepts to avoid #VMEXIT.
1401 */
1402 if (vls) {
1403 clr_intercept(svm, INTERCEPT_VMLOAD);
1404 clr_intercept(svm, INTERCEPT_VMSAVE);
1405 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1406 }
1407
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001408 if (vgif) {
1409 clr_intercept(svm, INTERCEPT_STGI);
1410 clr_intercept(svm, INTERCEPT_CLGI);
1411 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1412 }
1413
Brijesh Singh1654efc2017-12-04 10:57:34 -06001414 if (sev_guest(svm->vcpu.kvm))
1415 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
1416
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001417 mark_all_dirty(svm->vmcb);
1418
Joerg Roedel2af91942009-08-07 11:49:28 +02001419 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001420
1421}
1422
Dan Carpenterd3e7dec2017-05-18 10:38:53 +03001423static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1424 unsigned int index)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001425{
1426 u64 *avic_physical_id_table;
1427 struct kvm_arch *vm_data = &vcpu->kvm->arch;
1428
1429 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1430 return NULL;
1431
1432 avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page);
1433
1434 return &avic_physical_id_table[index];
1435}
1436
1437/**
1438 * Note:
1439 * AVIC hardware walks the nested page table to check permissions,
1440 * but does not use the SPA address specified in the leaf page
1441 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1442 * field of the VMCB. Therefore, we set up the
1443 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1444 */
1445static int avic_init_access_page(struct kvm_vcpu *vcpu)
1446{
1447 struct kvm *kvm = vcpu->kvm;
1448 int ret;
1449
1450 if (kvm->arch.apic_access_page_done)
1451 return 0;
1452
1453 ret = x86_set_memory_region(kvm,
1454 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1455 APIC_DEFAULT_PHYS_BASE,
1456 PAGE_SIZE);
1457 if (ret)
1458 return ret;
1459
1460 kvm->arch.apic_access_page_done = true;
1461 return 0;
1462}
1463
1464static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1465{
1466 int ret;
1467 u64 *entry, new_entry;
1468 int id = vcpu->vcpu_id;
1469 struct vcpu_svm *svm = to_svm(vcpu);
1470
1471 ret = avic_init_access_page(vcpu);
1472 if (ret)
1473 return ret;
1474
1475 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1476 return -EINVAL;
1477
1478 if (!svm->vcpu.arch.apic->regs)
1479 return -EINVAL;
1480
1481 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1482
1483 /* Setting AVIC backing page address in the phy APIC ID table */
1484 entry = avic_get_physical_id_entry(vcpu, id);
1485 if (!entry)
1486 return -EINVAL;
1487
1488 new_entry = READ_ONCE(*entry);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001489 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
1490 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1491 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001492 WRITE_ONCE(*entry, new_entry);
1493
1494 svm->avic_physical_id_cache = entry;
1495
1496 return 0;
1497}
1498
Brijesh Singh1654efc2017-12-04 10:57:34 -06001499static void __sev_asid_free(int asid)
1500{
1501 int pos;
1502
1503 pos = asid - 1;
1504 clear_bit(pos, sev_asid_bitmap);
1505}
1506
1507static void sev_asid_free(struct kvm *kvm)
1508{
1509 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1510
1511 __sev_asid_free(sev->asid);
1512}
1513
1514static void sev_vm_destroy(struct kvm *kvm)
1515{
1516 if (!sev_guest(kvm))
1517 return;
1518
1519 sev_asid_free(kvm);
1520}
1521
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001522static void avic_vm_destroy(struct kvm *kvm)
1523{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001524 unsigned long flags;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001525 struct kvm_arch *vm_data = &kvm->arch;
1526
Dmitry Vyukov3863dff2017-01-24 14:06:48 +01001527 if (!avic)
1528 return;
1529
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001530 if (vm_data->avic_logical_id_table_page)
1531 __free_page(vm_data->avic_logical_id_table_page);
1532 if (vm_data->avic_physical_id_table_page)
1533 __free_page(vm_data->avic_physical_id_table_page);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001534
1535 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1536 hash_del(&vm_data->hnode);
1537 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001538}
1539
Brijesh Singh1654efc2017-12-04 10:57:34 -06001540static void svm_vm_destroy(struct kvm *kvm)
1541{
1542 avic_vm_destroy(kvm);
1543 sev_vm_destroy(kvm);
1544}
1545
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001546static int avic_vm_init(struct kvm *kvm)
1547{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001548 unsigned long flags;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001549 int err = -ENOMEM;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001550 struct kvm_arch *vm_data = &kvm->arch;
1551 struct page *p_page;
1552 struct page *l_page;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001553 struct kvm_arch *ka;
1554 u32 vm_id;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001555
1556 if (!avic)
1557 return 0;
1558
1559 /* Allocating physical APIC ID table (4KB) */
1560 p_page = alloc_page(GFP_KERNEL);
1561 if (!p_page)
1562 goto free_avic;
1563
1564 vm_data->avic_physical_id_table_page = p_page;
1565 clear_page(page_address(p_page));
1566
1567 /* Allocating logical APIC ID table (4KB) */
1568 l_page = alloc_page(GFP_KERNEL);
1569 if (!l_page)
1570 goto free_avic;
1571
1572 vm_data->avic_logical_id_table_page = l_page;
1573 clear_page(page_address(l_page));
1574
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001575 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001576 again:
1577 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
1578 if (vm_id == 0) { /* id is 1-based, zero is not okay */
1579 next_vm_id_wrapped = 1;
1580 goto again;
1581 }
1582 /* Is it still in use? Only possible if wrapped at least once */
1583 if (next_vm_id_wrapped) {
1584 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1585 struct kvm *k2 = container_of(ka, struct kvm, arch);
1586 struct kvm_arch *vd2 = &k2->arch;
1587 if (vd2->avic_vm_id == vm_id)
1588 goto again;
1589 }
1590 }
1591 vm_data->avic_vm_id = vm_id;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001592 hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id);
1593 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1594
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001595 return 0;
1596
1597free_avic:
1598 avic_vm_destroy(kvm);
1599 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600}
1601
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001602static inline int
1603avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001604{
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001605 int ret = 0;
1606 unsigned long flags;
1607 struct amd_svm_iommu_ir *ir;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001608 struct vcpu_svm *svm = to_svm(vcpu);
1609
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001610 if (!kvm_arch_has_assigned_device(vcpu->kvm))
1611 return 0;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001612
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001613 /*
1614 * Here, we go through the per-vcpu ir_list to update all existing
1615 * interrupt remapping table entry targeting this vcpu.
1616 */
1617 spin_lock_irqsave(&svm->ir_list_lock, flags);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001618
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001619 if (list_empty(&svm->ir_list))
1620 goto out;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001621
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001622 list_for_each_entry(ir, &svm->ir_list, node) {
1623 ret = amd_iommu_update_ga(cpu, r, ir->data);
1624 if (ret)
1625 break;
1626 }
1627out:
1628 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
1629 return ret;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001630}
1631
1632static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1633{
1634 u64 entry;
1635 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05001636 int h_physical_id = kvm_cpu_get_apicid(cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001637 struct vcpu_svm *svm = to_svm(vcpu);
1638
1639 if (!kvm_vcpu_apicv_active(vcpu))
1640 return;
1641
1642 if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
1643 return;
1644
1645 entry = READ_ONCE(*(svm->avic_physical_id_cache));
1646 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
1647
1648 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
1649 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
1650
1651 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1652 if (svm->avic_is_running)
1653 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1654
1655 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001656 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
1657 svm->avic_is_running);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001658}
1659
1660static void avic_vcpu_put(struct kvm_vcpu *vcpu)
1661{
1662 u64 entry;
1663 struct vcpu_svm *svm = to_svm(vcpu);
1664
1665 if (!kvm_vcpu_apicv_active(vcpu))
1666 return;
1667
1668 entry = READ_ONCE(*(svm->avic_physical_id_cache));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001669 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
1670 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
1671
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001672 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1673 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674}
1675
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001676/**
1677 * This function is called during VCPU halt/unhalt.
1678 */
1679static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
1680{
1681 struct vcpu_svm *svm = to_svm(vcpu);
1682
1683 svm->avic_is_running = is_run;
1684 if (is_run)
1685 avic_vcpu_load(vcpu, vcpu->cpu);
1686 else
1687 avic_vcpu_put(vcpu);
1688}
1689
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001690static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001691{
1692 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001693 u32 dummy;
1694 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001695
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001696 if (!init_event) {
1697 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1698 MSR_IA32_APICBASE_ENABLE;
1699 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1700 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1701 }
Paolo Bonzini56908912015-10-19 11:30:19 +02001702 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001703
Yu Zhange911eb32017-08-24 20:27:52 +08001704 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001705 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001706
1707 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1708 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001709}
1710
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001711static int avic_init_vcpu(struct vcpu_svm *svm)
1712{
1713 int ret;
1714
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001715 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001716 return 0;
1717
1718 ret = avic_init_backing_page(&svm->vcpu);
1719 if (ret)
1720 return ret;
1721
1722 INIT_LIST_HEAD(&svm->ir_list);
1723 spin_lock_init(&svm->ir_list_lock);
1724
1725 return ret;
1726}
1727
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001728static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001730 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001731 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001732 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001733 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001734 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001735 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001736
Rusty Russellc16f8622007-07-30 21:12:19 +10001737 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001738 if (!svm) {
1739 err = -ENOMEM;
1740 goto out;
1741 }
1742
1743 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1744 if (err)
1745 goto free_svm;
1746
Joerg Roedelf65c2292008-02-13 18:58:46 +01001747 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001748 page = alloc_page(GFP_KERNEL);
1749 if (!page)
1750 goto uninit;
1751
Joerg Roedelf65c2292008-02-13 18:58:46 +01001752 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1753 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001754 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001755
1756 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1757 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001758 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001759
Alexander Grafb286d5d2008-11-25 20:17:05 +01001760 hsave_page = alloc_page(GFP_KERNEL);
1761 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001762 goto free_page3;
1763
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001764 err = avic_init_vcpu(svm);
1765 if (err)
1766 goto free_page4;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001767
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001768 /* We initialize this flag to true to make sure that the is_running
1769 * bit would be set the first time the vcpu is loaded.
1770 */
1771 svm->avic_is_running = true;
1772
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001773 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001774
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001775 svm->msrpm = page_address(msrpm_pages);
1776 svm_vcpu_init_msrpm(svm->msrpm);
1777
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001778 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001779 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001780
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001781 svm->vmcb = page_address(page);
1782 clear_page(svm->vmcb);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001783 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001784 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02001785 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001787 svm_init_osvw(&svm->vcpu);
1788
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001789 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001790
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001791free_page4:
1792 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001793free_page3:
1794 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1795free_page2:
1796 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1797free_page1:
1798 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001799uninit:
1800 kvm_vcpu_uninit(&svm->vcpu);
1801free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001802 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001803out:
1804 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805}
1806
1807static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1808{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001809 struct vcpu_svm *svm = to_svm(vcpu);
1810
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001811 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001812 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001813 __free_page(virt_to_page(svm->nested.hsave));
1814 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001815 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001816 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817}
1818
Avi Kivity15ad7142007-07-11 18:17:21 +03001819static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001821 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001822 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001823
Avi Kivity0cc50642007-03-25 12:07:27 +02001824 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001825 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001826 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001827 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001828
Avi Kivity82ca2d12010-10-21 12:20:34 +02001829#ifdef CONFIG_X86_64
1830 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1831#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001832 savesegment(fs, svm->host.fs);
1833 savesegment(gs, svm->host.gs);
1834 svm->host.ldt = kvm_read_ldt();
1835
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001836 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001837 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001838
Haozhong Zhangad7218832015-10-20 15:39:02 +08001839 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1840 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1841 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1842 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1843 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1844 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001845 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01001846 /* This assumes that the kernel never uses MSR_TSC_AUX */
1847 if (static_cpu_has(X86_FEATURE_RDTSCP))
1848 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001849
1850 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001851}
1852
1853static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1854{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001855 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001856 int i;
1857
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001858 avic_vcpu_put(vcpu);
1859
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001860 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001861 kvm_load_ldt(svm->host.ldt);
1862#ifdef CONFIG_X86_64
1863 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07001864 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001865 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001866#else
Avi Kivity831ca602011-03-08 16:09:51 +02001867#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001868 loadsegment(gs, svm->host.gs);
1869#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001870#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001871 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001872 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873}
1874
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001875static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
1876{
1877 avic_set_running(vcpu, false);
1878}
1879
1880static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
1881{
1882 avic_set_running(vcpu, true);
1883}
1884
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1886{
Ladi Prosek9b611742017-06-21 09:06:59 +02001887 struct vcpu_svm *svm = to_svm(vcpu);
1888 unsigned long rflags = svm->vmcb->save.rflags;
1889
1890 if (svm->nmi_singlestep) {
1891 /* Hide our flags if they were not set by the guest */
1892 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1893 rflags &= ~X86_EFLAGS_TF;
1894 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1895 rflags &= ~X86_EFLAGS_RF;
1896 }
1897 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898}
1899
1900static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1901{
Ladi Prosek9b611742017-06-21 09:06:59 +02001902 if (to_svm(vcpu)->nmi_singlestep)
1903 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1904
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001905 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02001906 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001907 * (caused by either a task switch or an inter-privilege IRET),
1908 * so we do not need to update the CPL here.
1909 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001910 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911}
1912
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001913static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1914{
1915 switch (reg) {
1916 case VCPU_EXREG_PDPTR:
1917 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001918 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001919 break;
1920 default:
1921 BUG();
1922 }
1923}
1924
Alexander Graff0b85052008-11-25 20:17:01 +01001925static void svm_set_vintr(struct vcpu_svm *svm)
1926{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001927 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001928}
1929
1930static void svm_clear_vintr(struct vcpu_svm *svm)
1931{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001932 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001933}
1934
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1936{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001937 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938
1939 switch (seg) {
1940 case VCPU_SREG_CS: return &save->cs;
1941 case VCPU_SREG_DS: return &save->ds;
1942 case VCPU_SREG_ES: return &save->es;
1943 case VCPU_SREG_FS: return &save->fs;
1944 case VCPU_SREG_GS: return &save->gs;
1945 case VCPU_SREG_SS: return &save->ss;
1946 case VCPU_SREG_TR: return &save->tr;
1947 case VCPU_SREG_LDTR: return &save->ldtr;
1948 }
1949 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001950 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001951}
1952
1953static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1954{
1955 struct vmcb_seg *s = svm_seg(vcpu, seg);
1956
1957 return s->base;
1958}
1959
1960static void svm_get_segment(struct kvm_vcpu *vcpu,
1961 struct kvm_segment *var, int seg)
1962{
1963 struct vmcb_seg *s = svm_seg(vcpu, seg);
1964
1965 var->base = s->base;
1966 var->limit = s->limit;
1967 var->selector = s->selector;
1968 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1969 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1970 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1971 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1972 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1973 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1974 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301975
1976 /*
1977 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1978 * However, the SVM spec states that the G bit is not observed by the
1979 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1980 * So let's synthesize a legal G bit for all segments, this helps
1981 * running KVM nested. It also helps cross-vendor migration, because
1982 * Intel's vmentry has a check on the 'G' bit.
1983 */
1984 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001985
Joerg Roedele0231712010-02-24 18:59:10 +01001986 /*
1987 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001988 * for cross vendor migration purposes by "not present"
1989 */
Gioh Kim8eae9572017-05-30 15:24:45 +02001990 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02001991
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001992 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001993 case VCPU_SREG_TR:
1994 /*
1995 * Work around a bug where the busy flag in the tr selector
1996 * isn't exposed
1997 */
Amit Shahc0d09822008-10-27 09:04:18 +00001998 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001999 break;
2000 case VCPU_SREG_DS:
2001 case VCPU_SREG_ES:
2002 case VCPU_SREG_FS:
2003 case VCPU_SREG_GS:
2004 /*
2005 * The accessed bit must always be set in the segment
2006 * descriptor cache, although it can be cleared in the
2007 * descriptor, the cached bit always remains at 1. Since
2008 * Intel has a check on this, set it here to support
2009 * cross-vendor migration.
2010 */
2011 if (!var->unusable)
2012 var->type |= 0x1;
2013 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02002014 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01002015 /*
2016 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02002017 * descriptor is left as 1, although the whole segment has
2018 * been made unusable. Clear it here to pass an Intel VMX
2019 * entry check when cross vendor migrating.
2020 */
2021 if (var->unusable)
2022 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02002023 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02002024 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02002025 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002026 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027}
2028
Izik Eidus2e4d2652008-03-24 19:38:34 +02002029static int svm_get_cpl(struct kvm_vcpu *vcpu)
2030{
2031 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
2032
2033 return save->cpl;
2034}
2035
Gleb Natapov89a27f42010-02-16 10:51:48 +02002036static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002038 struct vcpu_svm *svm = to_svm(vcpu);
2039
Gleb Natapov89a27f42010-02-16 10:51:48 +02002040 dt->size = svm->vmcb->save.idtr.limit;
2041 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002042}
2043
Gleb Natapov89a27f42010-02-16 10:51:48 +02002044static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002046 struct vcpu_svm *svm = to_svm(vcpu);
2047
Gleb Natapov89a27f42010-02-16 10:51:48 +02002048 svm->vmcb->save.idtr.limit = dt->size;
2049 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002050 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051}
2052
Gleb Natapov89a27f42010-02-16 10:51:48 +02002053static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002055 struct vcpu_svm *svm = to_svm(vcpu);
2056
Gleb Natapov89a27f42010-02-16 10:51:48 +02002057 dt->size = svm->vmcb->save.gdtr.limit;
2058 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059}
2060
Gleb Natapov89a27f42010-02-16 10:51:48 +02002061static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002063 struct vcpu_svm *svm = to_svm(vcpu);
2064
Gleb Natapov89a27f42010-02-16 10:51:48 +02002065 svm->vmcb->save.gdtr.limit = dt->size;
2066 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002067 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068}
2069
Avi Kivitye8467fd2009-12-29 18:43:06 +02002070static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2071{
2072}
2073
Avi Kivityaff48ba2010-12-05 18:56:11 +02002074static void svm_decache_cr3(struct kvm_vcpu *vcpu)
2075{
2076}
2077
Anthony Liguori25c4c272007-04-27 09:29:21 +03002078static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002079{
2080}
2081
Avi Kivityd2251572010-01-06 10:55:27 +02002082static void update_cr0_intercept(struct vcpu_svm *svm)
2083{
2084 ulong gcr0 = svm->vcpu.arch.cr0;
2085 u64 *hcr0 = &svm->vmcb->save.cr0;
2086
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002087 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
2088 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
Avi Kivityd2251572010-01-06 10:55:27 +02002089
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002090 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002091
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002092 if (gcr0 == *hcr0) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002093 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
2094 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002095 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002096 set_cr_intercept(svm, INTERCEPT_CR0_READ);
2097 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002098 }
2099}
2100
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2102{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002103 struct vcpu_svm *svm = to_svm(vcpu);
2104
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002105#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002106 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002107 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002108 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002109 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110 }
2111
Mike Dayd77c26f2007-10-08 09:02:08 -04002112 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002113 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002114 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115 }
2116 }
2117#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002118 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002119
2120 if (!npt_enabled)
2121 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02002122
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02002123 /*
2124 * re-enable caching here because the QEMU bios
2125 * does not do it - this results in some delay at
2126 * reboot
2127 */
2128 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
2129 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002130 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002131 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002132 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133}
2134
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002135static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002137 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002138 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
2139
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002140 if (cr4 & X86_CR4_VMXE)
2141 return 1;
2142
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002143 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002144 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02002145
Joerg Roedelec077262008-04-09 14:15:28 +02002146 vcpu->arch.cr4 = cr4;
2147 if (!npt_enabled)
2148 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02002149 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02002150 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002151 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002152 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153}
2154
2155static void svm_set_segment(struct kvm_vcpu *vcpu,
2156 struct kvm_segment *var, int seg)
2157{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002158 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159 struct vmcb_seg *s = svm_seg(vcpu, seg);
2160
2161 s->base = var->base;
2162 s->limit = var->limit;
2163 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02002164 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2165 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2166 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2167 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2168 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2169 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2170 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2171 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002172
2173 /*
2174 * This is always accurate, except if SYSRET returned to a segment
2175 * with SS.DPL != 3. Intel does not have this quirk, and always
2176 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2177 * would entail passing the CPL to userspace and back.
2178 */
2179 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02002180 /* This is symmetric with svm_get_segment() */
2181 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182
Joerg Roedel060d0c92010-12-03 11:45:57 +01002183 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184}
2185
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01002186static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002188 struct vcpu_svm *svm = to_svm(vcpu);
2189
Joerg Roedel18c918c2010-11-30 18:03:59 +01002190 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03002191
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002192 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002193 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01002194 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002195 } else
2196 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03002197}
2198
Tejun Heo0fe1e002009-10-29 22:34:14 +09002199static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200{
Tejun Heo0fe1e002009-10-29 22:34:14 +09002201 if (sd->next_asid > sd->max_asid) {
2202 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06002203 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002204 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002205 }
2206
Tejun Heo0fe1e002009-10-29 22:34:14 +09002207 svm->asid_generation = sd->asid_generation;
2208 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01002209
2210 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002211}
2212
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002213static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2214{
2215 return to_svm(vcpu)->vmcb->save.dr6;
2216}
2217
2218static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2219{
2220 struct vcpu_svm *svm = to_svm(vcpu);
2221
2222 svm->vmcb->save.dr6 = value;
2223 mark_dirty(svm->vmcb, VMCB_DR);
2224}
2225
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002226static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2227{
2228 struct vcpu_svm *svm = to_svm(vcpu);
2229
2230 get_debugreg(vcpu->arch.db[0], 0);
2231 get_debugreg(vcpu->arch.db[1], 1);
2232 get_debugreg(vcpu->arch.db[2], 2);
2233 get_debugreg(vcpu->arch.db[3], 3);
2234 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2235 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2236
2237 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2238 set_dr_intercepts(svm);
2239}
2240
Gleb Natapov020df072010-04-13 10:05:23 +03002241static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002242{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002243 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002244
Gleb Natapov020df072010-04-13 10:05:23 +03002245 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01002246 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247}
2248
Avi Kivity851ba692009-08-24 11:10:17 +03002249static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250{
Gleb Natapov631bc482010-10-14 11:22:52 +02002251 u64 fault_address = svm->vmcb->control.exit_info_2;
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002252 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002254 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
Andre Przywaradc25e892010-12-21 11:12:07 +01002255 svm->vmcb->control.insn_bytes,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002256 svm->vmcb->control.insn_len);
2257}
2258
2259static int npf_interception(struct vcpu_svm *svm)
2260{
2261 u64 fault_address = svm->vmcb->control.exit_info_2;
2262 u64 error_code = svm->vmcb->control.exit_info_1;
2263
2264 trace_kvm_page_fault(fault_address, error_code);
2265 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
2266 svm->vmcb->control.insn_bytes,
2267 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268}
2269
Avi Kivity851ba692009-08-24 11:10:17 +03002270static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002271{
Avi Kivity851ba692009-08-24 11:10:17 +03002272 struct kvm_run *kvm_run = svm->vcpu.run;
2273
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002274 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03002275 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02002276 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002277 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2278 return 1;
2279 }
Gleb Natapov44c11432009-05-11 13:35:52 +03002280
Jan Kiszka6be7d302009-10-18 13:24:54 +02002281 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02002282 disable_nmi_singlestep(svm);
Gleb Natapov44c11432009-05-11 13:35:52 +03002283 }
2284
2285 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002286 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002287 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2288 kvm_run->debug.arch.pc =
2289 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2290 kvm_run->debug.arch.exception = DB_VECTOR;
2291 return 0;
2292 }
2293
2294 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002295}
2296
Avi Kivity851ba692009-08-24 11:10:17 +03002297static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002298{
Avi Kivity851ba692009-08-24 11:10:17 +03002299 struct kvm_run *kvm_run = svm->vcpu.run;
2300
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002301 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2302 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2303 kvm_run->debug.arch.exception = BP_VECTOR;
2304 return 0;
2305}
2306
Avi Kivity851ba692009-08-24 11:10:17 +03002307static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002308{
2309 int er;
2310
Andre Przywara51d8b662010-12-21 11:12:02 +01002311 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002312 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002313 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002314 return 1;
2315}
2316
Eric Northup54a20552015-11-03 18:03:53 +01002317static int ac_interception(struct vcpu_svm *svm)
2318{
2319 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2320 return 1;
2321}
2322
Joerg Roedel67ec6602010-05-17 14:43:35 +02002323static bool is_erratum_383(void)
2324{
2325 int err, i;
2326 u64 value;
2327
2328 if (!erratum_383_found)
2329 return false;
2330
2331 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2332 if (err)
2333 return false;
2334
2335 /* Bit 62 may or may not be set for this mce */
2336 value &= ~(1ULL << 62);
2337
2338 if (value != 0xb600000000010015ULL)
2339 return false;
2340
2341 /* Clear MCi_STATUS registers */
2342 for (i = 0; i < 6; ++i)
2343 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2344
2345 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2346 if (!err) {
2347 u32 low, high;
2348
2349 value &= ~(1ULL << 2);
2350 low = lower_32_bits(value);
2351 high = upper_32_bits(value);
2352
2353 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2354 }
2355
2356 /* Flush tlb to evict multi-match entries */
2357 __flush_tlb_all();
2358
2359 return true;
2360}
2361
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002362static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002363{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002364 if (is_erratum_383()) {
2365 /*
2366 * Erratum 383 triggered. Guest state is corrupt so kill the
2367 * guest.
2368 */
2369 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2370
Avi Kivitya8eeb042010-05-10 12:34:53 +03002371 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002372
2373 return;
2374 }
2375
Joerg Roedel53371b52008-04-09 14:15:30 +02002376 /*
2377 * On an #MC intercept the MCE handler is not called automatically in
2378 * the host. So do it by hand here.
2379 */
2380 asm volatile (
2381 "int $0x12\n");
2382 /* not sure if we ever come back to this point */
2383
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002384 return;
2385}
2386
2387static int mc_interception(struct vcpu_svm *svm)
2388{
Joerg Roedel53371b52008-04-09 14:15:30 +02002389 return 1;
2390}
2391
Avi Kivity851ba692009-08-24 11:10:17 +03002392static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002393{
Avi Kivity851ba692009-08-24 11:10:17 +03002394 struct kvm_run *kvm_run = svm->vcpu.run;
2395
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002396 /*
2397 * VMCB is undefined after a SHUTDOWN intercept
2398 * so reinitialize it.
2399 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002400 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002401 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002402
2403 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2404 return 0;
2405}
2406
Avi Kivity851ba692009-08-24 11:10:17 +03002407static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002409 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002410 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002411 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002412 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413
Rusty Russelle756fc62007-07-30 20:07:08 +10002414 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002415 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002416 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002417 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01002418 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002419
Avi Kivity039576c2007-03-20 12:46:50 +02002420 port = io_info >> 16;
2421 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002422 svm->next_rip = svm->vmcb->control.exit_info_2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002423 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002424
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002425 /*
2426 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
2427 * KVM_EXIT_DEBUG here.
2428 */
2429 if (in)
2430 return kvm_fast_pio_in(vcpu, size, port) && ret;
2431 else
2432 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433}
2434
Avi Kivity851ba692009-08-24 11:10:17 +03002435static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002436{
2437 return 1;
2438}
2439
Avi Kivity851ba692009-08-24 11:10:17 +03002440static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002441{
2442 ++svm->vcpu.stat.irq_exits;
2443 return 1;
2444}
2445
Avi Kivity851ba692009-08-24 11:10:17 +03002446static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447{
2448 return 1;
2449}
2450
Avi Kivity851ba692009-08-24 11:10:17 +03002451static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002453 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002454 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455}
2456
Avi Kivity851ba692009-08-24 11:10:17 +03002457static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002458{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002459 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002460 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002461}
2462
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002463static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2464{
2465 struct vcpu_svm *svm = to_svm(vcpu);
2466
2467 return svm->nested.nested_cr3;
2468}
2469
Avi Kivitye4e517b2011-07-28 11:36:17 +03002470static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2471{
2472 struct vcpu_svm *svm = to_svm(vcpu);
2473 u64 cr3 = svm->nested.nested_cr3;
2474 u64 pdpte;
2475 int ret;
2476
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002477 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002478 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002479 if (ret)
2480 return 0;
2481 return pdpte;
2482}
2483
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002484static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2485 unsigned long root)
2486{
2487 struct vcpu_svm *svm = to_svm(vcpu);
2488
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002489 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01002490 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002491 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002492}
2493
Avi Kivity6389ee92010-11-29 16:12:30 +02002494static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2495 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002496{
2497 struct vcpu_svm *svm = to_svm(vcpu);
2498
Paolo Bonzini5e352512014-09-02 13:18:37 +02002499 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2500 /*
2501 * TODO: track the cause of the nested page fault, and
2502 * correctly fill in the high bits of exit_info_1.
2503 */
2504 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2505 svm->vmcb->control.exit_code_hi = 0;
2506 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2507 svm->vmcb->control.exit_info_2 = fault->address;
2508 }
2509
2510 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2511 svm->vmcb->control.exit_info_1 |= fault->error_code;
2512
2513 /*
2514 * The present bit is always zero for page structure faults on real
2515 * hardware.
2516 */
2517 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2518 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002519
2520 nested_svm_vmexit(svm);
2521}
2522
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002523static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002524{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002525 WARN_ON(mmu_is_nested(vcpu));
2526 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002527 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2528 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002529 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002530 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
Yu Zhang855feb62017-08-24 20:27:55 +08002531 vcpu->arch.mmu.shadow_root_level = get_npt_level(vcpu);
Xiao Guangrongc258b622015-08-05 12:04:24 +08002532 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002533 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002534}
2535
2536static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2537{
2538 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2539}
2540
Alexander Grafc0725422008-11-25 20:17:03 +01002541static int nested_svm_check_permissions(struct vcpu_svm *svm)
2542{
Dan Carpentere9196ce2017-05-18 10:39:53 +03002543 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
2544 !is_paging(&svm->vcpu)) {
Alexander Grafc0725422008-11-25 20:17:03 +01002545 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2546 return 1;
2547 }
2548
2549 if (svm->vmcb->save.cpl) {
2550 kvm_inject_gp(&svm->vcpu, 0);
2551 return 1;
2552 }
2553
Dan Carpentere9196ce2017-05-18 10:39:53 +03002554 return 0;
Alexander Grafc0725422008-11-25 20:17:03 +01002555}
2556
Alexander Grafcf74a782008-11-25 20:17:08 +01002557static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2558 bool has_error_code, u32 error_code)
2559{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002560 int vmexit;
2561
Joerg Roedel20307532010-11-29 17:51:48 +01002562 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002563 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002564
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002565 vmexit = nested_svm_intercept(svm);
2566 if (vmexit != NESTED_EXIT_DONE)
2567 return 0;
2568
Joerg Roedel0295ad72009-08-07 11:49:37 +02002569 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2570 svm->vmcb->control.exit_code_hi = 0;
2571 svm->vmcb->control.exit_info_1 = error_code;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002572
2573 /*
2574 * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception.
2575 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2576 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be
2577 * written only when inject_pending_event runs (DR6 would written here
2578 * too). This should be conditional on a new capability---if the
2579 * capability is disabled, kvm_multiple_exception would write the
2580 * ancillary information to CR2 or DR6, for backwards ABI-compatibility.
2581 */
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002582 if (svm->vcpu.arch.exception.nested_apf)
2583 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
2584 else
2585 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
Joerg Roedel0295ad72009-08-07 11:49:37 +02002586
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002587 svm->nested.exit_required = true;
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002588 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002589}
2590
Joerg Roedel8fe54652010-02-19 16:23:01 +01002591/* This function returns true if it is save to enable the irq window */
2592static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002593{
Joerg Roedel20307532010-11-29 17:51:48 +01002594 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002595 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002596
Joerg Roedel26666952009-08-07 11:49:46 +02002597 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002598 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002599
Joerg Roedel26666952009-08-07 11:49:46 +02002600 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002601 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002602
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002603 /*
2604 * if vmexit was already requested (by intercepted exception
2605 * for instance) do not overwrite it with "external interrupt"
2606 * vmexit.
2607 */
2608 if (svm->nested.exit_required)
2609 return false;
2610
Joerg Roedel197717d2010-02-24 18:59:19 +01002611 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2612 svm->vmcb->control.exit_info_1 = 0;
2613 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002614
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002615 if (svm->nested.intercept & 1ULL) {
2616 /*
2617 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002618 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002619 * #vmexit emulation might sleep. Only signal request for
2620 * the #vmexit here.
2621 */
2622 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002623 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002624 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002625 }
2626
Joerg Roedel8fe54652010-02-19 16:23:01 +01002627 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002628}
2629
Joerg Roedel887f5002010-02-24 18:59:12 +01002630/* This function returns true if it is save to enable the nmi window */
2631static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2632{
Joerg Roedel20307532010-11-29 17:51:48 +01002633 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002634 return true;
2635
2636 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2637 return true;
2638
2639 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2640 svm->nested.exit_required = true;
2641
2642 return false;
2643}
2644
Joerg Roedel7597f122010-02-19 16:23:00 +01002645static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002646{
2647 struct page *page;
2648
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002649 might_sleep();
2650
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002651 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002652 if (is_error_page(page))
2653 goto error;
2654
Joerg Roedel7597f122010-02-19 16:23:00 +01002655 *_page = page;
2656
2657 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002658
2659error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002660 kvm_inject_gp(&svm->vcpu, 0);
2661
2662 return NULL;
2663}
2664
Joerg Roedel7597f122010-02-19 16:23:00 +01002665static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002666{
Joerg Roedel7597f122010-02-19 16:23:00 +01002667 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002668 kvm_release_page_dirty(page);
2669}
2670
Joerg Roedelce2ac082010-03-01 15:34:39 +01002671static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002672{
Jan Kiszka9bf41832014-06-30 10:54:17 +02002673 unsigned port, size, iopm_len;
2674 u16 val, mask;
2675 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002676 u64 gpa;
2677
2678 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2679 return NESTED_EXIT_HOST;
2680
2681 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02002682 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2683 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002684 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02002685 start_bit = port % 8;
2686 iopm_len = (start_bit + size > 8) ? 2 : 1;
2687 mask = (0xf >> (4 - size)) << start_bit;
2688 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002689
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002690 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02002691 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002692
Jan Kiszka9bf41832014-06-30 10:54:17 +02002693 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002694}
2695
Joerg Roedeld2477822010-03-01 15:34:34 +01002696static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002697{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002698 u32 offset, msr, value;
2699 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002700
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002701 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002702 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002703
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002704 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2705 offset = svm_msrpm_offset(msr);
2706 write = svm->vmcb->control.exit_info_1 & 1;
2707 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002708
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002709 if (offset == MSR_INVALID)
2710 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002711
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002712 /* Offset is in 32 bit units but need in 8 bit units */
2713 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002714
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002715 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002716 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002717
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002718 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002719}
2720
Ladi Prosekab2f4d732017-06-21 09:06:58 +02002721/* DB exceptions for our internal use must not cause vmexit */
2722static int nested_svm_intercept_db(struct vcpu_svm *svm)
2723{
2724 unsigned long dr6;
2725
2726 /* if we're not singlestepping, it's not ours */
2727 if (!svm->nmi_singlestep)
2728 return NESTED_EXIT_DONE;
2729
2730 /* if it's not a singlestep exception, it's not ours */
2731 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
2732 return NESTED_EXIT_DONE;
2733 if (!(dr6 & DR6_BS))
2734 return NESTED_EXIT_DONE;
2735
2736 /* if the guest is singlestepping, it should get the vmexit */
2737 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
2738 disable_nmi_singlestep(svm);
2739 return NESTED_EXIT_DONE;
2740 }
2741
2742 /* it's ours, the nested hypervisor must not see this one */
2743 return NESTED_EXIT_HOST;
2744}
2745
Joerg Roedel410e4d52009-08-07 11:49:44 +02002746static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002747{
Alexander Grafcf74a782008-11-25 20:17:08 +01002748 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002749
Joerg Roedel410e4d52009-08-07 11:49:44 +02002750 switch (exit_code) {
2751 case SVM_EXIT_INTR:
2752 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002753 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002754 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002755 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002756 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002757 if (npt_enabled)
2758 return NESTED_EXIT_HOST;
2759 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002760 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002761 /* When we're shadowing, trap PFs, but not async PF */
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002762 if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002763 return NESTED_EXIT_HOST;
2764 break;
2765 default:
2766 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002767 }
2768
Joerg Roedel410e4d52009-08-07 11:49:44 +02002769 return NESTED_EXIT_CONTINUE;
2770}
2771
2772/*
2773 * If this function returns true, this #vmexit was already handled
2774 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002775static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002776{
2777 u32 exit_code = svm->vmcb->control.exit_code;
2778 int vmexit = NESTED_EXIT_HOST;
2779
Alexander Grafcf74a782008-11-25 20:17:08 +01002780 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002781 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002782 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002783 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002784 case SVM_EXIT_IOIO:
2785 vmexit = nested_svm_intercept_ioio(svm);
2786 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002787 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2788 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2789 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002790 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002791 break;
2792 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002793 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2794 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2795 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002796 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002797 break;
2798 }
2799 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2800 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Ladi Prosekab2f4d732017-06-21 09:06:58 +02002801 if (svm->nested.intercept_exceptions & excp_bits) {
2802 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
2803 vmexit = nested_svm_intercept_db(svm);
2804 else
2805 vmexit = NESTED_EXIT_DONE;
2806 }
Gleb Natapov631bc482010-10-14 11:22:52 +02002807 /* async page fault always cause vmexit */
2808 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002809 svm->vcpu.arch.exception.nested_apf != 0)
Gleb Natapov631bc482010-10-14 11:22:52 +02002810 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002811 break;
2812 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002813 case SVM_EXIT_ERR: {
2814 vmexit = NESTED_EXIT_DONE;
2815 break;
2816 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002817 default: {
2818 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002819 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002820 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002821 }
2822 }
2823
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002824 return vmexit;
2825}
2826
2827static int nested_svm_exit_handled(struct vcpu_svm *svm)
2828{
2829 int vmexit;
2830
2831 vmexit = nested_svm_intercept(svm);
2832
2833 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002834 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002835
2836 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002837}
2838
Joerg Roedel0460a972009-08-07 11:49:31 +02002839static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2840{
2841 struct vmcb_control_area *dst = &dst_vmcb->control;
2842 struct vmcb_control_area *from = &from_vmcb->control;
2843
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002844 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002845 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002846 dst->intercept_exceptions = from->intercept_exceptions;
2847 dst->intercept = from->intercept;
2848 dst->iopm_base_pa = from->iopm_base_pa;
2849 dst->msrpm_base_pa = from->msrpm_base_pa;
2850 dst->tsc_offset = from->tsc_offset;
2851 dst->asid = from->asid;
2852 dst->tlb_ctl = from->tlb_ctl;
2853 dst->int_ctl = from->int_ctl;
2854 dst->int_vector = from->int_vector;
2855 dst->int_state = from->int_state;
2856 dst->exit_code = from->exit_code;
2857 dst->exit_code_hi = from->exit_code_hi;
2858 dst->exit_info_1 = from->exit_info_1;
2859 dst->exit_info_2 = from->exit_info_2;
2860 dst->exit_int_info = from->exit_int_info;
2861 dst->exit_int_info_err = from->exit_int_info_err;
2862 dst->nested_ctl = from->nested_ctl;
2863 dst->event_inj = from->event_inj;
2864 dst->event_inj_err = from->event_inj_err;
2865 dst->nested_cr3 = from->nested_cr3;
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05002866 dst->virt_ext = from->virt_ext;
Joerg Roedel0460a972009-08-07 11:49:31 +02002867}
2868
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002869static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002870{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002871 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002872 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002873 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002874 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002875
Joerg Roedel17897f32009-10-09 16:08:29 +02002876 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2877 vmcb->control.exit_info_1,
2878 vmcb->control.exit_info_2,
2879 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002880 vmcb->control.exit_int_info_err,
2881 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002882
Joerg Roedel7597f122010-02-19 16:23:00 +01002883 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002884 if (!nested_vmcb)
2885 return 1;
2886
Joerg Roedel20307532010-11-29 17:51:48 +01002887 /* Exit Guest-Mode */
2888 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002889 svm->nested.vmcb = 0;
2890
Alexander Grafcf74a782008-11-25 20:17:08 +01002891 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002892 disable_gif(svm);
2893
2894 nested_vmcb->save.es = vmcb->save.es;
2895 nested_vmcb->save.cs = vmcb->save.cs;
2896 nested_vmcb->save.ss = vmcb->save.ss;
2897 nested_vmcb->save.ds = vmcb->save.ds;
2898 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2899 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002900 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002901 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002902 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002903 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002904 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002905 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002906 nested_vmcb->save.rip = vmcb->save.rip;
2907 nested_vmcb->save.rsp = vmcb->save.rsp;
2908 nested_vmcb->save.rax = vmcb->save.rax;
2909 nested_vmcb->save.dr7 = vmcb->save.dr7;
2910 nested_vmcb->save.dr6 = vmcb->save.dr6;
2911 nested_vmcb->save.cpl = vmcb->save.cpl;
2912
2913 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2914 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2915 nested_vmcb->control.int_state = vmcb->control.int_state;
2916 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2917 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2918 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2919 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2920 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2921 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02002922
2923 if (svm->nrips_enabled)
2924 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002925
2926 /*
2927 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2928 * to make sure that we do not lose injected events. So check event_inj
2929 * here and copy it to exit_int_info if it is valid.
2930 * Exit_int_info and event_inj can't be both valid because the case
2931 * below only happens on a VMRUN instruction intercept which has
2932 * no valid exit_int_info set.
2933 */
2934 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2935 struct vmcb_control_area *nc = &nested_vmcb->control;
2936
2937 nc->exit_int_info = vmcb->control.event_inj;
2938 nc->exit_int_info_err = vmcb->control.event_inj_err;
2939 }
2940
Joerg Roedel33740e42009-08-07 11:49:29 +02002941 nested_vmcb->control.tlb_ctl = 0;
2942 nested_vmcb->control.event_inj = 0;
2943 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002944
2945 /* We always set V_INTR_MASKING and remember the old value in hflags */
2946 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2947 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2948
Alexander Grafcf74a782008-11-25 20:17:08 +01002949 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002950 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002951
Alexander Graf219b65d2009-06-15 15:21:25 +02002952 kvm_clear_exception_queue(&svm->vcpu);
2953 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002954
Joerg Roedel4b161842010-09-10 17:31:03 +02002955 svm->nested.nested_cr3 = 0;
2956
Alexander Grafcf74a782008-11-25 20:17:08 +01002957 /* Restore selected save entries */
2958 svm->vmcb->save.es = hsave->save.es;
2959 svm->vmcb->save.cs = hsave->save.cs;
2960 svm->vmcb->save.ss = hsave->save.ss;
2961 svm->vmcb->save.ds = hsave->save.ds;
2962 svm->vmcb->save.gdtr = hsave->save.gdtr;
2963 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002964 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002965 svm_set_efer(&svm->vcpu, hsave->save.efer);
2966 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2967 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2968 if (npt_enabled) {
2969 svm->vmcb->save.cr3 = hsave->save.cr3;
2970 svm->vcpu.arch.cr3 = hsave->save.cr3;
2971 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002972 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002973 }
2974 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2975 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2976 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2977 svm->vmcb->save.dr7 = 0;
2978 svm->vmcb->save.cpl = 0;
2979 svm->vmcb->control.exit_int_info = 0;
2980
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002981 mark_all_dirty(svm->vmcb);
2982
Joerg Roedel7597f122010-02-19 16:23:00 +01002983 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002984
Joerg Roedel4b161842010-09-10 17:31:03 +02002985 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002986 kvm_mmu_reset_context(&svm->vcpu);
2987 kvm_mmu_load(&svm->vcpu);
2988
2989 return 0;
2990}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002991
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002992static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002993{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002994 /*
2995 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002996 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01002997 * the kvm msr permission bitmap may contain zero bits
2998 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002999 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003000
Joerg Roedel323c3d82010-03-01 15:34:37 +01003001 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3002 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003003
Joerg Roedel323c3d82010-03-01 15:34:37 +01003004 for (i = 0; i < MSRPM_OFFSETS; i++) {
3005 u32 value, p;
3006 u64 offset;
3007
3008 if (msrpm_offsets[i] == 0xffffffff)
3009 break;
3010
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003011 p = msrpm_offsets[i];
3012 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01003013
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003014 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01003015 return false;
3016
3017 svm->nested.msrpm[p] = svm->msrpm[p] | value;
3018 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003019
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05003020 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
Alexander Graf3d6368e2008-11-25 20:17:07 +01003021
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003022 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003023}
3024
Joerg Roedel52c65a302010-08-02 16:46:44 +02003025static bool nested_vmcb_checks(struct vmcb *vmcb)
3026{
3027 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
3028 return false;
3029
Joerg Roedeldbe77582010-08-02 16:46:45 +02003030 if (vmcb->control.asid == 0)
3031 return false;
3032
Tom Lendackycea3a192017-12-04 10:57:24 -06003033 if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
3034 !npt_enabled)
Joerg Roedel4b161842010-09-10 17:31:03 +02003035 return false;
3036
Joerg Roedel52c65a302010-08-02 16:46:44 +02003037 return true;
3038}
3039
Ladi Prosekc2634062017-10-11 16:54:44 +02003040static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
3041 struct vmcb *nested_vmcb, struct page *page)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003042{
Avi Kivityf6e78472010-08-02 15:30:20 +03003043 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003044 svm->vcpu.arch.hflags |= HF_HIF_MASK;
3045 else
3046 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
3047
Tom Lendackycea3a192017-12-04 10:57:24 -06003048 if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
Joerg Roedel4b161842010-09-10 17:31:03 +02003049 kvm_mmu_unload(&svm->vcpu);
3050 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
3051 nested_svm_init_mmu_context(&svm->vcpu);
3052 }
3053
Alexander Graf3d6368e2008-11-25 20:17:07 +01003054 /* Load the nested guest state */
3055 svm->vmcb->save.es = nested_vmcb->save.es;
3056 svm->vmcb->save.cs = nested_vmcb->save.cs;
3057 svm->vmcb->save.ss = nested_vmcb->save.ss;
3058 svm->vmcb->save.ds = nested_vmcb->save.ds;
3059 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
3060 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003061 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003062 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
3063 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
3064 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
3065 if (npt_enabled) {
3066 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
3067 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003068 } else
Avi Kivity23902182010-06-10 17:02:16 +03003069 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003070
3071 /* Guest paging mode is active - reset mmu */
3072 kvm_mmu_reset_context(&svm->vcpu);
3073
Joerg Roedeldefbba52009-08-07 11:49:30 +02003074 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003075 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
3076 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
3077 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01003078
Alexander Graf3d6368e2008-11-25 20:17:07 +01003079 /* In case we don't even reach vcpu_run, the fields are not updated */
3080 svm->vmcb->save.rax = nested_vmcb->save.rax;
3081 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3082 svm->vmcb->save.rip = nested_vmcb->save.rip;
3083 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3084 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3085 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3086
Joerg Roedelf7138532010-03-01 15:34:40 +01003087 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003088 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003089
Joerg Roedelaad42c62009-08-07 11:49:34 +02003090 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003091 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003092 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02003093 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3094 svm->nested.intercept = nested_vmcb->control.intercept;
3095
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003096 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003097 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003098 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3099 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3100 else
3101 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3102
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003103 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3104 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003105 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3106 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003107 }
3108
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003109 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003110 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003111
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003112 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003113 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3114 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
3115 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003116 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3117 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3118
Joerg Roedel7597f122010-02-19 16:23:00 +01003119 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003120
Joerg Roedel20307532010-11-29 17:51:48 +01003121 /* Enter Guest-Mode */
3122 enter_guest_mode(&svm->vcpu);
3123
Joerg Roedel384c6362010-11-30 18:03:56 +01003124 /*
3125 * Merge guest and host intercepts - must be called with vcpu in
3126 * guest-mode to take affect here
3127 */
3128 recalc_intercepts(svm);
3129
Joerg Roedel06fc77722010-02-19 16:23:07 +01003130 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003131
Joerg Roedel2af91942009-08-07 11:49:28 +02003132 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003133
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003134 mark_all_dirty(svm->vmcb);
Ladi Prosekc2634062017-10-11 16:54:44 +02003135}
3136
3137static bool nested_svm_vmrun(struct vcpu_svm *svm)
3138{
3139 struct vmcb *nested_vmcb;
3140 struct vmcb *hsave = svm->nested.hsave;
3141 struct vmcb *vmcb = svm->vmcb;
3142 struct page *page;
3143 u64 vmcb_gpa;
3144
3145 vmcb_gpa = svm->vmcb->save.rax;
3146
3147 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
3148 if (!nested_vmcb)
3149 return false;
3150
3151 if (!nested_vmcb_checks(nested_vmcb)) {
3152 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3153 nested_vmcb->control.exit_code_hi = 0;
3154 nested_vmcb->control.exit_info_1 = 0;
3155 nested_vmcb->control.exit_info_2 = 0;
3156
3157 nested_svm_unmap(page);
3158
3159 return false;
3160 }
3161
3162 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3163 nested_vmcb->save.rip,
3164 nested_vmcb->control.int_ctl,
3165 nested_vmcb->control.event_inj,
3166 nested_vmcb->control.nested_ctl);
3167
3168 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3169 nested_vmcb->control.intercept_cr >> 16,
3170 nested_vmcb->control.intercept_exceptions,
3171 nested_vmcb->control.intercept);
3172
3173 /* Clear internal status */
3174 kvm_clear_exception_queue(&svm->vcpu);
3175 kvm_clear_interrupt_queue(&svm->vcpu);
3176
3177 /*
3178 * Save the old vmcb, so we don't need to pick what we save, but can
3179 * restore everything when a VMEXIT occurs
3180 */
3181 hsave->save.es = vmcb->save.es;
3182 hsave->save.cs = vmcb->save.cs;
3183 hsave->save.ss = vmcb->save.ss;
3184 hsave->save.ds = vmcb->save.ds;
3185 hsave->save.gdtr = vmcb->save.gdtr;
3186 hsave->save.idtr = vmcb->save.idtr;
3187 hsave->save.efer = svm->vcpu.arch.efer;
3188 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3189 hsave->save.cr4 = svm->vcpu.arch.cr4;
3190 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3191 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3192 hsave->save.rsp = vmcb->save.rsp;
3193 hsave->save.rax = vmcb->save.rax;
3194 if (npt_enabled)
3195 hsave->save.cr3 = vmcb->save.cr3;
3196 else
3197 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3198
3199 copy_vmcb_control_area(hsave, vmcb);
3200
3201 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003202
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003203 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003204}
3205
Joerg Roedel9966bf62009-08-07 11:49:40 +02003206static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01003207{
3208 to_vmcb->save.fs = from_vmcb->save.fs;
3209 to_vmcb->save.gs = from_vmcb->save.gs;
3210 to_vmcb->save.tr = from_vmcb->save.tr;
3211 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3212 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3213 to_vmcb->save.star = from_vmcb->save.star;
3214 to_vmcb->save.lstar = from_vmcb->save.lstar;
3215 to_vmcb->save.cstar = from_vmcb->save.cstar;
3216 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3217 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3218 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3219 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01003220}
3221
Avi Kivity851ba692009-08-24 11:10:17 +03003222static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003223{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003224 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003225 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003226 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003227
Alexander Graf55426752008-11-25 20:17:06 +01003228 if (nested_svm_check_permissions(svm))
3229 return 1;
3230
Joerg Roedel7597f122010-02-19 16:23:00 +01003231 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003232 if (!nested_vmcb)
3233 return 1;
3234
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003235 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003236 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003237
Joerg Roedel9966bf62009-08-07 11:49:40 +02003238 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003239 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003240
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003241 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003242}
3243
Avi Kivity851ba692009-08-24 11:10:17 +03003244static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003245{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003246 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003247 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003248 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003249
Alexander Graf55426752008-11-25 20:17:06 +01003250 if (nested_svm_check_permissions(svm))
3251 return 1;
3252
Joerg Roedel7597f122010-02-19 16:23:00 +01003253 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003254 if (!nested_vmcb)
3255 return 1;
3256
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003257 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003258 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003259
Joerg Roedel9966bf62009-08-07 11:49:40 +02003260 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003261 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003262
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003263 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003264}
3265
Avi Kivity851ba692009-08-24 11:10:17 +03003266static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003267{
Alexander Graf3d6368e2008-11-25 20:17:07 +01003268 if (nested_svm_check_permissions(svm))
3269 return 1;
3270
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02003271 /* Save rip after vmrun instruction */
3272 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003273
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003274 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01003275 return 1;
3276
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003277 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02003278 goto failed;
3279
3280 return 1;
3281
3282failed:
3283
3284 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3285 svm->vmcb->control.exit_code_hi = 0;
3286 svm->vmcb->control.exit_info_1 = 0;
3287 svm->vmcb->control.exit_info_2 = 0;
3288
3289 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003290
3291 return 1;
3292}
3293
Avi Kivity851ba692009-08-24 11:10:17 +03003294static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003295{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003296 int ret;
3297
Alexander Graf1371d902008-11-25 20:17:04 +01003298 if (nested_svm_check_permissions(svm))
3299 return 1;
3300
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003301 /*
3302 * If VGIF is enabled, the STGI intercept is only added to
Ladi Prosekcc3d9672017-10-17 16:02:39 +02003303 * detect the opening of the SMI/NMI window; remove it now.
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003304 */
3305 if (vgif_enabled(svm))
3306 clr_intercept(svm, INTERCEPT_STGI);
3307
Alexander Graf1371d902008-11-25 20:17:04 +01003308 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003309 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003310 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003311
Joerg Roedel2af91942009-08-07 11:49:28 +02003312 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003313
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003314 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003315}
3316
Avi Kivity851ba692009-08-24 11:10:17 +03003317static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003318{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003319 int ret;
3320
Alexander Graf1371d902008-11-25 20:17:04 +01003321 if (nested_svm_check_permissions(svm))
3322 return 1;
3323
3324 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003325 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003326
Joerg Roedel2af91942009-08-07 11:49:28 +02003327 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003328
3329 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05003330 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3331 svm_clear_vintr(svm);
3332 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3333 mark_dirty(svm->vmcb, VMCB_INTR);
3334 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003335
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003336 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003337}
3338
Avi Kivity851ba692009-08-24 11:10:17 +03003339static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02003340{
3341 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02003342
David Kaplan668f1982015-02-20 16:02:10 -06003343 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
3344 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02003345
Alexander Grafff092382009-06-15 15:21:24 +02003346 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06003347 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02003348
3349 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003350 return kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02003351}
3352
Joerg Roedel532a46b2009-10-09 16:08:32 +02003353static int skinit_interception(struct vcpu_svm *svm)
3354{
David Kaplan668f1982015-02-20 16:02:10 -06003355 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003356
3357 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3358 return 1;
3359}
3360
David Kaplandab429a2015-03-02 13:43:37 -06003361static int wbinvd_interception(struct vcpu_svm *svm)
3362{
Kyle Huey6affcbe2016-11-29 12:40:40 -08003363 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06003364}
3365
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003366static int xsetbv_interception(struct vcpu_svm *svm)
3367{
3368 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
3369 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3370
3371 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3372 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003373 return kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003374 }
3375
3376 return 1;
3377}
3378
Avi Kivity851ba692009-08-24 11:10:17 +03003379static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380{
Izik Eidus37817f22008-03-24 23:14:53 +02003381 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003382 int reason;
3383 int int_type = svm->vmcb->control.exit_int_info &
3384 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003385 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003386 uint32_t type =
3387 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3388 uint32_t idt_v =
3389 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003390 bool has_error_code = false;
3391 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003392
3393 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003394
Izik Eidus37817f22008-03-24 23:14:53 +02003395 if (svm->vmcb->control.exit_info_2 &
3396 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003397 reason = TASK_SWITCH_IRET;
3398 else if (svm->vmcb->control.exit_info_2 &
3399 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3400 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003401 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003402 reason = TASK_SWITCH_GATE;
3403 else
3404 reason = TASK_SWITCH_CALL;
3405
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003406 if (reason == TASK_SWITCH_GATE) {
3407 switch (type) {
3408 case SVM_EXITINTINFO_TYPE_NMI:
3409 svm->vcpu.arch.nmi_injected = false;
3410 break;
3411 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003412 if (svm->vmcb->control.exit_info_2 &
3413 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3414 has_error_code = true;
3415 error_code =
3416 (u32)svm->vmcb->control.exit_info_2;
3417 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003418 kvm_clear_exception_queue(&svm->vcpu);
3419 break;
3420 case SVM_EXITINTINFO_TYPE_INTR:
3421 kvm_clear_interrupt_queue(&svm->vcpu);
3422 break;
3423 default:
3424 break;
3425 }
3426 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003427
Gleb Natapov8317c292009-04-12 13:37:02 +03003428 if (reason != TASK_SWITCH_GATE ||
3429 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3430 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03003431 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3432 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003433
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003434 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3435 int_vec = -1;
3436
3437 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03003438 has_error_code, error_code) == EMULATE_FAIL) {
3439 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3440 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3441 svm->vcpu.run->internal.ndata = 0;
3442 return 0;
3443 }
3444 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445}
3446
Avi Kivity851ba692009-08-24 11:10:17 +03003447static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003449 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Kyle Huey6a908b62016-11-29 12:40:37 -08003450 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003451}
3452
Avi Kivity851ba692009-08-24 11:10:17 +03003453static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003454{
3455 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003456 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003457 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003458 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003459 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003460 return 1;
3461}
3462
Avi Kivity851ba692009-08-24 11:10:17 +03003463static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003464{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003465 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3466 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3467
3468 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003469 return kvm_skip_emulated_instruction(&svm->vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003470}
3471
Avi Kivity851ba692009-08-24 11:10:17 +03003472static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003473{
Andre Przywara51d8b662010-12-21 11:12:02 +01003474 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003475}
3476
Avi Kivity332b56e2011-11-10 14:57:24 +02003477static int rdpmc_interception(struct vcpu_svm *svm)
3478{
3479 int err;
3480
3481 if (!static_cpu_has(X86_FEATURE_NRIPS))
3482 return emulate_on_interception(svm);
3483
3484 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08003485 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02003486}
3487
Xiubo Li52eb5a62015-03-13 17:39:45 +08003488static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3489 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003490{
3491 unsigned long cr0 = svm->vcpu.arch.cr0;
3492 bool ret = false;
3493 u64 intercept;
3494
3495 intercept = svm->nested.intercept;
3496
3497 if (!is_guest_mode(&svm->vcpu) ||
3498 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3499 return false;
3500
3501 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3502 val &= ~SVM_CR0_SELECTIVE_MASK;
3503
3504 if (cr0 ^ val) {
3505 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3506 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3507 }
3508
3509 return ret;
3510}
3511
Andre Przywara7ff76d52010-12-21 11:12:04 +01003512#define CR_VALID (1ULL << 63)
3513
3514static int cr_interception(struct vcpu_svm *svm)
3515{
3516 int reg, cr;
3517 unsigned long val;
3518 int err;
3519
3520 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3521 return emulate_on_interception(svm);
3522
3523 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3524 return emulate_on_interception(svm);
3525
3526 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003527 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3528 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3529 else
3530 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003531
3532 err = 0;
3533 if (cr >= 16) { /* mov to cr */
3534 cr -= 16;
3535 val = kvm_register_read(&svm->vcpu, reg);
3536 switch (cr) {
3537 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003538 if (!check_selective_cr0_intercepted(svm, val))
3539 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003540 else
3541 return 1;
3542
Andre Przywara7ff76d52010-12-21 11:12:04 +01003543 break;
3544 case 3:
3545 err = kvm_set_cr3(&svm->vcpu, val);
3546 break;
3547 case 4:
3548 err = kvm_set_cr4(&svm->vcpu, val);
3549 break;
3550 case 8:
3551 err = kvm_set_cr8(&svm->vcpu, val);
3552 break;
3553 default:
3554 WARN(1, "unhandled write to CR%d", cr);
3555 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3556 return 1;
3557 }
3558 } else { /* mov from cr */
3559 switch (cr) {
3560 case 0:
3561 val = kvm_read_cr0(&svm->vcpu);
3562 break;
3563 case 2:
3564 val = svm->vcpu.arch.cr2;
3565 break;
3566 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003567 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003568 break;
3569 case 4:
3570 val = kvm_read_cr4(&svm->vcpu);
3571 break;
3572 case 8:
3573 val = kvm_get_cr8(&svm->vcpu);
3574 break;
3575 default:
3576 WARN(1, "unhandled read from CR%d", cr);
3577 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3578 return 1;
3579 }
3580 kvm_register_write(&svm->vcpu, reg, val);
3581 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08003582 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003583}
3584
Andre Przywaracae37972010-12-21 11:12:05 +01003585static int dr_interception(struct vcpu_svm *svm)
3586{
3587 int reg, dr;
3588 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003589
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003590 if (svm->vcpu.guest_debug == 0) {
3591 /*
3592 * No more DR vmexits; force a reload of the debug registers
3593 * and reenter on this instruction. The next vmexit will
3594 * retrieve the full state of the debug registers.
3595 */
3596 clr_dr_intercepts(svm);
3597 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3598 return 1;
3599 }
3600
Andre Przywaracae37972010-12-21 11:12:05 +01003601 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3602 return emulate_on_interception(svm);
3603
3604 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3605 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3606
3607 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003608 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3609 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003610 val = kvm_register_read(&svm->vcpu, reg);
3611 kvm_set_dr(&svm->vcpu, dr - 16, val);
3612 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003613 if (!kvm_require_dr(&svm->vcpu, dr))
3614 return 1;
3615 kvm_get_dr(&svm->vcpu, dr, &val);
3616 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003617 }
3618
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003619 return kvm_skip_emulated_instruction(&svm->vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01003620}
3621
Avi Kivity851ba692009-08-24 11:10:17 +03003622static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003623{
Avi Kivity851ba692009-08-24 11:10:17 +03003624 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003625 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003626
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003627 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3628 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003629 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003630 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003631 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003632 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003633 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003634 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3635 return 0;
3636}
3637
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003638static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003639{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003640 struct vcpu_svm *svm = to_svm(vcpu);
3641
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003642 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303643 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003644 msr_info->data = svm->vmcb->control.tsc_offset +
Haozhong Zhang35181e82015-10-20 15:39:03 +08003645 kvm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003646
Avi Kivity6aa8b732006-12-10 02:21:36 -08003647 break;
3648 }
Brian Gerst8c065852010-07-17 09:03:26 -04003649 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003650 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003651 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003652#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003653 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003654 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003655 break;
3656 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003657 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003658 break;
3659 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003660 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003661 break;
3662 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003663 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003664 break;
3665#endif
3666 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003667 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003668 break;
3669 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003670 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003671 break;
3672 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003673 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003674 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003675 case MSR_TSC_AUX:
3676 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3677 return 1;
3678 msr_info->data = svm->tsc_aux;
3679 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003680 /*
3681 * Nobody will change the following 5 values in the VMCB so we can
3682 * safely return them on rdmsr. They will always be 0 until LBRV is
3683 * implemented.
3684 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003685 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003686 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01003687 break;
3688 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003689 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003690 break;
3691 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003692 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003693 break;
3694 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003695 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003696 break;
3697 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003698 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003699 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003700 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003701 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003702 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003703 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003704 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003705 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003706 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003707 msr_info->data = 0x01000065;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003708 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01003709 case MSR_F15H_IC_CFG: {
3710
3711 int family, model;
3712
3713 family = guest_cpuid_family(vcpu);
3714 model = guest_cpuid_model(vcpu);
3715
3716 if (family < 0 || model < 0)
3717 return kvm_get_msr_common(vcpu, msr_info);
3718
3719 msr_info->data = 0;
3720
3721 if (family == 0x15 &&
3722 (model >= 0x2 && model < 0x20))
3723 msr_info->data = 0x1E;
3724 }
3725 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003726 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003727 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003728 }
3729 return 0;
3730}
3731
Avi Kivity851ba692009-08-24 11:10:17 +03003732static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003733{
David Kaplan668f1982015-02-20 16:02:10 -06003734 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003735 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003737 msr_info.index = ecx;
3738 msr_info.host_initiated = false;
3739 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02003740 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003741 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003742 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02003743 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003744 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003745
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003746 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
3747 msr_info.data & 0xffffffff);
3748 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
3749 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003750 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003751 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003752 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003753}
3754
Joerg Roedel4a810182010-02-24 18:59:15 +01003755static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3756{
3757 struct vcpu_svm *svm = to_svm(vcpu);
3758 int svm_dis, chg_mask;
3759
3760 if (data & ~SVM_VM_CR_VALID_MASK)
3761 return 1;
3762
3763 chg_mask = SVM_VM_CR_VALID_MASK;
3764
3765 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3766 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3767
3768 svm->nested.vm_cr_msr &= ~chg_mask;
3769 svm->nested.vm_cr_msr |= (data & chg_mask);
3770
3771 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3772
3773 /* check for svm_disable while efer.svme is set */
3774 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3775 return 1;
3776
3777 return 0;
3778}
3779
Will Auld8fe8ab42012-11-29 12:42:12 -08003780static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003781{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003782 struct vcpu_svm *svm = to_svm(vcpu);
3783
Will Auld8fe8ab42012-11-29 12:42:12 -08003784 u32 ecx = msr->index;
3785 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02003787 case MSR_IA32_CR_PAT:
3788 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3789 return 1;
3790 vcpu->arch.pat = data;
3791 svm->vmcb->save.g_pat = data;
3792 mark_dirty(svm->vmcb, VMCB_NPT);
3793 break;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003794 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003795 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003797 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003798 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003800#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003801 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003802 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003803 break;
3804 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003805 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003806 break;
3807 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003808 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003809 break;
3810 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003811 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003812 break;
3813#endif
3814 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003815 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003816 break;
3817 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003818 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003819 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003820 break;
3821 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003822 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003823 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003824 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003825 case MSR_TSC_AUX:
3826 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3827 return 1;
3828
3829 /*
3830 * This is rare, so we update the MSR here instead of using
3831 * direct_access_msrs. Doing that would require a rdmsr in
3832 * svm_vcpu_put.
3833 */
3834 svm->tsc_aux = data;
3835 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
3836 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003837 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003838 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003839 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
3840 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003841 break;
3842 }
3843 if (data & DEBUGCTL_RESERVED_BITS)
3844 return 1;
3845
3846 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003847 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003848 if (data & (1ULL<<0))
3849 svm_enable_lbrv(svm);
3850 else
3851 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003852 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003853 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003854 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003855 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003856 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003857 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003858 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03003859 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003860 break;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003861 case MSR_IA32_APICBASE:
3862 if (kvm_vcpu_apicv_active(vcpu))
3863 avic_update_vapic_bar(to_svm(vcpu), data);
3864 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003865 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08003866 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003867 }
3868 return 0;
3869}
3870
Avi Kivity851ba692009-08-24 11:10:17 +03003871static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872{
Will Auld8fe8ab42012-11-29 12:42:12 -08003873 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06003874 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3875 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003876
Will Auld8fe8ab42012-11-29 12:42:12 -08003877 msr.data = data;
3878 msr.index = ecx;
3879 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003880
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003881 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03003882 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02003883 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003884 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003885 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02003886 } else {
3887 trace_kvm_msr_write(ecx, data);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003888 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003889 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003890}
3891
Avi Kivity851ba692009-08-24 11:10:17 +03003892static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893{
Rusty Russelle756fc62007-07-30 20:07:08 +10003894 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003895 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003896 else
Avi Kivity851ba692009-08-24 11:10:17 +03003897 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003898}
3899
Avi Kivity851ba692009-08-24 11:10:17 +03003900static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003901{
Avi Kivity3842d132010-07-27 12:30:24 +03003902 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003903 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003904 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003905 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08003906 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003907 return 1;
3908}
3909
Mark Langsdorf565d0992009-10-06 14:25:02 -05003910static int pause_interception(struct vcpu_svm *svm)
3911{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003912 struct kvm_vcpu *vcpu = &svm->vcpu;
3913 bool in_kernel = (svm_get_cpl(vcpu) == 0);
3914
3915 kvm_vcpu_on_spin(vcpu, in_kernel);
Mark Langsdorf565d0992009-10-06 14:25:02 -05003916 return 1;
3917}
3918
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003919static int nop_interception(struct vcpu_svm *svm)
3920{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003921 return kvm_skip_emulated_instruction(&(svm->vcpu));
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003922}
3923
3924static int monitor_interception(struct vcpu_svm *svm)
3925{
3926 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
3927 return nop_interception(svm);
3928}
3929
3930static int mwait_interception(struct vcpu_svm *svm)
3931{
3932 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
3933 return nop_interception(svm);
3934}
3935
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003936enum avic_ipi_failure_cause {
3937 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
3938 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
3939 AVIC_IPI_FAILURE_INVALID_TARGET,
3940 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
3941};
3942
3943static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
3944{
3945 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
3946 u32 icrl = svm->vmcb->control.exit_info_1;
3947 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03003948 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003949 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3950
3951 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
3952
3953 switch (id) {
3954 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
3955 /*
3956 * AVIC hardware handles the generation of
3957 * IPIs when the specified Message Type is Fixed
3958 * (also known as fixed delivery mode) and
3959 * the Trigger Mode is edge-triggered. The hardware
3960 * also supports self and broadcast delivery modes
3961 * specified via the Destination Shorthand(DSH)
3962 * field of the ICRL. Logical and physical APIC ID
3963 * formats are supported. All other IPI types cause
3964 * a #VMEXIT, which needs to emulated.
3965 */
3966 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
3967 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
3968 break;
3969 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
3970 int i;
3971 struct kvm_vcpu *vcpu;
3972 struct kvm *kvm = svm->vcpu.kvm;
3973 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3974
3975 /*
3976 * At this point, we expect that the AVIC HW has already
3977 * set the appropriate IRR bits on the valid target
3978 * vcpus. So, we just need to kick the appropriate vcpu.
3979 */
3980 kvm_for_each_vcpu(i, vcpu, kvm) {
3981 bool m = kvm_apic_match_dest(vcpu, apic,
3982 icrl & KVM_APIC_SHORT_MASK,
3983 GET_APIC_DEST_FIELD(icrh),
3984 icrl & KVM_APIC_DEST_MASK);
3985
3986 if (m && !avic_vcpu_is_running(vcpu))
3987 kvm_vcpu_wake_up(vcpu);
3988 }
3989 break;
3990 }
3991 case AVIC_IPI_FAILURE_INVALID_TARGET:
3992 break;
3993 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
3994 WARN_ONCE(1, "Invalid backing page\n");
3995 break;
3996 default:
3997 pr_err("Unknown IPI interception\n");
3998 }
3999
4000 return 1;
4001}
4002
4003static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
4004{
4005 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4006 int index;
4007 u32 *logical_apic_id_table;
4008 int dlid = GET_APIC_LOGICAL_ID(ldr);
4009
4010 if (!dlid)
4011 return NULL;
4012
4013 if (flat) { /* flat */
4014 index = ffs(dlid) - 1;
4015 if (index > 7)
4016 return NULL;
4017 } else { /* cluster */
4018 int cluster = (dlid & 0xf0) >> 4;
4019 int apic = ffs(dlid & 0x0f) - 1;
4020
4021 if ((apic < 0) || (apic > 7) ||
4022 (cluster >= 0xf))
4023 return NULL;
4024 index = (cluster << 2) + apic;
4025 }
4026
4027 logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page);
4028
4029 return &logical_apic_id_table[index];
4030}
4031
4032static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
4033 bool valid)
4034{
4035 bool flat;
4036 u32 *entry, new_entry;
4037
4038 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4039 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4040 if (!entry)
4041 return -EINVAL;
4042
4043 new_entry = READ_ONCE(*entry);
4044 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4045 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
4046 if (valid)
4047 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4048 else
4049 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4050 WRITE_ONCE(*entry, new_entry);
4051
4052 return 0;
4053}
4054
4055static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4056{
4057 int ret;
4058 struct vcpu_svm *svm = to_svm(vcpu);
4059 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
4060
4061 if (!ldr)
4062 return 1;
4063
4064 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
4065 if (ret && svm->ldr_reg) {
4066 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
4067 svm->ldr_reg = 0;
4068 } else {
4069 svm->ldr_reg = ldr;
4070 }
4071 return ret;
4072}
4073
4074static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4075{
4076 u64 *old, *new;
4077 struct vcpu_svm *svm = to_svm(vcpu);
4078 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
4079 u32 id = (apic_id_reg >> 24) & 0xff;
4080
4081 if (vcpu->vcpu_id == id)
4082 return 0;
4083
4084 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4085 new = avic_get_physical_id_entry(vcpu, id);
4086 if (!new || !old)
4087 return 1;
4088
4089 /* We need to move physical_id_entry to new offset */
4090 *new = *old;
4091 *old = 0ULL;
4092 to_svm(vcpu)->avic_physical_id_cache = new;
4093
4094 /*
4095 * Also update the guest physical APIC ID in the logical
4096 * APIC ID table entry if already setup the LDR.
4097 */
4098 if (svm->ldr_reg)
4099 avic_handle_ldr_update(vcpu);
4100
4101 return 0;
4102}
4103
4104static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
4105{
4106 struct vcpu_svm *svm = to_svm(vcpu);
4107 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4108 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
4109 u32 mod = (dfr >> 28) & 0xf;
4110
4111 /*
4112 * We assume that all local APICs are using the same type.
4113 * If this changes, we need to flush the AVIC logical
4114 * APID id table.
4115 */
4116 if (vm_data->ldr_mode == mod)
4117 return 0;
4118
4119 clear_page(page_address(vm_data->avic_logical_id_table_page));
4120 vm_data->ldr_mode = mod;
4121
4122 if (svm->ldr_reg)
4123 avic_handle_ldr_update(vcpu);
4124 return 0;
4125}
4126
4127static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4128{
4129 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4130 u32 offset = svm->vmcb->control.exit_info_1 &
4131 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4132
4133 switch (offset) {
4134 case APIC_ID:
4135 if (avic_handle_apic_id_update(&svm->vcpu))
4136 return 0;
4137 break;
4138 case APIC_LDR:
4139 if (avic_handle_ldr_update(&svm->vcpu))
4140 return 0;
4141 break;
4142 case APIC_DFR:
4143 avic_handle_dfr_update(&svm->vcpu);
4144 break;
4145 default:
4146 break;
4147 }
4148
4149 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4150
4151 return 1;
4152}
4153
4154static bool is_avic_unaccelerated_access_trap(u32 offset)
4155{
4156 bool ret = false;
4157
4158 switch (offset) {
4159 case APIC_ID:
4160 case APIC_EOI:
4161 case APIC_RRR:
4162 case APIC_LDR:
4163 case APIC_DFR:
4164 case APIC_SPIV:
4165 case APIC_ESR:
4166 case APIC_ICR:
4167 case APIC_LVTT:
4168 case APIC_LVTTHMR:
4169 case APIC_LVTPC:
4170 case APIC_LVT0:
4171 case APIC_LVT1:
4172 case APIC_LVTERR:
4173 case APIC_TMICT:
4174 case APIC_TDCR:
4175 ret = true;
4176 break;
4177 default:
4178 break;
4179 }
4180 return ret;
4181}
4182
4183static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4184{
4185 int ret = 0;
4186 u32 offset = svm->vmcb->control.exit_info_1 &
4187 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4188 u32 vector = svm->vmcb->control.exit_info_2 &
4189 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4190 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4191 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4192 bool trap = is_avic_unaccelerated_access_trap(offset);
4193
4194 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4195 trap, write, vector);
4196 if (trap) {
4197 /* Handling Trap */
4198 WARN_ONCE(!write, "svm: Handling trap read.\n");
4199 ret = avic_unaccel_trap_write(svm);
4200 } else {
4201 /* Handling Fault */
4202 ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
4203 }
4204
4205 return ret;
4206}
4207
Mathias Krause09941fb2012-08-30 01:30:20 +02004208static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01004209 [SVM_EXIT_READ_CR0] = cr_interception,
4210 [SVM_EXIT_READ_CR3] = cr_interception,
4211 [SVM_EXIT_READ_CR4] = cr_interception,
4212 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06004213 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02004214 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01004215 [SVM_EXIT_WRITE_CR3] = cr_interception,
4216 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004217 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01004218 [SVM_EXIT_READ_DR0] = dr_interception,
4219 [SVM_EXIT_READ_DR1] = dr_interception,
4220 [SVM_EXIT_READ_DR2] = dr_interception,
4221 [SVM_EXIT_READ_DR3] = dr_interception,
4222 [SVM_EXIT_READ_DR4] = dr_interception,
4223 [SVM_EXIT_READ_DR5] = dr_interception,
4224 [SVM_EXIT_READ_DR6] = dr_interception,
4225 [SVM_EXIT_READ_DR7] = dr_interception,
4226 [SVM_EXIT_WRITE_DR0] = dr_interception,
4227 [SVM_EXIT_WRITE_DR1] = dr_interception,
4228 [SVM_EXIT_WRITE_DR2] = dr_interception,
4229 [SVM_EXIT_WRITE_DR3] = dr_interception,
4230 [SVM_EXIT_WRITE_DR4] = dr_interception,
4231 [SVM_EXIT_WRITE_DR5] = dr_interception,
4232 [SVM_EXIT_WRITE_DR6] = dr_interception,
4233 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004234 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4235 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004236 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004237 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004238 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01004239 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004240 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02004241 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004242 [SVM_EXIT_SMI] = nop_on_interception,
4243 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08004244 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02004245 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004246 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004247 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02004248 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05004249 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004250 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004251 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02004252 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004253 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004254 [SVM_EXIT_MSR] = msr_interception,
4255 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08004256 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01004257 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02004258 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01004259 [SVM_EXIT_VMLOAD] = vmload_interception,
4260 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01004261 [SVM_EXIT_STGI] = stgi_interception,
4262 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02004263 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06004264 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004265 [SVM_EXIT_MONITOR] = monitor_interception,
4266 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01004267 [SVM_EXIT_XSETBV] = xsetbv_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02004268 [SVM_EXIT_NPF] = npf_interception,
Paolo Bonzini64d60672015-05-07 11:36:11 +02004269 [SVM_EXIT_RSM] = emulate_on_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004270 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4271 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272};
4273
Joe Perchesae8cc052011-04-24 22:00:50 -07004274static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02004275{
4276 struct vcpu_svm *svm = to_svm(vcpu);
4277 struct vmcb_control_area *control = &svm->vmcb->control;
4278 struct vmcb_save_area *save = &svm->vmcb->save;
4279
4280 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004281 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4282 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4283 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4284 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4285 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4286 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4287 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
4288 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4289 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4290 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4291 pr_err("%-20s%d\n", "asid:", control->asid);
4292 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4293 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4294 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4295 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4296 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4297 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4298 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4299 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4300 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4301 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4302 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004303 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07004304 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4305 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05004306 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07004307 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004308 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4309 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4310 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004311 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004312 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4313 "es:",
4314 save->es.selector, save->es.attrib,
4315 save->es.limit, save->es.base);
4316 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4317 "cs:",
4318 save->cs.selector, save->cs.attrib,
4319 save->cs.limit, save->cs.base);
4320 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4321 "ss:",
4322 save->ss.selector, save->ss.attrib,
4323 save->ss.limit, save->ss.base);
4324 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4325 "ds:",
4326 save->ds.selector, save->ds.attrib,
4327 save->ds.limit, save->ds.base);
4328 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4329 "fs:",
4330 save->fs.selector, save->fs.attrib,
4331 save->fs.limit, save->fs.base);
4332 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4333 "gs:",
4334 save->gs.selector, save->gs.attrib,
4335 save->gs.limit, save->gs.base);
4336 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4337 "gdtr:",
4338 save->gdtr.selector, save->gdtr.attrib,
4339 save->gdtr.limit, save->gdtr.base);
4340 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4341 "ldtr:",
4342 save->ldtr.selector, save->ldtr.attrib,
4343 save->ldtr.limit, save->ldtr.base);
4344 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4345 "idtr:",
4346 save->idtr.selector, save->idtr.attrib,
4347 save->idtr.limit, save->idtr.base);
4348 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4349 "tr:",
4350 save->tr.selector, save->tr.attrib,
4351 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004352 pr_err("cpl: %d efer: %016llx\n",
4353 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004354 pr_err("%-15s %016llx %-13s %016llx\n",
4355 "cr0:", save->cr0, "cr2:", save->cr2);
4356 pr_err("%-15s %016llx %-13s %016llx\n",
4357 "cr3:", save->cr3, "cr4:", save->cr4);
4358 pr_err("%-15s %016llx %-13s %016llx\n",
4359 "dr6:", save->dr6, "dr7:", save->dr7);
4360 pr_err("%-15s %016llx %-13s %016llx\n",
4361 "rip:", save->rip, "rflags:", save->rflags);
4362 pr_err("%-15s %016llx %-13s %016llx\n",
4363 "rsp:", save->rsp, "rax:", save->rax);
4364 pr_err("%-15s %016llx %-13s %016llx\n",
4365 "star:", save->star, "lstar:", save->lstar);
4366 pr_err("%-15s %016llx %-13s %016llx\n",
4367 "cstar:", save->cstar, "sfmask:", save->sfmask);
4368 pr_err("%-15s %016llx %-13s %016llx\n",
4369 "kernel_gs_base:", save->kernel_gs_base,
4370 "sysenter_cs:", save->sysenter_cs);
4371 pr_err("%-15s %016llx %-13s %016llx\n",
4372 "sysenter_esp:", save->sysenter_esp,
4373 "sysenter_eip:", save->sysenter_eip);
4374 pr_err("%-15s %016llx %-13s %016llx\n",
4375 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4376 pr_err("%-15s %016llx %-13s %016llx\n",
4377 "br_from:", save->br_from, "br_to:", save->br_to);
4378 pr_err("%-15s %016llx %-13s %016llx\n",
4379 "excp_from:", save->last_excp_from,
4380 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004381}
4382
Avi Kivity586f9602010-11-18 13:09:54 +02004383static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4384{
4385 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4386
4387 *info1 = control->exit_info_1;
4388 *info2 = control->exit_info_2;
4389}
4390
Avi Kivity851ba692009-08-24 11:10:17 +03004391static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004393 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004394 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004395 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004396
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004397 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4398
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004399 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004400 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4401 if (npt_enabled)
4402 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004403
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004404 if (unlikely(svm->nested.exit_required)) {
4405 nested_svm_vmexit(svm);
4406 svm->nested.exit_required = false;
4407
4408 return 1;
4409 }
4410
Joerg Roedel20307532010-11-29 17:51:48 +01004411 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004412 int vmexit;
4413
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004414 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4415 svm->vmcb->control.exit_info_1,
4416 svm->vmcb->control.exit_info_2,
4417 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004418 svm->vmcb->control.exit_int_info_err,
4419 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004420
Joerg Roedel410e4d52009-08-07 11:49:44 +02004421 vmexit = nested_svm_exit_special(svm);
4422
4423 if (vmexit == NESTED_EXIT_CONTINUE)
4424 vmexit = nested_svm_exit_handled(svm);
4425
4426 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004427 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004428 }
4429
Joerg Roedela5c38322009-08-07 11:49:32 +02004430 svm_complete_interrupts(svm);
4431
Avi Kivity04d2cc72007-09-10 18:10:54 +03004432 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4433 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4434 kvm_run->fail_entry.hardware_entry_failure_reason
4435 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004436 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4437 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004438 return 0;
4439 }
4440
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004441 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004442 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004443 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4444 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004445 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004446 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004447 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004448 exit_code);
4449
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004450 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004451 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04004452 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03004453 kvm_queue_exception(vcpu, UD_VECTOR);
4454 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004455 }
4456
Avi Kivity851ba692009-08-24 11:10:17 +03004457 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004458}
4459
4460static void reload_tss(struct kvm_vcpu *vcpu)
4461{
4462 int cpu = raw_smp_processor_id();
4463
Tejun Heo0fe1e002009-10-29 22:34:14 +09004464 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4465 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004466 load_TR_desc();
4467}
4468
Rusty Russelle756fc62007-07-30 20:07:08 +10004469static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004470{
4471 int cpu = raw_smp_processor_id();
4472
Tejun Heo0fe1e002009-10-29 22:34:14 +09004473 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004474
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03004475 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09004476 if (svm->asid_generation != sd->asid_generation)
4477 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004478}
4479
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004480static void svm_inject_nmi(struct kvm_vcpu *vcpu)
4481{
4482 struct vcpu_svm *svm = to_svm(vcpu);
4483
4484 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
4485 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004486 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004487 ++vcpu->stat.nmi_injections;
4488}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004489
Eddie Dong85f455f2007-07-06 12:20:49 +03004490static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004491{
4492 struct vmcb_control_area *control;
4493
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004494 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10004495 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03004496 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004497 control->int_ctl &= ~V_INTR_PRIO_MASK;
4498 control->int_ctl |= V_IRQ_MASK |
4499 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004500 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004501}
4502
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004503static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03004504{
4505 struct vcpu_svm *svm = to_svm(vcpu);
4506
Joerg Roedel2af91942009-08-07 11:49:28 +02004507 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01004508
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03004509 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
4510 ++vcpu->stat.irq_injections;
4511
Alexander Graf219b65d2009-06-15 15:21:25 +02004512 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
4513 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03004514}
4515
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004516static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
4517{
4518 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
4519}
4520
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004521static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
4522{
4523 struct vcpu_svm *svm = to_svm(vcpu);
4524
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004525 if (svm_nested_virtualize_tpr(vcpu) ||
4526 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004527 return;
4528
Radim Krčmář596f3142014-03-11 19:11:18 +01004529 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
4530
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004531 if (irr == -1)
4532 return;
4533
4534 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004535 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004536}
4537
Yang Zhang8d146952013-01-25 10:18:50 +08004538static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
4539{
4540 return;
4541}
4542
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05004543static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004544{
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05004545 return avic && irqchip_split(vcpu->kvm);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004546}
4547
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004548static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
4549{
4550}
4551
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02004552static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004553{
4554}
4555
4556/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03004557static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4558{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004559 struct vcpu_svm *svm = to_svm(vcpu);
4560 struct vmcb *vmcb = svm->vmcb;
4561
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05004562 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004563 return;
4564
4565 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
4566 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004567}
4568
Andrey Smetanin63086302015-11-10 15:36:32 +03004569static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004570{
4571 return;
4572}
4573
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004574static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
4575{
4576 kvm_lapic_set_irr(vec, vcpu->arch.apic);
4577 smp_mb__after_atomic();
4578
4579 if (avic_vcpu_is_running(vcpu))
4580 wrmsrl(SVM_AVIC_DOORBELL,
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05004581 kvm_cpu_get_apicid(vcpu->cpu));
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004582 else
4583 kvm_vcpu_wake_up(vcpu);
4584}
4585
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004586static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4587{
4588 unsigned long flags;
4589 struct amd_svm_iommu_ir *cur;
4590
4591 spin_lock_irqsave(&svm->ir_list_lock, flags);
4592 list_for_each_entry(cur, &svm->ir_list, node) {
4593 if (cur->data != pi->ir_data)
4594 continue;
4595 list_del(&cur->node);
4596 kfree(cur);
4597 break;
4598 }
4599 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4600}
4601
4602static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4603{
4604 int ret = 0;
4605 unsigned long flags;
4606 struct amd_svm_iommu_ir *ir;
4607
4608 /**
4609 * In some cases, the existing irte is updaed and re-set,
4610 * so we need to check here if it's already been * added
4611 * to the ir_list.
4612 */
4613 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
4614 struct kvm *kvm = svm->vcpu.kvm;
4615 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
4616 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
4617 struct vcpu_svm *prev_svm;
4618
4619 if (!prev_vcpu) {
4620 ret = -EINVAL;
4621 goto out;
4622 }
4623
4624 prev_svm = to_svm(prev_vcpu);
4625 svm_ir_list_del(prev_svm, pi);
4626 }
4627
4628 /**
4629 * Allocating new amd_iommu_pi_data, which will get
4630 * add to the per-vcpu ir_list.
4631 */
4632 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
4633 if (!ir) {
4634 ret = -ENOMEM;
4635 goto out;
4636 }
4637 ir->data = pi->ir_data;
4638
4639 spin_lock_irqsave(&svm->ir_list_lock, flags);
4640 list_add(&ir->node, &svm->ir_list);
4641 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4642out:
4643 return ret;
4644}
4645
4646/**
4647 * Note:
4648 * The HW cannot support posting multicast/broadcast
4649 * interrupts to a vCPU. So, we still use legacy interrupt
4650 * remapping for these kind of interrupts.
4651 *
4652 * For lowest-priority interrupts, we only support
4653 * those with single CPU as the destination, e.g. user
4654 * configures the interrupts via /proc/irq or uses
4655 * irqbalance to make the interrupts single-CPU.
4656 */
4657static int
4658get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
4659 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
4660{
4661 struct kvm_lapic_irq irq;
4662 struct kvm_vcpu *vcpu = NULL;
4663
4664 kvm_set_msi_irq(kvm, e, &irq);
4665
4666 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
4667 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
4668 __func__, irq.vector);
4669 return -1;
4670 }
4671
4672 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
4673 irq.vector);
4674 *svm = to_svm(vcpu);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05004675 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004676 vcpu_info->vector = irq.vector;
4677
4678 return 0;
4679}
4680
4681/*
4682 * svm_update_pi_irte - set IRTE for Posted-Interrupts
4683 *
4684 * @kvm: kvm
4685 * @host_irq: host irq of the interrupt
4686 * @guest_irq: gsi of the interrupt
4687 * @set: set or unset PI
4688 * returns 0 on success, < 0 on failure
4689 */
4690static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
4691 uint32_t guest_irq, bool set)
4692{
4693 struct kvm_kernel_irq_routing_entry *e;
4694 struct kvm_irq_routing_table *irq_rt;
4695 int idx, ret = -EINVAL;
4696
4697 if (!kvm_arch_has_assigned_device(kvm) ||
4698 !irq_remapping_cap(IRQ_POSTING_CAP))
4699 return 0;
4700
4701 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
4702 __func__, host_irq, guest_irq, set);
4703
4704 idx = srcu_read_lock(&kvm->irq_srcu);
4705 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
4706 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
4707
4708 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
4709 struct vcpu_data vcpu_info;
4710 struct vcpu_svm *svm = NULL;
4711
4712 if (e->type != KVM_IRQ_ROUTING_MSI)
4713 continue;
4714
4715 /**
4716 * Here, we setup with legacy mode in the following cases:
4717 * 1. When cannot target interrupt to a specific vcpu.
4718 * 2. Unsetting posted interrupt.
4719 * 3. APIC virtialization is disabled for the vcpu.
4720 */
4721 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
4722 kvm_vcpu_apicv_active(&svm->vcpu)) {
4723 struct amd_iommu_pi_data pi;
4724
4725 /* Try to enable guest_mode in IRTE */
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05004726 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
4727 AVIC_HPA_MASK);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004728 pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id,
4729 svm->vcpu.vcpu_id);
4730 pi.is_guest_mode = true;
4731 pi.vcpu_data = &vcpu_info;
4732 ret = irq_set_vcpu_affinity(host_irq, &pi);
4733
4734 /**
4735 * Here, we successfully setting up vcpu affinity in
4736 * IOMMU guest mode. Now, we need to store the posted
4737 * interrupt information in a per-vcpu ir_list so that
4738 * we can reference to them directly when we update vcpu
4739 * scheduling information in IOMMU irte.
4740 */
4741 if (!ret && pi.is_guest_mode)
4742 svm_ir_list_add(svm, &pi);
4743 } else {
4744 /* Use legacy mode in IRTE */
4745 struct amd_iommu_pi_data pi;
4746
4747 /**
4748 * Here, pi is used to:
4749 * - Tell IOMMU to use legacy mode for this interrupt.
4750 * - Retrieve ga_tag of prior interrupt remapping data.
4751 */
4752 pi.is_guest_mode = false;
4753 ret = irq_set_vcpu_affinity(host_irq, &pi);
4754
4755 /**
4756 * Check if the posted interrupt was previously
4757 * setup with the guest_mode by checking if the ga_tag
4758 * was cached. If so, we need to clean up the per-vcpu
4759 * ir_list.
4760 */
4761 if (!ret && pi.prev_ga_tag) {
4762 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
4763 struct kvm_vcpu *vcpu;
4764
4765 vcpu = kvm_get_vcpu_by_id(kvm, id);
4766 if (vcpu)
4767 svm_ir_list_del(to_svm(vcpu), &pi);
4768 }
4769 }
4770
4771 if (!ret && svm) {
4772 trace_kvm_pi_irte_update(svm->vcpu.vcpu_id,
4773 host_irq, e->gsi,
4774 vcpu_info.vector,
4775 vcpu_info.pi_desc_addr, set);
4776 }
4777
4778 if (ret < 0) {
4779 pr_err("%s: failed to update PI IRTE\n", __func__);
4780 goto out;
4781 }
4782 }
4783
4784 ret = 0;
4785out:
4786 srcu_read_unlock(&kvm->irq_srcu, idx);
4787 return ret;
4788}
4789
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004790static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02004791{
4792 struct vcpu_svm *svm = to_svm(vcpu);
4793 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02004794 int ret;
4795 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
4796 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
4797 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
4798
4799 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02004800}
4801
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004802static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
4803{
4804 struct vcpu_svm *svm = to_svm(vcpu);
4805
4806 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
4807}
4808
4809static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4810{
4811 struct vcpu_svm *svm = to_svm(vcpu);
4812
4813 if (masked) {
4814 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004815 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004816 } else {
4817 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004818 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004819 }
4820}
4821
Gleb Natapov78646122009-03-23 12:12:11 +02004822static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
4823{
4824 struct vcpu_svm *svm = to_svm(vcpu);
4825 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02004826 int ret;
4827
4828 if (!gif_set(svm) ||
4829 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
4830 return 0;
4831
Avi Kivityf6e78472010-08-02 15:30:20 +03004832 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02004833
Joerg Roedel20307532010-11-29 17:51:48 +01004834 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02004835 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
4836
4837 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02004838}
4839
Jan Kiszkac9a79532014-03-07 20:03:15 +01004840static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03004841{
Alexander Graf219b65d2009-06-15 15:21:25 +02004842 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02004843
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004844 if (kvm_vcpu_apicv_active(vcpu))
4845 return;
4846
Joerg Roedele0231712010-02-24 18:59:10 +01004847 /*
4848 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
4849 * 1, because that's a separate STGI/VMRUN intercept. The next time we
4850 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05004851 * we'll get the vintr intercept. However, if the vGIF feature is
4852 * enabled, the STGI interception will not occur. Enable the irq
4853 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01004854 */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05004855 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02004856 svm_set_vintr(svm);
4857 svm_inject_irq(svm, 0x0);
4858 }
Gleb Natapov9222be12009-04-23 17:14:37 +03004859}
4860
Jan Kiszkac9a79532014-03-07 20:03:15 +01004861static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004862{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004863 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004864
Gleb Natapov44c11432009-05-11 13:35:52 +03004865 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
4866 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01004867 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03004868
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05004869 if (!gif_set(svm)) {
4870 if (vgif_enabled(svm))
4871 set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02004872 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05004873 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02004874
4875 if (svm->nested.exit_required)
4876 return; /* we're not going to run the guest yet */
4877
Joerg Roedele0231712010-02-24 18:59:10 +01004878 /*
4879 * Something prevents NMI from been injected. Single step over possible
4880 * problem (IRET or exception injection or interrupt shadow)
4881 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02004882 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02004883 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03004884 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03004885}
4886
Izik Eiduscbc94022007-10-25 00:29:55 +02004887static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
4888{
4889 return 0;
4890}
4891
Avi Kivityd9e368d2007-06-07 19:18:30 +03004892static void svm_flush_tlb(struct kvm_vcpu *vcpu)
4893{
Joerg Roedel38e5e922010-12-03 15:25:16 +01004894 struct vcpu_svm *svm = to_svm(vcpu);
4895
4896 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
4897 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4898 else
4899 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03004900}
4901
Avi Kivity04d2cc72007-09-10 18:10:54 +03004902static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
4903{
4904}
4905
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004906static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
4907{
4908 struct vcpu_svm *svm = to_svm(vcpu);
4909
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004910 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004911 return;
4912
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004913 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004914 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03004915 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004916 }
4917}
4918
Joerg Roedel649d6862008-04-16 16:51:15 +02004919static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
4920{
4921 struct vcpu_svm *svm = to_svm(vcpu);
4922 u64 cr8;
4923
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004924 if (svm_nested_virtualize_tpr(vcpu) ||
4925 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004926 return;
4927
Joerg Roedel649d6862008-04-16 16:51:15 +02004928 cr8 = kvm_get_cr8(vcpu);
4929 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
4930 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
4931}
4932
Gleb Natapov9222be12009-04-23 17:14:37 +03004933static void svm_complete_interrupts(struct vcpu_svm *svm)
4934{
4935 u8 vector;
4936 int type;
4937 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01004938 unsigned int3_injected = svm->int3_injected;
4939
4940 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03004941
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02004942 /*
4943 * If we've made progress since setting HF_IRET_MASK, we've
4944 * executed an IRET and can allow NMI injection.
4945 */
4946 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
4947 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03004948 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03004949 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
4950 }
Gleb Natapov44c11432009-05-11 13:35:52 +03004951
Gleb Natapov9222be12009-04-23 17:14:37 +03004952 svm->vcpu.arch.nmi_injected = false;
4953 kvm_clear_exception_queue(&svm->vcpu);
4954 kvm_clear_interrupt_queue(&svm->vcpu);
4955
4956 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
4957 return;
4958
Avi Kivity3842d132010-07-27 12:30:24 +03004959 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
4960
Gleb Natapov9222be12009-04-23 17:14:37 +03004961 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
4962 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
4963
4964 switch (type) {
4965 case SVM_EXITINTINFO_TYPE_NMI:
4966 svm->vcpu.arch.nmi_injected = true;
4967 break;
4968 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01004969 /*
4970 * In case of software exceptions, do not reinject the vector,
4971 * but re-execute the instruction instead. Rewind RIP first
4972 * if we emulated INT3 before.
4973 */
4974 if (kvm_exception_is_soft(vector)) {
4975 if (vector == BP_VECTOR && int3_injected &&
4976 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
4977 kvm_rip_write(&svm->vcpu,
4978 kvm_rip_read(&svm->vcpu) -
4979 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02004980 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01004981 }
Gleb Natapov9222be12009-04-23 17:14:37 +03004982 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
4983 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02004984 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03004985
4986 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02004987 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03004988 break;
4989 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004990 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03004991 break;
4992 default:
4993 break;
4994 }
4995}
4996
Avi Kivityb463a6f2010-07-20 15:06:17 +03004997static void svm_cancel_injection(struct kvm_vcpu *vcpu)
4998{
4999 struct vcpu_svm *svm = to_svm(vcpu);
5000 struct vmcb_control_area *control = &svm->vmcb->control;
5001
5002 control->exit_int_info = control->event_inj;
5003 control->exit_int_info_err = control->event_inj_err;
5004 control->event_inj = 0;
5005 svm_complete_interrupts(svm);
5006}
5007
Avi Kivity851ba692009-08-24 11:10:17 +03005008static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005009{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005010 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03005011
Joerg Roedel2041a062010-04-22 12:33:08 +02005012 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5013 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5014 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5015
Joerg Roedelcd3ff652009-10-09 16:08:26 +02005016 /*
5017 * A vmexit emulation is required before the vcpu can be executed
5018 * again.
5019 */
5020 if (unlikely(svm->nested.exit_required))
5021 return;
5022
Ladi Proseka12713c2017-06-21 09:07:00 +02005023 /*
5024 * Disable singlestep if we're injecting an interrupt/exception.
5025 * We don't want our modified rflags to be pushed on the stack where
5026 * we might not be able to easily reset them if we disabled NMI
5027 * singlestep later.
5028 */
5029 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5030 /*
5031 * Event injection happens before external interrupts cause a
5032 * vmexit and interrupts are disabled here, so smp_send_reschedule
5033 * is enough to force an immediate vmexit.
5034 */
5035 disable_nmi_singlestep(svm);
5036 smp_send_reschedule(vcpu->cpu);
5037 }
5038
Rusty Russelle756fc62007-07-30 20:07:08 +10005039 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005040
Joerg Roedel649d6862008-04-16 16:51:15 +02005041 sync_lapic_to_cr8(vcpu);
5042
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02005043 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005044
Avi Kivity04d2cc72007-09-10 18:10:54 +03005045 clgi();
5046
5047 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08005048
Avi Kivity6aa8b732006-12-10 02:21:36 -08005049 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03005050 "push %%" _ASM_BP "; \n\t"
5051 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5052 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5053 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5054 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5055 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5056 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005057#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005058 "mov %c[r8](%[svm]), %%r8 \n\t"
5059 "mov %c[r9](%[svm]), %%r9 \n\t"
5060 "mov %c[r10](%[svm]), %%r10 \n\t"
5061 "mov %c[r11](%[svm]), %%r11 \n\t"
5062 "mov %c[r12](%[svm]), %%r12 \n\t"
5063 "mov %c[r13](%[svm]), %%r13 \n\t"
5064 "mov %c[r14](%[svm]), %%r14 \n\t"
5065 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005066#endif
5067
Avi Kivity6aa8b732006-12-10 02:21:36 -08005068 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03005069 "push %%" _ASM_AX " \n\t"
5070 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03005071 __ex(SVM_VMLOAD) "\n\t"
5072 __ex(SVM_VMRUN) "\n\t"
5073 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005074 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005075
5076 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03005077 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5078 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5079 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5080 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5081 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5082 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005083#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005084 "mov %%r8, %c[r8](%[svm]) \n\t"
5085 "mov %%r9, %c[r9](%[svm]) \n\t"
5086 "mov %%r10, %c[r10](%[svm]) \n\t"
5087 "mov %%r11, %c[r11](%[svm]) \n\t"
5088 "mov %%r12, %c[r12](%[svm]) \n\t"
5089 "mov %%r13, %c[r13](%[svm]) \n\t"
5090 "mov %%r14, %c[r14](%[svm]) \n\t"
5091 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005092#endif
Avi Kivity74547662012-09-16 15:10:59 +03005093 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08005094 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005095 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08005096 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005097 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5098 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5099 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5100 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5101 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5102 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005103#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005104 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5105 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5106 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5107 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5108 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5109 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5110 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5111 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005112#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02005113 : "cc", "memory"
5114#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03005115 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005116 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03005117#else
5118 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005119#endif
5120 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08005121
Avi Kivity82ca2d12010-10-21 12:20:34 +02005122#ifdef CONFIG_X86_64
5123 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5124#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02005125 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02005126#ifndef CONFIG_X86_32_LAZY_GS
5127 loadsegment(gs, svm->host.gs);
5128#endif
Avi Kivity9581d442010-10-19 16:46:55 +02005129#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005130
5131 reload_tss(vcpu);
5132
Avi Kivity56ba47d2007-11-07 17:14:18 +02005133 local_irq_disable();
5134
Avi Kivity13c34e02010-10-21 12:20:31 +02005135 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5136 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5137 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5138 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5139
Joerg Roedel3781c012011-01-14 16:45:02 +01005140 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5141 kvm_before_handle_nmi(&svm->vcpu);
5142
5143 stgi();
5144
5145 /* Any pending NMI will happen here */
5146
5147 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5148 kvm_after_handle_nmi(&svm->vcpu);
5149
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005150 sync_cr8_to_lapic(vcpu);
5151
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005152 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005153
Joerg Roedel38e5e922010-12-03 15:25:16 +01005154 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5155
Gleb Natapov631bc482010-10-14 11:22:52 +02005156 /* if exit due to PF check for async PF */
5157 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005158 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
Gleb Natapov631bc482010-10-14 11:22:52 +02005159
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005160 if (npt_enabled) {
5161 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5162 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5163 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02005164
5165 /*
5166 * We need to handle MC intercepts here before the vcpu has a chance to
5167 * change the physical cpu
5168 */
5169 if (unlikely(svm->vmcb->control.exit_code ==
5170 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5171 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01005172
5173 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005174}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005175STACK_FRAME_NON_STANDARD(svm_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005176
Avi Kivity6aa8b732006-12-10 02:21:36 -08005177static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5178{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005179 struct vcpu_svm *svm = to_svm(vcpu);
5180
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005181 svm->vmcb->save.cr3 = __sme_set(root);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005182 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01005183 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005184}
5185
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005186static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5187{
5188 struct vcpu_svm *svm = to_svm(vcpu);
5189
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005190 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01005191 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005192
5193 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02005194 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005195 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005196
Joerg Roedelf40f6a42010-12-03 15:25:15 +01005197 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005198}
5199
Avi Kivity6aa8b732006-12-10 02:21:36 -08005200static int is_disabled(void)
5201{
Joerg Roedel6031a612007-06-22 12:29:50 +03005202 u64 vm_cr;
5203
5204 rdmsrl(MSR_VM_CR, vm_cr);
5205 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5206 return 1;
5207
Avi Kivity6aa8b732006-12-10 02:21:36 -08005208 return 0;
5209}
5210
Ingo Molnar102d8322007-02-19 14:37:47 +02005211static void
5212svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5213{
5214 /*
5215 * Patch in the VMMCALL instruction:
5216 */
5217 hypercall[0] = 0x0f;
5218 hypercall[1] = 0x01;
5219 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02005220}
5221
Yang, Sheng002c7f72007-07-31 14:23:01 +03005222static void svm_check_processor_compat(void *rtn)
5223{
5224 *(int *)rtn = 0;
5225}
5226
Avi Kivity774ead32007-12-26 13:57:04 +02005227static bool svm_cpu_has_accelerated_tpr(void)
5228{
5229 return false;
5230}
5231
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005232static bool svm_has_high_real_mode_segbase(void)
5233{
5234 return true;
5235}
5236
Paolo Bonzinifc07e762015-10-01 13:20:22 +02005237static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5238{
5239 return 0;
5240}
5241
Sheng Yang0e851882009-12-18 16:48:46 +08005242static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5243{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005244 struct vcpu_svm *svm = to_svm(vcpu);
5245
5246 /* Update nrips enabled cache */
Radim Krčmářd6321d42017-08-05 00:12:49 +02005247 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005248
5249 if (!kvm_vcpu_apicv_active(vcpu))
5250 return;
5251
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02005252 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08005253}
5254
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005255static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5256{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005257 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005258 case 0x1:
5259 if (avic)
5260 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5261 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02005262 case 0x80000001:
5263 if (nested)
5264 entry->ecx |= (1 << 2); /* Set SVM bit */
5265 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005266 case 0x8000000A:
5267 entry->eax = 1; /* SVM revision 1 */
5268 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5269 ASID emulation to nested SVM */
5270 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02005271 entry->edx = 0; /* Per default do not support any
5272 additional features */
5273
5274 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02005275 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02005276 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005277
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005278 /* Support NPT for the guest if enabled */
5279 if (npt_enabled)
5280 entry->edx |= SVM_FEATURE_NPT;
5281
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005282 break;
Brijesh Singh8765d752017-12-04 10:57:25 -06005283 case 0x8000001F:
5284 /* Support memory encryption cpuid if host supports it */
5285 if (boot_cpu_has(X86_FEATURE_SEV))
5286 cpuid(0x8000001f, &entry->eax, &entry->ebx,
5287 &entry->ecx, &entry->edx);
5288
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005289 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005290}
5291
Sheng Yang17cc3932010-01-05 19:02:27 +08005292static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005293{
Sheng Yang17cc3932010-01-05 19:02:27 +08005294 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005295}
5296
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005297static bool svm_rdtscp_supported(void)
5298{
Paolo Bonzini46896c72015-11-12 14:49:16 +01005299 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005300}
5301
Mao, Junjiead756a12012-07-02 01:18:48 +00005302static bool svm_invpcid_supported(void)
5303{
5304 return false;
5305}
5306
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005307static bool svm_mpx_supported(void)
5308{
5309 return false;
5310}
5311
Wanpeng Li55412b22014-12-02 19:21:30 +08005312static bool svm_xsaves_supported(void)
5313{
5314 return false;
5315}
5316
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005317static bool svm_has_wbinvd_exit(void)
5318{
5319 return true;
5320}
5321
Joerg Roedel80612522011-04-04 12:39:33 +02005322#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005323 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005324#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005325 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005326#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005327 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005328
Mathias Krause09941fb2012-08-30 01:30:20 +02005329static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005330 u32 exit_code;
5331 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005332} x86_intercept_map[] = {
5333 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
5334 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
5335 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
5336 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
5337 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02005338 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
5339 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02005340 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
5341 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
5342 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
5343 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
5344 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
5345 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
5346 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
5347 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02005348 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
5349 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
5350 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
5351 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
5352 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
5353 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
5354 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
5355 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005356 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
5357 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
5358 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02005359 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
5360 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
5361 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
5362 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
5363 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
5364 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
5365 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
5366 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
5367 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02005368 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
5369 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
5370 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
5371 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
5372 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
5373 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
5374 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02005375 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
5376 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
5377 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
5378 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005379};
5380
Joerg Roedel80612522011-04-04 12:39:33 +02005381#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005382#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005383#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005384
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005385static int svm_check_intercept(struct kvm_vcpu *vcpu,
5386 struct x86_instruction_info *info,
5387 enum x86_intercept_stage stage)
5388{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005389 struct vcpu_svm *svm = to_svm(vcpu);
5390 int vmexit, ret = X86EMUL_CONTINUE;
5391 struct __x86_intercept icpt_info;
5392 struct vmcb *vmcb = svm->vmcb;
5393
5394 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
5395 goto out;
5396
5397 icpt_info = x86_intercept_map[info->intercept];
5398
Avi Kivity40e19b52011-04-21 12:35:41 +03005399 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005400 goto out;
5401
5402 switch (icpt_info.exit_code) {
5403 case SVM_EXIT_READ_CR0:
5404 if (info->intercept == x86_intercept_cr_read)
5405 icpt_info.exit_code += info->modrm_reg;
5406 break;
5407 case SVM_EXIT_WRITE_CR0: {
5408 unsigned long cr0, val;
5409 u64 intercept;
5410
5411 if (info->intercept == x86_intercept_cr_write)
5412 icpt_info.exit_code += info->modrm_reg;
5413
Jan Kiszka62baf442014-06-29 21:55:53 +02005414 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
5415 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005416 break;
5417
5418 intercept = svm->nested.intercept;
5419
5420 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
5421 break;
5422
5423 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
5424 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
5425
5426 if (info->intercept == x86_intercept_lmsw) {
5427 cr0 &= 0xfUL;
5428 val &= 0xfUL;
5429 /* lmsw can't clear PE - catch this here */
5430 if (cr0 & X86_CR0_PE)
5431 val |= X86_CR0_PE;
5432 }
5433
5434 if (cr0 ^ val)
5435 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
5436
5437 break;
5438 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02005439 case SVM_EXIT_READ_DR0:
5440 case SVM_EXIT_WRITE_DR0:
5441 icpt_info.exit_code += info->modrm_reg;
5442 break;
Joerg Roedel80612522011-04-04 12:39:33 +02005443 case SVM_EXIT_MSR:
5444 if (info->intercept == x86_intercept_wrmsr)
5445 vmcb->control.exit_info_1 = 1;
5446 else
5447 vmcb->control.exit_info_1 = 0;
5448 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02005449 case SVM_EXIT_PAUSE:
5450 /*
5451 * We get this for NOP only, but pause
5452 * is rep not, check this here
5453 */
5454 if (info->rep_prefix != REPE_PREFIX)
5455 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02005456 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02005457 case SVM_EXIT_IOIO: {
5458 u64 exit_info;
5459 u32 bytes;
5460
Joerg Roedelf6511932011-04-04 12:39:35 +02005461 if (info->intercept == x86_intercept_in ||
5462 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005463 exit_info = ((info->src_val & 0xffff) << 16) |
5464 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02005465 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02005466 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005467 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02005468 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02005469 }
5470
5471 if (info->intercept == x86_intercept_outs ||
5472 info->intercept == x86_intercept_ins)
5473 exit_info |= SVM_IOIO_STR_MASK;
5474
5475 if (info->rep_prefix)
5476 exit_info |= SVM_IOIO_REP_MASK;
5477
5478 bytes = min(bytes, 4u);
5479
5480 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
5481
5482 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
5483
5484 vmcb->control.exit_info_1 = exit_info;
5485 vmcb->control.exit_info_2 = info->next_rip;
5486
5487 break;
5488 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005489 default:
5490 break;
5491 }
5492
Bandan Dasf1047652015-06-11 02:05:33 -04005493 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
5494 if (static_cpu_has(X86_FEATURE_NRIPS))
5495 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005496 vmcb->control.exit_code = icpt_info.exit_code;
5497 vmexit = nested_svm_exit_handled(svm);
5498
5499 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
5500 : X86EMUL_CONTINUE;
5501
5502out:
5503 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005504}
5505
Yang Zhanga547c6d2013-04-11 19:25:10 +08005506static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
5507{
5508 local_irq_enable();
Paolo Bonzinif2485b32016-06-15 15:23:11 +02005509 /*
5510 * We must have an instruction with interrupts enabled, so
5511 * the timer interrupt isn't delayed by the interrupt shadow.
5512 */
5513 asm("nop");
5514 local_irq_disable();
Yang Zhanga547c6d2013-04-11 19:25:10 +08005515}
5516
Radim Krčmářae97a3b2014-08-21 18:08:06 +02005517static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
5518{
5519}
5520
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05005521static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
5522{
5523 if (avic_handle_apic_id_update(vcpu) != 0)
5524 return;
5525 if (avic_handle_dfr_update(vcpu) != 0)
5526 return;
5527 avic_handle_ldr_update(vcpu);
5528}
5529
Borislav Petkov74f16902017-03-26 23:51:24 +02005530static void svm_setup_mce(struct kvm_vcpu *vcpu)
5531{
5532 /* [63:9] are reserved. */
5533 vcpu->arch.mcg_cap &= 0x1ff;
5534}
5535
Ladi Prosek72d7b372017-10-11 16:54:41 +02005536static int svm_smi_allowed(struct kvm_vcpu *vcpu)
5537{
Ladi Prosek05cade72017-10-11 16:54:45 +02005538 struct vcpu_svm *svm = to_svm(vcpu);
5539
5540 /* Per APM Vol.2 15.22.2 "Response to SMI" */
5541 if (!gif_set(svm))
5542 return 0;
5543
5544 if (is_guest_mode(&svm->vcpu) &&
5545 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
5546 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
5547 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
5548 svm->nested.exit_required = true;
5549 return 0;
5550 }
5551
Ladi Prosek72d7b372017-10-11 16:54:41 +02005552 return 1;
5553}
5554
Ladi Prosek0234bf82017-10-11 16:54:40 +02005555static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
5556{
Ladi Prosek05cade72017-10-11 16:54:45 +02005557 struct vcpu_svm *svm = to_svm(vcpu);
5558 int ret;
5559
5560 if (is_guest_mode(vcpu)) {
5561 /* FED8h - SVM Guest */
5562 put_smstate(u64, smstate, 0x7ed8, 1);
5563 /* FEE0h - SVM Guest VMCB Physical Address */
5564 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
5565
5566 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5567 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5568 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5569
5570 ret = nested_svm_vmexit(svm);
5571 if (ret)
5572 return ret;
5573 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02005574 return 0;
5575}
5576
5577static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
5578{
Ladi Prosek05cade72017-10-11 16:54:45 +02005579 struct vcpu_svm *svm = to_svm(vcpu);
5580 struct vmcb *nested_vmcb;
5581 struct page *page;
5582 struct {
5583 u64 guest;
5584 u64 vmcb;
5585 } svm_state_save;
5586 int ret;
5587
5588 ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save,
5589 sizeof(svm_state_save));
5590 if (ret)
5591 return ret;
5592
5593 if (svm_state_save.guest) {
5594 vcpu->arch.hflags &= ~HF_SMM_MASK;
5595 nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page);
5596 if (nested_vmcb)
5597 enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page);
5598 else
5599 ret = 1;
5600 vcpu->arch.hflags |= HF_SMM_MASK;
5601 }
5602 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02005603}
5604
Ladi Prosekcc3d9672017-10-17 16:02:39 +02005605static int enable_smi_window(struct kvm_vcpu *vcpu)
5606{
5607 struct vcpu_svm *svm = to_svm(vcpu);
5608
5609 if (!gif_set(svm)) {
5610 if (vgif_enabled(svm))
5611 set_intercept(svm, INTERCEPT_STGI);
5612 /* STGI will cause a vm exit */
5613 return 1;
5614 }
5615 return 0;
5616}
5617
Brijesh Singh1654efc2017-12-04 10:57:34 -06005618static int sev_asid_new(void)
5619{
5620 int pos;
5621
5622 /*
5623 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
5624 */
5625 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
5626 if (pos >= max_sev_asid)
5627 return -EBUSY;
5628
5629 set_bit(pos, sev_asid_bitmap);
5630 return pos + 1;
5631}
5632
5633static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
5634{
5635 struct kvm_sev_info *sev = &kvm->arch.sev_info;
5636 int asid, ret;
5637
5638 ret = -EBUSY;
5639 asid = sev_asid_new();
5640 if (asid < 0)
5641 return ret;
5642
5643 ret = sev_platform_init(&argp->error);
5644 if (ret)
5645 goto e_free;
5646
5647 sev->active = true;
5648 sev->asid = asid;
5649
5650 return 0;
5651
5652e_free:
5653 __sev_asid_free(asid);
5654 return ret;
5655}
5656
5657static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
5658{
5659 struct kvm_sev_cmd sev_cmd;
5660 int r;
5661
5662 if (!svm_sev_enabled())
5663 return -ENOTTY;
5664
5665 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
5666 return -EFAULT;
5667
5668 mutex_lock(&kvm->lock);
5669
5670 switch (sev_cmd.id) {
5671 case KVM_SEV_INIT:
5672 r = sev_guest_init(kvm, &sev_cmd);
5673 break;
5674 default:
5675 r = -EINVAL;
5676 goto out;
5677 }
5678
5679 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
5680 r = -EFAULT;
5681
5682out:
5683 mutex_unlock(&kvm->lock);
5684 return r;
5685}
5686
Kees Cook404f6aa2016-08-08 16:29:06 -07005687static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005688 .cpu_has_kvm_support = has_svm,
5689 .disabled_by_bios = is_disabled,
5690 .hardware_setup = svm_hardware_setup,
5691 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03005692 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005693 .hardware_enable = svm_hardware_enable,
5694 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02005695 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005696 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005697
5698 .vcpu_create = svm_create_vcpu,
5699 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03005700 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005701
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005702 .vm_init = avic_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06005703 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005704
Avi Kivity04d2cc72007-09-10 18:10:54 +03005705 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005706 .vcpu_load = svm_vcpu_load,
5707 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05005708 .vcpu_blocking = svm_vcpu_blocking,
5709 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005710
Paolo Bonzinia96036b2015-11-10 11:55:36 +01005711 .update_bp_intercept = update_bp_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005712 .get_msr = svm_get_msr,
5713 .set_msr = svm_set_msr,
5714 .get_segment_base = svm_get_segment_base,
5715 .get_segment = svm_get_segment,
5716 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02005717 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10005718 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02005719 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02005720 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03005721 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005722 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005723 .set_cr3 = svm_set_cr3,
5724 .set_cr4 = svm_set_cr4,
5725 .set_efer = svm_set_efer,
5726 .get_idt = svm_get_idt,
5727 .set_idt = svm_set_idt,
5728 .get_gdt = svm_get_gdt,
5729 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005730 .get_dr6 = svm_get_dr6,
5731 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03005732 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01005733 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005734 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005735 .get_rflags = svm_get_rflags,
5736 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005737
Avi Kivity6aa8b732006-12-10 02:21:36 -08005738 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005739
Avi Kivity6aa8b732006-12-10 02:21:36 -08005740 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03005741 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005742 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04005743 .set_interrupt_shadow = svm_set_interrupt_shadow,
5744 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02005745 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03005746 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005747 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02005748 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03005749 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02005750 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005751 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005752 .get_nmi_mask = svm_get_nmi_mask,
5753 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005754 .enable_nmi_window = enable_nmi_window,
5755 .enable_irq_window = enable_irq_window,
5756 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08005757 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03005758 .get_enable_apicv = svm_get_enable_apicv,
5759 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005760 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005761 .hwapic_irr_update = svm_hwapic_irr_update,
5762 .hwapic_isr_update = svm_hwapic_isr_update,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05005763 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02005764
5765 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08005766 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005767 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005768
Avi Kivity586f9602010-11-18 13:09:54 +02005769 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02005770
Sheng Yang17cc3932010-01-05 19:02:27 +08005771 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08005772
5773 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005774
5775 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00005776 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005777 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08005778 .xsaves_supported = svm_xsaves_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005779
5780 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005781
5782 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10005783
5784 .write_tsc_offset = svm_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005785
5786 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005787
5788 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08005789 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02005790
5791 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02005792
5793 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005794 .deliver_posted_interrupt = svm_deliver_avic_intr,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005795 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02005796 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02005797
Ladi Prosek72d7b372017-10-11 16:54:41 +02005798 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02005799 .pre_enter_smm = svm_pre_enter_smm,
5800 .pre_leave_smm = svm_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02005801 .enable_smi_window = enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06005802
5803 .mem_enc_op = svm_mem_enc_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005804};
5805
5806static int __init svm_init(void)
5807{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08005808 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03005809 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005810}
5811
5812static void __exit svm_exit(void)
5813{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08005814 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08005815}
5816
5817module_init(svm_init)
5818module_exit(svm_exit)