blob: f2c8e4917688a34a9dfb3994d776e8efab512fc2 [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 Roedel8a05a1b2010-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
Paolo Bonzini56908912015-10-19 11:30:19 +02001092static void init_vmcb(struct vcpu_svm *svm)
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 Roedel8a05a1b2010-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 Roedel8a05a1b2010-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
Paolo Bonzini56908912015-10-19 11:30:19 +02001163 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001164 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001165 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001167 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168
Joerg Roedele0231712010-02-24 18:59:10 +01001169 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001170 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001171 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001173 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001174 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001175
Rusty Russell66aee912007-07-17 23:34:16 +10001176 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001177 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001178
1179 if (npt_enabled) {
1180 /* Setup VMCB for Nested Paging */
1181 control->nested_ctl = 1;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001182 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001183 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001184 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1185 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001186 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001187 save->cr3 = 0;
1188 save->cr4 = 0;
1189 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001190 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001191
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001192 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001193 svm->vcpu.arch.hflags = 0;
1194
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001195 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001196 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001197 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001198 }
1199
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001200 mark_all_dirty(svm->vmcb);
1201
Joerg Roedel2af91942009-08-07 11:49:28 +02001202 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203}
1204
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001205static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001206{
1207 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001208 u32 dummy;
1209 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001210
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001211 if (!init_event) {
1212 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1213 MSR_IA32_APICBASE_ENABLE;
1214 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1215 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1216 }
Paolo Bonzini56908912015-10-19 11:30:19 +02001217 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001218
Julian Stecklina66f7b722012-12-05 15:26:19 +01001219 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy);
1220 kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001221}
1222
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001223static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001225 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001227 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001228 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001229 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001230 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231
Rusty Russellc16f8622007-07-30 21:12:19 +10001232 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001233 if (!svm) {
1234 err = -ENOMEM;
1235 goto out;
1236 }
1237
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001238 svm->tsc_ratio = TSC_RATIO_DEFAULT;
1239
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001240 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1241 if (err)
1242 goto free_svm;
1243
Joerg Roedelf65c2292008-02-13 18:58:46 +01001244 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001245 page = alloc_page(GFP_KERNEL);
1246 if (!page)
1247 goto uninit;
1248
Joerg Roedelf65c2292008-02-13 18:58:46 +01001249 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1250 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001251 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001252
1253 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1254 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001255 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001256
Alexander Grafb286d5d2008-11-25 20:17:05 +01001257 hsave_page = alloc_page(GFP_KERNEL);
1258 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001259 goto free_page3;
1260
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001261 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001262
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001263 svm->msrpm = page_address(msrpm_pages);
1264 svm_vcpu_init_msrpm(svm->msrpm);
1265
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001266 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001267 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001268
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001269 svm->vmcb = page_address(page);
1270 clear_page(svm->vmcb);
1271 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1272 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02001273 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001274
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001275 svm_init_osvw(&svm->vcpu);
1276
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001277 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001278
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001279free_page3:
1280 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1281free_page2:
1282 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1283free_page1:
1284 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001285uninit:
1286 kvm_vcpu_uninit(&svm->vcpu);
1287free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001288 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001289out:
1290 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291}
1292
1293static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1294{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001295 struct vcpu_svm *svm = to_svm(vcpu);
1296
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001297 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001298 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001299 __free_page(virt_to_page(svm->nested.hsave));
1300 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001301 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001302 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303}
1304
Avi Kivity15ad7142007-07-11 18:17:21 +03001305static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001307 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001308 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001309
Avi Kivity0cc50642007-03-25 12:07:27 +02001310 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001311 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001312 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001313 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001314
Avi Kivity82ca2d12010-10-21 12:20:34 +02001315#ifdef CONFIG_X86_64
1316 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1317#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001318 savesegment(fs, svm->host.fs);
1319 savesegment(gs, svm->host.gs);
1320 svm->host.ldt = kvm_read_ldt();
1321
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001322 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001323 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001324
1325 if (static_cpu_has(X86_FEATURE_TSCRATEMSR) &&
Christoph Lameter89cbc762014-08-17 12:30:40 -05001326 svm->tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1327 __this_cpu_write(current_tsc_ratio, svm->tsc_ratio);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001328 wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_ratio);
1329 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330}
1331
1332static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1333{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001334 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001335 int i;
1336
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001337 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001338 kvm_load_ldt(svm->host.ldt);
1339#ifdef CONFIG_X86_64
1340 loadsegment(fs, svm->host.fs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001341 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001342 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001343#else
Avi Kivity831ca602011-03-08 16:09:51 +02001344#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001345 loadsegment(gs, svm->host.gs);
1346#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001347#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001348 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001349 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350}
1351
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1353{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001354 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355}
1356
1357static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1358{
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001359 /*
1360 * Any change of EFLAGS.VM is accompained by a reload of SS
1361 * (caused by either a task switch or an inter-privilege IRET),
1362 * so we do not need to update the CPL here.
1363 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001364 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365}
1366
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001367static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1368{
1369 switch (reg) {
1370 case VCPU_EXREG_PDPTR:
1371 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001372 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001373 break;
1374 default:
1375 BUG();
1376 }
1377}
1378
Alexander Graff0b85052008-11-25 20:17:01 +01001379static void svm_set_vintr(struct vcpu_svm *svm)
1380{
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001381 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001382}
1383
1384static void svm_clear_vintr(struct vcpu_svm *svm)
1385{
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01001386 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001387}
1388
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1390{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001391 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392
1393 switch (seg) {
1394 case VCPU_SREG_CS: return &save->cs;
1395 case VCPU_SREG_DS: return &save->ds;
1396 case VCPU_SREG_ES: return &save->es;
1397 case VCPU_SREG_FS: return &save->fs;
1398 case VCPU_SREG_GS: return &save->gs;
1399 case VCPU_SREG_SS: return &save->ss;
1400 case VCPU_SREG_TR: return &save->tr;
1401 case VCPU_SREG_LDTR: return &save->ldtr;
1402 }
1403 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001404 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405}
1406
1407static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1408{
1409 struct vmcb_seg *s = svm_seg(vcpu, seg);
1410
1411 return s->base;
1412}
1413
1414static void svm_get_segment(struct kvm_vcpu *vcpu,
1415 struct kvm_segment *var, int seg)
1416{
1417 struct vmcb_seg *s = svm_seg(vcpu, seg);
1418
1419 var->base = s->base;
1420 var->limit = s->limit;
1421 var->selector = s->selector;
1422 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1423 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1424 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1425 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1426 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1427 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1428 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301429
1430 /*
1431 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1432 * However, the SVM spec states that the G bit is not observed by the
1433 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1434 * So let's synthesize a legal G bit for all segments, this helps
1435 * running KVM nested. It also helps cross-vendor migration, because
1436 * Intel's vmentry has a check on the 'G' bit.
1437 */
1438 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001439
Joerg Roedele0231712010-02-24 18:59:10 +01001440 /*
1441 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001442 * for cross vendor migration purposes by "not present"
1443 */
1444 var->unusable = !var->present || (var->type == 0);
1445
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001446 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001447 case VCPU_SREG_TR:
1448 /*
1449 * Work around a bug where the busy flag in the tr selector
1450 * isn't exposed
1451 */
Amit Shahc0d09822008-10-27 09:04:18 +00001452 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001453 break;
1454 case VCPU_SREG_DS:
1455 case VCPU_SREG_ES:
1456 case VCPU_SREG_FS:
1457 case VCPU_SREG_GS:
1458 /*
1459 * The accessed bit must always be set in the segment
1460 * descriptor cache, although it can be cleared in the
1461 * descriptor, the cached bit always remains at 1. Since
1462 * Intel has a check on this, set it here to support
1463 * cross-vendor migration.
1464 */
1465 if (!var->unusable)
1466 var->type |= 0x1;
1467 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001468 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001469 /*
1470 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001471 * descriptor is left as 1, although the whole segment has
1472 * been made unusable. Clear it here to pass an Intel VMX
1473 * entry check when cross vendor migrating.
1474 */
1475 if (var->unusable)
1476 var->db = 0;
Jan Kiszka33b458d2014-06-29 17:12:43 +02001477 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02001478 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001479 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480}
1481
Izik Eidus2e4d2652008-03-24 19:38:34 +02001482static int svm_get_cpl(struct kvm_vcpu *vcpu)
1483{
1484 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1485
1486 return save->cpl;
1487}
1488
Gleb Natapov89a27f42010-02-16 10:51:48 +02001489static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001491 struct vcpu_svm *svm = to_svm(vcpu);
1492
Gleb Natapov89a27f42010-02-16 10:51:48 +02001493 dt->size = svm->vmcb->save.idtr.limit;
1494 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495}
1496
Gleb Natapov89a27f42010-02-16 10:51:48 +02001497static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001499 struct vcpu_svm *svm = to_svm(vcpu);
1500
Gleb Natapov89a27f42010-02-16 10:51:48 +02001501 svm->vmcb->save.idtr.limit = dt->size;
1502 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001503 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504}
1505
Gleb Natapov89a27f42010-02-16 10:51:48 +02001506static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001508 struct vcpu_svm *svm = to_svm(vcpu);
1509
Gleb Natapov89a27f42010-02-16 10:51:48 +02001510 dt->size = svm->vmcb->save.gdtr.limit;
1511 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512}
1513
Gleb Natapov89a27f42010-02-16 10:51:48 +02001514static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001516 struct vcpu_svm *svm = to_svm(vcpu);
1517
Gleb Natapov89a27f42010-02-16 10:51:48 +02001518 svm->vmcb->save.gdtr.limit = dt->size;
1519 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001520 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521}
1522
Avi Kivitye8467fd2009-12-29 18:43:06 +02001523static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1524{
1525}
1526
Avi Kivityaff48ba2010-12-05 18:56:11 +02001527static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1528{
1529}
1530
Anthony Liguori25c4c272007-04-27 09:29:21 +03001531static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001532{
1533}
1534
Avi Kivityd2251572010-01-06 10:55:27 +02001535static void update_cr0_intercept(struct vcpu_svm *svm)
1536{
1537 ulong gcr0 = svm->vcpu.arch.cr0;
1538 u64 *hcr0 = &svm->vmcb->save.cr0;
1539
1540 if (!svm->vcpu.fpu_active)
1541 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1542 else
1543 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1544 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1545
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001546 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001547
1548 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001549 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1550 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001551 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001552 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1553 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001554 }
1555}
1556
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1558{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001559 struct vcpu_svm *svm = to_svm(vcpu);
1560
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001561#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001562 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001563 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001564 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001565 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566 }
1567
Mike Dayd77c26f2007-10-08 09:02:08 -04001568 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001569 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001570 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 }
1572 }
1573#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001574 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001575
1576 if (!npt_enabled)
1577 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001578
1579 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001580 cr0 |= X86_CR0_TS;
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02001581 /*
1582 * re-enable caching here because the QEMU bios
1583 * does not do it - this results in some delay at
1584 * reboot
1585 */
1586 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
1587 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001588 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001589 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001590 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001591}
1592
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001593static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001595 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001596 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1597
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001598 if (cr4 & X86_CR4_VMXE)
1599 return 1;
1600
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001601 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001602 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001603
Joerg Roedelec077262008-04-09 14:15:28 +02001604 vcpu->arch.cr4 = cr4;
1605 if (!npt_enabled)
1606 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001607 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001608 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001609 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001610 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611}
1612
1613static void svm_set_segment(struct kvm_vcpu *vcpu,
1614 struct kvm_segment *var, int seg)
1615{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001616 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001617 struct vmcb_seg *s = svm_seg(vcpu, seg);
1618
1619 s->base = var->base;
1620 s->limit = var->limit;
1621 s->selector = var->selector;
1622 if (var->unusable)
1623 s->attrib = 0;
1624 else {
1625 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1626 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1627 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1628 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1629 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1630 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1631 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1632 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1633 }
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001634
1635 /*
1636 * This is always accurate, except if SYSRET returned to a segment
1637 * with SS.DPL != 3. Intel does not have this quirk, and always
1638 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1639 * would entail passing the CPL to userspace and back.
1640 */
1641 if (seg == VCPU_SREG_SS)
1642 svm->vmcb->save.cpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643
Joerg Roedel060d0c92010-12-03 11:45:57 +01001644 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645}
1646
Jan Kiszkac8639012012-09-21 05:42:55 +02001647static void update_db_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001649 struct vcpu_svm *svm = to_svm(vcpu);
1650
Joerg Roedel18c918c2010-11-30 18:03:59 +01001651 clr_exception_intercept(svm, DB_VECTOR);
1652 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001653
Jan Kiszka6be7d302009-10-18 13:24:54 +02001654 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001655 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001656
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001657 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1658 if (vcpu->guest_debug &
1659 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001660 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001661 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001662 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001663 } else
1664 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001665}
1666
Tejun Heo0fe1e002009-10-29 22:34:14 +09001667static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001669 if (sd->next_asid > sd->max_asid) {
1670 ++sd->asid_generation;
1671 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001672 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673 }
1674
Tejun Heo0fe1e002009-10-29 22:34:14 +09001675 svm->asid_generation = sd->asid_generation;
1676 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001677
1678 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679}
1680
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001681static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
1682{
1683 return to_svm(vcpu)->vmcb->save.dr6;
1684}
1685
1686static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
1687{
1688 struct vcpu_svm *svm = to_svm(vcpu);
1689
1690 svm->vmcb->save.dr6 = value;
1691 mark_dirty(svm->vmcb, VMCB_DR);
1692}
1693
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001694static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1695{
1696 struct vcpu_svm *svm = to_svm(vcpu);
1697
1698 get_debugreg(vcpu->arch.db[0], 0);
1699 get_debugreg(vcpu->arch.db[1], 1);
1700 get_debugreg(vcpu->arch.db[2], 2);
1701 get_debugreg(vcpu->arch.db[3], 3);
1702 vcpu->arch.dr6 = svm_get_dr6(vcpu);
1703 vcpu->arch.dr7 = svm->vmcb->save.dr7;
1704
1705 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1706 set_dr_intercepts(svm);
1707}
1708
Gleb Natapov020df072010-04-13 10:05:23 +03001709static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001710{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001711 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001712
Gleb Natapov020df072010-04-13 10:05:23 +03001713 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01001714 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001715}
1716
Avi Kivity851ba692009-08-24 11:10:17 +03001717static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001718{
Gleb Natapov631bc482010-10-14 11:22:52 +02001719 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001721 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722
Gleb Natapov631bc482010-10-14 11:22:52 +02001723 switch (svm->apf_reason) {
1724 default:
1725 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001726
Gleb Natapov631bc482010-10-14 11:22:52 +02001727 trace_kvm_page_fault(fault_address, error_code);
1728 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1729 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01001730 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1731 svm->vmcb->control.insn_bytes,
1732 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02001733 break;
1734 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1735 svm->apf_reason = 0;
1736 local_irq_disable();
1737 kvm_async_pf_task_wait(fault_address);
1738 local_irq_enable();
1739 break;
1740 case KVM_PV_REASON_PAGE_READY:
1741 svm->apf_reason = 0;
1742 local_irq_disable();
1743 kvm_async_pf_task_wake(fault_address);
1744 local_irq_enable();
1745 break;
1746 }
1747 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748}
1749
Avi Kivity851ba692009-08-24 11:10:17 +03001750static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001751{
Avi Kivity851ba692009-08-24 11:10:17 +03001752 struct kvm_run *kvm_run = svm->vcpu.run;
1753
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001754 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001755 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001756 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001757 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1758 return 1;
1759 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001760
Jan Kiszka6be7d302009-10-18 13:24:54 +02001761 if (svm->nmi_singlestep) {
1762 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001763 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1764 svm->vmcb->save.rflags &=
1765 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkac8639012012-09-21 05:42:55 +02001766 update_db_bp_intercept(&svm->vcpu);
Gleb Natapov44c11432009-05-11 13:35:52 +03001767 }
1768
1769 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001770 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001771 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1772 kvm_run->debug.arch.pc =
1773 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1774 kvm_run->debug.arch.exception = DB_VECTOR;
1775 return 0;
1776 }
1777
1778 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001779}
1780
Avi Kivity851ba692009-08-24 11:10:17 +03001781static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001782{
Avi Kivity851ba692009-08-24 11:10:17 +03001783 struct kvm_run *kvm_run = svm->vcpu.run;
1784
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001785 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1786 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1787 kvm_run->debug.arch.exception = BP_VECTOR;
1788 return 0;
1789}
1790
Avi Kivity851ba692009-08-24 11:10:17 +03001791static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001792{
1793 int er;
1794
Andre Przywara51d8b662010-12-21 11:12:02 +01001795 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001796 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001797 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001798 return 1;
1799}
1800
Avi Kivity6b52d182010-01-21 15:31:47 +02001801static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001802{
Avi Kivity6b52d182010-01-21 15:31:47 +02001803 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001804
Joerg Roedel18c918c2010-11-30 18:03:59 +01001805 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001806
Rusty Russelle756fc62007-07-30 20:07:08 +10001807 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001808 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001809}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001810
Avi Kivity6b52d182010-01-21 15:31:47 +02001811static int nm_interception(struct vcpu_svm *svm)
1812{
1813 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001814 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001815}
1816
Joerg Roedel67ec6602010-05-17 14:43:35 +02001817static bool is_erratum_383(void)
1818{
1819 int err, i;
1820 u64 value;
1821
1822 if (!erratum_383_found)
1823 return false;
1824
1825 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1826 if (err)
1827 return false;
1828
1829 /* Bit 62 may or may not be set for this mce */
1830 value &= ~(1ULL << 62);
1831
1832 if (value != 0xb600000000010015ULL)
1833 return false;
1834
1835 /* Clear MCi_STATUS registers */
1836 for (i = 0; i < 6; ++i)
1837 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1838
1839 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1840 if (!err) {
1841 u32 low, high;
1842
1843 value &= ~(1ULL << 2);
1844 low = lower_32_bits(value);
1845 high = upper_32_bits(value);
1846
1847 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1848 }
1849
1850 /* Flush tlb to evict multi-match entries */
1851 __flush_tlb_all();
1852
1853 return true;
1854}
1855
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001856static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001857{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001858 if (is_erratum_383()) {
1859 /*
1860 * Erratum 383 triggered. Guest state is corrupt so kill the
1861 * guest.
1862 */
1863 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1864
Avi Kivitya8eeb042010-05-10 12:34:53 +03001865 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001866
1867 return;
1868 }
1869
Joerg Roedel53371b52008-04-09 14:15:30 +02001870 /*
1871 * On an #MC intercept the MCE handler is not called automatically in
1872 * the host. So do it by hand here.
1873 */
1874 asm volatile (
1875 "int $0x12\n");
1876 /* not sure if we ever come back to this point */
1877
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001878 return;
1879}
1880
1881static int mc_interception(struct vcpu_svm *svm)
1882{
Joerg Roedel53371b52008-04-09 14:15:30 +02001883 return 1;
1884}
1885
Avi Kivity851ba692009-08-24 11:10:17 +03001886static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001887{
Avi Kivity851ba692009-08-24 11:10:17 +03001888 struct kvm_run *kvm_run = svm->vcpu.run;
1889
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001890 /*
1891 * VMCB is undefined after a SHUTDOWN intercept
1892 * so reinitialize it.
1893 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001894 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02001895 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001896
1897 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1898 return 0;
1899}
1900
Avi Kivity851ba692009-08-24 11:10:17 +03001901static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001903 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001904 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001905 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001906 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907
Rusty Russelle756fc62007-07-30 20:07:08 +10001908 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001909 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001910 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001911 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01001912 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001913
Avi Kivity039576c2007-03-20 12:46:50 +02001914 port = io_info >> 16;
1915 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001916 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001917 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001918
1919 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920}
1921
Avi Kivity851ba692009-08-24 11:10:17 +03001922static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001923{
1924 return 1;
1925}
1926
Avi Kivity851ba692009-08-24 11:10:17 +03001927static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001928{
1929 ++svm->vcpu.stat.irq_exits;
1930 return 1;
1931}
1932
Avi Kivity851ba692009-08-24 11:10:17 +03001933static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934{
1935 return 1;
1936}
1937
Avi Kivity851ba692009-08-24 11:10:17 +03001938static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001940 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001941 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942}
1943
Avi Kivity851ba692009-08-24 11:10:17 +03001944static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001945{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001946 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001947 kvm_emulate_hypercall(&svm->vcpu);
1948 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001949}
1950
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001951static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1952{
1953 struct vcpu_svm *svm = to_svm(vcpu);
1954
1955 return svm->nested.nested_cr3;
1956}
1957
Avi Kivitye4e517b2011-07-28 11:36:17 +03001958static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
1959{
1960 struct vcpu_svm *svm = to_svm(vcpu);
1961 u64 cr3 = svm->nested.nested_cr3;
1962 u64 pdpte;
1963 int ret;
1964
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02001965 ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
1966 offset_in_page(cr3) + index * 8, 8);
Avi Kivitye4e517b2011-07-28 11:36:17 +03001967 if (ret)
1968 return 0;
1969 return pdpte;
1970}
1971
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001972static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1973 unsigned long root)
1974{
1975 struct vcpu_svm *svm = to_svm(vcpu);
1976
1977 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01001978 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001979 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001980}
1981
Avi Kivity6389ee92010-11-29 16:12:30 +02001982static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1983 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001984{
1985 struct vcpu_svm *svm = to_svm(vcpu);
1986
Paolo Bonzini5e352512014-09-02 13:18:37 +02001987 if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
1988 /*
1989 * TODO: track the cause of the nested page fault, and
1990 * correctly fill in the high bits of exit_info_1.
1991 */
1992 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1993 svm->vmcb->control.exit_code_hi = 0;
1994 svm->vmcb->control.exit_info_1 = (1ULL << 32);
1995 svm->vmcb->control.exit_info_2 = fault->address;
1996 }
1997
1998 svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
1999 svm->vmcb->control.exit_info_1 |= fault->error_code;
2000
2001 /*
2002 * The present bit is always zero for page structure faults on real
2003 * hardware.
2004 */
2005 if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
2006 svm->vmcb->control.exit_info_1 &= ~1;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02002007
2008 nested_svm_vmexit(svm);
2009}
2010
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02002011static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +02002012{
Paolo Bonziniad896af2013-10-02 16:56:14 +02002013 WARN_ON(mmu_is_nested(vcpu));
2014 kvm_init_shadow_mmu(vcpu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002015 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
2016 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03002017 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02002018 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
2019 vcpu->arch.mmu.shadow_root_level = get_npt_level();
Xiao Guangrongc258b622015-08-05 12:04:24 +08002020 reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
Joerg Roedel4b161842010-09-10 17:31:03 +02002021 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Joerg Roedel4b161842010-09-10 17:31:03 +02002022}
2023
2024static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
2025{
2026 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
2027}
2028
Alexander Grafc0725422008-11-25 20:17:03 +01002029static int nested_svm_check_permissions(struct vcpu_svm *svm)
2030{
Avi Kivityf6801df2010-01-21 15:31:50 +02002031 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01002032 || !is_paging(&svm->vcpu)) {
2033 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2034 return 1;
2035 }
2036
2037 if (svm->vmcb->save.cpl) {
2038 kvm_inject_gp(&svm->vcpu, 0);
2039 return 1;
2040 }
2041
2042 return 0;
2043}
2044
Alexander Grafcf74a782008-11-25 20:17:08 +01002045static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2046 bool has_error_code, u32 error_code)
2047{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002048 int vmexit;
2049
Joerg Roedel20307532010-11-29 17:51:48 +01002050 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002051 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002052
Joerg Roedel0295ad72009-08-07 11:49:37 +02002053 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2054 svm->vmcb->control.exit_code_hi = 0;
2055 svm->vmcb->control.exit_info_1 = error_code;
2056 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
2057
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002058 vmexit = nested_svm_intercept(svm);
2059 if (vmexit == NESTED_EXIT_DONE)
2060 svm->nested.exit_required = true;
2061
2062 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002063}
2064
Joerg Roedel8fe54652010-02-19 16:23:01 +01002065/* This function returns true if it is save to enable the irq window */
2066static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002067{
Joerg Roedel20307532010-11-29 17:51:48 +01002068 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002069 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002070
Joerg Roedel26666952009-08-07 11:49:46 +02002071 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002072 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002073
Joerg Roedel26666952009-08-07 11:49:46 +02002074 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002075 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002076
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002077 /*
2078 * if vmexit was already requested (by intercepted exception
2079 * for instance) do not overwrite it with "external interrupt"
2080 * vmexit.
2081 */
2082 if (svm->nested.exit_required)
2083 return false;
2084
Joerg Roedel197717d2010-02-24 18:59:19 +01002085 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2086 svm->vmcb->control.exit_info_1 = 0;
2087 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002088
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002089 if (svm->nested.intercept & 1ULL) {
2090 /*
2091 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002092 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002093 * #vmexit emulation might sleep. Only signal request for
2094 * the #vmexit here.
2095 */
2096 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002097 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002098 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002099 }
2100
Joerg Roedel8fe54652010-02-19 16:23:01 +01002101 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002102}
2103
Joerg Roedel887f5002010-02-24 18:59:12 +01002104/* This function returns true if it is save to enable the nmi window */
2105static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2106{
Joerg Roedel20307532010-11-29 17:51:48 +01002107 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002108 return true;
2109
2110 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2111 return true;
2112
2113 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2114 svm->nested.exit_required = true;
2115
2116 return false;
2117}
2118
Joerg Roedel7597f122010-02-19 16:23:00 +01002119static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002120{
2121 struct page *page;
2122
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002123 might_sleep();
2124
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002125 page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002126 if (is_error_page(page))
2127 goto error;
2128
Joerg Roedel7597f122010-02-19 16:23:00 +01002129 *_page = page;
2130
2131 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002132
2133error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002134 kvm_inject_gp(&svm->vcpu, 0);
2135
2136 return NULL;
2137}
2138
Joerg Roedel7597f122010-02-19 16:23:00 +01002139static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002140{
Joerg Roedel7597f122010-02-19 16:23:00 +01002141 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002142 kvm_release_page_dirty(page);
2143}
2144
Joerg Roedelce2ac082010-03-01 15:34:39 +01002145static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002146{
Jan Kiszka9bf41832014-06-30 10:54:17 +02002147 unsigned port, size, iopm_len;
2148 u16 val, mask;
2149 u8 start_bit;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002150 u64 gpa;
2151
2152 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2153 return NESTED_EXIT_HOST;
2154
2155 port = svm->vmcb->control.exit_info_1 >> 16;
Jan Kiszka9bf41832014-06-30 10:54:17 +02002156 size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
2157 SVM_IOIO_SIZE_SHIFT;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002158 gpa = svm->nested.vmcb_iopm + (port / 8);
Jan Kiszka9bf41832014-06-30 10:54:17 +02002159 start_bit = port % 8;
2160 iopm_len = (start_bit + size > 8) ? 2 : 1;
2161 mask = (0xf >> (4 - size)) << start_bit;
2162 val = 0;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002163
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002164 if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
Jan Kiszka9bf41832014-06-30 10:54:17 +02002165 return NESTED_EXIT_DONE;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002166
Jan Kiszka9bf41832014-06-30 10:54:17 +02002167 return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002168}
2169
Joerg Roedeld2477822010-03-01 15:34:34 +01002170static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002171{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002172 u32 offset, msr, value;
2173 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002174
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002175 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002176 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002177
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002178 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2179 offset = svm_msrpm_offset(msr);
2180 write = svm->vmcb->control.exit_info_1 & 1;
2181 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002182
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002183 if (offset == MSR_INVALID)
2184 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002185
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002186 /* Offset is in 32 bit units but need in 8 bit units */
2187 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002188
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002189 if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002190 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002191
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002192 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002193}
2194
Joerg Roedel410e4d52009-08-07 11:49:44 +02002195static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002196{
Alexander Grafcf74a782008-11-25 20:17:08 +01002197 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002198
Joerg Roedel410e4d52009-08-07 11:49:44 +02002199 switch (exit_code) {
2200 case SVM_EXIT_INTR:
2201 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002202 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002203 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002204 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002205 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002206 if (npt_enabled)
2207 return NESTED_EXIT_HOST;
2208 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002209 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002210 /* When we're shadowing, trap PFs, but not async PF */
2211 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002212 return NESTED_EXIT_HOST;
2213 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01002214 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
2215 nm_interception(svm);
2216 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002217 default:
2218 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002219 }
2220
Joerg Roedel410e4d52009-08-07 11:49:44 +02002221 return NESTED_EXIT_CONTINUE;
2222}
2223
2224/*
2225 * If this function returns true, this #vmexit was already handled
2226 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002227static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002228{
2229 u32 exit_code = svm->vmcb->control.exit_code;
2230 int vmexit = NESTED_EXIT_HOST;
2231
Alexander Grafcf74a782008-11-25 20:17:08 +01002232 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002233 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002234 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002235 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002236 case SVM_EXIT_IOIO:
2237 vmexit = nested_svm_intercept_ioio(svm);
2238 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002239 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2240 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2241 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002242 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002243 break;
2244 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002245 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2246 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2247 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002248 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002249 break;
2250 }
2251 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2252 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002253 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002254 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002255 /* async page fault always cause vmexit */
2256 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2257 svm->apf_reason != 0)
2258 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002259 break;
2260 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002261 case SVM_EXIT_ERR: {
2262 vmexit = NESTED_EXIT_DONE;
2263 break;
2264 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002265 default: {
2266 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002267 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002268 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002269 }
2270 }
2271
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002272 return vmexit;
2273}
2274
2275static int nested_svm_exit_handled(struct vcpu_svm *svm)
2276{
2277 int vmexit;
2278
2279 vmexit = nested_svm_intercept(svm);
2280
2281 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002282 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002283
2284 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002285}
2286
Joerg Roedel0460a972009-08-07 11:49:31 +02002287static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2288{
2289 struct vmcb_control_area *dst = &dst_vmcb->control;
2290 struct vmcb_control_area *from = &from_vmcb->control;
2291
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002292 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002293 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002294 dst->intercept_exceptions = from->intercept_exceptions;
2295 dst->intercept = from->intercept;
2296 dst->iopm_base_pa = from->iopm_base_pa;
2297 dst->msrpm_base_pa = from->msrpm_base_pa;
2298 dst->tsc_offset = from->tsc_offset;
2299 dst->asid = from->asid;
2300 dst->tlb_ctl = from->tlb_ctl;
2301 dst->int_ctl = from->int_ctl;
2302 dst->int_vector = from->int_vector;
2303 dst->int_state = from->int_state;
2304 dst->exit_code = from->exit_code;
2305 dst->exit_code_hi = from->exit_code_hi;
2306 dst->exit_info_1 = from->exit_info_1;
2307 dst->exit_info_2 = from->exit_info_2;
2308 dst->exit_int_info = from->exit_int_info;
2309 dst->exit_int_info_err = from->exit_int_info_err;
2310 dst->nested_ctl = from->nested_ctl;
2311 dst->event_inj = from->event_inj;
2312 dst->event_inj_err = from->event_inj_err;
2313 dst->nested_cr3 = from->nested_cr3;
2314 dst->lbr_ctl = from->lbr_ctl;
2315}
2316
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002317static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002318{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002319 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002320 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002321 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002322 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002323
Joerg Roedel17897f32009-10-09 16:08:29 +02002324 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2325 vmcb->control.exit_info_1,
2326 vmcb->control.exit_info_2,
2327 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002328 vmcb->control.exit_int_info_err,
2329 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002330
Joerg Roedel7597f122010-02-19 16:23:00 +01002331 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002332 if (!nested_vmcb)
2333 return 1;
2334
Joerg Roedel20307532010-11-29 17:51:48 +01002335 /* Exit Guest-Mode */
2336 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002337 svm->nested.vmcb = 0;
2338
Alexander Grafcf74a782008-11-25 20:17:08 +01002339 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002340 disable_gif(svm);
2341
2342 nested_vmcb->save.es = vmcb->save.es;
2343 nested_vmcb->save.cs = vmcb->save.cs;
2344 nested_vmcb->save.ss = vmcb->save.ss;
2345 nested_vmcb->save.ds = vmcb->save.ds;
2346 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2347 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002348 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002349 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002350 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002351 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002352 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002353 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002354 nested_vmcb->save.rip = vmcb->save.rip;
2355 nested_vmcb->save.rsp = vmcb->save.rsp;
2356 nested_vmcb->save.rax = vmcb->save.rax;
2357 nested_vmcb->save.dr7 = vmcb->save.dr7;
2358 nested_vmcb->save.dr6 = vmcb->save.dr6;
2359 nested_vmcb->save.cpl = vmcb->save.cpl;
2360
2361 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2362 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2363 nested_vmcb->control.int_state = vmcb->control.int_state;
2364 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2365 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2366 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2367 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2368 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2369 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02002370
2371 if (svm->nrips_enabled)
2372 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002373
2374 /*
2375 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2376 * to make sure that we do not lose injected events. So check event_inj
2377 * here and copy it to exit_int_info if it is valid.
2378 * Exit_int_info and event_inj can't be both valid because the case
2379 * below only happens on a VMRUN instruction intercept which has
2380 * no valid exit_int_info set.
2381 */
2382 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2383 struct vmcb_control_area *nc = &nested_vmcb->control;
2384
2385 nc->exit_int_info = vmcb->control.event_inj;
2386 nc->exit_int_info_err = vmcb->control.event_inj_err;
2387 }
2388
Joerg Roedel33740e42009-08-07 11:49:29 +02002389 nested_vmcb->control.tlb_ctl = 0;
2390 nested_vmcb->control.event_inj = 0;
2391 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002392
2393 /* We always set V_INTR_MASKING and remember the old value in hflags */
2394 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2395 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2396
Alexander Grafcf74a782008-11-25 20:17:08 +01002397 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002398 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002399
Alexander Graf219b65d2009-06-15 15:21:25 +02002400 kvm_clear_exception_queue(&svm->vcpu);
2401 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002402
Joerg Roedel4b161842010-09-10 17:31:03 +02002403 svm->nested.nested_cr3 = 0;
2404
Alexander Grafcf74a782008-11-25 20:17:08 +01002405 /* Restore selected save entries */
2406 svm->vmcb->save.es = hsave->save.es;
2407 svm->vmcb->save.cs = hsave->save.cs;
2408 svm->vmcb->save.ss = hsave->save.ss;
2409 svm->vmcb->save.ds = hsave->save.ds;
2410 svm->vmcb->save.gdtr = hsave->save.gdtr;
2411 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002412 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002413 svm_set_efer(&svm->vcpu, hsave->save.efer);
2414 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2415 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2416 if (npt_enabled) {
2417 svm->vmcb->save.cr3 = hsave->save.cr3;
2418 svm->vcpu.arch.cr3 = hsave->save.cr3;
2419 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002420 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002421 }
2422 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2423 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2424 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2425 svm->vmcb->save.dr7 = 0;
2426 svm->vmcb->save.cpl = 0;
2427 svm->vmcb->control.exit_int_info = 0;
2428
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002429 mark_all_dirty(svm->vmcb);
2430
Joerg Roedel7597f122010-02-19 16:23:00 +01002431 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002432
Joerg Roedel4b161842010-09-10 17:31:03 +02002433 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002434 kvm_mmu_reset_context(&svm->vcpu);
2435 kvm_mmu_load(&svm->vcpu);
2436
2437 return 0;
2438}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002439
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002440static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002441{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002442 /*
2443 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002444 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01002445 * the kvm msr permission bitmap may contain zero bits
2446 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002447 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002448
Joerg Roedel323c3d82010-03-01 15:34:37 +01002449 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2450 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002451
Joerg Roedel323c3d82010-03-01 15:34:37 +01002452 for (i = 0; i < MSRPM_OFFSETS; i++) {
2453 u32 value, p;
2454 u64 offset;
2455
2456 if (msrpm_offsets[i] == 0xffffffff)
2457 break;
2458
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002459 p = msrpm_offsets[i];
2460 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002461
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02002462 if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
Joerg Roedel323c3d82010-03-01 15:34:37 +01002463 return false;
2464
2465 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2466 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002467
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002468 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002469
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002470 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002471}
2472
Joerg Roedel52c65a302010-08-02 16:46:44 +02002473static bool nested_vmcb_checks(struct vmcb *vmcb)
2474{
2475 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2476 return false;
2477
Joerg Roedeldbe77582010-08-02 16:46:45 +02002478 if (vmcb->control.asid == 0)
2479 return false;
2480
Joerg Roedel4b161842010-09-10 17:31:03 +02002481 if (vmcb->control.nested_ctl && !npt_enabled)
2482 return false;
2483
Joerg Roedel52c65a302010-08-02 16:46:44 +02002484 return true;
2485}
2486
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002487static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002488{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002489 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002490 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002491 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002492 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002493 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002494
Joerg Roedel06fc77722010-02-19 16:23:07 +01002495 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002496
Joerg Roedel7597f122010-02-19 16:23:00 +01002497 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002498 if (!nested_vmcb)
2499 return false;
2500
Joerg Roedel52c65a302010-08-02 16:46:44 +02002501 if (!nested_vmcb_checks(nested_vmcb)) {
2502 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2503 nested_vmcb->control.exit_code_hi = 0;
2504 nested_vmcb->control.exit_info_1 = 0;
2505 nested_vmcb->control.exit_info_2 = 0;
2506
2507 nested_svm_unmap(page);
2508
2509 return false;
2510 }
2511
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002512 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002513 nested_vmcb->save.rip,
2514 nested_vmcb->control.int_ctl,
2515 nested_vmcb->control.event_inj,
2516 nested_vmcb->control.nested_ctl);
2517
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002518 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2519 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002520 nested_vmcb->control.intercept_exceptions,
2521 nested_vmcb->control.intercept);
2522
Alexander Graf3d6368e2008-11-25 20:17:07 +01002523 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002524 kvm_clear_exception_queue(&svm->vcpu);
2525 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002526
Joerg Roedele0231712010-02-24 18:59:10 +01002527 /*
2528 * Save the old vmcb, so we don't need to pick what we save, but can
2529 * restore everything when a VMEXIT occurs
2530 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002531 hsave->save.es = vmcb->save.es;
2532 hsave->save.cs = vmcb->save.cs;
2533 hsave->save.ss = vmcb->save.ss;
2534 hsave->save.ds = vmcb->save.ds;
2535 hsave->save.gdtr = vmcb->save.gdtr;
2536 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002537 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002538 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002539 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002540 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002541 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002542 hsave->save.rsp = vmcb->save.rsp;
2543 hsave->save.rax = vmcb->save.rax;
2544 if (npt_enabled)
2545 hsave->save.cr3 = vmcb->save.cr3;
2546 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002547 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002548
Joerg Roedel0460a972009-08-07 11:49:31 +02002549 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002550
Avi Kivityf6e78472010-08-02 15:30:20 +03002551 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002552 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2553 else
2554 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2555
Joerg Roedel4b161842010-09-10 17:31:03 +02002556 if (nested_vmcb->control.nested_ctl) {
2557 kvm_mmu_unload(&svm->vcpu);
2558 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2559 nested_svm_init_mmu_context(&svm->vcpu);
2560 }
2561
Alexander Graf3d6368e2008-11-25 20:17:07 +01002562 /* Load the nested guest state */
2563 svm->vmcb->save.es = nested_vmcb->save.es;
2564 svm->vmcb->save.cs = nested_vmcb->save.cs;
2565 svm->vmcb->save.ss = nested_vmcb->save.ss;
2566 svm->vmcb->save.ds = nested_vmcb->save.ds;
2567 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2568 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002569 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002570 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2571 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2572 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2573 if (npt_enabled) {
2574 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2575 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002576 } else
Avi Kivity23902182010-06-10 17:02:16 +03002577 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002578
2579 /* Guest paging mode is active - reset mmu */
2580 kvm_mmu_reset_context(&svm->vcpu);
2581
Joerg Roedeldefbba52009-08-07 11:49:30 +02002582 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002583 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2584 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2585 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002586
Alexander Graf3d6368e2008-11-25 20:17:07 +01002587 /* In case we don't even reach vcpu_run, the fields are not updated */
2588 svm->vmcb->save.rax = nested_vmcb->save.rax;
2589 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2590 svm->vmcb->save.rip = nested_vmcb->save.rip;
2591 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2592 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2593 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2594
Joerg Roedelf7138532010-03-01 15:34:40 +01002595 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002596 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002597
Joerg Roedelaad42c62009-08-07 11:49:34 +02002598 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002599 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002600 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002601 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2602 svm->nested.intercept = nested_vmcb->control.intercept;
2603
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002604 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002605 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002606 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2607 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2608 else
2609 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2610
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002611 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2612 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002613 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2614 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002615 }
2616
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002617 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01002618 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002619
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002620 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002621 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2622 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2623 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002624 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2625 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2626
Joerg Roedel7597f122010-02-19 16:23:00 +01002627 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002628
Joerg Roedel20307532010-11-29 17:51:48 +01002629 /* Enter Guest-Mode */
2630 enter_guest_mode(&svm->vcpu);
2631
Joerg Roedel384c6362010-11-30 18:03:56 +01002632 /*
2633 * Merge guest and host intercepts - must be called with vcpu in
2634 * guest-mode to take affect here
2635 */
2636 recalc_intercepts(svm);
2637
Joerg Roedel06fc77722010-02-19 16:23:07 +01002638 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002639
Joerg Roedel2af91942009-08-07 11:49:28 +02002640 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002641
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002642 mark_all_dirty(svm->vmcb);
2643
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002644 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002645}
2646
Joerg Roedel9966bf62009-08-07 11:49:40 +02002647static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002648{
2649 to_vmcb->save.fs = from_vmcb->save.fs;
2650 to_vmcb->save.gs = from_vmcb->save.gs;
2651 to_vmcb->save.tr = from_vmcb->save.tr;
2652 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2653 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2654 to_vmcb->save.star = from_vmcb->save.star;
2655 to_vmcb->save.lstar = from_vmcb->save.lstar;
2656 to_vmcb->save.cstar = from_vmcb->save.cstar;
2657 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2658 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2659 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2660 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002661}
2662
Avi Kivity851ba692009-08-24 11:10:17 +03002663static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002664{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002665 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002666 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002667
Alexander Graf55426752008-11-25 20:17:06 +01002668 if (nested_svm_check_permissions(svm))
2669 return 1;
2670
Joerg Roedel7597f122010-02-19 16:23:00 +01002671 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002672 if (!nested_vmcb)
2673 return 1;
2674
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002675 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2676 skip_emulated_instruction(&svm->vcpu);
2677
Joerg Roedel9966bf62009-08-07 11:49:40 +02002678 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002679 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002680
2681 return 1;
2682}
2683
Avi Kivity851ba692009-08-24 11:10:17 +03002684static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002685{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002686 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002687 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002688
Alexander Graf55426752008-11-25 20:17:06 +01002689 if (nested_svm_check_permissions(svm))
2690 return 1;
2691
Joerg Roedel7597f122010-02-19 16:23:00 +01002692 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002693 if (!nested_vmcb)
2694 return 1;
2695
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002696 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2697 skip_emulated_instruction(&svm->vcpu);
2698
Joerg Roedel9966bf62009-08-07 11:49:40 +02002699 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002700 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002701
2702 return 1;
2703}
2704
Avi Kivity851ba692009-08-24 11:10:17 +03002705static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002706{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002707 if (nested_svm_check_permissions(svm))
2708 return 1;
2709
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002710 /* Save rip after vmrun instruction */
2711 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002712
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002713 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002714 return 1;
2715
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002716 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002717 goto failed;
2718
2719 return 1;
2720
2721failed:
2722
2723 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2724 svm->vmcb->control.exit_code_hi = 0;
2725 svm->vmcb->control.exit_info_1 = 0;
2726 svm->vmcb->control.exit_info_2 = 0;
2727
2728 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002729
2730 return 1;
2731}
2732
Avi Kivity851ba692009-08-24 11:10:17 +03002733static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002734{
2735 if (nested_svm_check_permissions(svm))
2736 return 1;
2737
2738 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2739 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002740 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002741
Joerg Roedel2af91942009-08-07 11:49:28 +02002742 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002743
2744 return 1;
2745}
2746
Avi Kivity851ba692009-08-24 11:10:17 +03002747static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002748{
2749 if (nested_svm_check_permissions(svm))
2750 return 1;
2751
2752 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2753 skip_emulated_instruction(&svm->vcpu);
2754
Joerg Roedel2af91942009-08-07 11:49:28 +02002755 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002756
2757 /* After a CLGI no interrupts should come */
2758 svm_clear_vintr(svm);
2759 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2760
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002761 mark_dirty(svm->vmcb, VMCB_INTR);
2762
Alexander Graf1371d902008-11-25 20:17:04 +01002763 return 1;
2764}
2765
Avi Kivity851ba692009-08-24 11:10:17 +03002766static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002767{
2768 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002769
David Kaplan668f1982015-02-20 16:02:10 -06002770 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
2771 kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedelec1ff792009-10-09 16:08:31 +02002772
Alexander Grafff092382009-06-15 15:21:24 +02002773 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
David Kaplan668f1982015-02-20 16:02:10 -06002774 kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Alexander Grafff092382009-06-15 15:21:24 +02002775
2776 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2777 skip_emulated_instruction(&svm->vcpu);
2778 return 1;
2779}
2780
Joerg Roedel532a46b2009-10-09 16:08:32 +02002781static int skinit_interception(struct vcpu_svm *svm)
2782{
David Kaplan668f1982015-02-20 16:02:10 -06002783 trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
Joerg Roedel532a46b2009-10-09 16:08:32 +02002784
2785 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2786 return 1;
2787}
2788
David Kaplandab429a2015-03-02 13:43:37 -06002789static int wbinvd_interception(struct vcpu_svm *svm)
2790{
2791 kvm_emulate_wbinvd(&svm->vcpu);
2792 return 1;
2793}
2794
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002795static int xsetbv_interception(struct vcpu_svm *svm)
2796{
2797 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
2798 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
2799
2800 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
2801 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2802 skip_emulated_instruction(&svm->vcpu);
2803 }
2804
2805 return 1;
2806}
2807
Avi Kivity851ba692009-08-24 11:10:17 +03002808static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809{
Izik Eidus37817f22008-03-24 23:14:53 +02002810 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002811 int reason;
2812 int int_type = svm->vmcb->control.exit_int_info &
2813 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002814 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002815 uint32_t type =
2816 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2817 uint32_t idt_v =
2818 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002819 bool has_error_code = false;
2820 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002821
2822 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002823
Izik Eidus37817f22008-03-24 23:14:53 +02002824 if (svm->vmcb->control.exit_info_2 &
2825 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002826 reason = TASK_SWITCH_IRET;
2827 else if (svm->vmcb->control.exit_info_2 &
2828 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2829 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002830 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002831 reason = TASK_SWITCH_GATE;
2832 else
2833 reason = TASK_SWITCH_CALL;
2834
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002835 if (reason == TASK_SWITCH_GATE) {
2836 switch (type) {
2837 case SVM_EXITINTINFO_TYPE_NMI:
2838 svm->vcpu.arch.nmi_injected = false;
2839 break;
2840 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002841 if (svm->vmcb->control.exit_info_2 &
2842 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2843 has_error_code = true;
2844 error_code =
2845 (u32)svm->vmcb->control.exit_info_2;
2846 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002847 kvm_clear_exception_queue(&svm->vcpu);
2848 break;
2849 case SVM_EXITINTINFO_TYPE_INTR:
2850 kvm_clear_interrupt_queue(&svm->vcpu);
2851 break;
2852 default:
2853 break;
2854 }
2855 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002856
Gleb Natapov8317c292009-04-12 13:37:02 +03002857 if (reason != TASK_SWITCH_GATE ||
2858 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2859 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002860 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2861 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002862
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002863 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2864 int_vec = -1;
2865
2866 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03002867 has_error_code, error_code) == EMULATE_FAIL) {
2868 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2869 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2870 svm->vcpu.run->internal.ndata = 0;
2871 return 0;
2872 }
2873 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874}
2875
Avi Kivity851ba692009-08-24 11:10:17 +03002876static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002878 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002879 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002880 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881}
2882
Avi Kivity851ba692009-08-24 11:10:17 +03002883static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002884{
2885 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01002886 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002887 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02002888 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01002889 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002890 return 1;
2891}
2892
Avi Kivity851ba692009-08-24 11:10:17 +03002893static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002894{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002895 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2896 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
2897
2898 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
2899 skip_emulated_instruction(&svm->vcpu);
2900 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002901}
2902
Avi Kivity851ba692009-08-24 11:10:17 +03002903static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904{
Andre Przywara51d8b662010-12-21 11:12:02 +01002905 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906}
2907
Avi Kivity332b56e2011-11-10 14:57:24 +02002908static int rdpmc_interception(struct vcpu_svm *svm)
2909{
2910 int err;
2911
2912 if (!static_cpu_has(X86_FEATURE_NRIPS))
2913 return emulate_on_interception(svm);
2914
2915 err = kvm_rdpmc(&svm->vcpu);
2916 kvm_complete_insn_gp(&svm->vcpu, err);
2917
2918 return 1;
2919}
2920
Xiubo Li52eb5a62015-03-13 17:39:45 +08002921static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
2922 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02002923{
2924 unsigned long cr0 = svm->vcpu.arch.cr0;
2925 bool ret = false;
2926 u64 intercept;
2927
2928 intercept = svm->nested.intercept;
2929
2930 if (!is_guest_mode(&svm->vcpu) ||
2931 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
2932 return false;
2933
2934 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2935 val &= ~SVM_CR0_SELECTIVE_MASK;
2936
2937 if (cr0 ^ val) {
2938 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2939 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2940 }
2941
2942 return ret;
2943}
2944
Andre Przywara7ff76d52010-12-21 11:12:04 +01002945#define CR_VALID (1ULL << 63)
2946
2947static int cr_interception(struct vcpu_svm *svm)
2948{
2949 int reg, cr;
2950 unsigned long val;
2951 int err;
2952
2953 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2954 return emulate_on_interception(svm);
2955
2956 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2957 return emulate_on_interception(svm);
2958
2959 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06002960 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
2961 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
2962 else
2963 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002964
2965 err = 0;
2966 if (cr >= 16) { /* mov to cr */
2967 cr -= 16;
2968 val = kvm_register_read(&svm->vcpu, reg);
2969 switch (cr) {
2970 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02002971 if (!check_selective_cr0_intercepted(svm, val))
2972 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002973 else
2974 return 1;
2975
Andre Przywara7ff76d52010-12-21 11:12:04 +01002976 break;
2977 case 3:
2978 err = kvm_set_cr3(&svm->vcpu, val);
2979 break;
2980 case 4:
2981 err = kvm_set_cr4(&svm->vcpu, val);
2982 break;
2983 case 8:
2984 err = kvm_set_cr8(&svm->vcpu, val);
2985 break;
2986 default:
2987 WARN(1, "unhandled write to CR%d", cr);
2988 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2989 return 1;
2990 }
2991 } else { /* mov from cr */
2992 switch (cr) {
2993 case 0:
2994 val = kvm_read_cr0(&svm->vcpu);
2995 break;
2996 case 2:
2997 val = svm->vcpu.arch.cr2;
2998 break;
2999 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003000 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003001 break;
3002 case 4:
3003 val = kvm_read_cr4(&svm->vcpu);
3004 break;
3005 case 8:
3006 val = kvm_get_cr8(&svm->vcpu);
3007 break;
3008 default:
3009 WARN(1, "unhandled read from CR%d", cr);
3010 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
3011 return 1;
3012 }
3013 kvm_register_write(&svm->vcpu, reg, val);
3014 }
3015 kvm_complete_insn_gp(&svm->vcpu, err);
3016
3017 return 1;
3018}
3019
Andre Przywaracae37972010-12-21 11:12:05 +01003020static int dr_interception(struct vcpu_svm *svm)
3021{
3022 int reg, dr;
3023 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01003024
Paolo Bonzinifacb0132014-02-21 10:32:27 +01003025 if (svm->vcpu.guest_debug == 0) {
3026 /*
3027 * No more DR vmexits; force a reload of the debug registers
3028 * and reenter on this instruction. The next vmexit will
3029 * retrieve the full state of the debug registers.
3030 */
3031 clr_dr_intercepts(svm);
3032 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
3033 return 1;
3034 }
3035
Andre Przywaracae37972010-12-21 11:12:05 +01003036 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
3037 return emulate_on_interception(svm);
3038
3039 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
3040 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
3041
3042 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003043 if (!kvm_require_dr(&svm->vcpu, dr - 16))
3044 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01003045 val = kvm_register_read(&svm->vcpu, reg);
3046 kvm_set_dr(&svm->vcpu, dr - 16, val);
3047 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03003048 if (!kvm_require_dr(&svm->vcpu, dr))
3049 return 1;
3050 kvm_get_dr(&svm->vcpu, dr, &val);
3051 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01003052 }
3053
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01003054 skip_emulated_instruction(&svm->vcpu);
3055
Andre Przywaracae37972010-12-21 11:12:05 +01003056 return 1;
3057}
3058
Avi Kivity851ba692009-08-24 11:10:17 +03003059static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01003060{
Avi Kivity851ba692009-08-24 11:10:17 +03003061 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003062 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003063
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003064 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3065 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003066 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02003067 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003068 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003069 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003070 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003071 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3072 return 0;
3073}
3074
Paolo Bonzini48d89b92014-08-26 13:27:46 +02003075static u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003076{
3077 struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
3078 return vmcb->control.tsc_offset +
Marcelo Tosatti886b4702012-11-27 23:28:58 -02003079 svm_scale_tsc(vcpu, host_tsc);
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003080}
3081
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003082static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003084 struct vcpu_svm *svm = to_svm(vcpu);
3085
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003086 switch (msr_info->index) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303087 case MSR_IA32_TSC: {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003088 msr_info->data = svm->vmcb->control.tsc_offset +
Andy Lutomirski4ea16362015-06-25 18:44:07 +02003089 svm_scale_tsc(vcpu, rdtsc());
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003090
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091 break;
3092 }
Brian Gerst8c065852010-07-17 09:03:26 -04003093 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003094 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003096#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003098 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099 break;
3100 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003101 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102 break;
3103 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003104 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105 break;
3106 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003107 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108 break;
3109#endif
3110 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003111 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003112 break;
3113 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003114 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115 break;
3116 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003117 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003119 /*
3120 * Nobody will change the following 5 values in the VMCB so we can
3121 * safely return them on rdmsr. They will always be 0 until LBRV is
3122 * implemented.
3123 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003124 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003125 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01003126 break;
3127 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003128 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003129 break;
3130 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003131 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003132 break;
3133 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003134 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01003135 break;
3136 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003137 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01003138 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003139 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003140 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003141 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003142 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003143 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003144 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003145 case MSR_IA32_UCODE_REV:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003146 msr_info->data = 0x01000065;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003147 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003149 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150 }
3151 return 0;
3152}
3153
Avi Kivity851ba692009-08-24 11:10:17 +03003154static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155{
David Kaplan668f1982015-02-20 16:02:10 -06003156 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003157 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003159 msr_info.index = ecx;
3160 msr_info.host_initiated = false;
3161 if (svm_get_msr(&svm->vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02003162 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003163 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003164 } else {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003165 trace_kvm_msr_read(ecx, msr_info.data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003166
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003167 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
3168 msr_info.data & 0xffffffff);
3169 kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
3170 msr_info.data >> 32);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003171 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003172 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 }
3174 return 1;
3175}
3176
Joerg Roedel4a810182010-02-24 18:59:15 +01003177static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3178{
3179 struct vcpu_svm *svm = to_svm(vcpu);
3180 int svm_dis, chg_mask;
3181
3182 if (data & ~SVM_VM_CR_VALID_MASK)
3183 return 1;
3184
3185 chg_mask = SVM_VM_CR_VALID_MASK;
3186
3187 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3188 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3189
3190 svm->nested.vm_cr_msr &= ~chg_mask;
3191 svm->nested.vm_cr_msr |= (data & chg_mask);
3192
3193 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3194
3195 /* check for svm_disable while efer.svme is set */
3196 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3197 return 1;
3198
3199 return 0;
3200}
3201
Will Auld8fe8ab42012-11-29 12:42:12 -08003202static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003204 struct vcpu_svm *svm = to_svm(vcpu);
3205
Will Auld8fe8ab42012-11-29 12:42:12 -08003206 u32 ecx = msr->index;
3207 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003209 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003210 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003211 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003212 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003213 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003215#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003217 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218 break;
3219 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003220 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221 break;
3222 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003223 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224 break;
3225 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003226 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227 break;
3228#endif
3229 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003230 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231 break;
3232 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003233 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003234 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235 break;
3236 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003237 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003238 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003240 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003241 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003242 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
3243 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003244 break;
3245 }
3246 if (data & DEBUGCTL_RESERVED_BITS)
3247 return 1;
3248
3249 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003250 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003251 if (data & (1ULL<<0))
3252 svm_enable_lbrv(svm);
3253 else
3254 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003255 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003256 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003257 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003258 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003259 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003260 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003261 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03003262 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003263 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003264 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08003265 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266 }
3267 return 0;
3268}
3269
Avi Kivity851ba692009-08-24 11:10:17 +03003270static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271{
Will Auld8fe8ab42012-11-29 12:42:12 -08003272 struct msr_data msr;
David Kaplan668f1982015-02-20 16:02:10 -06003273 u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
3274 u64 data = kvm_read_edx_eax(&svm->vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003275
Will Auld8fe8ab42012-11-29 12:42:12 -08003276 msr.data = data;
3277 msr.index = ecx;
3278 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003279
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003280 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Nadav Amit854e8bb2014-09-16 03:24:05 +03003281 if (kvm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02003282 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003283 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003284 } else {
3285 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10003286 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003287 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003288 return 1;
3289}
3290
Avi Kivity851ba692009-08-24 11:10:17 +03003291static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003292{
Rusty Russelle756fc62007-07-30 20:07:08 +10003293 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003294 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003295 else
Avi Kivity851ba692009-08-24 11:10:17 +03003296 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297}
3298
Avi Kivity851ba692009-08-24 11:10:17 +03003299static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003300{
Avi Kivity3842d132010-07-27 12:30:24 +03003301 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003302 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003303 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003304 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08003305 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003306 return 1;
3307}
3308
Mark Langsdorf565d0992009-10-06 14:25:02 -05003309static int pause_interception(struct vcpu_svm *svm)
3310{
3311 kvm_vcpu_on_spin(&(svm->vcpu));
3312 return 1;
3313}
3314
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003315static int nop_interception(struct vcpu_svm *svm)
3316{
3317 skip_emulated_instruction(&(svm->vcpu));
3318 return 1;
3319}
3320
3321static int monitor_interception(struct vcpu_svm *svm)
3322{
3323 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
3324 return nop_interception(svm);
3325}
3326
3327static int mwait_interception(struct vcpu_svm *svm)
3328{
3329 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
3330 return nop_interception(svm);
3331}
3332
Mathias Krause09941fb2012-08-30 01:30:20 +02003333static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003334 [SVM_EXIT_READ_CR0] = cr_interception,
3335 [SVM_EXIT_READ_CR3] = cr_interception,
3336 [SVM_EXIT_READ_CR4] = cr_interception,
3337 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06003338 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003339 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003340 [SVM_EXIT_WRITE_CR3] = cr_interception,
3341 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003342 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003343 [SVM_EXIT_READ_DR0] = dr_interception,
3344 [SVM_EXIT_READ_DR1] = dr_interception,
3345 [SVM_EXIT_READ_DR2] = dr_interception,
3346 [SVM_EXIT_READ_DR3] = dr_interception,
3347 [SVM_EXIT_READ_DR4] = dr_interception,
3348 [SVM_EXIT_READ_DR5] = dr_interception,
3349 [SVM_EXIT_READ_DR6] = dr_interception,
3350 [SVM_EXIT_READ_DR7] = dr_interception,
3351 [SVM_EXIT_WRITE_DR0] = dr_interception,
3352 [SVM_EXIT_WRITE_DR1] = dr_interception,
3353 [SVM_EXIT_WRITE_DR2] = dr_interception,
3354 [SVM_EXIT_WRITE_DR3] = dr_interception,
3355 [SVM_EXIT_WRITE_DR4] = dr_interception,
3356 [SVM_EXIT_WRITE_DR5] = dr_interception,
3357 [SVM_EXIT_WRITE_DR6] = dr_interception,
3358 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003359 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3360 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003361 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003362 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3363 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
3364 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
3365 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003366 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003367 [SVM_EXIT_SMI] = nop_on_interception,
3368 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003369 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02003370 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003371 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003372 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003373 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003374 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003375 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003376 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003377 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003378 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003379 [SVM_EXIT_MSR] = msr_interception,
3380 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003381 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003382 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003383 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003384 [SVM_EXIT_VMLOAD] = vmload_interception,
3385 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003386 [SVM_EXIT_STGI] = stgi_interception,
3387 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003388 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06003389 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003390 [SVM_EXIT_MONITOR] = monitor_interception,
3391 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003392 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003393 [SVM_EXIT_NPF] = pf_interception,
Paolo Bonzini64d60672015-05-07 11:36:11 +02003394 [SVM_EXIT_RSM] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395};
3396
Joe Perchesae8cc052011-04-24 22:00:50 -07003397static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003398{
3399 struct vcpu_svm *svm = to_svm(vcpu);
3400 struct vmcb_control_area *control = &svm->vmcb->control;
3401 struct vmcb_save_area *save = &svm->vmcb->save;
3402
3403 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003404 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
3405 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
3406 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
3407 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
3408 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
3409 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
3410 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3411 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3412 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3413 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3414 pr_err("%-20s%d\n", "asid:", control->asid);
3415 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3416 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3417 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3418 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3419 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3420 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3421 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3422 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3423 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3424 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3425 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
3426 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3427 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3428 pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
3429 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003430 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003431 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3432 "es:",
3433 save->es.selector, save->es.attrib,
3434 save->es.limit, save->es.base);
3435 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3436 "cs:",
3437 save->cs.selector, save->cs.attrib,
3438 save->cs.limit, save->cs.base);
3439 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3440 "ss:",
3441 save->ss.selector, save->ss.attrib,
3442 save->ss.limit, save->ss.base);
3443 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3444 "ds:",
3445 save->ds.selector, save->ds.attrib,
3446 save->ds.limit, save->ds.base);
3447 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3448 "fs:",
3449 save->fs.selector, save->fs.attrib,
3450 save->fs.limit, save->fs.base);
3451 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3452 "gs:",
3453 save->gs.selector, save->gs.attrib,
3454 save->gs.limit, save->gs.base);
3455 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3456 "gdtr:",
3457 save->gdtr.selector, save->gdtr.attrib,
3458 save->gdtr.limit, save->gdtr.base);
3459 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3460 "ldtr:",
3461 save->ldtr.selector, save->ldtr.attrib,
3462 save->ldtr.limit, save->ldtr.base);
3463 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3464 "idtr:",
3465 save->idtr.selector, save->idtr.attrib,
3466 save->idtr.limit, save->idtr.base);
3467 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3468 "tr:",
3469 save->tr.selector, save->tr.attrib,
3470 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003471 pr_err("cpl: %d efer: %016llx\n",
3472 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003473 pr_err("%-15s %016llx %-13s %016llx\n",
3474 "cr0:", save->cr0, "cr2:", save->cr2);
3475 pr_err("%-15s %016llx %-13s %016llx\n",
3476 "cr3:", save->cr3, "cr4:", save->cr4);
3477 pr_err("%-15s %016llx %-13s %016llx\n",
3478 "dr6:", save->dr6, "dr7:", save->dr7);
3479 pr_err("%-15s %016llx %-13s %016llx\n",
3480 "rip:", save->rip, "rflags:", save->rflags);
3481 pr_err("%-15s %016llx %-13s %016llx\n",
3482 "rsp:", save->rsp, "rax:", save->rax);
3483 pr_err("%-15s %016llx %-13s %016llx\n",
3484 "star:", save->star, "lstar:", save->lstar);
3485 pr_err("%-15s %016llx %-13s %016llx\n",
3486 "cstar:", save->cstar, "sfmask:", save->sfmask);
3487 pr_err("%-15s %016llx %-13s %016llx\n",
3488 "kernel_gs_base:", save->kernel_gs_base,
3489 "sysenter_cs:", save->sysenter_cs);
3490 pr_err("%-15s %016llx %-13s %016llx\n",
3491 "sysenter_esp:", save->sysenter_esp,
3492 "sysenter_eip:", save->sysenter_eip);
3493 pr_err("%-15s %016llx %-13s %016llx\n",
3494 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3495 pr_err("%-15s %016llx %-13s %016llx\n",
3496 "br_from:", save->br_from, "br_to:", save->br_to);
3497 pr_err("%-15s %016llx %-13s %016llx\n",
3498 "excp_from:", save->last_excp_from,
3499 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003500}
3501
Avi Kivity586f9602010-11-18 13:09:54 +02003502static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3503{
3504 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3505
3506 *info1 = control->exit_info_1;
3507 *info2 = control->exit_info_2;
3508}
3509
Avi Kivity851ba692009-08-24 11:10:17 +03003510static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003511{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003512 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003513 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003514 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003515
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003516 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003517 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3518 if (npt_enabled)
3519 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003520
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003521 if (unlikely(svm->nested.exit_required)) {
3522 nested_svm_vmexit(svm);
3523 svm->nested.exit_required = false;
3524
3525 return 1;
3526 }
3527
Joerg Roedel20307532010-11-29 17:51:48 +01003528 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003529 int vmexit;
3530
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003531 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3532 svm->vmcb->control.exit_info_1,
3533 svm->vmcb->control.exit_info_2,
3534 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003535 svm->vmcb->control.exit_int_info_err,
3536 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003537
Joerg Roedel410e4d52009-08-07 11:49:44 +02003538 vmexit = nested_svm_exit_special(svm);
3539
3540 if (vmexit == NESTED_EXIT_CONTINUE)
3541 vmexit = nested_svm_exit_handled(svm);
3542
3543 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003544 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003545 }
3546
Joerg Roedela5c38322009-08-07 11:49:32 +02003547 svm_complete_interrupts(svm);
3548
Avi Kivity04d2cc72007-09-10 18:10:54 +03003549 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3550 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3551 kvm_run->fail_entry.hardware_entry_failure_reason
3552 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003553 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3554 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003555 return 0;
3556 }
3557
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003558 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003559 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003560 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3561 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02003562 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003563 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003564 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003565 exit_code);
3566
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003567 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003568 || !svm_exit_handlers[exit_code]) {
Bandan Dasfaac2452015-03-16 17:18:25 -04003569 WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03003570 kvm_queue_exception(vcpu, UD_VECTOR);
3571 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003572 }
3573
Avi Kivity851ba692009-08-24 11:10:17 +03003574 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003575}
3576
3577static void reload_tss(struct kvm_vcpu *vcpu)
3578{
3579 int cpu = raw_smp_processor_id();
3580
Tejun Heo0fe1e002009-10-29 22:34:14 +09003581 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3582 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003583 load_TR_desc();
3584}
3585
Rusty Russelle756fc62007-07-30 20:07:08 +10003586static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003587{
3588 int cpu = raw_smp_processor_id();
3589
Tejun Heo0fe1e002009-10-29 22:34:14 +09003590 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003591
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003592 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003593 if (svm->asid_generation != sd->asid_generation)
3594 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003595}
3596
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003597static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3598{
3599 struct vcpu_svm *svm = to_svm(vcpu);
3600
3601 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3602 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01003603 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003604 ++vcpu->stat.nmi_injections;
3605}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003606
Eddie Dong85f455f2007-07-06 12:20:49 +03003607static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003608{
3609 struct vmcb_control_area *control;
3610
Rusty Russelle756fc62007-07-30 20:07:08 +10003611 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003612 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003613 control->int_ctl &= ~V_INTR_PRIO_MASK;
3614 control->int_ctl |= V_IRQ_MASK |
3615 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003616 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003617}
3618
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003619static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003620{
3621 struct vcpu_svm *svm = to_svm(vcpu);
3622
Joerg Roedel2af91942009-08-07 11:49:28 +02003623 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003624
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003625 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3626 ++vcpu->stat.irq_injections;
3627
Alexander Graf219b65d2009-06-15 15:21:25 +02003628 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3629 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003630}
3631
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003632static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3633{
3634 struct vcpu_svm *svm = to_svm(vcpu);
3635
Joerg Roedel20307532010-11-29 17:51:48 +01003636 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003637 return;
3638
Radim Krčmář596f3142014-03-11 19:11:18 +01003639 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
3640
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003641 if (irr == -1)
3642 return;
3643
3644 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003645 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003646}
3647
Yang Zhang8d146952013-01-25 10:18:50 +08003648static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
3649{
3650 return;
3651}
3652
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02003653static int svm_cpu_uses_apicv(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08003654{
3655 return 0;
3656}
3657
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02003658static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08003659{
3660 return;
3661}
3662
Yang Zhanga20ed542013-04-11 19:25:15 +08003663static void svm_sync_pir_to_irr(struct kvm_vcpu *vcpu)
3664{
3665 return;
3666}
3667
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003668static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003669{
3670 struct vcpu_svm *svm = to_svm(vcpu);
3671 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003672 int ret;
3673 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3674 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3675 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3676
3677 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003678}
3679
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003680static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3681{
3682 struct vcpu_svm *svm = to_svm(vcpu);
3683
3684 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3685}
3686
3687static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3688{
3689 struct vcpu_svm *svm = to_svm(vcpu);
3690
3691 if (masked) {
3692 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01003693 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003694 } else {
3695 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b2010-11-30 18:04:00 +01003696 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003697 }
3698}
3699
Gleb Natapov78646122009-03-23 12:12:11 +02003700static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3701{
3702 struct vcpu_svm *svm = to_svm(vcpu);
3703 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003704 int ret;
3705
3706 if (!gif_set(svm) ||
3707 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3708 return 0;
3709
Avi Kivityf6e78472010-08-02 15:30:20 +03003710 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003711
Joerg Roedel20307532010-11-29 17:51:48 +01003712 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003713 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3714
3715 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003716}
3717
Jan Kiszkac9a79532014-03-07 20:03:15 +01003718static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003719{
Alexander Graf219b65d2009-06-15 15:21:25 +02003720 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003721
Joerg Roedele0231712010-02-24 18:59:10 +01003722 /*
3723 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3724 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3725 * get that intercept, this function will be called again though and
3726 * we'll get the vintr intercept.
3727 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003728 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003729 svm_set_vintr(svm);
3730 svm_inject_irq(svm, 0x0);
3731 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003732}
3733
Jan Kiszkac9a79532014-03-07 20:03:15 +01003734static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003735{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003736 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003737
Gleb Natapov44c11432009-05-11 13:35:52 +03003738 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3739 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01003740 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03003741
Joerg Roedele0231712010-02-24 18:59:10 +01003742 /*
3743 * Something prevents NMI from been injected. Single step over possible
3744 * problem (IRET or exception injection or interrupt shadow)
3745 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003746 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003747 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkac8639012012-09-21 05:42:55 +02003748 update_db_bp_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003749}
3750
Izik Eiduscbc94022007-10-25 00:29:55 +02003751static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3752{
3753 return 0;
3754}
3755
Avi Kivityd9e368d2007-06-07 19:18:30 +03003756static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3757{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003758 struct vcpu_svm *svm = to_svm(vcpu);
3759
3760 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3761 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3762 else
3763 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003764}
3765
Avi Kivity04d2cc72007-09-10 18:10:54 +03003766static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3767{
3768}
3769
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003770static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3771{
3772 struct vcpu_svm *svm = to_svm(vcpu);
3773
Joerg Roedel20307532010-11-29 17:51:48 +01003774 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003775 return;
3776
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003777 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003778 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003779 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003780 }
3781}
3782
Joerg Roedel649d6862008-04-16 16:51:15 +02003783static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3784{
3785 struct vcpu_svm *svm = to_svm(vcpu);
3786 u64 cr8;
3787
Joerg Roedel20307532010-11-29 17:51:48 +01003788 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003789 return;
3790
Joerg Roedel649d6862008-04-16 16:51:15 +02003791 cr8 = kvm_get_cr8(vcpu);
3792 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3793 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3794}
3795
Gleb Natapov9222be12009-04-23 17:14:37 +03003796static void svm_complete_interrupts(struct vcpu_svm *svm)
3797{
3798 u8 vector;
3799 int type;
3800 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003801 unsigned int3_injected = svm->int3_injected;
3802
3803 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003804
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003805 /*
3806 * If we've made progress since setting HF_IRET_MASK, we've
3807 * executed an IRET and can allow NMI injection.
3808 */
3809 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
3810 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003811 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003812 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3813 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003814
Gleb Natapov9222be12009-04-23 17:14:37 +03003815 svm->vcpu.arch.nmi_injected = false;
3816 kvm_clear_exception_queue(&svm->vcpu);
3817 kvm_clear_interrupt_queue(&svm->vcpu);
3818
3819 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3820 return;
3821
Avi Kivity3842d132010-07-27 12:30:24 +03003822 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3823
Gleb Natapov9222be12009-04-23 17:14:37 +03003824 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3825 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3826
3827 switch (type) {
3828 case SVM_EXITINTINFO_TYPE_NMI:
3829 svm->vcpu.arch.nmi_injected = true;
3830 break;
3831 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003832 /*
3833 * In case of software exceptions, do not reinject the vector,
3834 * but re-execute the instruction instead. Rewind RIP first
3835 * if we emulated INT3 before.
3836 */
3837 if (kvm_exception_is_soft(vector)) {
3838 if (vector == BP_VECTOR && int3_injected &&
3839 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3840 kvm_rip_write(&svm->vcpu,
3841 kvm_rip_read(&svm->vcpu) -
3842 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003843 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003844 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003845 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3846 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003847 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003848
3849 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003850 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003851 break;
3852 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003853 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003854 break;
3855 default:
3856 break;
3857 }
3858}
3859
Avi Kivityb463a6f2010-07-20 15:06:17 +03003860static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3861{
3862 struct vcpu_svm *svm = to_svm(vcpu);
3863 struct vmcb_control_area *control = &svm->vmcb->control;
3864
3865 control->exit_int_info = control->event_inj;
3866 control->exit_int_info_err = control->event_inj_err;
3867 control->event_inj = 0;
3868 svm_complete_interrupts(svm);
3869}
3870
Avi Kivity851ba692009-08-24 11:10:17 +03003871static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003873 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003874
Joerg Roedel2041a062010-04-22 12:33:08 +02003875 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3876 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3877 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3878
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003879 /*
3880 * A vmexit emulation is required before the vcpu can be executed
3881 * again.
3882 */
3883 if (unlikely(svm->nested.exit_required))
3884 return;
3885
Rusty Russelle756fc62007-07-30 20:07:08 +10003886 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003887
Joerg Roedel649d6862008-04-16 16:51:15 +02003888 sync_lapic_to_cr8(vcpu);
3889
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003890 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003891
Avi Kivity04d2cc72007-09-10 18:10:54 +03003892 clgi();
3893
3894 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003895
Avi Kivity6aa8b732006-12-10 02:21:36 -08003896 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03003897 "push %%" _ASM_BP "; \n\t"
3898 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
3899 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
3900 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
3901 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
3902 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
3903 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003904#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003905 "mov %c[r8](%[svm]), %%r8 \n\t"
3906 "mov %c[r9](%[svm]), %%r9 \n\t"
3907 "mov %c[r10](%[svm]), %%r10 \n\t"
3908 "mov %c[r11](%[svm]), %%r11 \n\t"
3909 "mov %c[r12](%[svm]), %%r12 \n\t"
3910 "mov %c[r13](%[svm]), %%r13 \n\t"
3911 "mov %c[r14](%[svm]), %%r14 \n\t"
3912 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003913#endif
3914
Avi Kivity6aa8b732006-12-10 02:21:36 -08003915 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03003916 "push %%" _ASM_AX " \n\t"
3917 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003918 __ex(SVM_VMLOAD) "\n\t"
3919 __ex(SVM_VMRUN) "\n\t"
3920 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03003921 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922
3923 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03003924 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
3925 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
3926 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
3927 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
3928 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
3929 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003930#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003931 "mov %%r8, %c[r8](%[svm]) \n\t"
3932 "mov %%r9, %c[r9](%[svm]) \n\t"
3933 "mov %%r10, %c[r10](%[svm]) \n\t"
3934 "mov %%r11, %c[r11](%[svm]) \n\t"
3935 "mov %%r12, %c[r12](%[svm]) \n\t"
3936 "mov %%r13, %c[r13](%[svm]) \n\t"
3937 "mov %%r14, %c[r14](%[svm]) \n\t"
3938 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003939#endif
Avi Kivity74547662012-09-16 15:10:59 +03003940 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08003941 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003942 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003943 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003944 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3945 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3946 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3947 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3948 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3949 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003950#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003951 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3952 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3953 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3954 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3955 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3956 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3957 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3958 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003959#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003960 : "cc", "memory"
3961#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03003962 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003963 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03003964#else
3965 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003966#endif
3967 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003968
Avi Kivity82ca2d12010-10-21 12:20:34 +02003969#ifdef CONFIG_X86_64
3970 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3971#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003972 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02003973#ifndef CONFIG_X86_32_LAZY_GS
3974 loadsegment(gs, svm->host.gs);
3975#endif
Avi Kivity9581d442010-10-19 16:46:55 +02003976#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003977
3978 reload_tss(vcpu);
3979
Avi Kivity56ba47d2007-11-07 17:14:18 +02003980 local_irq_disable();
3981
Avi Kivity13c34e02010-10-21 12:20:31 +02003982 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3983 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3984 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3985 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3986
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02003987 trace_kvm_exit(svm->vmcb->control.exit_code, vcpu, KVM_ISA_SVM);
3988
Joerg Roedel3781c012011-01-14 16:45:02 +01003989 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3990 kvm_before_handle_nmi(&svm->vcpu);
3991
3992 stgi();
3993
3994 /* Any pending NMI will happen here */
3995
3996 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3997 kvm_after_handle_nmi(&svm->vcpu);
3998
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003999 sync_cr8_to_lapic(vcpu);
4000
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004001 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03004002
Joerg Roedel38e5e922010-12-03 15:25:16 +01004003 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
4004
Gleb Natapov631bc482010-10-14 11:22:52 +02004005 /* if exit due to PF check for async PF */
4006 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
4007 svm->apf_reason = kvm_read_and_reset_pf_reason();
4008
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004009 if (npt_enabled) {
4010 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
4011 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
4012 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02004013
4014 /*
4015 * We need to handle MC intercepts here before the vcpu has a chance to
4016 * change the physical cpu
4017 */
4018 if (unlikely(svm->vmcb->control.exit_code ==
4019 SVM_EXIT_EXCP_BASE + MC_VECTOR))
4020 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01004021
4022 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004023}
4024
Avi Kivity6aa8b732006-12-10 02:21:36 -08004025static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
4026{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004027 struct vcpu_svm *svm = to_svm(vcpu);
4028
4029 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01004030 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01004031 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004032}
4033
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004034static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
4035{
4036 struct vcpu_svm *svm = to_svm(vcpu);
4037
4038 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01004039 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004040
4041 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02004042 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01004043 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004044
Joerg Roedelf40f6a42010-12-03 15:25:15 +01004045 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004046}
4047
Avi Kivity6aa8b732006-12-10 02:21:36 -08004048static int is_disabled(void)
4049{
Joerg Roedel6031a612007-06-22 12:29:50 +03004050 u64 vm_cr;
4051
4052 rdmsrl(MSR_VM_CR, vm_cr);
4053 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
4054 return 1;
4055
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056 return 0;
4057}
4058
Ingo Molnar102d8322007-02-19 14:37:47 +02004059static void
4060svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4061{
4062 /*
4063 * Patch in the VMMCALL instruction:
4064 */
4065 hypercall[0] = 0x0f;
4066 hypercall[1] = 0x01;
4067 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02004068}
4069
Yang, Sheng002c7f72007-07-31 14:23:01 +03004070static void svm_check_processor_compat(void *rtn)
4071{
4072 *(int *)rtn = 0;
4073}
4074
Avi Kivity774ead32007-12-26 13:57:04 +02004075static bool svm_cpu_has_accelerated_tpr(void)
4076{
4077 return false;
4078}
4079
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004080static bool svm_has_high_real_mode_segbase(void)
4081{
4082 return true;
4083}
4084
Paolo Bonzinifc07e762015-10-01 13:20:22 +02004085static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
4086{
4087 return 0;
4088}
4089
Sheng Yang0e851882009-12-18 16:48:46 +08004090static void svm_cpuid_update(struct kvm_vcpu *vcpu)
4091{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004092 struct vcpu_svm *svm = to_svm(vcpu);
4093
4094 /* Update nrips enabled cache */
4095 svm->nrips_enabled = !!guest_cpuid_has_nrips(&svm->vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08004096}
4097
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004098static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
4099{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004100 switch (func) {
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02004101 case 0x80000001:
4102 if (nested)
4103 entry->ecx |= (1 << 2); /* Set SVM bit */
4104 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004105 case 0x8000000A:
4106 entry->eax = 1; /* SVM revision 1 */
4107 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
4108 ASID emulation to nested SVM */
4109 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02004110 entry->edx = 0; /* Per default do not support any
4111 additional features */
4112
4113 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004114 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02004115 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004116
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02004117 /* Support NPT for the guest if enabled */
4118 if (npt_enabled)
4119 entry->edx |= SVM_FEATURE_NPT;
4120
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004121 break;
4122 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004123}
4124
Sheng Yang17cc3932010-01-05 19:02:27 +08004125static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004126{
Sheng Yang17cc3932010-01-05 19:02:27 +08004127 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004128}
4129
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004130static bool svm_rdtscp_supported(void)
4131{
4132 return false;
4133}
4134
Mao, Junjiead756a12012-07-02 01:18:48 +00004135static bool svm_invpcid_supported(void)
4136{
4137 return false;
4138}
4139
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004140static bool svm_mpx_supported(void)
4141{
4142 return false;
4143}
4144
Wanpeng Li55412b22014-12-02 19:21:30 +08004145static bool svm_xsaves_supported(void)
4146{
4147 return false;
4148}
4149
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004150static bool svm_has_wbinvd_exit(void)
4151{
4152 return true;
4153}
4154
Avi Kivity02daab22009-12-30 12:40:26 +02004155static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
4156{
4157 struct vcpu_svm *svm = to_svm(vcpu);
4158
Joerg Roedel18c918c2010-11-30 18:03:59 +01004159 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01004160 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02004161}
4162
Joerg Roedel80612522011-04-04 12:39:33 +02004163#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004164 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004165#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004166 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004167#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004168 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004169
Mathias Krause09941fb2012-08-30 01:30:20 +02004170static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004171 u32 exit_code;
4172 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004173} x86_intercept_map[] = {
4174 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4175 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4176 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4177 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4178 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004179 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4180 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004181 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4182 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4183 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4184 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4185 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4186 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4187 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4188 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004189 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4190 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4191 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4192 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4193 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4194 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4195 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4196 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004197 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4198 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4199 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004200 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4201 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4202 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4203 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4204 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4205 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4206 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4207 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4208 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004209 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4210 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4211 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4212 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4213 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4214 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4215 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004216 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4217 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4218 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4219 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004220};
4221
Joerg Roedel80612522011-04-04 12:39:33 +02004222#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004223#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004224#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004225
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004226static int svm_check_intercept(struct kvm_vcpu *vcpu,
4227 struct x86_instruction_info *info,
4228 enum x86_intercept_stage stage)
4229{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004230 struct vcpu_svm *svm = to_svm(vcpu);
4231 int vmexit, ret = X86EMUL_CONTINUE;
4232 struct __x86_intercept icpt_info;
4233 struct vmcb *vmcb = svm->vmcb;
4234
4235 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4236 goto out;
4237
4238 icpt_info = x86_intercept_map[info->intercept];
4239
Avi Kivity40e19b52011-04-21 12:35:41 +03004240 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004241 goto out;
4242
4243 switch (icpt_info.exit_code) {
4244 case SVM_EXIT_READ_CR0:
4245 if (info->intercept == x86_intercept_cr_read)
4246 icpt_info.exit_code += info->modrm_reg;
4247 break;
4248 case SVM_EXIT_WRITE_CR0: {
4249 unsigned long cr0, val;
4250 u64 intercept;
4251
4252 if (info->intercept == x86_intercept_cr_write)
4253 icpt_info.exit_code += info->modrm_reg;
4254
Jan Kiszka62baf442014-06-29 21:55:53 +02004255 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4256 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004257 break;
4258
4259 intercept = svm->nested.intercept;
4260
4261 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
4262 break;
4263
4264 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4265 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4266
4267 if (info->intercept == x86_intercept_lmsw) {
4268 cr0 &= 0xfUL;
4269 val &= 0xfUL;
4270 /* lmsw can't clear PE - catch this here */
4271 if (cr0 & X86_CR0_PE)
4272 val |= X86_CR0_PE;
4273 }
4274
4275 if (cr0 ^ val)
4276 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4277
4278 break;
4279 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004280 case SVM_EXIT_READ_DR0:
4281 case SVM_EXIT_WRITE_DR0:
4282 icpt_info.exit_code += info->modrm_reg;
4283 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004284 case SVM_EXIT_MSR:
4285 if (info->intercept == x86_intercept_wrmsr)
4286 vmcb->control.exit_info_1 = 1;
4287 else
4288 vmcb->control.exit_info_1 = 0;
4289 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004290 case SVM_EXIT_PAUSE:
4291 /*
4292 * We get this for NOP only, but pause
4293 * is rep not, check this here
4294 */
4295 if (info->rep_prefix != REPE_PREFIX)
4296 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02004297 case SVM_EXIT_IOIO: {
4298 u64 exit_info;
4299 u32 bytes;
4300
Joerg Roedelf6511932011-04-04 12:39:35 +02004301 if (info->intercept == x86_intercept_in ||
4302 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004303 exit_info = ((info->src_val & 0xffff) << 16) |
4304 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02004305 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02004306 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004307 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02004308 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02004309 }
4310
4311 if (info->intercept == x86_intercept_outs ||
4312 info->intercept == x86_intercept_ins)
4313 exit_info |= SVM_IOIO_STR_MASK;
4314
4315 if (info->rep_prefix)
4316 exit_info |= SVM_IOIO_REP_MASK;
4317
4318 bytes = min(bytes, 4u);
4319
4320 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4321
4322 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4323
4324 vmcb->control.exit_info_1 = exit_info;
4325 vmcb->control.exit_info_2 = info->next_rip;
4326
4327 break;
4328 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004329 default:
4330 break;
4331 }
4332
Bandan Dasf1047652015-06-11 02:05:33 -04004333 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4334 if (static_cpu_has(X86_FEATURE_NRIPS))
4335 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004336 vmcb->control.exit_code = icpt_info.exit_code;
4337 vmexit = nested_svm_exit_handled(svm);
4338
4339 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4340 : X86EMUL_CONTINUE;
4341
4342out:
4343 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004344}
4345
Yang Zhanga547c6d2013-04-11 19:25:10 +08004346static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
4347{
4348 local_irq_enable();
4349}
4350
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004351static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4352{
4353}
4354
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004355static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004356 .cpu_has_kvm_support = has_svm,
4357 .disabled_by_bios = is_disabled,
4358 .hardware_setup = svm_hardware_setup,
4359 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004360 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004361 .hardware_enable = svm_hardware_enable,
4362 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004363 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004364 .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365
4366 .vcpu_create = svm_create_vcpu,
4367 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004368 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004369
Avi Kivity04d2cc72007-09-10 18:10:54 +03004370 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004371 .vcpu_load = svm_vcpu_load,
4372 .vcpu_put = svm_vcpu_put,
4373
Jan Kiszkac8639012012-09-21 05:42:55 +02004374 .update_db_bp_intercept = update_db_bp_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004375 .get_msr = svm_get_msr,
4376 .set_msr = svm_set_msr,
4377 .get_segment_base = svm_get_segment_base,
4378 .get_segment = svm_get_segment,
4379 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004380 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004381 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004382 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004383 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004384 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004385 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004386 .set_cr3 = svm_set_cr3,
4387 .set_cr4 = svm_set_cr4,
4388 .set_efer = svm_set_efer,
4389 .get_idt = svm_get_idt,
4390 .set_idt = svm_set_idt,
4391 .get_gdt = svm_get_gdt,
4392 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01004393 .get_dr6 = svm_get_dr6,
4394 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03004395 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01004396 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004397 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004398 .get_rflags = svm_get_rflags,
4399 .set_rflags = svm_set_rflags,
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +02004400 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004401 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004402
Avi Kivity6aa8b732006-12-10 02:21:36 -08004403 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004404
Avi Kivity6aa8b732006-12-10 02:21:36 -08004405 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004406 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004407 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004408 .set_interrupt_shadow = svm_set_interrupt_shadow,
4409 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004410 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004411 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004412 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004413 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004414 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004415 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004416 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004417 .get_nmi_mask = svm_get_nmi_mask,
4418 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004419 .enable_nmi_window = enable_nmi_window,
4420 .enable_irq_window = enable_irq_window,
4421 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08004422 .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004423 .cpu_uses_apicv = svm_cpu_uses_apicv,
Yang Zhangc7c9c562013-01-25 10:18:51 +08004424 .load_eoi_exitmap = svm_load_eoi_exitmap,
Yang Zhanga20ed542013-04-11 19:25:15 +08004425 .sync_pir_to_irr = svm_sync_pir_to_irr,
Izik Eiduscbc94022007-10-25 00:29:55 +02004426
4427 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004428 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004429 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004430
Avi Kivity586f9602010-11-18 13:09:54 +02004431 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004432
Sheng Yang17cc3932010-01-05 19:02:27 +08004433 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004434
4435 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004436
4437 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00004438 .invpcid_supported = svm_invpcid_supported,
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004439 .mpx_supported = svm_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08004440 .xsaves_supported = svm_xsaves_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004441
4442 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004443
4444 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004445
Joerg Roedel4051b182011-03-25 09:44:49 +01004446 .set_tsc_khz = svm_set_tsc_khz,
Will Auldba904632012-11-29 12:42:50 -08004447 .read_tsc_offset = svm_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004448 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004449 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01004450 .compute_tsc_offset = svm_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03004451 .read_l1_tsc = svm_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004452
4453 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004454
4455 .check_intercept = svm_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08004456 .handle_external_intr = svm_handle_external_intr,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004457
4458 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02004459
4460 .pmu_ops = &amd_pmu_ops,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004461};
4462
4463static int __init svm_init(void)
4464{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004465 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004466 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004467}
4468
4469static void __exit svm_exit(void)
4470{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004471 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004472}
4473
4474module_init(svm_init)
4475module_exit(svm_exit)