blob: cd8659cfc632bf4bd986ff5cac80e95c9011e593 [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 */
Avi Kivityedf88412007-12-16 11:02:48 +020017#include <linux/kvm_host.h>
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030021#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020022#include "x86.h"
Julian Stecklina66f7b722012-12-05 15:26:19 +010023#include "cpuid.h"
Wei Huang25462f72015-06-19 15:45:05 +020024#include "pmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040025
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/module.h>
Josh Triplettae759542012-03-28 11:32:28 -070027#include <linux/mod_devicetable.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020028#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029#include <linux/vmalloc.h>
30#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040031#include <linux/sched.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Joerg Roedel1018faa2012-02-29 14:57:32 +010035#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020036#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040037#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010038#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020039#include <asm/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080040
Eduardo Habkost63d11422008-11-17 19:03:20 -020041#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030042#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020043
Avi Kivity4ecac3f2008-05-13 13:23:38 +030044#define __ex(x) __kvm_handle_fault_on_reboot(x)
45
Avi Kivity6aa8b732006-12-10 02:21:36 -080046MODULE_AUTHOR("Qumranet");
47MODULE_LICENSE("GPL");
48
Josh Triplettae759542012-03-28 11:32:28 -070049static const struct x86_cpu_id svm_cpu_id[] = {
50 X86_FEATURE_MATCH(X86_FEATURE_SVM),
51 {}
52};
53MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
54
Avi Kivity6aa8b732006-12-10 02:21:36 -080055#define IOPM_ALLOC_ORDER 2
56#define MSRPM_ALLOC_ORDER 1
57
Avi Kivity6aa8b732006-12-10 02:21:36 -080058#define SEG_TYPE_LDT 2
59#define SEG_TYPE_BUSY_TSS16 3
60
Andre Przywara6bc31bd2010-04-11 23:07:28 +020061#define SVM_FEATURE_NPT (1 << 0)
62#define SVM_FEATURE_LBRV (1 << 1)
63#define SVM_FEATURE_SVML (1 << 2)
64#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010065#define SVM_FEATURE_TSC_RATE (1 << 4)
66#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
67#define SVM_FEATURE_FLUSH_ASID (1 << 6)
68#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020069#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030070
Joerg Roedel410e4d52009-08-07 11:49:44 +020071#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
72#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
73#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
74
Joerg Roedel24e09cb2008-02-13 18:58:47 +010075#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
76
Joerg Roedelfbc0db72011-03-25 09:44:46 +010077#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010078#define TSC_RATIO_MIN 0x0000000000000001ULL
79#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010080
Joerg Roedel67ec6602010-05-17 14:43:35 +020081static bool erratum_383_found __read_mostly;
82
Avi Kivity6c8166a2009-05-31 18:15:37 +030083static const u32 host_save_user_msrs[] = {
84#ifdef CONFIG_X86_64
85 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
86 MSR_FS_BASE,
87#endif
88 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
89};
90
91#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
92
93struct kvm_vcpu;
94
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020095struct nested_state {
96 struct vmcb *hsave;
97 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010098 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020099 u64 vmcb;
100
101 /* These are the merged vectors */
102 u32 *msrpm;
103
104 /* gpa pointers to the real vectors */
105 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100106 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200107
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200108 /* A VMEXIT is required but not yet emulated */
109 bool exit_required;
110
Joerg Roedelaad42c62009-08-07 11:49:34 +0200111 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100112 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100113 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200114 u32 intercept_exceptions;
115 u64 intercept;
116
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200117 /* Nested Paging related state */
118 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200119};
120
Joerg Roedel323c3d82010-03-01 15:34:37 +0100121#define MSRPM_OFFSETS 16
122static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
123
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500124/*
125 * Set osvw_len to higher value when updated Revision Guides
126 * are published and we know what the new status bits are
127 */
128static uint64_t osvw_len = 4, osvw_status;
129
Avi Kivity6c8166a2009-05-31 18:15:37 +0300130struct vcpu_svm {
131 struct kvm_vcpu vcpu;
132 struct vmcb *vmcb;
133 unsigned long vmcb_pa;
134 struct svm_cpu_data *svm_data;
135 uint64_t asid_generation;
136 uint64_t sysenter_esp;
137 uint64_t sysenter_eip;
138
139 u64 next_rip;
140
141 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200142 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200143 u16 fs;
144 u16 gs;
145 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200146 u64 gs_base;
147 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300148
149 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300150
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200151 ulong nmi_iret_rip;
152
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200153 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200154
155 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100156
157 unsigned int3_injected;
158 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200159 u32 apf_reason;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100160
161 u64 tsc_ratio;
Joerg Roedel6092d3d2015-10-14 15:10:54 +0200162
163 /* cached guest cpuid flags for faster access */
164 bool nrips_enabled : 1;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300165};
166
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100167static DEFINE_PER_CPU(u64, current_tsc_ratio);
168#define TSC_RATIO_DEFAULT 0x0100000000ULL
169
Joerg Roedel455716f2010-03-01 15:34:35 +0100170#define MSR_INVALID 0xffffffffU
171
Mathias Krause09941fb2012-08-30 01:30:20 +0200172static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100173 u32 index; /* Index of the MSR */
174 bool always; /* True if intercept is always on */
175} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400176 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100177 { .index = MSR_IA32_SYSENTER_CS, .always = true },
178#ifdef CONFIG_X86_64
179 { .index = MSR_GS_BASE, .always = true },
180 { .index = MSR_FS_BASE, .always = true },
181 { .index = MSR_KERNEL_GS_BASE, .always = true },
182 { .index = MSR_LSTAR, .always = true },
183 { .index = MSR_CSTAR, .always = true },
184 { .index = MSR_SYSCALL_MASK, .always = true },
185#endif
186 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
187 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
188 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
189 { .index = MSR_IA32_LASTINTTOIP, .always = false },
190 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191};
192
193/* enable NPT for AMD64 and X86 with PAE */
194#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
195static bool npt_enabled = true;
196#else
Joerg Roedele0231712010-02-24 18:59:10 +0100197static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198#endif
199
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100200/* allow nested paging (virtualized MMU) for all guests */
201static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202module_param(npt, int, S_IRUGO);
203
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100204/* allow nested virtualization in KVM/SVM */
205static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800206module_param(nested, int, S_IRUGO);
207
Paolo Bonzini79a80592015-09-21 07:46:55 +0200208static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200210static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211
Joerg Roedel410e4d52009-08-07 11:49:44 +0200212static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100213static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
216 bool has_error_code, u32 error_code);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100217static u64 __scale_tsc(u64 ratio, u64 tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100219enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100220 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
221 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100222 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100223 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100224 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100225 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100226 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100227 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100228 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100229 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100230 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100231 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100232 VMCB_DIRTY_MAX,
233};
234
Joerg Roedel0574dec2010-12-03 11:45:58 +0100235/* TPR and CR2 are always written before VMRUN */
236#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100237
238static inline void mark_all_dirty(struct vmcb *vmcb)
239{
240 vmcb->control.clean = 0;
241}
242
243static inline void mark_all_clean(struct vmcb *vmcb)
244{
245 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
246 & ~VMCB_ALWAYS_DIRTY_MASK;
247}
248
249static inline void mark_dirty(struct vmcb *vmcb, int bit)
250{
251 vmcb->control.clean &= ~(1 << bit);
252}
253
Avi Kivity6aa8b732006-12-10 02:21:36 -0800254static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
255{
256 return container_of(vcpu, struct vcpu_svm, vcpu);
257}
258
Joerg Roedel384c6362010-11-30 18:03:56 +0100259static void recalc_intercepts(struct vcpu_svm *svm)
260{
261 struct vmcb_control_area *c, *h;
262 struct nested_state *g;
263
Joerg Roedel116a0a22010-12-03 11:45:49 +0100264 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
265
Joerg Roedel384c6362010-11-30 18:03:56 +0100266 if (!is_guest_mode(&svm->vcpu))
267 return;
268
269 c = &svm->vmcb->control;
270 h = &svm->nested.hsave->control;
271 g = &svm->nested;
272
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100273 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100274 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100275 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
276 c->intercept = h->intercept | g->intercept;
277}
278
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100279static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
280{
281 if (is_guest_mode(&svm->vcpu))
282 return svm->nested.hsave;
283 else
284 return svm->vmcb;
285}
286
287static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
288{
289 struct vmcb *vmcb = get_host_vmcb(svm);
290
291 vmcb->control.intercept_cr |= (1U << bit);
292
293 recalc_intercepts(svm);
294}
295
296static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
297{
298 struct vmcb *vmcb = get_host_vmcb(svm);
299
300 vmcb->control.intercept_cr &= ~(1U << bit);
301
302 recalc_intercepts(svm);
303}
304
305static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
306{
307 struct vmcb *vmcb = get_host_vmcb(svm);
308
309 return vmcb->control.intercept_cr & (1U << bit);
310}
311
Paolo Bonzini5315c712014-03-03 13:08:29 +0100312static inline void set_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100313{
314 struct vmcb *vmcb = get_host_vmcb(svm);
315
Paolo Bonzini5315c712014-03-03 13:08:29 +0100316 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
317 | (1 << INTERCEPT_DR1_READ)
318 | (1 << INTERCEPT_DR2_READ)
319 | (1 << INTERCEPT_DR3_READ)
320 | (1 << INTERCEPT_DR4_READ)
321 | (1 << INTERCEPT_DR5_READ)
322 | (1 << INTERCEPT_DR6_READ)
323 | (1 << INTERCEPT_DR7_READ)
324 | (1 << INTERCEPT_DR0_WRITE)
325 | (1 << INTERCEPT_DR1_WRITE)
326 | (1 << INTERCEPT_DR2_WRITE)
327 | (1 << INTERCEPT_DR3_WRITE)
328 | (1 << INTERCEPT_DR4_WRITE)
329 | (1 << INTERCEPT_DR5_WRITE)
330 | (1 << INTERCEPT_DR6_WRITE)
331 | (1 << INTERCEPT_DR7_WRITE);
Joerg Roedel3aed0412010-11-30 18:03:58 +0100332
333 recalc_intercepts(svm);
334}
335
Paolo Bonzini5315c712014-03-03 13:08:29 +0100336static inline void clr_dr_intercepts(struct vcpu_svm *svm)
Joerg Roedel3aed0412010-11-30 18:03:58 +0100337{
338 struct vmcb *vmcb = get_host_vmcb(svm);
339
Paolo Bonzini5315c712014-03-03 13:08:29 +0100340 vmcb->control.intercept_dr = 0;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100341
342 recalc_intercepts(svm);
343}
344
Joerg Roedel18c918c2010-11-30 18:03:59 +0100345static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
346{
347 struct vmcb *vmcb = get_host_vmcb(svm);
348
349 vmcb->control.intercept_exceptions |= (1U << bit);
350
351 recalc_intercepts(svm);
352}
353
354static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
355{
356 struct vmcb *vmcb = get_host_vmcb(svm);
357
358 vmcb->control.intercept_exceptions &= ~(1U << bit);
359
360 recalc_intercepts(svm);
361}
362
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100363static inline void set_intercept(struct vcpu_svm *svm, int bit)
364{
365 struct vmcb *vmcb = get_host_vmcb(svm);
366
367 vmcb->control.intercept |= (1ULL << bit);
368
369 recalc_intercepts(svm);
370}
371
372static inline void clr_intercept(struct vcpu_svm *svm, int bit)
373{
374 struct vmcb *vmcb = get_host_vmcb(svm);
375
376 vmcb->control.intercept &= ~(1ULL << bit);
377
378 recalc_intercepts(svm);
379}
380
Joerg Roedel2af91942009-08-07 11:49:28 +0200381static inline void enable_gif(struct vcpu_svm *svm)
382{
383 svm->vcpu.arch.hflags |= HF_GIF_MASK;
384}
385
386static inline void disable_gif(struct vcpu_svm *svm)
387{
388 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
389}
390
391static inline bool gif_set(struct vcpu_svm *svm)
392{
393 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
394}
395
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396static unsigned long iopm_base;
397
398struct kvm_ldttss_desc {
399 u16 limit0;
400 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100401 unsigned base1:8, type:5, dpl:2, p:1;
402 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800403 u32 base3;
404 u32 zero1;
405} __attribute__((packed));
406
407struct svm_cpu_data {
408 int cpu;
409
Avi Kivity5008fdf2007-04-02 13:05:50 +0300410 u64 asid_generation;
411 u32 max_asid;
412 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413 struct kvm_ldttss_desc *tss_desc;
414
415 struct page *save_area;
416};
417
418static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
419
420struct svm_init_data {
421 int cpu;
422 int r;
423};
424
Mathias Krause09941fb2012-08-30 01:30:20 +0200425static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200427#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428#define MSRS_RANGE_SIZE 2048
429#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
430
Joerg Roedel455716f2010-03-01 15:34:35 +0100431static u32 svm_msrpm_offset(u32 msr)
432{
433 u32 offset;
434 int i;
435
436 for (i = 0; i < NUM_MSR_MAPS; i++) {
437 if (msr < msrpm_ranges[i] ||
438 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
439 continue;
440
441 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
442 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
443
444 /* Now we have the u8 offset - but need the u32 offset */
445 return offset / 4;
446 }
447
448 /* MSR not in any range */
449 return MSR_INVALID;
450}
451
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452#define MAX_INST_SIZE 15
453
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454static inline void clgi(void)
455{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300456 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457}
458
459static inline void stgi(void)
460{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300461 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462}
463
464static inline void invlpga(unsigned long addr, u32 asid)
465{
Joerg Roedele0231712010-02-24 18:59:10 +0100466 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467}
468
Joerg Roedel4b161842010-09-10 17:31:03 +0200469static int get_npt_level(void)
470{
471#ifdef CONFIG_X86_64
472 return PT64_ROOT_LEVEL;
473#else
474 return PT32E_ROOT_LEVEL;
475#endif
476}
477
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
479{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000480 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100481 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600482 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483
Alexander Graf9962d032008-11-25 20:17:02 +0100484 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100485 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486}
487
Avi Kivity6aa8b732006-12-10 02:21:36 -0800488static int is_external_interrupt(u32 info)
489{
490 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
491 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
492}
493
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200494static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400495{
496 struct vcpu_svm *svm = to_svm(vcpu);
497 u32 ret = 0;
498
499 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200500 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
501 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400502}
503
504static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
505{
506 struct vcpu_svm *svm = to_svm(vcpu);
507
508 if (mask == 0)
509 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
510 else
511 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
512
513}
514
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
516{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400517 struct vcpu_svm *svm = to_svm(vcpu);
518
Bandan Dasf1047652015-06-11 02:05:33 -0400519 if (svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200520 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200521 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400522 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200523
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400524 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100525 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300526 EMULATE_DONE)
527 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528 return;
529 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300530 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
531 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
532 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300534 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400535 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536}
537
Jan Kiszka116a4752010-02-23 17:47:54 +0100538static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200539 bool has_error_code, u32 error_code,
540 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100541{
542 struct vcpu_svm *svm = to_svm(vcpu);
543
Joerg Roedele0231712010-02-24 18:59:10 +0100544 /*
545 * If we are within a nested VM we'd better #VMEXIT and let the guest
546 * handle the exception
547 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200548 if (!reinject &&
549 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100550 return;
551
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200552 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100553 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
554
555 /*
556 * For guest debugging where we have to reinject #BP if some
557 * INT3 is guest-owned:
558 * Emulate nRIP by moving RIP forward. Will fail if injection
559 * raises a fault that is not intercepted. Still better than
560 * failing in all cases.
561 */
562 skip_emulated_instruction(&svm->vcpu);
563 rip = kvm_rip_read(&svm->vcpu);
564 svm->int3_rip = rip + svm->vmcb->save.cs.base;
565 svm->int3_injected = rip - old_rip;
566 }
567
Jan Kiszka116a4752010-02-23 17:47:54 +0100568 svm->vmcb->control.event_inj = nr
569 | SVM_EVTINJ_VALID
570 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
571 | SVM_EVTINJ_TYPE_EXEPT;
572 svm->vmcb->control.event_inj_err = error_code;
573}
574
Joerg Roedel67ec6602010-05-17 14:43:35 +0200575static void svm_init_erratum_383(void)
576{
577 u32 low, high;
578 int err;
579 u64 val;
580
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100581 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200582 return;
583
584 /* Use _safe variants to not break nested virtualization */
585 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
586 if (err)
587 return;
588
589 val |= (1ULL << 47);
590
591 low = lower_32_bits(val);
592 high = upper_32_bits(val);
593
594 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
595
596 erratum_383_found = true;
597}
598
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500599static void svm_init_osvw(struct kvm_vcpu *vcpu)
600{
601 /*
602 * Guests should see errata 400 and 415 as fixed (assuming that
603 * HLT and IO instructions are intercepted).
604 */
605 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
606 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
607
608 /*
609 * By increasing VCPU's osvw.length to 3 we are telling the guest that
610 * all osvw.status bits inside that length, including bit 0 (which is
611 * reserved for erratum 298), are valid. However, if host processor's
612 * osvw_len is 0 then osvw_status[0] carries no information. We need to
613 * be conservative here and therefore we tell the guest that erratum 298
614 * is present (because we really don't know).
615 */
616 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
617 vcpu->arch.osvw.status |= 1;
618}
619
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620static int has_svm(void)
621{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200622 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623
Eduardo Habkost63d11422008-11-17 19:03:20 -0200624 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800625 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626 return 0;
627 }
628
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629 return 1;
630}
631
Radim Krčmář13a34e02014-08-28 15:13:03 +0200632static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100634 /* Make sure we clean up behind us */
635 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
636 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
637
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200638 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100639
640 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641}
642
Radim Krčmář13a34e02014-08-28 15:13:03 +0200643static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644{
645
Tejun Heo0fe1e002009-10-29 22:34:14 +0900646 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200648 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649 struct desc_struct *gdt;
650 int me = raw_smp_processor_id();
651
Alexander Graf10474ae2009-09-15 11:37:46 +0200652 rdmsrl(MSR_EFER, efer);
653 if (efer & EFER_SVME)
654 return -EBUSY;
655
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200657 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200658 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900660 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900661 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200662 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200663 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 }
665
Tejun Heo0fe1e002009-10-29 22:34:14 +0900666 sd->asid_generation = 1;
667 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
668 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800669
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200670 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200671 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900672 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800673
Alexander Graf9962d032008-11-25 20:17:02 +0100674 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675
Linus Torvaldsd0316552009-12-14 09:58:24 -0800676 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200677
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100678 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
679 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500680 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100681 }
682
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500683
684 /*
685 * Get OSVW bits.
686 *
687 * Note that it is possible to have a system with mixed processor
688 * revisions and therefore different OSVW bits. If bits are not the same
689 * on different processors then choose the worst case (i.e. if erratum
690 * is present on one processor and not on another then assume that the
691 * erratum is present everywhere).
692 */
693 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
694 uint64_t len, status = 0;
695 int err;
696
697 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
698 if (!err)
699 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
700 &err);
701
702 if (err)
703 osvw_status = osvw_len = 0;
704 else {
705 if (len < osvw_len)
706 osvw_len = len;
707 osvw_status |= status;
708 osvw_status &= (1ULL << osvw_len) - 1;
709 }
710 } else
711 osvw_status = osvw_len = 0;
712
Joerg Roedel67ec6602010-05-17 14:43:35 +0200713 svm_init_erratum_383();
714
Joerg Roedel1018faa2012-02-29 14:57:32 +0100715 amd_pmu_enable_virt();
716
Alexander Graf10474ae2009-09-15 11:37:46 +0200717 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718}
719
Joerg Roedel0da1db752008-07-02 16:02:11 +0200720static void svm_cpu_uninit(int cpu)
721{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900722 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200723
Tejun Heo0fe1e002009-10-29 22:34:14 +0900724 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200725 return;
726
727 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900728 __free_page(sd->save_area);
729 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200730}
731
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732static int svm_cpu_init(int cpu)
733{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900734 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735 int r;
736
Tejun Heo0fe1e002009-10-29 22:34:14 +0900737 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
738 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900740 sd->cpu = cpu;
741 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900743 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 goto err_1;
745
Tejun Heo0fe1e002009-10-29 22:34:14 +0900746 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747
748 return 0;
749
750err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900751 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752 return r;
753
754}
755
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100756static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757{
758 int i;
759
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100760 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
761 if (direct_access_msrs[i].index == index)
762 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800763
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100764 return false;
765}
766
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767static void set_msr_interception(u32 *msrpm, unsigned msr,
768 int read, int write)
769{
Joerg Roedel455716f2010-03-01 15:34:35 +0100770 u8 bit_read, bit_write;
771 unsigned long tmp;
772 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100774 /*
775 * If this warning triggers extend the direct_access_msrs list at the
776 * beginning of the file
777 */
778 WARN_ON(!valid_msr_intercept(msr));
779
Joerg Roedel455716f2010-03-01 15:34:35 +0100780 offset = svm_msrpm_offset(msr);
781 bit_read = 2 * (msr & 0x0f);
782 bit_write = 2 * (msr & 0x0f) + 1;
783 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784
Joerg Roedel455716f2010-03-01 15:34:35 +0100785 BUG_ON(offset == MSR_INVALID);
786
787 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
788 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
789
790 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791}
792
Joerg Roedelf65c2292008-02-13 18:58:46 +0100793static void svm_vcpu_init_msrpm(u32 *msrpm)
794{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100795 int i;
796
Joerg Roedelf65c2292008-02-13 18:58:46 +0100797 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
798
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100799 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
800 if (!direct_access_msrs[i].always)
801 continue;
802
803 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
804 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100805}
806
Joerg Roedel323c3d82010-03-01 15:34:37 +0100807static void add_msr_offset(u32 offset)
808{
809 int i;
810
811 for (i = 0; i < MSRPM_OFFSETS; ++i) {
812
813 /* Offset already in list? */
814 if (msrpm_offsets[i] == offset)
815 return;
816
817 /* Slot used by another offset? */
818 if (msrpm_offsets[i] != MSR_INVALID)
819 continue;
820
821 /* Add offset to list */
822 msrpm_offsets[i] = offset;
823
824 return;
825 }
826
827 /*
828 * If this BUG triggers the msrpm_offsets table has an overflow. Just
829 * increase MSRPM_OFFSETS in this case.
830 */
831 BUG();
832}
833
834static void init_msrpm_offsets(void)
835{
836 int i;
837
838 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
839
840 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
841 u32 offset;
842
843 offset = svm_msrpm_offset(direct_access_msrs[i].index);
844 BUG_ON(offset == MSR_INVALID);
845
846 add_msr_offset(offset);
847 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848}
849
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100850static void svm_enable_lbrv(struct vcpu_svm *svm)
851{
852 u32 *msrpm = svm->msrpm;
853
854 svm->vmcb->control.lbr_ctl = 1;
855 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
856 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
857 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
858 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
859}
860
861static void svm_disable_lbrv(struct vcpu_svm *svm)
862{
863 u32 *msrpm = svm->msrpm;
864
865 svm->vmcb->control.lbr_ctl = 0;
866 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
867 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
868 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
869 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
870}
871
Avi Kivity6aa8b732006-12-10 02:21:36 -0800872static __init int svm_hardware_setup(void)
873{
874 int cpu;
875 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100876 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877 int r;
878
Avi Kivity6aa8b732006-12-10 02:21:36 -0800879 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
880
881 if (!iopm_pages)
882 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300883
884 iopm_va = page_address(iopm_pages);
885 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
887
Joerg Roedel323c3d82010-03-01 15:34:37 +0100888 init_msrpm_offsets();
889
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100890 if (boot_cpu_has(X86_FEATURE_NX))
891 kvm_enable_efer_bits(EFER_NX);
892
Alexander Graf1b2fd702009-02-02 16:23:51 +0100893 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
894 kvm_enable_efer_bits(EFER_FFXSR);
895
Joerg Roedel92a1f122011-03-25 09:44:51 +0100896 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
897 u64 max;
898
899 kvm_has_tsc_control = true;
900
901 /*
902 * Make sure the user can only configure tsc_khz values that
903 * fit into a signed integer.
904 * A min value is not calculated needed because it will always
905 * be 1 on all machines and a value of 0 is used to disable
906 * tsc-scaling for the vcpu.
907 */
908 max = min(0x7fffffffULL, __scale_tsc(tsc_khz, TSC_RATIO_MAX));
909
910 kvm_max_guest_tsc_khz = max;
911 }
912
Alexander Graf236de052008-11-25 20:17:10 +0100913 if (nested) {
914 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200915 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100916 }
917
Zachary Amsden3230bb42009-09-29 11:38:37 -1000918 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919 r = svm_cpu_init(cpu);
920 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100921 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100923
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200924 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100925 npt_enabled = false;
926
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100927 if (npt_enabled && !npt) {
928 printk(KERN_INFO "kvm: Nested Paging disabled\n");
929 npt_enabled = false;
930 }
931
Joerg Roedel18552672008-02-07 13:47:41 +0100932 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100933 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100934 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200935 } else
936 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100937
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 return 0;
939
Joerg Roedelf65c2292008-02-13 18:58:46 +0100940err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
942 iopm_base = 0;
943 return r;
944}
945
946static __exit void svm_hardware_unsetup(void)
947{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200948 int cpu;
949
Zachary Amsden3230bb42009-09-29 11:38:37 -1000950 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200951 svm_cpu_uninit(cpu);
952
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100954 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955}
956
957static void init_seg(struct vmcb_seg *seg)
958{
959 seg->selector = 0;
960 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100961 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 seg->limit = 0xffff;
963 seg->base = 0;
964}
965
966static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
967{
968 seg->selector = 0;
969 seg->attrib = SVM_SELECTOR_P_MASK | type;
970 seg->limit = 0xffff;
971 seg->base = 0;
972}
973
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100974static u64 __scale_tsc(u64 ratio, u64 tsc)
975{
976 u64 mult, frac, _tsc;
977
978 mult = ratio >> 32;
979 frac = ratio & ((1ULL << 32) - 1);
980
981 _tsc = tsc;
982 _tsc *= mult;
983 _tsc += (tsc >> 32) * frac;
984 _tsc += ((tsc & ((1ULL << 32) - 1)) * frac) >> 32;
985
986 return _tsc;
987}
988
989static u64 svm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
990{
991 struct vcpu_svm *svm = to_svm(vcpu);
992 u64 _tsc = tsc;
993
994 if (svm->tsc_ratio != TSC_RATIO_DEFAULT)
995 _tsc = __scale_tsc(svm->tsc_ratio, tsc);
996
997 return _tsc;
998}
999
Zachary Amsdencc578282012-02-03 15:43:50 -02001000static void svm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +01001001{
1002 struct vcpu_svm *svm = to_svm(vcpu);
1003 u64 ratio;
1004 u64 khz;
1005
Zachary Amsdencc578282012-02-03 15:43:50 -02001006 /* Guest TSC same frequency as host TSC? */
1007 if (!scale) {
Joerg Roedel4051b182011-03-25 09:44:49 +01001008 svm->tsc_ratio = TSC_RATIO_DEFAULT;
1009 return;
1010 }
1011
Zachary Amsdencc578282012-02-03 15:43:50 -02001012 /* TSC scaling supported? */
1013 if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1014 if (user_tsc_khz > tsc_khz) {
1015 vcpu->arch.tsc_catchup = 1;
1016 vcpu->arch.tsc_always_catchup = 1;
1017 } else
1018 WARN(1, "user requested TSC rate below hardware speed\n");
1019 return;
1020 }
1021
Joerg Roedel4051b182011-03-25 09:44:49 +01001022 khz = user_tsc_khz;
1023
1024 /* TSC scaling required - calculate ratio */
1025 ratio = khz << 32;
1026 do_div(ratio, tsc_khz);
1027
1028 if (ratio == 0 || ratio & TSC_RATIO_RSVD) {
1029 WARN_ONCE(1, "Invalid TSC ratio - virtual-tsc-khz=%u\n",
1030 user_tsc_khz);
1031 return;
1032 }
Joerg Roedel4051b182011-03-25 09:44:49 +01001033 svm->tsc_ratio = ratio;
1034}
1035
Will Auldba904632012-11-29 12:42:50 -08001036static u64 svm_read_tsc_offset(struct kvm_vcpu *vcpu)
1037{
1038 struct vcpu_svm *svm = to_svm(vcpu);
1039
1040 return svm->vmcb->control.tsc_offset;
1041}
1042
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001043static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1044{
1045 struct vcpu_svm *svm = to_svm(vcpu);
1046 u64 g_tsc_offset = 0;
1047
Joerg Roedel20307532010-11-29 17:51:48 +01001048 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001049 g_tsc_offset = svm->vmcb->control.tsc_offset -
1050 svm->nested.hsave->control.tsc_offset;
1051 svm->nested.hsave->control.tsc_offset = offset;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001052 } else
1053 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1054 svm->vmcb->control.tsc_offset,
1055 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001056
1057 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001058
1059 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001060}
1061
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001062static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10001063{
1064 struct vcpu_svm *svm = to_svm(vcpu);
1065
Chris J Argesd913b902014-11-12 21:00:39 -06001066 if (host) {
1067 if (svm->tsc_ratio != TSC_RATIO_DEFAULT)
1068 WARN_ON(adjustment < 0);
1069 adjustment = svm_scale_tsc(vcpu, (u64)adjustment);
1070 }
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001071
Zachary Amsdene48672f2010-08-19 22:07:23 -10001072 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +01001073 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -10001074 svm->nested.hsave->control.tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09001075 else
1076 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1077 svm->vmcb->control.tsc_offset - adjustment,
1078 svm->vmcb->control.tsc_offset);
1079
Joerg Roedel116a0a22010-12-03 11:45:49 +01001080 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001081}
1082
Joerg Roedel857e4092011-03-25 09:44:50 +01001083static u64 svm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1084{
1085 u64 tsc;
1086
Andy Lutomirski4ea16362015-06-25 18:44:07 +02001087 tsc = svm_scale_tsc(vcpu, rdtsc());
Joerg Roedel857e4092011-03-25 09:44:50 +01001088
1089 return target_tsc - tsc;
1090}
1091
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001092static void init_vmcb(struct vcpu_svm *svm, bool init_event)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093{
Joerg Roedele6101a92008-02-13 18:58:45 +01001094 struct vmcb_control_area *control = &svm->vmcb->control;
1095 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096
Avi Kivitybff78272010-01-07 13:16:08 +02001097 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001098 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001099
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001100 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1101 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1102 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1103 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1104 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1105 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
1106 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107
Paolo Bonzini5315c712014-03-03 13:08:29 +01001108 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109
Joerg Roedel18c918c2010-11-30 18:03:59 +01001110 set_exception_intercept(svm, PF_VECTOR);
1111 set_exception_intercept(svm, UD_VECTOR);
1112 set_exception_intercept(svm, MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001114 set_intercept(svm, INTERCEPT_INTR);
1115 set_intercept(svm, INTERCEPT_NMI);
1116 set_intercept(svm, INTERCEPT_SMI);
1117 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001118 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001119 set_intercept(svm, INTERCEPT_CPUID);
1120 set_intercept(svm, INTERCEPT_INVD);
1121 set_intercept(svm, INTERCEPT_HLT);
1122 set_intercept(svm, INTERCEPT_INVLPG);
1123 set_intercept(svm, INTERCEPT_INVLPGA);
1124 set_intercept(svm, INTERCEPT_IOIO_PROT);
1125 set_intercept(svm, INTERCEPT_MSR_PROT);
1126 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1127 set_intercept(svm, INTERCEPT_SHUTDOWN);
1128 set_intercept(svm, INTERCEPT_VMRUN);
1129 set_intercept(svm, INTERCEPT_VMMCALL);
1130 set_intercept(svm, INTERCEPT_VMLOAD);
1131 set_intercept(svm, INTERCEPT_VMSAVE);
1132 set_intercept(svm, INTERCEPT_STGI);
1133 set_intercept(svm, INTERCEPT_CLGI);
1134 set_intercept(svm, INTERCEPT_SKINIT);
1135 set_intercept(svm, INTERCEPT_WBINVD);
1136 set_intercept(svm, INTERCEPT_MONITOR);
1137 set_intercept(svm, INTERCEPT_MWAIT);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001138 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139
1140 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001141 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 control->int_ctl = V_INTR_MASKING_MASK;
1143
1144 init_seg(&save->es);
1145 init_seg(&save->ss);
1146 init_seg(&save->ds);
1147 init_seg(&save->fs);
1148 init_seg(&save->gs);
1149
1150 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001151 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152 /* Executable/Readable Code Segment */
1153 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1154 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1155 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156
1157 save->gdtr.limit = 0xffff;
1158 save->idtr.limit = 0xffff;
1159
1160 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1161 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1162
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001163 if (!init_event)
1164 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001165 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001166 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001168 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001169
Joerg Roedele0231712010-02-24 18:59:10 +01001170 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001171 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001172 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001174 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001175 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001176
Rusty Russell66aee912007-07-17 23:34:16 +10001177 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001179
1180 if (npt_enabled) {
1181 /* Setup VMCB for Nested Paging */
1182 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001183 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001184 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001185 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1186 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001187 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001188 save->cr3 = 0;
1189 save->cr4 = 0;
1190 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001191 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001192
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001193 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001194 svm->vcpu.arch.hflags = 0;
1195
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001196 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001197 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001198 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001199 }
1200
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001201 mark_all_dirty(svm->vmcb);
1202
Joerg Roedel2af91942009-08-07 11:49:28 +02001203 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204}
1205
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001206static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001207{
1208 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001209 u32 dummy;
1210 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001211
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001212 if (!init_event) {
1213 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1214 MSR_IA32_APICBASE_ENABLE;
1215 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1216 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1217 }
1218 init_vmcb(svm, init_event);
Avi Kivity70433382007-11-07 12:57:23 +02001219
Julian Stecklina66f7b722012-12-05 15:26:19 +01001220 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy);
1221 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001222}
1223
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001224static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001226 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001228 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001229 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001230 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001231 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232
Rusty Russellc16f8622007-07-30 21:12:19 +10001233 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001234 if (!svm) {
1235 err = -ENOMEM;
1236 goto out;
1237 }
1238
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001239 svm->tsc_ratio = TSC_RATIO_DEFAULT;
1240
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001241 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1242 if (err)
1243 goto free_svm;
1244
Joerg Roedelf65c2292008-02-13 18:58:46 +01001245 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001246 page = alloc_page(GFP_KERNEL);
1247 if (!page)
1248 goto uninit;
1249
Joerg Roedelf65c2292008-02-13 18:58:46 +01001250 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1251 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001252 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001253
1254 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1255 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001256 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001257
Alexander Grafb286d5d2008-11-25 20:17:05 +01001258 hsave_page = alloc_page(GFP_KERNEL);
1259 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001260 goto free_page3;
1261
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001262 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001263
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001264 svm->msrpm = page_address(msrpm_pages);
1265 svm_vcpu_init_msrpm(svm->msrpm);
1266
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001267 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001268 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001269
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001270 svm->vmcb = page_address(page);
1271 clear_page(svm->vmcb);
1272 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1273 svm->asid_generation = 0;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001274 init_vmcb(svm, false);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001276 svm_init_osvw(&svm->vcpu);
1277
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001278 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001279
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001280free_page3:
1281 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1282free_page2:
1283 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1284free_page1:
1285 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001286uninit:
1287 kvm_vcpu_uninit(&svm->vcpu);
1288free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001289 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001290out:
1291 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292}
1293
1294static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1295{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001296 struct vcpu_svm *svm = to_svm(vcpu);
1297
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001298 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001299 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001300 __free_page(virt_to_page(svm->nested.hsave));
1301 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001302 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001303 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304}
1305
Avi Kivity15ad7142007-07-11 18:17:21 +03001306static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001308 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001309 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001310
Avi Kivity0cc50642007-03-25 12:07:27 +02001311 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001312 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001313 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001314 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001315
Avi Kivity82ca2d12010-10-21 12:20:34 +02001316#ifdef CONFIG_X86_64
1317 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1318#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001319 savesegment(fs, svm->host.fs);
1320 savesegment(gs, svm->host.gs);
1321 svm->host.ldt = kvm_read_ldt();
1322
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001323 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001324 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001325
1326 if (static_cpu_has(X86_FEATURE_TSCRATEMSR) &&
Christoph Lameter89cbc762014-08-17 12:30:40 -05001327 svm->tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1328 __this_cpu_write(current_tsc_ratio, svm->tsc_ratio);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001329 wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_ratio);
1330 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331}
1332
1333static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1334{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001335 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001336 int i;
1337
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001338 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001339 kvm_load_ldt(svm->host.ldt);
1340#ifdef CONFIG_X86_64
1341 loadsegment(fs, svm->host.fs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001342 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001343 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001344#else
Avi Kivity831ca602011-03-08 16:09:51 +02001345#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001346 loadsegment(gs, svm->host.gs);
1347#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001348#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001349 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001350 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351}
1352
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1354{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001355 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356}
1357
1358static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1359{
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001360 /*
1361 * Any change of EFLAGS.VM is accompained by a reload of SS
1362 * (caused by either a task switch or an inter-privilege IRET),
1363 * so we do not need to update the CPL here.
1364 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001365 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366}
1367
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001368static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1369{
1370 switch (reg) {
1371 case VCPU_EXREG_PDPTR:
1372 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001373 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001374 break;
1375 default:
1376 BUG();
1377 }
1378}
1379
Alexander Graff0b85052008-11-25 20:17:01 +01001380static void svm_set_vintr(struct vcpu_svm *svm)
1381{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001382 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001383}
1384
1385static void svm_clear_vintr(struct vcpu_svm *svm)
1386{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001387 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001388}
1389
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1391{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001392 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393
1394 switch (seg) {
1395 case VCPU_SREG_CS: return &save->cs;
1396 case VCPU_SREG_DS: return &save->ds;
1397 case VCPU_SREG_ES: return &save->es;
1398 case VCPU_SREG_FS: return &save->fs;
1399 case VCPU_SREG_GS: return &save->gs;
1400 case VCPU_SREG_SS: return &save->ss;
1401 case VCPU_SREG_TR: return &save->tr;
1402 case VCPU_SREG_LDTR: return &save->ldtr;
1403 }
1404 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001405 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406}
1407
1408static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1409{
1410 struct vmcb_seg *s = svm_seg(vcpu, seg);
1411
1412 return s->base;
1413}
1414
1415static void svm_get_segment(struct kvm_vcpu *vcpu,
1416 struct kvm_segment *var, int seg)
1417{
1418 struct vmcb_seg *s = svm_seg(vcpu, seg);
1419
1420 var->base = s->base;
1421 var->limit = s->limit;
1422 var->selector = s->selector;
1423 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1424 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1425 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1426 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1427 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1428 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1429 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301430
1431 /*
1432 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1433 * However, the SVM spec states that the G bit is not observed by the
1434 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1435 * So let's synthesize a legal G bit for all segments, this helps
1436 * running KVM nested. It also helps cross-vendor migration, because
1437 * Intel's vmentry has a check on the 'G' bit.
1438 */
1439 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001440
Joerg Roedele0231712010-02-24 18:59:10 +01001441 /*
1442 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001443 * for cross vendor migration purposes by "not present"
1444 */
1445 var->unusable = !var->present || (var->type == 0);
1446
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001447 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001448 case VCPU_SREG_TR:
1449 /*
1450 * Work around a bug where the busy flag in the tr selector
1451 * isn't exposed
1452 */
Amit Shahc0d09822008-10-27 09:04:18 +00001453 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001454 break;
1455 case VCPU_SREG_DS:
1456 case VCPU_SREG_ES:
1457 case VCPU_SREG_FS:
1458 case VCPU_SREG_GS:
1459 /*
1460 * The accessed bit must always be set in the segment
1461 * descriptor cache, although it can be cleared in the
1462 * descriptor, the cached bit always remains at 1. Since
1463 * Intel has a check on this, set it here to support
1464 * cross-vendor migration.
1465 */
1466 if (!var->unusable)
1467 var->type |= 0x1;
1468 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001469 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001470 /*
1471 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001472 * descriptor is left as 1, although the whole segment has
1473 * been made unusable. Clear it here to pass an Intel VMX
1474 * entry check when cross vendor migrating.
1475 */
1476 if (var->unusable)
1477 var->db = 0;
Jan Kiszka33b458d2014-06-29 17:12:43 +02001478 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02001479 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001480 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481}
1482
Izik Eidus2e4d2652008-03-24 19:38:34 +02001483static int svm_get_cpl(struct kvm_vcpu *vcpu)
1484{
1485 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1486
1487 return save->cpl;
1488}
1489
Gleb Natapov89a27f42010-02-16 10:51:48 +02001490static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001492 struct vcpu_svm *svm = to_svm(vcpu);
1493
Gleb Natapov89a27f42010-02-16 10:51:48 +02001494 dt->size = svm->vmcb->save.idtr.limit;
1495 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496}
1497
Gleb Natapov89a27f42010-02-16 10:51:48 +02001498static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001500 struct vcpu_svm *svm = to_svm(vcpu);
1501
Gleb Natapov89a27f42010-02-16 10:51:48 +02001502 svm->vmcb->save.idtr.limit = dt->size;
1503 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001504 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505}
1506
Gleb Natapov89a27f42010-02-16 10:51:48 +02001507static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001509 struct vcpu_svm *svm = to_svm(vcpu);
1510
Gleb Natapov89a27f42010-02-16 10:51:48 +02001511 dt->size = svm->vmcb->save.gdtr.limit;
1512 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513}
1514
Gleb Natapov89a27f42010-02-16 10:51:48 +02001515static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001517 struct vcpu_svm *svm = to_svm(vcpu);
1518
Gleb Natapov89a27f42010-02-16 10:51:48 +02001519 svm->vmcb->save.gdtr.limit = dt->size;
1520 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001521 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522}
1523
Avi Kivitye8467fd2009-12-29 18:43:06 +02001524static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1525{
1526}
1527
Avi Kivityaff48ba2010-12-05 18:56:11 +02001528static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1529{
1530}
1531
Anthony Liguori25c4c272007-04-27 09:29:21 +03001532static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001533{
1534}
1535
Avi Kivityd2251572010-01-06 10:55:27 +02001536static void update_cr0_intercept(struct vcpu_svm *svm)
1537{
1538 ulong gcr0 = svm->vcpu.arch.cr0;
1539 u64 *hcr0 = &svm->vmcb->save.cr0;
1540
1541 if (!svm->vcpu.fpu_active)
1542 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1543 else
1544 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1545 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1546
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001547 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001548
1549 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001550 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1551 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001552 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001553 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1554 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001555 }
1556}
1557
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1559{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001560 struct vcpu_svm *svm = to_svm(vcpu);
1561
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001562#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001563 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001564 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001565 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001566 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 }
1568
Mike Dayd77c26f2007-10-08 09:02:08 -04001569 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001570 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001571 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572 }
1573 }
1574#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001575 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001576
1577 if (!npt_enabled)
1578 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001579
1580 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001581 cr0 |= X86_CR0_TS;
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02001582 /*
1583 * re-enable caching here because the QEMU bios
1584 * does not do it - this results in some delay at
1585 * reboot
1586 */
1587 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
1588 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001589 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001590 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001591 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592}
1593
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001594static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001596 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001597 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1598
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001599 if (cr4 & X86_CR4_VMXE)
1600 return 1;
1601
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001602 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001603 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001604
Joerg Roedelec077262008-04-09 14:15:28 +02001605 vcpu->arch.cr4 = cr4;
1606 if (!npt_enabled)
1607 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001608 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001609 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001610 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001611 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612}
1613
1614static void svm_set_segment(struct kvm_vcpu *vcpu,
1615 struct kvm_segment *var, int seg)
1616{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001617 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618 struct vmcb_seg *s = svm_seg(vcpu, seg);
1619
1620 s->base = var->base;
1621 s->limit = var->limit;
1622 s->selector = var->selector;
1623 if (var->unusable)
1624 s->attrib = 0;
1625 else {
1626 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1627 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1628 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1629 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1630 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1631 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1632 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1633 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1634 }
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001635
1636 /*
1637 * This is always accurate, except if SYSRET returned to a segment
1638 * with SS.DPL != 3. Intel does not have this quirk, and always
1639 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1640 * would entail passing the CPL to userspace and back.
1641 */
1642 if (seg == VCPU_SREG_SS)
1643 svm->vmcb->save.cpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644
Joerg Roedel060d0c92010-12-03 11:45:57 +01001645 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646}
1647
Jan Kiszkac8639012012-09-21 05:42:55 +02001648static void update_db_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001650 struct vcpu_svm *svm = to_svm(vcpu);
1651
Joerg Roedel18c918c2010-11-30 18:03:59 +01001652 clr_exception_intercept(svm, DB_VECTOR);
1653 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001654
Jan Kiszka6be7d302009-10-18 13:24:54 +02001655 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001656 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001657
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001658 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1659 if (vcpu->guest_debug &
1660 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001661 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001662 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001663 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001664 } else
1665 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001666}
1667
Tejun Heo0fe1e002009-10-29 22:34:14 +09001668static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001670 if (sd->next_asid > sd->max_asid) {
1671 ++sd->asid_generation;
1672 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001673 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 }
1675
Tejun Heo0fe1e002009-10-29 22:34:14 +09001676 svm->asid_generation = sd->asid_generation;
1677 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001678
1679 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680}
1681
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001682static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
1683{
1684 return to_svm(vcpu)->vmcb->save.dr6;
1685}
1686
1687static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
1688{
1689 struct vcpu_svm *svm = to_svm(vcpu);
1690
1691 svm->vmcb->save.dr6 = value;
1692 mark_dirty(svm->vmcb, VMCB_DR);
1693}
1694
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001695static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1696{
1697 struct vcpu_svm *svm = to_svm(vcpu);
1698
1699 get_debugreg(vcpu->arch.db[0], 0);
1700 get_debugreg(vcpu->arch.db[1], 1);
1701 get_debugreg(vcpu->arch.db[2], 2);
1702 get_debugreg(vcpu->arch.db[3], 3);
1703 vcpu->arch.dr6 = svm_get_dr6(vcpu);
1704 vcpu->arch.dr7 = svm->vmcb->save.dr7;
1705
1706 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1707 set_dr_intercepts(svm);
1708}
1709
Gleb Natapov020df072010-04-13 10:05:23 +03001710static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001712 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001713
Gleb Natapov020df072010-04-13 10:05:23 +03001714 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01001715 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001716}
1717
Avi Kivity851ba692009-08-24 11:10:17 +03001718static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719{
Gleb Natapov631bc482010-10-14 11:22:52 +02001720 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001722 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723
Gleb Natapov631bc482010-10-14 11:22:52 +02001724 switch (svm->apf_reason) {
1725 default:
1726 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001727
Gleb Natapov631bc482010-10-14 11:22:52 +02001728 trace_kvm_page_fault(fault_address, error_code);
1729 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1730 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01001731 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1732 svm->vmcb->control.insn_bytes,
1733 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02001734 break;
1735 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1736 svm->apf_reason = 0;
1737 local_irq_disable();
1738 kvm_async_pf_task_wait(fault_address);
1739 local_irq_enable();
1740 break;
1741 case KVM_PV_REASON_PAGE_READY:
1742 svm->apf_reason = 0;
1743 local_irq_disable();
1744 kvm_async_pf_task_wake(fault_address);
1745 local_irq_enable();
1746 break;
1747 }
1748 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001749}
1750
Avi Kivity851ba692009-08-24 11:10:17 +03001751static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001752{
Avi Kivity851ba692009-08-24 11:10:17 +03001753 struct kvm_run *kvm_run = svm->vcpu.run;
1754
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001755 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001756 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001757 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001758 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1759 return 1;
1760 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001761
Jan Kiszka6be7d302009-10-18 13:24:54 +02001762 if (svm->nmi_singlestep) {
1763 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001764 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1765 svm->vmcb->save.rflags &=
1766 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkac8639012012-09-21 05:42:55 +02001767 update_db_bp_intercept(&svm->vcpu);
Gleb Natapov44c11432009-05-11 13:35:52 +03001768 }
1769
1770 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001771 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001772 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1773 kvm_run->debug.arch.pc =
1774 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1775 kvm_run->debug.arch.exception = DB_VECTOR;
1776 return 0;
1777 }
1778
1779 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001780}
1781
Avi Kivity851ba692009-08-24 11:10:17 +03001782static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001783{
Avi Kivity851ba692009-08-24 11:10:17 +03001784 struct kvm_run *kvm_run = svm->vcpu.run;
1785
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001786 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1787 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1788 kvm_run->debug.arch.exception = BP_VECTOR;
1789 return 0;
1790}
1791
Avi Kivity851ba692009-08-24 11:10:17 +03001792static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001793{
1794 int er;
1795
Andre Przywara51d8b662010-12-21 11:12:02 +01001796 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001797 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001798 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001799 return 1;
1800}
1801
Avi Kivity6b52d182010-01-21 15:31:47 +02001802static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001803{
Avi Kivity6b52d182010-01-21 15:31:47 +02001804 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001805
Joerg Roedel18c918c2010-11-30 18:03:59 +01001806 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001807
Rusty Russelle756fc62007-07-30 20:07:08 +10001808 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001809 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001810}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001811
Avi Kivity6b52d182010-01-21 15:31:47 +02001812static int nm_interception(struct vcpu_svm *svm)
1813{
1814 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001815 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001816}
1817
Joerg Roedel67ec6602010-05-17 14:43:35 +02001818static bool is_erratum_383(void)
1819{
1820 int err, i;
1821 u64 value;
1822
1823 if (!erratum_383_found)
1824 return false;
1825
1826 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1827 if (err)
1828 return false;
1829
1830 /* Bit 62 may or may not be set for this mce */
1831 value &= ~(1ULL << 62);
1832
1833 if (value != 0xb600000000010015ULL)
1834 return false;
1835
1836 /* Clear MCi_STATUS registers */
1837 for (i = 0; i < 6; ++i)
1838 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1839
1840 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1841 if (!err) {
1842 u32 low, high;
1843
1844 value &= ~(1ULL << 2);
1845 low = lower_32_bits(value);
1846 high = upper_32_bits(value);
1847
1848 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1849 }
1850
1851 /* Flush tlb to evict multi-match entries */
1852 __flush_tlb_all();
1853
1854 return true;
1855}
1856
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001857static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001858{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001859 if (is_erratum_383()) {
1860 /*
1861 * Erratum 383 triggered. Guest state is corrupt so kill the
1862 * guest.
1863 */
1864 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1865
Avi Kivitya8eeb042010-05-10 12:34:53 +03001866 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001867
1868 return;
1869 }
1870
Joerg Roedel53371b52008-04-09 14:15:30 +02001871 /*
1872 * On an #MC intercept the MCE handler is not called automatically in
1873 * the host. So do it by hand here.
1874 */
1875 asm volatile (
1876 "int $0x12\n");
1877 /* not sure if we ever come back to this point */
1878
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001879 return;
1880}
1881
1882static int mc_interception(struct vcpu_svm *svm)
1883{
Joerg Roedel53371b52008-04-09 14:15:30 +02001884 return 1;
1885}
1886
Avi Kivity851ba692009-08-24 11:10:17 +03001887static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001888{
Avi Kivity851ba692009-08-24 11:10:17 +03001889 struct kvm_run *kvm_run = svm->vcpu.run;
1890
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001891 /*
1892 * VMCB is undefined after a SHUTDOWN intercept
1893 * so reinitialize it.
1894 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001895 clear_page(svm->vmcb);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001896 init_vmcb(svm, false);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001897
1898 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1899 return 0;
1900}
1901
Avi Kivity851ba692009-08-24 11:10:17 +03001902static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001904 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001905 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001906 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001907 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001908
Rusty Russelle756fc62007-07-30 20:07:08 +10001909 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001910 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001911 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001912 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01001913 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001914
Avi Kivity039576c2007-03-20 12:46:50 +02001915 port = io_info >> 16;
1916 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001917 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001918 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001919
1920 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921}
1922
Avi Kivity851ba692009-08-24 11:10:17 +03001923static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001924{
1925 return 1;
1926}
1927
Avi Kivity851ba692009-08-24 11:10:17 +03001928static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001929{
1930 ++svm->vcpu.stat.irq_exits;
1931 return 1;
1932}
1933
Avi Kivity851ba692009-08-24 11:10:17 +03001934static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935{
1936 return 1;
1937}
1938
Avi Kivity851ba692009-08-24 11:10:17 +03001939static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001941 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001942 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943}
1944
Avi Kivity851ba692009-08-24 11:10:17 +03001945static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001946{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001947 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001948 kvm_emulate_hypercall(&svm->vcpu);
1949 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001950}
1951
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001952static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1953{
1954 struct vcpu_svm *svm = to_svm(vcpu);
1955
1956 return svm->nested.nested_cr3;
1957}
1958
Avi Kivitye4e517b2011-07-28 11:36:17 +03001959static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
1960{
1961 struct vcpu_svm *svm = to_svm(vcpu);
1962 u64 cr3 = svm->nested.nested_cr3;
1963 u64 pdpte;
1964 int ret;
1965
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001966 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
1967 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03001968 if (ret)
1969 return 0;
1970 return pdpte;
1971}
1972
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001973static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1974 unsigned long root)
1975{
1976 struct vcpu_svm *svm = to_svm(vcpu);
1977
1978 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01001979 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001980 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001981}
1982
Avi Kivity6389ee92010-11-29 16:12:30 +02001983static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1984 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001985{
1986 struct vcpu_svm *svm = to_svm(vcpu);
1987
Paolo Bonzini5e352512014-09-02 13:18:37 +02001988 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
1989 /*
1990 * TODO: track the cause of the nested page fault, and
1991 * correctly fill in the high bits of exit_info_1.
1992 */
1993 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1994 svm->vmcb->control.exit_code_hi = 0;
1995 svm->vmcb->control.exit_info_1 = (1ULL << 32);
1996 svm->vmcb->control.exit_info_2 = fault->address;
1997 }
1998
1999 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
2000 svm->vmcb->control.exit_info_1 |= fault->error_code;
2001
2002 /*
2003 * The present bit is always zero for page structure faults on real
2004 * hardware.
2005 */
2006 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2007 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002008
2009 nested_svm_vmexit(svm);
2010}
2011
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002012static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002013{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002014 WARN_ON(mmu_is_nested(vcpu));
2015 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002016 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2017 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002018 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002019 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
2020 vcpu->arch.mmu.shadow_root_level = get_npt_level();
Xiao Guangrongc258b622015-08-05 12:04:24 +08002021 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002022 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002023}
2024
2025static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2026{
2027 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2028}
2029
Alexander Grafc0725422008-11-25 20:17:03 +01002030static int nested_svm_check_permissions(struct vcpu_svm *svm)
2031{
Avi Kivityf6801df2010-01-21 15:31:50 +02002032 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01002033 || !is_paging(&svm->vcpu)) {
2034 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2035 return 1;
2036 }
2037
2038 if (svm->vmcb->save.cpl) {
2039 kvm_inject_gp(&svm->vcpu, 0);
2040 return 1;
2041 }
2042
2043 return 0;
2044}
2045
Alexander Grafcf74a782008-11-25 20:17:08 +01002046static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2047 bool has_error_code, u32 error_code)
2048{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002049 int vmexit;
2050
Joerg Roedel20307532010-11-29 17:51:48 +01002051 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002052 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002053
Joerg Roedel0295ad72009-08-07 11:49:37 +02002054 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2055 svm->vmcb->control.exit_code_hi = 0;
2056 svm->vmcb->control.exit_info_1 = error_code;
2057 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
2058
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002059 vmexit = nested_svm_intercept(svm);
2060 if (vmexit == NESTED_EXIT_DONE)
2061 svm->nested.exit_required = true;
2062
2063 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002064}
2065
Joerg Roedel8fe54652010-02-19 16:23:01 +01002066/* This function returns true if it is save to enable the irq window */
2067static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002068{
Joerg Roedel20307532010-11-29 17:51:48 +01002069 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002070 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002071
Joerg Roedel26666952009-08-07 11:49:46 +02002072 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002073 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002074
Joerg Roedel26666952009-08-07 11:49:46 +02002075 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002076 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002077
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002078 /*
2079 * if vmexit was already requested (by intercepted exception
2080 * for instance) do not overwrite it with "external interrupt"
2081 * vmexit.
2082 */
2083 if (svm->nested.exit_required)
2084 return false;
2085
Joerg Roedel197717d2010-02-24 18:59:19 +01002086 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2087 svm->vmcb->control.exit_info_1 = 0;
2088 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002089
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002090 if (svm->nested.intercept & 1ULL) {
2091 /*
2092 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002093 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002094 * #vmexit emulation might sleep. Only signal request for
2095 * the #vmexit here.
2096 */
2097 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002098 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002099 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002100 }
2101
Joerg Roedel8fe54652010-02-19 16:23:01 +01002102 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002103}
2104
Joerg Roedel887f5002010-02-24 18:59:12 +01002105/* This function returns true if it is save to enable the nmi window */
2106static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2107{
Joerg Roedel20307532010-11-29 17:51:48 +01002108 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002109 return true;
2110
2111 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2112 return true;
2113
2114 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2115 svm->nested.exit_required = true;
2116
2117 return false;
2118}
2119
Joerg Roedel7597f122010-02-19 16:23:00 +01002120static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002121{
2122 struct page *page;
2123
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002124 might_sleep();
2125
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002126 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002127 if (is_error_page(page))
2128 goto error;
2129
Joerg Roedel7597f122010-02-19 16:23:00 +01002130 *_page = page;
2131
2132 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002133
2134error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002135 kvm_inject_gp(&svm->vcpu, 0);
2136
2137 return NULL;
2138}
2139
Joerg Roedel7597f122010-02-19 16:23:00 +01002140static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002141{
Joerg Roedel7597f122010-02-19 16:23:00 +01002142 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002143 kvm_release_page_dirty(page);
2144}
2145
Joerg Roedelce2ac082010-03-01 15:34:39 +01002146static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002147{
Jan Kiszka9bf41832014-06-30 10:54:17 +02002148 unsigned port, size, iopm_len;
2149 u16 val, mask;
2150 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002151 u64 gpa;
2152
2153 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2154 return NESTED_EXIT_HOST;
2155
2156 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02002157 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2158 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002159 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02002160 start_bit = port % 8;
2161 iopm_len = (start_bit + size > 8) ? 2 : 1;
2162 mask = (0xf >> (4 - size)) << start_bit;
2163 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002164
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002165 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02002166 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002167
Jan Kiszka9bf41832014-06-30 10:54:17 +02002168 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002169}
2170
Joerg Roedeld2477822010-03-01 15:34:34 +01002171static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002172{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002173 u32 offset, msr, value;
2174 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002175
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002176 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002177 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002178
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002179 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2180 offset = svm_msrpm_offset(msr);
2181 write = svm->vmcb->control.exit_info_1 & 1;
2182 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002183
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002184 if (offset == MSR_INVALID)
2185 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002186
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002187 /* Offset is in 32 bit units but need in 8 bit units */
2188 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002189
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002190 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002191 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002192
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002193 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002194}
2195
Joerg Roedel410e4d52009-08-07 11:49:44 +02002196static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002197{
Alexander Grafcf74a782008-11-25 20:17:08 +01002198 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002199
Joerg Roedel410e4d52009-08-07 11:49:44 +02002200 switch (exit_code) {
2201 case SVM_EXIT_INTR:
2202 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002203 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002204 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002205 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002206 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002207 if (npt_enabled)
2208 return NESTED_EXIT_HOST;
2209 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002210 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002211 /* When we're shadowing, trap PFs, but not async PF */
2212 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002213 return NESTED_EXIT_HOST;
2214 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01002215 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
2216 nm_interception(svm);
2217 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002218 default:
2219 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002220 }
2221
Joerg Roedel410e4d52009-08-07 11:49:44 +02002222 return NESTED_EXIT_CONTINUE;
2223}
2224
2225/*
2226 * If this function returns true, this #vmexit was already handled
2227 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002228static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002229{
2230 u32 exit_code = svm->vmcb->control.exit_code;
2231 int vmexit = NESTED_EXIT_HOST;
2232
Alexander Grafcf74a782008-11-25 20:17:08 +01002233 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002234 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002235 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002236 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002237 case SVM_EXIT_IOIO:
2238 vmexit = nested_svm_intercept_ioio(svm);
2239 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002240 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2241 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2242 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002243 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002244 break;
2245 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002246 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2247 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2248 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002249 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002250 break;
2251 }
2252 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2253 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002254 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002255 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002256 /* async page fault always cause vmexit */
2257 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2258 svm->apf_reason != 0)
2259 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002260 break;
2261 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002262 case SVM_EXIT_ERR: {
2263 vmexit = NESTED_EXIT_DONE;
2264 break;
2265 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002266 default: {
2267 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002268 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002269 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002270 }
2271 }
2272
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002273 return vmexit;
2274}
2275
2276static int nested_svm_exit_handled(struct vcpu_svm *svm)
2277{
2278 int vmexit;
2279
2280 vmexit = nested_svm_intercept(svm);
2281
2282 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002283 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002284
2285 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002286}
2287
Joerg Roedel0460a972009-08-07 11:49:31 +02002288static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2289{
2290 struct vmcb_control_area *dst = &dst_vmcb->control;
2291 struct vmcb_control_area *from = &from_vmcb->control;
2292
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002293 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002294 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002295 dst->intercept_exceptions = from->intercept_exceptions;
2296 dst->intercept = from->intercept;
2297 dst->iopm_base_pa = from->iopm_base_pa;
2298 dst->msrpm_base_pa = from->msrpm_base_pa;
2299 dst->tsc_offset = from->tsc_offset;
2300 dst->asid = from->asid;
2301 dst->tlb_ctl = from->tlb_ctl;
2302 dst->int_ctl = from->int_ctl;
2303 dst->int_vector = from->int_vector;
2304 dst->int_state = from->int_state;
2305 dst->exit_code = from->exit_code;
2306 dst->exit_code_hi = from->exit_code_hi;
2307 dst->exit_info_1 = from->exit_info_1;
2308 dst->exit_info_2 = from->exit_info_2;
2309 dst->exit_int_info = from->exit_int_info;
2310 dst->exit_int_info_err = from->exit_int_info_err;
2311 dst->nested_ctl = from->nested_ctl;
2312 dst->event_inj = from->event_inj;
2313 dst->event_inj_err = from->event_inj_err;
2314 dst->nested_cr3 = from->nested_cr3;
2315 dst->lbr_ctl = from->lbr_ctl;
2316}
2317
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002318static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002319{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002320 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002321 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002322 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002323 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002324
Joerg Roedel17897f32009-10-09 16:08:29 +02002325 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2326 vmcb->control.exit_info_1,
2327 vmcb->control.exit_info_2,
2328 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002329 vmcb->control.exit_int_info_err,
2330 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002331
Joerg Roedel7597f122010-02-19 16:23:00 +01002332 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002333 if (!nested_vmcb)
2334 return 1;
2335
Joerg Roedel20307532010-11-29 17:51:48 +01002336 /* Exit Guest-Mode */
2337 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002338 svm->nested.vmcb = 0;
2339
Alexander Grafcf74a782008-11-25 20:17:08 +01002340 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002341 disable_gif(svm);
2342
2343 nested_vmcb->save.es = vmcb->save.es;
2344 nested_vmcb->save.cs = vmcb->save.cs;
2345 nested_vmcb->save.ss = vmcb->save.ss;
2346 nested_vmcb->save.ds = vmcb->save.ds;
2347 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2348 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002349 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002350 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002351 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002352 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002353 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002354 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002355 nested_vmcb->save.rip = vmcb->save.rip;
2356 nested_vmcb->save.rsp = vmcb->save.rsp;
2357 nested_vmcb->save.rax = vmcb->save.rax;
2358 nested_vmcb->save.dr7 = vmcb->save.dr7;
2359 nested_vmcb->save.dr6 = vmcb->save.dr6;
2360 nested_vmcb->save.cpl = vmcb->save.cpl;
2361
2362 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2363 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2364 nested_vmcb->control.int_state = vmcb->control.int_state;
2365 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2366 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2367 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2368 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2369 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2370 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02002371
2372 if (svm->nrips_enabled)
2373 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002374
2375 /*
2376 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2377 * to make sure that we do not lose injected events. So check event_inj
2378 * here and copy it to exit_int_info if it is valid.
2379 * Exit_int_info and event_inj can't be both valid because the case
2380 * below only happens on a VMRUN instruction intercept which has
2381 * no valid exit_int_info set.
2382 */
2383 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2384 struct vmcb_control_area *nc = &nested_vmcb->control;
2385
2386 nc->exit_int_info = vmcb->control.event_inj;
2387 nc->exit_int_info_err = vmcb->control.event_inj_err;
2388 }
2389
Joerg Roedel33740e42009-08-07 11:49:29 +02002390 nested_vmcb->control.tlb_ctl = 0;
2391 nested_vmcb->control.event_inj = 0;
2392 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002393
2394 /* We always set V_INTR_MASKING and remember the old value in hflags */
2395 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2396 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2397
Alexander Grafcf74a782008-11-25 20:17:08 +01002398 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002399 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002400
Alexander Graf219b65d2009-06-15 15:21:25 +02002401 kvm_clear_exception_queue(&svm->vcpu);
2402 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002403
Joerg Roedel4b161842010-09-10 17:31:03 +02002404 svm->nested.nested_cr3 = 0;
2405
Alexander Grafcf74a782008-11-25 20:17:08 +01002406 /* Restore selected save entries */
2407 svm->vmcb->save.es = hsave->save.es;
2408 svm->vmcb->save.cs = hsave->save.cs;
2409 svm->vmcb->save.ss = hsave->save.ss;
2410 svm->vmcb->save.ds = hsave->save.ds;
2411 svm->vmcb->save.gdtr = hsave->save.gdtr;
2412 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002413 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002414 svm_set_efer(&svm->vcpu, hsave->save.efer);
2415 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2416 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2417 if (npt_enabled) {
2418 svm->vmcb->save.cr3 = hsave->save.cr3;
2419 svm->vcpu.arch.cr3 = hsave->save.cr3;
2420 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002421 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002422 }
2423 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2424 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2425 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2426 svm->vmcb->save.dr7 = 0;
2427 svm->vmcb->save.cpl = 0;
2428 svm->vmcb->control.exit_int_info = 0;
2429
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002430 mark_all_dirty(svm->vmcb);
2431
Joerg Roedel7597f122010-02-19 16:23:00 +01002432 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002433
Joerg Roedel4b161842010-09-10 17:31:03 +02002434 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002435 kvm_mmu_reset_context(&svm->vcpu);
2436 kvm_mmu_load(&svm->vcpu);
2437
2438 return 0;
2439}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002440
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002441static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002442{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002443 /*
2444 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002445 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01002446 * the kvm msr permission bitmap may contain zero bits
2447 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002448 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002449
Joerg Roedel323c3d82010-03-01 15:34:37 +01002450 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2451 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002452
Joerg Roedel323c3d82010-03-01 15:34:37 +01002453 for (i = 0; i < MSRPM_OFFSETS; i++) {
2454 u32 value, p;
2455 u64 offset;
2456
2457 if (msrpm_offsets[i] == 0xffffffff)
2458 break;
2459
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002460 p = msrpm_offsets[i];
2461 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002462
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002463 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01002464 return false;
2465
2466 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2467 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002468
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002469 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002470
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002471 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002472}
2473
Joerg Roedel52c65a302010-08-02 16:46:44 +02002474static bool nested_vmcb_checks(struct vmcb *vmcb)
2475{
2476 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2477 return false;
2478
Joerg Roedeldbe77582010-08-02 16:46:45 +02002479 if (vmcb->control.asid == 0)
2480 return false;
2481
Joerg Roedel4b161842010-09-10 17:31:03 +02002482 if (vmcb->control.nested_ctl && !npt_enabled)
2483 return false;
2484
Joerg Roedel52c65a302010-08-02 16:46:44 +02002485 return true;
2486}
2487
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002488static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002489{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002490 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002491 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002492 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002493 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002494 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002495
Joerg Roedel06fc77722010-02-19 16:23:07 +01002496 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002497
Joerg Roedel7597f122010-02-19 16:23:00 +01002498 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002499 if (!nested_vmcb)
2500 return false;
2501
Joerg Roedel52c65a302010-08-02 16:46:44 +02002502 if (!nested_vmcb_checks(nested_vmcb)) {
2503 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2504 nested_vmcb->control.exit_code_hi = 0;
2505 nested_vmcb->control.exit_info_1 = 0;
2506 nested_vmcb->control.exit_info_2 = 0;
2507
2508 nested_svm_unmap(page);
2509
2510 return false;
2511 }
2512
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002513 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002514 nested_vmcb->save.rip,
2515 nested_vmcb->control.int_ctl,
2516 nested_vmcb->control.event_inj,
2517 nested_vmcb->control.nested_ctl);
2518
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002519 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2520 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002521 nested_vmcb->control.intercept_exceptions,
2522 nested_vmcb->control.intercept);
2523
Alexander Graf3d6368e2008-11-25 20:17:07 +01002524 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002525 kvm_clear_exception_queue(&svm->vcpu);
2526 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002527
Joerg Roedele0231712010-02-24 18:59:10 +01002528 /*
2529 * Save the old vmcb, so we don't need to pick what we save, but can
2530 * restore everything when a VMEXIT occurs
2531 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002532 hsave->save.es = vmcb->save.es;
2533 hsave->save.cs = vmcb->save.cs;
2534 hsave->save.ss = vmcb->save.ss;
2535 hsave->save.ds = vmcb->save.ds;
2536 hsave->save.gdtr = vmcb->save.gdtr;
2537 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002538 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002539 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002540 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002541 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002542 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002543 hsave->save.rsp = vmcb->save.rsp;
2544 hsave->save.rax = vmcb->save.rax;
2545 if (npt_enabled)
2546 hsave->save.cr3 = vmcb->save.cr3;
2547 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002548 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002549
Joerg Roedel0460a972009-08-07 11:49:31 +02002550 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002551
Avi Kivityf6e78472010-08-02 15:30:20 +03002552 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002553 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2554 else
2555 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2556
Joerg Roedel4b161842010-09-10 17:31:03 +02002557 if (nested_vmcb->control.nested_ctl) {
2558 kvm_mmu_unload(&svm->vcpu);
2559 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2560 nested_svm_init_mmu_context(&svm->vcpu);
2561 }
2562
Alexander Graf3d6368e2008-11-25 20:17:07 +01002563 /* Load the nested guest state */
2564 svm->vmcb->save.es = nested_vmcb->save.es;
2565 svm->vmcb->save.cs = nested_vmcb->save.cs;
2566 svm->vmcb->save.ss = nested_vmcb->save.ss;
2567 svm->vmcb->save.ds = nested_vmcb->save.ds;
2568 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2569 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002570 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002571 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2572 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2573 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2574 if (npt_enabled) {
2575 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2576 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002577 } else
Avi Kivity23902182010-06-10 17:02:16 +03002578 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002579
2580 /* Guest paging mode is active - reset mmu */
2581 kvm_mmu_reset_context(&svm->vcpu);
2582
Joerg Roedeldefbba52009-08-07 11:49:30 +02002583 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002584 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2585 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2586 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002587
Alexander Graf3d6368e2008-11-25 20:17:07 +01002588 /* In case we don't even reach vcpu_run, the fields are not updated */
2589 svm->vmcb->save.rax = nested_vmcb->save.rax;
2590 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2591 svm->vmcb->save.rip = nested_vmcb->save.rip;
2592 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2593 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2594 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2595
Joerg Roedelf7138532010-03-01 15:34:40 +01002596 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002597 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002598
Joerg Roedelaad42c62009-08-07 11:49:34 +02002599 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002600 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002601 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002602 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2603 svm->nested.intercept = nested_vmcb->control.intercept;
2604
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002605 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002606 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002607 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2608 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2609 else
2610 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2611
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002612 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2613 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002614 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2615 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002616 }
2617
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002618 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002619 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002620
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002621 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002622 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2623 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2624 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002625 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2626 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2627
Joerg Roedel7597f122010-02-19 16:23:00 +01002628 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002629
Joerg Roedel20307532010-11-29 17:51:48 +01002630 /* Enter Guest-Mode */
2631 enter_guest_mode(&svm->vcpu);
2632
Joerg Roedel384c6362010-11-30 18:03:56 +01002633 /*
2634 * Merge guest and host intercepts - must be called with vcpu in
2635 * guest-mode to take affect here
2636 */
2637 recalc_intercepts(svm);
2638
Joerg Roedel06fc77722010-02-19 16:23:07 +01002639 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002640
Joerg Roedel2af91942009-08-07 11:49:28 +02002641 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002642
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002643 mark_all_dirty(svm->vmcb);
2644
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002645 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002646}
2647
Joerg Roedel9966bf62009-08-07 11:49:40 +02002648static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002649{
2650 to_vmcb->save.fs = from_vmcb->save.fs;
2651 to_vmcb->save.gs = from_vmcb->save.gs;
2652 to_vmcb->save.tr = from_vmcb->save.tr;
2653 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2654 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2655 to_vmcb->save.star = from_vmcb->save.star;
2656 to_vmcb->save.lstar = from_vmcb->save.lstar;
2657 to_vmcb->save.cstar = from_vmcb->save.cstar;
2658 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2659 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2660 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2661 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002662}
2663
Avi Kivity851ba692009-08-24 11:10:17 +03002664static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002665{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002666 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002667 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002668
Alexander Graf55426752008-11-25 20:17:06 +01002669 if (nested_svm_check_permissions(svm))
2670 return 1;
2671
Joerg Roedel7597f122010-02-19 16:23:00 +01002672 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002673 if (!nested_vmcb)
2674 return 1;
2675
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002676 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2677 skip_emulated_instruction(&svm->vcpu);
2678
Joerg Roedel9966bf62009-08-07 11:49:40 +02002679 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002680 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002681
2682 return 1;
2683}
2684
Avi Kivity851ba692009-08-24 11:10:17 +03002685static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002686{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002687 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002688 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002689
Alexander Graf55426752008-11-25 20:17:06 +01002690 if (nested_svm_check_permissions(svm))
2691 return 1;
2692
Joerg Roedel7597f122010-02-19 16:23:00 +01002693 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002694 if (!nested_vmcb)
2695 return 1;
2696
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002697 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2698 skip_emulated_instruction(&svm->vcpu);
2699
Joerg Roedel9966bf62009-08-07 11:49:40 +02002700 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002701 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002702
2703 return 1;
2704}
2705
Avi Kivity851ba692009-08-24 11:10:17 +03002706static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002707{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002708 if (nested_svm_check_permissions(svm))
2709 return 1;
2710
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002711 /* Save rip after vmrun instruction */
2712 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002713
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002714 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002715 return 1;
2716
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002717 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002718 goto failed;
2719
2720 return 1;
2721
2722failed:
2723
2724 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2725 svm->vmcb->control.exit_code_hi = 0;
2726 svm->vmcb->control.exit_info_1 = 0;
2727 svm->vmcb->control.exit_info_2 = 0;
2728
2729 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002730
2731 return 1;
2732}
2733
Avi Kivity851ba692009-08-24 11:10:17 +03002734static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002735{
2736 if (nested_svm_check_permissions(svm))
2737 return 1;
2738
2739 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2740 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002741 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002742
Joerg Roedel2af91942009-08-07 11:49:28 +02002743 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002744
2745 return 1;
2746}
2747
Avi Kivity851ba692009-08-24 11:10:17 +03002748static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002749{
2750 if (nested_svm_check_permissions(svm))
2751 return 1;
2752
2753 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2754 skip_emulated_instruction(&svm->vcpu);
2755
Joerg Roedel2af91942009-08-07 11:49:28 +02002756 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002757
2758 /* After a CLGI no interrupts should come */
2759 svm_clear_vintr(svm);
2760 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2761
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002762 mark_dirty(svm->vmcb, VMCB_INTR);
2763
Alexander Graf1371d902008-11-25 20:17:04 +01002764 return 1;
2765}
2766
Avi Kivity851ba692009-08-24 11:10:17 +03002767static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002768{
2769 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002770
David Kaplan668f1982015-02-20 16:02:10 -06002771 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
2772 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02002773
Alexander Grafff092382009-06-15 15:21:24 +02002774 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06002775 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02002776
2777 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2778 skip_emulated_instruction(&svm->vcpu);
2779 return 1;
2780}
2781
Joerg Roedel532a46b2009-10-09 16:08:32 +02002782static int skinit_interception(struct vcpu_svm *svm)
2783{
David Kaplan668f1982015-02-20 16:02:10 -06002784 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02002785
2786 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2787 return 1;
2788}
2789
David Kaplandab429a2015-03-02 13:43:37 -06002790static int wbinvd_interception(struct vcpu_svm *svm)
2791{
2792 kvm_emulate_wbinvd(&svm->vcpu);
2793 return 1;
2794}
2795
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002796static int xsetbv_interception(struct vcpu_svm *svm)
2797{
2798 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
2799 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
2800
2801 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
2802 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2803 skip_emulated_instruction(&svm->vcpu);
2804 }
2805
2806 return 1;
2807}
2808
Avi Kivity851ba692009-08-24 11:10:17 +03002809static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810{
Izik Eidus37817f22008-03-24 23:14:53 +02002811 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002812 int reason;
2813 int int_type = svm->vmcb->control.exit_int_info &
2814 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002815 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002816 uint32_t type =
2817 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2818 uint32_t idt_v =
2819 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002820 bool has_error_code = false;
2821 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002822
2823 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002824
Izik Eidus37817f22008-03-24 23:14:53 +02002825 if (svm->vmcb->control.exit_info_2 &
2826 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002827 reason = TASK_SWITCH_IRET;
2828 else if (svm->vmcb->control.exit_info_2 &
2829 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2830 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002831 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002832 reason = TASK_SWITCH_GATE;
2833 else
2834 reason = TASK_SWITCH_CALL;
2835
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002836 if (reason == TASK_SWITCH_GATE) {
2837 switch (type) {
2838 case SVM_EXITINTINFO_TYPE_NMI:
2839 svm->vcpu.arch.nmi_injected = false;
2840 break;
2841 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002842 if (svm->vmcb->control.exit_info_2 &
2843 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2844 has_error_code = true;
2845 error_code =
2846 (u32)svm->vmcb->control.exit_info_2;
2847 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002848 kvm_clear_exception_queue(&svm->vcpu);
2849 break;
2850 case SVM_EXITINTINFO_TYPE_INTR:
2851 kvm_clear_interrupt_queue(&svm->vcpu);
2852 break;
2853 default:
2854 break;
2855 }
2856 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002857
Gleb Natapov8317c292009-04-12 13:37:02 +03002858 if (reason != TASK_SWITCH_GATE ||
2859 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2860 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002861 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2862 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002863
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002864 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2865 int_vec = -1;
2866
2867 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03002868 has_error_code, error_code) == EMULATE_FAIL) {
2869 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2870 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2871 svm->vcpu.run->internal.ndata = 0;
2872 return 0;
2873 }
2874 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875}
2876
Avi Kivity851ba692009-08-24 11:10:17 +03002877static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002879 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002880 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002881 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882}
2883
Avi Kivity851ba692009-08-24 11:10:17 +03002884static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002885{
2886 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002887 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002888 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02002889 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01002890 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002891 return 1;
2892}
2893
Avi Kivity851ba692009-08-24 11:10:17 +03002894static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002895{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002896 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2897 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
2898
2899 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
2900 skip_emulated_instruction(&svm->vcpu);
2901 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002902}
2903
Avi Kivity851ba692009-08-24 11:10:17 +03002904static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905{
Andre Przywara51d8b662010-12-21 11:12:02 +01002906 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907}
2908
Avi Kivity332b56e2011-11-10 14:57:24 +02002909static int rdpmc_interception(struct vcpu_svm *svm)
2910{
2911 int err;
2912
2913 if (!static_cpu_has(X86_FEATURE_NRIPS))
2914 return emulate_on_interception(svm);
2915
2916 err = kvm_rdpmc(&svm->vcpu);
2917 kvm_complete_insn_gp(&svm->vcpu, err);
2918
2919 return 1;
2920}
2921
Xiubo Li52eb5a62015-03-13 17:39:45 +08002922static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
2923 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02002924{
2925 unsigned long cr0 = svm->vcpu.arch.cr0;
2926 bool ret = false;
2927 u64 intercept;
2928
2929 intercept = svm->nested.intercept;
2930
2931 if (!is_guest_mode(&svm->vcpu) ||
2932 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
2933 return false;
2934
2935 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2936 val &= ~SVM_CR0_SELECTIVE_MASK;
2937
2938 if (cr0 ^ val) {
2939 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2940 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2941 }
2942
2943 return ret;
2944}
2945
Andre Przywara7ff76d52010-12-21 11:12:04 +01002946#define CR_VALID (1ULL << 63)
2947
2948static int cr_interception(struct vcpu_svm *svm)
2949{
2950 int reg, cr;
2951 unsigned long val;
2952 int err;
2953
2954 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2955 return emulate_on_interception(svm);
2956
2957 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2958 return emulate_on_interception(svm);
2959
2960 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06002961 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
2962 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
2963 else
2964 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002965
2966 err = 0;
2967 if (cr >= 16) { /* mov to cr */
2968 cr -= 16;
2969 val = kvm_register_read(&svm->vcpu, reg);
2970 switch (cr) {
2971 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02002972 if (!check_selective_cr0_intercepted(svm, val))
2973 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002974 else
2975 return 1;
2976
Andre Przywara7ff76d52010-12-21 11:12:04 +01002977 break;
2978 case 3:
2979 err = kvm_set_cr3(&svm->vcpu, val);
2980 break;
2981 case 4:
2982 err = kvm_set_cr4(&svm->vcpu, val);
2983 break;
2984 case 8:
2985 err = kvm_set_cr8(&svm->vcpu, val);
2986 break;
2987 default:
2988 WARN(1, "unhandled write to CR%d", cr);
2989 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2990 return 1;
2991 }
2992 } else { /* mov from cr */
2993 switch (cr) {
2994 case 0:
2995 val = kvm_read_cr0(&svm->vcpu);
2996 break;
2997 case 2:
2998 val = svm->vcpu.arch.cr2;
2999 break;
3000 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003001 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003002 break;
3003 case 4:
3004 val = kvm_read_cr4(&svm->vcpu);
3005 break;
3006 case 8:
3007 val = kvm_get_cr8(&svm->vcpu);
3008 break;
3009 default:
3010 WARN(1, "unhandled read from CR%d", cr);
3011 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3012 return 1;
3013 }
3014 kvm_register_write(&svm->vcpu, reg, val);
3015 }
3016 kvm_complete_insn_gp(&svm->vcpu, err);
3017
3018 return 1;
3019}
3020
Andre Przywaracae37972010-12-21 11:12:05 +01003021static int dr_interception(struct vcpu_svm *svm)
3022{
3023 int reg, dr;
3024 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003025
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003026 if (svm->vcpu.guest_debug == 0) {
3027 /*
3028 * No more DR vmexits; force a reload of the debug registers
3029 * and reenter on this instruction. The next vmexit will
3030 * retrieve the full state of the debug registers.
3031 */
3032 clr_dr_intercepts(svm);
3033 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3034 return 1;
3035 }
3036
Andre Przywaracae37972010-12-21 11:12:05 +01003037 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3038 return emulate_on_interception(svm);
3039
3040 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3041 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3042
3043 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003044 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3045 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003046 val = kvm_register_read(&svm->vcpu, reg);
3047 kvm_set_dr(&svm->vcpu, dr - 16, val);
3048 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003049 if (!kvm_require_dr(&svm->vcpu, dr))
3050 return 1;
3051 kvm_get_dr(&svm->vcpu, dr, &val);
3052 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003053 }
3054
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01003055 skip_emulated_instruction(&svm->vcpu);
3056
Andre Przywaracae37972010-12-21 11:12:05 +01003057 return 1;
3058}
3059
Avi Kivity851ba692009-08-24 11:10:17 +03003060static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003061{
Avi Kivity851ba692009-08-24 11:10:17 +03003062 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003063 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003064
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003065 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3066 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003067 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003068 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003069 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003070 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003071 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003072 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3073 return 0;
3074}
3075
Paolo Bonzini48d89b92014-08-26 13:27:46 +02003076static u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003077{
3078 struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
3079 return vmcb->control.tsc_offset +
Marcelo Tosatti886b4702012-11-27 23:28:58 -02003080 svm_scale_tsc(vcpu, host_tsc);
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003081}
3082
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003083static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003085 struct vcpu_svm *svm = to_svm(vcpu);
3086
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003087 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303088 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003089 msr_info->data = svm->vmcb->control.tsc_offset +
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003090 svm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003091
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092 break;
3093 }
Brian Gerst8c065852010-07-17 09:03:26 -04003094 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003095 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003097#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003099 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100 break;
3101 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003102 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103 break;
3104 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003105 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106 break;
3107 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003108 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109 break;
3110#endif
3111 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003112 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113 break;
3114 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003115 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116 break;
3117 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003118 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003119 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003120 /*
3121 * Nobody will change the following 5 values in the VMCB so we can
3122 * safely return them on rdmsr. They will always be 0 until LBRV is
3123 * implemented.
3124 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003125 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003126 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01003127 break;
3128 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003129 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003130 break;
3131 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003132 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003133 break;
3134 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003135 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003136 break;
3137 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003138 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003139 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003140 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003141 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003142 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003143 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003144 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003145 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003146 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003147 msr_info->data = 0x01000065;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003148 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003150 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151 }
3152 return 0;
3153}
3154
Avi Kivity851ba692009-08-24 11:10:17 +03003155static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156{
David Kaplan668f1982015-02-20 16:02:10 -06003157 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003158 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003160 msr_info.index = ecx;
3161 msr_info.host_initiated = false;
3162 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02003163 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003164 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003165 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003166 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003167
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003168 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
3169 msr_info.data & 0xffffffff);
3170 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
3171 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003172 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003173 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 }
3175 return 1;
3176}
3177
Joerg Roedel4a810182010-02-24 18:59:15 +01003178static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3179{
3180 struct vcpu_svm *svm = to_svm(vcpu);
3181 int svm_dis, chg_mask;
3182
3183 if (data & ~SVM_VM_CR_VALID_MASK)
3184 return 1;
3185
3186 chg_mask = SVM_VM_CR_VALID_MASK;
3187
3188 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3189 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3190
3191 svm->nested.vm_cr_msr &= ~chg_mask;
3192 svm->nested.vm_cr_msr |= (data & chg_mask);
3193
3194 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3195
3196 /* check for svm_disable while efer.svme is set */
3197 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3198 return 1;
3199
3200 return 0;
3201}
3202
Will Auld8fe8ab42012-11-29 12:42:12 -08003203static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003205 struct vcpu_svm *svm = to_svm(vcpu);
3206
Will Auld8fe8ab42012-11-29 12:42:12 -08003207 u32 ecx = msr->index;
3208 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003210 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003211 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003212 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003213 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003214 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003216#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003218 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219 break;
3220 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003221 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222 break;
3223 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003224 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225 break;
3226 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003227 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228 break;
3229#endif
3230 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003231 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232 break;
3233 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003234 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003235 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236 break;
3237 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003238 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003239 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003240 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003241 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003242 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003243 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
3244 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003245 break;
3246 }
3247 if (data & DEBUGCTL_RESERVED_BITS)
3248 return 1;
3249
3250 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003251 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003252 if (data & (1ULL<<0))
3253 svm_enable_lbrv(svm);
3254 else
3255 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003256 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003257 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003258 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003259 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003260 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003261 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003262 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03003263 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003264 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08003266 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 }
3268 return 0;
3269}
3270
Avi Kivity851ba692009-08-24 11:10:17 +03003271static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272{
Will Auld8fe8ab42012-11-29 12:42:12 -08003273 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06003274 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3275 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003276
Will Auld8fe8ab42012-11-29 12:42:12 -08003277 msr.data = data;
3278 msr.index = ecx;
3279 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003280
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003281 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03003282 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02003283 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003284 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003285 } else {
3286 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10003287 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003288 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003289 return 1;
3290}
3291
Avi Kivity851ba692009-08-24 11:10:17 +03003292static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293{
Rusty Russelle756fc62007-07-30 20:07:08 +10003294 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003295 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003296 else
Avi Kivity851ba692009-08-24 11:10:17 +03003297 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003298}
3299
Avi Kivity851ba692009-08-24 11:10:17 +03003300static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003301{
Avi Kivity3842d132010-07-27 12:30:24 +03003302 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003303 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003304 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003305 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08003306 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003307 return 1;
3308}
3309
Mark Langsdorf565d0992009-10-06 14:25:02 -05003310static int pause_interception(struct vcpu_svm *svm)
3311{
3312 kvm_vcpu_on_spin(&(svm->vcpu));
3313 return 1;
3314}
3315
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003316static int nop_interception(struct vcpu_svm *svm)
3317{
3318 skip_emulated_instruction(&(svm->vcpu));
3319 return 1;
3320}
3321
3322static int monitor_interception(struct vcpu_svm *svm)
3323{
3324 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
3325 return nop_interception(svm);
3326}
3327
3328static int mwait_interception(struct vcpu_svm *svm)
3329{
3330 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
3331 return nop_interception(svm);
3332}
3333
Mathias Krause09941fb2012-08-30 01:30:20 +02003334static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003335 [SVM_EXIT_READ_CR0] = cr_interception,
3336 [SVM_EXIT_READ_CR3] = cr_interception,
3337 [SVM_EXIT_READ_CR4] = cr_interception,
3338 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06003339 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003340 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003341 [SVM_EXIT_WRITE_CR3] = cr_interception,
3342 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003343 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003344 [SVM_EXIT_READ_DR0] = dr_interception,
3345 [SVM_EXIT_READ_DR1] = dr_interception,
3346 [SVM_EXIT_READ_DR2] = dr_interception,
3347 [SVM_EXIT_READ_DR3] = dr_interception,
3348 [SVM_EXIT_READ_DR4] = dr_interception,
3349 [SVM_EXIT_READ_DR5] = dr_interception,
3350 [SVM_EXIT_READ_DR6] = dr_interception,
3351 [SVM_EXIT_READ_DR7] = dr_interception,
3352 [SVM_EXIT_WRITE_DR0] = dr_interception,
3353 [SVM_EXIT_WRITE_DR1] = dr_interception,
3354 [SVM_EXIT_WRITE_DR2] = dr_interception,
3355 [SVM_EXIT_WRITE_DR3] = dr_interception,
3356 [SVM_EXIT_WRITE_DR4] = dr_interception,
3357 [SVM_EXIT_WRITE_DR5] = dr_interception,
3358 [SVM_EXIT_WRITE_DR6] = dr_interception,
3359 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003360 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3361 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003362 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003363 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3364 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
3365 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
3366 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003367 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368 [SVM_EXIT_SMI] = nop_on_interception,
3369 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003370 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02003371 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003372 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003373 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003374 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003375 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003376 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003377 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003378 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003379 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380 [SVM_EXIT_MSR] = msr_interception,
3381 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003382 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003383 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003384 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003385 [SVM_EXIT_VMLOAD] = vmload_interception,
3386 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003387 [SVM_EXIT_STGI] = stgi_interception,
3388 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003389 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06003390 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003391 [SVM_EXIT_MONITOR] = monitor_interception,
3392 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003393 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003394 [SVM_EXIT_NPF] = pf_interception,
Paolo Bonzini64d60672015-05-07 11:36:11 +02003395 [SVM_EXIT_RSM] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003396};
3397
Joe Perchesae8cc052011-04-24 22:00:50 -07003398static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003399{
3400 struct vcpu_svm *svm = to_svm(vcpu);
3401 struct vmcb_control_area *control = &svm->vmcb->control;
3402 struct vmcb_save_area *save = &svm->vmcb->save;
3403
3404 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003405 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
3406 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
3407 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
3408 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
3409 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
3410 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
3411 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3412 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3413 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3414 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3415 pr_err("%-20s%d\n", "asid:", control->asid);
3416 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3417 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3418 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3419 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3420 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3421 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3422 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3423 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3424 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3425 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3426 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
3427 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3428 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3429 pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
3430 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003431 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003432 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3433 "es:",
3434 save->es.selector, save->es.attrib,
3435 save->es.limit, save->es.base);
3436 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3437 "cs:",
3438 save->cs.selector, save->cs.attrib,
3439 save->cs.limit, save->cs.base);
3440 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3441 "ss:",
3442 save->ss.selector, save->ss.attrib,
3443 save->ss.limit, save->ss.base);
3444 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3445 "ds:",
3446 save->ds.selector, save->ds.attrib,
3447 save->ds.limit, save->ds.base);
3448 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3449 "fs:",
3450 save->fs.selector, save->fs.attrib,
3451 save->fs.limit, save->fs.base);
3452 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3453 "gs:",
3454 save->gs.selector, save->gs.attrib,
3455 save->gs.limit, save->gs.base);
3456 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3457 "gdtr:",
3458 save->gdtr.selector, save->gdtr.attrib,
3459 save->gdtr.limit, save->gdtr.base);
3460 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3461 "ldtr:",
3462 save->ldtr.selector, save->ldtr.attrib,
3463 save->ldtr.limit, save->ldtr.base);
3464 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3465 "idtr:",
3466 save->idtr.selector, save->idtr.attrib,
3467 save->idtr.limit, save->idtr.base);
3468 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3469 "tr:",
3470 save->tr.selector, save->tr.attrib,
3471 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003472 pr_err("cpl: %d efer: %016llx\n",
3473 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003474 pr_err("%-15s %016llx %-13s %016llx\n",
3475 "cr0:", save->cr0, "cr2:", save->cr2);
3476 pr_err("%-15s %016llx %-13s %016llx\n",
3477 "cr3:", save->cr3, "cr4:", save->cr4);
3478 pr_err("%-15s %016llx %-13s %016llx\n",
3479 "dr6:", save->dr6, "dr7:", save->dr7);
3480 pr_err("%-15s %016llx %-13s %016llx\n",
3481 "rip:", save->rip, "rflags:", save->rflags);
3482 pr_err("%-15s %016llx %-13s %016llx\n",
3483 "rsp:", save->rsp, "rax:", save->rax);
3484 pr_err("%-15s %016llx %-13s %016llx\n",
3485 "star:", save->star, "lstar:", save->lstar);
3486 pr_err("%-15s %016llx %-13s %016llx\n",
3487 "cstar:", save->cstar, "sfmask:", save->sfmask);
3488 pr_err("%-15s %016llx %-13s %016llx\n",
3489 "kernel_gs_base:", save->kernel_gs_base,
3490 "sysenter_cs:", save->sysenter_cs);
3491 pr_err("%-15s %016llx %-13s %016llx\n",
3492 "sysenter_esp:", save->sysenter_esp,
3493 "sysenter_eip:", save->sysenter_eip);
3494 pr_err("%-15s %016llx %-13s %016llx\n",
3495 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3496 pr_err("%-15s %016llx %-13s %016llx\n",
3497 "br_from:", save->br_from, "br_to:", save->br_to);
3498 pr_err("%-15s %016llx %-13s %016llx\n",
3499 "excp_from:", save->last_excp_from,
3500 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003501}
3502
Avi Kivity586f9602010-11-18 13:09:54 +02003503static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3504{
3505 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3506
3507 *info1 = control->exit_info_1;
3508 *info2 = control->exit_info_2;
3509}
3510
Avi Kivity851ba692009-08-24 11:10:17 +03003511static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003512{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003513 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003514 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003515 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003517 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003518 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3519 if (npt_enabled)
3520 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003521
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003522 if (unlikely(svm->nested.exit_required)) {
3523 nested_svm_vmexit(svm);
3524 svm->nested.exit_required = false;
3525
3526 return 1;
3527 }
3528
Joerg Roedel20307532010-11-29 17:51:48 +01003529 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003530 int vmexit;
3531
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003532 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3533 svm->vmcb->control.exit_info_1,
3534 svm->vmcb->control.exit_info_2,
3535 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003536 svm->vmcb->control.exit_int_info_err,
3537 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003538
Joerg Roedel410e4d52009-08-07 11:49:44 +02003539 vmexit = nested_svm_exit_special(svm);
3540
3541 if (vmexit == NESTED_EXIT_CONTINUE)
3542 vmexit = nested_svm_exit_handled(svm);
3543
3544 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003545 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003546 }
3547
Joerg Roedela5c38322009-08-07 11:49:32 +02003548 svm_complete_interrupts(svm);
3549
Avi Kivity04d2cc72007-09-10 18:10:54 +03003550 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3551 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3552 kvm_run->fail_entry.hardware_entry_failure_reason
3553 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003554 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3555 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003556 return 0;
3557 }
3558
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003559 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003560 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003561 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3562 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02003563 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003564 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003565 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003566 exit_code);
3567
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003568 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003569 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04003570 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03003571 kvm_queue_exception(vcpu, UD_VECTOR);
3572 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003573 }
3574
Avi Kivity851ba692009-08-24 11:10:17 +03003575 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003576}
3577
3578static void reload_tss(struct kvm_vcpu *vcpu)
3579{
3580 int cpu = raw_smp_processor_id();
3581
Tejun Heo0fe1e002009-10-29 22:34:14 +09003582 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3583 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003584 load_TR_desc();
3585}
3586
Rusty Russelle756fc62007-07-30 20:07:08 +10003587static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003588{
3589 int cpu = raw_smp_processor_id();
3590
Tejun Heo0fe1e002009-10-29 22:34:14 +09003591 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003592
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003593 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003594 if (svm->asid_generation != sd->asid_generation)
3595 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003596}
3597
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003598static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3599{
3600 struct vcpu_svm *svm = to_svm(vcpu);
3601
3602 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3603 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003604 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003605 ++vcpu->stat.nmi_injections;
3606}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003607
Eddie Dong85f455f2007-07-06 12:20:49 +03003608static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003609{
3610 struct vmcb_control_area *control;
3611
Rusty Russelle756fc62007-07-30 20:07:08 +10003612 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003613 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003614 control->int_ctl &= ~V_INTR_PRIO_MASK;
3615 control->int_ctl |= V_IRQ_MASK |
3616 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003617 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003618}
3619
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003620static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003621{
3622 struct vcpu_svm *svm = to_svm(vcpu);
3623
Joerg Roedel2af91942009-08-07 11:49:28 +02003624 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003625
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003626 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3627 ++vcpu->stat.irq_injections;
3628
Alexander Graf219b65d2009-06-15 15:21:25 +02003629 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3630 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003631}
3632
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003633static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3634{
3635 struct vcpu_svm *svm = to_svm(vcpu);
3636
Joerg Roedel20307532010-11-29 17:51:48 +01003637 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003638 return;
3639
Radim Krčmář596f3142014-03-11 19:11:18 +01003640 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
3641
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003642 if (irr == -1)
3643 return;
3644
3645 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003646 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003647}
3648
Yang Zhang8d146952013-01-25 10:18:50 +08003649static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
3650{
3651 return;
3652}
3653
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02003654static int svm_cpu_uses_apicv(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08003655{
3656 return 0;
3657}
3658
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02003659static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08003660{
3661 return;
3662}
3663
Yang Zhanga20ed542013-04-11 19:25:15 +08003664static void svm_sync_pir_to_irr(struct kvm_vcpu *vcpu)
3665{
3666 return;
3667}
3668
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003669static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003670{
3671 struct vcpu_svm *svm = to_svm(vcpu);
3672 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003673 int ret;
3674 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3675 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3676 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3677
3678 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003679}
3680
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003681static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3682{
3683 struct vcpu_svm *svm = to_svm(vcpu);
3684
3685 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3686}
3687
3688static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3689{
3690 struct vcpu_svm *svm = to_svm(vcpu);
3691
3692 if (masked) {
3693 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003694 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003695 } else {
3696 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003697 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003698 }
3699}
3700
Gleb Natapov78646122009-03-23 12:12:11 +02003701static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3702{
3703 struct vcpu_svm *svm = to_svm(vcpu);
3704 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003705 int ret;
3706
3707 if (!gif_set(svm) ||
3708 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3709 return 0;
3710
Avi Kivityf6e78472010-08-02 15:30:20 +03003711 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003712
Joerg Roedel20307532010-11-29 17:51:48 +01003713 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003714 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3715
3716 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003717}
3718
Jan Kiszkac9a79532014-03-07 20:03:15 +01003719static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003720{
Alexander Graf219b65d2009-06-15 15:21:25 +02003721 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003722
Joerg Roedele0231712010-02-24 18:59:10 +01003723 /*
3724 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3725 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3726 * get that intercept, this function will be called again though and
3727 * we'll get the vintr intercept.
3728 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003729 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003730 svm_set_vintr(svm);
3731 svm_inject_irq(svm, 0x0);
3732 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003733}
3734
Jan Kiszkac9a79532014-03-07 20:03:15 +01003735static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003737 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003738
Gleb Natapov44c11432009-05-11 13:35:52 +03003739 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3740 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01003741 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03003742
Joerg Roedele0231712010-02-24 18:59:10 +01003743 /*
3744 * Something prevents NMI from been injected. Single step over possible
3745 * problem (IRET or exception injection or interrupt shadow)
3746 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003747 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003748 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkac8639012012-09-21 05:42:55 +02003749 update_db_bp_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003750}
3751
Izik Eiduscbc94022007-10-25 00:29:55 +02003752static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3753{
3754 return 0;
3755}
3756
Avi Kivityd9e368d2007-06-07 19:18:30 +03003757static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3758{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003759 struct vcpu_svm *svm = to_svm(vcpu);
3760
3761 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3762 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3763 else
3764 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003765}
3766
Avi Kivity04d2cc72007-09-10 18:10:54 +03003767static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3768{
3769}
3770
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003771static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3772{
3773 struct vcpu_svm *svm = to_svm(vcpu);
3774
Joerg Roedel20307532010-11-29 17:51:48 +01003775 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003776 return;
3777
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003778 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003779 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003780 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003781 }
3782}
3783
Joerg Roedel649d6862008-04-16 16:51:15 +02003784static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3785{
3786 struct vcpu_svm *svm = to_svm(vcpu);
3787 u64 cr8;
3788
Joerg Roedel20307532010-11-29 17:51:48 +01003789 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003790 return;
3791
Joerg Roedel649d6862008-04-16 16:51:15 +02003792 cr8 = kvm_get_cr8(vcpu);
3793 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3794 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3795}
3796
Gleb Natapov9222be12009-04-23 17:14:37 +03003797static void svm_complete_interrupts(struct vcpu_svm *svm)
3798{
3799 u8 vector;
3800 int type;
3801 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003802 unsigned int3_injected = svm->int3_injected;
3803
3804 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003805
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003806 /*
3807 * If we've made progress since setting HF_IRET_MASK, we've
3808 * executed an IRET and can allow NMI injection.
3809 */
3810 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
3811 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003812 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003813 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3814 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003815
Gleb Natapov9222be12009-04-23 17:14:37 +03003816 svm->vcpu.arch.nmi_injected = false;
3817 kvm_clear_exception_queue(&svm->vcpu);
3818 kvm_clear_interrupt_queue(&svm->vcpu);
3819
3820 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3821 return;
3822
Avi Kivity3842d132010-07-27 12:30:24 +03003823 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3824
Gleb Natapov9222be12009-04-23 17:14:37 +03003825 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3826 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3827
3828 switch (type) {
3829 case SVM_EXITINTINFO_TYPE_NMI:
3830 svm->vcpu.arch.nmi_injected = true;
3831 break;
3832 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003833 /*
3834 * In case of software exceptions, do not reinject the vector,
3835 * but re-execute the instruction instead. Rewind RIP first
3836 * if we emulated INT3 before.
3837 */
3838 if (kvm_exception_is_soft(vector)) {
3839 if (vector == BP_VECTOR && int3_injected &&
3840 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3841 kvm_rip_write(&svm->vcpu,
3842 kvm_rip_read(&svm->vcpu) -
3843 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003844 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003845 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003846 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3847 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003848 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003849
3850 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003851 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003852 break;
3853 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003854 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003855 break;
3856 default:
3857 break;
3858 }
3859}
3860
Avi Kivityb463a6f2010-07-20 15:06:17 +03003861static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3862{
3863 struct vcpu_svm *svm = to_svm(vcpu);
3864 struct vmcb_control_area *control = &svm->vmcb->control;
3865
3866 control->exit_int_info = control->event_inj;
3867 control->exit_int_info_err = control->event_inj_err;
3868 control->event_inj = 0;
3869 svm_complete_interrupts(svm);
3870}
3871
Avi Kivity851ba692009-08-24 11:10:17 +03003872static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003873{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003874 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003875
Joerg Roedel2041a062010-04-22 12:33:08 +02003876 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3877 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3878 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3879
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003880 /*
3881 * A vmexit emulation is required before the vcpu can be executed
3882 * again.
3883 */
3884 if (unlikely(svm->nested.exit_required))
3885 return;
3886
Rusty Russelle756fc62007-07-30 20:07:08 +10003887 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003888
Joerg Roedel649d6862008-04-16 16:51:15 +02003889 sync_lapic_to_cr8(vcpu);
3890
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003891 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003892
Avi Kivity04d2cc72007-09-10 18:10:54 +03003893 clgi();
3894
3895 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003896
Avi Kivity6aa8b732006-12-10 02:21:36 -08003897 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03003898 "push %%" _ASM_BP "; \n\t"
3899 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
3900 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
3901 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
3902 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
3903 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
3904 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003905#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003906 "mov %c[r8](%[svm]), %%r8 \n\t"
3907 "mov %c[r9](%[svm]), %%r9 \n\t"
3908 "mov %c[r10](%[svm]), %%r10 \n\t"
3909 "mov %c[r11](%[svm]), %%r11 \n\t"
3910 "mov %c[r12](%[svm]), %%r12 \n\t"
3911 "mov %c[r13](%[svm]), %%r13 \n\t"
3912 "mov %c[r14](%[svm]), %%r14 \n\t"
3913 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003914#endif
3915
Avi Kivity6aa8b732006-12-10 02:21:36 -08003916 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03003917 "push %%" _ASM_AX " \n\t"
3918 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003919 __ex(SVM_VMLOAD) "\n\t"
3920 __ex(SVM_VMRUN) "\n\t"
3921 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03003922 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923
3924 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03003925 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
3926 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
3927 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
3928 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
3929 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
3930 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003931#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003932 "mov %%r8, %c[r8](%[svm]) \n\t"
3933 "mov %%r9, %c[r9](%[svm]) \n\t"
3934 "mov %%r10, %c[r10](%[svm]) \n\t"
3935 "mov %%r11, %c[r11](%[svm]) \n\t"
3936 "mov %%r12, %c[r12](%[svm]) \n\t"
3937 "mov %%r13, %c[r13](%[svm]) \n\t"
3938 "mov %%r14, %c[r14](%[svm]) \n\t"
3939 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003940#endif
Avi Kivity74547662012-09-16 15:10:59 +03003941 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08003942 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003943 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003944 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003945 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3946 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3947 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3948 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3949 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3950 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003951#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003952 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3953 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3954 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3955 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3956 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3957 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3958 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3959 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003960#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003961 : "cc", "memory"
3962#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03003963 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003964 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03003965#else
3966 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003967#endif
3968 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003969
Avi Kivity82ca2d12010-10-21 12:20:34 +02003970#ifdef CONFIG_X86_64
3971 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3972#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003973 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02003974#ifndef CONFIG_X86_32_LAZY_GS
3975 loadsegment(gs, svm->host.gs);
3976#endif
Avi Kivity9581d442010-10-19 16:46:55 +02003977#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978
3979 reload_tss(vcpu);
3980
Avi Kivity56ba47d2007-11-07 17:14:18 +02003981 local_irq_disable();
3982
Avi Kivity13c34e02010-10-21 12:20:31 +02003983 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3984 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3985 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3986 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3987
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02003988 trace_kvm_exit(svm->vmcb->control.exit_code, vcpu, KVM_ISA_SVM);
3989
Joerg Roedel3781c012011-01-14 16:45:02 +01003990 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3991 kvm_before_handle_nmi(&svm->vcpu);
3992
3993 stgi();
3994
3995 /* Any pending NMI will happen here */
3996
3997 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3998 kvm_after_handle_nmi(&svm->vcpu);
3999
Joerg Roedeld7bf8222008-04-16 16:51:17 +02004000 sync_cr8_to_lapic(vcpu);
4001
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004002 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03004003
Joerg Roedel38e5e922010-12-03 15:25:16 +01004004 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
4005
Gleb Natapov631bc482010-10-14 11:22:52 +02004006 /* if exit due to PF check for async PF */
4007 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
4008 svm->apf_reason = kvm_read_and_reset_pf_reason();
4009
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004010 if (npt_enabled) {
4011 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
4012 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
4013 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02004014
4015 /*
4016 * We need to handle MC intercepts here before the vcpu has a chance to
4017 * change the physical cpu
4018 */
4019 if (unlikely(svm->vmcb->control.exit_code ==
4020 SVM_EXIT_EXCP_BASE + MC_VECTOR))
4021 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01004022
4023 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004024}
4025
Avi Kivity6aa8b732006-12-10 02:21:36 -08004026static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
4027{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004028 struct vcpu_svm *svm = to_svm(vcpu);
4029
4030 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01004031 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01004032 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004033}
4034
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004035static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
4036{
4037 struct vcpu_svm *svm = to_svm(vcpu);
4038
4039 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01004040 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004041
4042 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02004043 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01004044 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004045
Joerg Roedelf40f6a42010-12-03 15:25:15 +01004046 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004047}
4048
Avi Kivity6aa8b732006-12-10 02:21:36 -08004049static int is_disabled(void)
4050{
Joerg Roedel6031a612007-06-22 12:29:50 +03004051 u64 vm_cr;
4052
4053 rdmsrl(MSR_VM_CR, vm_cr);
4054 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
4055 return 1;
4056
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057 return 0;
4058}
4059
Ingo Molnar102d8322007-02-19 14:37:47 +02004060static void
4061svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4062{
4063 /*
4064 * Patch in the VMMCALL instruction:
4065 */
4066 hypercall[0] = 0x0f;
4067 hypercall[1] = 0x01;
4068 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02004069}
4070
Yang, Sheng002c7f72007-07-31 14:23:01 +03004071static void svm_check_processor_compat(void *rtn)
4072{
4073 *(int *)rtn = 0;
4074}
4075
Avi Kivity774ead32007-12-26 13:57:04 +02004076static bool svm_cpu_has_accelerated_tpr(void)
4077{
4078 return false;
4079}
4080
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004081static bool svm_has_high_real_mode_segbase(void)
4082{
4083 return true;
4084}
4085
Paolo Bonzinifc07e762015-10-01 13:20:22 +02004086static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
4087{
4088 return 0;
4089}
4090
Sheng Yang0e851882009-12-18 16:48:46 +08004091static void svm_cpuid_update(struct kvm_vcpu *vcpu)
4092{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004093 struct vcpu_svm *svm = to_svm(vcpu);
4094
4095 /* Update nrips enabled cache */
4096 svm->nrips_enabled = !!guest_cpuid_has_nrips(&svm->vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08004097}
4098
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004099static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
4100{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004101 switch (func) {
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02004102 case 0x80000001:
4103 if (nested)
4104 entry->ecx |= (1 << 2); /* Set SVM bit */
4105 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004106 case 0x8000000A:
4107 entry->eax = 1; /* SVM revision 1 */
4108 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
4109 ASID emulation to nested SVM */
4110 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02004111 entry->edx = 0; /* Per default do not support any
4112 additional features */
4113
4114 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004115 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02004116 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004117
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02004118 /* Support NPT for the guest if enabled */
4119 if (npt_enabled)
4120 entry->edx |= SVM_FEATURE_NPT;
4121
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004122 break;
4123 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004124}
4125
Sheng Yang17cc3932010-01-05 19:02:27 +08004126static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004127{
Sheng Yang17cc3932010-01-05 19:02:27 +08004128 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004129}
4130
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004131static bool svm_rdtscp_supported(void)
4132{
4133 return false;
4134}
4135
Mao, Junjiead756a12012-07-02 01:18:48 +00004136static bool svm_invpcid_supported(void)
4137{
4138 return false;
4139}
4140
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004141static bool svm_mpx_supported(void)
4142{
4143 return false;
4144}
4145
Wanpeng Li55412b22014-12-02 19:21:30 +08004146static bool svm_xsaves_supported(void)
4147{
4148 return false;
4149}
4150
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004151static bool svm_has_wbinvd_exit(void)
4152{
4153 return true;
4154}
4155
Avi Kivity02daab22009-12-30 12:40:26 +02004156static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
4157{
4158 struct vcpu_svm *svm = to_svm(vcpu);
4159
Joerg Roedel18c918c2010-11-30 18:03:59 +01004160 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01004161 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02004162}
4163
Joerg Roedel80612522011-04-04 12:39:33 +02004164#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004165 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004166#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004167 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004168#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004169 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004170
Mathias Krause09941fb2012-08-30 01:30:20 +02004171static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004172 u32 exit_code;
4173 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004174} x86_intercept_map[] = {
4175 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4176 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4177 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4178 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4179 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004180 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4181 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004182 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4183 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4184 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4185 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4186 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4187 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4188 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4189 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004190 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4191 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4192 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4193 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4194 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4195 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4196 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4197 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004198 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4199 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4200 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004201 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4202 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4203 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4204 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4205 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4206 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4207 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4208 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4209 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004210 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4211 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4212 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4213 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4214 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4215 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4216 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004217 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4218 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4219 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4220 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004221};
4222
Joerg Roedel80612522011-04-04 12:39:33 +02004223#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004224#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004225#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004226
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004227static int svm_check_intercept(struct kvm_vcpu *vcpu,
4228 struct x86_instruction_info *info,
4229 enum x86_intercept_stage stage)
4230{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004231 struct vcpu_svm *svm = to_svm(vcpu);
4232 int vmexit, ret = X86EMUL_CONTINUE;
4233 struct __x86_intercept icpt_info;
4234 struct vmcb *vmcb = svm->vmcb;
4235
4236 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4237 goto out;
4238
4239 icpt_info = x86_intercept_map[info->intercept];
4240
Avi Kivity40e19b52011-04-21 12:35:41 +03004241 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004242 goto out;
4243
4244 switch (icpt_info.exit_code) {
4245 case SVM_EXIT_READ_CR0:
4246 if (info->intercept == x86_intercept_cr_read)
4247 icpt_info.exit_code += info->modrm_reg;
4248 break;
4249 case SVM_EXIT_WRITE_CR0: {
4250 unsigned long cr0, val;
4251 u64 intercept;
4252
4253 if (info->intercept == x86_intercept_cr_write)
4254 icpt_info.exit_code += info->modrm_reg;
4255
Jan Kiszka62baf442014-06-29 21:55:53 +02004256 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4257 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004258 break;
4259
4260 intercept = svm->nested.intercept;
4261
4262 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
4263 break;
4264
4265 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4266 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4267
4268 if (info->intercept == x86_intercept_lmsw) {
4269 cr0 &= 0xfUL;
4270 val &= 0xfUL;
4271 /* lmsw can't clear PE - catch this here */
4272 if (cr0 & X86_CR0_PE)
4273 val |= X86_CR0_PE;
4274 }
4275
4276 if (cr0 ^ val)
4277 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4278
4279 break;
4280 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004281 case SVM_EXIT_READ_DR0:
4282 case SVM_EXIT_WRITE_DR0:
4283 icpt_info.exit_code += info->modrm_reg;
4284 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004285 case SVM_EXIT_MSR:
4286 if (info->intercept == x86_intercept_wrmsr)
4287 vmcb->control.exit_info_1 = 1;
4288 else
4289 vmcb->control.exit_info_1 = 0;
4290 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004291 case SVM_EXIT_PAUSE:
4292 /*
4293 * We get this for NOP only, but pause
4294 * is rep not, check this here
4295 */
4296 if (info->rep_prefix != REPE_PREFIX)
4297 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02004298 case SVM_EXIT_IOIO: {
4299 u64 exit_info;
4300 u32 bytes;
4301
Joerg Roedelf6511932011-04-04 12:39:35 +02004302 if (info->intercept == x86_intercept_in ||
4303 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004304 exit_info = ((info->src_val & 0xffff) << 16) |
4305 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02004306 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02004307 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004308 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02004309 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02004310 }
4311
4312 if (info->intercept == x86_intercept_outs ||
4313 info->intercept == x86_intercept_ins)
4314 exit_info |= SVM_IOIO_STR_MASK;
4315
4316 if (info->rep_prefix)
4317 exit_info |= SVM_IOIO_REP_MASK;
4318
4319 bytes = min(bytes, 4u);
4320
4321 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4322
4323 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4324
4325 vmcb->control.exit_info_1 = exit_info;
4326 vmcb->control.exit_info_2 = info->next_rip;
4327
4328 break;
4329 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004330 default:
4331 break;
4332 }
4333
Bandan Dasf1047652015-06-11 02:05:33 -04004334 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4335 if (static_cpu_has(X86_FEATURE_NRIPS))
4336 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004337 vmcb->control.exit_code = icpt_info.exit_code;
4338 vmexit = nested_svm_exit_handled(svm);
4339
4340 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4341 : X86EMUL_CONTINUE;
4342
4343out:
4344 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004345}
4346
Yang Zhanga547c6d2013-04-11 19:25:10 +08004347static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
4348{
4349 local_irq_enable();
4350}
4351
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004352static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4353{
4354}
4355
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004356static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004357 .cpu_has_kvm_support = has_svm,
4358 .disabled_by_bios = is_disabled,
4359 .hardware_setup = svm_hardware_setup,
4360 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004361 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004362 .hardware_enable = svm_hardware_enable,
4363 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004364 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004365 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004366
4367 .vcpu_create = svm_create_vcpu,
4368 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004369 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004370
Avi Kivity04d2cc72007-09-10 18:10:54 +03004371 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004372 .vcpu_load = svm_vcpu_load,
4373 .vcpu_put = svm_vcpu_put,
4374
Jan Kiszkac8639012012-09-21 05:42:55 +02004375 .update_db_bp_intercept = update_db_bp_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004376 .get_msr = svm_get_msr,
4377 .set_msr = svm_set_msr,
4378 .get_segment_base = svm_get_segment_base,
4379 .get_segment = svm_get_segment,
4380 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004381 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004382 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004383 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004384 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004385 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004386 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004387 .set_cr3 = svm_set_cr3,
4388 .set_cr4 = svm_set_cr4,
4389 .set_efer = svm_set_efer,
4390 .get_idt = svm_get_idt,
4391 .set_idt = svm_set_idt,
4392 .get_gdt = svm_get_gdt,
4393 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004394 .get_dr6 = svm_get_dr6,
4395 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03004396 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01004397 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004398 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004399 .get_rflags = svm_get_rflags,
4400 .set_rflags = svm_set_rflags,
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +02004401 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004402 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004403
Avi Kivity6aa8b732006-12-10 02:21:36 -08004404 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004405
Avi Kivity6aa8b732006-12-10 02:21:36 -08004406 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004407 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004408 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004409 .set_interrupt_shadow = svm_set_interrupt_shadow,
4410 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004411 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004412 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004413 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004414 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004415 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004416 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004417 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004418 .get_nmi_mask = svm_get_nmi_mask,
4419 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004420 .enable_nmi_window = enable_nmi_window,
4421 .enable_irq_window = enable_irq_window,
4422 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08004423 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004424 .cpu_uses_apicv = svm_cpu_uses_apicv,
Yang Zhangc7c9c562013-01-25 10:18:51 +08004425 .load_eoi_exitmap = svm_load_eoi_exitmap,
Yang Zhanga20ed542013-04-11 19:25:15 +08004426 .sync_pir_to_irr = svm_sync_pir_to_irr,
Izik Eiduscbc94022007-10-25 00:29:55 +02004427
4428 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004429 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004430 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004431
Avi Kivity586f9602010-11-18 13:09:54 +02004432 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004433
Sheng Yang17cc3932010-01-05 19:02:27 +08004434 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004435
4436 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004437
4438 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00004439 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004440 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08004441 .xsaves_supported = svm_xsaves_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004442
4443 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004444
4445 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004446
Joerg Roedel4051b182011-03-25 09:44:49 +01004447 .set_tsc_khz = svm_set_tsc_khz,
Will Auldba904632012-11-29 12:42:50 -08004448 .read_tsc_offset = svm_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004449 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004450 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01004451 .compute_tsc_offset = svm_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03004452 .read_l1_tsc = svm_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004453
4454 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004455
4456 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08004457 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004458
4459 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02004460
4461 .pmu_ops = &amd_pmu_ops,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004462};
4463
4464static int __init svm_init(void)
4465{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004466 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004467 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004468}
4469
4470static void __exit svm_exit(void)
4471{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004472 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473}
4474
4475module_init(svm_init)
4476module_exit(svm_exit)