blob: 16ef31b874522143e275591eb93c9705df83c873 [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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080037
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050038#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010039#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020040#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040041#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010042#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020043#include <asm/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080044
Eduardo Habkost63d11422008-11-17 19:03:20 -020045#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030046#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020047
Avi Kivity4ecac3f2008-05-13 13:23:38 +030048#define __ex(x) __kvm_handle_fault_on_reboot(x)
49
Avi Kivity6aa8b732006-12-10 02:21:36 -080050MODULE_AUTHOR("Qumranet");
51MODULE_LICENSE("GPL");
52
Josh Triplettae759542012-03-28 11:32:28 -070053static const struct x86_cpu_id svm_cpu_id[] = {
54 X86_FEATURE_MATCH(X86_FEATURE_SVM),
55 {}
56};
57MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
58
Avi Kivity6aa8b732006-12-10 02:21:36 -080059#define IOPM_ALLOC_ORDER 2
60#define MSRPM_ALLOC_ORDER 1
61
Avi Kivity6aa8b732006-12-10 02:21:36 -080062#define SEG_TYPE_LDT 2
63#define SEG_TYPE_BUSY_TSS16 3
64
Andre Przywara6bc31bd2010-04-11 23:07:28 +020065#define SVM_FEATURE_NPT (1 << 0)
66#define SVM_FEATURE_LBRV (1 << 1)
67#define SVM_FEATURE_SVML (1 << 2)
68#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010069#define SVM_FEATURE_TSC_RATE (1 << 4)
70#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
71#define SVM_FEATURE_FLUSH_ASID (1 << 6)
72#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020073#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030074
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -050075#define SVM_AVIC_DOORBELL 0xc001011b
76
Joerg Roedel410e4d52009-08-07 11:49:44 +020077#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
78#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
79#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
80
Joerg Roedel24e09cb2008-02-13 18:58:47 +010081#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
82
Joerg Roedelfbc0db72011-03-25 09:44:46 +010083#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010084#define TSC_RATIO_MIN 0x0000000000000001ULL
85#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010086
Dan Carpenter5446a972016-05-23 13:20:10 +030087#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -050088
89/*
90 * 0xff is broadcast, so the max index allowed for physical APIC ID
91 * table is 0xfe. APIC IDs above 0xff are reserved.
92 */
93#define AVIC_MAX_PHYSICAL_ID_COUNT 255
94
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -050095#define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
96#define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
97#define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
98
Joerg Roedel67ec6602010-05-17 14:43:35 +020099static bool erratum_383_found __read_mostly;
100
Avi Kivity6c8166a2009-05-31 18:15:37 +0300101static const u32 host_save_user_msrs[] = {
102#ifdef CONFIG_X86_64
103 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
104 MSR_FS_BASE,
105#endif
106 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
Paolo Bonzini46896c72015-11-12 14:49:16 +0100107 MSR_TSC_AUX,
Avi Kivity6c8166a2009-05-31 18:15:37 +0300108};
109
110#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
111
112struct kvm_vcpu;
113
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200114struct nested_state {
115 struct vmcb *hsave;
116 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +0100117 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200118 u64 vmcb;
119
120 /* These are the merged vectors */
121 u32 *msrpm;
122
123 /* gpa pointers to the real vectors */
124 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100125 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200126
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200127 /* A VMEXIT is required but not yet emulated */
128 bool exit_required;
129
Joerg Roedelaad42c62009-08-07 11:49:34 +0200130 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100131 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100132 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200133 u32 intercept_exceptions;
134 u64 intercept;
135
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200136 /* Nested Paging related state */
137 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200138};
139
Joerg Roedel323c3d82010-03-01 15:34:37 +0100140#define MSRPM_OFFSETS 16
141static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
142
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500143/*
144 * Set osvw_len to higher value when updated Revision Guides
145 * are published and we know what the new status bits are
146 */
147static uint64_t osvw_len = 4, osvw_status;
148
Avi Kivity6c8166a2009-05-31 18:15:37 +0300149struct vcpu_svm {
150 struct kvm_vcpu vcpu;
151 struct vmcb *vmcb;
152 unsigned long vmcb_pa;
153 struct svm_cpu_data *svm_data;
154 uint64_t asid_generation;
155 uint64_t sysenter_esp;
156 uint64_t sysenter_eip;
Paolo Bonzini46896c72015-11-12 14:49:16 +0100157 uint64_t tsc_aux;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300158
159 u64 next_rip;
160
161 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200162 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200163 u16 fs;
164 u16 gs;
165 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200166 u64 gs_base;
167 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300168
169 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300170
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200171 ulong nmi_iret_rip;
172
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200173 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200174
175 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100176
177 unsigned int3_injected;
178 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200179 u32 apf_reason;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100180
Joerg Roedel6092d3d2015-10-14 15:10:54 +0200181 /* cached guest cpuid flags for faster access */
182 bool nrips_enabled : 1;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500183
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -0500184 u32 ldr_reg;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500185 struct page *avic_backing_page;
186 u64 *avic_physical_id_cache;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -0500187 bool avic_is_running;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300188};
189
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500190#define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
191#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
192
193#define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
194#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
195#define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
196#define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
197
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100198static DEFINE_PER_CPU(u64, current_tsc_ratio);
199#define TSC_RATIO_DEFAULT 0x0100000000ULL
200
Joerg Roedel455716f2010-03-01 15:34:35 +0100201#define MSR_INVALID 0xffffffffU
202
Mathias Krause09941fb2012-08-30 01:30:20 +0200203static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100204 u32 index; /* Index of the MSR */
205 bool always; /* True if intercept is always on */
206} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400207 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100208 { .index = MSR_IA32_SYSENTER_CS, .always = true },
209#ifdef CONFIG_X86_64
210 { .index = MSR_GS_BASE, .always = true },
211 { .index = MSR_FS_BASE, .always = true },
212 { .index = MSR_KERNEL_GS_BASE, .always = true },
213 { .index = MSR_LSTAR, .always = true },
214 { .index = MSR_CSTAR, .always = true },
215 { .index = MSR_SYSCALL_MASK, .always = true },
216#endif
217 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
218 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
219 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
220 { .index = MSR_IA32_LASTINTTOIP, .always = false },
221 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222};
223
224/* enable NPT for AMD64 and X86 with PAE */
225#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
226static bool npt_enabled = true;
227#else
Joerg Roedele0231712010-02-24 18:59:10 +0100228static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229#endif
230
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100231/* allow nested paging (virtualized MMU) for all guests */
232static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233module_param(npt, int, S_IRUGO);
234
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100235/* allow nested virtualization in KVM/SVM */
236static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800237module_param(nested, int, S_IRUGO);
238
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500239/* enable / disable AVIC */
240static int avic;
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500241#ifdef CONFIG_X86_LOCAL_APIC
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500242module_param(avic, int, S_IRUGO);
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500243#endif
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500244
Paolo Bonzini79a80592015-09-21 07:46:55 +0200245static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200247static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800248
Joerg Roedel410e4d52009-08-07 11:49:44 +0200249static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100250static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800252static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
253 bool has_error_code, u32 error_code);
254
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100255enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100256 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
257 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100258 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100259 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100260 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100261 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100262 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100263 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100264 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100265 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100266 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100267 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500268 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
269 * AVIC PHYSICAL_TABLE pointer,
270 * AVIC LOGICAL_TABLE pointer
271 */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100272 VMCB_DIRTY_MAX,
273};
274
Joerg Roedel0574dec2010-12-03 11:45:58 +0100275/* TPR and CR2 are always written before VMRUN */
276#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100277
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500278#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
279
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100280static inline void mark_all_dirty(struct vmcb *vmcb)
281{
282 vmcb->control.clean = 0;
283}
284
285static inline void mark_all_clean(struct vmcb *vmcb)
286{
287 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
288 & ~VMCB_ALWAYS_DIRTY_MASK;
289}
290
291static inline void mark_dirty(struct vmcb *vmcb, int bit)
292{
293 vmcb->control.clean &= ~(1 << bit);
294}
295
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
297{
298 return container_of(vcpu, struct vcpu_svm, vcpu);
299}
300
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500301static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
302{
303 svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
304 mark_dirty(svm->vmcb, VMCB_AVIC);
305}
306
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -0500307static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
308{
309 struct vcpu_svm *svm = to_svm(vcpu);
310 u64 *entry = svm->avic_physical_id_cache;
311
312 if (!entry)
313 return false;
314
315 return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
316}
317
Joerg Roedel384c6362010-11-30 18:03:56 +0100318static void recalc_intercepts(struct vcpu_svm *svm)
319{
320 struct vmcb_control_area *c, *h;
321 struct nested_state *g;
322
Joerg Roedel116a0a22010-12-03 11:45:49 +0100323 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
324
Joerg Roedel384c6362010-11-30 18:03:56 +0100325 if (!is_guest_mode(&svm->vcpu))
326 return;
327
328 c = &svm->vmcb->control;
329 h = &svm->nested.hsave->control;
330 g = &svm->nested;
331
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100332 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100333 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100334 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
335 c->intercept = h->intercept | g->intercept;
336}
337
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100338static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
339{
340 if (is_guest_mode(&svm->vcpu))
341 return svm->nested.hsave;
342 else
343 return svm->vmcb;
344}
345
346static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
347{
348 struct vmcb *vmcb = get_host_vmcb(svm);
349
350 vmcb->control.intercept_cr |= (1U << bit);
351
352 recalc_intercepts(svm);
353}
354
355static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
356{
357 struct vmcb *vmcb = get_host_vmcb(svm);
358
359 vmcb->control.intercept_cr &= ~(1U << bit);
360
361 recalc_intercepts(svm);
362}
363
364static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
365{
366 struct vmcb *vmcb = get_host_vmcb(svm);
367
368 return vmcb->control.intercept_cr & (1U << bit);
369}
370
Paolo Bonzini5315c712014-03-03 13:08:29 +0100371static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100372{
373 struct vmcb *vmcb = get_host_vmcb(svm);
374
Paolo Bonzini5315c712014-03-03 13:08:29 +0100375 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
376 | (1 << INTERCEPT_DR1_READ)
377 | (1 << INTERCEPT_DR2_READ)
378 | (1 << INTERCEPT_DR3_READ)
379 | (1 << INTERCEPT_DR4_READ)
380 | (1 << INTERCEPT_DR5_READ)
381 | (1 << INTERCEPT_DR6_READ)
382 | (1 << INTERCEPT_DR7_READ)
383 | (1 << INTERCEPT_DR0_WRITE)
384 | (1 << INTERCEPT_DR1_WRITE)
385 | (1 << INTERCEPT_DR2_WRITE)
386 | (1 << INTERCEPT_DR3_WRITE)
387 | (1 << INTERCEPT_DR4_WRITE)
388 | (1 << INTERCEPT_DR5_WRITE)
389 | (1 << INTERCEPT_DR6_WRITE)
390 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100391
392 recalc_intercepts(svm);
393}
394
Paolo Bonzini5315c712014-03-03 13:08:29 +0100395static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100396{
397 struct vmcb *vmcb = get_host_vmcb(svm);
398
Paolo Bonzini5315c712014-03-03 13:08:29 +0100399 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100400
401 recalc_intercepts(svm);
402}
403
Joerg Roedel18c918c2010-11-30 18:03:59 +0100404static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
405{
406 struct vmcb *vmcb = get_host_vmcb(svm);
407
408 vmcb->control.intercept_exceptions |= (1U << bit);
409
410 recalc_intercepts(svm);
411}
412
413static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
414{
415 struct vmcb *vmcb = get_host_vmcb(svm);
416
417 vmcb->control.intercept_exceptions &= ~(1U << bit);
418
419 recalc_intercepts(svm);
420}
421
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100422static inline void set_intercept(struct vcpu_svm *svm, int bit)
423{
424 struct vmcb *vmcb = get_host_vmcb(svm);
425
426 vmcb->control.intercept |= (1ULL << bit);
427
428 recalc_intercepts(svm);
429}
430
431static inline void clr_intercept(struct vcpu_svm *svm, int bit)
432{
433 struct vmcb *vmcb = get_host_vmcb(svm);
434
435 vmcb->control.intercept &= ~(1ULL << bit);
436
437 recalc_intercepts(svm);
438}
439
Joerg Roedel2af91942009-08-07 11:49:28 +0200440static inline void enable_gif(struct vcpu_svm *svm)
441{
442 svm->vcpu.arch.hflags |= HF_GIF_MASK;
443}
444
445static inline void disable_gif(struct vcpu_svm *svm)
446{
447 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
448}
449
450static inline bool gif_set(struct vcpu_svm *svm)
451{
452 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
453}
454
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455static unsigned long iopm_base;
456
457struct kvm_ldttss_desc {
458 u16 limit0;
459 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100460 unsigned base1:8, type:5, dpl:2, p:1;
461 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462 u32 base3;
463 u32 zero1;
464} __attribute__((packed));
465
466struct svm_cpu_data {
467 int cpu;
468
Avi Kivity5008fdf2007-04-02 13:05:50 +0300469 u64 asid_generation;
470 u32 max_asid;
471 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472 struct kvm_ldttss_desc *tss_desc;
473
474 struct page *save_area;
475};
476
477static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
478
479struct svm_init_data {
480 int cpu;
481 int r;
482};
483
Mathias Krause09941fb2012-08-30 01:30:20 +0200484static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200486#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487#define MSRS_RANGE_SIZE 2048
488#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
489
Joerg Roedel455716f2010-03-01 15:34:35 +0100490static u32 svm_msrpm_offset(u32 msr)
491{
492 u32 offset;
493 int i;
494
495 for (i = 0; i < NUM_MSR_MAPS; i++) {
496 if (msr < msrpm_ranges[i] ||
497 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
498 continue;
499
500 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
501 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
502
503 /* Now we have the u8 offset - but need the u32 offset */
504 return offset / 4;
505 }
506
507 /* MSR not in any range */
508 return MSR_INVALID;
509}
510
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511#define MAX_INST_SIZE 15
512
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513static inline void clgi(void)
514{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300515 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516}
517
518static inline void stgi(void)
519{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300520 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521}
522
523static inline void invlpga(unsigned long addr, u32 asid)
524{
Joerg Roedele0231712010-02-24 18:59:10 +0100525 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526}
527
Joerg Roedel4b161842010-09-10 17:31:03 +0200528static int get_npt_level(void)
529{
530#ifdef CONFIG_X86_64
531 return PT64_ROOT_LEVEL;
532#else
533 return PT32E_ROOT_LEVEL;
534#endif
535}
536
Avi Kivity6aa8b732006-12-10 02:21:36 -0800537static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
538{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000539 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100540 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600541 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542
Alexander Graf9962d032008-11-25 20:17:02 +0100543 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100544 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545}
546
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547static int is_external_interrupt(u32 info)
548{
549 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
550 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
551}
552
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200553static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400554{
555 struct vcpu_svm *svm = to_svm(vcpu);
556 u32 ret = 0;
557
558 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200559 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
560 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400561}
562
563static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
564{
565 struct vcpu_svm *svm = to_svm(vcpu);
566
567 if (mask == 0)
568 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
569 else
570 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
571
572}
573
Avi Kivity6aa8b732006-12-10 02:21:36 -0800574static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
575{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400576 struct vcpu_svm *svm = to_svm(vcpu);
577
Bandan Dasf1047652015-06-11 02:05:33 -0400578 if (svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200579 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200580 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400581 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200582
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400583 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100584 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300585 EMULATE_DONE)
586 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587 return;
588 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300589 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
590 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
591 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300593 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400594 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595}
596
Jan Kiszka116a4752010-02-23 17:47:54 +0100597static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200598 bool has_error_code, u32 error_code,
599 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100600{
601 struct vcpu_svm *svm = to_svm(vcpu);
602
Joerg Roedele0231712010-02-24 18:59:10 +0100603 /*
604 * If we are within a nested VM we'd better #VMEXIT and let the guest
605 * handle the exception
606 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200607 if (!reinject &&
608 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100609 return;
610
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200611 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100612 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
613
614 /*
615 * For guest debugging where we have to reinject #BP if some
616 * INT3 is guest-owned:
617 * Emulate nRIP by moving RIP forward. Will fail if injection
618 * raises a fault that is not intercepted. Still better than
619 * failing in all cases.
620 */
621 skip_emulated_instruction(&svm->vcpu);
622 rip = kvm_rip_read(&svm->vcpu);
623 svm->int3_rip = rip + svm->vmcb->save.cs.base;
624 svm->int3_injected = rip - old_rip;
625 }
626
Jan Kiszka116a4752010-02-23 17:47:54 +0100627 svm->vmcb->control.event_inj = nr
628 | SVM_EVTINJ_VALID
629 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
630 | SVM_EVTINJ_TYPE_EXEPT;
631 svm->vmcb->control.event_inj_err = error_code;
632}
633
Joerg Roedel67ec6602010-05-17 14:43:35 +0200634static void svm_init_erratum_383(void)
635{
636 u32 low, high;
637 int err;
638 u64 val;
639
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100640 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200641 return;
642
643 /* Use _safe variants to not break nested virtualization */
644 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
645 if (err)
646 return;
647
648 val |= (1ULL << 47);
649
650 low = lower_32_bits(val);
651 high = upper_32_bits(val);
652
653 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
654
655 erratum_383_found = true;
656}
657
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500658static void svm_init_osvw(struct kvm_vcpu *vcpu)
659{
660 /*
661 * Guests should see errata 400 and 415 as fixed (assuming that
662 * HLT and IO instructions are intercepted).
663 */
664 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
665 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
666
667 /*
668 * By increasing VCPU's osvw.length to 3 we are telling the guest that
669 * all osvw.status bits inside that length, including bit 0 (which is
670 * reserved for erratum 298), are valid. However, if host processor's
671 * osvw_len is 0 then osvw_status[0] carries no information. We need to
672 * be conservative here and therefore we tell the guest that erratum 298
673 * is present (because we really don't know).
674 */
675 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
676 vcpu->arch.osvw.status |= 1;
677}
678
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679static int has_svm(void)
680{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200681 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682
Eduardo Habkost63d11422008-11-17 19:03:20 -0200683 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800684 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685 return 0;
686 }
687
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688 return 1;
689}
690
Radim Krčmář13a34e02014-08-28 15:13:03 +0200691static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100693 /* Make sure we clean up behind us */
694 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
695 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
696
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200697 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100698
699 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700}
701
Radim Krčmář13a34e02014-08-28 15:13:03 +0200702static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703{
704
Tejun Heo0fe1e002009-10-29 22:34:14 +0900705 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200707 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708 struct desc_struct *gdt;
709 int me = raw_smp_processor_id();
710
Alexander Graf10474ae2009-09-15 11:37:46 +0200711 rdmsrl(MSR_EFER, efer);
712 if (efer & EFER_SVME)
713 return -EBUSY;
714
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200716 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200717 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900719 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900720 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200721 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200722 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723 }
724
Tejun Heo0fe1e002009-10-29 22:34:14 +0900725 sd->asid_generation = 1;
726 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
727 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200729 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200730 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900731 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732
Alexander Graf9962d032008-11-25 20:17:02 +0100733 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734
Linus Torvaldsd0316552009-12-14 09:58:24 -0800735 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200736
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100737 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
738 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500739 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100740 }
741
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500742
743 /*
744 * Get OSVW bits.
745 *
746 * Note that it is possible to have a system with mixed processor
747 * revisions and therefore different OSVW bits. If bits are not the same
748 * on different processors then choose the worst case (i.e. if erratum
749 * is present on one processor and not on another then assume that the
750 * erratum is present everywhere).
751 */
752 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
753 uint64_t len, status = 0;
754 int err;
755
756 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
757 if (!err)
758 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
759 &err);
760
761 if (err)
762 osvw_status = osvw_len = 0;
763 else {
764 if (len < osvw_len)
765 osvw_len = len;
766 osvw_status |= status;
767 osvw_status &= (1ULL << osvw_len) - 1;
768 }
769 } else
770 osvw_status = osvw_len = 0;
771
Joerg Roedel67ec6602010-05-17 14:43:35 +0200772 svm_init_erratum_383();
773
Joerg Roedel1018faa2012-02-29 14:57:32 +0100774 amd_pmu_enable_virt();
775
Alexander Graf10474ae2009-09-15 11:37:46 +0200776 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777}
778
Joerg Roedel0da1db752008-07-02 16:02:11 +0200779static void svm_cpu_uninit(int cpu)
780{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900781 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200782
Tejun Heo0fe1e002009-10-29 22:34:14 +0900783 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200784 return;
785
786 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900787 __free_page(sd->save_area);
788 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200789}
790
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791static int svm_cpu_init(int cpu)
792{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900793 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 int r;
795
Tejun Heo0fe1e002009-10-29 22:34:14 +0900796 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
797 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900799 sd->cpu = cpu;
800 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900802 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803 goto err_1;
804
Tejun Heo0fe1e002009-10-29 22:34:14 +0900805 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806
807 return 0;
808
809err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900810 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 return r;
812
813}
814
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100815static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816{
817 int i;
818
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100819 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
820 if (direct_access_msrs[i].index == index)
821 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100823 return false;
824}
825
Avi Kivity6aa8b732006-12-10 02:21:36 -0800826static void set_msr_interception(u32 *msrpm, unsigned msr,
827 int read, int write)
828{
Joerg Roedel455716f2010-03-01 15:34:35 +0100829 u8 bit_read, bit_write;
830 unsigned long tmp;
831 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100833 /*
834 * If this warning triggers extend the direct_access_msrs list at the
835 * beginning of the file
836 */
837 WARN_ON(!valid_msr_intercept(msr));
838
Joerg Roedel455716f2010-03-01 15:34:35 +0100839 offset = svm_msrpm_offset(msr);
840 bit_read = 2 * (msr & 0x0f);
841 bit_write = 2 * (msr & 0x0f) + 1;
842 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843
Joerg Roedel455716f2010-03-01 15:34:35 +0100844 BUG_ON(offset == MSR_INVALID);
845
846 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
847 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
848
849 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850}
851
Joerg Roedelf65c2292008-02-13 18:58:46 +0100852static void svm_vcpu_init_msrpm(u32 *msrpm)
853{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100854 int i;
855
Joerg Roedelf65c2292008-02-13 18:58:46 +0100856 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
857
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100858 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
859 if (!direct_access_msrs[i].always)
860 continue;
861
862 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
863 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100864}
865
Joerg Roedel323c3d82010-03-01 15:34:37 +0100866static void add_msr_offset(u32 offset)
867{
868 int i;
869
870 for (i = 0; i < MSRPM_OFFSETS; ++i) {
871
872 /* Offset already in list? */
873 if (msrpm_offsets[i] == offset)
874 return;
875
876 /* Slot used by another offset? */
877 if (msrpm_offsets[i] != MSR_INVALID)
878 continue;
879
880 /* Add offset to list */
881 msrpm_offsets[i] = offset;
882
883 return;
884 }
885
886 /*
887 * If this BUG triggers the msrpm_offsets table has an overflow. Just
888 * increase MSRPM_OFFSETS in this case.
889 */
890 BUG();
891}
892
893static void init_msrpm_offsets(void)
894{
895 int i;
896
897 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
898
899 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
900 u32 offset;
901
902 offset = svm_msrpm_offset(direct_access_msrs[i].index);
903 BUG_ON(offset == MSR_INVALID);
904
905 add_msr_offset(offset);
906 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907}
908
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100909static void svm_enable_lbrv(struct vcpu_svm *svm)
910{
911 u32 *msrpm = svm->msrpm;
912
913 svm->vmcb->control.lbr_ctl = 1;
914 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
915 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
916 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
917 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
918}
919
920static void svm_disable_lbrv(struct vcpu_svm *svm)
921{
922 u32 *msrpm = svm->msrpm;
923
924 svm->vmcb->control.lbr_ctl = 0;
925 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
926 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
927 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
928 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
929}
930
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931static __init int svm_hardware_setup(void)
932{
933 int cpu;
934 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100935 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 int r;
937
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
939
940 if (!iopm_pages)
941 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300942
943 iopm_va = page_address(iopm_pages);
944 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
946
Joerg Roedel323c3d82010-03-01 15:34:37 +0100947 init_msrpm_offsets();
948
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100949 if (boot_cpu_has(X86_FEATURE_NX))
950 kvm_enable_efer_bits(EFER_NX);
951
Alexander Graf1b2fd702009-02-02 16:23:51 +0100952 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
953 kvm_enable_efer_bits(EFER_FFXSR);
954
Joerg Roedel92a1f122011-03-25 09:44:51 +0100955 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +0100956 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800957 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
958 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100959 }
960
Alexander Graf236de052008-11-25 20:17:10 +0100961 if (nested) {
962 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200963 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100964 }
965
Zachary Amsden3230bb42009-09-29 11:38:37 -1000966 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 r = svm_cpu_init(cpu);
968 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100969 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100971
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200972 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100973 npt_enabled = false;
974
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100975 if (npt_enabled && !npt) {
976 printk(KERN_INFO "kvm: Nested Paging disabled\n");
977 npt_enabled = false;
978 }
979
Joerg Roedel18552672008-02-07 13:47:41 +0100980 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100981 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100982 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200983 } else
984 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100985
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -0500986 if (avic) {
987 if (!npt_enabled ||
988 !boot_cpu_has(X86_FEATURE_AVIC) ||
989 !IS_ENABLED(CONFIG_X86_LOCAL_APIC))
990 avic = false;
991 else
992 pr_info("AVIC enabled\n");
993 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500994
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995 return 0;
996
Joerg Roedelf65c2292008-02-13 18:58:46 +0100997err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
999 iopm_base = 0;
1000 return r;
1001}
1002
1003static __exit void svm_hardware_unsetup(void)
1004{
Joerg Roedel0da1db752008-07-02 16:02:11 +02001005 int cpu;
1006
Zachary Amsden3230bb42009-09-29 11:38:37 -10001007 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +02001008 svm_cpu_uninit(cpu);
1009
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001011 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012}
1013
1014static void init_seg(struct vmcb_seg *seg)
1015{
1016 seg->selector = 0;
1017 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001018 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019 seg->limit = 0xffff;
1020 seg->base = 0;
1021}
1022
1023static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1024{
1025 seg->selector = 0;
1026 seg->attrib = SVM_SELECTOR_P_MASK | type;
1027 seg->limit = 0xffff;
1028 seg->base = 0;
1029}
1030
Will Auldba904632012-11-29 12:42:50 -08001031static u64 svm_read_tsc_offset(struct kvm_vcpu *vcpu)
1032{
1033 struct vcpu_svm *svm = to_svm(vcpu);
1034
1035 return svm->vmcb->control.tsc_offset;
1036}
1037
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001038static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1039{
1040 struct vcpu_svm *svm = to_svm(vcpu);
1041 u64 g_tsc_offset = 0;
1042
Joerg Roedel20307532010-11-29 17:51:48 +01001043 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001044 g_tsc_offset = svm->vmcb->control.tsc_offset -
1045 svm->nested.hsave->control.tsc_offset;
1046 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001047 } else
1048 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1049 svm->vmcb->control.tsc_offset,
1050 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001051
1052 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001053
1054 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001055}
1056
Haozhong Zhang58ea6762015-10-20 15:39:06 +08001057static void svm_adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, s64 adjustment)
Zachary Amsdene48672f2010-08-19 22:07:23 -10001058{
1059 struct vcpu_svm *svm = to_svm(vcpu);
1060
1061 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +01001062 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -10001063 svm->nested.hsave->control.tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001064 else
1065 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1066 svm->vmcb->control.tsc_offset - adjustment,
1067 svm->vmcb->control.tsc_offset);
1068
Joerg Roedel116a0a22010-12-03 11:45:49 +01001069 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001070}
1071
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001072static void avic_init_vmcb(struct vcpu_svm *svm)
1073{
1074 struct vmcb *vmcb = svm->vmcb;
1075 struct kvm_arch *vm_data = &svm->vcpu.kvm->arch;
1076 phys_addr_t bpa = page_to_phys(svm->avic_backing_page);
1077 phys_addr_t lpa = page_to_phys(vm_data->avic_logical_id_table_page);
1078 phys_addr_t ppa = page_to_phys(vm_data->avic_physical_id_table_page);
1079
1080 vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
1081 vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
1082 vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
1083 vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
1084 vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
1085 svm->vcpu.arch.apicv_active = true;
1086}
1087
Paolo Bonzini56908912015-10-19 11:30:19 +02001088static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089{
Joerg Roedele6101a92008-02-13 18:58:45 +01001090 struct vmcb_control_area *control = &svm->vmcb->control;
1091 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092
Avi Kivitybff78272010-01-07 13:16:08 +02001093 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001094 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001095
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001096 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1097 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1098 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1099 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1100 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1101 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001102 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1103 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104
Paolo Bonzini5315c712014-03-03 13:08:29 +01001105 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106
Joerg Roedel18c918c2010-11-30 18:03:59 +01001107 set_exception_intercept(svm, PF_VECTOR);
1108 set_exception_intercept(svm, UD_VECTOR);
1109 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001110 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001111 set_exception_intercept(svm, DB_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001113 set_intercept(svm, INTERCEPT_INTR);
1114 set_intercept(svm, INTERCEPT_NMI);
1115 set_intercept(svm, INTERCEPT_SMI);
1116 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001117 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001118 set_intercept(svm, INTERCEPT_CPUID);
1119 set_intercept(svm, INTERCEPT_INVD);
1120 set_intercept(svm, INTERCEPT_HLT);
1121 set_intercept(svm, INTERCEPT_INVLPG);
1122 set_intercept(svm, INTERCEPT_INVLPGA);
1123 set_intercept(svm, INTERCEPT_IOIO_PROT);
1124 set_intercept(svm, INTERCEPT_MSR_PROT);
1125 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1126 set_intercept(svm, INTERCEPT_SHUTDOWN);
1127 set_intercept(svm, INTERCEPT_VMRUN);
1128 set_intercept(svm, INTERCEPT_VMMCALL);
1129 set_intercept(svm, INTERCEPT_VMLOAD);
1130 set_intercept(svm, INTERCEPT_VMSAVE);
1131 set_intercept(svm, INTERCEPT_STGI);
1132 set_intercept(svm, INTERCEPT_CLGI);
1133 set_intercept(svm, INTERCEPT_SKINIT);
1134 set_intercept(svm, INTERCEPT_WBINVD);
1135 set_intercept(svm, INTERCEPT_MONITOR);
1136 set_intercept(svm, INTERCEPT_MWAIT);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001137 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138
1139 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001140 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 control->int_ctl = V_INTR_MASKING_MASK;
1142
1143 init_seg(&save->es);
1144 init_seg(&save->ss);
1145 init_seg(&save->ds);
1146 init_seg(&save->fs);
1147 init_seg(&save->gs);
1148
1149 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001150 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 /* Executable/Readable Code Segment */
1152 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1153 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1154 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155
1156 save->gdtr.limit = 0xffff;
1157 save->idtr.limit = 0xffff;
1158
1159 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1160 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1161
Paolo Bonzini56908912015-10-19 11:30:19 +02001162 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001163 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001164 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001166 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167
Joerg Roedele0231712010-02-24 18:59:10 +01001168 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001169 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001170 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001172 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001173 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001174
Rusty Russell66aee912007-07-17 23:34:16 +10001175 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001177
1178 if (npt_enabled) {
1179 /* Setup VMCB for Nested Paging */
1180 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001181 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001182 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001183 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1184 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001185 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001186 save->cr3 = 0;
1187 save->cr4 = 0;
1188 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001189 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001190
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001191 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001192 svm->vcpu.arch.hflags = 0;
1193
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001194 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001195 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001196 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001197 }
1198
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001199 if (avic)
1200 avic_init_vmcb(svm);
1201
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001202 mark_all_dirty(svm->vmcb);
1203
Joerg Roedel2af91942009-08-07 11:49:28 +02001204 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001205
1206}
1207
1208static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu, int index)
1209{
1210 u64 *avic_physical_id_table;
1211 struct kvm_arch *vm_data = &vcpu->kvm->arch;
1212
1213 if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
1214 return NULL;
1215
1216 avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page);
1217
1218 return &avic_physical_id_table[index];
1219}
1220
1221/**
1222 * Note:
1223 * AVIC hardware walks the nested page table to check permissions,
1224 * but does not use the SPA address specified in the leaf page
1225 * table entry since it uses address in the AVIC_BACKING_PAGE pointer
1226 * field of the VMCB. Therefore, we set up the
1227 * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
1228 */
1229static int avic_init_access_page(struct kvm_vcpu *vcpu)
1230{
1231 struct kvm *kvm = vcpu->kvm;
1232 int ret;
1233
1234 if (kvm->arch.apic_access_page_done)
1235 return 0;
1236
1237 ret = x86_set_memory_region(kvm,
1238 APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
1239 APIC_DEFAULT_PHYS_BASE,
1240 PAGE_SIZE);
1241 if (ret)
1242 return ret;
1243
1244 kvm->arch.apic_access_page_done = true;
1245 return 0;
1246}
1247
1248static int avic_init_backing_page(struct kvm_vcpu *vcpu)
1249{
1250 int ret;
1251 u64 *entry, new_entry;
1252 int id = vcpu->vcpu_id;
1253 struct vcpu_svm *svm = to_svm(vcpu);
1254
1255 ret = avic_init_access_page(vcpu);
1256 if (ret)
1257 return ret;
1258
1259 if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
1260 return -EINVAL;
1261
1262 if (!svm->vcpu.arch.apic->regs)
1263 return -EINVAL;
1264
1265 svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
1266
1267 /* Setting AVIC backing page address in the phy APIC ID table */
1268 entry = avic_get_physical_id_entry(vcpu, id);
1269 if (!entry)
1270 return -EINVAL;
1271
1272 new_entry = READ_ONCE(*entry);
1273 new_entry = (page_to_phys(svm->avic_backing_page) &
1274 AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
1275 AVIC_PHYSICAL_ID_ENTRY_VALID_MASK;
1276 WRITE_ONCE(*entry, new_entry);
1277
1278 svm->avic_physical_id_cache = entry;
1279
1280 return 0;
1281}
1282
1283static void avic_vm_destroy(struct kvm *kvm)
1284{
1285 struct kvm_arch *vm_data = &kvm->arch;
1286
1287 if (vm_data->avic_logical_id_table_page)
1288 __free_page(vm_data->avic_logical_id_table_page);
1289 if (vm_data->avic_physical_id_table_page)
1290 __free_page(vm_data->avic_physical_id_table_page);
1291}
1292
1293static int avic_vm_init(struct kvm *kvm)
1294{
1295 int err = -ENOMEM;
1296 struct kvm_arch *vm_data = &kvm->arch;
1297 struct page *p_page;
1298 struct page *l_page;
1299
1300 if (!avic)
1301 return 0;
1302
1303 /* Allocating physical APIC ID table (4KB) */
1304 p_page = alloc_page(GFP_KERNEL);
1305 if (!p_page)
1306 goto free_avic;
1307
1308 vm_data->avic_physical_id_table_page = p_page;
1309 clear_page(page_address(p_page));
1310
1311 /* Allocating logical APIC ID table (4KB) */
1312 l_page = alloc_page(GFP_KERNEL);
1313 if (!l_page)
1314 goto free_avic;
1315
1316 vm_data->avic_logical_id_table_page = l_page;
1317 clear_page(page_address(l_page));
1318
1319 return 0;
1320
1321free_avic:
1322 avic_vm_destroy(kvm);
1323 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324}
1325
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001326/**
1327 * This function is called during VCPU halt/unhalt.
1328 */
1329static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
1330{
1331 u64 entry;
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05001332 int h_physical_id = kvm_cpu_get_apicid(vcpu->cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001333 struct vcpu_svm *svm = to_svm(vcpu);
1334
1335 if (!kvm_vcpu_apicv_active(vcpu))
1336 return;
1337
1338 svm->avic_is_running = is_run;
1339
1340 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
1341 if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
1342 return;
1343
1344 entry = READ_ONCE(*(svm->avic_physical_id_cache));
1345 WARN_ON(is_run == !!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK));
1346
1347 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1348 if (is_run)
1349 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1350 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
1351}
1352
1353static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1354{
1355 u64 entry;
1356 /* ID = 0xff (broadcast), ID > 0xff (reserved) */
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05001357 int h_physical_id = kvm_cpu_get_apicid(cpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001358 struct vcpu_svm *svm = to_svm(vcpu);
1359
1360 if (!kvm_vcpu_apicv_active(vcpu))
1361 return;
1362
1363 if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
1364 return;
1365
1366 entry = READ_ONCE(*(svm->avic_physical_id_cache));
1367 WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
1368
1369 entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
1370 entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
1371
1372 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1373 if (svm->avic_is_running)
1374 entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1375
1376 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
1377}
1378
1379static void avic_vcpu_put(struct kvm_vcpu *vcpu)
1380{
1381 u64 entry;
1382 struct vcpu_svm *svm = to_svm(vcpu);
1383
1384 if (!kvm_vcpu_apicv_active(vcpu))
1385 return;
1386
1387 entry = READ_ONCE(*(svm->avic_physical_id_cache));
1388 entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
1389 WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390}
1391
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001392static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001393{
1394 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001395 u32 dummy;
1396 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001397
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001398 if (!init_event) {
1399 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1400 MSR_IA32_APICBASE_ENABLE;
1401 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1402 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1403 }
Paolo Bonzini56908912015-10-19 11:30:19 +02001404 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001405
Julian Stecklina66f7b722012-12-05 15:26:19 +01001406 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy);
1407 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001408
1409 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1410 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001411}
1412
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001413static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001415 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001417 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001418 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001419 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001420 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421
Rusty Russellc16f8622007-07-30 21:12:19 +10001422 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001423 if (!svm) {
1424 err = -ENOMEM;
1425 goto out;
1426 }
1427
1428 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1429 if (err)
1430 goto free_svm;
1431
Joerg Roedelf65c2292008-02-13 18:58:46 +01001432 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001433 page = alloc_page(GFP_KERNEL);
1434 if (!page)
1435 goto uninit;
1436
Joerg Roedelf65c2292008-02-13 18:58:46 +01001437 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1438 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001439 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001440
1441 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1442 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001443 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001444
Alexander Grafb286d5d2008-11-25 20:17:05 +01001445 hsave_page = alloc_page(GFP_KERNEL);
1446 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001447 goto free_page3;
1448
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001449 if (avic) {
1450 err = avic_init_backing_page(&svm->vcpu);
1451 if (err)
1452 goto free_page4;
1453 }
1454
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001455 /* We initialize this flag to true to make sure that the is_running
1456 * bit would be set the first time the vcpu is loaded.
1457 */
1458 svm->avic_is_running = true;
1459
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001460 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001461
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001462 svm->msrpm = page_address(msrpm_pages);
1463 svm_vcpu_init_msrpm(svm->msrpm);
1464
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001465 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001466 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001467
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001468 svm->vmcb = page_address(page);
1469 clear_page(svm->vmcb);
1470 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1471 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02001472 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001474 svm_init_osvw(&svm->vcpu);
1475
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001476 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001477
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001478free_page4:
1479 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001480free_page3:
1481 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1482free_page2:
1483 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1484free_page1:
1485 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001486uninit:
1487 kvm_vcpu_uninit(&svm->vcpu);
1488free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001489 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001490out:
1491 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492}
1493
1494static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1495{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001496 struct vcpu_svm *svm = to_svm(vcpu);
1497
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001498 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001499 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001500 __free_page(virt_to_page(svm->nested.hsave));
1501 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001502 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001503 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504}
1505
Avi Kivity15ad7142007-07-11 18:17:21 +03001506static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001508 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001509 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001510
Avi Kivity0cc50642007-03-25 12:07:27 +02001511 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001512 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001513 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001514 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001515
Avi Kivity82ca2d12010-10-21 12:20:34 +02001516#ifdef CONFIG_X86_64
1517 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1518#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001519 savesegment(fs, svm->host.fs);
1520 savesegment(gs, svm->host.gs);
1521 svm->host.ldt = kvm_read_ldt();
1522
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001523 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001524 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001525
Haozhong Zhangad7218832015-10-20 15:39:02 +08001526 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1527 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1528 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1529 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1530 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1531 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001532 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01001533 /* This assumes that the kernel never uses MSR_TSC_AUX */
1534 if (static_cpu_has(X86_FEATURE_RDTSCP))
1535 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001536
1537 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538}
1539
1540static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1541{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001542 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001543 int i;
1544
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001545 avic_vcpu_put(vcpu);
1546
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001547 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001548 kvm_load_ldt(svm->host.ldt);
1549#ifdef CONFIG_X86_64
1550 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07001551 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001552 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001553#else
Avi Kivity831ca602011-03-08 16:09:51 +02001554#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001555 loadsegment(gs, svm->host.gs);
1556#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001557#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001558 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001559 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560}
1561
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001562static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
1563{
1564 avic_set_running(vcpu, false);
1565}
1566
1567static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
1568{
1569 avic_set_running(vcpu, true);
1570}
1571
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1573{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001574 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575}
1576
1577static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1578{
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001579 /*
1580 * Any change of EFLAGS.VM is accompained by a reload of SS
1581 * (caused by either a task switch or an inter-privilege IRET),
1582 * so we do not need to update the CPL here.
1583 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001584 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585}
1586
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08001587static u32 svm_get_pkru(struct kvm_vcpu *vcpu)
1588{
1589 return 0;
1590}
1591
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001592static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1593{
1594 switch (reg) {
1595 case VCPU_EXREG_PDPTR:
1596 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001597 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001598 break;
1599 default:
1600 BUG();
1601 }
1602}
1603
Alexander Graff0b85052008-11-25 20:17:01 +01001604static void svm_set_vintr(struct vcpu_svm *svm)
1605{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001606 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001607}
1608
1609static void svm_clear_vintr(struct vcpu_svm *svm)
1610{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001611 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001612}
1613
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1615{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001616 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617
1618 switch (seg) {
1619 case VCPU_SREG_CS: return &save->cs;
1620 case VCPU_SREG_DS: return &save->ds;
1621 case VCPU_SREG_ES: return &save->es;
1622 case VCPU_SREG_FS: return &save->fs;
1623 case VCPU_SREG_GS: return &save->gs;
1624 case VCPU_SREG_SS: return &save->ss;
1625 case VCPU_SREG_TR: return &save->tr;
1626 case VCPU_SREG_LDTR: return &save->ldtr;
1627 }
1628 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001629 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630}
1631
1632static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1633{
1634 struct vmcb_seg *s = svm_seg(vcpu, seg);
1635
1636 return s->base;
1637}
1638
1639static void svm_get_segment(struct kvm_vcpu *vcpu,
1640 struct kvm_segment *var, int seg)
1641{
1642 struct vmcb_seg *s = svm_seg(vcpu, seg);
1643
1644 var->base = s->base;
1645 var->limit = s->limit;
1646 var->selector = s->selector;
1647 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1648 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1649 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1650 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1651 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1652 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1653 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301654
1655 /*
1656 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1657 * However, the SVM spec states that the G bit is not observed by the
1658 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1659 * So let's synthesize a legal G bit for all segments, this helps
1660 * running KVM nested. It also helps cross-vendor migration, because
1661 * Intel's vmentry has a check on the 'G' bit.
1662 */
1663 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001664
Joerg Roedele0231712010-02-24 18:59:10 +01001665 /*
1666 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001667 * for cross vendor migration purposes by "not present"
1668 */
1669 var->unusable = !var->present || (var->type == 0);
1670
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001671 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001672 case VCPU_SREG_TR:
1673 /*
1674 * Work around a bug where the busy flag in the tr selector
1675 * isn't exposed
1676 */
Amit Shahc0d09822008-10-27 09:04:18 +00001677 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001678 break;
1679 case VCPU_SREG_DS:
1680 case VCPU_SREG_ES:
1681 case VCPU_SREG_FS:
1682 case VCPU_SREG_GS:
1683 /*
1684 * The accessed bit must always be set in the segment
1685 * descriptor cache, although it can be cleared in the
1686 * descriptor, the cached bit always remains at 1. Since
1687 * Intel has a check on this, set it here to support
1688 * cross-vendor migration.
1689 */
1690 if (!var->unusable)
1691 var->type |= 0x1;
1692 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001693 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001694 /*
1695 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001696 * descriptor is left as 1, although the whole segment has
1697 * been made unusable. Clear it here to pass an Intel VMX
1698 * entry check when cross vendor migrating.
1699 */
1700 if (var->unusable)
1701 var->db = 0;
Jan Kiszka33b458d2014-06-29 17:12:43 +02001702 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02001703 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001704 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705}
1706
Izik Eidus2e4d2652008-03-24 19:38:34 +02001707static int svm_get_cpl(struct kvm_vcpu *vcpu)
1708{
1709 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1710
1711 return save->cpl;
1712}
1713
Gleb Natapov89a27f42010-02-16 10:51:48 +02001714static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001715{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001716 struct vcpu_svm *svm = to_svm(vcpu);
1717
Gleb Natapov89a27f42010-02-16 10:51:48 +02001718 dt->size = svm->vmcb->save.idtr.limit;
1719 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720}
1721
Gleb Natapov89a27f42010-02-16 10:51:48 +02001722static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001724 struct vcpu_svm *svm = to_svm(vcpu);
1725
Gleb Natapov89a27f42010-02-16 10:51:48 +02001726 svm->vmcb->save.idtr.limit = dt->size;
1727 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001728 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729}
1730
Gleb Natapov89a27f42010-02-16 10:51:48 +02001731static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001732{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001733 struct vcpu_svm *svm = to_svm(vcpu);
1734
Gleb Natapov89a27f42010-02-16 10:51:48 +02001735 dt->size = svm->vmcb->save.gdtr.limit;
1736 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737}
1738
Gleb Natapov89a27f42010-02-16 10:51:48 +02001739static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001741 struct vcpu_svm *svm = to_svm(vcpu);
1742
Gleb Natapov89a27f42010-02-16 10:51:48 +02001743 svm->vmcb->save.gdtr.limit = dt->size;
1744 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001745 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746}
1747
Avi Kivitye8467fd2009-12-29 18:43:06 +02001748static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1749{
1750}
1751
Avi Kivityaff48ba2010-12-05 18:56:11 +02001752static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1753{
1754}
1755
Anthony Liguori25c4c272007-04-27 09:29:21 +03001756static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001757{
1758}
1759
Avi Kivityd2251572010-01-06 10:55:27 +02001760static void update_cr0_intercept(struct vcpu_svm *svm)
1761{
1762 ulong gcr0 = svm->vcpu.arch.cr0;
1763 u64 *hcr0 = &svm->vmcb->save.cr0;
1764
1765 if (!svm->vcpu.fpu_active)
1766 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1767 else
1768 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1769 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1770
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001771 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001772
1773 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001774 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1775 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001776 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001777 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1778 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001779 }
1780}
1781
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1783{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001784 struct vcpu_svm *svm = to_svm(vcpu);
1785
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001786#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001787 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001788 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001789 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001790 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001791 }
1792
Mike Dayd77c26f2007-10-08 09:02:08 -04001793 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001794 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001795 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796 }
1797 }
1798#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001799 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001800
1801 if (!npt_enabled)
1802 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001803
1804 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001805 cr0 |= X86_CR0_TS;
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02001806 /*
1807 * re-enable caching here because the QEMU bios
1808 * does not do it - this results in some delay at
1809 * reboot
1810 */
1811 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
1812 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001813 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001814 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001815 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816}
1817
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001818static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001819{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001820 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001821 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1822
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001823 if (cr4 & X86_CR4_VMXE)
1824 return 1;
1825
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001826 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001827 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001828
Joerg Roedelec077262008-04-09 14:15:28 +02001829 vcpu->arch.cr4 = cr4;
1830 if (!npt_enabled)
1831 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001832 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001833 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001834 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001835 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836}
1837
1838static void svm_set_segment(struct kvm_vcpu *vcpu,
1839 struct kvm_segment *var, int seg)
1840{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001841 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001842 struct vmcb_seg *s = svm_seg(vcpu, seg);
1843
1844 s->base = var->base;
1845 s->limit = var->limit;
1846 s->selector = var->selector;
1847 if (var->unusable)
1848 s->attrib = 0;
1849 else {
1850 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1851 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1852 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1853 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1854 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1855 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1856 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1857 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1858 }
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001859
1860 /*
1861 * This is always accurate, except if SYSRET returned to a segment
1862 * with SS.DPL != 3. Intel does not have this quirk, and always
1863 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1864 * would entail passing the CPL to userspace and back.
1865 */
1866 if (seg == VCPU_SREG_SS)
1867 svm->vmcb->save.cpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868
Joerg Roedel060d0c92010-12-03 11:45:57 +01001869 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870}
1871
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001872static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001874 struct vcpu_svm *svm = to_svm(vcpu);
1875
Joerg Roedel18c918c2010-11-30 18:03:59 +01001876 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001877
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001878 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001879 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001880 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001881 } else
1882 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001883}
1884
Tejun Heo0fe1e002009-10-29 22:34:14 +09001885static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001886{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001887 if (sd->next_asid > sd->max_asid) {
1888 ++sd->asid_generation;
1889 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001890 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891 }
1892
Tejun Heo0fe1e002009-10-29 22:34:14 +09001893 svm->asid_generation = sd->asid_generation;
1894 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001895
1896 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001897}
1898
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001899static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
1900{
1901 return to_svm(vcpu)->vmcb->save.dr6;
1902}
1903
1904static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
1905{
1906 struct vcpu_svm *svm = to_svm(vcpu);
1907
1908 svm->vmcb->save.dr6 = value;
1909 mark_dirty(svm->vmcb, VMCB_DR);
1910}
1911
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001912static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1913{
1914 struct vcpu_svm *svm = to_svm(vcpu);
1915
1916 get_debugreg(vcpu->arch.db[0], 0);
1917 get_debugreg(vcpu->arch.db[1], 1);
1918 get_debugreg(vcpu->arch.db[2], 2);
1919 get_debugreg(vcpu->arch.db[3], 3);
1920 vcpu->arch.dr6 = svm_get_dr6(vcpu);
1921 vcpu->arch.dr7 = svm->vmcb->save.dr7;
1922
1923 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1924 set_dr_intercepts(svm);
1925}
1926
Gleb Natapov020df072010-04-13 10:05:23 +03001927static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001929 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001930
Gleb Natapov020df072010-04-13 10:05:23 +03001931 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01001932 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933}
1934
Avi Kivity851ba692009-08-24 11:10:17 +03001935static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936{
Gleb Natapov631bc482010-10-14 11:22:52 +02001937 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001939 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940
Gleb Natapov631bc482010-10-14 11:22:52 +02001941 switch (svm->apf_reason) {
1942 default:
1943 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001944
Gleb Natapov631bc482010-10-14 11:22:52 +02001945 trace_kvm_page_fault(fault_address, error_code);
1946 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1947 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01001948 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1949 svm->vmcb->control.insn_bytes,
1950 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02001951 break;
1952 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1953 svm->apf_reason = 0;
1954 local_irq_disable();
1955 kvm_async_pf_task_wait(fault_address);
1956 local_irq_enable();
1957 break;
1958 case KVM_PV_REASON_PAGE_READY:
1959 svm->apf_reason = 0;
1960 local_irq_disable();
1961 kvm_async_pf_task_wake(fault_address);
1962 local_irq_enable();
1963 break;
1964 }
1965 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966}
1967
Avi Kivity851ba692009-08-24 11:10:17 +03001968static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001969{
Avi Kivity851ba692009-08-24 11:10:17 +03001970 struct kvm_run *kvm_run = svm->vcpu.run;
1971
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001972 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001973 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001974 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001975 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1976 return 1;
1977 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001978
Jan Kiszka6be7d302009-10-18 13:24:54 +02001979 if (svm->nmi_singlestep) {
1980 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001981 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1982 svm->vmcb->save.rflags &=
1983 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Gleb Natapov44c11432009-05-11 13:35:52 +03001984 }
1985
1986 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001987 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001988 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1989 kvm_run->debug.arch.pc =
1990 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1991 kvm_run->debug.arch.exception = DB_VECTOR;
1992 return 0;
1993 }
1994
1995 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001996}
1997
Avi Kivity851ba692009-08-24 11:10:17 +03001998static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001999{
Avi Kivity851ba692009-08-24 11:10:17 +03002000 struct kvm_run *kvm_run = svm->vcpu.run;
2001
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002002 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2003 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2004 kvm_run->debug.arch.exception = BP_VECTOR;
2005 return 0;
2006}
2007
Avi Kivity851ba692009-08-24 11:10:17 +03002008static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002009{
2010 int er;
2011
Andre Przywara51d8b662010-12-21 11:12:02 +01002012 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002013 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002014 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002015 return 1;
2016}
2017
Eric Northup54a20552015-11-03 18:03:53 +01002018static int ac_interception(struct vcpu_svm *svm)
2019{
2020 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2021 return 1;
2022}
2023
Avi Kivity6b52d182010-01-21 15:31:47 +02002024static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05002025{
Avi Kivity6b52d182010-01-21 15:31:47 +02002026 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01002027
Joerg Roedel18c918c2010-11-30 18:03:59 +01002028 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01002029
Rusty Russelle756fc62007-07-30 20:07:08 +10002030 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02002031 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02002032}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002033
Avi Kivity6b52d182010-01-21 15:31:47 +02002034static int nm_interception(struct vcpu_svm *svm)
2035{
2036 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002037 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05002038}
2039
Joerg Roedel67ec6602010-05-17 14:43:35 +02002040static bool is_erratum_383(void)
2041{
2042 int err, i;
2043 u64 value;
2044
2045 if (!erratum_383_found)
2046 return false;
2047
2048 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2049 if (err)
2050 return false;
2051
2052 /* Bit 62 may or may not be set for this mce */
2053 value &= ~(1ULL << 62);
2054
2055 if (value != 0xb600000000010015ULL)
2056 return false;
2057
2058 /* Clear MCi_STATUS registers */
2059 for (i = 0; i < 6; ++i)
2060 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2061
2062 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2063 if (!err) {
2064 u32 low, high;
2065
2066 value &= ~(1ULL << 2);
2067 low = lower_32_bits(value);
2068 high = upper_32_bits(value);
2069
2070 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2071 }
2072
2073 /* Flush tlb to evict multi-match entries */
2074 __flush_tlb_all();
2075
2076 return true;
2077}
2078
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002079static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002080{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002081 if (is_erratum_383()) {
2082 /*
2083 * Erratum 383 triggered. Guest state is corrupt so kill the
2084 * guest.
2085 */
2086 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2087
Avi Kivitya8eeb042010-05-10 12:34:53 +03002088 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002089
2090 return;
2091 }
2092
Joerg Roedel53371b52008-04-09 14:15:30 +02002093 /*
2094 * On an #MC intercept the MCE handler is not called automatically in
2095 * the host. So do it by hand here.
2096 */
2097 asm volatile (
2098 "int $0x12\n");
2099 /* not sure if we ever come back to this point */
2100
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002101 return;
2102}
2103
2104static int mc_interception(struct vcpu_svm *svm)
2105{
Joerg Roedel53371b52008-04-09 14:15:30 +02002106 return 1;
2107}
2108
Avi Kivity851ba692009-08-24 11:10:17 +03002109static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002110{
Avi Kivity851ba692009-08-24 11:10:17 +03002111 struct kvm_run *kvm_run = svm->vcpu.run;
2112
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002113 /*
2114 * VMCB is undefined after a SHUTDOWN intercept
2115 * so reinitialize it.
2116 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002117 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002118 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002119
2120 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2121 return 0;
2122}
2123
Avi Kivity851ba692009-08-24 11:10:17 +03002124static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002126 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002127 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01002128 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002129 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002130
Rusty Russelle756fc62007-07-30 20:07:08 +10002131 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002132 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002133 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002134 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01002135 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002136
Avi Kivity039576c2007-03-20 12:46:50 +02002137 port = io_info >> 16;
2138 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002139 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002140 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002141
2142 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143}
2144
Avi Kivity851ba692009-08-24 11:10:17 +03002145static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002146{
2147 return 1;
2148}
2149
Avi Kivity851ba692009-08-24 11:10:17 +03002150static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002151{
2152 ++svm->vcpu.stat.irq_exits;
2153 return 1;
2154}
2155
Avi Kivity851ba692009-08-24 11:10:17 +03002156static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157{
2158 return 1;
2159}
2160
Avi Kivity851ba692009-08-24 11:10:17 +03002161static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002163 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10002164 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165}
2166
Avi Kivity851ba692009-08-24 11:10:17 +03002167static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002168{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002169 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002170 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002171}
2172
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002173static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
2174{
2175 struct vcpu_svm *svm = to_svm(vcpu);
2176
2177 return svm->nested.nested_cr3;
2178}
2179
Avi Kivitye4e517b2011-07-28 11:36:17 +03002180static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
2181{
2182 struct vcpu_svm *svm = to_svm(vcpu);
2183 u64 cr3 = svm->nested.nested_cr3;
2184 u64 pdpte;
2185 int ret;
2186
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002187 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
2188 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03002189 if (ret)
2190 return 0;
2191 return pdpte;
2192}
2193
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002194static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
2195 unsigned long root)
2196{
2197 struct vcpu_svm *svm = to_svm(vcpu);
2198
2199 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01002200 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002201 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002202}
2203
Avi Kivity6389ee92010-11-29 16:12:30 +02002204static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
2205 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002206{
2207 struct vcpu_svm *svm = to_svm(vcpu);
2208
Paolo Bonzini5e352512014-09-02 13:18:37 +02002209 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
2210 /*
2211 * TODO: track the cause of the nested page fault, and
2212 * correctly fill in the high bits of exit_info_1.
2213 */
2214 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
2215 svm->vmcb->control.exit_code_hi = 0;
2216 svm->vmcb->control.exit_info_1 = (1ULL << 32);
2217 svm->vmcb->control.exit_info_2 = fault->address;
2218 }
2219
2220 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2221 svm->vmcb->control.exit_info_1 |= fault->error_code;
2222
2223 /*
2224 * The present bit is always zero for page structure faults on real
2225 * hardware.
2226 */
2227 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2228 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002229
2230 nested_svm_vmexit(svm);
2231}
2232
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002233static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002234{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002235 WARN_ON(mmu_is_nested(vcpu));
2236 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002237 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2238 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002239 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002240 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
2241 vcpu->arch.mmu.shadow_root_level = get_npt_level();
Xiao Guangrongc258b622015-08-05 12:04:24 +08002242 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002243 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002244}
2245
2246static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2247{
2248 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2249}
2250
Alexander Grafc0725422008-11-25 20:17:03 +01002251static int nested_svm_check_permissions(struct vcpu_svm *svm)
2252{
Avi Kivityf6801df2010-01-21 15:31:50 +02002253 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01002254 || !is_paging(&svm->vcpu)) {
2255 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2256 return 1;
2257 }
2258
2259 if (svm->vmcb->save.cpl) {
2260 kvm_inject_gp(&svm->vcpu, 0);
2261 return 1;
2262 }
2263
2264 return 0;
2265}
2266
Alexander Grafcf74a782008-11-25 20:17:08 +01002267static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2268 bool has_error_code, u32 error_code)
2269{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002270 int vmexit;
2271
Joerg Roedel20307532010-11-29 17:51:48 +01002272 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002273 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002274
Joerg Roedel0295ad72009-08-07 11:49:37 +02002275 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2276 svm->vmcb->control.exit_code_hi = 0;
2277 svm->vmcb->control.exit_info_1 = error_code;
2278 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
2279
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002280 vmexit = nested_svm_intercept(svm);
2281 if (vmexit == NESTED_EXIT_DONE)
2282 svm->nested.exit_required = true;
2283
2284 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002285}
2286
Joerg Roedel8fe54652010-02-19 16:23:01 +01002287/* This function returns true if it is save to enable the irq window */
2288static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002289{
Joerg Roedel20307532010-11-29 17:51:48 +01002290 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002291 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002292
Joerg Roedel26666952009-08-07 11:49:46 +02002293 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002294 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002295
Joerg Roedel26666952009-08-07 11:49:46 +02002296 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002297 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002298
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002299 /*
2300 * if vmexit was already requested (by intercepted exception
2301 * for instance) do not overwrite it with "external interrupt"
2302 * vmexit.
2303 */
2304 if (svm->nested.exit_required)
2305 return false;
2306
Joerg Roedel197717d2010-02-24 18:59:19 +01002307 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2308 svm->vmcb->control.exit_info_1 = 0;
2309 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002310
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002311 if (svm->nested.intercept & 1ULL) {
2312 /*
2313 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002314 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002315 * #vmexit emulation might sleep. Only signal request for
2316 * the #vmexit here.
2317 */
2318 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002319 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002320 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002321 }
2322
Joerg Roedel8fe54652010-02-19 16:23:01 +01002323 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002324}
2325
Joerg Roedel887f5002010-02-24 18:59:12 +01002326/* This function returns true if it is save to enable the nmi window */
2327static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2328{
Joerg Roedel20307532010-11-29 17:51:48 +01002329 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002330 return true;
2331
2332 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2333 return true;
2334
2335 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2336 svm->nested.exit_required = true;
2337
2338 return false;
2339}
2340
Joerg Roedel7597f122010-02-19 16:23:00 +01002341static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002342{
2343 struct page *page;
2344
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002345 might_sleep();
2346
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002347 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002348 if (is_error_page(page))
2349 goto error;
2350
Joerg Roedel7597f122010-02-19 16:23:00 +01002351 *_page = page;
2352
2353 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002354
2355error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002356 kvm_inject_gp(&svm->vcpu, 0);
2357
2358 return NULL;
2359}
2360
Joerg Roedel7597f122010-02-19 16:23:00 +01002361static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002362{
Joerg Roedel7597f122010-02-19 16:23:00 +01002363 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002364 kvm_release_page_dirty(page);
2365}
2366
Joerg Roedelce2ac082010-03-01 15:34:39 +01002367static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002368{
Jan Kiszka9bf41832014-06-30 10:54:17 +02002369 unsigned port, size, iopm_len;
2370 u16 val, mask;
2371 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002372 u64 gpa;
2373
2374 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2375 return NESTED_EXIT_HOST;
2376
2377 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02002378 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2379 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002380 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02002381 start_bit = port % 8;
2382 iopm_len = (start_bit + size > 8) ? 2 : 1;
2383 mask = (0xf >> (4 - size)) << start_bit;
2384 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002385
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002386 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02002387 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002388
Jan Kiszka9bf41832014-06-30 10:54:17 +02002389 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002390}
2391
Joerg Roedeld2477822010-03-01 15:34:34 +01002392static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002393{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002394 u32 offset, msr, value;
2395 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002396
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002397 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002398 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002399
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002400 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2401 offset = svm_msrpm_offset(msr);
2402 write = svm->vmcb->control.exit_info_1 & 1;
2403 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002404
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002405 if (offset == MSR_INVALID)
2406 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002407
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002408 /* Offset is in 32 bit units but need in 8 bit units */
2409 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002410
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002411 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002412 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002413
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002414 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002415}
2416
Joerg Roedel410e4d52009-08-07 11:49:44 +02002417static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002418{
Alexander Grafcf74a782008-11-25 20:17:08 +01002419 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002420
Joerg Roedel410e4d52009-08-07 11:49:44 +02002421 switch (exit_code) {
2422 case SVM_EXIT_INTR:
2423 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002424 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002425 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002426 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002427 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002428 if (npt_enabled)
2429 return NESTED_EXIT_HOST;
2430 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002431 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002432 /* When we're shadowing, trap PFs, but not async PF */
2433 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002434 return NESTED_EXIT_HOST;
2435 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01002436 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
2437 nm_interception(svm);
2438 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002439 default:
2440 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002441 }
2442
Joerg Roedel410e4d52009-08-07 11:49:44 +02002443 return NESTED_EXIT_CONTINUE;
2444}
2445
2446/*
2447 * If this function returns true, this #vmexit was already handled
2448 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002449static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002450{
2451 u32 exit_code = svm->vmcb->control.exit_code;
2452 int vmexit = NESTED_EXIT_HOST;
2453
Alexander Grafcf74a782008-11-25 20:17:08 +01002454 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002455 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002456 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002457 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002458 case SVM_EXIT_IOIO:
2459 vmexit = nested_svm_intercept_ioio(svm);
2460 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002461 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2462 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2463 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002464 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002465 break;
2466 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002467 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2468 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2469 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002470 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002471 break;
2472 }
2473 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2474 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002475 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002476 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002477 /* async page fault always cause vmexit */
2478 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2479 svm->apf_reason != 0)
2480 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002481 break;
2482 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002483 case SVM_EXIT_ERR: {
2484 vmexit = NESTED_EXIT_DONE;
2485 break;
2486 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002487 default: {
2488 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002489 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002490 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002491 }
2492 }
2493
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002494 return vmexit;
2495}
2496
2497static int nested_svm_exit_handled(struct vcpu_svm *svm)
2498{
2499 int vmexit;
2500
2501 vmexit = nested_svm_intercept(svm);
2502
2503 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002504 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002505
2506 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002507}
2508
Joerg Roedel0460a972009-08-07 11:49:31 +02002509static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2510{
2511 struct vmcb_control_area *dst = &dst_vmcb->control;
2512 struct vmcb_control_area *from = &from_vmcb->control;
2513
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002514 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002515 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002516 dst->intercept_exceptions = from->intercept_exceptions;
2517 dst->intercept = from->intercept;
2518 dst->iopm_base_pa = from->iopm_base_pa;
2519 dst->msrpm_base_pa = from->msrpm_base_pa;
2520 dst->tsc_offset = from->tsc_offset;
2521 dst->asid = from->asid;
2522 dst->tlb_ctl = from->tlb_ctl;
2523 dst->int_ctl = from->int_ctl;
2524 dst->int_vector = from->int_vector;
2525 dst->int_state = from->int_state;
2526 dst->exit_code = from->exit_code;
2527 dst->exit_code_hi = from->exit_code_hi;
2528 dst->exit_info_1 = from->exit_info_1;
2529 dst->exit_info_2 = from->exit_info_2;
2530 dst->exit_int_info = from->exit_int_info;
2531 dst->exit_int_info_err = from->exit_int_info_err;
2532 dst->nested_ctl = from->nested_ctl;
2533 dst->event_inj = from->event_inj;
2534 dst->event_inj_err = from->event_inj_err;
2535 dst->nested_cr3 = from->nested_cr3;
2536 dst->lbr_ctl = from->lbr_ctl;
2537}
2538
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002539static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002540{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002541 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002542 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002543 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002544 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002545
Joerg Roedel17897f32009-10-09 16:08:29 +02002546 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2547 vmcb->control.exit_info_1,
2548 vmcb->control.exit_info_2,
2549 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002550 vmcb->control.exit_int_info_err,
2551 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002552
Joerg Roedel7597f122010-02-19 16:23:00 +01002553 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002554 if (!nested_vmcb)
2555 return 1;
2556
Joerg Roedel20307532010-11-29 17:51:48 +01002557 /* Exit Guest-Mode */
2558 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002559 svm->nested.vmcb = 0;
2560
Alexander Grafcf74a782008-11-25 20:17:08 +01002561 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002562 disable_gif(svm);
2563
2564 nested_vmcb->save.es = vmcb->save.es;
2565 nested_vmcb->save.cs = vmcb->save.cs;
2566 nested_vmcb->save.ss = vmcb->save.ss;
2567 nested_vmcb->save.ds = vmcb->save.ds;
2568 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2569 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002570 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002571 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002572 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002573 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002574 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002575 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002576 nested_vmcb->save.rip = vmcb->save.rip;
2577 nested_vmcb->save.rsp = vmcb->save.rsp;
2578 nested_vmcb->save.rax = vmcb->save.rax;
2579 nested_vmcb->save.dr7 = vmcb->save.dr7;
2580 nested_vmcb->save.dr6 = vmcb->save.dr6;
2581 nested_vmcb->save.cpl = vmcb->save.cpl;
2582
2583 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2584 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2585 nested_vmcb->control.int_state = vmcb->control.int_state;
2586 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2587 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2588 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2589 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2590 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2591 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02002592
2593 if (svm->nrips_enabled)
2594 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002595
2596 /*
2597 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2598 * to make sure that we do not lose injected events. So check event_inj
2599 * here and copy it to exit_int_info if it is valid.
2600 * Exit_int_info and event_inj can't be both valid because the case
2601 * below only happens on a VMRUN instruction intercept which has
2602 * no valid exit_int_info set.
2603 */
2604 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2605 struct vmcb_control_area *nc = &nested_vmcb->control;
2606
2607 nc->exit_int_info = vmcb->control.event_inj;
2608 nc->exit_int_info_err = vmcb->control.event_inj_err;
2609 }
2610
Joerg Roedel33740e42009-08-07 11:49:29 +02002611 nested_vmcb->control.tlb_ctl = 0;
2612 nested_vmcb->control.event_inj = 0;
2613 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002614
2615 /* We always set V_INTR_MASKING and remember the old value in hflags */
2616 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2617 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2618
Alexander Grafcf74a782008-11-25 20:17:08 +01002619 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002620 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002621
Alexander Graf219b65d2009-06-15 15:21:25 +02002622 kvm_clear_exception_queue(&svm->vcpu);
2623 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002624
Joerg Roedel4b161842010-09-10 17:31:03 +02002625 svm->nested.nested_cr3 = 0;
2626
Alexander Grafcf74a782008-11-25 20:17:08 +01002627 /* Restore selected save entries */
2628 svm->vmcb->save.es = hsave->save.es;
2629 svm->vmcb->save.cs = hsave->save.cs;
2630 svm->vmcb->save.ss = hsave->save.ss;
2631 svm->vmcb->save.ds = hsave->save.ds;
2632 svm->vmcb->save.gdtr = hsave->save.gdtr;
2633 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002634 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002635 svm_set_efer(&svm->vcpu, hsave->save.efer);
2636 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2637 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2638 if (npt_enabled) {
2639 svm->vmcb->save.cr3 = hsave->save.cr3;
2640 svm->vcpu.arch.cr3 = hsave->save.cr3;
2641 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002642 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002643 }
2644 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2645 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2646 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2647 svm->vmcb->save.dr7 = 0;
2648 svm->vmcb->save.cpl = 0;
2649 svm->vmcb->control.exit_int_info = 0;
2650
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002651 mark_all_dirty(svm->vmcb);
2652
Joerg Roedel7597f122010-02-19 16:23:00 +01002653 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002654
Joerg Roedel4b161842010-09-10 17:31:03 +02002655 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002656 kvm_mmu_reset_context(&svm->vcpu);
2657 kvm_mmu_load(&svm->vcpu);
2658
2659 return 0;
2660}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002661
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002662static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002663{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002664 /*
2665 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002666 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01002667 * the kvm msr permission bitmap may contain zero bits
2668 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002669 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002670
Joerg Roedel323c3d82010-03-01 15:34:37 +01002671 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2672 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002673
Joerg Roedel323c3d82010-03-01 15:34:37 +01002674 for (i = 0; i < MSRPM_OFFSETS; i++) {
2675 u32 value, p;
2676 u64 offset;
2677
2678 if (msrpm_offsets[i] == 0xffffffff)
2679 break;
2680
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002681 p = msrpm_offsets[i];
2682 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002683
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002684 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01002685 return false;
2686
2687 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2688 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002689
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002690 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002691
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002692 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002693}
2694
Joerg Roedel52c65a302010-08-02 16:46:44 +02002695static bool nested_vmcb_checks(struct vmcb *vmcb)
2696{
2697 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2698 return false;
2699
Joerg Roedeldbe77582010-08-02 16:46:45 +02002700 if (vmcb->control.asid == 0)
2701 return false;
2702
Joerg Roedel4b161842010-09-10 17:31:03 +02002703 if (vmcb->control.nested_ctl && !npt_enabled)
2704 return false;
2705
Joerg Roedel52c65a302010-08-02 16:46:44 +02002706 return true;
2707}
2708
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002709static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002710{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002711 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002712 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002713 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002714 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002715 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002716
Joerg Roedel06fc77722010-02-19 16:23:07 +01002717 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002718
Joerg Roedel7597f122010-02-19 16:23:00 +01002719 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002720 if (!nested_vmcb)
2721 return false;
2722
Joerg Roedel52c65a302010-08-02 16:46:44 +02002723 if (!nested_vmcb_checks(nested_vmcb)) {
2724 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2725 nested_vmcb->control.exit_code_hi = 0;
2726 nested_vmcb->control.exit_info_1 = 0;
2727 nested_vmcb->control.exit_info_2 = 0;
2728
2729 nested_svm_unmap(page);
2730
2731 return false;
2732 }
2733
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002734 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002735 nested_vmcb->save.rip,
2736 nested_vmcb->control.int_ctl,
2737 nested_vmcb->control.event_inj,
2738 nested_vmcb->control.nested_ctl);
2739
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002740 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2741 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002742 nested_vmcb->control.intercept_exceptions,
2743 nested_vmcb->control.intercept);
2744
Alexander Graf3d6368e2008-11-25 20:17:07 +01002745 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002746 kvm_clear_exception_queue(&svm->vcpu);
2747 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002748
Joerg Roedele0231712010-02-24 18:59:10 +01002749 /*
2750 * Save the old vmcb, so we don't need to pick what we save, but can
2751 * restore everything when a VMEXIT occurs
2752 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002753 hsave->save.es = vmcb->save.es;
2754 hsave->save.cs = vmcb->save.cs;
2755 hsave->save.ss = vmcb->save.ss;
2756 hsave->save.ds = vmcb->save.ds;
2757 hsave->save.gdtr = vmcb->save.gdtr;
2758 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002759 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002760 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002761 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002762 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002763 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002764 hsave->save.rsp = vmcb->save.rsp;
2765 hsave->save.rax = vmcb->save.rax;
2766 if (npt_enabled)
2767 hsave->save.cr3 = vmcb->save.cr3;
2768 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002769 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002770
Joerg Roedel0460a972009-08-07 11:49:31 +02002771 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002772
Avi Kivityf6e78472010-08-02 15:30:20 +03002773 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002774 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2775 else
2776 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2777
Joerg Roedel4b161842010-09-10 17:31:03 +02002778 if (nested_vmcb->control.nested_ctl) {
2779 kvm_mmu_unload(&svm->vcpu);
2780 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2781 nested_svm_init_mmu_context(&svm->vcpu);
2782 }
2783
Alexander Graf3d6368e2008-11-25 20:17:07 +01002784 /* Load the nested guest state */
2785 svm->vmcb->save.es = nested_vmcb->save.es;
2786 svm->vmcb->save.cs = nested_vmcb->save.cs;
2787 svm->vmcb->save.ss = nested_vmcb->save.ss;
2788 svm->vmcb->save.ds = nested_vmcb->save.ds;
2789 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2790 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002791 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002792 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2793 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2794 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2795 if (npt_enabled) {
2796 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2797 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002798 } else
Avi Kivity23902182010-06-10 17:02:16 +03002799 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002800
2801 /* Guest paging mode is active - reset mmu */
2802 kvm_mmu_reset_context(&svm->vcpu);
2803
Joerg Roedeldefbba52009-08-07 11:49:30 +02002804 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002805 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2806 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2807 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002808
Alexander Graf3d6368e2008-11-25 20:17:07 +01002809 /* In case we don't even reach vcpu_run, the fields are not updated */
2810 svm->vmcb->save.rax = nested_vmcb->save.rax;
2811 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2812 svm->vmcb->save.rip = nested_vmcb->save.rip;
2813 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2814 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2815 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2816
Joerg Roedelf7138532010-03-01 15:34:40 +01002817 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002818 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002819
Joerg Roedelaad42c62009-08-07 11:49:34 +02002820 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002821 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002822 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002823 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2824 svm->nested.intercept = nested_vmcb->control.intercept;
2825
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002826 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002827 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002828 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2829 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2830 else
2831 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2832
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002833 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2834 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002835 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2836 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002837 }
2838
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002839 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002840 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002841
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002842 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002843 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2844 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2845 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002846 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2847 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2848
Joerg Roedel7597f122010-02-19 16:23:00 +01002849 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002850
Joerg Roedel20307532010-11-29 17:51:48 +01002851 /* Enter Guest-Mode */
2852 enter_guest_mode(&svm->vcpu);
2853
Joerg Roedel384c6362010-11-30 18:03:56 +01002854 /*
2855 * Merge guest and host intercepts - must be called with vcpu in
2856 * guest-mode to take affect here
2857 */
2858 recalc_intercepts(svm);
2859
Joerg Roedel06fc77722010-02-19 16:23:07 +01002860 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002861
Joerg Roedel2af91942009-08-07 11:49:28 +02002862 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002863
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002864 mark_all_dirty(svm->vmcb);
2865
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002866 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002867}
2868
Joerg Roedel9966bf62009-08-07 11:49:40 +02002869static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002870{
2871 to_vmcb->save.fs = from_vmcb->save.fs;
2872 to_vmcb->save.gs = from_vmcb->save.gs;
2873 to_vmcb->save.tr = from_vmcb->save.tr;
2874 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2875 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2876 to_vmcb->save.star = from_vmcb->save.star;
2877 to_vmcb->save.lstar = from_vmcb->save.lstar;
2878 to_vmcb->save.cstar = from_vmcb->save.cstar;
2879 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2880 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2881 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2882 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002883}
2884
Avi Kivity851ba692009-08-24 11:10:17 +03002885static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002886{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002887 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002888 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002889
Alexander Graf55426752008-11-25 20:17:06 +01002890 if (nested_svm_check_permissions(svm))
2891 return 1;
2892
Joerg Roedel7597f122010-02-19 16:23:00 +01002893 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002894 if (!nested_vmcb)
2895 return 1;
2896
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002897 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2898 skip_emulated_instruction(&svm->vcpu);
2899
Joerg Roedel9966bf62009-08-07 11:49:40 +02002900 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002901 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002902
2903 return 1;
2904}
2905
Avi Kivity851ba692009-08-24 11:10:17 +03002906static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002907{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002908 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002909 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002910
Alexander Graf55426752008-11-25 20:17:06 +01002911 if (nested_svm_check_permissions(svm))
2912 return 1;
2913
Joerg Roedel7597f122010-02-19 16:23:00 +01002914 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002915 if (!nested_vmcb)
2916 return 1;
2917
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002918 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2919 skip_emulated_instruction(&svm->vcpu);
2920
Joerg Roedel9966bf62009-08-07 11:49:40 +02002921 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002922 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002923
2924 return 1;
2925}
2926
Avi Kivity851ba692009-08-24 11:10:17 +03002927static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002928{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002929 if (nested_svm_check_permissions(svm))
2930 return 1;
2931
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002932 /* Save rip after vmrun instruction */
2933 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002934
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002935 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002936 return 1;
2937
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002938 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002939 goto failed;
2940
2941 return 1;
2942
2943failed:
2944
2945 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2946 svm->vmcb->control.exit_code_hi = 0;
2947 svm->vmcb->control.exit_info_1 = 0;
2948 svm->vmcb->control.exit_info_2 = 0;
2949
2950 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002951
2952 return 1;
2953}
2954
Avi Kivity851ba692009-08-24 11:10:17 +03002955static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002956{
2957 if (nested_svm_check_permissions(svm))
2958 return 1;
2959
2960 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2961 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002962 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002963
Joerg Roedel2af91942009-08-07 11:49:28 +02002964 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002965
2966 return 1;
2967}
2968
Avi Kivity851ba692009-08-24 11:10:17 +03002969static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002970{
2971 if (nested_svm_check_permissions(svm))
2972 return 1;
2973
2974 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2975 skip_emulated_instruction(&svm->vcpu);
2976
Joerg Roedel2af91942009-08-07 11:49:28 +02002977 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002978
2979 /* After a CLGI no interrupts should come */
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05002980 if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
2981 svm_clear_vintr(svm);
2982 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2983 mark_dirty(svm->vmcb, VMCB_INTR);
2984 }
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002985
Alexander Graf1371d902008-11-25 20:17:04 +01002986 return 1;
2987}
2988
Avi Kivity851ba692009-08-24 11:10:17 +03002989static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002990{
2991 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002992
David Kaplan668f1982015-02-20 16:02:10 -06002993 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
2994 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02002995
Alexander Grafff092382009-06-15 15:21:24 +02002996 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06002997 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02002998
2999 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
3000 skip_emulated_instruction(&svm->vcpu);
3001 return 1;
3002}
3003
Joerg Roedel532a46b2009-10-09 16:08:32 +02003004static int skinit_interception(struct vcpu_svm *svm)
3005{
David Kaplan668f1982015-02-20 16:02:10 -06003006 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02003007
3008 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3009 return 1;
3010}
3011
David Kaplandab429a2015-03-02 13:43:37 -06003012static int wbinvd_interception(struct vcpu_svm *svm)
3013{
3014 kvm_emulate_wbinvd(&svm->vcpu);
3015 return 1;
3016}
3017
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003018static int xsetbv_interception(struct vcpu_svm *svm)
3019{
3020 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
3021 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3022
3023 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
3024 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
3025 skip_emulated_instruction(&svm->vcpu);
3026 }
3027
3028 return 1;
3029}
3030
Avi Kivity851ba692009-08-24 11:10:17 +03003031static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003032{
Izik Eidus37817f22008-03-24 23:14:53 +02003033 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003034 int reason;
3035 int int_type = svm->vmcb->control.exit_int_info &
3036 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03003037 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003038 uint32_t type =
3039 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
3040 uint32_t idt_v =
3041 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003042 bool has_error_code = false;
3043 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003044
3045 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003046
Izik Eidus37817f22008-03-24 23:14:53 +02003047 if (svm->vmcb->control.exit_info_2 &
3048 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003049 reason = TASK_SWITCH_IRET;
3050 else if (svm->vmcb->control.exit_info_2 &
3051 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
3052 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003053 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003054 reason = TASK_SWITCH_GATE;
3055 else
3056 reason = TASK_SWITCH_CALL;
3057
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003058 if (reason == TASK_SWITCH_GATE) {
3059 switch (type) {
3060 case SVM_EXITINTINFO_TYPE_NMI:
3061 svm->vcpu.arch.nmi_injected = false;
3062 break;
3063 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003064 if (svm->vmcb->control.exit_info_2 &
3065 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
3066 has_error_code = true;
3067 error_code =
3068 (u32)svm->vmcb->control.exit_info_2;
3069 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03003070 kvm_clear_exception_queue(&svm->vcpu);
3071 break;
3072 case SVM_EXITINTINFO_TYPE_INTR:
3073 kvm_clear_interrupt_queue(&svm->vcpu);
3074 break;
3075 default:
3076 break;
3077 }
3078 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003079
Gleb Natapov8317c292009-04-12 13:37:02 +03003080 if (reason != TASK_SWITCH_GATE ||
3081 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
3082 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03003083 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
3084 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003085
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01003086 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
3087 int_vec = -1;
3088
3089 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03003090 has_error_code, error_code) == EMULATE_FAIL) {
3091 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3092 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3093 svm->vcpu.run->internal.ndata = 0;
3094 return 0;
3095 }
3096 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097}
3098
Avi Kivity851ba692009-08-24 11:10:17 +03003099static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003101 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003102 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02003103 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104}
3105
Avi Kivity851ba692009-08-24 11:10:17 +03003106static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003107{
3108 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003109 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03003110 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003111 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01003112 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003113 return 1;
3114}
3115
Avi Kivity851ba692009-08-24 11:10:17 +03003116static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003117{
Andre Przywaradf4f31082010-12-21 11:12:06 +01003118 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3119 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3120
3121 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
3122 skip_emulated_instruction(&svm->vcpu);
3123 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03003124}
3125
Avi Kivity851ba692009-08-24 11:10:17 +03003126static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127{
Andre Przywara51d8b662010-12-21 11:12:02 +01003128 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129}
3130
Avi Kivity332b56e2011-11-10 14:57:24 +02003131static int rdpmc_interception(struct vcpu_svm *svm)
3132{
3133 int err;
3134
3135 if (!static_cpu_has(X86_FEATURE_NRIPS))
3136 return emulate_on_interception(svm);
3137
3138 err = kvm_rdpmc(&svm->vcpu);
3139 kvm_complete_insn_gp(&svm->vcpu, err);
3140
3141 return 1;
3142}
3143
Xiubo Li52eb5a62015-03-13 17:39:45 +08003144static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
3145 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02003146{
3147 unsigned long cr0 = svm->vcpu.arch.cr0;
3148 bool ret = false;
3149 u64 intercept;
3150
3151 intercept = svm->nested.intercept;
3152
3153 if (!is_guest_mode(&svm->vcpu) ||
3154 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
3155 return false;
3156
3157 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
3158 val &= ~SVM_CR0_SELECTIVE_MASK;
3159
3160 if (cr0 ^ val) {
3161 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3162 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
3163 }
3164
3165 return ret;
3166}
3167
Andre Przywara7ff76d52010-12-21 11:12:04 +01003168#define CR_VALID (1ULL << 63)
3169
3170static int cr_interception(struct vcpu_svm *svm)
3171{
3172 int reg, cr;
3173 unsigned long val;
3174 int err;
3175
3176 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3177 return emulate_on_interception(svm);
3178
3179 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
3180 return emulate_on_interception(svm);
3181
3182 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06003183 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
3184 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
3185 else
3186 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01003187
3188 err = 0;
3189 if (cr >= 16) { /* mov to cr */
3190 cr -= 16;
3191 val = kvm_register_read(&svm->vcpu, reg);
3192 switch (cr) {
3193 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02003194 if (!check_selective_cr0_intercepted(svm, val))
3195 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02003196 else
3197 return 1;
3198
Andre Przywara7ff76d52010-12-21 11:12:04 +01003199 break;
3200 case 3:
3201 err = kvm_set_cr3(&svm->vcpu, val);
3202 break;
3203 case 4:
3204 err = kvm_set_cr4(&svm->vcpu, val);
3205 break;
3206 case 8:
3207 err = kvm_set_cr8(&svm->vcpu, val);
3208 break;
3209 default:
3210 WARN(1, "unhandled write to CR%d", cr);
3211 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3212 return 1;
3213 }
3214 } else { /* mov from cr */
3215 switch (cr) {
3216 case 0:
3217 val = kvm_read_cr0(&svm->vcpu);
3218 break;
3219 case 2:
3220 val = svm->vcpu.arch.cr2;
3221 break;
3222 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003223 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003224 break;
3225 case 4:
3226 val = kvm_read_cr4(&svm->vcpu);
3227 break;
3228 case 8:
3229 val = kvm_get_cr8(&svm->vcpu);
3230 break;
3231 default:
3232 WARN(1, "unhandled read from CR%d", cr);
3233 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3234 return 1;
3235 }
3236 kvm_register_write(&svm->vcpu, reg, val);
3237 }
3238 kvm_complete_insn_gp(&svm->vcpu, err);
3239
3240 return 1;
3241}
3242
Andre Przywaracae37972010-12-21 11:12:05 +01003243static int dr_interception(struct vcpu_svm *svm)
3244{
3245 int reg, dr;
3246 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003247
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003248 if (svm->vcpu.guest_debug == 0) {
3249 /*
3250 * No more DR vmexits; force a reload of the debug registers
3251 * and reenter on this instruction. The next vmexit will
3252 * retrieve the full state of the debug registers.
3253 */
3254 clr_dr_intercepts(svm);
3255 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3256 return 1;
3257 }
3258
Andre Przywaracae37972010-12-21 11:12:05 +01003259 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3260 return emulate_on_interception(svm);
3261
3262 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3263 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3264
3265 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003266 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3267 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003268 val = kvm_register_read(&svm->vcpu, reg);
3269 kvm_set_dr(&svm->vcpu, dr - 16, val);
3270 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003271 if (!kvm_require_dr(&svm->vcpu, dr))
3272 return 1;
3273 kvm_get_dr(&svm->vcpu, dr, &val);
3274 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003275 }
3276
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01003277 skip_emulated_instruction(&svm->vcpu);
3278
Andre Przywaracae37972010-12-21 11:12:05 +01003279 return 1;
3280}
3281
Avi Kivity851ba692009-08-24 11:10:17 +03003282static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003283{
Avi Kivity851ba692009-08-24 11:10:17 +03003284 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003285 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003286
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003287 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3288 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003289 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003290 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003291 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003292 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003293 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003294 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3295 return 0;
3296}
3297
Paolo Bonzini48d89b92014-08-26 13:27:46 +02003298static u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003299{
3300 struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
Haozhong Zhang4ba76532015-10-20 15:39:07 +08003301 return vmcb->control.tsc_offset + host_tsc;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003302}
3303
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003304static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003306 struct vcpu_svm *svm = to_svm(vcpu);
3307
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003308 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303309 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003310 msr_info->data = svm->vmcb->control.tsc_offset +
Haozhong Zhang35181e82015-10-20 15:39:03 +08003311 kvm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003312
Avi Kivity6aa8b732006-12-10 02:21:36 -08003313 break;
3314 }
Brian Gerst8c065852010-07-17 09:03:26 -04003315 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003316 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003317 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003318#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003319 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003320 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321 break;
3322 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003323 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324 break;
3325 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003326 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003327 break;
3328 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003329 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003330 break;
3331#endif
3332 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003333 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003334 break;
3335 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003336 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003337 break;
3338 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003339 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003341 case MSR_TSC_AUX:
3342 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3343 return 1;
3344 msr_info->data = svm->tsc_aux;
3345 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003346 /*
3347 * Nobody will change the following 5 values in the VMCB so we can
3348 * safely return them on rdmsr. They will always be 0 until LBRV is
3349 * implemented.
3350 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003351 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003352 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01003353 break;
3354 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003355 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003356 break;
3357 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003358 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003359 break;
3360 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003361 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003362 break;
3363 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003364 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003365 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003366 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003367 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003368 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003369 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003370 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003371 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003372 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003373 msr_info->data = 0x01000065;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003374 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01003375 case MSR_F15H_IC_CFG: {
3376
3377 int family, model;
3378
3379 family = guest_cpuid_family(vcpu);
3380 model = guest_cpuid_model(vcpu);
3381
3382 if (family < 0 || model < 0)
3383 return kvm_get_msr_common(vcpu, msr_info);
3384
3385 msr_info->data = 0;
3386
3387 if (family == 0x15 &&
3388 (model >= 0x2 && model < 0x20))
3389 msr_info->data = 0x1E;
3390 }
3391 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003392 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003393 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003394 }
3395 return 0;
3396}
3397
Avi Kivity851ba692009-08-24 11:10:17 +03003398static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003399{
David Kaplan668f1982015-02-20 16:02:10 -06003400 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003401 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003403 msr_info.index = ecx;
3404 msr_info.host_initiated = false;
3405 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02003406 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003407 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003408 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003409 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003410
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003411 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
3412 msr_info.data & 0xffffffff);
3413 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
3414 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003415 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003416 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417 }
3418 return 1;
3419}
3420
Joerg Roedel4a810182010-02-24 18:59:15 +01003421static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3422{
3423 struct vcpu_svm *svm = to_svm(vcpu);
3424 int svm_dis, chg_mask;
3425
3426 if (data & ~SVM_VM_CR_VALID_MASK)
3427 return 1;
3428
3429 chg_mask = SVM_VM_CR_VALID_MASK;
3430
3431 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3432 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3433
3434 svm->nested.vm_cr_msr &= ~chg_mask;
3435 svm->nested.vm_cr_msr |= (data & chg_mask);
3436
3437 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3438
3439 /* check for svm_disable while efer.svme is set */
3440 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3441 return 1;
3442
3443 return 0;
3444}
3445
Will Auld8fe8ab42012-11-29 12:42:12 -08003446static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003447{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003448 struct vcpu_svm *svm = to_svm(vcpu);
3449
Will Auld8fe8ab42012-11-29 12:42:12 -08003450 u32 ecx = msr->index;
3451 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003452 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003453 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003454 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003456 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003457 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003458 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003459#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003460 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003461 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462 break;
3463 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003464 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003465 break;
3466 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003467 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003468 break;
3469 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003470 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003471 break;
3472#endif
3473 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003474 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003475 break;
3476 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003477 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003478 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479 break;
3480 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003481 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003482 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003483 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003484 case MSR_TSC_AUX:
3485 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
3486 return 1;
3487
3488 /*
3489 * This is rare, so we update the MSR here instead of using
3490 * direct_access_msrs. Doing that would require a rdmsr in
3491 * svm_vcpu_put.
3492 */
3493 svm->tsc_aux = data;
3494 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
3495 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003496 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003497 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003498 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
3499 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003500 break;
3501 }
3502 if (data & DEBUGCTL_RESERVED_BITS)
3503 return 1;
3504
3505 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003506 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003507 if (data & (1ULL<<0))
3508 svm_enable_lbrv(svm);
3509 else
3510 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003511 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003512 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003513 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003514 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003515 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003516 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003517 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03003518 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003519 break;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003520 case MSR_IA32_APICBASE:
3521 if (kvm_vcpu_apicv_active(vcpu))
3522 avic_update_vapic_bar(to_svm(vcpu), data);
3523 /* Follow through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08003525 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526 }
3527 return 0;
3528}
3529
Avi Kivity851ba692009-08-24 11:10:17 +03003530static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531{
Will Auld8fe8ab42012-11-29 12:42:12 -08003532 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06003533 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3534 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003535
Will Auld8fe8ab42012-11-29 12:42:12 -08003536 msr.data = data;
3537 msr.index = ecx;
3538 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003539
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003540 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03003541 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02003542 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003543 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003544 } else {
3545 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10003546 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003547 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003548 return 1;
3549}
3550
Avi Kivity851ba692009-08-24 11:10:17 +03003551static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003552{
Rusty Russelle756fc62007-07-30 20:07:08 +10003553 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003554 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003555 else
Avi Kivity851ba692009-08-24 11:10:17 +03003556 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003557}
3558
Avi Kivity851ba692009-08-24 11:10:17 +03003559static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003560{
Avi Kivity3842d132010-07-27 12:30:24 +03003561 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003562 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003563 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003564 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08003565 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003566 return 1;
3567}
3568
Mark Langsdorf565d0992009-10-06 14:25:02 -05003569static int pause_interception(struct vcpu_svm *svm)
3570{
3571 kvm_vcpu_on_spin(&(svm->vcpu));
3572 return 1;
3573}
3574
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003575static int nop_interception(struct vcpu_svm *svm)
3576{
3577 skip_emulated_instruction(&(svm->vcpu));
3578 return 1;
3579}
3580
3581static int monitor_interception(struct vcpu_svm *svm)
3582{
3583 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
3584 return nop_interception(svm);
3585}
3586
3587static int mwait_interception(struct vcpu_svm *svm)
3588{
3589 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
3590 return nop_interception(svm);
3591}
3592
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003593enum avic_ipi_failure_cause {
3594 AVIC_IPI_FAILURE_INVALID_INT_TYPE,
3595 AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
3596 AVIC_IPI_FAILURE_INVALID_TARGET,
3597 AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
3598};
3599
3600static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
3601{
3602 u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
3603 u32 icrl = svm->vmcb->control.exit_info_1;
3604 u32 id = svm->vmcb->control.exit_info_2 >> 32;
Dan Carpenter5446a972016-05-23 13:20:10 +03003605 u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003606 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3607
3608 trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
3609
3610 switch (id) {
3611 case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
3612 /*
3613 * AVIC hardware handles the generation of
3614 * IPIs when the specified Message Type is Fixed
3615 * (also known as fixed delivery mode) and
3616 * the Trigger Mode is edge-triggered. The hardware
3617 * also supports self and broadcast delivery modes
3618 * specified via the Destination Shorthand(DSH)
3619 * field of the ICRL. Logical and physical APIC ID
3620 * formats are supported. All other IPI types cause
3621 * a #VMEXIT, which needs to emulated.
3622 */
3623 kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
3624 kvm_lapic_reg_write(apic, APIC_ICR, icrl);
3625 break;
3626 case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
3627 int i;
3628 struct kvm_vcpu *vcpu;
3629 struct kvm *kvm = svm->vcpu.kvm;
3630 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3631
3632 /*
3633 * At this point, we expect that the AVIC HW has already
3634 * set the appropriate IRR bits on the valid target
3635 * vcpus. So, we just need to kick the appropriate vcpu.
3636 */
3637 kvm_for_each_vcpu(i, vcpu, kvm) {
3638 bool m = kvm_apic_match_dest(vcpu, apic,
3639 icrl & KVM_APIC_SHORT_MASK,
3640 GET_APIC_DEST_FIELD(icrh),
3641 icrl & KVM_APIC_DEST_MASK);
3642
3643 if (m && !avic_vcpu_is_running(vcpu))
3644 kvm_vcpu_wake_up(vcpu);
3645 }
3646 break;
3647 }
3648 case AVIC_IPI_FAILURE_INVALID_TARGET:
3649 break;
3650 case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
3651 WARN_ONCE(1, "Invalid backing page\n");
3652 break;
3653 default:
3654 pr_err("Unknown IPI interception\n");
3655 }
3656
3657 return 1;
3658}
3659
3660static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
3661{
3662 struct kvm_arch *vm_data = &vcpu->kvm->arch;
3663 int index;
3664 u32 *logical_apic_id_table;
3665 int dlid = GET_APIC_LOGICAL_ID(ldr);
3666
3667 if (!dlid)
3668 return NULL;
3669
3670 if (flat) { /* flat */
3671 index = ffs(dlid) - 1;
3672 if (index > 7)
3673 return NULL;
3674 } else { /* cluster */
3675 int cluster = (dlid & 0xf0) >> 4;
3676 int apic = ffs(dlid & 0x0f) - 1;
3677
3678 if ((apic < 0) || (apic > 7) ||
3679 (cluster >= 0xf))
3680 return NULL;
3681 index = (cluster << 2) + apic;
3682 }
3683
3684 logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page);
3685
3686 return &logical_apic_id_table[index];
3687}
3688
3689static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
3690 bool valid)
3691{
3692 bool flat;
3693 u32 *entry, new_entry;
3694
3695 flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
3696 entry = avic_get_logical_id_entry(vcpu, ldr, flat);
3697 if (!entry)
3698 return -EINVAL;
3699
3700 new_entry = READ_ONCE(*entry);
3701 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
3702 new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
3703 if (valid)
3704 new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
3705 else
3706 new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
3707 WRITE_ONCE(*entry, new_entry);
3708
3709 return 0;
3710}
3711
3712static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
3713{
3714 int ret;
3715 struct vcpu_svm *svm = to_svm(vcpu);
3716 u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
3717
3718 if (!ldr)
3719 return 1;
3720
3721 ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
3722 if (ret && svm->ldr_reg) {
3723 avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
3724 svm->ldr_reg = 0;
3725 } else {
3726 svm->ldr_reg = ldr;
3727 }
3728 return ret;
3729}
3730
3731static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
3732{
3733 u64 *old, *new;
3734 struct vcpu_svm *svm = to_svm(vcpu);
3735 u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
3736 u32 id = (apic_id_reg >> 24) & 0xff;
3737
3738 if (vcpu->vcpu_id == id)
3739 return 0;
3740
3741 old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
3742 new = avic_get_physical_id_entry(vcpu, id);
3743 if (!new || !old)
3744 return 1;
3745
3746 /* We need to move physical_id_entry to new offset */
3747 *new = *old;
3748 *old = 0ULL;
3749 to_svm(vcpu)->avic_physical_id_cache = new;
3750
3751 /*
3752 * Also update the guest physical APIC ID in the logical
3753 * APIC ID table entry if already setup the LDR.
3754 */
3755 if (svm->ldr_reg)
3756 avic_handle_ldr_update(vcpu);
3757
3758 return 0;
3759}
3760
3761static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
3762{
3763 struct vcpu_svm *svm = to_svm(vcpu);
3764 struct kvm_arch *vm_data = &vcpu->kvm->arch;
3765 u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
3766 u32 mod = (dfr >> 28) & 0xf;
3767
3768 /*
3769 * We assume that all local APICs are using the same type.
3770 * If this changes, we need to flush the AVIC logical
3771 * APID id table.
3772 */
3773 if (vm_data->ldr_mode == mod)
3774 return 0;
3775
3776 clear_page(page_address(vm_data->avic_logical_id_table_page));
3777 vm_data->ldr_mode = mod;
3778
3779 if (svm->ldr_reg)
3780 avic_handle_ldr_update(vcpu);
3781 return 0;
3782}
3783
3784static int avic_unaccel_trap_write(struct vcpu_svm *svm)
3785{
3786 struct kvm_lapic *apic = svm->vcpu.arch.apic;
3787 u32 offset = svm->vmcb->control.exit_info_1 &
3788 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
3789
3790 switch (offset) {
3791 case APIC_ID:
3792 if (avic_handle_apic_id_update(&svm->vcpu))
3793 return 0;
3794 break;
3795 case APIC_LDR:
3796 if (avic_handle_ldr_update(&svm->vcpu))
3797 return 0;
3798 break;
3799 case APIC_DFR:
3800 avic_handle_dfr_update(&svm->vcpu);
3801 break;
3802 default:
3803 break;
3804 }
3805
3806 kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
3807
3808 return 1;
3809}
3810
3811static bool is_avic_unaccelerated_access_trap(u32 offset)
3812{
3813 bool ret = false;
3814
3815 switch (offset) {
3816 case APIC_ID:
3817 case APIC_EOI:
3818 case APIC_RRR:
3819 case APIC_LDR:
3820 case APIC_DFR:
3821 case APIC_SPIV:
3822 case APIC_ESR:
3823 case APIC_ICR:
3824 case APIC_LVTT:
3825 case APIC_LVTTHMR:
3826 case APIC_LVTPC:
3827 case APIC_LVT0:
3828 case APIC_LVT1:
3829 case APIC_LVTERR:
3830 case APIC_TMICT:
3831 case APIC_TDCR:
3832 ret = true;
3833 break;
3834 default:
3835 break;
3836 }
3837 return ret;
3838}
3839
3840static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
3841{
3842 int ret = 0;
3843 u32 offset = svm->vmcb->control.exit_info_1 &
3844 AVIC_UNACCEL_ACCESS_OFFSET_MASK;
3845 u32 vector = svm->vmcb->control.exit_info_2 &
3846 AVIC_UNACCEL_ACCESS_VECTOR_MASK;
3847 bool write = (svm->vmcb->control.exit_info_1 >> 32) &
3848 AVIC_UNACCEL_ACCESS_WRITE_MASK;
3849 bool trap = is_avic_unaccelerated_access_trap(offset);
3850
3851 trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
3852 trap, write, vector);
3853 if (trap) {
3854 /* Handling Trap */
3855 WARN_ONCE(!write, "svm: Handling trap read.\n");
3856 ret = avic_unaccel_trap_write(svm);
3857 } else {
3858 /* Handling Fault */
3859 ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
3860 }
3861
3862 return ret;
3863}
3864
Mathias Krause09941fb2012-08-30 01:30:20 +02003865static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003866 [SVM_EXIT_READ_CR0] = cr_interception,
3867 [SVM_EXIT_READ_CR3] = cr_interception,
3868 [SVM_EXIT_READ_CR4] = cr_interception,
3869 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06003870 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003871 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003872 [SVM_EXIT_WRITE_CR3] = cr_interception,
3873 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003874 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003875 [SVM_EXIT_READ_DR0] = dr_interception,
3876 [SVM_EXIT_READ_DR1] = dr_interception,
3877 [SVM_EXIT_READ_DR2] = dr_interception,
3878 [SVM_EXIT_READ_DR3] = dr_interception,
3879 [SVM_EXIT_READ_DR4] = dr_interception,
3880 [SVM_EXIT_READ_DR5] = dr_interception,
3881 [SVM_EXIT_READ_DR6] = dr_interception,
3882 [SVM_EXIT_READ_DR7] = dr_interception,
3883 [SVM_EXIT_WRITE_DR0] = dr_interception,
3884 [SVM_EXIT_WRITE_DR1] = dr_interception,
3885 [SVM_EXIT_WRITE_DR2] = dr_interception,
3886 [SVM_EXIT_WRITE_DR3] = dr_interception,
3887 [SVM_EXIT_WRITE_DR4] = dr_interception,
3888 [SVM_EXIT_WRITE_DR5] = dr_interception,
3889 [SVM_EXIT_WRITE_DR6] = dr_interception,
3890 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003891 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3892 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003893 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003894 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3895 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
3896 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01003897 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003898 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003899 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003900 [SVM_EXIT_SMI] = nop_on_interception,
3901 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003902 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02003903 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003904 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003905 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003906 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003907 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003908 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003909 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003910 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003911 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003912 [SVM_EXIT_MSR] = msr_interception,
3913 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003914 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003915 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003916 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003917 [SVM_EXIT_VMLOAD] = vmload_interception,
3918 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003919 [SVM_EXIT_STGI] = stgi_interception,
3920 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003921 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06003922 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003923 [SVM_EXIT_MONITOR] = monitor_interception,
3924 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003925 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003926 [SVM_EXIT_NPF] = pf_interception,
Paolo Bonzini64d60672015-05-07 11:36:11 +02003927 [SVM_EXIT_RSM] = emulate_on_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003928 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
3929 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930};
3931
Joe Perchesae8cc052011-04-24 22:00:50 -07003932static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003933{
3934 struct vcpu_svm *svm = to_svm(vcpu);
3935 struct vmcb_control_area *control = &svm->vmcb->control;
3936 struct vmcb_save_area *save = &svm->vmcb->save;
3937
3938 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003939 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
3940 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
3941 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
3942 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
3943 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
3944 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
3945 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3946 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3947 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3948 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3949 pr_err("%-20s%d\n", "asid:", control->asid);
3950 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3951 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3952 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3953 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3954 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3955 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3956 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3957 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3958 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3959 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3960 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003961 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07003962 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3963 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3964 pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
3965 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003966 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3967 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3968 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003969 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003970 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3971 "es:",
3972 save->es.selector, save->es.attrib,
3973 save->es.limit, save->es.base);
3974 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3975 "cs:",
3976 save->cs.selector, save->cs.attrib,
3977 save->cs.limit, save->cs.base);
3978 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3979 "ss:",
3980 save->ss.selector, save->ss.attrib,
3981 save->ss.limit, save->ss.base);
3982 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3983 "ds:",
3984 save->ds.selector, save->ds.attrib,
3985 save->ds.limit, save->ds.base);
3986 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3987 "fs:",
3988 save->fs.selector, save->fs.attrib,
3989 save->fs.limit, save->fs.base);
3990 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3991 "gs:",
3992 save->gs.selector, save->gs.attrib,
3993 save->gs.limit, save->gs.base);
3994 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3995 "gdtr:",
3996 save->gdtr.selector, save->gdtr.attrib,
3997 save->gdtr.limit, save->gdtr.base);
3998 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3999 "ldtr:",
4000 save->ldtr.selector, save->ldtr.attrib,
4001 save->ldtr.limit, save->ldtr.base);
4002 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4003 "idtr:",
4004 save->idtr.selector, save->idtr.attrib,
4005 save->idtr.limit, save->idtr.base);
4006 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
4007 "tr:",
4008 save->tr.selector, save->tr.attrib,
4009 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004010 pr_err("cpl: %d efer: %016llx\n",
4011 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07004012 pr_err("%-15s %016llx %-13s %016llx\n",
4013 "cr0:", save->cr0, "cr2:", save->cr2);
4014 pr_err("%-15s %016llx %-13s %016llx\n",
4015 "cr3:", save->cr3, "cr4:", save->cr4);
4016 pr_err("%-15s %016llx %-13s %016llx\n",
4017 "dr6:", save->dr6, "dr7:", save->dr7);
4018 pr_err("%-15s %016llx %-13s %016llx\n",
4019 "rip:", save->rip, "rflags:", save->rflags);
4020 pr_err("%-15s %016llx %-13s %016llx\n",
4021 "rsp:", save->rsp, "rax:", save->rax);
4022 pr_err("%-15s %016llx %-13s %016llx\n",
4023 "star:", save->star, "lstar:", save->lstar);
4024 pr_err("%-15s %016llx %-13s %016llx\n",
4025 "cstar:", save->cstar, "sfmask:", save->sfmask);
4026 pr_err("%-15s %016llx %-13s %016llx\n",
4027 "kernel_gs_base:", save->kernel_gs_base,
4028 "sysenter_cs:", save->sysenter_cs);
4029 pr_err("%-15s %016llx %-13s %016llx\n",
4030 "sysenter_esp:", save->sysenter_esp,
4031 "sysenter_eip:", save->sysenter_eip);
4032 pr_err("%-15s %016llx %-13s %016llx\n",
4033 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
4034 pr_err("%-15s %016llx %-13s %016llx\n",
4035 "br_from:", save->br_from, "br_to:", save->br_to);
4036 pr_err("%-15s %016llx %-13s %016llx\n",
4037 "excp_from:", save->last_excp_from,
4038 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02004039}
4040
Avi Kivity586f9602010-11-18 13:09:54 +02004041static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4042{
4043 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
4044
4045 *info1 = control->exit_info_1;
4046 *info2 = control->exit_info_2;
4047}
4048
Avi Kivity851ba692009-08-24 11:10:17 +03004049static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004050{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004051 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004052 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004053 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004054
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01004055 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
4056
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004057 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02004058 vcpu->arch.cr0 = svm->vmcb->save.cr0;
4059 if (npt_enabled)
4060 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02004061
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004062 if (unlikely(svm->nested.exit_required)) {
4063 nested_svm_vmexit(svm);
4064 svm->nested.exit_required = false;
4065
4066 return 1;
4067 }
4068
Joerg Roedel20307532010-11-29 17:51:48 +01004069 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02004070 int vmexit;
4071
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004072 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
4073 svm->vmcb->control.exit_info_1,
4074 svm->vmcb->control.exit_info_2,
4075 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01004076 svm->vmcb->control.exit_int_info_err,
4077 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02004078
Joerg Roedel410e4d52009-08-07 11:49:44 +02004079 vmexit = nested_svm_exit_special(svm);
4080
4081 if (vmexit == NESTED_EXIT_CONTINUE)
4082 vmexit = nested_svm_exit_handled(svm);
4083
4084 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01004085 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01004086 }
4087
Joerg Roedela5c38322009-08-07 11:49:32 +02004088 svm_complete_interrupts(svm);
4089
Avi Kivity04d2cc72007-09-10 18:10:54 +03004090 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
4091 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4092 kvm_run->fail_entry.hardware_entry_failure_reason
4093 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02004094 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
4095 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03004096 return 0;
4097 }
4098
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004099 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01004100 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02004101 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
4102 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02004103 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004104 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08004105 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004106 exit_code);
4107
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02004108 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08004109 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04004110 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03004111 kvm_queue_exception(vcpu, UD_VECTOR);
4112 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004113 }
4114
Avi Kivity851ba692009-08-24 11:10:17 +03004115 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004116}
4117
4118static void reload_tss(struct kvm_vcpu *vcpu)
4119{
4120 int cpu = raw_smp_processor_id();
4121
Tejun Heo0fe1e002009-10-29 22:34:14 +09004122 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
4123 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004124 load_TR_desc();
4125}
4126
Rusty Russelle756fc62007-07-30 20:07:08 +10004127static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128{
4129 int cpu = raw_smp_processor_id();
4130
Tejun Heo0fe1e002009-10-29 22:34:14 +09004131 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004132
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03004133 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09004134 if (svm->asid_generation != sd->asid_generation)
4135 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136}
4137
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004138static void svm_inject_nmi(struct kvm_vcpu *vcpu)
4139{
4140 struct vcpu_svm *svm = to_svm(vcpu);
4141
4142 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
4143 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004144 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004145 ++vcpu->stat.nmi_injections;
4146}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147
Eddie Dong85f455f2007-07-06 12:20:49 +03004148static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149{
4150 struct vmcb_control_area *control;
4151
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004152 /* The following fields are ignored when AVIC is enabled */
Rusty Russelle756fc62007-07-30 20:07:08 +10004153 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03004154 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 control->int_ctl &= ~V_INTR_PRIO_MASK;
4156 control->int_ctl |= V_IRQ_MASK |
4157 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01004158 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004159}
4160
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004161static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03004162{
4163 struct vcpu_svm *svm = to_svm(vcpu);
4164
Joerg Roedel2af91942009-08-07 11:49:28 +02004165 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01004166
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03004167 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
4168 ++vcpu->stat.irq_injections;
4169
Alexander Graf219b65d2009-06-15 15:21:25 +02004170 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
4171 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03004172}
4173
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004174static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
4175{
4176 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
4177}
4178
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004179static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
4180{
4181 struct vcpu_svm *svm = to_svm(vcpu);
4182
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004183 if (svm_nested_virtualize_tpr(vcpu) ||
4184 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004185 return;
4186
Radim Krčmář596f3142014-03-11 19:11:18 +01004187 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
4188
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004189 if (irr == -1)
4190 return;
4191
4192 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004193 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004194}
4195
Yang Zhang8d146952013-01-25 10:18:50 +08004196static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
4197{
4198 return;
4199}
4200
Andrey Smetanind62caab2015-11-10 15:36:33 +03004201static bool svm_get_enable_apicv(void)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004202{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004203 return avic;
Yang Zhangc7c9c562013-01-25 10:18:51 +08004204}
4205
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004206static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
4207{
4208}
4209
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02004210static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004211{
4212}
4213
4214/* Note: Currently only used by Hyper-V. */
Andrey Smetanind62caab2015-11-10 15:36:33 +03004215static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4216{
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004217 struct vcpu_svm *svm = to_svm(vcpu);
4218 struct vmcb *vmcb = svm->vmcb;
4219
4220 if (!avic)
4221 return;
4222
4223 vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
4224 mark_dirty(vmcb, VMCB_INTR);
Yang Zhangc7c9c562013-01-25 10:18:51 +08004225}
4226
Andrey Smetanin63086302015-11-10 15:36:32 +03004227static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08004228{
4229 return;
4230}
4231
Yang Zhanga20ed542013-04-11 19:25:15 +08004232static void svm_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4233{
4234 return;
4235}
4236
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004237static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
4238{
4239 kvm_lapic_set_irr(vec, vcpu->arch.apic);
4240 smp_mb__after_atomic();
4241
4242 if (avic_vcpu_is_running(vcpu))
4243 wrmsrl(SVM_AVIC_DOORBELL,
Suravee Suthikulpanit7d669f52016-06-15 17:23:45 -05004244 kvm_cpu_get_apicid(vcpu->cpu));
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004245 else
4246 kvm_vcpu_wake_up(vcpu);
4247}
4248
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004249static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02004250{
4251 struct vcpu_svm *svm = to_svm(vcpu);
4252 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02004253 int ret;
4254 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
4255 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
4256 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
4257
4258 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02004259}
4260
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004261static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
4262{
4263 struct vcpu_svm *svm = to_svm(vcpu);
4264
4265 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
4266}
4267
4268static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4269{
4270 struct vcpu_svm *svm = to_svm(vcpu);
4271
4272 if (masked) {
4273 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004274 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004275 } else {
4276 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01004277 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004278 }
4279}
4280
Gleb Natapov78646122009-03-23 12:12:11 +02004281static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
4282{
4283 struct vcpu_svm *svm = to_svm(vcpu);
4284 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02004285 int ret;
4286
4287 if (!gif_set(svm) ||
4288 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
4289 return 0;
4290
Avi Kivityf6e78472010-08-02 15:30:20 +03004291 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02004292
Joerg Roedel20307532010-11-29 17:51:48 +01004293 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02004294 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
4295
4296 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02004297}
4298
Jan Kiszkac9a79532014-03-07 20:03:15 +01004299static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03004300{
Alexander Graf219b65d2009-06-15 15:21:25 +02004301 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02004302
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004303 if (kvm_vcpu_apicv_active(vcpu))
4304 return;
4305
Joerg Roedele0231712010-02-24 18:59:10 +01004306 /*
4307 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
4308 * 1, because that's a separate STGI/VMRUN intercept. The next time we
4309 * get that intercept, this function will be called again though and
4310 * we'll get the vintr intercept.
4311 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01004312 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02004313 svm_set_vintr(svm);
4314 svm_inject_irq(svm, 0x0);
4315 }
Gleb Natapov9222be12009-04-23 17:14:37 +03004316}
4317
Jan Kiszkac9a79532014-03-07 20:03:15 +01004318static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004319{
Avi Kivity04d2cc72007-09-10 18:10:54 +03004320 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004321
Gleb Natapov44c11432009-05-11 13:35:52 +03004322 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
4323 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01004324 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03004325
Joerg Roedele0231712010-02-24 18:59:10 +01004326 /*
4327 * Something prevents NMI from been injected. Single step over possible
4328 * problem (IRET or exception injection or interrupt shadow)
4329 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02004330 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03004331 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03004332}
4333
Izik Eiduscbc94022007-10-25 00:29:55 +02004334static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
4335{
4336 return 0;
4337}
4338
Avi Kivityd9e368d2007-06-07 19:18:30 +03004339static void svm_flush_tlb(struct kvm_vcpu *vcpu)
4340{
Joerg Roedel38e5e922010-12-03 15:25:16 +01004341 struct vcpu_svm *svm = to_svm(vcpu);
4342
4343 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
4344 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
4345 else
4346 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03004347}
4348
Avi Kivity04d2cc72007-09-10 18:10:54 +03004349static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
4350{
4351}
4352
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004353static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
4354{
4355 struct vcpu_svm *svm = to_svm(vcpu);
4356
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004357 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004358 return;
4359
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01004360 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004361 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03004362 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004363 }
4364}
4365
Joerg Roedel649d6862008-04-16 16:51:15 +02004366static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
4367{
4368 struct vcpu_svm *svm = to_svm(vcpu);
4369 u64 cr8;
4370
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05004371 if (svm_nested_virtualize_tpr(vcpu) ||
4372 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01004373 return;
4374
Joerg Roedel649d6862008-04-16 16:51:15 +02004375 cr8 = kvm_get_cr8(vcpu);
4376 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
4377 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
4378}
4379
Gleb Natapov9222be12009-04-23 17:14:37 +03004380static void svm_complete_interrupts(struct vcpu_svm *svm)
4381{
4382 u8 vector;
4383 int type;
4384 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01004385 unsigned int3_injected = svm->int3_injected;
4386
4387 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03004388
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02004389 /*
4390 * If we've made progress since setting HF_IRET_MASK, we've
4391 * executed an IRET and can allow NMI injection.
4392 */
4393 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
4394 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03004395 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03004396 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
4397 }
Gleb Natapov44c11432009-05-11 13:35:52 +03004398
Gleb Natapov9222be12009-04-23 17:14:37 +03004399 svm->vcpu.arch.nmi_injected = false;
4400 kvm_clear_exception_queue(&svm->vcpu);
4401 kvm_clear_interrupt_queue(&svm->vcpu);
4402
4403 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
4404 return;
4405
Avi Kivity3842d132010-07-27 12:30:24 +03004406 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
4407
Gleb Natapov9222be12009-04-23 17:14:37 +03004408 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
4409 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
4410
4411 switch (type) {
4412 case SVM_EXITINTINFO_TYPE_NMI:
4413 svm->vcpu.arch.nmi_injected = true;
4414 break;
4415 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01004416 /*
4417 * In case of software exceptions, do not reinject the vector,
4418 * but re-execute the instruction instead. Rewind RIP first
4419 * if we emulated INT3 before.
4420 */
4421 if (kvm_exception_is_soft(vector)) {
4422 if (vector == BP_VECTOR && int3_injected &&
4423 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
4424 kvm_rip_write(&svm->vcpu,
4425 kvm_rip_read(&svm->vcpu) -
4426 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02004427 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01004428 }
Gleb Natapov9222be12009-04-23 17:14:37 +03004429 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
4430 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02004431 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03004432
4433 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02004434 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03004435 break;
4436 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004437 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03004438 break;
4439 default:
4440 break;
4441 }
4442}
4443
Avi Kivityb463a6f2010-07-20 15:06:17 +03004444static void svm_cancel_injection(struct kvm_vcpu *vcpu)
4445{
4446 struct vcpu_svm *svm = to_svm(vcpu);
4447 struct vmcb_control_area *control = &svm->vmcb->control;
4448
4449 control->exit_int_info = control->event_inj;
4450 control->exit_int_info_err = control->event_inj_err;
4451 control->event_inj = 0;
4452 svm_complete_interrupts(svm);
4453}
4454
Avi Kivity851ba692009-08-24 11:10:17 +03004455static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004456{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004457 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03004458
Joerg Roedel2041a062010-04-22 12:33:08 +02004459 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4460 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4461 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4462
Joerg Roedelcd3ff652009-10-09 16:08:26 +02004463 /*
4464 * A vmexit emulation is required before the vcpu can be executed
4465 * again.
4466 */
4467 if (unlikely(svm->nested.exit_required))
4468 return;
4469
Rusty Russelle756fc62007-07-30 20:07:08 +10004470 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004471
Joerg Roedel649d6862008-04-16 16:51:15 +02004472 sync_lapic_to_cr8(vcpu);
4473
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02004474 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004475
Avi Kivity04d2cc72007-09-10 18:10:54 +03004476 clgi();
4477
4478 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08004479
Avi Kivity6aa8b732006-12-10 02:21:36 -08004480 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03004481 "push %%" _ASM_BP "; \n\t"
4482 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
4483 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
4484 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
4485 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
4486 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
4487 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004488#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004489 "mov %c[r8](%[svm]), %%r8 \n\t"
4490 "mov %c[r9](%[svm]), %%r9 \n\t"
4491 "mov %c[r10](%[svm]), %%r10 \n\t"
4492 "mov %c[r11](%[svm]), %%r11 \n\t"
4493 "mov %c[r12](%[svm]), %%r12 \n\t"
4494 "mov %c[r13](%[svm]), %%r13 \n\t"
4495 "mov %c[r14](%[svm]), %%r14 \n\t"
4496 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004497#endif
4498
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03004500 "push %%" _ASM_AX " \n\t"
4501 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004502 __ex(SVM_VMLOAD) "\n\t"
4503 __ex(SVM_VMRUN) "\n\t"
4504 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03004505 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004506
4507 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03004508 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
4509 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
4510 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
4511 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
4512 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
4513 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004514#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004515 "mov %%r8, %c[r8](%[svm]) \n\t"
4516 "mov %%r9, %c[r9](%[svm]) \n\t"
4517 "mov %%r10, %c[r10](%[svm]) \n\t"
4518 "mov %%r11, %c[r11](%[svm]) \n\t"
4519 "mov %%r12, %c[r12](%[svm]) \n\t"
4520 "mov %%r13, %c[r13](%[svm]) \n\t"
4521 "mov %%r14, %c[r14](%[svm]) \n\t"
4522 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004523#endif
Avi Kivity74547662012-09-16 15:10:59 +03004524 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08004525 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004526 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08004527 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004528 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
4529 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
4530 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
4531 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
4532 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
4533 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004534#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004535 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
4536 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
4537 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
4538 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
4539 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
4540 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
4541 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
4542 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004543#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02004544 : "cc", "memory"
4545#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03004546 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02004547 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03004548#else
4549 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02004550#endif
4551 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08004552
Avi Kivity82ca2d12010-10-21 12:20:34 +02004553#ifdef CONFIG_X86_64
4554 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
4555#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02004556 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02004557#ifndef CONFIG_X86_32_LAZY_GS
4558 loadsegment(gs, svm->host.gs);
4559#endif
Avi Kivity9581d442010-10-19 16:46:55 +02004560#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004561
4562 reload_tss(vcpu);
4563
Avi Kivity56ba47d2007-11-07 17:14:18 +02004564 local_irq_disable();
4565
Avi Kivity13c34e02010-10-21 12:20:31 +02004566 vcpu->arch.cr2 = svm->vmcb->save.cr2;
4567 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
4568 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
4569 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
4570
Joerg Roedel3781c012011-01-14 16:45:02 +01004571 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
4572 kvm_before_handle_nmi(&svm->vcpu);
4573
4574 stgi();
4575
4576 /* Any pending NMI will happen here */
4577
4578 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
4579 kvm_after_handle_nmi(&svm->vcpu);
4580
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004581 sync_cr8_to_lapic(vcpu);
4582
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004583 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03004584
Joerg Roedel38e5e922010-12-03 15:25:16 +01004585 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
4586
Gleb Natapov631bc482010-10-14 11:22:52 +02004587 /* if exit due to PF check for async PF */
4588 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
4589 svm->apf_reason = kvm_read_and_reset_pf_reason();
4590
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004591 if (npt_enabled) {
4592 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
4593 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
4594 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02004595
4596 /*
4597 * We need to handle MC intercepts here before the vcpu has a chance to
4598 * change the physical cpu
4599 */
4600 if (unlikely(svm->vmcb->control.exit_code ==
4601 SVM_EXIT_EXCP_BASE + MC_VECTOR))
4602 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01004603
4604 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004605}
4606
Avi Kivity6aa8b732006-12-10 02:21:36 -08004607static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
4608{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004609 struct vcpu_svm *svm = to_svm(vcpu);
4610
4611 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01004612 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01004613 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004614}
4615
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004616static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
4617{
4618 struct vcpu_svm *svm = to_svm(vcpu);
4619
4620 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01004621 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004622
4623 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02004624 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01004625 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004626
Joerg Roedelf40f6a42010-12-03 15:25:15 +01004627 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004628}
4629
Avi Kivity6aa8b732006-12-10 02:21:36 -08004630static int is_disabled(void)
4631{
Joerg Roedel6031a612007-06-22 12:29:50 +03004632 u64 vm_cr;
4633
4634 rdmsrl(MSR_VM_CR, vm_cr);
4635 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
4636 return 1;
4637
Avi Kivity6aa8b732006-12-10 02:21:36 -08004638 return 0;
4639}
4640
Ingo Molnar102d8322007-02-19 14:37:47 +02004641static void
4642svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4643{
4644 /*
4645 * Patch in the VMMCALL instruction:
4646 */
4647 hypercall[0] = 0x0f;
4648 hypercall[1] = 0x01;
4649 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02004650}
4651
Yang, Sheng002c7f72007-07-31 14:23:01 +03004652static void svm_check_processor_compat(void *rtn)
4653{
4654 *(int *)rtn = 0;
4655}
4656
Avi Kivity774ead32007-12-26 13:57:04 +02004657static bool svm_cpu_has_accelerated_tpr(void)
4658{
4659 return false;
4660}
4661
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004662static bool svm_has_high_real_mode_segbase(void)
4663{
4664 return true;
4665}
4666
Paolo Bonzinifc07e762015-10-01 13:20:22 +02004667static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
4668{
4669 return 0;
4670}
4671
Sheng Yang0e851882009-12-18 16:48:46 +08004672static void svm_cpuid_update(struct kvm_vcpu *vcpu)
4673{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004674 struct vcpu_svm *svm = to_svm(vcpu);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05004675 struct kvm_cpuid_entry2 *entry;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004676
4677 /* Update nrips enabled cache */
4678 svm->nrips_enabled = !!guest_cpuid_has_nrips(&svm->vcpu);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05004679
4680 if (!kvm_vcpu_apicv_active(vcpu))
4681 return;
4682
4683 entry = kvm_find_cpuid_entry(vcpu, 1, 0);
4684 if (entry)
4685 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
Sheng Yang0e851882009-12-18 16:48:46 +08004686}
4687
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004688static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
4689{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004690 switch (func) {
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05004691 case 0x1:
4692 if (avic)
4693 entry->ecx &= ~bit(X86_FEATURE_X2APIC);
4694 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02004695 case 0x80000001:
4696 if (nested)
4697 entry->ecx |= (1 << 2); /* Set SVM bit */
4698 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004699 case 0x8000000A:
4700 entry->eax = 1; /* SVM revision 1 */
4701 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
4702 ASID emulation to nested SVM */
4703 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02004704 entry->edx = 0; /* Per default do not support any
4705 additional features */
4706
4707 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004708 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02004709 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004710
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02004711 /* Support NPT for the guest if enabled */
4712 if (npt_enabled)
4713 entry->edx |= SVM_FEATURE_NPT;
4714
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004715 break;
4716 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004717}
4718
Sheng Yang17cc3932010-01-05 19:02:27 +08004719static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004720{
Sheng Yang17cc3932010-01-05 19:02:27 +08004721 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004722}
4723
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004724static bool svm_rdtscp_supported(void)
4725{
Paolo Bonzini46896c72015-11-12 14:49:16 +01004726 return boot_cpu_has(X86_FEATURE_RDTSCP);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004727}
4728
Mao, Junjiead756a12012-07-02 01:18:48 +00004729static bool svm_invpcid_supported(void)
4730{
4731 return false;
4732}
4733
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004734static bool svm_mpx_supported(void)
4735{
4736 return false;
4737}
4738
Wanpeng Li55412b22014-12-02 19:21:30 +08004739static bool svm_xsaves_supported(void)
4740{
4741 return false;
4742}
4743
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004744static bool svm_has_wbinvd_exit(void)
4745{
4746 return true;
4747}
4748
Avi Kivity02daab22009-12-30 12:40:26 +02004749static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
4750{
4751 struct vcpu_svm *svm = to_svm(vcpu);
4752
Joerg Roedel18c918c2010-11-30 18:03:59 +01004753 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01004754 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02004755}
4756
Joerg Roedel80612522011-04-04 12:39:33 +02004757#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004758 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004759#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004760 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004761#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004762 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004763
Mathias Krause09941fb2012-08-30 01:30:20 +02004764static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004765 u32 exit_code;
4766 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004767} x86_intercept_map[] = {
4768 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4769 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4770 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4771 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4772 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004773 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4774 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004775 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4776 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4777 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4778 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4779 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4780 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4781 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4782 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004783 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4784 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4785 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4786 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4787 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4788 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4789 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4790 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004791 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4792 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4793 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004794 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4795 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4796 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4797 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4798 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4799 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4800 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4801 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4802 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004803 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4804 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4805 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4806 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4807 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4808 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4809 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004810 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4811 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4812 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4813 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004814};
4815
Joerg Roedel80612522011-04-04 12:39:33 +02004816#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004817#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004818#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004819
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004820static int svm_check_intercept(struct kvm_vcpu *vcpu,
4821 struct x86_instruction_info *info,
4822 enum x86_intercept_stage stage)
4823{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004824 struct vcpu_svm *svm = to_svm(vcpu);
4825 int vmexit, ret = X86EMUL_CONTINUE;
4826 struct __x86_intercept icpt_info;
4827 struct vmcb *vmcb = svm->vmcb;
4828
4829 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4830 goto out;
4831
4832 icpt_info = x86_intercept_map[info->intercept];
4833
Avi Kivity40e19b52011-04-21 12:35:41 +03004834 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004835 goto out;
4836
4837 switch (icpt_info.exit_code) {
4838 case SVM_EXIT_READ_CR0:
4839 if (info->intercept == x86_intercept_cr_read)
4840 icpt_info.exit_code += info->modrm_reg;
4841 break;
4842 case SVM_EXIT_WRITE_CR0: {
4843 unsigned long cr0, val;
4844 u64 intercept;
4845
4846 if (info->intercept == x86_intercept_cr_write)
4847 icpt_info.exit_code += info->modrm_reg;
4848
Jan Kiszka62baf442014-06-29 21:55:53 +02004849 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4850 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004851 break;
4852
4853 intercept = svm->nested.intercept;
4854
4855 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
4856 break;
4857
4858 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4859 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4860
4861 if (info->intercept == x86_intercept_lmsw) {
4862 cr0 &= 0xfUL;
4863 val &= 0xfUL;
4864 /* lmsw can't clear PE - catch this here */
4865 if (cr0 & X86_CR0_PE)
4866 val |= X86_CR0_PE;
4867 }
4868
4869 if (cr0 ^ val)
4870 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4871
4872 break;
4873 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004874 case SVM_EXIT_READ_DR0:
4875 case SVM_EXIT_WRITE_DR0:
4876 icpt_info.exit_code += info->modrm_reg;
4877 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004878 case SVM_EXIT_MSR:
4879 if (info->intercept == x86_intercept_wrmsr)
4880 vmcb->control.exit_info_1 = 1;
4881 else
4882 vmcb->control.exit_info_1 = 0;
4883 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004884 case SVM_EXIT_PAUSE:
4885 /*
4886 * We get this for NOP only, but pause
4887 * is rep not, check this here
4888 */
4889 if (info->rep_prefix != REPE_PREFIX)
4890 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02004891 case SVM_EXIT_IOIO: {
4892 u64 exit_info;
4893 u32 bytes;
4894
Joerg Roedelf6511932011-04-04 12:39:35 +02004895 if (info->intercept == x86_intercept_in ||
4896 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004897 exit_info = ((info->src_val & 0xffff) << 16) |
4898 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02004899 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02004900 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004901 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02004902 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02004903 }
4904
4905 if (info->intercept == x86_intercept_outs ||
4906 info->intercept == x86_intercept_ins)
4907 exit_info |= SVM_IOIO_STR_MASK;
4908
4909 if (info->rep_prefix)
4910 exit_info |= SVM_IOIO_REP_MASK;
4911
4912 bytes = min(bytes, 4u);
4913
4914 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4915
4916 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4917
4918 vmcb->control.exit_info_1 = exit_info;
4919 vmcb->control.exit_info_2 = info->next_rip;
4920
4921 break;
4922 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004923 default:
4924 break;
4925 }
4926
Bandan Dasf1047652015-06-11 02:05:33 -04004927 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4928 if (static_cpu_has(X86_FEATURE_NRIPS))
4929 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004930 vmcb->control.exit_code = icpt_info.exit_code;
4931 vmexit = nested_svm_exit_handled(svm);
4932
4933 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4934 : X86EMUL_CONTINUE;
4935
4936out:
4937 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004938}
4939
Yang Zhanga547c6d2013-04-11 19:25:10 +08004940static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
4941{
4942 local_irq_enable();
4943}
4944
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004945static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4946{
4947}
4948
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05004949static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
4950{
4951 if (avic_handle_apic_id_update(vcpu) != 0)
4952 return;
4953 if (avic_handle_dfr_update(vcpu) != 0)
4954 return;
4955 avic_handle_ldr_update(vcpu);
4956}
4957
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004958static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004959 .cpu_has_kvm_support = has_svm,
4960 .disabled_by_bios = is_disabled,
4961 .hardware_setup = svm_hardware_setup,
4962 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004963 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004964 .hardware_enable = svm_hardware_enable,
4965 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004966 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004967 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004968
4969 .vcpu_create = svm_create_vcpu,
4970 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004971 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004972
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004973 .vm_init = avic_vm_init,
4974 .vm_destroy = avic_vm_destroy,
4975
Avi Kivity04d2cc72007-09-10 18:10:54 +03004976 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977 .vcpu_load = svm_vcpu_load,
4978 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05004979 .vcpu_blocking = svm_vcpu_blocking,
4980 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004981
Paolo Bonzinia96036b2015-11-10 11:55:36 +01004982 .update_bp_intercept = update_bp_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004983 .get_msr = svm_get_msr,
4984 .set_msr = svm_set_msr,
4985 .get_segment_base = svm_get_segment_base,
4986 .get_segment = svm_get_segment,
4987 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004988 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004989 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004990 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004991 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004992 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004993 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004994 .set_cr3 = svm_set_cr3,
4995 .set_cr4 = svm_set_cr4,
4996 .set_efer = svm_set_efer,
4997 .get_idt = svm_get_idt,
4998 .set_idt = svm_set_idt,
4999 .get_gdt = svm_get_gdt,
5000 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005001 .get_dr6 = svm_get_dr6,
5002 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03005003 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01005004 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005005 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005006 .get_rflags = svm_get_rflags,
5007 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005008
5009 .get_pkru = svm_get_pkru,
5010
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +02005011 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02005012 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005013
Avi Kivity6aa8b732006-12-10 02:21:36 -08005014 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005015
Avi Kivity6aa8b732006-12-10 02:21:36 -08005016 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03005017 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005018 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04005019 .set_interrupt_shadow = svm_set_interrupt_shadow,
5020 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02005021 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03005022 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005023 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02005024 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03005025 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02005026 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005027 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005028 .get_nmi_mask = svm_get_nmi_mask,
5029 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005030 .enable_nmi_window = enable_nmi_window,
5031 .enable_irq_window = enable_irq_window,
5032 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08005033 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03005034 .get_enable_apicv = svm_get_enable_apicv,
5035 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005036 .load_eoi_exitmap = svm_load_eoi_exitmap,
Yang Zhanga20ed542013-04-11 19:25:15 +08005037 .sync_pir_to_irr = svm_sync_pir_to_irr,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005038 .hwapic_irr_update = svm_hwapic_irr_update,
5039 .hwapic_isr_update = svm_hwapic_isr_update,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05005040 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02005041
5042 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08005043 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005044 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005045
Avi Kivity586f9602010-11-18 13:09:54 +02005046 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02005047
Sheng Yang17cc3932010-01-05 19:02:27 +08005048 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08005049
5050 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005051
5052 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00005053 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01005054 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08005055 .xsaves_supported = svm_xsaves_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02005056
5057 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005058
5059 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10005060
Will Auldba904632012-11-29 12:42:50 -08005061 .read_tsc_offset = svm_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10005062 .write_tsc_offset = svm_write_tsc_offset,
Haozhong Zhang58ea6762015-10-20 15:39:06 +08005063 .adjust_tsc_offset_guest = svm_adjust_tsc_offset_guest,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03005064 .read_l1_tsc = svm_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005065
5066 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005067
5068 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08005069 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02005070
5071 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02005072
5073 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005074 .deliver_posted_interrupt = svm_deliver_avic_intr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005075};
5076
5077static int __init svm_init(void)
5078{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08005079 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03005080 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005081}
5082
5083static void __exit svm_exit(void)
5084{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08005085 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08005086}
5087
5088module_init(svm_init)
5089module_exit(svm_exit)