blob: 779b09194f03efd22ba1274815c24114c075b200 [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)
Andre Przywaraddce97a2010-12-21 11:12:03 +010054#define SVM_FEATURE_TSC_RATE (1 << 4)
55#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
56#define SVM_FEATURE_FLUSH_ASID (1 << 6)
57#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020058#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030059
Joerg Roedel410e4d52009-08-07 11:49:44 +020060#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
61#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
62#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
63
Joerg Roedel24e09cb2008-02-13 18:58:47 +010064#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
65
Joerg Roedel67ec6602010-05-17 14:43:35 +020066static bool erratum_383_found __read_mostly;
67
Avi Kivity6c8166a2009-05-31 18:15:37 +030068static const u32 host_save_user_msrs[] = {
69#ifdef CONFIG_X86_64
70 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
71 MSR_FS_BASE,
72#endif
73 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
74};
75
76#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
77
78struct kvm_vcpu;
79
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020080struct nested_state {
81 struct vmcb *hsave;
82 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010083 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020084 u64 vmcb;
85
86 /* These are the merged vectors */
87 u32 *msrpm;
88
89 /* gpa pointers to the real vectors */
90 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +010091 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020092
Joerg Roedelcd3ff652009-10-09 16:08:26 +020093 /* A VMEXIT is required but not yet emulated */
94 bool exit_required;
95
Joerg Roedelaad42c62009-08-07 11:49:34 +020096 /* cache for intercepts of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +010097 u32 intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +010098 u32 intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +020099 u32 intercept_exceptions;
100 u64 intercept;
101
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200102 /* Nested Paging related state */
103 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200104};
105
Joerg Roedel323c3d82010-03-01 15:34:37 +0100106#define MSRPM_OFFSETS 16
107static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
108
Avi Kivity6c8166a2009-05-31 18:15:37 +0300109struct vcpu_svm {
110 struct kvm_vcpu vcpu;
111 struct vmcb *vmcb;
112 unsigned long vmcb_pa;
113 struct svm_cpu_data *svm_data;
114 uint64_t asid_generation;
115 uint64_t sysenter_esp;
116 uint64_t sysenter_eip;
117
118 u64 next_rip;
119
120 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200121 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200122 u16 fs;
123 u16 gs;
124 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200125 u64 gs_base;
126 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300127
128 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300129
Avi Kivitybd3d1ec2011-02-03 15:29:52 +0200130 ulong nmi_iret_rip;
131
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200132 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200133
134 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100135
136 unsigned int3_injected;
137 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200138 u32 apf_reason;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300139};
140
Joerg Roedel455716f2010-03-01 15:34:35 +0100141#define MSR_INVALID 0xffffffffU
142
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100143static struct svm_direct_access_msrs {
144 u32 index; /* Index of the MSR */
145 bool always; /* True if intercept is always on */
146} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400147 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100148 { .index = MSR_IA32_SYSENTER_CS, .always = true },
149#ifdef CONFIG_X86_64
150 { .index = MSR_GS_BASE, .always = true },
151 { .index = MSR_FS_BASE, .always = true },
152 { .index = MSR_KERNEL_GS_BASE, .always = true },
153 { .index = MSR_LSTAR, .always = true },
154 { .index = MSR_CSTAR, .always = true },
155 { .index = MSR_SYSCALL_MASK, .always = true },
156#endif
157 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
158 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
159 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
160 { .index = MSR_IA32_LASTINTTOIP, .always = false },
161 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162};
163
164/* enable NPT for AMD64 and X86 with PAE */
165#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
166static bool npt_enabled = true;
167#else
Joerg Roedele0231712010-02-24 18:59:10 +0100168static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169#endif
170static int npt = 1;
171
172module_param(npt, int, S_IRUGO);
173
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200174static int nested = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800175module_param(nested, int, S_IRUGO);
176
177static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200178static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179
Joerg Roedel410e4d52009-08-07 11:49:44 +0200180static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100181static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
184 bool has_error_code, u32 error_code);
185
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100186enum {
Joerg Roedel116a0a22010-12-03 11:45:49 +0100187 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
188 pause filter count */
Joerg Roedelf56838e2010-12-03 11:45:50 +0100189 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
Joerg Roedeld48086d2010-12-03 11:45:51 +0100190 VMCB_ASID, /* ASID */
Joerg Roedeldecdbf62010-12-03 11:45:52 +0100191 VMCB_INTR, /* int_ctl, int_vector */
Joerg Roedelb2747162010-12-03 11:45:53 +0100192 VMCB_NPT, /* npt_en, nCR3, gPAT */
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100193 VMCB_CR, /* CR0, CR3, CR4, EFER */
Joerg Roedel72214b92010-12-03 11:45:55 +0100194 VMCB_DR, /* DR6, DR7 */
Joerg Roedel17a703c2010-12-03 11:45:56 +0100195 VMCB_DT, /* GDT, IDT */
Joerg Roedel060d0c92010-12-03 11:45:57 +0100196 VMCB_SEG, /* CS, DS, SS, ES, CPL */
Joerg Roedel0574dec2010-12-03 11:45:58 +0100197 VMCB_CR2, /* CR2 only */
Joerg Roedelb53ba3f2010-12-03 11:45:59 +0100198 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100199 VMCB_DIRTY_MAX,
200};
201
Joerg Roedel0574dec2010-12-03 11:45:58 +0100202/* TPR and CR2 are always written before VMRUN */
203#define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Roedel, Joerg8d28fec2010-12-03 13:15:21 +0100204
205static inline void mark_all_dirty(struct vmcb *vmcb)
206{
207 vmcb->control.clean = 0;
208}
209
210static inline void mark_all_clean(struct vmcb *vmcb)
211{
212 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
213 & ~VMCB_ALWAYS_DIRTY_MASK;
214}
215
216static inline void mark_dirty(struct vmcb *vmcb, int bit)
217{
218 vmcb->control.clean &= ~(1 << bit);
219}
220
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
222{
223 return container_of(vcpu, struct vcpu_svm, vcpu);
224}
225
Joerg Roedel384c6362010-11-30 18:03:56 +0100226static void recalc_intercepts(struct vcpu_svm *svm)
227{
228 struct vmcb_control_area *c, *h;
229 struct nested_state *g;
230
Joerg Roedel116a0a22010-12-03 11:45:49 +0100231 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
232
Joerg Roedel384c6362010-11-30 18:03:56 +0100233 if (!is_guest_mode(&svm->vcpu))
234 return;
235
236 c = &svm->vmcb->control;
237 h = &svm->nested.hsave->control;
238 g = &svm->nested;
239
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100240 c->intercept_cr = h->intercept_cr | g->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +0100241 c->intercept_dr = h->intercept_dr | g->intercept_dr;
Joerg Roedel384c6362010-11-30 18:03:56 +0100242 c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
243 c->intercept = h->intercept | g->intercept;
244}
245
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100246static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
247{
248 if (is_guest_mode(&svm->vcpu))
249 return svm->nested.hsave;
250 else
251 return svm->vmcb;
252}
253
254static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
255{
256 struct vmcb *vmcb = get_host_vmcb(svm);
257
258 vmcb->control.intercept_cr |= (1U << bit);
259
260 recalc_intercepts(svm);
261}
262
263static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
264{
265 struct vmcb *vmcb = get_host_vmcb(svm);
266
267 vmcb->control.intercept_cr &= ~(1U << bit);
268
269 recalc_intercepts(svm);
270}
271
272static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
273{
274 struct vmcb *vmcb = get_host_vmcb(svm);
275
276 return vmcb->control.intercept_cr & (1U << bit);
277}
278
Joerg Roedel3aed0412010-11-30 18:03:58 +0100279static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
280{
281 struct vmcb *vmcb = get_host_vmcb(svm);
282
283 vmcb->control.intercept_dr |= (1U << bit);
284
285 recalc_intercepts(svm);
286}
287
288static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
289{
290 struct vmcb *vmcb = get_host_vmcb(svm);
291
292 vmcb->control.intercept_dr &= ~(1U << bit);
293
294 recalc_intercepts(svm);
295}
296
Joerg Roedel18c918c2010-11-30 18:03:59 +0100297static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
298{
299 struct vmcb *vmcb = get_host_vmcb(svm);
300
301 vmcb->control.intercept_exceptions |= (1U << bit);
302
303 recalc_intercepts(svm);
304}
305
306static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
307{
308 struct vmcb *vmcb = get_host_vmcb(svm);
309
310 vmcb->control.intercept_exceptions &= ~(1U << bit);
311
312 recalc_intercepts(svm);
313}
314
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100315static inline void set_intercept(struct vcpu_svm *svm, int bit)
316{
317 struct vmcb *vmcb = get_host_vmcb(svm);
318
319 vmcb->control.intercept |= (1ULL << bit);
320
321 recalc_intercepts(svm);
322}
323
324static inline void clr_intercept(struct vcpu_svm *svm, int bit)
325{
326 struct vmcb *vmcb = get_host_vmcb(svm);
327
328 vmcb->control.intercept &= ~(1ULL << bit);
329
330 recalc_intercepts(svm);
331}
332
Joerg Roedel2af91942009-08-07 11:49:28 +0200333static inline void enable_gif(struct vcpu_svm *svm)
334{
335 svm->vcpu.arch.hflags |= HF_GIF_MASK;
336}
337
338static inline void disable_gif(struct vcpu_svm *svm)
339{
340 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
341}
342
343static inline bool gif_set(struct vcpu_svm *svm)
344{
345 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
346}
347
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348static unsigned long iopm_base;
349
350struct kvm_ldttss_desc {
351 u16 limit0;
352 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100353 unsigned base1:8, type:5, dpl:2, p:1;
354 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355 u32 base3;
356 u32 zero1;
357} __attribute__((packed));
358
359struct svm_cpu_data {
360 int cpu;
361
Avi Kivity5008fdf2007-04-02 13:05:50 +0300362 u64 asid_generation;
363 u32 max_asid;
364 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800365 struct kvm_ldttss_desc *tss_desc;
366
367 struct page *save_area;
368};
369
370static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
371
372struct svm_init_data {
373 int cpu;
374 int r;
375};
376
377static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
378
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200379#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380#define MSRS_RANGE_SIZE 2048
381#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
382
Joerg Roedel455716f2010-03-01 15:34:35 +0100383static u32 svm_msrpm_offset(u32 msr)
384{
385 u32 offset;
386 int i;
387
388 for (i = 0; i < NUM_MSR_MAPS; i++) {
389 if (msr < msrpm_ranges[i] ||
390 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
391 continue;
392
393 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
394 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
395
396 /* Now we have the u8 offset - but need the u32 offset */
397 return offset / 4;
398 }
399
400 /* MSR not in any range */
401 return MSR_INVALID;
402}
403
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404#define MAX_INST_SIZE 15
405
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406static inline void clgi(void)
407{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300408 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409}
410
411static inline void stgi(void)
412{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300413 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414}
415
416static inline void invlpga(unsigned long addr, u32 asid)
417{
Joerg Roedele0231712010-02-24 18:59:10 +0100418 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800419}
420
Joerg Roedel4b161842010-09-10 17:31:03 +0200421static int get_npt_level(void)
422{
423#ifdef CONFIG_X86_64
424 return PT64_ROOT_LEVEL;
425#else
426 return PT32E_ROOT_LEVEL;
427#endif
428}
429
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
431{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000432 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100433 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600434 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435
Alexander Graf9962d032008-11-25 20:17:02 +0100436 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100437 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438}
439
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440static int is_external_interrupt(u32 info)
441{
442 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
443 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
444}
445
Glauber Costa2809f5d2009-05-12 16:21:05 -0400446static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
447{
448 struct vcpu_svm *svm = to_svm(vcpu);
449 u32 ret = 0;
450
451 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100452 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400453 return ret & mask;
454}
455
456static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
457{
458 struct vcpu_svm *svm = to_svm(vcpu);
459
460 if (mask == 0)
461 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
462 else
463 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
464
465}
466
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
468{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400469 struct vcpu_svm *svm = to_svm(vcpu);
470
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200471 if (svm->vmcb->control.next_rip != 0)
472 svm->next_rip = svm->vmcb->control.next_rip;
473
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400474 if (!svm->next_rip) {
Andre Przywara51d8b662010-12-21 11:12:02 +0100475 if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300476 EMULATE_DONE)
477 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478 return;
479 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300480 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
481 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
482 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300484 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400485 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486}
487
Jan Kiszka116a4752010-02-23 17:47:54 +0100488static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200489 bool has_error_code, u32 error_code,
490 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100491{
492 struct vcpu_svm *svm = to_svm(vcpu);
493
Joerg Roedele0231712010-02-24 18:59:10 +0100494 /*
495 * If we are within a nested VM we'd better #VMEXIT and let the guest
496 * handle the exception
497 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200498 if (!reinject &&
499 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100500 return;
501
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200502 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100503 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
504
505 /*
506 * For guest debugging where we have to reinject #BP if some
507 * INT3 is guest-owned:
508 * Emulate nRIP by moving RIP forward. Will fail if injection
509 * raises a fault that is not intercepted. Still better than
510 * failing in all cases.
511 */
512 skip_emulated_instruction(&svm->vcpu);
513 rip = kvm_rip_read(&svm->vcpu);
514 svm->int3_rip = rip + svm->vmcb->save.cs.base;
515 svm->int3_injected = rip - old_rip;
516 }
517
Jan Kiszka116a4752010-02-23 17:47:54 +0100518 svm->vmcb->control.event_inj = nr
519 | SVM_EVTINJ_VALID
520 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
521 | SVM_EVTINJ_TYPE_EXEPT;
522 svm->vmcb->control.event_inj_err = error_code;
523}
524
Joerg Roedel67ec6602010-05-17 14:43:35 +0200525static void svm_init_erratum_383(void)
526{
527 u32 low, high;
528 int err;
529 u64 val;
530
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200531 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200532 return;
533
534 /* Use _safe variants to not break nested virtualization */
535 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
536 if (err)
537 return;
538
539 val |= (1ULL << 47);
540
541 low = lower_32_bits(val);
542 high = upper_32_bits(val);
543
544 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
545
546 erratum_383_found = true;
547}
548
Avi Kivity6aa8b732006-12-10 02:21:36 -0800549static int has_svm(void)
550{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200551 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800552
Eduardo Habkost63d11422008-11-17 19:03:20 -0200553 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800554 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555 return 0;
556 }
557
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558 return 1;
559}
560
561static void svm_hardware_disable(void *garbage)
562{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200563 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564}
565
Alexander Graf10474ae2009-09-15 11:37:46 +0200566static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567{
568
Tejun Heo0fe1e002009-10-29 22:34:14 +0900569 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200571 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572 struct desc_struct *gdt;
573 int me = raw_smp_processor_id();
574
Alexander Graf10474ae2009-09-15 11:37:46 +0200575 rdmsrl(MSR_EFER, efer);
576 if (efer & EFER_SVME)
577 return -EBUSY;
578
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000580 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
581 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200582 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900584 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585
Tejun Heo0fe1e002009-10-29 22:34:14 +0900586 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000587 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200589 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590 }
591
Tejun Heo0fe1e002009-10-29 22:34:14 +0900592 sd->asid_generation = 1;
593 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
594 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200596 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200597 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900598 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599
Alexander Graf9962d032008-11-25 20:17:02 +0100600 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800601
Linus Torvaldsd0316552009-12-14 09:58:24 -0800602 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200603
Joerg Roedel67ec6602010-05-17 14:43:35 +0200604 svm_init_erratum_383();
605
Alexander Graf10474ae2009-09-15 11:37:46 +0200606 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607}
608
Joerg Roedel0da1db752008-07-02 16:02:11 +0200609static void svm_cpu_uninit(int cpu)
610{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900611 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200612
Tejun Heo0fe1e002009-10-29 22:34:14 +0900613 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200614 return;
615
616 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900617 __free_page(sd->save_area);
618 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200619}
620
Avi Kivity6aa8b732006-12-10 02:21:36 -0800621static int svm_cpu_init(int cpu)
622{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900623 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624 int r;
625
Tejun Heo0fe1e002009-10-29 22:34:14 +0900626 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
627 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800628 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900629 sd->cpu = cpu;
630 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900632 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633 goto err_1;
634
Tejun Heo0fe1e002009-10-29 22:34:14 +0900635 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800636
637 return 0;
638
639err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900640 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641 return r;
642
643}
644
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100645static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646{
647 int i;
648
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100649 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
650 if (direct_access_msrs[i].index == index)
651 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800652
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100653 return false;
654}
655
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656static void set_msr_interception(u32 *msrpm, unsigned msr,
657 int read, int write)
658{
Joerg Roedel455716f2010-03-01 15:34:35 +0100659 u8 bit_read, bit_write;
660 unsigned long tmp;
661 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100663 /*
664 * If this warning triggers extend the direct_access_msrs list at the
665 * beginning of the file
666 */
667 WARN_ON(!valid_msr_intercept(msr));
668
Joerg Roedel455716f2010-03-01 15:34:35 +0100669 offset = svm_msrpm_offset(msr);
670 bit_read = 2 * (msr & 0x0f);
671 bit_write = 2 * (msr & 0x0f) + 1;
672 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800673
Joerg Roedel455716f2010-03-01 15:34:35 +0100674 BUG_ON(offset == MSR_INVALID);
675
676 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
677 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
678
679 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680}
681
Joerg Roedelf65c2292008-02-13 18:58:46 +0100682static void svm_vcpu_init_msrpm(u32 *msrpm)
683{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100684 int i;
685
Joerg Roedelf65c2292008-02-13 18:58:46 +0100686 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
687
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100688 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
689 if (!direct_access_msrs[i].always)
690 continue;
691
692 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
693 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100694}
695
Joerg Roedel323c3d82010-03-01 15:34:37 +0100696static void add_msr_offset(u32 offset)
697{
698 int i;
699
700 for (i = 0; i < MSRPM_OFFSETS; ++i) {
701
702 /* Offset already in list? */
703 if (msrpm_offsets[i] == offset)
704 return;
705
706 /* Slot used by another offset? */
707 if (msrpm_offsets[i] != MSR_INVALID)
708 continue;
709
710 /* Add offset to list */
711 msrpm_offsets[i] = offset;
712
713 return;
714 }
715
716 /*
717 * If this BUG triggers the msrpm_offsets table has an overflow. Just
718 * increase MSRPM_OFFSETS in this case.
719 */
720 BUG();
721}
722
723static void init_msrpm_offsets(void)
724{
725 int i;
726
727 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
728
729 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
730 u32 offset;
731
732 offset = svm_msrpm_offset(direct_access_msrs[i].index);
733 BUG_ON(offset == MSR_INVALID);
734
735 add_msr_offset(offset);
736 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737}
738
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100739static void svm_enable_lbrv(struct vcpu_svm *svm)
740{
741 u32 *msrpm = svm->msrpm;
742
743 svm->vmcb->control.lbr_ctl = 1;
744 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
745 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
746 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
747 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
748}
749
750static void svm_disable_lbrv(struct vcpu_svm *svm)
751{
752 u32 *msrpm = svm->msrpm;
753
754 svm->vmcb->control.lbr_ctl = 0;
755 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
756 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
757 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
758 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
759}
760
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761static __init int svm_hardware_setup(void)
762{
763 int cpu;
764 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100765 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766 int r;
767
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
769
770 if (!iopm_pages)
771 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300772
773 iopm_va = page_address(iopm_pages);
774 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
776
Joerg Roedel323c3d82010-03-01 15:34:37 +0100777 init_msrpm_offsets();
778
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100779 if (boot_cpu_has(X86_FEATURE_NX))
780 kvm_enable_efer_bits(EFER_NX);
781
Alexander Graf1b2fd702009-02-02 16:23:51 +0100782 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
783 kvm_enable_efer_bits(EFER_FFXSR);
784
Alexander Graf236de052008-11-25 20:17:10 +0100785 if (nested) {
786 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200787 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100788 }
789
Zachary Amsden3230bb42009-09-29 11:38:37 -1000790 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791 r = svm_cpu_init(cpu);
792 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100793 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100795
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200796 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100797 npt_enabled = false;
798
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100799 if (npt_enabled && !npt) {
800 printk(KERN_INFO "kvm: Nested Paging disabled\n");
801 npt_enabled = false;
802 }
803
Joerg Roedel18552672008-02-07 13:47:41 +0100804 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100805 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100806 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200807 } else
808 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100809
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810 return 0;
811
Joerg Roedelf65c2292008-02-13 18:58:46 +0100812err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
814 iopm_base = 0;
815 return r;
816}
817
818static __exit void svm_hardware_unsetup(void)
819{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200820 int cpu;
821
Zachary Amsden3230bb42009-09-29 11:38:37 -1000822 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200823 svm_cpu_uninit(cpu);
824
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100826 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827}
828
829static void init_seg(struct vmcb_seg *seg)
830{
831 seg->selector = 0;
832 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100833 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834 seg->limit = 0xffff;
835 seg->base = 0;
836}
837
838static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
839{
840 seg->selector = 0;
841 seg->attrib = SVM_SELECTOR_P_MASK | type;
842 seg->limit = 0xffff;
843 seg->base = 0;
844}
845
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000846static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
847{
848 struct vcpu_svm *svm = to_svm(vcpu);
849 u64 g_tsc_offset = 0;
850
Joerg Roedel20307532010-11-29 17:51:48 +0100851 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000852 g_tsc_offset = svm->vmcb->control.tsc_offset -
853 svm->nested.hsave->control.tsc_offset;
854 svm->nested.hsave->control.tsc_offset = offset;
855 }
856
857 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +0100858
859 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000860}
861
Zachary Amsdene48672f2010-08-19 22:07:23 -1000862static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
863{
864 struct vcpu_svm *svm = to_svm(vcpu);
865
866 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +0100867 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -1000868 svm->nested.hsave->control.tsc_offset += adjustment;
Joerg Roedel116a0a22010-12-03 11:45:49 +0100869 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdene48672f2010-08-19 22:07:23 -1000870}
871
Joerg Roedele6101a92008-02-13 18:58:45 +0100872static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873{
Joerg Roedele6101a92008-02-13 18:58:45 +0100874 struct vmcb_control_area *control = &svm->vmcb->control;
875 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876
Avi Kivitybff78272010-01-07 13:16:08 +0200877 svm->vcpu.fpu_active = 1;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100878 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +0200879
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100880 set_cr_intercept(svm, INTERCEPT_CR0_READ);
881 set_cr_intercept(svm, INTERCEPT_CR3_READ);
882 set_cr_intercept(svm, INTERCEPT_CR4_READ);
883 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
884 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
885 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
886 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800887
Joerg Roedel3aed0412010-11-30 18:03:58 +0100888 set_dr_intercept(svm, INTERCEPT_DR0_READ);
889 set_dr_intercept(svm, INTERCEPT_DR1_READ);
890 set_dr_intercept(svm, INTERCEPT_DR2_READ);
891 set_dr_intercept(svm, INTERCEPT_DR3_READ);
892 set_dr_intercept(svm, INTERCEPT_DR4_READ);
893 set_dr_intercept(svm, INTERCEPT_DR5_READ);
894 set_dr_intercept(svm, INTERCEPT_DR6_READ);
895 set_dr_intercept(svm, INTERCEPT_DR7_READ);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800896
Joerg Roedel3aed0412010-11-30 18:03:58 +0100897 set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
898 set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
899 set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
900 set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
901 set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
902 set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
903 set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
904 set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905
Joerg Roedel18c918c2010-11-30 18:03:59 +0100906 set_exception_intercept(svm, PF_VECTOR);
907 set_exception_intercept(svm, UD_VECTOR);
908 set_exception_intercept(svm, MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100910 set_intercept(svm, INTERCEPT_INTR);
911 set_intercept(svm, INTERCEPT_NMI);
912 set_intercept(svm, INTERCEPT_SMI);
913 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
914 set_intercept(svm, INTERCEPT_CPUID);
915 set_intercept(svm, INTERCEPT_INVD);
916 set_intercept(svm, INTERCEPT_HLT);
917 set_intercept(svm, INTERCEPT_INVLPG);
918 set_intercept(svm, INTERCEPT_INVLPGA);
919 set_intercept(svm, INTERCEPT_IOIO_PROT);
920 set_intercept(svm, INTERCEPT_MSR_PROT);
921 set_intercept(svm, INTERCEPT_TASK_SWITCH);
922 set_intercept(svm, INTERCEPT_SHUTDOWN);
923 set_intercept(svm, INTERCEPT_VMRUN);
924 set_intercept(svm, INTERCEPT_VMMCALL);
925 set_intercept(svm, INTERCEPT_VMLOAD);
926 set_intercept(svm, INTERCEPT_VMSAVE);
927 set_intercept(svm, INTERCEPT_STGI);
928 set_intercept(svm, INTERCEPT_CLGI);
929 set_intercept(svm, INTERCEPT_SKINIT);
930 set_intercept(svm, INTERCEPT_WBINVD);
931 set_intercept(svm, INTERCEPT_MONITOR);
932 set_intercept(svm, INTERCEPT_MWAIT);
Joerg Roedel81dd35d2010-12-07 17:15:06 +0100933 set_intercept(svm, INTERCEPT_XSETBV);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
935 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100936 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937 control->int_ctl = V_INTR_MASKING_MASK;
938
939 init_seg(&save->es);
940 init_seg(&save->ss);
941 init_seg(&save->ds);
942 init_seg(&save->fs);
943 init_seg(&save->gs);
944
945 save->cs.selector = 0xf000;
946 /* Executable/Readable Code Segment */
947 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
948 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
949 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800950 /*
951 * cs.base should really be 0xffff0000, but vmx can't handle that, so
952 * be consistent with it.
953 *
954 * Replace when we have real mode working for vmx.
955 */
956 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957
958 save->gdtr.limit = 0xffff;
959 save->idtr.limit = 0xffff;
960
961 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
962 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
963
Marcelo Tosattieaa48512010-08-31 19:13:14 -0300964 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -0400965 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 save->dr7 = 0x400;
Avi Kivityf6e78472010-08-02 15:30:20 +0300967 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300969 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970
Joerg Roedele0231712010-02-24 18:59:10 +0100971 /*
972 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200973 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800974 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -0300975 svm->vcpu.arch.cr0 = 0;
976 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200977
Rusty Russell66aee912007-07-17 23:34:16 +1000978 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100980
981 if (npt_enabled) {
982 /* Setup VMCB for Nested Paging */
983 control->nested_ctl = 1;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +0100984 clr_intercept(svm, INTERCEPT_TASK_SWITCH);
985 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +0100986 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +0100987 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
988 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100989 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100990 save->cr3 = 0;
991 save->cr4 = 0;
992 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +0100993 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +0100994
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200995 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200996 svm->vcpu.arch.hflags = 0;
997
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200998 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -0500999 control->pause_filter_count = 3000;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001000 set_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001001 }
1002
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001003 mark_all_dirty(svm->vmcb);
1004
Joerg Roedel2af91942009-08-07 11:49:28 +02001005 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006}
1007
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001008static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001009{
1010 struct vcpu_svm *svm = to_svm(vcpu);
1011
Joerg Roedele6101a92008-02-13 18:58:45 +01001012 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001013
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001014 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001015 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001016 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
1017 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +02001018 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001019 vcpu->arch.regs_avail = ~0;
1020 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001021
1022 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001023}
1024
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001025static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001027 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001029 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001030 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001031 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001032 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033
Rusty Russellc16f8622007-07-30 21:12:19 +10001034 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001035 if (!svm) {
1036 err = -ENOMEM;
1037 goto out;
1038 }
1039
1040 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
1041 if (err)
1042 goto free_svm;
1043
Joerg Roedelf65c2292008-02-13 18:58:46 +01001044 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001045 page = alloc_page(GFP_KERNEL);
1046 if (!page)
1047 goto uninit;
1048
Joerg Roedelf65c2292008-02-13 18:58:46 +01001049 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1050 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001051 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001052
1053 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
1054 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001055 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001056
Alexander Grafb286d5d2008-11-25 20:17:05 +01001057 hsave_page = alloc_page(GFP_KERNEL);
1058 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001059 goto free_page3;
1060
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001061 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001062
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001063 svm->msrpm = page_address(msrpm_pages);
1064 svm_vcpu_init_msrpm(svm->msrpm);
1065
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001066 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001067 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001068
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001069 svm->vmcb = page_address(page);
1070 clear_page(svm->vmcb);
1071 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
1072 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +01001073 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -10001074 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001075
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001076 err = fx_init(&svm->vcpu);
1077 if (err)
1078 goto free_page4;
1079
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001080 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001081 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001082 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001084 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -08001085
Jan Kiszka10ab25c2010-05-25 16:01:50 +02001086free_page4:
1087 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001088free_page3:
1089 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1090free_page2:
1091 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1092free_page1:
1093 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001094uninit:
1095 kvm_vcpu_uninit(&svm->vcpu);
1096free_svm:
Rusty Russella4770342007-08-01 14:46:11 +10001097 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001098out:
1099 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100}
1101
1102static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1103{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001104 struct vcpu_svm *svm = to_svm(vcpu);
1105
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001106 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001107 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001108 __free_page(virt_to_page(svm->nested.hsave));
1109 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001110 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10001111 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112}
1113
Avi Kivity15ad7142007-07-11 18:17:21 +03001114static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001116 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001117 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001118
Avi Kivity0cc50642007-03-25 12:07:27 +02001119 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001120 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001121 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001122 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001123
Avi Kivity82ca2d12010-10-21 12:20:34 +02001124#ifdef CONFIG_X86_64
1125 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1126#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001127 savesegment(fs, svm->host.fs);
1128 savesegment(gs, svm->host.gs);
1129 svm->host.ldt = kvm_read_ldt();
1130
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001131 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001132 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133}
1134
1135static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1136{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001137 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001138 int i;
1139
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001140 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001141 kvm_load_ldt(svm->host.ldt);
1142#ifdef CONFIG_X86_64
1143 loadsegment(fs, svm->host.fs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001144 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001145 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001146#else
Avi Kivity831ca602011-03-08 16:09:51 +02001147#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001148 loadsegment(gs, svm->host.gs);
1149#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001150#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001151 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001152 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153}
1154
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1156{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001157 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158}
1159
1160static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1161{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001162 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163}
1164
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001165static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1166{
1167 switch (reg) {
1168 case VCPU_EXREG_PDPTR:
1169 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001170 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001171 break;
1172 default:
1173 BUG();
1174 }
1175}
1176
Alexander Graff0b85052008-11-25 20:17:01 +01001177static void svm_set_vintr(struct vcpu_svm *svm)
1178{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001179 set_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001180}
1181
1182static void svm_clear_vintr(struct vcpu_svm *svm)
1183{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001184 clr_intercept(svm, INTERCEPT_VINTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001185}
1186
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1188{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001189 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190
1191 switch (seg) {
1192 case VCPU_SREG_CS: return &save->cs;
1193 case VCPU_SREG_DS: return &save->ds;
1194 case VCPU_SREG_ES: return &save->es;
1195 case VCPU_SREG_FS: return &save->fs;
1196 case VCPU_SREG_GS: return &save->gs;
1197 case VCPU_SREG_SS: return &save->ss;
1198 case VCPU_SREG_TR: return &save->tr;
1199 case VCPU_SREG_LDTR: return &save->ldtr;
1200 }
1201 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001202 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203}
1204
1205static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1206{
1207 struct vmcb_seg *s = svm_seg(vcpu, seg);
1208
1209 return s->base;
1210}
1211
1212static void svm_get_segment(struct kvm_vcpu *vcpu,
1213 struct kvm_segment *var, int seg)
1214{
1215 struct vmcb_seg *s = svm_seg(vcpu, seg);
1216
1217 var->base = s->base;
1218 var->limit = s->limit;
1219 var->selector = s->selector;
1220 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1221 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1222 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1223 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1224 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1225 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1226 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1227 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001228
Joerg Roedele0231712010-02-24 18:59:10 +01001229 /*
1230 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001231 * for cross vendor migration purposes by "not present"
1232 */
1233 var->unusable = !var->present || (var->type == 0);
1234
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001235 switch (seg) {
1236 case VCPU_SREG_CS:
1237 /*
1238 * SVM always stores 0 for the 'G' bit in the CS selector in
1239 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1240 * Intel's VMENTRY has a check on the 'G' bit.
1241 */
Amit Shah25022ac2008-10-27 09:04:17 +00001242 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001243 break;
1244 case VCPU_SREG_TR:
1245 /*
1246 * Work around a bug where the busy flag in the tr selector
1247 * isn't exposed
1248 */
Amit Shahc0d09822008-10-27 09:04:18 +00001249 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001250 break;
1251 case VCPU_SREG_DS:
1252 case VCPU_SREG_ES:
1253 case VCPU_SREG_FS:
1254 case VCPU_SREG_GS:
1255 /*
1256 * The accessed bit must always be set in the segment
1257 * descriptor cache, although it can be cleared in the
1258 * descriptor, the cached bit always remains at 1. Since
1259 * Intel has a check on this, set it here to support
1260 * cross-vendor migration.
1261 */
1262 if (!var->unusable)
1263 var->type |= 0x1;
1264 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001265 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001266 /*
1267 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001268 * descriptor is left as 1, although the whole segment has
1269 * been made unusable. Clear it here to pass an Intel VMX
1270 * entry check when cross vendor migrating.
1271 */
1272 if (var->unusable)
1273 var->db = 0;
1274 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001275 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276}
1277
Izik Eidus2e4d2652008-03-24 19:38:34 +02001278static int svm_get_cpl(struct kvm_vcpu *vcpu)
1279{
1280 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1281
1282 return save->cpl;
1283}
1284
Gleb Natapov89a27f42010-02-16 10:51:48 +02001285static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001287 struct vcpu_svm *svm = to_svm(vcpu);
1288
Gleb Natapov89a27f42010-02-16 10:51:48 +02001289 dt->size = svm->vmcb->save.idtr.limit;
1290 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001291}
1292
Gleb Natapov89a27f42010-02-16 10:51:48 +02001293static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001295 struct vcpu_svm *svm = to_svm(vcpu);
1296
Gleb Natapov89a27f42010-02-16 10:51:48 +02001297 svm->vmcb->save.idtr.limit = dt->size;
1298 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001299 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300}
1301
Gleb Natapov89a27f42010-02-16 10:51:48 +02001302static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001303{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001304 struct vcpu_svm *svm = to_svm(vcpu);
1305
Gleb Natapov89a27f42010-02-16 10:51:48 +02001306 dt->size = svm->vmcb->save.gdtr.limit;
1307 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308}
1309
Gleb Natapov89a27f42010-02-16 10:51:48 +02001310static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001312 struct vcpu_svm *svm = to_svm(vcpu);
1313
Gleb Natapov89a27f42010-02-16 10:51:48 +02001314 svm->vmcb->save.gdtr.limit = dt->size;
1315 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001316 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317}
1318
Avi Kivitye8467fd2009-12-29 18:43:06 +02001319static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1320{
1321}
1322
Avi Kivityaff48ba2010-12-05 18:56:11 +02001323static void svm_decache_cr3(struct kvm_vcpu *vcpu)
1324{
1325}
1326
Anthony Liguori25c4c272007-04-27 09:29:21 +03001327static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001328{
1329}
1330
Avi Kivityd2251572010-01-06 10:55:27 +02001331static void update_cr0_intercept(struct vcpu_svm *svm)
1332{
1333 ulong gcr0 = svm->vcpu.arch.cr0;
1334 u64 *hcr0 = &svm->vmcb->save.cr0;
1335
1336 if (!svm->vcpu.fpu_active)
1337 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1338 else
1339 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1340 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1341
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001342 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001343
1344 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001345 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1346 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001347 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001348 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1349 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001350 }
1351}
1352
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1354{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001355 struct vcpu_svm *svm = to_svm(vcpu);
1356
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001357#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001358 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001359 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001360 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001361 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 }
1363
Mike Dayd77c26f2007-10-08 09:02:08 -04001364 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001365 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001366 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367 }
1368 }
1369#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001370 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001371
1372 if (!npt_enabled)
1373 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001374
1375 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001376 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001377 /*
1378 * re-enable caching here because the QEMU bios
1379 * does not do it - this results in some delay at
1380 * reboot
1381 */
1382 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001383 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001384 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001385 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386}
1387
1388static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1389{
Joerg Roedel6394b642008-04-09 14:15:29 +02001390 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001391 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1392
1393 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001394 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001395
Joerg Roedelec077262008-04-09 14:15:28 +02001396 vcpu->arch.cr4 = cr4;
1397 if (!npt_enabled)
1398 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001399 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001400 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001401 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402}
1403
1404static void svm_set_segment(struct kvm_vcpu *vcpu,
1405 struct kvm_segment *var, int seg)
1406{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001407 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408 struct vmcb_seg *s = svm_seg(vcpu, seg);
1409
1410 s->base = var->base;
1411 s->limit = var->limit;
1412 s->selector = var->selector;
1413 if (var->unusable)
1414 s->attrib = 0;
1415 else {
1416 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1417 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1418 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1419 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1420 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1421 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1422 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1423 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1424 }
1425 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001426 svm->vmcb->save.cpl
1427 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1429
Joerg Roedel060d0c92010-12-03 11:45:57 +01001430 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431}
1432
Gleb Natapov44c11432009-05-11 13:35:52 +03001433static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001434{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001435 struct vcpu_svm *svm = to_svm(vcpu);
1436
Joerg Roedel18c918c2010-11-30 18:03:59 +01001437 clr_exception_intercept(svm, DB_VECTOR);
1438 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001439
Jan Kiszka6be7d302009-10-18 13:24:54 +02001440 if (svm->nmi_singlestep)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001441 set_exception_intercept(svm, DB_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001442
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001443 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1444 if (vcpu->guest_debug &
1445 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
Joerg Roedel18c918c2010-11-30 18:03:59 +01001446 set_exception_intercept(svm, DB_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001447 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001448 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001449 } else
1450 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001451}
1452
Jan Kiszka355be0b2009-10-03 00:31:21 +02001453static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001454{
Gleb Natapov44c11432009-05-11 13:35:52 +03001455 struct vcpu_svm *svm = to_svm(vcpu);
1456
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001457 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1458 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1459 else
1460 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1461
Joerg Roedel72214b92010-12-03 11:45:55 +01001462 mark_dirty(svm->vmcb, VMCB_DR);
1463
Jan Kiszka355be0b2009-10-03 00:31:21 +02001464 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465}
1466
Tejun Heo0fe1e002009-10-29 22:34:14 +09001467static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001469 if (sd->next_asid > sd->max_asid) {
1470 ++sd->asid_generation;
1471 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001472 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473 }
1474
Tejun Heo0fe1e002009-10-29 22:34:14 +09001475 svm->asid_generation = sd->asid_generation;
1476 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001477
1478 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479}
1480
Gleb Natapov020df072010-04-13 10:05:23 +03001481static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001483 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001484
Gleb Natapov020df072010-04-13 10:05:23 +03001485 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01001486 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487}
1488
Avi Kivity851ba692009-08-24 11:10:17 +03001489static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001490{
Gleb Natapov631bc482010-10-14 11:22:52 +02001491 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001493 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494
Gleb Natapov631bc482010-10-14 11:22:52 +02001495 switch (svm->apf_reason) {
1496 default:
1497 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001498
Gleb Natapov631bc482010-10-14 11:22:52 +02001499 trace_kvm_page_fault(fault_address, error_code);
1500 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1501 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Andre Przywaradc25e892010-12-21 11:12:07 +01001502 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
1503 svm->vmcb->control.insn_bytes,
1504 svm->vmcb->control.insn_len);
Gleb Natapov631bc482010-10-14 11:22:52 +02001505 break;
1506 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1507 svm->apf_reason = 0;
1508 local_irq_disable();
1509 kvm_async_pf_task_wait(fault_address);
1510 local_irq_enable();
1511 break;
1512 case KVM_PV_REASON_PAGE_READY:
1513 svm->apf_reason = 0;
1514 local_irq_disable();
1515 kvm_async_pf_task_wake(fault_address);
1516 local_irq_enable();
1517 break;
1518 }
1519 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520}
1521
Avi Kivity851ba692009-08-24 11:10:17 +03001522static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001523{
Avi Kivity851ba692009-08-24 11:10:17 +03001524 struct kvm_run *kvm_run = svm->vcpu.run;
1525
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001526 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001527 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001528 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001529 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1530 return 1;
1531 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001532
Jan Kiszka6be7d302009-10-18 13:24:54 +02001533 if (svm->nmi_singlestep) {
1534 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001535 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1536 svm->vmcb->save.rflags &=
1537 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1538 update_db_intercept(&svm->vcpu);
1539 }
1540
1541 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001542 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001543 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1544 kvm_run->debug.arch.pc =
1545 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1546 kvm_run->debug.arch.exception = DB_VECTOR;
1547 return 0;
1548 }
1549
1550 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001551}
1552
Avi Kivity851ba692009-08-24 11:10:17 +03001553static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001554{
Avi Kivity851ba692009-08-24 11:10:17 +03001555 struct kvm_run *kvm_run = svm->vcpu.run;
1556
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001557 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1558 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1559 kvm_run->debug.arch.exception = BP_VECTOR;
1560 return 0;
1561}
1562
Avi Kivity851ba692009-08-24 11:10:17 +03001563static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001564{
1565 int er;
1566
Andre Przywara51d8b662010-12-21 11:12:02 +01001567 er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001568 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001569 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001570 return 1;
1571}
1572
Avi Kivity6b52d182010-01-21 15:31:47 +02001573static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001574{
Avi Kivity6b52d182010-01-21 15:31:47 +02001575 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001576
Joerg Roedel18c918c2010-11-30 18:03:59 +01001577 clr_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001578
Rusty Russelle756fc62007-07-30 20:07:08 +10001579 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001580 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001581}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001582
Avi Kivity6b52d182010-01-21 15:31:47 +02001583static int nm_interception(struct vcpu_svm *svm)
1584{
1585 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001586 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001587}
1588
Joerg Roedel67ec6602010-05-17 14:43:35 +02001589static bool is_erratum_383(void)
1590{
1591 int err, i;
1592 u64 value;
1593
1594 if (!erratum_383_found)
1595 return false;
1596
1597 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1598 if (err)
1599 return false;
1600
1601 /* Bit 62 may or may not be set for this mce */
1602 value &= ~(1ULL << 62);
1603
1604 if (value != 0xb600000000010015ULL)
1605 return false;
1606
1607 /* Clear MCi_STATUS registers */
1608 for (i = 0; i < 6; ++i)
1609 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1610
1611 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1612 if (!err) {
1613 u32 low, high;
1614
1615 value &= ~(1ULL << 2);
1616 low = lower_32_bits(value);
1617 high = upper_32_bits(value);
1618
1619 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1620 }
1621
1622 /* Flush tlb to evict multi-match entries */
1623 __flush_tlb_all();
1624
1625 return true;
1626}
1627
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001628static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001629{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001630 if (is_erratum_383()) {
1631 /*
1632 * Erratum 383 triggered. Guest state is corrupt so kill the
1633 * guest.
1634 */
1635 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1636
Avi Kivitya8eeb042010-05-10 12:34:53 +03001637 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001638
1639 return;
1640 }
1641
Joerg Roedel53371b52008-04-09 14:15:30 +02001642 /*
1643 * On an #MC intercept the MCE handler is not called automatically in
1644 * the host. So do it by hand here.
1645 */
1646 asm volatile (
1647 "int $0x12\n");
1648 /* not sure if we ever come back to this point */
1649
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001650 return;
1651}
1652
1653static int mc_interception(struct vcpu_svm *svm)
1654{
Joerg Roedel53371b52008-04-09 14:15:30 +02001655 return 1;
1656}
1657
Avi Kivity851ba692009-08-24 11:10:17 +03001658static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001659{
Avi Kivity851ba692009-08-24 11:10:17 +03001660 struct kvm_run *kvm_run = svm->vcpu.run;
1661
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001662 /*
1663 * VMCB is undefined after a SHUTDOWN intercept
1664 * so reinitialize it.
1665 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001666 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001667 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001668
1669 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1670 return 0;
1671}
1672
Avi Kivity851ba692009-08-24 11:10:17 +03001673static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001674{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001675 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001676 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001677 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001678 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679
Rusty Russelle756fc62007-07-30 20:07:08 +10001680 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001681 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001682 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001683 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01001684 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001685
Avi Kivity039576c2007-03-20 12:46:50 +02001686 port = io_info >> 16;
1687 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001688 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001689 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001690
1691 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692}
1693
Avi Kivity851ba692009-08-24 11:10:17 +03001694static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001695{
1696 return 1;
1697}
1698
Avi Kivity851ba692009-08-24 11:10:17 +03001699static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001700{
1701 ++svm->vcpu.stat.irq_exits;
1702 return 1;
1703}
1704
Avi Kivity851ba692009-08-24 11:10:17 +03001705static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706{
1707 return 1;
1708}
1709
Avi Kivity851ba692009-08-24 11:10:17 +03001710static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001712 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001713 skip_emulated_instruction(&svm->vcpu);
1714 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001715}
1716
Avi Kivity851ba692009-08-24 11:10:17 +03001717static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001718{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001719 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001720 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001721 kvm_emulate_hypercall(&svm->vcpu);
1722 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001723}
1724
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001725static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1726{
1727 struct vcpu_svm *svm = to_svm(vcpu);
1728
1729 return svm->nested.nested_cr3;
1730}
1731
1732static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1733 unsigned long root)
1734{
1735 struct vcpu_svm *svm = to_svm(vcpu);
1736
1737 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01001738 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001739 svm_flush_tlb(vcpu);
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001740}
1741
Avi Kivity6389ee92010-11-29 16:12:30 +02001742static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1743 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001744{
1745 struct vcpu_svm *svm = to_svm(vcpu);
1746
1747 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1748 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001749 svm->vmcb->control.exit_info_1 = fault->error_code;
1750 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001751
1752 nested_svm_vmexit(svm);
1753}
1754
Joerg Roedel4b161842010-09-10 17:31:03 +02001755static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1756{
1757 int r;
1758
1759 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1760
1761 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1762 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
1763 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1764 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1765 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1766
1767 return r;
1768}
1769
1770static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1771{
1772 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1773}
1774
Alexander Grafc0725422008-11-25 20:17:03 +01001775static int nested_svm_check_permissions(struct vcpu_svm *svm)
1776{
Avi Kivityf6801df2010-01-21 15:31:50 +02001777 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001778 || !is_paging(&svm->vcpu)) {
1779 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1780 return 1;
1781 }
1782
1783 if (svm->vmcb->save.cpl) {
1784 kvm_inject_gp(&svm->vcpu, 0);
1785 return 1;
1786 }
1787
1788 return 0;
1789}
1790
Alexander Grafcf74a782008-11-25 20:17:08 +01001791static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1792 bool has_error_code, u32 error_code)
1793{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001794 int vmexit;
1795
Joerg Roedel20307532010-11-29 17:51:48 +01001796 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02001797 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001798
Joerg Roedel0295ad72009-08-07 11:49:37 +02001799 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1800 svm->vmcb->control.exit_code_hi = 0;
1801 svm->vmcb->control.exit_info_1 = error_code;
1802 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1803
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001804 vmexit = nested_svm_intercept(svm);
1805 if (vmexit == NESTED_EXIT_DONE)
1806 svm->nested.exit_required = true;
1807
1808 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001809}
1810
Joerg Roedel8fe54652010-02-19 16:23:01 +01001811/* This function returns true if it is save to enable the irq window */
1812static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001813{
Joerg Roedel20307532010-11-29 17:51:48 +01001814 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001815 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001816
Joerg Roedel26666952009-08-07 11:49:46 +02001817 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001818 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001819
Joerg Roedel26666952009-08-07 11:49:46 +02001820 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001821 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001822
Gleb Natapova0a07cd2010-09-20 10:15:32 +02001823 /*
1824 * if vmexit was already requested (by intercepted exception
1825 * for instance) do not overwrite it with "external interrupt"
1826 * vmexit.
1827 */
1828 if (svm->nested.exit_required)
1829 return false;
1830
Joerg Roedel197717d2010-02-24 18:59:19 +01001831 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1832 svm->vmcb->control.exit_info_1 = 0;
1833 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001834
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001835 if (svm->nested.intercept & 1ULL) {
1836 /*
1837 * The #vmexit can't be emulated here directly because this
1838 * code path runs with irqs and preemtion disabled. A
1839 * #vmexit emulation might sleep. Only signal request for
1840 * the #vmexit here.
1841 */
1842 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001843 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001844 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001845 }
1846
Joerg Roedel8fe54652010-02-19 16:23:01 +01001847 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001848}
1849
Joerg Roedel887f5002010-02-24 18:59:12 +01001850/* This function returns true if it is save to enable the nmi window */
1851static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1852{
Joerg Roedel20307532010-11-29 17:51:48 +01001853 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01001854 return true;
1855
1856 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1857 return true;
1858
1859 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1860 svm->nested.exit_required = true;
1861
1862 return false;
1863}
1864
Joerg Roedel7597f122010-02-19 16:23:00 +01001865static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001866{
1867 struct page *page;
1868
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001869 might_sleep();
1870
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001871 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001872 if (is_error_page(page))
1873 goto error;
1874
Joerg Roedel7597f122010-02-19 16:23:00 +01001875 *_page = page;
1876
1877 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001878
1879error:
1880 kvm_release_page_clean(page);
1881 kvm_inject_gp(&svm->vcpu, 0);
1882
1883 return NULL;
1884}
1885
Joerg Roedel7597f122010-02-19 16:23:00 +01001886static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001887{
Joerg Roedel7597f122010-02-19 16:23:00 +01001888 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001889 kvm_release_page_dirty(page);
1890}
1891
Joerg Roedelce2ac082010-03-01 15:34:39 +01001892static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001893{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001894 unsigned port;
1895 u8 val, bit;
1896 u64 gpa;
1897
1898 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1899 return NESTED_EXIT_HOST;
1900
1901 port = svm->vmcb->control.exit_info_1 >> 16;
1902 gpa = svm->nested.vmcb_iopm + (port / 8);
1903 bit = port % 8;
1904 val = 0;
1905
1906 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1907 val &= (1 << bit);
1908
1909 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1910}
1911
Joerg Roedeld2477822010-03-01 15:34:34 +01001912static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001913{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001914 u32 offset, msr, value;
1915 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001916
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001917 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001918 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001919
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001920 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1921 offset = svm_msrpm_offset(msr);
1922 write = svm->vmcb->control.exit_info_1 & 1;
1923 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001924
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001925 if (offset == MSR_INVALID)
1926 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001927
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001928 /* Offset is in 32 bit units but need in 8 bit units */
1929 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001930
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001931 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1932 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001933
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001934 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001935}
1936
Joerg Roedel410e4d52009-08-07 11:49:44 +02001937static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001938{
Alexander Grafcf74a782008-11-25 20:17:08 +01001939 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001940
Joerg Roedel410e4d52009-08-07 11:49:44 +02001941 switch (exit_code) {
1942 case SVM_EXIT_INTR:
1943 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001944 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001945 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001946 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001947 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001948 if (npt_enabled)
1949 return NESTED_EXIT_HOST;
1950 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001951 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02001952 /* When we're shadowing, trap PFs, but not async PF */
1953 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001954 return NESTED_EXIT_HOST;
1955 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001956 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1957 nm_interception(svm);
1958 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001959 default:
1960 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001961 }
1962
Joerg Roedel410e4d52009-08-07 11:49:44 +02001963 return NESTED_EXIT_CONTINUE;
1964}
1965
1966/*
1967 * If this function returns true, this #vmexit was already handled
1968 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001969static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001970{
1971 u32 exit_code = svm->vmcb->control.exit_code;
1972 int vmexit = NESTED_EXIT_HOST;
1973
Alexander Grafcf74a782008-11-25 20:17:08 +01001974 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001975 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001976 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001977 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001978 case SVM_EXIT_IOIO:
1979 vmexit = nested_svm_intercept_ioio(svm);
1980 break;
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001981 case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
1982 u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
1983 if (svm->nested.intercept_cr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001984 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001985 break;
1986 }
Joerg Roedel3aed0412010-11-30 18:03:58 +01001987 case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
1988 u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
1989 if (svm->nested.intercept_dr & bit)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001990 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001991 break;
1992 }
1993 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1994 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001995 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001996 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02001997 /* async page fault always cause vmexit */
1998 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
1999 svm->apf_reason != 0)
2000 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002001 break;
2002 }
Joerg Roedel228070b2010-04-22 12:33:10 +02002003 case SVM_EXIT_ERR: {
2004 vmexit = NESTED_EXIT_DONE;
2005 break;
2006 }
Alexander Grafcf74a782008-11-25 20:17:08 +01002007 default: {
2008 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02002009 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02002010 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01002011 }
2012 }
2013
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01002014 return vmexit;
2015}
2016
2017static int nested_svm_exit_handled(struct vcpu_svm *svm)
2018{
2019 int vmexit;
2020
2021 vmexit = nested_svm_intercept(svm);
2022
2023 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002024 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02002025
2026 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01002027}
2028
Joerg Roedel0460a972009-08-07 11:49:31 +02002029static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
2030{
2031 struct vmcb_control_area *dst = &dst_vmcb->control;
2032 struct vmcb_control_area *from = &from_vmcb->control;
2033
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002034 dst->intercept_cr = from->intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002035 dst->intercept_dr = from->intercept_dr;
Joerg Roedel0460a972009-08-07 11:49:31 +02002036 dst->intercept_exceptions = from->intercept_exceptions;
2037 dst->intercept = from->intercept;
2038 dst->iopm_base_pa = from->iopm_base_pa;
2039 dst->msrpm_base_pa = from->msrpm_base_pa;
2040 dst->tsc_offset = from->tsc_offset;
2041 dst->asid = from->asid;
2042 dst->tlb_ctl = from->tlb_ctl;
2043 dst->int_ctl = from->int_ctl;
2044 dst->int_vector = from->int_vector;
2045 dst->int_state = from->int_state;
2046 dst->exit_code = from->exit_code;
2047 dst->exit_code_hi = from->exit_code_hi;
2048 dst->exit_info_1 = from->exit_info_1;
2049 dst->exit_info_2 = from->exit_info_2;
2050 dst->exit_int_info = from->exit_int_info;
2051 dst->exit_int_info_err = from->exit_int_info_err;
2052 dst->nested_ctl = from->nested_ctl;
2053 dst->event_inj = from->event_inj;
2054 dst->event_inj_err = from->event_inj_err;
2055 dst->nested_cr3 = from->nested_cr3;
2056 dst->lbr_ctl = from->lbr_ctl;
2057}
2058
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002059static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002060{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002061 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002062 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002063 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002064 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002065
Joerg Roedel17897f32009-10-09 16:08:29 +02002066 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2067 vmcb->control.exit_info_1,
2068 vmcb->control.exit_info_2,
2069 vmcb->control.exit_int_info,
2070 vmcb->control.exit_int_info_err);
2071
Joerg Roedel7597f122010-02-19 16:23:00 +01002072 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002073 if (!nested_vmcb)
2074 return 1;
2075
Joerg Roedel20307532010-11-29 17:51:48 +01002076 /* Exit Guest-Mode */
2077 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01002078 svm->nested.vmcb = 0;
2079
Alexander Grafcf74a782008-11-25 20:17:08 +01002080 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002081 disable_gif(svm);
2082
2083 nested_vmcb->save.es = vmcb->save.es;
2084 nested_vmcb->save.cs = vmcb->save.cs;
2085 nested_vmcb->save.ss = vmcb->save.ss;
2086 nested_vmcb->save.ds = vmcb->save.ds;
2087 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2088 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002089 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002090 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002091 nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002092 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002093 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002094 nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
Joerg Roedel33740e42009-08-07 11:49:29 +02002095 nested_vmcb->save.rip = vmcb->save.rip;
2096 nested_vmcb->save.rsp = vmcb->save.rsp;
2097 nested_vmcb->save.rax = vmcb->save.rax;
2098 nested_vmcb->save.dr7 = vmcb->save.dr7;
2099 nested_vmcb->save.dr6 = vmcb->save.dr6;
2100 nested_vmcb->save.cpl = vmcb->save.cpl;
2101
2102 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2103 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2104 nested_vmcb->control.int_state = vmcb->control.int_state;
2105 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2106 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2107 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2108 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2109 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2110 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002111 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002112
2113 /*
2114 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2115 * to make sure that we do not lose injected events. So check event_inj
2116 * here and copy it to exit_int_info if it is valid.
2117 * Exit_int_info and event_inj can't be both valid because the case
2118 * below only happens on a VMRUN instruction intercept which has
2119 * no valid exit_int_info set.
2120 */
2121 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2122 struct vmcb_control_area *nc = &nested_vmcb->control;
2123
2124 nc->exit_int_info = vmcb->control.event_inj;
2125 nc->exit_int_info_err = vmcb->control.event_inj_err;
2126 }
2127
Joerg Roedel33740e42009-08-07 11:49:29 +02002128 nested_vmcb->control.tlb_ctl = 0;
2129 nested_vmcb->control.event_inj = 0;
2130 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002131
2132 /* We always set V_INTR_MASKING and remember the old value in hflags */
2133 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2134 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2135
Alexander Grafcf74a782008-11-25 20:17:08 +01002136 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002137 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002138
Alexander Graf219b65d2009-06-15 15:21:25 +02002139 kvm_clear_exception_queue(&svm->vcpu);
2140 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002141
Joerg Roedel4b161842010-09-10 17:31:03 +02002142 svm->nested.nested_cr3 = 0;
2143
Alexander Grafcf74a782008-11-25 20:17:08 +01002144 /* Restore selected save entries */
2145 svm->vmcb->save.es = hsave->save.es;
2146 svm->vmcb->save.cs = hsave->save.cs;
2147 svm->vmcb->save.ss = hsave->save.ss;
2148 svm->vmcb->save.ds = hsave->save.ds;
2149 svm->vmcb->save.gdtr = hsave->save.gdtr;
2150 svm->vmcb->save.idtr = hsave->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002151 kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
Alexander Grafcf74a782008-11-25 20:17:08 +01002152 svm_set_efer(&svm->vcpu, hsave->save.efer);
2153 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2154 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2155 if (npt_enabled) {
2156 svm->vmcb->save.cr3 = hsave->save.cr3;
2157 svm->vcpu.arch.cr3 = hsave->save.cr3;
2158 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002159 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002160 }
2161 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2162 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2163 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2164 svm->vmcb->save.dr7 = 0;
2165 svm->vmcb->save.cpl = 0;
2166 svm->vmcb->control.exit_int_info = 0;
2167
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002168 mark_all_dirty(svm->vmcb);
2169
Joerg Roedel7597f122010-02-19 16:23:00 +01002170 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002171
Joerg Roedel4b161842010-09-10 17:31:03 +02002172 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002173 kvm_mmu_reset_context(&svm->vcpu);
2174 kvm_mmu_load(&svm->vcpu);
2175
2176 return 0;
2177}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002178
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002179static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002180{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002181 /*
2182 * This function merges the msr permission bitmaps of kvm and the
2183 * nested vmcb. It is omptimized in that it only merges the parts where
2184 * the kvm msr permission bitmap may contain zero bits
2185 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002186 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002187
Joerg Roedel323c3d82010-03-01 15:34:37 +01002188 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2189 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002190
Joerg Roedel323c3d82010-03-01 15:34:37 +01002191 for (i = 0; i < MSRPM_OFFSETS; i++) {
2192 u32 value, p;
2193 u64 offset;
2194
2195 if (msrpm_offsets[i] == 0xffffffff)
2196 break;
2197
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002198 p = msrpm_offsets[i];
2199 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002200
2201 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2202 return false;
2203
2204 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2205 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002206
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002207 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002208
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002209 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002210}
2211
Joerg Roedel52c65a302010-08-02 16:46:44 +02002212static bool nested_vmcb_checks(struct vmcb *vmcb)
2213{
2214 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2215 return false;
2216
Joerg Roedeldbe77582010-08-02 16:46:45 +02002217 if (vmcb->control.asid == 0)
2218 return false;
2219
Joerg Roedel4b161842010-09-10 17:31:03 +02002220 if (vmcb->control.nested_ctl && !npt_enabled)
2221 return false;
2222
Joerg Roedel52c65a302010-08-02 16:46:44 +02002223 return true;
2224}
2225
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002226static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002227{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002228 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002229 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002230 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002231 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002232 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002233
Joerg Roedel06fc77722010-02-19 16:23:07 +01002234 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002235
Joerg Roedel7597f122010-02-19 16:23:00 +01002236 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002237 if (!nested_vmcb)
2238 return false;
2239
Joerg Roedel52c65a302010-08-02 16:46:44 +02002240 if (!nested_vmcb_checks(nested_vmcb)) {
2241 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2242 nested_vmcb->control.exit_code_hi = 0;
2243 nested_vmcb->control.exit_info_1 = 0;
2244 nested_vmcb->control.exit_info_2 = 0;
2245
2246 nested_svm_unmap(page);
2247
2248 return false;
2249 }
2250
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002251 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002252 nested_vmcb->save.rip,
2253 nested_vmcb->control.int_ctl,
2254 nested_vmcb->control.event_inj,
2255 nested_vmcb->control.nested_ctl);
2256
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002257 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
2258 nested_vmcb->control.intercept_cr >> 16,
Joerg Roedel2e554e82010-02-24 18:59:14 +01002259 nested_vmcb->control.intercept_exceptions,
2260 nested_vmcb->control.intercept);
2261
Alexander Graf3d6368e2008-11-25 20:17:07 +01002262 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002263 kvm_clear_exception_queue(&svm->vcpu);
2264 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002265
Joerg Roedele0231712010-02-24 18:59:10 +01002266 /*
2267 * Save the old vmcb, so we don't need to pick what we save, but can
2268 * restore everything when a VMEXIT occurs
2269 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002270 hsave->save.es = vmcb->save.es;
2271 hsave->save.cs = vmcb->save.cs;
2272 hsave->save.ss = vmcb->save.ss;
2273 hsave->save.ds = vmcb->save.ds;
2274 hsave->save.gdtr = vmcb->save.gdtr;
2275 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002276 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002277 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002278 hsave->save.cr4 = svm->vcpu.arch.cr4;
Avi Kivityf6e78472010-08-02 15:30:20 +03002279 hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002280 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002281 hsave->save.rsp = vmcb->save.rsp;
2282 hsave->save.rax = vmcb->save.rax;
2283 if (npt_enabled)
2284 hsave->save.cr3 = vmcb->save.cr3;
2285 else
Avi Kivity9f8fe502010-12-05 17:30:00 +02002286 hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002287
Joerg Roedel0460a972009-08-07 11:49:31 +02002288 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002289
Avi Kivityf6e78472010-08-02 15:30:20 +03002290 if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002291 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2292 else
2293 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2294
Joerg Roedel4b161842010-09-10 17:31:03 +02002295 if (nested_vmcb->control.nested_ctl) {
2296 kvm_mmu_unload(&svm->vcpu);
2297 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2298 nested_svm_init_mmu_context(&svm->vcpu);
2299 }
2300
Alexander Graf3d6368e2008-11-25 20:17:07 +01002301 /* Load the nested guest state */
2302 svm->vmcb->save.es = nested_vmcb->save.es;
2303 svm->vmcb->save.cs = nested_vmcb->save.cs;
2304 svm->vmcb->save.ss = nested_vmcb->save.ss;
2305 svm->vmcb->save.ds = nested_vmcb->save.ds;
2306 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2307 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
Avi Kivityf6e78472010-08-02 15:30:20 +03002308 kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002309 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2310 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2311 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2312 if (npt_enabled) {
2313 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2314 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002315 } else
Avi Kivity23902182010-06-10 17:02:16 +03002316 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002317
2318 /* Guest paging mode is active - reset mmu */
2319 kvm_mmu_reset_context(&svm->vcpu);
2320
Joerg Roedeldefbba52009-08-07 11:49:30 +02002321 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002322 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2323 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2324 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002325
Alexander Graf3d6368e2008-11-25 20:17:07 +01002326 /* In case we don't even reach vcpu_run, the fields are not updated */
2327 svm->vmcb->save.rax = nested_vmcb->save.rax;
2328 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2329 svm->vmcb->save.rip = nested_vmcb->save.rip;
2330 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2331 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2332 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2333
Joerg Roedelf7138532010-03-01 15:34:40 +01002334 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002335 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002336
Joerg Roedelaad42c62009-08-07 11:49:34 +02002337 /* cache intercepts */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002338 svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
Joerg Roedel3aed0412010-11-30 18:03:58 +01002339 svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
Joerg Roedelaad42c62009-08-07 11:49:34 +02002340 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2341 svm->nested.intercept = nested_vmcb->control.intercept;
2342
Joerg Roedelf40f6a42010-12-03 15:25:15 +01002343 svm_flush_tlb(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002344 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002345 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2346 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2347 else
2348 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2349
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002350 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2351 /* We only want the cr8 intercept bits of the guest */
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002352 clr_cr_intercept(svm, INTERCEPT_CR8_READ);
2353 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002354 }
2355
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002356 /* We don't want to see VMMCALLs from a nested guest */
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002357 clr_intercept(svm, INTERCEPT_VMMCALL);
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002358
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002359 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002360 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2361 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2362 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002363 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2364 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2365
Joerg Roedel7597f122010-02-19 16:23:00 +01002366 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002367
Joerg Roedel20307532010-11-29 17:51:48 +01002368 /* Enter Guest-Mode */
2369 enter_guest_mode(&svm->vcpu);
2370
Joerg Roedel384c6362010-11-30 18:03:56 +01002371 /*
2372 * Merge guest and host intercepts - must be called with vcpu in
2373 * guest-mode to take affect here
2374 */
2375 recalc_intercepts(svm);
2376
Joerg Roedel06fc77722010-02-19 16:23:07 +01002377 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002378
Joerg Roedel2af91942009-08-07 11:49:28 +02002379 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002380
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01002381 mark_all_dirty(svm->vmcb);
2382
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002383 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002384}
2385
Joerg Roedel9966bf62009-08-07 11:49:40 +02002386static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002387{
2388 to_vmcb->save.fs = from_vmcb->save.fs;
2389 to_vmcb->save.gs = from_vmcb->save.gs;
2390 to_vmcb->save.tr = from_vmcb->save.tr;
2391 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2392 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2393 to_vmcb->save.star = from_vmcb->save.star;
2394 to_vmcb->save.lstar = from_vmcb->save.lstar;
2395 to_vmcb->save.cstar = from_vmcb->save.cstar;
2396 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2397 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2398 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2399 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002400}
2401
Avi Kivity851ba692009-08-24 11:10:17 +03002402static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002403{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002404 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002405 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002406
Alexander Graf55426752008-11-25 20:17:06 +01002407 if (nested_svm_check_permissions(svm))
2408 return 1;
2409
2410 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2411 skip_emulated_instruction(&svm->vcpu);
2412
Joerg Roedel7597f122010-02-19 16:23:00 +01002413 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002414 if (!nested_vmcb)
2415 return 1;
2416
2417 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002418 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002419
2420 return 1;
2421}
2422
Avi Kivity851ba692009-08-24 11:10:17 +03002423static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002424{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002425 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002426 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002427
Alexander Graf55426752008-11-25 20:17:06 +01002428 if (nested_svm_check_permissions(svm))
2429 return 1;
2430
2431 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2432 skip_emulated_instruction(&svm->vcpu);
2433
Joerg Roedel7597f122010-02-19 16:23:00 +01002434 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002435 if (!nested_vmcb)
2436 return 1;
2437
2438 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002439 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002440
2441 return 1;
2442}
2443
Avi Kivity851ba692009-08-24 11:10:17 +03002444static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002445{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002446 if (nested_svm_check_permissions(svm))
2447 return 1;
2448
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002449 /* Save rip after vmrun instruction */
2450 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002451
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002452 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002453 return 1;
2454
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002455 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002456 goto failed;
2457
2458 return 1;
2459
2460failed:
2461
2462 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2463 svm->vmcb->control.exit_code_hi = 0;
2464 svm->vmcb->control.exit_info_1 = 0;
2465 svm->vmcb->control.exit_info_2 = 0;
2466
2467 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002468
2469 return 1;
2470}
2471
Avi Kivity851ba692009-08-24 11:10:17 +03002472static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002473{
2474 if (nested_svm_check_permissions(svm))
2475 return 1;
2476
2477 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2478 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002479 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002480
Joerg Roedel2af91942009-08-07 11:49:28 +02002481 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002482
2483 return 1;
2484}
2485
Avi Kivity851ba692009-08-24 11:10:17 +03002486static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002487{
2488 if (nested_svm_check_permissions(svm))
2489 return 1;
2490
2491 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2492 skip_emulated_instruction(&svm->vcpu);
2493
Joerg Roedel2af91942009-08-07 11:49:28 +02002494 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002495
2496 /* After a CLGI no interrupts should come */
2497 svm_clear_vintr(svm);
2498 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2499
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002500 mark_dirty(svm->vmcb, VMCB_INTR);
2501
Alexander Graf1371d902008-11-25 20:17:04 +01002502 return 1;
2503}
2504
Avi Kivity851ba692009-08-24 11:10:17 +03002505static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002506{
2507 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002508
Joerg Roedelec1ff792009-10-09 16:08:31 +02002509 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2510 vcpu->arch.regs[VCPU_REGS_RAX]);
2511
Alexander Grafff092382009-06-15 15:21:24 +02002512 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2513 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2514
2515 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2516 skip_emulated_instruction(&svm->vcpu);
2517 return 1;
2518}
2519
Joerg Roedel532a46b2009-10-09 16:08:32 +02002520static int skinit_interception(struct vcpu_svm *svm)
2521{
2522 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2523
2524 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2525 return 1;
2526}
2527
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002528static int xsetbv_interception(struct vcpu_svm *svm)
2529{
2530 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
2531 u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
2532
2533 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
2534 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2535 skip_emulated_instruction(&svm->vcpu);
2536 }
2537
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;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02002624 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002625 return 1;
2626}
2627
Avi Kivity851ba692009-08-24 11:10:17 +03002628static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002629{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002630 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2631 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
2632
2633 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
2634 skip_emulated_instruction(&svm->vcpu);
2635 return 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002636}
2637
Avi Kivity851ba692009-08-24 11:10:17 +03002638static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639{
Andre Przywara51d8b662010-12-21 11:12:02 +01002640 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641}
2642
Joerg Roedel628afd22011-04-04 12:39:36 +02002643bool check_selective_cr0_intercepted(struct vcpu_svm *svm, unsigned long val)
2644{
2645 unsigned long cr0 = svm->vcpu.arch.cr0;
2646 bool ret = false;
2647 u64 intercept;
2648
2649 intercept = svm->nested.intercept;
2650
2651 if (!is_guest_mode(&svm->vcpu) ||
2652 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
2653 return false;
2654
2655 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2656 val &= ~SVM_CR0_SELECTIVE_MASK;
2657
2658 if (cr0 ^ val) {
2659 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2660 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2661 }
2662
2663 return ret;
2664}
2665
Andre Przywara7ff76d52010-12-21 11:12:04 +01002666#define CR_VALID (1ULL << 63)
2667
2668static int cr_interception(struct vcpu_svm *svm)
2669{
2670 int reg, cr;
2671 unsigned long val;
2672 int err;
2673
2674 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2675 return emulate_on_interception(svm);
2676
2677 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2678 return emulate_on_interception(svm);
2679
2680 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2681 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
2682
2683 err = 0;
2684 if (cr >= 16) { /* mov to cr */
2685 cr -= 16;
2686 val = kvm_register_read(&svm->vcpu, reg);
2687 switch (cr) {
2688 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02002689 if (!check_selective_cr0_intercepted(svm, val))
2690 err = kvm_set_cr0(&svm->vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002691 break;
2692 case 3:
2693 err = kvm_set_cr3(&svm->vcpu, val);
2694 break;
2695 case 4:
2696 err = kvm_set_cr4(&svm->vcpu, val);
2697 break;
2698 case 8:
2699 err = kvm_set_cr8(&svm->vcpu, val);
2700 break;
2701 default:
2702 WARN(1, "unhandled write to CR%d", cr);
2703 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2704 return 1;
2705 }
2706 } else { /* mov from cr */
2707 switch (cr) {
2708 case 0:
2709 val = kvm_read_cr0(&svm->vcpu);
2710 break;
2711 case 2:
2712 val = svm->vcpu.arch.cr2;
2713 break;
2714 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02002715 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002716 break;
2717 case 4:
2718 val = kvm_read_cr4(&svm->vcpu);
2719 break;
2720 case 8:
2721 val = kvm_get_cr8(&svm->vcpu);
2722 break;
2723 default:
2724 WARN(1, "unhandled read from CR%d", cr);
2725 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2726 return 1;
2727 }
2728 kvm_register_write(&svm->vcpu, reg, val);
2729 }
2730 kvm_complete_insn_gp(&svm->vcpu, err);
2731
2732 return 1;
2733}
2734
Andre Przywaracae37972010-12-21 11:12:05 +01002735static int dr_interception(struct vcpu_svm *svm)
2736{
2737 int reg, dr;
2738 unsigned long val;
2739 int err;
2740
2741 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
2742 return emulate_on_interception(svm);
2743
2744 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2745 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
2746
2747 if (dr >= 16) { /* mov to DRn */
2748 val = kvm_register_read(&svm->vcpu, reg);
2749 kvm_set_dr(&svm->vcpu, dr - 16, val);
2750 } else {
2751 err = kvm_get_dr(&svm->vcpu, dr, &val);
2752 if (!err)
2753 kvm_register_write(&svm->vcpu, reg, val);
2754 }
2755
Joerg Roedel2c46d2a2011-02-09 18:29:39 +01002756 skip_emulated_instruction(&svm->vcpu);
2757
Andre Przywaracae37972010-12-21 11:12:05 +01002758 return 1;
2759}
2760
Avi Kivity851ba692009-08-24 11:10:17 +03002761static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002762{
Avi Kivity851ba692009-08-24 11:10:17 +03002763 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01002764 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03002765
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002766 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2767 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01002768 r = cr_interception(svm);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002769 if (irqchip_in_kernel(svm->vcpu.kvm)) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01002770 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002771 return r;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002772 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002773 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002774 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01002775 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2776 return 0;
2777}
2778
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2780{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002781 struct vcpu_svm *svm = to_svm(vcpu);
2782
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302784 case MSR_IA32_TSC: {
Joerg Roedel4cc70312010-11-30 18:04:01 +01002785 struct vmcb *vmcb = get_host_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786
Joerg Roedel4cc70312010-11-30 18:04:01 +01002787 *data = vmcb->control.tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002788 break;
2789 }
Brian Gerst8c065852010-07-17 09:03:26 -04002790 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002791 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002793#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002795 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002796 break;
2797 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002798 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 break;
2800 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002801 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 break;
2803 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002804 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002805 break;
2806#endif
2807 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002808 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809 break;
2810 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002811 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812 break;
2813 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002814 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002815 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002816 /*
2817 * Nobody will change the following 5 values in the VMCB so we can
2818 * safely return them on rdmsr. They will always be 0 until LBRV is
2819 * implemented.
2820 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002821 case MSR_IA32_DEBUGCTLMSR:
2822 *data = svm->vmcb->save.dbgctl;
2823 break;
2824 case MSR_IA32_LASTBRANCHFROMIP:
2825 *data = svm->vmcb->save.br_from;
2826 break;
2827 case MSR_IA32_LASTBRANCHTOIP:
2828 *data = svm->vmcb->save.br_to;
2829 break;
2830 case MSR_IA32_LASTINTFROMIP:
2831 *data = svm->vmcb->save.last_excp_from;
2832 break;
2833 case MSR_IA32_LASTINTTOIP:
2834 *data = svm->vmcb->save.last_excp_to;
2835 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002836 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002837 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002838 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002839 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002840 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002841 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002842 case MSR_IA32_UCODE_REV:
2843 *data = 0x01000065;
2844 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002846 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 }
2848 return 0;
2849}
2850
Avi Kivity851ba692009-08-24 11:10:17 +03002851static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002853 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854 u64 data;
2855
Avi Kivity59200272010-01-25 19:47:02 +02002856 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2857 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002858 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002859 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002860 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002861
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002862 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002863 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002864 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002865 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 }
2867 return 1;
2868}
2869
Joerg Roedel4a810182010-02-24 18:59:15 +01002870static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2871{
2872 struct vcpu_svm *svm = to_svm(vcpu);
2873 int svm_dis, chg_mask;
2874
2875 if (data & ~SVM_VM_CR_VALID_MASK)
2876 return 1;
2877
2878 chg_mask = SVM_VM_CR_VALID_MASK;
2879
2880 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2881 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2882
2883 svm->nested.vm_cr_msr &= ~chg_mask;
2884 svm->nested.vm_cr_msr |= (data & chg_mask);
2885
2886 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2887
2888 /* check for svm_disable while efer.svme is set */
2889 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2890 return 1;
2891
2892 return 0;
2893}
2894
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2896{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002897 struct vcpu_svm *svm = to_svm(vcpu);
2898
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002900 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10002901 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002903 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002904 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002906#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002908 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909 break;
2910 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002911 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912 break;
2913 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002914 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002915 break;
2916 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002917 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918 break;
2919#endif
2920 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002921 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922 break;
2923 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002924 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002925 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926 break;
2927 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002928 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002929 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002930 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002931 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02002932 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002933 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002934 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002935 break;
2936 }
2937 if (data & DEBUGCTL_RESERVED_BITS)
2938 return 1;
2939
2940 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01002941 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002942 if (data & (1ULL<<0))
2943 svm_enable_lbrv(svm);
2944 else
2945 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002946 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002947 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002948 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002949 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002950 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002951 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002952 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002953 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2954 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002955 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002956 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957 }
2958 return 0;
2959}
2960
Avi Kivity851ba692009-08-24 11:10:17 +03002961static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002963 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002964 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002965 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002966
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002967
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002968 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002969 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2970 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002971 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002972 } else {
2973 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002974 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002975 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002976 return 1;
2977}
2978
Avi Kivity851ba692009-08-24 11:10:17 +03002979static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980{
Rusty Russelle756fc62007-07-30 20:07:08 +10002981 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002982 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983 else
Avi Kivity851ba692009-08-24 11:10:17 +03002984 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002985}
2986
Avi Kivity851ba692009-08-24 11:10:17 +03002987static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002988{
Avi Kivity851ba692009-08-24 11:10:17 +03002989 struct kvm_run *kvm_run = svm->vcpu.run;
2990
Avi Kivity3842d132010-07-27 12:30:24 +03002991 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01002992 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002993 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002994 mark_dirty(svm->vmcb, VMCB_INTR);
Dor Laorc1150d82007-01-05 16:36:24 -08002995 /*
2996 * If the user space waits to inject interrupts, exit as soon as
2997 * possible
2998 */
Gleb Natapov80618232009-04-21 17:44:56 +03002999 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
3000 kvm_run->request_interrupt_window &&
3001 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10003002 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003003 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
3004 return 0;
3005 }
3006
3007 return 1;
3008}
3009
Mark Langsdorf565d0992009-10-06 14:25:02 -05003010static int pause_interception(struct vcpu_svm *svm)
3011{
3012 kvm_vcpu_on_spin(&(svm->vcpu));
3013 return 1;
3014}
3015
Avi Kivity851ba692009-08-24 11:10:17 +03003016static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003017 [SVM_EXIT_READ_CR0] = cr_interception,
3018 [SVM_EXIT_READ_CR3] = cr_interception,
3019 [SVM_EXIT_READ_CR4] = cr_interception,
3020 [SVM_EXIT_READ_CR8] = cr_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02003021 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003022 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003023 [SVM_EXIT_WRITE_CR3] = cr_interception,
3024 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003025 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003026 [SVM_EXIT_READ_DR0] = dr_interception,
3027 [SVM_EXIT_READ_DR1] = dr_interception,
3028 [SVM_EXIT_READ_DR2] = dr_interception,
3029 [SVM_EXIT_READ_DR3] = dr_interception,
3030 [SVM_EXIT_READ_DR4] = dr_interception,
3031 [SVM_EXIT_READ_DR5] = dr_interception,
3032 [SVM_EXIT_READ_DR6] = dr_interception,
3033 [SVM_EXIT_READ_DR7] = dr_interception,
3034 [SVM_EXIT_WRITE_DR0] = dr_interception,
3035 [SVM_EXIT_WRITE_DR1] = dr_interception,
3036 [SVM_EXIT_WRITE_DR2] = dr_interception,
3037 [SVM_EXIT_WRITE_DR3] = dr_interception,
3038 [SVM_EXIT_WRITE_DR4] = dr_interception,
3039 [SVM_EXIT_WRITE_DR5] = dr_interception,
3040 [SVM_EXIT_WRITE_DR6] = dr_interception,
3041 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003042 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3043 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003044 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003045 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3046 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
3047 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
3048 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003049 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050 [SVM_EXIT_SMI] = nop_on_interception,
3051 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003052 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003053 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003054 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003055 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003056 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003058 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003059 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003060 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003061 [SVM_EXIT_MSR] = msr_interception,
3062 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003063 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003064 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003065 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003066 [SVM_EXIT_VMLOAD] = vmload_interception,
3067 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003068 [SVM_EXIT_STGI] = stgi_interception,
3069 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003070 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003071 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01003072 [SVM_EXIT_MONITOR] = invalid_op_interception,
3073 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003074 [SVM_EXIT_XSETBV] = xsetbv_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003075 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076};
3077
Joerg Roedel3f10c842010-05-05 16:04:42 +02003078void dump_vmcb(struct kvm_vcpu *vcpu)
3079{
3080 struct vcpu_svm *svm = to_svm(vcpu);
3081 struct vmcb_control_area *control = &svm->vmcb->control;
3082 struct vmcb_save_area *save = &svm->vmcb->save;
3083
3084 pr_err("VMCB Control Area:\n");
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003085 pr_err("cr_read: %04x\n", control->intercept_cr & 0xffff);
3086 pr_err("cr_write: %04x\n", control->intercept_cr >> 16);
Joerg Roedel3aed0412010-11-30 18:03:58 +01003087 pr_err("dr_read: %04x\n", control->intercept_dr & 0xffff);
3088 pr_err("dr_write: %04x\n", control->intercept_dr >> 16);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003089 pr_err("exceptions: %08x\n", control->intercept_exceptions);
3090 pr_err("intercepts: %016llx\n", control->intercept);
3091 pr_err("pause filter count: %d\n", control->pause_filter_count);
3092 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
3093 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
3094 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
3095 pr_err("asid: %d\n", control->asid);
3096 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
3097 pr_err("int_ctl: %08x\n", control->int_ctl);
3098 pr_err("int_vector: %08x\n", control->int_vector);
3099 pr_err("int_state: %08x\n", control->int_state);
3100 pr_err("exit_code: %08x\n", control->exit_code);
3101 pr_err("exit_info1: %016llx\n", control->exit_info_1);
3102 pr_err("exit_info2: %016llx\n", control->exit_info_2);
3103 pr_err("exit_int_info: %08x\n", control->exit_int_info);
3104 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
3105 pr_err("nested_ctl: %lld\n", control->nested_ctl);
3106 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
3107 pr_err("event_inj: %08x\n", control->event_inj);
3108 pr_err("event_inj_err: %08x\n", control->event_inj_err);
3109 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
3110 pr_err("next_rip: %016llx\n", control->next_rip);
3111 pr_err("VMCB State Save Area:\n");
3112 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
3113 save->es.selector, save->es.attrib,
3114 save->es.limit, save->es.base);
3115 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
3116 save->cs.selector, save->cs.attrib,
3117 save->cs.limit, save->cs.base);
3118 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
3119 save->ss.selector, save->ss.attrib,
3120 save->ss.limit, save->ss.base);
3121 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
3122 save->ds.selector, save->ds.attrib,
3123 save->ds.limit, save->ds.base);
3124 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
3125 save->fs.selector, save->fs.attrib,
3126 save->fs.limit, save->fs.base);
3127 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
3128 save->gs.selector, save->gs.attrib,
3129 save->gs.limit, save->gs.base);
3130 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
3131 save->gdtr.selector, save->gdtr.attrib,
3132 save->gdtr.limit, save->gdtr.base);
3133 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
3134 save->ldtr.selector, save->ldtr.attrib,
3135 save->ldtr.limit, save->ldtr.base);
3136 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
3137 save->idtr.selector, save->idtr.attrib,
3138 save->idtr.limit, save->idtr.base);
3139 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
3140 save->tr.selector, save->tr.attrib,
3141 save->tr.limit, save->tr.base);
3142 pr_err("cpl: %d efer: %016llx\n",
3143 save->cpl, save->efer);
3144 pr_err("cr0: %016llx cr2: %016llx\n",
3145 save->cr0, save->cr2);
3146 pr_err("cr3: %016llx cr4: %016llx\n",
3147 save->cr3, save->cr4);
3148 pr_err("dr6: %016llx dr7: %016llx\n",
3149 save->dr6, save->dr7);
3150 pr_err("rip: %016llx rflags: %016llx\n",
3151 save->rip, save->rflags);
3152 pr_err("rsp: %016llx rax: %016llx\n",
3153 save->rsp, save->rax);
3154 pr_err("star: %016llx lstar: %016llx\n",
3155 save->star, save->lstar);
3156 pr_err("cstar: %016llx sfmask: %016llx\n",
3157 save->cstar, save->sfmask);
3158 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
3159 save->kernel_gs_base, save->sysenter_cs);
3160 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
3161 save->sysenter_esp, save->sysenter_eip);
3162 pr_err("gpat: %016llx dbgctl: %016llx\n",
3163 save->g_pat, save->dbgctl);
3164 pr_err("br_from: %016llx br_to: %016llx\n",
3165 save->br_from, save->br_to);
3166 pr_err("excp_from: %016llx excp_to: %016llx\n",
3167 save->last_excp_from, save->last_excp_to);
3168
3169}
3170
Avi Kivity586f9602010-11-18 13:09:54 +02003171static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3172{
3173 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3174
3175 *info1 = control->exit_info_1;
3176 *info2 = control->exit_info_2;
3177}
3178
Avi Kivity851ba692009-08-24 11:10:17 +03003179static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003181 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003182 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003183 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003184
Avi Kivityaa179112010-11-17 18:44:19 +02003185 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003186
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003187 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003188 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3189 if (npt_enabled)
3190 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003192 if (unlikely(svm->nested.exit_required)) {
3193 nested_svm_vmexit(svm);
3194 svm->nested.exit_required = false;
3195
3196 return 1;
3197 }
3198
Joerg Roedel20307532010-11-29 17:51:48 +01003199 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003200 int vmexit;
3201
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003202 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3203 svm->vmcb->control.exit_info_1,
3204 svm->vmcb->control.exit_info_2,
3205 svm->vmcb->control.exit_int_info,
3206 svm->vmcb->control.exit_int_info_err);
3207
Joerg Roedel410e4d52009-08-07 11:49:44 +02003208 vmexit = nested_svm_exit_special(svm);
3209
3210 if (vmexit == NESTED_EXIT_CONTINUE)
3211 vmexit = nested_svm_exit_handled(svm);
3212
3213 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003214 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003215 }
3216
Joerg Roedela5c38322009-08-07 11:49:32 +02003217 svm_complete_interrupts(svm);
3218
Avi Kivity04d2cc72007-09-10 18:10:54 +03003219 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3220 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3221 kvm_run->fail_entry.hardware_entry_failure_reason
3222 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003223 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3224 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003225 return 0;
3226 }
3227
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003228 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003229 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003230 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3231 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3233 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003234 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235 exit_code);
3236
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003237 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003238 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003240 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241 return 0;
3242 }
3243
Avi Kivity851ba692009-08-24 11:10:17 +03003244 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245}
3246
3247static void reload_tss(struct kvm_vcpu *vcpu)
3248{
3249 int cpu = raw_smp_processor_id();
3250
Tejun Heo0fe1e002009-10-29 22:34:14 +09003251 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3252 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003253 load_TR_desc();
3254}
3255
Rusty Russelle756fc62007-07-30 20:07:08 +10003256static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003257{
3258 int cpu = raw_smp_processor_id();
3259
Tejun Heo0fe1e002009-10-29 22:34:14 +09003260 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003262 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003263 if (svm->asid_generation != sd->asid_generation)
3264 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265}
3266
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003267static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3268{
3269 struct vcpu_svm *svm = to_svm(vcpu);
3270
3271 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3272 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003273 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003274 ++vcpu->stat.nmi_injections;
3275}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276
Eddie Dong85f455f2007-07-06 12:20:49 +03003277static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278{
3279 struct vmcb_control_area *control;
3280
Rusty Russelle756fc62007-07-30 20:07:08 +10003281 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003282 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003283 control->int_ctl &= ~V_INTR_PRIO_MASK;
3284 control->int_ctl |= V_IRQ_MASK |
3285 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01003286 mark_dirty(svm->vmcb, VMCB_INTR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003287}
3288
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003289static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003290{
3291 struct vcpu_svm *svm = to_svm(vcpu);
3292
Joerg Roedel2af91942009-08-07 11:49:28 +02003293 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003294
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003295 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3296 ++vcpu->stat.irq_injections;
3297
Alexander Graf219b65d2009-06-15 15:21:25 +02003298 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3299 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003300}
3301
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003302static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3303{
3304 struct vcpu_svm *svm = to_svm(vcpu);
3305
Joerg Roedel20307532010-11-29 17:51:48 +01003306 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003307 return;
3308
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003309 if (irr == -1)
3310 return;
3311
3312 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003313 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003314}
3315
3316static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003317{
3318 struct vcpu_svm *svm = to_svm(vcpu);
3319 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003320 int ret;
3321 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3322 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3323 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3324
3325 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003326}
3327
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003328static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3329{
3330 struct vcpu_svm *svm = to_svm(vcpu);
3331
3332 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3333}
3334
3335static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3336{
3337 struct vcpu_svm *svm = to_svm(vcpu);
3338
3339 if (masked) {
3340 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003341 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003342 } else {
3343 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003344 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003345 }
3346}
3347
Gleb Natapov78646122009-03-23 12:12:11 +02003348static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3349{
3350 struct vcpu_svm *svm = to_svm(vcpu);
3351 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003352 int ret;
3353
3354 if (!gif_set(svm) ||
3355 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3356 return 0;
3357
Avi Kivityf6e78472010-08-02 15:30:20 +03003358 ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003359
Joerg Roedel20307532010-11-29 17:51:48 +01003360 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003361 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3362
3363 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003364}
3365
Gleb Natapov9222be12009-04-23 17:14:37 +03003366static void enable_irq_window(struct kvm_vcpu *vcpu)
3367{
Alexander Graf219b65d2009-06-15 15:21:25 +02003368 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003369
Joerg Roedele0231712010-02-24 18:59:10 +01003370 /*
3371 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3372 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3373 * get that intercept, this function will be called again though and
3374 * we'll get the vintr intercept.
3375 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003376 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003377 svm_set_vintr(svm);
3378 svm_inject_irq(svm, 0x0);
3379 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003380}
3381
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003382static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003383{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003384 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003385
Gleb Natapov44c11432009-05-11 13:35:52 +03003386 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3387 == HF_NMI_MASK)
3388 return; /* IRET will cause a vm exit */
3389
Joerg Roedele0231712010-02-24 18:59:10 +01003390 /*
3391 * Something prevents NMI from been injected. Single step over possible
3392 * problem (IRET or exception injection or interrupt shadow)
3393 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003394 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003395 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3396 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003397}
3398
Izik Eiduscbc94022007-10-25 00:29:55 +02003399static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3400{
3401 return 0;
3402}
3403
Avi Kivityd9e368d2007-06-07 19:18:30 +03003404static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3405{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003406 struct vcpu_svm *svm = to_svm(vcpu);
3407
3408 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3409 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3410 else
3411 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003412}
3413
Avi Kivity04d2cc72007-09-10 18:10:54 +03003414static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3415{
3416}
3417
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003418static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3419{
3420 struct vcpu_svm *svm = to_svm(vcpu);
3421
Joerg Roedel20307532010-11-29 17:51:48 +01003422 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003423 return;
3424
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003425 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003426 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003427 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003428 }
3429}
3430
Joerg Roedel649d6862008-04-16 16:51:15 +02003431static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3432{
3433 struct vcpu_svm *svm = to_svm(vcpu);
3434 u64 cr8;
3435
Joerg Roedel20307532010-11-29 17:51:48 +01003436 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003437 return;
3438
Joerg Roedel649d6862008-04-16 16:51:15 +02003439 cr8 = kvm_get_cr8(vcpu);
3440 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3441 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3442}
3443
Gleb Natapov9222be12009-04-23 17:14:37 +03003444static void svm_complete_interrupts(struct vcpu_svm *svm)
3445{
3446 u8 vector;
3447 int type;
3448 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003449 unsigned int3_injected = svm->int3_injected;
3450
3451 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003452
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003453 /*
3454 * If we've made progress since setting HF_IRET_MASK, we've
3455 * executed an IRET and can allow NMI injection.
3456 */
3457 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
3458 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003459 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003460 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3461 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003462
Gleb Natapov9222be12009-04-23 17:14:37 +03003463 svm->vcpu.arch.nmi_injected = false;
3464 kvm_clear_exception_queue(&svm->vcpu);
3465 kvm_clear_interrupt_queue(&svm->vcpu);
3466
3467 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3468 return;
3469
Avi Kivity3842d132010-07-27 12:30:24 +03003470 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3471
Gleb Natapov9222be12009-04-23 17:14:37 +03003472 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3473 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3474
3475 switch (type) {
3476 case SVM_EXITINTINFO_TYPE_NMI:
3477 svm->vcpu.arch.nmi_injected = true;
3478 break;
3479 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003480 /*
3481 * In case of software exceptions, do not reinject the vector,
3482 * but re-execute the instruction instead. Rewind RIP first
3483 * if we emulated INT3 before.
3484 */
3485 if (kvm_exception_is_soft(vector)) {
3486 if (vector == BP_VECTOR && int3_injected &&
3487 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3488 kvm_rip_write(&svm->vcpu,
3489 kvm_rip_read(&svm->vcpu) -
3490 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003491 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003492 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003493 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3494 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003495 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003496
3497 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003498 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003499 break;
3500 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003501 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003502 break;
3503 default:
3504 break;
3505 }
3506}
3507
Avi Kivityb463a6f2010-07-20 15:06:17 +03003508static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3509{
3510 struct vcpu_svm *svm = to_svm(vcpu);
3511 struct vmcb_control_area *control = &svm->vmcb->control;
3512
3513 control->exit_int_info = control->event_inj;
3514 control->exit_int_info_err = control->event_inj_err;
3515 control->event_inj = 0;
3516 svm_complete_interrupts(svm);
3517}
3518
Avi Kivity80e31d42008-07-14 14:44:59 +03003519#ifdef CONFIG_X86_64
3520#define R "r"
3521#else
3522#define R "e"
3523#endif
3524
Avi Kivity851ba692009-08-24 11:10:17 +03003525static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003527 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003528
Joerg Roedel2041a062010-04-22 12:33:08 +02003529 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3530 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3531 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3532
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003533 /*
3534 * A vmexit emulation is required before the vcpu can be executed
3535 * again.
3536 */
3537 if (unlikely(svm->nested.exit_required))
3538 return;
3539
Rusty Russelle756fc62007-07-30 20:07:08 +10003540 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003541
Joerg Roedel649d6862008-04-16 16:51:15 +02003542 sync_lapic_to_cr8(vcpu);
3543
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003544 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545
Avi Kivity04d2cc72007-09-10 18:10:54 +03003546 clgi();
3547
3548 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003549
Avi Kivity6aa8b732006-12-10 02:21:36 -08003550 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003551 "push %%"R"bp; \n\t"
3552 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3553 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3554 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3555 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3556 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3557 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003558#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003559 "mov %c[r8](%[svm]), %%r8 \n\t"
3560 "mov %c[r9](%[svm]), %%r9 \n\t"
3561 "mov %c[r10](%[svm]), %%r10 \n\t"
3562 "mov %c[r11](%[svm]), %%r11 \n\t"
3563 "mov %c[r12](%[svm]), %%r12 \n\t"
3564 "mov %c[r13](%[svm]), %%r13 \n\t"
3565 "mov %c[r14](%[svm]), %%r14 \n\t"
3566 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003567#endif
3568
Avi Kivity6aa8b732006-12-10 02:21:36 -08003569 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003570 "push %%"R"ax \n\t"
3571 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003572 __ex(SVM_VMLOAD) "\n\t"
3573 __ex(SVM_VMRUN) "\n\t"
3574 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003575 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003576
3577 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003578 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3579 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3580 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3581 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3582 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3583 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003584#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003585 "mov %%r8, %c[r8](%[svm]) \n\t"
3586 "mov %%r9, %c[r9](%[svm]) \n\t"
3587 "mov %%r10, %c[r10](%[svm]) \n\t"
3588 "mov %%r11, %c[r11](%[svm]) \n\t"
3589 "mov %%r12, %c[r12](%[svm]) \n\t"
3590 "mov %%r13, %c[r13](%[svm]) \n\t"
3591 "mov %%r14, %c[r14](%[svm]) \n\t"
3592 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003593#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003594 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003595 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003596 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003597 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003598 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3599 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3600 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3601 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3602 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3603 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003604#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003605 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3606 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3607 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3608 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3609 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3610 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3611 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3612 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003613#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003614 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003615 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003616#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003617 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3618#endif
3619 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003620
Avi Kivity82ca2d12010-10-21 12:20:34 +02003621#ifdef CONFIG_X86_64
3622 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3623#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003624 loadsegment(fs, svm->host.fs);
Avi Kivity831ca602011-03-08 16:09:51 +02003625#ifndef CONFIG_X86_32_LAZY_GS
3626 loadsegment(gs, svm->host.gs);
3627#endif
Avi Kivity9581d442010-10-19 16:46:55 +02003628#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003629
3630 reload_tss(vcpu);
3631
Avi Kivity56ba47d2007-11-07 17:14:18 +02003632 local_irq_disable();
3633
Avi Kivity13c34e02010-10-21 12:20:31 +02003634 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3635 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3636 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3637 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3638
Joerg Roedel3781c012011-01-14 16:45:02 +01003639 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3640 kvm_before_handle_nmi(&svm->vcpu);
3641
3642 stgi();
3643
3644 /* Any pending NMI will happen here */
3645
3646 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3647 kvm_after_handle_nmi(&svm->vcpu);
3648
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003649 sync_cr8_to_lapic(vcpu);
3650
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003651 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003652
Joerg Roedel38e5e922010-12-03 15:25:16 +01003653 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3654
Gleb Natapov631bc482010-10-14 11:22:52 +02003655 /* if exit due to PF check for async PF */
3656 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3657 svm->apf_reason = kvm_read_and_reset_pf_reason();
3658
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003659 if (npt_enabled) {
3660 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3661 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3662 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003663
3664 /*
3665 * We need to handle MC intercepts here before the vcpu has a chance to
3666 * change the physical cpu
3667 */
3668 if (unlikely(svm->vmcb->control.exit_code ==
3669 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3670 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003671
3672 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003673}
3674
Avi Kivity80e31d42008-07-14 14:44:59 +03003675#undef R
3676
Avi Kivity6aa8b732006-12-10 02:21:36 -08003677static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3678{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003679 struct vcpu_svm *svm = to_svm(vcpu);
3680
3681 svm->vmcb->save.cr3 = root;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003682 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003683 svm_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003684}
3685
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003686static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3687{
3688 struct vcpu_svm *svm = to_svm(vcpu);
3689
3690 svm->vmcb->control.nested_cr3 = root;
Joerg Roedelb2747162010-12-03 11:45:53 +01003691 mark_dirty(svm->vmcb, VMCB_NPT);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003692
3693 /* Also sync guest cr3 here in case we live migrate */
Avi Kivity9f8fe502010-12-05 17:30:00 +02003694 svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
Joerg Roedeldcca1a62010-12-03 11:45:54 +01003695 mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003696
Joerg Roedelf40f6a42010-12-03 15:25:15 +01003697 svm_flush_tlb(vcpu);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003698}
3699
Avi Kivity6aa8b732006-12-10 02:21:36 -08003700static int is_disabled(void)
3701{
Joerg Roedel6031a612007-06-22 12:29:50 +03003702 u64 vm_cr;
3703
3704 rdmsrl(MSR_VM_CR, vm_cr);
3705 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3706 return 1;
3707
Avi Kivity6aa8b732006-12-10 02:21:36 -08003708 return 0;
3709}
3710
Ingo Molnar102d8322007-02-19 14:37:47 +02003711static void
3712svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3713{
3714 /*
3715 * Patch in the VMMCALL instruction:
3716 */
3717 hypercall[0] = 0x0f;
3718 hypercall[1] = 0x01;
3719 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003720}
3721
Yang, Sheng002c7f72007-07-31 14:23:01 +03003722static void svm_check_processor_compat(void *rtn)
3723{
3724 *(int *)rtn = 0;
3725}
3726
Avi Kivity774ead32007-12-26 13:57:04 +02003727static bool svm_cpu_has_accelerated_tpr(void)
3728{
3729 return false;
3730}
3731
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003732static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003733{
3734 return 0;
3735}
3736
Sheng Yang0e851882009-12-18 16:48:46 +08003737static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3738{
3739}
3740
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003741static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3742{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003743 switch (func) {
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02003744 case 0x80000001:
3745 if (nested)
3746 entry->ecx |= (1 << 2); /* Set SVM bit */
3747 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003748 case 0x8000000A:
3749 entry->eax = 1; /* SVM revision 1 */
3750 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3751 ASID emulation to nested SVM */
3752 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003753 entry->edx = 0; /* Per default do not support any
3754 additional features */
3755
3756 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003757 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02003758 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003759
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02003760 /* Support NPT for the guest if enabled */
3761 if (npt_enabled)
3762 entry->edx |= SVM_FEATURE_NPT;
3763
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003764 break;
3765 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003766}
3767
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003768static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003769 { SVM_EXIT_READ_CR0, "read_cr0" },
3770 { SVM_EXIT_READ_CR3, "read_cr3" },
3771 { SVM_EXIT_READ_CR4, "read_cr4" },
3772 { SVM_EXIT_READ_CR8, "read_cr8" },
3773 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3774 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3775 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3776 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3777 { SVM_EXIT_READ_DR0, "read_dr0" },
3778 { SVM_EXIT_READ_DR1, "read_dr1" },
3779 { SVM_EXIT_READ_DR2, "read_dr2" },
3780 { SVM_EXIT_READ_DR3, "read_dr3" },
3781 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3782 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3783 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3784 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3785 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3786 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003787 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3788 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3789 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3790 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3791 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3792 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3793 { SVM_EXIT_INTR, "interrupt" },
3794 { SVM_EXIT_NMI, "nmi" },
3795 { SVM_EXIT_SMI, "smi" },
3796 { SVM_EXIT_INIT, "init" },
3797 { SVM_EXIT_VINTR, "vintr" },
3798 { SVM_EXIT_CPUID, "cpuid" },
3799 { SVM_EXIT_INVD, "invd" },
3800 { SVM_EXIT_HLT, "hlt" },
3801 { SVM_EXIT_INVLPG, "invlpg" },
3802 { SVM_EXIT_INVLPGA, "invlpga" },
3803 { SVM_EXIT_IOIO, "io" },
3804 { SVM_EXIT_MSR, "msr" },
3805 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3806 { SVM_EXIT_SHUTDOWN, "shutdown" },
3807 { SVM_EXIT_VMRUN, "vmrun" },
3808 { SVM_EXIT_VMMCALL, "hypercall" },
3809 { SVM_EXIT_VMLOAD, "vmload" },
3810 { SVM_EXIT_VMSAVE, "vmsave" },
3811 { SVM_EXIT_STGI, "stgi" },
3812 { SVM_EXIT_CLGI, "clgi" },
3813 { SVM_EXIT_SKINIT, "skinit" },
3814 { SVM_EXIT_WBINVD, "wbinvd" },
3815 { SVM_EXIT_MONITOR, "monitor" },
3816 { SVM_EXIT_MWAIT, "mwait" },
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003817 { SVM_EXIT_XSETBV, "xsetbv" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003818 { SVM_EXIT_NPF, "npf" },
3819 { -1, NULL }
3820};
3821
Sheng Yang17cc3932010-01-05 19:02:27 +08003822static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003823{
Sheng Yang17cc3932010-01-05 19:02:27 +08003824 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003825}
3826
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003827static bool svm_rdtscp_supported(void)
3828{
3829 return false;
3830}
3831
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003832static bool svm_has_wbinvd_exit(void)
3833{
3834 return true;
3835}
3836
Avi Kivity02daab22009-12-30 12:40:26 +02003837static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3838{
3839 struct vcpu_svm *svm = to_svm(vcpu);
3840
Joerg Roedel18c918c2010-11-30 18:03:59 +01003841 set_exception_intercept(svm, NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01003842 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003843}
3844
Joerg Roedel80612522011-04-04 12:39:33 +02003845#define PRE_EX(exit) { .exit_code = (exit), \
3846 .stage = X86_ICPT_PRE_EXCEPT, \
3847 .valid = true }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003848#define POST_EX(exit) { .exit_code = (exit), \
3849 .stage = X86_ICPT_POST_EXCEPT, \
3850 .valid = true }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003851#define POST_MEM(exit) { .exit_code = (exit), \
3852 .stage = X86_ICPT_POST_MEMACCESS, \
3853 .valid = true }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003854
3855static struct __x86_intercept {
3856 u32 exit_code;
3857 enum x86_intercept_stage stage;
3858 bool valid;
3859} x86_intercept_map[] = {
3860 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
3861 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
3862 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
3863 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
3864 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003865 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
3866 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003867 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
3868 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
3869 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
3870 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
3871 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
3872 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
3873 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
3874 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003875 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
3876 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
3877 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
3878 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
3879 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
3880 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
3881 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
3882 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003883 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
3884 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
3885 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02003886 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
3887 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
3888 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
3889 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
3890 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
3891 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
3892 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
3893 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
3894 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02003895 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
3896 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
3897 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
3898 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
3899 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
3900 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
3901 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02003902 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
3903 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
3904 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
3905 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003906};
3907
Joerg Roedel80612522011-04-04 12:39:33 +02003908#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003909#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003910#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003911
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003912static int svm_check_intercept(struct kvm_vcpu *vcpu,
3913 struct x86_instruction_info *info,
3914 enum x86_intercept_stage stage)
3915{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003916 struct vcpu_svm *svm = to_svm(vcpu);
3917 int vmexit, ret = X86EMUL_CONTINUE;
3918 struct __x86_intercept icpt_info;
3919 struct vmcb *vmcb = svm->vmcb;
3920
3921 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
3922 goto out;
3923
3924 icpt_info = x86_intercept_map[info->intercept];
3925
3926 if (!icpt_info.valid || stage != icpt_info.stage)
3927 goto out;
3928
3929 switch (icpt_info.exit_code) {
3930 case SVM_EXIT_READ_CR0:
3931 if (info->intercept == x86_intercept_cr_read)
3932 icpt_info.exit_code += info->modrm_reg;
3933 break;
3934 case SVM_EXIT_WRITE_CR0: {
3935 unsigned long cr0, val;
3936 u64 intercept;
3937
3938 if (info->intercept == x86_intercept_cr_write)
3939 icpt_info.exit_code += info->modrm_reg;
3940
3941 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0)
3942 break;
3943
3944 intercept = svm->nested.intercept;
3945
3946 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
3947 break;
3948
3949 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
3950 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
3951
3952 if (info->intercept == x86_intercept_lmsw) {
3953 cr0 &= 0xfUL;
3954 val &= 0xfUL;
3955 /* lmsw can't clear PE - catch this here */
3956 if (cr0 & X86_CR0_PE)
3957 val |= X86_CR0_PE;
3958 }
3959
3960 if (cr0 ^ val)
3961 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
3962
3963 break;
3964 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02003965 case SVM_EXIT_READ_DR0:
3966 case SVM_EXIT_WRITE_DR0:
3967 icpt_info.exit_code += info->modrm_reg;
3968 break;
Joerg Roedel80612522011-04-04 12:39:33 +02003969 case SVM_EXIT_MSR:
3970 if (info->intercept == x86_intercept_wrmsr)
3971 vmcb->control.exit_info_1 = 1;
3972 else
3973 vmcb->control.exit_info_1 = 0;
3974 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02003975 case SVM_EXIT_PAUSE:
3976 /*
3977 * We get this for NOP only, but pause
3978 * is rep not, check this here
3979 */
3980 if (info->rep_prefix != REPE_PREFIX)
3981 goto out;
Joerg Roedelf6511932011-04-04 12:39:35 +02003982 case SVM_EXIT_IOIO: {
3983 u64 exit_info;
3984 u32 bytes;
3985
3986 exit_info = (vcpu->arch.regs[VCPU_REGS_RDX] & 0xffff) << 16;
3987
3988 if (info->intercept == x86_intercept_in ||
3989 info->intercept == x86_intercept_ins) {
3990 exit_info |= SVM_IOIO_TYPE_MASK;
3991 bytes = info->src_bytes;
3992 } else {
3993 bytes = info->dst_bytes;
3994 }
3995
3996 if (info->intercept == x86_intercept_outs ||
3997 info->intercept == x86_intercept_ins)
3998 exit_info |= SVM_IOIO_STR_MASK;
3999
4000 if (info->rep_prefix)
4001 exit_info |= SVM_IOIO_REP_MASK;
4002
4003 bytes = min(bytes, 4u);
4004
4005 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4006
4007 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4008
4009 vmcb->control.exit_info_1 = exit_info;
4010 vmcb->control.exit_info_2 = info->next_rip;
4011
4012 break;
4013 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004014 default:
4015 break;
4016 }
4017
4018 vmcb->control.next_rip = info->next_rip;
4019 vmcb->control.exit_code = icpt_info.exit_code;
4020 vmexit = nested_svm_exit_handled(svm);
4021
4022 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4023 : X86EMUL_CONTINUE;
4024
4025out:
4026 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004027}
4028
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004029static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004030 .cpu_has_kvm_support = has_svm,
4031 .disabled_by_bios = is_disabled,
4032 .hardware_setup = svm_hardware_setup,
4033 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004034 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004035 .hardware_enable = svm_hardware_enable,
4036 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004037 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004038
4039 .vcpu_create = svm_create_vcpu,
4040 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004041 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004042
Avi Kivity04d2cc72007-09-10 18:10:54 +03004043 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004044 .vcpu_load = svm_vcpu_load,
4045 .vcpu_put = svm_vcpu_put,
4046
4047 .set_guest_debug = svm_guest_debug,
4048 .get_msr = svm_get_msr,
4049 .set_msr = svm_set_msr,
4050 .get_segment_base = svm_get_segment_base,
4051 .get_segment = svm_get_segment,
4052 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004053 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004054 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004055 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004056 .decache_cr3 = svm_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004057 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004058 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059 .set_cr3 = svm_set_cr3,
4060 .set_cr4 = svm_set_cr4,
4061 .set_efer = svm_set_efer,
4062 .get_idt = svm_get_idt,
4063 .set_idt = svm_set_idt,
4064 .get_gdt = svm_get_gdt,
4065 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004066 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004067 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068 .get_rflags = svm_get_rflags,
4069 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02004070 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004071 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004072
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074
Avi Kivity6aa8b732006-12-10 02:21:36 -08004075 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004076 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004077 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004078 .set_interrupt_shadow = svm_set_interrupt_shadow,
4079 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004080 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004081 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004082 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004083 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004084 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004085 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004086 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004087 .get_nmi_mask = svm_get_nmi_mask,
4088 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004089 .enable_nmi_window = enable_nmi_window,
4090 .enable_irq_window = enable_irq_window,
4091 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02004092
4093 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004094 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004095 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004096
Avi Kivity586f9602010-11-18 13:09:54 +02004097 .get_exit_info = svm_get_exit_info,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004098 .exit_reasons_str = svm_exit_reasons_str,
Avi Kivity586f9602010-11-18 13:09:54 +02004099
Sheng Yang17cc3932010-01-05 19:02:27 +08004100 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004101
4102 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004103
4104 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004105
4106 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004107
4108 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004109
4110 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004111 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004112
4113 .set_tdp_cr3 = set_tdp_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004114
4115 .check_intercept = svm_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004116};
4117
4118static int __init svm_init(void)
4119{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004120 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004121 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004122}
4123
4124static void __exit svm_exit(void)
4125{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004126 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004127}
4128
4129module_init(svm_init)
4130module_exit(svm_exit)