blob: f874798f82099230d9f52f7e20e4ea8aac73297c [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * AMD SVM support
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02007 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08008 *
9 * Authors:
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050017
18#define pr_fmt(fmt) "SVM: " fmt
19
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
21
Eddie Dong85f455f2007-07-06 12:20:49 +030022#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080023#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030024#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020025#include "x86.h"
Julian Stecklina66f7b722012-12-05 15:26:19 +010026#include "cpuid.h"
Wei Huang25462f72015-06-19 15:45:05 +020027#include "pmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040028
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/module.h>
Josh Triplettae759542012-03-28 11:32:28 -070030#include <linux/mod_devicetable.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020031#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <linux/vmalloc.h>
33#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040034#include <linux/sched.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040035#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -050037#include <linux/amd-iommu.h>
38#include <linux/hashtable.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050039#include <linux/frame.h>
Brijesh Singhe9df0942017-12-04 10:57:33 -060040#include <linux/psp-sev.h>
Brijesh Singh1654efc2017-12-04 10:57:34 -060041#include <linux/file.h>
Brijesh Singh89c50582017-12-04 10:57:35 -060042#include <linux/pagemap.h>
43#include <linux/swap.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050045#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010046#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020047#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040048#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020050#include <asm/kvm_para.h>
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -050051#include <asm/irq_remapping.h>
David Woodhouse117cc7a2018-01-12 11:11:27 +000052#include <asm/nospec-branch.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080053
Eduardo Habkost63d11422008-11-17 19:03:20 -020054#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030055#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020056
Avi Kivity4ecac3f2008-05-13 13:23:38 +030057#define __ex(x) __kvm_handle_fault_on_reboot(x)
58
Avi Kivity6aa8b732006-12-10 02:21:36 -080059MODULE_AUTHOR("Qumranet");
60MODULE_LICENSE("GPL");
61
Josh Triplettae759542012-03-28 11:32:28 -070062static const struct x86_cpu_id svm_cpu_id[] = {
63 X86_FEATURE_MATCH(X86_FEATURE_SVM),
64 {}
65};
66MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
67
Avi Kivity6aa8b732006-12-10 02:21:36 -080068#define IOPM_ALLOC_ORDER 2
69#define MSRPM_ALLOC_ORDER 1
70
Avi Kivity6aa8b732006-12-10 02:21:36 -080071#define SEG_TYPE_LDT 2
72#define SEG_TYPE_BUSY_TSS16 3
73
Andre Przywara6bc31bd2010-04-11 23:07:28 +020074#define SVM_FEATURE_NPT (1 << 0)
75#define SVM_FEATURE_LBRV (1 << 1)
76#define SVM_FEATURE_SVML (1 << 2)
77#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010078#define SVM_FEATURE_TSC_RATE (1 << 4)
79#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
80#define SVM_FEATURE_FLUSH_ASID (1 << 6)
81#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020082#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030083
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -050084#define SVM_AVIC_DOORBELL 0xc001011b
85
Joerg Roedel410e4d52009-08-07 11:49:44 +020086#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
87#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
88#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
89
Joerg Roedel24e09cb2008-02-13 18:58:47 +010090#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
91
Joerg Roedelfbc0db72011-03-25 09:44:46 +010092#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010093#define TSC_RATIO_MIN 0x0000000000000001ULL
94#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010095
Dan Carpenter5446a972016-05-23 13:20:10 +030096#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050097
98/*
99 * 0xff is broadcast, so the max index allowed for physical APIC ID
100 * table is 0xfe. APIC IDs above 0xff are reserved.
101 */
102#define AVIC_MAX_PHYSICAL_ID_COUNT 255
103
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500104#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
105#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
106#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
107
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500108/* AVIC GATAG is encoded using VM and VCPU IDs */
109#define AVIC_VCPU_ID_BITS 8
110#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
111
112#define AVIC_VM_ID_BITS 24
113#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
114#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
115
116#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
117 (y & AVIC_VCPU_ID_MASK))
118#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
119#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
120
Joerg Roedel67ec6602010-05-17 14:43:35 +0200121static bool erratum_383_found __read_mostly;
122
Avi Kivity6c8166a2009-05-31 18:15:37 +0300123static const u32 host_save_user_msrs[] = {
124#ifdef CONFIG_X86_64
125 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
126 MSR_FS_BASE,
127#endif
128 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Paolo Bonzini46896c72015-11-12 14:49:16 +0100129 MSR_TSC_AUX,
Avi Kivity6c8166a2009-05-31 18:15:37 +0300130};
131
132#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
133
134struct kvm_vcpu;
135
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200136struct nested_state {
137 struct vmcb *hsave;
138 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +0100139 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200140 u64 vmcb;
141
142 /* These are the merged vectors */
143 u32 *msrpm;
144
145 /* gpa pointers to the real vectors */
146 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100147 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200148
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200149 /* A VMEXIT is required but not yet emulated */
150 bool exit_required;
151
Joerg Roedelaad42c62009-08-07 11:49:34 +0200152 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100153 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100154 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200155 u32 intercept_exceptions;
156 u64 intercept;
157
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200158 /* Nested Paging related state */
159 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200160};
161
Joerg Roedel323c3d82010-03-01 15:34:37 +0100162#define MSRPM_OFFSETS 16
163static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
164
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500165/*
166 * Set osvw_len to higher value when updated Revision Guides
167 * are published and we know what the new status bits are
168 */
169static uint64_t osvw_len = 4, osvw_status;
170
Avi Kivity6c8166a2009-05-31 18:15:37 +0300171struct vcpu_svm {
172 struct kvm_vcpu vcpu;
173 struct vmcb *vmcb;
174 unsigned long vmcb_pa;
175 struct svm_cpu_data *svm_data;
176 uint64_t asid_generation;
177 uint64_t sysenter_esp;
178 uint64_t sysenter_eip;
Paolo Bonzini46896c72015-11-12 14:49:16 +0100179 uint64_t tsc_aux;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300180
Tom Lendackyd1d93fa2018-02-24 00:18:20 +0100181 u64 msr_decfg;
182
Avi Kivity6c8166a2009-05-31 18:15:37 +0300183 u64 next_rip;
184
185 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200186 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200187 u16 fs;
188 u16 gs;
189 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200190 u64 gs_base;
191 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300192
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100193 u64 spec_ctrl;
194
Avi Kivity6c8166a2009-05-31 18:15:37 +0300195 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300196
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200197 ulong nmi_iret_rip;
198
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200199 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200200
201 bool nmi_singlestep;
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200202 u64 nmi_singlestep_guest_rflags;
Jan Kiszka66b71382010-02-23 17:47:56 +0100203
204 unsigned int3_injected;
205 unsigned long int3_rip;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100206
Joerg Roedel6092d3d2015-10-14 15:10:54 +0200207 /* cached guest cpuid flags for faster access */
208 bool nrips_enabled : 1;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500209
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500210 u32 ldr_reg;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500211 struct page *avic_backing_page;
212 u64 *avic_physical_id_cache;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -0500213 bool avic_is_running;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500214
215 /*
216 * Per-vcpu list of struct amd_svm_iommu_ir:
217 * This is used mainly to store interrupt remapping information used
218 * when update the vcpu affinity. This avoids the need to scan for
219 * IRTE and try to match ga_tag in the IOMMU driver.
220 */
221 struct list_head ir_list;
222 spinlock_t ir_list_lock;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600223
224 /* which host CPU was used for running this vcpu */
225 unsigned int last_cpu;
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -0500226};
227
228/*
229 * This is a wrapper of struct amd_iommu_ir_data.
230 */
231struct amd_svm_iommu_ir {
232 struct list_head node; /* Used by SVM for per-vcpu ir_list */
233 void *data; /* Storing pointer to struct amd_ir_data */
Avi Kivity6c8166a2009-05-31 18:15:37 +0300234};
235
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500236#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
237#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
238
239#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
240#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
241#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
242#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
243
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100244static DEFINE_PER_CPU(u64, current_tsc_ratio);
245#define TSC_RATIO_DEFAULT 0x0100000000ULL
246
Joerg Roedel455716f2010-03-01 15:34:35 +0100247#define MSR_INVALID 0xffffffffU
248
Mathias Krause09941fb2012-08-30 01:30:20 +0200249static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100250 u32 index; /* Index of the MSR */
251 bool always; /* True if intercept is always on */
252} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400253 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100254 { .index = MSR_IA32_SYSENTER_CS, .always = true },
255#ifdef CONFIG_X86_64
256 { .index = MSR_GS_BASE, .always = true },
257 { .index = MSR_FS_BASE, .always = true },
258 { .index = MSR_KERNEL_GS_BASE, .always = true },
259 { .index = MSR_LSTAR, .always = true },
260 { .index = MSR_CSTAR, .always = true },
261 { .index = MSR_SYSCALL_MASK, .always = true },
262#endif
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100263 { .index = MSR_IA32_SPEC_CTRL, .always = false },
Ashok Raj15d45072018-02-01 22:59:43 +0100264 { .index = MSR_IA32_PRED_CMD, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100265 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
266 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
267 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
268 { .index = MSR_IA32_LASTINTTOIP, .always = false },
269 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270};
271
272/* enable NPT for AMD64 and X86 with PAE */
273#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
274static bool npt_enabled = true;
275#else
Joerg Roedele0231712010-02-24 18:59:10 +0100276static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277#endif
278
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100279/* allow nested paging (virtualized MMU) for all guests */
280static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800281module_param(npt, int, S_IRUGO);
282
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100283/* allow nested virtualization in KVM/SVM */
284static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285module_param(nested, int, S_IRUGO);
286
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500287/* enable / disable AVIC */
288static int avic;
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500289#ifdef CONFIG_X86_LOCAL_APIC
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500290module_param(avic, int, S_IRUGO);
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500291#endif
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500292
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -0500293/* enable/disable Virtual VMLOAD VMSAVE */
294static int vls = true;
295module_param(vls, int, 0444);
296
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500297/* enable/disable Virtual GIF */
298static int vgif = true;
299module_param(vgif, int, 0444);
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500300
Brijesh Singhe9df0942017-12-04 10:57:33 -0600301/* enable/disable SEV support */
302static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
303module_param(sev, int, 0444);
304
Brijesh Singh7607b712018-02-19 10:14:44 -0600305static u8 rsm_ins_bytes[] = "\x0f\xaa";
306
Paolo Bonzini79a80592015-09-21 07:46:55 +0200307static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -0800308static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa);
Joerg Roedela5c38322009-08-07 11:49:32 +0200309static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310
Joerg Roedel410e4d52009-08-07 11:49:44 +0200311static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100312static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800313static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800314static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
315 bool has_error_code, u32 error_code);
316
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100317enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100318 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
319 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100320 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100321 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100322 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100323 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100324 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100325 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100326 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100327 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100328 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100329 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500330 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
331 * AVIC PHYSICAL_TABLE pointer,
332 * AVIC LOGICAL_TABLE pointer
333 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100334 VMCB_DIRTY_MAX,
335};
336
Joerg Roedel0574dec2010-12-03 11:45:58 +0100337/* TPR and CR2 are always written before VMRUN */
338#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100339
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500340#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
341
Brijesh Singhed3cd232017-12-04 10:57:32 -0600342static unsigned int max_sev_asid;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600343static unsigned int min_sev_asid;
344static unsigned long *sev_asid_bitmap;
Brijesh Singh89c50582017-12-04 10:57:35 -0600345#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
Brijesh Singh1654efc2017-12-04 10:57:34 -0600346
Brijesh Singh1e80fdc2017-12-04 10:57:38 -0600347struct enc_region {
348 struct list_head list;
349 unsigned long npages;
350 struct page **pages;
351 unsigned long uaddr;
352 unsigned long size;
353};
354
Brijesh Singh1654efc2017-12-04 10:57:34 -0600355static inline bool svm_sev_enabled(void)
356{
357 return max_sev_asid;
358}
359
360static inline bool sev_guest(struct kvm *kvm)
361{
362 struct kvm_sev_info *sev = &kvm->arch.sev_info;
363
364 return sev->active;
365}
Brijesh Singhed3cd232017-12-04 10:57:32 -0600366
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600367static inline int sev_get_asid(struct kvm *kvm)
368{
369 struct kvm_sev_info *sev = &kvm->arch.sev_info;
370
371 return sev->asid;
372}
373
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100374static inline void mark_all_dirty(struct vmcb *vmcb)
375{
376 vmcb->control.clean = 0;
377}
378
379static inline void mark_all_clean(struct vmcb *vmcb)
380{
381 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
382 & ~VMCB_ALWAYS_DIRTY_MASK;
383}
384
385static inline void mark_dirty(struct vmcb *vmcb, int bit)
386{
387 vmcb->control.clean &= ~(1 << bit);
388}
389
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
391{
392 return container_of(vcpu, struct vcpu_svm, vcpu);
393}
394
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500395static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
396{
397 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
398 mark_dirty(svm->vmcb, VMCB_AVIC);
399}
400
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -0500401static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
402{
403 struct vcpu_svm *svm = to_svm(vcpu);
404 u64 *entry = svm->avic_physical_id_cache;
405
406 if (!entry)
407 return false;
408
409 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
410}
411
Joerg Roedel384c6362010-11-30 18:03:56 +0100412static void recalc_intercepts(struct vcpu_svm *svm)
413{
414 struct vmcb_control_area *c, *h;
415 struct nested_state *g;
416
Joerg Roedel116a0a22010-12-03 11:45:49 +0100417 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
418
Joerg Roedel384c6362010-11-30 18:03:56 +0100419 if (!is_guest_mode(&svm->vcpu))
420 return;
421
422 c = &svm->vmcb->control;
423 h = &svm->nested.hsave->control;
424 g = &svm->nested;
425
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100426 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100427 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Paolo Bonzinibd895252018-01-11 16:55:24 +0100428 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
Joerg Roedel384c6362010-11-30 18:03:56 +0100429 c->intercept = h->intercept | g->intercept;
430}
431
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100432static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
433{
434 if (is_guest_mode(&svm->vcpu))
435 return svm->nested.hsave;
436 else
437 return svm->vmcb;
438}
439
440static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
441{
442 struct vmcb *vmcb = get_host_vmcb(svm);
443
444 vmcb->control.intercept_cr |= (1U << bit);
445
446 recalc_intercepts(svm);
447}
448
449static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
450{
451 struct vmcb *vmcb = get_host_vmcb(svm);
452
453 vmcb->control.intercept_cr &= ~(1U << bit);
454
455 recalc_intercepts(svm);
456}
457
458static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
459{
460 struct vmcb *vmcb = get_host_vmcb(svm);
461
462 return vmcb->control.intercept_cr & (1U << bit);
463}
464
Paolo Bonzini5315c712014-03-03 13:08:29 +0100465static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100466{
467 struct vmcb *vmcb = get_host_vmcb(svm);
468
Paolo Bonzini5315c712014-03-03 13:08:29 +0100469 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
470 | (1 << INTERCEPT_DR1_READ)
471 | (1 << INTERCEPT_DR2_READ)
472 | (1 << INTERCEPT_DR3_READ)
473 | (1 << INTERCEPT_DR4_READ)
474 | (1 << INTERCEPT_DR5_READ)
475 | (1 << INTERCEPT_DR6_READ)
476 | (1 << INTERCEPT_DR7_READ)
477 | (1 << INTERCEPT_DR0_WRITE)
478 | (1 << INTERCEPT_DR1_WRITE)
479 | (1 << INTERCEPT_DR2_WRITE)
480 | (1 << INTERCEPT_DR3_WRITE)
481 | (1 << INTERCEPT_DR4_WRITE)
482 | (1 << INTERCEPT_DR5_WRITE)
483 | (1 << INTERCEPT_DR6_WRITE)
484 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100485
486 recalc_intercepts(svm);
487}
488
Paolo Bonzini5315c712014-03-03 13:08:29 +0100489static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100490{
491 struct vmcb *vmcb = get_host_vmcb(svm);
492
Paolo Bonzini5315c712014-03-03 13:08:29 +0100493 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100494
495 recalc_intercepts(svm);
496}
497
Joerg Roedel18c918c2010-11-30 18:03:59 +0100498static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
499{
500 struct vmcb *vmcb = get_host_vmcb(svm);
501
502 vmcb->control.intercept_exceptions |= (1U << bit);
503
504 recalc_intercepts(svm);
505}
506
507static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
508{
509 struct vmcb *vmcb = get_host_vmcb(svm);
510
511 vmcb->control.intercept_exceptions &= ~(1U << bit);
512
513 recalc_intercepts(svm);
514}
515
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100516static inline void set_intercept(struct vcpu_svm *svm, int bit)
517{
518 struct vmcb *vmcb = get_host_vmcb(svm);
519
520 vmcb->control.intercept |= (1ULL << bit);
521
522 recalc_intercepts(svm);
523}
524
525static inline void clr_intercept(struct vcpu_svm *svm, int bit)
526{
527 struct vmcb *vmcb = get_host_vmcb(svm);
528
529 vmcb->control.intercept &= ~(1ULL << bit);
530
531 recalc_intercepts(svm);
532}
533
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500534static inline bool vgif_enabled(struct vcpu_svm *svm)
535{
536 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
537}
538
Joerg Roedel2af91942009-08-07 11:49:28 +0200539static inline void enable_gif(struct vcpu_svm *svm)
540{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500541 if (vgif_enabled(svm))
542 svm->vmcb->control.int_ctl |= V_GIF_MASK;
543 else
544 svm->vcpu.arch.hflags |= HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200545}
546
547static inline void disable_gif(struct vcpu_svm *svm)
548{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500549 if (vgif_enabled(svm))
550 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
551 else
552 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
Joerg Roedel2af91942009-08-07 11:49:28 +0200553}
554
555static inline bool gif_set(struct vcpu_svm *svm)
556{
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500557 if (vgif_enabled(svm))
558 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
559 else
560 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
Joerg Roedel2af91942009-08-07 11:49:28 +0200561}
562
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563static unsigned long iopm_base;
564
565struct kvm_ldttss_desc {
566 u16 limit0;
567 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100568 unsigned base1:8, type:5, dpl:2, p:1;
569 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570 u32 base3;
571 u32 zero1;
572} __attribute__((packed));
573
574struct svm_cpu_data {
575 int cpu;
576
Avi Kivity5008fdf2007-04-02 13:05:50 +0300577 u64 asid_generation;
578 u32 max_asid;
579 u32 next_asid;
Brijesh Singh4faefff2017-12-04 10:57:25 -0600580 u32 min_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581 struct kvm_ldttss_desc *tss_desc;
582
583 struct page *save_area;
Ashok Raj15d45072018-02-01 22:59:43 +0100584 struct vmcb *current_vmcb;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600585
586 /* index = sev_asid, value = vmcb pointer */
587 struct vmcb **sev_vmcbs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588};
589
590static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
591
592struct svm_init_data {
593 int cpu;
594 int r;
595};
596
Mathias Krause09941fb2012-08-30 01:30:20 +0200597static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800598
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200599#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600#define MSRS_RANGE_SIZE 2048
601#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
602
Joerg Roedel455716f2010-03-01 15:34:35 +0100603static u32 svm_msrpm_offset(u32 msr)
604{
605 u32 offset;
606 int i;
607
608 for (i = 0; i < NUM_MSR_MAPS; i++) {
609 if (msr < msrpm_ranges[i] ||
610 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
611 continue;
612
613 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
614 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
615
616 /* Now we have the u8 offset - but need the u32 offset */
617 return offset / 4;
618 }
619
620 /* MSR not in any range */
621 return MSR_INVALID;
622}
623
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624#define MAX_INST_SIZE 15
625
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626static inline void clgi(void)
627{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300628 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629}
630
631static inline void stgi(void)
632{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300633 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634}
635
636static inline void invlpga(unsigned long addr, u32 asid)
637{
Joerg Roedele0231712010-02-24 18:59:10 +0100638 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639}
640
Yu Zhang855feb62017-08-24 20:27:55 +0800641static int get_npt_level(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +0200642{
643#ifdef CONFIG_X86_64
Yu Zhang2a7266a2017-08-24 20:27:54 +0800644 return PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200645#else
646 return PT32E_ROOT_LEVEL;
647#endif
648}
649
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
651{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000652 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100653 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600654 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655
Alexander Graf9962d032008-11-25 20:17:02 +0100656 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100657 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658}
659
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660static int is_external_interrupt(u32 info)
661{
662 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
663 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
664}
665
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200666static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400667{
668 struct vcpu_svm *svm = to_svm(vcpu);
669 u32 ret = 0;
670
671 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200672 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
673 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400674}
675
676static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
677{
678 struct vcpu_svm *svm = to_svm(vcpu);
679
680 if (mask == 0)
681 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
682 else
683 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
684
685}
686
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
688{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400689 struct vcpu_svm *svm = to_svm(vcpu);
690
Bandan Dasf1047652015-06-11 02:05:33 -0400691 if (svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200692 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200693 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400694 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200695
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400696 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100697 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300698 EMULATE_DONE)
699 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700 return;
701 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300702 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
703 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
704 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300706 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400707 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708}
709
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700710static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100711{
712 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700713 unsigned nr = vcpu->arch.exception.nr;
714 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700715 bool reinject = vcpu->arch.exception.injected;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700716 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100717
Joerg Roedele0231712010-02-24 18:59:10 +0100718 /*
719 * If we are within a nested VM we'd better #VMEXIT and let the guest
720 * handle the exception
721 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200722 if (!reinject &&
723 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100724 return;
725
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200726 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100727 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
728
729 /*
730 * For guest debugging where we have to reinject #BP if some
731 * INT3 is guest-owned:
732 * Emulate nRIP by moving RIP forward. Will fail if injection
733 * raises a fault that is not intercepted. Still better than
734 * failing in all cases.
735 */
736 skip_emulated_instruction(&svm->vcpu);
737 rip = kvm_rip_read(&svm->vcpu);
738 svm->int3_rip = rip + svm->vmcb->save.cs.base;
739 svm->int3_injected = rip - old_rip;
740 }
741
Jan Kiszka116a4752010-02-23 17:47:54 +0100742 svm->vmcb->control.event_inj = nr
743 | SVM_EVTINJ_VALID
744 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
745 | SVM_EVTINJ_TYPE_EXEPT;
746 svm->vmcb->control.event_inj_err = error_code;
747}
748
Joerg Roedel67ec6602010-05-17 14:43:35 +0200749static void svm_init_erratum_383(void)
750{
751 u32 low, high;
752 int err;
753 u64 val;
754
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100755 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200756 return;
757
758 /* Use _safe variants to not break nested virtualization */
759 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
760 if (err)
761 return;
762
763 val |= (1ULL << 47);
764
765 low = lower_32_bits(val);
766 high = upper_32_bits(val);
767
768 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
769
770 erratum_383_found = true;
771}
772
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500773static void svm_init_osvw(struct kvm_vcpu *vcpu)
774{
775 /*
776 * Guests should see errata 400 and 415 as fixed (assuming that
777 * HLT and IO instructions are intercepted).
778 */
779 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
780 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
781
782 /*
783 * By increasing VCPU's osvw.length to 3 we are telling the guest that
784 * all osvw.status bits inside that length, including bit 0 (which is
785 * reserved for erratum 298), are valid. However, if host processor's
786 * osvw_len is 0 then osvw_status[0] carries no information. We need to
787 * be conservative here and therefore we tell the guest that erratum 298
788 * is present (because we really don't know).
789 */
790 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
791 vcpu->arch.osvw.status |= 1;
792}
793
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794static int has_svm(void)
795{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200796 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797
Eduardo Habkost63d11422008-11-17 19:03:20 -0200798 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800799 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800 return 0;
801 }
802
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803 return 1;
804}
805
Radim Krčmář13a34e02014-08-28 15:13:03 +0200806static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100808 /* Make sure we clean up behind us */
809 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
810 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
811
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200812 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100813
814 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815}
816
Radim Krčmář13a34e02014-08-28 15:13:03 +0200817static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818{
819
Tejun Heo0fe1e002009-10-29 22:34:14 +0900820 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822 struct desc_struct *gdt;
823 int me = raw_smp_processor_id();
824
Alexander Graf10474ae2009-09-15 11:37:46 +0200825 rdmsrl(MSR_EFER, efer);
826 if (efer & EFER_SVME)
827 return -EBUSY;
828
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200830 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200831 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900833 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900834 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200835 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200836 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837 }
838
Tejun Heo0fe1e002009-10-29 22:34:14 +0900839 sd->asid_generation = 1;
840 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
841 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600842 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843
Thomas Garnier45fc8752017-03-14 10:05:08 -0700844 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900845 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846
Alexander Graf9962d032008-11-25 20:17:02 +0100847 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848
Linus Torvaldsd0316552009-12-14 09:58:24 -0800849 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200850
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100851 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
852 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500853 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100854 }
855
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500856
857 /*
858 * Get OSVW bits.
859 *
860 * Note that it is possible to have a system with mixed processor
861 * revisions and therefore different OSVW bits. If bits are not the same
862 * on different processors then choose the worst case (i.e. if erratum
863 * is present on one processor and not on another then assume that the
864 * erratum is present everywhere).
865 */
866 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
867 uint64_t len, status = 0;
868 int err;
869
870 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
871 if (!err)
872 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
873 &err);
874
875 if (err)
876 osvw_status = osvw_len = 0;
877 else {
878 if (len < osvw_len)
879 osvw_len = len;
880 osvw_status |= status;
881 osvw_status &= (1ULL << osvw_len) - 1;
882 }
883 } else
884 osvw_status = osvw_len = 0;
885
Joerg Roedel67ec6602010-05-17 14:43:35 +0200886 svm_init_erratum_383();
887
Joerg Roedel1018faa2012-02-29 14:57:32 +0100888 amd_pmu_enable_virt();
889
Alexander Graf10474ae2009-09-15 11:37:46 +0200890 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891}
892
Joerg Roedel0da1db752008-07-02 16:02:11 +0200893static void svm_cpu_uninit(int cpu)
894{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900895 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200896
Tejun Heo0fe1e002009-10-29 22:34:14 +0900897 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200898 return;
899
900 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600901 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900902 __free_page(sd->save_area);
903 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200904}
905
Avi Kivity6aa8b732006-12-10 02:21:36 -0800906static int svm_cpu_init(int cpu)
907{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900908 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909 int r;
910
Tejun Heo0fe1e002009-10-29 22:34:14 +0900911 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
912 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900914 sd->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915 r = -ENOMEM;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600916 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900917 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918 goto err_1;
919
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600920 if (svm_sev_enabled()) {
921 r = -ENOMEM;
922 sd->sev_vmcbs = kmalloc((max_sev_asid + 1) * sizeof(void *), GFP_KERNEL);
923 if (!sd->sev_vmcbs)
924 goto err_1;
925 }
926
Tejun Heo0fe1e002009-10-29 22:34:14 +0900927 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928
929 return 0;
930
931err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900932 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800933 return r;
934
935}
936
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100937static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938{
939 int i;
940
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100941 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
942 if (direct_access_msrs[i].index == index)
943 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100945 return false;
946}
947
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100948static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr)
949{
950 u8 bit_write;
951 unsigned long tmp;
952 u32 offset;
953 u32 *msrpm;
954
955 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
956 to_svm(vcpu)->msrpm;
957
958 offset = svm_msrpm_offset(msr);
959 bit_write = 2 * (msr & 0x0f) + 1;
960 tmp = msrpm[offset];
961
962 BUG_ON(offset == MSR_INVALID);
963
964 return !!test_bit(bit_write, &tmp);
965}
966
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967static void set_msr_interception(u32 *msrpm, unsigned msr,
968 int read, int write)
969{
Joerg Roedel455716f2010-03-01 15:34:35 +0100970 u8 bit_read, bit_write;
971 unsigned long tmp;
972 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100974 /*
975 * If this warning triggers extend the direct_access_msrs list at the
976 * beginning of the file
977 */
978 WARN_ON(!valid_msr_intercept(msr));
979
Joerg Roedel455716f2010-03-01 15:34:35 +0100980 offset = svm_msrpm_offset(msr);
981 bit_read = 2 * (msr & 0x0f);
982 bit_write = 2 * (msr & 0x0f) + 1;
983 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984
Joerg Roedel455716f2010-03-01 15:34:35 +0100985 BUG_ON(offset == MSR_INVALID);
986
987 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
988 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
989
990 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991}
992
Joerg Roedelf65c2292008-02-13 18:58:46 +0100993static void svm_vcpu_init_msrpm(u32 *msrpm)
994{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100995 int i;
996
Joerg Roedelf65c2292008-02-13 18:58:46 +0100997 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
998
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100999 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1000 if (!direct_access_msrs[i].always)
1001 continue;
1002
1003 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
1004 }
Joerg Roedelf65c2292008-02-13 18:58:46 +01001005}
1006
Joerg Roedel323c3d82010-03-01 15:34:37 +01001007static void add_msr_offset(u32 offset)
1008{
1009 int i;
1010
1011 for (i = 0; i < MSRPM_OFFSETS; ++i) {
1012
1013 /* Offset already in list? */
1014 if (msrpm_offsets[i] == offset)
1015 return;
1016
1017 /* Slot used by another offset? */
1018 if (msrpm_offsets[i] != MSR_INVALID)
1019 continue;
1020
1021 /* Add offset to list */
1022 msrpm_offsets[i] = offset;
1023
1024 return;
1025 }
1026
1027 /*
1028 * If this BUG triggers the msrpm_offsets table has an overflow. Just
1029 * increase MSRPM_OFFSETS in this case.
1030 */
1031 BUG();
1032}
1033
1034static void init_msrpm_offsets(void)
1035{
1036 int i;
1037
1038 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
1039
1040 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
1041 u32 offset;
1042
1043 offset = svm_msrpm_offset(direct_access_msrs[i].index);
1044 BUG_ON(offset == MSR_INVALID);
1045
1046 add_msr_offset(offset);
1047 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048}
1049
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001050static void svm_enable_lbrv(struct vcpu_svm *svm)
1051{
1052 u32 *msrpm = svm->msrpm;
1053
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001054 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001055 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
1056 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
1057 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
1058 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
1059}
1060
1061static void svm_disable_lbrv(struct vcpu_svm *svm)
1062{
1063 u32 *msrpm = svm->msrpm;
1064
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05001065 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +01001066 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
1067 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
1068 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
1069 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
1070}
1071
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001072static void disable_nmi_singlestep(struct vcpu_svm *svm)
1073{
1074 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001075
Ladi Prosekab2f4d732017-06-21 09:06:58 +02001076 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
1077 /* Clear our flags if they were not set by the guest */
1078 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1079 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
1080 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1081 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
1082 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001083}
1084
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001085/* Note:
1086 * This hash table is used to map VM_ID to a struct kvm_arch,
1087 * when handling AMD IOMMU GALOG notification to schedule in
1088 * a particular vCPU.
1089 */
1090#define SVM_VM_DATA_HASH_BITS 8
David Hildenbrand681bcea2017-01-24 22:21:16 +01001091static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001092static u32 next_vm_id = 0;
1093static bool next_vm_id_wrapped = 0;
David Hildenbrand681bcea2017-01-24 22:21:16 +01001094static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001095
1096/* Note:
1097 * This function is called from IOMMU driver to notify
1098 * SVM to schedule in a particular vCPU of a particular VM.
1099 */
1100static int avic_ga_log_notifier(u32 ga_tag)
1101{
1102 unsigned long flags;
1103 struct kvm_arch *ka = NULL;
1104 struct kvm_vcpu *vcpu = NULL;
1105 u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
1106 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
1107
1108 pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
1109
1110 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1111 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1112 struct kvm *kvm = container_of(ka, struct kvm, arch);
1113 struct kvm_arch *vm_data = &kvm->arch;
1114
1115 if (vm_data->avic_vm_id != vm_id)
1116 continue;
1117 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
1118 break;
1119 }
1120 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1121
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001122 /* Note:
1123 * At this point, the IOMMU should have already set the pending
1124 * bit in the vAPIC backing page. So, we just need to schedule
1125 * in the vcpu.
1126 */
Paolo Bonzini1cf53582017-10-10 12:51:56 +02001127 if (vcpu)
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001128 kvm_vcpu_wake_up(vcpu);
1129
1130 return 0;
1131}
1132
Brijesh Singhe9df0942017-12-04 10:57:33 -06001133static __init int sev_hardware_setup(void)
1134{
1135 struct sev_user_data_status *status;
1136 int rc;
1137
1138 /* Maximum number of encrypted guests supported simultaneously */
1139 max_sev_asid = cpuid_ecx(0x8000001F);
1140
1141 if (!max_sev_asid)
1142 return 1;
1143
Brijesh Singh1654efc2017-12-04 10:57:34 -06001144 /* Minimum ASID value that should be used for SEV guest */
1145 min_sev_asid = cpuid_edx(0x8000001F);
1146
1147 /* Initialize SEV ASID bitmap */
1148 sev_asid_bitmap = kcalloc(BITS_TO_LONGS(max_sev_asid),
1149 sizeof(unsigned long), GFP_KERNEL);
1150 if (!sev_asid_bitmap)
1151 return 1;
1152
Brijesh Singhe9df0942017-12-04 10:57:33 -06001153 status = kmalloc(sizeof(*status), GFP_KERNEL);
1154 if (!status)
1155 return 1;
1156
1157 /*
1158 * Check SEV platform status.
1159 *
1160 * PLATFORM_STATUS can be called in any state, if we failed to query
1161 * the PLATFORM status then either PSP firmware does not support SEV
1162 * feature or SEV firmware is dead.
1163 */
1164 rc = sev_platform_status(status, NULL);
1165 if (rc)
1166 goto err;
1167
1168 pr_info("SEV supported\n");
1169
1170err:
1171 kfree(status);
1172 return rc;
1173}
1174
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175static __init int svm_hardware_setup(void)
1176{
1177 int cpu;
1178 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001179 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180 int r;
1181
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
1183
1184 if (!iopm_pages)
1185 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +03001186
1187 iopm_va = page_address(iopm_pages);
1188 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
1190
Joerg Roedel323c3d82010-03-01 15:34:37 +01001191 init_msrpm_offsets();
1192
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001193 if (boot_cpu_has(X86_FEATURE_NX))
1194 kvm_enable_efer_bits(EFER_NX);
1195
Alexander Graf1b2fd702009-02-02 16:23:51 +01001196 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
1197 kvm_enable_efer_bits(EFER_FFXSR);
1198
Joerg Roedel92a1f122011-03-25 09:44:51 +01001199 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +01001200 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +08001201 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1202 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +01001203 }
1204
Alexander Graf236de052008-11-25 20:17:10 +01001205 if (nested) {
1206 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +02001207 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +01001208 }
1209
Brijesh Singhe9df0942017-12-04 10:57:33 -06001210 if (sev) {
1211 if (boot_cpu_has(X86_FEATURE_SEV) &&
1212 IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
1213 r = sev_hardware_setup();
1214 if (r)
1215 sev = false;
1216 } else {
1217 sev = false;
1218 }
1219 }
1220
Zachary Amsden3230bb42009-09-29 11:38:37 -10001221 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 r = svm_cpu_init(cpu);
1223 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +01001224 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +01001226
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001227 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001228 npt_enabled = false;
1229
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001230 if (npt_enabled && !npt) {
1231 printk(KERN_INFO "kvm: Nested Paging disabled\n");
1232 npt_enabled = false;
1233 }
1234
Joerg Roedel18552672008-02-07 13:47:41 +01001235 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001236 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +01001237 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +02001238 } else
1239 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001240
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001241 if (avic) {
1242 if (!npt_enabled ||
1243 !boot_cpu_has(X86_FEATURE_AVIC) ||
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001244 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001245 avic = false;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001246 } else {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001247 pr_info("AVIC enabled\n");
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001248
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001249 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1250 }
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001251 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001252
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001253 if (vls) {
1254 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001255 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001256 !IS_ENABLED(CONFIG_X86_64)) {
1257 vls = false;
1258 } else {
1259 pr_info("Virtual VMLOAD VMSAVE supported\n");
1260 }
1261 }
1262
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001263 if (vgif) {
1264 if (!boot_cpu_has(X86_FEATURE_VGIF))
1265 vgif = false;
1266 else
1267 pr_info("Virtual GIF supported\n");
1268 }
1269
Avi Kivity6aa8b732006-12-10 02:21:36 -08001270 return 0;
1271
Joerg Roedelf65c2292008-02-13 18:58:46 +01001272err:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1274 iopm_base = 0;
1275 return r;
1276}
1277
1278static __exit void svm_hardware_unsetup(void)
1279{
Joerg Roedel0da1db752008-07-02 16:02:11 +02001280 int cpu;
1281
Brijesh Singh1654efc2017-12-04 10:57:34 -06001282 if (svm_sev_enabled())
1283 kfree(sev_asid_bitmap);
1284
Zachary Amsden3230bb42009-09-29 11:38:37 -10001285 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +02001286 svm_cpu_uninit(cpu);
1287
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001289 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001290}
1291
1292static void init_seg(struct vmcb_seg *seg)
1293{
1294 seg->selector = 0;
1295 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001296 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 seg->limit = 0xffff;
1298 seg->base = 0;
1299}
1300
1301static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1302{
1303 seg->selector = 0;
1304 seg->attrib = SVM_SELECTOR_P_MASK | type;
1305 seg->limit = 0xffff;
1306 seg->base = 0;
1307}
1308
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001309static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1310{
1311 struct vcpu_svm *svm = to_svm(vcpu);
1312 u64 g_tsc_offset = 0;
1313
Joerg Roedel20307532010-11-29 17:51:48 +01001314 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001315 g_tsc_offset = svm->vmcb->control.tsc_offset -
1316 svm->nested.hsave->control.tsc_offset;
1317 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001318 } else
1319 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1320 svm->vmcb->control.tsc_offset,
1321 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001322
1323 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001324
1325 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001326}
1327
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001328static void avic_init_vmcb(struct vcpu_svm *svm)
1329{
1330 struct vmcb *vmcb = svm->vmcb;
1331 struct kvm_arch *vm_data = &svm->vcpu.kvm->arch;
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001332 phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page));
1333 phys_addr_t lpa = __sme_set(page_to_phys(vm_data->avic_logical_id_table_page));
1334 phys_addr_t ppa = __sme_set(page_to_phys(vm_data->avic_physical_id_table_page));
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001335
1336 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1337 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1338 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1339 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1340 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001341}
1342
Paolo Bonzini56908912015-10-19 11:30:19 +02001343static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344{
Joerg Roedele6101a92008-02-13 18:58:45 +01001345 struct vmcb_control_area *control = &svm->vmcb->control;
1346 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001348 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001349
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001350 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1351 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1352 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1353 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1354 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1355 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001356 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1357 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358
Paolo Bonzini5315c712014-03-03 13:08:29 +01001359 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360
Joerg Roedel18c918c2010-11-30 18:03:59 +01001361 set_exception_intercept(svm, PF_VECTOR);
1362 set_exception_intercept(svm, UD_VECTOR);
1363 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001364 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001365 set_exception_intercept(svm, DB_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001367 set_intercept(svm, INTERCEPT_INTR);
1368 set_intercept(svm, INTERCEPT_NMI);
1369 set_intercept(svm, INTERCEPT_SMI);
1370 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001371 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001372 set_intercept(svm, INTERCEPT_CPUID);
1373 set_intercept(svm, INTERCEPT_INVD);
1374 set_intercept(svm, INTERCEPT_HLT);
1375 set_intercept(svm, INTERCEPT_INVLPG);
1376 set_intercept(svm, INTERCEPT_INVLPGA);
1377 set_intercept(svm, INTERCEPT_IOIO_PROT);
1378 set_intercept(svm, INTERCEPT_MSR_PROT);
1379 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1380 set_intercept(svm, INTERCEPT_SHUTDOWN);
1381 set_intercept(svm, INTERCEPT_VMRUN);
1382 set_intercept(svm, INTERCEPT_VMMCALL);
1383 set_intercept(svm, INTERCEPT_VMLOAD);
1384 set_intercept(svm, INTERCEPT_VMSAVE);
1385 set_intercept(svm, INTERCEPT_STGI);
1386 set_intercept(svm, INTERCEPT_CLGI);
1387 set_intercept(svm, INTERCEPT_SKINIT);
1388 set_intercept(svm, INTERCEPT_WBINVD);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001389 set_intercept(svm, INTERCEPT_XSETBV);
Brijesh Singh7607b712018-02-19 10:14:44 -06001390 set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001392 if (!kvm_mwait_in_guest()) {
1393 set_intercept(svm, INTERCEPT_MONITOR);
1394 set_intercept(svm, INTERCEPT_MWAIT);
1395 }
1396
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001397 control->iopm_base_pa = __sme_set(iopm_base);
1398 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399 control->int_ctl = V_INTR_MASKING_MASK;
1400
1401 init_seg(&save->es);
1402 init_seg(&save->ss);
1403 init_seg(&save->ds);
1404 init_seg(&save->fs);
1405 init_seg(&save->gs);
1406
1407 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001408 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409 /* Executable/Readable Code Segment */
1410 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1411 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1412 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413
1414 save->gdtr.limit = 0xffff;
1415 save->idtr.limit = 0xffff;
1416
1417 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1418 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1419
Paolo Bonzini56908912015-10-19 11:30:19 +02001420 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001421 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001422 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001424 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425
Joerg Roedele0231712010-02-24 18:59:10 +01001426 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001427 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001428 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001430 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001431 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001432
Rusty Russell66aee912007-07-17 23:34:16 +10001433 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001434 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001435
1436 if (npt_enabled) {
1437 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001438 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001439 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001440 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001441 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1442 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001443 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001444 save->cr3 = 0;
1445 save->cr4 = 0;
1446 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001447 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001448
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001449 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001450 svm->vcpu.arch.hflags = 0;
1451
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001452 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001453 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001454 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001455 }
1456
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001457 if (kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001458 avic_init_vmcb(svm);
1459
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001460 /*
1461 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1462 * in VMCB and clear intercepts to avoid #VMEXIT.
1463 */
1464 if (vls) {
1465 clr_intercept(svm, INTERCEPT_VMLOAD);
1466 clr_intercept(svm, INTERCEPT_VMSAVE);
1467 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1468 }
1469
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001470 if (vgif) {
1471 clr_intercept(svm, INTERCEPT_STGI);
1472 clr_intercept(svm, INTERCEPT_CLGI);
1473 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1474 }
1475
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001476 if (sev_guest(svm->vcpu.kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001477 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001478 clr_exception_intercept(svm, UD_VECTOR);
1479 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001480
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001481 mark_all_dirty(svm->vmcb);
1482
Joerg Roedel2af91942009-08-07 11:49:28 +02001483 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001484
1485}
1486
Dan Carpenterd3e7dec2017-05-18 10:38:53 +03001487static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
1488 unsigned int index)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001489{
1490 u64 *avic_physical_id_table;
1491 struct kvm_arch *vm_data = &vcpu->kvm->arch;
1492
1493 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1494 return NULL;
1495
1496 avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page);
1497
1498 return &avic_physical_id_table[index];
1499}
1500
1501/**
1502 * Note:
1503 * AVIC hardware walks the nested page table to check permissions,
1504 * but does not use the SPA address specified in the leaf page
1505 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1506 * field of the VMCB. Therefore, we set up the
1507 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1508 */
1509static int avic_init_access_page(struct kvm_vcpu *vcpu)
1510{
1511 struct kvm *kvm = vcpu->kvm;
1512 int ret;
1513
1514 if (kvm->arch.apic_access_page_done)
1515 return 0;
1516
1517 ret = x86_set_memory_region(kvm,
1518 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1519 APIC_DEFAULT_PHYS_BASE,
1520 PAGE_SIZE);
1521 if (ret)
1522 return ret;
1523
1524 kvm->arch.apic_access_page_done = true;
1525 return 0;
1526}
1527
1528static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1529{
1530 int ret;
1531 u64 *entry, new_entry;
1532 int id = vcpu->vcpu_id;
1533 struct vcpu_svm *svm = to_svm(vcpu);
1534
1535 ret = avic_init_access_page(vcpu);
1536 if (ret)
1537 return ret;
1538
1539 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1540 return -EINVAL;
1541
1542 if (!svm->vcpu.arch.apic->regs)
1543 return -EINVAL;
1544
1545 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1546
1547 /* Setting AVIC backing page address in the phy APIC ID table */
1548 entry = avic_get_physical_id_entry(vcpu, id);
1549 if (!entry)
1550 return -EINVAL;
1551
1552 new_entry = READ_ONCE(*entry);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001553 new_entry = __sme_set((page_to_phys(svm->avic_backing_page) &
1554 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1555 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001556 WRITE_ONCE(*entry, new_entry);
1557
1558 svm->avic_physical_id_cache = entry;
1559
1560 return 0;
1561}
1562
Brijesh Singh1654efc2017-12-04 10:57:34 -06001563static void __sev_asid_free(int asid)
1564{
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001565 struct svm_cpu_data *sd;
1566 int cpu, pos;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001567
1568 pos = asid - 1;
1569 clear_bit(pos, sev_asid_bitmap);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001570
1571 for_each_possible_cpu(cpu) {
1572 sd = per_cpu(svm_data, cpu);
1573 sd->sev_vmcbs[pos] = NULL;
1574 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001575}
1576
1577static void sev_asid_free(struct kvm *kvm)
1578{
1579 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1580
1581 __sev_asid_free(sev->asid);
1582}
1583
Brijesh Singh59414c92017-12-04 10:57:35 -06001584static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
1585{
1586 struct sev_data_decommission *decommission;
1587 struct sev_data_deactivate *data;
1588
1589 if (!handle)
1590 return;
1591
1592 data = kzalloc(sizeof(*data), GFP_KERNEL);
1593 if (!data)
1594 return;
1595
1596 /* deactivate handle */
1597 data->handle = handle;
1598 sev_guest_deactivate(data, NULL);
1599
1600 wbinvd_on_all_cpus();
1601 sev_guest_df_flush(NULL);
1602 kfree(data);
1603
1604 decommission = kzalloc(sizeof(*decommission), GFP_KERNEL);
1605 if (!decommission)
1606 return;
1607
1608 /* decommission handle */
1609 decommission->handle = handle;
1610 sev_guest_decommission(decommission, NULL);
1611
1612 kfree(decommission);
1613}
1614
Brijesh Singh89c50582017-12-04 10:57:35 -06001615static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
1616 unsigned long ulen, unsigned long *n,
1617 int write)
1618{
1619 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1620 unsigned long npages, npinned, size;
1621 unsigned long locked, lock_limit;
1622 struct page **pages;
1623 int first, last;
1624
1625 /* Calculate number of pages. */
1626 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
1627 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
1628 npages = (last - first + 1);
1629
1630 locked = sev->pages_locked + npages;
1631 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1632 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
1633 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
1634 return NULL;
1635 }
1636
1637 /* Avoid using vmalloc for smaller buffers. */
1638 size = npages * sizeof(struct page *);
1639 if (size > PAGE_SIZE)
1640 pages = vmalloc(size);
1641 else
1642 pages = kmalloc(size, GFP_KERNEL);
1643
1644 if (!pages)
1645 return NULL;
1646
1647 /* Pin the user virtual address. */
1648 npinned = get_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages);
1649 if (npinned != npages) {
1650 pr_err("SEV: Failure locking %lu pages.\n", npages);
1651 goto err;
1652 }
1653
1654 *n = npages;
1655 sev->pages_locked = locked;
1656
1657 return pages;
1658
1659err:
1660 if (npinned > 0)
1661 release_pages(pages, npinned);
1662
1663 kvfree(pages);
1664 return NULL;
1665}
1666
1667static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
1668 unsigned long npages)
1669{
1670 struct kvm_sev_info *sev = &kvm->arch.sev_info;
1671
1672 release_pages(pages, npages);
1673 kvfree(pages);
1674 sev->pages_locked -= npages;
1675}
1676
1677static void sev_clflush_pages(struct page *pages[], unsigned long npages)
1678{
1679 uint8_t *page_virtual;
1680 unsigned long i;
1681
1682 if (npages == 0 || pages == NULL)
1683 return;
1684
1685 for (i = 0; i < npages; i++) {
1686 page_virtual = kmap_atomic(pages[i]);
1687 clflush_cache_range(page_virtual, PAGE_SIZE);
1688 kunmap_atomic(page_virtual);
1689 }
1690}
1691
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001692static void __unregister_enc_region_locked(struct kvm *kvm,
1693 struct enc_region *region)
1694{
1695 /*
1696 * The guest may change the memory encryption attribute from C=0 -> C=1
1697 * or vice versa for this memory range. Lets make sure caches are
1698 * flushed to ensure that guest data gets written into memory with
1699 * correct C-bit.
1700 */
1701 sev_clflush_pages(region->pages, region->npages);
1702
1703 sev_unpin_memory(kvm, region->pages, region->npages);
1704 list_del(&region->list);
1705 kfree(region);
1706}
1707
Brijesh Singh1654efc2017-12-04 10:57:34 -06001708static void sev_vm_destroy(struct kvm *kvm)
1709{
Brijesh Singh59414c92017-12-04 10:57:35 -06001710 struct kvm_sev_info *sev = &kvm->arch.sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001711 struct list_head *head = &sev->regions_list;
1712 struct list_head *pos, *q;
Brijesh Singh59414c92017-12-04 10:57:35 -06001713
Brijesh Singh1654efc2017-12-04 10:57:34 -06001714 if (!sev_guest(kvm))
1715 return;
1716
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001717 mutex_lock(&kvm->lock);
1718
1719 /*
1720 * if userspace was terminated before unregistering the memory regions
1721 * then lets unpin all the registered memory.
1722 */
1723 if (!list_empty(head)) {
1724 list_for_each_safe(pos, q, head) {
1725 __unregister_enc_region_locked(kvm,
1726 list_entry(pos, struct enc_region, list));
1727 }
1728 }
1729
1730 mutex_unlock(&kvm->lock);
1731
Brijesh Singh59414c92017-12-04 10:57:35 -06001732 sev_unbind_asid(kvm, sev->handle);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001733 sev_asid_free(kvm);
1734}
1735
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001736static void avic_vm_destroy(struct kvm *kvm)
1737{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001738 unsigned long flags;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001739 struct kvm_arch *vm_data = &kvm->arch;
1740
Dmitry Vyukov3863dff2017-01-24 14:06:48 +01001741 if (!avic)
1742 return;
1743
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001744 if (vm_data->avic_logical_id_table_page)
1745 __free_page(vm_data->avic_logical_id_table_page);
1746 if (vm_data->avic_physical_id_table_page)
1747 __free_page(vm_data->avic_physical_id_table_page);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001748
1749 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
1750 hash_del(&vm_data->hnode);
1751 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001752}
1753
Brijesh Singh1654efc2017-12-04 10:57:34 -06001754static void svm_vm_destroy(struct kvm *kvm)
1755{
1756 avic_vm_destroy(kvm);
1757 sev_vm_destroy(kvm);
1758}
1759
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001760static int avic_vm_init(struct kvm *kvm)
1761{
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001762 unsigned long flags;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001763 int err = -ENOMEM;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001764 struct kvm_arch *vm_data = &kvm->arch;
1765 struct page *p_page;
1766 struct page *l_page;
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001767 struct kvm_arch *ka;
1768 u32 vm_id;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001769
1770 if (!avic)
1771 return 0;
1772
1773 /* Allocating physical APIC ID table (4KB) */
1774 p_page = alloc_page(GFP_KERNEL);
1775 if (!p_page)
1776 goto free_avic;
1777
1778 vm_data->avic_physical_id_table_page = p_page;
1779 clear_page(page_address(p_page));
1780
1781 /* Allocating logical APIC ID table (4KB) */
1782 l_page = alloc_page(GFP_KERNEL);
1783 if (!l_page)
1784 goto free_avic;
1785
1786 vm_data->avic_logical_id_table_page = l_page;
1787 clear_page(page_address(l_page));
1788
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001789 spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
Denys Vlasenko3f0d4db2017-08-11 22:11:58 +02001790 again:
1791 vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
1792 if (vm_id == 0) { /* id is 1-based, zero is not okay */
1793 next_vm_id_wrapped = 1;
1794 goto again;
1795 }
1796 /* Is it still in use? Only possible if wrapped at least once */
1797 if (next_vm_id_wrapped) {
1798 hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
1799 struct kvm *k2 = container_of(ka, struct kvm, arch);
1800 struct kvm_arch *vd2 = &k2->arch;
1801 if (vd2->avic_vm_id == vm_id)
1802 goto again;
1803 }
1804 }
1805 vm_data->avic_vm_id = vm_id;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001806 hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id);
1807 spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
1808
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001809 return 0;
1810
1811free_avic:
1812 avic_vm_destroy(kvm);
1813 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001814}
1815
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001816static inline int
1817avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001818{
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001819 int ret = 0;
1820 unsigned long flags;
1821 struct amd_svm_iommu_ir *ir;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001822 struct vcpu_svm *svm = to_svm(vcpu);
1823
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001824 if (!kvm_arch_has_assigned_device(vcpu->kvm))
1825 return 0;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001826
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001827 /*
1828 * Here, we go through the per-vcpu ir_list to update all existing
1829 * interrupt remapping table entry targeting this vcpu.
1830 */
1831 spin_lock_irqsave(&svm->ir_list_lock, flags);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001832
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001833 if (list_empty(&svm->ir_list))
1834 goto out;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001835
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001836 list_for_each_entry(ir, &svm->ir_list, node) {
1837 ret = amd_iommu_update_ga(cpu, r, ir->data);
1838 if (ret)
1839 break;
1840 }
1841out:
1842 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
1843 return ret;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001844}
1845
1846static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1847{
1848 u64 entry;
1849 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05001850 int h_physical_id = kvm_cpu_get_apicid(cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001851 struct vcpu_svm *svm = to_svm(vcpu);
1852
1853 if (!kvm_vcpu_apicv_active(vcpu))
1854 return;
1855
1856 if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
1857 return;
1858
1859 entry = READ_ONCE(*(svm->avic_physical_id_cache));
1860 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
1861
1862 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
1863 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
1864
1865 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1866 if (svm->avic_is_running)
1867 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1868
1869 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001870 avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
1871 svm->avic_is_running);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001872}
1873
1874static void avic_vcpu_put(struct kvm_vcpu *vcpu)
1875{
1876 u64 entry;
1877 struct vcpu_svm *svm = to_svm(vcpu);
1878
1879 if (!kvm_vcpu_apicv_active(vcpu))
1880 return;
1881
1882 entry = READ_ONCE(*(svm->avic_physical_id_cache));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001883 if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
1884 avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
1885
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001886 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1887 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001888}
1889
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05001890/**
1891 * This function is called during VCPU halt/unhalt.
1892 */
1893static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
1894{
1895 struct vcpu_svm *svm = to_svm(vcpu);
1896
1897 svm->avic_is_running = is_run;
1898 if (is_run)
1899 avic_vcpu_load(vcpu, vcpu->cpu);
1900 else
1901 avic_vcpu_put(vcpu);
1902}
1903
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001904static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001905{
1906 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001907 u32 dummy;
1908 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001909
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001910 svm->spec_ctrl = 0;
1911
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001912 if (!init_event) {
1913 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1914 MSR_IA32_APICBASE_ENABLE;
1915 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1916 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1917 }
Paolo Bonzini56908912015-10-19 11:30:19 +02001918 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001919
Yu Zhange911eb32017-08-24 20:27:52 +08001920 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001921 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001922
1923 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1924 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001925}
1926
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001927static int avic_init_vcpu(struct vcpu_svm *svm)
1928{
1929 int ret;
1930
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001931 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001932 return 0;
1933
1934 ret = avic_init_backing_page(&svm->vcpu);
1935 if (ret)
1936 return ret;
1937
1938 INIT_LIST_HEAD(&svm->ir_list);
1939 spin_lock_init(&svm->ir_list_lock);
1940
1941 return ret;
1942}
1943
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001944static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001945{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001946 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001947 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001948 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001949 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001950 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001951 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952
Rusty Russellc16f8622007-07-30 21:12:19 +10001953 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001954 if (!svm) {
1955 err = -ENOMEM;
1956 goto out;
1957 }
1958
1959 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1960 if (err)
1961 goto free_svm;
1962
Joerg Roedelf65c2292008-02-13 18:58:46 +01001963 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001964 page = alloc_page(GFP_KERNEL);
1965 if (!page)
1966 goto uninit;
1967
Joerg Roedelf65c2292008-02-13 18:58:46 +01001968 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1969 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001970 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001971
1972 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1973 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001974 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001975
Alexander Grafb286d5d2008-11-25 20:17:05 +01001976 hsave_page = alloc_page(GFP_KERNEL);
1977 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001978 goto free_page3;
1979
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001980 err = avic_init_vcpu(svm);
1981 if (err)
1982 goto free_page4;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001983
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001984 /* We initialize this flag to true to make sure that the is_running
1985 * bit would be set the first time the vcpu is loaded.
1986 */
1987 svm->avic_is_running = true;
1988
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001989 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001990
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001991 svm->msrpm = page_address(msrpm_pages);
1992 svm_vcpu_init_msrpm(svm->msrpm);
1993
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001994 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001995 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001996
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001997 svm->vmcb = page_address(page);
1998 clear_page(svm->vmcb);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001999 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002000 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02002001 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002002
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05002003 svm_init_osvw(&svm->vcpu);
2004
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002005 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08002006
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002007free_page4:
2008 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09002009free_page3:
2010 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
2011free_page2:
2012 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
2013free_page1:
2014 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002015uninit:
2016 kvm_vcpu_uninit(&svm->vcpu);
2017free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10002018 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002019out:
2020 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021}
2022
2023static void svm_free_vcpu(struct kvm_vcpu *vcpu)
2024{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002025 struct vcpu_svm *svm = to_svm(vcpu);
2026
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002027 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01002028 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002029 __free_page(virt_to_page(svm->nested.hsave));
2030 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002031 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002032 kmem_cache_free(kvm_vcpu_cache, svm);
Ashok Raj15d45072018-02-01 22:59:43 +01002033 /*
2034 * The vmcb page can be recycled, causing a false negative in
2035 * svm_vcpu_load(). So do a full IBPB now.
2036 */
2037 indirect_branch_prediction_barrier();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002038}
2039
Avi Kivity15ad7142007-07-11 18:17:21 +03002040static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002041{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002042 struct vcpu_svm *svm = to_svm(vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01002043 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002044 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02002045
Avi Kivity0cc50642007-03-25 12:07:27 +02002046 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002047 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002048 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02002049 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002050
Avi Kivity82ca2d12010-10-21 12:20:34 +02002051#ifdef CONFIG_X86_64
2052 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
2053#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02002054 savesegment(fs, svm->host.fs);
2055 savesegment(gs, svm->host.gs);
2056 svm->host.ldt = kvm_read_ldt();
2057
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002058 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002059 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002060
Haozhong Zhangad7218832015-10-20 15:39:02 +08002061 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
2062 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2063 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
2064 __this_cpu_write(current_tsc_ratio, tsc_ratio);
2065 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
2066 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01002067 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01002068 /* This assumes that the kernel never uses MSR_TSC_AUX */
2069 if (static_cpu_has(X86_FEATURE_RDTSCP))
2070 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002071
Ashok Raj15d45072018-02-01 22:59:43 +01002072 if (sd->current_vmcb != svm->vmcb) {
2073 sd->current_vmcb = svm->vmcb;
2074 indirect_branch_prediction_barrier();
2075 }
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002076 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002077}
2078
2079static void svm_vcpu_put(struct kvm_vcpu *vcpu)
2080{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002081 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002082 int i;
2083
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002084 avic_vcpu_put(vcpu);
2085
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002086 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02002087 kvm_load_ldt(svm->host.ldt);
2088#ifdef CONFIG_X86_64
2089 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07002090 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01002091 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02002092#else
Avi Kivity831ca602011-03-08 16:09:51 +02002093#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02002094 loadsegment(gs, svm->host.gs);
2095#endif
Avi Kivity831ca602011-03-08 16:09:51 +02002096#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03002097 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002098 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099}
2100
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05002101static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
2102{
2103 avic_set_running(vcpu, false);
2104}
2105
2106static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
2107{
2108 avic_set_running(vcpu, true);
2109}
2110
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
2112{
Ladi Prosek9b611742017-06-21 09:06:59 +02002113 struct vcpu_svm *svm = to_svm(vcpu);
2114 unsigned long rflags = svm->vmcb->save.rflags;
2115
2116 if (svm->nmi_singlestep) {
2117 /* Hide our flags if they were not set by the guest */
2118 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
2119 rflags &= ~X86_EFLAGS_TF;
2120 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
2121 rflags &= ~X86_EFLAGS_RF;
2122 }
2123 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124}
2125
2126static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2127{
Ladi Prosek9b611742017-06-21 09:06:59 +02002128 if (to_svm(vcpu)->nmi_singlestep)
2129 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2130
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002131 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02002132 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002133 * (caused by either a task switch or an inter-privilege IRET),
2134 * so we do not need to update the CPL here.
2135 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002136 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137}
2138
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002139static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
2140{
2141 switch (reg) {
2142 case VCPU_EXREG_PDPTR:
2143 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002144 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002145 break;
2146 default:
2147 BUG();
2148 }
2149}
2150
Alexander Graff0b85052008-11-25 20:17:01 +01002151static void svm_set_vintr(struct vcpu_svm *svm)
2152{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002153 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002154}
2155
2156static void svm_clear_vintr(struct vcpu_svm *svm)
2157{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002158 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01002159}
2160
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
2162{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002163 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164
2165 switch (seg) {
2166 case VCPU_SREG_CS: return &save->cs;
2167 case VCPU_SREG_DS: return &save->ds;
2168 case VCPU_SREG_ES: return &save->es;
2169 case VCPU_SREG_FS: return &save->fs;
2170 case VCPU_SREG_GS: return &save->gs;
2171 case VCPU_SREG_SS: return &save->ss;
2172 case VCPU_SREG_TR: return &save->tr;
2173 case VCPU_SREG_LDTR: return &save->ldtr;
2174 }
2175 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00002176 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177}
2178
2179static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2180{
2181 struct vmcb_seg *s = svm_seg(vcpu, seg);
2182
2183 return s->base;
2184}
2185
2186static void svm_get_segment(struct kvm_vcpu *vcpu,
2187 struct kvm_segment *var, int seg)
2188{
2189 struct vmcb_seg *s = svm_seg(vcpu, seg);
2190
2191 var->base = s->base;
2192 var->limit = s->limit;
2193 var->selector = s->selector;
2194 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
2195 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
2196 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
2197 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
2198 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
2199 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
2200 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05302201
2202 /*
2203 * AMD CPUs circa 2014 track the G bit for all segments except CS.
2204 * However, the SVM spec states that the G bit is not observed by the
2205 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
2206 * So let's synthesize a legal G bit for all segments, this helps
2207 * running KVM nested. It also helps cross-vendor migration, because
2208 * Intel's vmentry has a check on the 'G' bit.
2209 */
2210 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00002211
Joerg Roedele0231712010-02-24 18:59:10 +01002212 /*
2213 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02002214 * for cross vendor migration purposes by "not present"
2215 */
Gioh Kim8eae9572017-05-30 15:24:45 +02002216 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02002217
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002218 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002219 case VCPU_SREG_TR:
2220 /*
2221 * Work around a bug where the busy flag in the tr selector
2222 * isn't exposed
2223 */
Amit Shahc0d09822008-10-27 09:04:18 +00002224 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002225 break;
2226 case VCPU_SREG_DS:
2227 case VCPU_SREG_ES:
2228 case VCPU_SREG_FS:
2229 case VCPU_SREG_GS:
2230 /*
2231 * The accessed bit must always be set in the segment
2232 * descriptor cache, although it can be cleared in the
2233 * descriptor, the cached bit always remains at 1. Since
2234 * Intel has a check on this, set it here to support
2235 * cross-vendor migration.
2236 */
2237 if (!var->unusable)
2238 var->type |= 0x1;
2239 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02002240 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01002241 /*
2242 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02002243 * descriptor is left as 1, although the whole segment has
2244 * been made unusable. Clear it here to pass an Intel VMX
2245 * entry check when cross vendor migrating.
2246 */
2247 if (var->unusable)
2248 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02002249 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02002250 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02002251 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01002252 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253}
2254
Izik Eidus2e4d2652008-03-24 19:38:34 +02002255static int svm_get_cpl(struct kvm_vcpu *vcpu)
2256{
2257 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
2258
2259 return save->cpl;
2260}
2261
Gleb Natapov89a27f42010-02-16 10:51:48 +02002262static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002264 struct vcpu_svm *svm = to_svm(vcpu);
2265
Gleb Natapov89a27f42010-02-16 10:51:48 +02002266 dt->size = svm->vmcb->save.idtr.limit;
2267 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268}
2269
Gleb Natapov89a27f42010-02-16 10:51:48 +02002270static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002272 struct vcpu_svm *svm = to_svm(vcpu);
2273
Gleb Natapov89a27f42010-02-16 10:51:48 +02002274 svm->vmcb->save.idtr.limit = dt->size;
2275 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002276 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277}
2278
Gleb Natapov89a27f42010-02-16 10:51:48 +02002279static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002281 struct vcpu_svm *svm = to_svm(vcpu);
2282
Gleb Natapov89a27f42010-02-16 10:51:48 +02002283 dt->size = svm->vmcb->save.gdtr.limit;
2284 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285}
2286
Gleb Natapov89a27f42010-02-16 10:51:48 +02002287static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002289 struct vcpu_svm *svm = to_svm(vcpu);
2290
Gleb Natapov89a27f42010-02-16 10:51:48 +02002291 svm->vmcb->save.gdtr.limit = dt->size;
2292 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01002293 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294}
2295
Avi Kivitye8467fd2009-12-29 18:43:06 +02002296static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2297{
2298}
2299
Avi Kivityaff48ba2010-12-05 18:56:11 +02002300static void svm_decache_cr3(struct kvm_vcpu *vcpu)
2301{
2302}
2303
Anthony Liguori25c4c272007-04-27 09:29:21 +03002304static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002305{
2306}
2307
Avi Kivityd2251572010-01-06 10:55:27 +02002308static void update_cr0_intercept(struct vcpu_svm *svm)
2309{
2310 ulong gcr0 = svm->vcpu.arch.cr0;
2311 u64 *hcr0 = &svm->vmcb->save.cr0;
2312
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002313 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
2314 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
Avi Kivityd2251572010-01-06 10:55:27 +02002315
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002316 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002317
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002318 if (gcr0 == *hcr0) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002319 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
2320 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002321 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002322 set_cr_intercept(svm, INTERCEPT_CR0_READ);
2323 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02002324 }
2325}
2326
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2328{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002329 struct vcpu_svm *svm = to_svm(vcpu);
2330
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002331#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002332 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002333 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002334 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002335 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336 }
2337
Mike Dayd77c26f2007-10-08 09:02:08 -04002338 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02002339 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06002340 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341 }
2342 }
2343#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002344 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02002345
2346 if (!npt_enabled)
2347 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02002348
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02002349 /*
2350 * re-enable caching here because the QEMU bios
2351 * does not do it - this results in some delay at
2352 * reboot
2353 */
2354 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
2355 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002356 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002357 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02002358 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359}
2360
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002361static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002363 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002364 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
2365
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002366 if (cr4 & X86_CR4_VMXE)
2367 return 1;
2368
Joerg Roedele5eab0c2008-09-09 19:11:51 +02002369 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002370 svm_flush_tlb(vcpu, true);
Joerg Roedel6394b642008-04-09 14:15:29 +02002371
Joerg Roedelec077262008-04-09 14:15:28 +02002372 vcpu->arch.cr4 = cr4;
2373 if (!npt_enabled)
2374 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02002375 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02002376 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01002377 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002378 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379}
2380
2381static void svm_set_segment(struct kvm_vcpu *vcpu,
2382 struct kvm_segment *var, int seg)
2383{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002384 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 struct vmcb_seg *s = svm_seg(vcpu, seg);
2386
2387 s->base = var->base;
2388 s->limit = var->limit;
2389 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02002390 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
2391 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
2392 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
2393 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
2394 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
2395 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
2396 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
2397 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02002398
2399 /*
2400 * This is always accurate, except if SYSRET returned to a segment
2401 * with SS.DPL != 3. Intel does not have this quirk, and always
2402 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
2403 * would entail passing the CPL to userspace and back.
2404 */
2405 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02002406 /* This is symmetric with svm_get_segment() */
2407 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408
Joerg Roedel060d0c92010-12-03 11:45:57 +01002409 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410}
2411
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01002412static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002414 struct vcpu_svm *svm = to_svm(vcpu);
2415
Joerg Roedel18c918c2010-11-30 18:03:59 +01002416 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03002417
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002418 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002419 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01002420 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002421 } else
2422 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03002423}
2424
Tejun Heo0fe1e002009-10-29 22:34:14 +09002425static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426{
Tejun Heo0fe1e002009-10-29 22:34:14 +09002427 if (sd->next_asid > sd->max_asid) {
2428 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06002429 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002430 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431 }
2432
Tejun Heo0fe1e002009-10-29 22:34:14 +09002433 svm->asid_generation = sd->asid_generation;
2434 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01002435
2436 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002437}
2438
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01002439static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
2440{
2441 return to_svm(vcpu)->vmcb->save.dr6;
2442}
2443
2444static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
2445{
2446 struct vcpu_svm *svm = to_svm(vcpu);
2447
2448 svm->vmcb->save.dr6 = value;
2449 mark_dirty(svm->vmcb, VMCB_DR);
2450}
2451
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002452static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
2453{
2454 struct vcpu_svm *svm = to_svm(vcpu);
2455
2456 get_debugreg(vcpu->arch.db[0], 0);
2457 get_debugreg(vcpu->arch.db[1], 1);
2458 get_debugreg(vcpu->arch.db[2], 2);
2459 get_debugreg(vcpu->arch.db[3], 3);
2460 vcpu->arch.dr6 = svm_get_dr6(vcpu);
2461 vcpu->arch.dr7 = svm->vmcb->save.dr7;
2462
2463 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
2464 set_dr_intercepts(svm);
2465}
2466
Gleb Natapov020df072010-04-13 10:05:23 +03002467static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002469 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002470
Gleb Natapov020df072010-04-13 10:05:23 +03002471 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01002472 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473}
2474
Avi Kivity851ba692009-08-24 11:10:17 +03002475static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002477 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002478 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002479
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002480 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002481 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2482 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002483 svm->vmcb->control.insn_len);
2484}
2485
2486static int npf_interception(struct vcpu_svm *svm)
2487{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002488 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Paolo Bonzinid0006532017-08-11 18:36:43 +02002489 u64 error_code = svm->vmcb->control.exit_info_1;
2490
2491 trace_kvm_page_fault(fault_address, error_code);
2492 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002493 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2494 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002495 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496}
2497
Avi Kivity851ba692009-08-24 11:10:17 +03002498static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002499{
Avi Kivity851ba692009-08-24 11:10:17 +03002500 struct kvm_run *kvm_run = svm->vcpu.run;
2501
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002502 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03002503 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02002504 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002505 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2506 return 1;
2507 }
Gleb Natapov44c11432009-05-11 13:35:52 +03002508
Jan Kiszka6be7d302009-10-18 13:24:54 +02002509 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02002510 disable_nmi_singlestep(svm);
Gleb Natapov44c11432009-05-11 13:35:52 +03002511 }
2512
2513 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002514 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002515 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2516 kvm_run->debug.arch.pc =
2517 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2518 kvm_run->debug.arch.exception = DB_VECTOR;
2519 return 0;
2520 }
2521
2522 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002523}
2524
Avi Kivity851ba692009-08-24 11:10:17 +03002525static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002526{
Avi Kivity851ba692009-08-24 11:10:17 +03002527 struct kvm_run *kvm_run = svm->vcpu.run;
2528
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002529 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2530 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2531 kvm_run->debug.arch.exception = BP_VECTOR;
2532 return 0;
2533}
2534
Avi Kivity851ba692009-08-24 11:10:17 +03002535static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002536{
2537 int er;
2538
Andre Przywara51d8b662010-12-21 11:12:02 +01002539 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02002540 if (er == EMULATE_USER_EXIT)
2541 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002542 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002543 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002544 return 1;
2545}
2546
Eric Northup54a20552015-11-03 18:03:53 +01002547static int ac_interception(struct vcpu_svm *svm)
2548{
2549 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2550 return 1;
2551}
2552
Joerg Roedel67ec6602010-05-17 14:43:35 +02002553static bool is_erratum_383(void)
2554{
2555 int err, i;
2556 u64 value;
2557
2558 if (!erratum_383_found)
2559 return false;
2560
2561 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2562 if (err)
2563 return false;
2564
2565 /* Bit 62 may or may not be set for this mce */
2566 value &= ~(1ULL << 62);
2567
2568 if (value != 0xb600000000010015ULL)
2569 return false;
2570
2571 /* Clear MCi_STATUS registers */
2572 for (i = 0; i < 6; ++i)
2573 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2574
2575 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2576 if (!err) {
2577 u32 low, high;
2578
2579 value &= ~(1ULL << 2);
2580 low = lower_32_bits(value);
2581 high = upper_32_bits(value);
2582
2583 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2584 }
2585
2586 /* Flush tlb to evict multi-match entries */
2587 __flush_tlb_all();
2588
2589 return true;
2590}
2591
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002592static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002593{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002594 if (is_erratum_383()) {
2595 /*
2596 * Erratum 383 triggered. Guest state is corrupt so kill the
2597 * guest.
2598 */
2599 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2600
Avi Kivitya8eeb042010-05-10 12:34:53 +03002601 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002602
2603 return;
2604 }
2605
Joerg Roedel53371b52008-04-09 14:15:30 +02002606 /*
2607 * On an #MC intercept the MCE handler is not called automatically in
2608 * the host. So do it by hand here.
2609 */
2610 asm volatile (
2611 "int $0x12\n");
2612 /* not sure if we ever come back to this point */
2613
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002614 return;
2615}
2616
2617static int mc_interception(struct vcpu_svm *svm)
2618{
Joerg Roedel53371b52008-04-09 14:15:30 +02002619 return 1;
2620}
2621
Avi Kivity851ba692009-08-24 11:10:17 +03002622static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002623{
Avi Kivity851ba692009-08-24 11:10:17 +03002624 struct kvm_run *kvm_run = svm->vcpu.run;
2625
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002626 /*
2627 * VMCB is undefined after a SHUTDOWN intercept
2628 * so reinitialize it.
2629 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002630 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002631 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002632
2633 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2634 return 0;
2635}
2636
Avi Kivity851ba692009-08-24 11:10:17 +03002637static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002639 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002640 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002641 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02002642 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643
Rusty Russelle756fc62007-07-30 20:07:08 +10002644 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002645 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002646 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002647 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01002648 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002649
Avi Kivity039576c2007-03-20 12:46:50 +02002650 port = io_info >> 16;
2651 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002652 svm->next_rip = svm->vmcb->control.exit_info_2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002653 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002654
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002655 /*
2656 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
2657 * KVM_EXIT_DEBUG here.
2658 */
2659 if (in)
2660 return kvm_fast_pio_in(vcpu, size, port) && ret;
2661 else
2662 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663}
2664
Avi Kivity851ba692009-08-24 11:10:17 +03002665static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002666{
2667 return 1;
2668}
2669
Avi Kivity851ba692009-08-24 11:10:17 +03002670static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002671{
2672 ++svm->vcpu.stat.irq_exits;
2673 return 1;
2674}
2675
Avi Kivity851ba692009-08-24 11:10:17 +03002676static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677{
2678 return 1;
2679}
2680
Avi Kivity851ba692009-08-24 11:10:17 +03002681static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002683 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002684 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685}
2686
Avi Kivity851ba692009-08-24 11:10:17 +03002687static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002688{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002689 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002690 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002691}
2692
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002693static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2694{
2695 struct vcpu_svm *svm = to_svm(vcpu);
2696
2697 return svm->nested.nested_cr3;
2698}
2699
Avi Kivitye4e517b2011-07-28 11:36:17 +03002700static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2701{
2702 struct vcpu_svm *svm = to_svm(vcpu);
2703 u64 cr3 = svm->nested.nested_cr3;
2704 u64 pdpte;
2705 int ret;
2706
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002707 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte,
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002708 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002709 if (ret)
2710 return 0;
2711 return pdpte;
2712}
2713
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002714static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2715 unsigned long root)
2716{
2717 struct vcpu_svm *svm = to_svm(vcpu);
2718
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05002719 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01002720 mark_dirty(svm->vmcb, VMCB_NPT);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08002721 svm_flush_tlb(vcpu, true);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002722}
2723
Avi Kivity6389ee92010-11-29 16:12:30 +02002724static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2725 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002726{
2727 struct vcpu_svm *svm = to_svm(vcpu);
2728
Paolo Bonzini5e352512014-09-02 13:18:37 +02002729 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2730 /*
2731 * TODO: track the cause of the nested page fault, and
2732 * correctly fill in the high bits of exit_info_1.
2733 */
2734 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2735 svm->vmcb->control.exit_code_hi = 0;
2736 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2737 svm->vmcb->control.exit_info_2 = fault->address;
2738 }
2739
2740 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2741 svm->vmcb->control.exit_info_1 |= fault->error_code;
2742
2743 /*
2744 * The present bit is always zero for page structure faults on real
2745 * hardware.
2746 */
2747 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2748 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002749
2750 nested_svm_vmexit(svm);
2751}
2752
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002753static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002754{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002755 WARN_ON(mmu_is_nested(vcpu));
2756 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002757 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2758 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002759 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002760 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
Yu Zhang855feb62017-08-24 20:27:55 +08002761 vcpu->arch.mmu.shadow_root_level = get_npt_level(vcpu);
Xiao Guangrongc258b622015-08-05 12:04:24 +08002762 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002763 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002764}
2765
2766static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2767{
2768 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2769}
2770
Alexander Grafc0725422008-11-25 20:17:03 +01002771static int nested_svm_check_permissions(struct vcpu_svm *svm)
2772{
Dan Carpentere9196ce2017-05-18 10:39:53 +03002773 if (!(svm->vcpu.arch.efer & EFER_SVME) ||
2774 !is_paging(&svm->vcpu)) {
Alexander Grafc0725422008-11-25 20:17:03 +01002775 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2776 return 1;
2777 }
2778
2779 if (svm->vmcb->save.cpl) {
2780 kvm_inject_gp(&svm->vcpu, 0);
2781 return 1;
2782 }
2783
Dan Carpentere9196ce2017-05-18 10:39:53 +03002784 return 0;
Alexander Grafc0725422008-11-25 20:17:03 +01002785}
2786
Alexander Grafcf74a782008-11-25 20:17:08 +01002787static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2788 bool has_error_code, u32 error_code)
2789{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002790 int vmexit;
2791
Joerg Roedel20307532010-11-29 17:51:48 +01002792 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002793 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002794
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002795 vmexit = nested_svm_intercept(svm);
2796 if (vmexit != NESTED_EXIT_DONE)
2797 return 0;
2798
Joerg Roedel0295ad72009-08-07 11:49:37 +02002799 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2800 svm->vmcb->control.exit_code_hi = 0;
2801 svm->vmcb->control.exit_info_1 = error_code;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002802
2803 /*
2804 * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception.
2805 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2806 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be
2807 * written only when inject_pending_event runs (DR6 would written here
2808 * too). This should be conditional on a new capability---if the
2809 * capability is disabled, kvm_multiple_exception would write the
2810 * ancillary information to CR2 or DR6, for backwards ABI-compatibility.
2811 */
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002812 if (svm->vcpu.arch.exception.nested_apf)
2813 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
2814 else
2815 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
Joerg Roedel0295ad72009-08-07 11:49:37 +02002816
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002817 svm->nested.exit_required = true;
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002818 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002819}
2820
Joerg Roedel8fe54652010-02-19 16:23:01 +01002821/* This function returns true if it is save to enable the irq window */
2822static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002823{
Joerg Roedel20307532010-11-29 17:51:48 +01002824 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002825 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002826
Joerg Roedel26666952009-08-07 11:49:46 +02002827 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002828 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002829
Joerg Roedel26666952009-08-07 11:49:46 +02002830 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002831 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002832
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002833 /*
2834 * if vmexit was already requested (by intercepted exception
2835 * for instance) do not overwrite it with "external interrupt"
2836 * vmexit.
2837 */
2838 if (svm->nested.exit_required)
2839 return false;
2840
Joerg Roedel197717d2010-02-24 18:59:19 +01002841 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2842 svm->vmcb->control.exit_info_1 = 0;
2843 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002844
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002845 if (svm->nested.intercept & 1ULL) {
2846 /*
2847 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002848 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002849 * #vmexit emulation might sleep. Only signal request for
2850 * the #vmexit here.
2851 */
2852 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002853 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002854 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002855 }
2856
Joerg Roedel8fe54652010-02-19 16:23:01 +01002857 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002858}
2859
Joerg Roedel887f5002010-02-24 18:59:12 +01002860/* This function returns true if it is save to enable the nmi window */
2861static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2862{
Joerg Roedel20307532010-11-29 17:51:48 +01002863 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002864 return true;
2865
2866 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2867 return true;
2868
2869 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2870 svm->nested.exit_required = true;
2871
2872 return false;
2873}
2874
Joerg Roedel7597f122010-02-19 16:23:00 +01002875static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002876{
2877 struct page *page;
2878
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002879 might_sleep();
2880
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002881 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002882 if (is_error_page(page))
2883 goto error;
2884
Joerg Roedel7597f122010-02-19 16:23:00 +01002885 *_page = page;
2886
2887 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002888
2889error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002890 kvm_inject_gp(&svm->vcpu, 0);
2891
2892 return NULL;
2893}
2894
Joerg Roedel7597f122010-02-19 16:23:00 +01002895static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002896{
Joerg Roedel7597f122010-02-19 16:23:00 +01002897 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002898 kvm_release_page_dirty(page);
2899}
2900
Joerg Roedelce2ac082010-03-01 15:34:39 +01002901static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002902{
Jan Kiszka9bf41832014-06-30 10:54:17 +02002903 unsigned port, size, iopm_len;
2904 u16 val, mask;
2905 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002906 u64 gpa;
2907
2908 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2909 return NESTED_EXIT_HOST;
2910
2911 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02002912 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2913 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002914 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02002915 start_bit = port % 8;
2916 iopm_len = (start_bit + size > 8) ? 2 : 1;
2917 mask = (0xf >> (4 - size)) << start_bit;
2918 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002919
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002920 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02002921 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002922
Jan Kiszka9bf41832014-06-30 10:54:17 +02002923 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002924}
2925
Joerg Roedeld2477822010-03-01 15:34:34 +01002926static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002927{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002928 u32 offset, msr, value;
2929 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002930
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002931 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002932 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002933
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002934 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2935 offset = svm_msrpm_offset(msr);
2936 write = svm->vmcb->control.exit_info_1 & 1;
2937 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002938
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002939 if (offset == MSR_INVALID)
2940 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002941
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002942 /* Offset is in 32 bit units but need in 8 bit units */
2943 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002944
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002945 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002946 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002947
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002948 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002949}
2950
Ladi Prosekab2f4d732017-06-21 09:06:58 +02002951/* DB exceptions for our internal use must not cause vmexit */
2952static int nested_svm_intercept_db(struct vcpu_svm *svm)
2953{
2954 unsigned long dr6;
2955
2956 /* if we're not singlestepping, it's not ours */
2957 if (!svm->nmi_singlestep)
2958 return NESTED_EXIT_DONE;
2959
2960 /* if it's not a singlestep exception, it's not ours */
2961 if (kvm_get_dr(&svm->vcpu, 6, &dr6))
2962 return NESTED_EXIT_DONE;
2963 if (!(dr6 & DR6_BS))
2964 return NESTED_EXIT_DONE;
2965
2966 /* if the guest is singlestepping, it should get the vmexit */
2967 if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
2968 disable_nmi_singlestep(svm);
2969 return NESTED_EXIT_DONE;
2970 }
2971
2972 /* it's ours, the nested hypervisor must not see this one */
2973 return NESTED_EXIT_HOST;
2974}
2975
Joerg Roedel410e4d52009-08-07 11:49:44 +02002976static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002977{
Alexander Grafcf74a782008-11-25 20:17:08 +01002978 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002979
Joerg Roedel410e4d52009-08-07 11:49:44 +02002980 switch (exit_code) {
2981 case SVM_EXIT_INTR:
2982 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002983 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002984 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002985 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002986 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002987 if (npt_enabled)
2988 return NESTED_EXIT_HOST;
2989 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002990 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002991 /* When we're shadowing, trap PFs, but not async PF */
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002992 if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002993 return NESTED_EXIT_HOST;
2994 break;
2995 default:
2996 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002997 }
2998
Joerg Roedel410e4d52009-08-07 11:49:44 +02002999 return NESTED_EXIT_CONTINUE;
3000}
3001
3002/*
3003 * If this function returns true, this #vmexit was already handled
3004 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003005static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003006{
3007 u32 exit_code = svm->vmcb->control.exit_code;
3008 int vmexit = NESTED_EXIT_HOST;
3009
Alexander Grafcf74a782008-11-25 20:17:08 +01003010 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003011 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02003012 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003013 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003014 case SVM_EXIT_IOIO:
3015 vmexit = nested_svm_intercept_ioio(svm);
3016 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003017 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
3018 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
3019 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003020 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003021 break;
3022 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01003023 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
3024 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
3025 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003026 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003027 break;
3028 }
3029 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
3030 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003031 if (svm->nested.intercept_exceptions & excp_bits) {
3032 if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
3033 vmexit = nested_svm_intercept_db(svm);
3034 else
3035 vmexit = NESTED_EXIT_DONE;
3036 }
Gleb Natapov631bc482010-10-14 11:22:52 +02003037 /* async page fault always cause vmexit */
3038 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003039 svm->vcpu.arch.exception.nested_apf != 0)
Gleb Natapov631bc482010-10-14 11:22:52 +02003040 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003041 break;
3042 }
Joerg Roedel228070b2010-04-22 12:33:10 +02003043 case SVM_EXIT_ERR: {
3044 vmexit = NESTED_EXIT_DONE;
3045 break;
3046 }
Alexander Grafcf74a782008-11-25 20:17:08 +01003047 default: {
3048 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02003049 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02003050 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01003051 }
3052 }
3053
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01003054 return vmexit;
3055}
3056
3057static int nested_svm_exit_handled(struct vcpu_svm *svm)
3058{
3059 int vmexit;
3060
3061 vmexit = nested_svm_intercept(svm);
3062
3063 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003064 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02003065
3066 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01003067}
3068
Joerg Roedel0460a972009-08-07 11:49:31 +02003069static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
3070{
3071 struct vmcb_control_area *dst = &dst_vmcb->control;
3072 struct vmcb_control_area *from = &from_vmcb->control;
3073
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003074 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003075 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02003076 dst->intercept_exceptions = from->intercept_exceptions;
3077 dst->intercept = from->intercept;
3078 dst->iopm_base_pa = from->iopm_base_pa;
3079 dst->msrpm_base_pa = from->msrpm_base_pa;
3080 dst->tsc_offset = from->tsc_offset;
3081 dst->asid = from->asid;
3082 dst->tlb_ctl = from->tlb_ctl;
3083 dst->int_ctl = from->int_ctl;
3084 dst->int_vector = from->int_vector;
3085 dst->int_state = from->int_state;
3086 dst->exit_code = from->exit_code;
3087 dst->exit_code_hi = from->exit_code_hi;
3088 dst->exit_info_1 = from->exit_info_1;
3089 dst->exit_info_2 = from->exit_info_2;
3090 dst->exit_int_info = from->exit_int_info;
3091 dst->exit_int_info_err = from->exit_int_info_err;
3092 dst->nested_ctl = from->nested_ctl;
3093 dst->event_inj = from->event_inj;
3094 dst->event_inj_err = from->event_inj_err;
3095 dst->nested_cr3 = from->nested_cr3;
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003096 dst->virt_ext = from->virt_ext;
Joerg Roedel0460a972009-08-07 11:49:31 +02003097}
3098
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003099static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01003100{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003101 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003102 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02003103 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003104 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01003105
Joerg Roedel17897f32009-10-09 16:08:29 +02003106 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
3107 vmcb->control.exit_info_1,
3108 vmcb->control.exit_info_2,
3109 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003110 vmcb->control.exit_int_info_err,
3111 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02003112
Joerg Roedel7597f122010-02-19 16:23:00 +01003113 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02003114 if (!nested_vmcb)
3115 return 1;
3116
Joerg Roedel20307532010-11-29 17:51:48 +01003117 /* Exit Guest-Mode */
3118 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01003119 svm->nested.vmcb = 0;
3120
Alexander Grafcf74a782008-11-25 20:17:08 +01003121 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02003122 disable_gif(svm);
3123
3124 nested_vmcb->save.es = vmcb->save.es;
3125 nested_vmcb->save.cs = vmcb->save.cs;
3126 nested_vmcb->save.ss = vmcb->save.ss;
3127 nested_vmcb->save.ds = vmcb->save.ds;
3128 nested_vmcb->save.gdtr = vmcb->save.gdtr;
3129 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02003130 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003131 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003132 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003133 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01003134 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03003135 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02003136 nested_vmcb->save.rip = vmcb->save.rip;
3137 nested_vmcb->save.rsp = vmcb->save.rsp;
3138 nested_vmcb->save.rax = vmcb->save.rax;
3139 nested_vmcb->save.dr7 = vmcb->save.dr7;
3140 nested_vmcb->save.dr6 = vmcb->save.dr6;
3141 nested_vmcb->save.cpl = vmcb->save.cpl;
3142
3143 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
3144 nested_vmcb->control.int_vector = vmcb->control.int_vector;
3145 nested_vmcb->control.int_state = vmcb->control.int_state;
3146 nested_vmcb->control.exit_code = vmcb->control.exit_code;
3147 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
3148 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
3149 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
3150 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
3151 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003152
3153 if (svm->nrips_enabled)
3154 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02003155
3156 /*
3157 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
3158 * to make sure that we do not lose injected events. So check event_inj
3159 * here and copy it to exit_int_info if it is valid.
3160 * Exit_int_info and event_inj can't be both valid because the case
3161 * below only happens on a VMRUN instruction intercept which has
3162 * no valid exit_int_info set.
3163 */
3164 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
3165 struct vmcb_control_area *nc = &nested_vmcb->control;
3166
3167 nc->exit_int_info = vmcb->control.event_inj;
3168 nc->exit_int_info_err = vmcb->control.event_inj_err;
3169 }
3170
Joerg Roedel33740e42009-08-07 11:49:29 +02003171 nested_vmcb->control.tlb_ctl = 0;
3172 nested_vmcb->control.event_inj = 0;
3173 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01003174
3175 /* We always set V_INTR_MASKING and remember the old value in hflags */
3176 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
3177 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
3178
Alexander Grafcf74a782008-11-25 20:17:08 +01003179 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02003180 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01003181
Alexander Graf219b65d2009-06-15 15:21:25 +02003182 kvm_clear_exception_queue(&svm->vcpu);
3183 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003184
Joerg Roedel4b161842010-09-10 17:31:03 +02003185 svm->nested.nested_cr3 = 0;
3186
Alexander Grafcf74a782008-11-25 20:17:08 +01003187 /* Restore selected save entries */
3188 svm->vmcb->save.es = hsave->save.es;
3189 svm->vmcb->save.cs = hsave->save.cs;
3190 svm->vmcb->save.ss = hsave->save.ss;
3191 svm->vmcb->save.ds = hsave->save.ds;
3192 svm->vmcb->save.gdtr = hsave->save.gdtr;
3193 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003194 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01003195 svm_set_efer(&svm->vcpu, hsave->save.efer);
3196 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
3197 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
3198 if (npt_enabled) {
3199 svm->vmcb->save.cr3 = hsave->save.cr3;
3200 svm->vcpu.arch.cr3 = hsave->save.cr3;
3201 } else {
Avi Kivity23902182010-06-10 17:02:16 +03003202 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01003203 }
3204 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
3205 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
3206 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
3207 svm->vmcb->save.dr7 = 0;
3208 svm->vmcb->save.cpl = 0;
3209 svm->vmcb->control.exit_int_info = 0;
3210
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003211 mark_all_dirty(svm->vmcb);
3212
Joerg Roedel7597f122010-02-19 16:23:00 +01003213 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01003214
Joerg Roedel4b161842010-09-10 17:31:03 +02003215 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01003216 kvm_mmu_reset_context(&svm->vcpu);
3217 kvm_mmu_load(&svm->vcpu);
3218
3219 return 0;
3220}
Alexander Graf3d6368e2008-11-25 20:17:07 +01003221
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003222static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003223{
Joerg Roedel323c3d82010-03-01 15:34:37 +01003224 /*
3225 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08003226 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01003227 * the kvm msr permission bitmap may contain zero bits
3228 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01003229 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003230
Joerg Roedel323c3d82010-03-01 15:34:37 +01003231 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
3232 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003233
Joerg Roedel323c3d82010-03-01 15:34:37 +01003234 for (i = 0; i < MSRPM_OFFSETS; i++) {
3235 u32 value, p;
3236 u64 offset;
3237
3238 if (msrpm_offsets[i] == 0xffffffff)
3239 break;
3240
Joerg Roedel0d6b3532010-03-01 15:34:38 +01003241 p = msrpm_offsets[i];
3242 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01003243
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02003244 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01003245 return false;
3246
3247 svm->nested.msrpm[p] = svm->msrpm[p] | value;
3248 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003249
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05003250 svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm));
Alexander Graf3d6368e2008-11-25 20:17:07 +01003251
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003252 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003253}
3254
Joerg Roedel52c65a302010-08-02 16:46:44 +02003255static bool nested_vmcb_checks(struct vmcb *vmcb)
3256{
3257 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
3258 return false;
3259
Joerg Roedeldbe77582010-08-02 16:46:45 +02003260 if (vmcb->control.asid == 0)
3261 return false;
3262
Tom Lendackycea3a192017-12-04 10:57:24 -06003263 if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) &&
3264 !npt_enabled)
Joerg Roedel4b161842010-09-10 17:31:03 +02003265 return false;
3266
Joerg Roedel52c65a302010-08-02 16:46:44 +02003267 return true;
3268}
3269
Ladi Prosekc2634062017-10-11 16:54:44 +02003270static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
3271 struct vmcb *nested_vmcb, struct page *page)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003272{
Avi Kivityf6e78472010-08-02 15:30:20 +03003273 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003274 svm->vcpu.arch.hflags |= HF_HIF_MASK;
3275 else
3276 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
3277
Tom Lendackycea3a192017-12-04 10:57:24 -06003278 if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) {
Joerg Roedel4b161842010-09-10 17:31:03 +02003279 kvm_mmu_unload(&svm->vcpu);
3280 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
3281 nested_svm_init_mmu_context(&svm->vcpu);
3282 }
3283
Alexander Graf3d6368e2008-11-25 20:17:07 +01003284 /* Load the nested guest state */
3285 svm->vmcb->save.es = nested_vmcb->save.es;
3286 svm->vmcb->save.cs = nested_vmcb->save.cs;
3287 svm->vmcb->save.ss = nested_vmcb->save.ss;
3288 svm->vmcb->save.ds = nested_vmcb->save.ds;
3289 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
3290 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03003291 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003292 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
3293 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
3294 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
3295 if (npt_enabled) {
3296 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
3297 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003298 } else
Avi Kivity23902182010-06-10 17:02:16 +03003299 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01003300
3301 /* Guest paging mode is active - reset mmu */
3302 kvm_mmu_reset_context(&svm->vcpu);
3303
Joerg Roedeldefbba52009-08-07 11:49:30 +02003304 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003305 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
3306 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
3307 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01003308
Alexander Graf3d6368e2008-11-25 20:17:07 +01003309 /* In case we don't even reach vcpu_run, the fields are not updated */
3310 svm->vmcb->save.rax = nested_vmcb->save.rax;
3311 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
3312 svm->vmcb->save.rip = nested_vmcb->save.rip;
3313 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
3314 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
3315 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
3316
Joerg Roedelf7138532010-03-01 15:34:40 +01003317 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01003318 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003319
Joerg Roedelaad42c62009-08-07 11:49:34 +02003320 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003321 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01003322 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02003323 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
3324 svm->nested.intercept = nested_vmcb->control.intercept;
3325
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08003326 svm_flush_tlb(&svm->vcpu, true);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003327 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003328 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
3329 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
3330 else
3331 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
3332
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003333 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
3334 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003335 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
3336 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003337 }
3338
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003339 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003340 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02003341
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003342 svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003343 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
3344 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
3345 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003346 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
3347 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
3348
Joerg Roedel7597f122010-02-19 16:23:00 +01003349 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003350
Joerg Roedel20307532010-11-29 17:51:48 +01003351 /* Enter Guest-Mode */
3352 enter_guest_mode(&svm->vcpu);
3353
Joerg Roedel384c6362010-11-30 18:03:56 +01003354 /*
3355 * Merge guest and host intercepts - must be called with vcpu in
3356 * guest-mode to take affect here
3357 */
3358 recalc_intercepts(svm);
3359
Joerg Roedel06fc77722010-02-19 16:23:07 +01003360 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003361
Joerg Roedel2af91942009-08-07 11:49:28 +02003362 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003363
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003364 mark_all_dirty(svm->vmcb);
Ladi Prosekc2634062017-10-11 16:54:44 +02003365}
3366
3367static bool nested_svm_vmrun(struct vcpu_svm *svm)
3368{
3369 struct vmcb *nested_vmcb;
3370 struct vmcb *hsave = svm->nested.hsave;
3371 struct vmcb *vmcb = svm->vmcb;
3372 struct page *page;
3373 u64 vmcb_gpa;
3374
3375 vmcb_gpa = svm->vmcb->save.rax;
3376
3377 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
3378 if (!nested_vmcb)
3379 return false;
3380
3381 if (!nested_vmcb_checks(nested_vmcb)) {
3382 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
3383 nested_vmcb->control.exit_code_hi = 0;
3384 nested_vmcb->control.exit_info_1 = 0;
3385 nested_vmcb->control.exit_info_2 = 0;
3386
3387 nested_svm_unmap(page);
3388
3389 return false;
3390 }
3391
3392 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
3393 nested_vmcb->save.rip,
3394 nested_vmcb->control.int_ctl,
3395 nested_vmcb->control.event_inj,
3396 nested_vmcb->control.nested_ctl);
3397
3398 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
3399 nested_vmcb->control.intercept_cr >> 16,
3400 nested_vmcb->control.intercept_exceptions,
3401 nested_vmcb->control.intercept);
3402
3403 /* Clear internal status */
3404 kvm_clear_exception_queue(&svm->vcpu);
3405 kvm_clear_interrupt_queue(&svm->vcpu);
3406
3407 /*
3408 * Save the old vmcb, so we don't need to pick what we save, but can
3409 * restore everything when a VMEXIT occurs
3410 */
3411 hsave->save.es = vmcb->save.es;
3412 hsave->save.cs = vmcb->save.cs;
3413 hsave->save.ss = vmcb->save.ss;
3414 hsave->save.ds = vmcb->save.ds;
3415 hsave->save.gdtr = vmcb->save.gdtr;
3416 hsave->save.idtr = vmcb->save.idtr;
3417 hsave->save.efer = svm->vcpu.arch.efer;
3418 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
3419 hsave->save.cr4 = svm->vcpu.arch.cr4;
3420 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
3421 hsave->save.rip = kvm_rip_read(&svm->vcpu);
3422 hsave->save.rsp = vmcb->save.rsp;
3423 hsave->save.rax = vmcb->save.rax;
3424 if (npt_enabled)
3425 hsave->save.cr3 = vmcb->save.cr3;
3426 else
3427 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
3428
3429 copy_vmcb_control_area(hsave, vmcb);
3430
3431 enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003432
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003433 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01003434}
3435
Joerg Roedel9966bf62009-08-07 11:49:40 +02003436static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01003437{
3438 to_vmcb->save.fs = from_vmcb->save.fs;
3439 to_vmcb->save.gs = from_vmcb->save.gs;
3440 to_vmcb->save.tr = from_vmcb->save.tr;
3441 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
3442 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
3443 to_vmcb->save.star = from_vmcb->save.star;
3444 to_vmcb->save.lstar = from_vmcb->save.lstar;
3445 to_vmcb->save.cstar = from_vmcb->save.cstar;
3446 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
3447 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
3448 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
3449 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01003450}
3451
Avi Kivity851ba692009-08-24 11:10:17 +03003452static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003453{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003454 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003455 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003456 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003457
Alexander Graf55426752008-11-25 20:17:06 +01003458 if (nested_svm_check_permissions(svm))
3459 return 1;
3460
Joerg Roedel7597f122010-02-19 16:23:00 +01003461 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003462 if (!nested_vmcb)
3463 return 1;
3464
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003465 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003466 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003467
Joerg Roedel9966bf62009-08-07 11:49:40 +02003468 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003469 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003470
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003471 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003472}
3473
Avi Kivity851ba692009-08-24 11:10:17 +03003474static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01003475{
Joerg Roedel9966bf62009-08-07 11:49:40 +02003476 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01003477 struct page *page;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003478 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02003479
Alexander Graf55426752008-11-25 20:17:06 +01003480 if (nested_svm_check_permissions(svm))
3481 return 1;
3482
Joerg Roedel7597f122010-02-19 16:23:00 +01003483 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02003484 if (!nested_vmcb)
3485 return 1;
3486
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003487 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003488 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02003489
Joerg Roedel9966bf62009-08-07 11:49:40 +02003490 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01003491 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01003492
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003493 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01003494}
3495
Avi Kivity851ba692009-08-24 11:10:17 +03003496static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01003497{
Alexander Graf3d6368e2008-11-25 20:17:07 +01003498 if (nested_svm_check_permissions(svm))
3499 return 1;
3500
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02003501 /* Save rip after vmrun instruction */
3502 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003503
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003504 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01003505 return 1;
3506
Joerg Roedel9738b2c2009-08-07 11:49:41 +02003507 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02003508 goto failed;
3509
3510 return 1;
3511
3512failed:
3513
3514 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
3515 svm->vmcb->control.exit_code_hi = 0;
3516 svm->vmcb->control.exit_info_1 = 0;
3517 svm->vmcb->control.exit_info_2 = 0;
3518
3519 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01003520
3521 return 1;
3522}
3523
Avi Kivity851ba692009-08-24 11:10:17 +03003524static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003525{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003526 int ret;
3527
Alexander Graf1371d902008-11-25 20:17:04 +01003528 if (nested_svm_check_permissions(svm))
3529 return 1;
3530
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003531 /*
3532 * If VGIF is enabled, the STGI intercept is only added to
Ladi Prosekcc3d9672017-10-17 16:02:39 +02003533 * detect the opening of the SMI/NMI window; remove it now.
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003534 */
3535 if (vgif_enabled(svm))
3536 clr_intercept(svm, INTERCEPT_STGI);
3537
Alexander Graf1371d902008-11-25 20:17:04 +01003538 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003539 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003540 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003541
Joerg Roedel2af91942009-08-07 11:49:28 +02003542 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003543
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003544 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003545}
3546
Avi Kivity851ba692009-08-24 11:10:17 +03003547static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01003548{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003549 int ret;
3550
Alexander Graf1371d902008-11-25 20:17:04 +01003551 if (nested_svm_check_permissions(svm))
3552 return 1;
3553
3554 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003555 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01003556
Joerg Roedel2af91942009-08-07 11:49:28 +02003557 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01003558
3559 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05003560 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
3561 svm_clear_vintr(svm);
3562 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
3563 mark_dirty(svm->vmcb, VMCB_INTR);
3564 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003565
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003566 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01003567}
3568
Avi Kivity851ba692009-08-24 11:10:17 +03003569static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02003570{
3571 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02003572
David Kaplan668f1982015-02-20 16:02:10 -06003573 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
3574 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02003575
Alexander Grafff092382009-06-15 15:21:24 +02003576 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06003577 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02003578
3579 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003580 return kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02003581}
3582
Joerg Roedel532a46b2009-10-09 16:08:32 +02003583static int skinit_interception(struct vcpu_svm *svm)
3584{
David Kaplan668f1982015-02-20 16:02:10 -06003585 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003586
3587 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3588 return 1;
3589}
3590
David Kaplandab429a2015-03-02 13:43:37 -06003591static int wbinvd_interception(struct vcpu_svm *svm)
3592{
Kyle Huey6affcbe2016-11-29 12:40:40 -08003593 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06003594}
3595
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003596static int xsetbv_interception(struct vcpu_svm *svm)
3597{
3598 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
3599 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3600
3601 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3602 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003603 return kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003604 }
3605
3606 return 1;
3607}
3608
Avi Kivity851ba692009-08-24 11:10:17 +03003609static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003610{
Izik Eidus37817f22008-03-24 23:14:53 +02003611 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003612 int reason;
3613 int int_type = svm->vmcb->control.exit_int_info &
3614 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003615 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003616 uint32_t type =
3617 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3618 uint32_t idt_v =
3619 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003620 bool has_error_code = false;
3621 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003622
3623 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003624
Izik Eidus37817f22008-03-24 23:14:53 +02003625 if (svm->vmcb->control.exit_info_2 &
3626 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003627 reason = TASK_SWITCH_IRET;
3628 else if (svm->vmcb->control.exit_info_2 &
3629 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3630 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003631 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003632 reason = TASK_SWITCH_GATE;
3633 else
3634 reason = TASK_SWITCH_CALL;
3635
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003636 if (reason == TASK_SWITCH_GATE) {
3637 switch (type) {
3638 case SVM_EXITINTINFO_TYPE_NMI:
3639 svm->vcpu.arch.nmi_injected = false;
3640 break;
3641 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003642 if (svm->vmcb->control.exit_info_2 &
3643 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3644 has_error_code = true;
3645 error_code =
3646 (u32)svm->vmcb->control.exit_info_2;
3647 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003648 kvm_clear_exception_queue(&svm->vcpu);
3649 break;
3650 case SVM_EXITINTINFO_TYPE_INTR:
3651 kvm_clear_interrupt_queue(&svm->vcpu);
3652 break;
3653 default:
3654 break;
3655 }
3656 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003657
Gleb Natapov8317c292009-04-12 13:37:02 +03003658 if (reason != TASK_SWITCH_GATE ||
3659 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3660 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03003661 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3662 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003663
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003664 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3665 int_vec = -1;
3666
3667 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03003668 has_error_code, error_code) == EMULATE_FAIL) {
3669 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3670 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3671 svm->vcpu.run->internal.ndata = 0;
3672 return 0;
3673 }
3674 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003675}
3676
Avi Kivity851ba692009-08-24 11:10:17 +03003677static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003678{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003679 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Kyle Huey6a908b62016-11-29 12:40:37 -08003680 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003681}
3682
Avi Kivity851ba692009-08-24 11:10:17 +03003683static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003684{
3685 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003686 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003687 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003688 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003689 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003690 return 1;
3691}
3692
Avi Kivity851ba692009-08-24 11:10:17 +03003693static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003694{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003695 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3696 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3697
3698 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003699 return kvm_skip_emulated_instruction(&svm->vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003700}
3701
Avi Kivity851ba692009-08-24 11:10:17 +03003702static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003703{
Andre Przywara51d8b662010-12-21 11:12:02 +01003704 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003705}
3706
Brijesh Singh7607b712018-02-19 10:14:44 -06003707static int rsm_interception(struct vcpu_svm *svm)
3708{
3709 return x86_emulate_instruction(&svm->vcpu, 0, 0,
3710 rsm_ins_bytes, 2) == EMULATE_DONE;
3711}
3712
Avi Kivity332b56e2011-11-10 14:57:24 +02003713static int rdpmc_interception(struct vcpu_svm *svm)
3714{
3715 int err;
3716
3717 if (!static_cpu_has(X86_FEATURE_NRIPS))
3718 return emulate_on_interception(svm);
3719
3720 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08003721 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02003722}
3723
Xiubo Li52eb5a62015-03-13 17:39:45 +08003724static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3725 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003726{
3727 unsigned long cr0 = svm->vcpu.arch.cr0;
3728 bool ret = false;
3729 u64 intercept;
3730
3731 intercept = svm->nested.intercept;
3732
3733 if (!is_guest_mode(&svm->vcpu) ||
3734 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3735 return false;
3736
3737 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3738 val &= ~SVM_CR0_SELECTIVE_MASK;
3739
3740 if (cr0 ^ val) {
3741 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3742 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3743 }
3744
3745 return ret;
3746}
3747
Andre Przywara7ff76d52010-12-21 11:12:04 +01003748#define CR_VALID (1ULL << 63)
3749
3750static int cr_interception(struct vcpu_svm *svm)
3751{
3752 int reg, cr;
3753 unsigned long val;
3754 int err;
3755
3756 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3757 return emulate_on_interception(svm);
3758
3759 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3760 return emulate_on_interception(svm);
3761
3762 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003763 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3764 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3765 else
3766 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003767
3768 err = 0;
3769 if (cr >= 16) { /* mov to cr */
3770 cr -= 16;
3771 val = kvm_register_read(&svm->vcpu, reg);
3772 switch (cr) {
3773 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003774 if (!check_selective_cr0_intercepted(svm, val))
3775 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003776 else
3777 return 1;
3778
Andre Przywara7ff76d52010-12-21 11:12:04 +01003779 break;
3780 case 3:
3781 err = kvm_set_cr3(&svm->vcpu, val);
3782 break;
3783 case 4:
3784 err = kvm_set_cr4(&svm->vcpu, val);
3785 break;
3786 case 8:
3787 err = kvm_set_cr8(&svm->vcpu, val);
3788 break;
3789 default:
3790 WARN(1, "unhandled write to CR%d", cr);
3791 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3792 return 1;
3793 }
3794 } else { /* mov from cr */
3795 switch (cr) {
3796 case 0:
3797 val = kvm_read_cr0(&svm->vcpu);
3798 break;
3799 case 2:
3800 val = svm->vcpu.arch.cr2;
3801 break;
3802 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003803 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003804 break;
3805 case 4:
3806 val = kvm_read_cr4(&svm->vcpu);
3807 break;
3808 case 8:
3809 val = kvm_get_cr8(&svm->vcpu);
3810 break;
3811 default:
3812 WARN(1, "unhandled read from CR%d", cr);
3813 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3814 return 1;
3815 }
3816 kvm_register_write(&svm->vcpu, reg, val);
3817 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08003818 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003819}
3820
Andre Przywaracae37972010-12-21 11:12:05 +01003821static int dr_interception(struct vcpu_svm *svm)
3822{
3823 int reg, dr;
3824 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003825
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003826 if (svm->vcpu.guest_debug == 0) {
3827 /*
3828 * No more DR vmexits; force a reload of the debug registers
3829 * and reenter on this instruction. The next vmexit will
3830 * retrieve the full state of the debug registers.
3831 */
3832 clr_dr_intercepts(svm);
3833 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3834 return 1;
3835 }
3836
Andre Przywaracae37972010-12-21 11:12:05 +01003837 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3838 return emulate_on_interception(svm);
3839
3840 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3841 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3842
3843 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003844 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3845 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003846 val = kvm_register_read(&svm->vcpu, reg);
3847 kvm_set_dr(&svm->vcpu, dr - 16, val);
3848 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003849 if (!kvm_require_dr(&svm->vcpu, dr))
3850 return 1;
3851 kvm_get_dr(&svm->vcpu, dr, &val);
3852 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003853 }
3854
Ladi Prosekb742c1e2017-06-22 09:05:26 +02003855 return kvm_skip_emulated_instruction(&svm->vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01003856}
3857
Avi Kivity851ba692009-08-24 11:10:17 +03003858static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003859{
Avi Kivity851ba692009-08-24 11:10:17 +03003860 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003861 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003862
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003863 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3864 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003865 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003866 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003867 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003868 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003869 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003870 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3871 return 0;
3872}
3873
Tom Lendacky801e4592018-02-21 13:39:51 -06003874static int svm_get_msr_feature(struct kvm_msr_entry *msr)
3875{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01003876 msr->data = 0;
3877
3878 switch (msr->index) {
3879 case MSR_F10H_DECFG:
3880 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
3881 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
3882 break;
3883 default:
3884 return 1;
3885 }
3886
3887 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003888}
3889
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003890static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003891{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003892 struct vcpu_svm *svm = to_svm(vcpu);
3893
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003894 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303895 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003896 msr_info->data = svm->vmcb->control.tsc_offset +
Haozhong Zhang35181e82015-10-20 15:39:03 +08003897 kvm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003898
Avi Kivity6aa8b732006-12-10 02:21:36 -08003899 break;
3900 }
Brian Gerst8c065852010-07-17 09:03:26 -04003901 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003902 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003903 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003904#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003905 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003906 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003907 break;
3908 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003909 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910 break;
3911 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003912 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003913 break;
3914 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003915 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003916 break;
3917#endif
3918 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003919 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920 break;
3921 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003922 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923 break;
3924 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003925 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003926 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003927 case MSR_TSC_AUX:
3928 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3929 return 1;
3930 msr_info->data = svm->tsc_aux;
3931 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003932 /*
3933 * Nobody will change the following 5 values in the VMCB so we can
3934 * safely return them on rdmsr. They will always be 0 until LBRV is
3935 * implemented.
3936 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003937 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003938 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01003939 break;
3940 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003941 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003942 break;
3943 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003944 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003945 break;
3946 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003947 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003948 break;
3949 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003950 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003951 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003952 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003953 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003954 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003955 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003956 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003957 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003958 case MSR_IA32_SPEC_CTRL:
3959 if (!msr_info->host_initiated &&
3960 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
3961 return 1;
3962
3963 msr_info->data = svm->spec_ctrl;
3964 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003965 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003966 msr_info->data = 0x01000065;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003967 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01003968 case MSR_F15H_IC_CFG: {
3969
3970 int family, model;
3971
3972 family = guest_cpuid_family(vcpu);
3973 model = guest_cpuid_model(vcpu);
3974
3975 if (family < 0 || model < 0)
3976 return kvm_get_msr_common(vcpu, msr_info);
3977
3978 msr_info->data = 0;
3979
3980 if (family == 0x15 &&
3981 (model >= 0x2 && model < 0x20))
3982 msr_info->data = 0x1E;
3983 }
3984 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01003985 case MSR_F10H_DECFG:
3986 msr_info->data = svm->msr_decfg;
3987 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003988 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003989 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003990 }
3991 return 0;
3992}
3993
Avi Kivity851ba692009-08-24 11:10:17 +03003994static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003995{
David Kaplan668f1982015-02-20 16:02:10 -06003996 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003997 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003998
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003999 msr_info.index = ecx;
4000 msr_info.host_initiated = false;
4001 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02004002 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004003 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004004 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004005 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004006 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004007
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004008 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
4009 msr_info.data & 0xffffffff);
4010 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
4011 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004012 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004013 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004014 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004015}
4016
Joerg Roedel4a810182010-02-24 18:59:15 +01004017static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
4018{
4019 struct vcpu_svm *svm = to_svm(vcpu);
4020 int svm_dis, chg_mask;
4021
4022 if (data & ~SVM_VM_CR_VALID_MASK)
4023 return 1;
4024
4025 chg_mask = SVM_VM_CR_VALID_MASK;
4026
4027 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
4028 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
4029
4030 svm->nested.vm_cr_msr &= ~chg_mask;
4031 svm->nested.vm_cr_msr |= (data & chg_mask);
4032
4033 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
4034
4035 /* check for svm_disable while efer.svme is set */
4036 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
4037 return 1;
4038
4039 return 0;
4040}
4041
Will Auld8fe8ab42012-11-29 12:42:12 -08004042static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004043{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004044 struct vcpu_svm *svm = to_svm(vcpu);
4045
Will Auld8fe8ab42012-11-29 12:42:12 -08004046 u32 ecx = msr->index;
4047 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004048 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02004049 case MSR_IA32_CR_PAT:
4050 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4051 return 1;
4052 vcpu->arch.pat = data;
4053 svm->vmcb->save.g_pat = data;
4054 mark_dirty(svm->vmcb, VMCB_NPT);
4055 break;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10004056 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08004057 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004058 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01004059 case MSR_IA32_SPEC_CTRL:
4060 if (!msr->host_initiated &&
4061 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS))
4062 return 1;
4063
4064 /* The STIBP bit doesn't fault even if it's not advertised */
4065 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
4066 return 1;
4067
4068 svm->spec_ctrl = data;
4069
4070 if (!data)
4071 break;
4072
4073 /*
4074 * For non-nested:
4075 * When it's written (to non-zero) for the first time, pass
4076 * it through.
4077 *
4078 * For nested:
4079 * The handling of the MSR bitmap for L2 guests is done in
4080 * nested_svm_vmrun_msrpm.
4081 * We update the L1 MSR bit as well since it will end up
4082 * touching the MSR anyway now.
4083 */
4084 set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
4085 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004086 case MSR_IA32_PRED_CMD:
4087 if (!msr->host_initiated &&
4088 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB))
4089 return 1;
4090
4091 if (data & ~PRED_CMD_IBPB)
4092 return 1;
4093
4094 if (!data)
4095 break;
4096
4097 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4098 if (is_guest_mode(vcpu))
4099 break;
4100 set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
4101 break;
Brian Gerst8c065852010-07-17 09:03:26 -04004102 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004103 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004104 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08004105#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004106 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004107 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004108 break;
4109 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004110 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004111 break;
4112 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004113 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004114 break;
4115 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004116 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004117 break;
4118#endif
4119 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004120 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004121 break;
4122 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02004123 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004124 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004125 break;
4126 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02004127 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004128 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004129 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01004130 case MSR_TSC_AUX:
4131 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
4132 return 1;
4133
4134 /*
4135 * This is rare, so we update the MSR here instead of using
4136 * direct_access_msrs. Doing that would require a rdmsr in
4137 * svm_vcpu_put.
4138 */
4139 svm->tsc_aux = data;
4140 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
4141 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01004142 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004143 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03004144 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
4145 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004146 break;
4147 }
4148 if (data & DEBUGCTL_RESERVED_BITS)
4149 return 1;
4150
4151 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01004152 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01004153 if (data & (1ULL<<0))
4154 svm_enable_lbrv(svm);
4155 else
4156 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01004157 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004158 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02004159 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01004160 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004161 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01004162 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004163 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03004164 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02004165 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01004166 case MSR_F10H_DECFG: {
4167 struct kvm_msr_entry msr_entry;
4168
4169 msr_entry.index = msr->index;
4170 if (svm_get_msr_feature(&msr_entry))
4171 return 1;
4172
4173 /* Check the supported bits */
4174 if (data & ~msr_entry.data)
4175 return 1;
4176
4177 /* Don't allow the guest to change a bit, #GP */
4178 if (!msr->host_initiated && (data ^ msr_entry.data))
4179 return 1;
4180
4181 svm->msr_decfg = data;
4182 break;
4183 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004184 case MSR_IA32_APICBASE:
4185 if (kvm_vcpu_apicv_active(vcpu))
4186 avic_update_vapic_bar(to_svm(vcpu), data);
4187 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08004189 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004190 }
4191 return 0;
4192}
4193
Avi Kivity851ba692009-08-24 11:10:17 +03004194static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004195{
Will Auld8fe8ab42012-11-29 12:42:12 -08004196 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06004197 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
4198 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004199
Will Auld8fe8ab42012-11-29 12:42:12 -08004200 msr.data = data;
4201 msr.index = ecx;
4202 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004203
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004204 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03004205 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02004206 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004207 kvm_inject_gp(&svm->vcpu, 0);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004208 return 1;
Avi Kivity59200272010-01-25 19:47:02 +02004209 } else {
4210 trace_kvm_msr_write(ecx, data);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004211 return kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02004212 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004213}
4214
Avi Kivity851ba692009-08-24 11:10:17 +03004215static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004216{
Rusty Russelle756fc62007-07-30 20:07:08 +10004217 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03004218 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004219 else
Avi Kivity851ba692009-08-24 11:10:17 +03004220 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004221}
4222
Avi Kivity851ba692009-08-24 11:10:17 +03004223static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08004224{
Avi Kivity3842d132010-07-27 12:30:24 +03004225 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01004226 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03004227 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004228 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08004229 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08004230 return 1;
4231}
4232
Mark Langsdorf565d0992009-10-06 14:25:02 -05004233static int pause_interception(struct vcpu_svm *svm)
4234{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08004235 struct kvm_vcpu *vcpu = &svm->vcpu;
4236 bool in_kernel = (svm_get_cpl(vcpu) == 0);
4237
4238 kvm_vcpu_on_spin(vcpu, in_kernel);
Mark Langsdorf565d0992009-10-06 14:25:02 -05004239 return 1;
4240}
4241
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004242static int nop_interception(struct vcpu_svm *svm)
4243{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02004244 return kvm_skip_emulated_instruction(&(svm->vcpu));
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004245}
4246
4247static int monitor_interception(struct vcpu_svm *svm)
4248{
4249 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
4250 return nop_interception(svm);
4251}
4252
4253static int mwait_interception(struct vcpu_svm *svm)
4254{
4255 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
4256 return nop_interception(svm);
4257}
4258
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004259enum avic_ipi_failure_cause {
4260 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
4261 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
4262 AVIC_IPI_FAILURE_INVALID_TARGET,
4263 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
4264};
4265
4266static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
4267{
4268 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
4269 u32 icrl = svm->vmcb->control.exit_info_1;
4270 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03004271 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004272 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4273
4274 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
4275
4276 switch (id) {
4277 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
4278 /*
4279 * AVIC hardware handles the generation of
4280 * IPIs when the specified Message Type is Fixed
4281 * (also known as fixed delivery mode) and
4282 * the Trigger Mode is edge-triggered. The hardware
4283 * also supports self and broadcast delivery modes
4284 * specified via the Destination Shorthand(DSH)
4285 * field of the ICRL. Logical and physical APIC ID
4286 * formats are supported. All other IPI types cause
4287 * a #VMEXIT, which needs to emulated.
4288 */
4289 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
4290 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
4291 break;
4292 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
4293 int i;
4294 struct kvm_vcpu *vcpu;
4295 struct kvm *kvm = svm->vcpu.kvm;
4296 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4297
4298 /*
4299 * At this point, we expect that the AVIC HW has already
4300 * set the appropriate IRR bits on the valid target
4301 * vcpus. So, we just need to kick the appropriate vcpu.
4302 */
4303 kvm_for_each_vcpu(i, vcpu, kvm) {
4304 bool m = kvm_apic_match_dest(vcpu, apic,
4305 icrl & KVM_APIC_SHORT_MASK,
4306 GET_APIC_DEST_FIELD(icrh),
4307 icrl & KVM_APIC_DEST_MASK);
4308
4309 if (m && !avic_vcpu_is_running(vcpu))
4310 kvm_vcpu_wake_up(vcpu);
4311 }
4312 break;
4313 }
4314 case AVIC_IPI_FAILURE_INVALID_TARGET:
4315 break;
4316 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
4317 WARN_ONCE(1, "Invalid backing page\n");
4318 break;
4319 default:
4320 pr_err("Unknown IPI interception\n");
4321 }
4322
4323 return 1;
4324}
4325
4326static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
4327{
4328 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4329 int index;
4330 u32 *logical_apic_id_table;
4331 int dlid = GET_APIC_LOGICAL_ID(ldr);
4332
4333 if (!dlid)
4334 return NULL;
4335
4336 if (flat) { /* flat */
4337 index = ffs(dlid) - 1;
4338 if (index > 7)
4339 return NULL;
4340 } else { /* cluster */
4341 int cluster = (dlid & 0xf0) >> 4;
4342 int apic = ffs(dlid & 0x0f) - 1;
4343
4344 if ((apic < 0) || (apic > 7) ||
4345 (cluster >= 0xf))
4346 return NULL;
4347 index = (cluster << 2) + apic;
4348 }
4349
4350 logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page);
4351
4352 return &logical_apic_id_table[index];
4353}
4354
4355static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
4356 bool valid)
4357{
4358 bool flat;
4359 u32 *entry, new_entry;
4360
4361 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
4362 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
4363 if (!entry)
4364 return -EINVAL;
4365
4366 new_entry = READ_ONCE(*entry);
4367 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
4368 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
4369 if (valid)
4370 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4371 else
4372 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
4373 WRITE_ONCE(*entry, new_entry);
4374
4375 return 0;
4376}
4377
4378static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
4379{
4380 int ret;
4381 struct vcpu_svm *svm = to_svm(vcpu);
4382 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
4383
4384 if (!ldr)
4385 return 1;
4386
4387 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
4388 if (ret && svm->ldr_reg) {
4389 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
4390 svm->ldr_reg = 0;
4391 } else {
4392 svm->ldr_reg = ldr;
4393 }
4394 return ret;
4395}
4396
4397static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
4398{
4399 u64 *old, *new;
4400 struct vcpu_svm *svm = to_svm(vcpu);
4401 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
4402 u32 id = (apic_id_reg >> 24) & 0xff;
4403
4404 if (vcpu->vcpu_id == id)
4405 return 0;
4406
4407 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
4408 new = avic_get_physical_id_entry(vcpu, id);
4409 if (!new || !old)
4410 return 1;
4411
4412 /* We need to move physical_id_entry to new offset */
4413 *new = *old;
4414 *old = 0ULL;
4415 to_svm(vcpu)->avic_physical_id_cache = new;
4416
4417 /*
4418 * Also update the guest physical APIC ID in the logical
4419 * APIC ID table entry if already setup the LDR.
4420 */
4421 if (svm->ldr_reg)
4422 avic_handle_ldr_update(vcpu);
4423
4424 return 0;
4425}
4426
4427static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
4428{
4429 struct vcpu_svm *svm = to_svm(vcpu);
4430 struct kvm_arch *vm_data = &vcpu->kvm->arch;
4431 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
4432 u32 mod = (dfr >> 28) & 0xf;
4433
4434 /*
4435 * We assume that all local APICs are using the same type.
4436 * If this changes, we need to flush the AVIC logical
4437 * APID id table.
4438 */
4439 if (vm_data->ldr_mode == mod)
4440 return 0;
4441
4442 clear_page(page_address(vm_data->avic_logical_id_table_page));
4443 vm_data->ldr_mode = mod;
4444
4445 if (svm->ldr_reg)
4446 avic_handle_ldr_update(vcpu);
4447 return 0;
4448}
4449
4450static int avic_unaccel_trap_write(struct vcpu_svm *svm)
4451{
4452 struct kvm_lapic *apic = svm->vcpu.arch.apic;
4453 u32 offset = svm->vmcb->control.exit_info_1 &
4454 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4455
4456 switch (offset) {
4457 case APIC_ID:
4458 if (avic_handle_apic_id_update(&svm->vcpu))
4459 return 0;
4460 break;
4461 case APIC_LDR:
4462 if (avic_handle_ldr_update(&svm->vcpu))
4463 return 0;
4464 break;
4465 case APIC_DFR:
4466 avic_handle_dfr_update(&svm->vcpu);
4467 break;
4468 default:
4469 break;
4470 }
4471
4472 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
4473
4474 return 1;
4475}
4476
4477static bool is_avic_unaccelerated_access_trap(u32 offset)
4478{
4479 bool ret = false;
4480
4481 switch (offset) {
4482 case APIC_ID:
4483 case APIC_EOI:
4484 case APIC_RRR:
4485 case APIC_LDR:
4486 case APIC_DFR:
4487 case APIC_SPIV:
4488 case APIC_ESR:
4489 case APIC_ICR:
4490 case APIC_LVTT:
4491 case APIC_LVTTHMR:
4492 case APIC_LVTPC:
4493 case APIC_LVT0:
4494 case APIC_LVT1:
4495 case APIC_LVTERR:
4496 case APIC_TMICT:
4497 case APIC_TDCR:
4498 ret = true;
4499 break;
4500 default:
4501 break;
4502 }
4503 return ret;
4504}
4505
4506static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
4507{
4508 int ret = 0;
4509 u32 offset = svm->vmcb->control.exit_info_1 &
4510 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
4511 u32 vector = svm->vmcb->control.exit_info_2 &
4512 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
4513 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
4514 AVIC_UNACCEL_ACCESS_WRITE_MASK;
4515 bool trap = is_avic_unaccelerated_access_trap(offset);
4516
4517 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
4518 trap, write, vector);
4519 if (trap) {
4520 /* Handling Trap */
4521 WARN_ONCE(!write, "svm: Handling trap read.\n");
4522 ret = avic_unaccel_trap_write(svm);
4523 } else {
4524 /* Handling Fault */
4525 ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
4526 }
4527
4528 return ret;
4529}
4530
Mathias Krause09941fb2012-08-30 01:30:20 +02004531static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01004532 [SVM_EXIT_READ_CR0] = cr_interception,
4533 [SVM_EXIT_READ_CR3] = cr_interception,
4534 [SVM_EXIT_READ_CR4] = cr_interception,
4535 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06004536 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02004537 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01004538 [SVM_EXIT_WRITE_CR3] = cr_interception,
4539 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004540 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01004541 [SVM_EXIT_READ_DR0] = dr_interception,
4542 [SVM_EXIT_READ_DR1] = dr_interception,
4543 [SVM_EXIT_READ_DR2] = dr_interception,
4544 [SVM_EXIT_READ_DR3] = dr_interception,
4545 [SVM_EXIT_READ_DR4] = dr_interception,
4546 [SVM_EXIT_READ_DR5] = dr_interception,
4547 [SVM_EXIT_READ_DR6] = dr_interception,
4548 [SVM_EXIT_READ_DR7] = dr_interception,
4549 [SVM_EXIT_WRITE_DR0] = dr_interception,
4550 [SVM_EXIT_WRITE_DR1] = dr_interception,
4551 [SVM_EXIT_WRITE_DR2] = dr_interception,
4552 [SVM_EXIT_WRITE_DR3] = dr_interception,
4553 [SVM_EXIT_WRITE_DR4] = dr_interception,
4554 [SVM_EXIT_WRITE_DR5] = dr_interception,
4555 [SVM_EXIT_WRITE_DR6] = dr_interception,
4556 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004557 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
4558 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004559 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004560 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004561 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01004562 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004563 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02004564 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004565 [SVM_EXIT_SMI] = nop_on_interception,
4566 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08004567 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02004568 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004569 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004570 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02004571 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05004572 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004573 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03004574 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02004575 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01004576 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004577 [SVM_EXIT_MSR] = msr_interception,
4578 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08004579 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01004580 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02004581 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01004582 [SVM_EXIT_VMLOAD] = vmload_interception,
4583 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01004584 [SVM_EXIT_STGI] = stgi_interception,
4585 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02004586 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06004587 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04004588 [SVM_EXIT_MONITOR] = monitor_interception,
4589 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01004590 [SVM_EXIT_XSETBV] = xsetbv_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02004591 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06004592 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05004593 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
4594 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004595};
4596
Joe Perchesae8cc052011-04-24 22:00:50 -07004597static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02004598{
4599 struct vcpu_svm *svm = to_svm(vcpu);
4600 struct vmcb_control_area *control = &svm->vmcb->control;
4601 struct vmcb_save_area *save = &svm->vmcb->save;
4602
4603 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004604 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
4605 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
4606 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
4607 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
4608 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
4609 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
4610 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
4611 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
4612 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
4613 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
4614 pr_err("%-20s%d\n", "asid:", control->asid);
4615 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
4616 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
4617 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
4618 pr_err("%-20s%08x\n", "int_state:", control->int_state);
4619 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
4620 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
4621 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
4622 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
4623 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
4624 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
4625 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004626 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07004627 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
4628 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05004629 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07004630 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004631 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
4632 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
4633 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004634 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07004635 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4636 "es:",
4637 save->es.selector, save->es.attrib,
4638 save->es.limit, save->es.base);
4639 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4640 "cs:",
4641 save->cs.selector, save->cs.attrib,
4642 save->cs.limit, save->cs.base);
4643 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4644 "ss:",
4645 save->ss.selector, save->ss.attrib,
4646 save->ss.limit, save->ss.base);
4647 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4648 "ds:",
4649 save->ds.selector, save->ds.attrib,
4650 save->ds.limit, save->ds.base);
4651 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4652 "fs:",
4653 save->fs.selector, save->fs.attrib,
4654 save->fs.limit, save->fs.base);
4655 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4656 "gs:",
4657 save->gs.selector, save->gs.attrib,
4658 save->gs.limit, save->gs.base);
4659 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4660 "gdtr:",
4661 save->gdtr.selector, save->gdtr.attrib,
4662 save->gdtr.limit, save->gdtr.base);
4663 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4664 "ldtr:",
4665 save->ldtr.selector, save->ldtr.attrib,
4666 save->ldtr.limit, save->ldtr.base);
4667 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4668 "idtr:",
4669 save->idtr.selector, save->idtr.attrib,
4670 save->idtr.limit, save->idtr.base);
4671 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4672 "tr:",
4673 save->tr.selector, save->tr.attrib,
4674 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004675 pr_err("cpl: %d efer: %016llx\n",
4676 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004677 pr_err("%-15s %016llx %-13s %016llx\n",
4678 "cr0:", save->cr0, "cr2:", save->cr2);
4679 pr_err("%-15s %016llx %-13s %016llx\n",
4680 "cr3:", save->cr3, "cr4:", save->cr4);
4681 pr_err("%-15s %016llx %-13s %016llx\n",
4682 "dr6:", save->dr6, "dr7:", save->dr7);
4683 pr_err("%-15s %016llx %-13s %016llx\n",
4684 "rip:", save->rip, "rflags:", save->rflags);
4685 pr_err("%-15s %016llx %-13s %016llx\n",
4686 "rsp:", save->rsp, "rax:", save->rax);
4687 pr_err("%-15s %016llx %-13s %016llx\n",
4688 "star:", save->star, "lstar:", save->lstar);
4689 pr_err("%-15s %016llx %-13s %016llx\n",
4690 "cstar:", save->cstar, "sfmask:", save->sfmask);
4691 pr_err("%-15s %016llx %-13s %016llx\n",
4692 "kernel_gs_base:", save->kernel_gs_base,
4693 "sysenter_cs:", save->sysenter_cs);
4694 pr_err("%-15s %016llx %-13s %016llx\n",
4695 "sysenter_esp:", save->sysenter_esp,
4696 "sysenter_eip:", save->sysenter_eip);
4697 pr_err("%-15s %016llx %-13s %016llx\n",
4698 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4699 pr_err("%-15s %016llx %-13s %016llx\n",
4700 "br_from:", save->br_from, "br_to:", save->br_to);
4701 pr_err("%-15s %016llx %-13s %016llx\n",
4702 "excp_from:", save->last_excp_from,
4703 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004704}
4705
Avi Kivity586f9602010-11-18 13:09:54 +02004706static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4707{
4708 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4709
4710 *info1 = control->exit_info_1;
4711 *info2 = control->exit_info_2;
4712}
4713
Avi Kivity851ba692009-08-24 11:10:17 +03004714static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004716 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004717 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004718 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004719
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004720 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4721
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004722 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004723 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4724 if (npt_enabled)
4725 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004726
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004727 if (unlikely(svm->nested.exit_required)) {
4728 nested_svm_vmexit(svm);
4729 svm->nested.exit_required = false;
4730
4731 return 1;
4732 }
4733
Joerg Roedel20307532010-11-29 17:51:48 +01004734 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004735 int vmexit;
4736
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004737 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4738 svm->vmcb->control.exit_info_1,
4739 svm->vmcb->control.exit_info_2,
4740 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004741 svm->vmcb->control.exit_int_info_err,
4742 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004743
Joerg Roedel410e4d52009-08-07 11:49:44 +02004744 vmexit = nested_svm_exit_special(svm);
4745
4746 if (vmexit == NESTED_EXIT_CONTINUE)
4747 vmexit = nested_svm_exit_handled(svm);
4748
4749 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004750 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004751 }
4752
Joerg Roedela5c38322009-08-07 11:49:32 +02004753 svm_complete_interrupts(svm);
4754
Avi Kivity04d2cc72007-09-10 18:10:54 +03004755 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4756 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4757 kvm_run->fail_entry.hardware_entry_failure_reason
4758 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004759 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4760 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004761 return 0;
4762 }
4763
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004764 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004765 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004766 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4767 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004768 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004769 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004770 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004771 exit_code);
4772
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004773 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004774 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04004775 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03004776 kvm_queue_exception(vcpu, UD_VECTOR);
4777 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004778 }
4779
Avi Kivity851ba692009-08-24 11:10:17 +03004780 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004781}
4782
4783static void reload_tss(struct kvm_vcpu *vcpu)
4784{
4785 int cpu = raw_smp_processor_id();
4786
Tejun Heo0fe1e002009-10-29 22:34:14 +09004787 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4788 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004789 load_TR_desc();
4790}
4791
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004792static void pre_sev_run(struct vcpu_svm *svm, int cpu)
4793{
4794 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4795 int asid = sev_get_asid(svm->vcpu.kvm);
4796
4797 /* Assign the asid allocated with this SEV guest */
4798 svm->vmcb->control.asid = asid;
4799
4800 /*
4801 * Flush guest TLB:
4802 *
4803 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
4804 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
4805 */
4806 if (sd->sev_vmcbs[asid] == svm->vmcb &&
4807 svm->last_cpu == cpu)
4808 return;
4809
4810 svm->last_cpu = cpu;
4811 sd->sev_vmcbs[asid] = svm->vmcb;
4812 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4813 mark_dirty(svm->vmcb, VMCB_ASID);
4814}
4815
Rusty Russelle756fc62007-07-30 20:07:08 +10004816static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004817{
4818 int cpu = raw_smp_processor_id();
4819
Tejun Heo0fe1e002009-10-29 22:34:14 +09004820 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004821
Brijesh Singh70cd94e2017-12-04 10:57:34 -06004822 if (sev_guest(svm->vcpu.kvm))
4823 return pre_sev_run(svm, cpu);
4824
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03004825 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09004826 if (svm->asid_generation != sd->asid_generation)
4827 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004828}
4829
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004830static void svm_inject_nmi(struct kvm_vcpu *vcpu)
4831{
4832 struct vcpu_svm *svm = to_svm(vcpu);
4833
4834 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
4835 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004836 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004837 ++vcpu->stat.nmi_injections;
4838}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004839
Eddie Dong85f455f2007-07-06 12:20:49 +03004840static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004841{
4842 struct vmcb_control_area *control;
4843
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004844 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10004845 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03004846 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004847 control->int_ctl &= ~V_INTR_PRIO_MASK;
4848 control->int_ctl |= V_IRQ_MASK |
4849 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004850 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004851}
4852
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004853static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03004854{
4855 struct vcpu_svm *svm = to_svm(vcpu);
4856
Joerg Roedel2af91942009-08-07 11:49:28 +02004857 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01004858
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03004859 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
4860 ++vcpu->stat.irq_injections;
4861
Alexander Graf219b65d2009-06-15 15:21:25 +02004862 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
4863 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03004864}
4865
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004866static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
4867{
4868 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
4869}
4870
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004871static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
4872{
4873 struct vcpu_svm *svm = to_svm(vcpu);
4874
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004875 if (svm_nested_virtualize_tpr(vcpu) ||
4876 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004877 return;
4878
Radim Krčmář596f3142014-03-11 19:11:18 +01004879 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
4880
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004881 if (irr == -1)
4882 return;
4883
4884 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004885 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004886}
4887
Yang Zhang8d146952013-01-25 10:18:50 +08004888static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
4889{
4890 return;
4891}
4892
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05004893static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004894{
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05004895 return avic && irqchip_split(vcpu->kvm);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004896}
4897
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004898static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
4899{
4900}
4901
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02004902static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004903{
4904}
4905
4906/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03004907static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4908{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004909 struct vcpu_svm *svm = to_svm(vcpu);
4910 struct vmcb *vmcb = svm->vmcb;
4911
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05004912 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004913 return;
4914
4915 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
4916 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004917}
4918
Andrey Smetanin63086302015-11-10 15:36:32 +03004919static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004920{
4921 return;
4922}
4923
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004924static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
4925{
4926 kvm_lapic_set_irr(vec, vcpu->arch.apic);
4927 smp_mb__after_atomic();
4928
4929 if (avic_vcpu_is_running(vcpu))
4930 wrmsrl(SVM_AVIC_DOORBELL,
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05004931 kvm_cpu_get_apicid(vcpu->cpu));
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004932 else
4933 kvm_vcpu_wake_up(vcpu);
4934}
4935
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004936static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4937{
4938 unsigned long flags;
4939 struct amd_svm_iommu_ir *cur;
4940
4941 spin_lock_irqsave(&svm->ir_list_lock, flags);
4942 list_for_each_entry(cur, &svm->ir_list, node) {
4943 if (cur->data != pi->ir_data)
4944 continue;
4945 list_del(&cur->node);
4946 kfree(cur);
4947 break;
4948 }
4949 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4950}
4951
4952static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
4953{
4954 int ret = 0;
4955 unsigned long flags;
4956 struct amd_svm_iommu_ir *ir;
4957
4958 /**
4959 * In some cases, the existing irte is updaed and re-set,
4960 * so we need to check here if it's already been * added
4961 * to the ir_list.
4962 */
4963 if (pi->ir_data && (pi->prev_ga_tag != 0)) {
4964 struct kvm *kvm = svm->vcpu.kvm;
4965 u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
4966 struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
4967 struct vcpu_svm *prev_svm;
4968
4969 if (!prev_vcpu) {
4970 ret = -EINVAL;
4971 goto out;
4972 }
4973
4974 prev_svm = to_svm(prev_vcpu);
4975 svm_ir_list_del(prev_svm, pi);
4976 }
4977
4978 /**
4979 * Allocating new amd_iommu_pi_data, which will get
4980 * add to the per-vcpu ir_list.
4981 */
4982 ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
4983 if (!ir) {
4984 ret = -ENOMEM;
4985 goto out;
4986 }
4987 ir->data = pi->ir_data;
4988
4989 spin_lock_irqsave(&svm->ir_list_lock, flags);
4990 list_add(&ir->node, &svm->ir_list);
4991 spin_unlock_irqrestore(&svm->ir_list_lock, flags);
4992out:
4993 return ret;
4994}
4995
4996/**
4997 * Note:
4998 * The HW cannot support posting multicast/broadcast
4999 * interrupts to a vCPU. So, we still use legacy interrupt
5000 * remapping for these kind of interrupts.
5001 *
5002 * For lowest-priority interrupts, we only support
5003 * those with single CPU as the destination, e.g. user
5004 * configures the interrupts via /proc/irq or uses
5005 * irqbalance to make the interrupts single-CPU.
5006 */
5007static int
5008get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
5009 struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
5010{
5011 struct kvm_lapic_irq irq;
5012 struct kvm_vcpu *vcpu = NULL;
5013
5014 kvm_set_msi_irq(kvm, e, &irq);
5015
5016 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
5017 pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
5018 __func__, irq.vector);
5019 return -1;
5020 }
5021
5022 pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
5023 irq.vector);
5024 *svm = to_svm(vcpu);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005025 vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page));
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005026 vcpu_info->vector = irq.vector;
5027
5028 return 0;
5029}
5030
5031/*
5032 * svm_update_pi_irte - set IRTE for Posted-Interrupts
5033 *
5034 * @kvm: kvm
5035 * @host_irq: host irq of the interrupt
5036 * @guest_irq: gsi of the interrupt
5037 * @set: set or unset PI
5038 * returns 0 on success, < 0 on failure
5039 */
5040static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
5041 uint32_t guest_irq, bool set)
5042{
5043 struct kvm_kernel_irq_routing_entry *e;
5044 struct kvm_irq_routing_table *irq_rt;
5045 int idx, ret = -EINVAL;
5046
5047 if (!kvm_arch_has_assigned_device(kvm) ||
5048 !irq_remapping_cap(IRQ_POSTING_CAP))
5049 return 0;
5050
5051 pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
5052 __func__, host_irq, guest_irq, set);
5053
5054 idx = srcu_read_lock(&kvm->irq_srcu);
5055 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
5056 WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
5057
5058 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
5059 struct vcpu_data vcpu_info;
5060 struct vcpu_svm *svm = NULL;
5061
5062 if (e->type != KVM_IRQ_ROUTING_MSI)
5063 continue;
5064
5065 /**
5066 * Here, we setup with legacy mode in the following cases:
5067 * 1. When cannot target interrupt to a specific vcpu.
5068 * 2. Unsetting posted interrupt.
5069 * 3. APIC virtialization is disabled for the vcpu.
5070 */
5071 if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
5072 kvm_vcpu_apicv_active(&svm->vcpu)) {
5073 struct amd_iommu_pi_data pi;
5074
5075 /* Try to enable guest_mode in IRTE */
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005076 pi.base = __sme_set(page_to_phys(svm->avic_backing_page) &
5077 AVIC_HPA_MASK);
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005078 pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id,
5079 svm->vcpu.vcpu_id);
5080 pi.is_guest_mode = true;
5081 pi.vcpu_data = &vcpu_info;
5082 ret = irq_set_vcpu_affinity(host_irq, &pi);
5083
5084 /**
5085 * Here, we successfully setting up vcpu affinity in
5086 * IOMMU guest mode. Now, we need to store the posted
5087 * interrupt information in a per-vcpu ir_list so that
5088 * we can reference to them directly when we update vcpu
5089 * scheduling information in IOMMU irte.
5090 */
5091 if (!ret && pi.is_guest_mode)
5092 svm_ir_list_add(svm, &pi);
5093 } else {
5094 /* Use legacy mode in IRTE */
5095 struct amd_iommu_pi_data pi;
5096
5097 /**
5098 * Here, pi is used to:
5099 * - Tell IOMMU to use legacy mode for this interrupt.
5100 * - Retrieve ga_tag of prior interrupt remapping data.
5101 */
5102 pi.is_guest_mode = false;
5103 ret = irq_set_vcpu_affinity(host_irq, &pi);
5104
5105 /**
5106 * Check if the posted interrupt was previously
5107 * setup with the guest_mode by checking if the ga_tag
5108 * was cached. If so, we need to clean up the per-vcpu
5109 * ir_list.
5110 */
5111 if (!ret && pi.prev_ga_tag) {
5112 int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
5113 struct kvm_vcpu *vcpu;
5114
5115 vcpu = kvm_get_vcpu_by_id(kvm, id);
5116 if (vcpu)
5117 svm_ir_list_del(to_svm(vcpu), &pi);
5118 }
5119 }
5120
5121 if (!ret && svm) {
5122 trace_kvm_pi_irte_update(svm->vcpu.vcpu_id,
5123 host_irq, e->gsi,
5124 vcpu_info.vector,
5125 vcpu_info.pi_desc_addr, set);
5126 }
5127
5128 if (ret < 0) {
5129 pr_err("%s: failed to update PI IRTE\n", __func__);
5130 goto out;
5131 }
5132 }
5133
5134 ret = 0;
5135out:
5136 srcu_read_unlock(&kvm->irq_srcu, idx);
5137 return ret;
5138}
5139
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005140static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005141{
5142 struct vcpu_svm *svm = to_svm(vcpu);
5143 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02005144 int ret;
5145 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
5146 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
5147 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
5148
5149 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02005150}
5151
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005152static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
5153{
5154 struct vcpu_svm *svm = to_svm(vcpu);
5155
5156 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
5157}
5158
5159static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5160{
5161 struct vcpu_svm *svm = to_svm(vcpu);
5162
5163 if (masked) {
5164 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005165 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005166 } else {
5167 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01005168 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005169 }
5170}
5171
Gleb Natapov78646122009-03-23 12:12:11 +02005172static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
5173{
5174 struct vcpu_svm *svm = to_svm(vcpu);
5175 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005176 int ret;
5177
5178 if (!gif_set(svm) ||
5179 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
5180 return 0;
5181
Avi Kivityf6e78472010-08-02 15:30:20 +03005182 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005183
Joerg Roedel20307532010-11-29 17:51:48 +01005184 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02005185 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
5186
5187 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02005188}
5189
Jan Kiszkac9a79532014-03-07 20:03:15 +01005190static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03005191{
Alexander Graf219b65d2009-06-15 15:21:25 +02005192 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02005193
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005194 if (kvm_vcpu_apicv_active(vcpu))
5195 return;
5196
Joerg Roedele0231712010-02-24 18:59:10 +01005197 /*
5198 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
5199 * 1, because that's a separate STGI/VMRUN intercept. The next time we
5200 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005201 * we'll get the vintr intercept. However, if the vGIF feature is
5202 * enabled, the STGI interception will not occur. Enable the irq
5203 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01005204 */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005205 if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02005206 svm_set_vintr(svm);
5207 svm_inject_irq(svm, 0x0);
5208 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005209}
5210
Jan Kiszkac9a79532014-03-07 20:03:15 +01005211static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005212{
Avi Kivity04d2cc72007-09-10 18:10:54 +03005213 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005214
Gleb Natapov44c11432009-05-11 13:35:52 +03005215 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
5216 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01005217 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03005218
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005219 if (!gif_set(svm)) {
5220 if (vgif_enabled(svm))
5221 set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005222 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05005223 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02005224
5225 if (svm->nested.exit_required)
5226 return; /* we're not going to run the guest yet */
5227
Joerg Roedele0231712010-02-24 18:59:10 +01005228 /*
5229 * Something prevents NMI from been injected. Single step over possible
5230 * problem (IRET or exception injection or interrupt shadow)
5231 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02005232 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02005233 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03005234 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03005235}
5236
Izik Eiduscbc94022007-10-25 00:29:55 +02005237static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
5238{
5239 return 0;
5240}
5241
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005242static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Avi Kivityd9e368d2007-06-07 19:18:30 +03005243{
Joerg Roedel38e5e922010-12-03 15:25:16 +01005244 struct vcpu_svm *svm = to_svm(vcpu);
5245
5246 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
5247 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
5248 else
5249 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03005250}
5251
Avi Kivity04d2cc72007-09-10 18:10:54 +03005252static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
5253{
5254}
5255
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005256static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
5257{
5258 struct vcpu_svm *svm = to_svm(vcpu);
5259
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005260 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005261 return;
5262
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01005263 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005264 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03005265 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005266 }
5267}
5268
Joerg Roedel649d6862008-04-16 16:51:15 +02005269static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
5270{
5271 struct vcpu_svm *svm = to_svm(vcpu);
5272 u64 cr8;
5273
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05005274 if (svm_nested_virtualize_tpr(vcpu) ||
5275 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01005276 return;
5277
Joerg Roedel649d6862008-04-16 16:51:15 +02005278 cr8 = kvm_get_cr8(vcpu);
5279 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
5280 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
5281}
5282
Gleb Natapov9222be12009-04-23 17:14:37 +03005283static void svm_complete_interrupts(struct vcpu_svm *svm)
5284{
5285 u8 vector;
5286 int type;
5287 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01005288 unsigned int3_injected = svm->int3_injected;
5289
5290 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005291
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02005292 /*
5293 * If we've made progress since setting HF_IRET_MASK, we've
5294 * executed an IRET and can allow NMI injection.
5295 */
5296 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
5297 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03005298 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03005299 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5300 }
Gleb Natapov44c11432009-05-11 13:35:52 +03005301
Gleb Natapov9222be12009-04-23 17:14:37 +03005302 svm->vcpu.arch.nmi_injected = false;
5303 kvm_clear_exception_queue(&svm->vcpu);
5304 kvm_clear_interrupt_queue(&svm->vcpu);
5305
5306 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
5307 return;
5308
Avi Kivity3842d132010-07-27 12:30:24 +03005309 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
5310
Gleb Natapov9222be12009-04-23 17:14:37 +03005311 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
5312 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
5313
5314 switch (type) {
5315 case SVM_EXITINTINFO_TYPE_NMI:
5316 svm->vcpu.arch.nmi_injected = true;
5317 break;
5318 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01005319 /*
5320 * In case of software exceptions, do not reinject the vector,
5321 * but re-execute the instruction instead. Rewind RIP first
5322 * if we emulated INT3 before.
5323 */
5324 if (kvm_exception_is_soft(vector)) {
5325 if (vector == BP_VECTOR && int3_injected &&
5326 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
5327 kvm_rip_write(&svm->vcpu,
5328 kvm_rip_read(&svm->vcpu) -
5329 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02005330 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01005331 }
Gleb Natapov9222be12009-04-23 17:14:37 +03005332 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
5333 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005334 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03005335
5336 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02005337 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03005338 break;
5339 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005340 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03005341 break;
5342 default:
5343 break;
5344 }
5345}
5346
Avi Kivityb463a6f2010-07-20 15:06:17 +03005347static void svm_cancel_injection(struct kvm_vcpu *vcpu)
5348{
5349 struct vcpu_svm *svm = to_svm(vcpu);
5350 struct vmcb_control_area *control = &svm->vmcb->control;
5351
5352 control->exit_int_info = control->event_inj;
5353 control->exit_int_info_err = control->event_inj_err;
5354 control->event_inj = 0;
5355 svm_complete_interrupts(svm);
5356}
5357
Avi Kivity851ba692009-08-24 11:10:17 +03005358static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005359{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005360 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03005361
Joerg Roedel2041a062010-04-22 12:33:08 +02005362 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5363 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5364 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5365
Joerg Roedelcd3ff652009-10-09 16:08:26 +02005366 /*
5367 * A vmexit emulation is required before the vcpu can be executed
5368 * again.
5369 */
5370 if (unlikely(svm->nested.exit_required))
5371 return;
5372
Ladi Proseka12713c2017-06-21 09:07:00 +02005373 /*
5374 * Disable singlestep if we're injecting an interrupt/exception.
5375 * We don't want our modified rflags to be pushed on the stack where
5376 * we might not be able to easily reset them if we disabled NMI
5377 * singlestep later.
5378 */
5379 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
5380 /*
5381 * Event injection happens before external interrupts cause a
5382 * vmexit and interrupts are disabled here, so smp_send_reschedule
5383 * is enough to force an immediate vmexit.
5384 */
5385 disable_nmi_singlestep(svm);
5386 smp_send_reschedule(vcpu->cpu);
5387 }
5388
Rusty Russelle756fc62007-07-30 20:07:08 +10005389 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005390
Joerg Roedel649d6862008-04-16 16:51:15 +02005391 sync_lapic_to_cr8(vcpu);
5392
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02005393 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005394
Avi Kivity04d2cc72007-09-10 18:10:54 +03005395 clgi();
5396
5397 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08005398
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005399 /*
5400 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
5401 * it's non-zero. Since vmentry is serialising on affected CPUs, there
5402 * is no need to worry about the conditional branch over the wrmsr
5403 * being speculatively taken.
5404 */
5405 if (svm->spec_ctrl)
5406 wrmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl);
5407
Avi Kivity6aa8b732006-12-10 02:21:36 -08005408 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03005409 "push %%" _ASM_BP "; \n\t"
5410 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
5411 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
5412 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
5413 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
5414 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
5415 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005416#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005417 "mov %c[r8](%[svm]), %%r8 \n\t"
5418 "mov %c[r9](%[svm]), %%r9 \n\t"
5419 "mov %c[r10](%[svm]), %%r10 \n\t"
5420 "mov %c[r11](%[svm]), %%r11 \n\t"
5421 "mov %c[r12](%[svm]), %%r12 \n\t"
5422 "mov %c[r13](%[svm]), %%r13 \n\t"
5423 "mov %c[r14](%[svm]), %%r14 \n\t"
5424 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005425#endif
5426
Avi Kivity6aa8b732006-12-10 02:21:36 -08005427 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03005428 "push %%" _ASM_AX " \n\t"
5429 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03005430 __ex(SVM_VMLOAD) "\n\t"
5431 __ex(SVM_VMRUN) "\n\t"
5432 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03005433 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005434
5435 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03005436 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
5437 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
5438 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
5439 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
5440 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
5441 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005442#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005443 "mov %%r8, %c[r8](%[svm]) \n\t"
5444 "mov %%r9, %c[r9](%[svm]) \n\t"
5445 "mov %%r10, %c[r10](%[svm]) \n\t"
5446 "mov %%r11, %c[r11](%[svm]) \n\t"
5447 "mov %%r12, %c[r12](%[svm]) \n\t"
5448 "mov %%r13, %c[r13](%[svm]) \n\t"
5449 "mov %%r14, %c[r14](%[svm]) \n\t"
5450 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08005451#endif
Jim Mattson0cb5b302018-01-03 14:31:38 -08005452 /*
5453 * Clear host registers marked as clobbered to prevent
5454 * speculative use.
5455 */
5456 "xor %%" _ASM_BX ", %%" _ASM_BX " \n\t"
5457 "xor %%" _ASM_CX ", %%" _ASM_CX " \n\t"
5458 "xor %%" _ASM_DX ", %%" _ASM_DX " \n\t"
5459 "xor %%" _ASM_SI ", %%" _ASM_SI " \n\t"
5460 "xor %%" _ASM_DI ", %%" _ASM_DI " \n\t"
5461#ifdef CONFIG_X86_64
5462 "xor %%r8, %%r8 \n\t"
5463 "xor %%r9, %%r9 \n\t"
5464 "xor %%r10, %%r10 \n\t"
5465 "xor %%r11, %%r11 \n\t"
5466 "xor %%r12, %%r12 \n\t"
5467 "xor %%r13, %%r13 \n\t"
5468 "xor %%r14, %%r14 \n\t"
5469 "xor %%r15, %%r15 \n\t"
5470#endif
Avi Kivity74547662012-09-16 15:10:59 +03005471 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08005472 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10005473 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08005474 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005475 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
5476 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
5477 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
5478 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
5479 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
5480 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005481#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005482 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
5483 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
5484 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
5485 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
5486 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
5487 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
5488 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
5489 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005490#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02005491 : "cc", "memory"
5492#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03005493 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005494 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03005495#else
5496 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02005497#endif
5498 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08005499
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01005500 /*
5501 * We do not use IBRS in the kernel. If this vCPU has used the
5502 * SPEC_CTRL MSR it may have left it on; save the value and
5503 * turn it off. This is much more efficient than blindly adding
5504 * it to the atomic save/restore list. Especially as the former
5505 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
5506 *
5507 * For non-nested case:
5508 * If the L01 MSR bitmap does not intercept the MSR, then we need to
5509 * save it.
5510 *
5511 * For nested case:
5512 * If the L02 MSR bitmap does not intercept the MSR, then we need to
5513 * save it.
5514 */
5515 if (!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))
5516 rdmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl);
5517
5518 if (svm->spec_ctrl)
5519 wrmsrl(MSR_IA32_SPEC_CTRL, 0);
5520
David Woodhouse117cc7a2018-01-12 11:11:27 +00005521 /* Eliminate branch target predictions from guest mode */
5522 vmexit_fill_RSB();
5523
Avi Kivity82ca2d12010-10-21 12:20:34 +02005524#ifdef CONFIG_X86_64
5525 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
5526#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02005527 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02005528#ifndef CONFIG_X86_32_LAZY_GS
5529 loadsegment(gs, svm->host.gs);
5530#endif
Avi Kivity9581d442010-10-19 16:46:55 +02005531#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005532
5533 reload_tss(vcpu);
5534
Avi Kivity56ba47d2007-11-07 17:14:18 +02005535 local_irq_disable();
5536
Avi Kivity13c34e02010-10-21 12:20:31 +02005537 vcpu->arch.cr2 = svm->vmcb->save.cr2;
5538 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
5539 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
5540 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
5541
Joerg Roedel3781c012011-01-14 16:45:02 +01005542 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5543 kvm_before_handle_nmi(&svm->vcpu);
5544
5545 stgi();
5546
5547 /* Any pending NMI will happen here */
5548
5549 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
5550 kvm_after_handle_nmi(&svm->vcpu);
5551
Joerg Roedeld7bf8222008-04-16 16:51:17 +02005552 sync_cr8_to_lapic(vcpu);
5553
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005554 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03005555
Joerg Roedel38e5e922010-12-03 15:25:16 +01005556 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
5557
Gleb Natapov631bc482010-10-14 11:22:52 +02005558 /* if exit due to PF check for async PF */
5559 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07005560 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
Gleb Natapov631bc482010-10-14 11:22:52 +02005561
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005562 if (npt_enabled) {
5563 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
5564 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
5565 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02005566
5567 /*
5568 * We need to handle MC intercepts here before the vcpu has a chance to
5569 * change the physical cpu
5570 */
5571 if (unlikely(svm->vmcb->control.exit_code ==
5572 SVM_EXIT_EXCP_BASE + MC_VECTOR))
5573 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01005574
5575 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005576}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05005577STACK_FRAME_NON_STANDARD(svm_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005578
Avi Kivity6aa8b732006-12-10 02:21:36 -08005579static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5580{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005581 struct vcpu_svm *svm = to_svm(vcpu);
5582
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005583 svm->vmcb->save.cr3 = __sme_set(root);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005584 mark_dirty(svm->vmcb, VMCB_CR);
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005585 svm_flush_tlb(vcpu, true);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005586}
5587
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005588static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
5589{
5590 struct vcpu_svm *svm = to_svm(vcpu);
5591
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005592 svm->vmcb->control.nested_cr3 = __sme_set(root);
Joerg Roedelb2747162010-12-03 11:45:53 +01005593 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005594
5595 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02005596 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01005597 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005598
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005599 svm_flush_tlb(vcpu, true);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005600}
5601
Avi Kivity6aa8b732006-12-10 02:21:36 -08005602static int is_disabled(void)
5603{
Joerg Roedel6031a612007-06-22 12:29:50 +03005604 u64 vm_cr;
5605
5606 rdmsrl(MSR_VM_CR, vm_cr);
5607 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
5608 return 1;
5609
Avi Kivity6aa8b732006-12-10 02:21:36 -08005610 return 0;
5611}
5612
Ingo Molnar102d8322007-02-19 14:37:47 +02005613static void
5614svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5615{
5616 /*
5617 * Patch in the VMMCALL instruction:
5618 */
5619 hypercall[0] = 0x0f;
5620 hypercall[1] = 0x01;
5621 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02005622}
5623
Yang, Sheng002c7f72007-07-31 14:23:01 +03005624static void svm_check_processor_compat(void *rtn)
5625{
5626 *(int *)rtn = 0;
5627}
5628
Avi Kivity774ead32007-12-26 13:57:04 +02005629static bool svm_cpu_has_accelerated_tpr(void)
5630{
5631 return false;
5632}
5633
Paolo Bonzini6d396b52015-04-01 14:25:33 +02005634static bool svm_has_high_real_mode_segbase(void)
5635{
5636 return true;
5637}
5638
Paolo Bonzinifc07e762015-10-01 13:20:22 +02005639static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
5640{
5641 return 0;
5642}
5643
Sheng Yang0e851882009-12-18 16:48:46 +08005644static void svm_cpuid_update(struct kvm_vcpu *vcpu)
5645{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02005646 struct vcpu_svm *svm = to_svm(vcpu);
5647
5648 /* Update nrips enabled cache */
Radim Krčmářd6321d42017-08-05 00:12:49 +02005649 svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005650
5651 if (!kvm_vcpu_apicv_active(vcpu))
5652 return;
5653
Radim Krčmář1b4d56b2017-08-05 00:12:50 +02005654 guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08005655}
5656
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005657static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5658{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005659 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05005660 case 0x1:
5661 if (avic)
5662 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5663 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02005664 case 0x80000001:
5665 if (nested)
5666 entry->ecx |= (1 << 2); /* Set SVM bit */
5667 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005668 case 0x8000000A:
5669 entry->eax = 1; /* SVM revision 1 */
5670 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
5671 ASID emulation to nested SVM */
5672 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02005673 entry->edx = 0; /* Per default do not support any
5674 additional features */
5675
5676 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02005677 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02005678 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005679
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02005680 /* Support NPT for the guest if enabled */
5681 if (npt_enabled)
5682 entry->edx |= SVM_FEATURE_NPT;
5683
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005684 break;
Brijesh Singh8765d752017-12-04 10:57:25 -06005685 case 0x8000001F:
5686 /* Support memory encryption cpuid if host supports it */
5687 if (boot_cpu_has(X86_FEATURE_SEV))
5688 cpuid(0x8000001f, &entry->eax, &entry->ebx,
5689 &entry->ecx, &entry->edx);
5690
Joerg Roedelc2c63a42010-04-22 12:33:12 +02005691 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005692}
5693
Sheng Yang17cc3932010-01-05 19:02:27 +08005694static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02005695{
Sheng Yang17cc3932010-01-05 19:02:27 +08005696 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02005697}
5698
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005699static bool svm_rdtscp_supported(void)
5700{
Paolo Bonzini46896c72015-11-12 14:49:16 +01005701 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005702}
5703
Mao, Junjiead756a12012-07-02 01:18:48 +00005704static bool svm_invpcid_supported(void)
5705{
5706 return false;
5707}
5708
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005709static bool svm_mpx_supported(void)
5710{
5711 return false;
5712}
5713
Wanpeng Li55412b22014-12-02 19:21:30 +08005714static bool svm_xsaves_supported(void)
5715{
5716 return false;
5717}
5718
Paolo Bonzini66336ca2016-07-12 10:36:41 +02005719static bool svm_umip_emulated(void)
5720{
5721 return false;
5722}
5723
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005724static bool svm_has_wbinvd_exit(void)
5725{
5726 return true;
5727}
5728
Joerg Roedel80612522011-04-04 12:39:33 +02005729#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005730 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005731#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005732 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005733#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03005734 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005735
Mathias Krause09941fb2012-08-30 01:30:20 +02005736static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005737 u32 exit_code;
5738 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005739} x86_intercept_map[] = {
5740 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
5741 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
5742 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
5743 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
5744 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02005745 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
5746 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02005747 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
5748 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
5749 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
5750 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
5751 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
5752 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
5753 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
5754 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02005755 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
5756 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
5757 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
5758 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
5759 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
5760 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
5761 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
5762 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005763 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
5764 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
5765 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02005766 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
5767 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
5768 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
5769 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
5770 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
5771 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
5772 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
5773 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
5774 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02005775 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
5776 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
5777 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
5778 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
5779 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
5780 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
5781 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02005782 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
5783 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
5784 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
5785 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005786};
5787
Joerg Roedel80612522011-04-04 12:39:33 +02005788#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005789#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02005790#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005791
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005792static int svm_check_intercept(struct kvm_vcpu *vcpu,
5793 struct x86_instruction_info *info,
5794 enum x86_intercept_stage stage)
5795{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005796 struct vcpu_svm *svm = to_svm(vcpu);
5797 int vmexit, ret = X86EMUL_CONTINUE;
5798 struct __x86_intercept icpt_info;
5799 struct vmcb *vmcb = svm->vmcb;
5800
5801 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
5802 goto out;
5803
5804 icpt_info = x86_intercept_map[info->intercept];
5805
Avi Kivity40e19b52011-04-21 12:35:41 +03005806 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005807 goto out;
5808
5809 switch (icpt_info.exit_code) {
5810 case SVM_EXIT_READ_CR0:
5811 if (info->intercept == x86_intercept_cr_read)
5812 icpt_info.exit_code += info->modrm_reg;
5813 break;
5814 case SVM_EXIT_WRITE_CR0: {
5815 unsigned long cr0, val;
5816 u64 intercept;
5817
5818 if (info->intercept == x86_intercept_cr_write)
5819 icpt_info.exit_code += info->modrm_reg;
5820
Jan Kiszka62baf442014-06-29 21:55:53 +02005821 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
5822 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005823 break;
5824
5825 intercept = svm->nested.intercept;
5826
5827 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
5828 break;
5829
5830 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
5831 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
5832
5833 if (info->intercept == x86_intercept_lmsw) {
5834 cr0 &= 0xfUL;
5835 val &= 0xfUL;
5836 /* lmsw can't clear PE - catch this here */
5837 if (cr0 & X86_CR0_PE)
5838 val |= X86_CR0_PE;
5839 }
5840
5841 if (cr0 ^ val)
5842 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
5843
5844 break;
5845 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02005846 case SVM_EXIT_READ_DR0:
5847 case SVM_EXIT_WRITE_DR0:
5848 icpt_info.exit_code += info->modrm_reg;
5849 break;
Joerg Roedel80612522011-04-04 12:39:33 +02005850 case SVM_EXIT_MSR:
5851 if (info->intercept == x86_intercept_wrmsr)
5852 vmcb->control.exit_info_1 = 1;
5853 else
5854 vmcb->control.exit_info_1 = 0;
5855 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02005856 case SVM_EXIT_PAUSE:
5857 /*
5858 * We get this for NOP only, but pause
5859 * is rep not, check this here
5860 */
5861 if (info->rep_prefix != REPE_PREFIX)
5862 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02005863 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02005864 case SVM_EXIT_IOIO: {
5865 u64 exit_info;
5866 u32 bytes;
5867
Joerg Roedelf6511932011-04-04 12:39:35 +02005868 if (info->intercept == x86_intercept_in ||
5869 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005870 exit_info = ((info->src_val & 0xffff) << 16) |
5871 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02005872 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02005873 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02005874 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02005875 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02005876 }
5877
5878 if (info->intercept == x86_intercept_outs ||
5879 info->intercept == x86_intercept_ins)
5880 exit_info |= SVM_IOIO_STR_MASK;
5881
5882 if (info->rep_prefix)
5883 exit_info |= SVM_IOIO_REP_MASK;
5884
5885 bytes = min(bytes, 4u);
5886
5887 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
5888
5889 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
5890
5891 vmcb->control.exit_info_1 = exit_info;
5892 vmcb->control.exit_info_2 = info->next_rip;
5893
5894 break;
5895 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005896 default:
5897 break;
5898 }
5899
Bandan Dasf1047652015-06-11 02:05:33 -04005900 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
5901 if (static_cpu_has(X86_FEATURE_NRIPS))
5902 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02005903 vmcb->control.exit_code = icpt_info.exit_code;
5904 vmexit = nested_svm_exit_handled(svm);
5905
5906 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
5907 : X86EMUL_CONTINUE;
5908
5909out:
5910 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005911}
5912
Yang Zhanga547c6d2013-04-11 19:25:10 +08005913static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
5914{
5915 local_irq_enable();
Paolo Bonzinif2485b32016-06-15 15:23:11 +02005916 /*
5917 * We must have an instruction with interrupts enabled, so
5918 * the timer interrupt isn't delayed by the interrupt shadow.
5919 */
5920 asm("nop");
5921 local_irq_disable();
Yang Zhanga547c6d2013-04-11 19:25:10 +08005922}
5923
Radim Krčmářae97a3b2014-08-21 18:08:06 +02005924static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
5925{
5926}
5927
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05005928static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
5929{
5930 if (avic_handle_apic_id_update(vcpu) != 0)
5931 return;
5932 if (avic_handle_dfr_update(vcpu) != 0)
5933 return;
5934 avic_handle_ldr_update(vcpu);
5935}
5936
Borislav Petkov74f16902017-03-26 23:51:24 +02005937static void svm_setup_mce(struct kvm_vcpu *vcpu)
5938{
5939 /* [63:9] are reserved. */
5940 vcpu->arch.mcg_cap &= 0x1ff;
5941}
5942
Ladi Prosek72d7b372017-10-11 16:54:41 +02005943static int svm_smi_allowed(struct kvm_vcpu *vcpu)
5944{
Ladi Prosek05cade72017-10-11 16:54:45 +02005945 struct vcpu_svm *svm = to_svm(vcpu);
5946
5947 /* Per APM Vol.2 15.22.2 "Response to SMI" */
5948 if (!gif_set(svm))
5949 return 0;
5950
5951 if (is_guest_mode(&svm->vcpu) &&
5952 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
5953 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
5954 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
5955 svm->nested.exit_required = true;
5956 return 0;
5957 }
5958
Ladi Prosek72d7b372017-10-11 16:54:41 +02005959 return 1;
5960}
5961
Ladi Prosek0234bf82017-10-11 16:54:40 +02005962static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
5963{
Ladi Prosek05cade72017-10-11 16:54:45 +02005964 struct vcpu_svm *svm = to_svm(vcpu);
5965 int ret;
5966
5967 if (is_guest_mode(vcpu)) {
5968 /* FED8h - SVM Guest */
5969 put_smstate(u64, smstate, 0x7ed8, 1);
5970 /* FEE0h - SVM Guest VMCB Physical Address */
5971 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
5972
5973 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
5974 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
5975 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
5976
5977 ret = nested_svm_vmexit(svm);
5978 if (ret)
5979 return ret;
5980 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02005981 return 0;
5982}
5983
5984static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
5985{
Ladi Prosek05cade72017-10-11 16:54:45 +02005986 struct vcpu_svm *svm = to_svm(vcpu);
5987 struct vmcb *nested_vmcb;
5988 struct page *page;
5989 struct {
5990 u64 guest;
5991 u64 vmcb;
5992 } svm_state_save;
5993 int ret;
5994
5995 ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save,
5996 sizeof(svm_state_save));
5997 if (ret)
5998 return ret;
5999
6000 if (svm_state_save.guest) {
6001 vcpu->arch.hflags &= ~HF_SMM_MASK;
6002 nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page);
6003 if (nested_vmcb)
6004 enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page);
6005 else
6006 ret = 1;
6007 vcpu->arch.hflags |= HF_SMM_MASK;
6008 }
6009 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02006010}
6011
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006012static int enable_smi_window(struct kvm_vcpu *vcpu)
6013{
6014 struct vcpu_svm *svm = to_svm(vcpu);
6015
6016 if (!gif_set(svm)) {
6017 if (vgif_enabled(svm))
6018 set_intercept(svm, INTERCEPT_STGI);
6019 /* STGI will cause a vm exit */
6020 return 1;
6021 }
6022 return 0;
6023}
6024
Brijesh Singh1654efc2017-12-04 10:57:34 -06006025static int sev_asid_new(void)
6026{
6027 int pos;
6028
6029 /*
6030 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
6031 */
6032 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
6033 if (pos >= max_sev_asid)
6034 return -EBUSY;
6035
6036 set_bit(pos, sev_asid_bitmap);
6037 return pos + 1;
6038}
6039
6040static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
6041{
6042 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6043 int asid, ret;
6044
6045 ret = -EBUSY;
6046 asid = sev_asid_new();
6047 if (asid < 0)
6048 return ret;
6049
6050 ret = sev_platform_init(&argp->error);
6051 if (ret)
6052 goto e_free;
6053
6054 sev->active = true;
6055 sev->asid = asid;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006056 INIT_LIST_HEAD(&sev->regions_list);
Brijesh Singh1654efc2017-12-04 10:57:34 -06006057
6058 return 0;
6059
6060e_free:
6061 __sev_asid_free(asid);
6062 return ret;
6063}
6064
Brijesh Singh59414c92017-12-04 10:57:35 -06006065static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
6066{
6067 struct sev_data_activate *data;
6068 int asid = sev_get_asid(kvm);
6069 int ret;
6070
6071 wbinvd_on_all_cpus();
6072
6073 ret = sev_guest_df_flush(error);
6074 if (ret)
6075 return ret;
6076
6077 data = kzalloc(sizeof(*data), GFP_KERNEL);
6078 if (!data)
6079 return -ENOMEM;
6080
6081 /* activate ASID on the given handle */
6082 data->handle = handle;
6083 data->asid = asid;
6084 ret = sev_guest_activate(data, error);
6085 kfree(data);
6086
6087 return ret;
6088}
6089
Brijesh Singh89c50582017-12-04 10:57:35 -06006090static int __sev_issue_cmd(int fd, int id, void *data, int *error)
Brijesh Singh59414c92017-12-04 10:57:35 -06006091{
6092 struct fd f;
6093 int ret;
6094
6095 f = fdget(fd);
6096 if (!f.file)
6097 return -EBADF;
6098
6099 ret = sev_issue_cmd_external_user(f.file, id, data, error);
6100
6101 fdput(f);
6102 return ret;
6103}
6104
Brijesh Singh89c50582017-12-04 10:57:35 -06006105static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
6106{
6107 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6108
6109 return __sev_issue_cmd(sev->fd, id, data, error);
6110}
6111
Brijesh Singh59414c92017-12-04 10:57:35 -06006112static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
6113{
6114 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6115 struct sev_data_launch_start *start;
6116 struct kvm_sev_launch_start params;
6117 void *dh_blob, *session_blob;
6118 int *error = &argp->error;
6119 int ret;
6120
6121 if (!sev_guest(kvm))
6122 return -ENOTTY;
6123
6124 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6125 return -EFAULT;
6126
6127 start = kzalloc(sizeof(*start), GFP_KERNEL);
6128 if (!start)
6129 return -ENOMEM;
6130
6131 dh_blob = NULL;
6132 if (params.dh_uaddr) {
6133 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
6134 if (IS_ERR(dh_blob)) {
6135 ret = PTR_ERR(dh_blob);
6136 goto e_free;
6137 }
6138
6139 start->dh_cert_address = __sme_set(__pa(dh_blob));
6140 start->dh_cert_len = params.dh_len;
6141 }
6142
6143 session_blob = NULL;
6144 if (params.session_uaddr) {
6145 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
6146 if (IS_ERR(session_blob)) {
6147 ret = PTR_ERR(session_blob);
6148 goto e_free_dh;
6149 }
6150
6151 start->session_address = __sme_set(__pa(session_blob));
6152 start->session_len = params.session_len;
6153 }
6154
6155 start->handle = params.handle;
6156 start->policy = params.policy;
6157
6158 /* create memory encryption context */
Brijesh Singh89c50582017-12-04 10:57:35 -06006159 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
Brijesh Singh59414c92017-12-04 10:57:35 -06006160 if (ret)
6161 goto e_free_session;
6162
6163 /* Bind ASID to this guest */
6164 ret = sev_bind_asid(kvm, start->handle, error);
6165 if (ret)
6166 goto e_free_session;
6167
6168 /* return handle to userspace */
6169 params.handle = start->handle;
6170 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
6171 sev_unbind_asid(kvm, start->handle);
6172 ret = -EFAULT;
6173 goto e_free_session;
6174 }
6175
6176 sev->handle = start->handle;
6177 sev->fd = argp->sev_fd;
6178
6179e_free_session:
6180 kfree(session_blob);
6181e_free_dh:
6182 kfree(dh_blob);
6183e_free:
6184 kfree(start);
6185 return ret;
6186}
6187
Brijesh Singh89c50582017-12-04 10:57:35 -06006188static int get_num_contig_pages(int idx, struct page **inpages,
6189 unsigned long npages)
6190{
6191 unsigned long paddr, next_paddr;
6192 int i = idx + 1, pages = 1;
6193
6194 /* find the number of contiguous pages starting from idx */
6195 paddr = __sme_page_pa(inpages[idx]);
6196 while (i < npages) {
6197 next_paddr = __sme_page_pa(inpages[i++]);
6198 if ((paddr + PAGE_SIZE) == next_paddr) {
6199 pages++;
6200 paddr = next_paddr;
6201 continue;
6202 }
6203 break;
6204 }
6205
6206 return pages;
6207}
6208
6209static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
6210{
6211 unsigned long vaddr, vaddr_end, next_vaddr, npages, size;
6212 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6213 struct kvm_sev_launch_update_data params;
6214 struct sev_data_launch_update_data *data;
6215 struct page **inpages;
6216 int i, ret, pages;
6217
6218 if (!sev_guest(kvm))
6219 return -ENOTTY;
6220
6221 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6222 return -EFAULT;
6223
6224 data = kzalloc(sizeof(*data), GFP_KERNEL);
6225 if (!data)
6226 return -ENOMEM;
6227
6228 vaddr = params.uaddr;
6229 size = params.len;
6230 vaddr_end = vaddr + size;
6231
6232 /* Lock the user memory. */
6233 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
6234 if (!inpages) {
6235 ret = -ENOMEM;
6236 goto e_free;
6237 }
6238
6239 /*
6240 * The LAUNCH_UPDATE command will perform in-place encryption of the
6241 * memory content (i.e it will write the same memory region with C=1).
6242 * It's possible that the cache may contain the data with C=0, i.e.,
6243 * unencrypted so invalidate it first.
6244 */
6245 sev_clflush_pages(inpages, npages);
6246
6247 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
6248 int offset, len;
6249
6250 /*
6251 * If the user buffer is not page-aligned, calculate the offset
6252 * within the page.
6253 */
6254 offset = vaddr & (PAGE_SIZE - 1);
6255
6256 /* Calculate the number of pages that can be encrypted in one go. */
6257 pages = get_num_contig_pages(i, inpages, npages);
6258
6259 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
6260
6261 data->handle = sev->handle;
6262 data->len = len;
6263 data->address = __sme_page_pa(inpages[i]) + offset;
6264 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
6265 if (ret)
6266 goto e_unpin;
6267
6268 size -= len;
6269 next_vaddr = vaddr + len;
6270 }
6271
6272e_unpin:
6273 /* content of memory is updated, mark pages dirty */
6274 for (i = 0; i < npages; i++) {
6275 set_page_dirty_lock(inpages[i]);
6276 mark_page_accessed(inpages[i]);
6277 }
6278 /* unlock the user pages */
6279 sev_unpin_memory(kvm, inpages, npages);
6280e_free:
6281 kfree(data);
6282 return ret;
6283}
6284
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006285static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
6286{
Brijesh Singh3e233382018-02-23 12:36:50 -06006287 void __user *measure = (void __user *)(uintptr_t)argp->data;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006288 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6289 struct sev_data_launch_measure *data;
6290 struct kvm_sev_launch_measure params;
Brijesh Singh3e233382018-02-23 12:36:50 -06006291 void __user *p = NULL;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006292 void *blob = NULL;
6293 int ret;
6294
6295 if (!sev_guest(kvm))
6296 return -ENOTTY;
6297
Brijesh Singh3e233382018-02-23 12:36:50 -06006298 if (copy_from_user(&params, measure, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006299 return -EFAULT;
6300
6301 data = kzalloc(sizeof(*data), GFP_KERNEL);
6302 if (!data)
6303 return -ENOMEM;
6304
6305 /* User wants to query the blob length */
6306 if (!params.len)
6307 goto cmd;
6308
Brijesh Singh3e233382018-02-23 12:36:50 -06006309 p = (void __user *)(uintptr_t)params.uaddr;
6310 if (p) {
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006311 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
6312 ret = -EINVAL;
6313 goto e_free;
6314 }
6315
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006316 ret = -ENOMEM;
6317 blob = kmalloc(params.len, GFP_KERNEL);
6318 if (!blob)
6319 goto e_free;
6320
6321 data->address = __psp_pa(blob);
6322 data->len = params.len;
6323 }
6324
6325cmd:
6326 data->handle = sev->handle;
6327 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
6328
6329 /*
6330 * If we query the session length, FW responded with expected data.
6331 */
6332 if (!params.len)
6333 goto done;
6334
6335 if (ret)
6336 goto e_free_blob;
6337
6338 if (blob) {
Brijesh Singh3e233382018-02-23 12:36:50 -06006339 if (copy_to_user(p, blob, params.len))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006340 ret = -EFAULT;
6341 }
6342
6343done:
6344 params.len = data->len;
Brijesh Singh3e233382018-02-23 12:36:50 -06006345 if (copy_to_user(measure, &params, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006346 ret = -EFAULT;
6347e_free_blob:
6348 kfree(blob);
6349e_free:
6350 kfree(data);
6351 return ret;
6352}
6353
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006354static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
6355{
6356 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6357 struct sev_data_launch_finish *data;
6358 int ret;
6359
6360 if (!sev_guest(kvm))
6361 return -ENOTTY;
6362
6363 data = kzalloc(sizeof(*data), GFP_KERNEL);
6364 if (!data)
6365 return -ENOMEM;
6366
6367 data->handle = sev->handle;
6368 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
6369
6370 kfree(data);
6371 return ret;
6372}
6373
Brijesh Singh255d9e72017-12-04 10:57:37 -06006374static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
6375{
6376 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6377 struct kvm_sev_guest_status params;
6378 struct sev_data_guest_status *data;
6379 int ret;
6380
6381 if (!sev_guest(kvm))
6382 return -ENOTTY;
6383
6384 data = kzalloc(sizeof(*data), GFP_KERNEL);
6385 if (!data)
6386 return -ENOMEM;
6387
6388 data->handle = sev->handle;
6389 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
6390 if (ret)
6391 goto e_free;
6392
6393 params.policy = data->policy;
6394 params.state = data->state;
6395 params.handle = data->handle;
6396
6397 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6398 ret = -EFAULT;
6399e_free:
6400 kfree(data);
6401 return ret;
6402}
6403
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006404static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
6405 unsigned long dst, int size,
6406 int *error, bool enc)
6407{
6408 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6409 struct sev_data_dbg *data;
6410 int ret;
6411
6412 data = kzalloc(sizeof(*data), GFP_KERNEL);
6413 if (!data)
6414 return -ENOMEM;
6415
6416 data->handle = sev->handle;
6417 data->dst_addr = dst;
6418 data->src_addr = src;
6419 data->len = size;
6420
6421 ret = sev_issue_cmd(kvm,
6422 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
6423 data, error);
6424 kfree(data);
6425 return ret;
6426}
6427
6428static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
6429 unsigned long dst_paddr, int sz, int *err)
6430{
6431 int offset;
6432
6433 /*
6434 * Its safe to read more than we are asked, caller should ensure that
6435 * destination has enough space.
6436 */
6437 src_paddr = round_down(src_paddr, 16);
6438 offset = src_paddr & 15;
6439 sz = round_up(sz + offset, 16);
6440
6441 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
6442}
6443
6444static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
6445 unsigned long __user dst_uaddr,
6446 unsigned long dst_paddr,
6447 int size, int *err)
6448{
6449 struct page *tpage = NULL;
6450 int ret, offset;
6451
6452 /* if inputs are not 16-byte then use intermediate buffer */
6453 if (!IS_ALIGNED(dst_paddr, 16) ||
6454 !IS_ALIGNED(paddr, 16) ||
6455 !IS_ALIGNED(size, 16)) {
6456 tpage = (void *)alloc_page(GFP_KERNEL);
6457 if (!tpage)
6458 return -ENOMEM;
6459
6460 dst_paddr = __sme_page_pa(tpage);
6461 }
6462
6463 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
6464 if (ret)
6465 goto e_free;
6466
6467 if (tpage) {
6468 offset = paddr & 15;
6469 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
6470 page_address(tpage) + offset, size))
6471 ret = -EFAULT;
6472 }
6473
6474e_free:
6475 if (tpage)
6476 __free_page(tpage);
6477
6478 return ret;
6479}
6480
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006481static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
6482 unsigned long __user vaddr,
6483 unsigned long dst_paddr,
6484 unsigned long __user dst_vaddr,
6485 int size, int *error)
6486{
6487 struct page *src_tpage = NULL;
6488 struct page *dst_tpage = NULL;
6489 int ret, len = size;
6490
6491 /* If source buffer is not aligned then use an intermediate buffer */
6492 if (!IS_ALIGNED(vaddr, 16)) {
6493 src_tpage = alloc_page(GFP_KERNEL);
6494 if (!src_tpage)
6495 return -ENOMEM;
6496
6497 if (copy_from_user(page_address(src_tpage),
6498 (void __user *)(uintptr_t)vaddr, size)) {
6499 __free_page(src_tpage);
6500 return -EFAULT;
6501 }
6502
6503 paddr = __sme_page_pa(src_tpage);
6504 }
6505
6506 /*
6507 * If destination buffer or length is not aligned then do read-modify-write:
6508 * - decrypt destination in an intermediate buffer
6509 * - copy the source buffer in an intermediate buffer
6510 * - use the intermediate buffer as source buffer
6511 */
6512 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
6513 int dst_offset;
6514
6515 dst_tpage = alloc_page(GFP_KERNEL);
6516 if (!dst_tpage) {
6517 ret = -ENOMEM;
6518 goto e_free;
6519 }
6520
6521 ret = __sev_dbg_decrypt(kvm, dst_paddr,
6522 __sme_page_pa(dst_tpage), size, error);
6523 if (ret)
6524 goto e_free;
6525
6526 /*
6527 * If source is kernel buffer then use memcpy() otherwise
6528 * copy_from_user().
6529 */
6530 dst_offset = dst_paddr & 15;
6531
6532 if (src_tpage)
6533 memcpy(page_address(dst_tpage) + dst_offset,
6534 page_address(src_tpage), size);
6535 else {
6536 if (copy_from_user(page_address(dst_tpage) + dst_offset,
6537 (void __user *)(uintptr_t)vaddr, size)) {
6538 ret = -EFAULT;
6539 goto e_free;
6540 }
6541 }
6542
6543 paddr = __sme_page_pa(dst_tpage);
6544 dst_paddr = round_down(dst_paddr, 16);
6545 len = round_up(size, 16);
6546 }
6547
6548 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
6549
6550e_free:
6551 if (src_tpage)
6552 __free_page(src_tpage);
6553 if (dst_tpage)
6554 __free_page(dst_tpage);
6555 return ret;
6556}
6557
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006558static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
6559{
6560 unsigned long vaddr, vaddr_end, next_vaddr;
6561 unsigned long dst_vaddr, dst_vaddr_end;
6562 struct page **src_p, **dst_p;
6563 struct kvm_sev_dbg debug;
6564 unsigned long n;
6565 int ret, size;
6566
6567 if (!sev_guest(kvm))
6568 return -ENOTTY;
6569
6570 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
6571 return -EFAULT;
6572
6573 vaddr = debug.src_uaddr;
6574 size = debug.len;
6575 vaddr_end = vaddr + size;
6576 dst_vaddr = debug.dst_uaddr;
6577 dst_vaddr_end = dst_vaddr + size;
6578
6579 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
6580 int len, s_off, d_off;
6581
6582 /* lock userspace source and destination page */
6583 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
6584 if (!src_p)
6585 return -EFAULT;
6586
6587 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
6588 if (!dst_p) {
6589 sev_unpin_memory(kvm, src_p, n);
6590 return -EFAULT;
6591 }
6592
6593 /*
6594 * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
6595 * memory content (i.e it will write the same memory region with C=1).
6596 * It's possible that the cache may contain the data with C=0, i.e.,
6597 * unencrypted so invalidate it first.
6598 */
6599 sev_clflush_pages(src_p, 1);
6600 sev_clflush_pages(dst_p, 1);
6601
6602 /*
6603 * Since user buffer may not be page aligned, calculate the
6604 * offset within the page.
6605 */
6606 s_off = vaddr & ~PAGE_MASK;
6607 d_off = dst_vaddr & ~PAGE_MASK;
6608 len = min_t(size_t, (PAGE_SIZE - s_off), size);
6609
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006610 if (dec)
6611 ret = __sev_dbg_decrypt_user(kvm,
6612 __sme_page_pa(src_p[0]) + s_off,
6613 dst_vaddr,
6614 __sme_page_pa(dst_p[0]) + d_off,
6615 len, &argp->error);
6616 else
6617 ret = __sev_dbg_encrypt_user(kvm,
6618 __sme_page_pa(src_p[0]) + s_off,
6619 vaddr,
6620 __sme_page_pa(dst_p[0]) + d_off,
6621 dst_vaddr,
6622 len, &argp->error);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006623
6624 sev_unpin_memory(kvm, src_p, 1);
6625 sev_unpin_memory(kvm, dst_p, 1);
6626
6627 if (ret)
6628 goto err;
6629
6630 next_vaddr = vaddr + len;
6631 dst_vaddr = dst_vaddr + len;
6632 size -= len;
6633 }
6634err:
6635 return ret;
6636}
6637
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006638static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
6639{
6640 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6641 struct sev_data_launch_secret *data;
6642 struct kvm_sev_launch_secret params;
6643 struct page **pages;
6644 void *blob, *hdr;
6645 unsigned long n;
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006646 int ret, offset;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006647
6648 if (!sev_guest(kvm))
6649 return -ENOTTY;
6650
6651 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6652 return -EFAULT;
6653
6654 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
6655 if (!pages)
6656 return -ENOMEM;
6657
6658 /*
6659 * The secret must be copied into contiguous memory region, lets verify
6660 * that userspace memory pages are contiguous before we issue command.
6661 */
6662 if (get_num_contig_pages(0, pages, n) != n) {
6663 ret = -EINVAL;
6664 goto e_unpin_memory;
6665 }
6666
6667 ret = -ENOMEM;
6668 data = kzalloc(sizeof(*data), GFP_KERNEL);
6669 if (!data)
6670 goto e_unpin_memory;
6671
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006672 offset = params.guest_uaddr & (PAGE_SIZE - 1);
6673 data->guest_address = __sme_page_pa(pages[0]) + offset;
6674 data->guest_len = params.guest_len;
6675
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006676 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
6677 if (IS_ERR(blob)) {
6678 ret = PTR_ERR(blob);
6679 goto e_free;
6680 }
6681
6682 data->trans_address = __psp_pa(blob);
6683 data->trans_len = params.trans_len;
6684
6685 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
6686 if (IS_ERR(hdr)) {
6687 ret = PTR_ERR(hdr);
6688 goto e_free_blob;
6689 }
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06006690 data->hdr_address = __psp_pa(hdr);
6691 data->hdr_len = params.hdr_len;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006692
6693 data->handle = sev->handle;
6694 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
6695
6696 kfree(hdr);
6697
6698e_free_blob:
6699 kfree(blob);
6700e_free:
6701 kfree(data);
6702e_unpin_memory:
6703 sev_unpin_memory(kvm, pages, n);
6704 return ret;
6705}
6706
Brijesh Singh1654efc2017-12-04 10:57:34 -06006707static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
6708{
6709 struct kvm_sev_cmd sev_cmd;
6710 int r;
6711
6712 if (!svm_sev_enabled())
6713 return -ENOTTY;
6714
6715 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
6716 return -EFAULT;
6717
6718 mutex_lock(&kvm->lock);
6719
6720 switch (sev_cmd.id) {
6721 case KVM_SEV_INIT:
6722 r = sev_guest_init(kvm, &sev_cmd);
6723 break;
Brijesh Singh59414c92017-12-04 10:57:35 -06006724 case KVM_SEV_LAUNCH_START:
6725 r = sev_launch_start(kvm, &sev_cmd);
6726 break;
Brijesh Singh89c50582017-12-04 10:57:35 -06006727 case KVM_SEV_LAUNCH_UPDATE_DATA:
6728 r = sev_launch_update_data(kvm, &sev_cmd);
6729 break;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06006730 case KVM_SEV_LAUNCH_MEASURE:
6731 r = sev_launch_measure(kvm, &sev_cmd);
6732 break;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06006733 case KVM_SEV_LAUNCH_FINISH:
6734 r = sev_launch_finish(kvm, &sev_cmd);
6735 break;
Brijesh Singh255d9e72017-12-04 10:57:37 -06006736 case KVM_SEV_GUEST_STATUS:
6737 r = sev_guest_status(kvm, &sev_cmd);
6738 break;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06006739 case KVM_SEV_DBG_DECRYPT:
6740 r = sev_dbg_crypt(kvm, &sev_cmd, true);
6741 break;
Brijesh Singh7d1594f2017-12-04 10:57:37 -06006742 case KVM_SEV_DBG_ENCRYPT:
6743 r = sev_dbg_crypt(kvm, &sev_cmd, false);
6744 break;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06006745 case KVM_SEV_LAUNCH_SECRET:
6746 r = sev_launch_secret(kvm, &sev_cmd);
6747 break;
Brijesh Singh1654efc2017-12-04 10:57:34 -06006748 default:
6749 r = -EINVAL;
6750 goto out;
6751 }
6752
6753 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
6754 r = -EFAULT;
6755
6756out:
6757 mutex_unlock(&kvm->lock);
6758 return r;
6759}
6760
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006761static int svm_register_enc_region(struct kvm *kvm,
6762 struct kvm_enc_region *range)
6763{
6764 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6765 struct enc_region *region;
6766 int ret = 0;
6767
6768 if (!sev_guest(kvm))
6769 return -ENOTTY;
6770
6771 region = kzalloc(sizeof(*region), GFP_KERNEL);
6772 if (!region)
6773 return -ENOMEM;
6774
6775 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
6776 if (!region->pages) {
6777 ret = -ENOMEM;
6778 goto e_free;
6779 }
6780
6781 /*
6782 * The guest may change the memory encryption attribute from C=0 -> C=1
6783 * or vice versa for this memory range. Lets make sure caches are
6784 * flushed to ensure that guest data gets written into memory with
6785 * correct C-bit.
6786 */
6787 sev_clflush_pages(region->pages, region->npages);
6788
6789 region->uaddr = range->addr;
6790 region->size = range->size;
6791
6792 mutex_lock(&kvm->lock);
6793 list_add_tail(&region->list, &sev->regions_list);
6794 mutex_unlock(&kvm->lock);
6795
6796 return ret;
6797
6798e_free:
6799 kfree(region);
6800 return ret;
6801}
6802
6803static struct enc_region *
6804find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
6805{
6806 struct kvm_sev_info *sev = &kvm->arch.sev_info;
6807 struct list_head *head = &sev->regions_list;
6808 struct enc_region *i;
6809
6810 list_for_each_entry(i, head, list) {
6811 if (i->uaddr == range->addr &&
6812 i->size == range->size)
6813 return i;
6814 }
6815
6816 return NULL;
6817}
6818
6819
6820static int svm_unregister_enc_region(struct kvm *kvm,
6821 struct kvm_enc_region *range)
6822{
6823 struct enc_region *region;
6824 int ret;
6825
6826 mutex_lock(&kvm->lock);
6827
6828 if (!sev_guest(kvm)) {
6829 ret = -ENOTTY;
6830 goto failed;
6831 }
6832
6833 region = find_enc_region(kvm, range);
6834 if (!region) {
6835 ret = -EINVAL;
6836 goto failed;
6837 }
6838
6839 __unregister_enc_region_locked(kvm, region);
6840
6841 mutex_unlock(&kvm->lock);
6842 return 0;
6843
6844failed:
6845 mutex_unlock(&kvm->lock);
6846 return ret;
6847}
6848
Kees Cook404f6aa2016-08-08 16:29:06 -07006849static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006850 .cpu_has_kvm_support = has_svm,
6851 .disabled_by_bios = is_disabled,
6852 .hardware_setup = svm_hardware_setup,
6853 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03006854 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006855 .hardware_enable = svm_hardware_enable,
6856 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02006857 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006858 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006859
6860 .vcpu_create = svm_create_vcpu,
6861 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03006862 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006863
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006864 .vm_init = avic_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06006865 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006866
Avi Kivity04d2cc72007-09-10 18:10:54 +03006867 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006868 .vcpu_load = svm_vcpu_load,
6869 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05006870 .vcpu_blocking = svm_vcpu_blocking,
6871 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006872
Paolo Bonzinia96036b2015-11-10 11:55:36 +01006873 .update_bp_intercept = update_bp_intercept,
Tom Lendacky801e4592018-02-21 13:39:51 -06006874 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006875 .get_msr = svm_get_msr,
6876 .set_msr = svm_set_msr,
6877 .get_segment_base = svm_get_segment_base,
6878 .get_segment = svm_get_segment,
6879 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02006880 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10006881 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02006882 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02006883 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03006884 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006885 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006886 .set_cr3 = svm_set_cr3,
6887 .set_cr4 = svm_set_cr4,
6888 .set_efer = svm_set_efer,
6889 .get_idt = svm_get_idt,
6890 .set_idt = svm_set_idt,
6891 .get_gdt = svm_get_gdt,
6892 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006893 .get_dr6 = svm_get_dr6,
6894 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03006895 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01006896 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03006897 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006898 .get_rflags = svm_get_rflags,
6899 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08006900
Avi Kivity6aa8b732006-12-10 02:21:36 -08006901 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006902
Avi Kivity6aa8b732006-12-10 02:21:36 -08006903 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03006904 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006905 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04006906 .set_interrupt_shadow = svm_set_interrupt_shadow,
6907 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02006908 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03006909 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006910 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02006911 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006912 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02006913 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006914 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006915 .get_nmi_mask = svm_get_nmi_mask,
6916 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006917 .enable_nmi_window = enable_nmi_window,
6918 .enable_irq_window = enable_irq_window,
6919 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08006920 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03006921 .get_enable_apicv = svm_get_enable_apicv,
6922 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08006923 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05006924 .hwapic_irr_update = svm_hwapic_irr_update,
6925 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02006926 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05006927 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02006928
6929 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08006930 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006931 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006932
Avi Kivity586f9602010-11-18 13:09:54 +02006933 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02006934
Sheng Yang17cc3932010-01-05 19:02:27 +08006935 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08006936
6937 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006938
6939 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00006940 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01006941 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08006942 .xsaves_supported = svm_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +02006943 .umip_emulated = svm_umip_emulated,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006944
6945 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006946
6947 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10006948
6949 .write_tsc_offset = svm_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02006950
6951 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02006952
6953 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08006954 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02006955
6956 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02006957
6958 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05006959 .deliver_posted_interrupt = svm_deliver_avic_intr,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05006960 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02006961 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006962
Ladi Prosek72d7b372017-10-11 16:54:41 +02006963 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02006964 .pre_enter_smm = svm_pre_enter_smm,
6965 .pre_leave_smm = svm_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02006966 .enable_smi_window = enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06006967
6968 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06006969 .mem_enc_reg_region = svm_register_enc_region,
6970 .mem_enc_unreg_region = svm_unregister_enc_region,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006971};
6972
6973static int __init svm_init(void)
6974{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08006975 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03006976 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006977}
6978
6979static void __exit svm_exit(void)
6980{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08006981 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08006982}
6983
6984module_init(svm_init)
6985module_exit(svm_exit)