blob: dcb79527e7aaadd35ca6301122ea58f98e310fc7 [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"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Josh Triplettae759542012-03-28 11:32:28 -070025#include <linux/mod_devicetable.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/vmalloc.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030030#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032
Joerg Roedel1018faa2012-02-29 14:57:32 +010033#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020034#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040035#include <asm/desc.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020036#include <asm/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080037
Eduardo Habkost63d11422008-11-17 19:03:20 -020038#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030039#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020040
Avi Kivity4ecac3f2008-05-13 13:23:38 +030041#define __ex(x) __kvm_handle_fault_on_reboot(x)
42
Avi Kivity6aa8b732006-12-10 02:21:36 -080043MODULE_AUTHOR("Qumranet");
44MODULE_LICENSE("GPL");
45
Josh Triplettae759542012-03-28 11:32:28 -070046static const struct x86_cpu_id svm_cpu_id[] = {
47 X86_FEATURE_MATCH(X86_FEATURE_SVM),
48 {}
49};
50MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
51
Avi Kivity6aa8b732006-12-10 02:21:36 -080052#define IOPM_ALLOC_ORDER 2
53#define MSRPM_ALLOC_ORDER 1
54
Avi Kivity6aa8b732006-12-10 02:21:36 -080055#define SEG_TYPE_LDT 2
56#define SEG_TYPE_BUSY_TSS16 3
57
Andre Przywara6bc31bd2010-04-11 23:07:28 +020058#define SVM_FEATURE_NPT (1 << 0)
59#define SVM_FEATURE_LBRV (1 << 1)
60#define SVM_FEATURE_SVML (1 << 2)
61#define SVM_FEATURE_NRIP (1 << 3)
Andre Przywaraddce97a2010-12-21 11:12:03 +010062#define SVM_FEATURE_TSC_RATE (1 << 4)
63#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
64#define SVM_FEATURE_FLUSH_ASID (1 << 6)
65#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020066#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030067
Joerg Roedel410e4d52009-08-07 11:49:44 +020068#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
69#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
70#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
71
Joerg Roedel24e09cb2008-02-13 18:58:47 +010072#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
73
Joerg Roedelfbc0db72011-03-25 09:44:46 +010074#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010075#define TSC_RATIO_MIN 0x0000000000000001ULL
76#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010077
Joerg Roedel67ec6602010-05-17 14:43:35 +020078static bool erratum_383_found __read_mostly;
79
Avi Kivity6c8166a2009-05-31 18:15:37 +030080static const u32 host_save_user_msrs[] = {
81#ifdef CONFIG_X86_64
82 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
83 MSR_FS_BASE,
84#endif
85 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
86};
87
88#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
89
90struct kvm_vcpu;
91
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020092struct nested_state {
93 struct vmcb *hsave;
94 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010095 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020096 u64 vmcb;
97
98 /* These are the merged vectors */
99 u32 *msrpm;
100
101 /* gpa pointers to the real vectors */
102 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +0100103 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200104
Joerg Roedelcd3ff652009-10-09 16:08:26 +0200105 /* A VMEXIT is required but not yet emulated */
106 bool exit_required;
107
Joerg Roedelaad42c62009-08-07 11:49:34 +0200108 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100109 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100110 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200111 u32 intercept_exceptions;
112 u64 intercept;
113
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200114 /* Nested Paging related state */
115 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200116};
117
Joerg Roedel323c3d82010-03-01 15:34:37 +0100118#define MSRPM_OFFSETS 16
119static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
120
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500121/*
122 * Set osvw_len to higher value when updated Revision Guides
123 * are published and we know what the new status bits are
124 */
125static uint64_t osvw_len = 4, osvw_status;
126
Avi Kivity6c8166a2009-05-31 18:15:37 +0300127struct vcpu_svm {
128 struct kvm_vcpu vcpu;
129 struct vmcb *vmcb;
130 unsigned long vmcb_pa;
131 struct svm_cpu_data *svm_data;
132 uint64_t asid_generation;
133 uint64_t sysenter_esp;
134 uint64_t sysenter_eip;
135
136 u64 next_rip;
137
138 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200139 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200140 u16 fs;
141 u16 gs;
142 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200143 u64 gs_base;
144 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300145
146 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300147
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200148 ulong nmi_iret_rip;
149
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200150 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200151
152 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100153
154 unsigned int3_injected;
155 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200156 u32 apf_reason;
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100157
158 u64 tsc_ratio;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300159};
160
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100161static DEFINE_PER_CPU(u64, current_tsc_ratio);
162#define TSC_RATIO_DEFAULT 0x0100000000ULL
163
Joerg Roedel455716f2010-03-01 15:34:35 +0100164#define MSR_INVALID 0xffffffffU
165
Mathias Krause09941fb2012-08-30 01:30:20 +0200166static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100167 u32 index; /* Index of the MSR */
168 bool always; /* True if intercept is always on */
169} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400170 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100171 { .index = MSR_IA32_SYSENTER_CS, .always = true },
172#ifdef CONFIG_X86_64
173 { .index = MSR_GS_BASE, .always = true },
174 { .index = MSR_FS_BASE, .always = true },
175 { .index = MSR_KERNEL_GS_BASE, .always = true },
176 { .index = MSR_LSTAR, .always = true },
177 { .index = MSR_CSTAR, .always = true },
178 { .index = MSR_SYSCALL_MASK, .always = true },
179#endif
180 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
181 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
182 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
183 { .index = MSR_IA32_LASTINTTOIP, .always = false },
184 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185};
186
187/* enable NPT for AMD64 and X86 with PAE */
188#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
189static bool npt_enabled = true;
190#else
Joerg Roedele0231712010-02-24 18:59:10 +0100191static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192#endif
193
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100194/* allow nested paging (virtualized MMU) for all guests */
195static int npt = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196module_param(npt, int, S_IRUGO);
197
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100198/* allow nested virtualization in KVM/SVM */
199static int nested = true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200module_param(nested, int, S_IRUGO);
201
202static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200203static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800204
Joerg Roedel410e4d52009-08-07 11:49:44 +0200205static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100206static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
209 bool has_error_code, u32 error_code);
Joerg Roedel92a1f122011-03-25 09:44:51 +0100210static u64 __scale_tsc(u64 ratio, u64 tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800211
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100212enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100213 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
214 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100215 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100216 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100217 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100218 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100219 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100220 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100221 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100222 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100223 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100224 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100225 VMCB_DIRTY_MAX,
226};
227
Joerg Roedel0574dec2010-12-03 11:45:58 +0100228/* TPR and CR2 are always written before VMRUN */
229#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100230
231static inline void mark_all_dirty(struct vmcb *vmcb)
232{
233 vmcb->control.clean = 0;
234}
235
236static inline void mark_all_clean(struct vmcb *vmcb)
237{
238 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
239 & ~VMCB_ALWAYS_DIRTY_MASK;
240}
241
242static inline void mark_dirty(struct vmcb *vmcb, int bit)
243{
244 vmcb->control.clean &= ~(1 << bit);
245}
246
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
248{
249 return container_of(vcpu, struct vcpu_svm, vcpu);
250}
251
Joerg Roedel384c6362010-11-30 18:03:56 +0100252static void recalc_intercepts(struct vcpu_svm *svm)
253{
254 struct vmcb_control_area *c, *h;
255 struct nested_state *g;
256
Joerg Roedel116a0a22010-12-03 11:45:49 +0100257 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
258
Joerg Roedel384c6362010-11-30 18:03:56 +0100259 if (!is_guest_mode(&svm->vcpu))
260 return;
261
262 c = &svm->vmcb->control;
263 h = &svm->nested.hsave->control;
264 g = &svm->nested;
265
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100266 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100267 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100268 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
269 c->intercept = h->intercept | g->intercept;
270}
271
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100272static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
273{
274 if (is_guest_mode(&svm->vcpu))
275 return svm->nested.hsave;
276 else
277 return svm->vmcb;
278}
279
280static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
281{
282 struct vmcb *vmcb = get_host_vmcb(svm);
283
284 vmcb->control.intercept_cr |= (1U << bit);
285
286 recalc_intercepts(svm);
287}
288
289static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
290{
291 struct vmcb *vmcb = get_host_vmcb(svm);
292
293 vmcb->control.intercept_cr &= ~(1U << bit);
294
295 recalc_intercepts(svm);
296}
297
298static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
299{
300 struct vmcb *vmcb = get_host_vmcb(svm);
301
302 return vmcb->control.intercept_cr & (1U << bit);
303}
304
Joerg Roedel3aed0412010-11-30 18:03:58 +0100305static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
306{
307 struct vmcb *vmcb = get_host_vmcb(svm);
308
309 vmcb->control.intercept_dr |= (1U << bit);
310
311 recalc_intercepts(svm);
312}
313
314static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
315{
316 struct vmcb *vmcb = get_host_vmcb(svm);
317
318 vmcb->control.intercept_dr &= ~(1U << bit);
319
320 recalc_intercepts(svm);
321}
322
Joerg Roedel18c918c2010-11-30 18:03:59 +0100323static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
324{
325 struct vmcb *vmcb = get_host_vmcb(svm);
326
327 vmcb->control.intercept_exceptions |= (1U << bit);
328
329 recalc_intercepts(svm);
330}
331
332static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
333{
334 struct vmcb *vmcb = get_host_vmcb(svm);
335
336 vmcb->control.intercept_exceptions &= ~(1U << bit);
337
338 recalc_intercepts(svm);
339}
340
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100341static inline void set_intercept(struct vcpu_svm *svm, int bit)
342{
343 struct vmcb *vmcb = get_host_vmcb(svm);
344
345 vmcb->control.intercept |= (1ULL << bit);
346
347 recalc_intercepts(svm);
348}
349
350static inline void clr_intercept(struct vcpu_svm *svm, int bit)
351{
352 struct vmcb *vmcb = get_host_vmcb(svm);
353
354 vmcb->control.intercept &= ~(1ULL << bit);
355
356 recalc_intercepts(svm);
357}
358
Joerg Roedel2af91942009-08-07 11:49:28 +0200359static inline void enable_gif(struct vcpu_svm *svm)
360{
361 svm->vcpu.arch.hflags |= HF_GIF_MASK;
362}
363
364static inline void disable_gif(struct vcpu_svm *svm)
365{
366 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
367}
368
369static inline bool gif_set(struct vcpu_svm *svm)
370{
371 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
372}
373
Avi Kivity6aa8b732006-12-10 02:21:36 -0800374static unsigned long iopm_base;
375
376struct kvm_ldttss_desc {
377 u16 limit0;
378 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100379 unsigned base1:8, type:5, dpl:2, p:1;
380 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381 u32 base3;
382 u32 zero1;
383} __attribute__((packed));
384
385struct svm_cpu_data {
386 int cpu;
387
Avi Kivity5008fdf2007-04-02 13:05:50 +0300388 u64 asid_generation;
389 u32 max_asid;
390 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391 struct kvm_ldttss_desc *tss_desc;
392
393 struct page *save_area;
394};
395
396static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
397
398struct svm_init_data {
399 int cpu;
400 int r;
401};
402
Mathias Krause09941fb2012-08-30 01:30:20 +0200403static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200405#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406#define MSRS_RANGE_SIZE 2048
407#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
408
Joerg Roedel455716f2010-03-01 15:34:35 +0100409static u32 svm_msrpm_offset(u32 msr)
410{
411 u32 offset;
412 int i;
413
414 for (i = 0; i < NUM_MSR_MAPS; i++) {
415 if (msr < msrpm_ranges[i] ||
416 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
417 continue;
418
419 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
420 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
421
422 /* Now we have the u8 offset - but need the u32 offset */
423 return offset / 4;
424 }
425
426 /* MSR not in any range */
427 return MSR_INVALID;
428}
429
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430#define MAX_INST_SIZE 15
431
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432static inline void clgi(void)
433{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300434 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435}
436
437static inline void stgi(void)
438{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300439 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440}
441
442static inline void invlpga(unsigned long addr, u32 asid)
443{
Joerg Roedele0231712010-02-24 18:59:10 +0100444 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445}
446
Joerg Roedel4b161842010-09-10 17:31:03 +0200447static int get_npt_level(void)
448{
449#ifdef CONFIG_X86_64
450 return PT64_ROOT_LEVEL;
451#else
452 return PT32E_ROOT_LEVEL;
453#endif
454}
455
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
457{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000458 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100459 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600460 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461
Alexander Graf9962d032008-11-25 20:17:02 +0100462 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100463 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464}
465
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466static int is_external_interrupt(u32 info)
467{
468 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
469 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
470}
471
Glauber Costa2809f5d2009-05-12 16:21:05 -0400472static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
473{
474 struct vcpu_svm *svm = to_svm(vcpu);
475 u32 ret = 0;
476
477 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100478 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400479 return ret & mask;
480}
481
482static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
483{
484 struct vcpu_svm *svm = to_svm(vcpu);
485
486 if (mask == 0)
487 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
488 else
489 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
490
491}
492
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
494{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400495 struct vcpu_svm *svm = to_svm(vcpu);
496
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200497 if (svm->vmcb->control.next_rip != 0)
498 svm->next_rip = svm->vmcb->control.next_rip;
499
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400500 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100501 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300502 EMULATE_DONE)
503 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 return;
505 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300506 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
507 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
508 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300510 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400511 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512}
513
Jan Kiszka116a4752010-02-23 17:47:54 +0100514static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200515 bool has_error_code, u32 error_code,
516 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100517{
518 struct vcpu_svm *svm = to_svm(vcpu);
519
Joerg Roedele0231712010-02-24 18:59:10 +0100520 /*
521 * If we are within a nested VM we'd better #VMEXIT and let the guest
522 * handle the exception
523 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200524 if (!reinject &&
525 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100526 return;
527
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200528 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100529 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
530
531 /*
532 * For guest debugging where we have to reinject #BP if some
533 * INT3 is guest-owned:
534 * Emulate nRIP by moving RIP forward. Will fail if injection
535 * raises a fault that is not intercepted. Still better than
536 * failing in all cases.
537 */
538 skip_emulated_instruction(&svm->vcpu);
539 rip = kvm_rip_read(&svm->vcpu);
540 svm->int3_rip = rip + svm->vmcb->save.cs.base;
541 svm->int3_injected = rip - old_rip;
542 }
543
Jan Kiszka116a4752010-02-23 17:47:54 +0100544 svm->vmcb->control.event_inj = nr
545 | SVM_EVTINJ_VALID
546 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
547 | SVM_EVTINJ_TYPE_EXEPT;
548 svm->vmcb->control.event_inj_err = error_code;
549}
550
Joerg Roedel67ec6602010-05-17 14:43:35 +0200551static void svm_init_erratum_383(void)
552{
553 u32 low, high;
554 int err;
555 u64 val;
556
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200557 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200558 return;
559
560 /* Use _safe variants to not break nested virtualization */
561 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
562 if (err)
563 return;
564
565 val |= (1ULL << 47);
566
567 low = lower_32_bits(val);
568 high = upper_32_bits(val);
569
570 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
571
572 erratum_383_found = true;
573}
574
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500575static void svm_init_osvw(struct kvm_vcpu *vcpu)
576{
577 /*
578 * Guests should see errata 400 and 415 as fixed (assuming that
579 * HLT and IO instructions are intercepted).
580 */
581 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
582 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
583
584 /*
585 * By increasing VCPU's osvw.length to 3 we are telling the guest that
586 * all osvw.status bits inside that length, including bit 0 (which is
587 * reserved for erratum 298), are valid. However, if host processor's
588 * osvw_len is 0 then osvw_status[0] carries no information. We need to
589 * be conservative here and therefore we tell the guest that erratum 298
590 * is present (because we really don't know).
591 */
592 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
593 vcpu->arch.osvw.status |= 1;
594}
595
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596static int has_svm(void)
597{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200598 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599
Eduardo Habkost63d11422008-11-17 19:03:20 -0200600 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800601 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800602 return 0;
603 }
604
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605 return 1;
606}
607
608static void svm_hardware_disable(void *garbage)
609{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100610 /* Make sure we clean up behind us */
611 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
612 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
613
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200614 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100615
616 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617}
618
Alexander Graf10474ae2009-09-15 11:37:46 +0200619static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620{
621
Tejun Heo0fe1e002009-10-29 22:34:14 +0900622 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200624 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625 struct desc_struct *gdt;
626 int me = raw_smp_processor_id();
627
Alexander Graf10474ae2009-09-15 11:37:46 +0200628 rdmsrl(MSR_EFER, efer);
629 if (efer & EFER_SVME)
630 return -EBUSY;
631
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200633 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200634 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800635 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900636 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900637 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200638 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200639 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800640 }
641
Tejun Heo0fe1e002009-10-29 22:34:14 +0900642 sd->asid_generation = 1;
643 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
644 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200646 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200647 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900648 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649
Alexander Graf9962d032008-11-25 20:17:02 +0100650 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651
Linus Torvaldsd0316552009-12-14 09:58:24 -0800652 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200653
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100654 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
655 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
656 __get_cpu_var(current_tsc_ratio) = TSC_RATIO_DEFAULT;
657 }
658
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500659
660 /*
661 * Get OSVW bits.
662 *
663 * Note that it is possible to have a system with mixed processor
664 * revisions and therefore different OSVW bits. If bits are not the same
665 * on different processors then choose the worst case (i.e. if erratum
666 * is present on one processor and not on another then assume that the
667 * erratum is present everywhere).
668 */
669 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
670 uint64_t len, status = 0;
671 int err;
672
673 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
674 if (!err)
675 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
676 &err);
677
678 if (err)
679 osvw_status = osvw_len = 0;
680 else {
681 if (len < osvw_len)
682 osvw_len = len;
683 osvw_status |= status;
684 osvw_status &= (1ULL << osvw_len) - 1;
685 }
686 } else
687 osvw_status = osvw_len = 0;
688
Joerg Roedel67ec6602010-05-17 14:43:35 +0200689 svm_init_erratum_383();
690
Joerg Roedel1018faa2012-02-29 14:57:32 +0100691 amd_pmu_enable_virt();
692
Alexander Graf10474ae2009-09-15 11:37:46 +0200693 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694}
695
Joerg Roedel0da1db752008-07-02 16:02:11 +0200696static void svm_cpu_uninit(int cpu)
697{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900698 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200699
Tejun Heo0fe1e002009-10-29 22:34:14 +0900700 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200701 return;
702
703 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900704 __free_page(sd->save_area);
705 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200706}
707
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708static int svm_cpu_init(int cpu)
709{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900710 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800711 int r;
712
Tejun Heo0fe1e002009-10-29 22:34:14 +0900713 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
714 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900716 sd->cpu = cpu;
717 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800718 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900719 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720 goto err_1;
721
Tejun Heo0fe1e002009-10-29 22:34:14 +0900722 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723
724 return 0;
725
726err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900727 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 return r;
729
730}
731
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100732static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733{
734 int i;
735
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100736 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
737 if (direct_access_msrs[i].index == index)
738 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100740 return false;
741}
742
Avi Kivity6aa8b732006-12-10 02:21:36 -0800743static void set_msr_interception(u32 *msrpm, unsigned msr,
744 int read, int write)
745{
Joerg Roedel455716f2010-03-01 15:34:35 +0100746 u8 bit_read, bit_write;
747 unsigned long tmp;
748 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100750 /*
751 * If this warning triggers extend the direct_access_msrs list at the
752 * beginning of the file
753 */
754 WARN_ON(!valid_msr_intercept(msr));
755
Joerg Roedel455716f2010-03-01 15:34:35 +0100756 offset = svm_msrpm_offset(msr);
757 bit_read = 2 * (msr & 0x0f);
758 bit_write = 2 * (msr & 0x0f) + 1;
759 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760
Joerg Roedel455716f2010-03-01 15:34:35 +0100761 BUG_ON(offset == MSR_INVALID);
762
763 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
764 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
765
766 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767}
768
Joerg Roedelf65c2292008-02-13 18:58:46 +0100769static void svm_vcpu_init_msrpm(u32 *msrpm)
770{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100771 int i;
772
Joerg Roedelf65c2292008-02-13 18:58:46 +0100773 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
774
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100775 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
776 if (!direct_access_msrs[i].always)
777 continue;
778
779 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
780 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100781}
782
Joerg Roedel323c3d82010-03-01 15:34:37 +0100783static void add_msr_offset(u32 offset)
784{
785 int i;
786
787 for (i = 0; i < MSRPM_OFFSETS; ++i) {
788
789 /* Offset already in list? */
790 if (msrpm_offsets[i] == offset)
791 return;
792
793 /* Slot used by another offset? */
794 if (msrpm_offsets[i] != MSR_INVALID)
795 continue;
796
797 /* Add offset to list */
798 msrpm_offsets[i] = offset;
799
800 return;
801 }
802
803 /*
804 * If this BUG triggers the msrpm_offsets table has an overflow. Just
805 * increase MSRPM_OFFSETS in this case.
806 */
807 BUG();
808}
809
810static void init_msrpm_offsets(void)
811{
812 int i;
813
814 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
815
816 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
817 u32 offset;
818
819 offset = svm_msrpm_offset(direct_access_msrs[i].index);
820 BUG_ON(offset == MSR_INVALID);
821
822 add_msr_offset(offset);
823 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824}
825
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100826static void svm_enable_lbrv(struct vcpu_svm *svm)
827{
828 u32 *msrpm = svm->msrpm;
829
830 svm->vmcb->control.lbr_ctl = 1;
831 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
832 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
833 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
834 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
835}
836
837static void svm_disable_lbrv(struct vcpu_svm *svm)
838{
839 u32 *msrpm = svm->msrpm;
840
841 svm->vmcb->control.lbr_ctl = 0;
842 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
843 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
844 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
845 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
846}
847
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848static __init int svm_hardware_setup(void)
849{
850 int cpu;
851 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100852 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853 int r;
854
Avi Kivity6aa8b732006-12-10 02:21:36 -0800855 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
856
857 if (!iopm_pages)
858 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300859
860 iopm_va = page_address(iopm_pages);
861 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
863
Joerg Roedel323c3d82010-03-01 15:34:37 +0100864 init_msrpm_offsets();
865
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100866 if (boot_cpu_has(X86_FEATURE_NX))
867 kvm_enable_efer_bits(EFER_NX);
868
Alexander Graf1b2fd702009-02-02 16:23:51 +0100869 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
870 kvm_enable_efer_bits(EFER_FFXSR);
871
Joerg Roedel92a1f122011-03-25 09:44:51 +0100872 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
873 u64 max;
874
875 kvm_has_tsc_control = true;
876
877 /*
878 * Make sure the user can only configure tsc_khz values that
879 * fit into a signed integer.
880 * A min value is not calculated needed because it will always
881 * be 1 on all machines and a value of 0 is used to disable
882 * tsc-scaling for the vcpu.
883 */
884 max = min(0x7fffffffULL, __scale_tsc(tsc_khz, TSC_RATIO_MAX));
885
886 kvm_max_guest_tsc_khz = max;
887 }
888
Alexander Graf236de052008-11-25 20:17:10 +0100889 if (nested) {
890 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200891 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100892 }
893
Zachary Amsden3230bb42009-09-29 11:38:37 -1000894 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895 r = svm_cpu_init(cpu);
896 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100897 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800898 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100899
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200900 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100901 npt_enabled = false;
902
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100903 if (npt_enabled && !npt) {
904 printk(KERN_INFO "kvm: Nested Paging disabled\n");
905 npt_enabled = false;
906 }
907
Joerg Roedel18552672008-02-07 13:47:41 +0100908 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100909 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100910 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200911 } else
912 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100913
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 return 0;
915
Joerg Roedelf65c2292008-02-13 18:58:46 +0100916err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
918 iopm_base = 0;
919 return r;
920}
921
922static __exit void svm_hardware_unsetup(void)
923{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200924 int cpu;
925
Zachary Amsden3230bb42009-09-29 11:38:37 -1000926 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200927 svm_cpu_uninit(cpu);
928
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100930 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800931}
932
933static void init_seg(struct vmcb_seg *seg)
934{
935 seg->selector = 0;
936 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100937 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 seg->limit = 0xffff;
939 seg->base = 0;
940}
941
942static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
943{
944 seg->selector = 0;
945 seg->attrib = SVM_SELECTOR_P_MASK | type;
946 seg->limit = 0xffff;
947 seg->base = 0;
948}
949
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100950static u64 __scale_tsc(u64 ratio, u64 tsc)
951{
952 u64 mult, frac, _tsc;
953
954 mult = ratio >> 32;
955 frac = ratio & ((1ULL << 32) - 1);
956
957 _tsc = tsc;
958 _tsc *= mult;
959 _tsc += (tsc >> 32) * frac;
960 _tsc += ((tsc & ((1ULL << 32) - 1)) * frac) >> 32;
961
962 return _tsc;
963}
964
965static u64 svm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
966{
967 struct vcpu_svm *svm = to_svm(vcpu);
968 u64 _tsc = tsc;
969
970 if (svm->tsc_ratio != TSC_RATIO_DEFAULT)
971 _tsc = __scale_tsc(svm->tsc_ratio, tsc);
972
973 return _tsc;
974}
975
Zachary Amsdencc578282012-02-03 15:43:50 -0200976static void svm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +0100977{
978 struct vcpu_svm *svm = to_svm(vcpu);
979 u64 ratio;
980 u64 khz;
981
Zachary Amsdencc578282012-02-03 15:43:50 -0200982 /* Guest TSC same frequency as host TSC? */
983 if (!scale) {
Joerg Roedel4051b182011-03-25 09:44:49 +0100984 svm->tsc_ratio = TSC_RATIO_DEFAULT;
985 return;
986 }
987
Zachary Amsdencc578282012-02-03 15:43:50 -0200988 /* TSC scaling supported? */
989 if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
990 if (user_tsc_khz > tsc_khz) {
991 vcpu->arch.tsc_catchup = 1;
992 vcpu->arch.tsc_always_catchup = 1;
993 } else
994 WARN(1, "user requested TSC rate below hardware speed\n");
995 return;
996 }
997
Joerg Roedel4051b182011-03-25 09:44:49 +0100998 khz = user_tsc_khz;
999
1000 /* TSC scaling required - calculate ratio */
1001 ratio = khz << 32;
1002 do_div(ratio, tsc_khz);
1003
1004 if (ratio == 0 || ratio & TSC_RATIO_RSVD) {
1005 WARN_ONCE(1, "Invalid TSC ratio - virtual-tsc-khz=%u\n",
1006 user_tsc_khz);
1007 return;
1008 }
Joerg Roedel4051b182011-03-25 09:44:49 +01001009 svm->tsc_ratio = ratio;
1010}
1011
Will Auldba904632012-11-29 12:42:50 -08001012static u64 svm_read_tsc_offset(struct kvm_vcpu *vcpu)
1013{
1014 struct vcpu_svm *svm = to_svm(vcpu);
1015
1016 return svm->vmcb->control.tsc_offset;
1017}
1018
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001019static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1020{
1021 struct vcpu_svm *svm = to_svm(vcpu);
1022 u64 g_tsc_offset = 0;
1023
Joerg Roedel20307532010-11-29 17:51:48 +01001024 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001025 g_tsc_offset = svm->vmcb->control.tsc_offset -
1026 svm->nested.hsave->control.tsc_offset;
1027 svm->nested.hsave->control.tsc_offset = offset;
1028 }
1029
1030 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001031
1032 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001033}
1034
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001035static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10001036{
1037 struct vcpu_svm *svm = to_svm(vcpu);
1038
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001039 WARN_ON(adjustment < 0);
1040 if (host)
1041 adjustment = svm_scale_tsc(vcpu, adjustment);
1042
Zachary Amsdene48672f2010-08-19 22:07:23 -10001043 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +01001044 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -10001045 svm->nested.hsave->control.tsc_offset += adjustment;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001046 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -10001047}
1048
Joerg Roedel857e4092011-03-25 09:44:50 +01001049static u64 svm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1050{
1051 u64 tsc;
1052
1053 tsc = svm_scale_tsc(vcpu, native_read_tsc());
1054
1055 return target_tsc - tsc;
1056}
1057
Joerg Roedele6101a92008-02-13 18:58:45 +01001058static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059{
Joerg Roedele6101a92008-02-13 18:58:45 +01001060 struct vmcb_control_area *control = &svm->vmcb->control;
1061 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062
Avi Kivitybff78272010-01-07 13:16:08 +02001063 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001064 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001065
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001066 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1067 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1068 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1069 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1070 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1071 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
1072 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073
Joerg Roedel3aed0412010-11-30 18:03:58 +01001074 set_dr_intercept(svm, INTERCEPT_DR0_READ);
1075 set_dr_intercept(svm, INTERCEPT_DR1_READ);
1076 set_dr_intercept(svm, INTERCEPT_DR2_READ);
1077 set_dr_intercept(svm, INTERCEPT_DR3_READ);
1078 set_dr_intercept(svm, INTERCEPT_DR4_READ);
1079 set_dr_intercept(svm, INTERCEPT_DR5_READ);
1080 set_dr_intercept(svm, INTERCEPT_DR6_READ);
1081 set_dr_intercept(svm, INTERCEPT_DR7_READ);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001082
Joerg Roedel3aed0412010-11-30 18:03:58 +01001083 set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
1084 set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
1085 set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
1086 set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
1087 set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
1088 set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
1089 set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
1090 set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001091
Joerg Roedel18c918c2010-11-30 18:03:59 +01001092 set_exception_intercept(svm, PF_VECTOR);
1093 set_exception_intercept(svm, UD_VECTOR);
1094 set_exception_intercept(svm, MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001096 set_intercept(svm, INTERCEPT_INTR);
1097 set_intercept(svm, INTERCEPT_NMI);
1098 set_intercept(svm, INTERCEPT_SMI);
1099 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001100 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001101 set_intercept(svm, INTERCEPT_CPUID);
1102 set_intercept(svm, INTERCEPT_INVD);
1103 set_intercept(svm, INTERCEPT_HLT);
1104 set_intercept(svm, INTERCEPT_INVLPG);
1105 set_intercept(svm, INTERCEPT_INVLPGA);
1106 set_intercept(svm, INTERCEPT_IOIO_PROT);
1107 set_intercept(svm, INTERCEPT_MSR_PROT);
1108 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1109 set_intercept(svm, INTERCEPT_SHUTDOWN);
1110 set_intercept(svm, INTERCEPT_VMRUN);
1111 set_intercept(svm, INTERCEPT_VMMCALL);
1112 set_intercept(svm, INTERCEPT_VMLOAD);
1113 set_intercept(svm, INTERCEPT_VMSAVE);
1114 set_intercept(svm, INTERCEPT_STGI);
1115 set_intercept(svm, INTERCEPT_CLGI);
1116 set_intercept(svm, INTERCEPT_SKINIT);
1117 set_intercept(svm, INTERCEPT_WBINVD);
1118 set_intercept(svm, INTERCEPT_MONITOR);
1119 set_intercept(svm, INTERCEPT_MWAIT);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001120 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121
1122 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001123 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124 control->int_ctl = V_INTR_MASKING_MASK;
1125
1126 init_seg(&save->es);
1127 init_seg(&save->ss);
1128 init_seg(&save->ds);
1129 init_seg(&save->fs);
1130 init_seg(&save->gs);
1131
1132 save->cs.selector = 0xf000;
1133 /* Executable/Readable Code Segment */
1134 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1135 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1136 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -08001137 /*
1138 * cs.base should really be 0xffff0000, but vmx can't handle that, so
1139 * be consistent with it.
1140 *
1141 * Replace when we have real mode working for vmx.
1142 */
1143 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144
1145 save->gdtr.limit = 0xffff;
1146 save->idtr.limit = 0xffff;
1147
1148 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1149 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1150
Marcelo Tosattieaa48512010-08-31 19:13:14 -03001151 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001152 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001153 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001155 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156
Joerg Roedele0231712010-02-24 18:59:10 +01001157 /*
1158 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001159 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -03001161 svm->vcpu.arch.cr0 = 0;
1162 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001163
Rusty Russell66aee912007-07-17 23:34:16 +10001164 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001166
1167 if (npt_enabled) {
1168 /* Setup VMCB for Nested Paging */
1169 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001170 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001171 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001172 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1173 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001174 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001175 save->cr3 = 0;
1176 save->cr4 = 0;
1177 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001178 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001179
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001180 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001181 svm->vcpu.arch.hflags = 0;
1182
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001183 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001184 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001185 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001186 }
1187
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001188 mark_all_dirty(svm->vmcb);
1189
Joerg Roedel2af91942009-08-07 11:49:28 +02001190 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191}
1192
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001193static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001194{
1195 struct vcpu_svm *svm = to_svm(vcpu);
1196
Joerg Roedele6101a92008-02-13 18:58:45 +01001197 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001198
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001199 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001200 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001201 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
1202 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +02001203 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001204 vcpu->arch.regs_avail = ~0;
1205 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001206
1207 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001208}
1209
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001210static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001212 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001214 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001215 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001216 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001217 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218
Rusty Russellc16f8622007-07-30 21:12:19 +10001219 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001220 if (!svm) {
1221 err = -ENOMEM;
1222 goto out;
1223 }
1224
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001225 svm->tsc_ratio = TSC_RATIO_DEFAULT;
1226
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001227 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1228 if (err)
1229 goto free_svm;
1230
Joerg Roedelf65c2292008-02-13 18:58:46 +01001231 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001232 page = alloc_page(GFP_KERNEL);
1233 if (!page)
1234 goto uninit;
1235
Joerg Roedelf65c2292008-02-13 18:58:46 +01001236 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1237 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001238 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001239
1240 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1241 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001242 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001243
Alexander Grafb286d5d2008-11-25 20:17:05 +01001244 hsave_page = alloc_page(GFP_KERNEL);
1245 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001246 goto free_page3;
1247
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001248 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001249
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001250 svm->msrpm = page_address(msrpm_pages);
1251 svm_vcpu_init_msrpm(svm->msrpm);
1252
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001253 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001254 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001255
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001256 svm->vmcb = page_address(page);
1257 clear_page(svm->vmcb);
1258 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1259 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +01001260 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001261
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001262 err = fx_init(&svm->vcpu);
1263 if (err)
1264 goto free_page4;
1265
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001266 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001267 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001268 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001270 svm_init_osvw(&svm->vcpu);
1271
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001272 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001273
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001274free_page4:
1275 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001276free_page3:
1277 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1278free_page2:
1279 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1280free_page1:
1281 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001282uninit:
1283 kvm_vcpu_uninit(&svm->vcpu);
1284free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001285 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001286out:
1287 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288}
1289
1290static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1291{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001292 struct vcpu_svm *svm = to_svm(vcpu);
1293
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001294 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001295 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001296 __free_page(virt_to_page(svm->nested.hsave));
1297 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001298 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001299 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300}
1301
Avi Kivity15ad7142007-07-11 18:17:21 +03001302static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001304 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001305 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001306
Avi Kivity0cc50642007-03-25 12:07:27 +02001307 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001308 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001309 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001310 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001311
Avi Kivity82ca2d12010-10-21 12:20:34 +02001312#ifdef CONFIG_X86_64
1313 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1314#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001315 savesegment(fs, svm->host.fs);
1316 savesegment(gs, svm->host.gs);
1317 svm->host.ldt = kvm_read_ldt();
1318
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001319 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001320 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001321
1322 if (static_cpu_has(X86_FEATURE_TSCRATEMSR) &&
1323 svm->tsc_ratio != __get_cpu_var(current_tsc_ratio)) {
1324 __get_cpu_var(current_tsc_ratio) = svm->tsc_ratio;
1325 wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_ratio);
1326 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327}
1328
1329static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1330{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001331 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001332 int i;
1333
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001334 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001335 kvm_load_ldt(svm->host.ldt);
1336#ifdef CONFIG_X86_64
1337 loadsegment(fs, svm->host.fs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001338 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001339 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001340#else
Avi Kivity831ca602011-03-08 16:09:51 +02001341#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001342 loadsegment(gs, svm->host.gs);
1343#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001344#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001345 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001346 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347}
1348
Kevin Wolfea5e97e2012-02-08 14:34:40 +01001349static void svm_update_cpl(struct kvm_vcpu *vcpu)
1350{
1351 struct vcpu_svm *svm = to_svm(vcpu);
1352 int cpl;
1353
1354 if (!is_protmode(vcpu))
1355 cpl = 0;
1356 else if (svm->vmcb->save.rflags & X86_EFLAGS_VM)
1357 cpl = 3;
1358 else
1359 cpl = svm->vmcb->save.cs.selector & 0x3;
1360
1361 svm->vmcb->save.cpl = cpl;
1362}
1363
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1365{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001366 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367}
1368
1369static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1370{
Kevin Wolf4cee4792012-02-08 14:34:41 +01001371 unsigned long old_rflags = to_svm(vcpu)->vmcb->save.rflags;
1372
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001373 to_svm(vcpu)->vmcb->save.rflags = rflags;
Kevin Wolf4cee4792012-02-08 14:34:41 +01001374 if ((old_rflags ^ rflags) & X86_EFLAGS_VM)
1375 svm_update_cpl(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376}
1377
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001378static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1379{
1380 switch (reg) {
1381 case VCPU_EXREG_PDPTR:
1382 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001383 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001384 break;
1385 default:
1386 BUG();
1387 }
1388}
1389
Alexander Graff0b85052008-11-25 20:17:01 +01001390static void svm_set_vintr(struct vcpu_svm *svm)
1391{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001392 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001393}
1394
1395static void svm_clear_vintr(struct vcpu_svm *svm)
1396{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001397 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001398}
1399
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1401{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001402 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403
1404 switch (seg) {
1405 case VCPU_SREG_CS: return &save->cs;
1406 case VCPU_SREG_DS: return &save->ds;
1407 case VCPU_SREG_ES: return &save->es;
1408 case VCPU_SREG_FS: return &save->fs;
1409 case VCPU_SREG_GS: return &save->gs;
1410 case VCPU_SREG_SS: return &save->ss;
1411 case VCPU_SREG_TR: return &save->tr;
1412 case VCPU_SREG_LDTR: return &save->ldtr;
1413 }
1414 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001415 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416}
1417
1418static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1419{
1420 struct vmcb_seg *s = svm_seg(vcpu, seg);
1421
1422 return s->base;
1423}
1424
1425static void svm_get_segment(struct kvm_vcpu *vcpu,
1426 struct kvm_segment *var, int seg)
1427{
1428 struct vmcb_seg *s = svm_seg(vcpu, seg);
1429
1430 var->base = s->base;
1431 var->limit = s->limit;
1432 var->selector = s->selector;
1433 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1434 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1435 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1436 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1437 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1438 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1439 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1440 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001441
Joerg Roedele0231712010-02-24 18:59:10 +01001442 /*
1443 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001444 * for cross vendor migration purposes by "not present"
1445 */
1446 var->unusable = !var->present || (var->type == 0);
1447
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001448 switch (seg) {
1449 case VCPU_SREG_CS:
1450 /*
1451 * SVM always stores 0 for the 'G' bit in the CS selector in
1452 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1453 * Intel's VMENTRY has a check on the 'G' bit.
1454 */
Amit Shah25022ac2008-10-27 09:04:17 +00001455 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001456 break;
1457 case VCPU_SREG_TR:
1458 /*
1459 * Work around a bug where the busy flag in the tr selector
1460 * isn't exposed
1461 */
Amit Shahc0d09822008-10-27 09:04:18 +00001462 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001463 break;
1464 case VCPU_SREG_DS:
1465 case VCPU_SREG_ES:
1466 case VCPU_SREG_FS:
1467 case VCPU_SREG_GS:
1468 /*
1469 * The accessed bit must always be set in the segment
1470 * descriptor cache, although it can be cleared in the
1471 * descriptor, the cached bit always remains at 1. Since
1472 * Intel has a check on this, set it here to support
1473 * cross-vendor migration.
1474 */
1475 if (!var->unusable)
1476 var->type |= 0x1;
1477 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001478 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001479 /*
1480 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001481 * descriptor is left as 1, although the whole segment has
1482 * been made unusable. Clear it here to pass an Intel VMX
1483 * entry check when cross vendor migrating.
1484 */
1485 if (var->unusable)
1486 var->db = 0;
1487 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001488 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001489}
1490
Izik Eidus2e4d2652008-03-24 19:38:34 +02001491static int svm_get_cpl(struct kvm_vcpu *vcpu)
1492{
1493 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1494
1495 return save->cpl;
1496}
1497
Gleb Natapov89a27f42010-02-16 10:51:48 +02001498static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001500 struct vcpu_svm *svm = to_svm(vcpu);
1501
Gleb Natapov89a27f42010-02-16 10:51:48 +02001502 dt->size = svm->vmcb->save.idtr.limit;
1503 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504}
1505
Gleb Natapov89a27f42010-02-16 10:51:48 +02001506static void svm_set_idt(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 svm->vmcb->save.idtr.limit = dt->size;
1511 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001512 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513}
1514
Gleb Natapov89a27f42010-02-16 10:51:48 +02001515static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001517 struct vcpu_svm *svm = to_svm(vcpu);
1518
Gleb Natapov89a27f42010-02-16 10:51:48 +02001519 dt->size = svm->vmcb->save.gdtr.limit;
1520 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521}
1522
Gleb Natapov89a27f42010-02-16 10:51:48 +02001523static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001525 struct vcpu_svm *svm = to_svm(vcpu);
1526
Gleb Natapov89a27f42010-02-16 10:51:48 +02001527 svm->vmcb->save.gdtr.limit = dt->size;
1528 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001529 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001530}
1531
Avi Kivitye8467fd2009-12-29 18:43:06 +02001532static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1533{
1534}
1535
Avi Kivityaff48ba2010-12-05 18:56:11 +02001536static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1537{
1538}
1539
Anthony Liguori25c4c272007-04-27 09:29:21 +03001540static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001541{
1542}
1543
Avi Kivityd2251572010-01-06 10:55:27 +02001544static void update_cr0_intercept(struct vcpu_svm *svm)
1545{
1546 ulong gcr0 = svm->vcpu.arch.cr0;
1547 u64 *hcr0 = &svm->vmcb->save.cr0;
1548
1549 if (!svm->vcpu.fpu_active)
1550 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1551 else
1552 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1553 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1554
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001555 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001556
1557 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001558 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1559 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001560 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001561 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1562 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001563 }
1564}
1565
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1567{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001568 struct vcpu_svm *svm = to_svm(vcpu);
1569
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001570#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001571 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001572 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001573 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001574 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575 }
1576
Mike Dayd77c26f2007-10-08 09:02:08 -04001577 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001578 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001579 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580 }
1581 }
1582#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001583 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001584
1585 if (!npt_enabled)
1586 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001587
1588 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001589 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001590 /*
1591 * re-enable caching here because the QEMU bios
1592 * does not do it - this results in some delay at
1593 * reboot
1594 */
1595 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001596 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001597 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001598 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599}
1600
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001601static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602{
Joerg Roedel6394b642008-04-09 14:15:29 +02001603 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001604 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1605
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001606 if (cr4 & X86_CR4_VMXE)
1607 return 1;
1608
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001609 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001610 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001611
Joerg Roedelec077262008-04-09 14:15:28 +02001612 vcpu->arch.cr4 = cr4;
1613 if (!npt_enabled)
1614 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001615 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001616 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001617 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001618 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619}
1620
1621static void svm_set_segment(struct kvm_vcpu *vcpu,
1622 struct kvm_segment *var, int seg)
1623{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001624 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625 struct vmcb_seg *s = svm_seg(vcpu, seg);
1626
1627 s->base = var->base;
1628 s->limit = var->limit;
1629 s->selector = var->selector;
1630 if (var->unusable)
1631 s->attrib = 0;
1632 else {
1633 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1634 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1635 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1636 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1637 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1638 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1639 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1640 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1641 }
1642 if (seg == VCPU_SREG_CS)
Kevin Wolfea5e97e2012-02-08 14:34:40 +01001643 svm_update_cpl(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644
Joerg Roedel060d0c92010-12-03 11:45:57 +01001645 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646}
1647
Jan Kiszkac8639012012-09-21 05:42:55 +02001648static void update_db_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001650 struct vcpu_svm *svm = to_svm(vcpu);
1651
Joerg Roedel18c918c2010-11-30 18:03:59 +01001652 clr_exception_intercept(svm, DB_VECTOR);
1653 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001654
Jan Kiszka6be7d302009-10-18 13:24:54 +02001655 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001656 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001657
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001658 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1659 if (vcpu->guest_debug &
1660 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001661 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001662 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001663 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001664 } else
1665 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001666}
1667
Tejun Heo0fe1e002009-10-29 22:34:14 +09001668static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001670 if (sd->next_asid > sd->max_asid) {
1671 ++sd->asid_generation;
1672 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001673 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674 }
1675
Tejun Heo0fe1e002009-10-29 22:34:14 +09001676 svm->asid_generation = sd->asid_generation;
1677 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001678
1679 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680}
1681
Gleb Natapov020df072010-04-13 10:05:23 +03001682static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001684 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001685
Gleb Natapov020df072010-04-13 10:05:23 +03001686 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01001687 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688}
1689
Avi Kivity851ba692009-08-24 11:10:17 +03001690static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691{
Gleb Natapov631bc482010-10-14 11:22:52 +02001692 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001694 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695
Gleb Natapov631bc482010-10-14 11:22:52 +02001696 switch (svm->apf_reason) {
1697 default:
1698 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001699
Gleb Natapov631bc482010-10-14 11:22:52 +02001700 trace_kvm_page_fault(fault_address, error_code);
1701 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1702 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01001703 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1704 svm->vmcb->control.insn_bytes,
1705 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02001706 break;
1707 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1708 svm->apf_reason = 0;
1709 local_irq_disable();
1710 kvm_async_pf_task_wait(fault_address);
1711 local_irq_enable();
1712 break;
1713 case KVM_PV_REASON_PAGE_READY:
1714 svm->apf_reason = 0;
1715 local_irq_disable();
1716 kvm_async_pf_task_wake(fault_address);
1717 local_irq_enable();
1718 break;
1719 }
1720 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721}
1722
Avi Kivity851ba692009-08-24 11:10:17 +03001723static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001724{
Avi Kivity851ba692009-08-24 11:10:17 +03001725 struct kvm_run *kvm_run = svm->vcpu.run;
1726
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001727 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001728 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001729 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001730 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1731 return 1;
1732 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001733
Jan Kiszka6be7d302009-10-18 13:24:54 +02001734 if (svm->nmi_singlestep) {
1735 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001736 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1737 svm->vmcb->save.rflags &=
1738 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkac8639012012-09-21 05:42:55 +02001739 update_db_bp_intercept(&svm->vcpu);
Gleb Natapov44c11432009-05-11 13:35:52 +03001740 }
1741
1742 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001743 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001744 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1745 kvm_run->debug.arch.pc =
1746 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1747 kvm_run->debug.arch.exception = DB_VECTOR;
1748 return 0;
1749 }
1750
1751 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001752}
1753
Avi Kivity851ba692009-08-24 11:10:17 +03001754static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001755{
Avi Kivity851ba692009-08-24 11:10:17 +03001756 struct kvm_run *kvm_run = svm->vcpu.run;
1757
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001758 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1759 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1760 kvm_run->debug.arch.exception = BP_VECTOR;
1761 return 0;
1762}
1763
Avi Kivity851ba692009-08-24 11:10:17 +03001764static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001765{
1766 int er;
1767
Andre Przywara51d8b662010-12-21 11:12:02 +01001768 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001769 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001770 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001771 return 1;
1772}
1773
Avi Kivity6b52d182010-01-21 15:31:47 +02001774static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001775{
Avi Kivity6b52d182010-01-21 15:31:47 +02001776 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001777
Joerg Roedel18c918c2010-11-30 18:03:59 +01001778 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001779
Rusty Russelle756fc62007-07-30 20:07:08 +10001780 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001781 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001782}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001783
Avi Kivity6b52d182010-01-21 15:31:47 +02001784static int nm_interception(struct vcpu_svm *svm)
1785{
1786 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001787 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001788}
1789
Joerg Roedel67ec6602010-05-17 14:43:35 +02001790static bool is_erratum_383(void)
1791{
1792 int err, i;
1793 u64 value;
1794
1795 if (!erratum_383_found)
1796 return false;
1797
1798 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1799 if (err)
1800 return false;
1801
1802 /* Bit 62 may or may not be set for this mce */
1803 value &= ~(1ULL << 62);
1804
1805 if (value != 0xb600000000010015ULL)
1806 return false;
1807
1808 /* Clear MCi_STATUS registers */
1809 for (i = 0; i < 6; ++i)
1810 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1811
1812 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1813 if (!err) {
1814 u32 low, high;
1815
1816 value &= ~(1ULL << 2);
1817 low = lower_32_bits(value);
1818 high = upper_32_bits(value);
1819
1820 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1821 }
1822
1823 /* Flush tlb to evict multi-match entries */
1824 __flush_tlb_all();
1825
1826 return true;
1827}
1828
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001829static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001830{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001831 if (is_erratum_383()) {
1832 /*
1833 * Erratum 383 triggered. Guest state is corrupt so kill the
1834 * guest.
1835 */
1836 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1837
Avi Kivitya8eeb042010-05-10 12:34:53 +03001838 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001839
1840 return;
1841 }
1842
Joerg Roedel53371b52008-04-09 14:15:30 +02001843 /*
1844 * On an #MC intercept the MCE handler is not called automatically in
1845 * the host. So do it by hand here.
1846 */
1847 asm volatile (
1848 "int $0x12\n");
1849 /* not sure if we ever come back to this point */
1850
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001851 return;
1852}
1853
1854static int mc_interception(struct vcpu_svm *svm)
1855{
Joerg Roedel53371b52008-04-09 14:15:30 +02001856 return 1;
1857}
1858
Avi Kivity851ba692009-08-24 11:10:17 +03001859static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001860{
Avi Kivity851ba692009-08-24 11:10:17 +03001861 struct kvm_run *kvm_run = svm->vcpu.run;
1862
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001863 /*
1864 * VMCB is undefined after a SHUTDOWN intercept
1865 * so reinitialize it.
1866 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001867 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001868 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001869
1870 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1871 return 0;
1872}
1873
Avi Kivity851ba692009-08-24 11:10:17 +03001874static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001876 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001877 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001878 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001879 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880
Rusty Russelle756fc62007-07-30 20:07:08 +10001881 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001882 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001883 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001884 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01001885 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001886
Avi Kivity039576c2007-03-20 12:46:50 +02001887 port = io_info >> 16;
1888 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001889 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001890 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001891
1892 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893}
1894
Avi Kivity851ba692009-08-24 11:10:17 +03001895static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001896{
1897 return 1;
1898}
1899
Avi Kivity851ba692009-08-24 11:10:17 +03001900static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001901{
1902 ++svm->vcpu.stat.irq_exits;
1903 return 1;
1904}
1905
Avi Kivity851ba692009-08-24 11:10:17 +03001906static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907{
1908 return 1;
1909}
1910
Avi Kivity851ba692009-08-24 11:10:17 +03001911static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001913 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001914 skip_emulated_instruction(&svm->vcpu);
1915 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916}
1917
Avi Kivity851ba692009-08-24 11:10:17 +03001918static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001919{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001920 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001921 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001922 kvm_emulate_hypercall(&svm->vcpu);
1923 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001924}
1925
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001926static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1927{
1928 struct vcpu_svm *svm = to_svm(vcpu);
1929
1930 return svm->nested.nested_cr3;
1931}
1932
Avi Kivitye4e517b2011-07-28 11:36:17 +03001933static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
1934{
1935 struct vcpu_svm *svm = to_svm(vcpu);
1936 u64 cr3 = svm->nested.nested_cr3;
1937 u64 pdpte;
1938 int ret;
1939
1940 ret = kvm_read_guest_page(vcpu->kvm, gpa_to_gfn(cr3), &pdpte,
1941 offset_in_page(cr3) + index * 8, 8);
1942 if (ret)
1943 return 0;
1944 return pdpte;
1945}
1946
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001947static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1948 unsigned long root)
1949{
1950 struct vcpu_svm *svm = to_svm(vcpu);
1951
1952 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01001953 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001954 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001955}
1956
Avi Kivity6389ee92010-11-29 16:12:30 +02001957static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1958 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001959{
1960 struct vcpu_svm *svm = to_svm(vcpu);
1961
1962 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1963 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001964 svm->vmcb->control.exit_info_1 = fault->error_code;
1965 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001966
1967 nested_svm_vmexit(svm);
1968}
1969
Joerg Roedel4b161842010-09-10 17:31:03 +02001970static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1971{
1972 int r;
1973
1974 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1975
1976 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1977 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
Avi Kivitye4e517b2011-07-28 11:36:17 +03001978 vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
Joerg Roedel4b161842010-09-10 17:31:03 +02001979 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1980 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1981 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1982
1983 return r;
1984}
1985
1986static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1987{
1988 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1989}
1990
Alexander Grafc0725422008-11-25 20:17:03 +01001991static int nested_svm_check_permissions(struct vcpu_svm *svm)
1992{
Avi Kivityf6801df2010-01-21 15:31:50 +02001993 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001994 || !is_paging(&svm->vcpu)) {
1995 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1996 return 1;
1997 }
1998
1999 if (svm->vmcb->save.cpl) {
2000 kvm_inject_gp(&svm->vcpu, 0);
2001 return 1;
2002 }
2003
2004 return 0;
2005}
2006
Alexander Grafcf74a782008-11-25 20:17:08 +01002007static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
2008 bool has_error_code, u32 error_code)
2009{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002010 int vmexit;
2011
Joerg Roedel20307532010-11-29 17:51:48 +01002012 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02002013 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002014
Joerg Roedel0295ad72009-08-07 11:49:37 +02002015 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
2016 svm->vmcb->control.exit_code_hi = 0;
2017 svm->vmcb->control.exit_info_1 = error_code;
2018 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
2019
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002020 vmexit = nested_svm_intercept(svm);
2021 if (vmexit == NESTED_EXIT_DONE)
2022 svm->nested.exit_required = true;
2023
2024 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002025}
2026
Joerg Roedel8fe54652010-02-19 16:23:01 +01002027/* This function returns true if it is save to enable the irq window */
2028static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002029{
Joerg Roedel20307532010-11-29 17:51:48 +01002030 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002031 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002032
Joerg Roedel26666952009-08-07 11:49:46 +02002033 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002034 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002035
Joerg Roedel26666952009-08-07 11:49:46 +02002036 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01002037 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002038
Gleb Natapova0a07cd2010-09-20 10:15:32 +02002039 /*
2040 * if vmexit was already requested (by intercepted exception
2041 * for instance) do not overwrite it with "external interrupt"
2042 * vmexit.
2043 */
2044 if (svm->nested.exit_required)
2045 return false;
2046
Joerg Roedel197717d2010-02-24 18:59:19 +01002047 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
2048 svm->vmcb->control.exit_info_1 = 0;
2049 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02002050
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002051 if (svm->nested.intercept & 1ULL) {
2052 /*
2053 * The #vmexit can't be emulated here directly because this
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002054 * code path runs with irqs and preemption disabled. A
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002055 * #vmexit emulation might sleep. Only signal request for
2056 * the #vmexit here.
2057 */
2058 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02002059 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01002060 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01002061 }
2062
Joerg Roedel8fe54652010-02-19 16:23:01 +01002063 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01002064}
2065
Joerg Roedel887f5002010-02-24 18:59:12 +01002066/* This function returns true if it is save to enable the nmi window */
2067static inline bool nested_svm_nmi(struct vcpu_svm *svm)
2068{
Joerg Roedel20307532010-11-29 17:51:48 +01002069 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01002070 return true;
2071
2072 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
2073 return true;
2074
2075 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
2076 svm->nested.exit_required = true;
2077
2078 return false;
2079}
2080
Joerg Roedel7597f122010-02-19 16:23:00 +01002081static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002082{
2083 struct page *page;
2084
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01002085 might_sleep();
2086
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002087 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002088 if (is_error_page(page))
2089 goto error;
2090
Joerg Roedel7597f122010-02-19 16:23:00 +01002091 *_page = page;
2092
2093 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002094
2095error:
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002096 kvm_inject_gp(&svm->vcpu, 0);
2097
2098 return NULL;
2099}
2100
Joerg Roedel7597f122010-02-19 16:23:00 +01002101static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002102{
Joerg Roedel7597f122010-02-19 16:23:00 +01002103 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002104 kvm_release_page_dirty(page);
2105}
2106
Joerg Roedelce2ac082010-03-01 15:34:39 +01002107static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002108{
Joerg Roedelce2ac082010-03-01 15:34:39 +01002109 unsigned port;
2110 u8 val, bit;
2111 u64 gpa;
2112
2113 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
2114 return NESTED_EXIT_HOST;
2115
2116 port = svm->vmcb->control.exit_info_1 >> 16;
2117 gpa = svm->nested.vmcb_iopm + (port / 8);
2118 bit = port % 8;
2119 val = 0;
2120
2121 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
2122 val &= (1 << bit);
2123
2124 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
2125}
2126
Joerg Roedeld2477822010-03-01 15:34:34 +01002127static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002128{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002129 u32 offset, msr, value;
2130 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002131
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002132 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01002133 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002134
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002135 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2136 offset = svm_msrpm_offset(msr);
2137 write = svm->vmcb->control.exit_info_1 & 1;
2138 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002139
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002140 if (offset == MSR_INVALID)
2141 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002142
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002143 /* Offset is in 32 bit units but need in 8 bit units */
2144 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002145
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002146 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
2147 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002148
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002149 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002150}
2151
Joerg Roedel410e4d52009-08-07 11:49:44 +02002152static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002153{
Alexander Grafcf74a782008-11-25 20:17:08 +01002154 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02002155
Joerg Roedel410e4d52009-08-07 11:49:44 +02002156 switch (exit_code) {
2157 case SVM_EXIT_INTR:
2158 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02002159 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02002160 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002161 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01002162 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02002163 if (npt_enabled)
2164 return NESTED_EXIT_HOST;
2165 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002166 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02002167 /* When we're shadowing, trap PFs, but not async PF */
2168 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002169 return NESTED_EXIT_HOST;
2170 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01002171 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
2172 nm_interception(svm);
2173 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02002174 default:
2175 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01002176 }
2177
Joerg Roedel410e4d52009-08-07 11:49:44 +02002178 return NESTED_EXIT_CONTINUE;
2179}
2180
2181/*
2182 * If this function returns true, this #vmexit was already handled
2183 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002184static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002185{
2186 u32 exit_code = svm->vmcb->control.exit_code;
2187 int vmexit = NESTED_EXIT_HOST;
2188
Alexander Grafcf74a782008-11-25 20:17:08 +01002189 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002190 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02002191 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002192 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002193 case SVM_EXIT_IOIO:
2194 vmexit = nested_svm_intercept_ioio(svm);
2195 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002196 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
2197 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
2198 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002199 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002200 break;
2201 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002202 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2203 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2204 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002205 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002206 break;
2207 }
2208 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2209 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002210 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002211 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002212 /* async page fault always cause vmexit */
2213 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2214 svm->apf_reason != 0)
2215 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002216 break;
2217 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002218 case SVM_EXIT_ERR: {
2219 vmexit = NESTED_EXIT_DONE;
2220 break;
2221 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002222 default: {
2223 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002224 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002225 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002226 }
2227 }
2228
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002229 return vmexit;
2230}
2231
2232static int nested_svm_exit_handled(struct vcpu_svm *svm)
2233{
2234 int vmexit;
2235
2236 vmexit = nested_svm_intercept(svm);
2237
2238 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002239 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002240
2241 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002242}
2243
Joerg Roedel0460a972009-08-07 11:49:31 +02002244static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2245{
2246 struct vmcb_control_area *dst = &dst_vmcb->control;
2247 struct vmcb_control_area *from = &from_vmcb->control;
2248
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002249 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002250 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002251 dst->intercept_exceptions = from->intercept_exceptions;
2252 dst->intercept = from->intercept;
2253 dst->iopm_base_pa = from->iopm_base_pa;
2254 dst->msrpm_base_pa = from->msrpm_base_pa;
2255 dst->tsc_offset = from->tsc_offset;
2256 dst->asid = from->asid;
2257 dst->tlb_ctl = from->tlb_ctl;
2258 dst->int_ctl = from->int_ctl;
2259 dst->int_vector = from->int_vector;
2260 dst->int_state = from->int_state;
2261 dst->exit_code = from->exit_code;
2262 dst->exit_code_hi = from->exit_code_hi;
2263 dst->exit_info_1 = from->exit_info_1;
2264 dst->exit_info_2 = from->exit_info_2;
2265 dst->exit_int_info = from->exit_int_info;
2266 dst->exit_int_info_err = from->exit_int_info_err;
2267 dst->nested_ctl = from->nested_ctl;
2268 dst->event_inj = from->event_inj;
2269 dst->event_inj_err = from->event_inj_err;
2270 dst->nested_cr3 = from->nested_cr3;
2271 dst->lbr_ctl = from->lbr_ctl;
2272}
2273
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002274static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002275{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002276 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002277 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002278 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002279 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002280
Joerg Roedel17897f32009-10-09 16:08:29 +02002281 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2282 vmcb->control.exit_info_1,
2283 vmcb->control.exit_info_2,
2284 vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01002285 vmcb->control.exit_int_info_err,
2286 KVM_ISA_SVM);
Joerg Roedel17897f32009-10-09 16:08:29 +02002287
Joerg Roedel7597f122010-02-19 16:23:00 +01002288 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002289 if (!nested_vmcb)
2290 return 1;
2291
Joerg Roedel20307532010-11-29 17:51:48 +01002292 /* Exit Guest-Mode */
2293 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002294 svm->nested.vmcb = 0;
2295
Alexander Grafcf74a782008-11-25 20:17:08 +01002296 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002297 disable_gif(svm);
2298
2299 nested_vmcb->save.es = vmcb->save.es;
2300 nested_vmcb->save.cs = vmcb->save.cs;
2301 nested_vmcb->save.ss = vmcb->save.ss;
2302 nested_vmcb->save.ds = vmcb->save.ds;
2303 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2304 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002305 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002306 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002307 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002308 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002309 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002310 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002311 nested_vmcb->save.rip = vmcb->save.rip;
2312 nested_vmcb->save.rsp = vmcb->save.rsp;
2313 nested_vmcb->save.rax = vmcb->save.rax;
2314 nested_vmcb->save.dr7 = vmcb->save.dr7;
2315 nested_vmcb->save.dr6 = vmcb->save.dr6;
2316 nested_vmcb->save.cpl = vmcb->save.cpl;
2317
2318 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2319 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2320 nested_vmcb->control.int_state = vmcb->control.int_state;
2321 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2322 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2323 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2324 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2325 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2326 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002327 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002328
2329 /*
2330 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2331 * to make sure that we do not lose injected events. So check event_inj
2332 * here and copy it to exit_int_info if it is valid.
2333 * Exit_int_info and event_inj can't be both valid because the case
2334 * below only happens on a VMRUN instruction intercept which has
2335 * no valid exit_int_info set.
2336 */
2337 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2338 struct vmcb_control_area *nc = &nested_vmcb->control;
2339
2340 nc->exit_int_info = vmcb->control.event_inj;
2341 nc->exit_int_info_err = vmcb->control.event_inj_err;
2342 }
2343
Joerg Roedel33740e42009-08-07 11:49:29 +02002344 nested_vmcb->control.tlb_ctl = 0;
2345 nested_vmcb->control.event_inj = 0;
2346 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002347
2348 /* We always set V_INTR_MASKING and remember the old value in hflags */
2349 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2350 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2351
Alexander Grafcf74a782008-11-25 20:17:08 +01002352 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002353 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002354
Alexander Graf219b65d2009-06-15 15:21:25 +02002355 kvm_clear_exception_queue(&svm->vcpu);
2356 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002357
Joerg Roedel4b161842010-09-10 17:31:03 +02002358 svm->nested.nested_cr3 = 0;
2359
Alexander Grafcf74a782008-11-25 20:17:08 +01002360 /* Restore selected save entries */
2361 svm->vmcb->save.es = hsave->save.es;
2362 svm->vmcb->save.cs = hsave->save.cs;
2363 svm->vmcb->save.ss = hsave->save.ss;
2364 svm->vmcb->save.ds = hsave->save.ds;
2365 svm->vmcb->save.gdtr = hsave->save.gdtr;
2366 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002367 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002368 svm_set_efer(&svm->vcpu, hsave->save.efer);
2369 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2370 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2371 if (npt_enabled) {
2372 svm->vmcb->save.cr3 = hsave->save.cr3;
2373 svm->vcpu.arch.cr3 = hsave->save.cr3;
2374 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002375 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002376 }
2377 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2378 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2379 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2380 svm->vmcb->save.dr7 = 0;
2381 svm->vmcb->save.cpl = 0;
2382 svm->vmcb->control.exit_int_info = 0;
2383
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002384 mark_all_dirty(svm->vmcb);
2385
Joerg Roedel7597f122010-02-19 16:23:00 +01002386 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002387
Joerg Roedel4b161842010-09-10 17:31:03 +02002388 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002389 kvm_mmu_reset_context(&svm->vcpu);
2390 kvm_mmu_load(&svm->vcpu);
2391
2392 return 0;
2393}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002394
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002395static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002396{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002397 /*
2398 * This function merges the msr permission bitmaps of kvm and the
Guo Chaoc5ec2e52012-06-28 15:16:43 +08002399 * nested vmcb. It is optimized in that it only merges the parts where
Joerg Roedel323c3d82010-03-01 15:34:37 +01002400 * the kvm msr permission bitmap may contain zero bits
2401 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002402 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002403
Joerg Roedel323c3d82010-03-01 15:34:37 +01002404 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2405 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002406
Joerg Roedel323c3d82010-03-01 15:34:37 +01002407 for (i = 0; i < MSRPM_OFFSETS; i++) {
2408 u32 value, p;
2409 u64 offset;
2410
2411 if (msrpm_offsets[i] == 0xffffffff)
2412 break;
2413
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002414 p = msrpm_offsets[i];
2415 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002416
2417 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2418 return false;
2419
2420 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2421 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002422
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002423 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002424
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002425 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002426}
2427
Joerg Roedel52c65a302010-08-02 16:46:44 +02002428static bool nested_vmcb_checks(struct vmcb *vmcb)
2429{
2430 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2431 return false;
2432
Joerg Roedeldbe77582010-08-02 16:46:45 +02002433 if (vmcb->control.asid == 0)
2434 return false;
2435
Joerg Roedel4b161842010-09-10 17:31:03 +02002436 if (vmcb->control.nested_ctl && !npt_enabled)
2437 return false;
2438
Joerg Roedel52c65a302010-08-02 16:46:44 +02002439 return true;
2440}
2441
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002442static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002443{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002444 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002445 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002446 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002447 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002448 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002449
Joerg Roedel06fc77722010-02-19 16:23:07 +01002450 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002451
Joerg Roedel7597f122010-02-19 16:23:00 +01002452 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002453 if (!nested_vmcb)
2454 return false;
2455
Joerg Roedel52c65a302010-08-02 16:46:44 +02002456 if (!nested_vmcb_checks(nested_vmcb)) {
2457 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2458 nested_vmcb->control.exit_code_hi = 0;
2459 nested_vmcb->control.exit_info_1 = 0;
2460 nested_vmcb->control.exit_info_2 = 0;
2461
2462 nested_svm_unmap(page);
2463
2464 return false;
2465 }
2466
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002467 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002468 nested_vmcb->save.rip,
2469 nested_vmcb->control.int_ctl,
2470 nested_vmcb->control.event_inj,
2471 nested_vmcb->control.nested_ctl);
2472
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002473 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2474 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002475 nested_vmcb->control.intercept_exceptions,
2476 nested_vmcb->control.intercept);
2477
Alexander Graf3d6368e2008-11-25 20:17:07 +01002478 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002479 kvm_clear_exception_queue(&svm->vcpu);
2480 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002481
Joerg Roedele0231712010-02-24 18:59:10 +01002482 /*
2483 * Save the old vmcb, so we don't need to pick what we save, but can
2484 * restore everything when a VMEXIT occurs
2485 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002486 hsave->save.es = vmcb->save.es;
2487 hsave->save.cs = vmcb->save.cs;
2488 hsave->save.ss = vmcb->save.ss;
2489 hsave->save.ds = vmcb->save.ds;
2490 hsave->save.gdtr = vmcb->save.gdtr;
2491 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002492 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002493 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002494 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002495 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002496 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002497 hsave->save.rsp = vmcb->save.rsp;
2498 hsave->save.rax = vmcb->save.rax;
2499 if (npt_enabled)
2500 hsave->save.cr3 = vmcb->save.cr3;
2501 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002502 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002503
Joerg Roedel0460a972009-08-07 11:49:31 +02002504 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002505
Avi Kivityf6e78472010-08-02 15:30:20 +03002506 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002507 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2508 else
2509 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2510
Joerg Roedel4b161842010-09-10 17:31:03 +02002511 if (nested_vmcb->control.nested_ctl) {
2512 kvm_mmu_unload(&svm->vcpu);
2513 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2514 nested_svm_init_mmu_context(&svm->vcpu);
2515 }
2516
Alexander Graf3d6368e2008-11-25 20:17:07 +01002517 /* Load the nested guest state */
2518 svm->vmcb->save.es = nested_vmcb->save.es;
2519 svm->vmcb->save.cs = nested_vmcb->save.cs;
2520 svm->vmcb->save.ss = nested_vmcb->save.ss;
2521 svm->vmcb->save.ds = nested_vmcb->save.ds;
2522 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2523 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002524 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002525 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2526 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2527 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2528 if (npt_enabled) {
2529 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2530 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002531 } else
Avi Kivity23902182010-06-10 17:02:16 +03002532 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002533
2534 /* Guest paging mode is active - reset mmu */
2535 kvm_mmu_reset_context(&svm->vcpu);
2536
Joerg Roedeldefbba52009-08-07 11:49:30 +02002537 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002538 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2539 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2540 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002541
Alexander Graf3d6368e2008-11-25 20:17:07 +01002542 /* In case we don't even reach vcpu_run, the fields are not updated */
2543 svm->vmcb->save.rax = nested_vmcb->save.rax;
2544 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2545 svm->vmcb->save.rip = nested_vmcb->save.rip;
2546 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2547 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2548 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2549
Joerg Roedelf7138532010-03-01 15:34:40 +01002550 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002551 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002552
Joerg Roedelaad42c62009-08-07 11:49:34 +02002553 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002554 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002555 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002556 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2557 svm->nested.intercept = nested_vmcb->control.intercept;
2558
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002559 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002560 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002561 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2562 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2563 else
2564 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2565
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002566 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2567 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002568 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2569 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002570 }
2571
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002572 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002573 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002574
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002575 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002576 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2577 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2578 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002579 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2580 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2581
Joerg Roedel7597f122010-02-19 16:23:00 +01002582 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002583
Joerg Roedel20307532010-11-29 17:51:48 +01002584 /* Enter Guest-Mode */
2585 enter_guest_mode(&svm->vcpu);
2586
Joerg Roedel384c6362010-11-30 18:03:56 +01002587 /*
2588 * Merge guest and host intercepts - must be called with vcpu in
2589 * guest-mode to take affect here
2590 */
2591 recalc_intercepts(svm);
2592
Joerg Roedel06fc77722010-02-19 16:23:07 +01002593 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002594
Joerg Roedel2af91942009-08-07 11:49:28 +02002595 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002596
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002597 mark_all_dirty(svm->vmcb);
2598
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002599 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002600}
2601
Joerg Roedel9966bf62009-08-07 11:49:40 +02002602static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002603{
2604 to_vmcb->save.fs = from_vmcb->save.fs;
2605 to_vmcb->save.gs = from_vmcb->save.gs;
2606 to_vmcb->save.tr = from_vmcb->save.tr;
2607 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2608 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2609 to_vmcb->save.star = from_vmcb->save.star;
2610 to_vmcb->save.lstar = from_vmcb->save.lstar;
2611 to_vmcb->save.cstar = from_vmcb->save.cstar;
2612 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2613 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2614 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2615 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002616}
2617
Avi Kivity851ba692009-08-24 11:10:17 +03002618static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002619{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002620 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002621 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002622
Alexander Graf55426752008-11-25 20:17:06 +01002623 if (nested_svm_check_permissions(svm))
2624 return 1;
2625
Joerg Roedel7597f122010-02-19 16:23:00 +01002626 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002627 if (!nested_vmcb)
2628 return 1;
2629
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002630 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2631 skip_emulated_instruction(&svm->vcpu);
2632
Joerg Roedel9966bf62009-08-07 11:49:40 +02002633 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002634 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002635
2636 return 1;
2637}
2638
Avi Kivity851ba692009-08-24 11:10:17 +03002639static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002640{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002641 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002642 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002643
Alexander Graf55426752008-11-25 20:17:06 +01002644 if (nested_svm_check_permissions(svm))
2645 return 1;
2646
Joerg Roedel7597f122010-02-19 16:23:00 +01002647 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002648 if (!nested_vmcb)
2649 return 1;
2650
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002651 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2652 skip_emulated_instruction(&svm->vcpu);
2653
Joerg Roedel9966bf62009-08-07 11:49:40 +02002654 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002655 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002656
2657 return 1;
2658}
2659
Avi Kivity851ba692009-08-24 11:10:17 +03002660static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002661{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002662 if (nested_svm_check_permissions(svm))
2663 return 1;
2664
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002665 /* Save rip after vmrun instruction */
2666 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002667
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002668 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002669 return 1;
2670
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002671 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002672 goto failed;
2673
2674 return 1;
2675
2676failed:
2677
2678 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2679 svm->vmcb->control.exit_code_hi = 0;
2680 svm->vmcb->control.exit_info_1 = 0;
2681 svm->vmcb->control.exit_info_2 = 0;
2682
2683 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002684
2685 return 1;
2686}
2687
Avi Kivity851ba692009-08-24 11:10:17 +03002688static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002689{
2690 if (nested_svm_check_permissions(svm))
2691 return 1;
2692
2693 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2694 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002695 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002696
Joerg Roedel2af91942009-08-07 11:49:28 +02002697 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002698
2699 return 1;
2700}
2701
Avi Kivity851ba692009-08-24 11:10:17 +03002702static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002703{
2704 if (nested_svm_check_permissions(svm))
2705 return 1;
2706
2707 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2708 skip_emulated_instruction(&svm->vcpu);
2709
Joerg Roedel2af91942009-08-07 11:49:28 +02002710 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002711
2712 /* After a CLGI no interrupts should come */
2713 svm_clear_vintr(svm);
2714 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2715
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002716 mark_dirty(svm->vmcb, VMCB_INTR);
2717
Alexander Graf1371d902008-11-25 20:17:04 +01002718 return 1;
2719}
2720
Avi Kivity851ba692009-08-24 11:10:17 +03002721static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002722{
2723 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002724
Joerg Roedelec1ff792009-10-09 16:08:31 +02002725 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2726 vcpu->arch.regs[VCPU_REGS_RAX]);
2727
Alexander Grafff092382009-06-15 15:21:24 +02002728 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2729 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2730
2731 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2732 skip_emulated_instruction(&svm->vcpu);
2733 return 1;
2734}
2735
Joerg Roedel532a46b2009-10-09 16:08:32 +02002736static int skinit_interception(struct vcpu_svm *svm)
2737{
2738 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2739
2740 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2741 return 1;
2742}
2743
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002744static int xsetbv_interception(struct vcpu_svm *svm)
2745{
2746 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
2747 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
2748
2749 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
2750 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2751 skip_emulated_instruction(&svm->vcpu);
2752 }
2753
2754 return 1;
2755}
2756
Avi Kivity851ba692009-08-24 11:10:17 +03002757static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002759 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 return 1;
2761}
2762
Avi Kivity851ba692009-08-24 11:10:17 +03002763static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764{
Izik Eidus37817f22008-03-24 23:14:53 +02002765 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002766 int reason;
2767 int int_type = svm->vmcb->control.exit_int_info &
2768 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002769 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002770 uint32_t type =
2771 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2772 uint32_t idt_v =
2773 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002774 bool has_error_code = false;
2775 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002776
2777 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002778
Izik Eidus37817f22008-03-24 23:14:53 +02002779 if (svm->vmcb->control.exit_info_2 &
2780 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002781 reason = TASK_SWITCH_IRET;
2782 else if (svm->vmcb->control.exit_info_2 &
2783 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2784 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002785 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002786 reason = TASK_SWITCH_GATE;
2787 else
2788 reason = TASK_SWITCH_CALL;
2789
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002790 if (reason == TASK_SWITCH_GATE) {
2791 switch (type) {
2792 case SVM_EXITINTINFO_TYPE_NMI:
2793 svm->vcpu.arch.nmi_injected = false;
2794 break;
2795 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002796 if (svm->vmcb->control.exit_info_2 &
2797 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2798 has_error_code = true;
2799 error_code =
2800 (u32)svm->vmcb->control.exit_info_2;
2801 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002802 kvm_clear_exception_queue(&svm->vcpu);
2803 break;
2804 case SVM_EXITINTINFO_TYPE_INTR:
2805 kvm_clear_interrupt_queue(&svm->vcpu);
2806 break;
2807 default:
2808 break;
2809 }
2810 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002811
Gleb Natapov8317c292009-04-12 13:37:02 +03002812 if (reason != TASK_SWITCH_GATE ||
2813 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2814 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002815 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2816 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002817
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002818 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2819 int_vec = -1;
2820
2821 if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Gleb Natapovacb54512010-04-15 21:03:50 +03002822 has_error_code, error_code) == EMULATE_FAIL) {
2823 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2824 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2825 svm->vcpu.run->internal.ndata = 0;
2826 return 0;
2827 }
2828 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829}
2830
Avi Kivity851ba692009-08-24 11:10:17 +03002831static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002833 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002834 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002835 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836}
2837
Avi Kivity851ba692009-08-24 11:10:17 +03002838static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002839{
2840 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002841 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002842 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02002843 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002844 return 1;
2845}
2846
Avi Kivity851ba692009-08-24 11:10:17 +03002847static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002848{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002849 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2850 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
2851
2852 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
2853 skip_emulated_instruction(&svm->vcpu);
2854 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002855}
2856
Avi Kivity851ba692009-08-24 11:10:17 +03002857static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858{
Andre Przywara51d8b662010-12-21 11:12:02 +01002859 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860}
2861
Avi Kivity332b56e2011-11-10 14:57:24 +02002862static int rdpmc_interception(struct vcpu_svm *svm)
2863{
2864 int err;
2865
2866 if (!static_cpu_has(X86_FEATURE_NRIPS))
2867 return emulate_on_interception(svm);
2868
2869 err = kvm_rdpmc(&svm->vcpu);
2870 kvm_complete_insn_gp(&svm->vcpu, err);
2871
2872 return 1;
2873}
2874
Joerg Roedel628afd22011-04-04 12:39:36 +02002875bool check_selective_cr0_intercepted(struct vcpu_svm *svm, unsigned long val)
2876{
2877 unsigned long cr0 = svm->vcpu.arch.cr0;
2878 bool ret = false;
2879 u64 intercept;
2880
2881 intercept = svm->nested.intercept;
2882
2883 if (!is_guest_mode(&svm->vcpu) ||
2884 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
2885 return false;
2886
2887 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2888 val &= ~SVM_CR0_SELECTIVE_MASK;
2889
2890 if (cr0 ^ val) {
2891 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2892 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2893 }
2894
2895 return ret;
2896}
2897
Andre Przywara7ff76d52010-12-21 11:12:04 +01002898#define CR_VALID (1ULL << 63)
2899
2900static int cr_interception(struct vcpu_svm *svm)
2901{
2902 int reg, cr;
2903 unsigned long val;
2904 int err;
2905
2906 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2907 return emulate_on_interception(svm);
2908
2909 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2910 return emulate_on_interception(svm);
2911
2912 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2913 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
2914
2915 err = 0;
2916 if (cr >= 16) { /* mov to cr */
2917 cr -= 16;
2918 val = kvm_register_read(&svm->vcpu, reg);
2919 switch (cr) {
2920 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02002921 if (!check_selective_cr0_intercepted(svm, val))
2922 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002923 else
2924 return 1;
2925
Andre Przywara7ff76d52010-12-21 11:12:04 +01002926 break;
2927 case 3:
2928 err = kvm_set_cr3(&svm->vcpu, val);
2929 break;
2930 case 4:
2931 err = kvm_set_cr4(&svm->vcpu, val);
2932 break;
2933 case 8:
2934 err = kvm_set_cr8(&svm->vcpu, val);
2935 break;
2936 default:
2937 WARN(1, "unhandled write to CR%d", cr);
2938 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2939 return 1;
2940 }
2941 } else { /* mov from cr */
2942 switch (cr) {
2943 case 0:
2944 val = kvm_read_cr0(&svm->vcpu);
2945 break;
2946 case 2:
2947 val = svm->vcpu.arch.cr2;
2948 break;
2949 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02002950 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002951 break;
2952 case 4:
2953 val = kvm_read_cr4(&svm->vcpu);
2954 break;
2955 case 8:
2956 val = kvm_get_cr8(&svm->vcpu);
2957 break;
2958 default:
2959 WARN(1, "unhandled read from CR%d", cr);
2960 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2961 return 1;
2962 }
2963 kvm_register_write(&svm->vcpu, reg, val);
2964 }
2965 kvm_complete_insn_gp(&svm->vcpu, err);
2966
2967 return 1;
2968}
2969
Andre Przywaracae37972010-12-21 11:12:05 +01002970static int dr_interception(struct vcpu_svm *svm)
2971{
2972 int reg, dr;
2973 unsigned long val;
2974 int err;
2975
2976 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
2977 return emulate_on_interception(svm);
2978
2979 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2980 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
2981
2982 if (dr >= 16) { /* mov to DRn */
2983 val = kvm_register_read(&svm->vcpu, reg);
2984 kvm_set_dr(&svm->vcpu, dr - 16, val);
2985 } else {
2986 err = kvm_get_dr(&svm->vcpu, dr, &val);
2987 if (!err)
2988 kvm_register_write(&svm->vcpu, reg, val);
2989 }
2990
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01002991 skip_emulated_instruction(&svm->vcpu);
2992
Andre Przywaracae37972010-12-21 11:12:05 +01002993 return 1;
2994}
2995
Avi Kivity851ba692009-08-24 11:10:17 +03002996static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002997{
Avi Kivity851ba692009-08-24 11:10:17 +03002998 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01002999 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03003000
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003001 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
3002 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01003003 r = cr_interception(svm);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003004 if (irqchip_in_kernel(svm->vcpu.kvm)) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003005 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Andre Przywara7ff76d52010-12-21 11:12:04 +01003006 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003007 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003008 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01003009 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01003010 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
3011 return 0;
3012}
3013
Marcelo Tosatti886b4702012-11-27 23:28:58 -02003014u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003015{
3016 struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
3017 return vmcb->control.tsc_offset +
Marcelo Tosatti886b4702012-11-27 23:28:58 -02003018 svm_scale_tsc(vcpu, host_tsc);
Nadav Har'Eld5c17852011-08-02 15:54:20 +03003019}
3020
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
3022{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003023 struct vcpu_svm *svm = to_svm(vcpu);
3024
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303026 case MSR_IA32_TSC: {
Nadav Har'El45133ec2011-08-02 15:55:23 +03003027 *data = svm->vmcb->control.tsc_offset +
Joerg Roedelfbc0db72011-03-25 09:44:46 +01003028 svm_scale_tsc(vcpu, native_read_tsc());
3029
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030 break;
3031 }
Brian Gerst8c065852010-07-17 09:03:26 -04003032 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003033 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003034 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08003035#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003036 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003037 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038 break;
3039 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003040 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003041 break;
3042 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003043 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044 break;
3045 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003046 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047 break;
3048#endif
3049 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003050 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051 break;
3052 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003053 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003054 break;
3055 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003056 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057 break;
Joerg Roedele0231712010-02-24 18:59:10 +01003058 /*
3059 * Nobody will change the following 5 values in the VMCB so we can
3060 * safely return them on rdmsr. They will always be 0 until LBRV is
3061 * implemented.
3062 */
Joerg Roedela2938c82008-02-13 16:30:28 +01003063 case MSR_IA32_DEBUGCTLMSR:
3064 *data = svm->vmcb->save.dbgctl;
3065 break;
3066 case MSR_IA32_LASTBRANCHFROMIP:
3067 *data = svm->vmcb->save.br_from;
3068 break;
3069 case MSR_IA32_LASTBRANCHTOIP:
3070 *data = svm->vmcb->save.br_to;
3071 break;
3072 case MSR_IA32_LASTINTFROMIP:
3073 *data = svm->vmcb->save.last_excp_from;
3074 break;
3075 case MSR_IA32_LASTINTTOIP:
3076 *data = svm->vmcb->save.last_excp_to;
3077 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003078 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003079 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003080 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003081 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003082 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01003083 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01003084 case MSR_IA32_UCODE_REV:
3085 *data = 0x01000065;
3086 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08003088 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 }
3090 return 0;
3091}
3092
Avi Kivity851ba692009-08-24 11:10:17 +03003093static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003095 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096 u64 data;
3097
Avi Kivity59200272010-01-25 19:47:02 +02003098 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
3099 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003100 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003101 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003102 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003103
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003104 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003105 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003106 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10003107 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108 }
3109 return 1;
3110}
3111
Joerg Roedel4a810182010-02-24 18:59:15 +01003112static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
3113{
3114 struct vcpu_svm *svm = to_svm(vcpu);
3115 int svm_dis, chg_mask;
3116
3117 if (data & ~SVM_VM_CR_VALID_MASK)
3118 return 1;
3119
3120 chg_mask = SVM_VM_CR_VALID_MASK;
3121
3122 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
3123 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
3124
3125 svm->nested.vm_cr_msr &= ~chg_mask;
3126 svm->nested.vm_cr_msr |= (data & chg_mask);
3127
3128 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
3129
3130 /* check for svm_disable while efer.svme is set */
3131 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
3132 return 1;
3133
3134 return 0;
3135}
3136
Will Auld8fe8ab42012-11-29 12:42:12 -08003137static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003139 struct vcpu_svm *svm = to_svm(vcpu);
3140
Will Auld8fe8ab42012-11-29 12:42:12 -08003141 u32 ecx = msr->index;
3142 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10003144 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003145 kvm_write_tsc(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146 break;
Brian Gerst8c065852010-07-17 09:03:26 -04003147 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003148 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08003150#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003152 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153 break;
3154 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003155 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156 break;
3157 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003158 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159 break;
3160 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003161 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162 break;
3163#endif
3164 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003165 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166 break;
3167 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02003168 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003169 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 break;
3171 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02003172 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003173 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003175 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003176 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003177 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
3178 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003179 break;
3180 }
3181 if (data & DEBUGCTL_RESERVED_BITS)
3182 return 1;
3183
3184 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01003185 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003186 if (data & (1ULL<<0))
3187 svm_enable_lbrv(svm);
3188 else
3189 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01003190 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003191 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02003192 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003193 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003194 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003195 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003196 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03003197 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003198 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08003200 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201 }
3202 return 0;
3203}
3204
Avi Kivity851ba692009-08-24 11:10:17 +03003205static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206{
Will Auld8fe8ab42012-11-29 12:42:12 -08003207 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003208 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003209 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003210 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003211
Will Auld8fe8ab42012-11-29 12:42:12 -08003212 msr.data = data;
3213 msr.index = ecx;
3214 msr.host_initiated = false;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003215
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003216 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Will Auld8fe8ab42012-11-29 12:42:12 -08003217 if (svm_set_msr(&svm->vcpu, &msr)) {
Avi Kivity59200272010-01-25 19:47:02 +02003218 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003219 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02003220 } else {
3221 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10003222 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02003223 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224 return 1;
3225}
3226
Avi Kivity851ba692009-08-24 11:10:17 +03003227static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228{
Rusty Russelle756fc62007-07-30 20:07:08 +10003229 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03003230 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231 else
Avi Kivity851ba692009-08-24 11:10:17 +03003232 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233}
3234
Avi Kivity851ba692009-08-24 11:10:17 +03003235static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08003236{
Avi Kivity851ba692009-08-24 11:10:17 +03003237 struct kvm_run *kvm_run = svm->vcpu.run;
3238
Avi Kivity3842d132010-07-27 12:30:24 +03003239 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01003240 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03003241 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003242 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08003243 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003244 /*
3245 * If the user space waits to inject interrupts, exit as soon as
3246 * possible
3247 */
Gleb Natapov80618232009-04-21 17:44:56 +03003248 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
3249 kvm_run->request_interrupt_window &&
3250 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Dor Laorc1150d82007-01-05 16:36:24 -08003251 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
3252 return 0;
3253 }
3254
3255 return 1;
3256}
3257
Mark Langsdorf565d0992009-10-06 14:25:02 -05003258static int pause_interception(struct vcpu_svm *svm)
3259{
3260 kvm_vcpu_on_spin(&(svm->vcpu));
3261 return 1;
3262}
3263
Mathias Krause09941fb2012-08-30 01:30:20 +02003264static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003265 [SVM_EXIT_READ_CR0] = cr_interception,
3266 [SVM_EXIT_READ_CR3] = cr_interception,
3267 [SVM_EXIT_READ_CR4] = cr_interception,
3268 [SVM_EXIT_READ_CR8] = cr_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02003269 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003270 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003271 [SVM_EXIT_WRITE_CR3] = cr_interception,
3272 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003273 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003274 [SVM_EXIT_READ_DR0] = dr_interception,
3275 [SVM_EXIT_READ_DR1] = dr_interception,
3276 [SVM_EXIT_READ_DR2] = dr_interception,
3277 [SVM_EXIT_READ_DR3] = dr_interception,
3278 [SVM_EXIT_READ_DR4] = dr_interception,
3279 [SVM_EXIT_READ_DR5] = dr_interception,
3280 [SVM_EXIT_READ_DR6] = dr_interception,
3281 [SVM_EXIT_READ_DR7] = dr_interception,
3282 [SVM_EXIT_WRITE_DR0] = dr_interception,
3283 [SVM_EXIT_WRITE_DR1] = dr_interception,
3284 [SVM_EXIT_WRITE_DR2] = dr_interception,
3285 [SVM_EXIT_WRITE_DR3] = dr_interception,
3286 [SVM_EXIT_WRITE_DR4] = dr_interception,
3287 [SVM_EXIT_WRITE_DR5] = dr_interception,
3288 [SVM_EXIT_WRITE_DR6] = dr_interception,
3289 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003290 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3291 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003292 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003293 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3294 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
3295 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
3296 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003297 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003298 [SVM_EXIT_SMI] = nop_on_interception,
3299 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003300 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02003301 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003302 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003303 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003304 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003305 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003306 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003307 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003308 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003309 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 [SVM_EXIT_MSR] = msr_interception,
3311 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003312 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003313 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003314 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003315 [SVM_EXIT_VMLOAD] = vmload_interception,
3316 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003317 [SVM_EXIT_STGI] = stgi_interception,
3318 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003319 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003320 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01003321 [SVM_EXIT_MONITOR] = invalid_op_interception,
3322 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003323 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003324 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003325};
3326
Joe Perchesae8cc052011-04-24 22:00:50 -07003327static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003328{
3329 struct vcpu_svm *svm = to_svm(vcpu);
3330 struct vmcb_control_area *control = &svm->vmcb->control;
3331 struct vmcb_save_area *save = &svm->vmcb->save;
3332
3333 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003334 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
3335 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
3336 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
3337 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
3338 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
3339 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
3340 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3341 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3342 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3343 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3344 pr_err("%-20s%d\n", "asid:", control->asid);
3345 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3346 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3347 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3348 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3349 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3350 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3351 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3352 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3353 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3354 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3355 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
3356 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3357 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3358 pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
3359 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003360 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003361 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3362 "es:",
3363 save->es.selector, save->es.attrib,
3364 save->es.limit, save->es.base);
3365 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3366 "cs:",
3367 save->cs.selector, save->cs.attrib,
3368 save->cs.limit, save->cs.base);
3369 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3370 "ss:",
3371 save->ss.selector, save->ss.attrib,
3372 save->ss.limit, save->ss.base);
3373 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3374 "ds:",
3375 save->ds.selector, save->ds.attrib,
3376 save->ds.limit, save->ds.base);
3377 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3378 "fs:",
3379 save->fs.selector, save->fs.attrib,
3380 save->fs.limit, save->fs.base);
3381 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3382 "gs:",
3383 save->gs.selector, save->gs.attrib,
3384 save->gs.limit, save->gs.base);
3385 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3386 "gdtr:",
3387 save->gdtr.selector, save->gdtr.attrib,
3388 save->gdtr.limit, save->gdtr.base);
3389 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3390 "ldtr:",
3391 save->ldtr.selector, save->ldtr.attrib,
3392 save->ldtr.limit, save->ldtr.base);
3393 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3394 "idtr:",
3395 save->idtr.selector, save->idtr.attrib,
3396 save->idtr.limit, save->idtr.base);
3397 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3398 "tr:",
3399 save->tr.selector, save->tr.attrib,
3400 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003401 pr_err("cpl: %d efer: %016llx\n",
3402 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003403 pr_err("%-15s %016llx %-13s %016llx\n",
3404 "cr0:", save->cr0, "cr2:", save->cr2);
3405 pr_err("%-15s %016llx %-13s %016llx\n",
3406 "cr3:", save->cr3, "cr4:", save->cr4);
3407 pr_err("%-15s %016llx %-13s %016llx\n",
3408 "dr6:", save->dr6, "dr7:", save->dr7);
3409 pr_err("%-15s %016llx %-13s %016llx\n",
3410 "rip:", save->rip, "rflags:", save->rflags);
3411 pr_err("%-15s %016llx %-13s %016llx\n",
3412 "rsp:", save->rsp, "rax:", save->rax);
3413 pr_err("%-15s %016llx %-13s %016llx\n",
3414 "star:", save->star, "lstar:", save->lstar);
3415 pr_err("%-15s %016llx %-13s %016llx\n",
3416 "cstar:", save->cstar, "sfmask:", save->sfmask);
3417 pr_err("%-15s %016llx %-13s %016llx\n",
3418 "kernel_gs_base:", save->kernel_gs_base,
3419 "sysenter_cs:", save->sysenter_cs);
3420 pr_err("%-15s %016llx %-13s %016llx\n",
3421 "sysenter_esp:", save->sysenter_esp,
3422 "sysenter_eip:", save->sysenter_eip);
3423 pr_err("%-15s %016llx %-13s %016llx\n",
3424 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3425 pr_err("%-15s %016llx %-13s %016llx\n",
3426 "br_from:", save->br_from, "br_to:", save->br_to);
3427 pr_err("%-15s %016llx %-13s %016llx\n",
3428 "excp_from:", save->last_excp_from,
3429 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003430}
3431
Avi Kivity586f9602010-11-18 13:09:54 +02003432static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3433{
3434 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3435
3436 *info1 = control->exit_info_1;
3437 *info2 = control->exit_info_2;
3438}
3439
Avi Kivity851ba692009-08-24 11:10:17 +03003440static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003442 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003443 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003444 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003446 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003447 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3448 if (npt_enabled)
3449 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003450
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003451 if (unlikely(svm->nested.exit_required)) {
3452 nested_svm_vmexit(svm);
3453 svm->nested.exit_required = false;
3454
3455 return 1;
3456 }
3457
Joerg Roedel20307532010-11-29 17:51:48 +01003458 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003459 int vmexit;
3460
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003461 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3462 svm->vmcb->control.exit_info_1,
3463 svm->vmcb->control.exit_info_2,
3464 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003465 svm->vmcb->control.exit_int_info_err,
3466 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003467
Joerg Roedel410e4d52009-08-07 11:49:44 +02003468 vmexit = nested_svm_exit_special(svm);
3469
3470 if (vmexit == NESTED_EXIT_CONTINUE)
3471 vmexit = nested_svm_exit_handled(svm);
3472
3473 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003474 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003475 }
3476
Joerg Roedela5c38322009-08-07 11:49:32 +02003477 svm_complete_interrupts(svm);
3478
Avi Kivity04d2cc72007-09-10 18:10:54 +03003479 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3480 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3481 kvm_run->fail_entry.hardware_entry_failure_reason
3482 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003483 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3484 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003485 return 0;
3486 }
3487
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003488 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003489 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003490 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3491 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3493 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003494 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003495 exit_code);
3496
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003497 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003498 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003499 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003500 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501 return 0;
3502 }
3503
Avi Kivity851ba692009-08-24 11:10:17 +03003504 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505}
3506
3507static void reload_tss(struct kvm_vcpu *vcpu)
3508{
3509 int cpu = raw_smp_processor_id();
3510
Tejun Heo0fe1e002009-10-29 22:34:14 +09003511 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3512 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513 load_TR_desc();
3514}
3515
Rusty Russelle756fc62007-07-30 20:07:08 +10003516static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003517{
3518 int cpu = raw_smp_processor_id();
3519
Tejun Heo0fe1e002009-10-29 22:34:14 +09003520 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003522 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003523 if (svm->asid_generation != sd->asid_generation)
3524 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003525}
3526
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003527static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3528{
3529 struct vcpu_svm *svm = to_svm(vcpu);
3530
3531 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3532 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003533 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003534 ++vcpu->stat.nmi_injections;
3535}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536
Eddie Dong85f455f2007-07-06 12:20:49 +03003537static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003538{
3539 struct vmcb_control_area *control;
3540
Rusty Russelle756fc62007-07-30 20:07:08 +10003541 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003542 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003543 control->int_ctl &= ~V_INTR_PRIO_MASK;
3544 control->int_ctl |= V_IRQ_MASK |
3545 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003546 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003547}
3548
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003549static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003550{
3551 struct vcpu_svm *svm = to_svm(vcpu);
3552
Joerg Roedel2af91942009-08-07 11:49:28 +02003553 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003554
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003555 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3556 ++vcpu->stat.irq_injections;
3557
Alexander Graf219b65d2009-06-15 15:21:25 +02003558 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3559 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003560}
3561
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003562static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3563{
3564 struct vcpu_svm *svm = to_svm(vcpu);
3565
Joerg Roedel20307532010-11-29 17:51:48 +01003566 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003567 return;
3568
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003569 if (irr == -1)
3570 return;
3571
3572 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003573 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003574}
3575
3576static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003577{
3578 struct vcpu_svm *svm = to_svm(vcpu);
3579 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003580 int ret;
3581 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3582 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3583 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3584
3585 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003586}
3587
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003588static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3589{
3590 struct vcpu_svm *svm = to_svm(vcpu);
3591
3592 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3593}
3594
3595static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3596{
3597 struct vcpu_svm *svm = to_svm(vcpu);
3598
3599 if (masked) {
3600 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003601 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003602 } else {
3603 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003604 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003605 }
3606}
3607
Gleb Natapov78646122009-03-23 12:12:11 +02003608static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3609{
3610 struct vcpu_svm *svm = to_svm(vcpu);
3611 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003612 int ret;
3613
3614 if (!gif_set(svm) ||
3615 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3616 return 0;
3617
Avi Kivityf6e78472010-08-02 15:30:20 +03003618 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003619
Joerg Roedel20307532010-11-29 17:51:48 +01003620 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003621 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3622
3623 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003624}
3625
Gleb Natapov9222be12009-04-23 17:14:37 +03003626static void enable_irq_window(struct kvm_vcpu *vcpu)
3627{
Alexander Graf219b65d2009-06-15 15:21:25 +02003628 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003629
Joerg Roedele0231712010-02-24 18:59:10 +01003630 /*
3631 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3632 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3633 * get that intercept, this function will be called again though and
3634 * we'll get the vintr intercept.
3635 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003636 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003637 svm_set_vintr(svm);
3638 svm_inject_irq(svm, 0x0);
3639 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003640}
3641
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003642static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003643{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003644 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003645
Gleb Natapov44c11432009-05-11 13:35:52 +03003646 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3647 == HF_NMI_MASK)
3648 return; /* IRET will cause a vm exit */
3649
Joerg Roedele0231712010-02-24 18:59:10 +01003650 /*
3651 * Something prevents NMI from been injected. Single step over possible
3652 * problem (IRET or exception injection or interrupt shadow)
3653 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003654 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003655 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkac8639012012-09-21 05:42:55 +02003656 update_db_bp_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003657}
3658
Izik Eiduscbc94022007-10-25 00:29:55 +02003659static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3660{
3661 return 0;
3662}
3663
Avi Kivityd9e368d2007-06-07 19:18:30 +03003664static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3665{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003666 struct vcpu_svm *svm = to_svm(vcpu);
3667
3668 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3669 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3670 else
3671 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003672}
3673
Avi Kivity04d2cc72007-09-10 18:10:54 +03003674static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3675{
3676}
3677
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003678static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3679{
3680 struct vcpu_svm *svm = to_svm(vcpu);
3681
Joerg Roedel20307532010-11-29 17:51:48 +01003682 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003683 return;
3684
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003685 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003686 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003687 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003688 }
3689}
3690
Joerg Roedel649d6862008-04-16 16:51:15 +02003691static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3692{
3693 struct vcpu_svm *svm = to_svm(vcpu);
3694 u64 cr8;
3695
Joerg Roedel20307532010-11-29 17:51:48 +01003696 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003697 return;
3698
Joerg Roedel649d6862008-04-16 16:51:15 +02003699 cr8 = kvm_get_cr8(vcpu);
3700 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3701 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3702}
3703
Gleb Natapov9222be12009-04-23 17:14:37 +03003704static void svm_complete_interrupts(struct vcpu_svm *svm)
3705{
3706 u8 vector;
3707 int type;
3708 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003709 unsigned int3_injected = svm->int3_injected;
3710
3711 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003712
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003713 /*
3714 * If we've made progress since setting HF_IRET_MASK, we've
3715 * executed an IRET and can allow NMI injection.
3716 */
3717 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
3718 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003719 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003720 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3721 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003722
Gleb Natapov9222be12009-04-23 17:14:37 +03003723 svm->vcpu.arch.nmi_injected = false;
3724 kvm_clear_exception_queue(&svm->vcpu);
3725 kvm_clear_interrupt_queue(&svm->vcpu);
3726
3727 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3728 return;
3729
Avi Kivity3842d132010-07-27 12:30:24 +03003730 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3731
Gleb Natapov9222be12009-04-23 17:14:37 +03003732 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3733 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3734
3735 switch (type) {
3736 case SVM_EXITINTINFO_TYPE_NMI:
3737 svm->vcpu.arch.nmi_injected = true;
3738 break;
3739 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003740 /*
3741 * In case of software exceptions, do not reinject the vector,
3742 * but re-execute the instruction instead. Rewind RIP first
3743 * if we emulated INT3 before.
3744 */
3745 if (kvm_exception_is_soft(vector)) {
3746 if (vector == BP_VECTOR && int3_injected &&
3747 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3748 kvm_rip_write(&svm->vcpu,
3749 kvm_rip_read(&svm->vcpu) -
3750 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003751 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003752 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003753 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3754 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003755 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003756
3757 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003758 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003759 break;
3760 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003761 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003762 break;
3763 default:
3764 break;
3765 }
3766}
3767
Avi Kivityb463a6f2010-07-20 15:06:17 +03003768static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3769{
3770 struct vcpu_svm *svm = to_svm(vcpu);
3771 struct vmcb_control_area *control = &svm->vmcb->control;
3772
3773 control->exit_int_info = control->event_inj;
3774 control->exit_int_info_err = control->event_inj_err;
3775 control->event_inj = 0;
3776 svm_complete_interrupts(svm);
3777}
3778
Avi Kivity851ba692009-08-24 11:10:17 +03003779static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003780{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003781 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003782
Joerg Roedel2041a062010-04-22 12:33:08 +02003783 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3784 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3785 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3786
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003787 /*
3788 * A vmexit emulation is required before the vcpu can be executed
3789 * again.
3790 */
3791 if (unlikely(svm->nested.exit_required))
3792 return;
3793
Rusty Russelle756fc62007-07-30 20:07:08 +10003794 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003795
Joerg Roedel649d6862008-04-16 16:51:15 +02003796 sync_lapic_to_cr8(vcpu);
3797
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003798 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799
Avi Kivity04d2cc72007-09-10 18:10:54 +03003800 clgi();
3801
3802 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003803
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03003805 "push %%" _ASM_BP "; \n\t"
3806 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
3807 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
3808 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
3809 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
3810 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
3811 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003812#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003813 "mov %c[r8](%[svm]), %%r8 \n\t"
3814 "mov %c[r9](%[svm]), %%r9 \n\t"
3815 "mov %c[r10](%[svm]), %%r10 \n\t"
3816 "mov %c[r11](%[svm]), %%r11 \n\t"
3817 "mov %c[r12](%[svm]), %%r12 \n\t"
3818 "mov %c[r13](%[svm]), %%r13 \n\t"
3819 "mov %c[r14](%[svm]), %%r14 \n\t"
3820 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003821#endif
3822
Avi Kivity6aa8b732006-12-10 02:21:36 -08003823 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03003824 "push %%" _ASM_AX " \n\t"
3825 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003826 __ex(SVM_VMLOAD) "\n\t"
3827 __ex(SVM_VMRUN) "\n\t"
3828 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03003829 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003830
3831 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03003832 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
3833 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
3834 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
3835 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
3836 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
3837 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003838#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003839 "mov %%r8, %c[r8](%[svm]) \n\t"
3840 "mov %%r9, %c[r9](%[svm]) \n\t"
3841 "mov %%r10, %c[r10](%[svm]) \n\t"
3842 "mov %%r11, %c[r11](%[svm]) \n\t"
3843 "mov %%r12, %c[r12](%[svm]) \n\t"
3844 "mov %%r13, %c[r13](%[svm]) \n\t"
3845 "mov %%r14, %c[r14](%[svm]) \n\t"
3846 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847#endif
Avi Kivity74547662012-09-16 15:10:59 +03003848 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08003849 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003850 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003851 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003852 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3853 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3854 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3855 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3856 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3857 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003858#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003859 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3860 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3861 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3862 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3863 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3864 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3865 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3866 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003867#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003868 : "cc", "memory"
3869#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03003870 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003871 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03003872#else
3873 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003874#endif
3875 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003876
Avi Kivity82ca2d12010-10-21 12:20:34 +02003877#ifdef CONFIG_X86_64
3878 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3879#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003880 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02003881#ifndef CONFIG_X86_32_LAZY_GS
3882 loadsegment(gs, svm->host.gs);
3883#endif
Avi Kivity9581d442010-10-19 16:46:55 +02003884#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003885
3886 reload_tss(vcpu);
3887
Avi Kivity56ba47d2007-11-07 17:14:18 +02003888 local_irq_disable();
3889
Avi Kivity13c34e02010-10-21 12:20:31 +02003890 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3891 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3892 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3893 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3894
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02003895 trace_kvm_exit(svm->vmcb->control.exit_code, vcpu, KVM_ISA_SVM);
3896
Joerg Roedel3781c012011-01-14 16:45:02 +01003897 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3898 kvm_before_handle_nmi(&svm->vcpu);
3899
3900 stgi();
3901
3902 /* Any pending NMI will happen here */
3903
3904 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3905 kvm_after_handle_nmi(&svm->vcpu);
3906
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003907 sync_cr8_to_lapic(vcpu);
3908
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003909 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003910
Joerg Roedel38e5e922010-12-03 15:25:16 +01003911 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3912
Gleb Natapov631bc482010-10-14 11:22:52 +02003913 /* if exit due to PF check for async PF */
3914 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3915 svm->apf_reason = kvm_read_and_reset_pf_reason();
3916
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003917 if (npt_enabled) {
3918 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3919 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3920 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003921
3922 /*
3923 * We need to handle MC intercepts here before the vcpu has a chance to
3924 * change the physical cpu
3925 */
3926 if (unlikely(svm->vmcb->control.exit_code ==
3927 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3928 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003929
3930 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003931}
3932
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3934{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003935 struct vcpu_svm *svm = to_svm(vcpu);
3936
3937 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003938 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003939 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003940}
3941
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003942static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3943{
3944 struct vcpu_svm *svm = to_svm(vcpu);
3945
3946 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01003947 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003948
3949 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02003950 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003951 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003952
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003953 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003954}
3955
Avi Kivity6aa8b732006-12-10 02:21:36 -08003956static int is_disabled(void)
3957{
Joerg Roedel6031a612007-06-22 12:29:50 +03003958 u64 vm_cr;
3959
3960 rdmsrl(MSR_VM_CR, vm_cr);
3961 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3962 return 1;
3963
Avi Kivity6aa8b732006-12-10 02:21:36 -08003964 return 0;
3965}
3966
Ingo Molnar102d8322007-02-19 14:37:47 +02003967static void
3968svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3969{
3970 /*
3971 * Patch in the VMMCALL instruction:
3972 */
3973 hypercall[0] = 0x0f;
3974 hypercall[1] = 0x01;
3975 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003976}
3977
Yang, Sheng002c7f72007-07-31 14:23:01 +03003978static void svm_check_processor_compat(void *rtn)
3979{
3980 *(int *)rtn = 0;
3981}
3982
Avi Kivity774ead32007-12-26 13:57:04 +02003983static bool svm_cpu_has_accelerated_tpr(void)
3984{
3985 return false;
3986}
3987
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003988static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003989{
3990 return 0;
3991}
3992
Sheng Yang0e851882009-12-18 16:48:46 +08003993static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3994{
3995}
3996
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003997static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3998{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003999 switch (func) {
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02004000 case 0x80000001:
4001 if (nested)
4002 entry->ecx |= (1 << 2); /* Set SVM bit */
4003 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004004 case 0x8000000A:
4005 entry->eax = 1; /* SVM revision 1 */
4006 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
4007 ASID emulation to nested SVM */
4008 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02004009 entry->edx = 0; /* Per default do not support any
4010 additional features */
4011
4012 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02004013 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02004014 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004015
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02004016 /* Support NPT for the guest if enabled */
4017 if (npt_enabled)
4018 entry->edx |= SVM_FEATURE_NPT;
4019
Joerg Roedelc2c63a42010-04-22 12:33:12 +02004020 break;
4021 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004022}
4023
Sheng Yang17cc3932010-01-05 19:02:27 +08004024static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004025{
Sheng Yang17cc3932010-01-05 19:02:27 +08004026 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004027}
4028
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004029static bool svm_rdtscp_supported(void)
4030{
4031 return false;
4032}
4033
Mao, Junjiead756a12012-07-02 01:18:48 +00004034static bool svm_invpcid_supported(void)
4035{
4036 return false;
4037}
4038
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004039static bool svm_has_wbinvd_exit(void)
4040{
4041 return true;
4042}
4043
Avi Kivity02daab22009-12-30 12:40:26 +02004044static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
4045{
4046 struct vcpu_svm *svm = to_svm(vcpu);
4047
Joerg Roedel18c918c2010-11-30 18:03:59 +01004048 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01004049 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02004050}
4051
Joerg Roedel80612522011-04-04 12:39:33 +02004052#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004053 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004054#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004055 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004056#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004057 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004058
Mathias Krause09941fb2012-08-30 01:30:20 +02004059static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004060 u32 exit_code;
4061 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004062} x86_intercept_map[] = {
4063 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4064 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4065 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4066 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4067 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004068 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4069 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004070 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4071 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4072 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4073 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4074 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4075 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4076 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4077 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004078 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4079 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4080 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4081 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4082 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4083 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4084 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4085 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004086 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4087 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4088 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004089 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4090 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4091 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4092 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4093 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4094 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4095 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4096 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4097 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004098 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4099 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4100 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4101 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4102 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4103 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4104 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004105 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4106 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4107 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4108 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004109};
4110
Joerg Roedel80612522011-04-04 12:39:33 +02004111#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004112#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004113#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004114
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004115static int svm_check_intercept(struct kvm_vcpu *vcpu,
4116 struct x86_instruction_info *info,
4117 enum x86_intercept_stage stage)
4118{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004119 struct vcpu_svm *svm = to_svm(vcpu);
4120 int vmexit, ret = X86EMUL_CONTINUE;
4121 struct __x86_intercept icpt_info;
4122 struct vmcb *vmcb = svm->vmcb;
4123
4124 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4125 goto out;
4126
4127 icpt_info = x86_intercept_map[info->intercept];
4128
Avi Kivity40e19b52011-04-21 12:35:41 +03004129 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004130 goto out;
4131
4132 switch (icpt_info.exit_code) {
4133 case SVM_EXIT_READ_CR0:
4134 if (info->intercept == x86_intercept_cr_read)
4135 icpt_info.exit_code += info->modrm_reg;
4136 break;
4137 case SVM_EXIT_WRITE_CR0: {
4138 unsigned long cr0, val;
4139 u64 intercept;
4140
4141 if (info->intercept == x86_intercept_cr_write)
4142 icpt_info.exit_code += info->modrm_reg;
4143
4144 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0)
4145 break;
4146
4147 intercept = svm->nested.intercept;
4148
4149 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
4150 break;
4151
4152 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4153 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4154
4155 if (info->intercept == x86_intercept_lmsw) {
4156 cr0 &= 0xfUL;
4157 val &= 0xfUL;
4158 /* lmsw can't clear PE - catch this here */
4159 if (cr0 & X86_CR0_PE)
4160 val |= X86_CR0_PE;
4161 }
4162
4163 if (cr0 ^ val)
4164 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4165
4166 break;
4167 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004168 case SVM_EXIT_READ_DR0:
4169 case SVM_EXIT_WRITE_DR0:
4170 icpt_info.exit_code += info->modrm_reg;
4171 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004172 case SVM_EXIT_MSR:
4173 if (info->intercept == x86_intercept_wrmsr)
4174 vmcb->control.exit_info_1 = 1;
4175 else
4176 vmcb->control.exit_info_1 = 0;
4177 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004178 case SVM_EXIT_PAUSE:
4179 /*
4180 * We get this for NOP only, but pause
4181 * is rep not, check this here
4182 */
4183 if (info->rep_prefix != REPE_PREFIX)
4184 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02004185 case SVM_EXIT_IOIO: {
4186 u64 exit_info;
4187 u32 bytes;
4188
4189 exit_info = (vcpu->arch.regs[VCPU_REGS_RDX] & 0xffff) << 16;
4190
4191 if (info->intercept == x86_intercept_in ||
4192 info->intercept == x86_intercept_ins) {
4193 exit_info |= SVM_IOIO_TYPE_MASK;
4194 bytes = info->src_bytes;
4195 } else {
4196 bytes = info->dst_bytes;
4197 }
4198
4199 if (info->intercept == x86_intercept_outs ||
4200 info->intercept == x86_intercept_ins)
4201 exit_info |= SVM_IOIO_STR_MASK;
4202
4203 if (info->rep_prefix)
4204 exit_info |= SVM_IOIO_REP_MASK;
4205
4206 bytes = min(bytes, 4u);
4207
4208 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4209
4210 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4211
4212 vmcb->control.exit_info_1 = exit_info;
4213 vmcb->control.exit_info_2 = info->next_rip;
4214
4215 break;
4216 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004217 default:
4218 break;
4219 }
4220
4221 vmcb->control.next_rip = info->next_rip;
4222 vmcb->control.exit_code = icpt_info.exit_code;
4223 vmexit = nested_svm_exit_handled(svm);
4224
4225 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4226 : X86EMUL_CONTINUE;
4227
4228out:
4229 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004230}
4231
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004232static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004233 .cpu_has_kvm_support = has_svm,
4234 .disabled_by_bios = is_disabled,
4235 .hardware_setup = svm_hardware_setup,
4236 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004237 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004238 .hardware_enable = svm_hardware_enable,
4239 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004240 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004241
4242 .vcpu_create = svm_create_vcpu,
4243 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004244 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004245
Avi Kivity04d2cc72007-09-10 18:10:54 +03004246 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004247 .vcpu_load = svm_vcpu_load,
4248 .vcpu_put = svm_vcpu_put,
4249
Jan Kiszkac8639012012-09-21 05:42:55 +02004250 .update_db_bp_intercept = update_db_bp_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004251 .get_msr = svm_get_msr,
4252 .set_msr = svm_set_msr,
4253 .get_segment_base = svm_get_segment_base,
4254 .get_segment = svm_get_segment,
4255 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004256 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004257 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004258 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004259 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004260 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004261 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004262 .set_cr3 = svm_set_cr3,
4263 .set_cr4 = svm_set_cr4,
4264 .set_efer = svm_set_efer,
4265 .get_idt = svm_get_idt,
4266 .set_idt = svm_set_idt,
4267 .get_gdt = svm_get_gdt,
4268 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004269 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004270 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004271 .get_rflags = svm_get_rflags,
4272 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02004273 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004274 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004275
Avi Kivity6aa8b732006-12-10 02:21:36 -08004276 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004277
Avi Kivity6aa8b732006-12-10 02:21:36 -08004278 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004279 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004280 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004281 .set_interrupt_shadow = svm_set_interrupt_shadow,
4282 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004283 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004284 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004285 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004286 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004287 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004288 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004289 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004290 .get_nmi_mask = svm_get_nmi_mask,
4291 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004292 .enable_nmi_window = enable_nmi_window,
4293 .enable_irq_window = enable_irq_window,
4294 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02004295
4296 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004297 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004298 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004299
Avi Kivity586f9602010-11-18 13:09:54 +02004300 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004301
Sheng Yang17cc3932010-01-05 19:02:27 +08004302 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004303
4304 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004305
4306 .rdtscp_supported = svm_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00004307 .invpcid_supported = svm_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004308
4309 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004310
4311 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004312
Joerg Roedel4051b182011-03-25 09:44:49 +01004313 .set_tsc_khz = svm_set_tsc_khz,
Will Auldba904632012-11-29 12:42:50 -08004314 .read_tsc_offset = svm_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004315 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004316 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01004317 .compute_tsc_offset = svm_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03004318 .read_l1_tsc = svm_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004319
4320 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004321
4322 .check_intercept = svm_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004323};
4324
4325static int __init svm_init(void)
4326{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004327 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004328 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004329}
4330
4331static void __exit svm_exit(void)
4332{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004333 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004334}
4335
4336module_init(svm_init)
4337module_exit(svm_exit)