blob: 748569ef8505a8c3d6e48f87cd2afd083e3679de [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>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020025#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/vmalloc.h>
27#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030029#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080031
Joerg Roedel67ec6602010-05-17 14:43:35 +020032#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040033#include <asm/desc.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020034#include <asm/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Eduardo Habkost63d11422008-11-17 19:03:20 -020036#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030037#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020038
Avi Kivity4ecac3f2008-05-13 13:23:38 +030039#define __ex(x) __kvm_handle_fault_on_reboot(x)
40
Avi Kivity6aa8b732006-12-10 02:21:36 -080041MODULE_AUTHOR("Qumranet");
42MODULE_LICENSE("GPL");
43
44#define IOPM_ALLOC_ORDER 2
45#define MSRPM_ALLOC_ORDER 1
46
Avi Kivity6aa8b732006-12-10 02:21:36 -080047#define SEG_TYPE_LDT 2
48#define SEG_TYPE_BUSY_TSS16 3
49
Andre Przywara6bc31bd2010-04-11 23:07:28 +020050#define SVM_FEATURE_NPT (1 << 0)
51#define SVM_FEATURE_LBRV (1 << 1)
52#define SVM_FEATURE_SVML (1 << 2)
53#define SVM_FEATURE_NRIP (1 << 3)
54#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030055
Joerg Roedel410e4d52009-08-07 11:49:44 +020056#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
57#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
58#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
59
Joerg Roedel24e09cb2008-02-13 18:58:47 +010060#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
61
Joerg Roedel67ec6602010-05-17 14:43:35 +020062static bool erratum_383_found __read_mostly;
63
Avi Kivity6c8166a2009-05-31 18:15:37 +030064static const u32 host_save_user_msrs[] = {
65#ifdef CONFIG_X86_64
66 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
67 MSR_FS_BASE,
68#endif
69 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
70};
71
72#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
73
74struct kvm_vcpu;
75
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020076struct nested_state {
77 struct vmcb *hsave;
78 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010079 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020080 u64 vmcb;
81
82 /* These are the merged vectors */
83 u32 *msrpm;
84
85 /* gpa pointers to the real vectors */
86 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +010087 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020088
Joerg Roedelcd3ff652009-10-09 16:08:26 +020089 /* A VMEXIT is required but not yet emulated */
90 bool exit_required;
91
Joerg Roedelcda00082010-09-02 17:29:46 +020092 /*
93 * If we vmexit during an instruction emulation we need this to restore
94 * the l1 guest rip after the emulation
95 */
96 unsigned long vmexit_rip;
97 unsigned long vmexit_rsp;
98 unsigned long vmexit_rax;
99
Joerg Roedelaad42c62009-08-07 11:49:34 +0200100 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100101 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100102 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +0200103 u32 intercept_exceptions;
104 u64 intercept;
105
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200106 /* Nested Paging related state */
107 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200108};
109
Joerg Roedel323c3d82010-03-01 15:34:37 +0100110#define MSRPM_OFFSETS 16
111static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
112
Avi Kivity6c8166a2009-05-31 18:15:37 +0300113struct vcpu_svm {
114 struct kvm_vcpu vcpu;
115 struct vmcb *vmcb;
116 unsigned long vmcb_pa;
117 struct svm_cpu_data *svm_data;
118 uint64_t asid_generation;
119 uint64_t sysenter_esp;
120 uint64_t sysenter_eip;
121
122 u64 next_rip;
123
124 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200125 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200126 u16 fs;
127 u16 gs;
128 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200129 u64 gs_base;
130 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300131
132 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300133
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200134 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200135
136 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100137
138 unsigned int3_injected;
139 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200140 u32 apf_reason;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300141};
142
Joerg Roedel455716f2010-03-01 15:34:35 +0100143#define MSR_INVALID 0xffffffffU
144
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100145static struct svm_direct_access_msrs {
146 u32 index; /* Index of the MSR */
147 bool always; /* True if intercept is always on */
148} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400149 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100150 { .index = MSR_IA32_SYSENTER_CS, .always = true },
151#ifdef CONFIG_X86_64
152 { .index = MSR_GS_BASE, .always = true },
153 { .index = MSR_FS_BASE, .always = true },
154 { .index = MSR_KERNEL_GS_BASE, .always = true },
155 { .index = MSR_LSTAR, .always = true },
156 { .index = MSR_CSTAR, .always = true },
157 { .index = MSR_SYSCALL_MASK, .always = true },
158#endif
159 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
160 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
161 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
162 { .index = MSR_IA32_LASTINTTOIP, .always = false },
163 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164};
165
166/* enable NPT for AMD64 and X86 with PAE */
167#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
168static bool npt_enabled = true;
169#else
Joerg Roedele0231712010-02-24 18:59:10 +0100170static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171#endif
172static int npt = 1;
173
174module_param(npt, int, S_IRUGO);
175
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200176static int nested = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177module_param(nested, int, S_IRUGO);
178
179static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200180static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181
Joerg Roedel410e4d52009-08-07 11:49:44 +0200182static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100183static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800185static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
186 bool has_error_code, u32 error_code);
187
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100188enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100189 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
190 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100191 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100192 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100193 VMCB_INTR, /* int_ctl, int_vector */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100194 VMCB_DIRTY_MAX,
195};
196
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100197/* TPR is always written before VMRUN */
198#define VMCB_ALWAYS_DIRTY_MASK (1U << VMCB_INTR)
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100199
200static inline void mark_all_dirty(struct vmcb *vmcb)
201{
202 vmcb->control.clean = 0;
203}
204
205static inline void mark_all_clean(struct vmcb *vmcb)
206{
207 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
208 & ~VMCB_ALWAYS_DIRTY_MASK;
209}
210
211static inline void mark_dirty(struct vmcb *vmcb, int bit)
212{
213 vmcb->control.clean &= ~(1 << bit);
214}
215
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
217{
218 return container_of(vcpu, struct vcpu_svm, vcpu);
219}
220
Joerg Roedel384c6362010-11-30 18:03:56 +0100221static void recalc_intercepts(struct vcpu_svm *svm)
222{
223 struct vmcb_control_area *c, *h;
224 struct nested_state *g;
225
Joerg Roedel116a0a22010-12-03 11:45:49 +0100226 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
227
Joerg Roedel384c6362010-11-30 18:03:56 +0100228 if (!is_guest_mode(&svm->vcpu))
229 return;
230
231 c = &svm->vmcb->control;
232 h = &svm->nested.hsave->control;
233 g = &svm->nested;
234
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100235 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100236 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100237 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
238 c->intercept = h->intercept | g->intercept;
239}
240
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100241static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
242{
243 if (is_guest_mode(&svm->vcpu))
244 return svm->nested.hsave;
245 else
246 return svm->vmcb;
247}
248
249static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
250{
251 struct vmcb *vmcb = get_host_vmcb(svm);
252
253 vmcb->control.intercept_cr |= (1U << bit);
254
255 recalc_intercepts(svm);
256}
257
258static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
259{
260 struct vmcb *vmcb = get_host_vmcb(svm);
261
262 vmcb->control.intercept_cr &= ~(1U << bit);
263
264 recalc_intercepts(svm);
265}
266
267static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
268{
269 struct vmcb *vmcb = get_host_vmcb(svm);
270
271 return vmcb->control.intercept_cr & (1U << bit);
272}
273
Joerg Roedel3aed0412010-11-30 18:03:58 +0100274static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
275{
276 struct vmcb *vmcb = get_host_vmcb(svm);
277
278 vmcb->control.intercept_dr |= (1U << bit);
279
280 recalc_intercepts(svm);
281}
282
283static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
284{
285 struct vmcb *vmcb = get_host_vmcb(svm);
286
287 vmcb->control.intercept_dr &= ~(1U << bit);
288
289 recalc_intercepts(svm);
290}
291
Joerg Roedel18c918c2010-11-30 18:03:59 +0100292static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
293{
294 struct vmcb *vmcb = get_host_vmcb(svm);
295
296 vmcb->control.intercept_exceptions |= (1U << bit);
297
298 recalc_intercepts(svm);
299}
300
301static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
302{
303 struct vmcb *vmcb = get_host_vmcb(svm);
304
305 vmcb->control.intercept_exceptions &= ~(1U << bit);
306
307 recalc_intercepts(svm);
308}
309
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100310static inline void set_intercept(struct vcpu_svm *svm, int bit)
311{
312 struct vmcb *vmcb = get_host_vmcb(svm);
313
314 vmcb->control.intercept |= (1ULL << bit);
315
316 recalc_intercepts(svm);
317}
318
319static inline void clr_intercept(struct vcpu_svm *svm, int bit)
320{
321 struct vmcb *vmcb = get_host_vmcb(svm);
322
323 vmcb->control.intercept &= ~(1ULL << bit);
324
325 recalc_intercepts(svm);
326}
327
Joerg Roedel2af91942009-08-07 11:49:28 +0200328static inline void enable_gif(struct vcpu_svm *svm)
329{
330 svm->vcpu.arch.hflags |= HF_GIF_MASK;
331}
332
333static inline void disable_gif(struct vcpu_svm *svm)
334{
335 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
336}
337
338static inline bool gif_set(struct vcpu_svm *svm)
339{
340 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
341}
342
Avi Kivity6aa8b732006-12-10 02:21:36 -0800343static unsigned long iopm_base;
344
345struct kvm_ldttss_desc {
346 u16 limit0;
347 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100348 unsigned base1:8, type:5, dpl:2, p:1;
349 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800350 u32 base3;
351 u32 zero1;
352} __attribute__((packed));
353
354struct svm_cpu_data {
355 int cpu;
356
Avi Kivity5008fdf2007-04-02 13:05:50 +0300357 u64 asid_generation;
358 u32 max_asid;
359 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360 struct kvm_ldttss_desc *tss_desc;
361
362 struct page *save_area;
363};
364
365static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300366static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367
368struct svm_init_data {
369 int cpu;
370 int r;
371};
372
373static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
374
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200375#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800376#define MSRS_RANGE_SIZE 2048
377#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
378
Joerg Roedel455716f2010-03-01 15:34:35 +0100379static u32 svm_msrpm_offset(u32 msr)
380{
381 u32 offset;
382 int i;
383
384 for (i = 0; i < NUM_MSR_MAPS; i++) {
385 if (msr < msrpm_ranges[i] ||
386 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
387 continue;
388
389 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
390 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
391
392 /* Now we have the u8 offset - but need the u32 offset */
393 return offset / 4;
394 }
395
396 /* MSR not in any range */
397 return MSR_INVALID;
398}
399
Avi Kivity6aa8b732006-12-10 02:21:36 -0800400#define MAX_INST_SIZE 15
401
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402static inline void clgi(void)
403{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300404 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405}
406
407static inline void stgi(void)
408{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300409 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800410}
411
412static inline void invlpga(unsigned long addr, u32 asid)
413{
Joerg Roedele0231712010-02-24 18:59:10 +0100414 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415}
416
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417static inline void force_new_asid(struct kvm_vcpu *vcpu)
418{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400419 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420}
421
422static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
423{
424 force_new_asid(vcpu);
425}
426
Joerg Roedel4b161842010-09-10 17:31:03 +0200427static int get_npt_level(void)
428{
429#ifdef CONFIG_X86_64
430 return PT64_ROOT_LEVEL;
431#else
432 return PT32E_ROOT_LEVEL;
433#endif
434}
435
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
437{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000438 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100439 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600440 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441
Alexander Graf9962d032008-11-25 20:17:02 +0100442 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443}
444
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445static int is_external_interrupt(u32 info)
446{
447 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
448 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
449}
450
Glauber Costa2809f5d2009-05-12 16:21:05 -0400451static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
452{
453 struct vcpu_svm *svm = to_svm(vcpu);
454 u32 ret = 0;
455
456 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100457 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400458 return ret & mask;
459}
460
461static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
462{
463 struct vcpu_svm *svm = to_svm(vcpu);
464
465 if (mask == 0)
466 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
467 else
468 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
469
470}
471
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
473{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400474 struct vcpu_svm *svm = to_svm(vcpu);
475
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200476 if (svm->vmcb->control.next_rip != 0)
477 svm->next_rip = svm->vmcb->control.next_rip;
478
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400479 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300480 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300481 EMULATE_DONE)
482 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483 return;
484 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300485 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
486 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
487 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800488
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300489 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400490 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491}
492
Jan Kiszka116a4752010-02-23 17:47:54 +0100493static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200494 bool has_error_code, u32 error_code,
495 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100496{
497 struct vcpu_svm *svm = to_svm(vcpu);
498
Joerg Roedele0231712010-02-24 18:59:10 +0100499 /*
500 * If we are within a nested VM we'd better #VMEXIT and let the guest
501 * handle the exception
502 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200503 if (!reinject &&
504 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100505 return;
506
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200507 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100508 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
509
510 /*
511 * For guest debugging where we have to reinject #BP if some
512 * INT3 is guest-owned:
513 * Emulate nRIP by moving RIP forward. Will fail if injection
514 * raises a fault that is not intercepted. Still better than
515 * failing in all cases.
516 */
517 skip_emulated_instruction(&svm->vcpu);
518 rip = kvm_rip_read(&svm->vcpu);
519 svm->int3_rip = rip + svm->vmcb->save.cs.base;
520 svm->int3_injected = rip - old_rip;
521 }
522
Jan Kiszka116a4752010-02-23 17:47:54 +0100523 svm->vmcb->control.event_inj = nr
524 | SVM_EVTINJ_VALID
525 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
526 | SVM_EVTINJ_TYPE_EXEPT;
527 svm->vmcb->control.event_inj_err = error_code;
528}
529
Joerg Roedel67ec6602010-05-17 14:43:35 +0200530static void svm_init_erratum_383(void)
531{
532 u32 low, high;
533 int err;
534 u64 val;
535
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200536 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200537 return;
538
539 /* Use _safe variants to not break nested virtualization */
540 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
541 if (err)
542 return;
543
544 val |= (1ULL << 47);
545
546 low = lower_32_bits(val);
547 high = upper_32_bits(val);
548
549 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
550
551 erratum_383_found = true;
552}
553
Avi Kivity6aa8b732006-12-10 02:21:36 -0800554static int has_svm(void)
555{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200556 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800557
Eduardo Habkost63d11422008-11-17 19:03:20 -0200558 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800559 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800560 return 0;
561 }
562
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 return 1;
564}
565
566static void svm_hardware_disable(void *garbage)
567{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200568 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800569}
570
Alexander Graf10474ae2009-09-15 11:37:46 +0200571static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572{
573
Tejun Heo0fe1e002009-10-29 22:34:14 +0900574 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200576 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577 struct desc_struct *gdt;
578 int me = raw_smp_processor_id();
579
Alexander Graf10474ae2009-09-15 11:37:46 +0200580 rdmsrl(MSR_EFER, efer);
581 if (efer & EFER_SVME)
582 return -EBUSY;
583
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000585 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
586 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200587 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900589 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590
Tejun Heo0fe1e002009-10-29 22:34:14 +0900591 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000592 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200594 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595 }
596
Tejun Heo0fe1e002009-10-29 22:34:14 +0900597 sd->asid_generation = 1;
598 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
599 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800600
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200601 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200602 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900603 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800604
Alexander Graf9962d032008-11-25 20:17:02 +0100605 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800606
Linus Torvaldsd0316552009-12-14 09:58:24 -0800607 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200608
Joerg Roedel67ec6602010-05-17 14:43:35 +0200609 svm_init_erratum_383();
610
Alexander Graf10474ae2009-09-15 11:37:46 +0200611 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612}
613
Joerg Roedel0da1db752008-07-02 16:02:11 +0200614static void svm_cpu_uninit(int cpu)
615{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900616 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200617
Tejun Heo0fe1e002009-10-29 22:34:14 +0900618 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200619 return;
620
621 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900622 __free_page(sd->save_area);
623 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200624}
625
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626static int svm_cpu_init(int cpu)
627{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900628 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800629 int r;
630
Tejun Heo0fe1e002009-10-29 22:34:14 +0900631 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
632 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900634 sd->cpu = cpu;
635 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900637 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638 goto err_1;
639
Tejun Heo0fe1e002009-10-29 22:34:14 +0900640 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641
642 return 0;
643
644err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900645 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646 return r;
647
648}
649
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100650static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651{
652 int i;
653
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100654 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
655 if (direct_access_msrs[i].index == index)
656 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800657
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100658 return false;
659}
660
Avi Kivity6aa8b732006-12-10 02:21:36 -0800661static void set_msr_interception(u32 *msrpm, unsigned msr,
662 int read, int write)
663{
Joerg Roedel455716f2010-03-01 15:34:35 +0100664 u8 bit_read, bit_write;
665 unsigned long tmp;
666 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100668 /*
669 * If this warning triggers extend the direct_access_msrs list at the
670 * beginning of the file
671 */
672 WARN_ON(!valid_msr_intercept(msr));
673
Joerg Roedel455716f2010-03-01 15:34:35 +0100674 offset = svm_msrpm_offset(msr);
675 bit_read = 2 * (msr & 0x0f);
676 bit_write = 2 * (msr & 0x0f) + 1;
677 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678
Joerg Roedel455716f2010-03-01 15:34:35 +0100679 BUG_ON(offset == MSR_INVALID);
680
681 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
682 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
683
684 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685}
686
Joerg Roedelf65c2292008-02-13 18:58:46 +0100687static void svm_vcpu_init_msrpm(u32 *msrpm)
688{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100689 int i;
690
Joerg Roedelf65c2292008-02-13 18:58:46 +0100691 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
692
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100693 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
694 if (!direct_access_msrs[i].always)
695 continue;
696
697 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
698 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100699}
700
Joerg Roedel323c3d82010-03-01 15:34:37 +0100701static void add_msr_offset(u32 offset)
702{
703 int i;
704
705 for (i = 0; i < MSRPM_OFFSETS; ++i) {
706
707 /* Offset already in list? */
708 if (msrpm_offsets[i] == offset)
709 return;
710
711 /* Slot used by another offset? */
712 if (msrpm_offsets[i] != MSR_INVALID)
713 continue;
714
715 /* Add offset to list */
716 msrpm_offsets[i] = offset;
717
718 return;
719 }
720
721 /*
722 * If this BUG triggers the msrpm_offsets table has an overflow. Just
723 * increase MSRPM_OFFSETS in this case.
724 */
725 BUG();
726}
727
728static void init_msrpm_offsets(void)
729{
730 int i;
731
732 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
733
734 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
735 u32 offset;
736
737 offset = svm_msrpm_offset(direct_access_msrs[i].index);
738 BUG_ON(offset == MSR_INVALID);
739
740 add_msr_offset(offset);
741 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800742}
743
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100744static void svm_enable_lbrv(struct vcpu_svm *svm)
745{
746 u32 *msrpm = svm->msrpm;
747
748 svm->vmcb->control.lbr_ctl = 1;
749 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
750 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
751 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
752 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
753}
754
755static void svm_disable_lbrv(struct vcpu_svm *svm)
756{
757 u32 *msrpm = svm->msrpm;
758
759 svm->vmcb->control.lbr_ctl = 0;
760 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
761 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
762 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
763 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
764}
765
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766static __init int svm_hardware_setup(void)
767{
768 int cpu;
769 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100770 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771 int r;
772
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
774
775 if (!iopm_pages)
776 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300777
778 iopm_va = page_address(iopm_pages);
779 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
781
Joerg Roedel323c3d82010-03-01 15:34:37 +0100782 init_msrpm_offsets();
783
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100784 if (boot_cpu_has(X86_FEATURE_NX))
785 kvm_enable_efer_bits(EFER_NX);
786
Alexander Graf1b2fd702009-02-02 16:23:51 +0100787 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
788 kvm_enable_efer_bits(EFER_FFXSR);
789
Alexander Graf236de052008-11-25 20:17:10 +0100790 if (nested) {
791 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200792 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100793 }
794
Zachary Amsden3230bb42009-09-29 11:38:37 -1000795 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 r = svm_cpu_init(cpu);
797 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100798 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100800
801 svm_features = cpuid_edx(SVM_CPUID_FUNC);
802
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200803 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100804 npt_enabled = false;
805
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100806 if (npt_enabled && !npt) {
807 printk(KERN_INFO "kvm: Nested Paging disabled\n");
808 npt_enabled = false;
809 }
810
Joerg Roedel18552672008-02-07 13:47:41 +0100811 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100812 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100813 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200814 } else
815 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100816
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817 return 0;
818
Joerg Roedelf65c2292008-02-13 18:58:46 +0100819err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
821 iopm_base = 0;
822 return r;
823}
824
825static __exit void svm_hardware_unsetup(void)
826{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200827 int cpu;
828
Zachary Amsden3230bb42009-09-29 11:38:37 -1000829 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200830 svm_cpu_uninit(cpu);
831
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100833 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834}
835
836static void init_seg(struct vmcb_seg *seg)
837{
838 seg->selector = 0;
839 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100840 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841 seg->limit = 0xffff;
842 seg->base = 0;
843}
844
845static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
846{
847 seg->selector = 0;
848 seg->attrib = SVM_SELECTOR_P_MASK | type;
849 seg->limit = 0xffff;
850 seg->base = 0;
851}
852
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000853static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
854{
855 struct vcpu_svm *svm = to_svm(vcpu);
856 u64 g_tsc_offset = 0;
857
Joerg Roedel20307532010-11-29 17:51:48 +0100858 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000859 g_tsc_offset = svm->vmcb->control.tsc_offset -
860 svm->nested.hsave->control.tsc_offset;
861 svm->nested.hsave->control.tsc_offset = offset;
862 }
863
864 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +0100865
866 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000867}
868
Zachary Amsdene48672f2010-08-19 22:07:23 -1000869static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
870{
871 struct vcpu_svm *svm = to_svm(vcpu);
872
873 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +0100874 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -1000875 svm->nested.hsave->control.tsc_offset += adjustment;
Joerg Roedel116a0a22010-12-03 11:45:49 +0100876 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -1000877}
878
Joerg Roedele6101a92008-02-13 18:58:45 +0100879static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880{
Joerg Roedele6101a92008-02-13 18:58:45 +0100881 struct vmcb_control_area *control = &svm->vmcb->control;
882 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883
Avi Kivitybff78272010-01-07 13:16:08 +0200884 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100885 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +0200886
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100887 set_cr_intercept(svm, INTERCEPT_CR0_READ);
888 set_cr_intercept(svm, INTERCEPT_CR3_READ);
889 set_cr_intercept(svm, INTERCEPT_CR4_READ);
890 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
891 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
892 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
893 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894
Joerg Roedel3aed0412010-11-30 18:03:58 +0100895 set_dr_intercept(svm, INTERCEPT_DR0_READ);
896 set_dr_intercept(svm, INTERCEPT_DR1_READ);
897 set_dr_intercept(svm, INTERCEPT_DR2_READ);
898 set_dr_intercept(svm, INTERCEPT_DR3_READ);
899 set_dr_intercept(svm, INTERCEPT_DR4_READ);
900 set_dr_intercept(svm, INTERCEPT_DR5_READ);
901 set_dr_intercept(svm, INTERCEPT_DR6_READ);
902 set_dr_intercept(svm, INTERCEPT_DR7_READ);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903
Joerg Roedel3aed0412010-11-30 18:03:58 +0100904 set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
905 set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
906 set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
907 set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
908 set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
909 set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
910 set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
911 set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912
Joerg Roedel18c918c2010-11-30 18:03:59 +0100913 set_exception_intercept(svm, PF_VECTOR);
914 set_exception_intercept(svm, UD_VECTOR);
915 set_exception_intercept(svm, MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100917 set_intercept(svm, INTERCEPT_INTR);
918 set_intercept(svm, INTERCEPT_NMI);
919 set_intercept(svm, INTERCEPT_SMI);
920 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
921 set_intercept(svm, INTERCEPT_CPUID);
922 set_intercept(svm, INTERCEPT_INVD);
923 set_intercept(svm, INTERCEPT_HLT);
924 set_intercept(svm, INTERCEPT_INVLPG);
925 set_intercept(svm, INTERCEPT_INVLPGA);
926 set_intercept(svm, INTERCEPT_IOIO_PROT);
927 set_intercept(svm, INTERCEPT_MSR_PROT);
928 set_intercept(svm, INTERCEPT_TASK_SWITCH);
929 set_intercept(svm, INTERCEPT_SHUTDOWN);
930 set_intercept(svm, INTERCEPT_VMRUN);
931 set_intercept(svm, INTERCEPT_VMMCALL);
932 set_intercept(svm, INTERCEPT_VMLOAD);
933 set_intercept(svm, INTERCEPT_VMSAVE);
934 set_intercept(svm, INTERCEPT_STGI);
935 set_intercept(svm, INTERCEPT_CLGI);
936 set_intercept(svm, INTERCEPT_SKINIT);
937 set_intercept(svm, INTERCEPT_WBINVD);
938 set_intercept(svm, INTERCEPT_MONITOR);
939 set_intercept(svm, INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940
941 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100942 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 control->int_ctl = V_INTR_MASKING_MASK;
944
945 init_seg(&save->es);
946 init_seg(&save->ss);
947 init_seg(&save->ds);
948 init_seg(&save->fs);
949 init_seg(&save->gs);
950
951 save->cs.selector = 0xf000;
952 /* Executable/Readable Code Segment */
953 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
954 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
955 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800956 /*
957 * cs.base should really be 0xffff0000, but vmx can't handle that, so
958 * be consistent with it.
959 *
960 * Replace when we have real mode working for vmx.
961 */
962 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963
964 save->gdtr.limit = 0xffff;
965 save->idtr.limit = 0xffff;
966
967 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
968 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
969
Marcelo Tosattieaa48512010-08-31 19:13:14 -0300970 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -0400971 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 save->dr7 = 0x400;
973 save->rflags = 2;
974 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300975 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976
Joerg Roedele0231712010-02-24 18:59:10 +0100977 /*
978 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200979 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -0300981 svm->vcpu.arch.cr0 = 0;
982 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200983
Rusty Russell66aee912007-07-17 23:34:16 +1000984 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100986
987 if (npt_enabled) {
988 /* Setup VMCB for Nested Paging */
989 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100990 clr_intercept(svm, INTERCEPT_TASK_SWITCH);
991 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +0100992 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100993 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
994 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100995 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100996 save->cr3 = 0;
997 save->cr4 = 0;
998 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300999 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01001000
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001001 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001002 svm->vcpu.arch.hflags = 0;
1003
Avi Kivity2a6b20b2010-11-09 16:15:42 +02001004 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -05001005 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001006 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001007 }
1008
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001009 mark_all_dirty(svm->vmcb);
1010
Joerg Roedel2af91942009-08-07 11:49:28 +02001011 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012}
1013
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001014static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001015{
1016 struct vcpu_svm *svm = to_svm(vcpu);
1017
Joerg Roedele6101a92008-02-13 18:58:45 +01001018 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001019
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001020 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001021 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001022 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
1023 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +02001024 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001025 vcpu->arch.regs_avail = ~0;
1026 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001027
1028 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001029}
1030
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001031static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001033 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001035 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001036 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001037 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001038 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039
Rusty Russellc16f8622007-07-30 21:12:19 +10001040 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001041 if (!svm) {
1042 err = -ENOMEM;
1043 goto out;
1044 }
1045
1046 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1047 if (err)
1048 goto free_svm;
1049
Joerg Roedelf65c2292008-02-13 18:58:46 +01001050 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001051 page = alloc_page(GFP_KERNEL);
1052 if (!page)
1053 goto uninit;
1054
Joerg Roedelf65c2292008-02-13 18:58:46 +01001055 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1056 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001057 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001058
1059 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1060 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001061 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001062
Alexander Grafb286d5d2008-11-25 20:17:05 +01001063 hsave_page = alloc_page(GFP_KERNEL);
1064 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001065 goto free_page3;
1066
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001067 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001068
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001069 svm->msrpm = page_address(msrpm_pages);
1070 svm_vcpu_init_msrpm(svm->msrpm);
1071
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001072 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001073 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001074
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001075 svm->vmcb = page_address(page);
1076 clear_page(svm->vmcb);
1077 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1078 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +01001079 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001080 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001081
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001082 err = fx_init(&svm->vcpu);
1083 if (err)
1084 goto free_page4;
1085
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001086 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001087 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001088 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001090 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001091
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001092free_page4:
1093 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001094free_page3:
1095 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1096free_page2:
1097 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1098free_page1:
1099 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001100uninit:
1101 kvm_vcpu_uninit(&svm->vcpu);
1102free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001103 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001104out:
1105 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106}
1107
1108static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1109{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001110 struct vcpu_svm *svm = to_svm(vcpu);
1111
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001112 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001113 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001114 __free_page(virt_to_page(svm->nested.hsave));
1115 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001116 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001117 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118}
1119
Avi Kivity15ad7142007-07-11 18:17:21 +03001120static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001122 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001123 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001124
Avi Kivity0cc50642007-03-25 12:07:27 +02001125 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001126 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001127 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001128 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001129
Avi Kivity82ca2d12010-10-21 12:20:34 +02001130#ifdef CONFIG_X86_64
1131 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1132#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001133 savesegment(fs, svm->host.fs);
1134 savesegment(gs, svm->host.gs);
1135 svm->host.ldt = kvm_read_ldt();
1136
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001137 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001138 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139}
1140
1141static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1142{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001143 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001144 int i;
1145
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001146 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001147 kvm_load_ldt(svm->host.ldt);
1148#ifdef CONFIG_X86_64
1149 loadsegment(fs, svm->host.fs);
1150 load_gs_index(svm->host.gs);
1151 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
1152#else
1153 loadsegment(gs, svm->host.gs);
1154#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001155 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001156 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157}
1158
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1160{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001161 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001162}
1163
1164static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1165{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001166 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167}
1168
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001169static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1170{
1171 switch (reg) {
1172 case VCPU_EXREG_PDPTR:
1173 BUG_ON(!npt_enabled);
Joerg Roedelff03a072010-09-10 17:30:57 +02001174 load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001175 break;
1176 default:
1177 BUG();
1178 }
1179}
1180
Alexander Graff0b85052008-11-25 20:17:01 +01001181static void svm_set_vintr(struct vcpu_svm *svm)
1182{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001183 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001184}
1185
1186static void svm_clear_vintr(struct vcpu_svm *svm)
1187{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001188 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001189}
1190
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1192{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001193 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194
1195 switch (seg) {
1196 case VCPU_SREG_CS: return &save->cs;
1197 case VCPU_SREG_DS: return &save->ds;
1198 case VCPU_SREG_ES: return &save->es;
1199 case VCPU_SREG_FS: return &save->fs;
1200 case VCPU_SREG_GS: return &save->gs;
1201 case VCPU_SREG_SS: return &save->ss;
1202 case VCPU_SREG_TR: return &save->tr;
1203 case VCPU_SREG_LDTR: return &save->ldtr;
1204 }
1205 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001206 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207}
1208
1209static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1210{
1211 struct vmcb_seg *s = svm_seg(vcpu, seg);
1212
1213 return s->base;
1214}
1215
1216static void svm_get_segment(struct kvm_vcpu *vcpu,
1217 struct kvm_segment *var, int seg)
1218{
1219 struct vmcb_seg *s = svm_seg(vcpu, seg);
1220
1221 var->base = s->base;
1222 var->limit = s->limit;
1223 var->selector = s->selector;
1224 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1225 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1226 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1227 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1228 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1229 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1230 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1231 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001232
Joerg Roedele0231712010-02-24 18:59:10 +01001233 /*
1234 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001235 * for cross vendor migration purposes by "not present"
1236 */
1237 var->unusable = !var->present || (var->type == 0);
1238
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001239 switch (seg) {
1240 case VCPU_SREG_CS:
1241 /*
1242 * SVM always stores 0 for the 'G' bit in the CS selector in
1243 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1244 * Intel's VMENTRY has a check on the 'G' bit.
1245 */
Amit Shah25022ac2008-10-27 09:04:17 +00001246 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001247 break;
1248 case VCPU_SREG_TR:
1249 /*
1250 * Work around a bug where the busy flag in the tr selector
1251 * isn't exposed
1252 */
Amit Shahc0d09822008-10-27 09:04:18 +00001253 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001254 break;
1255 case VCPU_SREG_DS:
1256 case VCPU_SREG_ES:
1257 case VCPU_SREG_FS:
1258 case VCPU_SREG_GS:
1259 /*
1260 * The accessed bit must always be set in the segment
1261 * descriptor cache, although it can be cleared in the
1262 * descriptor, the cached bit always remains at 1. Since
1263 * Intel has a check on this, set it here to support
1264 * cross-vendor migration.
1265 */
1266 if (!var->unusable)
1267 var->type |= 0x1;
1268 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001269 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001270 /*
1271 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001272 * descriptor is left as 1, although the whole segment has
1273 * been made unusable. Clear it here to pass an Intel VMX
1274 * entry check when cross vendor migrating.
1275 */
1276 if (var->unusable)
1277 var->db = 0;
1278 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001279 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280}
1281
Izik Eidus2e4d2652008-03-24 19:38:34 +02001282static int svm_get_cpl(struct kvm_vcpu *vcpu)
1283{
1284 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1285
1286 return save->cpl;
1287}
1288
Gleb Natapov89a27f42010-02-16 10:51:48 +02001289static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001290{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001291 struct vcpu_svm *svm = to_svm(vcpu);
1292
Gleb Natapov89a27f42010-02-16 10:51:48 +02001293 dt->size = svm->vmcb->save.idtr.limit;
1294 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295}
1296
Gleb Natapov89a27f42010-02-16 10:51:48 +02001297static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001299 struct vcpu_svm *svm = to_svm(vcpu);
1300
Gleb Natapov89a27f42010-02-16 10:51:48 +02001301 svm->vmcb->save.idtr.limit = dt->size;
1302 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303}
1304
Gleb Natapov89a27f42010-02-16 10:51:48 +02001305static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001307 struct vcpu_svm *svm = to_svm(vcpu);
1308
Gleb Natapov89a27f42010-02-16 10:51:48 +02001309 dt->size = svm->vmcb->save.gdtr.limit;
1310 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311}
1312
Gleb Natapov89a27f42010-02-16 10:51:48 +02001313static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001315 struct vcpu_svm *svm = to_svm(vcpu);
1316
Gleb Natapov89a27f42010-02-16 10:51:48 +02001317 svm->vmcb->save.gdtr.limit = dt->size;
1318 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319}
1320
Avi Kivitye8467fd2009-12-29 18:43:06 +02001321static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1322{
1323}
1324
Anthony Liguori25c4c272007-04-27 09:29:21 +03001325static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001326{
1327}
1328
Avi Kivityd2251572010-01-06 10:55:27 +02001329static void update_cr0_intercept(struct vcpu_svm *svm)
1330{
1331 ulong gcr0 = svm->vcpu.arch.cr0;
1332 u64 *hcr0 = &svm->vmcb->save.cr0;
1333
1334 if (!svm->vcpu.fpu_active)
1335 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1336 else
1337 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1338 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1339
1340
1341 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001342 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1343 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001344 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001345 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1346 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001347 }
1348}
1349
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1351{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001352 struct vcpu_svm *svm = to_svm(vcpu);
1353
Joerg Roedel20307532010-11-29 17:51:48 +01001354 if (is_guest_mode(vcpu)) {
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001355 /*
1356 * We are here because we run in nested mode, the host kvm
1357 * intercepts cr0 writes but the l1 hypervisor does not.
1358 * But the L1 hypervisor may intercept selective cr0 writes.
1359 * This needs to be checked here.
1360 */
1361 unsigned long old, new;
1362
1363 /* Remove bits that would trigger a real cr0 write intercept */
1364 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1365 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1366
1367 if (old == new) {
1368 /* cr0 write with ts and mp unchanged */
1369 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
Joerg Roedelcda00082010-09-02 17:29:46 +02001370 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
1371 svm->nested.vmexit_rip = kvm_rip_read(vcpu);
1372 svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
1373 svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001374 return;
Joerg Roedelcda00082010-09-02 17:29:46 +02001375 }
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001376 }
1377 }
1378
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001379#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001380 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001381 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001382 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001383 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384 }
1385
Mike Dayd77c26f2007-10-08 09:02:08 -04001386 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001387 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001388 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389 }
1390 }
1391#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001392 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001393
1394 if (!npt_enabled)
1395 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001396
1397 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001398 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001399 /*
1400 * re-enable caching here because the QEMU bios
1401 * does not do it - this results in some delay at
1402 * reboot
1403 */
1404 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001405 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001406 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407}
1408
1409static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1410{
Joerg Roedel6394b642008-04-09 14:15:29 +02001411 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001412 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1413
1414 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1415 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001416
Joerg Roedelec077262008-04-09 14:15:28 +02001417 vcpu->arch.cr4 = cr4;
1418 if (!npt_enabled)
1419 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001420 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001421 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422}
1423
1424static void svm_set_segment(struct kvm_vcpu *vcpu,
1425 struct kvm_segment *var, int seg)
1426{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001427 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428 struct vmcb_seg *s = svm_seg(vcpu, seg);
1429
1430 s->base = var->base;
1431 s->limit = var->limit;
1432 s->selector = var->selector;
1433 if (var->unusable)
1434 s->attrib = 0;
1435 else {
1436 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1437 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1438 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1439 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1440 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1441 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1442 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1443 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1444 }
1445 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001446 svm->vmcb->save.cpl
1447 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1449
1450}
1451
Gleb Natapov44c11432009-05-11 13:35:52 +03001452static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001454 struct vcpu_svm *svm = to_svm(vcpu);
1455
Joerg Roedel18c918c2010-11-30 18:03:59 +01001456 clr_exception_intercept(svm, DB_VECTOR);
1457 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001458
Jan Kiszka6be7d302009-10-18 13:24:54 +02001459 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001460 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001461
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001462 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1463 if (vcpu->guest_debug &
1464 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001465 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001466 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001467 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001468 } else
1469 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001470}
1471
Jan Kiszka355be0b2009-10-03 00:31:21 +02001472static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001473{
Gleb Natapov44c11432009-05-11 13:35:52 +03001474 struct vcpu_svm *svm = to_svm(vcpu);
1475
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001476 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1477 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1478 else
1479 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1480
Jan Kiszka355be0b2009-10-03 00:31:21 +02001481 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482}
1483
Tejun Heo0fe1e002009-10-29 22:34:14 +09001484static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001486 if (sd->next_asid > sd->max_asid) {
1487 ++sd->asid_generation;
1488 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001489 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490 }
1491
Tejun Heo0fe1e002009-10-29 22:34:14 +09001492 svm->asid_generation = sd->asid_generation;
1493 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001494
1495 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496}
1497
Gleb Natapov020df072010-04-13 10:05:23 +03001498static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001500 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001501
Gleb Natapov020df072010-04-13 10:05:23 +03001502 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503}
1504
Avi Kivity851ba692009-08-24 11:10:17 +03001505static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506{
Gleb Natapov631bc482010-10-14 11:22:52 +02001507 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001509 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510
Gleb Natapov631bc482010-10-14 11:22:52 +02001511 switch (svm->apf_reason) {
1512 default:
1513 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001514
Gleb Natapov631bc482010-10-14 11:22:52 +02001515 trace_kvm_page_fault(fault_address, error_code);
1516 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1517 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1518 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
1519 break;
1520 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1521 svm->apf_reason = 0;
1522 local_irq_disable();
1523 kvm_async_pf_task_wait(fault_address);
1524 local_irq_enable();
1525 break;
1526 case KVM_PV_REASON_PAGE_READY:
1527 svm->apf_reason = 0;
1528 local_irq_disable();
1529 kvm_async_pf_task_wake(fault_address);
1530 local_irq_enable();
1531 break;
1532 }
1533 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534}
1535
Avi Kivity851ba692009-08-24 11:10:17 +03001536static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001537{
Avi Kivity851ba692009-08-24 11:10:17 +03001538 struct kvm_run *kvm_run = svm->vcpu.run;
1539
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001540 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001541 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001542 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001543 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1544 return 1;
1545 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001546
Jan Kiszka6be7d302009-10-18 13:24:54 +02001547 if (svm->nmi_singlestep) {
1548 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001549 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1550 svm->vmcb->save.rflags &=
1551 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1552 update_db_intercept(&svm->vcpu);
1553 }
1554
1555 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001556 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001557 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1558 kvm_run->debug.arch.pc =
1559 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1560 kvm_run->debug.arch.exception = DB_VECTOR;
1561 return 0;
1562 }
1563
1564 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001565}
1566
Avi Kivity851ba692009-08-24 11:10:17 +03001567static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001568{
Avi Kivity851ba692009-08-24 11:10:17 +03001569 struct kvm_run *kvm_run = svm->vcpu.run;
1570
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001571 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1572 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1573 kvm_run->debug.arch.exception = BP_VECTOR;
1574 return 0;
1575}
1576
Avi Kivity851ba692009-08-24 11:10:17 +03001577static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001578{
1579 int er;
1580
Avi Kivity851ba692009-08-24 11:10:17 +03001581 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001582 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001583 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001584 return 1;
1585}
1586
Avi Kivity6b52d182010-01-21 15:31:47 +02001587static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001588{
Avi Kivity6b52d182010-01-21 15:31:47 +02001589 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001590
Joerg Roedel18c918c2010-11-30 18:03:59 +01001591 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001592
Rusty Russelle756fc62007-07-30 20:07:08 +10001593 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001594 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001595}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001596
Avi Kivity6b52d182010-01-21 15:31:47 +02001597static int nm_interception(struct vcpu_svm *svm)
1598{
1599 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001600 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001601}
1602
Joerg Roedel67ec6602010-05-17 14:43:35 +02001603static bool is_erratum_383(void)
1604{
1605 int err, i;
1606 u64 value;
1607
1608 if (!erratum_383_found)
1609 return false;
1610
1611 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1612 if (err)
1613 return false;
1614
1615 /* Bit 62 may or may not be set for this mce */
1616 value &= ~(1ULL << 62);
1617
1618 if (value != 0xb600000000010015ULL)
1619 return false;
1620
1621 /* Clear MCi_STATUS registers */
1622 for (i = 0; i < 6; ++i)
1623 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1624
1625 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1626 if (!err) {
1627 u32 low, high;
1628
1629 value &= ~(1ULL << 2);
1630 low = lower_32_bits(value);
1631 high = upper_32_bits(value);
1632
1633 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1634 }
1635
1636 /* Flush tlb to evict multi-match entries */
1637 __flush_tlb_all();
1638
1639 return true;
1640}
1641
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001642static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001643{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001644 if (is_erratum_383()) {
1645 /*
1646 * Erratum 383 triggered. Guest state is corrupt so kill the
1647 * guest.
1648 */
1649 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1650
Avi Kivitya8eeb042010-05-10 12:34:53 +03001651 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001652
1653 return;
1654 }
1655
Joerg Roedel53371b52008-04-09 14:15:30 +02001656 /*
1657 * On an #MC intercept the MCE handler is not called automatically in
1658 * the host. So do it by hand here.
1659 */
1660 asm volatile (
1661 "int $0x12\n");
1662 /* not sure if we ever come back to this point */
1663
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001664 return;
1665}
1666
1667static int mc_interception(struct vcpu_svm *svm)
1668{
Joerg Roedel53371b52008-04-09 14:15:30 +02001669 return 1;
1670}
1671
Avi Kivity851ba692009-08-24 11:10:17 +03001672static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001673{
Avi Kivity851ba692009-08-24 11:10:17 +03001674 struct kvm_run *kvm_run = svm->vcpu.run;
1675
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001676 /*
1677 * VMCB is undefined after a SHUTDOWN intercept
1678 * so reinitialize it.
1679 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001680 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001681 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001682
1683 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1684 return 0;
1685}
1686
Avi Kivity851ba692009-08-24 11:10:17 +03001687static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001689 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001690 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001691 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001692 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693
Rusty Russelle756fc62007-07-30 20:07:08 +10001694 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001695 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001696 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001697 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001698 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001699
Avi Kivity039576c2007-03-20 12:46:50 +02001700 port = io_info >> 16;
1701 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001702 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001703 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001704
1705 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706}
1707
Avi Kivity851ba692009-08-24 11:10:17 +03001708static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001709{
1710 return 1;
1711}
1712
Avi Kivity851ba692009-08-24 11:10:17 +03001713static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001714{
1715 ++svm->vcpu.stat.irq_exits;
1716 return 1;
1717}
1718
Avi Kivity851ba692009-08-24 11:10:17 +03001719static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001720{
1721 return 1;
1722}
1723
Avi Kivity851ba692009-08-24 11:10:17 +03001724static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001726 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001727 skip_emulated_instruction(&svm->vcpu);
1728 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729}
1730
Avi Kivity851ba692009-08-24 11:10:17 +03001731static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001732{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001733 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001734 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001735 kvm_emulate_hypercall(&svm->vcpu);
1736 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001737}
1738
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001739static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1740{
1741 struct vcpu_svm *svm = to_svm(vcpu);
1742
1743 return svm->nested.nested_cr3;
1744}
1745
1746static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1747 unsigned long root)
1748{
1749 struct vcpu_svm *svm = to_svm(vcpu);
1750
1751 svm->vmcb->control.nested_cr3 = root;
1752 force_new_asid(vcpu);
1753}
1754
Avi Kivity6389ee92010-11-29 16:12:30 +02001755static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1756 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001757{
1758 struct vcpu_svm *svm = to_svm(vcpu);
1759
1760 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1761 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001762 svm->vmcb->control.exit_info_1 = fault->error_code;
1763 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001764
1765 nested_svm_vmexit(svm);
1766}
1767
Joerg Roedel4b161842010-09-10 17:31:03 +02001768static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1769{
1770 int r;
1771
1772 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1773
1774 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1775 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
1776 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1777 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1778 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1779
1780 return r;
1781}
1782
1783static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1784{
1785 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1786}
1787
Alexander Grafc0725422008-11-25 20:17:03 +01001788static int nested_svm_check_permissions(struct vcpu_svm *svm)
1789{
Avi Kivityf6801df2010-01-21 15:31:50 +02001790 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001791 || !is_paging(&svm->vcpu)) {
1792 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1793 return 1;
1794 }
1795
1796 if (svm->vmcb->save.cpl) {
1797 kvm_inject_gp(&svm->vcpu, 0);
1798 return 1;
1799 }
1800
1801 return 0;
1802}
1803
Alexander Grafcf74a782008-11-25 20:17:08 +01001804static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1805 bool has_error_code, u32 error_code)
1806{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001807 int vmexit;
1808
Joerg Roedel20307532010-11-29 17:51:48 +01001809 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02001810 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001811
Joerg Roedel0295ad72009-08-07 11:49:37 +02001812 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1813 svm->vmcb->control.exit_code_hi = 0;
1814 svm->vmcb->control.exit_info_1 = error_code;
1815 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1816
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001817 vmexit = nested_svm_intercept(svm);
1818 if (vmexit == NESTED_EXIT_DONE)
1819 svm->nested.exit_required = true;
1820
1821 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001822}
1823
Joerg Roedel8fe54652010-02-19 16:23:01 +01001824/* This function returns true if it is save to enable the irq window */
1825static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001826{
Joerg Roedel20307532010-11-29 17:51:48 +01001827 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001828 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001829
Joerg Roedel26666952009-08-07 11:49:46 +02001830 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001831 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001832
Joerg Roedel26666952009-08-07 11:49:46 +02001833 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001834 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001835
Gleb Natapova0a07cd2010-09-20 10:15:32 +02001836 /*
1837 * if vmexit was already requested (by intercepted exception
1838 * for instance) do not overwrite it with "external interrupt"
1839 * vmexit.
1840 */
1841 if (svm->nested.exit_required)
1842 return false;
1843
Joerg Roedel197717d2010-02-24 18:59:19 +01001844 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1845 svm->vmcb->control.exit_info_1 = 0;
1846 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001847
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001848 if (svm->nested.intercept & 1ULL) {
1849 /*
1850 * The #vmexit can't be emulated here directly because this
1851 * code path runs with irqs and preemtion disabled. A
1852 * #vmexit emulation might sleep. Only signal request for
1853 * the #vmexit here.
1854 */
1855 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001856 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001857 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001858 }
1859
Joerg Roedel8fe54652010-02-19 16:23:01 +01001860 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001861}
1862
Joerg Roedel887f5002010-02-24 18:59:12 +01001863/* This function returns true if it is save to enable the nmi window */
1864static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1865{
Joerg Roedel20307532010-11-29 17:51:48 +01001866 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01001867 return true;
1868
1869 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1870 return true;
1871
1872 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1873 svm->nested.exit_required = true;
1874
1875 return false;
1876}
1877
Joerg Roedel7597f122010-02-19 16:23:00 +01001878static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001879{
1880 struct page *page;
1881
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001882 might_sleep();
1883
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001884 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001885 if (is_error_page(page))
1886 goto error;
1887
Joerg Roedel7597f122010-02-19 16:23:00 +01001888 *_page = page;
1889
1890 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001891
1892error:
1893 kvm_release_page_clean(page);
1894 kvm_inject_gp(&svm->vcpu, 0);
1895
1896 return NULL;
1897}
1898
Joerg Roedel7597f122010-02-19 16:23:00 +01001899static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001900{
Joerg Roedel7597f122010-02-19 16:23:00 +01001901 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001902 kvm_release_page_dirty(page);
1903}
1904
Joerg Roedelce2ac082010-03-01 15:34:39 +01001905static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001906{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001907 unsigned port;
1908 u8 val, bit;
1909 u64 gpa;
1910
1911 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1912 return NESTED_EXIT_HOST;
1913
1914 port = svm->vmcb->control.exit_info_1 >> 16;
1915 gpa = svm->nested.vmcb_iopm + (port / 8);
1916 bit = port % 8;
1917 val = 0;
1918
1919 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1920 val &= (1 << bit);
1921
1922 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1923}
1924
Joerg Roedeld2477822010-03-01 15:34:34 +01001925static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001926{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001927 u32 offset, msr, value;
1928 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001929
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001930 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001931 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001932
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001933 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1934 offset = svm_msrpm_offset(msr);
1935 write = svm->vmcb->control.exit_info_1 & 1;
1936 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001937
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001938 if (offset == MSR_INVALID)
1939 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001940
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001941 /* Offset is in 32 bit units but need in 8 bit units */
1942 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001943
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001944 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1945 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001946
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001947 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001948}
1949
Joerg Roedel410e4d52009-08-07 11:49:44 +02001950static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001951{
Alexander Grafcf74a782008-11-25 20:17:08 +01001952 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001953
Joerg Roedel410e4d52009-08-07 11:49:44 +02001954 switch (exit_code) {
1955 case SVM_EXIT_INTR:
1956 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001957 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001958 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001959 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001960 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001961 if (npt_enabled)
1962 return NESTED_EXIT_HOST;
1963 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001964 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02001965 /* When we're shadowing, trap PFs, but not async PF */
1966 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001967 return NESTED_EXIT_HOST;
1968 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001969 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1970 nm_interception(svm);
1971 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001972 default:
1973 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001974 }
1975
Joerg Roedel410e4d52009-08-07 11:49:44 +02001976 return NESTED_EXIT_CONTINUE;
1977}
1978
1979/*
1980 * If this function returns true, this #vmexit was already handled
1981 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001982static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001983{
1984 u32 exit_code = svm->vmcb->control.exit_code;
1985 int vmexit = NESTED_EXIT_HOST;
1986
Alexander Grafcf74a782008-11-25 20:17:08 +01001987 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001988 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001989 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001990 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001991 case SVM_EXIT_IOIO:
1992 vmexit = nested_svm_intercept_ioio(svm);
1993 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001994 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1995 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
1996 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001997 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001998 break;
1999 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01002000 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
2001 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
2002 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002003 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002004 break;
2005 }
2006 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
2007 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002008 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002009 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02002010 /* async page fault always cause vmexit */
2011 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2012 svm->apf_reason != 0)
2013 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002014 break;
2015 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002016 case SVM_EXIT_ERR: {
2017 vmexit = NESTED_EXIT_DONE;
2018 break;
2019 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002020 default: {
2021 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002022 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002023 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002024 }
2025 }
2026
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002027 return vmexit;
2028}
2029
2030static int nested_svm_exit_handled(struct vcpu_svm *svm)
2031{
2032 int vmexit;
2033
2034 vmexit = nested_svm_intercept(svm);
2035
2036 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002037 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002038
2039 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002040}
2041
Joerg Roedel0460a972009-08-07 11:49:31 +02002042static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2043{
2044 struct vmcb_control_area *dst = &dst_vmcb->control;
2045 struct vmcb_control_area *from = &from_vmcb->control;
2046
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002047 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002048 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002049 dst->intercept_exceptions = from->intercept_exceptions;
2050 dst->intercept = from->intercept;
2051 dst->iopm_base_pa = from->iopm_base_pa;
2052 dst->msrpm_base_pa = from->msrpm_base_pa;
2053 dst->tsc_offset = from->tsc_offset;
2054 dst->asid = from->asid;
2055 dst->tlb_ctl = from->tlb_ctl;
2056 dst->int_ctl = from->int_ctl;
2057 dst->int_vector = from->int_vector;
2058 dst->int_state = from->int_state;
2059 dst->exit_code = from->exit_code;
2060 dst->exit_code_hi = from->exit_code_hi;
2061 dst->exit_info_1 = from->exit_info_1;
2062 dst->exit_info_2 = from->exit_info_2;
2063 dst->exit_int_info = from->exit_int_info;
2064 dst->exit_int_info_err = from->exit_int_info_err;
2065 dst->nested_ctl = from->nested_ctl;
2066 dst->event_inj = from->event_inj;
2067 dst->event_inj_err = from->event_inj_err;
2068 dst->nested_cr3 = from->nested_cr3;
2069 dst->lbr_ctl = from->lbr_ctl;
2070}
2071
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002072static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002073{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002074 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002075 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002076 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002077 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002078
Joerg Roedel17897f32009-10-09 16:08:29 +02002079 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2080 vmcb->control.exit_info_1,
2081 vmcb->control.exit_info_2,
2082 vmcb->control.exit_int_info,
2083 vmcb->control.exit_int_info_err);
2084
Joerg Roedel7597f122010-02-19 16:23:00 +01002085 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002086 if (!nested_vmcb)
2087 return 1;
2088
Joerg Roedel20307532010-11-29 17:51:48 +01002089 /* Exit Guest-Mode */
2090 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002091 svm->nested.vmcb = 0;
2092
Alexander Grafcf74a782008-11-25 20:17:08 +01002093 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002094 disable_gif(svm);
2095
2096 nested_vmcb->save.es = vmcb->save.es;
2097 nested_vmcb->save.cs = vmcb->save.cs;
2098 nested_vmcb->save.ss = vmcb->save.ss;
2099 nested_vmcb->save.ds = vmcb->save.ds;
2100 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2101 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002102 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002103 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002104 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02002105 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002106 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02002107 nested_vmcb->save.rflags = vmcb->save.rflags;
2108 nested_vmcb->save.rip = vmcb->save.rip;
2109 nested_vmcb->save.rsp = vmcb->save.rsp;
2110 nested_vmcb->save.rax = vmcb->save.rax;
2111 nested_vmcb->save.dr7 = vmcb->save.dr7;
2112 nested_vmcb->save.dr6 = vmcb->save.dr6;
2113 nested_vmcb->save.cpl = vmcb->save.cpl;
2114
2115 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2116 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2117 nested_vmcb->control.int_state = vmcb->control.int_state;
2118 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2119 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2120 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2121 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2122 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2123 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002124 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002125
2126 /*
2127 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2128 * to make sure that we do not lose injected events. So check event_inj
2129 * here and copy it to exit_int_info if it is valid.
2130 * Exit_int_info and event_inj can't be both valid because the case
2131 * below only happens on a VMRUN instruction intercept which has
2132 * no valid exit_int_info set.
2133 */
2134 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2135 struct vmcb_control_area *nc = &nested_vmcb->control;
2136
2137 nc->exit_int_info = vmcb->control.event_inj;
2138 nc->exit_int_info_err = vmcb->control.event_inj_err;
2139 }
2140
Joerg Roedel33740e42009-08-07 11:49:29 +02002141 nested_vmcb->control.tlb_ctl = 0;
2142 nested_vmcb->control.event_inj = 0;
2143 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002144
2145 /* We always set V_INTR_MASKING and remember the old value in hflags */
2146 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2147 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2148
Alexander Grafcf74a782008-11-25 20:17:08 +01002149 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002150 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002151
Alexander Graf219b65d2009-06-15 15:21:25 +02002152 kvm_clear_exception_queue(&svm->vcpu);
2153 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002154
Joerg Roedel4b161842010-09-10 17:31:03 +02002155 svm->nested.nested_cr3 = 0;
2156
Alexander Grafcf74a782008-11-25 20:17:08 +01002157 /* Restore selected save entries */
2158 svm->vmcb->save.es = hsave->save.es;
2159 svm->vmcb->save.cs = hsave->save.cs;
2160 svm->vmcb->save.ss = hsave->save.ss;
2161 svm->vmcb->save.ds = hsave->save.ds;
2162 svm->vmcb->save.gdtr = hsave->save.gdtr;
2163 svm->vmcb->save.idtr = hsave->save.idtr;
2164 svm->vmcb->save.rflags = hsave->save.rflags;
2165 svm_set_efer(&svm->vcpu, hsave->save.efer);
2166 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2167 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2168 if (npt_enabled) {
2169 svm->vmcb->save.cr3 = hsave->save.cr3;
2170 svm->vcpu.arch.cr3 = hsave->save.cr3;
2171 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002172 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002173 }
2174 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2175 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2176 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2177 svm->vmcb->save.dr7 = 0;
2178 svm->vmcb->save.cpl = 0;
2179 svm->vmcb->control.exit_int_info = 0;
2180
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002181 mark_all_dirty(svm->vmcb);
2182
Joerg Roedel7597f122010-02-19 16:23:00 +01002183 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002184
Joerg Roedel4b161842010-09-10 17:31:03 +02002185 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002186 kvm_mmu_reset_context(&svm->vcpu);
2187 kvm_mmu_load(&svm->vcpu);
2188
2189 return 0;
2190}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002191
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002192static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002193{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002194 /*
2195 * This function merges the msr permission bitmaps of kvm and the
2196 * nested vmcb. It is omptimized in that it only merges the parts where
2197 * the kvm msr permission bitmap may contain zero bits
2198 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002199 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002200
Joerg Roedel323c3d82010-03-01 15:34:37 +01002201 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2202 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002203
Joerg Roedel323c3d82010-03-01 15:34:37 +01002204 for (i = 0; i < MSRPM_OFFSETS; i++) {
2205 u32 value, p;
2206 u64 offset;
2207
2208 if (msrpm_offsets[i] == 0xffffffff)
2209 break;
2210
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002211 p = msrpm_offsets[i];
2212 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002213
2214 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2215 return false;
2216
2217 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2218 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002219
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002220 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002221
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002222 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002223}
2224
Joerg Roedel52c65a302010-08-02 16:46:44 +02002225static bool nested_vmcb_checks(struct vmcb *vmcb)
2226{
2227 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2228 return false;
2229
Joerg Roedeldbe77582010-08-02 16:46:45 +02002230 if (vmcb->control.asid == 0)
2231 return false;
2232
Joerg Roedel4b161842010-09-10 17:31:03 +02002233 if (vmcb->control.nested_ctl && !npt_enabled)
2234 return false;
2235
Joerg Roedel52c65a302010-08-02 16:46:44 +02002236 return true;
2237}
2238
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002239static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002240{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002241 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002242 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002243 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002244 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002245 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002246
Joerg Roedel06fc77722010-02-19 16:23:07 +01002247 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002248
Joerg Roedel7597f122010-02-19 16:23:00 +01002249 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002250 if (!nested_vmcb)
2251 return false;
2252
Joerg Roedel52c65a302010-08-02 16:46:44 +02002253 if (!nested_vmcb_checks(nested_vmcb)) {
2254 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2255 nested_vmcb->control.exit_code_hi = 0;
2256 nested_vmcb->control.exit_info_1 = 0;
2257 nested_vmcb->control.exit_info_2 = 0;
2258
2259 nested_svm_unmap(page);
2260
2261 return false;
2262 }
2263
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002264 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002265 nested_vmcb->save.rip,
2266 nested_vmcb->control.int_ctl,
2267 nested_vmcb->control.event_inj,
2268 nested_vmcb->control.nested_ctl);
2269
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002270 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2271 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002272 nested_vmcb->control.intercept_exceptions,
2273 nested_vmcb->control.intercept);
2274
Alexander Graf3d6368e2008-11-25 20:17:07 +01002275 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002276 kvm_clear_exception_queue(&svm->vcpu);
2277 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002278
Joerg Roedele0231712010-02-24 18:59:10 +01002279 /*
2280 * Save the old vmcb, so we don't need to pick what we save, but can
2281 * restore everything when a VMEXIT occurs
2282 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002283 hsave->save.es = vmcb->save.es;
2284 hsave->save.cs = vmcb->save.cs;
2285 hsave->save.ss = vmcb->save.ss;
2286 hsave->save.ds = vmcb->save.ds;
2287 hsave->save.gdtr = vmcb->save.gdtr;
2288 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002289 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002290 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002291 hsave->save.cr4 = svm->vcpu.arch.cr4;
2292 hsave->save.rflags = vmcb->save.rflags;
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002293 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002294 hsave->save.rsp = vmcb->save.rsp;
2295 hsave->save.rax = vmcb->save.rax;
2296 if (npt_enabled)
2297 hsave->save.cr3 = vmcb->save.cr3;
2298 else
2299 hsave->save.cr3 = svm->vcpu.arch.cr3;
2300
Joerg Roedel0460a972009-08-07 11:49:31 +02002301 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002302
2303 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2304 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2305 else
2306 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2307
Joerg Roedel4b161842010-09-10 17:31:03 +02002308 if (nested_vmcb->control.nested_ctl) {
2309 kvm_mmu_unload(&svm->vcpu);
2310 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2311 nested_svm_init_mmu_context(&svm->vcpu);
2312 }
2313
Alexander Graf3d6368e2008-11-25 20:17:07 +01002314 /* Load the nested guest state */
2315 svm->vmcb->save.es = nested_vmcb->save.es;
2316 svm->vmcb->save.cs = nested_vmcb->save.cs;
2317 svm->vmcb->save.ss = nested_vmcb->save.ss;
2318 svm->vmcb->save.ds = nested_vmcb->save.ds;
2319 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2320 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2321 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2322 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2323 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2324 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2325 if (npt_enabled) {
2326 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2327 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002328 } else
Avi Kivity23902182010-06-10 17:02:16 +03002329 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002330
2331 /* Guest paging mode is active - reset mmu */
2332 kvm_mmu_reset_context(&svm->vcpu);
2333
Joerg Roedeldefbba52009-08-07 11:49:30 +02002334 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002335 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2336 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2337 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002338
Alexander Graf3d6368e2008-11-25 20:17:07 +01002339 /* In case we don't even reach vcpu_run, the fields are not updated */
2340 svm->vmcb->save.rax = nested_vmcb->save.rax;
2341 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2342 svm->vmcb->save.rip = nested_vmcb->save.rip;
2343 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2344 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2345 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2346
Joerg Roedelf7138532010-03-01 15:34:40 +01002347 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002348 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002349
Joerg Roedelaad42c62009-08-07 11:49:34 +02002350 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002351 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002352 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002353 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2354 svm->nested.intercept = nested_vmcb->control.intercept;
2355
Alexander Graf3d6368e2008-11-25 20:17:07 +01002356 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002357 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002358 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2359 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2360 else
2361 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2362
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002363 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2364 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002365 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2366 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002367 }
2368
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002369 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002370 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002371
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002372 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002373 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2374 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2375 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002376 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2377 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2378
Joerg Roedel7597f122010-02-19 16:23:00 +01002379 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002380
Joerg Roedel20307532010-11-29 17:51:48 +01002381 /* Enter Guest-Mode */
2382 enter_guest_mode(&svm->vcpu);
2383
Joerg Roedel384c6362010-11-30 18:03:56 +01002384 /*
2385 * Merge guest and host intercepts - must be called with vcpu in
2386 * guest-mode to take affect here
2387 */
2388 recalc_intercepts(svm);
2389
Joerg Roedel06fc77722010-02-19 16:23:07 +01002390 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002391
Joerg Roedel2af91942009-08-07 11:49:28 +02002392 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002393
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002394 mark_all_dirty(svm->vmcb);
2395
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002396 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002397}
2398
Joerg Roedel9966bf62009-08-07 11:49:40 +02002399static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002400{
2401 to_vmcb->save.fs = from_vmcb->save.fs;
2402 to_vmcb->save.gs = from_vmcb->save.gs;
2403 to_vmcb->save.tr = from_vmcb->save.tr;
2404 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2405 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2406 to_vmcb->save.star = from_vmcb->save.star;
2407 to_vmcb->save.lstar = from_vmcb->save.lstar;
2408 to_vmcb->save.cstar = from_vmcb->save.cstar;
2409 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2410 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2411 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2412 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002413}
2414
Avi Kivity851ba692009-08-24 11:10:17 +03002415static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002416{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002417 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002418 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002419
Alexander Graf55426752008-11-25 20:17:06 +01002420 if (nested_svm_check_permissions(svm))
2421 return 1;
2422
2423 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2424 skip_emulated_instruction(&svm->vcpu);
2425
Joerg Roedel7597f122010-02-19 16:23:00 +01002426 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002427 if (!nested_vmcb)
2428 return 1;
2429
2430 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002431 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002432
2433 return 1;
2434}
2435
Avi Kivity851ba692009-08-24 11:10:17 +03002436static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002437{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002438 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002439 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002440
Alexander Graf55426752008-11-25 20:17:06 +01002441 if (nested_svm_check_permissions(svm))
2442 return 1;
2443
2444 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2445 skip_emulated_instruction(&svm->vcpu);
2446
Joerg Roedel7597f122010-02-19 16:23:00 +01002447 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002448 if (!nested_vmcb)
2449 return 1;
2450
2451 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002452 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002453
2454 return 1;
2455}
2456
Avi Kivity851ba692009-08-24 11:10:17 +03002457static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002458{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002459 if (nested_svm_check_permissions(svm))
2460 return 1;
2461
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002462 /* Save rip after vmrun instruction */
2463 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002464
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002465 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002466 return 1;
2467
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002468 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002469 goto failed;
2470
2471 return 1;
2472
2473failed:
2474
2475 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2476 svm->vmcb->control.exit_code_hi = 0;
2477 svm->vmcb->control.exit_info_1 = 0;
2478 svm->vmcb->control.exit_info_2 = 0;
2479
2480 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002481
2482 return 1;
2483}
2484
Avi Kivity851ba692009-08-24 11:10:17 +03002485static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002486{
2487 if (nested_svm_check_permissions(svm))
2488 return 1;
2489
2490 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2491 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002492 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002493
Joerg Roedel2af91942009-08-07 11:49:28 +02002494 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002495
2496 return 1;
2497}
2498
Avi Kivity851ba692009-08-24 11:10:17 +03002499static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002500{
2501 if (nested_svm_check_permissions(svm))
2502 return 1;
2503
2504 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2505 skip_emulated_instruction(&svm->vcpu);
2506
Joerg Roedel2af91942009-08-07 11:49:28 +02002507 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002508
2509 /* After a CLGI no interrupts should come */
2510 svm_clear_vintr(svm);
2511 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2512
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002513 mark_dirty(svm->vmcb, VMCB_INTR);
2514
Alexander Graf1371d902008-11-25 20:17:04 +01002515 return 1;
2516}
2517
Avi Kivity851ba692009-08-24 11:10:17 +03002518static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002519{
2520 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002521
Joerg Roedelec1ff792009-10-09 16:08:31 +02002522 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2523 vcpu->arch.regs[VCPU_REGS_RAX]);
2524
Alexander Grafff092382009-06-15 15:21:24 +02002525 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2526 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2527
2528 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2529 skip_emulated_instruction(&svm->vcpu);
2530 return 1;
2531}
2532
Joerg Roedel532a46b2009-10-09 16:08:32 +02002533static int skinit_interception(struct vcpu_svm *svm)
2534{
2535 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2536
2537 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2538 return 1;
2539}
2540
Avi Kivity851ba692009-08-24 11:10:17 +03002541static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002543 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544 return 1;
2545}
2546
Avi Kivity851ba692009-08-24 11:10:17 +03002547static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002548{
Izik Eidus37817f22008-03-24 23:14:53 +02002549 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002550 int reason;
2551 int int_type = svm->vmcb->control.exit_int_info &
2552 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002553 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002554 uint32_t type =
2555 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2556 uint32_t idt_v =
2557 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002558 bool has_error_code = false;
2559 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002560
2561 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002562
Izik Eidus37817f22008-03-24 23:14:53 +02002563 if (svm->vmcb->control.exit_info_2 &
2564 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002565 reason = TASK_SWITCH_IRET;
2566 else if (svm->vmcb->control.exit_info_2 &
2567 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2568 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002569 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002570 reason = TASK_SWITCH_GATE;
2571 else
2572 reason = TASK_SWITCH_CALL;
2573
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002574 if (reason == TASK_SWITCH_GATE) {
2575 switch (type) {
2576 case SVM_EXITINTINFO_TYPE_NMI:
2577 svm->vcpu.arch.nmi_injected = false;
2578 break;
2579 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002580 if (svm->vmcb->control.exit_info_2 &
2581 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2582 has_error_code = true;
2583 error_code =
2584 (u32)svm->vmcb->control.exit_info_2;
2585 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002586 kvm_clear_exception_queue(&svm->vcpu);
2587 break;
2588 case SVM_EXITINTINFO_TYPE_INTR:
2589 kvm_clear_interrupt_queue(&svm->vcpu);
2590 break;
2591 default:
2592 break;
2593 }
2594 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002595
Gleb Natapov8317c292009-04-12 13:37:02 +03002596 if (reason != TASK_SWITCH_GATE ||
2597 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2598 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002599 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2600 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002601
Gleb Natapovacb54512010-04-15 21:03:50 +03002602 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2603 has_error_code, error_code) == EMULATE_FAIL) {
2604 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2605 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2606 svm->vcpu.run->internal.ndata = 0;
2607 return 0;
2608 }
2609 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610}
2611
Avi Kivity851ba692009-08-24 11:10:17 +03002612static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002614 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002615 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002616 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002617}
2618
Avi Kivity851ba692009-08-24 11:10:17 +03002619static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002620{
2621 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002622 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002623 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002624 return 1;
2625}
2626
Avi Kivity851ba692009-08-24 11:10:17 +03002627static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002628{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002629 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002630}
2631
Avi Kivity851ba692009-08-24 11:10:17 +03002632static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002634 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635}
2636
Joerg Roedelcda00082010-09-02 17:29:46 +02002637static int cr0_write_interception(struct vcpu_svm *svm)
2638{
2639 struct kvm_vcpu *vcpu = &svm->vcpu;
2640 int r;
2641
2642 r = emulate_instruction(&svm->vcpu, 0, 0, 0);
2643
2644 if (svm->nested.vmexit_rip) {
2645 kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
2646 kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
2647 kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
2648 svm->nested.vmexit_rip = 0;
2649 }
2650
2651 return r == EMULATE_DONE;
2652}
2653
Avi Kivity851ba692009-08-24 11:10:17 +03002654static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002655{
Avi Kivity851ba692009-08-24 11:10:17 +03002656 struct kvm_run *kvm_run = svm->vcpu.run;
2657
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002658 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2659 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002660 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002661 if (irqchip_in_kernel(svm->vcpu.kvm)) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002662 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel1d075432007-12-06 21:02:25 +01002663 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002664 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002665 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2666 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002667 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2668 return 0;
2669}
2670
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2672{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002673 struct vcpu_svm *svm = to_svm(vcpu);
2674
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302676 case MSR_IA32_TSC: {
Joerg Roedel4cc70312010-11-30 18:04:01 +01002677 struct vmcb *vmcb = get_host_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678
Joerg Roedel4cc70312010-11-30 18:04:01 +01002679 *data = vmcb->control.tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 break;
2681 }
Brian Gerst8c065852010-07-17 09:03:26 -04002682 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002683 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002685#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002687 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688 break;
2689 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002690 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002691 break;
2692 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002693 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694 break;
2695 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002696 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002697 break;
2698#endif
2699 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002700 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701 break;
2702 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002703 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 break;
2705 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002706 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002708 /*
2709 * Nobody will change the following 5 values in the VMCB so we can
2710 * safely return them on rdmsr. They will always be 0 until LBRV is
2711 * implemented.
2712 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002713 case MSR_IA32_DEBUGCTLMSR:
2714 *data = svm->vmcb->save.dbgctl;
2715 break;
2716 case MSR_IA32_LASTBRANCHFROMIP:
2717 *data = svm->vmcb->save.br_from;
2718 break;
2719 case MSR_IA32_LASTBRANCHTOIP:
2720 *data = svm->vmcb->save.br_to;
2721 break;
2722 case MSR_IA32_LASTINTFROMIP:
2723 *data = svm->vmcb->save.last_excp_from;
2724 break;
2725 case MSR_IA32_LASTINTTOIP:
2726 *data = svm->vmcb->save.last_excp_to;
2727 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002728 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002729 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002730 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002731 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002732 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002733 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002734 case MSR_IA32_UCODE_REV:
2735 *data = 0x01000065;
2736 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002738 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 }
2740 return 0;
2741}
2742
Avi Kivity851ba692009-08-24 11:10:17 +03002743static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002745 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746 u64 data;
2747
Avi Kivity59200272010-01-25 19:47:02 +02002748 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2749 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002750 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002751 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002752 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002753
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002754 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002755 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002756 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002757 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 }
2759 return 1;
2760}
2761
Joerg Roedel4a810182010-02-24 18:59:15 +01002762static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2763{
2764 struct vcpu_svm *svm = to_svm(vcpu);
2765 int svm_dis, chg_mask;
2766
2767 if (data & ~SVM_VM_CR_VALID_MASK)
2768 return 1;
2769
2770 chg_mask = SVM_VM_CR_VALID_MASK;
2771
2772 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2773 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2774
2775 svm->nested.vm_cr_msr &= ~chg_mask;
2776 svm->nested.vm_cr_msr |= (data & chg_mask);
2777
2778 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2779
2780 /* check for svm_disable while efer.svme is set */
2781 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2782 return 1;
2783
2784 return 0;
2785}
2786
Avi Kivity6aa8b732006-12-10 02:21:36 -08002787static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2788{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002789 struct vcpu_svm *svm = to_svm(vcpu);
2790
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002792 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10002793 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002795 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002796 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002798#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002800 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801 break;
2802 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002803 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 break;
2805 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002806 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807 break;
2808 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002809 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810 break;
2811#endif
2812 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002813 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814 break;
2815 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002816 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002817 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 break;
2819 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002820 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002821 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002823 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02002824 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002825 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002826 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002827 break;
2828 }
2829 if (data & DEBUGCTL_RESERVED_BITS)
2830 return 1;
2831
2832 svm->vmcb->save.dbgctl = data;
2833 if (data & (1ULL<<0))
2834 svm_enable_lbrv(svm);
2835 else
2836 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002837 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002838 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002839 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002840 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002841 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002842 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002843 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002844 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2845 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002846 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002847 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848 }
2849 return 0;
2850}
2851
Avi Kivity851ba692009-08-24 11:10:17 +03002852static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002854 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002855 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002856 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002857
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002858
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002859 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002860 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2861 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002862 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002863 } else {
2864 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002865 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002866 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002867 return 1;
2868}
2869
Avi Kivity851ba692009-08-24 11:10:17 +03002870static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871{
Rusty Russelle756fc62007-07-30 20:07:08 +10002872 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002873 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 else
Avi Kivity851ba692009-08-24 11:10:17 +03002875 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876}
2877
Avi Kivity851ba692009-08-24 11:10:17 +03002878static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002879{
Avi Kivity851ba692009-08-24 11:10:17 +03002880 struct kvm_run *kvm_run = svm->vcpu.run;
2881
Avi Kivity3842d132010-07-27 12:30:24 +03002882 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01002883 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002884 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002885 mark_dirty(svm->vmcb, VMCB_INTR);
Dor Laorc1150d82007-01-05 16:36:24 -08002886 /*
2887 * If the user space waits to inject interrupts, exit as soon as
2888 * possible
2889 */
Gleb Natapov80618232009-04-21 17:44:56 +03002890 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2891 kvm_run->request_interrupt_window &&
2892 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002893 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002894 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2895 return 0;
2896 }
2897
2898 return 1;
2899}
2900
Mark Langsdorf565d0992009-10-06 14:25:02 -05002901static int pause_interception(struct vcpu_svm *svm)
2902{
2903 kvm_vcpu_on_spin(&(svm->vcpu));
2904 return 1;
2905}
2906
Avi Kivity851ba692009-08-24 11:10:17 +03002907static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002908 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2909 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2910 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2911 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002912 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedelcda00082010-09-02 17:29:46 +02002913 [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002914 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2915 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2916 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2917 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2919 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2920 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002921 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2922 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2923 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2924 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002925 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2926 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2927 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2928 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002929 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002931 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002933 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2934 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002935 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002936 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2937 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2938 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2939 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002940 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941 [SVM_EXIT_SMI] = nop_on_interception,
2942 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002943 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002945 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002946 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002947 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002949 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002950 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002951 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952 [SVM_EXIT_MSR] = msr_interception,
2953 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002954 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002955 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002956 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002957 [SVM_EXIT_VMLOAD] = vmload_interception,
2958 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002959 [SVM_EXIT_STGI] = stgi_interception,
2960 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002961 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002962 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002963 [SVM_EXIT_MONITOR] = invalid_op_interception,
2964 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002965 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002966};
2967
Joerg Roedel3f10c842010-05-05 16:04:42 +02002968void dump_vmcb(struct kvm_vcpu *vcpu)
2969{
2970 struct vcpu_svm *svm = to_svm(vcpu);
2971 struct vmcb_control_area *control = &svm->vmcb->control;
2972 struct vmcb_save_area *save = &svm->vmcb->save;
2973
2974 pr_err("VMCB Control Area:\n");
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002975 pr_err("cr_read: %04x\n", control->intercept_cr & 0xffff);
2976 pr_err("cr_write: %04x\n", control->intercept_cr >> 16);
Joerg Roedel3aed0412010-11-30 18:03:58 +01002977 pr_err("dr_read: %04x\n", control->intercept_dr & 0xffff);
2978 pr_err("dr_write: %04x\n", control->intercept_dr >> 16);
Joerg Roedel3f10c842010-05-05 16:04:42 +02002979 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2980 pr_err("intercepts: %016llx\n", control->intercept);
2981 pr_err("pause filter count: %d\n", control->pause_filter_count);
2982 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2983 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2984 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2985 pr_err("asid: %d\n", control->asid);
2986 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2987 pr_err("int_ctl: %08x\n", control->int_ctl);
2988 pr_err("int_vector: %08x\n", control->int_vector);
2989 pr_err("int_state: %08x\n", control->int_state);
2990 pr_err("exit_code: %08x\n", control->exit_code);
2991 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2992 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2993 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2994 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2995 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2996 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2997 pr_err("event_inj: %08x\n", control->event_inj);
2998 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2999 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
3000 pr_err("next_rip: %016llx\n", control->next_rip);
3001 pr_err("VMCB State Save Area:\n");
3002 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
3003 save->es.selector, save->es.attrib,
3004 save->es.limit, save->es.base);
3005 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
3006 save->cs.selector, save->cs.attrib,
3007 save->cs.limit, save->cs.base);
3008 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
3009 save->ss.selector, save->ss.attrib,
3010 save->ss.limit, save->ss.base);
3011 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
3012 save->ds.selector, save->ds.attrib,
3013 save->ds.limit, save->ds.base);
3014 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
3015 save->fs.selector, save->fs.attrib,
3016 save->fs.limit, save->fs.base);
3017 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
3018 save->gs.selector, save->gs.attrib,
3019 save->gs.limit, save->gs.base);
3020 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
3021 save->gdtr.selector, save->gdtr.attrib,
3022 save->gdtr.limit, save->gdtr.base);
3023 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
3024 save->ldtr.selector, save->ldtr.attrib,
3025 save->ldtr.limit, save->ldtr.base);
3026 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
3027 save->idtr.selector, save->idtr.attrib,
3028 save->idtr.limit, save->idtr.base);
3029 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
3030 save->tr.selector, save->tr.attrib,
3031 save->tr.limit, save->tr.base);
3032 pr_err("cpl: %d efer: %016llx\n",
3033 save->cpl, save->efer);
3034 pr_err("cr0: %016llx cr2: %016llx\n",
3035 save->cr0, save->cr2);
3036 pr_err("cr3: %016llx cr4: %016llx\n",
3037 save->cr3, save->cr4);
3038 pr_err("dr6: %016llx dr7: %016llx\n",
3039 save->dr6, save->dr7);
3040 pr_err("rip: %016llx rflags: %016llx\n",
3041 save->rip, save->rflags);
3042 pr_err("rsp: %016llx rax: %016llx\n",
3043 save->rsp, save->rax);
3044 pr_err("star: %016llx lstar: %016llx\n",
3045 save->star, save->lstar);
3046 pr_err("cstar: %016llx sfmask: %016llx\n",
3047 save->cstar, save->sfmask);
3048 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
3049 save->kernel_gs_base, save->sysenter_cs);
3050 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
3051 save->sysenter_esp, save->sysenter_eip);
3052 pr_err("gpat: %016llx dbgctl: %016llx\n",
3053 save->g_pat, save->dbgctl);
3054 pr_err("br_from: %016llx br_to: %016llx\n",
3055 save->br_from, save->br_to);
3056 pr_err("excp_from: %016llx excp_to: %016llx\n",
3057 save->last_excp_from, save->last_excp_to);
3058
3059}
3060
Avi Kivity586f9602010-11-18 13:09:54 +02003061static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3062{
3063 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3064
3065 *info1 = control->exit_info_1;
3066 *info2 = control->exit_info_2;
3067}
3068
Avi Kivity851ba692009-08-24 11:10:17 +03003069static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003070{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003071 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003072 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003073 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003074
Avi Kivityaa179112010-11-17 18:44:19 +02003075 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003076
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003077 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003078 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3079 if (npt_enabled)
3080 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003082 if (unlikely(svm->nested.exit_required)) {
3083 nested_svm_vmexit(svm);
3084 svm->nested.exit_required = false;
3085
3086 return 1;
3087 }
3088
Joerg Roedel20307532010-11-29 17:51:48 +01003089 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003090 int vmexit;
3091
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003092 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3093 svm->vmcb->control.exit_info_1,
3094 svm->vmcb->control.exit_info_2,
3095 svm->vmcb->control.exit_int_info,
3096 svm->vmcb->control.exit_int_info_err);
3097
Joerg Roedel410e4d52009-08-07 11:49:44 +02003098 vmexit = nested_svm_exit_special(svm);
3099
3100 if (vmexit == NESTED_EXIT_CONTINUE)
3101 vmexit = nested_svm_exit_handled(svm);
3102
3103 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003104 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003105 }
3106
Joerg Roedela5c38322009-08-07 11:49:32 +02003107 svm_complete_interrupts(svm);
3108
Avi Kivity04d2cc72007-09-10 18:10:54 +03003109 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3110 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3111 kvm_run->fail_entry.hardware_entry_failure_reason
3112 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003113 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3114 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003115 return 0;
3116 }
3117
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003118 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003119 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003120 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3121 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003122 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3123 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003124 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003125 exit_code);
3126
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003127 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003128 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003130 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131 return 0;
3132 }
3133
Avi Kivity851ba692009-08-24 11:10:17 +03003134 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135}
3136
3137static void reload_tss(struct kvm_vcpu *vcpu)
3138{
3139 int cpu = raw_smp_processor_id();
3140
Tejun Heo0fe1e002009-10-29 22:34:14 +09003141 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3142 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143 load_TR_desc();
3144}
3145
Rusty Russelle756fc62007-07-30 20:07:08 +10003146static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147{
3148 int cpu = raw_smp_processor_id();
3149
Tejun Heo0fe1e002009-10-29 22:34:14 +09003150 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003152 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003153 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003154 if (svm->asid_generation != sd->asid_generation)
3155 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156}
3157
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003158static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3159{
3160 struct vcpu_svm *svm = to_svm(vcpu);
3161
3162 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3163 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003164 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003165 ++vcpu->stat.nmi_injections;
3166}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167
Eddie Dong85f455f2007-07-06 12:20:49 +03003168static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169{
3170 struct vmcb_control_area *control;
3171
Rusty Russelle756fc62007-07-30 20:07:08 +10003172 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003173 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 control->int_ctl &= ~V_INTR_PRIO_MASK;
3175 control->int_ctl |= V_IRQ_MASK |
3176 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003177 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178}
3179
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003180static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003181{
3182 struct vcpu_svm *svm = to_svm(vcpu);
3183
Joerg Roedel2af91942009-08-07 11:49:28 +02003184 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003185
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003186 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3187 ++vcpu->stat.irq_injections;
3188
Alexander Graf219b65d2009-06-15 15:21:25 +02003189 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3190 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003191}
3192
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003193static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3194{
3195 struct vcpu_svm *svm = to_svm(vcpu);
3196
Joerg Roedel20307532010-11-29 17:51:48 +01003197 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003198 return;
3199
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003200 if (irr == -1)
3201 return;
3202
3203 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003204 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003205}
3206
3207static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003208{
3209 struct vcpu_svm *svm = to_svm(vcpu);
3210 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003211 int ret;
3212 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3213 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3214 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3215
3216 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003217}
3218
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003219static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3220{
3221 struct vcpu_svm *svm = to_svm(vcpu);
3222
3223 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3224}
3225
3226static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3227{
3228 struct vcpu_svm *svm = to_svm(vcpu);
3229
3230 if (masked) {
3231 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003232 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003233 } else {
3234 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003235 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003236 }
3237}
3238
Gleb Natapov78646122009-03-23 12:12:11 +02003239static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3240{
3241 struct vcpu_svm *svm = to_svm(vcpu);
3242 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003243 int ret;
3244
3245 if (!gif_set(svm) ||
3246 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3247 return 0;
3248
3249 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3250
Joerg Roedel20307532010-11-29 17:51:48 +01003251 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003252 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3253
3254 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003255}
3256
Gleb Natapov9222be12009-04-23 17:14:37 +03003257static void enable_irq_window(struct kvm_vcpu *vcpu)
3258{
Alexander Graf219b65d2009-06-15 15:21:25 +02003259 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003260
Joerg Roedele0231712010-02-24 18:59:10 +01003261 /*
3262 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3263 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3264 * get that intercept, this function will be called again though and
3265 * we'll get the vintr intercept.
3266 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003267 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003268 svm_set_vintr(svm);
3269 svm_inject_irq(svm, 0x0);
3270 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003271}
3272
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003273static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003274{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003275 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003276
Gleb Natapov44c11432009-05-11 13:35:52 +03003277 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3278 == HF_NMI_MASK)
3279 return; /* IRET will cause a vm exit */
3280
Joerg Roedele0231712010-02-24 18:59:10 +01003281 /*
3282 * Something prevents NMI from been injected. Single step over possible
3283 * problem (IRET or exception injection or interrupt shadow)
3284 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003285 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003286 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3287 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003288}
3289
Izik Eiduscbc94022007-10-25 00:29:55 +02003290static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3291{
3292 return 0;
3293}
3294
Avi Kivityd9e368d2007-06-07 19:18:30 +03003295static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3296{
3297 force_new_asid(vcpu);
3298}
3299
Avi Kivity04d2cc72007-09-10 18:10:54 +03003300static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3301{
3302}
3303
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003304static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3305{
3306 struct vcpu_svm *svm = to_svm(vcpu);
3307
Joerg Roedel20307532010-11-29 17:51:48 +01003308 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003309 return;
3310
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003311 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003312 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003313 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003314 }
3315}
3316
Joerg Roedel649d6862008-04-16 16:51:15 +02003317static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3318{
3319 struct vcpu_svm *svm = to_svm(vcpu);
3320 u64 cr8;
3321
Joerg Roedel20307532010-11-29 17:51:48 +01003322 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003323 return;
3324
Joerg Roedel649d6862008-04-16 16:51:15 +02003325 cr8 = kvm_get_cr8(vcpu);
3326 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3327 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3328}
3329
Gleb Natapov9222be12009-04-23 17:14:37 +03003330static void svm_complete_interrupts(struct vcpu_svm *svm)
3331{
3332 u8 vector;
3333 int type;
3334 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003335 unsigned int3_injected = svm->int3_injected;
3336
3337 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003338
Avi Kivity3842d132010-07-27 12:30:24 +03003339 if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003340 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003341 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3342 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003343
Gleb Natapov9222be12009-04-23 17:14:37 +03003344 svm->vcpu.arch.nmi_injected = false;
3345 kvm_clear_exception_queue(&svm->vcpu);
3346 kvm_clear_interrupt_queue(&svm->vcpu);
3347
3348 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3349 return;
3350
Avi Kivity3842d132010-07-27 12:30:24 +03003351 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3352
Gleb Natapov9222be12009-04-23 17:14:37 +03003353 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3354 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3355
3356 switch (type) {
3357 case SVM_EXITINTINFO_TYPE_NMI:
3358 svm->vcpu.arch.nmi_injected = true;
3359 break;
3360 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003361 /*
3362 * In case of software exceptions, do not reinject the vector,
3363 * but re-execute the instruction instead. Rewind RIP first
3364 * if we emulated INT3 before.
3365 */
3366 if (kvm_exception_is_soft(vector)) {
3367 if (vector == BP_VECTOR && int3_injected &&
3368 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3369 kvm_rip_write(&svm->vcpu,
3370 kvm_rip_read(&svm->vcpu) -
3371 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003372 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003373 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003374 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3375 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003376 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003377
3378 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003379 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003380 break;
3381 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003382 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003383 break;
3384 default:
3385 break;
3386 }
3387}
3388
Avi Kivityb463a6f2010-07-20 15:06:17 +03003389static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3390{
3391 struct vcpu_svm *svm = to_svm(vcpu);
3392 struct vmcb_control_area *control = &svm->vmcb->control;
3393
3394 control->exit_int_info = control->event_inj;
3395 control->exit_int_info_err = control->event_inj_err;
3396 control->event_inj = 0;
3397 svm_complete_interrupts(svm);
3398}
3399
Avi Kivity80e31d42008-07-14 14:44:59 +03003400#ifdef CONFIG_X86_64
3401#define R "r"
3402#else
3403#define R "e"
3404#endif
3405
Avi Kivity851ba692009-08-24 11:10:17 +03003406static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003408 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003409
Joerg Roedel2041a062010-04-22 12:33:08 +02003410 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3411 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3412 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3413
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003414 /*
3415 * A vmexit emulation is required before the vcpu can be executed
3416 * again.
3417 */
3418 if (unlikely(svm->nested.exit_required))
3419 return;
3420
Rusty Russelle756fc62007-07-30 20:07:08 +10003421 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422
Joerg Roedel649d6862008-04-16 16:51:15 +02003423 sync_lapic_to_cr8(vcpu);
3424
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003425 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426
Avi Kivity04d2cc72007-09-10 18:10:54 +03003427 clgi();
3428
3429 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003430
Avi Kivity6aa8b732006-12-10 02:21:36 -08003431 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003432 "push %%"R"bp; \n\t"
3433 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3434 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3435 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3436 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3437 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3438 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003439#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003440 "mov %c[r8](%[svm]), %%r8 \n\t"
3441 "mov %c[r9](%[svm]), %%r9 \n\t"
3442 "mov %c[r10](%[svm]), %%r10 \n\t"
3443 "mov %c[r11](%[svm]), %%r11 \n\t"
3444 "mov %c[r12](%[svm]), %%r12 \n\t"
3445 "mov %c[r13](%[svm]), %%r13 \n\t"
3446 "mov %c[r14](%[svm]), %%r14 \n\t"
3447 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448#endif
3449
Avi Kivity6aa8b732006-12-10 02:21:36 -08003450 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003451 "push %%"R"ax \n\t"
3452 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003453 __ex(SVM_VMLOAD) "\n\t"
3454 __ex(SVM_VMRUN) "\n\t"
3455 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003456 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003457
3458 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003459 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3460 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3461 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3462 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3463 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3464 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003465#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003466 "mov %%r8, %c[r8](%[svm]) \n\t"
3467 "mov %%r9, %c[r9](%[svm]) \n\t"
3468 "mov %%r10, %c[r10](%[svm]) \n\t"
3469 "mov %%r11, %c[r11](%[svm]) \n\t"
3470 "mov %%r12, %c[r12](%[svm]) \n\t"
3471 "mov %%r13, %c[r13](%[svm]) \n\t"
3472 "mov %%r14, %c[r14](%[svm]) \n\t"
3473 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003474#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003475 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003477 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003478 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003479 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3480 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3481 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3482 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3483 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3484 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003485#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003486 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3487 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3488 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3489 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3490 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3491 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3492 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3493 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003494#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003495 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003496 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003497#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003498 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3499#endif
3500 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501
Avi Kivity82ca2d12010-10-21 12:20:34 +02003502#ifdef CONFIG_X86_64
3503 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3504#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003505 loadsegment(fs, svm->host.fs);
Avi Kivity9581d442010-10-19 16:46:55 +02003506#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003507
3508 reload_tss(vcpu);
3509
Avi Kivity56ba47d2007-11-07 17:14:18 +02003510 local_irq_disable();
3511
3512 stgi();
3513
Avi Kivity13c34e02010-10-21 12:20:31 +02003514 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3515 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3516 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3517 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3518
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003519 sync_cr8_to_lapic(vcpu);
3520
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003521 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003522
Gleb Natapov631bc482010-10-14 11:22:52 +02003523 /* if exit due to PF check for async PF */
3524 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3525 svm->apf_reason = kvm_read_and_reset_pf_reason();
3526
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003527 if (npt_enabled) {
3528 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3529 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3530 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003531
3532 /*
3533 * We need to handle MC intercepts here before the vcpu has a chance to
3534 * change the physical cpu
3535 */
3536 if (unlikely(svm->vmcb->control.exit_code ==
3537 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3538 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003539
3540 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003541}
3542
Avi Kivity80e31d42008-07-14 14:44:59 +03003543#undef R
3544
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3546{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003547 struct vcpu_svm *svm = to_svm(vcpu);
3548
3549 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003550 force_new_asid(vcpu);
3551}
3552
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003553static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3554{
3555 struct vcpu_svm *svm = to_svm(vcpu);
3556
3557 svm->vmcb->control.nested_cr3 = root;
3558
3559 /* Also sync guest cr3 here in case we live migrate */
3560 svm->vmcb->save.cr3 = vcpu->arch.cr3;
3561
3562 force_new_asid(vcpu);
3563}
3564
Avi Kivity6aa8b732006-12-10 02:21:36 -08003565static int is_disabled(void)
3566{
Joerg Roedel6031a612007-06-22 12:29:50 +03003567 u64 vm_cr;
3568
3569 rdmsrl(MSR_VM_CR, vm_cr);
3570 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3571 return 1;
3572
Avi Kivity6aa8b732006-12-10 02:21:36 -08003573 return 0;
3574}
3575
Ingo Molnar102d8322007-02-19 14:37:47 +02003576static void
3577svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3578{
3579 /*
3580 * Patch in the VMMCALL instruction:
3581 */
3582 hypercall[0] = 0x0f;
3583 hypercall[1] = 0x01;
3584 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003585}
3586
Yang, Sheng002c7f72007-07-31 14:23:01 +03003587static void svm_check_processor_compat(void *rtn)
3588{
3589 *(int *)rtn = 0;
3590}
3591
Avi Kivity774ead32007-12-26 13:57:04 +02003592static bool svm_cpu_has_accelerated_tpr(void)
3593{
3594 return false;
3595}
3596
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003597static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003598{
3599 return 0;
3600}
3601
Sheng Yang0e851882009-12-18 16:48:46 +08003602static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3603{
3604}
3605
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003606static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3607{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003608 switch (func) {
Joerg Roedel24d1b152010-12-07 17:15:05 +01003609 case 0x00000001:
3610 /* Mask out xsave bit as long as it is not supported by SVM */
3611 entry->ecx &= ~(bit(X86_FEATURE_XSAVE));
3612 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02003613 case 0x80000001:
3614 if (nested)
3615 entry->ecx |= (1 << 2); /* Set SVM bit */
3616 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003617 case 0x8000000A:
3618 entry->eax = 1; /* SVM revision 1 */
3619 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3620 ASID emulation to nested SVM */
3621 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003622 entry->edx = 0; /* Per default do not support any
3623 additional features */
3624
3625 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003626 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02003627 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003628
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02003629 /* Support NPT for the guest if enabled */
3630 if (npt_enabled)
3631 entry->edx |= SVM_FEATURE_NPT;
3632
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003633 break;
3634 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003635}
3636
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003637static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003638 { SVM_EXIT_READ_CR0, "read_cr0" },
3639 { SVM_EXIT_READ_CR3, "read_cr3" },
3640 { SVM_EXIT_READ_CR4, "read_cr4" },
3641 { SVM_EXIT_READ_CR8, "read_cr8" },
3642 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3643 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3644 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3645 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3646 { SVM_EXIT_READ_DR0, "read_dr0" },
3647 { SVM_EXIT_READ_DR1, "read_dr1" },
3648 { SVM_EXIT_READ_DR2, "read_dr2" },
3649 { SVM_EXIT_READ_DR3, "read_dr3" },
3650 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3651 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3652 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3653 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3654 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3655 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003656 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3657 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3658 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3659 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3660 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3661 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3662 { SVM_EXIT_INTR, "interrupt" },
3663 { SVM_EXIT_NMI, "nmi" },
3664 { SVM_EXIT_SMI, "smi" },
3665 { SVM_EXIT_INIT, "init" },
3666 { SVM_EXIT_VINTR, "vintr" },
3667 { SVM_EXIT_CPUID, "cpuid" },
3668 { SVM_EXIT_INVD, "invd" },
3669 { SVM_EXIT_HLT, "hlt" },
3670 { SVM_EXIT_INVLPG, "invlpg" },
3671 { SVM_EXIT_INVLPGA, "invlpga" },
3672 { SVM_EXIT_IOIO, "io" },
3673 { SVM_EXIT_MSR, "msr" },
3674 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3675 { SVM_EXIT_SHUTDOWN, "shutdown" },
3676 { SVM_EXIT_VMRUN, "vmrun" },
3677 { SVM_EXIT_VMMCALL, "hypercall" },
3678 { SVM_EXIT_VMLOAD, "vmload" },
3679 { SVM_EXIT_VMSAVE, "vmsave" },
3680 { SVM_EXIT_STGI, "stgi" },
3681 { SVM_EXIT_CLGI, "clgi" },
3682 { SVM_EXIT_SKINIT, "skinit" },
3683 { SVM_EXIT_WBINVD, "wbinvd" },
3684 { SVM_EXIT_MONITOR, "monitor" },
3685 { SVM_EXIT_MWAIT, "mwait" },
3686 { SVM_EXIT_NPF, "npf" },
3687 { -1, NULL }
3688};
3689
Sheng Yang17cc3932010-01-05 19:02:27 +08003690static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003691{
Sheng Yang17cc3932010-01-05 19:02:27 +08003692 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003693}
3694
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003695static bool svm_rdtscp_supported(void)
3696{
3697 return false;
3698}
3699
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003700static bool svm_has_wbinvd_exit(void)
3701{
3702 return true;
3703}
3704
Avi Kivity02daab22009-12-30 12:40:26 +02003705static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3706{
3707 struct vcpu_svm *svm = to_svm(vcpu);
3708
Joerg Roedel18c918c2010-11-30 18:03:59 +01003709 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01003710 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003711}
3712
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003713static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003714 .cpu_has_kvm_support = has_svm,
3715 .disabled_by_bios = is_disabled,
3716 .hardware_setup = svm_hardware_setup,
3717 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003718 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719 .hardware_enable = svm_hardware_enable,
3720 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003721 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003722
3723 .vcpu_create = svm_create_vcpu,
3724 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003725 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003726
Avi Kivity04d2cc72007-09-10 18:10:54 +03003727 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003728 .vcpu_load = svm_vcpu_load,
3729 .vcpu_put = svm_vcpu_put,
3730
3731 .set_guest_debug = svm_guest_debug,
3732 .get_msr = svm_get_msr,
3733 .set_msr = svm_set_msr,
3734 .get_segment_base = svm_get_segment_base,
3735 .get_segment = svm_get_segment,
3736 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003737 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003738 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003739 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003740 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003741 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003742 .set_cr3 = svm_set_cr3,
3743 .set_cr4 = svm_set_cr4,
3744 .set_efer = svm_set_efer,
3745 .get_idt = svm_get_idt,
3746 .set_idt = svm_set_idt,
3747 .get_gdt = svm_get_gdt,
3748 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003749 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003750 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003751 .get_rflags = svm_get_rflags,
3752 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003753 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003754 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003755
Avi Kivity6aa8b732006-12-10 02:21:36 -08003756 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003757
Avi Kivity6aa8b732006-12-10 02:21:36 -08003758 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003759 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003760 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003761 .set_interrupt_shadow = svm_set_interrupt_shadow,
3762 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003763 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003764 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003765 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003766 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03003767 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02003768 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003769 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003770 .get_nmi_mask = svm_get_nmi_mask,
3771 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003772 .enable_nmi_window = enable_nmi_window,
3773 .enable_irq_window = enable_irq_window,
3774 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003775
3776 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003777 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003778 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003779
Avi Kivity586f9602010-11-18 13:09:54 +02003780 .get_exit_info = svm_get_exit_info,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003781 .exit_reasons_str = svm_exit_reasons_str,
Avi Kivity586f9602010-11-18 13:09:54 +02003782
Sheng Yang17cc3932010-01-05 19:02:27 +08003783 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003784
3785 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003786
3787 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003788
3789 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003790
3791 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10003792
3793 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10003794 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003795
3796 .set_tdp_cr3 = set_tdp_cr3,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003797};
3798
3799static int __init svm_init(void)
3800{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003801 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003802 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003803}
3804
3805static void __exit svm_exit(void)
3806{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003807 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003808}
3809
3810module_init(svm_init)
3811module_exit(svm_exit)