blob: ca69d53d7e6dbc3352d5ce31371fe72dc84310ab [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * AMD SVM support
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02007 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08008 *
9 * Authors:
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050017
18#define pr_fmt(fmt) "SVM: " fmt
19
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
21
Eddie Dong85f455f2007-07-06 12:20:49 +030022#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080023#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020025#include "x86.h"
Julian Stecklina66f7b722012-12-05 15:26:19 +010026#include "cpuid.h"
Wei Huang25462f72015-06-19 15:45:05 +020027#include "pmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040028
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/module.h>
Josh Triplettae759542012-03-28 11:32:28 -070030#include <linux/mod_devicetable.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020031#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/vmalloc.h>
33#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040034#include <linux/sched.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040035#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -050037#include <linux/amd-iommu.h>
38#include <linux/hashtable.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050039#include <linux/frame.h>
Brijesh Singhe9df0942017-12-04 10:57:33 -060040#include <linux/psp-sev.h>
Brijesh Singh1654efc2017-12-04 10:57:34 -060041#include <linux/file.h>
Brijesh Singh89c50582017-12-04 10:57:35 -060042#include <linux/pagemap.h>
43#include <linux/swap.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050045#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010046#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020047#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040048#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020050#include <asm/kvm_para.h>
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -050051#include <asm/irq_remapping.h>
David Woodhouse117cc7a2018-01-12 11:11:27 +000052#include <asm/nospec-branch.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080053
Eduardo Habkost63d11422008-11-17 19:03:20 -020054#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030055#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020056
Avi Kivity4ecac3f2008-05-13 13:23:38 +030057#define __ex(x) __kvm_handle_fault_on_reboot(x)
58
Avi Kivity6aa8b732006-12-10 02:21:36 -080059MODULE_AUTHOR("Qumranet");
60MODULE_LICENSE("GPL");
61
Josh Triplettae759542012-03-28 11:32:28 -070062static const struct x86_cpu_id svm_cpu_id[] = {
63 X86_FEATURE_MATCH(X86_FEATURE_SVM),
64 {}
65};
66MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
67
Avi Kivity6aa8b732006-12-10 02:21:36 -080068#define IOPM_ALLOC_ORDER 2
69#define MSRPM_ALLOC_ORDER 1
70
Avi Kivity6aa8b732006-12-10 02:21:36 -080071#define SEG_TYPE_LDT 2
72#define SEG_TYPE_BUSY_TSS16 3
73
Andre Przywara6bc31bd2010-04-11 23:07:28 +020074#define SVM_FEATURE_NPT (1 << 0)
75#define SVM_FEATURE_LBRV (1 << 1)
76#define SVM_FEATURE_SVML (1 << 2)
77#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010078#define SVM_FEATURE_TSC_RATE (1 << 4)
79#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
80#define SVM_FEATURE_FLUSH_ASID (1 << 6)
81#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020082#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030083
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -050084#define SVM_AVIC_DOORBELL 0xc001011b
85
Joerg Roedel410e4d52009-08-07 11:49:44 +020086#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
87#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
88#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
89
Joerg Roedel24e09cb2008-02-13 18:58:47 +010090#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
91
Joerg Roedelfbc0db72011-03-25 09:44:46 +010092#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010093#define TSC_RATIO_MIN 0x0000000000000001ULL
94#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010095
Dan Carpenter5446a972016-05-23 13:20:10 +030096#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050097
98/*
99 * 0xff is broadcast, so the max index allowed for physical APIC ID
100 * table is 0xfe. APIC IDs above 0xff are reserved.
101 */
102#define AVIC_MAX_PHYSICAL_ID_COUNT 255
103
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500104#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
105#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
106#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
107
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500108/* AVIC GATAG is encoded using VM and VCPU IDs */
109#define AVIC_VCPU_ID_BITS 8
110#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
111
112#define AVIC_VM_ID_BITS 24
113#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
114#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
115
116#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
117 (y & AVIC_VCPU_ID_MASK))
118#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
119#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
120
Joerg Roedel67ec6602010-05-17 14:43:35 +0200121static bool erratum_383_found __read_mostly;
122
Avi Kivity6c8166a2009-05-31 18:15:37 +0300123static const u32 host_save_user_msrs[] = {
124#ifdef CONFIG_X86_64
125 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
126 MSR_FS_BASE,
127#endif
128 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Paolo Bonzini46896c72015-11-12 14:49:16 +0100129 MSR_TSC_AUX,
Avi Kivity6c8166a2009-05-31 18:15:37 +0300130};
131
132#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
133
134struct kvm_vcpu;
135
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200136struct nested_state {
137 struct vmcb *hsave;
138 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +0100139 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200140 u64 vmcb;
141
142 /* These are the merged vectors */
143 u32 *msrpm;
144
145 /* gpa pointers to the real vectors */
146 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100147 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200148
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200149 /* A VMEXIT is required but not yet emulated */
150 bool exit_required;
151
Joerg Roedelaad42c62009-08-07 11:49:34 +0200152 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100153 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100154 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200155 u32 intercept_exceptions;
156 u64 intercept;
157
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200158 /* Nested Paging related state */
159 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200160};
161
Joerg Roedel323c3d82010-03-01 15:34:37 +0100162#define MSRPM_OFFSETS 16
163static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
164
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500165/*
166 * Set osvw_len to higher value when updated Revision Guides
167 * are published and we know what the new status bits are
168 */
169static uint64_t osvw_len = 4, osvw_status;
170
Avi Kivity6c8166a2009-05-31 18:15:37 +0300171struct vcpu_svm {
172 struct kvm_vcpu vcpu;
173 struct vmcb *vmcb;
174 unsigned long vmcb_pa;
175 struct svm_cpu_data *svm_data;
176 uint64_t asid_generation;
177 uint64_t sysenter_esp;
178 uint64_t sysenter_eip;
Paolo Bonzini46896c72015-11-12 14:49:16 +0100179 uint64_t tsc_aux;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300180
181 u64 next_rip;
182
183 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200184 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200185 u16 fs;
186 u16 gs;
187 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200188 u64 gs_base;
189 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300190
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100191 u64 spec_ctrl;
192
Avi Kivity6c8166a2009-05-31 18:15:37 +0300193 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300194
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200195 ulong nmi_iret_rip;
196
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200197 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200198
199 bool nmi_singlestep;
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200200 u64 nmi_singlestep_guest_rflags;
Jan Kiszka66b71382010-02-23 17:47:56 +0100201
202 unsigned int3_injected;
203 unsigned long int3_rip;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100204
Joerg Roedel6092d3d2015-10-14 15:10:54 +0200205 /* cached guest cpuid flags for faster access */
206 bool nrips_enabled : 1;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500207
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500208 u32 ldr_reg;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500209 struct page *avic_backing_page;
210 u64 *avic_physical_id_cache;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -0500211 bool avic_is_running;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500212
213 /*
214 * Per-vcpu list of struct amd_svm_iommu_ir:
215 * This is used mainly to store interrupt remapping information used
216 * when update the vcpu affinity. This avoids the need to scan for
217 * IRTE and try to match ga_tag in the IOMMU driver.
218 */
219 struct list_head ir_list;
220 spinlock_t ir_list_lock;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600221
222 /* which host CPU was used for running this vcpu */
223 unsigned int last_cpu;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500224};
225
226/*
227 * This is a wrapper of struct amd_iommu_ir_data.
228 */
229struct amd_svm_iommu_ir {
230 struct list_head node; /* Used by SVM for per-vcpu ir_list */
231 void *data; /* Storing pointer to struct amd_ir_data */
Avi Kivity6c8166a2009-05-31 18:15:37 +0300232};
233
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500234#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
235#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
236
237#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
238#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
239#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
240#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
241
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100242static DEFINE_PER_CPU(u64, current_tsc_ratio);
243#define TSC_RATIO_DEFAULT 0x0100000000ULL
244
Joerg Roedel455716f2010-03-01 15:34:35 +0100245#define MSR_INVALID 0xffffffffU
246
Mathias Krause09941fb2012-08-30 01:30:20 +0200247static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100248 u32 index; /* Index of the MSR */
249 bool always; /* True if intercept is always on */
250} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400251 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100252 { .index = MSR_IA32_SYSENTER_CS, .always = true },
253#ifdef CONFIG_X86_64
254 { .index = MSR_GS_BASE, .always = true },
255 { .index = MSR_FS_BASE, .always = true },
256 { .index = MSR_KERNEL_GS_BASE, .always = true },
257 { .index = MSR_LSTAR, .always = true },
258 { .index = MSR_CSTAR, .always = true },
259 { .index = MSR_SYSCALL_MASK, .always = true },
260#endif
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100261 { .index = MSR_IA32_SPEC_CTRL, .always = false },
Ashok Raj15d45072018-02-01 22:59:43 +0100262 { .index = MSR_IA32_PRED_CMD, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100263 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
264 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
265 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
266 { .index = MSR_IA32_LASTINTTOIP, .always = false },
267 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800268};
269
270/* enable NPT for AMD64 and X86 with PAE */
271#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
272static bool npt_enabled = true;
273#else
Joerg Roedele0231712010-02-24 18:59:10 +0100274static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275#endif
276
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100277/* allow nested paging (virtualized MMU) for all guests */
278static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279module_param(npt, int, S_IRUGO);
280
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100281/* allow nested virtualization in KVM/SVM */
282static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800283module_param(nested, int, S_IRUGO);
284
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500285/* enable / disable AVIC */
286static int avic;
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500287#ifdef CONFIG_X86_LOCAL_APIC
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500288module_param(avic, int, S_IRUGO);
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500289#endif
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500290
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -0500291/* enable/disable Virtual VMLOAD VMSAVE */
292static int vls = true;
293module_param(vls, int, 0444);
294
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500295/* enable/disable Virtual GIF */
296static int vgif = true;
297module_param(vgif, int, 0444);
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500298
Brijesh Singhe9df0942017-12-04 10:57:33 -0600299/* enable/disable SEV support */
300static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
301module_param(sev, int, 0444);
302
Paolo Bonzini79a80592015-09-21 07:46:55 +0200303static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -0800304static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa);
Joerg Roedela5c38322009-08-07 11:49:32 +0200305static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306
Joerg Roedel410e4d52009-08-07 11:49:44 +0200307static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100308static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800309static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
311 bool has_error_code, u32 error_code);
312
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100313enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100314 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
315 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100316 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100317 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100318 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100319 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100320 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100321 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100322 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100323 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100324 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100325 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500326 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
327 * AVIC PHYSICAL_TABLE pointer,
328 * AVIC LOGICAL_TABLE pointer
329 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100330 VMCB_DIRTY_MAX,
331};
332
Joerg Roedel0574dec2010-12-03 11:45:58 +0100333/* TPR and CR2 are always written before VMRUN */
334#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100335
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500336#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
337
Brijesh Singhed3cd232017-12-04 10:57:32 -0600338static unsigned int max_sev_asid;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600339static unsigned int min_sev_asid;
340static unsigned long *sev_asid_bitmap;
Brijesh Singh89c50582017-12-04 10:57:35 -0600341#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
Brijesh Singh1654efc2017-12-04 10:57:34 -0600342
Brijesh Singh1e80fdc2017-12-04 10:57:38 -0600343struct enc_region {
344 struct list_head list;
345 unsigned long npages;
346 struct page **pages;
347 unsigned long uaddr;
348 unsigned long size;
349};
350
Brijesh Singh1654efc2017-12-04 10:57:34 -0600351static inline bool svm_sev_enabled(void)
352{
353 return max_sev_asid;
354}
355
356static inline bool sev_guest(struct kvm *kvm)
357{
358 struct kvm_sev_info *sev = &kvm->arch.sev_info;
359
360 return sev->active;
361}
Brijesh Singhed3cd232017-12-04 10:57:32 -0600362
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600363static inline int sev_get_asid(struct kvm *kvm)
364{
365 struct kvm_sev_info *sev = &kvm->arch.sev_info;
366
367 return sev->asid;
368}
369
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100370static inline void mark_all_dirty(struct vmcb *vmcb)
371{
372 vmcb->control.clean = 0;
373}
374
375static inline void mark_all_clean(struct vmcb *vmcb)
376{
377 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
378 & ~VMCB_ALWAYS_DIRTY_MASK;
379}
380
381static inline void mark_dirty(struct vmcb *vmcb, int bit)
382{
383 vmcb->control.clean &= ~(1 << bit);
384}
385
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
387{
388 return container_of(vcpu, struct vcpu_svm, vcpu);
389}
390
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500391static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
392{
393 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
394 mark_dirty(svm->vmcb, VMCB_AVIC);
395}
396
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -0500397static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
398{
399 struct vcpu_svm *svm = to_svm(vcpu);
400 u64 *entry = svm->avic_physical_id_cache;
401
402 if (!entry)
403 return false;
404
405 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
406}
407
Joerg Roedel384c6362010-11-30 18:03:56 +0100408static void recalc_intercepts(struct vcpu_svm *svm)
409{
410 struct vmcb_control_area *c, *h;
411 struct nested_state *g;
412
Joerg Roedel116a0a22010-12-03 11:45:49 +0100413 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
414
Joerg Roedel384c6362010-11-30 18:03:56 +0100415 if (!is_guest_mode(&svm->vcpu))
416 return;
417
418 c = &svm->vmcb->control;
419 h = &svm->nested.hsave->control;
420 g = &svm->nested;
421
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100422 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100423 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Paolo Bonzinibd895252018-01-11 16:55:24 +0100424 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
Joerg Roedel384c6362010-11-30 18:03:56 +0100425 c->intercept = h->intercept | g->intercept;
426}
427
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100428static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
429{
430 if (is_guest_mode(&svm->vcpu))
431 return svm->nested.hsave;
432 else
433 return svm->vmcb;
434}
435
436static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
437{
438 struct vmcb *vmcb = get_host_vmcb(svm);
439
440 vmcb->control.intercept_cr |= (1U << bit);
441
442 recalc_intercepts(svm);
443}
444
445static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
446{
447 struct vmcb *vmcb = get_host_vmcb(svm);
448
449 vmcb->control.intercept_cr &= ~(1U << bit);
450
451 recalc_intercepts(svm);
452}
453
454static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
455{
456 struct vmcb *vmcb = get_host_vmcb(svm);
457
458 return vmcb->control.intercept_cr & (1U << bit);
459}
460
Paolo Bonzini5315c712014-03-03 13:08:29 +0100461static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100462{
463 struct vmcb *vmcb = get_host_vmcb(svm);
464
Paolo Bonzini5315c712014-03-03 13:08:29 +0100465 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
466 | (1 << INTERCEPT_DR1_READ)
467 | (1 << INTERCEPT_DR2_READ)
468 | (1 << INTERCEPT_DR3_READ)
469 | (1 << INTERCEPT_DR4_READ)
470 | (1 << INTERCEPT_DR5_READ)
471 | (1 << INTERCEPT_DR6_READ)
472 | (1 << INTERCEPT_DR7_READ)
473 | (1 << INTERCEPT_DR0_WRITE)
474 | (1 << INTERCEPT_DR1_WRITE)
475 | (1 << INTERCEPT_DR2_WRITE)
476 | (1 << INTERCEPT_DR3_WRITE)
477 | (1 << INTERCEPT_DR4_WRITE)
478 | (1 << INTERCEPT_DR5_WRITE)
479 | (1 << INTERCEPT_DR6_WRITE)
480 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100481
482 recalc_intercepts(svm);
483}
484
Paolo Bonzini5315c712014-03-03 13:08:29 +0100485static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100486{
487 struct vmcb *vmcb = get_host_vmcb(svm);
488
Paolo Bonzini5315c712014-03-03 13:08:29 +0100489 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100490
491 recalc_intercepts(svm);
492}
493
Joerg Roedel18c918c2010-11-30 18:03:59 +0100494static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
495{
496 struct vmcb *vmcb = get_host_vmcb(svm);
497
498 vmcb->control.intercept_exceptions |= (1U << bit);
499
500 recalc_intercepts(svm);
501}
502
503static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
504{
505 struct vmcb *vmcb = get_host_vmcb(svm);
506
507 vmcb->control.intercept_exceptions &= ~(1U << bit);
508
509 recalc_intercepts(svm);
510}
511
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100512static inline void set_intercept(struct vcpu_svm *svm, int bit)
513{
514 struct vmcb *vmcb = get_host_vmcb(svm);
515
516 vmcb->control.intercept |= (1ULL << bit);
517
518 recalc_intercepts(svm);
519}
520
521static inline void clr_intercept(struct vcpu_svm *svm, int bit)
522{
523 struct vmcb *vmcb = get_host_vmcb(svm);
524
525 vmcb->control.intercept &= ~(1ULL << bit);
526
527 recalc_intercepts(svm);
528}
529
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500530static inline bool vgif_enabled(struct vcpu_svm *svm)
531{
532 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
533}
534
Joerg Roedel2af91942009-08-07 11:49:28 +0200535static inline void enable_gif(struct vcpu_svm *svm)
536{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500537 if (vgif_enabled(svm))
538 svm->vmcb->control.int_ctl |= V_GIF_MASK;
539 else
540 svm->vcpu.arch.hflags |= HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200541}
542
543static inline void disable_gif(struct vcpu_svm *svm)
544{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500545 if (vgif_enabled(svm))
546 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
547 else
548 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200549}
550
551static inline bool gif_set(struct vcpu_svm *svm)
552{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500553 if (vgif_enabled(svm))
554 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
555 else
556 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
Joerg Roedel2af91942009-08-07 11:49:28 +0200557}
558
Avi Kivity6aa8b732006-12-10 02:21:36 -0800559static unsigned long iopm_base;
560
561struct kvm_ldttss_desc {
562 u16 limit0;
563 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100564 unsigned base1:8, type:5, dpl:2, p:1;
565 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566 u32 base3;
567 u32 zero1;
568} __attribute__((packed));
569
570struct svm_cpu_data {
571 int cpu;
572
Avi Kivity5008fdf2007-04-02 13:05:50 +0300573 u64 asid_generation;
574 u32 max_asid;
575 u32 next_asid;
Brijesh Singh4faefff2017-12-04 10:57:25 -0600576 u32 min_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577 struct kvm_ldttss_desc *tss_desc;
578
579 struct page *save_area;
Ashok Raj15d45072018-02-01 22:59:43 +0100580 struct vmcb *current_vmcb;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600581
582 /* index = sev_asid, value = vmcb pointer */
583 struct vmcb **sev_vmcbs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584};
585
586static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
587
588struct svm_init_data {
589 int cpu;
590 int r;
591};
592
Mathias Krause09941fb2012-08-30 01:30:20 +0200593static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200595#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596#define MSRS_RANGE_SIZE 2048
597#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
598
Joerg Roedel455716f2010-03-01 15:34:35 +0100599static u32 svm_msrpm_offset(u32 msr)
600{
601 u32 offset;
602 int i;
603
604 for (i = 0; i < NUM_MSR_MAPS; i++) {
605 if (msr < msrpm_ranges[i] ||
606 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
607 continue;
608
609 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
610 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
611
612 /* Now we have the u8 offset - but need the u32 offset */
613 return offset / 4;
614 }
615
616 /* MSR not in any range */
617 return MSR_INVALID;
618}
619
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620#define MAX_INST_SIZE 15
621
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622static inline void clgi(void)
623{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300624 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625}
626
627static inline void stgi(void)
628{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300629 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630}
631
632static inline void invlpga(unsigned long addr, u32 asid)
633{
Joerg Roedele0231712010-02-24 18:59:10 +0100634 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635}
636
Yu Zhang855feb62017-08-24 20:27:55 +0800637static int get_npt_level(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +0200638{
639#ifdef CONFIG_X86_64
Yu Zhang2a7266a2017-08-24 20:27:54 +0800640 return PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200641#else
642 return PT32E_ROOT_LEVEL;
643#endif
644}
645
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
647{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000648 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100649 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600650 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651
Alexander Graf9962d032008-11-25 20:17:02 +0100652 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100653 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654}
655
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656static int is_external_interrupt(u32 info)
657{
658 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
659 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
660}
661
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200662static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400663{
664 struct vcpu_svm *svm = to_svm(vcpu);
665 u32 ret = 0;
666
667 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200668 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
669 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400670}
671
672static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
673{
674 struct vcpu_svm *svm = to_svm(vcpu);
675
676 if (mask == 0)
677 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
678 else
679 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
680
681}
682
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
684{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400685 struct vcpu_svm *svm = to_svm(vcpu);
686
Bandan Dasf1047652015-06-11 02:05:33 -0400687 if (svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200688 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200689 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400690 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200691
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400692 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100693 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300694 EMULATE_DONE)
695 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696 return;
697 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300698 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
699 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
700 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300702 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400703 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800704}
705
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700706static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100707{
708 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700709 unsigned nr = vcpu->arch.exception.nr;
710 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700711 bool reinject = vcpu->arch.exception.injected;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700712 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100713
Joerg Roedele0231712010-02-24 18:59:10 +0100714 /*
715 * If we are within a nested VM we'd better #VMEXIT and let the guest
716 * handle the exception
717 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200718 if (!reinject &&
719 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100720 return;
721
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200722 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100723 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
724
725 /*
726 * For guest debugging where we have to reinject #BP if some
727 * INT3 is guest-owned:
728 * Emulate nRIP by moving RIP forward. Will fail if injection
729 * raises a fault that is not intercepted. Still better than
730 * failing in all cases.
731 */
732 skip_emulated_instruction(&svm->vcpu);
733 rip = kvm_rip_read(&svm->vcpu);
734 svm->int3_rip = rip + svm->vmcb->save.cs.base;
735 svm->int3_injected = rip - old_rip;
736 }
737
Jan Kiszka116a4752010-02-23 17:47:54 +0100738 svm->vmcb->control.event_inj = nr
739 | SVM_EVTINJ_VALID
740 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
741 | SVM_EVTINJ_TYPE_EXEPT;
742 svm->vmcb->control.event_inj_err = error_code;
743}
744
Joerg Roedel67ec6602010-05-17 14:43:35 +0200745static void svm_init_erratum_383(void)
746{
747 u32 low, high;
748 int err;
749 u64 val;
750
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100751 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200752 return;
753
754 /* Use _safe variants to not break nested virtualization */
755 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
756 if (err)
757 return;
758
759 val |= (1ULL << 47);
760
761 low = lower_32_bits(val);
762 high = upper_32_bits(val);
763
764 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
765
766 erratum_383_found = true;
767}
768
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500769static void svm_init_osvw(struct kvm_vcpu *vcpu)
770{
771 /*
772 * Guests should see errata 400 and 415 as fixed (assuming that
773 * HLT and IO instructions are intercepted).
774 */
775 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
776 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
777
778 /*
779 * By increasing VCPU's osvw.length to 3 we are telling the guest that
780 * all osvw.status bits inside that length, including bit 0 (which is
781 * reserved for erratum 298), are valid. However, if host processor's
782 * osvw_len is 0 then osvw_status[0] carries no information. We need to
783 * be conservative here and therefore we tell the guest that erratum 298
784 * is present (because we really don't know).
785 */
786 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
787 vcpu->arch.osvw.status |= 1;
788}
789
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790static int has_svm(void)
791{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200792 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793
Eduardo Habkost63d11422008-11-17 19:03:20 -0200794 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800795 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 return 0;
797 }
798
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799 return 1;
800}
801
Radim Krčmář13a34e02014-08-28 15:13:03 +0200802static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100804 /* Make sure we clean up behind us */
805 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
806 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
807
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200808 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100809
810 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811}
812
Radim Krčmář13a34e02014-08-28 15:13:03 +0200813static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814{
815
Tejun Heo0fe1e002009-10-29 22:34:14 +0900816 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818 struct desc_struct *gdt;
819 int me = raw_smp_processor_id();
820
Alexander Graf10474ae2009-09-15 11:37:46 +0200821 rdmsrl(MSR_EFER, efer);
822 if (efer & EFER_SVME)
823 return -EBUSY;
824
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200826 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200827 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900829 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900830 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200831 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200832 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833 }
834
Tejun Heo0fe1e002009-10-29 22:34:14 +0900835 sd->asid_generation = 1;
836 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
837 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600838 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839
Thomas Garnier45fc8752017-03-14 10:05:08 -0700840 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900841 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842
Alexander Graf9962d032008-11-25 20:17:02 +0100843 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844
Linus Torvaldsd0316552009-12-14 09:58:24 -0800845 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200846
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100847 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
848 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500849 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100850 }
851
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500852
853 /*
854 * Get OSVW bits.
855 *
856 * Note that it is possible to have a system with mixed processor
857 * revisions and therefore different OSVW bits. If bits are not the same
858 * on different processors then choose the worst case (i.e. if erratum
859 * is present on one processor and not on another then assume that the
860 * erratum is present everywhere).
861 */
862 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
863 uint64_t len, status = 0;
864 int err;
865
866 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
867 if (!err)
868 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
869 &err);
870
871 if (err)
872 osvw_status = osvw_len = 0;
873 else {
874 if (len < osvw_len)
875 osvw_len = len;
876 osvw_status |= status;
877 osvw_status &= (1ULL << osvw_len) - 1;
878 }
879 } else
880 osvw_status = osvw_len = 0;
881
Joerg Roedel67ec6602010-05-17 14:43:35 +0200882 svm_init_erratum_383();
883
Joerg Roedel1018faa2012-02-29 14:57:32 +0100884 amd_pmu_enable_virt();
885
Alexander Graf10474ae2009-09-15 11:37:46 +0200886 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887}
888
Joerg Roedel0da1db752008-07-02 16:02:11 +0200889static void svm_cpu_uninit(int cpu)
890{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900891 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200892
Tejun Heo0fe1e002009-10-29 22:34:14 +0900893 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200894 return;
895
896 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600897 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900898 __free_page(sd->save_area);
899 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200900}
901
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902static int svm_cpu_init(int cpu)
903{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900904 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905 int r;
906
Tejun Heo0fe1e002009-10-29 22:34:14 +0900907 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
908 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900910 sd->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 r = -ENOMEM;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600912 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900913 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 goto err_1;
915
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600916 if (svm_sev_enabled()) {
917 r = -ENOMEM;
918 sd->sev_vmcbs = kmalloc((max_sev_asid + 1) * sizeof(void *), GFP_KERNEL);
919 if (!sd->sev_vmcbs)
920 goto err_1;
921 }
922
Tejun Heo0fe1e002009-10-29 22:34:14 +0900923 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924
925 return 0;
926
927err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900928 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 return r;
930
931}
932
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100933static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934{
935 int i;
936
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100937 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
938 if (direct_access_msrs[i].index == index)
939 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100941 return false;
942}
943
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100944static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr)
945{
946 u8 bit_write;
947 unsigned long tmp;
948 u32 offset;
949 u32 *msrpm;
950
951 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
952 to_svm(vcpu)->msrpm;
953
954 offset = svm_msrpm_offset(msr);
955 bit_write = 2 * (msr & 0x0f) + 1;
956 tmp = msrpm[offset];
957
958 BUG_ON(offset == MSR_INVALID);
959
960 return !!test_bit(bit_write, &tmp);
961}
962
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963static void set_msr_interception(u32 *msrpm, unsigned msr,
964 int read, int write)
965{
Joerg Roedel455716f2010-03-01 15:34:35 +0100966 u8 bit_read, bit_write;
967 unsigned long tmp;
968 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100970 /*
971 * If this warning triggers extend the direct_access_msrs list at the
972 * beginning of the file
973 */
974 WARN_ON(!valid_msr_intercept(msr));
975
Joerg Roedel455716f2010-03-01 15:34:35 +0100976 offset = svm_msrpm_offset(msr);
977 bit_read = 2 * (msr & 0x0f);
978 bit_write = 2 * (msr & 0x0f) + 1;
979 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980
Joerg Roedel455716f2010-03-01 15:34:35 +0100981 BUG_ON(offset == MSR_INVALID);
982
983 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
984 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
985
986 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987}
988
Joerg Roedelf65c2292008-02-13 18:58:46 +0100989static void svm_vcpu_init_msrpm(u32 *msrpm)
990{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100991 int i;
992
Joerg Roedelf65c2292008-02-13 18:58:46 +0100993 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
994
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100995 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
996 if (!direct_access_msrs[i].always)
997 continue;
998
999 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
1000 }
Joerg Roedelf65c2292008-02-13 18:58:46 +01001001}
1002
Joerg Roedel323c3d82010-03-01 15:34:37 +01001003static void add_msr_offset(u32 offset)
1004{
1005 int i;
1006
1007 for (i = 0; i < MSRPM_OFFSETS; ++i) {
1008
1009 /* Offset already in list? */
1010 if (msrpm_offsets[i] == offset)
1011 return;
1012
1013 /* Slot used by another offset? */
1014 if (msrpm_offsets[i] != MSR_INVALID)
1015 continue;
1016
1017 /* Add offset to list */
1018 msrpm_offsets[i] = offset;
1019
1020 return;
1021 }
1022
1023 /*
1024 * If this BUG triggers the msrpm_offsets table has an overflow. Just
1025 * increase MSRPM_OFFSETS in this case.
1026 */
1027 BUG();
1028}
1029
1030static void init_msrpm_offsets(void)
1031{
1032 int i;
1033
1034 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
1035
1036 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1037 u32 offset;
1038
1039 offset = svm_msrpm_offset(direct_access_msrs[i].index);
1040 BUG_ON(offset == MSR_INVALID);
1041
1042 add_msr_offset(offset);
1043 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044}
1045
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001046static void svm_enable_lbrv(struct vcpu_svm *svm)
1047{
1048 u32 *msrpm = svm->msrpm;
1049
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001050 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001051 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
1052 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
1053 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
1054 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
1055}
1056
1057static void svm_disable_lbrv(struct vcpu_svm *svm)
1058{
1059 u32 *msrpm = svm->msrpm;
1060
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001061 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001062 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
1063 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
1064 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
1065 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
1066}
1067
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001068static void disable_nmi_singlestep(struct vcpu_svm *svm)
1069{
1070 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001071
Ladi Prosekab2f4d732017-06-21 09:06:58 +02001072 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
1073 /* Clear our flags if they were not set by the guest */
1074 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1075 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
1076 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1077 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
1078 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001079}
1080
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001081/* Note:
1082 * This hash table is used to map VM_ID to a struct kvm_arch,
1083 * when handling AMD IOMMU GALOG notification to schedule in
1084 * a particular vCPU.
1085 */
1086#define SVM_VM_DATA_HASH_BITS 8
David Hildenbrand681bcea2017-01-24 22:21:16 +01001087static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001088static u32 next_vm_id = 0;
1089static bool next_vm_id_wrapped = 0;
David Hildenbrand681bcea2017-01-24 22:21:16 +01001090static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001091
1092/* Note:
1093 * This function is called from IOMMU driver to notify
1094 * SVM to schedule in a particular vCPU of a particular VM.
1095 */
1096static int avic_ga_log_notifier(u32 ga_tag)
1097{
1098 unsigned long flags;
1099 struct kvm_arch *ka = NULL;
1100 struct kvm_vcpu *vcpu = NULL;
1101 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
1102 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
1103
1104 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
1105
1106 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1107 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1108 struct kvm *kvm = container_of(ka, struct kvm, arch);
1109 struct kvm_arch *vm_data = &kvm->arch;
1110
1111 if (vm_data->avic_vm_id != vm_id)
1112 continue;
1113 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
1114 break;
1115 }
1116 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1117
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001118 /* Note:
1119 * At this point, the IOMMU should have already set the pending
1120 * bit in the vAPIC backing page. So, we just need to schedule
1121 * in the vcpu.
1122 */
Paolo Bonzini1cf53582017-10-10 12:51:56 +02001123 if (vcpu)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001124 kvm_vcpu_wake_up(vcpu);
1125
1126 return 0;
1127}
1128
Brijesh Singhe9df0942017-12-04 10:57:33 -06001129static __init int sev_hardware_setup(void)
1130{
1131 struct sev_user_data_status *status;
1132 int rc;
1133
1134 /* Maximum number of encrypted guests supported simultaneously */
1135 max_sev_asid = cpuid_ecx(0x8000001F);
1136
1137 if (!max_sev_asid)
1138 return 1;
1139
Brijesh Singh1654efc2017-12-04 10:57:34 -06001140 /* Minimum ASID value that should be used for SEV guest */
1141 min_sev_asid = cpuid_edx(0x8000001F);
1142
1143 /* Initialize SEV ASID bitmap */
1144 sev_asid_bitmap = kcalloc(BITS_TO_LONGS(max_sev_asid),
1145 sizeof(unsigned long), GFP_KERNEL);
1146 if (!sev_asid_bitmap)
1147 return 1;
1148
Brijesh Singhe9df0942017-12-04 10:57:33 -06001149 status = kmalloc(sizeof(*status), GFP_KERNEL);
1150 if (!status)
1151 return 1;
1152
1153 /*
1154 * Check SEV platform status.
1155 *
1156 * PLATFORM_STATUS can be called in any state, if we failed to query
1157 * the PLATFORM status then either PSP firmware does not support SEV
1158 * feature or SEV firmware is dead.
1159 */
1160 rc = sev_platform_status(status, NULL);
1161 if (rc)
1162 goto err;
1163
1164 pr_info("SEV supported\n");
1165
1166err:
1167 kfree(status);
1168 return rc;
1169}
1170
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171static __init int svm_hardware_setup(void)
1172{
1173 int cpu;
1174 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001175 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176 int r;
1177
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
1179
1180 if (!iopm_pages)
1181 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +03001182
1183 iopm_va = page_address(iopm_pages);
1184 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
1186
Joerg Roedel323c3d82010-03-01 15:34:37 +01001187 init_msrpm_offsets();
1188
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001189 if (boot_cpu_has(X86_FEATURE_NX))
1190 kvm_enable_efer_bits(EFER_NX);
1191
Alexander Graf1b2fd702009-02-02 16:23:51 +01001192 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
1193 kvm_enable_efer_bits(EFER_FFXSR);
1194
Joerg Roedel92a1f122011-03-25 09:44:51 +01001195 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +01001196 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +08001197 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1198 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +01001199 }
1200
Alexander Graf236de052008-11-25 20:17:10 +01001201 if (nested) {
1202 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +02001203 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +01001204 }
1205
Brijesh Singhe9df0942017-12-04 10:57:33 -06001206 if (sev) {
1207 if (boot_cpu_has(X86_FEATURE_SEV) &&
1208 IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
1209 r = sev_hardware_setup();
1210 if (r)
1211 sev = false;
1212 } else {
1213 sev = false;
1214 }
1215 }
1216
Zachary Amsden3230bb42009-09-29 11:38:37 -10001217 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218 r = svm_cpu_init(cpu);
1219 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +01001220 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +01001222
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001223 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001224 npt_enabled = false;
1225
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001226 if (npt_enabled && !npt) {
1227 printk(KERN_INFO "kvm: Nested Paging disabled\n");
1228 npt_enabled = false;
1229 }
1230
Joerg Roedel18552672008-02-07 13:47:41 +01001231 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001232 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +01001233 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +02001234 } else
1235 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001236
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001237 if (avic) {
1238 if (!npt_enabled ||
1239 !boot_cpu_has(X86_FEATURE_AVIC) ||
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001240 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001241 avic = false;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001242 } else {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001243 pr_info("AVIC enabled\n");
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001244
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001245 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1246 }
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001247 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001248
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001249 if (vls) {
1250 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001251 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001252 !IS_ENABLED(CONFIG_X86_64)) {
1253 vls = false;
1254 } else {
1255 pr_info("Virtual VMLOAD VMSAVE supported\n");
1256 }
1257 }
1258
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001259 if (vgif) {
1260 if (!boot_cpu_has(X86_FEATURE_VGIF))
1261 vgif = false;
1262 else
1263 pr_info("Virtual GIF supported\n");
1264 }
1265
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 return 0;
1267
Joerg Roedelf65c2292008-02-13 18:58:46 +01001268err:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1270 iopm_base = 0;
1271 return r;
1272}
1273
1274static __exit void svm_hardware_unsetup(void)
1275{
Joerg Roedel0da1db752008-07-02 16:02:11 +02001276 int cpu;
1277
Brijesh Singh1654efc2017-12-04 10:57:34 -06001278 if (svm_sev_enabled())
1279 kfree(sev_asid_bitmap);
1280
Zachary Amsden3230bb42009-09-29 11:38:37 -10001281 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +02001282 svm_cpu_uninit(cpu);
1283
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001285 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286}
1287
1288static void init_seg(struct vmcb_seg *seg)
1289{
1290 seg->selector = 0;
1291 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001292 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293 seg->limit = 0xffff;
1294 seg->base = 0;
1295}
1296
1297static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1298{
1299 seg->selector = 0;
1300 seg->attrib = SVM_SELECTOR_P_MASK | type;
1301 seg->limit = 0xffff;
1302 seg->base = 0;
1303}
1304
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001305static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1306{
1307 struct vcpu_svm *svm = to_svm(vcpu);
1308 u64 g_tsc_offset = 0;
1309
Joerg Roedel20307532010-11-29 17:51:48 +01001310 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001311 g_tsc_offset = svm->vmcb->control.tsc_offset -
1312 svm->nested.hsave->control.tsc_offset;
1313 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001314 } else
1315 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1316 svm->vmcb->control.tsc_offset,
1317 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001318
1319 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001320
1321 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001322}
1323
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001324static void avic_init_vmcb(struct vcpu_svm *svm)
1325{
1326 struct vmcb *vmcb = svm->vmcb;
1327 struct kvm_arch *vm_data = &svm->vcpu.kvm->arch;
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001328 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
1329 phys_addr_t lpa = __sme_set(page_to_phys(vm_data->avic_logical_id_table_page));
1330 phys_addr_t ppa = __sme_set(page_to_phys(vm_data->avic_physical_id_table_page));
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001331
1332 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1333 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1334 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1335 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1336 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001337}
1338
Paolo Bonzini56908912015-10-19 11:30:19 +02001339static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340{
Joerg Roedele6101a92008-02-13 18:58:45 +01001341 struct vmcb_control_area *control = &svm->vmcb->control;
1342 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001344 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001345
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001346 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1347 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1348 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1349 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1350 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1351 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001352 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1353 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354
Paolo Bonzini5315c712014-03-03 13:08:29 +01001355 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356
Joerg Roedel18c918c2010-11-30 18:03:59 +01001357 set_exception_intercept(svm, PF_VECTOR);
1358 set_exception_intercept(svm, UD_VECTOR);
1359 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001360 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001361 set_exception_intercept(svm, DB_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001363 set_intercept(svm, INTERCEPT_INTR);
1364 set_intercept(svm, INTERCEPT_NMI);
1365 set_intercept(svm, INTERCEPT_SMI);
1366 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001367 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001368 set_intercept(svm, INTERCEPT_CPUID);
1369 set_intercept(svm, INTERCEPT_INVD);
1370 set_intercept(svm, INTERCEPT_HLT);
1371 set_intercept(svm, INTERCEPT_INVLPG);
1372 set_intercept(svm, INTERCEPT_INVLPGA);
1373 set_intercept(svm, INTERCEPT_IOIO_PROT);
1374 set_intercept(svm, INTERCEPT_MSR_PROT);
1375 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1376 set_intercept(svm, INTERCEPT_SHUTDOWN);
1377 set_intercept(svm, INTERCEPT_VMRUN);
1378 set_intercept(svm, INTERCEPT_VMMCALL);
1379 set_intercept(svm, INTERCEPT_VMLOAD);
1380 set_intercept(svm, INTERCEPT_VMSAVE);
1381 set_intercept(svm, INTERCEPT_STGI);
1382 set_intercept(svm, INTERCEPT_CLGI);
1383 set_intercept(svm, INTERCEPT_SKINIT);
1384 set_intercept(svm, INTERCEPT_WBINVD);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001385 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001387 if (!kvm_mwait_in_guest()) {
1388 set_intercept(svm, INTERCEPT_MONITOR);
1389 set_intercept(svm, INTERCEPT_MWAIT);
1390 }
1391
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001392 control->iopm_base_pa = __sme_set(iopm_base);
1393 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 control->int_ctl = V_INTR_MASKING_MASK;
1395
1396 init_seg(&save->es);
1397 init_seg(&save->ss);
1398 init_seg(&save->ds);
1399 init_seg(&save->fs);
1400 init_seg(&save->gs);
1401
1402 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001403 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404 /* Executable/Readable Code Segment */
1405 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1406 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1407 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408
1409 save->gdtr.limit = 0xffff;
1410 save->idtr.limit = 0xffff;
1411
1412 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1413 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1414
Paolo Bonzini56908912015-10-19 11:30:19 +02001415 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001416 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001417 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001419 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420
Joerg Roedele0231712010-02-24 18:59:10 +01001421 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001422 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001423 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001425 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001426 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001427
Rusty Russell66aee912007-07-17 23:34:16 +10001428 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001430
1431 if (npt_enabled) {
1432 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001433 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001434 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001435 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001436 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1437 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001438 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001439 save->cr3 = 0;
1440 save->cr4 = 0;
1441 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001442 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001443
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001444 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001445 svm->vcpu.arch.hflags = 0;
1446
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001447 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001448 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001449 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001450 }
1451
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001452 if (kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001453 avic_init_vmcb(svm);
1454
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001455 /*
1456 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1457 * in VMCB and clear intercepts to avoid #VMEXIT.
1458 */
1459 if (vls) {
1460 clr_intercept(svm, INTERCEPT_VMLOAD);
1461 clr_intercept(svm, INTERCEPT_VMSAVE);
1462 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1463 }
1464
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001465 if (vgif) {
1466 clr_intercept(svm, INTERCEPT_STGI);
1467 clr_intercept(svm, INTERCEPT_CLGI);
1468 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1469 }
1470
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001471 if (sev_guest(svm->vcpu.kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001472 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001473 clr_exception_intercept(svm, UD_VECTOR);
1474 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001475
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001476 mark_all_dirty(svm->vmcb);
1477
Joerg Roedel2af91942009-08-07 11:49:28 +02001478 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001479
1480}
1481
Dan Carpenterd3e7dec2017-05-18 10:38:53 +03001482static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1483 unsigned int index)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001484{
1485 u64 *avic_physical_id_table;
1486 struct kvm_arch *vm_data = &vcpu->kvm->arch;
1487
1488 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1489 return NULL;
1490
1491 avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page);
1492
1493 return &avic_physical_id_table[index];
1494}
1495
1496/**
1497 * Note:
1498 * AVIC hardware walks the nested page table to check permissions,
1499 * but does not use the SPA address specified in the leaf page
1500 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1501 * field of the VMCB. Therefore, we set up the
1502 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1503 */
1504static int avic_init_access_page(struct kvm_vcpu *vcpu)
1505{
1506 struct kvm *kvm = vcpu->kvm;
1507 int ret;
1508
1509 if (kvm->arch.apic_access_page_done)
1510 return 0;
1511
1512 ret = x86_set_memory_region(kvm,
1513 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1514 APIC_DEFAULT_PHYS_BASE,
1515 PAGE_SIZE);
1516 if (ret)
1517 return ret;
1518
1519 kvm->arch.apic_access_page_done = true;
1520 return 0;
1521}
1522
1523static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1524{
1525 int ret;
1526 u64 *entry, new_entry;
1527 int id = vcpu->vcpu_id;
1528 struct vcpu_svm *svm = to_svm(vcpu);
1529
1530 ret = avic_init_access_page(vcpu);
1531 if (ret)
1532 return ret;
1533
1534 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1535 return -EINVAL;
1536
1537 if (!svm->vcpu.arch.apic->regs)
1538 return -EINVAL;
1539
1540 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1541
1542 /* Setting AVIC backing page address in the phy APIC ID table */
1543 entry = avic_get_physical_id_entry(vcpu, id);
1544 if (!entry)
1545 return -EINVAL;
1546
1547 new_entry = READ_ONCE(*entry);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001548 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
1549 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1550 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001551 WRITE_ONCE(*entry, new_entry);
1552
1553 svm->avic_physical_id_cache = entry;
1554
1555 return 0;
1556}
1557
Brijesh Singh1654efc2017-12-04 10:57:34 -06001558static void __sev_asid_free(int asid)
1559{
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001560 struct svm_cpu_data *sd;
1561 int cpu, pos;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001562
1563 pos = asid - 1;
1564 clear_bit(pos, sev_asid_bitmap);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001565
1566 for_each_possible_cpu(cpu) {
1567 sd = per_cpu(svm_data, cpu);
1568 sd->sev_vmcbs[pos] = NULL;
1569 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001570}
1571
1572static void sev_asid_free(struct kvm *kvm)
1573{
1574 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1575
1576 __sev_asid_free(sev->asid);
1577}
1578
Brijesh Singh59414c92017-12-04 10:57:35 -06001579static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
1580{
1581 struct sev_data_decommission *decommission;
1582 struct sev_data_deactivate *data;
1583
1584 if (!handle)
1585 return;
1586
1587 data = kzalloc(sizeof(*data), GFP_KERNEL);
1588 if (!data)
1589 return;
1590
1591 /* deactivate handle */
1592 data->handle = handle;
1593 sev_guest_deactivate(data, NULL);
1594
1595 wbinvd_on_all_cpus();
1596 sev_guest_df_flush(NULL);
1597 kfree(data);
1598
1599 decommission = kzalloc(sizeof(*decommission), GFP_KERNEL);
1600 if (!decommission)
1601 return;
1602
1603 /* decommission handle */
1604 decommission->handle = handle;
1605 sev_guest_decommission(decommission, NULL);
1606
1607 kfree(decommission);
1608}
1609
Brijesh Singh89c50582017-12-04 10:57:35 -06001610static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
1611 unsigned long ulen, unsigned long *n,
1612 int write)
1613{
1614 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1615 unsigned long npages, npinned, size;
1616 unsigned long locked, lock_limit;
1617 struct page **pages;
1618 int first, last;
1619
1620 /* Calculate number of pages. */
1621 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
1622 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
1623 npages = (last - first + 1);
1624
1625 locked = sev->pages_locked + npages;
1626 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1627 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
1628 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
1629 return NULL;
1630 }
1631
1632 /* Avoid using vmalloc for smaller buffers. */
1633 size = npages * sizeof(struct page *);
1634 if (size > PAGE_SIZE)
1635 pages = vmalloc(size);
1636 else
1637 pages = kmalloc(size, GFP_KERNEL);
1638
1639 if (!pages)
1640 return NULL;
1641
1642 /* Pin the user virtual address. */
1643 npinned = get_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
1644 if (npinned != npages) {
1645 pr_err("SEV: Failure locking %lu pages.\n", npages);
1646 goto err;
1647 }
1648
1649 *n = npages;
1650 sev->pages_locked = locked;
1651
1652 return pages;
1653
1654err:
1655 if (npinned > 0)
1656 release_pages(pages, npinned);
1657
1658 kvfree(pages);
1659 return NULL;
1660}
1661
1662static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
1663 unsigned long npages)
1664{
1665 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1666
1667 release_pages(pages, npages);
1668 kvfree(pages);
1669 sev->pages_locked -= npages;
1670}
1671
1672static void sev_clflush_pages(struct page *pages[], unsigned long npages)
1673{
1674 uint8_t *page_virtual;
1675 unsigned long i;
1676
1677 if (npages == 0 || pages == NULL)
1678 return;
1679
1680 for (i = 0; i < npages; i++) {
1681 page_virtual = kmap_atomic(pages[i]);
1682 clflush_cache_range(page_virtual, PAGE_SIZE);
1683 kunmap_atomic(page_virtual);
1684 }
1685}
1686
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001687static void __unregister_enc_region_locked(struct kvm *kvm,
1688 struct enc_region *region)
1689{
1690 /*
1691 * The guest may change the memory encryption attribute from C=0 -> C=1
1692 * or vice versa for this memory range. Lets make sure caches are
1693 * flushed to ensure that guest data gets written into memory with
1694 * correct C-bit.
1695 */
1696 sev_clflush_pages(region->pages, region->npages);
1697
1698 sev_unpin_memory(kvm, region->pages, region->npages);
1699 list_del(&region->list);
1700 kfree(region);
1701}
1702
Brijesh Singh1654efc2017-12-04 10:57:34 -06001703static void sev_vm_destroy(struct kvm *kvm)
1704{
Brijesh Singh59414c92017-12-04 10:57:35 -06001705 struct kvm_sev_info *sev = &kvm->arch.sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001706 struct list_head *head = &sev->regions_list;
1707 struct list_head *pos, *q;
Brijesh Singh59414c92017-12-04 10:57:35 -06001708
Brijesh Singh1654efc2017-12-04 10:57:34 -06001709 if (!sev_guest(kvm))
1710 return;
1711
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001712 mutex_lock(&kvm->lock);
1713
1714 /*
1715 * if userspace was terminated before unregistering the memory regions
1716 * then lets unpin all the registered memory.
1717 */
1718 if (!list_empty(head)) {
1719 list_for_each_safe(pos, q, head) {
1720 __unregister_enc_region_locked(kvm,
1721 list_entry(pos, struct enc_region, list));
1722 }
1723 }
1724
1725 mutex_unlock(&kvm->lock);
1726
Brijesh Singh59414c92017-12-04 10:57:35 -06001727 sev_unbind_asid(kvm, sev->handle);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001728 sev_asid_free(kvm);
1729}
1730
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001731static void avic_vm_destroy(struct kvm *kvm)
1732{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001733 unsigned long flags;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001734 struct kvm_arch *vm_data = &kvm->arch;
1735
Dmitry Vyukov3863dff2017-01-24 14:06:48 +01001736 if (!avic)
1737 return;
1738
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001739 if (vm_data->avic_logical_id_table_page)
1740 __free_page(vm_data->avic_logical_id_table_page);
1741 if (vm_data->avic_physical_id_table_page)
1742 __free_page(vm_data->avic_physical_id_table_page);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001743
1744 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1745 hash_del(&vm_data->hnode);
1746 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001747}
1748
Brijesh Singh1654efc2017-12-04 10:57:34 -06001749static void svm_vm_destroy(struct kvm *kvm)
1750{
1751 avic_vm_destroy(kvm);
1752 sev_vm_destroy(kvm);
1753}
1754
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001755static int avic_vm_init(struct kvm *kvm)
1756{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001757 unsigned long flags;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001758 int err = -ENOMEM;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001759 struct kvm_arch *vm_data = &kvm->arch;
1760 struct page *p_page;
1761 struct page *l_page;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001762 struct kvm_arch *ka;
1763 u32 vm_id;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001764
1765 if (!avic)
1766 return 0;
1767
1768 /* Allocating physical APIC ID table (4KB) */
1769 p_page = alloc_page(GFP_KERNEL);
1770 if (!p_page)
1771 goto free_avic;
1772
1773 vm_data->avic_physical_id_table_page = p_page;
1774 clear_page(page_address(p_page));
1775
1776 /* Allocating logical APIC ID table (4KB) */
1777 l_page = alloc_page(GFP_KERNEL);
1778 if (!l_page)
1779 goto free_avic;
1780
1781 vm_data->avic_logical_id_table_page = l_page;
1782 clear_page(page_address(l_page));
1783
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001784 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001785 again:
1786 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
1787 if (vm_id == 0) { /* id is 1-based, zero is not okay */
1788 next_vm_id_wrapped = 1;
1789 goto again;
1790 }
1791 /* Is it still in use? Only possible if wrapped at least once */
1792 if (next_vm_id_wrapped) {
1793 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1794 struct kvm *k2 = container_of(ka, struct kvm, arch);
1795 struct kvm_arch *vd2 = &k2->arch;
1796 if (vd2->avic_vm_id == vm_id)
1797 goto again;
1798 }
1799 }
1800 vm_data->avic_vm_id = vm_id;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001801 hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id);
1802 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1803
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001804 return 0;
1805
1806free_avic:
1807 avic_vm_destroy(kvm);
1808 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809}
1810
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001811static inline int
1812avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001813{
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001814 int ret = 0;
1815 unsigned long flags;
1816 struct amd_svm_iommu_ir *ir;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001817 struct vcpu_svm *svm = to_svm(vcpu);
1818
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001819 if (!kvm_arch_has_assigned_device(vcpu->kvm))
1820 return 0;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001821
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001822 /*
1823 * Here, we go through the per-vcpu ir_list to update all existing
1824 * interrupt remapping table entry targeting this vcpu.
1825 */
1826 spin_lock_irqsave(&svm->ir_list_lock, flags);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001827
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001828 if (list_empty(&svm->ir_list))
1829 goto out;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001830
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001831 list_for_each_entry(ir, &svm->ir_list, node) {
1832 ret = amd_iommu_update_ga(cpu, r, ir->data);
1833 if (ret)
1834 break;
1835 }
1836out:
1837 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
1838 return ret;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001839}
1840
1841static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1842{
1843 u64 entry;
1844 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05001845 int h_physical_id = kvm_cpu_get_apicid(cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001846 struct vcpu_svm *svm = to_svm(vcpu);
1847
1848 if (!kvm_vcpu_apicv_active(vcpu))
1849 return;
1850
1851 if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
1852 return;
1853
1854 entry = READ_ONCE(*(svm->avic_physical_id_cache));
1855 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
1856
1857 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
1858 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
1859
1860 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1861 if (svm->avic_is_running)
1862 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1863
1864 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001865 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
1866 svm->avic_is_running);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001867}
1868
1869static void avic_vcpu_put(struct kvm_vcpu *vcpu)
1870{
1871 u64 entry;
1872 struct vcpu_svm *svm = to_svm(vcpu);
1873
1874 if (!kvm_vcpu_apicv_active(vcpu))
1875 return;
1876
1877 entry = READ_ONCE(*(svm->avic_physical_id_cache));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001878 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
1879 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
1880
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001881 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1882 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883}
1884
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001885/**
1886 * This function is called during VCPU halt/unhalt.
1887 */
1888static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
1889{
1890 struct vcpu_svm *svm = to_svm(vcpu);
1891
1892 svm->avic_is_running = is_run;
1893 if (is_run)
1894 avic_vcpu_load(vcpu, vcpu->cpu);
1895 else
1896 avic_vcpu_put(vcpu);
1897}
1898
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001899static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001900{
1901 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001902 u32 dummy;
1903 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001904
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001905 svm->spec_ctrl = 0;
1906
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001907 if (!init_event) {
1908 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1909 MSR_IA32_APICBASE_ENABLE;
1910 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1911 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1912 }
Paolo Bonzini56908912015-10-19 11:30:19 +02001913 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001914
Yu Zhange911eb32017-08-24 20:27:52 +08001915 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001916 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001917
1918 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1919 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001920}
1921
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001922static int avic_init_vcpu(struct vcpu_svm *svm)
1923{
1924 int ret;
1925
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001926 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001927 return 0;
1928
1929 ret = avic_init_backing_page(&svm->vcpu);
1930 if (ret)
1931 return ret;
1932
1933 INIT_LIST_HEAD(&svm->ir_list);
1934 spin_lock_init(&svm->ir_list_lock);
1935
1936 return ret;
1937}
1938
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001939static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001941 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001943 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001944 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001945 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001946 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947
Rusty Russellc16f8622007-07-30 21:12:19 +10001948 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001949 if (!svm) {
1950 err = -ENOMEM;
1951 goto out;
1952 }
1953
1954 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1955 if (err)
1956 goto free_svm;
1957
Joerg Roedelf65c2292008-02-13 18:58:46 +01001958 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001959 page = alloc_page(GFP_KERNEL);
1960 if (!page)
1961 goto uninit;
1962
Joerg Roedelf65c2292008-02-13 18:58:46 +01001963 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1964 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001965 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001966
1967 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1968 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001969 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001970
Alexander Grafb286d5d2008-11-25 20:17:05 +01001971 hsave_page = alloc_page(GFP_KERNEL);
1972 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001973 goto free_page3;
1974
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001975 err = avic_init_vcpu(svm);
1976 if (err)
1977 goto free_page4;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001978
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001979 /* We initialize this flag to true to make sure that the is_running
1980 * bit would be set the first time the vcpu is loaded.
1981 */
1982 svm->avic_is_running = true;
1983
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001984 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001985
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001986 svm->msrpm = page_address(msrpm_pages);
1987 svm_vcpu_init_msrpm(svm->msrpm);
1988
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001989 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001990 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001991
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001992 svm->vmcb = page_address(page);
1993 clear_page(svm->vmcb);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001994 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001995 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02001996 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001998 svm_init_osvw(&svm->vcpu);
1999
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002000 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08002001
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002002free_page4:
2003 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002004free_page3:
2005 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
2006free_page2:
2007 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
2008free_page1:
2009 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002010uninit:
2011 kvm_vcpu_uninit(&svm->vcpu);
2012free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10002013 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002014out:
2015 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002016}
2017
2018static void svm_free_vcpu(struct kvm_vcpu *vcpu)
2019{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002020 struct vcpu_svm *svm = to_svm(vcpu);
2021
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002022 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01002023 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002024 __free_page(virt_to_page(svm->nested.hsave));
2025 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002026 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002027 kmem_cache_free(kvm_vcpu_cache, svm);
Ashok Raj15d45072018-02-01 22:59:43 +01002028 /*
2029 * The vmcb page can be recycled, causing a false negative in
2030 * svm_vcpu_load(). So do a full IBPB now.
2031 */
2032 indirect_branch_prediction_barrier();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002033}
2034
Avi Kivity15ad7142007-07-11 18:17:21 +03002035static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002036{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002037 struct vcpu_svm *svm = to_svm(vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01002038 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002039 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02002040
Avi Kivity0cc50642007-03-25 12:07:27 +02002041 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002042 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002043 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02002044 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002045
Avi Kivity82ca2d12010-10-21 12:20:34 +02002046#ifdef CONFIG_X86_64
2047 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
2048#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02002049 savesegment(fs, svm->host.fs);
2050 savesegment(gs, svm->host.gs);
2051 svm->host.ldt = kvm_read_ldt();
2052
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002053 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002054 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002055
Haozhong Zhangad7218832015-10-20 15:39:02 +08002056 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
2057 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2058 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
2059 __this_cpu_write(current_tsc_ratio, tsc_ratio);
2060 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
2061 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002062 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01002063 /* This assumes that the kernel never uses MSR_TSC_AUX */
2064 if (static_cpu_has(X86_FEATURE_RDTSCP))
2065 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002066
Ashok Raj15d45072018-02-01 22:59:43 +01002067 if (sd->current_vmcb != svm->vmcb) {
2068 sd->current_vmcb = svm->vmcb;
2069 indirect_branch_prediction_barrier();
2070 }
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002071 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002072}
2073
2074static void svm_vcpu_put(struct kvm_vcpu *vcpu)
2075{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002076 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002077 int i;
2078
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002079 avic_vcpu_put(vcpu);
2080
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002081 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02002082 kvm_load_ldt(svm->host.ldt);
2083#ifdef CONFIG_X86_64
2084 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07002085 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01002086 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02002087#else
Avi Kivity831ca602011-03-08 16:09:51 +02002088#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02002089 loadsegment(gs, svm->host.gs);
2090#endif
Avi Kivity831ca602011-03-08 16:09:51 +02002091#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002092 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002093 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094}
2095
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002096static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
2097{
2098 avic_set_running(vcpu, false);
2099}
2100
2101static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
2102{
2103 avic_set_running(vcpu, true);
2104}
2105
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
2107{
Ladi Prosek9b611742017-06-21 09:06:59 +02002108 struct vcpu_svm *svm = to_svm(vcpu);
2109 unsigned long rflags = svm->vmcb->save.rflags;
2110
2111 if (svm->nmi_singlestep) {
2112 /* Hide our flags if they were not set by the guest */
2113 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
2114 rflags &= ~X86_EFLAGS_TF;
2115 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
2116 rflags &= ~X86_EFLAGS_RF;
2117 }
2118 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119}
2120
2121static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2122{
Ladi Prosek9b611742017-06-21 09:06:59 +02002123 if (to_svm(vcpu)->nmi_singlestep)
2124 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2125
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002126 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02002127 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002128 * (caused by either a task switch or an inter-privilege IRET),
2129 * so we do not need to update the CPL here.
2130 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002131 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002132}
2133
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002134static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
2135{
2136 switch (reg) {
2137 case VCPU_EXREG_PDPTR:
2138 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002139 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002140 break;
2141 default:
2142 BUG();
2143 }
2144}
2145
Alexander Graff0b85052008-11-25 20:17:01 +01002146static void svm_set_vintr(struct vcpu_svm *svm)
2147{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002148 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002149}
2150
2151static void svm_clear_vintr(struct vcpu_svm *svm)
2152{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002153 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002154}
2155
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
2157{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002158 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159
2160 switch (seg) {
2161 case VCPU_SREG_CS: return &save->cs;
2162 case VCPU_SREG_DS: return &save->ds;
2163 case VCPU_SREG_ES: return &save->es;
2164 case VCPU_SREG_FS: return &save->fs;
2165 case VCPU_SREG_GS: return &save->gs;
2166 case VCPU_SREG_SS: return &save->ss;
2167 case VCPU_SREG_TR: return &save->tr;
2168 case VCPU_SREG_LDTR: return &save->ldtr;
2169 }
2170 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00002171 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172}
2173
2174static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2175{
2176 struct vmcb_seg *s = svm_seg(vcpu, seg);
2177
2178 return s->base;
2179}
2180
2181static void svm_get_segment(struct kvm_vcpu *vcpu,
2182 struct kvm_segment *var, int seg)
2183{
2184 struct vmcb_seg *s = svm_seg(vcpu, seg);
2185
2186 var->base = s->base;
2187 var->limit = s->limit;
2188 var->selector = s->selector;
2189 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
2190 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
2191 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
2192 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
2193 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
2194 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
2195 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05302196
2197 /*
2198 * AMD CPUs circa 2014 track the G bit for all segments except CS.
2199 * However, the SVM spec states that the G bit is not observed by the
2200 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
2201 * So let's synthesize a legal G bit for all segments, this helps
2202 * running KVM nested. It also helps cross-vendor migration, because
2203 * Intel's vmentry has a check on the 'G' bit.
2204 */
2205 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00002206
Joerg Roedele0231712010-02-24 18:59:10 +01002207 /*
2208 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02002209 * for cross vendor migration purposes by "not present"
2210 */
Gioh Kim8eae9572017-05-30 15:24:45 +02002211 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02002212
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002213 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002214 case VCPU_SREG_TR:
2215 /*
2216 * Work around a bug where the busy flag in the tr selector
2217 * isn't exposed
2218 */
Amit Shahc0d09822008-10-27 09:04:18 +00002219 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002220 break;
2221 case VCPU_SREG_DS:
2222 case VCPU_SREG_ES:
2223 case VCPU_SREG_FS:
2224 case VCPU_SREG_GS:
2225 /*
2226 * The accessed bit must always be set in the segment
2227 * descriptor cache, although it can be cleared in the
2228 * descriptor, the cached bit always remains at 1. Since
2229 * Intel has a check on this, set it here to support
2230 * cross-vendor migration.
2231 */
2232 if (!var->unusable)
2233 var->type |= 0x1;
2234 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02002235 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01002236 /*
2237 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02002238 * descriptor is left as 1, although the whole segment has
2239 * been made unusable. Clear it here to pass an Intel VMX
2240 * entry check when cross vendor migrating.
2241 */
2242 if (var->unusable)
2243 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02002244 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02002245 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02002246 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002247 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002248}
2249
Izik Eidus2e4d2652008-03-24 19:38:34 +02002250static int svm_get_cpl(struct kvm_vcpu *vcpu)
2251{
2252 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
2253
2254 return save->cpl;
2255}
2256
Gleb Natapov89a27f42010-02-16 10:51:48 +02002257static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002258{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002259 struct vcpu_svm *svm = to_svm(vcpu);
2260
Gleb Natapov89a27f42010-02-16 10:51:48 +02002261 dt->size = svm->vmcb->save.idtr.limit;
2262 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263}
2264
Gleb Natapov89a27f42010-02-16 10:51:48 +02002265static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002267 struct vcpu_svm *svm = to_svm(vcpu);
2268
Gleb Natapov89a27f42010-02-16 10:51:48 +02002269 svm->vmcb->save.idtr.limit = dt->size;
2270 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002271 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272}
2273
Gleb Natapov89a27f42010-02-16 10:51:48 +02002274static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002276 struct vcpu_svm *svm = to_svm(vcpu);
2277
Gleb Natapov89a27f42010-02-16 10:51:48 +02002278 dt->size = svm->vmcb->save.gdtr.limit;
2279 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280}
2281
Gleb Natapov89a27f42010-02-16 10:51:48 +02002282static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002284 struct vcpu_svm *svm = to_svm(vcpu);
2285
Gleb Natapov89a27f42010-02-16 10:51:48 +02002286 svm->vmcb->save.gdtr.limit = dt->size;
2287 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002288 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289}
2290
Avi Kivitye8467fd2009-12-29 18:43:06 +02002291static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2292{
2293}
2294
Avi Kivityaff48ba2010-12-05 18:56:11 +02002295static void svm_decache_cr3(struct kvm_vcpu *vcpu)
2296{
2297}
2298
Anthony Liguori25c4c272007-04-27 09:29:21 +03002299static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002300{
2301}
2302
Avi Kivityd2251572010-01-06 10:55:27 +02002303static void update_cr0_intercept(struct vcpu_svm *svm)
2304{
2305 ulong gcr0 = svm->vcpu.arch.cr0;
2306 u64 *hcr0 = &svm->vmcb->save.cr0;
2307
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002308 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
2309 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
Avi Kivityd2251572010-01-06 10:55:27 +02002310
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002311 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002312
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002313 if (gcr0 == *hcr0) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002314 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
2315 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002316 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002317 set_cr_intercept(svm, INTERCEPT_CR0_READ);
2318 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002319 }
2320}
2321
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2323{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002324 struct vcpu_svm *svm = to_svm(vcpu);
2325
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002326#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002327 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002328 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002329 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002330 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331 }
2332
Mike Dayd77c26f2007-10-08 09:02:08 -04002333 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002334 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002335 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336 }
2337 }
2338#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002339 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002340
2341 if (!npt_enabled)
2342 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02002343
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02002344 /*
2345 * re-enable caching here because the QEMU bios
2346 * does not do it - this results in some delay at
2347 * reboot
2348 */
2349 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
2350 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002351 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002352 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002353 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354}
2355
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002356static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002358 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002359 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
2360
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002361 if (cr4 & X86_CR4_VMXE)
2362 return 1;
2363
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002364 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002365 svm_flush_tlb(vcpu, true);
Joerg Roedel6394b642008-04-09 14:15:29 +02002366
Joerg Roedelec077262008-04-09 14:15:28 +02002367 vcpu->arch.cr4 = cr4;
2368 if (!npt_enabled)
2369 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02002370 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02002371 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002372 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002373 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374}
2375
2376static void svm_set_segment(struct kvm_vcpu *vcpu,
2377 struct kvm_segment *var, int seg)
2378{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002379 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380 struct vmcb_seg *s = svm_seg(vcpu, seg);
2381
2382 s->base = var->base;
2383 s->limit = var->limit;
2384 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02002385 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2386 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2387 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2388 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2389 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2390 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2391 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2392 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002393
2394 /*
2395 * This is always accurate, except if SYSRET returned to a segment
2396 * with SS.DPL != 3. Intel does not have this quirk, and always
2397 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2398 * would entail passing the CPL to userspace and back.
2399 */
2400 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02002401 /* This is symmetric with svm_get_segment() */
2402 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403
Joerg Roedel060d0c92010-12-03 11:45:57 +01002404 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405}
2406
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01002407static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002409 struct vcpu_svm *svm = to_svm(vcpu);
2410
Joerg Roedel18c918c2010-11-30 18:03:59 +01002411 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03002412
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002413 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002414 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01002415 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002416 } else
2417 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03002418}
2419
Tejun Heo0fe1e002009-10-29 22:34:14 +09002420static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421{
Tejun Heo0fe1e002009-10-29 22:34:14 +09002422 if (sd->next_asid > sd->max_asid) {
2423 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06002424 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002425 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426 }
2427
Tejun Heo0fe1e002009-10-29 22:34:14 +09002428 svm->asid_generation = sd->asid_generation;
2429 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01002430
2431 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432}
2433
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002434static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2435{
2436 return to_svm(vcpu)->vmcb->save.dr6;
2437}
2438
2439static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2440{
2441 struct vcpu_svm *svm = to_svm(vcpu);
2442
2443 svm->vmcb->save.dr6 = value;
2444 mark_dirty(svm->vmcb, VMCB_DR);
2445}
2446
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002447static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2448{
2449 struct vcpu_svm *svm = to_svm(vcpu);
2450
2451 get_debugreg(vcpu->arch.db[0], 0);
2452 get_debugreg(vcpu->arch.db[1], 1);
2453 get_debugreg(vcpu->arch.db[2], 2);
2454 get_debugreg(vcpu->arch.db[3], 3);
2455 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2456 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2457
2458 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2459 set_dr_intercepts(svm);
2460}
2461
Gleb Natapov020df072010-04-13 10:05:23 +03002462static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002464 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002465
Gleb Natapov020df072010-04-13 10:05:23 +03002466 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01002467 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468}
2469
Avi Kivity851ba692009-08-24 11:10:17 +03002470static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002472 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002473 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002474
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002475 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002476 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2477 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002478 svm->vmcb->control.insn_len);
2479}
2480
2481static int npf_interception(struct vcpu_svm *svm)
2482{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002483 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Paolo Bonzinid0006532017-08-11 18:36:43 +02002484 u64 error_code = svm->vmcb->control.exit_info_1;
2485
2486 trace_kvm_page_fault(fault_address, error_code);
2487 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002488 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2489 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002490 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491}
2492
Avi Kivity851ba692009-08-24 11:10:17 +03002493static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002494{
Avi Kivity851ba692009-08-24 11:10:17 +03002495 struct kvm_run *kvm_run = svm->vcpu.run;
2496
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002497 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03002498 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02002499 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002500 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2501 return 1;
2502 }
Gleb Natapov44c11432009-05-11 13:35:52 +03002503
Jan Kiszka6be7d302009-10-18 13:24:54 +02002504 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02002505 disable_nmi_singlestep(svm);
Gleb Natapov44c11432009-05-11 13:35:52 +03002506 }
2507
2508 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002509 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002510 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2511 kvm_run->debug.arch.pc =
2512 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2513 kvm_run->debug.arch.exception = DB_VECTOR;
2514 return 0;
2515 }
2516
2517 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002518}
2519
Avi Kivity851ba692009-08-24 11:10:17 +03002520static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002521{
Avi Kivity851ba692009-08-24 11:10:17 +03002522 struct kvm_run *kvm_run = svm->vcpu.run;
2523
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002524 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2525 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2526 kvm_run->debug.arch.exception = BP_VECTOR;
2527 return 0;
2528}
2529
Avi Kivity851ba692009-08-24 11:10:17 +03002530static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002531{
2532 int er;
2533
Andre Przywara51d8b662010-12-21 11:12:02 +01002534 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02002535 if (er == EMULATE_USER_EXIT)
2536 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002537 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002538 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002539 return 1;
2540}
2541
Eric Northup54a20552015-11-03 18:03:53 +01002542static int ac_interception(struct vcpu_svm *svm)
2543{
2544 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2545 return 1;
2546}
2547
Joerg Roedel67ec6602010-05-17 14:43:35 +02002548static bool is_erratum_383(void)
2549{
2550 int err, i;
2551 u64 value;
2552
2553 if (!erratum_383_found)
2554 return false;
2555
2556 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2557 if (err)
2558 return false;
2559
2560 /* Bit 62 may or may not be set for this mce */
2561 value &= ~(1ULL << 62);
2562
2563 if (value != 0xb600000000010015ULL)
2564 return false;
2565
2566 /* Clear MCi_STATUS registers */
2567 for (i = 0; i < 6; ++i)
2568 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2569
2570 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2571 if (!err) {
2572 u32 low, high;
2573
2574 value &= ~(1ULL << 2);
2575 low = lower_32_bits(value);
2576 high = upper_32_bits(value);
2577
2578 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2579 }
2580
2581 /* Flush tlb to evict multi-match entries */
2582 __flush_tlb_all();
2583
2584 return true;
2585}
2586
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002587static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002588{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002589 if (is_erratum_383()) {
2590 /*
2591 * Erratum 383 triggered. Guest state is corrupt so kill the
2592 * guest.
2593 */
2594 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2595
Avi Kivitya8eeb042010-05-10 12:34:53 +03002596 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002597
2598 return;
2599 }
2600
Joerg Roedel53371b52008-04-09 14:15:30 +02002601 /*
2602 * On an #MC intercept the MCE handler is not called automatically in
2603 * the host. So do it by hand here.
2604 */
2605 asm volatile (
2606 "int $0x12\n");
2607 /* not sure if we ever come back to this point */
2608
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002609 return;
2610}
2611
2612static int mc_interception(struct vcpu_svm *svm)
2613{
Joerg Roedel53371b52008-04-09 14:15:30 +02002614 return 1;
2615}
2616
Avi Kivity851ba692009-08-24 11:10:17 +03002617static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002618{
Avi Kivity851ba692009-08-24 11:10:17 +03002619 struct kvm_run *kvm_run = svm->vcpu.run;
2620
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002621 /*
2622 * VMCB is undefined after a SHUTDOWN intercept
2623 * so reinitialize it.
2624 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002625 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002626 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002627
2628 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2629 return 0;
2630}
2631
Avi Kivity851ba692009-08-24 11:10:17 +03002632static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002634 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002635 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002636 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002637 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638
Rusty Russelle756fc62007-07-30 20:07:08 +10002639 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002640 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002641 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002642 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01002643 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002644
Avi Kivity039576c2007-03-20 12:46:50 +02002645 port = io_info >> 16;
2646 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002647 svm->next_rip = svm->vmcb->control.exit_info_2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002648 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002649
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002650 /*
2651 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
2652 * KVM_EXIT_DEBUG here.
2653 */
2654 if (in)
2655 return kvm_fast_pio_in(vcpu, size, port) && ret;
2656 else
2657 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658}
2659
Avi Kivity851ba692009-08-24 11:10:17 +03002660static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002661{
2662 return 1;
2663}
2664
Avi Kivity851ba692009-08-24 11:10:17 +03002665static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002666{
2667 ++svm->vcpu.stat.irq_exits;
2668 return 1;
2669}
2670
Avi Kivity851ba692009-08-24 11:10:17 +03002671static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672{
2673 return 1;
2674}
2675
Avi Kivity851ba692009-08-24 11:10:17 +03002676static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002678 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002679 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680}
2681
Avi Kivity851ba692009-08-24 11:10:17 +03002682static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002683{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002684 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002685 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002686}
2687
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002688static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2689{
2690 struct vcpu_svm *svm = to_svm(vcpu);
2691
2692 return svm->nested.nested_cr3;
2693}
2694
Avi Kivitye4e517b2011-07-28 11:36:17 +03002695static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2696{
2697 struct vcpu_svm *svm = to_svm(vcpu);
2698 u64 cr3 = svm->nested.nested_cr3;
2699 u64 pdpte;
2700 int ret;
2701
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002702 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002703 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002704 if (ret)
2705 return 0;
2706 return pdpte;
2707}
2708
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002709static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2710 unsigned long root)
2711{
2712 struct vcpu_svm *svm = to_svm(vcpu);
2713
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002714 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01002715 mark_dirty(svm->vmcb, VMCB_NPT);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002716 svm_flush_tlb(vcpu, true);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002717}
2718
Avi Kivity6389ee92010-11-29 16:12:30 +02002719static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2720 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002721{
2722 struct vcpu_svm *svm = to_svm(vcpu);
2723
Paolo Bonzini5e352512014-09-02 13:18:37 +02002724 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2725 /*
2726 * TODO: track the cause of the nested page fault, and
2727 * correctly fill in the high bits of exit_info_1.
2728 */
2729 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2730 svm->vmcb->control.exit_code_hi = 0;
2731 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2732 svm->vmcb->control.exit_info_2 = fault->address;
2733 }
2734
2735 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2736 svm->vmcb->control.exit_info_1 |= fault->error_code;
2737
2738 /*
2739 * The present bit is always zero for page structure faults on real
2740 * hardware.
2741 */
2742 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2743 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002744
2745 nested_svm_vmexit(svm);
2746}
2747
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002748static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002749{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002750 WARN_ON(mmu_is_nested(vcpu));
2751 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002752 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2753 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002754 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002755 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
Yu Zhang855feb62017-08-24 20:27:55 +08002756 vcpu->arch.mmu.shadow_root_level = get_npt_level(vcpu);
Xiao Guangrongc258b622015-08-05 12:04:24 +08002757 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002758 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002759}
2760
2761static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2762{
2763 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2764}
2765
Alexander Grafc0725422008-11-25 20:17:03 +01002766static int nested_svm_check_permissions(struct vcpu_svm *svm)
2767{
Dan Carpentere9196ce2017-05-18 10:39:53 +03002768 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
2769 !is_paging(&svm->vcpu)) {
Alexander Grafc0725422008-11-25 20:17:03 +01002770 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2771 return 1;
2772 }
2773
2774 if (svm->vmcb->save.cpl) {
2775 kvm_inject_gp(&svm->vcpu, 0);
2776 return 1;
2777 }
2778
Dan Carpentere9196ce2017-05-18 10:39:53 +03002779 return 0;
Alexander Grafc0725422008-11-25 20:17:03 +01002780}
2781
Alexander Grafcf74a782008-11-25 20:17:08 +01002782static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2783 bool has_error_code, u32 error_code)
2784{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002785 int vmexit;
2786
Joerg Roedel20307532010-11-29 17:51:48 +01002787 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002788 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002789
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002790 vmexit = nested_svm_intercept(svm);
2791 if (vmexit != NESTED_EXIT_DONE)
2792 return 0;
2793
Joerg Roedel0295ad72009-08-07 11:49:37 +02002794 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2795 svm->vmcb->control.exit_code_hi = 0;
2796 svm->vmcb->control.exit_info_1 = error_code;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002797
2798 /*
2799 * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception.
2800 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2801 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be
2802 * written only when inject_pending_event runs (DR6 would written here
2803 * too). This should be conditional on a new capability---if the
2804 * capability is disabled, kvm_multiple_exception would write the
2805 * ancillary information to CR2 or DR6, for backwards ABI-compatibility.
2806 */
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002807 if (svm->vcpu.arch.exception.nested_apf)
2808 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
2809 else
2810 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
Joerg Roedel0295ad72009-08-07 11:49:37 +02002811
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002812 svm->nested.exit_required = true;
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002813 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002814}
2815
Joerg Roedel8fe54652010-02-19 16:23:01 +01002816/* This function returns true if it is save to enable the irq window */
2817static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002818{
Joerg Roedel20307532010-11-29 17:51:48 +01002819 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002820 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002821
Joerg Roedel26666952009-08-07 11:49:46 +02002822 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002823 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002824
Joerg Roedel26666952009-08-07 11:49:46 +02002825 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002826 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002827
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002828 /*
2829 * if vmexit was already requested (by intercepted exception
2830 * for instance) do not overwrite it with "external interrupt"
2831 * vmexit.
2832 */
2833 if (svm->nested.exit_required)
2834 return false;
2835
Joerg Roedel197717d2010-02-24 18:59:19 +01002836 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2837 svm->vmcb->control.exit_info_1 = 0;
2838 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002839
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002840 if (svm->nested.intercept & 1ULL) {
2841 /*
2842 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002843 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002844 * #vmexit emulation might sleep. Only signal request for
2845 * the #vmexit here.
2846 */
2847 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002848 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002849 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002850 }
2851
Joerg Roedel8fe54652010-02-19 16:23:01 +01002852 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002853}
2854
Joerg Roedel887f5002010-02-24 18:59:12 +01002855/* This function returns true if it is save to enable the nmi window */
2856static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2857{
Joerg Roedel20307532010-11-29 17:51:48 +01002858 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002859 return true;
2860
2861 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2862 return true;
2863
2864 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2865 svm->nested.exit_required = true;
2866
2867 return false;
2868}
2869
Joerg Roedel7597f122010-02-19 16:23:00 +01002870static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002871{
2872 struct page *page;
2873
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002874 might_sleep();
2875
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002876 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002877 if (is_error_page(page))
2878 goto error;
2879
Joerg Roedel7597f122010-02-19 16:23:00 +01002880 *_page = page;
2881
2882 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002883
2884error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002885 kvm_inject_gp(&svm->vcpu, 0);
2886
2887 return NULL;
2888}
2889
Joerg Roedel7597f122010-02-19 16:23:00 +01002890static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002891{
Joerg Roedel7597f122010-02-19 16:23:00 +01002892 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002893 kvm_release_page_dirty(page);
2894}
2895
Joerg Roedelce2ac082010-03-01 15:34:39 +01002896static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002897{
Jan Kiszka9bf41832014-06-30 10:54:17 +02002898 unsigned port, size, iopm_len;
2899 u16 val, mask;
2900 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002901 u64 gpa;
2902
2903 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2904 return NESTED_EXIT_HOST;
2905
2906 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02002907 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2908 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002909 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02002910 start_bit = port % 8;
2911 iopm_len = (start_bit + size > 8) ? 2 : 1;
2912 mask = (0xf >> (4 - size)) << start_bit;
2913 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002914
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002915 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02002916 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002917
Jan Kiszka9bf41832014-06-30 10:54:17 +02002918 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002919}
2920
Joerg Roedeld2477822010-03-01 15:34:34 +01002921static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002922{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002923 u32 offset, msr, value;
2924 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002925
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002926 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002927 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002928
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002929 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2930 offset = svm_msrpm_offset(msr);
2931 write = svm->vmcb->control.exit_info_1 & 1;
2932 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002933
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002934 if (offset == MSR_INVALID)
2935 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002936
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002937 /* Offset is in 32 bit units but need in 8 bit units */
2938 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002939
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002940 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002941 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002942
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002943 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002944}
2945
Ladi Prosekab2f4d732017-06-21 09:06:58 +02002946/* DB exceptions for our internal use must not cause vmexit */
2947static int nested_svm_intercept_db(struct vcpu_svm *svm)
2948{
2949 unsigned long dr6;
2950
2951 /* if we're not singlestepping, it's not ours */
2952 if (!svm->nmi_singlestep)
2953 return NESTED_EXIT_DONE;
2954
2955 /* if it's not a singlestep exception, it's not ours */
2956 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
2957 return NESTED_EXIT_DONE;
2958 if (!(dr6 & DR6_BS))
2959 return NESTED_EXIT_DONE;
2960
2961 /* if the guest is singlestepping, it should get the vmexit */
2962 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
2963 disable_nmi_singlestep(svm);
2964 return NESTED_EXIT_DONE;
2965 }
2966
2967 /* it's ours, the nested hypervisor must not see this one */
2968 return NESTED_EXIT_HOST;
2969}
2970
Joerg Roedel410e4d52009-08-07 11:49:44 +02002971static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002972{
Alexander Grafcf74a782008-11-25 20:17:08 +01002973 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002974
Joerg Roedel410e4d52009-08-07 11:49:44 +02002975 switch (exit_code) {
2976 case SVM_EXIT_INTR:
2977 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002978 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002979 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002980 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002981 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002982 if (npt_enabled)
2983 return NESTED_EXIT_HOST;
2984 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002985 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002986 /* When we're shadowing, trap PFs, but not async PF */
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002987 if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002988 return NESTED_EXIT_HOST;
2989 break;
2990 default:
2991 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002992 }
2993
Joerg Roedel410e4d52009-08-07 11:49:44 +02002994 return NESTED_EXIT_CONTINUE;
2995}
2996
2997/*
2998 * If this function returns true, this #vmexit was already handled
2999 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003000static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003001{
3002 u32 exit_code = svm->vmcb->control.exit_code;
3003 int vmexit = NESTED_EXIT_HOST;
3004
Alexander Grafcf74a782008-11-25 20:17:08 +01003005 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003006 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003007 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003008 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003009 case SVM_EXIT_IOIO:
3010 vmexit = nested_svm_intercept_ioio(svm);
3011 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003012 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
3013 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
3014 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003015 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003016 break;
3017 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01003018 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
3019 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
3020 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003021 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003022 break;
3023 }
3024 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
3025 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003026 if (svm->nested.intercept_exceptions & excp_bits) {
3027 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
3028 vmexit = nested_svm_intercept_db(svm);
3029 else
3030 vmexit = NESTED_EXIT_DONE;
3031 }
Gleb Natapov631bc482010-10-14 11:22:52 +02003032 /* async page fault always cause vmexit */
3033 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003034 svm->vcpu.arch.exception.nested_apf != 0)
Gleb Natapov631bc482010-10-14 11:22:52 +02003035 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003036 break;
3037 }
Joerg Roedel228070b2010-04-22 12:33:10 +02003038 case SVM_EXIT_ERR: {
3039 vmexit = NESTED_EXIT_DONE;
3040 break;
3041 }
Alexander Grafcf74a782008-11-25 20:17:08 +01003042 default: {
3043 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02003044 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003045 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003046 }
3047 }
3048
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003049 return vmexit;
3050}
3051
3052static int nested_svm_exit_handled(struct vcpu_svm *svm)
3053{
3054 int vmexit;
3055
3056 vmexit = nested_svm_intercept(svm);
3057
3058 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003059 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003060
3061 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01003062}
3063
Joerg Roedel0460a972009-08-07 11:49:31 +02003064static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
3065{
3066 struct vmcb_control_area *dst = &dst_vmcb->control;
3067 struct vmcb_control_area *from = &from_vmcb->control;
3068
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003069 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003070 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02003071 dst->intercept_exceptions = from->intercept_exceptions;
3072 dst->intercept = from->intercept;
3073 dst->iopm_base_pa = from->iopm_base_pa;
3074 dst->msrpm_base_pa = from->msrpm_base_pa;
3075 dst->tsc_offset = from->tsc_offset;
3076 dst->asid = from->asid;
3077 dst->tlb_ctl = from->tlb_ctl;
3078 dst->int_ctl = from->int_ctl;
3079 dst->int_vector = from->int_vector;
3080 dst->int_state = from->int_state;
3081 dst->exit_code = from->exit_code;
3082 dst->exit_code_hi = from->exit_code_hi;
3083 dst->exit_info_1 = from->exit_info_1;
3084 dst->exit_info_2 = from->exit_info_2;
3085 dst->exit_int_info = from->exit_int_info;
3086 dst->exit_int_info_err = from->exit_int_info_err;
3087 dst->nested_ctl = from->nested_ctl;
3088 dst->event_inj = from->event_inj;
3089 dst->event_inj_err = from->event_inj_err;
3090 dst->nested_cr3 = from->nested_cr3;
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003091 dst->virt_ext = from->virt_ext;
Joerg Roedel0460a972009-08-07 11:49:31 +02003092}
3093
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003094static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003095{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003096 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003097 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02003098 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003099 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01003100
Joerg Roedel17897f32009-10-09 16:08:29 +02003101 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
3102 vmcb->control.exit_info_1,
3103 vmcb->control.exit_info_2,
3104 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003105 vmcb->control.exit_int_info_err,
3106 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02003107
Joerg Roedel7597f122010-02-19 16:23:00 +01003108 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003109 if (!nested_vmcb)
3110 return 1;
3111
Joerg Roedel20307532010-11-29 17:51:48 +01003112 /* Exit Guest-Mode */
3113 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01003114 svm->nested.vmcb = 0;
3115
Alexander Grafcf74a782008-11-25 20:17:08 +01003116 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02003117 disable_gif(svm);
3118
3119 nested_vmcb->save.es = vmcb->save.es;
3120 nested_vmcb->save.cs = vmcb->save.cs;
3121 nested_vmcb->save.ss = vmcb->save.ss;
3122 nested_vmcb->save.ds = vmcb->save.ds;
3123 nested_vmcb->save.gdtr = vmcb->save.gdtr;
3124 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02003125 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003126 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003127 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003128 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003129 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03003130 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003131 nested_vmcb->save.rip = vmcb->save.rip;
3132 nested_vmcb->save.rsp = vmcb->save.rsp;
3133 nested_vmcb->save.rax = vmcb->save.rax;
3134 nested_vmcb->save.dr7 = vmcb->save.dr7;
3135 nested_vmcb->save.dr6 = vmcb->save.dr6;
3136 nested_vmcb->save.cpl = vmcb->save.cpl;
3137
3138 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
3139 nested_vmcb->control.int_vector = vmcb->control.int_vector;
3140 nested_vmcb->control.int_state = vmcb->control.int_state;
3141 nested_vmcb->control.exit_code = vmcb->control.exit_code;
3142 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
3143 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
3144 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
3145 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
3146 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003147
3148 if (svm->nrips_enabled)
3149 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02003150
3151 /*
3152 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
3153 * to make sure that we do not lose injected events. So check event_inj
3154 * here and copy it to exit_int_info if it is valid.
3155 * Exit_int_info and event_inj can't be both valid because the case
3156 * below only happens on a VMRUN instruction intercept which has
3157 * no valid exit_int_info set.
3158 */
3159 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
3160 struct vmcb_control_area *nc = &nested_vmcb->control;
3161
3162 nc->exit_int_info = vmcb->control.event_inj;
3163 nc->exit_int_info_err = vmcb->control.event_inj_err;
3164 }
3165
Joerg Roedel33740e42009-08-07 11:49:29 +02003166 nested_vmcb->control.tlb_ctl = 0;
3167 nested_vmcb->control.event_inj = 0;
3168 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01003169
3170 /* We always set V_INTR_MASKING and remember the old value in hflags */
3171 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
3172 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
3173
Alexander Grafcf74a782008-11-25 20:17:08 +01003174 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02003175 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01003176
Alexander Graf219b65d2009-06-15 15:21:25 +02003177 kvm_clear_exception_queue(&svm->vcpu);
3178 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003179
Joerg Roedel4b161842010-09-10 17:31:03 +02003180 svm->nested.nested_cr3 = 0;
3181
Alexander Grafcf74a782008-11-25 20:17:08 +01003182 /* Restore selected save entries */
3183 svm->vmcb->save.es = hsave->save.es;
3184 svm->vmcb->save.cs = hsave->save.cs;
3185 svm->vmcb->save.ss = hsave->save.ss;
3186 svm->vmcb->save.ds = hsave->save.ds;
3187 svm->vmcb->save.gdtr = hsave->save.gdtr;
3188 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003189 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01003190 svm_set_efer(&svm->vcpu, hsave->save.efer);
3191 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
3192 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
3193 if (npt_enabled) {
3194 svm->vmcb->save.cr3 = hsave->save.cr3;
3195 svm->vcpu.arch.cr3 = hsave->save.cr3;
3196 } else {
Avi Kivity23902182010-06-10 17:02:16 +03003197 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01003198 }
3199 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
3200 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
3201 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
3202 svm->vmcb->save.dr7 = 0;
3203 svm->vmcb->save.cpl = 0;
3204 svm->vmcb->control.exit_int_info = 0;
3205
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003206 mark_all_dirty(svm->vmcb);
3207
Joerg Roedel7597f122010-02-19 16:23:00 +01003208 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01003209
Joerg Roedel4b161842010-09-10 17:31:03 +02003210 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003211 kvm_mmu_reset_context(&svm->vcpu);
3212 kvm_mmu_load(&svm->vcpu);
3213
3214 return 0;
3215}
Alexander Graf3d6368e2008-11-25 20:17:07 +01003216
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003217static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003218{
Joerg Roedel323c3d82010-03-01 15:34:37 +01003219 /*
3220 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003221 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01003222 * the kvm msr permission bitmap may contain zero bits
3223 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01003224 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003225
Joerg Roedel323c3d82010-03-01 15:34:37 +01003226 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3227 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003228
Joerg Roedel323c3d82010-03-01 15:34:37 +01003229 for (i = 0; i < MSRPM_OFFSETS; i++) {
3230 u32 value, p;
3231 u64 offset;
3232
3233 if (msrpm_offsets[i] == 0xffffffff)
3234 break;
3235
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003236 p = msrpm_offsets[i];
3237 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01003238
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003239 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01003240 return false;
3241
3242 svm->nested.msrpm[p] = svm->msrpm[p] | value;
3243 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003244
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05003245 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
Alexander Graf3d6368e2008-11-25 20:17:07 +01003246
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003247 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003248}
3249
Joerg Roedel52c65a302010-08-02 16:46:44 +02003250static bool nested_vmcb_checks(struct vmcb *vmcb)
3251{
3252 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
3253 return false;
3254
Joerg Roedeldbe77582010-08-02 16:46:45 +02003255 if (vmcb->control.asid == 0)
3256 return false;
3257
Tom Lendackycea3a192017-12-04 10:57:24 -06003258 if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
3259 !npt_enabled)
Joerg Roedel4b161842010-09-10 17:31:03 +02003260 return false;
3261
Joerg Roedel52c65a302010-08-02 16:46:44 +02003262 return true;
3263}
3264
Ladi Prosekc2634062017-10-11 16:54:44 +02003265static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
3266 struct vmcb *nested_vmcb, struct page *page)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003267{
Avi Kivityf6e78472010-08-02 15:30:20 +03003268 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003269 svm->vcpu.arch.hflags |= HF_HIF_MASK;
3270 else
3271 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
3272
Tom Lendackycea3a192017-12-04 10:57:24 -06003273 if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
Joerg Roedel4b161842010-09-10 17:31:03 +02003274 kvm_mmu_unload(&svm->vcpu);
3275 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
3276 nested_svm_init_mmu_context(&svm->vcpu);
3277 }
3278
Alexander Graf3d6368e2008-11-25 20:17:07 +01003279 /* Load the nested guest state */
3280 svm->vmcb->save.es = nested_vmcb->save.es;
3281 svm->vmcb->save.cs = nested_vmcb->save.cs;
3282 svm->vmcb->save.ss = nested_vmcb->save.ss;
3283 svm->vmcb->save.ds = nested_vmcb->save.ds;
3284 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
3285 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003286 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003287 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
3288 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
3289 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
3290 if (npt_enabled) {
3291 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
3292 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003293 } else
Avi Kivity23902182010-06-10 17:02:16 +03003294 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003295
3296 /* Guest paging mode is active - reset mmu */
3297 kvm_mmu_reset_context(&svm->vcpu);
3298
Joerg Roedeldefbba52009-08-07 11:49:30 +02003299 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003300 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
3301 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
3302 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01003303
Alexander Graf3d6368e2008-11-25 20:17:07 +01003304 /* In case we don't even reach vcpu_run, the fields are not updated */
3305 svm->vmcb->save.rax = nested_vmcb->save.rax;
3306 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3307 svm->vmcb->save.rip = nested_vmcb->save.rip;
3308 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3309 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3310 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3311
Joerg Roedelf7138532010-03-01 15:34:40 +01003312 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003313 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003314
Joerg Roedelaad42c62009-08-07 11:49:34 +02003315 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003316 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003317 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02003318 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3319 svm->nested.intercept = nested_vmcb->control.intercept;
3320
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08003321 svm_flush_tlb(&svm->vcpu, true);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003322 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003323 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3324 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3325 else
3326 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3327
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003328 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3329 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003330 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3331 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003332 }
3333
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003334 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003335 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003336
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003337 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003338 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3339 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
3340 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003341 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3342 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3343
Joerg Roedel7597f122010-02-19 16:23:00 +01003344 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003345
Joerg Roedel20307532010-11-29 17:51:48 +01003346 /* Enter Guest-Mode */
3347 enter_guest_mode(&svm->vcpu);
3348
Joerg Roedel384c6362010-11-30 18:03:56 +01003349 /*
3350 * Merge guest and host intercepts - must be called with vcpu in
3351 * guest-mode to take affect here
3352 */
3353 recalc_intercepts(svm);
3354
Joerg Roedel06fc77722010-02-19 16:23:07 +01003355 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003356
Joerg Roedel2af91942009-08-07 11:49:28 +02003357 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003358
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003359 mark_all_dirty(svm->vmcb);
Ladi Prosekc2634062017-10-11 16:54:44 +02003360}
3361
3362static bool nested_svm_vmrun(struct vcpu_svm *svm)
3363{
3364 struct vmcb *nested_vmcb;
3365 struct vmcb *hsave = svm->nested.hsave;
3366 struct vmcb *vmcb = svm->vmcb;
3367 struct page *page;
3368 u64 vmcb_gpa;
3369
3370 vmcb_gpa = svm->vmcb->save.rax;
3371
3372 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
3373 if (!nested_vmcb)
3374 return false;
3375
3376 if (!nested_vmcb_checks(nested_vmcb)) {
3377 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3378 nested_vmcb->control.exit_code_hi = 0;
3379 nested_vmcb->control.exit_info_1 = 0;
3380 nested_vmcb->control.exit_info_2 = 0;
3381
3382 nested_svm_unmap(page);
3383
3384 return false;
3385 }
3386
3387 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3388 nested_vmcb->save.rip,
3389 nested_vmcb->control.int_ctl,
3390 nested_vmcb->control.event_inj,
3391 nested_vmcb->control.nested_ctl);
3392
3393 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3394 nested_vmcb->control.intercept_cr >> 16,
3395 nested_vmcb->control.intercept_exceptions,
3396 nested_vmcb->control.intercept);
3397
3398 /* Clear internal status */
3399 kvm_clear_exception_queue(&svm->vcpu);
3400 kvm_clear_interrupt_queue(&svm->vcpu);
3401
3402 /*
3403 * Save the old vmcb, so we don't need to pick what we save, but can
3404 * restore everything when a VMEXIT occurs
3405 */
3406 hsave->save.es = vmcb->save.es;
3407 hsave->save.cs = vmcb->save.cs;
3408 hsave->save.ss = vmcb->save.ss;
3409 hsave->save.ds = vmcb->save.ds;
3410 hsave->save.gdtr = vmcb->save.gdtr;
3411 hsave->save.idtr = vmcb->save.idtr;
3412 hsave->save.efer = svm->vcpu.arch.efer;
3413 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3414 hsave->save.cr4 = svm->vcpu.arch.cr4;
3415 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3416 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3417 hsave->save.rsp = vmcb->save.rsp;
3418 hsave->save.rax = vmcb->save.rax;
3419 if (npt_enabled)
3420 hsave->save.cr3 = vmcb->save.cr3;
3421 else
3422 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3423
3424 copy_vmcb_control_area(hsave, vmcb);
3425
3426 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003427
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003428 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003429}
3430
Joerg Roedel9966bf62009-08-07 11:49:40 +02003431static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01003432{
3433 to_vmcb->save.fs = from_vmcb->save.fs;
3434 to_vmcb->save.gs = from_vmcb->save.gs;
3435 to_vmcb->save.tr = from_vmcb->save.tr;
3436 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3437 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3438 to_vmcb->save.star = from_vmcb->save.star;
3439 to_vmcb->save.lstar = from_vmcb->save.lstar;
3440 to_vmcb->save.cstar = from_vmcb->save.cstar;
3441 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3442 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3443 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3444 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01003445}
3446
Avi Kivity851ba692009-08-24 11:10:17 +03003447static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003448{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003449 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003450 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003451 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003452
Alexander Graf55426752008-11-25 20:17:06 +01003453 if (nested_svm_check_permissions(svm))
3454 return 1;
3455
Joerg Roedel7597f122010-02-19 16:23:00 +01003456 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003457 if (!nested_vmcb)
3458 return 1;
3459
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003460 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003461 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003462
Joerg Roedel9966bf62009-08-07 11:49:40 +02003463 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003464 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003465
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003466 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003467}
3468
Avi Kivity851ba692009-08-24 11:10:17 +03003469static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003470{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003471 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003472 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003473 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003474
Alexander Graf55426752008-11-25 20:17:06 +01003475 if (nested_svm_check_permissions(svm))
3476 return 1;
3477
Joerg Roedel7597f122010-02-19 16:23:00 +01003478 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003479 if (!nested_vmcb)
3480 return 1;
3481
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003482 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003483 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003484
Joerg Roedel9966bf62009-08-07 11:49:40 +02003485 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003486 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003487
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003488 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003489}
3490
Avi Kivity851ba692009-08-24 11:10:17 +03003491static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003492{
Alexander Graf3d6368e2008-11-25 20:17:07 +01003493 if (nested_svm_check_permissions(svm))
3494 return 1;
3495
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02003496 /* Save rip after vmrun instruction */
3497 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003498
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003499 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01003500 return 1;
3501
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003502 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02003503 goto failed;
3504
3505 return 1;
3506
3507failed:
3508
3509 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3510 svm->vmcb->control.exit_code_hi = 0;
3511 svm->vmcb->control.exit_info_1 = 0;
3512 svm->vmcb->control.exit_info_2 = 0;
3513
3514 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003515
3516 return 1;
3517}
3518
Avi Kivity851ba692009-08-24 11:10:17 +03003519static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003520{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003521 int ret;
3522
Alexander Graf1371d902008-11-25 20:17:04 +01003523 if (nested_svm_check_permissions(svm))
3524 return 1;
3525
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003526 /*
3527 * If VGIF is enabled, the STGI intercept is only added to
Ladi Prosekcc3d9672017-10-17 16:02:39 +02003528 * detect the opening of the SMI/NMI window; remove it now.
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003529 */
3530 if (vgif_enabled(svm))
3531 clr_intercept(svm, INTERCEPT_STGI);
3532
Alexander Graf1371d902008-11-25 20:17:04 +01003533 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003534 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003535 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003536
Joerg Roedel2af91942009-08-07 11:49:28 +02003537 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003538
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003539 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003540}
3541
Avi Kivity851ba692009-08-24 11:10:17 +03003542static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003543{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003544 int ret;
3545
Alexander Graf1371d902008-11-25 20:17:04 +01003546 if (nested_svm_check_permissions(svm))
3547 return 1;
3548
3549 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003550 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003551
Joerg Roedel2af91942009-08-07 11:49:28 +02003552 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003553
3554 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05003555 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3556 svm_clear_vintr(svm);
3557 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3558 mark_dirty(svm->vmcb, VMCB_INTR);
3559 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003560
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003561 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003562}
3563
Avi Kivity851ba692009-08-24 11:10:17 +03003564static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02003565{
3566 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02003567
David Kaplan668f1982015-02-20 16:02:10 -06003568 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
3569 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02003570
Alexander Grafff092382009-06-15 15:21:24 +02003571 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06003572 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02003573
3574 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003575 return kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02003576}
3577
Joerg Roedel532a46b2009-10-09 16:08:32 +02003578static int skinit_interception(struct vcpu_svm *svm)
3579{
David Kaplan668f1982015-02-20 16:02:10 -06003580 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003581
3582 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3583 return 1;
3584}
3585
David Kaplandab429a2015-03-02 13:43:37 -06003586static int wbinvd_interception(struct vcpu_svm *svm)
3587{
Kyle Huey6affcbe2016-11-29 12:40:40 -08003588 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06003589}
3590
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003591static int xsetbv_interception(struct vcpu_svm *svm)
3592{
3593 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
3594 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3595
3596 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3597 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003598 return kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003599 }
3600
3601 return 1;
3602}
3603
Avi Kivity851ba692009-08-24 11:10:17 +03003604static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003605{
Izik Eidus37817f22008-03-24 23:14:53 +02003606 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003607 int reason;
3608 int int_type = svm->vmcb->control.exit_int_info &
3609 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003610 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003611 uint32_t type =
3612 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3613 uint32_t idt_v =
3614 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003615 bool has_error_code = false;
3616 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003617
3618 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003619
Izik Eidus37817f22008-03-24 23:14:53 +02003620 if (svm->vmcb->control.exit_info_2 &
3621 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003622 reason = TASK_SWITCH_IRET;
3623 else if (svm->vmcb->control.exit_info_2 &
3624 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3625 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003626 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003627 reason = TASK_SWITCH_GATE;
3628 else
3629 reason = TASK_SWITCH_CALL;
3630
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003631 if (reason == TASK_SWITCH_GATE) {
3632 switch (type) {
3633 case SVM_EXITINTINFO_TYPE_NMI:
3634 svm->vcpu.arch.nmi_injected = false;
3635 break;
3636 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003637 if (svm->vmcb->control.exit_info_2 &
3638 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3639 has_error_code = true;
3640 error_code =
3641 (u32)svm->vmcb->control.exit_info_2;
3642 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003643 kvm_clear_exception_queue(&svm->vcpu);
3644 break;
3645 case SVM_EXITINTINFO_TYPE_INTR:
3646 kvm_clear_interrupt_queue(&svm->vcpu);
3647 break;
3648 default:
3649 break;
3650 }
3651 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003652
Gleb Natapov8317c292009-04-12 13:37:02 +03003653 if (reason != TASK_SWITCH_GATE ||
3654 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3655 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03003656 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3657 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003658
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003659 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3660 int_vec = -1;
3661
3662 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03003663 has_error_code, error_code) == EMULATE_FAIL) {
3664 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3665 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3666 svm->vcpu.run->internal.ndata = 0;
3667 return 0;
3668 }
3669 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003670}
3671
Avi Kivity851ba692009-08-24 11:10:17 +03003672static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003673{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003674 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Kyle Huey6a908b62016-11-29 12:40:37 -08003675 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003676}
3677
Avi Kivity851ba692009-08-24 11:10:17 +03003678static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003679{
3680 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003681 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003682 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003683 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003684 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003685 return 1;
3686}
3687
Avi Kivity851ba692009-08-24 11:10:17 +03003688static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003689{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003690 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3691 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3692
3693 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003694 return kvm_skip_emulated_instruction(&svm->vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003695}
3696
Avi Kivity851ba692009-08-24 11:10:17 +03003697static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003698{
Andre Przywara51d8b662010-12-21 11:12:02 +01003699 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003700}
3701
Avi Kivity332b56e2011-11-10 14:57:24 +02003702static int rdpmc_interception(struct vcpu_svm *svm)
3703{
3704 int err;
3705
3706 if (!static_cpu_has(X86_FEATURE_NRIPS))
3707 return emulate_on_interception(svm);
3708
3709 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08003710 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02003711}
3712
Xiubo Li52eb5a62015-03-13 17:39:45 +08003713static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3714 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003715{
3716 unsigned long cr0 = svm->vcpu.arch.cr0;
3717 bool ret = false;
3718 u64 intercept;
3719
3720 intercept = svm->nested.intercept;
3721
3722 if (!is_guest_mode(&svm->vcpu) ||
3723 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3724 return false;
3725
3726 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3727 val &= ~SVM_CR0_SELECTIVE_MASK;
3728
3729 if (cr0 ^ val) {
3730 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3731 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3732 }
3733
3734 return ret;
3735}
3736
Andre Przywara7ff76d52010-12-21 11:12:04 +01003737#define CR_VALID (1ULL << 63)
3738
3739static int cr_interception(struct vcpu_svm *svm)
3740{
3741 int reg, cr;
3742 unsigned long val;
3743 int err;
3744
3745 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3746 return emulate_on_interception(svm);
3747
3748 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3749 return emulate_on_interception(svm);
3750
3751 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003752 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3753 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3754 else
3755 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003756
3757 err = 0;
3758 if (cr >= 16) { /* mov to cr */
3759 cr -= 16;
3760 val = kvm_register_read(&svm->vcpu, reg);
3761 switch (cr) {
3762 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003763 if (!check_selective_cr0_intercepted(svm, val))
3764 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003765 else
3766 return 1;
3767
Andre Przywara7ff76d52010-12-21 11:12:04 +01003768 break;
3769 case 3:
3770 err = kvm_set_cr3(&svm->vcpu, val);
3771 break;
3772 case 4:
3773 err = kvm_set_cr4(&svm->vcpu, val);
3774 break;
3775 case 8:
3776 err = kvm_set_cr8(&svm->vcpu, val);
3777 break;
3778 default:
3779 WARN(1, "unhandled write to CR%d", cr);
3780 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3781 return 1;
3782 }
3783 } else { /* mov from cr */
3784 switch (cr) {
3785 case 0:
3786 val = kvm_read_cr0(&svm->vcpu);
3787 break;
3788 case 2:
3789 val = svm->vcpu.arch.cr2;
3790 break;
3791 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003792 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003793 break;
3794 case 4:
3795 val = kvm_read_cr4(&svm->vcpu);
3796 break;
3797 case 8:
3798 val = kvm_get_cr8(&svm->vcpu);
3799 break;
3800 default:
3801 WARN(1, "unhandled read from CR%d", cr);
3802 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3803 return 1;
3804 }
3805 kvm_register_write(&svm->vcpu, reg, val);
3806 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08003807 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003808}
3809
Andre Przywaracae37972010-12-21 11:12:05 +01003810static int dr_interception(struct vcpu_svm *svm)
3811{
3812 int reg, dr;
3813 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003814
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003815 if (svm->vcpu.guest_debug == 0) {
3816 /*
3817 * No more DR vmexits; force a reload of the debug registers
3818 * and reenter on this instruction. The next vmexit will
3819 * retrieve the full state of the debug registers.
3820 */
3821 clr_dr_intercepts(svm);
3822 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3823 return 1;
3824 }
3825
Andre Przywaracae37972010-12-21 11:12:05 +01003826 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3827 return emulate_on_interception(svm);
3828
3829 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3830 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3831
3832 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003833 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3834 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003835 val = kvm_register_read(&svm->vcpu, reg);
3836 kvm_set_dr(&svm->vcpu, dr - 16, val);
3837 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003838 if (!kvm_require_dr(&svm->vcpu, dr))
3839 return 1;
3840 kvm_get_dr(&svm->vcpu, dr, &val);
3841 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003842 }
3843
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003844 return kvm_skip_emulated_instruction(&svm->vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01003845}
3846
Avi Kivity851ba692009-08-24 11:10:17 +03003847static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003848{
Avi Kivity851ba692009-08-24 11:10:17 +03003849 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003850 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003851
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003852 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3853 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003854 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003855 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003856 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003857 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003858 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003859 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3860 return 0;
3861}
3862
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003863static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003865 struct vcpu_svm *svm = to_svm(vcpu);
3866
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003867 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303868 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003869 msr_info->data = svm->vmcb->control.tsc_offset +
Haozhong Zhang35181e82015-10-20 15:39:03 +08003870 kvm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003871
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872 break;
3873 }
Brian Gerst8c065852010-07-17 09:03:26 -04003874 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003875 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003876 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003877#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003879 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880 break;
3881 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003882 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003883 break;
3884 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003885 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003886 break;
3887 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003888 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003889 break;
3890#endif
3891 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003892 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893 break;
3894 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003895 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003896 break;
3897 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003898 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003899 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003900 case MSR_TSC_AUX:
3901 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3902 return 1;
3903 msr_info->data = svm->tsc_aux;
3904 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003905 /*
3906 * Nobody will change the following 5 values in the VMCB so we can
3907 * safely return them on rdmsr. They will always be 0 until LBRV is
3908 * implemented.
3909 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003910 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003911 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01003912 break;
3913 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003914 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003915 break;
3916 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003917 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003918 break;
3919 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003920 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003921 break;
3922 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003923 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003924 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003925 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003926 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003927 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003928 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003929 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003930 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003931 case MSR_IA32_SPEC_CTRL:
3932 if (!msr_info->host_initiated &&
3933 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
3934 return 1;
3935
3936 msr_info->data = svm->spec_ctrl;
3937 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003938 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003939 msr_info->data = 0x01000065;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003940 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01003941 case MSR_F15H_IC_CFG: {
3942
3943 int family, model;
3944
3945 family = guest_cpuid_family(vcpu);
3946 model = guest_cpuid_model(vcpu);
3947
3948 if (family < 0 || model < 0)
3949 return kvm_get_msr_common(vcpu, msr_info);
3950
3951 msr_info->data = 0;
3952
3953 if (family == 0x15 &&
3954 (model >= 0x2 && model < 0x20))
3955 msr_info->data = 0x1E;
3956 }
3957 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003959 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003960 }
3961 return 0;
3962}
3963
Avi Kivity851ba692009-08-24 11:10:17 +03003964static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003965{
David Kaplan668f1982015-02-20 16:02:10 -06003966 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003967 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003968
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003969 msr_info.index = ecx;
3970 msr_info.host_initiated = false;
3971 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02003972 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003973 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003974 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02003975 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003976 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003977
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003978 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
3979 msr_info.data & 0xffffffff);
3980 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
3981 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003982 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003983 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003984 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003985}
3986
Joerg Roedel4a810182010-02-24 18:59:15 +01003987static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3988{
3989 struct vcpu_svm *svm = to_svm(vcpu);
3990 int svm_dis, chg_mask;
3991
3992 if (data & ~SVM_VM_CR_VALID_MASK)
3993 return 1;
3994
3995 chg_mask = SVM_VM_CR_VALID_MASK;
3996
3997 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3998 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3999
4000 svm->nested.vm_cr_msr &= ~chg_mask;
4001 svm->nested.vm_cr_msr |= (data & chg_mask);
4002
4003 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
4004
4005 /* check for svm_disable while efer.svme is set */
4006 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
4007 return 1;
4008
4009 return 0;
4010}
4011
Will Auld8fe8ab42012-11-29 12:42:12 -08004012static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004014 struct vcpu_svm *svm = to_svm(vcpu);
4015
Will Auld8fe8ab42012-11-29 12:42:12 -08004016 u32 ecx = msr->index;
4017 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004018 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02004019 case MSR_IA32_CR_PAT:
4020 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4021 return 1;
4022 vcpu->arch.pat = data;
4023 svm->vmcb->save.g_pat = data;
4024 mark_dirty(svm->vmcb, VMCB_NPT);
4025 break;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10004026 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08004027 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004028 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004029 case MSR_IA32_SPEC_CTRL:
4030 if (!msr->host_initiated &&
4031 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
4032 return 1;
4033
4034 /* The STIBP bit doesn't fault even if it's not advertised */
4035 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
4036 return 1;
4037
4038 svm->spec_ctrl = data;
4039
4040 if (!data)
4041 break;
4042
4043 /*
4044 * For non-nested:
4045 * When it's written (to non-zero) for the first time, pass
4046 * it through.
4047 *
4048 * For nested:
4049 * The handling of the MSR bitmap for L2 guests is done in
4050 * nested_svm_vmrun_msrpm.
4051 * We update the L1 MSR bit as well since it will end up
4052 * touching the MSR anyway now.
4053 */
4054 set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
4055 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004056 case MSR_IA32_PRED_CMD:
4057 if (!msr->host_initiated &&
4058 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB))
4059 return 1;
4060
4061 if (data & ~PRED_CMD_IBPB)
4062 return 1;
4063
4064 if (!data)
4065 break;
4066
4067 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4068 if (is_guest_mode(vcpu))
4069 break;
4070 set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
4071 break;
Brian Gerst8c065852010-07-17 09:03:26 -04004072 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004073 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08004075#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004076 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004077 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004078 break;
4079 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004080 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004081 break;
4082 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004083 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084 break;
4085 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004086 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004087 break;
4088#endif
4089 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004090 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004091 break;
4092 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02004093 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004094 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 break;
4096 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02004097 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004098 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004099 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004100 case MSR_TSC_AUX:
4101 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4102 return 1;
4103
4104 /*
4105 * This is rare, so we update the MSR here instead of using
4106 * direct_access_msrs. Doing that would require a rdmsr in
4107 * svm_vcpu_put.
4108 */
4109 svm->tsc_aux = data;
4110 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
4111 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01004112 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004113 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03004114 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
4115 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004116 break;
4117 }
4118 if (data & DEBUGCTL_RESERVED_BITS)
4119 return 1;
4120
4121 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01004122 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004123 if (data & (1ULL<<0))
4124 svm_enable_lbrv(svm);
4125 else
4126 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01004127 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004128 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02004129 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004130 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004131 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01004132 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004133 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03004134 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004135 break;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004136 case MSR_IA32_APICBASE:
4137 if (kvm_vcpu_apicv_active(vcpu))
4138 avic_update_vapic_bar(to_svm(vcpu), data);
4139 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004140 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08004141 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004142 }
4143 return 0;
4144}
4145
Avi Kivity851ba692009-08-24 11:10:17 +03004146static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147{
Will Auld8fe8ab42012-11-29 12:42:12 -08004148 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06004149 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
4150 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004151
Will Auld8fe8ab42012-11-29 12:42:12 -08004152 msr.data = data;
4153 msr.index = ecx;
4154 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004155
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004156 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03004157 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02004158 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004159 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004160 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004161 } else {
4162 trace_kvm_msr_write(ecx, data);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004163 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02004164 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165}
4166
Avi Kivity851ba692009-08-24 11:10:17 +03004167static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168{
Rusty Russelle756fc62007-07-30 20:07:08 +10004169 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03004170 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171 else
Avi Kivity851ba692009-08-24 11:10:17 +03004172 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004173}
4174
Avi Kivity851ba692009-08-24 11:10:17 +03004175static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08004176{
Avi Kivity3842d132010-07-27 12:30:24 +03004177 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01004178 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03004179 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004180 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08004181 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08004182 return 1;
4183}
4184
Mark Langsdorf565d0992009-10-06 14:25:02 -05004185static int pause_interception(struct vcpu_svm *svm)
4186{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004187 struct kvm_vcpu *vcpu = &svm->vcpu;
4188 bool in_kernel = (svm_get_cpl(vcpu) == 0);
4189
4190 kvm_vcpu_on_spin(vcpu, in_kernel);
Mark Langsdorf565d0992009-10-06 14:25:02 -05004191 return 1;
4192}
4193
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004194static int nop_interception(struct vcpu_svm *svm)
4195{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004196 return kvm_skip_emulated_instruction(&(svm->vcpu));
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004197}
4198
4199static int monitor_interception(struct vcpu_svm *svm)
4200{
4201 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
4202 return nop_interception(svm);
4203}
4204
4205static int mwait_interception(struct vcpu_svm *svm)
4206{
4207 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4208 return nop_interception(svm);
4209}
4210
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004211enum avic_ipi_failure_cause {
4212 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
4213 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
4214 AVIC_IPI_FAILURE_INVALID_TARGET,
4215 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
4216};
4217
4218static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
4219{
4220 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
4221 u32 icrl = svm->vmcb->control.exit_info_1;
4222 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03004223 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004224 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4225
4226 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
4227
4228 switch (id) {
4229 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
4230 /*
4231 * AVIC hardware handles the generation of
4232 * IPIs when the specified Message Type is Fixed
4233 * (also known as fixed delivery mode) and
4234 * the Trigger Mode is edge-triggered. The hardware
4235 * also supports self and broadcast delivery modes
4236 * specified via the Destination Shorthand(DSH)
4237 * field of the ICRL. Logical and physical APIC ID
4238 * formats are supported. All other IPI types cause
4239 * a #VMEXIT, which needs to emulated.
4240 */
4241 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
4242 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
4243 break;
4244 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
4245 int i;
4246 struct kvm_vcpu *vcpu;
4247 struct kvm *kvm = svm->vcpu.kvm;
4248 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4249
4250 /*
4251 * At this point, we expect that the AVIC HW has already
4252 * set the appropriate IRR bits on the valid target
4253 * vcpus. So, we just need to kick the appropriate vcpu.
4254 */
4255 kvm_for_each_vcpu(i, vcpu, kvm) {
4256 bool m = kvm_apic_match_dest(vcpu, apic,
4257 icrl & KVM_APIC_SHORT_MASK,
4258 GET_APIC_DEST_FIELD(icrh),
4259 icrl & KVM_APIC_DEST_MASK);
4260
4261 if (m && !avic_vcpu_is_running(vcpu))
4262 kvm_vcpu_wake_up(vcpu);
4263 }
4264 break;
4265 }
4266 case AVIC_IPI_FAILURE_INVALID_TARGET:
4267 break;
4268 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
4269 WARN_ONCE(1, "Invalid backing page\n");
4270 break;
4271 default:
4272 pr_err("Unknown IPI interception\n");
4273 }
4274
4275 return 1;
4276}
4277
4278static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
4279{
4280 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4281 int index;
4282 u32 *logical_apic_id_table;
4283 int dlid = GET_APIC_LOGICAL_ID(ldr);
4284
4285 if (!dlid)
4286 return NULL;
4287
4288 if (flat) { /* flat */
4289 index = ffs(dlid) - 1;
4290 if (index > 7)
4291 return NULL;
4292 } else { /* cluster */
4293 int cluster = (dlid & 0xf0) >> 4;
4294 int apic = ffs(dlid & 0x0f) - 1;
4295
4296 if ((apic < 0) || (apic > 7) ||
4297 (cluster >= 0xf))
4298 return NULL;
4299 index = (cluster << 2) + apic;
4300 }
4301
4302 logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page);
4303
4304 return &logical_apic_id_table[index];
4305}
4306
4307static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
4308 bool valid)
4309{
4310 bool flat;
4311 u32 *entry, new_entry;
4312
4313 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4314 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4315 if (!entry)
4316 return -EINVAL;
4317
4318 new_entry = READ_ONCE(*entry);
4319 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4320 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
4321 if (valid)
4322 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4323 else
4324 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4325 WRITE_ONCE(*entry, new_entry);
4326
4327 return 0;
4328}
4329
4330static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4331{
4332 int ret;
4333 struct vcpu_svm *svm = to_svm(vcpu);
4334 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
4335
4336 if (!ldr)
4337 return 1;
4338
4339 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
4340 if (ret && svm->ldr_reg) {
4341 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
4342 svm->ldr_reg = 0;
4343 } else {
4344 svm->ldr_reg = ldr;
4345 }
4346 return ret;
4347}
4348
4349static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4350{
4351 u64 *old, *new;
4352 struct vcpu_svm *svm = to_svm(vcpu);
4353 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
4354 u32 id = (apic_id_reg >> 24) & 0xff;
4355
4356 if (vcpu->vcpu_id == id)
4357 return 0;
4358
4359 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4360 new = avic_get_physical_id_entry(vcpu, id);
4361 if (!new || !old)
4362 return 1;
4363
4364 /* We need to move physical_id_entry to new offset */
4365 *new = *old;
4366 *old = 0ULL;
4367 to_svm(vcpu)->avic_physical_id_cache = new;
4368
4369 /*
4370 * Also update the guest physical APIC ID in the logical
4371 * APIC ID table entry if already setup the LDR.
4372 */
4373 if (svm->ldr_reg)
4374 avic_handle_ldr_update(vcpu);
4375
4376 return 0;
4377}
4378
4379static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
4380{
4381 struct vcpu_svm *svm = to_svm(vcpu);
4382 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4383 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
4384 u32 mod = (dfr >> 28) & 0xf;
4385
4386 /*
4387 * We assume that all local APICs are using the same type.
4388 * If this changes, we need to flush the AVIC logical
4389 * APID id table.
4390 */
4391 if (vm_data->ldr_mode == mod)
4392 return 0;
4393
4394 clear_page(page_address(vm_data->avic_logical_id_table_page));
4395 vm_data->ldr_mode = mod;
4396
4397 if (svm->ldr_reg)
4398 avic_handle_ldr_update(vcpu);
4399 return 0;
4400}
4401
4402static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4403{
4404 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4405 u32 offset = svm->vmcb->control.exit_info_1 &
4406 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4407
4408 switch (offset) {
4409 case APIC_ID:
4410 if (avic_handle_apic_id_update(&svm->vcpu))
4411 return 0;
4412 break;
4413 case APIC_LDR:
4414 if (avic_handle_ldr_update(&svm->vcpu))
4415 return 0;
4416 break;
4417 case APIC_DFR:
4418 avic_handle_dfr_update(&svm->vcpu);
4419 break;
4420 default:
4421 break;
4422 }
4423
4424 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4425
4426 return 1;
4427}
4428
4429static bool is_avic_unaccelerated_access_trap(u32 offset)
4430{
4431 bool ret = false;
4432
4433 switch (offset) {
4434 case APIC_ID:
4435 case APIC_EOI:
4436 case APIC_RRR:
4437 case APIC_LDR:
4438 case APIC_DFR:
4439 case APIC_SPIV:
4440 case APIC_ESR:
4441 case APIC_ICR:
4442 case APIC_LVTT:
4443 case APIC_LVTTHMR:
4444 case APIC_LVTPC:
4445 case APIC_LVT0:
4446 case APIC_LVT1:
4447 case APIC_LVTERR:
4448 case APIC_TMICT:
4449 case APIC_TDCR:
4450 ret = true;
4451 break;
4452 default:
4453 break;
4454 }
4455 return ret;
4456}
4457
4458static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4459{
4460 int ret = 0;
4461 u32 offset = svm->vmcb->control.exit_info_1 &
4462 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4463 u32 vector = svm->vmcb->control.exit_info_2 &
4464 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4465 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4466 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4467 bool trap = is_avic_unaccelerated_access_trap(offset);
4468
4469 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4470 trap, write, vector);
4471 if (trap) {
4472 /* Handling Trap */
4473 WARN_ONCE(!write, "svm: Handling trap read.\n");
4474 ret = avic_unaccel_trap_write(svm);
4475 } else {
4476 /* Handling Fault */
4477 ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
4478 }
4479
4480 return ret;
4481}
4482
Mathias Krause09941fb2012-08-30 01:30:20 +02004483static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01004484 [SVM_EXIT_READ_CR0] = cr_interception,
4485 [SVM_EXIT_READ_CR3] = cr_interception,
4486 [SVM_EXIT_READ_CR4] = cr_interception,
4487 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06004488 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02004489 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01004490 [SVM_EXIT_WRITE_CR3] = cr_interception,
4491 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004492 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01004493 [SVM_EXIT_READ_DR0] = dr_interception,
4494 [SVM_EXIT_READ_DR1] = dr_interception,
4495 [SVM_EXIT_READ_DR2] = dr_interception,
4496 [SVM_EXIT_READ_DR3] = dr_interception,
4497 [SVM_EXIT_READ_DR4] = dr_interception,
4498 [SVM_EXIT_READ_DR5] = dr_interception,
4499 [SVM_EXIT_READ_DR6] = dr_interception,
4500 [SVM_EXIT_READ_DR7] = dr_interception,
4501 [SVM_EXIT_WRITE_DR0] = dr_interception,
4502 [SVM_EXIT_WRITE_DR1] = dr_interception,
4503 [SVM_EXIT_WRITE_DR2] = dr_interception,
4504 [SVM_EXIT_WRITE_DR3] = dr_interception,
4505 [SVM_EXIT_WRITE_DR4] = dr_interception,
4506 [SVM_EXIT_WRITE_DR5] = dr_interception,
4507 [SVM_EXIT_WRITE_DR6] = dr_interception,
4508 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004509 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4510 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004511 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004512 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004513 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01004514 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004515 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02004516 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004517 [SVM_EXIT_SMI] = nop_on_interception,
4518 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08004519 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02004520 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004521 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004522 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02004523 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05004524 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004525 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004526 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02004527 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004528 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004529 [SVM_EXIT_MSR] = msr_interception,
4530 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08004531 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01004532 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02004533 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01004534 [SVM_EXIT_VMLOAD] = vmload_interception,
4535 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01004536 [SVM_EXIT_STGI] = stgi_interception,
4537 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02004538 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06004539 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004540 [SVM_EXIT_MONITOR] = monitor_interception,
4541 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01004542 [SVM_EXIT_XSETBV] = xsetbv_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02004543 [SVM_EXIT_NPF] = npf_interception,
Paolo Bonzini64d60672015-05-07 11:36:11 +02004544 [SVM_EXIT_RSM] = emulate_on_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004545 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4546 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004547};
4548
Joe Perchesae8cc052011-04-24 22:00:50 -07004549static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02004550{
4551 struct vcpu_svm *svm = to_svm(vcpu);
4552 struct vmcb_control_area *control = &svm->vmcb->control;
4553 struct vmcb_save_area *save = &svm->vmcb->save;
4554
4555 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004556 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4557 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4558 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4559 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4560 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4561 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4562 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
4563 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4564 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4565 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4566 pr_err("%-20s%d\n", "asid:", control->asid);
4567 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4568 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4569 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4570 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4571 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4572 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4573 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4574 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4575 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4576 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4577 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004578 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07004579 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4580 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05004581 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07004582 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004583 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4584 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4585 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004586 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004587 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4588 "es:",
4589 save->es.selector, save->es.attrib,
4590 save->es.limit, save->es.base);
4591 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4592 "cs:",
4593 save->cs.selector, save->cs.attrib,
4594 save->cs.limit, save->cs.base);
4595 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4596 "ss:",
4597 save->ss.selector, save->ss.attrib,
4598 save->ss.limit, save->ss.base);
4599 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4600 "ds:",
4601 save->ds.selector, save->ds.attrib,
4602 save->ds.limit, save->ds.base);
4603 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4604 "fs:",
4605 save->fs.selector, save->fs.attrib,
4606 save->fs.limit, save->fs.base);
4607 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4608 "gs:",
4609 save->gs.selector, save->gs.attrib,
4610 save->gs.limit, save->gs.base);
4611 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4612 "gdtr:",
4613 save->gdtr.selector, save->gdtr.attrib,
4614 save->gdtr.limit, save->gdtr.base);
4615 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4616 "ldtr:",
4617 save->ldtr.selector, save->ldtr.attrib,
4618 save->ldtr.limit, save->ldtr.base);
4619 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4620 "idtr:",
4621 save->idtr.selector, save->idtr.attrib,
4622 save->idtr.limit, save->idtr.base);
4623 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4624 "tr:",
4625 save->tr.selector, save->tr.attrib,
4626 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004627 pr_err("cpl: %d efer: %016llx\n",
4628 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004629 pr_err("%-15s %016llx %-13s %016llx\n",
4630 "cr0:", save->cr0, "cr2:", save->cr2);
4631 pr_err("%-15s %016llx %-13s %016llx\n",
4632 "cr3:", save->cr3, "cr4:", save->cr4);
4633 pr_err("%-15s %016llx %-13s %016llx\n",
4634 "dr6:", save->dr6, "dr7:", save->dr7);
4635 pr_err("%-15s %016llx %-13s %016llx\n",
4636 "rip:", save->rip, "rflags:", save->rflags);
4637 pr_err("%-15s %016llx %-13s %016llx\n",
4638 "rsp:", save->rsp, "rax:", save->rax);
4639 pr_err("%-15s %016llx %-13s %016llx\n",
4640 "star:", save->star, "lstar:", save->lstar);
4641 pr_err("%-15s %016llx %-13s %016llx\n",
4642 "cstar:", save->cstar, "sfmask:", save->sfmask);
4643 pr_err("%-15s %016llx %-13s %016llx\n",
4644 "kernel_gs_base:", save->kernel_gs_base,
4645 "sysenter_cs:", save->sysenter_cs);
4646 pr_err("%-15s %016llx %-13s %016llx\n",
4647 "sysenter_esp:", save->sysenter_esp,
4648 "sysenter_eip:", save->sysenter_eip);
4649 pr_err("%-15s %016llx %-13s %016llx\n",
4650 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4651 pr_err("%-15s %016llx %-13s %016llx\n",
4652 "br_from:", save->br_from, "br_to:", save->br_to);
4653 pr_err("%-15s %016llx %-13s %016llx\n",
4654 "excp_from:", save->last_excp_from,
4655 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004656}
4657
Avi Kivity586f9602010-11-18 13:09:54 +02004658static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4659{
4660 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4661
4662 *info1 = control->exit_info_1;
4663 *info2 = control->exit_info_2;
4664}
4665
Avi Kivity851ba692009-08-24 11:10:17 +03004666static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004667{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004668 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004669 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004670 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004671
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004672 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4673
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004674 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004675 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4676 if (npt_enabled)
4677 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004678
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004679 if (unlikely(svm->nested.exit_required)) {
4680 nested_svm_vmexit(svm);
4681 svm->nested.exit_required = false;
4682
4683 return 1;
4684 }
4685
Joerg Roedel20307532010-11-29 17:51:48 +01004686 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004687 int vmexit;
4688
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004689 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4690 svm->vmcb->control.exit_info_1,
4691 svm->vmcb->control.exit_info_2,
4692 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004693 svm->vmcb->control.exit_int_info_err,
4694 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004695
Joerg Roedel410e4d52009-08-07 11:49:44 +02004696 vmexit = nested_svm_exit_special(svm);
4697
4698 if (vmexit == NESTED_EXIT_CONTINUE)
4699 vmexit = nested_svm_exit_handled(svm);
4700
4701 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004702 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004703 }
4704
Joerg Roedela5c38322009-08-07 11:49:32 +02004705 svm_complete_interrupts(svm);
4706
Avi Kivity04d2cc72007-09-10 18:10:54 +03004707 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4708 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4709 kvm_run->fail_entry.hardware_entry_failure_reason
4710 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004711 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4712 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004713 return 0;
4714 }
4715
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004716 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004717 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004718 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4719 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004720 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004721 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004722 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004723 exit_code);
4724
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004725 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004726 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04004727 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03004728 kvm_queue_exception(vcpu, UD_VECTOR);
4729 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004730 }
4731
Avi Kivity851ba692009-08-24 11:10:17 +03004732 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004733}
4734
4735static void reload_tss(struct kvm_vcpu *vcpu)
4736{
4737 int cpu = raw_smp_processor_id();
4738
Tejun Heo0fe1e002009-10-29 22:34:14 +09004739 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4740 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004741 load_TR_desc();
4742}
4743
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004744static void pre_sev_run(struct vcpu_svm *svm, int cpu)
4745{
4746 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4747 int asid = sev_get_asid(svm->vcpu.kvm);
4748
4749 /* Assign the asid allocated with this SEV guest */
4750 svm->vmcb->control.asid = asid;
4751
4752 /*
4753 * Flush guest TLB:
4754 *
4755 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
4756 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
4757 */
4758 if (sd->sev_vmcbs[asid] == svm->vmcb &&
4759 svm->last_cpu == cpu)
4760 return;
4761
4762 svm->last_cpu = cpu;
4763 sd->sev_vmcbs[asid] = svm->vmcb;
4764 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4765 mark_dirty(svm->vmcb, VMCB_ASID);
4766}
4767
Rusty Russelle756fc62007-07-30 20:07:08 +10004768static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004769{
4770 int cpu = raw_smp_processor_id();
4771
Tejun Heo0fe1e002009-10-29 22:34:14 +09004772 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004773
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004774 if (sev_guest(svm->vcpu.kvm))
4775 return pre_sev_run(svm, cpu);
4776
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03004777 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09004778 if (svm->asid_generation != sd->asid_generation)
4779 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004780}
4781
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004782static void svm_inject_nmi(struct kvm_vcpu *vcpu)
4783{
4784 struct vcpu_svm *svm = to_svm(vcpu);
4785
4786 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
4787 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004788 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004789 ++vcpu->stat.nmi_injections;
4790}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004791
Eddie Dong85f455f2007-07-06 12:20:49 +03004792static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004793{
4794 struct vmcb_control_area *control;
4795
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004796 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10004797 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03004798 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799 control->int_ctl &= ~V_INTR_PRIO_MASK;
4800 control->int_ctl |= V_IRQ_MASK |
4801 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004802 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004803}
4804
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004805static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03004806{
4807 struct vcpu_svm *svm = to_svm(vcpu);
4808
Joerg Roedel2af91942009-08-07 11:49:28 +02004809 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01004810
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03004811 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
4812 ++vcpu->stat.irq_injections;
4813
Alexander Graf219b65d2009-06-15 15:21:25 +02004814 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
4815 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03004816}
4817
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004818static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
4819{
4820 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
4821}
4822
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004823static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
4824{
4825 struct vcpu_svm *svm = to_svm(vcpu);
4826
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004827 if (svm_nested_virtualize_tpr(vcpu) ||
4828 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004829 return;
4830
Radim Krčmář596f3142014-03-11 19:11:18 +01004831 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
4832
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004833 if (irr == -1)
4834 return;
4835
4836 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004837 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004838}
4839
Yang Zhang8d146952013-01-25 10:18:50 +08004840static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
4841{
4842 return;
4843}
4844
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05004845static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004846{
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05004847 return avic && irqchip_split(vcpu->kvm);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004848}
4849
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004850static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
4851{
4852}
4853
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02004854static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004855{
4856}
4857
4858/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03004859static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4860{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004861 struct vcpu_svm *svm = to_svm(vcpu);
4862 struct vmcb *vmcb = svm->vmcb;
4863
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05004864 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004865 return;
4866
4867 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
4868 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004869}
4870
Andrey Smetanin63086302015-11-10 15:36:32 +03004871static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004872{
4873 return;
4874}
4875
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004876static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
4877{
4878 kvm_lapic_set_irr(vec, vcpu->arch.apic);
4879 smp_mb__after_atomic();
4880
4881 if (avic_vcpu_is_running(vcpu))
4882 wrmsrl(SVM_AVIC_DOORBELL,
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05004883 kvm_cpu_get_apicid(vcpu->cpu));
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004884 else
4885 kvm_vcpu_wake_up(vcpu);
4886}
4887
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004888static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4889{
4890 unsigned long flags;
4891 struct amd_svm_iommu_ir *cur;
4892
4893 spin_lock_irqsave(&svm->ir_list_lock, flags);
4894 list_for_each_entry(cur, &svm->ir_list, node) {
4895 if (cur->data != pi->ir_data)
4896 continue;
4897 list_del(&cur->node);
4898 kfree(cur);
4899 break;
4900 }
4901 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4902}
4903
4904static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4905{
4906 int ret = 0;
4907 unsigned long flags;
4908 struct amd_svm_iommu_ir *ir;
4909
4910 /**
4911 * In some cases, the existing irte is updaed and re-set,
4912 * so we need to check here if it's already been * added
4913 * to the ir_list.
4914 */
4915 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
4916 struct kvm *kvm = svm->vcpu.kvm;
4917 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
4918 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
4919 struct vcpu_svm *prev_svm;
4920
4921 if (!prev_vcpu) {
4922 ret = -EINVAL;
4923 goto out;
4924 }
4925
4926 prev_svm = to_svm(prev_vcpu);
4927 svm_ir_list_del(prev_svm, pi);
4928 }
4929
4930 /**
4931 * Allocating new amd_iommu_pi_data, which will get
4932 * add to the per-vcpu ir_list.
4933 */
4934 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
4935 if (!ir) {
4936 ret = -ENOMEM;
4937 goto out;
4938 }
4939 ir->data = pi->ir_data;
4940
4941 spin_lock_irqsave(&svm->ir_list_lock, flags);
4942 list_add(&ir->node, &svm->ir_list);
4943 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4944out:
4945 return ret;
4946}
4947
4948/**
4949 * Note:
4950 * The HW cannot support posting multicast/broadcast
4951 * interrupts to a vCPU. So, we still use legacy interrupt
4952 * remapping for these kind of interrupts.
4953 *
4954 * For lowest-priority interrupts, we only support
4955 * those with single CPU as the destination, e.g. user
4956 * configures the interrupts via /proc/irq or uses
4957 * irqbalance to make the interrupts single-CPU.
4958 */
4959static int
4960get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
4961 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
4962{
4963 struct kvm_lapic_irq irq;
4964 struct kvm_vcpu *vcpu = NULL;
4965
4966 kvm_set_msi_irq(kvm, e, &irq);
4967
4968 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
4969 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
4970 __func__, irq.vector);
4971 return -1;
4972 }
4973
4974 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
4975 irq.vector);
4976 *svm = to_svm(vcpu);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05004977 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004978 vcpu_info->vector = irq.vector;
4979
4980 return 0;
4981}
4982
4983/*
4984 * svm_update_pi_irte - set IRTE for Posted-Interrupts
4985 *
4986 * @kvm: kvm
4987 * @host_irq: host irq of the interrupt
4988 * @guest_irq: gsi of the interrupt
4989 * @set: set or unset PI
4990 * returns 0 on success, < 0 on failure
4991 */
4992static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
4993 uint32_t guest_irq, bool set)
4994{
4995 struct kvm_kernel_irq_routing_entry *e;
4996 struct kvm_irq_routing_table *irq_rt;
4997 int idx, ret = -EINVAL;
4998
4999 if (!kvm_arch_has_assigned_device(kvm) ||
5000 !irq_remapping_cap(IRQ_POSTING_CAP))
5001 return 0;
5002
5003 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
5004 __func__, host_irq, guest_irq, set);
5005
5006 idx = srcu_read_lock(&kvm->irq_srcu);
5007 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
5008 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
5009
5010 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
5011 struct vcpu_data vcpu_info;
5012 struct vcpu_svm *svm = NULL;
5013
5014 if (e->type != KVM_IRQ_ROUTING_MSI)
5015 continue;
5016
5017 /**
5018 * Here, we setup with legacy mode in the following cases:
5019 * 1. When cannot target interrupt to a specific vcpu.
5020 * 2. Unsetting posted interrupt.
5021 * 3. APIC virtialization is disabled for the vcpu.
5022 */
5023 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
5024 kvm_vcpu_apicv_active(&svm->vcpu)) {
5025 struct amd_iommu_pi_data pi;
5026
5027 /* Try to enable guest_mode in IRTE */
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005028 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
5029 AVIC_HPA_MASK);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005030 pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id,
5031 svm->vcpu.vcpu_id);
5032 pi.is_guest_mode = true;
5033 pi.vcpu_data = &vcpu_info;
5034 ret = irq_set_vcpu_affinity(host_irq, &pi);
5035
5036 /**
5037 * Here, we successfully setting up vcpu affinity in
5038 * IOMMU guest mode. Now, we need to store the posted
5039 * interrupt information in a per-vcpu ir_list so that
5040 * we can reference to them directly when we update vcpu
5041 * scheduling information in IOMMU irte.
5042 */
5043 if (!ret && pi.is_guest_mode)
5044 svm_ir_list_add(svm, &pi);
5045 } else {
5046 /* Use legacy mode in IRTE */
5047 struct amd_iommu_pi_data pi;
5048
5049 /**
5050 * Here, pi is used to:
5051 * - Tell IOMMU to use legacy mode for this interrupt.
5052 * - Retrieve ga_tag of prior interrupt remapping data.
5053 */
5054 pi.is_guest_mode = false;
5055 ret = irq_set_vcpu_affinity(host_irq, &pi);
5056
5057 /**
5058 * Check if the posted interrupt was previously
5059 * setup with the guest_mode by checking if the ga_tag
5060 * was cached. If so, we need to clean up the per-vcpu
5061 * ir_list.
5062 */
5063 if (!ret && pi.prev_ga_tag) {
5064 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
5065 struct kvm_vcpu *vcpu;
5066
5067 vcpu = kvm_get_vcpu_by_id(kvm, id);
5068 if (vcpu)
5069 svm_ir_list_del(to_svm(vcpu), &pi);
5070 }
5071 }
5072
5073 if (!ret && svm) {
5074 trace_kvm_pi_irte_update(svm->vcpu.vcpu_id,
5075 host_irq, e->gsi,
5076 vcpu_info.vector,
5077 vcpu_info.pi_desc_addr, set);
5078 }
5079
5080 if (ret < 0) {
5081 pr_err("%s: failed to update PI IRTE\n", __func__);
5082 goto out;
5083 }
5084 }
5085
5086 ret = 0;
5087out:
5088 srcu_read_unlock(&kvm->irq_srcu, idx);
5089 return ret;
5090}
5091
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005092static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005093{
5094 struct vcpu_svm *svm = to_svm(vcpu);
5095 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02005096 int ret;
5097 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
5098 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
5099 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
5100
5101 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005102}
5103
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005104static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
5105{
5106 struct vcpu_svm *svm = to_svm(vcpu);
5107
5108 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
5109}
5110
5111static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5112{
5113 struct vcpu_svm *svm = to_svm(vcpu);
5114
5115 if (masked) {
5116 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005117 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005118 } else {
5119 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005120 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005121 }
5122}
5123
Gleb Natapov78646122009-03-23 12:12:11 +02005124static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
5125{
5126 struct vcpu_svm *svm = to_svm(vcpu);
5127 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005128 int ret;
5129
5130 if (!gif_set(svm) ||
5131 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
5132 return 0;
5133
Avi Kivityf6e78472010-08-02 15:30:20 +03005134 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005135
Joerg Roedel20307532010-11-29 17:51:48 +01005136 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005137 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
5138
5139 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02005140}
5141
Jan Kiszkac9a79532014-03-07 20:03:15 +01005142static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03005143{
Alexander Graf219b65d2009-06-15 15:21:25 +02005144 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02005145
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005146 if (kvm_vcpu_apicv_active(vcpu))
5147 return;
5148
Joerg Roedele0231712010-02-24 18:59:10 +01005149 /*
5150 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
5151 * 1, because that's a separate STGI/VMRUN intercept. The next time we
5152 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005153 * we'll get the vintr intercept. However, if the vGIF feature is
5154 * enabled, the STGI interception will not occur. Enable the irq
5155 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01005156 */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005157 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02005158 svm_set_vintr(svm);
5159 svm_inject_irq(svm, 0x0);
5160 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005161}
5162
Jan Kiszkac9a79532014-03-07 20:03:15 +01005163static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005164{
Avi Kivity04d2cc72007-09-10 18:10:54 +03005165 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005166
Gleb Natapov44c11432009-05-11 13:35:52 +03005167 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
5168 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01005169 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03005170
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005171 if (!gif_set(svm)) {
5172 if (vgif_enabled(svm))
5173 set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005174 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005175 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005176
5177 if (svm->nested.exit_required)
5178 return; /* we're not going to run the guest yet */
5179
Joerg Roedele0231712010-02-24 18:59:10 +01005180 /*
5181 * Something prevents NMI from been injected. Single step over possible
5182 * problem (IRET or exception injection or interrupt shadow)
5183 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02005184 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02005185 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03005186 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03005187}
5188
Izik Eiduscbc94022007-10-25 00:29:55 +02005189static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
5190{
5191 return 0;
5192}
5193
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005194static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Avi Kivityd9e368d2007-06-07 19:18:30 +03005195{
Joerg Roedel38e5e922010-12-03 15:25:16 +01005196 struct vcpu_svm *svm = to_svm(vcpu);
5197
5198 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
5199 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5200 else
5201 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03005202}
5203
Avi Kivity04d2cc72007-09-10 18:10:54 +03005204static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
5205{
5206}
5207
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005208static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
5209{
5210 struct vcpu_svm *svm = to_svm(vcpu);
5211
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005212 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005213 return;
5214
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005215 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005216 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03005217 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005218 }
5219}
5220
Joerg Roedel649d6862008-04-16 16:51:15 +02005221static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
5222{
5223 struct vcpu_svm *svm = to_svm(vcpu);
5224 u64 cr8;
5225
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005226 if (svm_nested_virtualize_tpr(vcpu) ||
5227 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005228 return;
5229
Joerg Roedel649d6862008-04-16 16:51:15 +02005230 cr8 = kvm_get_cr8(vcpu);
5231 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
5232 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
5233}
5234
Gleb Natapov9222be12009-04-23 17:14:37 +03005235static void svm_complete_interrupts(struct vcpu_svm *svm)
5236{
5237 u8 vector;
5238 int type;
5239 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01005240 unsigned int3_injected = svm->int3_injected;
5241
5242 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005243
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02005244 /*
5245 * If we've made progress since setting HF_IRET_MASK, we've
5246 * executed an IRET and can allow NMI injection.
5247 */
5248 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
5249 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03005250 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03005251 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5252 }
Gleb Natapov44c11432009-05-11 13:35:52 +03005253
Gleb Natapov9222be12009-04-23 17:14:37 +03005254 svm->vcpu.arch.nmi_injected = false;
5255 kvm_clear_exception_queue(&svm->vcpu);
5256 kvm_clear_interrupt_queue(&svm->vcpu);
5257
5258 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
5259 return;
5260
Avi Kivity3842d132010-07-27 12:30:24 +03005261 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5262
Gleb Natapov9222be12009-04-23 17:14:37 +03005263 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
5264 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
5265
5266 switch (type) {
5267 case SVM_EXITINTINFO_TYPE_NMI:
5268 svm->vcpu.arch.nmi_injected = true;
5269 break;
5270 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01005271 /*
5272 * In case of software exceptions, do not reinject the vector,
5273 * but re-execute the instruction instead. Rewind RIP first
5274 * if we emulated INT3 before.
5275 */
5276 if (kvm_exception_is_soft(vector)) {
5277 if (vector == BP_VECTOR && int3_injected &&
5278 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
5279 kvm_rip_write(&svm->vcpu,
5280 kvm_rip_read(&svm->vcpu) -
5281 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02005282 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01005283 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005284 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
5285 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005286 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03005287
5288 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005289 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03005290 break;
5291 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005292 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03005293 break;
5294 default:
5295 break;
5296 }
5297}
5298
Avi Kivityb463a6f2010-07-20 15:06:17 +03005299static void svm_cancel_injection(struct kvm_vcpu *vcpu)
5300{
5301 struct vcpu_svm *svm = to_svm(vcpu);
5302 struct vmcb_control_area *control = &svm->vmcb->control;
5303
5304 control->exit_int_info = control->event_inj;
5305 control->exit_int_info_err = control->event_inj_err;
5306 control->event_inj = 0;
5307 svm_complete_interrupts(svm);
5308}
5309
Avi Kivity851ba692009-08-24 11:10:17 +03005310static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005311{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005312 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03005313
Joerg Roedel2041a062010-04-22 12:33:08 +02005314 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5315 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5316 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5317
Joerg Roedelcd3ff652009-10-09 16:08:26 +02005318 /*
5319 * A vmexit emulation is required before the vcpu can be executed
5320 * again.
5321 */
5322 if (unlikely(svm->nested.exit_required))
5323 return;
5324
Ladi Proseka12713c2017-06-21 09:07:00 +02005325 /*
5326 * Disable singlestep if we're injecting an interrupt/exception.
5327 * We don't want our modified rflags to be pushed on the stack where
5328 * we might not be able to easily reset them if we disabled NMI
5329 * singlestep later.
5330 */
5331 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5332 /*
5333 * Event injection happens before external interrupts cause a
5334 * vmexit and interrupts are disabled here, so smp_send_reschedule
5335 * is enough to force an immediate vmexit.
5336 */
5337 disable_nmi_singlestep(svm);
5338 smp_send_reschedule(vcpu->cpu);
5339 }
5340
Rusty Russelle756fc62007-07-30 20:07:08 +10005341 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005342
Joerg Roedel649d6862008-04-16 16:51:15 +02005343 sync_lapic_to_cr8(vcpu);
5344
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02005345 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005346
Avi Kivity04d2cc72007-09-10 18:10:54 +03005347 clgi();
5348
5349 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08005350
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005351 /*
5352 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
5353 * it's non-zero. Since vmentry is serialising on affected CPUs, there
5354 * is no need to worry about the conditional branch over the wrmsr
5355 * being speculatively taken.
5356 */
5357 if (svm->spec_ctrl)
5358 wrmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl);
5359
Avi Kivity6aa8b732006-12-10 02:21:36 -08005360 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03005361 "push %%" _ASM_BP "; \n\t"
5362 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5363 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5364 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5365 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5366 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5367 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005368#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005369 "mov %c[r8](%[svm]), %%r8 \n\t"
5370 "mov %c[r9](%[svm]), %%r9 \n\t"
5371 "mov %c[r10](%[svm]), %%r10 \n\t"
5372 "mov %c[r11](%[svm]), %%r11 \n\t"
5373 "mov %c[r12](%[svm]), %%r12 \n\t"
5374 "mov %c[r13](%[svm]), %%r13 \n\t"
5375 "mov %c[r14](%[svm]), %%r14 \n\t"
5376 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005377#endif
5378
Avi Kivity6aa8b732006-12-10 02:21:36 -08005379 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03005380 "push %%" _ASM_AX " \n\t"
5381 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03005382 __ex(SVM_VMLOAD) "\n\t"
5383 __ex(SVM_VMRUN) "\n\t"
5384 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005385 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005386
5387 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03005388 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5389 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5390 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5391 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5392 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5393 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005394#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005395 "mov %%r8, %c[r8](%[svm]) \n\t"
5396 "mov %%r9, %c[r9](%[svm]) \n\t"
5397 "mov %%r10, %c[r10](%[svm]) \n\t"
5398 "mov %%r11, %c[r11](%[svm]) \n\t"
5399 "mov %%r12, %c[r12](%[svm]) \n\t"
5400 "mov %%r13, %c[r13](%[svm]) \n\t"
5401 "mov %%r14, %c[r14](%[svm]) \n\t"
5402 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005403#endif
Jim Mattson0cb5b302018-01-03 14:31:38 -08005404 /*
5405 * Clear host registers marked as clobbered to prevent
5406 * speculative use.
5407 */
5408 "xor %%" _ASM_BX ", %%" _ASM_BX " \n\t"
5409 "xor %%" _ASM_CX ", %%" _ASM_CX " \n\t"
5410 "xor %%" _ASM_DX ", %%" _ASM_DX " \n\t"
5411 "xor %%" _ASM_SI ", %%" _ASM_SI " \n\t"
5412 "xor %%" _ASM_DI ", %%" _ASM_DI " \n\t"
5413#ifdef CONFIG_X86_64
5414 "xor %%r8, %%r8 \n\t"
5415 "xor %%r9, %%r9 \n\t"
5416 "xor %%r10, %%r10 \n\t"
5417 "xor %%r11, %%r11 \n\t"
5418 "xor %%r12, %%r12 \n\t"
5419 "xor %%r13, %%r13 \n\t"
5420 "xor %%r14, %%r14 \n\t"
5421 "xor %%r15, %%r15 \n\t"
5422#endif
Avi Kivity74547662012-09-16 15:10:59 +03005423 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08005424 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005425 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08005426 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005427 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5428 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5429 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5430 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5431 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5432 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005433#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005434 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5435 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5436 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5437 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5438 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5439 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5440 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5441 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005442#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02005443 : "cc", "memory"
5444#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03005445 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005446 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03005447#else
5448 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005449#endif
5450 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08005451
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005452 /*
5453 * We do not use IBRS in the kernel. If this vCPU has used the
5454 * SPEC_CTRL MSR it may have left it on; save the value and
5455 * turn it off. This is much more efficient than blindly adding
5456 * it to the atomic save/restore list. Especially as the former
5457 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
5458 *
5459 * For non-nested case:
5460 * If the L01 MSR bitmap does not intercept the MSR, then we need to
5461 * save it.
5462 *
5463 * For nested case:
5464 * If the L02 MSR bitmap does not intercept the MSR, then we need to
5465 * save it.
5466 */
5467 if (!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))
5468 rdmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl);
5469
5470 if (svm->spec_ctrl)
5471 wrmsrl(MSR_IA32_SPEC_CTRL, 0);
5472
David Woodhouse117cc7a2018-01-12 11:11:27 +00005473 /* Eliminate branch target predictions from guest mode */
5474 vmexit_fill_RSB();
5475
Avi Kivity82ca2d12010-10-21 12:20:34 +02005476#ifdef CONFIG_X86_64
5477 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5478#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02005479 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02005480#ifndef CONFIG_X86_32_LAZY_GS
5481 loadsegment(gs, svm->host.gs);
5482#endif
Avi Kivity9581d442010-10-19 16:46:55 +02005483#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005484
5485 reload_tss(vcpu);
5486
Avi Kivity56ba47d2007-11-07 17:14:18 +02005487 local_irq_disable();
5488
Avi Kivity13c34e02010-10-21 12:20:31 +02005489 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5490 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5491 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5492 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5493
Joerg Roedel3781c012011-01-14 16:45:02 +01005494 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5495 kvm_before_handle_nmi(&svm->vcpu);
5496
5497 stgi();
5498
5499 /* Any pending NMI will happen here */
5500
5501 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5502 kvm_after_handle_nmi(&svm->vcpu);
5503
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005504 sync_cr8_to_lapic(vcpu);
5505
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005506 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005507
Joerg Roedel38e5e922010-12-03 15:25:16 +01005508 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5509
Gleb Natapov631bc482010-10-14 11:22:52 +02005510 /* if exit due to PF check for async PF */
5511 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005512 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
Gleb Natapov631bc482010-10-14 11:22:52 +02005513
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005514 if (npt_enabled) {
5515 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5516 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5517 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02005518
5519 /*
5520 * We need to handle MC intercepts here before the vcpu has a chance to
5521 * change the physical cpu
5522 */
5523 if (unlikely(svm->vmcb->control.exit_code ==
5524 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5525 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01005526
5527 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005528}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005529STACK_FRAME_NON_STANDARD(svm_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005530
Avi Kivity6aa8b732006-12-10 02:21:36 -08005531static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5532{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005533 struct vcpu_svm *svm = to_svm(vcpu);
5534
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005535 svm->vmcb->save.cr3 = __sme_set(root);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005536 mark_dirty(svm->vmcb, VMCB_CR);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005537 svm_flush_tlb(vcpu, true);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005538}
5539
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005540static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5541{
5542 struct vcpu_svm *svm = to_svm(vcpu);
5543
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005544 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01005545 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005546
5547 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02005548 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005549 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005550
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005551 svm_flush_tlb(vcpu, true);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005552}
5553
Avi Kivity6aa8b732006-12-10 02:21:36 -08005554static int is_disabled(void)
5555{
Joerg Roedel6031a612007-06-22 12:29:50 +03005556 u64 vm_cr;
5557
5558 rdmsrl(MSR_VM_CR, vm_cr);
5559 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5560 return 1;
5561
Avi Kivity6aa8b732006-12-10 02:21:36 -08005562 return 0;
5563}
5564
Ingo Molnar102d8322007-02-19 14:37:47 +02005565static void
5566svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5567{
5568 /*
5569 * Patch in the VMMCALL instruction:
5570 */
5571 hypercall[0] = 0x0f;
5572 hypercall[1] = 0x01;
5573 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02005574}
5575
Yang, Sheng002c7f72007-07-31 14:23:01 +03005576static void svm_check_processor_compat(void *rtn)
5577{
5578 *(int *)rtn = 0;
5579}
5580
Avi Kivity774ead32007-12-26 13:57:04 +02005581static bool svm_cpu_has_accelerated_tpr(void)
5582{
5583 return false;
5584}
5585
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005586static bool svm_has_high_real_mode_segbase(void)
5587{
5588 return true;
5589}
5590
Paolo Bonzinifc07e762015-10-01 13:20:22 +02005591static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5592{
5593 return 0;
5594}
5595
Sheng Yang0e851882009-12-18 16:48:46 +08005596static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5597{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005598 struct vcpu_svm *svm = to_svm(vcpu);
5599
5600 /* Update nrips enabled cache */
Radim Krčmářd6321d42017-08-05 00:12:49 +02005601 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005602
5603 if (!kvm_vcpu_apicv_active(vcpu))
5604 return;
5605
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02005606 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08005607}
5608
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005609static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5610{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005611 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005612 case 0x1:
5613 if (avic)
5614 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5615 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02005616 case 0x80000001:
5617 if (nested)
5618 entry->ecx |= (1 << 2); /* Set SVM bit */
5619 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005620 case 0x8000000A:
5621 entry->eax = 1; /* SVM revision 1 */
5622 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5623 ASID emulation to nested SVM */
5624 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02005625 entry->edx = 0; /* Per default do not support any
5626 additional features */
5627
5628 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02005629 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02005630 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005631
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005632 /* Support NPT for the guest if enabled */
5633 if (npt_enabled)
5634 entry->edx |= SVM_FEATURE_NPT;
5635
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005636 break;
Brijesh Singh8765d752017-12-04 10:57:25 -06005637 case 0x8000001F:
5638 /* Support memory encryption cpuid if host supports it */
5639 if (boot_cpu_has(X86_FEATURE_SEV))
5640 cpuid(0x8000001f, &entry->eax, &entry->ebx,
5641 &entry->ecx, &entry->edx);
5642
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005643 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005644}
5645
Sheng Yang17cc3932010-01-05 19:02:27 +08005646static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005647{
Sheng Yang17cc3932010-01-05 19:02:27 +08005648 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005649}
5650
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005651static bool svm_rdtscp_supported(void)
5652{
Paolo Bonzini46896c72015-11-12 14:49:16 +01005653 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005654}
5655
Mao, Junjiead756a12012-07-02 01:18:48 +00005656static bool svm_invpcid_supported(void)
5657{
5658 return false;
5659}
5660
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005661static bool svm_mpx_supported(void)
5662{
5663 return false;
5664}
5665
Wanpeng Li55412b22014-12-02 19:21:30 +08005666static bool svm_xsaves_supported(void)
5667{
5668 return false;
5669}
5670
Paolo Bonzini66336ca2016-07-12 10:36:41 +02005671static bool svm_umip_emulated(void)
5672{
5673 return false;
5674}
5675
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005676static bool svm_has_wbinvd_exit(void)
5677{
5678 return true;
5679}
5680
Joerg Roedel80612522011-04-04 12:39:33 +02005681#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005682 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005683#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005684 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005685#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005686 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005687
Mathias Krause09941fb2012-08-30 01:30:20 +02005688static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005689 u32 exit_code;
5690 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005691} x86_intercept_map[] = {
5692 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
5693 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
5694 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
5695 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
5696 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02005697 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
5698 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02005699 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
5700 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
5701 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
5702 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
5703 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
5704 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
5705 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
5706 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02005707 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
5708 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
5709 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
5710 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
5711 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
5712 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
5713 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
5714 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005715 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
5716 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
5717 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02005718 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
5719 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
5720 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
5721 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
5722 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
5723 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
5724 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
5725 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
5726 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02005727 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
5728 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
5729 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
5730 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
5731 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
5732 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
5733 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02005734 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
5735 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
5736 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
5737 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005738};
5739
Joerg Roedel80612522011-04-04 12:39:33 +02005740#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005741#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005742#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005743
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005744static int svm_check_intercept(struct kvm_vcpu *vcpu,
5745 struct x86_instruction_info *info,
5746 enum x86_intercept_stage stage)
5747{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005748 struct vcpu_svm *svm = to_svm(vcpu);
5749 int vmexit, ret = X86EMUL_CONTINUE;
5750 struct __x86_intercept icpt_info;
5751 struct vmcb *vmcb = svm->vmcb;
5752
5753 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
5754 goto out;
5755
5756 icpt_info = x86_intercept_map[info->intercept];
5757
Avi Kivity40e19b52011-04-21 12:35:41 +03005758 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005759 goto out;
5760
5761 switch (icpt_info.exit_code) {
5762 case SVM_EXIT_READ_CR0:
5763 if (info->intercept == x86_intercept_cr_read)
5764 icpt_info.exit_code += info->modrm_reg;
5765 break;
5766 case SVM_EXIT_WRITE_CR0: {
5767 unsigned long cr0, val;
5768 u64 intercept;
5769
5770 if (info->intercept == x86_intercept_cr_write)
5771 icpt_info.exit_code += info->modrm_reg;
5772
Jan Kiszka62baf442014-06-29 21:55:53 +02005773 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
5774 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005775 break;
5776
5777 intercept = svm->nested.intercept;
5778
5779 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
5780 break;
5781
5782 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
5783 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
5784
5785 if (info->intercept == x86_intercept_lmsw) {
5786 cr0 &= 0xfUL;
5787 val &= 0xfUL;
5788 /* lmsw can't clear PE - catch this here */
5789 if (cr0 & X86_CR0_PE)
5790 val |= X86_CR0_PE;
5791 }
5792
5793 if (cr0 ^ val)
5794 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
5795
5796 break;
5797 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02005798 case SVM_EXIT_READ_DR0:
5799 case SVM_EXIT_WRITE_DR0:
5800 icpt_info.exit_code += info->modrm_reg;
5801 break;
Joerg Roedel80612522011-04-04 12:39:33 +02005802 case SVM_EXIT_MSR:
5803 if (info->intercept == x86_intercept_wrmsr)
5804 vmcb->control.exit_info_1 = 1;
5805 else
5806 vmcb->control.exit_info_1 = 0;
5807 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02005808 case SVM_EXIT_PAUSE:
5809 /*
5810 * We get this for NOP only, but pause
5811 * is rep not, check this here
5812 */
5813 if (info->rep_prefix != REPE_PREFIX)
5814 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02005815 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02005816 case SVM_EXIT_IOIO: {
5817 u64 exit_info;
5818 u32 bytes;
5819
Joerg Roedelf6511932011-04-04 12:39:35 +02005820 if (info->intercept == x86_intercept_in ||
5821 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005822 exit_info = ((info->src_val & 0xffff) << 16) |
5823 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02005824 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02005825 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005826 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02005827 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02005828 }
5829
5830 if (info->intercept == x86_intercept_outs ||
5831 info->intercept == x86_intercept_ins)
5832 exit_info |= SVM_IOIO_STR_MASK;
5833
5834 if (info->rep_prefix)
5835 exit_info |= SVM_IOIO_REP_MASK;
5836
5837 bytes = min(bytes, 4u);
5838
5839 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
5840
5841 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
5842
5843 vmcb->control.exit_info_1 = exit_info;
5844 vmcb->control.exit_info_2 = info->next_rip;
5845
5846 break;
5847 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005848 default:
5849 break;
5850 }
5851
Bandan Dasf1047652015-06-11 02:05:33 -04005852 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
5853 if (static_cpu_has(X86_FEATURE_NRIPS))
5854 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005855 vmcb->control.exit_code = icpt_info.exit_code;
5856 vmexit = nested_svm_exit_handled(svm);
5857
5858 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
5859 : X86EMUL_CONTINUE;
5860
5861out:
5862 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005863}
5864
Yang Zhanga547c6d2013-04-11 19:25:10 +08005865static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
5866{
5867 local_irq_enable();
Paolo Bonzinif2485b32016-06-15 15:23:11 +02005868 /*
5869 * We must have an instruction with interrupts enabled, so
5870 * the timer interrupt isn't delayed by the interrupt shadow.
5871 */
5872 asm("nop");
5873 local_irq_disable();
Yang Zhanga547c6d2013-04-11 19:25:10 +08005874}
5875
Radim Krčmářae97a3b2014-08-21 18:08:06 +02005876static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
5877{
5878}
5879
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05005880static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
5881{
5882 if (avic_handle_apic_id_update(vcpu) != 0)
5883 return;
5884 if (avic_handle_dfr_update(vcpu) != 0)
5885 return;
5886 avic_handle_ldr_update(vcpu);
5887}
5888
Borislav Petkov74f16902017-03-26 23:51:24 +02005889static void svm_setup_mce(struct kvm_vcpu *vcpu)
5890{
5891 /* [63:9] are reserved. */
5892 vcpu->arch.mcg_cap &= 0x1ff;
5893}
5894
Ladi Prosek72d7b372017-10-11 16:54:41 +02005895static int svm_smi_allowed(struct kvm_vcpu *vcpu)
5896{
Ladi Prosek05cade72017-10-11 16:54:45 +02005897 struct vcpu_svm *svm = to_svm(vcpu);
5898
5899 /* Per APM Vol.2 15.22.2 "Response to SMI" */
5900 if (!gif_set(svm))
5901 return 0;
5902
5903 if (is_guest_mode(&svm->vcpu) &&
5904 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
5905 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
5906 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
5907 svm->nested.exit_required = true;
5908 return 0;
5909 }
5910
Ladi Prosek72d7b372017-10-11 16:54:41 +02005911 return 1;
5912}
5913
Ladi Prosek0234bf82017-10-11 16:54:40 +02005914static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
5915{
Ladi Prosek05cade72017-10-11 16:54:45 +02005916 struct vcpu_svm *svm = to_svm(vcpu);
5917 int ret;
5918
5919 if (is_guest_mode(vcpu)) {
5920 /* FED8h - SVM Guest */
5921 put_smstate(u64, smstate, 0x7ed8, 1);
5922 /* FEE0h - SVM Guest VMCB Physical Address */
5923 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
5924
5925 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5926 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5927 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5928
5929 ret = nested_svm_vmexit(svm);
5930 if (ret)
5931 return ret;
5932 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02005933 return 0;
5934}
5935
5936static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
5937{
Ladi Prosek05cade72017-10-11 16:54:45 +02005938 struct vcpu_svm *svm = to_svm(vcpu);
5939 struct vmcb *nested_vmcb;
5940 struct page *page;
5941 struct {
5942 u64 guest;
5943 u64 vmcb;
5944 } svm_state_save;
5945 int ret;
5946
5947 ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save,
5948 sizeof(svm_state_save));
5949 if (ret)
5950 return ret;
5951
5952 if (svm_state_save.guest) {
5953 vcpu->arch.hflags &= ~HF_SMM_MASK;
5954 nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page);
5955 if (nested_vmcb)
5956 enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page);
5957 else
5958 ret = 1;
5959 vcpu->arch.hflags |= HF_SMM_MASK;
5960 }
5961 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02005962}
5963
Ladi Prosekcc3d9672017-10-17 16:02:39 +02005964static int enable_smi_window(struct kvm_vcpu *vcpu)
5965{
5966 struct vcpu_svm *svm = to_svm(vcpu);
5967
5968 if (!gif_set(svm)) {
5969 if (vgif_enabled(svm))
5970 set_intercept(svm, INTERCEPT_STGI);
5971 /* STGI will cause a vm exit */
5972 return 1;
5973 }
5974 return 0;
5975}
5976
Brijesh Singh1654efc2017-12-04 10:57:34 -06005977static int sev_asid_new(void)
5978{
5979 int pos;
5980
5981 /*
5982 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
5983 */
5984 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
5985 if (pos >= max_sev_asid)
5986 return -EBUSY;
5987
5988 set_bit(pos, sev_asid_bitmap);
5989 return pos + 1;
5990}
5991
5992static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
5993{
5994 struct kvm_sev_info *sev = &kvm->arch.sev_info;
5995 int asid, ret;
5996
5997 ret = -EBUSY;
5998 asid = sev_asid_new();
5999 if (asid < 0)
6000 return ret;
6001
6002 ret = sev_platform_init(&argp->error);
6003 if (ret)
6004 goto e_free;
6005
6006 sev->active = true;
6007 sev->asid = asid;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006008 INIT_LIST_HEAD(&sev->regions_list);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006009
6010 return 0;
6011
6012e_free:
6013 __sev_asid_free(asid);
6014 return ret;
6015}
6016
Brijesh Singh59414c92017-12-04 10:57:35 -06006017static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
6018{
6019 struct sev_data_activate *data;
6020 int asid = sev_get_asid(kvm);
6021 int ret;
6022
6023 wbinvd_on_all_cpus();
6024
6025 ret = sev_guest_df_flush(error);
6026 if (ret)
6027 return ret;
6028
6029 data = kzalloc(sizeof(*data), GFP_KERNEL);
6030 if (!data)
6031 return -ENOMEM;
6032
6033 /* activate ASID on the given handle */
6034 data->handle = handle;
6035 data->asid = asid;
6036 ret = sev_guest_activate(data, error);
6037 kfree(data);
6038
6039 return ret;
6040}
6041
Brijesh Singh89c50582017-12-04 10:57:35 -06006042static int __sev_issue_cmd(int fd, int id, void *data, int *error)
Brijesh Singh59414c92017-12-04 10:57:35 -06006043{
6044 struct fd f;
6045 int ret;
6046
6047 f = fdget(fd);
6048 if (!f.file)
6049 return -EBADF;
6050
6051 ret = sev_issue_cmd_external_user(f.file, id, data, error);
6052
6053 fdput(f);
6054 return ret;
6055}
6056
Brijesh Singh89c50582017-12-04 10:57:35 -06006057static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
6058{
6059 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6060
6061 return __sev_issue_cmd(sev->fd, id, data, error);
6062}
6063
Brijesh Singh59414c92017-12-04 10:57:35 -06006064static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
6065{
6066 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6067 struct sev_data_launch_start *start;
6068 struct kvm_sev_launch_start params;
6069 void *dh_blob, *session_blob;
6070 int *error = &argp->error;
6071 int ret;
6072
6073 if (!sev_guest(kvm))
6074 return -ENOTTY;
6075
6076 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6077 return -EFAULT;
6078
6079 start = kzalloc(sizeof(*start), GFP_KERNEL);
6080 if (!start)
6081 return -ENOMEM;
6082
6083 dh_blob = NULL;
6084 if (params.dh_uaddr) {
6085 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
6086 if (IS_ERR(dh_blob)) {
6087 ret = PTR_ERR(dh_blob);
6088 goto e_free;
6089 }
6090
6091 start->dh_cert_address = __sme_set(__pa(dh_blob));
6092 start->dh_cert_len = params.dh_len;
6093 }
6094
6095 session_blob = NULL;
6096 if (params.session_uaddr) {
6097 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
6098 if (IS_ERR(session_blob)) {
6099 ret = PTR_ERR(session_blob);
6100 goto e_free_dh;
6101 }
6102
6103 start->session_address = __sme_set(__pa(session_blob));
6104 start->session_len = params.session_len;
6105 }
6106
6107 start->handle = params.handle;
6108 start->policy = params.policy;
6109
6110 /* create memory encryption context */
Brijesh Singh89c50582017-12-04 10:57:35 -06006111 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
Brijesh Singh59414c92017-12-04 10:57:35 -06006112 if (ret)
6113 goto e_free_session;
6114
6115 /* Bind ASID to this guest */
6116 ret = sev_bind_asid(kvm, start->handle, error);
6117 if (ret)
6118 goto e_free_session;
6119
6120 /* return handle to userspace */
6121 params.handle = start->handle;
6122 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
6123 sev_unbind_asid(kvm, start->handle);
6124 ret = -EFAULT;
6125 goto e_free_session;
6126 }
6127
6128 sev->handle = start->handle;
6129 sev->fd = argp->sev_fd;
6130
6131e_free_session:
6132 kfree(session_blob);
6133e_free_dh:
6134 kfree(dh_blob);
6135e_free:
6136 kfree(start);
6137 return ret;
6138}
6139
Brijesh Singh89c50582017-12-04 10:57:35 -06006140static int get_num_contig_pages(int idx, struct page **inpages,
6141 unsigned long npages)
6142{
6143 unsigned long paddr, next_paddr;
6144 int i = idx + 1, pages = 1;
6145
6146 /* find the number of contiguous pages starting from idx */
6147 paddr = __sme_page_pa(inpages[idx]);
6148 while (i < npages) {
6149 next_paddr = __sme_page_pa(inpages[i++]);
6150 if ((paddr + PAGE_SIZE) == next_paddr) {
6151 pages++;
6152 paddr = next_paddr;
6153 continue;
6154 }
6155 break;
6156 }
6157
6158 return pages;
6159}
6160
6161static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
6162{
6163 unsigned long vaddr, vaddr_end, next_vaddr, npages, size;
6164 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6165 struct kvm_sev_launch_update_data params;
6166 struct sev_data_launch_update_data *data;
6167 struct page **inpages;
6168 int i, ret, pages;
6169
6170 if (!sev_guest(kvm))
6171 return -ENOTTY;
6172
6173 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6174 return -EFAULT;
6175
6176 data = kzalloc(sizeof(*data), GFP_KERNEL);
6177 if (!data)
6178 return -ENOMEM;
6179
6180 vaddr = params.uaddr;
6181 size = params.len;
6182 vaddr_end = vaddr + size;
6183
6184 /* Lock the user memory. */
6185 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
6186 if (!inpages) {
6187 ret = -ENOMEM;
6188 goto e_free;
6189 }
6190
6191 /*
6192 * The LAUNCH_UPDATE command will perform in-place encryption of the
6193 * memory content (i.e it will write the same memory region with C=1).
6194 * It's possible that the cache may contain the data with C=0, i.e.,
6195 * unencrypted so invalidate it first.
6196 */
6197 sev_clflush_pages(inpages, npages);
6198
6199 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
6200 int offset, len;
6201
6202 /*
6203 * If the user buffer is not page-aligned, calculate the offset
6204 * within the page.
6205 */
6206 offset = vaddr & (PAGE_SIZE - 1);
6207
6208 /* Calculate the number of pages that can be encrypted in one go. */
6209 pages = get_num_contig_pages(i, inpages, npages);
6210
6211 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
6212
6213 data->handle = sev->handle;
6214 data->len = len;
6215 data->address = __sme_page_pa(inpages[i]) + offset;
6216 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
6217 if (ret)
6218 goto e_unpin;
6219
6220 size -= len;
6221 next_vaddr = vaddr + len;
6222 }
6223
6224e_unpin:
6225 /* content of memory is updated, mark pages dirty */
6226 for (i = 0; i < npages; i++) {
6227 set_page_dirty_lock(inpages[i]);
6228 mark_page_accessed(inpages[i]);
6229 }
6230 /* unlock the user pages */
6231 sev_unpin_memory(kvm, inpages, npages);
6232e_free:
6233 kfree(data);
6234 return ret;
6235}
6236
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006237static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
6238{
Brijesh Singh3e233382018-02-23 12:36:50 -06006239 void __user *measure = (void __user *)(uintptr_t)argp->data;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006240 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6241 struct sev_data_launch_measure *data;
6242 struct kvm_sev_launch_measure params;
Brijesh Singh3e233382018-02-23 12:36:50 -06006243 void __user *p = NULL;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006244 void *blob = NULL;
6245 int ret;
6246
6247 if (!sev_guest(kvm))
6248 return -ENOTTY;
6249
Brijesh Singh3e233382018-02-23 12:36:50 -06006250 if (copy_from_user(&params, measure, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006251 return -EFAULT;
6252
6253 data = kzalloc(sizeof(*data), GFP_KERNEL);
6254 if (!data)
6255 return -ENOMEM;
6256
6257 /* User wants to query the blob length */
6258 if (!params.len)
6259 goto cmd;
6260
Brijesh Singh3e233382018-02-23 12:36:50 -06006261 p = (void __user *)(uintptr_t)params.uaddr;
6262 if (p) {
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006263 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
6264 ret = -EINVAL;
6265 goto e_free;
6266 }
6267
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006268 ret = -ENOMEM;
6269 blob = kmalloc(params.len, GFP_KERNEL);
6270 if (!blob)
6271 goto e_free;
6272
6273 data->address = __psp_pa(blob);
6274 data->len = params.len;
6275 }
6276
6277cmd:
6278 data->handle = sev->handle;
6279 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
6280
6281 /*
6282 * If we query the session length, FW responded with expected data.
6283 */
6284 if (!params.len)
6285 goto done;
6286
6287 if (ret)
6288 goto e_free_blob;
6289
6290 if (blob) {
Brijesh Singh3e233382018-02-23 12:36:50 -06006291 if (copy_to_user(p, blob, params.len))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006292 ret = -EFAULT;
6293 }
6294
6295done:
6296 params.len = data->len;
Brijesh Singh3e233382018-02-23 12:36:50 -06006297 if (copy_to_user(measure, &params, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006298 ret = -EFAULT;
6299e_free_blob:
6300 kfree(blob);
6301e_free:
6302 kfree(data);
6303 return ret;
6304}
6305
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006306static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
6307{
6308 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6309 struct sev_data_launch_finish *data;
6310 int ret;
6311
6312 if (!sev_guest(kvm))
6313 return -ENOTTY;
6314
6315 data = kzalloc(sizeof(*data), GFP_KERNEL);
6316 if (!data)
6317 return -ENOMEM;
6318
6319 data->handle = sev->handle;
6320 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
6321
6322 kfree(data);
6323 return ret;
6324}
6325
Brijesh Singh255d9e72017-12-04 10:57:37 -06006326static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
6327{
6328 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6329 struct kvm_sev_guest_status params;
6330 struct sev_data_guest_status *data;
6331 int ret;
6332
6333 if (!sev_guest(kvm))
6334 return -ENOTTY;
6335
6336 data = kzalloc(sizeof(*data), GFP_KERNEL);
6337 if (!data)
6338 return -ENOMEM;
6339
6340 data->handle = sev->handle;
6341 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
6342 if (ret)
6343 goto e_free;
6344
6345 params.policy = data->policy;
6346 params.state = data->state;
6347 params.handle = data->handle;
6348
6349 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6350 ret = -EFAULT;
6351e_free:
6352 kfree(data);
6353 return ret;
6354}
6355
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006356static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
6357 unsigned long dst, int size,
6358 int *error, bool enc)
6359{
6360 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6361 struct sev_data_dbg *data;
6362 int ret;
6363
6364 data = kzalloc(sizeof(*data), GFP_KERNEL);
6365 if (!data)
6366 return -ENOMEM;
6367
6368 data->handle = sev->handle;
6369 data->dst_addr = dst;
6370 data->src_addr = src;
6371 data->len = size;
6372
6373 ret = sev_issue_cmd(kvm,
6374 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
6375 data, error);
6376 kfree(data);
6377 return ret;
6378}
6379
6380static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
6381 unsigned long dst_paddr, int sz, int *err)
6382{
6383 int offset;
6384
6385 /*
6386 * Its safe to read more than we are asked, caller should ensure that
6387 * destination has enough space.
6388 */
6389 src_paddr = round_down(src_paddr, 16);
6390 offset = src_paddr & 15;
6391 sz = round_up(sz + offset, 16);
6392
6393 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
6394}
6395
6396static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
6397 unsigned long __user dst_uaddr,
6398 unsigned long dst_paddr,
6399 int size, int *err)
6400{
6401 struct page *tpage = NULL;
6402 int ret, offset;
6403
6404 /* if inputs are not 16-byte then use intermediate buffer */
6405 if (!IS_ALIGNED(dst_paddr, 16) ||
6406 !IS_ALIGNED(paddr, 16) ||
6407 !IS_ALIGNED(size, 16)) {
6408 tpage = (void *)alloc_page(GFP_KERNEL);
6409 if (!tpage)
6410 return -ENOMEM;
6411
6412 dst_paddr = __sme_page_pa(tpage);
6413 }
6414
6415 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
6416 if (ret)
6417 goto e_free;
6418
6419 if (tpage) {
6420 offset = paddr & 15;
6421 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
6422 page_address(tpage) + offset, size))
6423 ret = -EFAULT;
6424 }
6425
6426e_free:
6427 if (tpage)
6428 __free_page(tpage);
6429
6430 return ret;
6431}
6432
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006433static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
6434 unsigned long __user vaddr,
6435 unsigned long dst_paddr,
6436 unsigned long __user dst_vaddr,
6437 int size, int *error)
6438{
6439 struct page *src_tpage = NULL;
6440 struct page *dst_tpage = NULL;
6441 int ret, len = size;
6442
6443 /* If source buffer is not aligned then use an intermediate buffer */
6444 if (!IS_ALIGNED(vaddr, 16)) {
6445 src_tpage = alloc_page(GFP_KERNEL);
6446 if (!src_tpage)
6447 return -ENOMEM;
6448
6449 if (copy_from_user(page_address(src_tpage),
6450 (void __user *)(uintptr_t)vaddr, size)) {
6451 __free_page(src_tpage);
6452 return -EFAULT;
6453 }
6454
6455 paddr = __sme_page_pa(src_tpage);
6456 }
6457
6458 /*
6459 * If destination buffer or length is not aligned then do read-modify-write:
6460 * - decrypt destination in an intermediate buffer
6461 * - copy the source buffer in an intermediate buffer
6462 * - use the intermediate buffer as source buffer
6463 */
6464 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
6465 int dst_offset;
6466
6467 dst_tpage = alloc_page(GFP_KERNEL);
6468 if (!dst_tpage) {
6469 ret = -ENOMEM;
6470 goto e_free;
6471 }
6472
6473 ret = __sev_dbg_decrypt(kvm, dst_paddr,
6474 __sme_page_pa(dst_tpage), size, error);
6475 if (ret)
6476 goto e_free;
6477
6478 /*
6479 * If source is kernel buffer then use memcpy() otherwise
6480 * copy_from_user().
6481 */
6482 dst_offset = dst_paddr & 15;
6483
6484 if (src_tpage)
6485 memcpy(page_address(dst_tpage) + dst_offset,
6486 page_address(src_tpage), size);
6487 else {
6488 if (copy_from_user(page_address(dst_tpage) + dst_offset,
6489 (void __user *)(uintptr_t)vaddr, size)) {
6490 ret = -EFAULT;
6491 goto e_free;
6492 }
6493 }
6494
6495 paddr = __sme_page_pa(dst_tpage);
6496 dst_paddr = round_down(dst_paddr, 16);
6497 len = round_up(size, 16);
6498 }
6499
6500 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
6501
6502e_free:
6503 if (src_tpage)
6504 __free_page(src_tpage);
6505 if (dst_tpage)
6506 __free_page(dst_tpage);
6507 return ret;
6508}
6509
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006510static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
6511{
6512 unsigned long vaddr, vaddr_end, next_vaddr;
6513 unsigned long dst_vaddr, dst_vaddr_end;
6514 struct page **src_p, **dst_p;
6515 struct kvm_sev_dbg debug;
6516 unsigned long n;
6517 int ret, size;
6518
6519 if (!sev_guest(kvm))
6520 return -ENOTTY;
6521
6522 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
6523 return -EFAULT;
6524
6525 vaddr = debug.src_uaddr;
6526 size = debug.len;
6527 vaddr_end = vaddr + size;
6528 dst_vaddr = debug.dst_uaddr;
6529 dst_vaddr_end = dst_vaddr + size;
6530
6531 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
6532 int len, s_off, d_off;
6533
6534 /* lock userspace source and destination page */
6535 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
6536 if (!src_p)
6537 return -EFAULT;
6538
6539 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
6540 if (!dst_p) {
6541 sev_unpin_memory(kvm, src_p, n);
6542 return -EFAULT;
6543 }
6544
6545 /*
6546 * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
6547 * memory content (i.e it will write the same memory region with C=1).
6548 * It's possible that the cache may contain the data with C=0, i.e.,
6549 * unencrypted so invalidate it first.
6550 */
6551 sev_clflush_pages(src_p, 1);
6552 sev_clflush_pages(dst_p, 1);
6553
6554 /*
6555 * Since user buffer may not be page aligned, calculate the
6556 * offset within the page.
6557 */
6558 s_off = vaddr & ~PAGE_MASK;
6559 d_off = dst_vaddr & ~PAGE_MASK;
6560 len = min_t(size_t, (PAGE_SIZE - s_off), size);
6561
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006562 if (dec)
6563 ret = __sev_dbg_decrypt_user(kvm,
6564 __sme_page_pa(src_p[0]) + s_off,
6565 dst_vaddr,
6566 __sme_page_pa(dst_p[0]) + d_off,
6567 len, &argp->error);
6568 else
6569 ret = __sev_dbg_encrypt_user(kvm,
6570 __sme_page_pa(src_p[0]) + s_off,
6571 vaddr,
6572 __sme_page_pa(dst_p[0]) + d_off,
6573 dst_vaddr,
6574 len, &argp->error);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006575
6576 sev_unpin_memory(kvm, src_p, 1);
6577 sev_unpin_memory(kvm, dst_p, 1);
6578
6579 if (ret)
6580 goto err;
6581
6582 next_vaddr = vaddr + len;
6583 dst_vaddr = dst_vaddr + len;
6584 size -= len;
6585 }
6586err:
6587 return ret;
6588}
6589
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006590static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
6591{
6592 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6593 struct sev_data_launch_secret *data;
6594 struct kvm_sev_launch_secret params;
6595 struct page **pages;
6596 void *blob, *hdr;
6597 unsigned long n;
6598 int ret;
6599
6600 if (!sev_guest(kvm))
6601 return -ENOTTY;
6602
6603 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6604 return -EFAULT;
6605
6606 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
6607 if (!pages)
6608 return -ENOMEM;
6609
6610 /*
6611 * The secret must be copied into contiguous memory region, lets verify
6612 * that userspace memory pages are contiguous before we issue command.
6613 */
6614 if (get_num_contig_pages(0, pages, n) != n) {
6615 ret = -EINVAL;
6616 goto e_unpin_memory;
6617 }
6618
6619 ret = -ENOMEM;
6620 data = kzalloc(sizeof(*data), GFP_KERNEL);
6621 if (!data)
6622 goto e_unpin_memory;
6623
6624 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
6625 if (IS_ERR(blob)) {
6626 ret = PTR_ERR(blob);
6627 goto e_free;
6628 }
6629
6630 data->trans_address = __psp_pa(blob);
6631 data->trans_len = params.trans_len;
6632
6633 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
6634 if (IS_ERR(hdr)) {
6635 ret = PTR_ERR(hdr);
6636 goto e_free_blob;
6637 }
6638 data->trans_address = __psp_pa(blob);
6639 data->trans_len = params.trans_len;
6640
6641 data->handle = sev->handle;
6642 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
6643
6644 kfree(hdr);
6645
6646e_free_blob:
6647 kfree(blob);
6648e_free:
6649 kfree(data);
6650e_unpin_memory:
6651 sev_unpin_memory(kvm, pages, n);
6652 return ret;
6653}
6654
Brijesh Singh1654efc2017-12-04 10:57:34 -06006655static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
6656{
6657 struct kvm_sev_cmd sev_cmd;
6658 int r;
6659
6660 if (!svm_sev_enabled())
6661 return -ENOTTY;
6662
6663 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
6664 return -EFAULT;
6665
6666 mutex_lock(&kvm->lock);
6667
6668 switch (sev_cmd.id) {
6669 case KVM_SEV_INIT:
6670 r = sev_guest_init(kvm, &sev_cmd);
6671 break;
Brijesh Singh59414c92017-12-04 10:57:35 -06006672 case KVM_SEV_LAUNCH_START:
6673 r = sev_launch_start(kvm, &sev_cmd);
6674 break;
Brijesh Singh89c50582017-12-04 10:57:35 -06006675 case KVM_SEV_LAUNCH_UPDATE_DATA:
6676 r = sev_launch_update_data(kvm, &sev_cmd);
6677 break;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006678 case KVM_SEV_LAUNCH_MEASURE:
6679 r = sev_launch_measure(kvm, &sev_cmd);
6680 break;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006681 case KVM_SEV_LAUNCH_FINISH:
6682 r = sev_launch_finish(kvm, &sev_cmd);
6683 break;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006684 case KVM_SEV_GUEST_STATUS:
6685 r = sev_guest_status(kvm, &sev_cmd);
6686 break;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006687 case KVM_SEV_DBG_DECRYPT:
6688 r = sev_dbg_crypt(kvm, &sev_cmd, true);
6689 break;
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006690 case KVM_SEV_DBG_ENCRYPT:
6691 r = sev_dbg_crypt(kvm, &sev_cmd, false);
6692 break;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006693 case KVM_SEV_LAUNCH_SECRET:
6694 r = sev_launch_secret(kvm, &sev_cmd);
6695 break;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006696 default:
6697 r = -EINVAL;
6698 goto out;
6699 }
6700
6701 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
6702 r = -EFAULT;
6703
6704out:
6705 mutex_unlock(&kvm->lock);
6706 return r;
6707}
6708
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006709static int svm_register_enc_region(struct kvm *kvm,
6710 struct kvm_enc_region *range)
6711{
6712 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6713 struct enc_region *region;
6714 int ret = 0;
6715
6716 if (!sev_guest(kvm))
6717 return -ENOTTY;
6718
6719 region = kzalloc(sizeof(*region), GFP_KERNEL);
6720 if (!region)
6721 return -ENOMEM;
6722
6723 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
6724 if (!region->pages) {
6725 ret = -ENOMEM;
6726 goto e_free;
6727 }
6728
6729 /*
6730 * The guest may change the memory encryption attribute from C=0 -> C=1
6731 * or vice versa for this memory range. Lets make sure caches are
6732 * flushed to ensure that guest data gets written into memory with
6733 * correct C-bit.
6734 */
6735 sev_clflush_pages(region->pages, region->npages);
6736
6737 region->uaddr = range->addr;
6738 region->size = range->size;
6739
6740 mutex_lock(&kvm->lock);
6741 list_add_tail(&region->list, &sev->regions_list);
6742 mutex_unlock(&kvm->lock);
6743
6744 return ret;
6745
6746e_free:
6747 kfree(region);
6748 return ret;
6749}
6750
6751static struct enc_region *
6752find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
6753{
6754 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6755 struct list_head *head = &sev->regions_list;
6756 struct enc_region *i;
6757
6758 list_for_each_entry(i, head, list) {
6759 if (i->uaddr == range->addr &&
6760 i->size == range->size)
6761 return i;
6762 }
6763
6764 return NULL;
6765}
6766
6767
6768static int svm_unregister_enc_region(struct kvm *kvm,
6769 struct kvm_enc_region *range)
6770{
6771 struct enc_region *region;
6772 int ret;
6773
6774 mutex_lock(&kvm->lock);
6775
6776 if (!sev_guest(kvm)) {
6777 ret = -ENOTTY;
6778 goto failed;
6779 }
6780
6781 region = find_enc_region(kvm, range);
6782 if (!region) {
6783 ret = -EINVAL;
6784 goto failed;
6785 }
6786
6787 __unregister_enc_region_locked(kvm, region);
6788
6789 mutex_unlock(&kvm->lock);
6790 return 0;
6791
6792failed:
6793 mutex_unlock(&kvm->lock);
6794 return ret;
6795}
6796
Kees Cook404f6aa2016-08-08 16:29:06 -07006797static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006798 .cpu_has_kvm_support = has_svm,
6799 .disabled_by_bios = is_disabled,
6800 .hardware_setup = svm_hardware_setup,
6801 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03006802 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006803 .hardware_enable = svm_hardware_enable,
6804 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02006805 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006806 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006807
6808 .vcpu_create = svm_create_vcpu,
6809 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03006810 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006811
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006812 .vm_init = avic_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06006813 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006814
Avi Kivity04d2cc72007-09-10 18:10:54 +03006815 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006816 .vcpu_load = svm_vcpu_load,
6817 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05006818 .vcpu_blocking = svm_vcpu_blocking,
6819 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006820
Paolo Bonzinia96036b2015-11-10 11:55:36 +01006821 .update_bp_intercept = update_bp_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006822 .get_msr = svm_get_msr,
6823 .set_msr = svm_set_msr,
6824 .get_segment_base = svm_get_segment_base,
6825 .get_segment = svm_get_segment,
6826 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02006827 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10006828 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02006829 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02006830 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03006831 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006832 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006833 .set_cr3 = svm_set_cr3,
6834 .set_cr4 = svm_set_cr4,
6835 .set_efer = svm_set_efer,
6836 .get_idt = svm_get_idt,
6837 .set_idt = svm_set_idt,
6838 .get_gdt = svm_get_gdt,
6839 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006840 .get_dr6 = svm_get_dr6,
6841 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03006842 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01006843 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03006844 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006845 .get_rflags = svm_get_rflags,
6846 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08006847
Avi Kivity6aa8b732006-12-10 02:21:36 -08006848 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006849
Avi Kivity6aa8b732006-12-10 02:21:36 -08006850 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03006851 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006852 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04006853 .set_interrupt_shadow = svm_set_interrupt_shadow,
6854 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02006855 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03006856 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006857 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02006858 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006859 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02006860 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006861 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006862 .get_nmi_mask = svm_get_nmi_mask,
6863 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006864 .enable_nmi_window = enable_nmi_window,
6865 .enable_irq_window = enable_irq_window,
6866 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08006867 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03006868 .get_enable_apicv = svm_get_enable_apicv,
6869 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08006870 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006871 .hwapic_irr_update = svm_hwapic_irr_update,
6872 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02006873 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05006874 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02006875
6876 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08006877 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006878 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006879
Avi Kivity586f9602010-11-18 13:09:54 +02006880 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02006881
Sheng Yang17cc3932010-01-05 19:02:27 +08006882 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08006883
6884 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006885
6886 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00006887 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006888 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08006889 .xsaves_supported = svm_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +02006890 .umip_emulated = svm_umip_emulated,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006891
6892 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006893
6894 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10006895
6896 .write_tsc_offset = svm_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02006897
6898 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006899
6900 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08006901 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006902
6903 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02006904
6905 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05006906 .deliver_posted_interrupt = svm_deliver_avic_intr,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05006907 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02006908 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006909
Ladi Prosek72d7b372017-10-11 16:54:41 +02006910 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006911 .pre_enter_smm = svm_pre_enter_smm,
6912 .pre_leave_smm = svm_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006913 .enable_smi_window = enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06006914
6915 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006916 .mem_enc_reg_region = svm_register_enc_region,
6917 .mem_enc_unreg_region = svm_unregister_enc_region,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006918};
6919
6920static int __init svm_init(void)
6921{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08006922 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03006923 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006924}
6925
6926static void __exit svm_exit(void)
6927{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08006928 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08006929}
6930
6931module_init(svm_init)
6932module_exit(svm_exit)