blob: be9c839e2c89967d689485dbb8e51763980dd151 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * AMD SVM support
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02007 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08008 *
9 * Authors:
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050017
18#define pr_fmt(fmt) "SVM: " fmt
19
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
21
Eddie Dong85f455f2007-07-06 12:20:49 +030022#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080023#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020025#include "x86.h"
Julian Stecklina66f7b722012-12-05 15:26:19 +010026#include "cpuid.h"
Wei Huang25462f72015-06-19 15:45:05 +020027#include "pmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040028
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/module.h>
Josh Triplettae759542012-03-28 11:32:28 -070030#include <linux/mod_devicetable.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020031#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/vmalloc.h>
33#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040034#include <linux/sched.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040035#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -050037#include <linux/amd-iommu.h>
38#include <linux/hashtable.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050039#include <linux/frame.h>
Brijesh Singhe9df0942017-12-04 10:57:33 -060040#include <linux/psp-sev.h>
Brijesh Singh1654efc2017-12-04 10:57:34 -060041#include <linux/file.h>
Brijesh Singh89c50582017-12-04 10:57:35 -060042#include <linux/pagemap.h>
43#include <linux/swap.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050045#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010046#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020047#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040048#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020050#include <asm/kvm_para.h>
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -050051#include <asm/irq_remapping.h>
Paolo Bonziniecb586b2018-02-22 16:43:17 +010052#include <asm/microcode.h>
David Woodhouse117cc7a2018-01-12 11:11:27 +000053#include <asm/nospec-branch.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080054
Eduardo Habkost63d11422008-11-17 19:03:20 -020055#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030056#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020057
Avi Kivity4ecac3f2008-05-13 13:23:38 +030058#define __ex(x) __kvm_handle_fault_on_reboot(x)
59
Avi Kivity6aa8b732006-12-10 02:21:36 -080060MODULE_AUTHOR("Qumranet");
61MODULE_LICENSE("GPL");
62
Josh Triplettae759542012-03-28 11:32:28 -070063static const struct x86_cpu_id svm_cpu_id[] = {
64 X86_FEATURE_MATCH(X86_FEATURE_SVM),
65 {}
66};
67MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
68
Avi Kivity6aa8b732006-12-10 02:21:36 -080069#define IOPM_ALLOC_ORDER 2
70#define MSRPM_ALLOC_ORDER 1
71
Avi Kivity6aa8b732006-12-10 02:21:36 -080072#define SEG_TYPE_LDT 2
73#define SEG_TYPE_BUSY_TSS16 3
74
Andre Przywara6bc31bd2010-04-11 23:07:28 +020075#define SVM_FEATURE_NPT (1 << 0)
76#define SVM_FEATURE_LBRV (1 << 1)
77#define SVM_FEATURE_SVML (1 << 2)
78#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010079#define SVM_FEATURE_TSC_RATE (1 << 4)
80#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
81#define SVM_FEATURE_FLUSH_ASID (1 << 6)
82#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020083#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030084
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -050085#define SVM_AVIC_DOORBELL 0xc001011b
86
Joerg Roedel410e4d52009-08-07 11:49:44 +020087#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
88#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
89#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
90
Joerg Roedel24e09cb2008-02-13 18:58:47 +010091#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
92
Joerg Roedelfbc0db72011-03-25 09:44:46 +010093#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010094#define TSC_RATIO_MIN 0x0000000000000001ULL
95#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010096
Dan Carpenter5446a972016-05-23 13:20:10 +030097#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050098
99/*
100 * 0xff is broadcast, so the max index allowed for physical APIC ID
101 * table is 0xfe. APIC IDs above 0xff are reserved.
102 */
103#define AVIC_MAX_PHYSICAL_ID_COUNT 255
104
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500105#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
106#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
107#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
108
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500109/* AVIC GATAG is encoded using VM and VCPU IDs */
110#define AVIC_VCPU_ID_BITS 8
111#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
112
113#define AVIC_VM_ID_BITS 24
114#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
115#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
116
117#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
118 (y & AVIC_VCPU_ID_MASK))
119#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
120#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
121
Joerg Roedel67ec6602010-05-17 14:43:35 +0200122static bool erratum_383_found __read_mostly;
123
Avi Kivity6c8166a2009-05-31 18:15:37 +0300124static const u32 host_save_user_msrs[] = {
125#ifdef CONFIG_X86_64
126 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
127 MSR_FS_BASE,
128#endif
129 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Paolo Bonzini46896c72015-11-12 14:49:16 +0100130 MSR_TSC_AUX,
Avi Kivity6c8166a2009-05-31 18:15:37 +0300131};
132
133#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
134
135struct kvm_vcpu;
136
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200137struct nested_state {
138 struct vmcb *hsave;
139 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +0100140 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200141 u64 vmcb;
142
143 /* These are the merged vectors */
144 u32 *msrpm;
145
146 /* gpa pointers to the real vectors */
147 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100148 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200149
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200150 /* A VMEXIT is required but not yet emulated */
151 bool exit_required;
152
Joerg Roedelaad42c62009-08-07 11:49:34 +0200153 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100154 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100155 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200156 u32 intercept_exceptions;
157 u64 intercept;
158
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200159 /* Nested Paging related state */
160 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200161};
162
Joerg Roedel323c3d82010-03-01 15:34:37 +0100163#define MSRPM_OFFSETS 16
164static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
165
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500166/*
167 * Set osvw_len to higher value when updated Revision Guides
168 * are published and we know what the new status bits are
169 */
170static uint64_t osvw_len = 4, osvw_status;
171
Avi Kivity6c8166a2009-05-31 18:15:37 +0300172struct vcpu_svm {
173 struct kvm_vcpu vcpu;
174 struct vmcb *vmcb;
175 unsigned long vmcb_pa;
176 struct svm_cpu_data *svm_data;
177 uint64_t asid_generation;
178 uint64_t sysenter_esp;
179 uint64_t sysenter_eip;
Paolo Bonzini46896c72015-11-12 14:49:16 +0100180 uint64_t tsc_aux;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300181
Tom Lendackyd1d93fa2018-02-24 00:18:20 +0100182 u64 msr_decfg;
183
Avi Kivity6c8166a2009-05-31 18:15:37 +0300184 u64 next_rip;
185
186 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200187 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200188 u16 fs;
189 u16 gs;
190 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200191 u64 gs_base;
192 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300193
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100194 u64 spec_ctrl;
195
Avi Kivity6c8166a2009-05-31 18:15:37 +0300196 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300197
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200198 ulong nmi_iret_rip;
199
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200200 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200201
202 bool nmi_singlestep;
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200203 u64 nmi_singlestep_guest_rflags;
Jan Kiszka66b71382010-02-23 17:47:56 +0100204
205 unsigned int3_injected;
206 unsigned long int3_rip;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100207
Joerg Roedel6092d3d2015-10-14 15:10:54 +0200208 /* cached guest cpuid flags for faster access */
209 bool nrips_enabled : 1;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500210
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500211 u32 ldr_reg;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500212 struct page *avic_backing_page;
213 u64 *avic_physical_id_cache;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -0500214 bool avic_is_running;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500215
216 /*
217 * Per-vcpu list of struct amd_svm_iommu_ir:
218 * This is used mainly to store interrupt remapping information used
219 * when update the vcpu affinity. This avoids the need to scan for
220 * IRTE and try to match ga_tag in the IOMMU driver.
221 */
222 struct list_head ir_list;
223 spinlock_t ir_list_lock;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600224
225 /* which host CPU was used for running this vcpu */
226 unsigned int last_cpu;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500227};
228
229/*
230 * This is a wrapper of struct amd_iommu_ir_data.
231 */
232struct amd_svm_iommu_ir {
233 struct list_head node; /* Used by SVM for per-vcpu ir_list */
234 void *data; /* Storing pointer to struct amd_ir_data */
Avi Kivity6c8166a2009-05-31 18:15:37 +0300235};
236
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500237#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
238#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
239
240#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
241#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
242#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
243#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
244
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100245static DEFINE_PER_CPU(u64, current_tsc_ratio);
246#define TSC_RATIO_DEFAULT 0x0100000000ULL
247
Joerg Roedel455716f2010-03-01 15:34:35 +0100248#define MSR_INVALID 0xffffffffU
249
Mathias Krause09941fb2012-08-30 01:30:20 +0200250static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100251 u32 index; /* Index of the MSR */
252 bool always; /* True if intercept is always on */
253} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400254 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100255 { .index = MSR_IA32_SYSENTER_CS, .always = true },
256#ifdef CONFIG_X86_64
257 { .index = MSR_GS_BASE, .always = true },
258 { .index = MSR_FS_BASE, .always = true },
259 { .index = MSR_KERNEL_GS_BASE, .always = true },
260 { .index = MSR_LSTAR, .always = true },
261 { .index = MSR_CSTAR, .always = true },
262 { .index = MSR_SYSCALL_MASK, .always = true },
263#endif
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100264 { .index = MSR_IA32_SPEC_CTRL, .always = false },
Ashok Raj15d45072018-02-01 22:59:43 +0100265 { .index = MSR_IA32_PRED_CMD, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100266 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
267 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
268 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
269 { .index = MSR_IA32_LASTINTTOIP, .always = false },
270 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271};
272
273/* enable NPT for AMD64 and X86 with PAE */
274#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
275static bool npt_enabled = true;
276#else
Joerg Roedele0231712010-02-24 18:59:10 +0100277static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278#endif
279
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100280/* allow nested paging (virtualized MMU) for all guests */
281static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282module_param(npt, int, S_IRUGO);
283
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100284/* allow nested virtualization in KVM/SVM */
285static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286module_param(nested, int, S_IRUGO);
287
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500288/* enable / disable AVIC */
289static int avic;
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500290#ifdef CONFIG_X86_LOCAL_APIC
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500291module_param(avic, int, S_IRUGO);
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500292#endif
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500293
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -0500294/* enable/disable Virtual VMLOAD VMSAVE */
295static int vls = true;
296module_param(vls, int, 0444);
297
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500298/* enable/disable Virtual GIF */
299static int vgif = true;
300module_param(vgif, int, 0444);
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500301
Brijesh Singhe9df0942017-12-04 10:57:33 -0600302/* enable/disable SEV support */
303static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
304module_param(sev, int, 0444);
305
Brijesh Singh7607b712018-02-19 10:14:44 -0600306static u8 rsm_ins_bytes[] = "\x0f\xaa";
307
Paolo Bonzini79a80592015-09-21 07:46:55 +0200308static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -0800309static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa);
Joerg Roedela5c38322009-08-07 11:49:32 +0200310static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800311
Joerg Roedel410e4d52009-08-07 11:49:44 +0200312static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100313static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800314static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800315static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
316 bool has_error_code, u32 error_code);
317
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100318enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100319 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
320 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100321 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100322 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100323 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100324 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100325 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100326 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100327 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100328 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100329 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100330 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500331 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
332 * AVIC PHYSICAL_TABLE pointer,
333 * AVIC LOGICAL_TABLE pointer
334 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100335 VMCB_DIRTY_MAX,
336};
337
Joerg Roedel0574dec2010-12-03 11:45:58 +0100338/* TPR and CR2 are always written before VMRUN */
339#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100340
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500341#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
342
Brijesh Singhed3cd232017-12-04 10:57:32 -0600343static unsigned int max_sev_asid;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600344static unsigned int min_sev_asid;
345static unsigned long *sev_asid_bitmap;
Brijesh Singh89c50582017-12-04 10:57:35 -0600346#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
Brijesh Singh1654efc2017-12-04 10:57:34 -0600347
Brijesh Singh1e80fdc2017-12-04 10:57:38 -0600348struct enc_region {
349 struct list_head list;
350 unsigned long npages;
351 struct page **pages;
352 unsigned long uaddr;
353 unsigned long size;
354};
355
Brijesh Singh1654efc2017-12-04 10:57:34 -0600356static inline bool svm_sev_enabled(void)
357{
358 return max_sev_asid;
359}
360
361static inline bool sev_guest(struct kvm *kvm)
362{
363 struct kvm_sev_info *sev = &kvm->arch.sev_info;
364
365 return sev->active;
366}
Brijesh Singhed3cd232017-12-04 10:57:32 -0600367
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600368static inline int sev_get_asid(struct kvm *kvm)
369{
370 struct kvm_sev_info *sev = &kvm->arch.sev_info;
371
372 return sev->asid;
373}
374
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100375static inline void mark_all_dirty(struct vmcb *vmcb)
376{
377 vmcb->control.clean = 0;
378}
379
380static inline void mark_all_clean(struct vmcb *vmcb)
381{
382 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
383 & ~VMCB_ALWAYS_DIRTY_MASK;
384}
385
386static inline void mark_dirty(struct vmcb *vmcb, int bit)
387{
388 vmcb->control.clean &= ~(1 << bit);
389}
390
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
392{
393 return container_of(vcpu, struct vcpu_svm, vcpu);
394}
395
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500396static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
397{
398 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
399 mark_dirty(svm->vmcb, VMCB_AVIC);
400}
401
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -0500402static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
403{
404 struct vcpu_svm *svm = to_svm(vcpu);
405 u64 *entry = svm->avic_physical_id_cache;
406
407 if (!entry)
408 return false;
409
410 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
411}
412
Joerg Roedel384c6362010-11-30 18:03:56 +0100413static void recalc_intercepts(struct vcpu_svm *svm)
414{
415 struct vmcb_control_area *c, *h;
416 struct nested_state *g;
417
Joerg Roedel116a0a22010-12-03 11:45:49 +0100418 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
419
Joerg Roedel384c6362010-11-30 18:03:56 +0100420 if (!is_guest_mode(&svm->vcpu))
421 return;
422
423 c = &svm->vmcb->control;
424 h = &svm->nested.hsave->control;
425 g = &svm->nested;
426
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100427 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100428 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Paolo Bonzinibd895252018-01-11 16:55:24 +0100429 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
Joerg Roedel384c6362010-11-30 18:03:56 +0100430 c->intercept = h->intercept | g->intercept;
431}
432
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100433static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
434{
435 if (is_guest_mode(&svm->vcpu))
436 return svm->nested.hsave;
437 else
438 return svm->vmcb;
439}
440
441static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
442{
443 struct vmcb *vmcb = get_host_vmcb(svm);
444
445 vmcb->control.intercept_cr |= (1U << bit);
446
447 recalc_intercepts(svm);
448}
449
450static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
451{
452 struct vmcb *vmcb = get_host_vmcb(svm);
453
454 vmcb->control.intercept_cr &= ~(1U << bit);
455
456 recalc_intercepts(svm);
457}
458
459static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
460{
461 struct vmcb *vmcb = get_host_vmcb(svm);
462
463 return vmcb->control.intercept_cr & (1U << bit);
464}
465
Paolo Bonzini5315c712014-03-03 13:08:29 +0100466static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100467{
468 struct vmcb *vmcb = get_host_vmcb(svm);
469
Paolo Bonzini5315c712014-03-03 13:08:29 +0100470 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
471 | (1 << INTERCEPT_DR1_READ)
472 | (1 << INTERCEPT_DR2_READ)
473 | (1 << INTERCEPT_DR3_READ)
474 | (1 << INTERCEPT_DR4_READ)
475 | (1 << INTERCEPT_DR5_READ)
476 | (1 << INTERCEPT_DR6_READ)
477 | (1 << INTERCEPT_DR7_READ)
478 | (1 << INTERCEPT_DR0_WRITE)
479 | (1 << INTERCEPT_DR1_WRITE)
480 | (1 << INTERCEPT_DR2_WRITE)
481 | (1 << INTERCEPT_DR3_WRITE)
482 | (1 << INTERCEPT_DR4_WRITE)
483 | (1 << INTERCEPT_DR5_WRITE)
484 | (1 << INTERCEPT_DR6_WRITE)
485 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100486
487 recalc_intercepts(svm);
488}
489
Paolo Bonzini5315c712014-03-03 13:08:29 +0100490static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100491{
492 struct vmcb *vmcb = get_host_vmcb(svm);
493
Paolo Bonzini5315c712014-03-03 13:08:29 +0100494 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100495
496 recalc_intercepts(svm);
497}
498
Joerg Roedel18c918c2010-11-30 18:03:59 +0100499static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
500{
501 struct vmcb *vmcb = get_host_vmcb(svm);
502
503 vmcb->control.intercept_exceptions |= (1U << bit);
504
505 recalc_intercepts(svm);
506}
507
508static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
509{
510 struct vmcb *vmcb = get_host_vmcb(svm);
511
512 vmcb->control.intercept_exceptions &= ~(1U << bit);
513
514 recalc_intercepts(svm);
515}
516
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100517static inline void set_intercept(struct vcpu_svm *svm, int bit)
518{
519 struct vmcb *vmcb = get_host_vmcb(svm);
520
521 vmcb->control.intercept |= (1ULL << bit);
522
523 recalc_intercepts(svm);
524}
525
526static inline void clr_intercept(struct vcpu_svm *svm, int bit)
527{
528 struct vmcb *vmcb = get_host_vmcb(svm);
529
530 vmcb->control.intercept &= ~(1ULL << bit);
531
532 recalc_intercepts(svm);
533}
534
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500535static inline bool vgif_enabled(struct vcpu_svm *svm)
536{
537 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
538}
539
Joerg Roedel2af91942009-08-07 11:49:28 +0200540static inline void enable_gif(struct vcpu_svm *svm)
541{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500542 if (vgif_enabled(svm))
543 svm->vmcb->control.int_ctl |= V_GIF_MASK;
544 else
545 svm->vcpu.arch.hflags |= HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200546}
547
548static inline void disable_gif(struct vcpu_svm *svm)
549{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500550 if (vgif_enabled(svm))
551 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
552 else
553 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200554}
555
556static inline bool gif_set(struct vcpu_svm *svm)
557{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500558 if (vgif_enabled(svm))
559 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
560 else
561 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
Joerg Roedel2af91942009-08-07 11:49:28 +0200562}
563
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564static unsigned long iopm_base;
565
566struct kvm_ldttss_desc {
567 u16 limit0;
568 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100569 unsigned base1:8, type:5, dpl:2, p:1;
570 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571 u32 base3;
572 u32 zero1;
573} __attribute__((packed));
574
575struct svm_cpu_data {
576 int cpu;
577
Avi Kivity5008fdf2007-04-02 13:05:50 +0300578 u64 asid_generation;
579 u32 max_asid;
580 u32 next_asid;
Brijesh Singh4faefff2017-12-04 10:57:25 -0600581 u32 min_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800582 struct kvm_ldttss_desc *tss_desc;
583
584 struct page *save_area;
Ashok Raj15d45072018-02-01 22:59:43 +0100585 struct vmcb *current_vmcb;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600586
587 /* index = sev_asid, value = vmcb pointer */
588 struct vmcb **sev_vmcbs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589};
590
591static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
592
593struct svm_init_data {
594 int cpu;
595 int r;
596};
597
Mathias Krause09941fb2012-08-30 01:30:20 +0200598static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200600#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800601#define MSRS_RANGE_SIZE 2048
602#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
603
Joerg Roedel455716f2010-03-01 15:34:35 +0100604static u32 svm_msrpm_offset(u32 msr)
605{
606 u32 offset;
607 int i;
608
609 for (i = 0; i < NUM_MSR_MAPS; i++) {
610 if (msr < msrpm_ranges[i] ||
611 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
612 continue;
613
614 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
615 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
616
617 /* Now we have the u8 offset - but need the u32 offset */
618 return offset / 4;
619 }
620
621 /* MSR not in any range */
622 return MSR_INVALID;
623}
624
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625#define MAX_INST_SIZE 15
626
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627static inline void clgi(void)
628{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300629 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630}
631
632static inline void stgi(void)
633{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300634 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635}
636
637static inline void invlpga(unsigned long addr, u32 asid)
638{
Joerg Roedele0231712010-02-24 18:59:10 +0100639 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800640}
641
Yu Zhang855feb62017-08-24 20:27:55 +0800642static int get_npt_level(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +0200643{
644#ifdef CONFIG_X86_64
Yu Zhang2a7266a2017-08-24 20:27:54 +0800645 return PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200646#else
647 return PT32E_ROOT_LEVEL;
648#endif
649}
650
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
652{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000653 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100654 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600655 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656
Alexander Graf9962d032008-11-25 20:17:02 +0100657 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100658 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659}
660
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661static int is_external_interrupt(u32 info)
662{
663 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
664 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
665}
666
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200667static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400668{
669 struct vcpu_svm *svm = to_svm(vcpu);
670 u32 ret = 0;
671
672 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200673 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
674 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400675}
676
677static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
678{
679 struct vcpu_svm *svm = to_svm(vcpu);
680
681 if (mask == 0)
682 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
683 else
684 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
685
686}
687
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
689{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400690 struct vcpu_svm *svm = to_svm(vcpu);
691
Bandan Dasf1047652015-06-11 02:05:33 -0400692 if (svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200693 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200694 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400695 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200696
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400697 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100698 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300699 EMULATE_DONE)
700 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701 return;
702 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300703 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
704 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
705 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300707 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400708 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800709}
710
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700711static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100712{
713 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700714 unsigned nr = vcpu->arch.exception.nr;
715 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700716 bool reinject = vcpu->arch.exception.injected;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700717 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100718
Joerg Roedele0231712010-02-24 18:59:10 +0100719 /*
720 * If we are within a nested VM we'd better #VMEXIT and let the guest
721 * handle the exception
722 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200723 if (!reinject &&
724 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100725 return;
726
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200727 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100728 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
729
730 /*
731 * For guest debugging where we have to reinject #BP if some
732 * INT3 is guest-owned:
733 * Emulate nRIP by moving RIP forward. Will fail if injection
734 * raises a fault that is not intercepted. Still better than
735 * failing in all cases.
736 */
737 skip_emulated_instruction(&svm->vcpu);
738 rip = kvm_rip_read(&svm->vcpu);
739 svm->int3_rip = rip + svm->vmcb->save.cs.base;
740 svm->int3_injected = rip - old_rip;
741 }
742
Jan Kiszka116a4752010-02-23 17:47:54 +0100743 svm->vmcb->control.event_inj = nr
744 | SVM_EVTINJ_VALID
745 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
746 | SVM_EVTINJ_TYPE_EXEPT;
747 svm->vmcb->control.event_inj_err = error_code;
748}
749
Joerg Roedel67ec6602010-05-17 14:43:35 +0200750static void svm_init_erratum_383(void)
751{
752 u32 low, high;
753 int err;
754 u64 val;
755
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100756 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200757 return;
758
759 /* Use _safe variants to not break nested virtualization */
760 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
761 if (err)
762 return;
763
764 val |= (1ULL << 47);
765
766 low = lower_32_bits(val);
767 high = upper_32_bits(val);
768
769 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
770
771 erratum_383_found = true;
772}
773
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500774static void svm_init_osvw(struct kvm_vcpu *vcpu)
775{
776 /*
777 * Guests should see errata 400 and 415 as fixed (assuming that
778 * HLT and IO instructions are intercepted).
779 */
780 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
781 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
782
783 /*
784 * By increasing VCPU's osvw.length to 3 we are telling the guest that
785 * all osvw.status bits inside that length, including bit 0 (which is
786 * reserved for erratum 298), are valid. However, if host processor's
787 * osvw_len is 0 then osvw_status[0] carries no information. We need to
788 * be conservative here and therefore we tell the guest that erratum 298
789 * is present (because we really don't know).
790 */
791 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
792 vcpu->arch.osvw.status |= 1;
793}
794
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795static int has_svm(void)
796{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200797 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798
Eduardo Habkost63d11422008-11-17 19:03:20 -0200799 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800800 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801 return 0;
802 }
803
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 return 1;
805}
806
Radim Krčmář13a34e02014-08-28 15:13:03 +0200807static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100809 /* Make sure we clean up behind us */
810 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
811 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
812
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200813 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100814
815 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816}
817
Radim Krčmář13a34e02014-08-28 15:13:03 +0200818static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819{
820
Tejun Heo0fe1e002009-10-29 22:34:14 +0900821 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823 struct desc_struct *gdt;
824 int me = raw_smp_processor_id();
825
Alexander Graf10474ae2009-09-15 11:37:46 +0200826 rdmsrl(MSR_EFER, efer);
827 if (efer & EFER_SVME)
828 return -EBUSY;
829
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200831 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200832 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900834 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900835 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200836 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200837 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800838 }
839
Tejun Heo0fe1e002009-10-29 22:34:14 +0900840 sd->asid_generation = 1;
841 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
842 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600843 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844
Thomas Garnier45fc8752017-03-14 10:05:08 -0700845 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900846 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847
Alexander Graf9962d032008-11-25 20:17:02 +0100848 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849
Linus Torvaldsd0316552009-12-14 09:58:24 -0800850 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200851
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100852 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
853 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500854 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100855 }
856
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500857
858 /*
859 * Get OSVW bits.
860 *
861 * Note that it is possible to have a system with mixed processor
862 * revisions and therefore different OSVW bits. If bits are not the same
863 * on different processors then choose the worst case (i.e. if erratum
864 * is present on one processor and not on another then assume that the
865 * erratum is present everywhere).
866 */
867 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
868 uint64_t len, status = 0;
869 int err;
870
871 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
872 if (!err)
873 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
874 &err);
875
876 if (err)
877 osvw_status = osvw_len = 0;
878 else {
879 if (len < osvw_len)
880 osvw_len = len;
881 osvw_status |= status;
882 osvw_status &= (1ULL << osvw_len) - 1;
883 }
884 } else
885 osvw_status = osvw_len = 0;
886
Joerg Roedel67ec6602010-05-17 14:43:35 +0200887 svm_init_erratum_383();
888
Joerg Roedel1018faa2012-02-29 14:57:32 +0100889 amd_pmu_enable_virt();
890
Alexander Graf10474ae2009-09-15 11:37:46 +0200891 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892}
893
Joerg Roedel0da1db752008-07-02 16:02:11 +0200894static void svm_cpu_uninit(int cpu)
895{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900896 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200897
Tejun Heo0fe1e002009-10-29 22:34:14 +0900898 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200899 return;
900
901 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600902 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900903 __free_page(sd->save_area);
904 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200905}
906
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907static int svm_cpu_init(int cpu)
908{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900909 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 int r;
911
Tejun Heo0fe1e002009-10-29 22:34:14 +0900912 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
913 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900915 sd->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916 r = -ENOMEM;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600917 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900918 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 goto err_1;
920
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600921 if (svm_sev_enabled()) {
922 r = -ENOMEM;
923 sd->sev_vmcbs = kmalloc((max_sev_asid + 1) * sizeof(void *), GFP_KERNEL);
924 if (!sd->sev_vmcbs)
925 goto err_1;
926 }
927
Tejun Heo0fe1e002009-10-29 22:34:14 +0900928 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929
930 return 0;
931
932err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900933 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934 return r;
935
936}
937
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100938static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939{
940 int i;
941
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100942 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
943 if (direct_access_msrs[i].index == index)
944 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100946 return false;
947}
948
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100949static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr)
950{
951 u8 bit_write;
952 unsigned long tmp;
953 u32 offset;
954 u32 *msrpm;
955
956 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
957 to_svm(vcpu)->msrpm;
958
959 offset = svm_msrpm_offset(msr);
960 bit_write = 2 * (msr & 0x0f) + 1;
961 tmp = msrpm[offset];
962
963 BUG_ON(offset == MSR_INVALID);
964
965 return !!test_bit(bit_write, &tmp);
966}
967
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968static void set_msr_interception(u32 *msrpm, unsigned msr,
969 int read, int write)
970{
Joerg Roedel455716f2010-03-01 15:34:35 +0100971 u8 bit_read, bit_write;
972 unsigned long tmp;
973 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100975 /*
976 * If this warning triggers extend the direct_access_msrs list at the
977 * beginning of the file
978 */
979 WARN_ON(!valid_msr_intercept(msr));
980
Joerg Roedel455716f2010-03-01 15:34:35 +0100981 offset = svm_msrpm_offset(msr);
982 bit_read = 2 * (msr & 0x0f);
983 bit_write = 2 * (msr & 0x0f) + 1;
984 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985
Joerg Roedel455716f2010-03-01 15:34:35 +0100986 BUG_ON(offset == MSR_INVALID);
987
988 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
989 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
990
991 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992}
993
Joerg Roedelf65c2292008-02-13 18:58:46 +0100994static void svm_vcpu_init_msrpm(u32 *msrpm)
995{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100996 int i;
997
Joerg Roedelf65c2292008-02-13 18:58:46 +0100998 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
999
Joerg Roedelac72a9b2010-03-01 15:34:36 +01001000 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1001 if (!direct_access_msrs[i].always)
1002 continue;
1003
1004 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
1005 }
Joerg Roedelf65c2292008-02-13 18:58:46 +01001006}
1007
Joerg Roedel323c3d82010-03-01 15:34:37 +01001008static void add_msr_offset(u32 offset)
1009{
1010 int i;
1011
1012 for (i = 0; i < MSRPM_OFFSETS; ++i) {
1013
1014 /* Offset already in list? */
1015 if (msrpm_offsets[i] == offset)
1016 return;
1017
1018 /* Slot used by another offset? */
1019 if (msrpm_offsets[i] != MSR_INVALID)
1020 continue;
1021
1022 /* Add offset to list */
1023 msrpm_offsets[i] = offset;
1024
1025 return;
1026 }
1027
1028 /*
1029 * If this BUG triggers the msrpm_offsets table has an overflow. Just
1030 * increase MSRPM_OFFSETS in this case.
1031 */
1032 BUG();
1033}
1034
1035static void init_msrpm_offsets(void)
1036{
1037 int i;
1038
1039 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
1040
1041 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1042 u32 offset;
1043
1044 offset = svm_msrpm_offset(direct_access_msrs[i].index);
1045 BUG_ON(offset == MSR_INVALID);
1046
1047 add_msr_offset(offset);
1048 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049}
1050
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001051static void svm_enable_lbrv(struct vcpu_svm *svm)
1052{
1053 u32 *msrpm = svm->msrpm;
1054
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001055 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001056 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
1057 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
1058 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
1059 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
1060}
1061
1062static void svm_disable_lbrv(struct vcpu_svm *svm)
1063{
1064 u32 *msrpm = svm->msrpm;
1065
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001066 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001067 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
1068 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
1069 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
1070 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
1071}
1072
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001073static void disable_nmi_singlestep(struct vcpu_svm *svm)
1074{
1075 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001076
Ladi Prosekab2f4d732017-06-21 09:06:58 +02001077 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
1078 /* Clear our flags if they were not set by the guest */
1079 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1080 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
1081 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1082 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
1083 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001084}
1085
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001086/* Note:
1087 * This hash table is used to map VM_ID to a struct kvm_arch,
1088 * when handling AMD IOMMU GALOG notification to schedule in
1089 * a particular vCPU.
1090 */
1091#define SVM_VM_DATA_HASH_BITS 8
David Hildenbrand681bcea2017-01-24 22:21:16 +01001092static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001093static u32 next_vm_id = 0;
1094static bool next_vm_id_wrapped = 0;
David Hildenbrand681bcea2017-01-24 22:21:16 +01001095static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001096
1097/* Note:
1098 * This function is called from IOMMU driver to notify
1099 * SVM to schedule in a particular vCPU of a particular VM.
1100 */
1101static int avic_ga_log_notifier(u32 ga_tag)
1102{
1103 unsigned long flags;
1104 struct kvm_arch *ka = NULL;
1105 struct kvm_vcpu *vcpu = NULL;
1106 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
1107 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
1108
1109 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
1110
1111 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1112 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1113 struct kvm *kvm = container_of(ka, struct kvm, arch);
1114 struct kvm_arch *vm_data = &kvm->arch;
1115
1116 if (vm_data->avic_vm_id != vm_id)
1117 continue;
1118 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
1119 break;
1120 }
1121 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1122
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001123 /* Note:
1124 * At this point, the IOMMU should have already set the pending
1125 * bit in the vAPIC backing page. So, we just need to schedule
1126 * in the vcpu.
1127 */
Paolo Bonzini1cf53582017-10-10 12:51:56 +02001128 if (vcpu)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001129 kvm_vcpu_wake_up(vcpu);
1130
1131 return 0;
1132}
1133
Brijesh Singhe9df0942017-12-04 10:57:33 -06001134static __init int sev_hardware_setup(void)
1135{
1136 struct sev_user_data_status *status;
1137 int rc;
1138
1139 /* Maximum number of encrypted guests supported simultaneously */
1140 max_sev_asid = cpuid_ecx(0x8000001F);
1141
1142 if (!max_sev_asid)
1143 return 1;
1144
Brijesh Singh1654efc2017-12-04 10:57:34 -06001145 /* Minimum ASID value that should be used for SEV guest */
1146 min_sev_asid = cpuid_edx(0x8000001F);
1147
1148 /* Initialize SEV ASID bitmap */
1149 sev_asid_bitmap = kcalloc(BITS_TO_LONGS(max_sev_asid),
1150 sizeof(unsigned long), GFP_KERNEL);
1151 if (!sev_asid_bitmap)
1152 return 1;
1153
Brijesh Singhe9df0942017-12-04 10:57:33 -06001154 status = kmalloc(sizeof(*status), GFP_KERNEL);
1155 if (!status)
1156 return 1;
1157
1158 /*
1159 * Check SEV platform status.
1160 *
1161 * PLATFORM_STATUS can be called in any state, if we failed to query
1162 * the PLATFORM status then either PSP firmware does not support SEV
1163 * feature or SEV firmware is dead.
1164 */
1165 rc = sev_platform_status(status, NULL);
1166 if (rc)
1167 goto err;
1168
1169 pr_info("SEV supported\n");
1170
1171err:
1172 kfree(status);
1173 return rc;
1174}
1175
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176static __init int svm_hardware_setup(void)
1177{
1178 int cpu;
1179 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001180 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181 int r;
1182
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
1184
1185 if (!iopm_pages)
1186 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +03001187
1188 iopm_va = page_address(iopm_pages);
1189 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
1191
Joerg Roedel323c3d82010-03-01 15:34:37 +01001192 init_msrpm_offsets();
1193
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001194 if (boot_cpu_has(X86_FEATURE_NX))
1195 kvm_enable_efer_bits(EFER_NX);
1196
Alexander Graf1b2fd702009-02-02 16:23:51 +01001197 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
1198 kvm_enable_efer_bits(EFER_FFXSR);
1199
Joerg Roedel92a1f122011-03-25 09:44:51 +01001200 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +01001201 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +08001202 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1203 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +01001204 }
1205
Alexander Graf236de052008-11-25 20:17:10 +01001206 if (nested) {
1207 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +02001208 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +01001209 }
1210
Brijesh Singhe9df0942017-12-04 10:57:33 -06001211 if (sev) {
1212 if (boot_cpu_has(X86_FEATURE_SEV) &&
1213 IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
1214 r = sev_hardware_setup();
1215 if (r)
1216 sev = false;
1217 } else {
1218 sev = false;
1219 }
1220 }
1221
Zachary Amsden3230bb42009-09-29 11:38:37 -10001222 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223 r = svm_cpu_init(cpu);
1224 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +01001225 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +01001227
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001228 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001229 npt_enabled = false;
1230
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001231 if (npt_enabled && !npt) {
1232 printk(KERN_INFO "kvm: Nested Paging disabled\n");
1233 npt_enabled = false;
1234 }
1235
Joerg Roedel18552672008-02-07 13:47:41 +01001236 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001237 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +01001238 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +02001239 } else
1240 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001241
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001242 if (avic) {
1243 if (!npt_enabled ||
1244 !boot_cpu_has(X86_FEATURE_AVIC) ||
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001245 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001246 avic = false;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001247 } else {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001248 pr_info("AVIC enabled\n");
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001249
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001250 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1251 }
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001252 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001253
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001254 if (vls) {
1255 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001256 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001257 !IS_ENABLED(CONFIG_X86_64)) {
1258 vls = false;
1259 } else {
1260 pr_info("Virtual VMLOAD VMSAVE supported\n");
1261 }
1262 }
1263
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001264 if (vgif) {
1265 if (!boot_cpu_has(X86_FEATURE_VGIF))
1266 vgif = false;
1267 else
1268 pr_info("Virtual GIF supported\n");
1269 }
1270
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271 return 0;
1272
Joerg Roedelf65c2292008-02-13 18:58:46 +01001273err:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1275 iopm_base = 0;
1276 return r;
1277}
1278
1279static __exit void svm_hardware_unsetup(void)
1280{
Joerg Roedel0da1db752008-07-02 16:02:11 +02001281 int cpu;
1282
Brijesh Singh1654efc2017-12-04 10:57:34 -06001283 if (svm_sev_enabled())
1284 kfree(sev_asid_bitmap);
1285
Zachary Amsden3230bb42009-09-29 11:38:37 -10001286 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +02001287 svm_cpu_uninit(cpu);
1288
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001290 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291}
1292
1293static void init_seg(struct vmcb_seg *seg)
1294{
1295 seg->selector = 0;
1296 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001297 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298 seg->limit = 0xffff;
1299 seg->base = 0;
1300}
1301
1302static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1303{
1304 seg->selector = 0;
1305 seg->attrib = SVM_SELECTOR_P_MASK | type;
1306 seg->limit = 0xffff;
1307 seg->base = 0;
1308}
1309
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001310static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1311{
1312 struct vcpu_svm *svm = to_svm(vcpu);
1313 u64 g_tsc_offset = 0;
1314
Joerg Roedel20307532010-11-29 17:51:48 +01001315 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001316 g_tsc_offset = svm->vmcb->control.tsc_offset -
1317 svm->nested.hsave->control.tsc_offset;
1318 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001319 } else
1320 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1321 svm->vmcb->control.tsc_offset,
1322 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001323
1324 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001325
1326 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001327}
1328
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001329static void avic_init_vmcb(struct vcpu_svm *svm)
1330{
1331 struct vmcb *vmcb = svm->vmcb;
1332 struct kvm_arch *vm_data = &svm->vcpu.kvm->arch;
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001333 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
1334 phys_addr_t lpa = __sme_set(page_to_phys(vm_data->avic_logical_id_table_page));
1335 phys_addr_t ppa = __sme_set(page_to_phys(vm_data->avic_physical_id_table_page));
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001336
1337 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1338 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1339 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1340 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1341 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001342}
1343
Paolo Bonzini56908912015-10-19 11:30:19 +02001344static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345{
Joerg Roedele6101a92008-02-13 18:58:45 +01001346 struct vmcb_control_area *control = &svm->vmcb->control;
1347 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001349 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001350
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001351 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1352 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1353 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1354 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1355 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1356 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001357 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1358 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359
Paolo Bonzini5315c712014-03-03 13:08:29 +01001360 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361
Joerg Roedel18c918c2010-11-30 18:03:59 +01001362 set_exception_intercept(svm, PF_VECTOR);
1363 set_exception_intercept(svm, UD_VECTOR);
1364 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001365 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001366 set_exception_intercept(svm, DB_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001368 set_intercept(svm, INTERCEPT_INTR);
1369 set_intercept(svm, INTERCEPT_NMI);
1370 set_intercept(svm, INTERCEPT_SMI);
1371 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001372 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001373 set_intercept(svm, INTERCEPT_CPUID);
1374 set_intercept(svm, INTERCEPT_INVD);
1375 set_intercept(svm, INTERCEPT_HLT);
1376 set_intercept(svm, INTERCEPT_INVLPG);
1377 set_intercept(svm, INTERCEPT_INVLPGA);
1378 set_intercept(svm, INTERCEPT_IOIO_PROT);
1379 set_intercept(svm, INTERCEPT_MSR_PROT);
1380 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1381 set_intercept(svm, INTERCEPT_SHUTDOWN);
1382 set_intercept(svm, INTERCEPT_VMRUN);
1383 set_intercept(svm, INTERCEPT_VMMCALL);
1384 set_intercept(svm, INTERCEPT_VMLOAD);
1385 set_intercept(svm, INTERCEPT_VMSAVE);
1386 set_intercept(svm, INTERCEPT_STGI);
1387 set_intercept(svm, INTERCEPT_CLGI);
1388 set_intercept(svm, INTERCEPT_SKINIT);
1389 set_intercept(svm, INTERCEPT_WBINVD);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001390 set_intercept(svm, INTERCEPT_XSETBV);
Brijesh Singh7607b712018-02-19 10:14:44 -06001391 set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001393 if (!kvm_mwait_in_guest()) {
1394 set_intercept(svm, INTERCEPT_MONITOR);
1395 set_intercept(svm, INTERCEPT_MWAIT);
1396 }
1397
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001398 control->iopm_base_pa = __sme_set(iopm_base);
1399 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400 control->int_ctl = V_INTR_MASKING_MASK;
1401
1402 init_seg(&save->es);
1403 init_seg(&save->ss);
1404 init_seg(&save->ds);
1405 init_seg(&save->fs);
1406 init_seg(&save->gs);
1407
1408 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001409 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410 /* Executable/Readable Code Segment */
1411 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1412 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1413 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414
1415 save->gdtr.limit = 0xffff;
1416 save->idtr.limit = 0xffff;
1417
1418 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1419 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1420
Paolo Bonzini56908912015-10-19 11:30:19 +02001421 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001422 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001423 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001425 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426
Joerg Roedele0231712010-02-24 18:59:10 +01001427 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001428 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001429 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001431 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001432 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001433
Rusty Russell66aee912007-07-17 23:34:16 +10001434 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001436
1437 if (npt_enabled) {
1438 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001439 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001440 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001441 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001442 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1443 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001444 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001445 save->cr3 = 0;
1446 save->cr4 = 0;
1447 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001448 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001449
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001450 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001451 svm->vcpu.arch.hflags = 0;
1452
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001453 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001454 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001455 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001456 }
1457
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001458 if (kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001459 avic_init_vmcb(svm);
1460
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001461 /*
1462 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1463 * in VMCB and clear intercepts to avoid #VMEXIT.
1464 */
1465 if (vls) {
1466 clr_intercept(svm, INTERCEPT_VMLOAD);
1467 clr_intercept(svm, INTERCEPT_VMSAVE);
1468 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1469 }
1470
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001471 if (vgif) {
1472 clr_intercept(svm, INTERCEPT_STGI);
1473 clr_intercept(svm, INTERCEPT_CLGI);
1474 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1475 }
1476
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001477 if (sev_guest(svm->vcpu.kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001478 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001479 clr_exception_intercept(svm, UD_VECTOR);
1480 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001481
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001482 mark_all_dirty(svm->vmcb);
1483
Joerg Roedel2af91942009-08-07 11:49:28 +02001484 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001485
1486}
1487
Dan Carpenterd3e7dec2017-05-18 10:38:53 +03001488static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1489 unsigned int index)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001490{
1491 u64 *avic_physical_id_table;
1492 struct kvm_arch *vm_data = &vcpu->kvm->arch;
1493
1494 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1495 return NULL;
1496
1497 avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page);
1498
1499 return &avic_physical_id_table[index];
1500}
1501
1502/**
1503 * Note:
1504 * AVIC hardware walks the nested page table to check permissions,
1505 * but does not use the SPA address specified in the leaf page
1506 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1507 * field of the VMCB. Therefore, we set up the
1508 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1509 */
1510static int avic_init_access_page(struct kvm_vcpu *vcpu)
1511{
1512 struct kvm *kvm = vcpu->kvm;
1513 int ret;
1514
1515 if (kvm->arch.apic_access_page_done)
1516 return 0;
1517
1518 ret = x86_set_memory_region(kvm,
1519 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1520 APIC_DEFAULT_PHYS_BASE,
1521 PAGE_SIZE);
1522 if (ret)
1523 return ret;
1524
1525 kvm->arch.apic_access_page_done = true;
1526 return 0;
1527}
1528
1529static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1530{
1531 int ret;
1532 u64 *entry, new_entry;
1533 int id = vcpu->vcpu_id;
1534 struct vcpu_svm *svm = to_svm(vcpu);
1535
1536 ret = avic_init_access_page(vcpu);
1537 if (ret)
1538 return ret;
1539
1540 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1541 return -EINVAL;
1542
1543 if (!svm->vcpu.arch.apic->regs)
1544 return -EINVAL;
1545
1546 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1547
1548 /* Setting AVIC backing page address in the phy APIC ID table */
1549 entry = avic_get_physical_id_entry(vcpu, id);
1550 if (!entry)
1551 return -EINVAL;
1552
1553 new_entry = READ_ONCE(*entry);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001554 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
1555 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1556 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001557 WRITE_ONCE(*entry, new_entry);
1558
1559 svm->avic_physical_id_cache = entry;
1560
1561 return 0;
1562}
1563
Brijesh Singh1654efc2017-12-04 10:57:34 -06001564static void __sev_asid_free(int asid)
1565{
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001566 struct svm_cpu_data *sd;
1567 int cpu, pos;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001568
1569 pos = asid - 1;
1570 clear_bit(pos, sev_asid_bitmap);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001571
1572 for_each_possible_cpu(cpu) {
1573 sd = per_cpu(svm_data, cpu);
1574 sd->sev_vmcbs[pos] = NULL;
1575 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001576}
1577
1578static void sev_asid_free(struct kvm *kvm)
1579{
1580 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1581
1582 __sev_asid_free(sev->asid);
1583}
1584
Brijesh Singh59414c92017-12-04 10:57:35 -06001585static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
1586{
1587 struct sev_data_decommission *decommission;
1588 struct sev_data_deactivate *data;
1589
1590 if (!handle)
1591 return;
1592
1593 data = kzalloc(sizeof(*data), GFP_KERNEL);
1594 if (!data)
1595 return;
1596
1597 /* deactivate handle */
1598 data->handle = handle;
1599 sev_guest_deactivate(data, NULL);
1600
1601 wbinvd_on_all_cpus();
1602 sev_guest_df_flush(NULL);
1603 kfree(data);
1604
1605 decommission = kzalloc(sizeof(*decommission), GFP_KERNEL);
1606 if (!decommission)
1607 return;
1608
1609 /* decommission handle */
1610 decommission->handle = handle;
1611 sev_guest_decommission(decommission, NULL);
1612
1613 kfree(decommission);
1614}
1615
Brijesh Singh89c50582017-12-04 10:57:35 -06001616static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
1617 unsigned long ulen, unsigned long *n,
1618 int write)
1619{
1620 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1621 unsigned long npages, npinned, size;
1622 unsigned long locked, lock_limit;
1623 struct page **pages;
1624 int first, last;
1625
1626 /* Calculate number of pages. */
1627 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
1628 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
1629 npages = (last - first + 1);
1630
1631 locked = sev->pages_locked + npages;
1632 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1633 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
1634 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
1635 return NULL;
1636 }
1637
1638 /* Avoid using vmalloc for smaller buffers. */
1639 size = npages * sizeof(struct page *);
1640 if (size > PAGE_SIZE)
1641 pages = vmalloc(size);
1642 else
1643 pages = kmalloc(size, GFP_KERNEL);
1644
1645 if (!pages)
1646 return NULL;
1647
1648 /* Pin the user virtual address. */
1649 npinned = get_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
1650 if (npinned != npages) {
1651 pr_err("SEV: Failure locking %lu pages.\n", npages);
1652 goto err;
1653 }
1654
1655 *n = npages;
1656 sev->pages_locked = locked;
1657
1658 return pages;
1659
1660err:
1661 if (npinned > 0)
1662 release_pages(pages, npinned);
1663
1664 kvfree(pages);
1665 return NULL;
1666}
1667
1668static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
1669 unsigned long npages)
1670{
1671 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1672
1673 release_pages(pages, npages);
1674 kvfree(pages);
1675 sev->pages_locked -= npages;
1676}
1677
1678static void sev_clflush_pages(struct page *pages[], unsigned long npages)
1679{
1680 uint8_t *page_virtual;
1681 unsigned long i;
1682
1683 if (npages == 0 || pages == NULL)
1684 return;
1685
1686 for (i = 0; i < npages; i++) {
1687 page_virtual = kmap_atomic(pages[i]);
1688 clflush_cache_range(page_virtual, PAGE_SIZE);
1689 kunmap_atomic(page_virtual);
1690 }
1691}
1692
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001693static void __unregister_enc_region_locked(struct kvm *kvm,
1694 struct enc_region *region)
1695{
1696 /*
1697 * The guest may change the memory encryption attribute from C=0 -> C=1
1698 * or vice versa for this memory range. Lets make sure caches are
1699 * flushed to ensure that guest data gets written into memory with
1700 * correct C-bit.
1701 */
1702 sev_clflush_pages(region->pages, region->npages);
1703
1704 sev_unpin_memory(kvm, region->pages, region->npages);
1705 list_del(&region->list);
1706 kfree(region);
1707}
1708
Brijesh Singh1654efc2017-12-04 10:57:34 -06001709static void sev_vm_destroy(struct kvm *kvm)
1710{
Brijesh Singh59414c92017-12-04 10:57:35 -06001711 struct kvm_sev_info *sev = &kvm->arch.sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001712 struct list_head *head = &sev->regions_list;
1713 struct list_head *pos, *q;
Brijesh Singh59414c92017-12-04 10:57:35 -06001714
Brijesh Singh1654efc2017-12-04 10:57:34 -06001715 if (!sev_guest(kvm))
1716 return;
1717
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001718 mutex_lock(&kvm->lock);
1719
1720 /*
1721 * if userspace was terminated before unregistering the memory regions
1722 * then lets unpin all the registered memory.
1723 */
1724 if (!list_empty(head)) {
1725 list_for_each_safe(pos, q, head) {
1726 __unregister_enc_region_locked(kvm,
1727 list_entry(pos, struct enc_region, list));
1728 }
1729 }
1730
1731 mutex_unlock(&kvm->lock);
1732
Brijesh Singh59414c92017-12-04 10:57:35 -06001733 sev_unbind_asid(kvm, sev->handle);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001734 sev_asid_free(kvm);
1735}
1736
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001737static void avic_vm_destroy(struct kvm *kvm)
1738{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001739 unsigned long flags;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001740 struct kvm_arch *vm_data = &kvm->arch;
1741
Dmitry Vyukov3863dff2017-01-24 14:06:48 +01001742 if (!avic)
1743 return;
1744
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001745 if (vm_data->avic_logical_id_table_page)
1746 __free_page(vm_data->avic_logical_id_table_page);
1747 if (vm_data->avic_physical_id_table_page)
1748 __free_page(vm_data->avic_physical_id_table_page);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001749
1750 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1751 hash_del(&vm_data->hnode);
1752 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001753}
1754
Brijesh Singh1654efc2017-12-04 10:57:34 -06001755static void svm_vm_destroy(struct kvm *kvm)
1756{
1757 avic_vm_destroy(kvm);
1758 sev_vm_destroy(kvm);
1759}
1760
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001761static int avic_vm_init(struct kvm *kvm)
1762{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001763 unsigned long flags;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001764 int err = -ENOMEM;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001765 struct kvm_arch *vm_data = &kvm->arch;
1766 struct page *p_page;
1767 struct page *l_page;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001768 struct kvm_arch *ka;
1769 u32 vm_id;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001770
1771 if (!avic)
1772 return 0;
1773
1774 /* Allocating physical APIC ID table (4KB) */
1775 p_page = alloc_page(GFP_KERNEL);
1776 if (!p_page)
1777 goto free_avic;
1778
1779 vm_data->avic_physical_id_table_page = p_page;
1780 clear_page(page_address(p_page));
1781
1782 /* Allocating logical APIC ID table (4KB) */
1783 l_page = alloc_page(GFP_KERNEL);
1784 if (!l_page)
1785 goto free_avic;
1786
1787 vm_data->avic_logical_id_table_page = l_page;
1788 clear_page(page_address(l_page));
1789
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001790 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001791 again:
1792 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
1793 if (vm_id == 0) { /* id is 1-based, zero is not okay */
1794 next_vm_id_wrapped = 1;
1795 goto again;
1796 }
1797 /* Is it still in use? Only possible if wrapped at least once */
1798 if (next_vm_id_wrapped) {
1799 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1800 struct kvm *k2 = container_of(ka, struct kvm, arch);
1801 struct kvm_arch *vd2 = &k2->arch;
1802 if (vd2->avic_vm_id == vm_id)
1803 goto again;
1804 }
1805 }
1806 vm_data->avic_vm_id = vm_id;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001807 hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id);
1808 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1809
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001810 return 0;
1811
1812free_avic:
1813 avic_vm_destroy(kvm);
1814 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815}
1816
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001817static inline int
1818avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001819{
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001820 int ret = 0;
1821 unsigned long flags;
1822 struct amd_svm_iommu_ir *ir;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001823 struct vcpu_svm *svm = to_svm(vcpu);
1824
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001825 if (!kvm_arch_has_assigned_device(vcpu->kvm))
1826 return 0;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001827
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001828 /*
1829 * Here, we go through the per-vcpu ir_list to update all existing
1830 * interrupt remapping table entry targeting this vcpu.
1831 */
1832 spin_lock_irqsave(&svm->ir_list_lock, flags);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001833
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001834 if (list_empty(&svm->ir_list))
1835 goto out;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001836
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001837 list_for_each_entry(ir, &svm->ir_list, node) {
1838 ret = amd_iommu_update_ga(cpu, r, ir->data);
1839 if (ret)
1840 break;
1841 }
1842out:
1843 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
1844 return ret;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001845}
1846
1847static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1848{
1849 u64 entry;
1850 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05001851 int h_physical_id = kvm_cpu_get_apicid(cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001852 struct vcpu_svm *svm = to_svm(vcpu);
1853
1854 if (!kvm_vcpu_apicv_active(vcpu))
1855 return;
1856
1857 if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
1858 return;
1859
1860 entry = READ_ONCE(*(svm->avic_physical_id_cache));
1861 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
1862
1863 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
1864 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
1865
1866 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1867 if (svm->avic_is_running)
1868 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1869
1870 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001871 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
1872 svm->avic_is_running);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001873}
1874
1875static void avic_vcpu_put(struct kvm_vcpu *vcpu)
1876{
1877 u64 entry;
1878 struct vcpu_svm *svm = to_svm(vcpu);
1879
1880 if (!kvm_vcpu_apicv_active(vcpu))
1881 return;
1882
1883 entry = READ_ONCE(*(svm->avic_physical_id_cache));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001884 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
1885 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
1886
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001887 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1888 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889}
1890
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001891/**
1892 * This function is called during VCPU halt/unhalt.
1893 */
1894static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
1895{
1896 struct vcpu_svm *svm = to_svm(vcpu);
1897
1898 svm->avic_is_running = is_run;
1899 if (is_run)
1900 avic_vcpu_load(vcpu, vcpu->cpu);
1901 else
1902 avic_vcpu_put(vcpu);
1903}
1904
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001905static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001906{
1907 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001908 u32 dummy;
1909 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001910
Wanpeng Li518e7b92018-02-28 14:03:31 +08001911 vcpu->arch.microcode_version = 0x01000065;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001912 svm->spec_ctrl = 0;
1913
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001914 if (!init_event) {
1915 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1916 MSR_IA32_APICBASE_ENABLE;
1917 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1918 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1919 }
Paolo Bonzini56908912015-10-19 11:30:19 +02001920 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001921
Yu Zhange911eb32017-08-24 20:27:52 +08001922 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001923 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001924
1925 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1926 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001927}
1928
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001929static int avic_init_vcpu(struct vcpu_svm *svm)
1930{
1931 int ret;
1932
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001933 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001934 return 0;
1935
1936 ret = avic_init_backing_page(&svm->vcpu);
1937 if (ret)
1938 return ret;
1939
1940 INIT_LIST_HEAD(&svm->ir_list);
1941 spin_lock_init(&svm->ir_list_lock);
1942
1943 return ret;
1944}
1945
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001946static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001948 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001950 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001951 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001952 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001953 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954
Rusty Russellc16f8622007-07-30 21:12:19 +10001955 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001956 if (!svm) {
1957 err = -ENOMEM;
1958 goto out;
1959 }
1960
1961 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1962 if (err)
1963 goto free_svm;
1964
Joerg Roedelf65c2292008-02-13 18:58:46 +01001965 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001966 page = alloc_page(GFP_KERNEL);
1967 if (!page)
1968 goto uninit;
1969
Joerg Roedelf65c2292008-02-13 18:58:46 +01001970 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1971 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001972 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001973
1974 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1975 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001976 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001977
Alexander Grafb286d5d2008-11-25 20:17:05 +01001978 hsave_page = alloc_page(GFP_KERNEL);
1979 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001980 goto free_page3;
1981
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001982 err = avic_init_vcpu(svm);
1983 if (err)
1984 goto free_page4;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001985
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001986 /* We initialize this flag to true to make sure that the is_running
1987 * bit would be set the first time the vcpu is loaded.
1988 */
1989 svm->avic_is_running = true;
1990
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001991 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001992
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001993 svm->msrpm = page_address(msrpm_pages);
1994 svm_vcpu_init_msrpm(svm->msrpm);
1995
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001996 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001997 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001998
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001999 svm->vmcb = page_address(page);
2000 clear_page(svm->vmcb);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002001 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002002 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02002003 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002005 svm_init_osvw(&svm->vcpu);
2006
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002007 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08002008
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002009free_page4:
2010 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002011free_page3:
2012 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
2013free_page2:
2014 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
2015free_page1:
2016 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002017uninit:
2018 kvm_vcpu_uninit(&svm->vcpu);
2019free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10002020 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002021out:
2022 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023}
2024
2025static void svm_free_vcpu(struct kvm_vcpu *vcpu)
2026{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002027 struct vcpu_svm *svm = to_svm(vcpu);
2028
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002029 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01002030 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002031 __free_page(virt_to_page(svm->nested.hsave));
2032 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002033 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002034 kmem_cache_free(kvm_vcpu_cache, svm);
Ashok Raj15d45072018-02-01 22:59:43 +01002035 /*
2036 * The vmcb page can be recycled, causing a false negative in
2037 * svm_vcpu_load(). So do a full IBPB now.
2038 */
2039 indirect_branch_prediction_barrier();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040}
2041
Avi Kivity15ad7142007-07-11 18:17:21 +03002042static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002043{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002044 struct vcpu_svm *svm = to_svm(vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01002045 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002046 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02002047
Avi Kivity0cc50642007-03-25 12:07:27 +02002048 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002049 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002050 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02002051 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002052
Avi Kivity82ca2d12010-10-21 12:20:34 +02002053#ifdef CONFIG_X86_64
2054 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
2055#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02002056 savesegment(fs, svm->host.fs);
2057 savesegment(gs, svm->host.gs);
2058 svm->host.ldt = kvm_read_ldt();
2059
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002060 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002061 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002062
Haozhong Zhangad7218832015-10-20 15:39:02 +08002063 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
2064 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2065 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
2066 __this_cpu_write(current_tsc_ratio, tsc_ratio);
2067 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
2068 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002069 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01002070 /* This assumes that the kernel never uses MSR_TSC_AUX */
2071 if (static_cpu_has(X86_FEATURE_RDTSCP))
2072 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002073
Ashok Raj15d45072018-02-01 22:59:43 +01002074 if (sd->current_vmcb != svm->vmcb) {
2075 sd->current_vmcb = svm->vmcb;
2076 indirect_branch_prediction_barrier();
2077 }
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002078 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002079}
2080
2081static void svm_vcpu_put(struct kvm_vcpu *vcpu)
2082{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002083 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002084 int i;
2085
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002086 avic_vcpu_put(vcpu);
2087
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002088 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02002089 kvm_load_ldt(svm->host.ldt);
2090#ifdef CONFIG_X86_64
2091 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07002092 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01002093 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02002094#else
Avi Kivity831ca602011-03-08 16:09:51 +02002095#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02002096 loadsegment(gs, svm->host.gs);
2097#endif
Avi Kivity831ca602011-03-08 16:09:51 +02002098#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002099 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002100 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101}
2102
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002103static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
2104{
2105 avic_set_running(vcpu, false);
2106}
2107
2108static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
2109{
2110 avic_set_running(vcpu, true);
2111}
2112
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
2114{
Ladi Prosek9b611742017-06-21 09:06:59 +02002115 struct vcpu_svm *svm = to_svm(vcpu);
2116 unsigned long rflags = svm->vmcb->save.rflags;
2117
2118 if (svm->nmi_singlestep) {
2119 /* Hide our flags if they were not set by the guest */
2120 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
2121 rflags &= ~X86_EFLAGS_TF;
2122 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
2123 rflags &= ~X86_EFLAGS_RF;
2124 }
2125 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126}
2127
2128static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2129{
Ladi Prosek9b611742017-06-21 09:06:59 +02002130 if (to_svm(vcpu)->nmi_singlestep)
2131 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2132
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002133 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02002134 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002135 * (caused by either a task switch or an inter-privilege IRET),
2136 * so we do not need to update the CPL here.
2137 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002138 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139}
2140
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002141static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
2142{
2143 switch (reg) {
2144 case VCPU_EXREG_PDPTR:
2145 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002146 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002147 break;
2148 default:
2149 BUG();
2150 }
2151}
2152
Alexander Graff0b85052008-11-25 20:17:01 +01002153static void svm_set_vintr(struct vcpu_svm *svm)
2154{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002155 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002156}
2157
2158static void svm_clear_vintr(struct vcpu_svm *svm)
2159{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002160 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002161}
2162
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
2164{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002165 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166
2167 switch (seg) {
2168 case VCPU_SREG_CS: return &save->cs;
2169 case VCPU_SREG_DS: return &save->ds;
2170 case VCPU_SREG_ES: return &save->es;
2171 case VCPU_SREG_FS: return &save->fs;
2172 case VCPU_SREG_GS: return &save->gs;
2173 case VCPU_SREG_SS: return &save->ss;
2174 case VCPU_SREG_TR: return &save->tr;
2175 case VCPU_SREG_LDTR: return &save->ldtr;
2176 }
2177 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00002178 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002179}
2180
2181static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2182{
2183 struct vmcb_seg *s = svm_seg(vcpu, seg);
2184
2185 return s->base;
2186}
2187
2188static void svm_get_segment(struct kvm_vcpu *vcpu,
2189 struct kvm_segment *var, int seg)
2190{
2191 struct vmcb_seg *s = svm_seg(vcpu, seg);
2192
2193 var->base = s->base;
2194 var->limit = s->limit;
2195 var->selector = s->selector;
2196 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
2197 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
2198 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
2199 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
2200 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
2201 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
2202 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05302203
2204 /*
2205 * AMD CPUs circa 2014 track the G bit for all segments except CS.
2206 * However, the SVM spec states that the G bit is not observed by the
2207 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
2208 * So let's synthesize a legal G bit for all segments, this helps
2209 * running KVM nested. It also helps cross-vendor migration, because
2210 * Intel's vmentry has a check on the 'G' bit.
2211 */
2212 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00002213
Joerg Roedele0231712010-02-24 18:59:10 +01002214 /*
2215 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02002216 * for cross vendor migration purposes by "not present"
2217 */
Gioh Kim8eae9572017-05-30 15:24:45 +02002218 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02002219
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002220 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002221 case VCPU_SREG_TR:
2222 /*
2223 * Work around a bug where the busy flag in the tr selector
2224 * isn't exposed
2225 */
Amit Shahc0d09822008-10-27 09:04:18 +00002226 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002227 break;
2228 case VCPU_SREG_DS:
2229 case VCPU_SREG_ES:
2230 case VCPU_SREG_FS:
2231 case VCPU_SREG_GS:
2232 /*
2233 * The accessed bit must always be set in the segment
2234 * descriptor cache, although it can be cleared in the
2235 * descriptor, the cached bit always remains at 1. Since
2236 * Intel has a check on this, set it here to support
2237 * cross-vendor migration.
2238 */
2239 if (!var->unusable)
2240 var->type |= 0x1;
2241 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02002242 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01002243 /*
2244 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02002245 * descriptor is left as 1, although the whole segment has
2246 * been made unusable. Clear it here to pass an Intel VMX
2247 * entry check when cross vendor migrating.
2248 */
2249 if (var->unusable)
2250 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02002251 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02002252 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02002253 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002254 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255}
2256
Izik Eidus2e4d2652008-03-24 19:38:34 +02002257static int svm_get_cpl(struct kvm_vcpu *vcpu)
2258{
2259 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
2260
2261 return save->cpl;
2262}
2263
Gleb Natapov89a27f42010-02-16 10:51:48 +02002264static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002266 struct vcpu_svm *svm = to_svm(vcpu);
2267
Gleb Natapov89a27f42010-02-16 10:51:48 +02002268 dt->size = svm->vmcb->save.idtr.limit;
2269 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270}
2271
Gleb Natapov89a27f42010-02-16 10:51:48 +02002272static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002274 struct vcpu_svm *svm = to_svm(vcpu);
2275
Gleb Natapov89a27f42010-02-16 10:51:48 +02002276 svm->vmcb->save.idtr.limit = dt->size;
2277 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002278 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279}
2280
Gleb Natapov89a27f42010-02-16 10:51:48 +02002281static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002283 struct vcpu_svm *svm = to_svm(vcpu);
2284
Gleb Natapov89a27f42010-02-16 10:51:48 +02002285 dt->size = svm->vmcb->save.gdtr.limit;
2286 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287}
2288
Gleb Natapov89a27f42010-02-16 10:51:48 +02002289static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002291 struct vcpu_svm *svm = to_svm(vcpu);
2292
Gleb Natapov89a27f42010-02-16 10:51:48 +02002293 svm->vmcb->save.gdtr.limit = dt->size;
2294 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002295 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296}
2297
Avi Kivitye8467fd2009-12-29 18:43:06 +02002298static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2299{
2300}
2301
Avi Kivityaff48ba2010-12-05 18:56:11 +02002302static void svm_decache_cr3(struct kvm_vcpu *vcpu)
2303{
2304}
2305
Anthony Liguori25c4c272007-04-27 09:29:21 +03002306static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002307{
2308}
2309
Avi Kivityd2251572010-01-06 10:55:27 +02002310static void update_cr0_intercept(struct vcpu_svm *svm)
2311{
2312 ulong gcr0 = svm->vcpu.arch.cr0;
2313 u64 *hcr0 = &svm->vmcb->save.cr0;
2314
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002315 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
2316 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
Avi Kivityd2251572010-01-06 10:55:27 +02002317
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002318 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002319
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002320 if (gcr0 == *hcr0) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002321 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
2322 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002323 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002324 set_cr_intercept(svm, INTERCEPT_CR0_READ);
2325 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002326 }
2327}
2328
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2330{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002331 struct vcpu_svm *svm = to_svm(vcpu);
2332
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002333#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002334 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002335 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002336 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002337 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338 }
2339
Mike Dayd77c26f2007-10-08 09:02:08 -04002340 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002341 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002342 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343 }
2344 }
2345#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002346 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002347
2348 if (!npt_enabled)
2349 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02002350
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02002351 /*
2352 * re-enable caching here because the QEMU bios
2353 * does not do it - this results in some delay at
2354 * reboot
2355 */
2356 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
2357 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002358 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002359 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002360 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361}
2362
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002363static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002365 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002366 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
2367
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002368 if (cr4 & X86_CR4_VMXE)
2369 return 1;
2370
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002371 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002372 svm_flush_tlb(vcpu, true);
Joerg Roedel6394b642008-04-09 14:15:29 +02002373
Joerg Roedelec077262008-04-09 14:15:28 +02002374 vcpu->arch.cr4 = cr4;
2375 if (!npt_enabled)
2376 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02002377 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02002378 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002379 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002380 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381}
2382
2383static void svm_set_segment(struct kvm_vcpu *vcpu,
2384 struct kvm_segment *var, int seg)
2385{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002386 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387 struct vmcb_seg *s = svm_seg(vcpu, seg);
2388
2389 s->base = var->base;
2390 s->limit = var->limit;
2391 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02002392 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2393 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2394 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2395 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2396 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2397 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2398 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2399 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002400
2401 /*
2402 * This is always accurate, except if SYSRET returned to a segment
2403 * with SS.DPL != 3. Intel does not have this quirk, and always
2404 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2405 * would entail passing the CPL to userspace and back.
2406 */
2407 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02002408 /* This is symmetric with svm_get_segment() */
2409 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410
Joerg Roedel060d0c92010-12-03 11:45:57 +01002411 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002412}
2413
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01002414static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002416 struct vcpu_svm *svm = to_svm(vcpu);
2417
Joerg Roedel18c918c2010-11-30 18:03:59 +01002418 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03002419
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002420 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002421 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01002422 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002423 } else
2424 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03002425}
2426
Tejun Heo0fe1e002009-10-29 22:34:14 +09002427static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428{
Tejun Heo0fe1e002009-10-29 22:34:14 +09002429 if (sd->next_asid > sd->max_asid) {
2430 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06002431 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002432 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433 }
2434
Tejun Heo0fe1e002009-10-29 22:34:14 +09002435 svm->asid_generation = sd->asid_generation;
2436 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01002437
2438 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439}
2440
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002441static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2442{
2443 return to_svm(vcpu)->vmcb->save.dr6;
2444}
2445
2446static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2447{
2448 struct vcpu_svm *svm = to_svm(vcpu);
2449
2450 svm->vmcb->save.dr6 = value;
2451 mark_dirty(svm->vmcb, VMCB_DR);
2452}
2453
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002454static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2455{
2456 struct vcpu_svm *svm = to_svm(vcpu);
2457
2458 get_debugreg(vcpu->arch.db[0], 0);
2459 get_debugreg(vcpu->arch.db[1], 1);
2460 get_debugreg(vcpu->arch.db[2], 2);
2461 get_debugreg(vcpu->arch.db[3], 3);
2462 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2463 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2464
2465 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2466 set_dr_intercepts(svm);
2467}
2468
Gleb Natapov020df072010-04-13 10:05:23 +03002469static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002471 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002472
Gleb Natapov020df072010-04-13 10:05:23 +03002473 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01002474 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002475}
2476
Avi Kivity851ba692009-08-24 11:10:17 +03002477static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002479 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002480 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002482 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002483 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2484 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002485 svm->vmcb->control.insn_len);
2486}
2487
2488static int npf_interception(struct vcpu_svm *svm)
2489{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002490 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Paolo Bonzinid0006532017-08-11 18:36:43 +02002491 u64 error_code = svm->vmcb->control.exit_info_1;
2492
2493 trace_kvm_page_fault(fault_address, error_code);
2494 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002495 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2496 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002497 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498}
2499
Avi Kivity851ba692009-08-24 11:10:17 +03002500static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002501{
Avi Kivity851ba692009-08-24 11:10:17 +03002502 struct kvm_run *kvm_run = svm->vcpu.run;
2503
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002504 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03002505 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02002506 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002507 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2508 return 1;
2509 }
Gleb Natapov44c11432009-05-11 13:35:52 +03002510
Jan Kiszka6be7d302009-10-18 13:24:54 +02002511 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02002512 disable_nmi_singlestep(svm);
Gleb Natapov44c11432009-05-11 13:35:52 +03002513 }
2514
2515 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002516 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002517 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2518 kvm_run->debug.arch.pc =
2519 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2520 kvm_run->debug.arch.exception = DB_VECTOR;
2521 return 0;
2522 }
2523
2524 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002525}
2526
Avi Kivity851ba692009-08-24 11:10:17 +03002527static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002528{
Avi Kivity851ba692009-08-24 11:10:17 +03002529 struct kvm_run *kvm_run = svm->vcpu.run;
2530
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002531 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2532 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2533 kvm_run->debug.arch.exception = BP_VECTOR;
2534 return 0;
2535}
2536
Avi Kivity851ba692009-08-24 11:10:17 +03002537static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002538{
2539 int er;
2540
Andre Przywara51d8b662010-12-21 11:12:02 +01002541 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02002542 if (er == EMULATE_USER_EXIT)
2543 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002544 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002545 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002546 return 1;
2547}
2548
Eric Northup54a20552015-11-03 18:03:53 +01002549static int ac_interception(struct vcpu_svm *svm)
2550{
2551 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2552 return 1;
2553}
2554
Joerg Roedel67ec6602010-05-17 14:43:35 +02002555static bool is_erratum_383(void)
2556{
2557 int err, i;
2558 u64 value;
2559
2560 if (!erratum_383_found)
2561 return false;
2562
2563 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2564 if (err)
2565 return false;
2566
2567 /* Bit 62 may or may not be set for this mce */
2568 value &= ~(1ULL << 62);
2569
2570 if (value != 0xb600000000010015ULL)
2571 return false;
2572
2573 /* Clear MCi_STATUS registers */
2574 for (i = 0; i < 6; ++i)
2575 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2576
2577 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2578 if (!err) {
2579 u32 low, high;
2580
2581 value &= ~(1ULL << 2);
2582 low = lower_32_bits(value);
2583 high = upper_32_bits(value);
2584
2585 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2586 }
2587
2588 /* Flush tlb to evict multi-match entries */
2589 __flush_tlb_all();
2590
2591 return true;
2592}
2593
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002594static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002595{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002596 if (is_erratum_383()) {
2597 /*
2598 * Erratum 383 triggered. Guest state is corrupt so kill the
2599 * guest.
2600 */
2601 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2602
Avi Kivitya8eeb042010-05-10 12:34:53 +03002603 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002604
2605 return;
2606 }
2607
Joerg Roedel53371b52008-04-09 14:15:30 +02002608 /*
2609 * On an #MC intercept the MCE handler is not called automatically in
2610 * the host. So do it by hand here.
2611 */
2612 asm volatile (
2613 "int $0x12\n");
2614 /* not sure if we ever come back to this point */
2615
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002616 return;
2617}
2618
2619static int mc_interception(struct vcpu_svm *svm)
2620{
Joerg Roedel53371b52008-04-09 14:15:30 +02002621 return 1;
2622}
2623
Avi Kivity851ba692009-08-24 11:10:17 +03002624static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002625{
Avi Kivity851ba692009-08-24 11:10:17 +03002626 struct kvm_run *kvm_run = svm->vcpu.run;
2627
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002628 /*
2629 * VMCB is undefined after a SHUTDOWN intercept
2630 * so reinitialize it.
2631 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002632 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002633 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002634
2635 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2636 return 0;
2637}
2638
Avi Kivity851ba692009-08-24 11:10:17 +03002639static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002641 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002642 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002643 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002644 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645
Rusty Russelle756fc62007-07-30 20:07:08 +10002646 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002647 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002648 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002649 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01002650 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002651
Avi Kivity039576c2007-03-20 12:46:50 +02002652 port = io_info >> 16;
2653 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002654 svm->next_rip = svm->vmcb->control.exit_info_2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002655 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002656
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002657 /*
2658 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
2659 * KVM_EXIT_DEBUG here.
2660 */
2661 if (in)
2662 return kvm_fast_pio_in(vcpu, size, port) && ret;
2663 else
2664 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665}
2666
Avi Kivity851ba692009-08-24 11:10:17 +03002667static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002668{
2669 return 1;
2670}
2671
Avi Kivity851ba692009-08-24 11:10:17 +03002672static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002673{
2674 ++svm->vcpu.stat.irq_exits;
2675 return 1;
2676}
2677
Avi Kivity851ba692009-08-24 11:10:17 +03002678static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679{
2680 return 1;
2681}
2682
Avi Kivity851ba692009-08-24 11:10:17 +03002683static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002685 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002686 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687}
2688
Avi Kivity851ba692009-08-24 11:10:17 +03002689static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002690{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002691 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002692 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002693}
2694
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002695static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2696{
2697 struct vcpu_svm *svm = to_svm(vcpu);
2698
2699 return svm->nested.nested_cr3;
2700}
2701
Avi Kivitye4e517b2011-07-28 11:36:17 +03002702static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2703{
2704 struct vcpu_svm *svm = to_svm(vcpu);
2705 u64 cr3 = svm->nested.nested_cr3;
2706 u64 pdpte;
2707 int ret;
2708
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002709 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002710 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002711 if (ret)
2712 return 0;
2713 return pdpte;
2714}
2715
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002716static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2717 unsigned long root)
2718{
2719 struct vcpu_svm *svm = to_svm(vcpu);
2720
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002721 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01002722 mark_dirty(svm->vmcb, VMCB_NPT);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002723 svm_flush_tlb(vcpu, true);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002724}
2725
Avi Kivity6389ee92010-11-29 16:12:30 +02002726static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2727 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002728{
2729 struct vcpu_svm *svm = to_svm(vcpu);
2730
Paolo Bonzini5e352512014-09-02 13:18:37 +02002731 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2732 /*
2733 * TODO: track the cause of the nested page fault, and
2734 * correctly fill in the high bits of exit_info_1.
2735 */
2736 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2737 svm->vmcb->control.exit_code_hi = 0;
2738 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2739 svm->vmcb->control.exit_info_2 = fault->address;
2740 }
2741
2742 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2743 svm->vmcb->control.exit_info_1 |= fault->error_code;
2744
2745 /*
2746 * The present bit is always zero for page structure faults on real
2747 * hardware.
2748 */
2749 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2750 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002751
2752 nested_svm_vmexit(svm);
2753}
2754
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002755static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002756{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002757 WARN_ON(mmu_is_nested(vcpu));
2758 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002759 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2760 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002761 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002762 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
Yu Zhang855feb62017-08-24 20:27:55 +08002763 vcpu->arch.mmu.shadow_root_level = get_npt_level(vcpu);
Xiao Guangrongc258b622015-08-05 12:04:24 +08002764 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002765 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002766}
2767
2768static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2769{
2770 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2771}
2772
Alexander Grafc0725422008-11-25 20:17:03 +01002773static int nested_svm_check_permissions(struct vcpu_svm *svm)
2774{
Dan Carpentere9196ce2017-05-18 10:39:53 +03002775 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
2776 !is_paging(&svm->vcpu)) {
Alexander Grafc0725422008-11-25 20:17:03 +01002777 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2778 return 1;
2779 }
2780
2781 if (svm->vmcb->save.cpl) {
2782 kvm_inject_gp(&svm->vcpu, 0);
2783 return 1;
2784 }
2785
Dan Carpentere9196ce2017-05-18 10:39:53 +03002786 return 0;
Alexander Grafc0725422008-11-25 20:17:03 +01002787}
2788
Alexander Grafcf74a782008-11-25 20:17:08 +01002789static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2790 bool has_error_code, u32 error_code)
2791{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002792 int vmexit;
2793
Joerg Roedel20307532010-11-29 17:51:48 +01002794 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002795 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002796
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002797 vmexit = nested_svm_intercept(svm);
2798 if (vmexit != NESTED_EXIT_DONE)
2799 return 0;
2800
Joerg Roedel0295ad72009-08-07 11:49:37 +02002801 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2802 svm->vmcb->control.exit_code_hi = 0;
2803 svm->vmcb->control.exit_info_1 = error_code;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002804
2805 /*
2806 * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception.
2807 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2808 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be
2809 * written only when inject_pending_event runs (DR6 would written here
2810 * too). This should be conditional on a new capability---if the
2811 * capability is disabled, kvm_multiple_exception would write the
2812 * ancillary information to CR2 or DR6, for backwards ABI-compatibility.
2813 */
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002814 if (svm->vcpu.arch.exception.nested_apf)
2815 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
2816 else
2817 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
Joerg Roedel0295ad72009-08-07 11:49:37 +02002818
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002819 svm->nested.exit_required = true;
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002820 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002821}
2822
Joerg Roedel8fe54652010-02-19 16:23:01 +01002823/* This function returns true if it is save to enable the irq window */
2824static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002825{
Joerg Roedel20307532010-11-29 17:51:48 +01002826 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002827 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002828
Joerg Roedel26666952009-08-07 11:49:46 +02002829 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002830 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002831
Joerg Roedel26666952009-08-07 11:49:46 +02002832 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002833 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002834
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002835 /*
2836 * if vmexit was already requested (by intercepted exception
2837 * for instance) do not overwrite it with "external interrupt"
2838 * vmexit.
2839 */
2840 if (svm->nested.exit_required)
2841 return false;
2842
Joerg Roedel197717d2010-02-24 18:59:19 +01002843 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2844 svm->vmcb->control.exit_info_1 = 0;
2845 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002846
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002847 if (svm->nested.intercept & 1ULL) {
2848 /*
2849 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002850 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002851 * #vmexit emulation might sleep. Only signal request for
2852 * the #vmexit here.
2853 */
2854 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002855 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002856 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002857 }
2858
Joerg Roedel8fe54652010-02-19 16:23:01 +01002859 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002860}
2861
Joerg Roedel887f5002010-02-24 18:59:12 +01002862/* This function returns true if it is save to enable the nmi window */
2863static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2864{
Joerg Roedel20307532010-11-29 17:51:48 +01002865 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002866 return true;
2867
2868 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2869 return true;
2870
2871 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2872 svm->nested.exit_required = true;
2873
2874 return false;
2875}
2876
Joerg Roedel7597f122010-02-19 16:23:00 +01002877static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002878{
2879 struct page *page;
2880
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002881 might_sleep();
2882
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002883 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002884 if (is_error_page(page))
2885 goto error;
2886
Joerg Roedel7597f122010-02-19 16:23:00 +01002887 *_page = page;
2888
2889 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002890
2891error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002892 kvm_inject_gp(&svm->vcpu, 0);
2893
2894 return NULL;
2895}
2896
Joerg Roedel7597f122010-02-19 16:23:00 +01002897static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002898{
Joerg Roedel7597f122010-02-19 16:23:00 +01002899 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002900 kvm_release_page_dirty(page);
2901}
2902
Joerg Roedelce2ac082010-03-01 15:34:39 +01002903static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002904{
Jan Kiszka9bf41832014-06-30 10:54:17 +02002905 unsigned port, size, iopm_len;
2906 u16 val, mask;
2907 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002908 u64 gpa;
2909
2910 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2911 return NESTED_EXIT_HOST;
2912
2913 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02002914 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2915 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002916 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02002917 start_bit = port % 8;
2918 iopm_len = (start_bit + size > 8) ? 2 : 1;
2919 mask = (0xf >> (4 - size)) << start_bit;
2920 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002921
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002922 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02002923 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002924
Jan Kiszka9bf41832014-06-30 10:54:17 +02002925 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002926}
2927
Joerg Roedeld2477822010-03-01 15:34:34 +01002928static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002929{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002930 u32 offset, msr, value;
2931 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002932
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002933 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002934 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002935
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002936 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2937 offset = svm_msrpm_offset(msr);
2938 write = svm->vmcb->control.exit_info_1 & 1;
2939 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002940
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002941 if (offset == MSR_INVALID)
2942 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002943
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002944 /* Offset is in 32 bit units but need in 8 bit units */
2945 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002946
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002947 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002948 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002949
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002950 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002951}
2952
Ladi Prosekab2f4d732017-06-21 09:06:58 +02002953/* DB exceptions for our internal use must not cause vmexit */
2954static int nested_svm_intercept_db(struct vcpu_svm *svm)
2955{
2956 unsigned long dr6;
2957
2958 /* if we're not singlestepping, it's not ours */
2959 if (!svm->nmi_singlestep)
2960 return NESTED_EXIT_DONE;
2961
2962 /* if it's not a singlestep exception, it's not ours */
2963 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
2964 return NESTED_EXIT_DONE;
2965 if (!(dr6 & DR6_BS))
2966 return NESTED_EXIT_DONE;
2967
2968 /* if the guest is singlestepping, it should get the vmexit */
2969 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
2970 disable_nmi_singlestep(svm);
2971 return NESTED_EXIT_DONE;
2972 }
2973
2974 /* it's ours, the nested hypervisor must not see this one */
2975 return NESTED_EXIT_HOST;
2976}
2977
Joerg Roedel410e4d52009-08-07 11:49:44 +02002978static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002979{
Alexander Grafcf74a782008-11-25 20:17:08 +01002980 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002981
Joerg Roedel410e4d52009-08-07 11:49:44 +02002982 switch (exit_code) {
2983 case SVM_EXIT_INTR:
2984 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002985 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002986 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002987 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002988 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002989 if (npt_enabled)
2990 return NESTED_EXIT_HOST;
2991 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002992 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002993 /* When we're shadowing, trap PFs, but not async PF */
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002994 if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002995 return NESTED_EXIT_HOST;
2996 break;
2997 default:
2998 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002999 }
3000
Joerg Roedel410e4d52009-08-07 11:49:44 +02003001 return NESTED_EXIT_CONTINUE;
3002}
3003
3004/*
3005 * If this function returns true, this #vmexit was already handled
3006 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003007static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003008{
3009 u32 exit_code = svm->vmcb->control.exit_code;
3010 int vmexit = NESTED_EXIT_HOST;
3011
Alexander Grafcf74a782008-11-25 20:17:08 +01003012 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003013 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003014 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003015 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003016 case SVM_EXIT_IOIO:
3017 vmexit = nested_svm_intercept_ioio(svm);
3018 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003019 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
3020 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
3021 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003022 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003023 break;
3024 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01003025 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
3026 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
3027 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003028 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003029 break;
3030 }
3031 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
3032 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003033 if (svm->nested.intercept_exceptions & excp_bits) {
3034 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
3035 vmexit = nested_svm_intercept_db(svm);
3036 else
3037 vmexit = NESTED_EXIT_DONE;
3038 }
Gleb Natapov631bc482010-10-14 11:22:52 +02003039 /* async page fault always cause vmexit */
3040 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003041 svm->vcpu.arch.exception.nested_apf != 0)
Gleb Natapov631bc482010-10-14 11:22:52 +02003042 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003043 break;
3044 }
Joerg Roedel228070b2010-04-22 12:33:10 +02003045 case SVM_EXIT_ERR: {
3046 vmexit = NESTED_EXIT_DONE;
3047 break;
3048 }
Alexander Grafcf74a782008-11-25 20:17:08 +01003049 default: {
3050 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02003051 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003052 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003053 }
3054 }
3055
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003056 return vmexit;
3057}
3058
3059static int nested_svm_exit_handled(struct vcpu_svm *svm)
3060{
3061 int vmexit;
3062
3063 vmexit = nested_svm_intercept(svm);
3064
3065 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003066 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003067
3068 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01003069}
3070
Joerg Roedel0460a972009-08-07 11:49:31 +02003071static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
3072{
3073 struct vmcb_control_area *dst = &dst_vmcb->control;
3074 struct vmcb_control_area *from = &from_vmcb->control;
3075
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003076 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003077 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02003078 dst->intercept_exceptions = from->intercept_exceptions;
3079 dst->intercept = from->intercept;
3080 dst->iopm_base_pa = from->iopm_base_pa;
3081 dst->msrpm_base_pa = from->msrpm_base_pa;
3082 dst->tsc_offset = from->tsc_offset;
3083 dst->asid = from->asid;
3084 dst->tlb_ctl = from->tlb_ctl;
3085 dst->int_ctl = from->int_ctl;
3086 dst->int_vector = from->int_vector;
3087 dst->int_state = from->int_state;
3088 dst->exit_code = from->exit_code;
3089 dst->exit_code_hi = from->exit_code_hi;
3090 dst->exit_info_1 = from->exit_info_1;
3091 dst->exit_info_2 = from->exit_info_2;
3092 dst->exit_int_info = from->exit_int_info;
3093 dst->exit_int_info_err = from->exit_int_info_err;
3094 dst->nested_ctl = from->nested_ctl;
3095 dst->event_inj = from->event_inj;
3096 dst->event_inj_err = from->event_inj_err;
3097 dst->nested_cr3 = from->nested_cr3;
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003098 dst->virt_ext = from->virt_ext;
Joerg Roedel0460a972009-08-07 11:49:31 +02003099}
3100
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003101static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003102{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003103 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003104 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02003105 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003106 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01003107
Joerg Roedel17897f32009-10-09 16:08:29 +02003108 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
3109 vmcb->control.exit_info_1,
3110 vmcb->control.exit_info_2,
3111 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003112 vmcb->control.exit_int_info_err,
3113 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02003114
Joerg Roedel7597f122010-02-19 16:23:00 +01003115 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003116 if (!nested_vmcb)
3117 return 1;
3118
Joerg Roedel20307532010-11-29 17:51:48 +01003119 /* Exit Guest-Mode */
3120 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01003121 svm->nested.vmcb = 0;
3122
Alexander Grafcf74a782008-11-25 20:17:08 +01003123 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02003124 disable_gif(svm);
3125
3126 nested_vmcb->save.es = vmcb->save.es;
3127 nested_vmcb->save.cs = vmcb->save.cs;
3128 nested_vmcb->save.ss = vmcb->save.ss;
3129 nested_vmcb->save.ds = vmcb->save.ds;
3130 nested_vmcb->save.gdtr = vmcb->save.gdtr;
3131 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02003132 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003133 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003134 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003135 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003136 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03003137 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003138 nested_vmcb->save.rip = vmcb->save.rip;
3139 nested_vmcb->save.rsp = vmcb->save.rsp;
3140 nested_vmcb->save.rax = vmcb->save.rax;
3141 nested_vmcb->save.dr7 = vmcb->save.dr7;
3142 nested_vmcb->save.dr6 = vmcb->save.dr6;
3143 nested_vmcb->save.cpl = vmcb->save.cpl;
3144
3145 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
3146 nested_vmcb->control.int_vector = vmcb->control.int_vector;
3147 nested_vmcb->control.int_state = vmcb->control.int_state;
3148 nested_vmcb->control.exit_code = vmcb->control.exit_code;
3149 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
3150 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
3151 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
3152 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
3153 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003154
3155 if (svm->nrips_enabled)
3156 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02003157
3158 /*
3159 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
3160 * to make sure that we do not lose injected events. So check event_inj
3161 * here and copy it to exit_int_info if it is valid.
3162 * Exit_int_info and event_inj can't be both valid because the case
3163 * below only happens on a VMRUN instruction intercept which has
3164 * no valid exit_int_info set.
3165 */
3166 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
3167 struct vmcb_control_area *nc = &nested_vmcb->control;
3168
3169 nc->exit_int_info = vmcb->control.event_inj;
3170 nc->exit_int_info_err = vmcb->control.event_inj_err;
3171 }
3172
Joerg Roedel33740e42009-08-07 11:49:29 +02003173 nested_vmcb->control.tlb_ctl = 0;
3174 nested_vmcb->control.event_inj = 0;
3175 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01003176
3177 /* We always set V_INTR_MASKING and remember the old value in hflags */
3178 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
3179 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
3180
Alexander Grafcf74a782008-11-25 20:17:08 +01003181 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02003182 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01003183
Alexander Graf219b65d2009-06-15 15:21:25 +02003184 kvm_clear_exception_queue(&svm->vcpu);
3185 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003186
Joerg Roedel4b161842010-09-10 17:31:03 +02003187 svm->nested.nested_cr3 = 0;
3188
Alexander Grafcf74a782008-11-25 20:17:08 +01003189 /* Restore selected save entries */
3190 svm->vmcb->save.es = hsave->save.es;
3191 svm->vmcb->save.cs = hsave->save.cs;
3192 svm->vmcb->save.ss = hsave->save.ss;
3193 svm->vmcb->save.ds = hsave->save.ds;
3194 svm->vmcb->save.gdtr = hsave->save.gdtr;
3195 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003196 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01003197 svm_set_efer(&svm->vcpu, hsave->save.efer);
3198 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
3199 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
3200 if (npt_enabled) {
3201 svm->vmcb->save.cr3 = hsave->save.cr3;
3202 svm->vcpu.arch.cr3 = hsave->save.cr3;
3203 } else {
Avi Kivity23902182010-06-10 17:02:16 +03003204 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01003205 }
3206 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
3207 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
3208 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
3209 svm->vmcb->save.dr7 = 0;
3210 svm->vmcb->save.cpl = 0;
3211 svm->vmcb->control.exit_int_info = 0;
3212
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003213 mark_all_dirty(svm->vmcb);
3214
Joerg Roedel7597f122010-02-19 16:23:00 +01003215 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01003216
Joerg Roedel4b161842010-09-10 17:31:03 +02003217 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003218 kvm_mmu_reset_context(&svm->vcpu);
3219 kvm_mmu_load(&svm->vcpu);
3220
3221 return 0;
3222}
Alexander Graf3d6368e2008-11-25 20:17:07 +01003223
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003224static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003225{
Joerg Roedel323c3d82010-03-01 15:34:37 +01003226 /*
3227 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003228 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01003229 * the kvm msr permission bitmap may contain zero bits
3230 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01003231 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003232
Joerg Roedel323c3d82010-03-01 15:34:37 +01003233 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3234 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003235
Joerg Roedel323c3d82010-03-01 15:34:37 +01003236 for (i = 0; i < MSRPM_OFFSETS; i++) {
3237 u32 value, p;
3238 u64 offset;
3239
3240 if (msrpm_offsets[i] == 0xffffffff)
3241 break;
3242
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003243 p = msrpm_offsets[i];
3244 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01003245
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003246 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01003247 return false;
3248
3249 svm->nested.msrpm[p] = svm->msrpm[p] | value;
3250 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003251
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05003252 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
Alexander Graf3d6368e2008-11-25 20:17:07 +01003253
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003254 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003255}
3256
Joerg Roedel52c65a302010-08-02 16:46:44 +02003257static bool nested_vmcb_checks(struct vmcb *vmcb)
3258{
3259 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
3260 return false;
3261
Joerg Roedeldbe77582010-08-02 16:46:45 +02003262 if (vmcb->control.asid == 0)
3263 return false;
3264
Tom Lendackycea3a192017-12-04 10:57:24 -06003265 if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
3266 !npt_enabled)
Joerg Roedel4b161842010-09-10 17:31:03 +02003267 return false;
3268
Joerg Roedel52c65a302010-08-02 16:46:44 +02003269 return true;
3270}
3271
Ladi Prosekc2634062017-10-11 16:54:44 +02003272static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
3273 struct vmcb *nested_vmcb, struct page *page)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003274{
Avi Kivityf6e78472010-08-02 15:30:20 +03003275 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003276 svm->vcpu.arch.hflags |= HF_HIF_MASK;
3277 else
3278 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
3279
Tom Lendackycea3a192017-12-04 10:57:24 -06003280 if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
Joerg Roedel4b161842010-09-10 17:31:03 +02003281 kvm_mmu_unload(&svm->vcpu);
3282 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
3283 nested_svm_init_mmu_context(&svm->vcpu);
3284 }
3285
Alexander Graf3d6368e2008-11-25 20:17:07 +01003286 /* Load the nested guest state */
3287 svm->vmcb->save.es = nested_vmcb->save.es;
3288 svm->vmcb->save.cs = nested_vmcb->save.cs;
3289 svm->vmcb->save.ss = nested_vmcb->save.ss;
3290 svm->vmcb->save.ds = nested_vmcb->save.ds;
3291 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
3292 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003293 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003294 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
3295 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
3296 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
3297 if (npt_enabled) {
3298 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
3299 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003300 } else
Avi Kivity23902182010-06-10 17:02:16 +03003301 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003302
3303 /* Guest paging mode is active - reset mmu */
3304 kvm_mmu_reset_context(&svm->vcpu);
3305
Joerg Roedeldefbba52009-08-07 11:49:30 +02003306 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003307 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
3308 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
3309 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01003310
Alexander Graf3d6368e2008-11-25 20:17:07 +01003311 /* In case we don't even reach vcpu_run, the fields are not updated */
3312 svm->vmcb->save.rax = nested_vmcb->save.rax;
3313 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3314 svm->vmcb->save.rip = nested_vmcb->save.rip;
3315 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3316 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3317 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3318
Joerg Roedelf7138532010-03-01 15:34:40 +01003319 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003320 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003321
Joerg Roedelaad42c62009-08-07 11:49:34 +02003322 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003323 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003324 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02003325 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3326 svm->nested.intercept = nested_vmcb->control.intercept;
3327
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08003328 svm_flush_tlb(&svm->vcpu, true);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003329 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003330 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3331 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3332 else
3333 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3334
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003335 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3336 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003337 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3338 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003339 }
3340
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003341 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003342 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003343
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003344 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003345 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3346 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
3347 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003348 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3349 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3350
Joerg Roedel7597f122010-02-19 16:23:00 +01003351 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003352
Joerg Roedel20307532010-11-29 17:51:48 +01003353 /* Enter Guest-Mode */
3354 enter_guest_mode(&svm->vcpu);
3355
Joerg Roedel384c6362010-11-30 18:03:56 +01003356 /*
3357 * Merge guest and host intercepts - must be called with vcpu in
3358 * guest-mode to take affect here
3359 */
3360 recalc_intercepts(svm);
3361
Joerg Roedel06fc77722010-02-19 16:23:07 +01003362 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003363
Joerg Roedel2af91942009-08-07 11:49:28 +02003364 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003365
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003366 mark_all_dirty(svm->vmcb);
Ladi Prosekc2634062017-10-11 16:54:44 +02003367}
3368
3369static bool nested_svm_vmrun(struct vcpu_svm *svm)
3370{
3371 struct vmcb *nested_vmcb;
3372 struct vmcb *hsave = svm->nested.hsave;
3373 struct vmcb *vmcb = svm->vmcb;
3374 struct page *page;
3375 u64 vmcb_gpa;
3376
3377 vmcb_gpa = svm->vmcb->save.rax;
3378
3379 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
3380 if (!nested_vmcb)
3381 return false;
3382
3383 if (!nested_vmcb_checks(nested_vmcb)) {
3384 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3385 nested_vmcb->control.exit_code_hi = 0;
3386 nested_vmcb->control.exit_info_1 = 0;
3387 nested_vmcb->control.exit_info_2 = 0;
3388
3389 nested_svm_unmap(page);
3390
3391 return false;
3392 }
3393
3394 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3395 nested_vmcb->save.rip,
3396 nested_vmcb->control.int_ctl,
3397 nested_vmcb->control.event_inj,
3398 nested_vmcb->control.nested_ctl);
3399
3400 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3401 nested_vmcb->control.intercept_cr >> 16,
3402 nested_vmcb->control.intercept_exceptions,
3403 nested_vmcb->control.intercept);
3404
3405 /* Clear internal status */
3406 kvm_clear_exception_queue(&svm->vcpu);
3407 kvm_clear_interrupt_queue(&svm->vcpu);
3408
3409 /*
3410 * Save the old vmcb, so we don't need to pick what we save, but can
3411 * restore everything when a VMEXIT occurs
3412 */
3413 hsave->save.es = vmcb->save.es;
3414 hsave->save.cs = vmcb->save.cs;
3415 hsave->save.ss = vmcb->save.ss;
3416 hsave->save.ds = vmcb->save.ds;
3417 hsave->save.gdtr = vmcb->save.gdtr;
3418 hsave->save.idtr = vmcb->save.idtr;
3419 hsave->save.efer = svm->vcpu.arch.efer;
3420 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3421 hsave->save.cr4 = svm->vcpu.arch.cr4;
3422 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3423 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3424 hsave->save.rsp = vmcb->save.rsp;
3425 hsave->save.rax = vmcb->save.rax;
3426 if (npt_enabled)
3427 hsave->save.cr3 = vmcb->save.cr3;
3428 else
3429 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3430
3431 copy_vmcb_control_area(hsave, vmcb);
3432
3433 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003434
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003435 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003436}
3437
Joerg Roedel9966bf62009-08-07 11:49:40 +02003438static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01003439{
3440 to_vmcb->save.fs = from_vmcb->save.fs;
3441 to_vmcb->save.gs = from_vmcb->save.gs;
3442 to_vmcb->save.tr = from_vmcb->save.tr;
3443 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3444 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3445 to_vmcb->save.star = from_vmcb->save.star;
3446 to_vmcb->save.lstar = from_vmcb->save.lstar;
3447 to_vmcb->save.cstar = from_vmcb->save.cstar;
3448 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3449 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3450 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3451 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01003452}
3453
Avi Kivity851ba692009-08-24 11:10:17 +03003454static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003455{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003456 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003457 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003458 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003459
Alexander Graf55426752008-11-25 20:17:06 +01003460 if (nested_svm_check_permissions(svm))
3461 return 1;
3462
Joerg Roedel7597f122010-02-19 16:23:00 +01003463 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003464 if (!nested_vmcb)
3465 return 1;
3466
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003467 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003468 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003469
Joerg Roedel9966bf62009-08-07 11:49:40 +02003470 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003471 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003472
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003473 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003474}
3475
Avi Kivity851ba692009-08-24 11:10:17 +03003476static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003477{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003478 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003479 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003480 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003481
Alexander Graf55426752008-11-25 20:17:06 +01003482 if (nested_svm_check_permissions(svm))
3483 return 1;
3484
Joerg Roedel7597f122010-02-19 16:23:00 +01003485 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003486 if (!nested_vmcb)
3487 return 1;
3488
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003489 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003490 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003491
Joerg Roedel9966bf62009-08-07 11:49:40 +02003492 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003493 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003494
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003495 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003496}
3497
Avi Kivity851ba692009-08-24 11:10:17 +03003498static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003499{
Alexander Graf3d6368e2008-11-25 20:17:07 +01003500 if (nested_svm_check_permissions(svm))
3501 return 1;
3502
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02003503 /* Save rip after vmrun instruction */
3504 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003505
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003506 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01003507 return 1;
3508
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003509 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02003510 goto failed;
3511
3512 return 1;
3513
3514failed:
3515
3516 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3517 svm->vmcb->control.exit_code_hi = 0;
3518 svm->vmcb->control.exit_info_1 = 0;
3519 svm->vmcb->control.exit_info_2 = 0;
3520
3521 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003522
3523 return 1;
3524}
3525
Avi Kivity851ba692009-08-24 11:10:17 +03003526static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003527{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003528 int ret;
3529
Alexander Graf1371d902008-11-25 20:17:04 +01003530 if (nested_svm_check_permissions(svm))
3531 return 1;
3532
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003533 /*
3534 * If VGIF is enabled, the STGI intercept is only added to
Ladi Prosekcc3d9672017-10-17 16:02:39 +02003535 * detect the opening of the SMI/NMI window; remove it now.
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003536 */
3537 if (vgif_enabled(svm))
3538 clr_intercept(svm, INTERCEPT_STGI);
3539
Alexander Graf1371d902008-11-25 20:17:04 +01003540 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003541 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003542 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003543
Joerg Roedel2af91942009-08-07 11:49:28 +02003544 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003545
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003546 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003547}
3548
Avi Kivity851ba692009-08-24 11:10:17 +03003549static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003550{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003551 int ret;
3552
Alexander Graf1371d902008-11-25 20:17:04 +01003553 if (nested_svm_check_permissions(svm))
3554 return 1;
3555
3556 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003557 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003558
Joerg Roedel2af91942009-08-07 11:49:28 +02003559 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003560
3561 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05003562 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3563 svm_clear_vintr(svm);
3564 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3565 mark_dirty(svm->vmcb, VMCB_INTR);
3566 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003567
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003568 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003569}
3570
Avi Kivity851ba692009-08-24 11:10:17 +03003571static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02003572{
3573 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02003574
David Kaplan668f1982015-02-20 16:02:10 -06003575 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
3576 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02003577
Alexander Grafff092382009-06-15 15:21:24 +02003578 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06003579 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02003580
3581 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003582 return kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02003583}
3584
Joerg Roedel532a46b2009-10-09 16:08:32 +02003585static int skinit_interception(struct vcpu_svm *svm)
3586{
David Kaplan668f1982015-02-20 16:02:10 -06003587 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003588
3589 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3590 return 1;
3591}
3592
David Kaplandab429a2015-03-02 13:43:37 -06003593static int wbinvd_interception(struct vcpu_svm *svm)
3594{
Kyle Huey6affcbe2016-11-29 12:40:40 -08003595 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06003596}
3597
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003598static int xsetbv_interception(struct vcpu_svm *svm)
3599{
3600 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
3601 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3602
3603 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3604 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003605 return kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003606 }
3607
3608 return 1;
3609}
3610
Avi Kivity851ba692009-08-24 11:10:17 +03003611static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003612{
Izik Eidus37817f22008-03-24 23:14:53 +02003613 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003614 int reason;
3615 int int_type = svm->vmcb->control.exit_int_info &
3616 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003617 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003618 uint32_t type =
3619 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3620 uint32_t idt_v =
3621 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003622 bool has_error_code = false;
3623 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003624
3625 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003626
Izik Eidus37817f22008-03-24 23:14:53 +02003627 if (svm->vmcb->control.exit_info_2 &
3628 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003629 reason = TASK_SWITCH_IRET;
3630 else if (svm->vmcb->control.exit_info_2 &
3631 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3632 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003633 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003634 reason = TASK_SWITCH_GATE;
3635 else
3636 reason = TASK_SWITCH_CALL;
3637
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003638 if (reason == TASK_SWITCH_GATE) {
3639 switch (type) {
3640 case SVM_EXITINTINFO_TYPE_NMI:
3641 svm->vcpu.arch.nmi_injected = false;
3642 break;
3643 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003644 if (svm->vmcb->control.exit_info_2 &
3645 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3646 has_error_code = true;
3647 error_code =
3648 (u32)svm->vmcb->control.exit_info_2;
3649 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003650 kvm_clear_exception_queue(&svm->vcpu);
3651 break;
3652 case SVM_EXITINTINFO_TYPE_INTR:
3653 kvm_clear_interrupt_queue(&svm->vcpu);
3654 break;
3655 default:
3656 break;
3657 }
3658 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003659
Gleb Natapov8317c292009-04-12 13:37:02 +03003660 if (reason != TASK_SWITCH_GATE ||
3661 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3662 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03003663 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3664 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003665
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003666 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3667 int_vec = -1;
3668
3669 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03003670 has_error_code, error_code) == EMULATE_FAIL) {
3671 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3672 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3673 svm->vcpu.run->internal.ndata = 0;
3674 return 0;
3675 }
3676 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003677}
3678
Avi Kivity851ba692009-08-24 11:10:17 +03003679static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003680{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003681 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Kyle Huey6a908b62016-11-29 12:40:37 -08003682 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003683}
3684
Avi Kivity851ba692009-08-24 11:10:17 +03003685static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003686{
3687 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003688 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003689 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003690 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003691 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003692 return 1;
3693}
3694
Avi Kivity851ba692009-08-24 11:10:17 +03003695static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003696{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003697 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3698 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3699
3700 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003701 return kvm_skip_emulated_instruction(&svm->vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003702}
3703
Avi Kivity851ba692009-08-24 11:10:17 +03003704static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003705{
Andre Przywara51d8b662010-12-21 11:12:02 +01003706 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003707}
3708
Brijesh Singh7607b712018-02-19 10:14:44 -06003709static int rsm_interception(struct vcpu_svm *svm)
3710{
3711 return x86_emulate_instruction(&svm->vcpu, 0, 0,
3712 rsm_ins_bytes, 2) == EMULATE_DONE;
3713}
3714
Avi Kivity332b56e2011-11-10 14:57:24 +02003715static int rdpmc_interception(struct vcpu_svm *svm)
3716{
3717 int err;
3718
3719 if (!static_cpu_has(X86_FEATURE_NRIPS))
3720 return emulate_on_interception(svm);
3721
3722 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08003723 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02003724}
3725
Xiubo Li52eb5a62015-03-13 17:39:45 +08003726static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3727 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003728{
3729 unsigned long cr0 = svm->vcpu.arch.cr0;
3730 bool ret = false;
3731 u64 intercept;
3732
3733 intercept = svm->nested.intercept;
3734
3735 if (!is_guest_mode(&svm->vcpu) ||
3736 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3737 return false;
3738
3739 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3740 val &= ~SVM_CR0_SELECTIVE_MASK;
3741
3742 if (cr0 ^ val) {
3743 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3744 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3745 }
3746
3747 return ret;
3748}
3749
Andre Przywara7ff76d52010-12-21 11:12:04 +01003750#define CR_VALID (1ULL << 63)
3751
3752static int cr_interception(struct vcpu_svm *svm)
3753{
3754 int reg, cr;
3755 unsigned long val;
3756 int err;
3757
3758 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3759 return emulate_on_interception(svm);
3760
3761 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3762 return emulate_on_interception(svm);
3763
3764 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003765 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3766 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3767 else
3768 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003769
3770 err = 0;
3771 if (cr >= 16) { /* mov to cr */
3772 cr -= 16;
3773 val = kvm_register_read(&svm->vcpu, reg);
3774 switch (cr) {
3775 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003776 if (!check_selective_cr0_intercepted(svm, val))
3777 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003778 else
3779 return 1;
3780
Andre Przywara7ff76d52010-12-21 11:12:04 +01003781 break;
3782 case 3:
3783 err = kvm_set_cr3(&svm->vcpu, val);
3784 break;
3785 case 4:
3786 err = kvm_set_cr4(&svm->vcpu, val);
3787 break;
3788 case 8:
3789 err = kvm_set_cr8(&svm->vcpu, val);
3790 break;
3791 default:
3792 WARN(1, "unhandled write to CR%d", cr);
3793 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3794 return 1;
3795 }
3796 } else { /* mov from cr */
3797 switch (cr) {
3798 case 0:
3799 val = kvm_read_cr0(&svm->vcpu);
3800 break;
3801 case 2:
3802 val = svm->vcpu.arch.cr2;
3803 break;
3804 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003805 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003806 break;
3807 case 4:
3808 val = kvm_read_cr4(&svm->vcpu);
3809 break;
3810 case 8:
3811 val = kvm_get_cr8(&svm->vcpu);
3812 break;
3813 default:
3814 WARN(1, "unhandled read from CR%d", cr);
3815 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3816 return 1;
3817 }
3818 kvm_register_write(&svm->vcpu, reg, val);
3819 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08003820 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003821}
3822
Andre Przywaracae37972010-12-21 11:12:05 +01003823static int dr_interception(struct vcpu_svm *svm)
3824{
3825 int reg, dr;
3826 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003827
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003828 if (svm->vcpu.guest_debug == 0) {
3829 /*
3830 * No more DR vmexits; force a reload of the debug registers
3831 * and reenter on this instruction. The next vmexit will
3832 * retrieve the full state of the debug registers.
3833 */
3834 clr_dr_intercepts(svm);
3835 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3836 return 1;
3837 }
3838
Andre Przywaracae37972010-12-21 11:12:05 +01003839 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3840 return emulate_on_interception(svm);
3841
3842 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3843 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3844
3845 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003846 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3847 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003848 val = kvm_register_read(&svm->vcpu, reg);
3849 kvm_set_dr(&svm->vcpu, dr - 16, val);
3850 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003851 if (!kvm_require_dr(&svm->vcpu, dr))
3852 return 1;
3853 kvm_get_dr(&svm->vcpu, dr, &val);
3854 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003855 }
3856
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003857 return kvm_skip_emulated_instruction(&svm->vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01003858}
3859
Avi Kivity851ba692009-08-24 11:10:17 +03003860static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003861{
Avi Kivity851ba692009-08-24 11:10:17 +03003862 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003863 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003864
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003865 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3866 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003867 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003868 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003869 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003870 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003871 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003872 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3873 return 0;
3874}
3875
Tom Lendacky801e4592018-02-21 13:39:51 -06003876static int svm_get_msr_feature(struct kvm_msr_entry *msr)
3877{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01003878 msr->data = 0;
3879
3880 switch (msr->index) {
3881 case MSR_F10H_DECFG:
3882 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
3883 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
3884 break;
3885 default:
3886 return 1;
3887 }
3888
3889 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003890}
3891
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003892static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003894 struct vcpu_svm *svm = to_svm(vcpu);
3895
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003896 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303897 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003898 msr_info->data = svm->vmcb->control.tsc_offset +
Haozhong Zhang35181e82015-10-20 15:39:03 +08003899 kvm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003900
Avi Kivity6aa8b732006-12-10 02:21:36 -08003901 break;
3902 }
Brian Gerst8c065852010-07-17 09:03:26 -04003903 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003904 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003905 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003906#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003907 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003908 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003909 break;
3910 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003911 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003912 break;
3913 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003914 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003915 break;
3916 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003917 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918 break;
3919#endif
3920 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003921 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922 break;
3923 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003924 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003925 break;
3926 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003927 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003928 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003929 case MSR_TSC_AUX:
3930 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3931 return 1;
3932 msr_info->data = svm->tsc_aux;
3933 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003934 /*
3935 * Nobody will change the following 5 values in the VMCB so we can
3936 * safely return them on rdmsr. They will always be 0 until LBRV is
3937 * implemented.
3938 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003939 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003940 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01003941 break;
3942 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003943 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003944 break;
3945 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003946 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003947 break;
3948 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003949 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003950 break;
3951 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003952 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003953 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003954 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003955 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003956 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003957 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003958 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003959 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003960 case MSR_IA32_SPEC_CTRL:
3961 if (!msr_info->host_initiated &&
3962 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
3963 return 1;
3964
3965 msr_info->data = svm->spec_ctrl;
3966 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01003967 case MSR_F15H_IC_CFG: {
3968
3969 int family, model;
3970
3971 family = guest_cpuid_family(vcpu);
3972 model = guest_cpuid_model(vcpu);
3973
3974 if (family < 0 || model < 0)
3975 return kvm_get_msr_common(vcpu, msr_info);
3976
3977 msr_info->data = 0;
3978
3979 if (family == 0x15 &&
3980 (model >= 0x2 && model < 0x20))
3981 msr_info->data = 0x1E;
3982 }
3983 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01003984 case MSR_F10H_DECFG:
3985 msr_info->data = svm->msr_decfg;
3986 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003987 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003988 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003989 }
3990 return 0;
3991}
3992
Avi Kivity851ba692009-08-24 11:10:17 +03003993static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003994{
David Kaplan668f1982015-02-20 16:02:10 -06003995 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003996 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003997
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003998 msr_info.index = ecx;
3999 msr_info.host_initiated = false;
4000 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02004001 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004002 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004003 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004004 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004005 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004006
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004007 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
4008 msr_info.data & 0xffffffff);
4009 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
4010 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004011 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004012 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004014}
4015
Joerg Roedel4a810182010-02-24 18:59:15 +01004016static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
4017{
4018 struct vcpu_svm *svm = to_svm(vcpu);
4019 int svm_dis, chg_mask;
4020
4021 if (data & ~SVM_VM_CR_VALID_MASK)
4022 return 1;
4023
4024 chg_mask = SVM_VM_CR_VALID_MASK;
4025
4026 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
4027 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
4028
4029 svm->nested.vm_cr_msr &= ~chg_mask;
4030 svm->nested.vm_cr_msr |= (data & chg_mask);
4031
4032 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
4033
4034 /* check for svm_disable while efer.svme is set */
4035 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
4036 return 1;
4037
4038 return 0;
4039}
4040
Will Auld8fe8ab42012-11-29 12:42:12 -08004041static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004042{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004043 struct vcpu_svm *svm = to_svm(vcpu);
4044
Will Auld8fe8ab42012-11-29 12:42:12 -08004045 u32 ecx = msr->index;
4046 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004047 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02004048 case MSR_IA32_CR_PAT:
4049 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4050 return 1;
4051 vcpu->arch.pat = data;
4052 svm->vmcb->save.g_pat = data;
4053 mark_dirty(svm->vmcb, VMCB_NPT);
4054 break;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10004055 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08004056 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004058 case MSR_IA32_SPEC_CTRL:
4059 if (!msr->host_initiated &&
4060 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
4061 return 1;
4062
4063 /* The STIBP bit doesn't fault even if it's not advertised */
4064 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
4065 return 1;
4066
4067 svm->spec_ctrl = data;
4068
4069 if (!data)
4070 break;
4071
4072 /*
4073 * For non-nested:
4074 * When it's written (to non-zero) for the first time, pass
4075 * it through.
4076 *
4077 * For nested:
4078 * The handling of the MSR bitmap for L2 guests is done in
4079 * nested_svm_vmrun_msrpm.
4080 * We update the L1 MSR bit as well since it will end up
4081 * touching the MSR anyway now.
4082 */
4083 set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
4084 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004085 case MSR_IA32_PRED_CMD:
4086 if (!msr->host_initiated &&
4087 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB))
4088 return 1;
4089
4090 if (data & ~PRED_CMD_IBPB)
4091 return 1;
4092
4093 if (!data)
4094 break;
4095
4096 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4097 if (is_guest_mode(vcpu))
4098 break;
4099 set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
4100 break;
Brian Gerst8c065852010-07-17 09:03:26 -04004101 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004102 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004103 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08004104#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004105 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004106 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004107 break;
4108 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004109 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004110 break;
4111 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004112 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004113 break;
4114 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004115 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004116 break;
4117#endif
4118 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004119 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004120 break;
4121 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02004122 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004123 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004124 break;
4125 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02004126 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004127 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004129 case MSR_TSC_AUX:
4130 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4131 return 1;
4132
4133 /*
4134 * This is rare, so we update the MSR here instead of using
4135 * direct_access_msrs. Doing that would require a rdmsr in
4136 * svm_vcpu_put.
4137 */
4138 svm->tsc_aux = data;
4139 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
4140 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01004141 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004142 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03004143 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
4144 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004145 break;
4146 }
4147 if (data & DEBUGCTL_RESERVED_BITS)
4148 return 1;
4149
4150 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01004151 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004152 if (data & (1ULL<<0))
4153 svm_enable_lbrv(svm);
4154 else
4155 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01004156 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004157 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02004158 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004159 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004160 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01004161 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004162 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03004163 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004164 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004165 case MSR_F10H_DECFG: {
4166 struct kvm_msr_entry msr_entry;
4167
4168 msr_entry.index = msr->index;
4169 if (svm_get_msr_feature(&msr_entry))
4170 return 1;
4171
4172 /* Check the supported bits */
4173 if (data & ~msr_entry.data)
4174 return 1;
4175
4176 /* Don't allow the guest to change a bit, #GP */
4177 if (!msr->host_initiated && (data ^ msr_entry.data))
4178 return 1;
4179
4180 svm->msr_decfg = data;
4181 break;
4182 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004183 case MSR_IA32_APICBASE:
4184 if (kvm_vcpu_apicv_active(vcpu))
4185 avic_update_vapic_bar(to_svm(vcpu), data);
4186 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004187 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08004188 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189 }
4190 return 0;
4191}
4192
Avi Kivity851ba692009-08-24 11:10:17 +03004193static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004194{
Will Auld8fe8ab42012-11-29 12:42:12 -08004195 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06004196 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
4197 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004198
Will Auld8fe8ab42012-11-29 12:42:12 -08004199 msr.data = data;
4200 msr.index = ecx;
4201 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004202
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004203 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03004204 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02004205 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004206 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004207 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004208 } else {
4209 trace_kvm_msr_write(ecx, data);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004210 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02004211 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004212}
4213
Avi Kivity851ba692009-08-24 11:10:17 +03004214static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004215{
Rusty Russelle756fc62007-07-30 20:07:08 +10004216 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03004217 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004218 else
Avi Kivity851ba692009-08-24 11:10:17 +03004219 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004220}
4221
Avi Kivity851ba692009-08-24 11:10:17 +03004222static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08004223{
Avi Kivity3842d132010-07-27 12:30:24 +03004224 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01004225 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03004226 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004227 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08004228 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08004229 return 1;
4230}
4231
Mark Langsdorf565d0992009-10-06 14:25:02 -05004232static int pause_interception(struct vcpu_svm *svm)
4233{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004234 struct kvm_vcpu *vcpu = &svm->vcpu;
4235 bool in_kernel = (svm_get_cpl(vcpu) == 0);
4236
4237 kvm_vcpu_on_spin(vcpu, in_kernel);
Mark Langsdorf565d0992009-10-06 14:25:02 -05004238 return 1;
4239}
4240
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004241static int nop_interception(struct vcpu_svm *svm)
4242{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004243 return kvm_skip_emulated_instruction(&(svm->vcpu));
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004244}
4245
4246static int monitor_interception(struct vcpu_svm *svm)
4247{
4248 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
4249 return nop_interception(svm);
4250}
4251
4252static int mwait_interception(struct vcpu_svm *svm)
4253{
4254 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4255 return nop_interception(svm);
4256}
4257
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004258enum avic_ipi_failure_cause {
4259 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
4260 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
4261 AVIC_IPI_FAILURE_INVALID_TARGET,
4262 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
4263};
4264
4265static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
4266{
4267 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
4268 u32 icrl = svm->vmcb->control.exit_info_1;
4269 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03004270 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004271 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4272
4273 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
4274
4275 switch (id) {
4276 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
4277 /*
4278 * AVIC hardware handles the generation of
4279 * IPIs when the specified Message Type is Fixed
4280 * (also known as fixed delivery mode) and
4281 * the Trigger Mode is edge-triggered. The hardware
4282 * also supports self and broadcast delivery modes
4283 * specified via the Destination Shorthand(DSH)
4284 * field of the ICRL. Logical and physical APIC ID
4285 * formats are supported. All other IPI types cause
4286 * a #VMEXIT, which needs to emulated.
4287 */
4288 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
4289 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
4290 break;
4291 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
4292 int i;
4293 struct kvm_vcpu *vcpu;
4294 struct kvm *kvm = svm->vcpu.kvm;
4295 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4296
4297 /*
4298 * At this point, we expect that the AVIC HW has already
4299 * set the appropriate IRR bits on the valid target
4300 * vcpus. So, we just need to kick the appropriate vcpu.
4301 */
4302 kvm_for_each_vcpu(i, vcpu, kvm) {
4303 bool m = kvm_apic_match_dest(vcpu, apic,
4304 icrl & KVM_APIC_SHORT_MASK,
4305 GET_APIC_DEST_FIELD(icrh),
4306 icrl & KVM_APIC_DEST_MASK);
4307
4308 if (m && !avic_vcpu_is_running(vcpu))
4309 kvm_vcpu_wake_up(vcpu);
4310 }
4311 break;
4312 }
4313 case AVIC_IPI_FAILURE_INVALID_TARGET:
4314 break;
4315 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
4316 WARN_ONCE(1, "Invalid backing page\n");
4317 break;
4318 default:
4319 pr_err("Unknown IPI interception\n");
4320 }
4321
4322 return 1;
4323}
4324
4325static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
4326{
4327 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4328 int index;
4329 u32 *logical_apic_id_table;
4330 int dlid = GET_APIC_LOGICAL_ID(ldr);
4331
4332 if (!dlid)
4333 return NULL;
4334
4335 if (flat) { /* flat */
4336 index = ffs(dlid) - 1;
4337 if (index > 7)
4338 return NULL;
4339 } else { /* cluster */
4340 int cluster = (dlid & 0xf0) >> 4;
4341 int apic = ffs(dlid & 0x0f) - 1;
4342
4343 if ((apic < 0) || (apic > 7) ||
4344 (cluster >= 0xf))
4345 return NULL;
4346 index = (cluster << 2) + apic;
4347 }
4348
4349 logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page);
4350
4351 return &logical_apic_id_table[index];
4352}
4353
4354static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
4355 bool valid)
4356{
4357 bool flat;
4358 u32 *entry, new_entry;
4359
4360 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4361 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4362 if (!entry)
4363 return -EINVAL;
4364
4365 new_entry = READ_ONCE(*entry);
4366 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4367 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
4368 if (valid)
4369 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4370 else
4371 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4372 WRITE_ONCE(*entry, new_entry);
4373
4374 return 0;
4375}
4376
4377static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4378{
4379 int ret;
4380 struct vcpu_svm *svm = to_svm(vcpu);
4381 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
4382
4383 if (!ldr)
4384 return 1;
4385
4386 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
4387 if (ret && svm->ldr_reg) {
4388 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
4389 svm->ldr_reg = 0;
4390 } else {
4391 svm->ldr_reg = ldr;
4392 }
4393 return ret;
4394}
4395
4396static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4397{
4398 u64 *old, *new;
4399 struct vcpu_svm *svm = to_svm(vcpu);
4400 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
4401 u32 id = (apic_id_reg >> 24) & 0xff;
4402
4403 if (vcpu->vcpu_id == id)
4404 return 0;
4405
4406 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4407 new = avic_get_physical_id_entry(vcpu, id);
4408 if (!new || !old)
4409 return 1;
4410
4411 /* We need to move physical_id_entry to new offset */
4412 *new = *old;
4413 *old = 0ULL;
4414 to_svm(vcpu)->avic_physical_id_cache = new;
4415
4416 /*
4417 * Also update the guest physical APIC ID in the logical
4418 * APIC ID table entry if already setup the LDR.
4419 */
4420 if (svm->ldr_reg)
4421 avic_handle_ldr_update(vcpu);
4422
4423 return 0;
4424}
4425
4426static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
4427{
4428 struct vcpu_svm *svm = to_svm(vcpu);
4429 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4430 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
4431 u32 mod = (dfr >> 28) & 0xf;
4432
4433 /*
4434 * We assume that all local APICs are using the same type.
4435 * If this changes, we need to flush the AVIC logical
4436 * APID id table.
4437 */
4438 if (vm_data->ldr_mode == mod)
4439 return 0;
4440
4441 clear_page(page_address(vm_data->avic_logical_id_table_page));
4442 vm_data->ldr_mode = mod;
4443
4444 if (svm->ldr_reg)
4445 avic_handle_ldr_update(vcpu);
4446 return 0;
4447}
4448
4449static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4450{
4451 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4452 u32 offset = svm->vmcb->control.exit_info_1 &
4453 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4454
4455 switch (offset) {
4456 case APIC_ID:
4457 if (avic_handle_apic_id_update(&svm->vcpu))
4458 return 0;
4459 break;
4460 case APIC_LDR:
4461 if (avic_handle_ldr_update(&svm->vcpu))
4462 return 0;
4463 break;
4464 case APIC_DFR:
4465 avic_handle_dfr_update(&svm->vcpu);
4466 break;
4467 default:
4468 break;
4469 }
4470
4471 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4472
4473 return 1;
4474}
4475
4476static bool is_avic_unaccelerated_access_trap(u32 offset)
4477{
4478 bool ret = false;
4479
4480 switch (offset) {
4481 case APIC_ID:
4482 case APIC_EOI:
4483 case APIC_RRR:
4484 case APIC_LDR:
4485 case APIC_DFR:
4486 case APIC_SPIV:
4487 case APIC_ESR:
4488 case APIC_ICR:
4489 case APIC_LVTT:
4490 case APIC_LVTTHMR:
4491 case APIC_LVTPC:
4492 case APIC_LVT0:
4493 case APIC_LVT1:
4494 case APIC_LVTERR:
4495 case APIC_TMICT:
4496 case APIC_TDCR:
4497 ret = true;
4498 break;
4499 default:
4500 break;
4501 }
4502 return ret;
4503}
4504
4505static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4506{
4507 int ret = 0;
4508 u32 offset = svm->vmcb->control.exit_info_1 &
4509 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4510 u32 vector = svm->vmcb->control.exit_info_2 &
4511 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4512 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4513 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4514 bool trap = is_avic_unaccelerated_access_trap(offset);
4515
4516 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4517 trap, write, vector);
4518 if (trap) {
4519 /* Handling Trap */
4520 WARN_ONCE(!write, "svm: Handling trap read.\n");
4521 ret = avic_unaccel_trap_write(svm);
4522 } else {
4523 /* Handling Fault */
4524 ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
4525 }
4526
4527 return ret;
4528}
4529
Mathias Krause09941fb2012-08-30 01:30:20 +02004530static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01004531 [SVM_EXIT_READ_CR0] = cr_interception,
4532 [SVM_EXIT_READ_CR3] = cr_interception,
4533 [SVM_EXIT_READ_CR4] = cr_interception,
4534 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06004535 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02004536 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01004537 [SVM_EXIT_WRITE_CR3] = cr_interception,
4538 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004539 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01004540 [SVM_EXIT_READ_DR0] = dr_interception,
4541 [SVM_EXIT_READ_DR1] = dr_interception,
4542 [SVM_EXIT_READ_DR2] = dr_interception,
4543 [SVM_EXIT_READ_DR3] = dr_interception,
4544 [SVM_EXIT_READ_DR4] = dr_interception,
4545 [SVM_EXIT_READ_DR5] = dr_interception,
4546 [SVM_EXIT_READ_DR6] = dr_interception,
4547 [SVM_EXIT_READ_DR7] = dr_interception,
4548 [SVM_EXIT_WRITE_DR0] = dr_interception,
4549 [SVM_EXIT_WRITE_DR1] = dr_interception,
4550 [SVM_EXIT_WRITE_DR2] = dr_interception,
4551 [SVM_EXIT_WRITE_DR3] = dr_interception,
4552 [SVM_EXIT_WRITE_DR4] = dr_interception,
4553 [SVM_EXIT_WRITE_DR5] = dr_interception,
4554 [SVM_EXIT_WRITE_DR6] = dr_interception,
4555 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004556 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4557 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004558 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004559 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004560 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01004561 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004562 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02004563 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004564 [SVM_EXIT_SMI] = nop_on_interception,
4565 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08004566 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02004567 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004568 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004569 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02004570 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05004571 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004572 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004573 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02004574 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004575 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004576 [SVM_EXIT_MSR] = msr_interception,
4577 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08004578 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01004579 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02004580 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01004581 [SVM_EXIT_VMLOAD] = vmload_interception,
4582 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01004583 [SVM_EXIT_STGI] = stgi_interception,
4584 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02004585 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06004586 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004587 [SVM_EXIT_MONITOR] = monitor_interception,
4588 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01004589 [SVM_EXIT_XSETBV] = xsetbv_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02004590 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06004591 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004592 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4593 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004594};
4595
Joe Perchesae8cc052011-04-24 22:00:50 -07004596static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02004597{
4598 struct vcpu_svm *svm = to_svm(vcpu);
4599 struct vmcb_control_area *control = &svm->vmcb->control;
4600 struct vmcb_save_area *save = &svm->vmcb->save;
4601
4602 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004603 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4604 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4605 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4606 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4607 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4608 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4609 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
4610 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4611 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4612 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4613 pr_err("%-20s%d\n", "asid:", control->asid);
4614 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4615 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4616 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4617 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4618 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4619 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4620 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4621 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4622 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4623 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4624 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004625 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07004626 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4627 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05004628 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07004629 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004630 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4631 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4632 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004633 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004634 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4635 "es:",
4636 save->es.selector, save->es.attrib,
4637 save->es.limit, save->es.base);
4638 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4639 "cs:",
4640 save->cs.selector, save->cs.attrib,
4641 save->cs.limit, save->cs.base);
4642 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4643 "ss:",
4644 save->ss.selector, save->ss.attrib,
4645 save->ss.limit, save->ss.base);
4646 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4647 "ds:",
4648 save->ds.selector, save->ds.attrib,
4649 save->ds.limit, save->ds.base);
4650 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4651 "fs:",
4652 save->fs.selector, save->fs.attrib,
4653 save->fs.limit, save->fs.base);
4654 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4655 "gs:",
4656 save->gs.selector, save->gs.attrib,
4657 save->gs.limit, save->gs.base);
4658 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4659 "gdtr:",
4660 save->gdtr.selector, save->gdtr.attrib,
4661 save->gdtr.limit, save->gdtr.base);
4662 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4663 "ldtr:",
4664 save->ldtr.selector, save->ldtr.attrib,
4665 save->ldtr.limit, save->ldtr.base);
4666 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4667 "idtr:",
4668 save->idtr.selector, save->idtr.attrib,
4669 save->idtr.limit, save->idtr.base);
4670 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4671 "tr:",
4672 save->tr.selector, save->tr.attrib,
4673 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004674 pr_err("cpl: %d efer: %016llx\n",
4675 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004676 pr_err("%-15s %016llx %-13s %016llx\n",
4677 "cr0:", save->cr0, "cr2:", save->cr2);
4678 pr_err("%-15s %016llx %-13s %016llx\n",
4679 "cr3:", save->cr3, "cr4:", save->cr4);
4680 pr_err("%-15s %016llx %-13s %016llx\n",
4681 "dr6:", save->dr6, "dr7:", save->dr7);
4682 pr_err("%-15s %016llx %-13s %016llx\n",
4683 "rip:", save->rip, "rflags:", save->rflags);
4684 pr_err("%-15s %016llx %-13s %016llx\n",
4685 "rsp:", save->rsp, "rax:", save->rax);
4686 pr_err("%-15s %016llx %-13s %016llx\n",
4687 "star:", save->star, "lstar:", save->lstar);
4688 pr_err("%-15s %016llx %-13s %016llx\n",
4689 "cstar:", save->cstar, "sfmask:", save->sfmask);
4690 pr_err("%-15s %016llx %-13s %016llx\n",
4691 "kernel_gs_base:", save->kernel_gs_base,
4692 "sysenter_cs:", save->sysenter_cs);
4693 pr_err("%-15s %016llx %-13s %016llx\n",
4694 "sysenter_esp:", save->sysenter_esp,
4695 "sysenter_eip:", save->sysenter_eip);
4696 pr_err("%-15s %016llx %-13s %016llx\n",
4697 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4698 pr_err("%-15s %016llx %-13s %016llx\n",
4699 "br_from:", save->br_from, "br_to:", save->br_to);
4700 pr_err("%-15s %016llx %-13s %016llx\n",
4701 "excp_from:", save->last_excp_from,
4702 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004703}
4704
Avi Kivity586f9602010-11-18 13:09:54 +02004705static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4706{
4707 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4708
4709 *info1 = control->exit_info_1;
4710 *info2 = control->exit_info_2;
4711}
4712
Avi Kivity851ba692009-08-24 11:10:17 +03004713static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004714{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004715 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004716 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004717 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004718
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004719 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4720
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004721 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004722 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4723 if (npt_enabled)
4724 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004725
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004726 if (unlikely(svm->nested.exit_required)) {
4727 nested_svm_vmexit(svm);
4728 svm->nested.exit_required = false;
4729
4730 return 1;
4731 }
4732
Joerg Roedel20307532010-11-29 17:51:48 +01004733 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004734 int vmexit;
4735
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004736 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4737 svm->vmcb->control.exit_info_1,
4738 svm->vmcb->control.exit_info_2,
4739 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004740 svm->vmcb->control.exit_int_info_err,
4741 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004742
Joerg Roedel410e4d52009-08-07 11:49:44 +02004743 vmexit = nested_svm_exit_special(svm);
4744
4745 if (vmexit == NESTED_EXIT_CONTINUE)
4746 vmexit = nested_svm_exit_handled(svm);
4747
4748 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004749 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004750 }
4751
Joerg Roedela5c38322009-08-07 11:49:32 +02004752 svm_complete_interrupts(svm);
4753
Avi Kivity04d2cc72007-09-10 18:10:54 +03004754 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4755 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4756 kvm_run->fail_entry.hardware_entry_failure_reason
4757 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004758 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4759 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004760 return 0;
4761 }
4762
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004763 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004764 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004765 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4766 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004767 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004768 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004769 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004770 exit_code);
4771
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004772 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004773 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04004774 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03004775 kvm_queue_exception(vcpu, UD_VECTOR);
4776 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004777 }
4778
Avi Kivity851ba692009-08-24 11:10:17 +03004779 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004780}
4781
4782static void reload_tss(struct kvm_vcpu *vcpu)
4783{
4784 int cpu = raw_smp_processor_id();
4785
Tejun Heo0fe1e002009-10-29 22:34:14 +09004786 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4787 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788 load_TR_desc();
4789}
4790
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004791static void pre_sev_run(struct vcpu_svm *svm, int cpu)
4792{
4793 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4794 int asid = sev_get_asid(svm->vcpu.kvm);
4795
4796 /* Assign the asid allocated with this SEV guest */
4797 svm->vmcb->control.asid = asid;
4798
4799 /*
4800 * Flush guest TLB:
4801 *
4802 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
4803 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
4804 */
4805 if (sd->sev_vmcbs[asid] == svm->vmcb &&
4806 svm->last_cpu == cpu)
4807 return;
4808
4809 svm->last_cpu = cpu;
4810 sd->sev_vmcbs[asid] = svm->vmcb;
4811 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4812 mark_dirty(svm->vmcb, VMCB_ASID);
4813}
4814
Rusty Russelle756fc62007-07-30 20:07:08 +10004815static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004816{
4817 int cpu = raw_smp_processor_id();
4818
Tejun Heo0fe1e002009-10-29 22:34:14 +09004819 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004821 if (sev_guest(svm->vcpu.kvm))
4822 return pre_sev_run(svm, cpu);
4823
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03004824 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09004825 if (svm->asid_generation != sd->asid_generation)
4826 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004827}
4828
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004829static void svm_inject_nmi(struct kvm_vcpu *vcpu)
4830{
4831 struct vcpu_svm *svm = to_svm(vcpu);
4832
4833 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
4834 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004835 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004836 ++vcpu->stat.nmi_injections;
4837}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004838
Eddie Dong85f455f2007-07-06 12:20:49 +03004839static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004840{
4841 struct vmcb_control_area *control;
4842
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004843 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10004844 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03004845 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004846 control->int_ctl &= ~V_INTR_PRIO_MASK;
4847 control->int_ctl |= V_IRQ_MASK |
4848 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004849 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850}
4851
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004852static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03004853{
4854 struct vcpu_svm *svm = to_svm(vcpu);
4855
Joerg Roedel2af91942009-08-07 11:49:28 +02004856 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01004857
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03004858 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
4859 ++vcpu->stat.irq_injections;
4860
Alexander Graf219b65d2009-06-15 15:21:25 +02004861 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
4862 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03004863}
4864
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004865static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
4866{
4867 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
4868}
4869
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004870static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
4871{
4872 struct vcpu_svm *svm = to_svm(vcpu);
4873
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004874 if (svm_nested_virtualize_tpr(vcpu) ||
4875 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004876 return;
4877
Radim Krčmář596f3142014-03-11 19:11:18 +01004878 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
4879
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004880 if (irr == -1)
4881 return;
4882
4883 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004884 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004885}
4886
Yang Zhang8d146952013-01-25 10:18:50 +08004887static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
4888{
4889 return;
4890}
4891
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05004892static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004893{
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05004894 return avic && irqchip_split(vcpu->kvm);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004895}
4896
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004897static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
4898{
4899}
4900
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02004901static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004902{
4903}
4904
4905/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03004906static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4907{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004908 struct vcpu_svm *svm = to_svm(vcpu);
4909 struct vmcb *vmcb = svm->vmcb;
4910
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05004911 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004912 return;
4913
4914 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
4915 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004916}
4917
Andrey Smetanin63086302015-11-10 15:36:32 +03004918static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004919{
4920 return;
4921}
4922
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004923static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
4924{
4925 kvm_lapic_set_irr(vec, vcpu->arch.apic);
4926 smp_mb__after_atomic();
4927
4928 if (avic_vcpu_is_running(vcpu))
4929 wrmsrl(SVM_AVIC_DOORBELL,
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05004930 kvm_cpu_get_apicid(vcpu->cpu));
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004931 else
4932 kvm_vcpu_wake_up(vcpu);
4933}
4934
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004935static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4936{
4937 unsigned long flags;
4938 struct amd_svm_iommu_ir *cur;
4939
4940 spin_lock_irqsave(&svm->ir_list_lock, flags);
4941 list_for_each_entry(cur, &svm->ir_list, node) {
4942 if (cur->data != pi->ir_data)
4943 continue;
4944 list_del(&cur->node);
4945 kfree(cur);
4946 break;
4947 }
4948 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4949}
4950
4951static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4952{
4953 int ret = 0;
4954 unsigned long flags;
4955 struct amd_svm_iommu_ir *ir;
4956
4957 /**
4958 * In some cases, the existing irte is updaed and re-set,
4959 * so we need to check here if it's already been * added
4960 * to the ir_list.
4961 */
4962 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
4963 struct kvm *kvm = svm->vcpu.kvm;
4964 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
4965 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
4966 struct vcpu_svm *prev_svm;
4967
4968 if (!prev_vcpu) {
4969 ret = -EINVAL;
4970 goto out;
4971 }
4972
4973 prev_svm = to_svm(prev_vcpu);
4974 svm_ir_list_del(prev_svm, pi);
4975 }
4976
4977 /**
4978 * Allocating new amd_iommu_pi_data, which will get
4979 * add to the per-vcpu ir_list.
4980 */
4981 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
4982 if (!ir) {
4983 ret = -ENOMEM;
4984 goto out;
4985 }
4986 ir->data = pi->ir_data;
4987
4988 spin_lock_irqsave(&svm->ir_list_lock, flags);
4989 list_add(&ir->node, &svm->ir_list);
4990 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4991out:
4992 return ret;
4993}
4994
4995/**
4996 * Note:
4997 * The HW cannot support posting multicast/broadcast
4998 * interrupts to a vCPU. So, we still use legacy interrupt
4999 * remapping for these kind of interrupts.
5000 *
5001 * For lowest-priority interrupts, we only support
5002 * those with single CPU as the destination, e.g. user
5003 * configures the interrupts via /proc/irq or uses
5004 * irqbalance to make the interrupts single-CPU.
5005 */
5006static int
5007get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
5008 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
5009{
5010 struct kvm_lapic_irq irq;
5011 struct kvm_vcpu *vcpu = NULL;
5012
5013 kvm_set_msi_irq(kvm, e, &irq);
5014
5015 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
5016 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
5017 __func__, irq.vector);
5018 return -1;
5019 }
5020
5021 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
5022 irq.vector);
5023 *svm = to_svm(vcpu);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005024 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005025 vcpu_info->vector = irq.vector;
5026
5027 return 0;
5028}
5029
5030/*
5031 * svm_update_pi_irte - set IRTE for Posted-Interrupts
5032 *
5033 * @kvm: kvm
5034 * @host_irq: host irq of the interrupt
5035 * @guest_irq: gsi of the interrupt
5036 * @set: set or unset PI
5037 * returns 0 on success, < 0 on failure
5038 */
5039static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
5040 uint32_t guest_irq, bool set)
5041{
5042 struct kvm_kernel_irq_routing_entry *e;
5043 struct kvm_irq_routing_table *irq_rt;
5044 int idx, ret = -EINVAL;
5045
5046 if (!kvm_arch_has_assigned_device(kvm) ||
5047 !irq_remapping_cap(IRQ_POSTING_CAP))
5048 return 0;
5049
5050 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
5051 __func__, host_irq, guest_irq, set);
5052
5053 idx = srcu_read_lock(&kvm->irq_srcu);
5054 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
5055 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
5056
5057 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
5058 struct vcpu_data vcpu_info;
5059 struct vcpu_svm *svm = NULL;
5060
5061 if (e->type != KVM_IRQ_ROUTING_MSI)
5062 continue;
5063
5064 /**
5065 * Here, we setup with legacy mode in the following cases:
5066 * 1. When cannot target interrupt to a specific vcpu.
5067 * 2. Unsetting posted interrupt.
5068 * 3. APIC virtialization is disabled for the vcpu.
5069 */
5070 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
5071 kvm_vcpu_apicv_active(&svm->vcpu)) {
5072 struct amd_iommu_pi_data pi;
5073
5074 /* Try to enable guest_mode in IRTE */
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005075 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
5076 AVIC_HPA_MASK);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005077 pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id,
5078 svm->vcpu.vcpu_id);
5079 pi.is_guest_mode = true;
5080 pi.vcpu_data = &vcpu_info;
5081 ret = irq_set_vcpu_affinity(host_irq, &pi);
5082
5083 /**
5084 * Here, we successfully setting up vcpu affinity in
5085 * IOMMU guest mode. Now, we need to store the posted
5086 * interrupt information in a per-vcpu ir_list so that
5087 * we can reference to them directly when we update vcpu
5088 * scheduling information in IOMMU irte.
5089 */
5090 if (!ret && pi.is_guest_mode)
5091 svm_ir_list_add(svm, &pi);
5092 } else {
5093 /* Use legacy mode in IRTE */
5094 struct amd_iommu_pi_data pi;
5095
5096 /**
5097 * Here, pi is used to:
5098 * - Tell IOMMU to use legacy mode for this interrupt.
5099 * - Retrieve ga_tag of prior interrupt remapping data.
5100 */
5101 pi.is_guest_mode = false;
5102 ret = irq_set_vcpu_affinity(host_irq, &pi);
5103
5104 /**
5105 * Check if the posted interrupt was previously
5106 * setup with the guest_mode by checking if the ga_tag
5107 * was cached. If so, we need to clean up the per-vcpu
5108 * ir_list.
5109 */
5110 if (!ret && pi.prev_ga_tag) {
5111 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
5112 struct kvm_vcpu *vcpu;
5113
5114 vcpu = kvm_get_vcpu_by_id(kvm, id);
5115 if (vcpu)
5116 svm_ir_list_del(to_svm(vcpu), &pi);
5117 }
5118 }
5119
5120 if (!ret && svm) {
5121 trace_kvm_pi_irte_update(svm->vcpu.vcpu_id,
5122 host_irq, e->gsi,
5123 vcpu_info.vector,
5124 vcpu_info.pi_desc_addr, set);
5125 }
5126
5127 if (ret < 0) {
5128 pr_err("%s: failed to update PI IRTE\n", __func__);
5129 goto out;
5130 }
5131 }
5132
5133 ret = 0;
5134out:
5135 srcu_read_unlock(&kvm->irq_srcu, idx);
5136 return ret;
5137}
5138
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005139static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005140{
5141 struct vcpu_svm *svm = to_svm(vcpu);
5142 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02005143 int ret;
5144 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
5145 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
5146 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
5147
5148 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005149}
5150
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005151static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
5152{
5153 struct vcpu_svm *svm = to_svm(vcpu);
5154
5155 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
5156}
5157
5158static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5159{
5160 struct vcpu_svm *svm = to_svm(vcpu);
5161
5162 if (masked) {
5163 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005164 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005165 } else {
5166 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005167 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005168 }
5169}
5170
Gleb Natapov78646122009-03-23 12:12:11 +02005171static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
5172{
5173 struct vcpu_svm *svm = to_svm(vcpu);
5174 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005175 int ret;
5176
5177 if (!gif_set(svm) ||
5178 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
5179 return 0;
5180
Avi Kivityf6e78472010-08-02 15:30:20 +03005181 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005182
Joerg Roedel20307532010-11-29 17:51:48 +01005183 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005184 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
5185
5186 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02005187}
5188
Jan Kiszkac9a79532014-03-07 20:03:15 +01005189static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03005190{
Alexander Graf219b65d2009-06-15 15:21:25 +02005191 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02005192
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005193 if (kvm_vcpu_apicv_active(vcpu))
5194 return;
5195
Joerg Roedele0231712010-02-24 18:59:10 +01005196 /*
5197 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
5198 * 1, because that's a separate STGI/VMRUN intercept. The next time we
5199 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005200 * we'll get the vintr intercept. However, if the vGIF feature is
5201 * enabled, the STGI interception will not occur. Enable the irq
5202 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01005203 */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005204 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02005205 svm_set_vintr(svm);
5206 svm_inject_irq(svm, 0x0);
5207 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005208}
5209
Jan Kiszkac9a79532014-03-07 20:03:15 +01005210static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005211{
Avi Kivity04d2cc72007-09-10 18:10:54 +03005212 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005213
Gleb Natapov44c11432009-05-11 13:35:52 +03005214 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
5215 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01005216 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03005217
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005218 if (!gif_set(svm)) {
5219 if (vgif_enabled(svm))
5220 set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005221 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005222 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005223
5224 if (svm->nested.exit_required)
5225 return; /* we're not going to run the guest yet */
5226
Joerg Roedele0231712010-02-24 18:59:10 +01005227 /*
5228 * Something prevents NMI from been injected. Single step over possible
5229 * problem (IRET or exception injection or interrupt shadow)
5230 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02005231 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02005232 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03005233 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03005234}
5235
Izik Eiduscbc94022007-10-25 00:29:55 +02005236static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
5237{
5238 return 0;
5239}
5240
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005241static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Avi Kivityd9e368d2007-06-07 19:18:30 +03005242{
Joerg Roedel38e5e922010-12-03 15:25:16 +01005243 struct vcpu_svm *svm = to_svm(vcpu);
5244
5245 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
5246 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5247 else
5248 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03005249}
5250
Avi Kivity04d2cc72007-09-10 18:10:54 +03005251static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
5252{
5253}
5254
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005255static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
5256{
5257 struct vcpu_svm *svm = to_svm(vcpu);
5258
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005259 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005260 return;
5261
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005262 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005263 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03005264 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005265 }
5266}
5267
Joerg Roedel649d6862008-04-16 16:51:15 +02005268static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
5269{
5270 struct vcpu_svm *svm = to_svm(vcpu);
5271 u64 cr8;
5272
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005273 if (svm_nested_virtualize_tpr(vcpu) ||
5274 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005275 return;
5276
Joerg Roedel649d6862008-04-16 16:51:15 +02005277 cr8 = kvm_get_cr8(vcpu);
5278 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
5279 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
5280}
5281
Gleb Natapov9222be12009-04-23 17:14:37 +03005282static void svm_complete_interrupts(struct vcpu_svm *svm)
5283{
5284 u8 vector;
5285 int type;
5286 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01005287 unsigned int3_injected = svm->int3_injected;
5288
5289 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005290
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02005291 /*
5292 * If we've made progress since setting HF_IRET_MASK, we've
5293 * executed an IRET and can allow NMI injection.
5294 */
5295 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
5296 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03005297 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03005298 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5299 }
Gleb Natapov44c11432009-05-11 13:35:52 +03005300
Gleb Natapov9222be12009-04-23 17:14:37 +03005301 svm->vcpu.arch.nmi_injected = false;
5302 kvm_clear_exception_queue(&svm->vcpu);
5303 kvm_clear_interrupt_queue(&svm->vcpu);
5304
5305 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
5306 return;
5307
Avi Kivity3842d132010-07-27 12:30:24 +03005308 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5309
Gleb Natapov9222be12009-04-23 17:14:37 +03005310 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
5311 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
5312
5313 switch (type) {
5314 case SVM_EXITINTINFO_TYPE_NMI:
5315 svm->vcpu.arch.nmi_injected = true;
5316 break;
5317 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01005318 /*
5319 * In case of software exceptions, do not reinject the vector,
5320 * but re-execute the instruction instead. Rewind RIP first
5321 * if we emulated INT3 before.
5322 */
5323 if (kvm_exception_is_soft(vector)) {
5324 if (vector == BP_VECTOR && int3_injected &&
5325 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
5326 kvm_rip_write(&svm->vcpu,
5327 kvm_rip_read(&svm->vcpu) -
5328 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02005329 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01005330 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005331 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
5332 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005333 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03005334
5335 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005336 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03005337 break;
5338 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005339 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03005340 break;
5341 default:
5342 break;
5343 }
5344}
5345
Avi Kivityb463a6f2010-07-20 15:06:17 +03005346static void svm_cancel_injection(struct kvm_vcpu *vcpu)
5347{
5348 struct vcpu_svm *svm = to_svm(vcpu);
5349 struct vmcb_control_area *control = &svm->vmcb->control;
5350
5351 control->exit_int_info = control->event_inj;
5352 control->exit_int_info_err = control->event_inj_err;
5353 control->event_inj = 0;
5354 svm_complete_interrupts(svm);
5355}
5356
Avi Kivity851ba692009-08-24 11:10:17 +03005357static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005358{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005359 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03005360
Joerg Roedel2041a062010-04-22 12:33:08 +02005361 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5362 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5363 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5364
Joerg Roedelcd3ff652009-10-09 16:08:26 +02005365 /*
5366 * A vmexit emulation is required before the vcpu can be executed
5367 * again.
5368 */
5369 if (unlikely(svm->nested.exit_required))
5370 return;
5371
Ladi Proseka12713c2017-06-21 09:07:00 +02005372 /*
5373 * Disable singlestep if we're injecting an interrupt/exception.
5374 * We don't want our modified rflags to be pushed on the stack where
5375 * we might not be able to easily reset them if we disabled NMI
5376 * singlestep later.
5377 */
5378 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5379 /*
5380 * Event injection happens before external interrupts cause a
5381 * vmexit and interrupts are disabled here, so smp_send_reschedule
5382 * is enough to force an immediate vmexit.
5383 */
5384 disable_nmi_singlestep(svm);
5385 smp_send_reschedule(vcpu->cpu);
5386 }
5387
Rusty Russelle756fc62007-07-30 20:07:08 +10005388 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005389
Joerg Roedel649d6862008-04-16 16:51:15 +02005390 sync_lapic_to_cr8(vcpu);
5391
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02005392 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005393
Avi Kivity04d2cc72007-09-10 18:10:54 +03005394 clgi();
5395
5396 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08005397
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005398 /*
5399 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
5400 * it's non-zero. Since vmentry is serialising on affected CPUs, there
5401 * is no need to worry about the conditional branch over the wrmsr
5402 * being speculatively taken.
5403 */
5404 if (svm->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005405 native_wrmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005406
Avi Kivity6aa8b732006-12-10 02:21:36 -08005407 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03005408 "push %%" _ASM_BP "; \n\t"
5409 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5410 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5411 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5412 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5413 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5414 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005415#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005416 "mov %c[r8](%[svm]), %%r8 \n\t"
5417 "mov %c[r9](%[svm]), %%r9 \n\t"
5418 "mov %c[r10](%[svm]), %%r10 \n\t"
5419 "mov %c[r11](%[svm]), %%r11 \n\t"
5420 "mov %c[r12](%[svm]), %%r12 \n\t"
5421 "mov %c[r13](%[svm]), %%r13 \n\t"
5422 "mov %c[r14](%[svm]), %%r14 \n\t"
5423 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005424#endif
5425
Avi Kivity6aa8b732006-12-10 02:21:36 -08005426 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03005427 "push %%" _ASM_AX " \n\t"
5428 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03005429 __ex(SVM_VMLOAD) "\n\t"
5430 __ex(SVM_VMRUN) "\n\t"
5431 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005432 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005433
5434 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03005435 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5436 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5437 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5438 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5439 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5440 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005441#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005442 "mov %%r8, %c[r8](%[svm]) \n\t"
5443 "mov %%r9, %c[r9](%[svm]) \n\t"
5444 "mov %%r10, %c[r10](%[svm]) \n\t"
5445 "mov %%r11, %c[r11](%[svm]) \n\t"
5446 "mov %%r12, %c[r12](%[svm]) \n\t"
5447 "mov %%r13, %c[r13](%[svm]) \n\t"
5448 "mov %%r14, %c[r14](%[svm]) \n\t"
5449 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005450#endif
Jim Mattson0cb5b302018-01-03 14:31:38 -08005451 /*
5452 * Clear host registers marked as clobbered to prevent
5453 * speculative use.
5454 */
5455 "xor %%" _ASM_BX ", %%" _ASM_BX " \n\t"
5456 "xor %%" _ASM_CX ", %%" _ASM_CX " \n\t"
5457 "xor %%" _ASM_DX ", %%" _ASM_DX " \n\t"
5458 "xor %%" _ASM_SI ", %%" _ASM_SI " \n\t"
5459 "xor %%" _ASM_DI ", %%" _ASM_DI " \n\t"
5460#ifdef CONFIG_X86_64
5461 "xor %%r8, %%r8 \n\t"
5462 "xor %%r9, %%r9 \n\t"
5463 "xor %%r10, %%r10 \n\t"
5464 "xor %%r11, %%r11 \n\t"
5465 "xor %%r12, %%r12 \n\t"
5466 "xor %%r13, %%r13 \n\t"
5467 "xor %%r14, %%r14 \n\t"
5468 "xor %%r15, %%r15 \n\t"
5469#endif
Avi Kivity74547662012-09-16 15:10:59 +03005470 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08005471 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005472 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08005473 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005474 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5475 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5476 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5477 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5478 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5479 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005480#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005481 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5482 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5483 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5484 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5485 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5486 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5487 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5488 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005489#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02005490 : "cc", "memory"
5491#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03005492 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005493 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03005494#else
5495 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005496#endif
5497 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08005498
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005499 /*
5500 * We do not use IBRS in the kernel. If this vCPU has used the
5501 * SPEC_CTRL MSR it may have left it on; save the value and
5502 * turn it off. This is much more efficient than blindly adding
5503 * it to the atomic save/restore list. Especially as the former
5504 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
5505 *
5506 * For non-nested case:
5507 * If the L01 MSR bitmap does not intercept the MSR, then we need to
5508 * save it.
5509 *
5510 * For nested case:
5511 * If the L02 MSR bitmap does not intercept the MSR, then we need to
5512 * save it.
5513 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01005514 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005515 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005516
5517 if (svm->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005518 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005519
David Woodhouse117cc7a2018-01-12 11:11:27 +00005520 /* Eliminate branch target predictions from guest mode */
5521 vmexit_fill_RSB();
5522
Avi Kivity82ca2d12010-10-21 12:20:34 +02005523#ifdef CONFIG_X86_64
5524 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5525#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02005526 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02005527#ifndef CONFIG_X86_32_LAZY_GS
5528 loadsegment(gs, svm->host.gs);
5529#endif
Avi Kivity9581d442010-10-19 16:46:55 +02005530#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005531
5532 reload_tss(vcpu);
5533
Avi Kivity56ba47d2007-11-07 17:14:18 +02005534 local_irq_disable();
5535
Avi Kivity13c34e02010-10-21 12:20:31 +02005536 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5537 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5538 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5539 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5540
Joerg Roedel3781c012011-01-14 16:45:02 +01005541 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5542 kvm_before_handle_nmi(&svm->vcpu);
5543
5544 stgi();
5545
5546 /* Any pending NMI will happen here */
5547
5548 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5549 kvm_after_handle_nmi(&svm->vcpu);
5550
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005551 sync_cr8_to_lapic(vcpu);
5552
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005553 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005554
Joerg Roedel38e5e922010-12-03 15:25:16 +01005555 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5556
Gleb Natapov631bc482010-10-14 11:22:52 +02005557 /* if exit due to PF check for async PF */
5558 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005559 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
Gleb Natapov631bc482010-10-14 11:22:52 +02005560
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005561 if (npt_enabled) {
5562 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5563 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5564 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02005565
5566 /*
5567 * We need to handle MC intercepts here before the vcpu has a chance to
5568 * change the physical cpu
5569 */
5570 if (unlikely(svm->vmcb->control.exit_code ==
5571 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5572 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01005573
5574 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005575}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005576STACK_FRAME_NON_STANDARD(svm_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005577
Avi Kivity6aa8b732006-12-10 02:21:36 -08005578static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5579{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005580 struct vcpu_svm *svm = to_svm(vcpu);
5581
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005582 svm->vmcb->save.cr3 = __sme_set(root);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005583 mark_dirty(svm->vmcb, VMCB_CR);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005584 svm_flush_tlb(vcpu, true);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005585}
5586
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005587static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5588{
5589 struct vcpu_svm *svm = to_svm(vcpu);
5590
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005591 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01005592 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005593
5594 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02005595 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005596 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005597
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005598 svm_flush_tlb(vcpu, true);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005599}
5600
Avi Kivity6aa8b732006-12-10 02:21:36 -08005601static int is_disabled(void)
5602{
Joerg Roedel6031a612007-06-22 12:29:50 +03005603 u64 vm_cr;
5604
5605 rdmsrl(MSR_VM_CR, vm_cr);
5606 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5607 return 1;
5608
Avi Kivity6aa8b732006-12-10 02:21:36 -08005609 return 0;
5610}
5611
Ingo Molnar102d8322007-02-19 14:37:47 +02005612static void
5613svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5614{
5615 /*
5616 * Patch in the VMMCALL instruction:
5617 */
5618 hypercall[0] = 0x0f;
5619 hypercall[1] = 0x01;
5620 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02005621}
5622
Yang, Sheng002c7f72007-07-31 14:23:01 +03005623static void svm_check_processor_compat(void *rtn)
5624{
5625 *(int *)rtn = 0;
5626}
5627
Avi Kivity774ead32007-12-26 13:57:04 +02005628static bool svm_cpu_has_accelerated_tpr(void)
5629{
5630 return false;
5631}
5632
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005633static bool svm_has_high_real_mode_segbase(void)
5634{
5635 return true;
5636}
5637
Paolo Bonzinifc07e762015-10-01 13:20:22 +02005638static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5639{
5640 return 0;
5641}
5642
Sheng Yang0e851882009-12-18 16:48:46 +08005643static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5644{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005645 struct vcpu_svm *svm = to_svm(vcpu);
5646
5647 /* Update nrips enabled cache */
Radim Krčmářd6321d42017-08-05 00:12:49 +02005648 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005649
5650 if (!kvm_vcpu_apicv_active(vcpu))
5651 return;
5652
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02005653 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08005654}
5655
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005656static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5657{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005658 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005659 case 0x1:
5660 if (avic)
5661 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5662 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02005663 case 0x80000001:
5664 if (nested)
5665 entry->ecx |= (1 << 2); /* Set SVM bit */
5666 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005667 case 0x8000000A:
5668 entry->eax = 1; /* SVM revision 1 */
5669 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5670 ASID emulation to nested SVM */
5671 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02005672 entry->edx = 0; /* Per default do not support any
5673 additional features */
5674
5675 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02005676 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02005677 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005678
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005679 /* Support NPT for the guest if enabled */
5680 if (npt_enabled)
5681 entry->edx |= SVM_FEATURE_NPT;
5682
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005683 break;
Brijesh Singh8765d752017-12-04 10:57:25 -06005684 case 0x8000001F:
5685 /* Support memory encryption cpuid if host supports it */
5686 if (boot_cpu_has(X86_FEATURE_SEV))
5687 cpuid(0x8000001f, &entry->eax, &entry->ebx,
5688 &entry->ecx, &entry->edx);
5689
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005690 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005691}
5692
Sheng Yang17cc3932010-01-05 19:02:27 +08005693static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005694{
Sheng Yang17cc3932010-01-05 19:02:27 +08005695 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005696}
5697
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005698static bool svm_rdtscp_supported(void)
5699{
Paolo Bonzini46896c72015-11-12 14:49:16 +01005700 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005701}
5702
Mao, Junjiead756a12012-07-02 01:18:48 +00005703static bool svm_invpcid_supported(void)
5704{
5705 return false;
5706}
5707
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005708static bool svm_mpx_supported(void)
5709{
5710 return false;
5711}
5712
Wanpeng Li55412b22014-12-02 19:21:30 +08005713static bool svm_xsaves_supported(void)
5714{
5715 return false;
5716}
5717
Paolo Bonzini66336ca2016-07-12 10:36:41 +02005718static bool svm_umip_emulated(void)
5719{
5720 return false;
5721}
5722
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005723static bool svm_has_wbinvd_exit(void)
5724{
5725 return true;
5726}
5727
Joerg Roedel80612522011-04-04 12:39:33 +02005728#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005729 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005730#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005731 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005732#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005733 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005734
Mathias Krause09941fb2012-08-30 01:30:20 +02005735static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005736 u32 exit_code;
5737 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005738} x86_intercept_map[] = {
5739 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
5740 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
5741 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
5742 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
5743 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02005744 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
5745 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02005746 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
5747 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
5748 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
5749 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
5750 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
5751 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
5752 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
5753 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02005754 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
5755 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
5756 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
5757 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
5758 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
5759 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
5760 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
5761 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005762 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
5763 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
5764 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02005765 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
5766 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
5767 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
5768 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
5769 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
5770 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
5771 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
5772 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
5773 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02005774 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
5775 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
5776 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
5777 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
5778 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
5779 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
5780 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02005781 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
5782 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
5783 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
5784 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005785};
5786
Joerg Roedel80612522011-04-04 12:39:33 +02005787#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005788#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005789#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005790
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005791static int svm_check_intercept(struct kvm_vcpu *vcpu,
5792 struct x86_instruction_info *info,
5793 enum x86_intercept_stage stage)
5794{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005795 struct vcpu_svm *svm = to_svm(vcpu);
5796 int vmexit, ret = X86EMUL_CONTINUE;
5797 struct __x86_intercept icpt_info;
5798 struct vmcb *vmcb = svm->vmcb;
5799
5800 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
5801 goto out;
5802
5803 icpt_info = x86_intercept_map[info->intercept];
5804
Avi Kivity40e19b52011-04-21 12:35:41 +03005805 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005806 goto out;
5807
5808 switch (icpt_info.exit_code) {
5809 case SVM_EXIT_READ_CR0:
5810 if (info->intercept == x86_intercept_cr_read)
5811 icpt_info.exit_code += info->modrm_reg;
5812 break;
5813 case SVM_EXIT_WRITE_CR0: {
5814 unsigned long cr0, val;
5815 u64 intercept;
5816
5817 if (info->intercept == x86_intercept_cr_write)
5818 icpt_info.exit_code += info->modrm_reg;
5819
Jan Kiszka62baf442014-06-29 21:55:53 +02005820 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
5821 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005822 break;
5823
5824 intercept = svm->nested.intercept;
5825
5826 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
5827 break;
5828
5829 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
5830 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
5831
5832 if (info->intercept == x86_intercept_lmsw) {
5833 cr0 &= 0xfUL;
5834 val &= 0xfUL;
5835 /* lmsw can't clear PE - catch this here */
5836 if (cr0 & X86_CR0_PE)
5837 val |= X86_CR0_PE;
5838 }
5839
5840 if (cr0 ^ val)
5841 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
5842
5843 break;
5844 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02005845 case SVM_EXIT_READ_DR0:
5846 case SVM_EXIT_WRITE_DR0:
5847 icpt_info.exit_code += info->modrm_reg;
5848 break;
Joerg Roedel80612522011-04-04 12:39:33 +02005849 case SVM_EXIT_MSR:
5850 if (info->intercept == x86_intercept_wrmsr)
5851 vmcb->control.exit_info_1 = 1;
5852 else
5853 vmcb->control.exit_info_1 = 0;
5854 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02005855 case SVM_EXIT_PAUSE:
5856 /*
5857 * We get this for NOP only, but pause
5858 * is rep not, check this here
5859 */
5860 if (info->rep_prefix != REPE_PREFIX)
5861 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02005862 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02005863 case SVM_EXIT_IOIO: {
5864 u64 exit_info;
5865 u32 bytes;
5866
Joerg Roedelf6511932011-04-04 12:39:35 +02005867 if (info->intercept == x86_intercept_in ||
5868 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005869 exit_info = ((info->src_val & 0xffff) << 16) |
5870 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02005871 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02005872 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005873 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02005874 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02005875 }
5876
5877 if (info->intercept == x86_intercept_outs ||
5878 info->intercept == x86_intercept_ins)
5879 exit_info |= SVM_IOIO_STR_MASK;
5880
5881 if (info->rep_prefix)
5882 exit_info |= SVM_IOIO_REP_MASK;
5883
5884 bytes = min(bytes, 4u);
5885
5886 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
5887
5888 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
5889
5890 vmcb->control.exit_info_1 = exit_info;
5891 vmcb->control.exit_info_2 = info->next_rip;
5892
5893 break;
5894 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005895 default:
5896 break;
5897 }
5898
Bandan Dasf1047652015-06-11 02:05:33 -04005899 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
5900 if (static_cpu_has(X86_FEATURE_NRIPS))
5901 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005902 vmcb->control.exit_code = icpt_info.exit_code;
5903 vmexit = nested_svm_exit_handled(svm);
5904
5905 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
5906 : X86EMUL_CONTINUE;
5907
5908out:
5909 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005910}
5911
Yang Zhanga547c6d2013-04-11 19:25:10 +08005912static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
5913{
5914 local_irq_enable();
Paolo Bonzinif2485b32016-06-15 15:23:11 +02005915 /*
5916 * We must have an instruction with interrupts enabled, so
5917 * the timer interrupt isn't delayed by the interrupt shadow.
5918 */
5919 asm("nop");
5920 local_irq_disable();
Yang Zhanga547c6d2013-04-11 19:25:10 +08005921}
5922
Radim Krčmářae97a3b2014-08-21 18:08:06 +02005923static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
5924{
5925}
5926
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05005927static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
5928{
5929 if (avic_handle_apic_id_update(vcpu) != 0)
5930 return;
5931 if (avic_handle_dfr_update(vcpu) != 0)
5932 return;
5933 avic_handle_ldr_update(vcpu);
5934}
5935
Borislav Petkov74f16902017-03-26 23:51:24 +02005936static void svm_setup_mce(struct kvm_vcpu *vcpu)
5937{
5938 /* [63:9] are reserved. */
5939 vcpu->arch.mcg_cap &= 0x1ff;
5940}
5941
Ladi Prosek72d7b372017-10-11 16:54:41 +02005942static int svm_smi_allowed(struct kvm_vcpu *vcpu)
5943{
Ladi Prosek05cade72017-10-11 16:54:45 +02005944 struct vcpu_svm *svm = to_svm(vcpu);
5945
5946 /* Per APM Vol.2 15.22.2 "Response to SMI" */
5947 if (!gif_set(svm))
5948 return 0;
5949
5950 if (is_guest_mode(&svm->vcpu) &&
5951 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
5952 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
5953 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
5954 svm->nested.exit_required = true;
5955 return 0;
5956 }
5957
Ladi Prosek72d7b372017-10-11 16:54:41 +02005958 return 1;
5959}
5960
Ladi Prosek0234bf82017-10-11 16:54:40 +02005961static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
5962{
Ladi Prosek05cade72017-10-11 16:54:45 +02005963 struct vcpu_svm *svm = to_svm(vcpu);
5964 int ret;
5965
5966 if (is_guest_mode(vcpu)) {
5967 /* FED8h - SVM Guest */
5968 put_smstate(u64, smstate, 0x7ed8, 1);
5969 /* FEE0h - SVM Guest VMCB Physical Address */
5970 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
5971
5972 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5973 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5974 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5975
5976 ret = nested_svm_vmexit(svm);
5977 if (ret)
5978 return ret;
5979 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02005980 return 0;
5981}
5982
5983static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
5984{
Ladi Prosek05cade72017-10-11 16:54:45 +02005985 struct vcpu_svm *svm = to_svm(vcpu);
5986 struct vmcb *nested_vmcb;
5987 struct page *page;
5988 struct {
5989 u64 guest;
5990 u64 vmcb;
5991 } svm_state_save;
5992 int ret;
5993
5994 ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save,
5995 sizeof(svm_state_save));
5996 if (ret)
5997 return ret;
5998
5999 if (svm_state_save.guest) {
6000 vcpu->arch.hflags &= ~HF_SMM_MASK;
6001 nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page);
6002 if (nested_vmcb)
6003 enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page);
6004 else
6005 ret = 1;
6006 vcpu->arch.hflags |= HF_SMM_MASK;
6007 }
6008 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02006009}
6010
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006011static int enable_smi_window(struct kvm_vcpu *vcpu)
6012{
6013 struct vcpu_svm *svm = to_svm(vcpu);
6014
6015 if (!gif_set(svm)) {
6016 if (vgif_enabled(svm))
6017 set_intercept(svm, INTERCEPT_STGI);
6018 /* STGI will cause a vm exit */
6019 return 1;
6020 }
6021 return 0;
6022}
6023
Brijesh Singh1654efc2017-12-04 10:57:34 -06006024static int sev_asid_new(void)
6025{
6026 int pos;
6027
6028 /*
6029 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
6030 */
6031 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
6032 if (pos >= max_sev_asid)
6033 return -EBUSY;
6034
6035 set_bit(pos, sev_asid_bitmap);
6036 return pos + 1;
6037}
6038
6039static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
6040{
6041 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6042 int asid, ret;
6043
6044 ret = -EBUSY;
6045 asid = sev_asid_new();
6046 if (asid < 0)
6047 return ret;
6048
6049 ret = sev_platform_init(&argp->error);
6050 if (ret)
6051 goto e_free;
6052
6053 sev->active = true;
6054 sev->asid = asid;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006055 INIT_LIST_HEAD(&sev->regions_list);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006056
6057 return 0;
6058
6059e_free:
6060 __sev_asid_free(asid);
6061 return ret;
6062}
6063
Brijesh Singh59414c92017-12-04 10:57:35 -06006064static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
6065{
6066 struct sev_data_activate *data;
6067 int asid = sev_get_asid(kvm);
6068 int ret;
6069
6070 wbinvd_on_all_cpus();
6071
6072 ret = sev_guest_df_flush(error);
6073 if (ret)
6074 return ret;
6075
6076 data = kzalloc(sizeof(*data), GFP_KERNEL);
6077 if (!data)
6078 return -ENOMEM;
6079
6080 /* activate ASID on the given handle */
6081 data->handle = handle;
6082 data->asid = asid;
6083 ret = sev_guest_activate(data, error);
6084 kfree(data);
6085
6086 return ret;
6087}
6088
Brijesh Singh89c50582017-12-04 10:57:35 -06006089static int __sev_issue_cmd(int fd, int id, void *data, int *error)
Brijesh Singh59414c92017-12-04 10:57:35 -06006090{
6091 struct fd f;
6092 int ret;
6093
6094 f = fdget(fd);
6095 if (!f.file)
6096 return -EBADF;
6097
6098 ret = sev_issue_cmd_external_user(f.file, id, data, error);
6099
6100 fdput(f);
6101 return ret;
6102}
6103
Brijesh Singh89c50582017-12-04 10:57:35 -06006104static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
6105{
6106 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6107
6108 return __sev_issue_cmd(sev->fd, id, data, error);
6109}
6110
Brijesh Singh59414c92017-12-04 10:57:35 -06006111static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
6112{
6113 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6114 struct sev_data_launch_start *start;
6115 struct kvm_sev_launch_start params;
6116 void *dh_blob, *session_blob;
6117 int *error = &argp->error;
6118 int ret;
6119
6120 if (!sev_guest(kvm))
6121 return -ENOTTY;
6122
6123 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6124 return -EFAULT;
6125
6126 start = kzalloc(sizeof(*start), GFP_KERNEL);
6127 if (!start)
6128 return -ENOMEM;
6129
6130 dh_blob = NULL;
6131 if (params.dh_uaddr) {
6132 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
6133 if (IS_ERR(dh_blob)) {
6134 ret = PTR_ERR(dh_blob);
6135 goto e_free;
6136 }
6137
6138 start->dh_cert_address = __sme_set(__pa(dh_blob));
6139 start->dh_cert_len = params.dh_len;
6140 }
6141
6142 session_blob = NULL;
6143 if (params.session_uaddr) {
6144 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
6145 if (IS_ERR(session_blob)) {
6146 ret = PTR_ERR(session_blob);
6147 goto e_free_dh;
6148 }
6149
6150 start->session_address = __sme_set(__pa(session_blob));
6151 start->session_len = params.session_len;
6152 }
6153
6154 start->handle = params.handle;
6155 start->policy = params.policy;
6156
6157 /* create memory encryption context */
Brijesh Singh89c50582017-12-04 10:57:35 -06006158 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
Brijesh Singh59414c92017-12-04 10:57:35 -06006159 if (ret)
6160 goto e_free_session;
6161
6162 /* Bind ASID to this guest */
6163 ret = sev_bind_asid(kvm, start->handle, error);
6164 if (ret)
6165 goto e_free_session;
6166
6167 /* return handle to userspace */
6168 params.handle = start->handle;
6169 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
6170 sev_unbind_asid(kvm, start->handle);
6171 ret = -EFAULT;
6172 goto e_free_session;
6173 }
6174
6175 sev->handle = start->handle;
6176 sev->fd = argp->sev_fd;
6177
6178e_free_session:
6179 kfree(session_blob);
6180e_free_dh:
6181 kfree(dh_blob);
6182e_free:
6183 kfree(start);
6184 return ret;
6185}
6186
Brijesh Singh89c50582017-12-04 10:57:35 -06006187static int get_num_contig_pages(int idx, struct page **inpages,
6188 unsigned long npages)
6189{
6190 unsigned long paddr, next_paddr;
6191 int i = idx + 1, pages = 1;
6192
6193 /* find the number of contiguous pages starting from idx */
6194 paddr = __sme_page_pa(inpages[idx]);
6195 while (i < npages) {
6196 next_paddr = __sme_page_pa(inpages[i++]);
6197 if ((paddr + PAGE_SIZE) == next_paddr) {
6198 pages++;
6199 paddr = next_paddr;
6200 continue;
6201 }
6202 break;
6203 }
6204
6205 return pages;
6206}
6207
6208static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
6209{
6210 unsigned long vaddr, vaddr_end, next_vaddr, npages, size;
6211 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6212 struct kvm_sev_launch_update_data params;
6213 struct sev_data_launch_update_data *data;
6214 struct page **inpages;
6215 int i, ret, pages;
6216
6217 if (!sev_guest(kvm))
6218 return -ENOTTY;
6219
6220 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6221 return -EFAULT;
6222
6223 data = kzalloc(sizeof(*data), GFP_KERNEL);
6224 if (!data)
6225 return -ENOMEM;
6226
6227 vaddr = params.uaddr;
6228 size = params.len;
6229 vaddr_end = vaddr + size;
6230
6231 /* Lock the user memory. */
6232 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
6233 if (!inpages) {
6234 ret = -ENOMEM;
6235 goto e_free;
6236 }
6237
6238 /*
6239 * The LAUNCH_UPDATE command will perform in-place encryption of the
6240 * memory content (i.e it will write the same memory region with C=1).
6241 * It's possible that the cache may contain the data with C=0, i.e.,
6242 * unencrypted so invalidate it first.
6243 */
6244 sev_clflush_pages(inpages, npages);
6245
6246 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
6247 int offset, len;
6248
6249 /*
6250 * If the user buffer is not page-aligned, calculate the offset
6251 * within the page.
6252 */
6253 offset = vaddr & (PAGE_SIZE - 1);
6254
6255 /* Calculate the number of pages that can be encrypted in one go. */
6256 pages = get_num_contig_pages(i, inpages, npages);
6257
6258 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
6259
6260 data->handle = sev->handle;
6261 data->len = len;
6262 data->address = __sme_page_pa(inpages[i]) + offset;
6263 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
6264 if (ret)
6265 goto e_unpin;
6266
6267 size -= len;
6268 next_vaddr = vaddr + len;
6269 }
6270
6271e_unpin:
6272 /* content of memory is updated, mark pages dirty */
6273 for (i = 0; i < npages; i++) {
6274 set_page_dirty_lock(inpages[i]);
6275 mark_page_accessed(inpages[i]);
6276 }
6277 /* unlock the user pages */
6278 sev_unpin_memory(kvm, inpages, npages);
6279e_free:
6280 kfree(data);
6281 return ret;
6282}
6283
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006284static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
6285{
Brijesh Singh3e233382018-02-23 12:36:50 -06006286 void __user *measure = (void __user *)(uintptr_t)argp->data;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006287 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6288 struct sev_data_launch_measure *data;
6289 struct kvm_sev_launch_measure params;
Brijesh Singh3e233382018-02-23 12:36:50 -06006290 void __user *p = NULL;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006291 void *blob = NULL;
6292 int ret;
6293
6294 if (!sev_guest(kvm))
6295 return -ENOTTY;
6296
Brijesh Singh3e233382018-02-23 12:36:50 -06006297 if (copy_from_user(&params, measure, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006298 return -EFAULT;
6299
6300 data = kzalloc(sizeof(*data), GFP_KERNEL);
6301 if (!data)
6302 return -ENOMEM;
6303
6304 /* User wants to query the blob length */
6305 if (!params.len)
6306 goto cmd;
6307
Brijesh Singh3e233382018-02-23 12:36:50 -06006308 p = (void __user *)(uintptr_t)params.uaddr;
6309 if (p) {
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006310 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
6311 ret = -EINVAL;
6312 goto e_free;
6313 }
6314
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006315 ret = -ENOMEM;
6316 blob = kmalloc(params.len, GFP_KERNEL);
6317 if (!blob)
6318 goto e_free;
6319
6320 data->address = __psp_pa(blob);
6321 data->len = params.len;
6322 }
6323
6324cmd:
6325 data->handle = sev->handle;
6326 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
6327
6328 /*
6329 * If we query the session length, FW responded with expected data.
6330 */
6331 if (!params.len)
6332 goto done;
6333
6334 if (ret)
6335 goto e_free_blob;
6336
6337 if (blob) {
Brijesh Singh3e233382018-02-23 12:36:50 -06006338 if (copy_to_user(p, blob, params.len))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006339 ret = -EFAULT;
6340 }
6341
6342done:
6343 params.len = data->len;
Brijesh Singh3e233382018-02-23 12:36:50 -06006344 if (copy_to_user(measure, &params, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006345 ret = -EFAULT;
6346e_free_blob:
6347 kfree(blob);
6348e_free:
6349 kfree(data);
6350 return ret;
6351}
6352
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006353static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
6354{
6355 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6356 struct sev_data_launch_finish *data;
6357 int ret;
6358
6359 if (!sev_guest(kvm))
6360 return -ENOTTY;
6361
6362 data = kzalloc(sizeof(*data), GFP_KERNEL);
6363 if (!data)
6364 return -ENOMEM;
6365
6366 data->handle = sev->handle;
6367 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
6368
6369 kfree(data);
6370 return ret;
6371}
6372
Brijesh Singh255d9e72017-12-04 10:57:37 -06006373static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
6374{
6375 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6376 struct kvm_sev_guest_status params;
6377 struct sev_data_guest_status *data;
6378 int ret;
6379
6380 if (!sev_guest(kvm))
6381 return -ENOTTY;
6382
6383 data = kzalloc(sizeof(*data), GFP_KERNEL);
6384 if (!data)
6385 return -ENOMEM;
6386
6387 data->handle = sev->handle;
6388 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
6389 if (ret)
6390 goto e_free;
6391
6392 params.policy = data->policy;
6393 params.state = data->state;
6394 params.handle = data->handle;
6395
6396 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6397 ret = -EFAULT;
6398e_free:
6399 kfree(data);
6400 return ret;
6401}
6402
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006403static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
6404 unsigned long dst, int size,
6405 int *error, bool enc)
6406{
6407 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6408 struct sev_data_dbg *data;
6409 int ret;
6410
6411 data = kzalloc(sizeof(*data), GFP_KERNEL);
6412 if (!data)
6413 return -ENOMEM;
6414
6415 data->handle = sev->handle;
6416 data->dst_addr = dst;
6417 data->src_addr = src;
6418 data->len = size;
6419
6420 ret = sev_issue_cmd(kvm,
6421 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
6422 data, error);
6423 kfree(data);
6424 return ret;
6425}
6426
6427static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
6428 unsigned long dst_paddr, int sz, int *err)
6429{
6430 int offset;
6431
6432 /*
6433 * Its safe to read more than we are asked, caller should ensure that
6434 * destination has enough space.
6435 */
6436 src_paddr = round_down(src_paddr, 16);
6437 offset = src_paddr & 15;
6438 sz = round_up(sz + offset, 16);
6439
6440 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
6441}
6442
6443static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
6444 unsigned long __user dst_uaddr,
6445 unsigned long dst_paddr,
6446 int size, int *err)
6447{
6448 struct page *tpage = NULL;
6449 int ret, offset;
6450
6451 /* if inputs are not 16-byte then use intermediate buffer */
6452 if (!IS_ALIGNED(dst_paddr, 16) ||
6453 !IS_ALIGNED(paddr, 16) ||
6454 !IS_ALIGNED(size, 16)) {
6455 tpage = (void *)alloc_page(GFP_KERNEL);
6456 if (!tpage)
6457 return -ENOMEM;
6458
6459 dst_paddr = __sme_page_pa(tpage);
6460 }
6461
6462 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
6463 if (ret)
6464 goto e_free;
6465
6466 if (tpage) {
6467 offset = paddr & 15;
6468 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
6469 page_address(tpage) + offset, size))
6470 ret = -EFAULT;
6471 }
6472
6473e_free:
6474 if (tpage)
6475 __free_page(tpage);
6476
6477 return ret;
6478}
6479
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006480static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
6481 unsigned long __user vaddr,
6482 unsigned long dst_paddr,
6483 unsigned long __user dst_vaddr,
6484 int size, int *error)
6485{
6486 struct page *src_tpage = NULL;
6487 struct page *dst_tpage = NULL;
6488 int ret, len = size;
6489
6490 /* If source buffer is not aligned then use an intermediate buffer */
6491 if (!IS_ALIGNED(vaddr, 16)) {
6492 src_tpage = alloc_page(GFP_KERNEL);
6493 if (!src_tpage)
6494 return -ENOMEM;
6495
6496 if (copy_from_user(page_address(src_tpage),
6497 (void __user *)(uintptr_t)vaddr, size)) {
6498 __free_page(src_tpage);
6499 return -EFAULT;
6500 }
6501
6502 paddr = __sme_page_pa(src_tpage);
6503 }
6504
6505 /*
6506 * If destination buffer or length is not aligned then do read-modify-write:
6507 * - decrypt destination in an intermediate buffer
6508 * - copy the source buffer in an intermediate buffer
6509 * - use the intermediate buffer as source buffer
6510 */
6511 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
6512 int dst_offset;
6513
6514 dst_tpage = alloc_page(GFP_KERNEL);
6515 if (!dst_tpage) {
6516 ret = -ENOMEM;
6517 goto e_free;
6518 }
6519
6520 ret = __sev_dbg_decrypt(kvm, dst_paddr,
6521 __sme_page_pa(dst_tpage), size, error);
6522 if (ret)
6523 goto e_free;
6524
6525 /*
6526 * If source is kernel buffer then use memcpy() otherwise
6527 * copy_from_user().
6528 */
6529 dst_offset = dst_paddr & 15;
6530
6531 if (src_tpage)
6532 memcpy(page_address(dst_tpage) + dst_offset,
6533 page_address(src_tpage), size);
6534 else {
6535 if (copy_from_user(page_address(dst_tpage) + dst_offset,
6536 (void __user *)(uintptr_t)vaddr, size)) {
6537 ret = -EFAULT;
6538 goto e_free;
6539 }
6540 }
6541
6542 paddr = __sme_page_pa(dst_tpage);
6543 dst_paddr = round_down(dst_paddr, 16);
6544 len = round_up(size, 16);
6545 }
6546
6547 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
6548
6549e_free:
6550 if (src_tpage)
6551 __free_page(src_tpage);
6552 if (dst_tpage)
6553 __free_page(dst_tpage);
6554 return ret;
6555}
6556
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006557static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
6558{
6559 unsigned long vaddr, vaddr_end, next_vaddr;
6560 unsigned long dst_vaddr, dst_vaddr_end;
6561 struct page **src_p, **dst_p;
6562 struct kvm_sev_dbg debug;
6563 unsigned long n;
6564 int ret, size;
6565
6566 if (!sev_guest(kvm))
6567 return -ENOTTY;
6568
6569 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
6570 return -EFAULT;
6571
6572 vaddr = debug.src_uaddr;
6573 size = debug.len;
6574 vaddr_end = vaddr + size;
6575 dst_vaddr = debug.dst_uaddr;
6576 dst_vaddr_end = dst_vaddr + size;
6577
6578 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
6579 int len, s_off, d_off;
6580
6581 /* lock userspace source and destination page */
6582 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
6583 if (!src_p)
6584 return -EFAULT;
6585
6586 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
6587 if (!dst_p) {
6588 sev_unpin_memory(kvm, src_p, n);
6589 return -EFAULT;
6590 }
6591
6592 /*
6593 * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
6594 * memory content (i.e it will write the same memory region with C=1).
6595 * It's possible that the cache may contain the data with C=0, i.e.,
6596 * unencrypted so invalidate it first.
6597 */
6598 sev_clflush_pages(src_p, 1);
6599 sev_clflush_pages(dst_p, 1);
6600
6601 /*
6602 * Since user buffer may not be page aligned, calculate the
6603 * offset within the page.
6604 */
6605 s_off = vaddr & ~PAGE_MASK;
6606 d_off = dst_vaddr & ~PAGE_MASK;
6607 len = min_t(size_t, (PAGE_SIZE - s_off), size);
6608
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006609 if (dec)
6610 ret = __sev_dbg_decrypt_user(kvm,
6611 __sme_page_pa(src_p[0]) + s_off,
6612 dst_vaddr,
6613 __sme_page_pa(dst_p[0]) + d_off,
6614 len, &argp->error);
6615 else
6616 ret = __sev_dbg_encrypt_user(kvm,
6617 __sme_page_pa(src_p[0]) + s_off,
6618 vaddr,
6619 __sme_page_pa(dst_p[0]) + d_off,
6620 dst_vaddr,
6621 len, &argp->error);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006622
6623 sev_unpin_memory(kvm, src_p, 1);
6624 sev_unpin_memory(kvm, dst_p, 1);
6625
6626 if (ret)
6627 goto err;
6628
6629 next_vaddr = vaddr + len;
6630 dst_vaddr = dst_vaddr + len;
6631 size -= len;
6632 }
6633err:
6634 return ret;
6635}
6636
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006637static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
6638{
6639 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6640 struct sev_data_launch_secret *data;
6641 struct kvm_sev_launch_secret params;
6642 struct page **pages;
6643 void *blob, *hdr;
6644 unsigned long n;
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006645 int ret, offset;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006646
6647 if (!sev_guest(kvm))
6648 return -ENOTTY;
6649
6650 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6651 return -EFAULT;
6652
6653 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
6654 if (!pages)
6655 return -ENOMEM;
6656
6657 /*
6658 * The secret must be copied into contiguous memory region, lets verify
6659 * that userspace memory pages are contiguous before we issue command.
6660 */
6661 if (get_num_contig_pages(0, pages, n) != n) {
6662 ret = -EINVAL;
6663 goto e_unpin_memory;
6664 }
6665
6666 ret = -ENOMEM;
6667 data = kzalloc(sizeof(*data), GFP_KERNEL);
6668 if (!data)
6669 goto e_unpin_memory;
6670
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006671 offset = params.guest_uaddr & (PAGE_SIZE - 1);
6672 data->guest_address = __sme_page_pa(pages[0]) + offset;
6673 data->guest_len = params.guest_len;
6674
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006675 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
6676 if (IS_ERR(blob)) {
6677 ret = PTR_ERR(blob);
6678 goto e_free;
6679 }
6680
6681 data->trans_address = __psp_pa(blob);
6682 data->trans_len = params.trans_len;
6683
6684 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
6685 if (IS_ERR(hdr)) {
6686 ret = PTR_ERR(hdr);
6687 goto e_free_blob;
6688 }
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006689 data->hdr_address = __psp_pa(hdr);
6690 data->hdr_len = params.hdr_len;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006691
6692 data->handle = sev->handle;
6693 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
6694
6695 kfree(hdr);
6696
6697e_free_blob:
6698 kfree(blob);
6699e_free:
6700 kfree(data);
6701e_unpin_memory:
6702 sev_unpin_memory(kvm, pages, n);
6703 return ret;
6704}
6705
Brijesh Singh1654efc2017-12-04 10:57:34 -06006706static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
6707{
6708 struct kvm_sev_cmd sev_cmd;
6709 int r;
6710
6711 if (!svm_sev_enabled())
6712 return -ENOTTY;
6713
6714 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
6715 return -EFAULT;
6716
6717 mutex_lock(&kvm->lock);
6718
6719 switch (sev_cmd.id) {
6720 case KVM_SEV_INIT:
6721 r = sev_guest_init(kvm, &sev_cmd);
6722 break;
Brijesh Singh59414c92017-12-04 10:57:35 -06006723 case KVM_SEV_LAUNCH_START:
6724 r = sev_launch_start(kvm, &sev_cmd);
6725 break;
Brijesh Singh89c50582017-12-04 10:57:35 -06006726 case KVM_SEV_LAUNCH_UPDATE_DATA:
6727 r = sev_launch_update_data(kvm, &sev_cmd);
6728 break;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006729 case KVM_SEV_LAUNCH_MEASURE:
6730 r = sev_launch_measure(kvm, &sev_cmd);
6731 break;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006732 case KVM_SEV_LAUNCH_FINISH:
6733 r = sev_launch_finish(kvm, &sev_cmd);
6734 break;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006735 case KVM_SEV_GUEST_STATUS:
6736 r = sev_guest_status(kvm, &sev_cmd);
6737 break;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006738 case KVM_SEV_DBG_DECRYPT:
6739 r = sev_dbg_crypt(kvm, &sev_cmd, true);
6740 break;
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006741 case KVM_SEV_DBG_ENCRYPT:
6742 r = sev_dbg_crypt(kvm, &sev_cmd, false);
6743 break;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006744 case KVM_SEV_LAUNCH_SECRET:
6745 r = sev_launch_secret(kvm, &sev_cmd);
6746 break;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006747 default:
6748 r = -EINVAL;
6749 goto out;
6750 }
6751
6752 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
6753 r = -EFAULT;
6754
6755out:
6756 mutex_unlock(&kvm->lock);
6757 return r;
6758}
6759
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006760static int svm_register_enc_region(struct kvm *kvm,
6761 struct kvm_enc_region *range)
6762{
6763 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6764 struct enc_region *region;
6765 int ret = 0;
6766
6767 if (!sev_guest(kvm))
6768 return -ENOTTY;
6769
6770 region = kzalloc(sizeof(*region), GFP_KERNEL);
6771 if (!region)
6772 return -ENOMEM;
6773
6774 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
6775 if (!region->pages) {
6776 ret = -ENOMEM;
6777 goto e_free;
6778 }
6779
6780 /*
6781 * The guest may change the memory encryption attribute from C=0 -> C=1
6782 * or vice versa for this memory range. Lets make sure caches are
6783 * flushed to ensure that guest data gets written into memory with
6784 * correct C-bit.
6785 */
6786 sev_clflush_pages(region->pages, region->npages);
6787
6788 region->uaddr = range->addr;
6789 region->size = range->size;
6790
6791 mutex_lock(&kvm->lock);
6792 list_add_tail(&region->list, &sev->regions_list);
6793 mutex_unlock(&kvm->lock);
6794
6795 return ret;
6796
6797e_free:
6798 kfree(region);
6799 return ret;
6800}
6801
6802static struct enc_region *
6803find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
6804{
6805 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6806 struct list_head *head = &sev->regions_list;
6807 struct enc_region *i;
6808
6809 list_for_each_entry(i, head, list) {
6810 if (i->uaddr == range->addr &&
6811 i->size == range->size)
6812 return i;
6813 }
6814
6815 return NULL;
6816}
6817
6818
6819static int svm_unregister_enc_region(struct kvm *kvm,
6820 struct kvm_enc_region *range)
6821{
6822 struct enc_region *region;
6823 int ret;
6824
6825 mutex_lock(&kvm->lock);
6826
6827 if (!sev_guest(kvm)) {
6828 ret = -ENOTTY;
6829 goto failed;
6830 }
6831
6832 region = find_enc_region(kvm, range);
6833 if (!region) {
6834 ret = -EINVAL;
6835 goto failed;
6836 }
6837
6838 __unregister_enc_region_locked(kvm, region);
6839
6840 mutex_unlock(&kvm->lock);
6841 return 0;
6842
6843failed:
6844 mutex_unlock(&kvm->lock);
6845 return ret;
6846}
6847
Kees Cook404f6aa2016-08-08 16:29:06 -07006848static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006849 .cpu_has_kvm_support = has_svm,
6850 .disabled_by_bios = is_disabled,
6851 .hardware_setup = svm_hardware_setup,
6852 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03006853 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006854 .hardware_enable = svm_hardware_enable,
6855 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02006856 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006857 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006858
6859 .vcpu_create = svm_create_vcpu,
6860 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03006861 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006862
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006863 .vm_init = avic_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06006864 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006865
Avi Kivity04d2cc72007-09-10 18:10:54 +03006866 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006867 .vcpu_load = svm_vcpu_load,
6868 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05006869 .vcpu_blocking = svm_vcpu_blocking,
6870 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006871
Paolo Bonzinia96036b2015-11-10 11:55:36 +01006872 .update_bp_intercept = update_bp_intercept,
Tom Lendacky801e4592018-02-21 13:39:51 -06006873 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006874 .get_msr = svm_get_msr,
6875 .set_msr = svm_set_msr,
6876 .get_segment_base = svm_get_segment_base,
6877 .get_segment = svm_get_segment,
6878 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02006879 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10006880 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02006881 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02006882 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03006883 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006884 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006885 .set_cr3 = svm_set_cr3,
6886 .set_cr4 = svm_set_cr4,
6887 .set_efer = svm_set_efer,
6888 .get_idt = svm_get_idt,
6889 .set_idt = svm_set_idt,
6890 .get_gdt = svm_get_gdt,
6891 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006892 .get_dr6 = svm_get_dr6,
6893 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03006894 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01006895 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03006896 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006897 .get_rflags = svm_get_rflags,
6898 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08006899
Avi Kivity6aa8b732006-12-10 02:21:36 -08006900 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006901
Avi Kivity6aa8b732006-12-10 02:21:36 -08006902 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03006903 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006904 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04006905 .set_interrupt_shadow = svm_set_interrupt_shadow,
6906 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02006907 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03006908 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006909 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02006910 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006911 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02006912 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006913 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006914 .get_nmi_mask = svm_get_nmi_mask,
6915 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006916 .enable_nmi_window = enable_nmi_window,
6917 .enable_irq_window = enable_irq_window,
6918 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08006919 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03006920 .get_enable_apicv = svm_get_enable_apicv,
6921 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08006922 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006923 .hwapic_irr_update = svm_hwapic_irr_update,
6924 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02006925 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05006926 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02006927
6928 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08006929 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006930 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006931
Avi Kivity586f9602010-11-18 13:09:54 +02006932 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02006933
Sheng Yang17cc3932010-01-05 19:02:27 +08006934 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08006935
6936 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006937
6938 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00006939 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006940 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08006941 .xsaves_supported = svm_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +02006942 .umip_emulated = svm_umip_emulated,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006943
6944 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006945
6946 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10006947
6948 .write_tsc_offset = svm_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02006949
6950 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006951
6952 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08006953 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006954
6955 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02006956
6957 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05006958 .deliver_posted_interrupt = svm_deliver_avic_intr,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05006959 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02006960 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006961
Ladi Prosek72d7b372017-10-11 16:54:41 +02006962 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006963 .pre_enter_smm = svm_pre_enter_smm,
6964 .pre_leave_smm = svm_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006965 .enable_smi_window = enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06006966
6967 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006968 .mem_enc_reg_region = svm_register_enc_region,
6969 .mem_enc_unreg_region = svm_unregister_enc_region,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006970};
6971
6972static int __init svm_init(void)
6973{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08006974 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03006975 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006976}
6977
6978static void __exit svm_exit(void)
6979{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08006980 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08006981}
6982
6983module_init(svm_init)
6984module_exit(svm_exit)