blob: af5b9ea51965d62b5a9ce82e5b8f38253492b0ae [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.
Avi Kivity221d0592010-05-23 18:37:00 +03007 * Copyright 2010 Red Hat, Inc. and/or its affilates.
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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080034
Eduardo Habkost63d11422008-11-17 19:03:20 -020035#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030036#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020037
Avi Kivity4ecac3f2008-05-13 13:23:38 +030038#define __ex(x) __kvm_handle_fault_on_reboot(x)
39
Avi Kivity6aa8b732006-12-10 02:21:36 -080040MODULE_AUTHOR("Qumranet");
41MODULE_LICENSE("GPL");
42
43#define IOPM_ALLOC_ORDER 2
44#define MSRPM_ALLOC_ORDER 1
45
Avi Kivity6aa8b732006-12-10 02:21:36 -080046#define SEG_TYPE_LDT 2
47#define SEG_TYPE_BUSY_TSS16 3
48
Andre Przywara6bc31bd2010-04-11 23:07:28 +020049#define SVM_FEATURE_NPT (1 << 0)
50#define SVM_FEATURE_LBRV (1 << 1)
51#define SVM_FEATURE_SVML (1 << 2)
52#define SVM_FEATURE_NRIP (1 << 3)
53#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030054
Joerg Roedel410e4d52009-08-07 11:49:44 +020055#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
56#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
57#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
58
Joerg Roedel24e09cb2008-02-13 18:58:47 +010059#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
60
Joerg Roedel67ec6602010-05-17 14:43:35 +020061static bool erratum_383_found __read_mostly;
62
Avi Kivity6c8166a2009-05-31 18:15:37 +030063static const u32 host_save_user_msrs[] = {
64#ifdef CONFIG_X86_64
65 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
66 MSR_FS_BASE,
67#endif
68 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
69};
70
71#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
72
73struct kvm_vcpu;
74
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020075struct nested_state {
76 struct vmcb *hsave;
77 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010078 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020079 u64 vmcb;
80
81 /* These are the merged vectors */
82 u32 *msrpm;
83
84 /* gpa pointers to the real vectors */
85 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +010086 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020087
Joerg Roedelcd3ff652009-10-09 16:08:26 +020088 /* A VMEXIT is required but not yet emulated */
89 bool exit_required;
90
Joerg Roedelaad42c62009-08-07 11:49:34 +020091 /* cache for intercepts of the guest */
92 u16 intercept_cr_read;
93 u16 intercept_cr_write;
94 u16 intercept_dr_read;
95 u16 intercept_dr_write;
96 u32 intercept_exceptions;
97 u64 intercept;
98
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020099};
100
Joerg Roedel323c3d82010-03-01 15:34:37 +0100101#define MSRPM_OFFSETS 16
102static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
103
Avi Kivity6c8166a2009-05-31 18:15:37 +0300104struct vcpu_svm {
105 struct kvm_vcpu vcpu;
106 struct vmcb *vmcb;
107 unsigned long vmcb_pa;
108 struct svm_cpu_data *svm_data;
109 uint64_t asid_generation;
110 uint64_t sysenter_esp;
111 uint64_t sysenter_eip;
112
113 u64 next_rip;
114
115 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
116 u64 host_gs_base;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300117
118 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300119
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200120 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200121
122 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100123
124 unsigned int3_injected;
125 unsigned long int3_rip;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300126};
127
Joerg Roedel455716f2010-03-01 15:34:35 +0100128#define MSR_INVALID 0xffffffffU
129
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100130static struct svm_direct_access_msrs {
131 u32 index; /* Index of the MSR */
132 bool always; /* True if intercept is always on */
133} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400134 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100135 { .index = MSR_IA32_SYSENTER_CS, .always = true },
136#ifdef CONFIG_X86_64
137 { .index = MSR_GS_BASE, .always = true },
138 { .index = MSR_FS_BASE, .always = true },
139 { .index = MSR_KERNEL_GS_BASE, .always = true },
140 { .index = MSR_LSTAR, .always = true },
141 { .index = MSR_CSTAR, .always = true },
142 { .index = MSR_SYSCALL_MASK, .always = true },
143#endif
144 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
145 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
146 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
147 { .index = MSR_IA32_LASTINTTOIP, .always = false },
148 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149};
150
151/* enable NPT for AMD64 and X86 with PAE */
152#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
153static bool npt_enabled = true;
154#else
Joerg Roedele0231712010-02-24 18:59:10 +0100155static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156#endif
157static int npt = 1;
158
159module_param(npt, int, S_IRUGO);
160
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200161static int nested = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162module_param(nested, int, S_IRUGO);
163
164static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200165static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166
Joerg Roedel410e4d52009-08-07 11:49:44 +0200167static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100168static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
171 bool has_error_code, u32 error_code);
172
173static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
174{
175 return container_of(vcpu, struct vcpu_svm, vcpu);
176}
177
178static inline bool is_nested(struct vcpu_svm *svm)
179{
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200180 return svm->nested.vmcb;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181}
182
Joerg Roedel2af91942009-08-07 11:49:28 +0200183static inline void enable_gif(struct vcpu_svm *svm)
184{
185 svm->vcpu.arch.hflags |= HF_GIF_MASK;
186}
187
188static inline void disable_gif(struct vcpu_svm *svm)
189{
190 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
191}
192
193static inline bool gif_set(struct vcpu_svm *svm)
194{
195 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
196}
197
Avi Kivity6aa8b732006-12-10 02:21:36 -0800198static unsigned long iopm_base;
199
200struct kvm_ldttss_desc {
201 u16 limit0;
202 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100203 unsigned base1:8, type:5, dpl:2, p:1;
204 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205 u32 base3;
206 u32 zero1;
207} __attribute__((packed));
208
209struct svm_cpu_data {
210 int cpu;
211
Avi Kivity5008fdf2007-04-02 13:05:50 +0300212 u64 asid_generation;
213 u32 max_asid;
214 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215 struct kvm_ldttss_desc *tss_desc;
216
217 struct page *save_area;
218};
219
220static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300221static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222
223struct svm_init_data {
224 int cpu;
225 int r;
226};
227
228static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
229
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200230#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231#define MSRS_RANGE_SIZE 2048
232#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
233
Joerg Roedel455716f2010-03-01 15:34:35 +0100234static u32 svm_msrpm_offset(u32 msr)
235{
236 u32 offset;
237 int i;
238
239 for (i = 0; i < NUM_MSR_MAPS; i++) {
240 if (msr < msrpm_ranges[i] ||
241 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
242 continue;
243
244 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
245 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
246
247 /* Now we have the u8 offset - but need the u32 offset */
248 return offset / 4;
249 }
250
251 /* MSR not in any range */
252 return MSR_INVALID;
253}
254
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255#define MAX_INST_SIZE 15
256
Joerg Roedel80b77062007-03-30 17:02:14 +0300257static inline u32 svm_has(u32 feat)
258{
259 return svm_features & feat;
260}
261
Avi Kivity6aa8b732006-12-10 02:21:36 -0800262static inline void clgi(void)
263{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300264 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265}
266
267static inline void stgi(void)
268{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300269 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270}
271
272static inline void invlpga(unsigned long addr, u32 asid)
273{
Joerg Roedele0231712010-02-24 18:59:10 +0100274 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275}
276
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277static inline void force_new_asid(struct kvm_vcpu *vcpu)
278{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400279 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280}
281
282static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
283{
284 force_new_asid(vcpu);
285}
286
287static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
288{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000289 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100290 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600291 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292
Alexander Graf9962d032008-11-25 20:17:02 +0100293 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294}
295
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296static int is_external_interrupt(u32 info)
297{
298 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
299 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
300}
301
Glauber Costa2809f5d2009-05-12 16:21:05 -0400302static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
303{
304 struct vcpu_svm *svm = to_svm(vcpu);
305 u32 ret = 0;
306
307 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100308 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400309 return ret & mask;
310}
311
312static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
313{
314 struct vcpu_svm *svm = to_svm(vcpu);
315
316 if (mask == 0)
317 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
318 else
319 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
320
321}
322
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
324{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400325 struct vcpu_svm *svm = to_svm(vcpu);
326
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200327 if (svm->vmcb->control.next_rip != 0)
328 svm->next_rip = svm->vmcb->control.next_rip;
329
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400330 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300331 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300332 EMULATE_DONE)
333 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800334 return;
335 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300336 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
337 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
338 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300340 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400341 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800342}
343
Jan Kiszka116a4752010-02-23 17:47:54 +0100344static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200345 bool has_error_code, u32 error_code,
346 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100347{
348 struct vcpu_svm *svm = to_svm(vcpu);
349
Joerg Roedele0231712010-02-24 18:59:10 +0100350 /*
351 * If we are within a nested VM we'd better #VMEXIT and let the guest
352 * handle the exception
353 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200354 if (!reinject &&
355 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100356 return;
357
Jan Kiszka66b71382010-02-23 17:47:56 +0100358 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
359 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
360
361 /*
362 * For guest debugging where we have to reinject #BP if some
363 * INT3 is guest-owned:
364 * Emulate nRIP by moving RIP forward. Will fail if injection
365 * raises a fault that is not intercepted. Still better than
366 * failing in all cases.
367 */
368 skip_emulated_instruction(&svm->vcpu);
369 rip = kvm_rip_read(&svm->vcpu);
370 svm->int3_rip = rip + svm->vmcb->save.cs.base;
371 svm->int3_injected = rip - old_rip;
372 }
373
Jan Kiszka116a4752010-02-23 17:47:54 +0100374 svm->vmcb->control.event_inj = nr
375 | SVM_EVTINJ_VALID
376 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
377 | SVM_EVTINJ_TYPE_EXEPT;
378 svm->vmcb->control.event_inj_err = error_code;
379}
380
Joerg Roedel67ec6602010-05-17 14:43:35 +0200381static void svm_init_erratum_383(void)
382{
383 u32 low, high;
384 int err;
385 u64 val;
386
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200387 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200388 return;
389
390 /* Use _safe variants to not break nested virtualization */
391 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
392 if (err)
393 return;
394
395 val |= (1ULL << 47);
396
397 low = lower_32_bits(val);
398 high = upper_32_bits(val);
399
400 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
401
402 erratum_383_found = true;
403}
404
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405static int has_svm(void)
406{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200407 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800408
Eduardo Habkost63d11422008-11-17 19:03:20 -0200409 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800410 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 return 0;
412 }
413
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 return 1;
415}
416
417static void svm_hardware_disable(void *garbage)
418{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200419 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420}
421
Alexander Graf10474ae2009-09-15 11:37:46 +0200422static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423{
424
Tejun Heo0fe1e002009-10-29 22:34:14 +0900425 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200427 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428 struct desc_struct *gdt;
429 int me = raw_smp_processor_id();
430
Alexander Graf10474ae2009-09-15 11:37:46 +0200431 rdmsrl(MSR_EFER, efer);
432 if (efer & EFER_SVME)
433 return -EBUSY;
434
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000436 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
437 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200438 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900440 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441
Tejun Heo0fe1e002009-10-29 22:34:14 +0900442 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000443 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200445 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446 }
447
Tejun Heo0fe1e002009-10-29 22:34:14 +0900448 sd->asid_generation = 1;
449 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
450 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200452 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200453 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900454 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455
Alexander Graf9962d032008-11-25 20:17:02 +0100456 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457
Linus Torvaldsd0316552009-12-14 09:58:24 -0800458 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200459
Joerg Roedel67ec6602010-05-17 14:43:35 +0200460 svm_init_erratum_383();
461
Alexander Graf10474ae2009-09-15 11:37:46 +0200462 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463}
464
Joerg Roedel0da1db752008-07-02 16:02:11 +0200465static void svm_cpu_uninit(int cpu)
466{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900467 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200468
Tejun Heo0fe1e002009-10-29 22:34:14 +0900469 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200470 return;
471
472 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900473 __free_page(sd->save_area);
474 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200475}
476
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477static int svm_cpu_init(int cpu)
478{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900479 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 int r;
481
Tejun Heo0fe1e002009-10-29 22:34:14 +0900482 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
483 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900485 sd->cpu = cpu;
486 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900488 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489 goto err_1;
490
Tejun Heo0fe1e002009-10-29 22:34:14 +0900491 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492
493 return 0;
494
495err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900496 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 return r;
498
499}
500
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100501static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502{
503 int i;
504
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100505 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
506 if (direct_access_msrs[i].index == index)
507 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100509 return false;
510}
511
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512static void set_msr_interception(u32 *msrpm, unsigned msr,
513 int read, int write)
514{
Joerg Roedel455716f2010-03-01 15:34:35 +0100515 u8 bit_read, bit_write;
516 unsigned long tmp;
517 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100519 /*
520 * If this warning triggers extend the direct_access_msrs list at the
521 * beginning of the file
522 */
523 WARN_ON(!valid_msr_intercept(msr));
524
Joerg Roedel455716f2010-03-01 15:34:35 +0100525 offset = svm_msrpm_offset(msr);
526 bit_read = 2 * (msr & 0x0f);
527 bit_write = 2 * (msr & 0x0f) + 1;
528 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529
Joerg Roedel455716f2010-03-01 15:34:35 +0100530 BUG_ON(offset == MSR_INVALID);
531
532 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
533 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
534
535 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536}
537
Joerg Roedelf65c2292008-02-13 18:58:46 +0100538static void svm_vcpu_init_msrpm(u32 *msrpm)
539{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100540 int i;
541
Joerg Roedelf65c2292008-02-13 18:58:46 +0100542 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
543
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100544 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
545 if (!direct_access_msrs[i].always)
546 continue;
547
548 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
549 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100550}
551
Joerg Roedel323c3d82010-03-01 15:34:37 +0100552static void add_msr_offset(u32 offset)
553{
554 int i;
555
556 for (i = 0; i < MSRPM_OFFSETS; ++i) {
557
558 /* Offset already in list? */
559 if (msrpm_offsets[i] == offset)
560 return;
561
562 /* Slot used by another offset? */
563 if (msrpm_offsets[i] != MSR_INVALID)
564 continue;
565
566 /* Add offset to list */
567 msrpm_offsets[i] = offset;
568
569 return;
570 }
571
572 /*
573 * If this BUG triggers the msrpm_offsets table has an overflow. Just
574 * increase MSRPM_OFFSETS in this case.
575 */
576 BUG();
577}
578
579static void init_msrpm_offsets(void)
580{
581 int i;
582
583 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
584
585 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
586 u32 offset;
587
588 offset = svm_msrpm_offset(direct_access_msrs[i].index);
589 BUG_ON(offset == MSR_INVALID);
590
591 add_msr_offset(offset);
592 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593}
594
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100595static void svm_enable_lbrv(struct vcpu_svm *svm)
596{
597 u32 *msrpm = svm->msrpm;
598
599 svm->vmcb->control.lbr_ctl = 1;
600 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
601 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
602 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
603 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
604}
605
606static void svm_disable_lbrv(struct vcpu_svm *svm)
607{
608 u32 *msrpm = svm->msrpm;
609
610 svm->vmcb->control.lbr_ctl = 0;
611 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
612 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
613 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
614 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
615}
616
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617static __init int svm_hardware_setup(void)
618{
619 int cpu;
620 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100621 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622 int r;
623
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
625
626 if (!iopm_pages)
627 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300628
629 iopm_va = page_address(iopm_pages);
630 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
632
Joerg Roedel323c3d82010-03-01 15:34:37 +0100633 init_msrpm_offsets();
634
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100635 if (boot_cpu_has(X86_FEATURE_NX))
636 kvm_enable_efer_bits(EFER_NX);
637
Alexander Graf1b2fd702009-02-02 16:23:51 +0100638 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
639 kvm_enable_efer_bits(EFER_FFXSR);
640
Alexander Graf236de052008-11-25 20:17:10 +0100641 if (nested) {
642 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200643 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100644 }
645
Zachary Amsden3230bb42009-09-29 11:38:37 -1000646 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647 r = svm_cpu_init(cpu);
648 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100649 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100651
652 svm_features = cpuid_edx(SVM_CPUID_FUNC);
653
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100654 if (!svm_has(SVM_FEATURE_NPT))
655 npt_enabled = false;
656
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100657 if (npt_enabled && !npt) {
658 printk(KERN_INFO "kvm: Nested Paging disabled\n");
659 npt_enabled = false;
660 }
661
Joerg Roedel18552672008-02-07 13:47:41 +0100662 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100663 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100664 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200665 } else
666 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100667
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668 return 0;
669
Joerg Roedelf65c2292008-02-13 18:58:46 +0100670err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
672 iopm_base = 0;
673 return r;
674}
675
676static __exit void svm_hardware_unsetup(void)
677{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200678 int cpu;
679
Zachary Amsden3230bb42009-09-29 11:38:37 -1000680 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200681 svm_cpu_uninit(cpu);
682
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100684 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685}
686
687static void init_seg(struct vmcb_seg *seg)
688{
689 seg->selector = 0;
690 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100691 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692 seg->limit = 0xffff;
693 seg->base = 0;
694}
695
696static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
697{
698 seg->selector = 0;
699 seg->attrib = SVM_SELECTOR_P_MASK | type;
700 seg->limit = 0xffff;
701 seg->base = 0;
702}
703
Joerg Roedele6101a92008-02-13 18:58:45 +0100704static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705{
Joerg Roedele6101a92008-02-13 18:58:45 +0100706 struct vmcb_control_area *control = &svm->vmcb->control;
707 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708
Avi Kivitybff78272010-01-07 13:16:08 +0200709 svm->vcpu.fpu_active = 1;
710
Joerg Roedele0231712010-02-24 18:59:10 +0100711 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200713 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714
Joerg Roedele0231712010-02-24 18:59:10 +0100715 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200717 INTERCEPT_CR4_MASK |
718 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719
Joerg Roedele0231712010-02-24 18:59:10 +0100720 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 INTERCEPT_DR1_MASK |
722 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100723 INTERCEPT_DR3_MASK |
724 INTERCEPT_DR4_MASK |
725 INTERCEPT_DR5_MASK |
726 INTERCEPT_DR6_MASK |
727 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728
Joerg Roedele0231712010-02-24 18:59:10 +0100729 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730 INTERCEPT_DR1_MASK |
731 INTERCEPT_DR2_MASK |
732 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100733 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100735 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736 INTERCEPT_DR7_MASK;
737
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500738 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200739 (1 << UD_VECTOR) |
740 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741
742
Joerg Roedele0231712010-02-24 18:59:10 +0100743 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200745 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200746 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200748 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300750 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 (1ULL << INTERCEPT_INVLPGA) |
752 (1ULL << INTERCEPT_IOIO_PROT) |
753 (1ULL << INTERCEPT_MSR_PROT) |
754 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800755 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756 (1ULL << INTERCEPT_VMRUN) |
757 (1ULL << INTERCEPT_VMMCALL) |
758 (1ULL << INTERCEPT_VMLOAD) |
759 (1ULL << INTERCEPT_VMSAVE) |
760 (1ULL << INTERCEPT_STGI) |
761 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100762 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200763 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100764 (1ULL << INTERCEPT_MONITOR) |
765 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766
767 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100768 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 control->int_ctl = V_INTR_MASKING_MASK;
770
771 init_seg(&save->es);
772 init_seg(&save->ss);
773 init_seg(&save->ds);
774 init_seg(&save->fs);
775 init_seg(&save->gs);
776
777 save->cs.selector = 0xf000;
778 /* Executable/Readable Code Segment */
779 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
780 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
781 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800782 /*
783 * cs.base should really be 0xffff0000, but vmx can't handle that, so
784 * be consistent with it.
785 *
786 * Replace when we have real mode working for vmx.
787 */
788 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789
790 save->gdtr.limit = 0xffff;
791 save->idtr.limit = 0xffff;
792
793 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
794 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
795
Alexander Graf9962d032008-11-25 20:17:02 +0100796 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400797 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 save->dr7 = 0x400;
799 save->rflags = 2;
800 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300801 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802
Joerg Roedele0231712010-02-24 18:59:10 +0100803 /*
804 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200805 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806 */
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200807 svm->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity49a9b072010-06-10 17:02:14 +0300808 (void)kvm_set_cr0(&svm->vcpu, svm->vcpu.arch.cr0);
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200809
Rusty Russell66aee912007-07-17 23:34:16 +1000810 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100812
813 if (npt_enabled) {
814 /* Setup VMCB for Nested Paging */
815 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300816 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
817 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100818 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200819 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
820 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100821 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100822 save->cr3 = 0;
823 save->cr4 = 0;
824 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300825 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100826
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200827 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200828 svm->vcpu.arch.hflags = 0;
829
Mark Langsdorf565d0992009-10-06 14:25:02 -0500830 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
831 control->pause_filter_count = 3000;
832 control->intercept |= (1ULL << INTERCEPT_PAUSE);
833 }
834
Joerg Roedel2af91942009-08-07 11:49:28 +0200835 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836}
837
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200838static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300839{
840 struct vcpu_svm *svm = to_svm(vcpu);
841
Joerg Roedele6101a92008-02-13 18:58:45 +0100842 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200843
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300844 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300845 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800846 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
847 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200848 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300849 vcpu->arch.regs_avail = ~0;
850 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200851
852 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300853}
854
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000855static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800856{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400857 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100859 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100860 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100861 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000862 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863
Rusty Russellc16f8622007-07-30 21:12:19 +1000864 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000865 if (!svm) {
866 err = -ENOMEM;
867 goto out;
868 }
869
870 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
871 if (err)
872 goto free_svm;
873
Joerg Roedelf65c2292008-02-13 18:58:46 +0100874 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900875 page = alloc_page(GFP_KERNEL);
876 if (!page)
877 goto uninit;
878
Joerg Roedelf65c2292008-02-13 18:58:46 +0100879 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
880 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900881 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100882
883 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
884 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900885 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100886
Alexander Grafb286d5d2008-11-25 20:17:05 +0100887 hsave_page = alloc_page(GFP_KERNEL);
888 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900889 goto free_page3;
890
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200891 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100892
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900893 svm->msrpm = page_address(msrpm_pages);
894 svm_vcpu_init_msrpm(svm->msrpm);
895
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200896 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +0100897 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100898
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400899 svm->vmcb = page_address(page);
900 clear_page(svm->vmcb);
901 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
902 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100903 init_vmcb(svm);
Zachary Amsden47008cd2010-08-19 22:07:19 -1000904 svm->vmcb->control.tsc_offset = 0-native_read_tsc();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400905
Jan Kiszka10ab25c2010-05-25 16:01:50 +0200906 err = fx_init(&svm->vcpu);
907 if (err)
908 goto free_page4;
909
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800910 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300911 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800912 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000914 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800915
Jan Kiszka10ab25c2010-05-25 16:01:50 +0200916free_page4:
917 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900918free_page3:
919 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
920free_page2:
921 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
922free_page1:
923 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000924uninit:
925 kvm_vcpu_uninit(&svm->vcpu);
926free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000927 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000928out:
929 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930}
931
932static void svm_free_vcpu(struct kvm_vcpu *vcpu)
933{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400934 struct vcpu_svm *svm = to_svm(vcpu);
935
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000936 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100937 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200938 __free_page(virt_to_page(svm->nested.hsave));
939 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000940 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000941 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942}
943
Avi Kivity15ad7142007-07-11 18:17:21 +0300944static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400946 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300947 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200948
Avi Kivity0cc50642007-03-25 12:07:27 +0200949 if (unlikely(cpu != vcpu->cpu)) {
Joerg Roedele935d482009-09-16 15:24:19 +0200950 u64 delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200951
Joerg Roedel953899b2009-12-14 12:22:20 +0100952 if (check_tsc_unstable()) {
953 /*
954 * Make sure that the guest sees a monotonically
955 * increasing TSC.
956 */
957 delta = vcpu->arch.host_tsc - native_read_tsc();
958 svm->vmcb->control.tsc_offset += delta;
959 if (is_nested(svm))
960 svm->nested.hsave->control.tsc_offset += delta;
961 }
Avi Kivity0cc50642007-03-25 12:07:27 +0200962 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300963 kvm_migrate_timers(vcpu);
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300964 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200965 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300966
967 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400968 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969}
970
971static void svm_vcpu_put(struct kvm_vcpu *vcpu)
972{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400973 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300974 int i;
975
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200976 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300977 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400978 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300979
Joerg Roedele935d482009-09-16 15:24:19 +0200980 vcpu->arch.host_tsc = native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981}
982
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
984{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400985 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986}
987
988static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
989{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400990 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991}
992
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300993static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
994{
995 switch (reg) {
996 case VCPU_EXREG_PDPTR:
997 BUG_ON(!npt_enabled);
998 load_pdptrs(vcpu, vcpu->arch.cr3);
999 break;
1000 default:
1001 BUG();
1002 }
1003}
1004
Alexander Graff0b85052008-11-25 20:17:01 +01001005static void svm_set_vintr(struct vcpu_svm *svm)
1006{
1007 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
1008}
1009
1010static void svm_clear_vintr(struct vcpu_svm *svm)
1011{
1012 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1013}
1014
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1016{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001017 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001018
1019 switch (seg) {
1020 case VCPU_SREG_CS: return &save->cs;
1021 case VCPU_SREG_DS: return &save->ds;
1022 case VCPU_SREG_ES: return &save->es;
1023 case VCPU_SREG_FS: return &save->fs;
1024 case VCPU_SREG_GS: return &save->gs;
1025 case VCPU_SREG_SS: return &save->ss;
1026 case VCPU_SREG_TR: return &save->tr;
1027 case VCPU_SREG_LDTR: return &save->ldtr;
1028 }
1029 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001030 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031}
1032
1033static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1034{
1035 struct vmcb_seg *s = svm_seg(vcpu, seg);
1036
1037 return s->base;
1038}
1039
1040static void svm_get_segment(struct kvm_vcpu *vcpu,
1041 struct kvm_segment *var, int seg)
1042{
1043 struct vmcb_seg *s = svm_seg(vcpu, seg);
1044
1045 var->base = s->base;
1046 var->limit = s->limit;
1047 var->selector = s->selector;
1048 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1049 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1050 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1051 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1052 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1053 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1054 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1055 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001056
Joerg Roedele0231712010-02-24 18:59:10 +01001057 /*
1058 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001059 * for cross vendor migration purposes by "not present"
1060 */
1061 var->unusable = !var->present || (var->type == 0);
1062
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001063 switch (seg) {
1064 case VCPU_SREG_CS:
1065 /*
1066 * SVM always stores 0 for the 'G' bit in the CS selector in
1067 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1068 * Intel's VMENTRY has a check on the 'G' bit.
1069 */
Amit Shah25022ac2008-10-27 09:04:17 +00001070 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001071 break;
1072 case VCPU_SREG_TR:
1073 /*
1074 * Work around a bug where the busy flag in the tr selector
1075 * isn't exposed
1076 */
Amit Shahc0d09822008-10-27 09:04:18 +00001077 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001078 break;
1079 case VCPU_SREG_DS:
1080 case VCPU_SREG_ES:
1081 case VCPU_SREG_FS:
1082 case VCPU_SREG_GS:
1083 /*
1084 * The accessed bit must always be set in the segment
1085 * descriptor cache, although it can be cleared in the
1086 * descriptor, the cached bit always remains at 1. Since
1087 * Intel has a check on this, set it here to support
1088 * cross-vendor migration.
1089 */
1090 if (!var->unusable)
1091 var->type |= 0x1;
1092 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001093 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001094 /*
1095 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001096 * descriptor is left as 1, although the whole segment has
1097 * been made unusable. Clear it here to pass an Intel VMX
1098 * entry check when cross vendor migrating.
1099 */
1100 if (var->unusable)
1101 var->db = 0;
1102 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001103 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104}
1105
Izik Eidus2e4d2652008-03-24 19:38:34 +02001106static int svm_get_cpl(struct kvm_vcpu *vcpu)
1107{
1108 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1109
1110 return save->cpl;
1111}
1112
Gleb Natapov89a27f42010-02-16 10:51:48 +02001113static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001115 struct vcpu_svm *svm = to_svm(vcpu);
1116
Gleb Natapov89a27f42010-02-16 10:51:48 +02001117 dt->size = svm->vmcb->save.idtr.limit;
1118 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119}
1120
Gleb Natapov89a27f42010-02-16 10:51:48 +02001121static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001123 struct vcpu_svm *svm = to_svm(vcpu);
1124
Gleb Natapov89a27f42010-02-16 10:51:48 +02001125 svm->vmcb->save.idtr.limit = dt->size;
1126 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127}
1128
Gleb Natapov89a27f42010-02-16 10:51:48 +02001129static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001131 struct vcpu_svm *svm = to_svm(vcpu);
1132
Gleb Natapov89a27f42010-02-16 10:51:48 +02001133 dt->size = svm->vmcb->save.gdtr.limit;
1134 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135}
1136
Gleb Natapov89a27f42010-02-16 10:51:48 +02001137static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001139 struct vcpu_svm *svm = to_svm(vcpu);
1140
Gleb Natapov89a27f42010-02-16 10:51:48 +02001141 svm->vmcb->save.gdtr.limit = dt->size;
1142 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143}
1144
Avi Kivitye8467fd2009-12-29 18:43:06 +02001145static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1146{
1147}
1148
Anthony Liguori25c4c272007-04-27 09:29:21 +03001149static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001150{
1151}
1152
Avi Kivityd2251572010-01-06 10:55:27 +02001153static void update_cr0_intercept(struct vcpu_svm *svm)
1154{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001155 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001156 ulong gcr0 = svm->vcpu.arch.cr0;
1157 u64 *hcr0 = &svm->vmcb->save.cr0;
1158
1159 if (!svm->vcpu.fpu_active)
1160 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1161 else
1162 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1163 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1164
1165
1166 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001167 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1168 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1169 if (is_nested(svm)) {
1170 struct vmcb *hsave = svm->nested.hsave;
1171
1172 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1173 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1174 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1175 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1176 }
Avi Kivityd2251572010-01-06 10:55:27 +02001177 } else {
1178 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1179 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001180 if (is_nested(svm)) {
1181 struct vmcb *hsave = svm->nested.hsave;
1182
1183 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1184 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1185 }
Avi Kivityd2251572010-01-06 10:55:27 +02001186 }
1187}
1188
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1190{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001191 struct vcpu_svm *svm = to_svm(vcpu);
1192
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001193 if (is_nested(svm)) {
1194 /*
1195 * We are here because we run in nested mode, the host kvm
1196 * intercepts cr0 writes but the l1 hypervisor does not.
1197 * But the L1 hypervisor may intercept selective cr0 writes.
1198 * This needs to be checked here.
1199 */
1200 unsigned long old, new;
1201
1202 /* Remove bits that would trigger a real cr0 write intercept */
1203 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1204 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1205
1206 if (old == new) {
1207 /* cr0 write with ts and mp unchanged */
1208 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
1209 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE)
1210 return;
1211 }
1212 }
1213
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001214#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001215 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001216 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001217 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001218 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 }
1220
Mike Dayd77c26f2007-10-08 09:02:08 -04001221 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001222 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001223 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224 }
1225 }
1226#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001227 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001228
1229 if (!npt_enabled)
1230 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001231
1232 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001233 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001234 /*
1235 * re-enable caching here because the QEMU bios
1236 * does not do it - this results in some delay at
1237 * reboot
1238 */
1239 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001240 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001241 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001242}
1243
1244static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1245{
Joerg Roedel6394b642008-04-09 14:15:29 +02001246 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001247 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1248
1249 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1250 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001251
Joerg Roedelec077262008-04-09 14:15:28 +02001252 vcpu->arch.cr4 = cr4;
1253 if (!npt_enabled)
1254 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001255 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001256 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257}
1258
1259static void svm_set_segment(struct kvm_vcpu *vcpu,
1260 struct kvm_segment *var, int seg)
1261{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001262 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001263 struct vmcb_seg *s = svm_seg(vcpu, seg);
1264
1265 s->base = var->base;
1266 s->limit = var->limit;
1267 s->selector = var->selector;
1268 if (var->unusable)
1269 s->attrib = 0;
1270 else {
1271 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1272 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1273 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1274 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1275 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1276 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1277 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1278 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1279 }
1280 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001281 svm->vmcb->save.cpl
1282 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1284
1285}
1286
Gleb Natapov44c11432009-05-11 13:35:52 +03001287static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001289 struct vcpu_svm *svm = to_svm(vcpu);
1290
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001291 svm->vmcb->control.intercept_exceptions &=
1292 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001293
Jan Kiszka6be7d302009-10-18 13:24:54 +02001294 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001295 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1296
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001297 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1298 if (vcpu->guest_debug &
1299 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1300 svm->vmcb->control.intercept_exceptions |=
1301 1 << DB_VECTOR;
1302 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1303 svm->vmcb->control.intercept_exceptions |=
1304 1 << BP_VECTOR;
1305 } else
1306 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001307}
1308
Jan Kiszka355be0b2009-10-03 00:31:21 +02001309static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001310{
Gleb Natapov44c11432009-05-11 13:35:52 +03001311 struct vcpu_svm *svm = to_svm(vcpu);
1312
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001313 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1314 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1315 else
1316 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1317
Jan Kiszka355be0b2009-10-03 00:31:21 +02001318 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319}
1320
1321static void load_host_msrs(struct kvm_vcpu *vcpu)
1322{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001323#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001324 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001325#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326}
1327
1328static void save_host_msrs(struct kvm_vcpu *vcpu)
1329{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001330#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001331 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001332#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333}
1334
Tejun Heo0fe1e002009-10-29 22:34:14 +09001335static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001337 if (sd->next_asid > sd->max_asid) {
1338 ++sd->asid_generation;
1339 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001340 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341 }
1342
Tejun Heo0fe1e002009-10-29 22:34:14 +09001343 svm->asid_generation = sd->asid_generation;
1344 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345}
1346
Gleb Natapov020df072010-04-13 10:05:23 +03001347static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001349 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001350
Gleb Natapov020df072010-04-13 10:05:23 +03001351 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352}
1353
Avi Kivity851ba692009-08-24 11:10:17 +03001354static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356 u64 fault_address;
1357 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001358
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001359 fault_address = svm->vmcb->control.exit_info_2;
1360 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001361
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001362 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001363 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1364 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001365 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366}
1367
Avi Kivity851ba692009-08-24 11:10:17 +03001368static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001369{
Avi Kivity851ba692009-08-24 11:10:17 +03001370 struct kvm_run *kvm_run = svm->vcpu.run;
1371
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001372 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001373 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001374 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001375 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1376 return 1;
1377 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001378
Jan Kiszka6be7d302009-10-18 13:24:54 +02001379 if (svm->nmi_singlestep) {
1380 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001381 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1382 svm->vmcb->save.rflags &=
1383 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1384 update_db_intercept(&svm->vcpu);
1385 }
1386
1387 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001388 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001389 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1390 kvm_run->debug.arch.pc =
1391 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1392 kvm_run->debug.arch.exception = DB_VECTOR;
1393 return 0;
1394 }
1395
1396 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001397}
1398
Avi Kivity851ba692009-08-24 11:10:17 +03001399static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001400{
Avi Kivity851ba692009-08-24 11:10:17 +03001401 struct kvm_run *kvm_run = svm->vcpu.run;
1402
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001403 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1404 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1405 kvm_run->debug.arch.exception = BP_VECTOR;
1406 return 0;
1407}
1408
Avi Kivity851ba692009-08-24 11:10:17 +03001409static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001410{
1411 int er;
1412
Avi Kivity851ba692009-08-24 11:10:17 +03001413 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001414 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001415 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001416 return 1;
1417}
1418
Avi Kivity6b52d182010-01-21 15:31:47 +02001419static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001420{
Avi Kivity6b52d182010-01-21 15:31:47 +02001421 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001422 u32 excp;
1423
1424 if (is_nested(svm)) {
1425 u32 h_excp, n_excp;
1426
1427 h_excp = svm->nested.hsave->control.intercept_exceptions;
1428 n_excp = svm->nested.intercept_exceptions;
1429 h_excp &= ~(1 << NM_VECTOR);
1430 excp = h_excp | n_excp;
1431 } else {
1432 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001433 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001434 }
1435
1436 svm->vmcb->control.intercept_exceptions = excp;
1437
Rusty Russelle756fc62007-07-30 20:07:08 +10001438 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001439 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001440}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001441
Avi Kivity6b52d182010-01-21 15:31:47 +02001442static int nm_interception(struct vcpu_svm *svm)
1443{
1444 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001445 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001446}
1447
Joerg Roedel67ec6602010-05-17 14:43:35 +02001448static bool is_erratum_383(void)
1449{
1450 int err, i;
1451 u64 value;
1452
1453 if (!erratum_383_found)
1454 return false;
1455
1456 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1457 if (err)
1458 return false;
1459
1460 /* Bit 62 may or may not be set for this mce */
1461 value &= ~(1ULL << 62);
1462
1463 if (value != 0xb600000000010015ULL)
1464 return false;
1465
1466 /* Clear MCi_STATUS registers */
1467 for (i = 0; i < 6; ++i)
1468 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1469
1470 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1471 if (!err) {
1472 u32 low, high;
1473
1474 value &= ~(1ULL << 2);
1475 low = lower_32_bits(value);
1476 high = upper_32_bits(value);
1477
1478 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1479 }
1480
1481 /* Flush tlb to evict multi-match entries */
1482 __flush_tlb_all();
1483
1484 return true;
1485}
1486
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001487static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001488{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001489 if (is_erratum_383()) {
1490 /*
1491 * Erratum 383 triggered. Guest state is corrupt so kill the
1492 * guest.
1493 */
1494 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1495
Avi Kivitya8eeb042010-05-10 12:34:53 +03001496 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001497
1498 return;
1499 }
1500
Joerg Roedel53371b52008-04-09 14:15:30 +02001501 /*
1502 * On an #MC intercept the MCE handler is not called automatically in
1503 * the host. So do it by hand here.
1504 */
1505 asm volatile (
1506 "int $0x12\n");
1507 /* not sure if we ever come back to this point */
1508
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001509 return;
1510}
1511
1512static int mc_interception(struct vcpu_svm *svm)
1513{
Joerg Roedel53371b52008-04-09 14:15:30 +02001514 return 1;
1515}
1516
Avi Kivity851ba692009-08-24 11:10:17 +03001517static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001518{
Avi Kivity851ba692009-08-24 11:10:17 +03001519 struct kvm_run *kvm_run = svm->vcpu.run;
1520
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001521 /*
1522 * VMCB is undefined after a SHUTDOWN intercept
1523 * so reinitialize it.
1524 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001525 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001526 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001527
1528 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1529 return 0;
1530}
1531
Avi Kivity851ba692009-08-24 11:10:17 +03001532static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001534 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001535 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001536 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001537 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538
Rusty Russelle756fc62007-07-30 20:07:08 +10001539 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001540 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001541 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001542 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001543 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001544
Avi Kivity039576c2007-03-20 12:46:50 +02001545 port = io_info >> 16;
1546 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001547 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001548 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001549
1550 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551}
1552
Avi Kivity851ba692009-08-24 11:10:17 +03001553static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001554{
1555 return 1;
1556}
1557
Avi Kivity851ba692009-08-24 11:10:17 +03001558static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001559{
1560 ++svm->vcpu.stat.irq_exits;
1561 return 1;
1562}
1563
Avi Kivity851ba692009-08-24 11:10:17 +03001564static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565{
1566 return 1;
1567}
1568
Avi Kivity851ba692009-08-24 11:10:17 +03001569static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001571 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001572 skip_emulated_instruction(&svm->vcpu);
1573 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001574}
1575
Avi Kivity851ba692009-08-24 11:10:17 +03001576static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001577{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001578 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001579 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001580 kvm_emulate_hypercall(&svm->vcpu);
1581 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001582}
1583
Alexander Grafc0725422008-11-25 20:17:03 +01001584static int nested_svm_check_permissions(struct vcpu_svm *svm)
1585{
Avi Kivityf6801df2010-01-21 15:31:50 +02001586 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001587 || !is_paging(&svm->vcpu)) {
1588 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1589 return 1;
1590 }
1591
1592 if (svm->vmcb->save.cpl) {
1593 kvm_inject_gp(&svm->vcpu, 0);
1594 return 1;
1595 }
1596
1597 return 0;
1598}
1599
Alexander Grafcf74a782008-11-25 20:17:08 +01001600static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1601 bool has_error_code, u32 error_code)
1602{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001603 int vmexit;
1604
Joerg Roedel0295ad72009-08-07 11:49:37 +02001605 if (!is_nested(svm))
1606 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001607
Joerg Roedel0295ad72009-08-07 11:49:37 +02001608 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1609 svm->vmcb->control.exit_code_hi = 0;
1610 svm->vmcb->control.exit_info_1 = error_code;
1611 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1612
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001613 vmexit = nested_svm_intercept(svm);
1614 if (vmexit == NESTED_EXIT_DONE)
1615 svm->nested.exit_required = true;
1616
1617 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001618}
1619
Joerg Roedel8fe54652010-02-19 16:23:01 +01001620/* This function returns true if it is save to enable the irq window */
1621static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001622{
Joerg Roedel26666952009-08-07 11:49:46 +02001623 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001624 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001625
Joerg Roedel26666952009-08-07 11:49:46 +02001626 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001627 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001628
Joerg Roedel26666952009-08-07 11:49:46 +02001629 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001630 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001631
Joerg Roedel197717d2010-02-24 18:59:19 +01001632 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1633 svm->vmcb->control.exit_info_1 = 0;
1634 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001635
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001636 if (svm->nested.intercept & 1ULL) {
1637 /*
1638 * The #vmexit can't be emulated here directly because this
1639 * code path runs with irqs and preemtion disabled. A
1640 * #vmexit emulation might sleep. Only signal request for
1641 * the #vmexit here.
1642 */
1643 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001644 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001645 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001646 }
1647
Joerg Roedel8fe54652010-02-19 16:23:01 +01001648 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001649}
1650
Joerg Roedel887f5002010-02-24 18:59:12 +01001651/* This function returns true if it is save to enable the nmi window */
1652static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1653{
1654 if (!is_nested(svm))
1655 return true;
1656
1657 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1658 return true;
1659
1660 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1661 svm->nested.exit_required = true;
1662
1663 return false;
1664}
1665
Joerg Roedel7597f122010-02-19 16:23:00 +01001666static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001667{
1668 struct page *page;
1669
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001670 might_sleep();
1671
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001672 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001673 if (is_error_page(page))
1674 goto error;
1675
Joerg Roedel7597f122010-02-19 16:23:00 +01001676 *_page = page;
1677
1678 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001679
1680error:
1681 kvm_release_page_clean(page);
1682 kvm_inject_gp(&svm->vcpu, 0);
1683
1684 return NULL;
1685}
1686
Joerg Roedel7597f122010-02-19 16:23:00 +01001687static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001688{
Joerg Roedel7597f122010-02-19 16:23:00 +01001689 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001690 kvm_release_page_dirty(page);
1691}
1692
Joerg Roedelce2ac082010-03-01 15:34:39 +01001693static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001694{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001695 unsigned port;
1696 u8 val, bit;
1697 u64 gpa;
1698
1699 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1700 return NESTED_EXIT_HOST;
1701
1702 port = svm->vmcb->control.exit_info_1 >> 16;
1703 gpa = svm->nested.vmcb_iopm + (port / 8);
1704 bit = port % 8;
1705 val = 0;
1706
1707 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1708 val &= (1 << bit);
1709
1710 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1711}
1712
Joerg Roedeld2477822010-03-01 15:34:34 +01001713static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001714{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001715 u32 offset, msr, value;
1716 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001717
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001718 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001719 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001720
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001721 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1722 offset = svm_msrpm_offset(msr);
1723 write = svm->vmcb->control.exit_info_1 & 1;
1724 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001725
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001726 if (offset == MSR_INVALID)
1727 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001728
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001729 /* Offset is in 32 bit units but need in 8 bit units */
1730 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001731
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001732 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1733 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001734
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001735 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001736}
1737
Joerg Roedel410e4d52009-08-07 11:49:44 +02001738static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001739{
Alexander Grafcf74a782008-11-25 20:17:08 +01001740 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001741
Joerg Roedel410e4d52009-08-07 11:49:44 +02001742 switch (exit_code) {
1743 case SVM_EXIT_INTR:
1744 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001745 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001746 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001747 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001748 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001749 if (npt_enabled)
1750 return NESTED_EXIT_HOST;
1751 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001752 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Joerg Roedele0231712010-02-24 18:59:10 +01001753 /* When we're shadowing, trap PFs */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001754 if (!npt_enabled)
1755 return NESTED_EXIT_HOST;
1756 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001757 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1758 nm_interception(svm);
1759 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001760 default:
1761 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001762 }
1763
Joerg Roedel410e4d52009-08-07 11:49:44 +02001764 return NESTED_EXIT_CONTINUE;
1765}
1766
1767/*
1768 * If this function returns true, this #vmexit was already handled
1769 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001770static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001771{
1772 u32 exit_code = svm->vmcb->control.exit_code;
1773 int vmexit = NESTED_EXIT_HOST;
1774
Alexander Grafcf74a782008-11-25 20:17:08 +01001775 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001776 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001777 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001778 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001779 case SVM_EXIT_IOIO:
1780 vmexit = nested_svm_intercept_ioio(svm);
1781 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001782 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1783 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001784 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001785 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001786 break;
1787 }
1788 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1789 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001790 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001791 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001792 break;
1793 }
1794 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1795 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001796 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001797 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001798 break;
1799 }
1800 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1801 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001802 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001803 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001804 break;
1805 }
1806 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1807 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001808 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001809 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001810 break;
1811 }
Joerg Roedel228070b2010-04-22 12:33:10 +02001812 case SVM_EXIT_ERR: {
1813 vmexit = NESTED_EXIT_DONE;
1814 break;
1815 }
Alexander Grafcf74a782008-11-25 20:17:08 +01001816 default: {
1817 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001818 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001819 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001820 }
1821 }
1822
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001823 return vmexit;
1824}
1825
1826static int nested_svm_exit_handled(struct vcpu_svm *svm)
1827{
1828 int vmexit;
1829
1830 vmexit = nested_svm_intercept(svm);
1831
1832 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001833 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001834
1835 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001836}
1837
Joerg Roedel0460a972009-08-07 11:49:31 +02001838static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1839{
1840 struct vmcb_control_area *dst = &dst_vmcb->control;
1841 struct vmcb_control_area *from = &from_vmcb->control;
1842
1843 dst->intercept_cr_read = from->intercept_cr_read;
1844 dst->intercept_cr_write = from->intercept_cr_write;
1845 dst->intercept_dr_read = from->intercept_dr_read;
1846 dst->intercept_dr_write = from->intercept_dr_write;
1847 dst->intercept_exceptions = from->intercept_exceptions;
1848 dst->intercept = from->intercept;
1849 dst->iopm_base_pa = from->iopm_base_pa;
1850 dst->msrpm_base_pa = from->msrpm_base_pa;
1851 dst->tsc_offset = from->tsc_offset;
1852 dst->asid = from->asid;
1853 dst->tlb_ctl = from->tlb_ctl;
1854 dst->int_ctl = from->int_ctl;
1855 dst->int_vector = from->int_vector;
1856 dst->int_state = from->int_state;
1857 dst->exit_code = from->exit_code;
1858 dst->exit_code_hi = from->exit_code_hi;
1859 dst->exit_info_1 = from->exit_info_1;
1860 dst->exit_info_2 = from->exit_info_2;
1861 dst->exit_int_info = from->exit_int_info;
1862 dst->exit_int_info_err = from->exit_int_info_err;
1863 dst->nested_ctl = from->nested_ctl;
1864 dst->event_inj = from->event_inj;
1865 dst->event_inj_err = from->event_inj_err;
1866 dst->nested_cr3 = from->nested_cr3;
1867 dst->lbr_ctl = from->lbr_ctl;
1868}
1869
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001870static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001871{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001872 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001873 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001874 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001875 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001876
Joerg Roedel17897f32009-10-09 16:08:29 +02001877 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1878 vmcb->control.exit_info_1,
1879 vmcb->control.exit_info_2,
1880 vmcb->control.exit_int_info,
1881 vmcb->control.exit_int_info_err);
1882
Joerg Roedel7597f122010-02-19 16:23:00 +01001883 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001884 if (!nested_vmcb)
1885 return 1;
1886
Joerg Roedel06fc77722010-02-19 16:23:07 +01001887 /* Exit nested SVM mode */
1888 svm->nested.vmcb = 0;
1889
Alexander Grafcf74a782008-11-25 20:17:08 +01001890 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001891 disable_gif(svm);
1892
1893 nested_vmcb->save.es = vmcb->save.es;
1894 nested_vmcb->save.cs = vmcb->save.cs;
1895 nested_vmcb->save.ss = vmcb->save.ss;
1896 nested_vmcb->save.ds = vmcb->save.ds;
1897 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1898 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02001899 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001900 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02001901 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001902 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001903 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001904 nested_vmcb->save.rflags = vmcb->save.rflags;
1905 nested_vmcb->save.rip = vmcb->save.rip;
1906 nested_vmcb->save.rsp = vmcb->save.rsp;
1907 nested_vmcb->save.rax = vmcb->save.rax;
1908 nested_vmcb->save.dr7 = vmcb->save.dr7;
1909 nested_vmcb->save.dr6 = vmcb->save.dr6;
1910 nested_vmcb->save.cpl = vmcb->save.cpl;
1911
1912 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1913 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1914 nested_vmcb->control.int_state = vmcb->control.int_state;
1915 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1916 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1917 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1918 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1919 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1920 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02001921 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02001922
1923 /*
1924 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1925 * to make sure that we do not lose injected events. So check event_inj
1926 * here and copy it to exit_int_info if it is valid.
1927 * Exit_int_info and event_inj can't be both valid because the case
1928 * below only happens on a VMRUN instruction intercept which has
1929 * no valid exit_int_info set.
1930 */
1931 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1932 struct vmcb_control_area *nc = &nested_vmcb->control;
1933
1934 nc->exit_int_info = vmcb->control.event_inj;
1935 nc->exit_int_info_err = vmcb->control.event_inj_err;
1936 }
1937
Joerg Roedel33740e42009-08-07 11:49:29 +02001938 nested_vmcb->control.tlb_ctl = 0;
1939 nested_vmcb->control.event_inj = 0;
1940 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001941
1942 /* We always set V_INTR_MASKING and remember the old value in hflags */
1943 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1944 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1945
Alexander Grafcf74a782008-11-25 20:17:08 +01001946 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001947 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001948
Alexander Graf219b65d2009-06-15 15:21:25 +02001949 kvm_clear_exception_queue(&svm->vcpu);
1950 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01001951
1952 /* Restore selected save entries */
1953 svm->vmcb->save.es = hsave->save.es;
1954 svm->vmcb->save.cs = hsave->save.cs;
1955 svm->vmcb->save.ss = hsave->save.ss;
1956 svm->vmcb->save.ds = hsave->save.ds;
1957 svm->vmcb->save.gdtr = hsave->save.gdtr;
1958 svm->vmcb->save.idtr = hsave->save.idtr;
1959 svm->vmcb->save.rflags = hsave->save.rflags;
1960 svm_set_efer(&svm->vcpu, hsave->save.efer);
1961 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1962 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1963 if (npt_enabled) {
1964 svm->vmcb->save.cr3 = hsave->save.cr3;
1965 svm->vcpu.arch.cr3 = hsave->save.cr3;
1966 } else {
Avi Kivity23902182010-06-10 17:02:16 +03001967 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01001968 }
1969 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1970 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1971 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1972 svm->vmcb->save.dr7 = 0;
1973 svm->vmcb->save.cpl = 0;
1974 svm->vmcb->control.exit_int_info = 0;
1975
Joerg Roedel7597f122010-02-19 16:23:00 +01001976 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01001977
1978 kvm_mmu_reset_context(&svm->vcpu);
1979 kvm_mmu_load(&svm->vcpu);
1980
1981 return 0;
1982}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001983
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001984static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001985{
Joerg Roedel323c3d82010-03-01 15:34:37 +01001986 /*
1987 * This function merges the msr permission bitmaps of kvm and the
1988 * nested vmcb. It is omptimized in that it only merges the parts where
1989 * the kvm msr permission bitmap may contain zero bits
1990 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01001991 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001992
Joerg Roedel323c3d82010-03-01 15:34:37 +01001993 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
1994 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001995
Joerg Roedel323c3d82010-03-01 15:34:37 +01001996 for (i = 0; i < MSRPM_OFFSETS; i++) {
1997 u32 value, p;
1998 u64 offset;
1999
2000 if (msrpm_offsets[i] == 0xffffffff)
2001 break;
2002
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002003 p = msrpm_offsets[i];
2004 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002005
2006 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2007 return false;
2008
2009 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2010 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002011
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002012 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002013
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002014 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002015}
2016
Joerg Roedel52c65a302010-08-02 16:46:44 +02002017static bool nested_vmcb_checks(struct vmcb *vmcb)
2018{
2019 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2020 return false;
2021
Joerg Roedeldbe77582010-08-02 16:46:45 +02002022 if (vmcb->control.asid == 0)
2023 return false;
2024
Joerg Roedel52c65a302010-08-02 16:46:44 +02002025 return true;
2026}
2027
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002028static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002029{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002030 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002031 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002032 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002033 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002034 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002035
Joerg Roedel06fc77722010-02-19 16:23:07 +01002036 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002037
Joerg Roedel7597f122010-02-19 16:23:00 +01002038 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002039 if (!nested_vmcb)
2040 return false;
2041
Joerg Roedel52c65a302010-08-02 16:46:44 +02002042 if (!nested_vmcb_checks(nested_vmcb)) {
2043 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2044 nested_vmcb->control.exit_code_hi = 0;
2045 nested_vmcb->control.exit_info_1 = 0;
2046 nested_vmcb->control.exit_info_2 = 0;
2047
2048 nested_svm_unmap(page);
2049
2050 return false;
2051 }
2052
Joerg Roedelecf14052010-02-24 18:59:13 +01002053 trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002054 nested_vmcb->save.rip,
2055 nested_vmcb->control.int_ctl,
2056 nested_vmcb->control.event_inj,
2057 nested_vmcb->control.nested_ctl);
2058
Joerg Roedel2e554e82010-02-24 18:59:14 +01002059 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
2060 nested_vmcb->control.intercept_cr_write,
2061 nested_vmcb->control.intercept_exceptions,
2062 nested_vmcb->control.intercept);
2063
Alexander Graf3d6368e2008-11-25 20:17:07 +01002064 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002065 kvm_clear_exception_queue(&svm->vcpu);
2066 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002067
Joerg Roedele0231712010-02-24 18:59:10 +01002068 /*
2069 * Save the old vmcb, so we don't need to pick what we save, but can
2070 * restore everything when a VMEXIT occurs
2071 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002072 hsave->save.es = vmcb->save.es;
2073 hsave->save.cs = vmcb->save.cs;
2074 hsave->save.ss = vmcb->save.ss;
2075 hsave->save.ds = vmcb->save.ds;
2076 hsave->save.gdtr = vmcb->save.gdtr;
2077 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002078 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002079 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002080 hsave->save.cr4 = svm->vcpu.arch.cr4;
2081 hsave->save.rflags = vmcb->save.rflags;
2082 hsave->save.rip = svm->next_rip;
2083 hsave->save.rsp = vmcb->save.rsp;
2084 hsave->save.rax = vmcb->save.rax;
2085 if (npt_enabled)
2086 hsave->save.cr3 = vmcb->save.cr3;
2087 else
2088 hsave->save.cr3 = svm->vcpu.arch.cr3;
2089
Joerg Roedel0460a972009-08-07 11:49:31 +02002090 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002091
2092 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2093 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2094 else
2095 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2096
2097 /* Load the nested guest state */
2098 svm->vmcb->save.es = nested_vmcb->save.es;
2099 svm->vmcb->save.cs = nested_vmcb->save.cs;
2100 svm->vmcb->save.ss = nested_vmcb->save.ss;
2101 svm->vmcb->save.ds = nested_vmcb->save.ds;
2102 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2103 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2104 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2105 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2106 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2107 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2108 if (npt_enabled) {
2109 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2110 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002111 } else
Avi Kivity23902182010-06-10 17:02:16 +03002112 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002113
2114 /* Guest paging mode is active - reset mmu */
2115 kvm_mmu_reset_context(&svm->vcpu);
2116
Joerg Roedeldefbba52009-08-07 11:49:30 +02002117 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002118 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2119 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2120 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002121
Alexander Graf3d6368e2008-11-25 20:17:07 +01002122 /* In case we don't even reach vcpu_run, the fields are not updated */
2123 svm->vmcb->save.rax = nested_vmcb->save.rax;
2124 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2125 svm->vmcb->save.rip = nested_vmcb->save.rip;
2126 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2127 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2128 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2129
Joerg Roedelf7138532010-03-01 15:34:40 +01002130 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002131 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002132
Joerg Roedelaad42c62009-08-07 11:49:34 +02002133 /* cache intercepts */
2134 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2135 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2136 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2137 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2138 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2139 svm->nested.intercept = nested_vmcb->control.intercept;
2140
Alexander Graf3d6368e2008-11-25 20:17:07 +01002141 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002142 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002143 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2144 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2145 else
2146 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2147
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002148 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2149 /* We only want the cr8 intercept bits of the guest */
2150 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2151 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2152 }
2153
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002154 /* We don't want to see VMMCALLs from a nested guest */
2155 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMMCALL);
2156
Joerg Roedele0231712010-02-24 18:59:10 +01002157 /*
2158 * We don't want a nested guest to be more powerful than the guest, so
2159 * all intercepts are ORed
2160 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002161 svm->vmcb->control.intercept_cr_read |=
2162 nested_vmcb->control.intercept_cr_read;
2163 svm->vmcb->control.intercept_cr_write |=
2164 nested_vmcb->control.intercept_cr_write;
2165 svm->vmcb->control.intercept_dr_read |=
2166 nested_vmcb->control.intercept_dr_read;
2167 svm->vmcb->control.intercept_dr_write |=
2168 nested_vmcb->control.intercept_dr_write;
2169 svm->vmcb->control.intercept_exceptions |=
2170 nested_vmcb->control.intercept_exceptions;
2171
2172 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2173
2174 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002175 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2176 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2177 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002178 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2179 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2180
Joerg Roedel7597f122010-02-19 16:23:00 +01002181 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002182
Joerg Roedel06fc77722010-02-19 16:23:07 +01002183 /* nested_vmcb is our indicator if nested SVM is activated */
2184 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002185
Joerg Roedel2af91942009-08-07 11:49:28 +02002186 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002187
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002188 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002189}
2190
Joerg Roedel9966bf62009-08-07 11:49:40 +02002191static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002192{
2193 to_vmcb->save.fs = from_vmcb->save.fs;
2194 to_vmcb->save.gs = from_vmcb->save.gs;
2195 to_vmcb->save.tr = from_vmcb->save.tr;
2196 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2197 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2198 to_vmcb->save.star = from_vmcb->save.star;
2199 to_vmcb->save.lstar = from_vmcb->save.lstar;
2200 to_vmcb->save.cstar = from_vmcb->save.cstar;
2201 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2202 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2203 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2204 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002205}
2206
Avi Kivity851ba692009-08-24 11:10:17 +03002207static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002208{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002209 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002210 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002211
Alexander Graf55426752008-11-25 20:17:06 +01002212 if (nested_svm_check_permissions(svm))
2213 return 1;
2214
2215 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2216 skip_emulated_instruction(&svm->vcpu);
2217
Joerg Roedel7597f122010-02-19 16:23:00 +01002218 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002219 if (!nested_vmcb)
2220 return 1;
2221
2222 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002223 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002224
2225 return 1;
2226}
2227
Avi Kivity851ba692009-08-24 11:10:17 +03002228static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002229{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002230 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002231 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002232
Alexander Graf55426752008-11-25 20:17:06 +01002233 if (nested_svm_check_permissions(svm))
2234 return 1;
2235
2236 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2237 skip_emulated_instruction(&svm->vcpu);
2238
Joerg Roedel7597f122010-02-19 16:23:00 +01002239 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002240 if (!nested_vmcb)
2241 return 1;
2242
2243 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002244 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002245
2246 return 1;
2247}
2248
Avi Kivity851ba692009-08-24 11:10:17 +03002249static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002250{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002251 if (nested_svm_check_permissions(svm))
2252 return 1;
2253
2254 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2255 skip_emulated_instruction(&svm->vcpu);
2256
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002257 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002258 return 1;
2259
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002260 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002261 goto failed;
2262
2263 return 1;
2264
2265failed:
2266
2267 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2268 svm->vmcb->control.exit_code_hi = 0;
2269 svm->vmcb->control.exit_info_1 = 0;
2270 svm->vmcb->control.exit_info_2 = 0;
2271
2272 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002273
2274 return 1;
2275}
2276
Avi Kivity851ba692009-08-24 11:10:17 +03002277static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002278{
2279 if (nested_svm_check_permissions(svm))
2280 return 1;
2281
2282 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2283 skip_emulated_instruction(&svm->vcpu);
2284
Joerg Roedel2af91942009-08-07 11:49:28 +02002285 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002286
2287 return 1;
2288}
2289
Avi Kivity851ba692009-08-24 11:10:17 +03002290static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002291{
2292 if (nested_svm_check_permissions(svm))
2293 return 1;
2294
2295 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2296 skip_emulated_instruction(&svm->vcpu);
2297
Joerg Roedel2af91942009-08-07 11:49:28 +02002298 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002299
2300 /* After a CLGI no interrupts should come */
2301 svm_clear_vintr(svm);
2302 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2303
2304 return 1;
2305}
2306
Avi Kivity851ba692009-08-24 11:10:17 +03002307static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002308{
2309 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002310
Joerg Roedelec1ff792009-10-09 16:08:31 +02002311 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2312 vcpu->arch.regs[VCPU_REGS_RAX]);
2313
Alexander Grafff092382009-06-15 15:21:24 +02002314 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2315 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2316
2317 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2318 skip_emulated_instruction(&svm->vcpu);
2319 return 1;
2320}
2321
Joerg Roedel532a46b2009-10-09 16:08:32 +02002322static int skinit_interception(struct vcpu_svm *svm)
2323{
2324 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2325
2326 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2327 return 1;
2328}
2329
Avi Kivity851ba692009-08-24 11:10:17 +03002330static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002332 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 return 1;
2334}
2335
Avi Kivity851ba692009-08-24 11:10:17 +03002336static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337{
Izik Eidus37817f22008-03-24 23:14:53 +02002338 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002339 int reason;
2340 int int_type = svm->vmcb->control.exit_int_info &
2341 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002342 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002343 uint32_t type =
2344 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2345 uint32_t idt_v =
2346 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002347 bool has_error_code = false;
2348 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002349
2350 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002351
Izik Eidus37817f22008-03-24 23:14:53 +02002352 if (svm->vmcb->control.exit_info_2 &
2353 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002354 reason = TASK_SWITCH_IRET;
2355 else if (svm->vmcb->control.exit_info_2 &
2356 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2357 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002358 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002359 reason = TASK_SWITCH_GATE;
2360 else
2361 reason = TASK_SWITCH_CALL;
2362
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002363 if (reason == TASK_SWITCH_GATE) {
2364 switch (type) {
2365 case SVM_EXITINTINFO_TYPE_NMI:
2366 svm->vcpu.arch.nmi_injected = false;
2367 break;
2368 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002369 if (svm->vmcb->control.exit_info_2 &
2370 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2371 has_error_code = true;
2372 error_code =
2373 (u32)svm->vmcb->control.exit_info_2;
2374 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002375 kvm_clear_exception_queue(&svm->vcpu);
2376 break;
2377 case SVM_EXITINTINFO_TYPE_INTR:
2378 kvm_clear_interrupt_queue(&svm->vcpu);
2379 break;
2380 default:
2381 break;
2382 }
2383 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002384
Gleb Natapov8317c292009-04-12 13:37:02 +03002385 if (reason != TASK_SWITCH_GATE ||
2386 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2387 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002388 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2389 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002390
Gleb Natapovacb54512010-04-15 21:03:50 +03002391 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2392 has_error_code, error_code) == EMULATE_FAIL) {
2393 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2394 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2395 svm->vcpu.run->internal.ndata = 0;
2396 return 0;
2397 }
2398 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399}
2400
Avi Kivity851ba692009-08-24 11:10:17 +03002401static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002403 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002404 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002405 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406}
2407
Avi Kivity851ba692009-08-24 11:10:17 +03002408static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002409{
2410 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002411 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002412 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002413 return 1;
2414}
2415
Avi Kivity851ba692009-08-24 11:10:17 +03002416static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002417{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002418 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002419}
2420
Avi Kivity851ba692009-08-24 11:10:17 +03002421static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002423 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424}
2425
Avi Kivity851ba692009-08-24 11:10:17 +03002426static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002427{
Avi Kivity851ba692009-08-24 11:10:17 +03002428 struct kvm_run *kvm_run = svm->vcpu.run;
2429
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002430 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2431 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002432 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002433 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2434 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002435 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002436 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002437 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2438 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002439 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2440 return 0;
2441}
2442
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2444{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002445 struct vcpu_svm *svm = to_svm(vcpu);
2446
Avi Kivity6aa8b732006-12-10 02:21:36 -08002447 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302448 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002449 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450
Joerg Roedel20824f32009-09-16 15:24:18 +02002451 if (is_nested(svm))
2452 tsc_offset = svm->nested.hsave->control.tsc_offset;
2453 else
2454 tsc_offset = svm->vmcb->control.tsc_offset;
2455
2456 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457 break;
2458 }
Brian Gerst8c065852010-07-17 09:03:26 -04002459 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002460 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002462#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002464 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465 break;
2466 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002467 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468 break;
2469 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002470 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471 break;
2472 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002473 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002474 break;
2475#endif
2476 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002477 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478 break;
2479 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002480 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481 break;
2482 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002483 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002485 /*
2486 * Nobody will change the following 5 values in the VMCB so we can
2487 * safely return them on rdmsr. They will always be 0 until LBRV is
2488 * implemented.
2489 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002490 case MSR_IA32_DEBUGCTLMSR:
2491 *data = svm->vmcb->save.dbgctl;
2492 break;
2493 case MSR_IA32_LASTBRANCHFROMIP:
2494 *data = svm->vmcb->save.br_from;
2495 break;
2496 case MSR_IA32_LASTBRANCHTOIP:
2497 *data = svm->vmcb->save.br_to;
2498 break;
2499 case MSR_IA32_LASTINTFROMIP:
2500 *data = svm->vmcb->save.last_excp_from;
2501 break;
2502 case MSR_IA32_LASTINTTOIP:
2503 *data = svm->vmcb->save.last_excp_to;
2504 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002505 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002506 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002507 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002508 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002509 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002510 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002511 case MSR_IA32_UCODE_REV:
2512 *data = 0x01000065;
2513 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002515 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516 }
2517 return 0;
2518}
2519
Avi Kivity851ba692009-08-24 11:10:17 +03002520static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002522 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523 u64 data;
2524
Avi Kivity59200272010-01-25 19:47:02 +02002525 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2526 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002527 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002528 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002529 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002530
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002531 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002532 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002533 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002534 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535 }
2536 return 1;
2537}
2538
Joerg Roedel4a810182010-02-24 18:59:15 +01002539static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2540{
2541 struct vcpu_svm *svm = to_svm(vcpu);
2542 int svm_dis, chg_mask;
2543
2544 if (data & ~SVM_VM_CR_VALID_MASK)
2545 return 1;
2546
2547 chg_mask = SVM_VM_CR_VALID_MASK;
2548
2549 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2550 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2551
2552 svm->nested.vm_cr_msr &= ~chg_mask;
2553 svm->nested.vm_cr_msr |= (data & chg_mask);
2554
2555 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2556
2557 /* check for svm_disable while efer.svme is set */
2558 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2559 return 1;
2560
2561 return 0;
2562}
2563
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2565{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002566 struct vcpu_svm *svm = to_svm(vcpu);
2567
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302569 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002570 u64 tsc_offset = data - native_read_tsc();
2571 u64 g_tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572
Joerg Roedel20824f32009-09-16 15:24:18 +02002573 if (is_nested(svm)) {
2574 g_tsc_offset = svm->vmcb->control.tsc_offset -
2575 svm->nested.hsave->control.tsc_offset;
2576 svm->nested.hsave->control.tsc_offset = tsc_offset;
2577 }
2578
2579 svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
2580
Avi Kivity6aa8b732006-12-10 02:21:36 -08002581 break;
2582 }
Brian Gerst8c065852010-07-17 09:03:26 -04002583 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002584 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002586#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002588 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589 break;
2590 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002591 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592 break;
2593 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002594 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002595 break;
2596 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002597 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598 break;
2599#endif
2600 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002601 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 break;
2603 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002604 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002605 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606 break;
2607 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002608 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002609 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002611 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002612 if (!svm_has(SVM_FEATURE_LBRV)) {
2613 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002614 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002615 break;
2616 }
2617 if (data & DEBUGCTL_RESERVED_BITS)
2618 return 1;
2619
2620 svm->vmcb->save.dbgctl = data;
2621 if (data & (1ULL<<0))
2622 svm_enable_lbrv(svm);
2623 else
2624 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002625 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002626 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002627 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002628 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002629 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002630 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002631 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002632 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2633 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002635 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636 }
2637 return 0;
2638}
2639
Avi Kivity851ba692009-08-24 11:10:17 +03002640static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002642 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002643 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002644 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002645
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002646
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002647 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002648 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2649 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002650 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002651 } else {
2652 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002653 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002654 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655 return 1;
2656}
2657
Avi Kivity851ba692009-08-24 11:10:17 +03002658static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659{
Rusty Russelle756fc62007-07-30 20:07:08 +10002660 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002661 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 else
Avi Kivity851ba692009-08-24 11:10:17 +03002663 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664}
2665
Avi Kivity851ba692009-08-24 11:10:17 +03002666static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002667{
Avi Kivity851ba692009-08-24 11:10:17 +03002668 struct kvm_run *kvm_run = svm->vcpu.run;
2669
Alexander Graff0b85052008-11-25 20:17:01 +01002670 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002671 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002672 /*
2673 * If the user space waits to inject interrupts, exit as soon as
2674 * possible
2675 */
Gleb Natapov80618232009-04-21 17:44:56 +03002676 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2677 kvm_run->request_interrupt_window &&
2678 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002679 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002680 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2681 return 0;
2682 }
2683
2684 return 1;
2685}
2686
Mark Langsdorf565d0992009-10-06 14:25:02 -05002687static int pause_interception(struct vcpu_svm *svm)
2688{
2689 kvm_vcpu_on_spin(&(svm->vcpu));
2690 return 1;
2691}
2692
Avi Kivity851ba692009-08-24 11:10:17 +03002693static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002694 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2695 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2696 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2697 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002698 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002699 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2700 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2701 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2702 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2703 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2705 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2706 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002707 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2708 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2709 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2710 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2712 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2713 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2714 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002715 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002717 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002718 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002719 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2720 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002721 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002722 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2723 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2724 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2725 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002726 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 [SVM_EXIT_SMI] = nop_on_interception,
2728 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002729 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002731 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002732 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002733 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002735 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002736 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002737 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738 [SVM_EXIT_MSR] = msr_interception,
2739 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002740 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002741 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002742 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002743 [SVM_EXIT_VMLOAD] = vmload_interception,
2744 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002745 [SVM_EXIT_STGI] = stgi_interception,
2746 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002747 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002748 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002749 [SVM_EXIT_MONITOR] = invalid_op_interception,
2750 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002751 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752};
2753
Joerg Roedel3f10c842010-05-05 16:04:42 +02002754void dump_vmcb(struct kvm_vcpu *vcpu)
2755{
2756 struct vcpu_svm *svm = to_svm(vcpu);
2757 struct vmcb_control_area *control = &svm->vmcb->control;
2758 struct vmcb_save_area *save = &svm->vmcb->save;
2759
2760 pr_err("VMCB Control Area:\n");
2761 pr_err("cr_read: %04x\n", control->intercept_cr_read);
2762 pr_err("cr_write: %04x\n", control->intercept_cr_write);
2763 pr_err("dr_read: %04x\n", control->intercept_dr_read);
2764 pr_err("dr_write: %04x\n", control->intercept_dr_write);
2765 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2766 pr_err("intercepts: %016llx\n", control->intercept);
2767 pr_err("pause filter count: %d\n", control->pause_filter_count);
2768 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2769 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2770 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2771 pr_err("asid: %d\n", control->asid);
2772 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2773 pr_err("int_ctl: %08x\n", control->int_ctl);
2774 pr_err("int_vector: %08x\n", control->int_vector);
2775 pr_err("int_state: %08x\n", control->int_state);
2776 pr_err("exit_code: %08x\n", control->exit_code);
2777 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2778 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2779 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2780 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2781 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2782 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2783 pr_err("event_inj: %08x\n", control->event_inj);
2784 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2785 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
2786 pr_err("next_rip: %016llx\n", control->next_rip);
2787 pr_err("VMCB State Save Area:\n");
2788 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2789 save->es.selector, save->es.attrib,
2790 save->es.limit, save->es.base);
2791 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2792 save->cs.selector, save->cs.attrib,
2793 save->cs.limit, save->cs.base);
2794 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
2795 save->ss.selector, save->ss.attrib,
2796 save->ss.limit, save->ss.base);
2797 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
2798 save->ds.selector, save->ds.attrib,
2799 save->ds.limit, save->ds.base);
2800 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
2801 save->fs.selector, save->fs.attrib,
2802 save->fs.limit, save->fs.base);
2803 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
2804 save->gs.selector, save->gs.attrib,
2805 save->gs.limit, save->gs.base);
2806 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
2807 save->gdtr.selector, save->gdtr.attrib,
2808 save->gdtr.limit, save->gdtr.base);
2809 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
2810 save->ldtr.selector, save->ldtr.attrib,
2811 save->ldtr.limit, save->ldtr.base);
2812 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
2813 save->idtr.selector, save->idtr.attrib,
2814 save->idtr.limit, save->idtr.base);
2815 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
2816 save->tr.selector, save->tr.attrib,
2817 save->tr.limit, save->tr.base);
2818 pr_err("cpl: %d efer: %016llx\n",
2819 save->cpl, save->efer);
2820 pr_err("cr0: %016llx cr2: %016llx\n",
2821 save->cr0, save->cr2);
2822 pr_err("cr3: %016llx cr4: %016llx\n",
2823 save->cr3, save->cr4);
2824 pr_err("dr6: %016llx dr7: %016llx\n",
2825 save->dr6, save->dr7);
2826 pr_err("rip: %016llx rflags: %016llx\n",
2827 save->rip, save->rflags);
2828 pr_err("rsp: %016llx rax: %016llx\n",
2829 save->rsp, save->rax);
2830 pr_err("star: %016llx lstar: %016llx\n",
2831 save->star, save->lstar);
2832 pr_err("cstar: %016llx sfmask: %016llx\n",
2833 save->cstar, save->sfmask);
2834 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
2835 save->kernel_gs_base, save->sysenter_cs);
2836 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
2837 save->sysenter_esp, save->sysenter_eip);
2838 pr_err("gpat: %016llx dbgctl: %016llx\n",
2839 save->g_pat, save->dbgctl);
2840 pr_err("br_from: %016llx br_to: %016llx\n",
2841 save->br_from, save->br_to);
2842 pr_err("excp_from: %016llx excp_to: %016llx\n",
2843 save->last_excp_from, save->last_excp_to);
2844
2845}
2846
Avi Kivity851ba692009-08-24 11:10:17 +03002847static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002849 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002850 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002851 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852
Avi Kivity5bfd8b52010-03-11 10:50:44 +02002853 trace_kvm_exit(exit_code, vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002854
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002855 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
2856 vcpu->arch.cr0 = svm->vmcb->save.cr0;
2857 if (npt_enabled)
2858 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002860 if (unlikely(svm->nested.exit_required)) {
2861 nested_svm_vmexit(svm);
2862 svm->nested.exit_required = false;
2863
2864 return 1;
2865 }
2866
Alexander Grafcf74a782008-11-25 20:17:08 +01002867 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002868 int vmexit;
2869
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002870 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2871 svm->vmcb->control.exit_info_1,
2872 svm->vmcb->control.exit_info_2,
2873 svm->vmcb->control.exit_int_info,
2874 svm->vmcb->control.exit_int_info_err);
2875
Joerg Roedel410e4d52009-08-07 11:49:44 +02002876 vmexit = nested_svm_exit_special(svm);
2877
2878 if (vmexit == NESTED_EXIT_CONTINUE)
2879 vmexit = nested_svm_exit_handled(svm);
2880
2881 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002882 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002883 }
2884
Joerg Roedela5c38322009-08-07 11:49:32 +02002885 svm_complete_interrupts(svm);
2886
Avi Kivity04d2cc72007-09-10 18:10:54 +03002887 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2888 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2889 kvm_run->fail_entry.hardware_entry_failure_reason
2890 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02002891 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
2892 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03002893 return 0;
2894 }
2895
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002896 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002897 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002898 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2900 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002901 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902 exit_code);
2903
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002904 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002905 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002907 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 return 0;
2909 }
2910
Avi Kivity851ba692009-08-24 11:10:17 +03002911 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912}
2913
2914static void reload_tss(struct kvm_vcpu *vcpu)
2915{
2916 int cpu = raw_smp_processor_id();
2917
Tejun Heo0fe1e002009-10-29 22:34:14 +09002918 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2919 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920 load_TR_desc();
2921}
2922
Rusty Russelle756fc62007-07-30 20:07:08 +10002923static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924{
2925 int cpu = raw_smp_processor_id();
2926
Tejun Heo0fe1e002009-10-29 22:34:14 +09002927 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002929 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002930 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09002931 if (svm->asid_generation != sd->asid_generation)
2932 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002933}
2934
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002935static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2936{
2937 struct vcpu_svm *svm = to_svm(vcpu);
2938
2939 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2940 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002941 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002942 ++vcpu->stat.nmi_injections;
2943}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944
Eddie Dong85f455f2007-07-06 12:20:49 +03002945static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002946{
2947 struct vmcb_control_area *control;
2948
Rusty Russelle756fc62007-07-30 20:07:08 +10002949 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002950 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951 control->int_ctl &= ~V_INTR_PRIO_MASK;
2952 control->int_ctl |= V_IRQ_MASK |
2953 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2954}
2955
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002956static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002957{
2958 struct vcpu_svm *svm = to_svm(vcpu);
2959
Joerg Roedel2af91942009-08-07 11:49:28 +02002960 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01002961
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03002962 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
2963 ++vcpu->stat.irq_injections;
2964
Alexander Graf219b65d2009-06-15 15:21:25 +02002965 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2966 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002967}
2968
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002969static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2970{
2971 struct vcpu_svm *svm = to_svm(vcpu);
2972
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002973 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2974 return;
2975
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002976 if (irr == -1)
2977 return;
2978
2979 if (tpr >= irr)
2980 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2981}
2982
2983static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002984{
2985 struct vcpu_svm *svm = to_svm(vcpu);
2986 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02002987 int ret;
2988 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2989 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
2990 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
2991
2992 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002993}
2994
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002995static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
2996{
2997 struct vcpu_svm *svm = to_svm(vcpu);
2998
2999 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3000}
3001
3002static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3003{
3004 struct vcpu_svm *svm = to_svm(vcpu);
3005
3006 if (masked) {
3007 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003008 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003009 } else {
3010 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003011 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003012 }
3013}
3014
Gleb Natapov78646122009-03-23 12:12:11 +02003015static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3016{
3017 struct vcpu_svm *svm = to_svm(vcpu);
3018 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003019 int ret;
3020
3021 if (!gif_set(svm) ||
3022 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3023 return 0;
3024
3025 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3026
3027 if (is_nested(svm))
3028 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3029
3030 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003031}
3032
Gleb Natapov9222be12009-04-23 17:14:37 +03003033static void enable_irq_window(struct kvm_vcpu *vcpu)
3034{
Alexander Graf219b65d2009-06-15 15:21:25 +02003035 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003036
Joerg Roedele0231712010-02-24 18:59:10 +01003037 /*
3038 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3039 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3040 * get that intercept, this function will be called again though and
3041 * we'll get the vintr intercept.
3042 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003043 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003044 svm_set_vintr(svm);
3045 svm_inject_irq(svm, 0x0);
3046 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003047}
3048
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003049static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003051 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003052
Gleb Natapov44c11432009-05-11 13:35:52 +03003053 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3054 == HF_NMI_MASK)
3055 return; /* IRET will cause a vm exit */
3056
Joerg Roedele0231712010-02-24 18:59:10 +01003057 /*
3058 * Something prevents NMI from been injected. Single step over possible
3059 * problem (IRET or exception injection or interrupt shadow)
3060 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003061 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003062 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3063 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003064}
3065
Izik Eiduscbc94022007-10-25 00:29:55 +02003066static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3067{
3068 return 0;
3069}
3070
Avi Kivityd9e368d2007-06-07 19:18:30 +03003071static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3072{
3073 force_new_asid(vcpu);
3074}
3075
Avi Kivity04d2cc72007-09-10 18:10:54 +03003076static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3077{
3078}
3079
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003080static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3081{
3082 struct vcpu_svm *svm = to_svm(vcpu);
3083
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003084 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3085 return;
3086
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003087 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
3088 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003089 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003090 }
3091}
3092
Joerg Roedel649d6862008-04-16 16:51:15 +02003093static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3094{
3095 struct vcpu_svm *svm = to_svm(vcpu);
3096 u64 cr8;
3097
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003098 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3099 return;
3100
Joerg Roedel649d6862008-04-16 16:51:15 +02003101 cr8 = kvm_get_cr8(vcpu);
3102 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3103 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3104}
3105
Gleb Natapov9222be12009-04-23 17:14:37 +03003106static void svm_complete_interrupts(struct vcpu_svm *svm)
3107{
3108 u8 vector;
3109 int type;
3110 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003111 unsigned int3_injected = svm->int3_injected;
3112
3113 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003114
Gleb Natapov44c11432009-05-11 13:35:52 +03003115 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
3116 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3117
Gleb Natapov9222be12009-04-23 17:14:37 +03003118 svm->vcpu.arch.nmi_injected = false;
3119 kvm_clear_exception_queue(&svm->vcpu);
3120 kvm_clear_interrupt_queue(&svm->vcpu);
3121
3122 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3123 return;
3124
3125 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3126 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3127
3128 switch (type) {
3129 case SVM_EXITINTINFO_TYPE_NMI:
3130 svm->vcpu.arch.nmi_injected = true;
3131 break;
3132 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003133 /*
3134 * In case of software exceptions, do not reinject the vector,
3135 * but re-execute the instruction instead. Rewind RIP first
3136 * if we emulated INT3 before.
3137 */
3138 if (kvm_exception_is_soft(vector)) {
3139 if (vector == BP_VECTOR && int3_injected &&
3140 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3141 kvm_rip_write(&svm->vcpu,
3142 kvm_rip_read(&svm->vcpu) -
3143 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003144 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003145 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003146 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3147 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003148 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003149
3150 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003151 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003152 break;
3153 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003154 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003155 break;
3156 default:
3157 break;
3158 }
3159}
3160
Avi Kivity80e31d42008-07-14 14:44:59 +03003161#ifdef CONFIG_X86_64
3162#define R "r"
3163#else
3164#define R "e"
3165#endif
3166
Avi Kivity851ba692009-08-24 11:10:17 +03003167static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003169 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 u16 fs_selector;
3171 u16 gs_selector;
3172 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003173
Joerg Roedel2041a062010-04-22 12:33:08 +02003174 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3175 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3176 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3177
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003178 /*
3179 * A vmexit emulation is required before the vcpu can be executed
3180 * again.
3181 */
3182 if (unlikely(svm->nested.exit_required))
3183 return;
3184
Rusty Russelle756fc62007-07-30 20:07:08 +10003185 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186
Joerg Roedel649d6862008-04-16 16:51:15 +02003187 sync_lapic_to_cr8(vcpu);
3188
Avi Kivity6aa8b732006-12-10 02:21:36 -08003189 save_host_msrs(vcpu);
Avi Kivity9581d442010-10-19 16:46:55 +02003190 savesegment(fs, fs_selector);
3191 savesegment(gs, gs_selector);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003192 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003193 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003194 /* required for live migration with NPT */
3195 if (npt_enabled)
3196 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003197
Avi Kivity04d2cc72007-09-10 18:10:54 +03003198 clgi();
3199
3200 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003201
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003203 "push %%"R"bp; \n\t"
3204 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3205 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3206 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3207 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3208 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3209 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003210#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003211 "mov %c[r8](%[svm]), %%r8 \n\t"
3212 "mov %c[r9](%[svm]), %%r9 \n\t"
3213 "mov %c[r10](%[svm]), %%r10 \n\t"
3214 "mov %c[r11](%[svm]), %%r11 \n\t"
3215 "mov %c[r12](%[svm]), %%r12 \n\t"
3216 "mov %c[r13](%[svm]), %%r13 \n\t"
3217 "mov %c[r14](%[svm]), %%r14 \n\t"
3218 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219#endif
3220
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003222 "push %%"R"ax \n\t"
3223 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003224 __ex(SVM_VMLOAD) "\n\t"
3225 __ex(SVM_VMRUN) "\n\t"
3226 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003227 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228
3229 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003230 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3231 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3232 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3233 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3234 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3235 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003236#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003237 "mov %%r8, %c[r8](%[svm]) \n\t"
3238 "mov %%r9, %c[r9](%[svm]) \n\t"
3239 "mov %%r10, %c[r10](%[svm]) \n\t"
3240 "mov %%r11, %c[r11](%[svm]) \n\t"
3241 "mov %%r12, %c[r12](%[svm]) \n\t"
3242 "mov %%r13, %c[r13](%[svm]) \n\t"
3243 "mov %%r14, %c[r14](%[svm]) \n\t"
3244 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003246 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003247 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003248 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003250 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3251 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3252 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3253 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3254 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3255 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003256#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003257 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3258 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3259 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3260 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3261 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3262 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3263 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3264 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003266 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003267 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003268#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003269 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3270#endif
3271 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003273 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003274 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3275 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3276 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278 load_host_msrs(vcpu);
Avi Kivity9581d442010-10-19 16:46:55 +02003279 loadsegment(fs, fs_selector);
3280#ifdef CONFIG_X86_64
3281 load_gs_index(gs_selector);
3282 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
3283#else
3284 loadsegment(gs, gs_selector);
3285#endif
3286 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003287
3288 reload_tss(vcpu);
3289
Avi Kivity56ba47d2007-11-07 17:14:18 +02003290 local_irq_disable();
3291
3292 stgi();
3293
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003294 sync_cr8_to_lapic(vcpu);
3295
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003296 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003297
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003298 if (npt_enabled) {
3299 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3300 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3301 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003302
3303 /*
3304 * We need to handle MC intercepts here before the vcpu has a chance to
3305 * change the physical cpu
3306 */
3307 if (unlikely(svm->vmcb->control.exit_code ==
3308 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3309 svm_handle_mce(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310}
3311
Avi Kivity80e31d42008-07-14 14:44:59 +03003312#undef R
3313
Avi Kivity6aa8b732006-12-10 02:21:36 -08003314static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3315{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003316 struct vcpu_svm *svm = to_svm(vcpu);
3317
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003318 if (npt_enabled) {
3319 svm->vmcb->control.nested_cr3 = root;
3320 force_new_asid(vcpu);
3321 return;
3322 }
3323
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003324 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003325 force_new_asid(vcpu);
3326}
3327
Avi Kivity6aa8b732006-12-10 02:21:36 -08003328static int is_disabled(void)
3329{
Joerg Roedel6031a612007-06-22 12:29:50 +03003330 u64 vm_cr;
3331
3332 rdmsrl(MSR_VM_CR, vm_cr);
3333 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3334 return 1;
3335
Avi Kivity6aa8b732006-12-10 02:21:36 -08003336 return 0;
3337}
3338
Ingo Molnar102d8322007-02-19 14:37:47 +02003339static void
3340svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3341{
3342 /*
3343 * Patch in the VMMCALL instruction:
3344 */
3345 hypercall[0] = 0x0f;
3346 hypercall[1] = 0x01;
3347 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003348}
3349
Yang, Sheng002c7f72007-07-31 14:23:01 +03003350static void svm_check_processor_compat(void *rtn)
3351{
3352 *(int *)rtn = 0;
3353}
3354
Avi Kivity774ead32007-12-26 13:57:04 +02003355static bool svm_cpu_has_accelerated_tpr(void)
3356{
3357 return false;
3358}
3359
Sheng Yang67253af2008-04-25 10:20:22 +08003360static int get_npt_level(void)
3361{
3362#ifdef CONFIG_X86_64
3363 return PT64_ROOT_LEVEL;
3364#else
3365 return PT32E_ROOT_LEVEL;
3366#endif
3367}
3368
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003369static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003370{
3371 return 0;
3372}
3373
Sheng Yang0e851882009-12-18 16:48:46 +08003374static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3375{
3376}
3377
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003378static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3379{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003380 switch (func) {
3381 case 0x8000000A:
3382 entry->eax = 1; /* SVM revision 1 */
3383 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3384 ASID emulation to nested SVM */
3385 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003386 entry->edx = 0; /* Per default do not support any
3387 additional features */
3388
3389 /* Support next_rip if host supports it */
3390 if (svm_has(SVM_FEATURE_NRIP))
3391 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003392
3393 break;
3394 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003395}
3396
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003397static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003398 { SVM_EXIT_READ_CR0, "read_cr0" },
3399 { SVM_EXIT_READ_CR3, "read_cr3" },
3400 { SVM_EXIT_READ_CR4, "read_cr4" },
3401 { SVM_EXIT_READ_CR8, "read_cr8" },
3402 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3403 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3404 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3405 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3406 { SVM_EXIT_READ_DR0, "read_dr0" },
3407 { SVM_EXIT_READ_DR1, "read_dr1" },
3408 { SVM_EXIT_READ_DR2, "read_dr2" },
3409 { SVM_EXIT_READ_DR3, "read_dr3" },
3410 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3411 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3412 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3413 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3414 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3415 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003416 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3417 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3418 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3419 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3420 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3421 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3422 { SVM_EXIT_INTR, "interrupt" },
3423 { SVM_EXIT_NMI, "nmi" },
3424 { SVM_EXIT_SMI, "smi" },
3425 { SVM_EXIT_INIT, "init" },
3426 { SVM_EXIT_VINTR, "vintr" },
3427 { SVM_EXIT_CPUID, "cpuid" },
3428 { SVM_EXIT_INVD, "invd" },
3429 { SVM_EXIT_HLT, "hlt" },
3430 { SVM_EXIT_INVLPG, "invlpg" },
3431 { SVM_EXIT_INVLPGA, "invlpga" },
3432 { SVM_EXIT_IOIO, "io" },
3433 { SVM_EXIT_MSR, "msr" },
3434 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3435 { SVM_EXIT_SHUTDOWN, "shutdown" },
3436 { SVM_EXIT_VMRUN, "vmrun" },
3437 { SVM_EXIT_VMMCALL, "hypercall" },
3438 { SVM_EXIT_VMLOAD, "vmload" },
3439 { SVM_EXIT_VMSAVE, "vmsave" },
3440 { SVM_EXIT_STGI, "stgi" },
3441 { SVM_EXIT_CLGI, "clgi" },
3442 { SVM_EXIT_SKINIT, "skinit" },
3443 { SVM_EXIT_WBINVD, "wbinvd" },
3444 { SVM_EXIT_MONITOR, "monitor" },
3445 { SVM_EXIT_MWAIT, "mwait" },
3446 { SVM_EXIT_NPF, "npf" },
3447 { -1, NULL }
3448};
3449
Sheng Yang17cc3932010-01-05 19:02:27 +08003450static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003451{
Sheng Yang17cc3932010-01-05 19:02:27 +08003452 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003453}
3454
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003455static bool svm_rdtscp_supported(void)
3456{
3457 return false;
3458}
3459
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003460static bool svm_has_wbinvd_exit(void)
3461{
3462 return true;
3463}
3464
Avi Kivity02daab22009-12-30 12:40:26 +02003465static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3466{
3467 struct vcpu_svm *svm = to_svm(vcpu);
3468
Avi Kivity02daab22009-12-30 12:40:26 +02003469 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003470 if (is_nested(svm))
3471 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3472 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003473}
3474
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003475static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476 .cpu_has_kvm_support = has_svm,
3477 .disabled_by_bios = is_disabled,
3478 .hardware_setup = svm_hardware_setup,
3479 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003480 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481 .hardware_enable = svm_hardware_enable,
3482 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003483 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484
3485 .vcpu_create = svm_create_vcpu,
3486 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003487 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003488
Avi Kivity04d2cc72007-09-10 18:10:54 +03003489 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490 .vcpu_load = svm_vcpu_load,
3491 .vcpu_put = svm_vcpu_put,
3492
3493 .set_guest_debug = svm_guest_debug,
3494 .get_msr = svm_get_msr,
3495 .set_msr = svm_set_msr,
3496 .get_segment_base = svm_get_segment_base,
3497 .get_segment = svm_get_segment,
3498 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003499 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003500 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003501 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003502 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003503 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003504 .set_cr3 = svm_set_cr3,
3505 .set_cr4 = svm_set_cr4,
3506 .set_efer = svm_set_efer,
3507 .get_idt = svm_get_idt,
3508 .set_idt = svm_set_idt,
3509 .get_gdt = svm_get_gdt,
3510 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003511 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003512 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513 .get_rflags = svm_get_rflags,
3514 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003515 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003516 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003517
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003519
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003521 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003522 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003523 .set_interrupt_shadow = svm_set_interrupt_shadow,
3524 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003525 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003526 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003527 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003528 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003529 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003530 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003531 .get_nmi_mask = svm_get_nmi_mask,
3532 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003533 .enable_nmi_window = enable_nmi_window,
3534 .enable_irq_window = enable_irq_window,
3535 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003536
3537 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003538 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003539 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003540
3541 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003542 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003543
3544 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003545
3546 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003547
3548 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003549
3550 .has_wbinvd_exit = svm_has_wbinvd_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003551};
3552
3553static int __init svm_init(void)
3554{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003555 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003556 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003557}
3558
3559static void __exit svm_exit(void)
3560{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003561 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003562}
3563
3564module_init(svm_init)
3565module_exit(svm_exit)