blob: 3ce5ba8dc58b2aeb45ce7f3e0fc4aaa59ea8547c [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? */
Sean Christophersondca7f122018-03-08 08:57:27 -08002643 int size, in, string;
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;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002655
Sean Christophersondca7f122018-03-08 08:57:27 -08002656 return kvm_fast_pio(&svm->vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657}
2658
Avi Kivity851ba692009-08-24 11:10:17 +03002659static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002660{
2661 return 1;
2662}
2663
Avi Kivity851ba692009-08-24 11:10:17 +03002664static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002665{
2666 ++svm->vcpu.stat.irq_exits;
2667 return 1;
2668}
2669
Avi Kivity851ba692009-08-24 11:10:17 +03002670static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671{
2672 return 1;
2673}
2674
Avi Kivity851ba692009-08-24 11:10:17 +03002675static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002677 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002678 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679}
2680
Avi Kivity851ba692009-08-24 11:10:17 +03002681static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002682{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002683 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002684 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002685}
2686
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002687static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2688{
2689 struct vcpu_svm *svm = to_svm(vcpu);
2690
2691 return svm->nested.nested_cr3;
2692}
2693
Avi Kivitye4e517b2011-07-28 11:36:17 +03002694static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2695{
2696 struct vcpu_svm *svm = to_svm(vcpu);
2697 u64 cr3 = svm->nested.nested_cr3;
2698 u64 pdpte;
2699 int ret;
2700
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002701 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002702 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002703 if (ret)
2704 return 0;
2705 return pdpte;
2706}
2707
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002708static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2709 unsigned long root)
2710{
2711 struct vcpu_svm *svm = to_svm(vcpu);
2712
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002713 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01002714 mark_dirty(svm->vmcb, VMCB_NPT);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002715 svm_flush_tlb(vcpu, true);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002716}
2717
Avi Kivity6389ee92010-11-29 16:12:30 +02002718static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2719 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002720{
2721 struct vcpu_svm *svm = to_svm(vcpu);
2722
Paolo Bonzini5e352512014-09-02 13:18:37 +02002723 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2724 /*
2725 * TODO: track the cause of the nested page fault, and
2726 * correctly fill in the high bits of exit_info_1.
2727 */
2728 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2729 svm->vmcb->control.exit_code_hi = 0;
2730 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2731 svm->vmcb->control.exit_info_2 = fault->address;
2732 }
2733
2734 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2735 svm->vmcb->control.exit_info_1 |= fault->error_code;
2736
2737 /*
2738 * The present bit is always zero for page structure faults on real
2739 * hardware.
2740 */
2741 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2742 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002743
2744 nested_svm_vmexit(svm);
2745}
2746
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002747static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002748{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002749 WARN_ON(mmu_is_nested(vcpu));
2750 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002751 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2752 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002753 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002754 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
Yu Zhang855feb62017-08-24 20:27:55 +08002755 vcpu->arch.mmu.shadow_root_level = get_npt_level(vcpu);
Xiao Guangrongc258b622015-08-05 12:04:24 +08002756 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002757 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002758}
2759
2760static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2761{
2762 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2763}
2764
Alexander Grafc0725422008-11-25 20:17:03 +01002765static int nested_svm_check_permissions(struct vcpu_svm *svm)
2766{
Dan Carpentere9196ce2017-05-18 10:39:53 +03002767 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
2768 !is_paging(&svm->vcpu)) {
Alexander Grafc0725422008-11-25 20:17:03 +01002769 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2770 return 1;
2771 }
2772
2773 if (svm->vmcb->save.cpl) {
2774 kvm_inject_gp(&svm->vcpu, 0);
2775 return 1;
2776 }
2777
Dan Carpentere9196ce2017-05-18 10:39:53 +03002778 return 0;
Alexander Grafc0725422008-11-25 20:17:03 +01002779}
2780
Alexander Grafcf74a782008-11-25 20:17:08 +01002781static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2782 bool has_error_code, u32 error_code)
2783{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002784 int vmexit;
2785
Joerg Roedel20307532010-11-29 17:51:48 +01002786 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002787 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002788
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002789 vmexit = nested_svm_intercept(svm);
2790 if (vmexit != NESTED_EXIT_DONE)
2791 return 0;
2792
Joerg Roedel0295ad72009-08-07 11:49:37 +02002793 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2794 svm->vmcb->control.exit_code_hi = 0;
2795 svm->vmcb->control.exit_info_1 = error_code;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002796
2797 /*
2798 * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception.
2799 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2800 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be
2801 * written only when inject_pending_event runs (DR6 would written here
2802 * too). This should be conditional on a new capability---if the
2803 * capability is disabled, kvm_multiple_exception would write the
2804 * ancillary information to CR2 or DR6, for backwards ABI-compatibility.
2805 */
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002806 if (svm->vcpu.arch.exception.nested_apf)
2807 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
2808 else
2809 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
Joerg Roedel0295ad72009-08-07 11:49:37 +02002810
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002811 svm->nested.exit_required = true;
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002812 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002813}
2814
Joerg Roedel8fe54652010-02-19 16:23:01 +01002815/* This function returns true if it is save to enable the irq window */
2816static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002817{
Joerg Roedel20307532010-11-29 17:51:48 +01002818 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002819 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002820
Joerg Roedel26666952009-08-07 11:49:46 +02002821 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002822 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002823
Joerg Roedel26666952009-08-07 11:49:46 +02002824 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002825 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002826
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002827 /*
2828 * if vmexit was already requested (by intercepted exception
2829 * for instance) do not overwrite it with "external interrupt"
2830 * vmexit.
2831 */
2832 if (svm->nested.exit_required)
2833 return false;
2834
Joerg Roedel197717d2010-02-24 18:59:19 +01002835 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2836 svm->vmcb->control.exit_info_1 = 0;
2837 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002838
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002839 if (svm->nested.intercept & 1ULL) {
2840 /*
2841 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002842 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002843 * #vmexit emulation might sleep. Only signal request for
2844 * the #vmexit here.
2845 */
2846 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002847 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002848 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002849 }
2850
Joerg Roedel8fe54652010-02-19 16:23:01 +01002851 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002852}
2853
Joerg Roedel887f5002010-02-24 18:59:12 +01002854/* This function returns true if it is save to enable the nmi window */
2855static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2856{
Joerg Roedel20307532010-11-29 17:51:48 +01002857 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002858 return true;
2859
2860 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2861 return true;
2862
2863 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2864 svm->nested.exit_required = true;
2865
2866 return false;
2867}
2868
Joerg Roedel7597f122010-02-19 16:23:00 +01002869static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002870{
2871 struct page *page;
2872
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002873 might_sleep();
2874
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002875 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002876 if (is_error_page(page))
2877 goto error;
2878
Joerg Roedel7597f122010-02-19 16:23:00 +01002879 *_page = page;
2880
2881 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002882
2883error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002884 kvm_inject_gp(&svm->vcpu, 0);
2885
2886 return NULL;
2887}
2888
Joerg Roedel7597f122010-02-19 16:23:00 +01002889static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002890{
Joerg Roedel7597f122010-02-19 16:23:00 +01002891 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002892 kvm_release_page_dirty(page);
2893}
2894
Joerg Roedelce2ac082010-03-01 15:34:39 +01002895static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002896{
Jan Kiszka9bf41832014-06-30 10:54:17 +02002897 unsigned port, size, iopm_len;
2898 u16 val, mask;
2899 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002900 u64 gpa;
2901
2902 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2903 return NESTED_EXIT_HOST;
2904
2905 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02002906 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2907 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002908 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02002909 start_bit = port % 8;
2910 iopm_len = (start_bit + size > 8) ? 2 : 1;
2911 mask = (0xf >> (4 - size)) << start_bit;
2912 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002913
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002914 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02002915 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002916
Jan Kiszka9bf41832014-06-30 10:54:17 +02002917 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002918}
2919
Joerg Roedeld2477822010-03-01 15:34:34 +01002920static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002921{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002922 u32 offset, msr, value;
2923 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002924
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002925 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002926 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002927
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002928 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2929 offset = svm_msrpm_offset(msr);
2930 write = svm->vmcb->control.exit_info_1 & 1;
2931 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002932
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002933 if (offset == MSR_INVALID)
2934 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002935
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002936 /* Offset is in 32 bit units but need in 8 bit units */
2937 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002938
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002939 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002940 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002941
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002942 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002943}
2944
Ladi Prosekab2f4d732017-06-21 09:06:58 +02002945/* DB exceptions for our internal use must not cause vmexit */
2946static int nested_svm_intercept_db(struct vcpu_svm *svm)
2947{
2948 unsigned long dr6;
2949
2950 /* if we're not singlestepping, it's not ours */
2951 if (!svm->nmi_singlestep)
2952 return NESTED_EXIT_DONE;
2953
2954 /* if it's not a singlestep exception, it's not ours */
2955 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
2956 return NESTED_EXIT_DONE;
2957 if (!(dr6 & DR6_BS))
2958 return NESTED_EXIT_DONE;
2959
2960 /* if the guest is singlestepping, it should get the vmexit */
2961 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
2962 disable_nmi_singlestep(svm);
2963 return NESTED_EXIT_DONE;
2964 }
2965
2966 /* it's ours, the nested hypervisor must not see this one */
2967 return NESTED_EXIT_HOST;
2968}
2969
Joerg Roedel410e4d52009-08-07 11:49:44 +02002970static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002971{
Alexander Grafcf74a782008-11-25 20:17:08 +01002972 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002973
Joerg Roedel410e4d52009-08-07 11:49:44 +02002974 switch (exit_code) {
2975 case SVM_EXIT_INTR:
2976 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002977 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002978 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002979 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002980 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002981 if (npt_enabled)
2982 return NESTED_EXIT_HOST;
2983 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002984 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002985 /* When we're shadowing, trap PFs, but not async PF */
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002986 if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002987 return NESTED_EXIT_HOST;
2988 break;
2989 default:
2990 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002991 }
2992
Joerg Roedel410e4d52009-08-07 11:49:44 +02002993 return NESTED_EXIT_CONTINUE;
2994}
2995
2996/*
2997 * If this function returns true, this #vmexit was already handled
2998 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002999static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003000{
3001 u32 exit_code = svm->vmcb->control.exit_code;
3002 int vmexit = NESTED_EXIT_HOST;
3003
Alexander Grafcf74a782008-11-25 20:17:08 +01003004 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003005 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003006 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003007 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003008 case SVM_EXIT_IOIO:
3009 vmexit = nested_svm_intercept_ioio(svm);
3010 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003011 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
3012 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
3013 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003014 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003015 break;
3016 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01003017 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
3018 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
3019 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003020 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003021 break;
3022 }
3023 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
3024 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003025 if (svm->nested.intercept_exceptions & excp_bits) {
3026 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
3027 vmexit = nested_svm_intercept_db(svm);
3028 else
3029 vmexit = NESTED_EXIT_DONE;
3030 }
Gleb Natapov631bc482010-10-14 11:22:52 +02003031 /* async page fault always cause vmexit */
3032 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003033 svm->vcpu.arch.exception.nested_apf != 0)
Gleb Natapov631bc482010-10-14 11:22:52 +02003034 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003035 break;
3036 }
Joerg Roedel228070b2010-04-22 12:33:10 +02003037 case SVM_EXIT_ERR: {
3038 vmexit = NESTED_EXIT_DONE;
3039 break;
3040 }
Alexander Grafcf74a782008-11-25 20:17:08 +01003041 default: {
3042 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02003043 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003044 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003045 }
3046 }
3047
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003048 return vmexit;
3049}
3050
3051static int nested_svm_exit_handled(struct vcpu_svm *svm)
3052{
3053 int vmexit;
3054
3055 vmexit = nested_svm_intercept(svm);
3056
3057 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003058 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003059
3060 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01003061}
3062
Joerg Roedel0460a972009-08-07 11:49:31 +02003063static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
3064{
3065 struct vmcb_control_area *dst = &dst_vmcb->control;
3066 struct vmcb_control_area *from = &from_vmcb->control;
3067
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003068 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003069 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02003070 dst->intercept_exceptions = from->intercept_exceptions;
3071 dst->intercept = from->intercept;
3072 dst->iopm_base_pa = from->iopm_base_pa;
3073 dst->msrpm_base_pa = from->msrpm_base_pa;
3074 dst->tsc_offset = from->tsc_offset;
3075 dst->asid = from->asid;
3076 dst->tlb_ctl = from->tlb_ctl;
3077 dst->int_ctl = from->int_ctl;
3078 dst->int_vector = from->int_vector;
3079 dst->int_state = from->int_state;
3080 dst->exit_code = from->exit_code;
3081 dst->exit_code_hi = from->exit_code_hi;
3082 dst->exit_info_1 = from->exit_info_1;
3083 dst->exit_info_2 = from->exit_info_2;
3084 dst->exit_int_info = from->exit_int_info;
3085 dst->exit_int_info_err = from->exit_int_info_err;
3086 dst->nested_ctl = from->nested_ctl;
3087 dst->event_inj = from->event_inj;
3088 dst->event_inj_err = from->event_inj_err;
3089 dst->nested_cr3 = from->nested_cr3;
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003090 dst->virt_ext = from->virt_ext;
Joerg Roedel0460a972009-08-07 11:49:31 +02003091}
3092
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003093static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003094{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003095 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003096 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02003097 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003098 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01003099
Joerg Roedel17897f32009-10-09 16:08:29 +02003100 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
3101 vmcb->control.exit_info_1,
3102 vmcb->control.exit_info_2,
3103 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003104 vmcb->control.exit_int_info_err,
3105 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02003106
Joerg Roedel7597f122010-02-19 16:23:00 +01003107 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003108 if (!nested_vmcb)
3109 return 1;
3110
Joerg Roedel20307532010-11-29 17:51:48 +01003111 /* Exit Guest-Mode */
3112 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01003113 svm->nested.vmcb = 0;
3114
Alexander Grafcf74a782008-11-25 20:17:08 +01003115 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02003116 disable_gif(svm);
3117
3118 nested_vmcb->save.es = vmcb->save.es;
3119 nested_vmcb->save.cs = vmcb->save.cs;
3120 nested_vmcb->save.ss = vmcb->save.ss;
3121 nested_vmcb->save.ds = vmcb->save.ds;
3122 nested_vmcb->save.gdtr = vmcb->save.gdtr;
3123 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02003124 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003125 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003126 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003127 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003128 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03003129 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003130 nested_vmcb->save.rip = vmcb->save.rip;
3131 nested_vmcb->save.rsp = vmcb->save.rsp;
3132 nested_vmcb->save.rax = vmcb->save.rax;
3133 nested_vmcb->save.dr7 = vmcb->save.dr7;
3134 nested_vmcb->save.dr6 = vmcb->save.dr6;
3135 nested_vmcb->save.cpl = vmcb->save.cpl;
3136
3137 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
3138 nested_vmcb->control.int_vector = vmcb->control.int_vector;
3139 nested_vmcb->control.int_state = vmcb->control.int_state;
3140 nested_vmcb->control.exit_code = vmcb->control.exit_code;
3141 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
3142 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
3143 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
3144 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
3145 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003146
3147 if (svm->nrips_enabled)
3148 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02003149
3150 /*
3151 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
3152 * to make sure that we do not lose injected events. So check event_inj
3153 * here and copy it to exit_int_info if it is valid.
3154 * Exit_int_info and event_inj can't be both valid because the case
3155 * below only happens on a VMRUN instruction intercept which has
3156 * no valid exit_int_info set.
3157 */
3158 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
3159 struct vmcb_control_area *nc = &nested_vmcb->control;
3160
3161 nc->exit_int_info = vmcb->control.event_inj;
3162 nc->exit_int_info_err = vmcb->control.event_inj_err;
3163 }
3164
Joerg Roedel33740e42009-08-07 11:49:29 +02003165 nested_vmcb->control.tlb_ctl = 0;
3166 nested_vmcb->control.event_inj = 0;
3167 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01003168
3169 /* We always set V_INTR_MASKING and remember the old value in hflags */
3170 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
3171 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
3172
Alexander Grafcf74a782008-11-25 20:17:08 +01003173 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02003174 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01003175
Alexander Graf219b65d2009-06-15 15:21:25 +02003176 kvm_clear_exception_queue(&svm->vcpu);
3177 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003178
Joerg Roedel4b161842010-09-10 17:31:03 +02003179 svm->nested.nested_cr3 = 0;
3180
Alexander Grafcf74a782008-11-25 20:17:08 +01003181 /* Restore selected save entries */
3182 svm->vmcb->save.es = hsave->save.es;
3183 svm->vmcb->save.cs = hsave->save.cs;
3184 svm->vmcb->save.ss = hsave->save.ss;
3185 svm->vmcb->save.ds = hsave->save.ds;
3186 svm->vmcb->save.gdtr = hsave->save.gdtr;
3187 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003188 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01003189 svm_set_efer(&svm->vcpu, hsave->save.efer);
3190 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
3191 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
3192 if (npt_enabled) {
3193 svm->vmcb->save.cr3 = hsave->save.cr3;
3194 svm->vcpu.arch.cr3 = hsave->save.cr3;
3195 } else {
Avi Kivity23902182010-06-10 17:02:16 +03003196 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01003197 }
3198 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
3199 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
3200 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
3201 svm->vmcb->save.dr7 = 0;
3202 svm->vmcb->save.cpl = 0;
3203 svm->vmcb->control.exit_int_info = 0;
3204
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003205 mark_all_dirty(svm->vmcb);
3206
Joerg Roedel7597f122010-02-19 16:23:00 +01003207 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01003208
Joerg Roedel4b161842010-09-10 17:31:03 +02003209 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003210 kvm_mmu_reset_context(&svm->vcpu);
3211 kvm_mmu_load(&svm->vcpu);
3212
3213 return 0;
3214}
Alexander Graf3d6368e2008-11-25 20:17:07 +01003215
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003216static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003217{
Joerg Roedel323c3d82010-03-01 15:34:37 +01003218 /*
3219 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003220 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01003221 * the kvm msr permission bitmap may contain zero bits
3222 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01003223 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003224
Joerg Roedel323c3d82010-03-01 15:34:37 +01003225 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3226 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003227
Joerg Roedel323c3d82010-03-01 15:34:37 +01003228 for (i = 0; i < MSRPM_OFFSETS; i++) {
3229 u32 value, p;
3230 u64 offset;
3231
3232 if (msrpm_offsets[i] == 0xffffffff)
3233 break;
3234
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003235 p = msrpm_offsets[i];
3236 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01003237
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003238 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01003239 return false;
3240
3241 svm->nested.msrpm[p] = svm->msrpm[p] | value;
3242 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003243
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05003244 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
Alexander Graf3d6368e2008-11-25 20:17:07 +01003245
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003246 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003247}
3248
Joerg Roedel52c65a302010-08-02 16:46:44 +02003249static bool nested_vmcb_checks(struct vmcb *vmcb)
3250{
3251 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
3252 return false;
3253
Joerg Roedeldbe77582010-08-02 16:46:45 +02003254 if (vmcb->control.asid == 0)
3255 return false;
3256
Tom Lendackycea3a192017-12-04 10:57:24 -06003257 if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
3258 !npt_enabled)
Joerg Roedel4b161842010-09-10 17:31:03 +02003259 return false;
3260
Joerg Roedel52c65a302010-08-02 16:46:44 +02003261 return true;
3262}
3263
Ladi Prosekc2634062017-10-11 16:54:44 +02003264static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
3265 struct vmcb *nested_vmcb, struct page *page)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003266{
Avi Kivityf6e78472010-08-02 15:30:20 +03003267 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003268 svm->vcpu.arch.hflags |= HF_HIF_MASK;
3269 else
3270 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
3271
Tom Lendackycea3a192017-12-04 10:57:24 -06003272 if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
Joerg Roedel4b161842010-09-10 17:31:03 +02003273 kvm_mmu_unload(&svm->vcpu);
3274 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
3275 nested_svm_init_mmu_context(&svm->vcpu);
3276 }
3277
Alexander Graf3d6368e2008-11-25 20:17:07 +01003278 /* Load the nested guest state */
3279 svm->vmcb->save.es = nested_vmcb->save.es;
3280 svm->vmcb->save.cs = nested_vmcb->save.cs;
3281 svm->vmcb->save.ss = nested_vmcb->save.ss;
3282 svm->vmcb->save.ds = nested_vmcb->save.ds;
3283 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
3284 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003285 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003286 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
3287 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
3288 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
3289 if (npt_enabled) {
3290 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
3291 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003292 } else
Avi Kivity23902182010-06-10 17:02:16 +03003293 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003294
3295 /* Guest paging mode is active - reset mmu */
3296 kvm_mmu_reset_context(&svm->vcpu);
3297
Joerg Roedeldefbba52009-08-07 11:49:30 +02003298 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003299 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
3300 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
3301 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01003302
Alexander Graf3d6368e2008-11-25 20:17:07 +01003303 /* In case we don't even reach vcpu_run, the fields are not updated */
3304 svm->vmcb->save.rax = nested_vmcb->save.rax;
3305 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3306 svm->vmcb->save.rip = nested_vmcb->save.rip;
3307 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3308 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3309 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3310
Joerg Roedelf7138532010-03-01 15:34:40 +01003311 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003312 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003313
Joerg Roedelaad42c62009-08-07 11:49:34 +02003314 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003315 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003316 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02003317 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3318 svm->nested.intercept = nested_vmcb->control.intercept;
3319
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08003320 svm_flush_tlb(&svm->vcpu, true);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003321 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003322 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3323 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3324 else
3325 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3326
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003327 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3328 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003329 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3330 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003331 }
3332
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003333 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003334 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003335
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003336 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003337 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3338 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
3339 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003340 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3341 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3342
Joerg Roedel7597f122010-02-19 16:23:00 +01003343 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003344
Joerg Roedel20307532010-11-29 17:51:48 +01003345 /* Enter Guest-Mode */
3346 enter_guest_mode(&svm->vcpu);
3347
Joerg Roedel384c6362010-11-30 18:03:56 +01003348 /*
3349 * Merge guest and host intercepts - must be called with vcpu in
3350 * guest-mode to take affect here
3351 */
3352 recalc_intercepts(svm);
3353
Joerg Roedel06fc77722010-02-19 16:23:07 +01003354 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003355
Joerg Roedel2af91942009-08-07 11:49:28 +02003356 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003357
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003358 mark_all_dirty(svm->vmcb);
Ladi Prosekc2634062017-10-11 16:54:44 +02003359}
3360
3361static bool nested_svm_vmrun(struct vcpu_svm *svm)
3362{
3363 struct vmcb *nested_vmcb;
3364 struct vmcb *hsave = svm->nested.hsave;
3365 struct vmcb *vmcb = svm->vmcb;
3366 struct page *page;
3367 u64 vmcb_gpa;
3368
3369 vmcb_gpa = svm->vmcb->save.rax;
3370
3371 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
3372 if (!nested_vmcb)
3373 return false;
3374
3375 if (!nested_vmcb_checks(nested_vmcb)) {
3376 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3377 nested_vmcb->control.exit_code_hi = 0;
3378 nested_vmcb->control.exit_info_1 = 0;
3379 nested_vmcb->control.exit_info_2 = 0;
3380
3381 nested_svm_unmap(page);
3382
3383 return false;
3384 }
3385
3386 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3387 nested_vmcb->save.rip,
3388 nested_vmcb->control.int_ctl,
3389 nested_vmcb->control.event_inj,
3390 nested_vmcb->control.nested_ctl);
3391
3392 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3393 nested_vmcb->control.intercept_cr >> 16,
3394 nested_vmcb->control.intercept_exceptions,
3395 nested_vmcb->control.intercept);
3396
3397 /* Clear internal status */
3398 kvm_clear_exception_queue(&svm->vcpu);
3399 kvm_clear_interrupt_queue(&svm->vcpu);
3400
3401 /*
3402 * Save the old vmcb, so we don't need to pick what we save, but can
3403 * restore everything when a VMEXIT occurs
3404 */
3405 hsave->save.es = vmcb->save.es;
3406 hsave->save.cs = vmcb->save.cs;
3407 hsave->save.ss = vmcb->save.ss;
3408 hsave->save.ds = vmcb->save.ds;
3409 hsave->save.gdtr = vmcb->save.gdtr;
3410 hsave->save.idtr = vmcb->save.idtr;
3411 hsave->save.efer = svm->vcpu.arch.efer;
3412 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3413 hsave->save.cr4 = svm->vcpu.arch.cr4;
3414 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3415 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3416 hsave->save.rsp = vmcb->save.rsp;
3417 hsave->save.rax = vmcb->save.rax;
3418 if (npt_enabled)
3419 hsave->save.cr3 = vmcb->save.cr3;
3420 else
3421 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3422
3423 copy_vmcb_control_area(hsave, vmcb);
3424
3425 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003426
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003427 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003428}
3429
Joerg Roedel9966bf62009-08-07 11:49:40 +02003430static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01003431{
3432 to_vmcb->save.fs = from_vmcb->save.fs;
3433 to_vmcb->save.gs = from_vmcb->save.gs;
3434 to_vmcb->save.tr = from_vmcb->save.tr;
3435 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3436 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3437 to_vmcb->save.star = from_vmcb->save.star;
3438 to_vmcb->save.lstar = from_vmcb->save.lstar;
3439 to_vmcb->save.cstar = from_vmcb->save.cstar;
3440 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3441 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3442 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3443 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01003444}
3445
Avi Kivity851ba692009-08-24 11:10:17 +03003446static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003447{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003448 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003449 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003450 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003451
Alexander Graf55426752008-11-25 20:17:06 +01003452 if (nested_svm_check_permissions(svm))
3453 return 1;
3454
Joerg Roedel7597f122010-02-19 16:23:00 +01003455 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003456 if (!nested_vmcb)
3457 return 1;
3458
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003459 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003460 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003461
Joerg Roedel9966bf62009-08-07 11:49:40 +02003462 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003463 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003464
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003465 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003466}
3467
Avi Kivity851ba692009-08-24 11:10:17 +03003468static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003469{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003470 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003471 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003472 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003473
Alexander Graf55426752008-11-25 20:17:06 +01003474 if (nested_svm_check_permissions(svm))
3475 return 1;
3476
Joerg Roedel7597f122010-02-19 16:23:00 +01003477 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003478 if (!nested_vmcb)
3479 return 1;
3480
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003481 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003482 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003483
Joerg Roedel9966bf62009-08-07 11:49:40 +02003484 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003485 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003486
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003487 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003488}
3489
Avi Kivity851ba692009-08-24 11:10:17 +03003490static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003491{
Alexander Graf3d6368e2008-11-25 20:17:07 +01003492 if (nested_svm_check_permissions(svm))
3493 return 1;
3494
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02003495 /* Save rip after vmrun instruction */
3496 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003497
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003498 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01003499 return 1;
3500
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003501 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02003502 goto failed;
3503
3504 return 1;
3505
3506failed:
3507
3508 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3509 svm->vmcb->control.exit_code_hi = 0;
3510 svm->vmcb->control.exit_info_1 = 0;
3511 svm->vmcb->control.exit_info_2 = 0;
3512
3513 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003514
3515 return 1;
3516}
3517
Avi Kivity851ba692009-08-24 11:10:17 +03003518static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003519{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003520 int ret;
3521
Alexander Graf1371d902008-11-25 20:17:04 +01003522 if (nested_svm_check_permissions(svm))
3523 return 1;
3524
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003525 /*
3526 * If VGIF is enabled, the STGI intercept is only added to
Ladi Prosekcc3d9672017-10-17 16:02:39 +02003527 * detect the opening of the SMI/NMI window; remove it now.
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003528 */
3529 if (vgif_enabled(svm))
3530 clr_intercept(svm, INTERCEPT_STGI);
3531
Alexander Graf1371d902008-11-25 20:17:04 +01003532 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003533 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003534 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003535
Joerg Roedel2af91942009-08-07 11:49:28 +02003536 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003537
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003538 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003539}
3540
Avi Kivity851ba692009-08-24 11:10:17 +03003541static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003542{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003543 int ret;
3544
Alexander Graf1371d902008-11-25 20:17:04 +01003545 if (nested_svm_check_permissions(svm))
3546 return 1;
3547
3548 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003549 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003550
Joerg Roedel2af91942009-08-07 11:49:28 +02003551 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003552
3553 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05003554 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3555 svm_clear_vintr(svm);
3556 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3557 mark_dirty(svm->vmcb, VMCB_INTR);
3558 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003559
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003560 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003561}
3562
Avi Kivity851ba692009-08-24 11:10:17 +03003563static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02003564{
3565 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02003566
David Kaplan668f1982015-02-20 16:02:10 -06003567 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
3568 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02003569
Alexander Grafff092382009-06-15 15:21:24 +02003570 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06003571 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02003572
3573 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003574 return kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02003575}
3576
Joerg Roedel532a46b2009-10-09 16:08:32 +02003577static int skinit_interception(struct vcpu_svm *svm)
3578{
David Kaplan668f1982015-02-20 16:02:10 -06003579 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003580
3581 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3582 return 1;
3583}
3584
David Kaplandab429a2015-03-02 13:43:37 -06003585static int wbinvd_interception(struct vcpu_svm *svm)
3586{
Kyle Huey6affcbe2016-11-29 12:40:40 -08003587 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06003588}
3589
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003590static int xsetbv_interception(struct vcpu_svm *svm)
3591{
3592 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
3593 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3594
3595 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3596 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003597 return kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003598 }
3599
3600 return 1;
3601}
3602
Avi Kivity851ba692009-08-24 11:10:17 +03003603static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003604{
Izik Eidus37817f22008-03-24 23:14:53 +02003605 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003606 int reason;
3607 int int_type = svm->vmcb->control.exit_int_info &
3608 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003609 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003610 uint32_t type =
3611 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3612 uint32_t idt_v =
3613 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003614 bool has_error_code = false;
3615 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003616
3617 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003618
Izik Eidus37817f22008-03-24 23:14:53 +02003619 if (svm->vmcb->control.exit_info_2 &
3620 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003621 reason = TASK_SWITCH_IRET;
3622 else if (svm->vmcb->control.exit_info_2 &
3623 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3624 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003625 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003626 reason = TASK_SWITCH_GATE;
3627 else
3628 reason = TASK_SWITCH_CALL;
3629
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003630 if (reason == TASK_SWITCH_GATE) {
3631 switch (type) {
3632 case SVM_EXITINTINFO_TYPE_NMI:
3633 svm->vcpu.arch.nmi_injected = false;
3634 break;
3635 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003636 if (svm->vmcb->control.exit_info_2 &
3637 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3638 has_error_code = true;
3639 error_code =
3640 (u32)svm->vmcb->control.exit_info_2;
3641 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003642 kvm_clear_exception_queue(&svm->vcpu);
3643 break;
3644 case SVM_EXITINTINFO_TYPE_INTR:
3645 kvm_clear_interrupt_queue(&svm->vcpu);
3646 break;
3647 default:
3648 break;
3649 }
3650 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003651
Gleb Natapov8317c292009-04-12 13:37:02 +03003652 if (reason != TASK_SWITCH_GATE ||
3653 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3654 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03003655 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3656 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003657
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003658 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3659 int_vec = -1;
3660
3661 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03003662 has_error_code, error_code) == EMULATE_FAIL) {
3663 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3664 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3665 svm->vcpu.run->internal.ndata = 0;
3666 return 0;
3667 }
3668 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003669}
3670
Avi Kivity851ba692009-08-24 11:10:17 +03003671static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003672{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003673 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Kyle Huey6a908b62016-11-29 12:40:37 -08003674 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003675}
3676
Avi Kivity851ba692009-08-24 11:10:17 +03003677static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003678{
3679 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003680 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003681 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003682 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003683 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003684 return 1;
3685}
3686
Avi Kivity851ba692009-08-24 11:10:17 +03003687static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003688{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003689 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3690 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3691
3692 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003693 return kvm_skip_emulated_instruction(&svm->vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003694}
3695
Avi Kivity851ba692009-08-24 11:10:17 +03003696static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003697{
Andre Przywara51d8b662010-12-21 11:12:02 +01003698 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003699}
3700
Brijesh Singh7607b712018-02-19 10:14:44 -06003701static int rsm_interception(struct vcpu_svm *svm)
3702{
3703 return x86_emulate_instruction(&svm->vcpu, 0, 0,
3704 rsm_ins_bytes, 2) == EMULATE_DONE;
3705}
3706
Avi Kivity332b56e2011-11-10 14:57:24 +02003707static int rdpmc_interception(struct vcpu_svm *svm)
3708{
3709 int err;
3710
3711 if (!static_cpu_has(X86_FEATURE_NRIPS))
3712 return emulate_on_interception(svm);
3713
3714 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08003715 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02003716}
3717
Xiubo Li52eb5a62015-03-13 17:39:45 +08003718static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3719 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003720{
3721 unsigned long cr0 = svm->vcpu.arch.cr0;
3722 bool ret = false;
3723 u64 intercept;
3724
3725 intercept = svm->nested.intercept;
3726
3727 if (!is_guest_mode(&svm->vcpu) ||
3728 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3729 return false;
3730
3731 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3732 val &= ~SVM_CR0_SELECTIVE_MASK;
3733
3734 if (cr0 ^ val) {
3735 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3736 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3737 }
3738
3739 return ret;
3740}
3741
Andre Przywara7ff76d52010-12-21 11:12:04 +01003742#define CR_VALID (1ULL << 63)
3743
3744static int cr_interception(struct vcpu_svm *svm)
3745{
3746 int reg, cr;
3747 unsigned long val;
3748 int err;
3749
3750 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3751 return emulate_on_interception(svm);
3752
3753 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3754 return emulate_on_interception(svm);
3755
3756 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003757 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3758 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3759 else
3760 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003761
3762 err = 0;
3763 if (cr >= 16) { /* mov to cr */
3764 cr -= 16;
3765 val = kvm_register_read(&svm->vcpu, reg);
3766 switch (cr) {
3767 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003768 if (!check_selective_cr0_intercepted(svm, val))
3769 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003770 else
3771 return 1;
3772
Andre Przywara7ff76d52010-12-21 11:12:04 +01003773 break;
3774 case 3:
3775 err = kvm_set_cr3(&svm->vcpu, val);
3776 break;
3777 case 4:
3778 err = kvm_set_cr4(&svm->vcpu, val);
3779 break;
3780 case 8:
3781 err = kvm_set_cr8(&svm->vcpu, val);
3782 break;
3783 default:
3784 WARN(1, "unhandled write to CR%d", cr);
3785 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3786 return 1;
3787 }
3788 } else { /* mov from cr */
3789 switch (cr) {
3790 case 0:
3791 val = kvm_read_cr0(&svm->vcpu);
3792 break;
3793 case 2:
3794 val = svm->vcpu.arch.cr2;
3795 break;
3796 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003797 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003798 break;
3799 case 4:
3800 val = kvm_read_cr4(&svm->vcpu);
3801 break;
3802 case 8:
3803 val = kvm_get_cr8(&svm->vcpu);
3804 break;
3805 default:
3806 WARN(1, "unhandled read from CR%d", cr);
3807 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3808 return 1;
3809 }
3810 kvm_register_write(&svm->vcpu, reg, val);
3811 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08003812 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003813}
3814
Andre Przywaracae37972010-12-21 11:12:05 +01003815static int dr_interception(struct vcpu_svm *svm)
3816{
3817 int reg, dr;
3818 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003819
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003820 if (svm->vcpu.guest_debug == 0) {
3821 /*
3822 * No more DR vmexits; force a reload of the debug registers
3823 * and reenter on this instruction. The next vmexit will
3824 * retrieve the full state of the debug registers.
3825 */
3826 clr_dr_intercepts(svm);
3827 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3828 return 1;
3829 }
3830
Andre Przywaracae37972010-12-21 11:12:05 +01003831 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3832 return emulate_on_interception(svm);
3833
3834 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3835 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3836
3837 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003838 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3839 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003840 val = kvm_register_read(&svm->vcpu, reg);
3841 kvm_set_dr(&svm->vcpu, dr - 16, val);
3842 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003843 if (!kvm_require_dr(&svm->vcpu, dr))
3844 return 1;
3845 kvm_get_dr(&svm->vcpu, dr, &val);
3846 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003847 }
3848
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003849 return kvm_skip_emulated_instruction(&svm->vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01003850}
3851
Avi Kivity851ba692009-08-24 11:10:17 +03003852static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003853{
Avi Kivity851ba692009-08-24 11:10:17 +03003854 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003855 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003856
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003857 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3858 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003859 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003860 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003861 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003862 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003863 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003864 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3865 return 0;
3866}
3867
Tom Lendacky801e4592018-02-21 13:39:51 -06003868static int svm_get_msr_feature(struct kvm_msr_entry *msr)
3869{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01003870 msr->data = 0;
3871
3872 switch (msr->index) {
3873 case MSR_F10H_DECFG:
3874 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
3875 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
3876 break;
3877 default:
3878 return 1;
3879 }
3880
3881 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003882}
3883
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003884static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003885{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003886 struct vcpu_svm *svm = to_svm(vcpu);
3887
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003888 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303889 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003890 msr_info->data = svm->vmcb->control.tsc_offset +
Haozhong Zhang35181e82015-10-20 15:39:03 +08003891 kvm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003892
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893 break;
3894 }
Brian Gerst8c065852010-07-17 09:03:26 -04003895 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003896 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003897 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003898#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003899 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003900 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003901 break;
3902 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003903 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003904 break;
3905 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003906 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003907 break;
3908 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003909 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910 break;
3911#endif
3912 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003913 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003914 break;
3915 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003916 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003917 break;
3918 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003919 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003921 case MSR_TSC_AUX:
3922 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3923 return 1;
3924 msr_info->data = svm->tsc_aux;
3925 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003926 /*
3927 * Nobody will change the following 5 values in the VMCB so we can
3928 * safely return them on rdmsr. They will always be 0 until LBRV is
3929 * implemented.
3930 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003931 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003932 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01003933 break;
3934 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003935 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003936 break;
3937 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003938 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003939 break;
3940 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003941 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003942 break;
3943 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003944 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003945 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003946 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003947 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003948 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003949 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003950 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003951 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003952 case MSR_IA32_SPEC_CTRL:
3953 if (!msr_info->host_initiated &&
3954 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
3955 return 1;
3956
3957 msr_info->data = svm->spec_ctrl;
3958 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01003959 case MSR_F15H_IC_CFG: {
3960
3961 int family, model;
3962
3963 family = guest_cpuid_family(vcpu);
3964 model = guest_cpuid_model(vcpu);
3965
3966 if (family < 0 || model < 0)
3967 return kvm_get_msr_common(vcpu, msr_info);
3968
3969 msr_info->data = 0;
3970
3971 if (family == 0x15 &&
3972 (model >= 0x2 && model < 0x20))
3973 msr_info->data = 0x1E;
3974 }
3975 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01003976 case MSR_F10H_DECFG:
3977 msr_info->data = svm->msr_decfg;
3978 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003979 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003980 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003981 }
3982 return 0;
3983}
3984
Avi Kivity851ba692009-08-24 11:10:17 +03003985static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003986{
David Kaplan668f1982015-02-20 16:02:10 -06003987 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003988 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003989
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003990 msr_info.index = ecx;
3991 msr_info.host_initiated = false;
3992 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02003993 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003994 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003995 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02003996 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003997 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003998
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003999 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
4000 msr_info.data & 0xffffffff);
4001 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
4002 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004003 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004004 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004005 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004006}
4007
Joerg Roedel4a810182010-02-24 18:59:15 +01004008static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
4009{
4010 struct vcpu_svm *svm = to_svm(vcpu);
4011 int svm_dis, chg_mask;
4012
4013 if (data & ~SVM_VM_CR_VALID_MASK)
4014 return 1;
4015
4016 chg_mask = SVM_VM_CR_VALID_MASK;
4017
4018 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
4019 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
4020
4021 svm->nested.vm_cr_msr &= ~chg_mask;
4022 svm->nested.vm_cr_msr |= (data & chg_mask);
4023
4024 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
4025
4026 /* check for svm_disable while efer.svme is set */
4027 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
4028 return 1;
4029
4030 return 0;
4031}
4032
Will Auld8fe8ab42012-11-29 12:42:12 -08004033static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004034{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004035 struct vcpu_svm *svm = to_svm(vcpu);
4036
Will Auld8fe8ab42012-11-29 12:42:12 -08004037 u32 ecx = msr->index;
4038 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004039 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02004040 case MSR_IA32_CR_PAT:
4041 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4042 return 1;
4043 vcpu->arch.pat = data;
4044 svm->vmcb->save.g_pat = data;
4045 mark_dirty(svm->vmcb, VMCB_NPT);
4046 break;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10004047 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08004048 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004049 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004050 case MSR_IA32_SPEC_CTRL:
4051 if (!msr->host_initiated &&
4052 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
4053 return 1;
4054
4055 /* The STIBP bit doesn't fault even if it's not advertised */
4056 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
4057 return 1;
4058
4059 svm->spec_ctrl = data;
4060
4061 if (!data)
4062 break;
4063
4064 /*
4065 * For non-nested:
4066 * When it's written (to non-zero) for the first time, pass
4067 * it through.
4068 *
4069 * For nested:
4070 * The handling of the MSR bitmap for L2 guests is done in
4071 * nested_svm_vmrun_msrpm.
4072 * We update the L1 MSR bit as well since it will end up
4073 * touching the MSR anyway now.
4074 */
4075 set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
4076 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004077 case MSR_IA32_PRED_CMD:
4078 if (!msr->host_initiated &&
4079 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB))
4080 return 1;
4081
4082 if (data & ~PRED_CMD_IBPB)
4083 return 1;
4084
4085 if (!data)
4086 break;
4087
4088 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4089 if (is_guest_mode(vcpu))
4090 break;
4091 set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
4092 break;
Brian Gerst8c065852010-07-17 09:03:26 -04004093 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004094 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08004096#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004097 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004098 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004099 break;
4100 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004101 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004102 break;
4103 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004104 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004105 break;
4106 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004107 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004108 break;
4109#endif
4110 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004111 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004112 break;
4113 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02004114 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004115 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004116 break;
4117 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02004118 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004119 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004120 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004121 case MSR_TSC_AUX:
4122 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4123 return 1;
4124
4125 /*
4126 * This is rare, so we update the MSR here instead of using
4127 * direct_access_msrs. Doing that would require a rdmsr in
4128 * svm_vcpu_put.
4129 */
4130 svm->tsc_aux = data;
4131 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
4132 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01004133 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004134 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03004135 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
4136 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004137 break;
4138 }
4139 if (data & DEBUGCTL_RESERVED_BITS)
4140 return 1;
4141
4142 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01004143 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004144 if (data & (1ULL<<0))
4145 svm_enable_lbrv(svm);
4146 else
4147 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01004148 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004149 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02004150 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004151 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004152 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01004153 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004154 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03004155 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004156 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004157 case MSR_F10H_DECFG: {
4158 struct kvm_msr_entry msr_entry;
4159
4160 msr_entry.index = msr->index;
4161 if (svm_get_msr_feature(&msr_entry))
4162 return 1;
4163
4164 /* Check the supported bits */
4165 if (data & ~msr_entry.data)
4166 return 1;
4167
4168 /* Don't allow the guest to change a bit, #GP */
4169 if (!msr->host_initiated && (data ^ msr_entry.data))
4170 return 1;
4171
4172 svm->msr_decfg = data;
4173 break;
4174 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004175 case MSR_IA32_APICBASE:
4176 if (kvm_vcpu_apicv_active(vcpu))
4177 avic_update_vapic_bar(to_svm(vcpu), data);
4178 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004179 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08004180 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181 }
4182 return 0;
4183}
4184
Avi Kivity851ba692009-08-24 11:10:17 +03004185static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186{
Will Auld8fe8ab42012-11-29 12:42:12 -08004187 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06004188 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
4189 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004190
Will Auld8fe8ab42012-11-29 12:42:12 -08004191 msr.data = data;
4192 msr.index = ecx;
4193 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004194
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004195 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03004196 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02004197 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004198 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004199 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004200 } else {
4201 trace_kvm_msr_write(ecx, data);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004202 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02004203 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004204}
4205
Avi Kivity851ba692009-08-24 11:10:17 +03004206static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004207{
Rusty Russelle756fc62007-07-30 20:07:08 +10004208 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03004209 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004210 else
Avi Kivity851ba692009-08-24 11:10:17 +03004211 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004212}
4213
Avi Kivity851ba692009-08-24 11:10:17 +03004214static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08004215{
Avi Kivity3842d132010-07-27 12:30:24 +03004216 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01004217 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03004218 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004219 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08004220 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08004221 return 1;
4222}
4223
Mark Langsdorf565d0992009-10-06 14:25:02 -05004224static int pause_interception(struct vcpu_svm *svm)
4225{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004226 struct kvm_vcpu *vcpu = &svm->vcpu;
4227 bool in_kernel = (svm_get_cpl(vcpu) == 0);
4228
4229 kvm_vcpu_on_spin(vcpu, in_kernel);
Mark Langsdorf565d0992009-10-06 14:25:02 -05004230 return 1;
4231}
4232
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004233static int nop_interception(struct vcpu_svm *svm)
4234{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004235 return kvm_skip_emulated_instruction(&(svm->vcpu));
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004236}
4237
4238static int monitor_interception(struct vcpu_svm *svm)
4239{
4240 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
4241 return nop_interception(svm);
4242}
4243
4244static int mwait_interception(struct vcpu_svm *svm)
4245{
4246 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4247 return nop_interception(svm);
4248}
4249
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004250enum avic_ipi_failure_cause {
4251 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
4252 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
4253 AVIC_IPI_FAILURE_INVALID_TARGET,
4254 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
4255};
4256
4257static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
4258{
4259 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
4260 u32 icrl = svm->vmcb->control.exit_info_1;
4261 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03004262 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004263 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4264
4265 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
4266
4267 switch (id) {
4268 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
4269 /*
4270 * AVIC hardware handles the generation of
4271 * IPIs when the specified Message Type is Fixed
4272 * (also known as fixed delivery mode) and
4273 * the Trigger Mode is edge-triggered. The hardware
4274 * also supports self and broadcast delivery modes
4275 * specified via the Destination Shorthand(DSH)
4276 * field of the ICRL. Logical and physical APIC ID
4277 * formats are supported. All other IPI types cause
4278 * a #VMEXIT, which needs to emulated.
4279 */
4280 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
4281 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
4282 break;
4283 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
4284 int i;
4285 struct kvm_vcpu *vcpu;
4286 struct kvm *kvm = svm->vcpu.kvm;
4287 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4288
4289 /*
4290 * At this point, we expect that the AVIC HW has already
4291 * set the appropriate IRR bits on the valid target
4292 * vcpus. So, we just need to kick the appropriate vcpu.
4293 */
4294 kvm_for_each_vcpu(i, vcpu, kvm) {
4295 bool m = kvm_apic_match_dest(vcpu, apic,
4296 icrl & KVM_APIC_SHORT_MASK,
4297 GET_APIC_DEST_FIELD(icrh),
4298 icrl & KVM_APIC_DEST_MASK);
4299
4300 if (m && !avic_vcpu_is_running(vcpu))
4301 kvm_vcpu_wake_up(vcpu);
4302 }
4303 break;
4304 }
4305 case AVIC_IPI_FAILURE_INVALID_TARGET:
4306 break;
4307 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
4308 WARN_ONCE(1, "Invalid backing page\n");
4309 break;
4310 default:
4311 pr_err("Unknown IPI interception\n");
4312 }
4313
4314 return 1;
4315}
4316
4317static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
4318{
4319 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4320 int index;
4321 u32 *logical_apic_id_table;
4322 int dlid = GET_APIC_LOGICAL_ID(ldr);
4323
4324 if (!dlid)
4325 return NULL;
4326
4327 if (flat) { /* flat */
4328 index = ffs(dlid) - 1;
4329 if (index > 7)
4330 return NULL;
4331 } else { /* cluster */
4332 int cluster = (dlid & 0xf0) >> 4;
4333 int apic = ffs(dlid & 0x0f) - 1;
4334
4335 if ((apic < 0) || (apic > 7) ||
4336 (cluster >= 0xf))
4337 return NULL;
4338 index = (cluster << 2) + apic;
4339 }
4340
4341 logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page);
4342
4343 return &logical_apic_id_table[index];
4344}
4345
4346static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
4347 bool valid)
4348{
4349 bool flat;
4350 u32 *entry, new_entry;
4351
4352 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4353 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4354 if (!entry)
4355 return -EINVAL;
4356
4357 new_entry = READ_ONCE(*entry);
4358 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4359 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
4360 if (valid)
4361 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4362 else
4363 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4364 WRITE_ONCE(*entry, new_entry);
4365
4366 return 0;
4367}
4368
4369static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4370{
4371 int ret;
4372 struct vcpu_svm *svm = to_svm(vcpu);
4373 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
4374
4375 if (!ldr)
4376 return 1;
4377
4378 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
4379 if (ret && svm->ldr_reg) {
4380 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
4381 svm->ldr_reg = 0;
4382 } else {
4383 svm->ldr_reg = ldr;
4384 }
4385 return ret;
4386}
4387
4388static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4389{
4390 u64 *old, *new;
4391 struct vcpu_svm *svm = to_svm(vcpu);
4392 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
4393 u32 id = (apic_id_reg >> 24) & 0xff;
4394
4395 if (vcpu->vcpu_id == id)
4396 return 0;
4397
4398 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4399 new = avic_get_physical_id_entry(vcpu, id);
4400 if (!new || !old)
4401 return 1;
4402
4403 /* We need to move physical_id_entry to new offset */
4404 *new = *old;
4405 *old = 0ULL;
4406 to_svm(vcpu)->avic_physical_id_cache = new;
4407
4408 /*
4409 * Also update the guest physical APIC ID in the logical
4410 * APIC ID table entry if already setup the LDR.
4411 */
4412 if (svm->ldr_reg)
4413 avic_handle_ldr_update(vcpu);
4414
4415 return 0;
4416}
4417
4418static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
4419{
4420 struct vcpu_svm *svm = to_svm(vcpu);
4421 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4422 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
4423 u32 mod = (dfr >> 28) & 0xf;
4424
4425 /*
4426 * We assume that all local APICs are using the same type.
4427 * If this changes, we need to flush the AVIC logical
4428 * APID id table.
4429 */
4430 if (vm_data->ldr_mode == mod)
4431 return 0;
4432
4433 clear_page(page_address(vm_data->avic_logical_id_table_page));
4434 vm_data->ldr_mode = mod;
4435
4436 if (svm->ldr_reg)
4437 avic_handle_ldr_update(vcpu);
4438 return 0;
4439}
4440
4441static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4442{
4443 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4444 u32 offset = svm->vmcb->control.exit_info_1 &
4445 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4446
4447 switch (offset) {
4448 case APIC_ID:
4449 if (avic_handle_apic_id_update(&svm->vcpu))
4450 return 0;
4451 break;
4452 case APIC_LDR:
4453 if (avic_handle_ldr_update(&svm->vcpu))
4454 return 0;
4455 break;
4456 case APIC_DFR:
4457 avic_handle_dfr_update(&svm->vcpu);
4458 break;
4459 default:
4460 break;
4461 }
4462
4463 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4464
4465 return 1;
4466}
4467
4468static bool is_avic_unaccelerated_access_trap(u32 offset)
4469{
4470 bool ret = false;
4471
4472 switch (offset) {
4473 case APIC_ID:
4474 case APIC_EOI:
4475 case APIC_RRR:
4476 case APIC_LDR:
4477 case APIC_DFR:
4478 case APIC_SPIV:
4479 case APIC_ESR:
4480 case APIC_ICR:
4481 case APIC_LVTT:
4482 case APIC_LVTTHMR:
4483 case APIC_LVTPC:
4484 case APIC_LVT0:
4485 case APIC_LVT1:
4486 case APIC_LVTERR:
4487 case APIC_TMICT:
4488 case APIC_TDCR:
4489 ret = true;
4490 break;
4491 default:
4492 break;
4493 }
4494 return ret;
4495}
4496
4497static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4498{
4499 int ret = 0;
4500 u32 offset = svm->vmcb->control.exit_info_1 &
4501 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4502 u32 vector = svm->vmcb->control.exit_info_2 &
4503 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4504 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4505 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4506 bool trap = is_avic_unaccelerated_access_trap(offset);
4507
4508 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4509 trap, write, vector);
4510 if (trap) {
4511 /* Handling Trap */
4512 WARN_ONCE(!write, "svm: Handling trap read.\n");
4513 ret = avic_unaccel_trap_write(svm);
4514 } else {
4515 /* Handling Fault */
4516 ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
4517 }
4518
4519 return ret;
4520}
4521
Mathias Krause09941fb2012-08-30 01:30:20 +02004522static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01004523 [SVM_EXIT_READ_CR0] = cr_interception,
4524 [SVM_EXIT_READ_CR3] = cr_interception,
4525 [SVM_EXIT_READ_CR4] = cr_interception,
4526 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06004527 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02004528 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01004529 [SVM_EXIT_WRITE_CR3] = cr_interception,
4530 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004531 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01004532 [SVM_EXIT_READ_DR0] = dr_interception,
4533 [SVM_EXIT_READ_DR1] = dr_interception,
4534 [SVM_EXIT_READ_DR2] = dr_interception,
4535 [SVM_EXIT_READ_DR3] = dr_interception,
4536 [SVM_EXIT_READ_DR4] = dr_interception,
4537 [SVM_EXIT_READ_DR5] = dr_interception,
4538 [SVM_EXIT_READ_DR6] = dr_interception,
4539 [SVM_EXIT_READ_DR7] = dr_interception,
4540 [SVM_EXIT_WRITE_DR0] = dr_interception,
4541 [SVM_EXIT_WRITE_DR1] = dr_interception,
4542 [SVM_EXIT_WRITE_DR2] = dr_interception,
4543 [SVM_EXIT_WRITE_DR3] = dr_interception,
4544 [SVM_EXIT_WRITE_DR4] = dr_interception,
4545 [SVM_EXIT_WRITE_DR5] = dr_interception,
4546 [SVM_EXIT_WRITE_DR6] = dr_interception,
4547 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004548 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4549 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004550 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004551 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004552 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01004553 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004554 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02004555 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004556 [SVM_EXIT_SMI] = nop_on_interception,
4557 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08004558 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02004559 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004560 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004561 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02004562 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05004563 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004564 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004565 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02004566 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004567 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004568 [SVM_EXIT_MSR] = msr_interception,
4569 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08004570 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01004571 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02004572 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01004573 [SVM_EXIT_VMLOAD] = vmload_interception,
4574 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01004575 [SVM_EXIT_STGI] = stgi_interception,
4576 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02004577 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06004578 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004579 [SVM_EXIT_MONITOR] = monitor_interception,
4580 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01004581 [SVM_EXIT_XSETBV] = xsetbv_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02004582 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06004583 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004584 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4585 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004586};
4587
Joe Perchesae8cc052011-04-24 22:00:50 -07004588static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02004589{
4590 struct vcpu_svm *svm = to_svm(vcpu);
4591 struct vmcb_control_area *control = &svm->vmcb->control;
4592 struct vmcb_save_area *save = &svm->vmcb->save;
4593
4594 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004595 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4596 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4597 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4598 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4599 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4600 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4601 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
4602 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4603 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4604 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4605 pr_err("%-20s%d\n", "asid:", control->asid);
4606 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4607 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4608 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4609 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4610 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4611 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4612 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4613 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4614 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4615 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4616 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004617 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07004618 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4619 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05004620 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07004621 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004622 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4623 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4624 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004625 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004626 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4627 "es:",
4628 save->es.selector, save->es.attrib,
4629 save->es.limit, save->es.base);
4630 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4631 "cs:",
4632 save->cs.selector, save->cs.attrib,
4633 save->cs.limit, save->cs.base);
4634 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4635 "ss:",
4636 save->ss.selector, save->ss.attrib,
4637 save->ss.limit, save->ss.base);
4638 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4639 "ds:",
4640 save->ds.selector, save->ds.attrib,
4641 save->ds.limit, save->ds.base);
4642 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4643 "fs:",
4644 save->fs.selector, save->fs.attrib,
4645 save->fs.limit, save->fs.base);
4646 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4647 "gs:",
4648 save->gs.selector, save->gs.attrib,
4649 save->gs.limit, save->gs.base);
4650 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4651 "gdtr:",
4652 save->gdtr.selector, save->gdtr.attrib,
4653 save->gdtr.limit, save->gdtr.base);
4654 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4655 "ldtr:",
4656 save->ldtr.selector, save->ldtr.attrib,
4657 save->ldtr.limit, save->ldtr.base);
4658 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4659 "idtr:",
4660 save->idtr.selector, save->idtr.attrib,
4661 save->idtr.limit, save->idtr.base);
4662 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4663 "tr:",
4664 save->tr.selector, save->tr.attrib,
4665 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004666 pr_err("cpl: %d efer: %016llx\n",
4667 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004668 pr_err("%-15s %016llx %-13s %016llx\n",
4669 "cr0:", save->cr0, "cr2:", save->cr2);
4670 pr_err("%-15s %016llx %-13s %016llx\n",
4671 "cr3:", save->cr3, "cr4:", save->cr4);
4672 pr_err("%-15s %016llx %-13s %016llx\n",
4673 "dr6:", save->dr6, "dr7:", save->dr7);
4674 pr_err("%-15s %016llx %-13s %016llx\n",
4675 "rip:", save->rip, "rflags:", save->rflags);
4676 pr_err("%-15s %016llx %-13s %016llx\n",
4677 "rsp:", save->rsp, "rax:", save->rax);
4678 pr_err("%-15s %016llx %-13s %016llx\n",
4679 "star:", save->star, "lstar:", save->lstar);
4680 pr_err("%-15s %016llx %-13s %016llx\n",
4681 "cstar:", save->cstar, "sfmask:", save->sfmask);
4682 pr_err("%-15s %016llx %-13s %016llx\n",
4683 "kernel_gs_base:", save->kernel_gs_base,
4684 "sysenter_cs:", save->sysenter_cs);
4685 pr_err("%-15s %016llx %-13s %016llx\n",
4686 "sysenter_esp:", save->sysenter_esp,
4687 "sysenter_eip:", save->sysenter_eip);
4688 pr_err("%-15s %016llx %-13s %016llx\n",
4689 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4690 pr_err("%-15s %016llx %-13s %016llx\n",
4691 "br_from:", save->br_from, "br_to:", save->br_to);
4692 pr_err("%-15s %016llx %-13s %016llx\n",
4693 "excp_from:", save->last_excp_from,
4694 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004695}
4696
Avi Kivity586f9602010-11-18 13:09:54 +02004697static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4698{
4699 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4700
4701 *info1 = control->exit_info_1;
4702 *info2 = control->exit_info_2;
4703}
4704
Avi Kivity851ba692009-08-24 11:10:17 +03004705static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004706{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004707 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004708 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004709 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004711 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4712
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004713 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004714 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4715 if (npt_enabled)
4716 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004717
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004718 if (unlikely(svm->nested.exit_required)) {
4719 nested_svm_vmexit(svm);
4720 svm->nested.exit_required = false;
4721
4722 return 1;
4723 }
4724
Joerg Roedel20307532010-11-29 17:51:48 +01004725 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004726 int vmexit;
4727
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004728 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4729 svm->vmcb->control.exit_info_1,
4730 svm->vmcb->control.exit_info_2,
4731 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004732 svm->vmcb->control.exit_int_info_err,
4733 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004734
Joerg Roedel410e4d52009-08-07 11:49:44 +02004735 vmexit = nested_svm_exit_special(svm);
4736
4737 if (vmexit == NESTED_EXIT_CONTINUE)
4738 vmexit = nested_svm_exit_handled(svm);
4739
4740 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004741 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004742 }
4743
Joerg Roedela5c38322009-08-07 11:49:32 +02004744 svm_complete_interrupts(svm);
4745
Avi Kivity04d2cc72007-09-10 18:10:54 +03004746 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4747 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4748 kvm_run->fail_entry.hardware_entry_failure_reason
4749 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004750 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4751 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004752 return 0;
4753 }
4754
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004755 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004756 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004757 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4758 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004759 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004760 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004761 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004762 exit_code);
4763
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004764 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004765 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04004766 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03004767 kvm_queue_exception(vcpu, UD_VECTOR);
4768 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004769 }
4770
Avi Kivity851ba692009-08-24 11:10:17 +03004771 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004772}
4773
4774static void reload_tss(struct kvm_vcpu *vcpu)
4775{
4776 int cpu = raw_smp_processor_id();
4777
Tejun Heo0fe1e002009-10-29 22:34:14 +09004778 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4779 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004780 load_TR_desc();
4781}
4782
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004783static void pre_sev_run(struct vcpu_svm *svm, int cpu)
4784{
4785 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4786 int asid = sev_get_asid(svm->vcpu.kvm);
4787
4788 /* Assign the asid allocated with this SEV guest */
4789 svm->vmcb->control.asid = asid;
4790
4791 /*
4792 * Flush guest TLB:
4793 *
4794 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
4795 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
4796 */
4797 if (sd->sev_vmcbs[asid] == svm->vmcb &&
4798 svm->last_cpu == cpu)
4799 return;
4800
4801 svm->last_cpu = cpu;
4802 sd->sev_vmcbs[asid] = svm->vmcb;
4803 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4804 mark_dirty(svm->vmcb, VMCB_ASID);
4805}
4806
Rusty Russelle756fc62007-07-30 20:07:08 +10004807static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004808{
4809 int cpu = raw_smp_processor_id();
4810
Tejun Heo0fe1e002009-10-29 22:34:14 +09004811 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004812
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004813 if (sev_guest(svm->vcpu.kvm))
4814 return pre_sev_run(svm, cpu);
4815
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03004816 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09004817 if (svm->asid_generation != sd->asid_generation)
4818 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004819}
4820
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004821static void svm_inject_nmi(struct kvm_vcpu *vcpu)
4822{
4823 struct vcpu_svm *svm = to_svm(vcpu);
4824
4825 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
4826 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004827 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004828 ++vcpu->stat.nmi_injections;
4829}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004830
Eddie Dong85f455f2007-07-06 12:20:49 +03004831static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004832{
4833 struct vmcb_control_area *control;
4834
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004835 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10004836 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03004837 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004838 control->int_ctl &= ~V_INTR_PRIO_MASK;
4839 control->int_ctl |= V_IRQ_MASK |
4840 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004841 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004842}
4843
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004844static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03004845{
4846 struct vcpu_svm *svm = to_svm(vcpu);
4847
Joerg Roedel2af91942009-08-07 11:49:28 +02004848 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01004849
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03004850 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
4851 ++vcpu->stat.irq_injections;
4852
Alexander Graf219b65d2009-06-15 15:21:25 +02004853 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
4854 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03004855}
4856
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004857static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
4858{
4859 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
4860}
4861
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004862static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
4863{
4864 struct vcpu_svm *svm = to_svm(vcpu);
4865
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004866 if (svm_nested_virtualize_tpr(vcpu) ||
4867 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004868 return;
4869
Radim Krčmář596f3142014-03-11 19:11:18 +01004870 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
4871
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004872 if (irr == -1)
4873 return;
4874
4875 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004876 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004877}
4878
Yang Zhang8d146952013-01-25 10:18:50 +08004879static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
4880{
4881 return;
4882}
4883
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05004884static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004885{
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05004886 return avic && irqchip_split(vcpu->kvm);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004887}
4888
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004889static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
4890{
4891}
4892
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02004893static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004894{
4895}
4896
4897/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03004898static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4899{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004900 struct vcpu_svm *svm = to_svm(vcpu);
4901 struct vmcb *vmcb = svm->vmcb;
4902
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05004903 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004904 return;
4905
4906 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
4907 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004908}
4909
Andrey Smetanin63086302015-11-10 15:36:32 +03004910static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004911{
4912 return;
4913}
4914
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004915static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
4916{
4917 kvm_lapic_set_irr(vec, vcpu->arch.apic);
4918 smp_mb__after_atomic();
4919
4920 if (avic_vcpu_is_running(vcpu))
4921 wrmsrl(SVM_AVIC_DOORBELL,
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05004922 kvm_cpu_get_apicid(vcpu->cpu));
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004923 else
4924 kvm_vcpu_wake_up(vcpu);
4925}
4926
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004927static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4928{
4929 unsigned long flags;
4930 struct amd_svm_iommu_ir *cur;
4931
4932 spin_lock_irqsave(&svm->ir_list_lock, flags);
4933 list_for_each_entry(cur, &svm->ir_list, node) {
4934 if (cur->data != pi->ir_data)
4935 continue;
4936 list_del(&cur->node);
4937 kfree(cur);
4938 break;
4939 }
4940 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4941}
4942
4943static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4944{
4945 int ret = 0;
4946 unsigned long flags;
4947 struct amd_svm_iommu_ir *ir;
4948
4949 /**
4950 * In some cases, the existing irte is updaed and re-set,
4951 * so we need to check here if it's already been * added
4952 * to the ir_list.
4953 */
4954 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
4955 struct kvm *kvm = svm->vcpu.kvm;
4956 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
4957 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
4958 struct vcpu_svm *prev_svm;
4959
4960 if (!prev_vcpu) {
4961 ret = -EINVAL;
4962 goto out;
4963 }
4964
4965 prev_svm = to_svm(prev_vcpu);
4966 svm_ir_list_del(prev_svm, pi);
4967 }
4968
4969 /**
4970 * Allocating new amd_iommu_pi_data, which will get
4971 * add to the per-vcpu ir_list.
4972 */
4973 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
4974 if (!ir) {
4975 ret = -ENOMEM;
4976 goto out;
4977 }
4978 ir->data = pi->ir_data;
4979
4980 spin_lock_irqsave(&svm->ir_list_lock, flags);
4981 list_add(&ir->node, &svm->ir_list);
4982 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4983out:
4984 return ret;
4985}
4986
4987/**
4988 * Note:
4989 * The HW cannot support posting multicast/broadcast
4990 * interrupts to a vCPU. So, we still use legacy interrupt
4991 * remapping for these kind of interrupts.
4992 *
4993 * For lowest-priority interrupts, we only support
4994 * those with single CPU as the destination, e.g. user
4995 * configures the interrupts via /proc/irq or uses
4996 * irqbalance to make the interrupts single-CPU.
4997 */
4998static int
4999get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
5000 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
5001{
5002 struct kvm_lapic_irq irq;
5003 struct kvm_vcpu *vcpu = NULL;
5004
5005 kvm_set_msi_irq(kvm, e, &irq);
5006
5007 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
5008 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
5009 __func__, irq.vector);
5010 return -1;
5011 }
5012
5013 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
5014 irq.vector);
5015 *svm = to_svm(vcpu);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005016 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005017 vcpu_info->vector = irq.vector;
5018
5019 return 0;
5020}
5021
5022/*
5023 * svm_update_pi_irte - set IRTE for Posted-Interrupts
5024 *
5025 * @kvm: kvm
5026 * @host_irq: host irq of the interrupt
5027 * @guest_irq: gsi of the interrupt
5028 * @set: set or unset PI
5029 * returns 0 on success, < 0 on failure
5030 */
5031static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
5032 uint32_t guest_irq, bool set)
5033{
5034 struct kvm_kernel_irq_routing_entry *e;
5035 struct kvm_irq_routing_table *irq_rt;
5036 int idx, ret = -EINVAL;
5037
5038 if (!kvm_arch_has_assigned_device(kvm) ||
5039 !irq_remapping_cap(IRQ_POSTING_CAP))
5040 return 0;
5041
5042 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
5043 __func__, host_irq, guest_irq, set);
5044
5045 idx = srcu_read_lock(&kvm->irq_srcu);
5046 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
5047 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
5048
5049 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
5050 struct vcpu_data vcpu_info;
5051 struct vcpu_svm *svm = NULL;
5052
5053 if (e->type != KVM_IRQ_ROUTING_MSI)
5054 continue;
5055
5056 /**
5057 * Here, we setup with legacy mode in the following cases:
5058 * 1. When cannot target interrupt to a specific vcpu.
5059 * 2. Unsetting posted interrupt.
5060 * 3. APIC virtialization is disabled for the vcpu.
5061 */
5062 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
5063 kvm_vcpu_apicv_active(&svm->vcpu)) {
5064 struct amd_iommu_pi_data pi;
5065
5066 /* Try to enable guest_mode in IRTE */
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005067 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
5068 AVIC_HPA_MASK);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005069 pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id,
5070 svm->vcpu.vcpu_id);
5071 pi.is_guest_mode = true;
5072 pi.vcpu_data = &vcpu_info;
5073 ret = irq_set_vcpu_affinity(host_irq, &pi);
5074
5075 /**
5076 * Here, we successfully setting up vcpu affinity in
5077 * IOMMU guest mode. Now, we need to store the posted
5078 * interrupt information in a per-vcpu ir_list so that
5079 * we can reference to them directly when we update vcpu
5080 * scheduling information in IOMMU irte.
5081 */
5082 if (!ret && pi.is_guest_mode)
5083 svm_ir_list_add(svm, &pi);
5084 } else {
5085 /* Use legacy mode in IRTE */
5086 struct amd_iommu_pi_data pi;
5087
5088 /**
5089 * Here, pi is used to:
5090 * - Tell IOMMU to use legacy mode for this interrupt.
5091 * - Retrieve ga_tag of prior interrupt remapping data.
5092 */
5093 pi.is_guest_mode = false;
5094 ret = irq_set_vcpu_affinity(host_irq, &pi);
5095
5096 /**
5097 * Check if the posted interrupt was previously
5098 * setup with the guest_mode by checking if the ga_tag
5099 * was cached. If so, we need to clean up the per-vcpu
5100 * ir_list.
5101 */
5102 if (!ret && pi.prev_ga_tag) {
5103 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
5104 struct kvm_vcpu *vcpu;
5105
5106 vcpu = kvm_get_vcpu_by_id(kvm, id);
5107 if (vcpu)
5108 svm_ir_list_del(to_svm(vcpu), &pi);
5109 }
5110 }
5111
5112 if (!ret && svm) {
5113 trace_kvm_pi_irte_update(svm->vcpu.vcpu_id,
5114 host_irq, e->gsi,
5115 vcpu_info.vector,
5116 vcpu_info.pi_desc_addr, set);
5117 }
5118
5119 if (ret < 0) {
5120 pr_err("%s: failed to update PI IRTE\n", __func__);
5121 goto out;
5122 }
5123 }
5124
5125 ret = 0;
5126out:
5127 srcu_read_unlock(&kvm->irq_srcu, idx);
5128 return ret;
5129}
5130
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005131static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005132{
5133 struct vcpu_svm *svm = to_svm(vcpu);
5134 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02005135 int ret;
5136 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
5137 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
5138 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
5139
5140 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005141}
5142
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005143static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
5144{
5145 struct vcpu_svm *svm = to_svm(vcpu);
5146
5147 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
5148}
5149
5150static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5151{
5152 struct vcpu_svm *svm = to_svm(vcpu);
5153
5154 if (masked) {
5155 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005156 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005157 } else {
5158 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005159 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005160 }
5161}
5162
Gleb Natapov78646122009-03-23 12:12:11 +02005163static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
5164{
5165 struct vcpu_svm *svm = to_svm(vcpu);
5166 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005167 int ret;
5168
5169 if (!gif_set(svm) ||
5170 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
5171 return 0;
5172
Avi Kivityf6e78472010-08-02 15:30:20 +03005173 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005174
Joerg Roedel20307532010-11-29 17:51:48 +01005175 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005176 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
5177
5178 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02005179}
5180
Jan Kiszkac9a79532014-03-07 20:03:15 +01005181static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03005182{
Alexander Graf219b65d2009-06-15 15:21:25 +02005183 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02005184
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005185 if (kvm_vcpu_apicv_active(vcpu))
5186 return;
5187
Joerg Roedele0231712010-02-24 18:59:10 +01005188 /*
5189 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
5190 * 1, because that's a separate STGI/VMRUN intercept. The next time we
5191 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005192 * we'll get the vintr intercept. However, if the vGIF feature is
5193 * enabled, the STGI interception will not occur. Enable the irq
5194 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01005195 */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005196 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02005197 svm_set_vintr(svm);
5198 svm_inject_irq(svm, 0x0);
5199 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005200}
5201
Jan Kiszkac9a79532014-03-07 20:03:15 +01005202static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005203{
Avi Kivity04d2cc72007-09-10 18:10:54 +03005204 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005205
Gleb Natapov44c11432009-05-11 13:35:52 +03005206 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
5207 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01005208 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03005209
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005210 if (!gif_set(svm)) {
5211 if (vgif_enabled(svm))
5212 set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005213 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005214 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005215
5216 if (svm->nested.exit_required)
5217 return; /* we're not going to run the guest yet */
5218
Joerg Roedele0231712010-02-24 18:59:10 +01005219 /*
5220 * Something prevents NMI from been injected. Single step over possible
5221 * problem (IRET or exception injection or interrupt shadow)
5222 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02005223 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02005224 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03005225 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03005226}
5227
Izik Eiduscbc94022007-10-25 00:29:55 +02005228static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
5229{
5230 return 0;
5231}
5232
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005233static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Avi Kivityd9e368d2007-06-07 19:18:30 +03005234{
Joerg Roedel38e5e922010-12-03 15:25:16 +01005235 struct vcpu_svm *svm = to_svm(vcpu);
5236
5237 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
5238 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5239 else
5240 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03005241}
5242
Avi Kivity04d2cc72007-09-10 18:10:54 +03005243static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
5244{
5245}
5246
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005247static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
5248{
5249 struct vcpu_svm *svm = to_svm(vcpu);
5250
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005251 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005252 return;
5253
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005254 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005255 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03005256 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005257 }
5258}
5259
Joerg Roedel649d6862008-04-16 16:51:15 +02005260static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
5261{
5262 struct vcpu_svm *svm = to_svm(vcpu);
5263 u64 cr8;
5264
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005265 if (svm_nested_virtualize_tpr(vcpu) ||
5266 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005267 return;
5268
Joerg Roedel649d6862008-04-16 16:51:15 +02005269 cr8 = kvm_get_cr8(vcpu);
5270 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
5271 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
5272}
5273
Gleb Natapov9222be12009-04-23 17:14:37 +03005274static void svm_complete_interrupts(struct vcpu_svm *svm)
5275{
5276 u8 vector;
5277 int type;
5278 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01005279 unsigned int3_injected = svm->int3_injected;
5280
5281 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005282
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02005283 /*
5284 * If we've made progress since setting HF_IRET_MASK, we've
5285 * executed an IRET and can allow NMI injection.
5286 */
5287 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
5288 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03005289 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03005290 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5291 }
Gleb Natapov44c11432009-05-11 13:35:52 +03005292
Gleb Natapov9222be12009-04-23 17:14:37 +03005293 svm->vcpu.arch.nmi_injected = false;
5294 kvm_clear_exception_queue(&svm->vcpu);
5295 kvm_clear_interrupt_queue(&svm->vcpu);
5296
5297 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
5298 return;
5299
Avi Kivity3842d132010-07-27 12:30:24 +03005300 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5301
Gleb Natapov9222be12009-04-23 17:14:37 +03005302 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
5303 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
5304
5305 switch (type) {
5306 case SVM_EXITINTINFO_TYPE_NMI:
5307 svm->vcpu.arch.nmi_injected = true;
5308 break;
5309 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01005310 /*
5311 * In case of software exceptions, do not reinject the vector,
5312 * but re-execute the instruction instead. Rewind RIP first
5313 * if we emulated INT3 before.
5314 */
5315 if (kvm_exception_is_soft(vector)) {
5316 if (vector == BP_VECTOR && int3_injected &&
5317 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
5318 kvm_rip_write(&svm->vcpu,
5319 kvm_rip_read(&svm->vcpu) -
5320 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02005321 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01005322 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005323 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
5324 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005325 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03005326
5327 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005328 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03005329 break;
5330 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005331 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03005332 break;
5333 default:
5334 break;
5335 }
5336}
5337
Avi Kivityb463a6f2010-07-20 15:06:17 +03005338static void svm_cancel_injection(struct kvm_vcpu *vcpu)
5339{
5340 struct vcpu_svm *svm = to_svm(vcpu);
5341 struct vmcb_control_area *control = &svm->vmcb->control;
5342
5343 control->exit_int_info = control->event_inj;
5344 control->exit_int_info_err = control->event_inj_err;
5345 control->event_inj = 0;
5346 svm_complete_interrupts(svm);
5347}
5348
Avi Kivity851ba692009-08-24 11:10:17 +03005349static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005350{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005351 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03005352
Joerg Roedel2041a062010-04-22 12:33:08 +02005353 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5354 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5355 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5356
Joerg Roedelcd3ff652009-10-09 16:08:26 +02005357 /*
5358 * A vmexit emulation is required before the vcpu can be executed
5359 * again.
5360 */
5361 if (unlikely(svm->nested.exit_required))
5362 return;
5363
Ladi Proseka12713c2017-06-21 09:07:00 +02005364 /*
5365 * Disable singlestep if we're injecting an interrupt/exception.
5366 * We don't want our modified rflags to be pushed on the stack where
5367 * we might not be able to easily reset them if we disabled NMI
5368 * singlestep later.
5369 */
5370 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5371 /*
5372 * Event injection happens before external interrupts cause a
5373 * vmexit and interrupts are disabled here, so smp_send_reschedule
5374 * is enough to force an immediate vmexit.
5375 */
5376 disable_nmi_singlestep(svm);
5377 smp_send_reschedule(vcpu->cpu);
5378 }
5379
Rusty Russelle756fc62007-07-30 20:07:08 +10005380 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005381
Joerg Roedel649d6862008-04-16 16:51:15 +02005382 sync_lapic_to_cr8(vcpu);
5383
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02005384 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005385
Avi Kivity04d2cc72007-09-10 18:10:54 +03005386 clgi();
5387
5388 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08005389
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005390 /*
5391 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
5392 * it's non-zero. Since vmentry is serialising on affected CPUs, there
5393 * is no need to worry about the conditional branch over the wrmsr
5394 * being speculatively taken.
5395 */
5396 if (svm->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005397 native_wrmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005398
Avi Kivity6aa8b732006-12-10 02:21:36 -08005399 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03005400 "push %%" _ASM_BP "; \n\t"
5401 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5402 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5403 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5404 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5405 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5406 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005407#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005408 "mov %c[r8](%[svm]), %%r8 \n\t"
5409 "mov %c[r9](%[svm]), %%r9 \n\t"
5410 "mov %c[r10](%[svm]), %%r10 \n\t"
5411 "mov %c[r11](%[svm]), %%r11 \n\t"
5412 "mov %c[r12](%[svm]), %%r12 \n\t"
5413 "mov %c[r13](%[svm]), %%r13 \n\t"
5414 "mov %c[r14](%[svm]), %%r14 \n\t"
5415 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005416#endif
5417
Avi Kivity6aa8b732006-12-10 02:21:36 -08005418 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03005419 "push %%" _ASM_AX " \n\t"
5420 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03005421 __ex(SVM_VMLOAD) "\n\t"
5422 __ex(SVM_VMRUN) "\n\t"
5423 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005424 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005425
5426 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03005427 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5428 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5429 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5430 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5431 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5432 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005433#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005434 "mov %%r8, %c[r8](%[svm]) \n\t"
5435 "mov %%r9, %c[r9](%[svm]) \n\t"
5436 "mov %%r10, %c[r10](%[svm]) \n\t"
5437 "mov %%r11, %c[r11](%[svm]) \n\t"
5438 "mov %%r12, %c[r12](%[svm]) \n\t"
5439 "mov %%r13, %c[r13](%[svm]) \n\t"
5440 "mov %%r14, %c[r14](%[svm]) \n\t"
5441 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005442#endif
Jim Mattson0cb5b302018-01-03 14:31:38 -08005443 /*
5444 * Clear host registers marked as clobbered to prevent
5445 * speculative use.
5446 */
5447 "xor %%" _ASM_BX ", %%" _ASM_BX " \n\t"
5448 "xor %%" _ASM_CX ", %%" _ASM_CX " \n\t"
5449 "xor %%" _ASM_DX ", %%" _ASM_DX " \n\t"
5450 "xor %%" _ASM_SI ", %%" _ASM_SI " \n\t"
5451 "xor %%" _ASM_DI ", %%" _ASM_DI " \n\t"
5452#ifdef CONFIG_X86_64
5453 "xor %%r8, %%r8 \n\t"
5454 "xor %%r9, %%r9 \n\t"
5455 "xor %%r10, %%r10 \n\t"
5456 "xor %%r11, %%r11 \n\t"
5457 "xor %%r12, %%r12 \n\t"
5458 "xor %%r13, %%r13 \n\t"
5459 "xor %%r14, %%r14 \n\t"
5460 "xor %%r15, %%r15 \n\t"
5461#endif
Avi Kivity74547662012-09-16 15:10:59 +03005462 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08005463 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005464 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08005465 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005466 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5467 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5468 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5469 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5470 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5471 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005472#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005473 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5474 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5475 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5476 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5477 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5478 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5479 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5480 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005481#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02005482 : "cc", "memory"
5483#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03005484 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005485 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03005486#else
5487 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005488#endif
5489 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08005490
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005491 /*
5492 * We do not use IBRS in the kernel. If this vCPU has used the
5493 * SPEC_CTRL MSR it may have left it on; save the value and
5494 * turn it off. This is much more efficient than blindly adding
5495 * it to the atomic save/restore list. Especially as the former
5496 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
5497 *
5498 * For non-nested case:
5499 * If the L01 MSR bitmap does not intercept the MSR, then we need to
5500 * save it.
5501 *
5502 * For nested case:
5503 * If the L02 MSR bitmap does not intercept the MSR, then we need to
5504 * save it.
5505 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01005506 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005507 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005508
5509 if (svm->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01005510 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005511
David Woodhouse117cc7a2018-01-12 11:11:27 +00005512 /* Eliminate branch target predictions from guest mode */
5513 vmexit_fill_RSB();
5514
Avi Kivity82ca2d12010-10-21 12:20:34 +02005515#ifdef CONFIG_X86_64
5516 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5517#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02005518 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02005519#ifndef CONFIG_X86_32_LAZY_GS
5520 loadsegment(gs, svm->host.gs);
5521#endif
Avi Kivity9581d442010-10-19 16:46:55 +02005522#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005523
5524 reload_tss(vcpu);
5525
Avi Kivity56ba47d2007-11-07 17:14:18 +02005526 local_irq_disable();
5527
Avi Kivity13c34e02010-10-21 12:20:31 +02005528 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5529 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5530 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5531 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5532
Joerg Roedel3781c012011-01-14 16:45:02 +01005533 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5534 kvm_before_handle_nmi(&svm->vcpu);
5535
5536 stgi();
5537
5538 /* Any pending NMI will happen here */
5539
5540 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5541 kvm_after_handle_nmi(&svm->vcpu);
5542
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005543 sync_cr8_to_lapic(vcpu);
5544
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005545 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005546
Joerg Roedel38e5e922010-12-03 15:25:16 +01005547 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5548
Gleb Natapov631bc482010-10-14 11:22:52 +02005549 /* if exit due to PF check for async PF */
5550 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005551 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
Gleb Natapov631bc482010-10-14 11:22:52 +02005552
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005553 if (npt_enabled) {
5554 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5555 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5556 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02005557
5558 /*
5559 * We need to handle MC intercepts here before the vcpu has a chance to
5560 * change the physical cpu
5561 */
5562 if (unlikely(svm->vmcb->control.exit_code ==
5563 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5564 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01005565
5566 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005567}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005568STACK_FRAME_NON_STANDARD(svm_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005569
Avi Kivity6aa8b732006-12-10 02:21:36 -08005570static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5571{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005572 struct vcpu_svm *svm = to_svm(vcpu);
5573
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005574 svm->vmcb->save.cr3 = __sme_set(root);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005575 mark_dirty(svm->vmcb, VMCB_CR);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005576 svm_flush_tlb(vcpu, true);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005577}
5578
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005579static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5580{
5581 struct vcpu_svm *svm = to_svm(vcpu);
5582
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005583 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01005584 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005585
5586 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02005587 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005588 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005589
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005590 svm_flush_tlb(vcpu, true);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005591}
5592
Avi Kivity6aa8b732006-12-10 02:21:36 -08005593static int is_disabled(void)
5594{
Joerg Roedel6031a612007-06-22 12:29:50 +03005595 u64 vm_cr;
5596
5597 rdmsrl(MSR_VM_CR, vm_cr);
5598 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5599 return 1;
5600
Avi Kivity6aa8b732006-12-10 02:21:36 -08005601 return 0;
5602}
5603
Ingo Molnar102d8322007-02-19 14:37:47 +02005604static void
5605svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5606{
5607 /*
5608 * Patch in the VMMCALL instruction:
5609 */
5610 hypercall[0] = 0x0f;
5611 hypercall[1] = 0x01;
5612 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02005613}
5614
Yang, Sheng002c7f72007-07-31 14:23:01 +03005615static void svm_check_processor_compat(void *rtn)
5616{
5617 *(int *)rtn = 0;
5618}
5619
Avi Kivity774ead32007-12-26 13:57:04 +02005620static bool svm_cpu_has_accelerated_tpr(void)
5621{
5622 return false;
5623}
5624
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005625static bool svm_has_high_real_mode_segbase(void)
5626{
5627 return true;
5628}
5629
Paolo Bonzinifc07e762015-10-01 13:20:22 +02005630static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5631{
5632 return 0;
5633}
5634
Sheng Yang0e851882009-12-18 16:48:46 +08005635static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5636{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005637 struct vcpu_svm *svm = to_svm(vcpu);
5638
5639 /* Update nrips enabled cache */
Radim Krčmářd6321d42017-08-05 00:12:49 +02005640 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005641
5642 if (!kvm_vcpu_apicv_active(vcpu))
5643 return;
5644
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02005645 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08005646}
5647
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005648static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5649{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005650 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005651 case 0x1:
5652 if (avic)
5653 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5654 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02005655 case 0x80000001:
5656 if (nested)
5657 entry->ecx |= (1 << 2); /* Set SVM bit */
5658 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005659 case 0x8000000A:
5660 entry->eax = 1; /* SVM revision 1 */
5661 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5662 ASID emulation to nested SVM */
5663 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02005664 entry->edx = 0; /* Per default do not support any
5665 additional features */
5666
5667 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02005668 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02005669 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005670
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005671 /* Support NPT for the guest if enabled */
5672 if (npt_enabled)
5673 entry->edx |= SVM_FEATURE_NPT;
5674
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005675 break;
Brijesh Singh8765d752017-12-04 10:57:25 -06005676 case 0x8000001F:
5677 /* Support memory encryption cpuid if host supports it */
5678 if (boot_cpu_has(X86_FEATURE_SEV))
5679 cpuid(0x8000001f, &entry->eax, &entry->ebx,
5680 &entry->ecx, &entry->edx);
5681
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005682 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005683}
5684
Sheng Yang17cc3932010-01-05 19:02:27 +08005685static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005686{
Sheng Yang17cc3932010-01-05 19:02:27 +08005687 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005688}
5689
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005690static bool svm_rdtscp_supported(void)
5691{
Paolo Bonzini46896c72015-11-12 14:49:16 +01005692 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005693}
5694
Mao, Junjiead756a12012-07-02 01:18:48 +00005695static bool svm_invpcid_supported(void)
5696{
5697 return false;
5698}
5699
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005700static bool svm_mpx_supported(void)
5701{
5702 return false;
5703}
5704
Wanpeng Li55412b22014-12-02 19:21:30 +08005705static bool svm_xsaves_supported(void)
5706{
5707 return false;
5708}
5709
Paolo Bonzini66336ca2016-07-12 10:36:41 +02005710static bool svm_umip_emulated(void)
5711{
5712 return false;
5713}
5714
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005715static bool svm_has_wbinvd_exit(void)
5716{
5717 return true;
5718}
5719
Joerg Roedel80612522011-04-04 12:39:33 +02005720#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005721 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005722#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005723 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005724#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005725 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005726
Mathias Krause09941fb2012-08-30 01:30:20 +02005727static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005728 u32 exit_code;
5729 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005730} x86_intercept_map[] = {
5731 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
5732 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
5733 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
5734 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
5735 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02005736 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
5737 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02005738 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
5739 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
5740 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
5741 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
5742 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
5743 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
5744 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
5745 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02005746 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
5747 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
5748 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
5749 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
5750 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
5751 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
5752 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
5753 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005754 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
5755 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
5756 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02005757 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
5758 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
5759 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
5760 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
5761 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
5762 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
5763 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
5764 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
5765 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02005766 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
5767 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
5768 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
5769 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
5770 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
5771 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
5772 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02005773 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
5774 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
5775 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
5776 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005777};
5778
Joerg Roedel80612522011-04-04 12:39:33 +02005779#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005780#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005781#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005782
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005783static int svm_check_intercept(struct kvm_vcpu *vcpu,
5784 struct x86_instruction_info *info,
5785 enum x86_intercept_stage stage)
5786{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005787 struct vcpu_svm *svm = to_svm(vcpu);
5788 int vmexit, ret = X86EMUL_CONTINUE;
5789 struct __x86_intercept icpt_info;
5790 struct vmcb *vmcb = svm->vmcb;
5791
5792 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
5793 goto out;
5794
5795 icpt_info = x86_intercept_map[info->intercept];
5796
Avi Kivity40e19b52011-04-21 12:35:41 +03005797 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005798 goto out;
5799
5800 switch (icpt_info.exit_code) {
5801 case SVM_EXIT_READ_CR0:
5802 if (info->intercept == x86_intercept_cr_read)
5803 icpt_info.exit_code += info->modrm_reg;
5804 break;
5805 case SVM_EXIT_WRITE_CR0: {
5806 unsigned long cr0, val;
5807 u64 intercept;
5808
5809 if (info->intercept == x86_intercept_cr_write)
5810 icpt_info.exit_code += info->modrm_reg;
5811
Jan Kiszka62baf442014-06-29 21:55:53 +02005812 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
5813 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005814 break;
5815
5816 intercept = svm->nested.intercept;
5817
5818 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
5819 break;
5820
5821 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
5822 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
5823
5824 if (info->intercept == x86_intercept_lmsw) {
5825 cr0 &= 0xfUL;
5826 val &= 0xfUL;
5827 /* lmsw can't clear PE - catch this here */
5828 if (cr0 & X86_CR0_PE)
5829 val |= X86_CR0_PE;
5830 }
5831
5832 if (cr0 ^ val)
5833 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
5834
5835 break;
5836 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02005837 case SVM_EXIT_READ_DR0:
5838 case SVM_EXIT_WRITE_DR0:
5839 icpt_info.exit_code += info->modrm_reg;
5840 break;
Joerg Roedel80612522011-04-04 12:39:33 +02005841 case SVM_EXIT_MSR:
5842 if (info->intercept == x86_intercept_wrmsr)
5843 vmcb->control.exit_info_1 = 1;
5844 else
5845 vmcb->control.exit_info_1 = 0;
5846 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02005847 case SVM_EXIT_PAUSE:
5848 /*
5849 * We get this for NOP only, but pause
5850 * is rep not, check this here
5851 */
5852 if (info->rep_prefix != REPE_PREFIX)
5853 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02005854 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02005855 case SVM_EXIT_IOIO: {
5856 u64 exit_info;
5857 u32 bytes;
5858
Joerg Roedelf6511932011-04-04 12:39:35 +02005859 if (info->intercept == x86_intercept_in ||
5860 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005861 exit_info = ((info->src_val & 0xffff) << 16) |
5862 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02005863 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02005864 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005865 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02005866 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02005867 }
5868
5869 if (info->intercept == x86_intercept_outs ||
5870 info->intercept == x86_intercept_ins)
5871 exit_info |= SVM_IOIO_STR_MASK;
5872
5873 if (info->rep_prefix)
5874 exit_info |= SVM_IOIO_REP_MASK;
5875
5876 bytes = min(bytes, 4u);
5877
5878 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
5879
5880 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
5881
5882 vmcb->control.exit_info_1 = exit_info;
5883 vmcb->control.exit_info_2 = info->next_rip;
5884
5885 break;
5886 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005887 default:
5888 break;
5889 }
5890
Bandan Dasf1047652015-06-11 02:05:33 -04005891 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
5892 if (static_cpu_has(X86_FEATURE_NRIPS))
5893 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005894 vmcb->control.exit_code = icpt_info.exit_code;
5895 vmexit = nested_svm_exit_handled(svm);
5896
5897 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
5898 : X86EMUL_CONTINUE;
5899
5900out:
5901 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005902}
5903
Yang Zhanga547c6d2013-04-11 19:25:10 +08005904static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
5905{
5906 local_irq_enable();
Paolo Bonzinif2485b32016-06-15 15:23:11 +02005907 /*
5908 * We must have an instruction with interrupts enabled, so
5909 * the timer interrupt isn't delayed by the interrupt shadow.
5910 */
5911 asm("nop");
5912 local_irq_disable();
Yang Zhanga547c6d2013-04-11 19:25:10 +08005913}
5914
Radim Krčmářae97a3b2014-08-21 18:08:06 +02005915static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
5916{
5917}
5918
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05005919static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
5920{
5921 if (avic_handle_apic_id_update(vcpu) != 0)
5922 return;
5923 if (avic_handle_dfr_update(vcpu) != 0)
5924 return;
5925 avic_handle_ldr_update(vcpu);
5926}
5927
Borislav Petkov74f16902017-03-26 23:51:24 +02005928static void svm_setup_mce(struct kvm_vcpu *vcpu)
5929{
5930 /* [63:9] are reserved. */
5931 vcpu->arch.mcg_cap &= 0x1ff;
5932}
5933
Ladi Prosek72d7b372017-10-11 16:54:41 +02005934static int svm_smi_allowed(struct kvm_vcpu *vcpu)
5935{
Ladi Prosek05cade72017-10-11 16:54:45 +02005936 struct vcpu_svm *svm = to_svm(vcpu);
5937
5938 /* Per APM Vol.2 15.22.2 "Response to SMI" */
5939 if (!gif_set(svm))
5940 return 0;
5941
5942 if (is_guest_mode(&svm->vcpu) &&
5943 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
5944 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
5945 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
5946 svm->nested.exit_required = true;
5947 return 0;
5948 }
5949
Ladi Prosek72d7b372017-10-11 16:54:41 +02005950 return 1;
5951}
5952
Ladi Prosek0234bf82017-10-11 16:54:40 +02005953static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
5954{
Ladi Prosek05cade72017-10-11 16:54:45 +02005955 struct vcpu_svm *svm = to_svm(vcpu);
5956 int ret;
5957
5958 if (is_guest_mode(vcpu)) {
5959 /* FED8h - SVM Guest */
5960 put_smstate(u64, smstate, 0x7ed8, 1);
5961 /* FEE0h - SVM Guest VMCB Physical Address */
5962 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
5963
5964 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5965 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5966 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5967
5968 ret = nested_svm_vmexit(svm);
5969 if (ret)
5970 return ret;
5971 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02005972 return 0;
5973}
5974
5975static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
5976{
Ladi Prosek05cade72017-10-11 16:54:45 +02005977 struct vcpu_svm *svm = to_svm(vcpu);
5978 struct vmcb *nested_vmcb;
5979 struct page *page;
5980 struct {
5981 u64 guest;
5982 u64 vmcb;
5983 } svm_state_save;
5984 int ret;
5985
5986 ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save,
5987 sizeof(svm_state_save));
5988 if (ret)
5989 return ret;
5990
5991 if (svm_state_save.guest) {
5992 vcpu->arch.hflags &= ~HF_SMM_MASK;
5993 nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page);
5994 if (nested_vmcb)
5995 enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page);
5996 else
5997 ret = 1;
5998 vcpu->arch.hflags |= HF_SMM_MASK;
5999 }
6000 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02006001}
6002
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006003static int enable_smi_window(struct kvm_vcpu *vcpu)
6004{
6005 struct vcpu_svm *svm = to_svm(vcpu);
6006
6007 if (!gif_set(svm)) {
6008 if (vgif_enabled(svm))
6009 set_intercept(svm, INTERCEPT_STGI);
6010 /* STGI will cause a vm exit */
6011 return 1;
6012 }
6013 return 0;
6014}
6015
Brijesh Singh1654efc2017-12-04 10:57:34 -06006016static int sev_asid_new(void)
6017{
6018 int pos;
6019
6020 /*
6021 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
6022 */
6023 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
6024 if (pos >= max_sev_asid)
6025 return -EBUSY;
6026
6027 set_bit(pos, sev_asid_bitmap);
6028 return pos + 1;
6029}
6030
6031static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
6032{
6033 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6034 int asid, ret;
6035
6036 ret = -EBUSY;
6037 asid = sev_asid_new();
6038 if (asid < 0)
6039 return ret;
6040
6041 ret = sev_platform_init(&argp->error);
6042 if (ret)
6043 goto e_free;
6044
6045 sev->active = true;
6046 sev->asid = asid;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006047 INIT_LIST_HEAD(&sev->regions_list);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006048
6049 return 0;
6050
6051e_free:
6052 __sev_asid_free(asid);
6053 return ret;
6054}
6055
Brijesh Singh59414c92017-12-04 10:57:35 -06006056static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
6057{
6058 struct sev_data_activate *data;
6059 int asid = sev_get_asid(kvm);
6060 int ret;
6061
6062 wbinvd_on_all_cpus();
6063
6064 ret = sev_guest_df_flush(error);
6065 if (ret)
6066 return ret;
6067
6068 data = kzalloc(sizeof(*data), GFP_KERNEL);
6069 if (!data)
6070 return -ENOMEM;
6071
6072 /* activate ASID on the given handle */
6073 data->handle = handle;
6074 data->asid = asid;
6075 ret = sev_guest_activate(data, error);
6076 kfree(data);
6077
6078 return ret;
6079}
6080
Brijesh Singh89c50582017-12-04 10:57:35 -06006081static int __sev_issue_cmd(int fd, int id, void *data, int *error)
Brijesh Singh59414c92017-12-04 10:57:35 -06006082{
6083 struct fd f;
6084 int ret;
6085
6086 f = fdget(fd);
6087 if (!f.file)
6088 return -EBADF;
6089
6090 ret = sev_issue_cmd_external_user(f.file, id, data, error);
6091
6092 fdput(f);
6093 return ret;
6094}
6095
Brijesh Singh89c50582017-12-04 10:57:35 -06006096static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
6097{
6098 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6099
6100 return __sev_issue_cmd(sev->fd, id, data, error);
6101}
6102
Brijesh Singh59414c92017-12-04 10:57:35 -06006103static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
6104{
6105 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6106 struct sev_data_launch_start *start;
6107 struct kvm_sev_launch_start params;
6108 void *dh_blob, *session_blob;
6109 int *error = &argp->error;
6110 int ret;
6111
6112 if (!sev_guest(kvm))
6113 return -ENOTTY;
6114
6115 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6116 return -EFAULT;
6117
6118 start = kzalloc(sizeof(*start), GFP_KERNEL);
6119 if (!start)
6120 return -ENOMEM;
6121
6122 dh_blob = NULL;
6123 if (params.dh_uaddr) {
6124 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
6125 if (IS_ERR(dh_blob)) {
6126 ret = PTR_ERR(dh_blob);
6127 goto e_free;
6128 }
6129
6130 start->dh_cert_address = __sme_set(__pa(dh_blob));
6131 start->dh_cert_len = params.dh_len;
6132 }
6133
6134 session_blob = NULL;
6135 if (params.session_uaddr) {
6136 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
6137 if (IS_ERR(session_blob)) {
6138 ret = PTR_ERR(session_blob);
6139 goto e_free_dh;
6140 }
6141
6142 start->session_address = __sme_set(__pa(session_blob));
6143 start->session_len = params.session_len;
6144 }
6145
6146 start->handle = params.handle;
6147 start->policy = params.policy;
6148
6149 /* create memory encryption context */
Brijesh Singh89c50582017-12-04 10:57:35 -06006150 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
Brijesh Singh59414c92017-12-04 10:57:35 -06006151 if (ret)
6152 goto e_free_session;
6153
6154 /* Bind ASID to this guest */
6155 ret = sev_bind_asid(kvm, start->handle, error);
6156 if (ret)
6157 goto e_free_session;
6158
6159 /* return handle to userspace */
6160 params.handle = start->handle;
6161 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
6162 sev_unbind_asid(kvm, start->handle);
6163 ret = -EFAULT;
6164 goto e_free_session;
6165 }
6166
6167 sev->handle = start->handle;
6168 sev->fd = argp->sev_fd;
6169
6170e_free_session:
6171 kfree(session_blob);
6172e_free_dh:
6173 kfree(dh_blob);
6174e_free:
6175 kfree(start);
6176 return ret;
6177}
6178
Brijesh Singh89c50582017-12-04 10:57:35 -06006179static int get_num_contig_pages(int idx, struct page **inpages,
6180 unsigned long npages)
6181{
6182 unsigned long paddr, next_paddr;
6183 int i = idx + 1, pages = 1;
6184
6185 /* find the number of contiguous pages starting from idx */
6186 paddr = __sme_page_pa(inpages[idx]);
6187 while (i < npages) {
6188 next_paddr = __sme_page_pa(inpages[i++]);
6189 if ((paddr + PAGE_SIZE) == next_paddr) {
6190 pages++;
6191 paddr = next_paddr;
6192 continue;
6193 }
6194 break;
6195 }
6196
6197 return pages;
6198}
6199
6200static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
6201{
6202 unsigned long vaddr, vaddr_end, next_vaddr, npages, size;
6203 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6204 struct kvm_sev_launch_update_data params;
6205 struct sev_data_launch_update_data *data;
6206 struct page **inpages;
6207 int i, ret, pages;
6208
6209 if (!sev_guest(kvm))
6210 return -ENOTTY;
6211
6212 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6213 return -EFAULT;
6214
6215 data = kzalloc(sizeof(*data), GFP_KERNEL);
6216 if (!data)
6217 return -ENOMEM;
6218
6219 vaddr = params.uaddr;
6220 size = params.len;
6221 vaddr_end = vaddr + size;
6222
6223 /* Lock the user memory. */
6224 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
6225 if (!inpages) {
6226 ret = -ENOMEM;
6227 goto e_free;
6228 }
6229
6230 /*
6231 * The LAUNCH_UPDATE command will perform in-place encryption of the
6232 * memory content (i.e it will write the same memory region with C=1).
6233 * It's possible that the cache may contain the data with C=0, i.e.,
6234 * unencrypted so invalidate it first.
6235 */
6236 sev_clflush_pages(inpages, npages);
6237
6238 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
6239 int offset, len;
6240
6241 /*
6242 * If the user buffer is not page-aligned, calculate the offset
6243 * within the page.
6244 */
6245 offset = vaddr & (PAGE_SIZE - 1);
6246
6247 /* Calculate the number of pages that can be encrypted in one go. */
6248 pages = get_num_contig_pages(i, inpages, npages);
6249
6250 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
6251
6252 data->handle = sev->handle;
6253 data->len = len;
6254 data->address = __sme_page_pa(inpages[i]) + offset;
6255 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
6256 if (ret)
6257 goto e_unpin;
6258
6259 size -= len;
6260 next_vaddr = vaddr + len;
6261 }
6262
6263e_unpin:
6264 /* content of memory is updated, mark pages dirty */
6265 for (i = 0; i < npages; i++) {
6266 set_page_dirty_lock(inpages[i]);
6267 mark_page_accessed(inpages[i]);
6268 }
6269 /* unlock the user pages */
6270 sev_unpin_memory(kvm, inpages, npages);
6271e_free:
6272 kfree(data);
6273 return ret;
6274}
6275
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006276static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
6277{
Brijesh Singh3e233382018-02-23 12:36:50 -06006278 void __user *measure = (void __user *)(uintptr_t)argp->data;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006279 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6280 struct sev_data_launch_measure *data;
6281 struct kvm_sev_launch_measure params;
Brijesh Singh3e233382018-02-23 12:36:50 -06006282 void __user *p = NULL;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006283 void *blob = NULL;
6284 int ret;
6285
6286 if (!sev_guest(kvm))
6287 return -ENOTTY;
6288
Brijesh Singh3e233382018-02-23 12:36:50 -06006289 if (copy_from_user(&params, measure, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006290 return -EFAULT;
6291
6292 data = kzalloc(sizeof(*data), GFP_KERNEL);
6293 if (!data)
6294 return -ENOMEM;
6295
6296 /* User wants to query the blob length */
6297 if (!params.len)
6298 goto cmd;
6299
Brijesh Singh3e233382018-02-23 12:36:50 -06006300 p = (void __user *)(uintptr_t)params.uaddr;
6301 if (p) {
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006302 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
6303 ret = -EINVAL;
6304 goto e_free;
6305 }
6306
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006307 ret = -ENOMEM;
6308 blob = kmalloc(params.len, GFP_KERNEL);
6309 if (!blob)
6310 goto e_free;
6311
6312 data->address = __psp_pa(blob);
6313 data->len = params.len;
6314 }
6315
6316cmd:
6317 data->handle = sev->handle;
6318 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
6319
6320 /*
6321 * If we query the session length, FW responded with expected data.
6322 */
6323 if (!params.len)
6324 goto done;
6325
6326 if (ret)
6327 goto e_free_blob;
6328
6329 if (blob) {
Brijesh Singh3e233382018-02-23 12:36:50 -06006330 if (copy_to_user(p, blob, params.len))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006331 ret = -EFAULT;
6332 }
6333
6334done:
6335 params.len = data->len;
Brijesh Singh3e233382018-02-23 12:36:50 -06006336 if (copy_to_user(measure, &params, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006337 ret = -EFAULT;
6338e_free_blob:
6339 kfree(blob);
6340e_free:
6341 kfree(data);
6342 return ret;
6343}
6344
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006345static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
6346{
6347 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6348 struct sev_data_launch_finish *data;
6349 int ret;
6350
6351 if (!sev_guest(kvm))
6352 return -ENOTTY;
6353
6354 data = kzalloc(sizeof(*data), GFP_KERNEL);
6355 if (!data)
6356 return -ENOMEM;
6357
6358 data->handle = sev->handle;
6359 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
6360
6361 kfree(data);
6362 return ret;
6363}
6364
Brijesh Singh255d9e72017-12-04 10:57:37 -06006365static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
6366{
6367 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6368 struct kvm_sev_guest_status params;
6369 struct sev_data_guest_status *data;
6370 int ret;
6371
6372 if (!sev_guest(kvm))
6373 return -ENOTTY;
6374
6375 data = kzalloc(sizeof(*data), GFP_KERNEL);
6376 if (!data)
6377 return -ENOMEM;
6378
6379 data->handle = sev->handle;
6380 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
6381 if (ret)
6382 goto e_free;
6383
6384 params.policy = data->policy;
6385 params.state = data->state;
6386 params.handle = data->handle;
6387
6388 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6389 ret = -EFAULT;
6390e_free:
6391 kfree(data);
6392 return ret;
6393}
6394
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006395static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
6396 unsigned long dst, int size,
6397 int *error, bool enc)
6398{
6399 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6400 struct sev_data_dbg *data;
6401 int ret;
6402
6403 data = kzalloc(sizeof(*data), GFP_KERNEL);
6404 if (!data)
6405 return -ENOMEM;
6406
6407 data->handle = sev->handle;
6408 data->dst_addr = dst;
6409 data->src_addr = src;
6410 data->len = size;
6411
6412 ret = sev_issue_cmd(kvm,
6413 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
6414 data, error);
6415 kfree(data);
6416 return ret;
6417}
6418
6419static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
6420 unsigned long dst_paddr, int sz, int *err)
6421{
6422 int offset;
6423
6424 /*
6425 * Its safe to read more than we are asked, caller should ensure that
6426 * destination has enough space.
6427 */
6428 src_paddr = round_down(src_paddr, 16);
6429 offset = src_paddr & 15;
6430 sz = round_up(sz + offset, 16);
6431
6432 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
6433}
6434
6435static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
6436 unsigned long __user dst_uaddr,
6437 unsigned long dst_paddr,
6438 int size, int *err)
6439{
6440 struct page *tpage = NULL;
6441 int ret, offset;
6442
6443 /* if inputs are not 16-byte then use intermediate buffer */
6444 if (!IS_ALIGNED(dst_paddr, 16) ||
6445 !IS_ALIGNED(paddr, 16) ||
6446 !IS_ALIGNED(size, 16)) {
6447 tpage = (void *)alloc_page(GFP_KERNEL);
6448 if (!tpage)
6449 return -ENOMEM;
6450
6451 dst_paddr = __sme_page_pa(tpage);
6452 }
6453
6454 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
6455 if (ret)
6456 goto e_free;
6457
6458 if (tpage) {
6459 offset = paddr & 15;
6460 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
6461 page_address(tpage) + offset, size))
6462 ret = -EFAULT;
6463 }
6464
6465e_free:
6466 if (tpage)
6467 __free_page(tpage);
6468
6469 return ret;
6470}
6471
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006472static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
6473 unsigned long __user vaddr,
6474 unsigned long dst_paddr,
6475 unsigned long __user dst_vaddr,
6476 int size, int *error)
6477{
6478 struct page *src_tpage = NULL;
6479 struct page *dst_tpage = NULL;
6480 int ret, len = size;
6481
6482 /* If source buffer is not aligned then use an intermediate buffer */
6483 if (!IS_ALIGNED(vaddr, 16)) {
6484 src_tpage = alloc_page(GFP_KERNEL);
6485 if (!src_tpage)
6486 return -ENOMEM;
6487
6488 if (copy_from_user(page_address(src_tpage),
6489 (void __user *)(uintptr_t)vaddr, size)) {
6490 __free_page(src_tpage);
6491 return -EFAULT;
6492 }
6493
6494 paddr = __sme_page_pa(src_tpage);
6495 }
6496
6497 /*
6498 * If destination buffer or length is not aligned then do read-modify-write:
6499 * - decrypt destination in an intermediate buffer
6500 * - copy the source buffer in an intermediate buffer
6501 * - use the intermediate buffer as source buffer
6502 */
6503 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
6504 int dst_offset;
6505
6506 dst_tpage = alloc_page(GFP_KERNEL);
6507 if (!dst_tpage) {
6508 ret = -ENOMEM;
6509 goto e_free;
6510 }
6511
6512 ret = __sev_dbg_decrypt(kvm, dst_paddr,
6513 __sme_page_pa(dst_tpage), size, error);
6514 if (ret)
6515 goto e_free;
6516
6517 /*
6518 * If source is kernel buffer then use memcpy() otherwise
6519 * copy_from_user().
6520 */
6521 dst_offset = dst_paddr & 15;
6522
6523 if (src_tpage)
6524 memcpy(page_address(dst_tpage) + dst_offset,
6525 page_address(src_tpage), size);
6526 else {
6527 if (copy_from_user(page_address(dst_tpage) + dst_offset,
6528 (void __user *)(uintptr_t)vaddr, size)) {
6529 ret = -EFAULT;
6530 goto e_free;
6531 }
6532 }
6533
6534 paddr = __sme_page_pa(dst_tpage);
6535 dst_paddr = round_down(dst_paddr, 16);
6536 len = round_up(size, 16);
6537 }
6538
6539 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
6540
6541e_free:
6542 if (src_tpage)
6543 __free_page(src_tpage);
6544 if (dst_tpage)
6545 __free_page(dst_tpage);
6546 return ret;
6547}
6548
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006549static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
6550{
6551 unsigned long vaddr, vaddr_end, next_vaddr;
6552 unsigned long dst_vaddr, dst_vaddr_end;
6553 struct page **src_p, **dst_p;
6554 struct kvm_sev_dbg debug;
6555 unsigned long n;
6556 int ret, size;
6557
6558 if (!sev_guest(kvm))
6559 return -ENOTTY;
6560
6561 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
6562 return -EFAULT;
6563
6564 vaddr = debug.src_uaddr;
6565 size = debug.len;
6566 vaddr_end = vaddr + size;
6567 dst_vaddr = debug.dst_uaddr;
6568 dst_vaddr_end = dst_vaddr + size;
6569
6570 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
6571 int len, s_off, d_off;
6572
6573 /* lock userspace source and destination page */
6574 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
6575 if (!src_p)
6576 return -EFAULT;
6577
6578 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
6579 if (!dst_p) {
6580 sev_unpin_memory(kvm, src_p, n);
6581 return -EFAULT;
6582 }
6583
6584 /*
6585 * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
6586 * memory content (i.e it will write the same memory region with C=1).
6587 * It's possible that the cache may contain the data with C=0, i.e.,
6588 * unencrypted so invalidate it first.
6589 */
6590 sev_clflush_pages(src_p, 1);
6591 sev_clflush_pages(dst_p, 1);
6592
6593 /*
6594 * Since user buffer may not be page aligned, calculate the
6595 * offset within the page.
6596 */
6597 s_off = vaddr & ~PAGE_MASK;
6598 d_off = dst_vaddr & ~PAGE_MASK;
6599 len = min_t(size_t, (PAGE_SIZE - s_off), size);
6600
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006601 if (dec)
6602 ret = __sev_dbg_decrypt_user(kvm,
6603 __sme_page_pa(src_p[0]) + s_off,
6604 dst_vaddr,
6605 __sme_page_pa(dst_p[0]) + d_off,
6606 len, &argp->error);
6607 else
6608 ret = __sev_dbg_encrypt_user(kvm,
6609 __sme_page_pa(src_p[0]) + s_off,
6610 vaddr,
6611 __sme_page_pa(dst_p[0]) + d_off,
6612 dst_vaddr,
6613 len, &argp->error);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006614
6615 sev_unpin_memory(kvm, src_p, 1);
6616 sev_unpin_memory(kvm, dst_p, 1);
6617
6618 if (ret)
6619 goto err;
6620
6621 next_vaddr = vaddr + len;
6622 dst_vaddr = dst_vaddr + len;
6623 size -= len;
6624 }
6625err:
6626 return ret;
6627}
6628
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006629static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
6630{
6631 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6632 struct sev_data_launch_secret *data;
6633 struct kvm_sev_launch_secret params;
6634 struct page **pages;
6635 void *blob, *hdr;
6636 unsigned long n;
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006637 int ret, offset;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006638
6639 if (!sev_guest(kvm))
6640 return -ENOTTY;
6641
6642 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6643 return -EFAULT;
6644
6645 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
6646 if (!pages)
6647 return -ENOMEM;
6648
6649 /*
6650 * The secret must be copied into contiguous memory region, lets verify
6651 * that userspace memory pages are contiguous before we issue command.
6652 */
6653 if (get_num_contig_pages(0, pages, n) != n) {
6654 ret = -EINVAL;
6655 goto e_unpin_memory;
6656 }
6657
6658 ret = -ENOMEM;
6659 data = kzalloc(sizeof(*data), GFP_KERNEL);
6660 if (!data)
6661 goto e_unpin_memory;
6662
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006663 offset = params.guest_uaddr & (PAGE_SIZE - 1);
6664 data->guest_address = __sme_page_pa(pages[0]) + offset;
6665 data->guest_len = params.guest_len;
6666
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006667 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
6668 if (IS_ERR(blob)) {
6669 ret = PTR_ERR(blob);
6670 goto e_free;
6671 }
6672
6673 data->trans_address = __psp_pa(blob);
6674 data->trans_len = params.trans_len;
6675
6676 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
6677 if (IS_ERR(hdr)) {
6678 ret = PTR_ERR(hdr);
6679 goto e_free_blob;
6680 }
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006681 data->hdr_address = __psp_pa(hdr);
6682 data->hdr_len = params.hdr_len;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006683
6684 data->handle = sev->handle;
6685 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
6686
6687 kfree(hdr);
6688
6689e_free_blob:
6690 kfree(blob);
6691e_free:
6692 kfree(data);
6693e_unpin_memory:
6694 sev_unpin_memory(kvm, pages, n);
6695 return ret;
6696}
6697
Brijesh Singh1654efc2017-12-04 10:57:34 -06006698static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
6699{
6700 struct kvm_sev_cmd sev_cmd;
6701 int r;
6702
6703 if (!svm_sev_enabled())
6704 return -ENOTTY;
6705
6706 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
6707 return -EFAULT;
6708
6709 mutex_lock(&kvm->lock);
6710
6711 switch (sev_cmd.id) {
6712 case KVM_SEV_INIT:
6713 r = sev_guest_init(kvm, &sev_cmd);
6714 break;
Brijesh Singh59414c92017-12-04 10:57:35 -06006715 case KVM_SEV_LAUNCH_START:
6716 r = sev_launch_start(kvm, &sev_cmd);
6717 break;
Brijesh Singh89c50582017-12-04 10:57:35 -06006718 case KVM_SEV_LAUNCH_UPDATE_DATA:
6719 r = sev_launch_update_data(kvm, &sev_cmd);
6720 break;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006721 case KVM_SEV_LAUNCH_MEASURE:
6722 r = sev_launch_measure(kvm, &sev_cmd);
6723 break;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006724 case KVM_SEV_LAUNCH_FINISH:
6725 r = sev_launch_finish(kvm, &sev_cmd);
6726 break;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006727 case KVM_SEV_GUEST_STATUS:
6728 r = sev_guest_status(kvm, &sev_cmd);
6729 break;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006730 case KVM_SEV_DBG_DECRYPT:
6731 r = sev_dbg_crypt(kvm, &sev_cmd, true);
6732 break;
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006733 case KVM_SEV_DBG_ENCRYPT:
6734 r = sev_dbg_crypt(kvm, &sev_cmd, false);
6735 break;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006736 case KVM_SEV_LAUNCH_SECRET:
6737 r = sev_launch_secret(kvm, &sev_cmd);
6738 break;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006739 default:
6740 r = -EINVAL;
6741 goto out;
6742 }
6743
6744 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
6745 r = -EFAULT;
6746
6747out:
6748 mutex_unlock(&kvm->lock);
6749 return r;
6750}
6751
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006752static int svm_register_enc_region(struct kvm *kvm,
6753 struct kvm_enc_region *range)
6754{
6755 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6756 struct enc_region *region;
6757 int ret = 0;
6758
6759 if (!sev_guest(kvm))
6760 return -ENOTTY;
6761
6762 region = kzalloc(sizeof(*region), GFP_KERNEL);
6763 if (!region)
6764 return -ENOMEM;
6765
6766 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
6767 if (!region->pages) {
6768 ret = -ENOMEM;
6769 goto e_free;
6770 }
6771
6772 /*
6773 * The guest may change the memory encryption attribute from C=0 -> C=1
6774 * or vice versa for this memory range. Lets make sure caches are
6775 * flushed to ensure that guest data gets written into memory with
6776 * correct C-bit.
6777 */
6778 sev_clflush_pages(region->pages, region->npages);
6779
6780 region->uaddr = range->addr;
6781 region->size = range->size;
6782
6783 mutex_lock(&kvm->lock);
6784 list_add_tail(&region->list, &sev->regions_list);
6785 mutex_unlock(&kvm->lock);
6786
6787 return ret;
6788
6789e_free:
6790 kfree(region);
6791 return ret;
6792}
6793
6794static struct enc_region *
6795find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
6796{
6797 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6798 struct list_head *head = &sev->regions_list;
6799 struct enc_region *i;
6800
6801 list_for_each_entry(i, head, list) {
6802 if (i->uaddr == range->addr &&
6803 i->size == range->size)
6804 return i;
6805 }
6806
6807 return NULL;
6808}
6809
6810
6811static int svm_unregister_enc_region(struct kvm *kvm,
6812 struct kvm_enc_region *range)
6813{
6814 struct enc_region *region;
6815 int ret;
6816
6817 mutex_lock(&kvm->lock);
6818
6819 if (!sev_guest(kvm)) {
6820 ret = -ENOTTY;
6821 goto failed;
6822 }
6823
6824 region = find_enc_region(kvm, range);
6825 if (!region) {
6826 ret = -EINVAL;
6827 goto failed;
6828 }
6829
6830 __unregister_enc_region_locked(kvm, region);
6831
6832 mutex_unlock(&kvm->lock);
6833 return 0;
6834
6835failed:
6836 mutex_unlock(&kvm->lock);
6837 return ret;
6838}
6839
Kees Cook404f6aa2016-08-08 16:29:06 -07006840static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006841 .cpu_has_kvm_support = has_svm,
6842 .disabled_by_bios = is_disabled,
6843 .hardware_setup = svm_hardware_setup,
6844 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03006845 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006846 .hardware_enable = svm_hardware_enable,
6847 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02006848 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006849 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006850
6851 .vcpu_create = svm_create_vcpu,
6852 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03006853 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006854
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006855 .vm_init = avic_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06006856 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006857
Avi Kivity04d2cc72007-09-10 18:10:54 +03006858 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006859 .vcpu_load = svm_vcpu_load,
6860 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05006861 .vcpu_blocking = svm_vcpu_blocking,
6862 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006863
Paolo Bonzinia96036b2015-11-10 11:55:36 +01006864 .update_bp_intercept = update_bp_intercept,
Tom Lendacky801e4592018-02-21 13:39:51 -06006865 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006866 .get_msr = svm_get_msr,
6867 .set_msr = svm_set_msr,
6868 .get_segment_base = svm_get_segment_base,
6869 .get_segment = svm_get_segment,
6870 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02006871 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10006872 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02006873 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02006874 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03006875 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006876 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006877 .set_cr3 = svm_set_cr3,
6878 .set_cr4 = svm_set_cr4,
6879 .set_efer = svm_set_efer,
6880 .get_idt = svm_get_idt,
6881 .set_idt = svm_set_idt,
6882 .get_gdt = svm_get_gdt,
6883 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006884 .get_dr6 = svm_get_dr6,
6885 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03006886 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01006887 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03006888 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006889 .get_rflags = svm_get_rflags,
6890 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08006891
Avi Kivity6aa8b732006-12-10 02:21:36 -08006892 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006893
Avi Kivity6aa8b732006-12-10 02:21:36 -08006894 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03006895 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006896 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04006897 .set_interrupt_shadow = svm_set_interrupt_shadow,
6898 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02006899 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03006900 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006901 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02006902 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006903 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02006904 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006905 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006906 .get_nmi_mask = svm_get_nmi_mask,
6907 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006908 .enable_nmi_window = enable_nmi_window,
6909 .enable_irq_window = enable_irq_window,
6910 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08006911 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03006912 .get_enable_apicv = svm_get_enable_apicv,
6913 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08006914 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006915 .hwapic_irr_update = svm_hwapic_irr_update,
6916 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02006917 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05006918 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02006919
6920 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08006921 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006922 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006923
Avi Kivity586f9602010-11-18 13:09:54 +02006924 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02006925
Sheng Yang17cc3932010-01-05 19:02:27 +08006926 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08006927
6928 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006929
6930 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00006931 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006932 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08006933 .xsaves_supported = svm_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +02006934 .umip_emulated = svm_umip_emulated,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006935
6936 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006937
6938 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10006939
6940 .write_tsc_offset = svm_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02006941
6942 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006943
6944 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08006945 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006946
6947 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02006948
6949 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05006950 .deliver_posted_interrupt = svm_deliver_avic_intr,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05006951 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02006952 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006953
Ladi Prosek72d7b372017-10-11 16:54:41 +02006954 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006955 .pre_enter_smm = svm_pre_enter_smm,
6956 .pre_leave_smm = svm_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006957 .enable_smi_window = enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06006958
6959 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006960 .mem_enc_reg_region = svm_register_enc_region,
6961 .mem_enc_unreg_region = svm_unregister_enc_region,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006962};
6963
6964static int __init svm_init(void)
6965{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08006966 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03006967 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006968}
6969
6970static void __exit svm_exit(void)
6971{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08006972 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08006973}
6974
6975module_init(svm_init)
6976module_exit(svm_exit)