blob: 69610c5d6dea3f0940296d4eaaa736434d1e3d32 [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.
7 *
8 * Authors:
9 * Yaniv Kamay <yaniv@qumranet.com>
10 * Avi Kivity <avi@qumranet.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
14 *
15 */
Avi Kivityedf88412007-12-16 11:02:48 +020016#include <linux/kvm_host.h>
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030020#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020021#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040022
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/vmalloc.h>
26#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030028#include <linux/ftrace_event.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029
Avi Kivitye4956062007-06-28 14:15:57 -040030#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080031
Eduardo Habkost63d11422008-11-17 19:03:20 -020032#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030033#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020034
Avi Kivity4ecac3f2008-05-13 13:23:38 +030035#define __ex(x) __kvm_handle_fault_on_reboot(x)
36
Avi Kivity6aa8b732006-12-10 02:21:36 -080037MODULE_AUTHOR("Qumranet");
38MODULE_LICENSE("GPL");
39
40#define IOPM_ALLOC_ORDER 2
41#define MSRPM_ALLOC_ORDER 1
42
Avi Kivity6aa8b732006-12-10 02:21:36 -080043#define SEG_TYPE_LDT 2
44#define SEG_TYPE_BUSY_TSS16 3
45
Joerg Roedel80b77062007-03-30 17:02:14 +030046#define SVM_FEATURE_NPT (1 << 0)
47#define SVM_FEATURE_LBRV (1 << 1)
Amit Shah94c935a12008-08-18 13:11:46 +030048#define SVM_FEATURE_SVML (1 << 2)
Joerg Roedel80b77062007-03-30 17:02:14 +030049
Joerg Roedel410e4d52009-08-07 11:49:44 +020050#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
51#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
52#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
53
Joerg Roedel24e09cb2008-02-13 18:58:47 +010054#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
55
Avi Kivity6c8166a2009-05-31 18:15:37 +030056static const u32 host_save_user_msrs[] = {
57#ifdef CONFIG_X86_64
58 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
59 MSR_FS_BASE,
60#endif
61 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
62};
63
64#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
65
66struct kvm_vcpu;
67
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020068struct nested_state {
69 struct vmcb *hsave;
70 u64 hsave_msr;
71 u64 vmcb;
72
73 /* These are the merged vectors */
74 u32 *msrpm;
75
76 /* gpa pointers to the real vectors */
77 u64 vmcb_msrpm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020078
Joerg Roedelcd3ff652009-10-09 16:08:26 +020079 /* A VMEXIT is required but not yet emulated */
80 bool exit_required;
81
Joerg Roedelaad42c62009-08-07 11:49:34 +020082 /* cache for intercepts of the guest */
83 u16 intercept_cr_read;
84 u16 intercept_cr_write;
85 u16 intercept_dr_read;
86 u16 intercept_dr_write;
87 u32 intercept_exceptions;
88 u64 intercept;
89
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020090};
91
Avi Kivity6c8166a2009-05-31 18:15:37 +030092struct vcpu_svm {
93 struct kvm_vcpu vcpu;
94 struct vmcb *vmcb;
95 unsigned long vmcb_pa;
96 struct svm_cpu_data *svm_data;
97 uint64_t asid_generation;
98 uint64_t sysenter_esp;
99 uint64_t sysenter_eip;
100
101 u64 next_rip;
102
103 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
104 u64 host_gs_base;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300105
106 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300107
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200108 struct nested_state nested;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300109};
110
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100111/* enable NPT for AMD64 and X86 with PAE */
112#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
113static bool npt_enabled = true;
114#else
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100115static bool npt_enabled = false;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100116#endif
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100117static int npt = 1;
118
119module_param(npt, int, S_IRUGO);
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100120
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200121static int nested = 1;
Alexander Graf236de052008-11-25 20:17:10 +0100122module_param(nested, int, S_IRUGO);
123
Joerg Roedel44874f82008-08-27 14:18:43 +0200124static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200125static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity04d2cc72007-09-10 18:10:54 +0300126
Joerg Roedel410e4d52009-08-07 11:49:44 +0200127static int nested_svm_exit_handled(struct vcpu_svm *svm);
Alexander Grafcf74a782008-11-25 20:17:08 +0100128static int nested_svm_vmexit(struct vcpu_svm *svm);
Alexander Grafcf74a782008-11-25 20:17:08 +0100129static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
130 bool has_error_code, u32 error_code);
131
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400132static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
133{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000134 return container_of(vcpu, struct vcpu_svm, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400135}
136
Alexander Graf3d6368e2008-11-25 20:17:07 +0100137static inline bool is_nested(struct vcpu_svm *svm)
138{
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200139 return svm->nested.vmcb;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100140}
141
Joerg Roedel2af91942009-08-07 11:49:28 +0200142static inline void enable_gif(struct vcpu_svm *svm)
143{
144 svm->vcpu.arch.hflags |= HF_GIF_MASK;
145}
146
147static inline void disable_gif(struct vcpu_svm *svm)
148{
149 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
150}
151
152static inline bool gif_set(struct vcpu_svm *svm)
153{
154 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
155}
156
Harvey Harrison4866d5e2008-02-19 10:32:02 -0800157static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800158
159struct kvm_ldttss_desc {
160 u16 limit0;
161 u16 base0;
162 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
163 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
164 u32 base3;
165 u32 zero1;
166} __attribute__((packed));
167
168struct svm_cpu_data {
169 int cpu;
170
Avi Kivity5008fdf2007-04-02 13:05:50 +0300171 u64 asid_generation;
172 u32 max_asid;
173 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800174 struct kvm_ldttss_desc *tss_desc;
175
176 struct page *save_area;
177};
178
179static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300180static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181
182struct svm_init_data {
183 int cpu;
184 int r;
185};
186
187static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
188
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200189#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800190#define MSRS_RANGE_SIZE 2048
191#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
192
193#define MAX_INST_SIZE 15
194
Joerg Roedel80b77062007-03-30 17:02:14 +0300195static inline u32 svm_has(u32 feat)
196{
197 return svm_features & feat;
198}
199
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200static inline void clgi(void)
201{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300202 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203}
204
205static inline void stgi(void)
206{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300207 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208}
209
210static inline void invlpga(unsigned long addr, u32 asid)
211{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300212 asm volatile (__ex(SVM_INVLPGA) :: "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213}
214
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215static inline void force_new_asid(struct kvm_vcpu *vcpu)
216{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400217 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218}
219
220static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
221{
222 force_new_asid(vcpu);
223}
224
225static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
226{
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100227 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600228 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229
Alexander Graf9962d032008-11-25 20:17:02 +0100230 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800231 vcpu->arch.shadow_efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232}
233
Avi Kivity298101d2007-11-25 13:41:11 +0200234static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
235 bool has_error_code, u32 error_code)
236{
237 struct vcpu_svm *svm = to_svm(vcpu);
238
Alexander Grafcf74a782008-11-25 20:17:08 +0100239 /* If we are within a nested VM we'd better #VMEXIT and let the
240 guest handle the exception */
241 if (nested_svm_check_exception(svm, nr, has_error_code, error_code))
242 return;
243
Avi Kivity298101d2007-11-25 13:41:11 +0200244 svm->vmcb->control.event_inj = nr
245 | SVM_EVTINJ_VALID
246 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
247 | SVM_EVTINJ_TYPE_EXEPT;
248 svm->vmcb->control.event_inj_err = error_code;
249}
250
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251static int is_external_interrupt(u32 info)
252{
253 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
254 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
255}
256
Glauber Costa2809f5d2009-05-12 16:21:05 -0400257static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
258{
259 struct vcpu_svm *svm = to_svm(vcpu);
260 u32 ret = 0;
261
262 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
263 ret |= X86_SHADOW_INT_STI | X86_SHADOW_INT_MOV_SS;
264 return ret & mask;
265}
266
267static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
268{
269 struct vcpu_svm *svm = to_svm(vcpu);
270
271 if (mask == 0)
272 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
273 else
274 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
275
276}
277
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
279{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400280 struct vcpu_svm *svm = to_svm(vcpu);
281
282 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300283 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300284 EMULATE_DONE)
285 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286 return;
287 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300288 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
289 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
290 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800291
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300292 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400293 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294}
295
296static int has_svm(void)
297{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200298 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800299
Eduardo Habkost63d11422008-11-17 19:03:20 -0200300 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800301 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800302 return 0;
303 }
304
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305 return 1;
306}
307
308static void svm_hardware_disable(void *garbage)
309{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200310 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800311}
312
Alexander Graf10474ae2009-09-15 11:37:46 +0200313static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800314{
315
316 struct svm_cpu_data *svm_data;
317 uint64_t efer;
Akinobu Mitab792c342009-07-19 00:00:01 +0900318 struct descriptor_table gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800319 struct desc_struct *gdt;
320 int me = raw_smp_processor_id();
321
Alexander Graf10474ae2009-09-15 11:37:46 +0200322 rdmsrl(MSR_EFER, efer);
323 if (efer & EFER_SVME)
324 return -EBUSY;
325
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000327 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
328 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200329 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800330 }
331 svm_data = per_cpu(svm_data, me);
332
333 if (!svm_data) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000334 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800335 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200336 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800337 }
338
339 svm_data->asid_generation = 1;
340 svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
341 svm_data->next_asid = svm_data->max_asid + 1;
342
Akinobu Mitab792c342009-07-19 00:00:01 +0900343 kvm_get_gdt(&gdt_descr);
344 gdt = (struct desc_struct *)gdt_descr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800345 svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
346
Alexander Graf9962d032008-11-25 20:17:02 +0100347 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348
349 wrmsrl(MSR_VM_HSAVE_PA,
350 page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200351
352 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353}
354
Joerg Roedel0da1db752008-07-02 16:02:11 +0200355static void svm_cpu_uninit(int cpu)
356{
357 struct svm_cpu_data *svm_data
358 = per_cpu(svm_data, raw_smp_processor_id());
359
360 if (!svm_data)
361 return;
362
363 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
364 __free_page(svm_data->save_area);
365 kfree(svm_data);
366}
367
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368static int svm_cpu_init(int cpu)
369{
370 struct svm_cpu_data *svm_data;
371 int r;
372
373 svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
374 if (!svm_data)
375 return -ENOMEM;
376 svm_data->cpu = cpu;
377 svm_data->save_area = alloc_page(GFP_KERNEL);
378 r = -ENOMEM;
379 if (!svm_data->save_area)
380 goto err_1;
381
382 per_cpu(svm_data, cpu) = svm_data;
383
384 return 0;
385
386err_1:
387 kfree(svm_data);
388 return r;
389
390}
391
Rusty Russellbfc733a2007-07-31 20:42:42 +1000392static void set_msr_interception(u32 *msrpm, unsigned msr,
393 int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800394{
395 int i;
396
397 for (i = 0; i < NUM_MSR_MAPS; i++) {
398 if (msr >= msrpm_ranges[i] &&
399 msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
400 u32 msr_offset = (i * MSRS_IN_RANGE + msr -
401 msrpm_ranges[i]) * 2;
402
403 u32 *base = msrpm + (msr_offset / 32);
404 u32 msr_shift = msr_offset % 32;
405 u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
406 *base = (*base & ~(0x3 << msr_shift)) |
407 (mask << msr_shift);
Rusty Russellbfc733a2007-07-31 20:42:42 +1000408 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800409 }
410 }
Rusty Russellbfc733a2007-07-31 20:42:42 +1000411 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412}
413
Joerg Roedelf65c2292008-02-13 18:58:46 +0100414static void svm_vcpu_init_msrpm(u32 *msrpm)
415{
416 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
417
418#ifdef CONFIG_X86_64
419 set_msr_interception(msrpm, MSR_GS_BASE, 1, 1);
420 set_msr_interception(msrpm, MSR_FS_BASE, 1, 1);
421 set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1);
422 set_msr_interception(msrpm, MSR_LSTAR, 1, 1);
423 set_msr_interception(msrpm, MSR_CSTAR, 1, 1);
424 set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1);
425#endif
426 set_msr_interception(msrpm, MSR_K6_STAR, 1, 1);
427 set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100428}
429
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100430static void svm_enable_lbrv(struct vcpu_svm *svm)
431{
432 u32 *msrpm = svm->msrpm;
433
434 svm->vmcb->control.lbr_ctl = 1;
435 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
436 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
437 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
438 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
439}
440
441static void svm_disable_lbrv(struct vcpu_svm *svm)
442{
443 u32 *msrpm = svm->msrpm;
444
445 svm->vmcb->control.lbr_ctl = 0;
446 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
447 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
448 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
449 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
450}
451
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452static __init int svm_hardware_setup(void)
453{
454 int cpu;
455 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100456 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457 int r;
458
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
460
461 if (!iopm_pages)
462 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300463
464 iopm_va = page_address(iopm_pages);
465 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
467
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100468 if (boot_cpu_has(X86_FEATURE_NX))
469 kvm_enable_efer_bits(EFER_NX);
470
Alexander Graf1b2fd702009-02-02 16:23:51 +0100471 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
472 kvm_enable_efer_bits(EFER_FFXSR);
473
Alexander Graf236de052008-11-25 20:17:10 +0100474 if (nested) {
475 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
476 kvm_enable_efer_bits(EFER_SVME);
477 }
478
Zachary Amsden3230bb42009-09-29 11:38:37 -1000479 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 r = svm_cpu_init(cpu);
481 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100482 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100484
485 svm_features = cpuid_edx(SVM_CPUID_FUNC);
486
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100487 if (!svm_has(SVM_FEATURE_NPT))
488 npt_enabled = false;
489
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100490 if (npt_enabled && !npt) {
491 printk(KERN_INFO "kvm: Nested Paging disabled\n");
492 npt_enabled = false;
493 }
494
Joerg Roedel18552672008-02-07 13:47:41 +0100495 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100496 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100497 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200498 } else
499 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100500
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501 return 0;
502
Joerg Roedelf65c2292008-02-13 18:58:46 +0100503err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
505 iopm_base = 0;
506 return r;
507}
508
509static __exit void svm_hardware_unsetup(void)
510{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200511 int cpu;
512
Zachary Amsden3230bb42009-09-29 11:38:37 -1000513 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200514 svm_cpu_uninit(cpu);
515
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100517 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518}
519
520static void init_seg(struct vmcb_seg *seg)
521{
522 seg->selector = 0;
523 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
524 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
525 seg->limit = 0xffff;
526 seg->base = 0;
527}
528
529static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
530{
531 seg->selector = 0;
532 seg->attrib = SVM_SELECTOR_P_MASK | type;
533 seg->limit = 0xffff;
534 seg->base = 0;
535}
536
Joerg Roedele6101a92008-02-13 18:58:45 +0100537static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538{
Joerg Roedele6101a92008-02-13 18:58:45 +0100539 struct vmcb_control_area *control = &svm->vmcb->control;
540 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541
542 control->intercept_cr_read = INTERCEPT_CR0_MASK |
543 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200544 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545
546 control->intercept_cr_write = INTERCEPT_CR0_MASK |
547 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200548 INTERCEPT_CR4_MASK |
549 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550
551 control->intercept_dr_read = INTERCEPT_DR0_MASK |
552 INTERCEPT_DR1_MASK |
553 INTERCEPT_DR2_MASK |
554 INTERCEPT_DR3_MASK;
555
556 control->intercept_dr_write = INTERCEPT_DR0_MASK |
557 INTERCEPT_DR1_MASK |
558 INTERCEPT_DR2_MASK |
559 INTERCEPT_DR3_MASK |
560 INTERCEPT_DR5_MASK |
561 INTERCEPT_DR7_MASK;
562
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500563 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200564 (1 << UD_VECTOR) |
565 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566
567
568 control->intercept = (1ULL << INTERCEPT_INTR) |
569 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200570 (1ULL << INTERCEPT_SMI) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200572 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300574 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 (1ULL << INTERCEPT_INVLPGA) |
576 (1ULL << INTERCEPT_IOIO_PROT) |
577 (1ULL << INTERCEPT_MSR_PROT) |
578 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800579 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800580 (1ULL << INTERCEPT_VMRUN) |
581 (1ULL << INTERCEPT_VMMCALL) |
582 (1ULL << INTERCEPT_VMLOAD) |
583 (1ULL << INTERCEPT_VMSAVE) |
584 (1ULL << INTERCEPT_STGI) |
585 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100586 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200587 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100588 (1ULL << INTERCEPT_MONITOR) |
589 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590
591 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100592 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200593 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594 control->int_ctl = V_INTR_MASKING_MASK;
595
596 init_seg(&save->es);
597 init_seg(&save->ss);
598 init_seg(&save->ds);
599 init_seg(&save->fs);
600 init_seg(&save->gs);
601
602 save->cs.selector = 0xf000;
603 /* Executable/Readable Code Segment */
604 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
605 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
606 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800607 /*
608 * cs.base should really be 0xffff0000, but vmx can't handle that, so
609 * be consistent with it.
610 *
611 * Replace when we have real mode working for vmx.
612 */
613 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800614
615 save->gdtr.limit = 0xffff;
616 save->idtr.limit = 0xffff;
617
618 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
619 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
620
Alexander Graf9962d032008-11-25 20:17:02 +0100621 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400622 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623 save->dr7 = 0x400;
624 save->rflags = 2;
625 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300626 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627
628 /*
629 * cr0 val on cpu init should be 0x60000010, we enable cpu
630 * cache by default. the orderly way is to enable cache in bios.
631 */
Rusty Russell707d92fa2007-07-17 23:19:08 +1000632 save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
Rusty Russell66aee912007-07-17 23:34:16 +1000633 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100635
636 if (npt_enabled) {
637 /* Setup VMCB for Nested Paging */
638 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300639 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
640 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100641 control->intercept_exceptions &= ~(1 << PF_VECTOR);
642 control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK|
643 INTERCEPT_CR3_MASK);
644 control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK|
645 INTERCEPT_CR3_MASK);
646 save->g_pat = 0x0007040600070406ULL;
647 /* enable caching because the QEMU Bios doesn't enable it */
648 save->cr0 = X86_CR0_ET;
649 save->cr3 = 0;
650 save->cr4 = 0;
651 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300652 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100653
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200654 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200655 svm->vcpu.arch.hflags = 0;
656
657 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658}
659
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200660static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300661{
662 struct vcpu_svm *svm = to_svm(vcpu);
663
Joerg Roedele6101a92008-02-13 18:58:45 +0100664 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200665
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300666 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300667 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800668 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
669 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200670 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300671 vcpu->arch.regs_avail = ~0;
672 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200673
674 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300675}
676
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000677static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400679 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100681 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100682 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100683 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000684 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685
Rusty Russellc16f8622007-07-30 21:12:19 +1000686 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000687 if (!svm) {
688 err = -ENOMEM;
689 goto out;
690 }
691
692 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
693 if (err)
694 goto free_svm;
695
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696 page = alloc_page(GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000697 if (!page) {
698 err = -ENOMEM;
699 goto uninit;
700 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701
Joerg Roedelf65c2292008-02-13 18:58:46 +0100702 err = -ENOMEM;
703 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
704 if (!msrpm_pages)
705 goto uninit;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100706
707 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
708 if (!nested_msrpm_pages)
709 goto uninit;
710
Joerg Roedelf65c2292008-02-13 18:58:46 +0100711 svm->msrpm = page_address(msrpm_pages);
712 svm_vcpu_init_msrpm(svm->msrpm);
713
Alexander Grafb286d5d2008-11-25 20:17:05 +0100714 hsave_page = alloc_page(GFP_KERNEL);
715 if (!hsave_page)
716 goto uninit;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200717 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100718
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200719 svm->nested.msrpm = page_address(nested_msrpm_pages);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100720
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400721 svm->vmcb = page_address(page);
722 clear_page(svm->vmcb);
723 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
724 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100725 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400726
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000727 fx_init(&svm->vcpu);
728 svm->vcpu.fpu_active = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800729 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300730 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800731 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000733 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800734
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000735uninit:
736 kvm_vcpu_uninit(&svm->vcpu);
737free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000738 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000739out:
740 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741}
742
743static void svm_free_vcpu(struct kvm_vcpu *vcpu)
744{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400745 struct vcpu_svm *svm = to_svm(vcpu);
746
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000747 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100748 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200749 __free_page(virt_to_page(svm->nested.hsave));
750 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000751 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000752 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753}
754
Avi Kivity15ad7142007-07-11 18:17:21 +0300755static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400757 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300758 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200759
Avi Kivity0cc50642007-03-25 12:07:27 +0200760 if (unlikely(cpu != vcpu->cpu)) {
Joerg Roedele935d482009-09-16 15:24:19 +0200761 u64 delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200762
763 /*
764 * Make sure that the guest sees a monotonically
765 * increasing TSC.
766 */
Joerg Roedele935d482009-09-16 15:24:19 +0200767 delta = vcpu->arch.host_tsc - native_read_tsc();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400768 svm->vmcb->control.tsc_offset += delta;
Joerg Roedel77b1ab12009-09-16 15:24:17 +0200769 if (is_nested(svm))
770 svm->nested.hsave->control.tsc_offset += delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200771 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300772 kvm_migrate_timers(vcpu);
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300773 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200774 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300775
776 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400777 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778}
779
780static void svm_vcpu_put(struct kvm_vcpu *vcpu)
781{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400782 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300783 int i;
784
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200785 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300786 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400787 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300788
Joerg Roedele935d482009-09-16 15:24:19 +0200789 vcpu->arch.host_tsc = native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790}
791
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
793{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400794 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795}
796
797static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
798{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400799 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800}
801
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300802static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
803{
804 switch (reg) {
805 case VCPU_EXREG_PDPTR:
806 BUG_ON(!npt_enabled);
807 load_pdptrs(vcpu, vcpu->arch.cr3);
808 break;
809 default:
810 BUG();
811 }
812}
813
Alexander Graff0b85052008-11-25 20:17:01 +0100814static void svm_set_vintr(struct vcpu_svm *svm)
815{
816 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
817}
818
819static void svm_clear_vintr(struct vcpu_svm *svm)
820{
821 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
822}
823
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
825{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400826 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827
828 switch (seg) {
829 case VCPU_SREG_CS: return &save->cs;
830 case VCPU_SREG_DS: return &save->ds;
831 case VCPU_SREG_ES: return &save->es;
832 case VCPU_SREG_FS: return &save->fs;
833 case VCPU_SREG_GS: return &save->gs;
834 case VCPU_SREG_SS: return &save->ss;
835 case VCPU_SREG_TR: return &save->tr;
836 case VCPU_SREG_LDTR: return &save->ldtr;
837 }
838 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +0000839 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800840}
841
842static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
843{
844 struct vmcb_seg *s = svm_seg(vcpu, seg);
845
846 return s->base;
847}
848
849static void svm_get_segment(struct kvm_vcpu *vcpu,
850 struct kvm_segment *var, int seg)
851{
852 struct vmcb_seg *s = svm_seg(vcpu, seg);
853
854 var->base = s->base;
855 var->limit = s->limit;
856 var->selector = s->selector;
857 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
858 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
859 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
860 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
861 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
862 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
863 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
864 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +0000865
Andre Przywara19bca6a2009-04-28 12:45:30 +0200866 /* AMD's VMCB does not have an explicit unusable field, so emulate it
867 * for cross vendor migration purposes by "not present"
868 */
869 var->unusable = !var->present || (var->type == 0);
870
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100871 switch (seg) {
872 case VCPU_SREG_CS:
873 /*
874 * SVM always stores 0 for the 'G' bit in the CS selector in
875 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
876 * Intel's VMENTRY has a check on the 'G' bit.
877 */
Amit Shah25022ac2008-10-27 09:04:17 +0000878 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100879 break;
880 case VCPU_SREG_TR:
881 /*
882 * Work around a bug where the busy flag in the tr selector
883 * isn't exposed
884 */
Amit Shahc0d09822008-10-27 09:04:18 +0000885 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100886 break;
887 case VCPU_SREG_DS:
888 case VCPU_SREG_ES:
889 case VCPU_SREG_FS:
890 case VCPU_SREG_GS:
891 /*
892 * The accessed bit must always be set in the segment
893 * descriptor cache, although it can be cleared in the
894 * descriptor, the cached bit always remains at 1. Since
895 * Intel has a check on this, set it here to support
896 * cross-vendor migration.
897 */
898 if (!var->unusable)
899 var->type |= 0x1;
900 break;
Andre Przywarab586eb02009-04-28 12:45:43 +0200901 case VCPU_SREG_SS:
902 /* On AMD CPUs sometimes the DB bit in the segment
903 * descriptor is left as 1, although the whole segment has
904 * been made unusable. Clear it here to pass an Intel VMX
905 * entry check when cross vendor migrating.
906 */
907 if (var->unusable)
908 var->db = 0;
909 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +0100910 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911}
912
Izik Eidus2e4d2652008-03-24 19:38:34 +0200913static int svm_get_cpl(struct kvm_vcpu *vcpu)
914{
915 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
916
917 return save->cpl;
918}
919
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
921{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400922 struct vcpu_svm *svm = to_svm(vcpu);
923
924 dt->limit = svm->vmcb->save.idtr.limit;
925 dt->base = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926}
927
928static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
929{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400930 struct vcpu_svm *svm = to_svm(vcpu);
931
932 svm->vmcb->save.idtr.limit = dt->limit;
933 svm->vmcb->save.idtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934}
935
936static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
937{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400938 struct vcpu_svm *svm = to_svm(vcpu);
939
940 dt->limit = svm->vmcb->save.gdtr.limit;
941 dt->base = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942}
943
944static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
945{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400946 struct vcpu_svm *svm = to_svm(vcpu);
947
948 svm->vmcb->save.gdtr.limit = dt->limit;
949 svm->vmcb->save.gdtr.base = dt->base ;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800950}
951
Anthony Liguori25c4c272007-04-27 09:29:21 +0300952static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -0800953{
954}
955
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
957{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400958 struct vcpu_svm *svm = to_svm(vcpu);
959
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800960#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800961 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +1000962 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800963 vcpu->arch.shadow_efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600964 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965 }
966
Mike Dayd77c26f2007-10-08 09:02:08 -0400967 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800968 vcpu->arch.shadow_efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600969 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970 }
971 }
972#endif
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100973 if (npt_enabled)
974 goto set;
975
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800976 if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400977 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Anthony Liguori7807fa62007-04-23 09:17:21 -0500978 vcpu->fpu_active = 1;
979 }
980
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800981 vcpu->arch.cr0 = cr0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000982 cr0 |= X86_CR0_PG | X86_CR0_WP;
Joerg Roedel6b390b62008-01-29 13:01:27 +0100983 if (!vcpu->fpu_active) {
984 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
Joerg Roedel334df502008-01-21 13:09:33 +0100985 cr0 |= X86_CR0_TS;
Joerg Roedel6b390b62008-01-29 13:01:27 +0100986 }
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100987set:
988 /*
989 * re-enable caching here because the QEMU bios
990 * does not do it - this results in some delay at
991 * reboot
992 */
993 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400994 svm->vmcb->save.cr0 = cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995}
996
997static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
998{
Joerg Roedel6394b642008-04-09 14:15:29 +0200999 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001000 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1001
1002 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1003 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001004
Joerg Roedelec077262008-04-09 14:15:28 +02001005 vcpu->arch.cr4 = cr4;
1006 if (!npt_enabled)
1007 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001008 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001009 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010}
1011
1012static void svm_set_segment(struct kvm_vcpu *vcpu,
1013 struct kvm_segment *var, int seg)
1014{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001015 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 struct vmcb_seg *s = svm_seg(vcpu, seg);
1017
1018 s->base = var->base;
1019 s->limit = var->limit;
1020 s->selector = var->selector;
1021 if (var->unusable)
1022 s->attrib = 0;
1023 else {
1024 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1025 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1026 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1027 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1028 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1029 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1030 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1031 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1032 }
1033 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001034 svm->vmcb->save.cpl
1035 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001036 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1037
1038}
1039
Gleb Natapov44c11432009-05-11 13:35:52 +03001040static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001042 struct vcpu_svm *svm = to_svm(vcpu);
1043
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001044 svm->vmcb->control.intercept_exceptions &=
1045 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001046
1047 if (vcpu->arch.singlestep)
1048 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1049
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001050 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1051 if (vcpu->guest_debug &
1052 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1053 svm->vmcb->control.intercept_exceptions |=
1054 1 << DB_VECTOR;
1055 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1056 svm->vmcb->control.intercept_exceptions |=
1057 1 << BP_VECTOR;
1058 } else
1059 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001060}
1061
Jan Kiszka355be0b2009-10-03 00:31:21 +02001062static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001063{
Gleb Natapov44c11432009-05-11 13:35:52 +03001064 struct vcpu_svm *svm = to_svm(vcpu);
1065
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001066 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1067 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1068 else
1069 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1070
Jan Kiszka355be0b2009-10-03 00:31:21 +02001071 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072}
1073
1074static void load_host_msrs(struct kvm_vcpu *vcpu)
1075{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001076#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001077 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001078#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001079}
1080
1081static void save_host_msrs(struct kvm_vcpu *vcpu)
1082{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001083#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001084 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001085#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086}
1087
Rusty Russelle756fc62007-07-30 20:07:08 +10001088static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089{
1090 if (svm_data->next_asid > svm_data->max_asid) {
1091 ++svm_data->asid_generation;
1092 svm_data->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001093 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001094 }
1095
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001096 svm->asid_generation = svm_data->asid_generation;
1097 svm->vmcb->control.asid = svm_data->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098}
1099
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
1101{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001102 struct vcpu_svm *svm = to_svm(vcpu);
1103 unsigned long val;
1104
1105 switch (dr) {
1106 case 0 ... 3:
1107 val = vcpu->arch.db[dr];
1108 break;
1109 case 6:
1110 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1111 val = vcpu->arch.dr6;
1112 else
1113 val = svm->vmcb->save.dr6;
1114 break;
1115 case 7:
1116 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1117 val = vcpu->arch.dr7;
1118 else
1119 val = svm->vmcb->save.dr7;
1120 break;
1121 default:
1122 val = 0;
1123 }
1124
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001125 return val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126}
1127
1128static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
1129 int *exception)
1130{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001131 struct vcpu_svm *svm = to_svm(vcpu);
1132
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001133 *exception = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134
1135 switch (dr) {
1136 case 0 ... 3:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001137 vcpu->arch.db[dr] = value;
1138 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1139 vcpu->arch.eff_db[dr] = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140 return;
1141 case 4 ... 5:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001142 if (vcpu->arch.cr4 & X86_CR4_DE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143 *exception = UD_VECTOR;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001144 return;
1145 case 6:
1146 if (value & 0xffffffff00000000ULL) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001147 *exception = GP_VECTOR;
1148 return;
1149 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001150 vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 return;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001152 case 7:
1153 if (value & 0xffffffff00000000ULL) {
1154 *exception = GP_VECTOR;
1155 return;
1156 }
1157 vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1;
1158 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1159 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1160 vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK);
1161 }
1162 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163 default:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001164 /* FIXME: Possible case? */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165 printk(KERN_DEBUG "%s: unexpected dr %u\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001166 __func__, dr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167 *exception = UD_VECTOR;
1168 return;
1169 }
1170}
1171
Avi Kivity851ba692009-08-24 11:10:17 +03001172static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001174 u64 fault_address;
1175 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001177 fault_address = svm->vmcb->control.exit_info_2;
1178 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001179
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001180 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001181 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1182 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001183 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184}
1185
Avi Kivity851ba692009-08-24 11:10:17 +03001186static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001187{
Avi Kivity851ba692009-08-24 11:10:17 +03001188 struct kvm_run *kvm_run = svm->vcpu.run;
1189
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001190 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001191 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
1192 !svm->vcpu.arch.singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001193 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1194 return 1;
1195 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001196
1197 if (svm->vcpu.arch.singlestep) {
1198 svm->vcpu.arch.singlestep = false;
1199 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1200 svm->vmcb->save.rflags &=
1201 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1202 update_db_intercept(&svm->vcpu);
1203 }
1204
1205 if (svm->vcpu.guest_debug &
1206 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)){
1207 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1208 kvm_run->debug.arch.pc =
1209 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1210 kvm_run->debug.arch.exception = DB_VECTOR;
1211 return 0;
1212 }
1213
1214 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001215}
1216
Avi Kivity851ba692009-08-24 11:10:17 +03001217static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001218{
Avi Kivity851ba692009-08-24 11:10:17 +03001219 struct kvm_run *kvm_run = svm->vcpu.run;
1220
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001221 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1222 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1223 kvm_run->debug.arch.exception = BP_VECTOR;
1224 return 0;
1225}
1226
Avi Kivity851ba692009-08-24 11:10:17 +03001227static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001228{
1229 int er;
1230
Avi Kivity851ba692009-08-24 11:10:17 +03001231 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001232 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001233 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001234 return 1;
1235}
1236
Avi Kivity851ba692009-08-24 11:10:17 +03001237static int nm_interception(struct vcpu_svm *svm)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001238{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001239 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001240 if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001241 svm->vmcb->save.cr0 &= ~X86_CR0_TS;
Rusty Russelle756fc62007-07-30 20:07:08 +10001242 svm->vcpu.fpu_active = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001243
1244 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001245}
1246
Avi Kivity851ba692009-08-24 11:10:17 +03001247static int mc_interception(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001248{
1249 /*
1250 * On an #MC intercept the MCE handler is not called automatically in
1251 * the host. So do it by hand here.
1252 */
1253 asm volatile (
1254 "int $0x12\n");
1255 /* not sure if we ever come back to this point */
1256
1257 return 1;
1258}
1259
Avi Kivity851ba692009-08-24 11:10:17 +03001260static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001261{
Avi Kivity851ba692009-08-24 11:10:17 +03001262 struct kvm_run *kvm_run = svm->vcpu.run;
1263
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001264 /*
1265 * VMCB is undefined after a SHUTDOWN intercept
1266 * so reinitialize it.
1267 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001268 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001269 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001270
1271 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1272 return 0;
1273}
1274
Avi Kivity851ba692009-08-24 11:10:17 +03001275static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276{
Mike Dayd77c26f2007-10-08 09:02:08 -04001277 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001278 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001279 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280
Rusty Russelle756fc62007-07-30 20:07:08 +10001281 ++svm->vcpu.stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001283 svm->next_rip = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284
Laurent Viviere70669a2007-08-05 10:36:40 +03001285 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1286
1287 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02001288 if (emulate_instruction(&svm->vcpu,
Avi Kivity851ba692009-08-24 11:10:17 +03001289 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03001290 return 0;
1291 return 1;
1292 }
1293
Avi Kivity039576c2007-03-20 12:46:50 +02001294 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1295 port = io_info >> 16;
1296 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001298 skip_emulated_instruction(&svm->vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03001299 return kvm_emulate_pio(&svm->vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300}
1301
Avi Kivity851ba692009-08-24 11:10:17 +03001302static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001303{
1304 return 1;
1305}
1306
Avi Kivity851ba692009-08-24 11:10:17 +03001307static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001308{
1309 ++svm->vcpu.stat.irq_exits;
1310 return 1;
1311}
1312
Avi Kivity851ba692009-08-24 11:10:17 +03001313static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314{
1315 return 1;
1316}
1317
Avi Kivity851ba692009-08-24 11:10:17 +03001318static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001320 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001321 skip_emulated_instruction(&svm->vcpu);
1322 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323}
1324
Avi Kivity851ba692009-08-24 11:10:17 +03001325static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001326{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001327 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001328 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001329 kvm_emulate_hypercall(&svm->vcpu);
1330 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001331}
1332
Alexander Grafc0725422008-11-25 20:17:03 +01001333static int nested_svm_check_permissions(struct vcpu_svm *svm)
1334{
1335 if (!(svm->vcpu.arch.shadow_efer & EFER_SVME)
1336 || !is_paging(&svm->vcpu)) {
1337 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1338 return 1;
1339 }
1340
1341 if (svm->vmcb->save.cpl) {
1342 kvm_inject_gp(&svm->vcpu, 0);
1343 return 1;
1344 }
1345
1346 return 0;
1347}
1348
Alexander Grafcf74a782008-11-25 20:17:08 +01001349static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1350 bool has_error_code, u32 error_code)
1351{
Joerg Roedel0295ad72009-08-07 11:49:37 +02001352 if (!is_nested(svm))
1353 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001354
Joerg Roedel0295ad72009-08-07 11:49:37 +02001355 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1356 svm->vmcb->control.exit_code_hi = 0;
1357 svm->vmcb->control.exit_info_1 = error_code;
1358 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1359
Joerg Roedel410e4d52009-08-07 11:49:44 +02001360 return nested_svm_exit_handled(svm);
Alexander Grafcf74a782008-11-25 20:17:08 +01001361}
1362
1363static inline int nested_svm_intr(struct vcpu_svm *svm)
1364{
Joerg Roedel26666952009-08-07 11:49:46 +02001365 if (!is_nested(svm))
1366 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001367
Joerg Roedel26666952009-08-07 11:49:46 +02001368 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1369 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001370
Joerg Roedel26666952009-08-07 11:49:46 +02001371 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
1372 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001373
Joerg Roedel26666952009-08-07 11:49:46 +02001374 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1375
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001376 if (svm->nested.intercept & 1ULL) {
1377 /*
1378 * The #vmexit can't be emulated here directly because this
1379 * code path runs with irqs and preemtion disabled. A
1380 * #vmexit emulation might sleep. Only signal request for
1381 * the #vmexit here.
1382 */
1383 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001384 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel26666952009-08-07 11:49:46 +02001385 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01001386 }
1387
1388 return 0;
1389}
1390
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001391static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, enum km_type idx)
1392{
1393 struct page *page;
1394
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001395 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001396 if (is_error_page(page))
1397 goto error;
1398
1399 return kmap_atomic(page, idx);
1400
1401error:
1402 kvm_release_page_clean(page);
1403 kvm_inject_gp(&svm->vcpu, 0);
1404
1405 return NULL;
1406}
1407
1408static void nested_svm_unmap(void *addr, enum km_type idx)
1409{
1410 struct page *page;
1411
1412 if (!addr)
1413 return;
1414
1415 page = kmap_atomic_to_page(addr);
1416
1417 kunmap_atomic(addr, idx);
1418 kvm_release_page_dirty(page);
1419}
1420
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001421static bool nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001422{
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001423 u32 param = svm->vmcb->control.exit_info_1 & 1;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001424 u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1425 bool ret = false;
1426 u32 t0, t1;
1427 u8 *msrpm;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001428
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001429 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
1430 return false;
1431
1432 msrpm = nested_svm_map(svm, svm->nested.vmcb_msrpm, KM_USER0);
1433
1434 if (!msrpm)
1435 goto out;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001436
1437 switch (msr) {
1438 case 0 ... 0x1fff:
1439 t0 = (msr * 2) % 8;
1440 t1 = msr / 8;
1441 break;
1442 case 0xc0000000 ... 0xc0001fff:
1443 t0 = (8192 + msr - 0xc0000000) * 2;
1444 t1 = (t0 / 8);
1445 t0 %= 8;
1446 break;
1447 case 0xc0010000 ... 0xc0011fff:
1448 t0 = (16384 + msr - 0xc0010000) * 2;
1449 t1 = (t0 / 8);
1450 t0 %= 8;
1451 break;
1452 default:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001453 ret = true;
1454 goto out;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001455 }
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001456
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001457 ret = msrpm[t1] & ((1 << param) << t0);
1458
1459out:
1460 nested_svm_unmap(msrpm, KM_USER0);
1461
1462 return ret;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001463}
1464
Joerg Roedel410e4d52009-08-07 11:49:44 +02001465static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001466{
Alexander Grafcf74a782008-11-25 20:17:08 +01001467 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001468
Joerg Roedel410e4d52009-08-07 11:49:44 +02001469 switch (exit_code) {
1470 case SVM_EXIT_INTR:
1471 case SVM_EXIT_NMI:
1472 return NESTED_EXIT_HOST;
Alexander Grafcf74a782008-11-25 20:17:08 +01001473 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001474 case SVM_EXIT_NPF:
1475 if (npt_enabled)
1476 return NESTED_EXIT_HOST;
1477 break;
1478 /* When we're shadowing, trap PFs */
1479 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
1480 if (!npt_enabled)
1481 return NESTED_EXIT_HOST;
1482 break;
1483 default:
1484 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001485 }
1486
Joerg Roedel410e4d52009-08-07 11:49:44 +02001487 return NESTED_EXIT_CONTINUE;
1488}
1489
1490/*
1491 * If this function returns true, this #vmexit was already handled
1492 */
1493static int nested_svm_exit_handled(struct vcpu_svm *svm)
1494{
1495 u32 exit_code = svm->vmcb->control.exit_code;
1496 int vmexit = NESTED_EXIT_HOST;
1497
Alexander Grafcf74a782008-11-25 20:17:08 +01001498 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001499 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001500 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001501 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001502 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1503 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001504 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001505 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001506 break;
1507 }
1508 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1509 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001510 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001511 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001512 break;
1513 }
1514 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1515 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001516 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001517 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001518 break;
1519 }
1520 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1521 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001522 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001523 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001524 break;
1525 }
1526 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1527 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001528 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001529 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001530 break;
1531 }
1532 default: {
1533 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001534 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001535 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001536 }
1537 }
1538
Joerg Roedel410e4d52009-08-07 11:49:44 +02001539 if (vmexit == NESTED_EXIT_DONE) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001540 nested_svm_vmexit(svm);
1541 }
1542
1543 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001544}
1545
Joerg Roedel0460a972009-08-07 11:49:31 +02001546static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1547{
1548 struct vmcb_control_area *dst = &dst_vmcb->control;
1549 struct vmcb_control_area *from = &from_vmcb->control;
1550
1551 dst->intercept_cr_read = from->intercept_cr_read;
1552 dst->intercept_cr_write = from->intercept_cr_write;
1553 dst->intercept_dr_read = from->intercept_dr_read;
1554 dst->intercept_dr_write = from->intercept_dr_write;
1555 dst->intercept_exceptions = from->intercept_exceptions;
1556 dst->intercept = from->intercept;
1557 dst->iopm_base_pa = from->iopm_base_pa;
1558 dst->msrpm_base_pa = from->msrpm_base_pa;
1559 dst->tsc_offset = from->tsc_offset;
1560 dst->asid = from->asid;
1561 dst->tlb_ctl = from->tlb_ctl;
1562 dst->int_ctl = from->int_ctl;
1563 dst->int_vector = from->int_vector;
1564 dst->int_state = from->int_state;
1565 dst->exit_code = from->exit_code;
1566 dst->exit_code_hi = from->exit_code_hi;
1567 dst->exit_info_1 = from->exit_info_1;
1568 dst->exit_info_2 = from->exit_info_2;
1569 dst->exit_int_info = from->exit_int_info;
1570 dst->exit_int_info_err = from->exit_int_info_err;
1571 dst->nested_ctl = from->nested_ctl;
1572 dst->event_inj = from->event_inj;
1573 dst->event_inj_err = from->event_inj_err;
1574 dst->nested_cr3 = from->nested_cr3;
1575 dst->lbr_ctl = from->lbr_ctl;
1576}
1577
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001578static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001579{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001580 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001581 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001582 struct vmcb *vmcb = svm->vmcb;
Alexander Grafcf74a782008-11-25 20:17:08 +01001583
Joerg Roedel17897f32009-10-09 16:08:29 +02001584 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1585 vmcb->control.exit_info_1,
1586 vmcb->control.exit_info_2,
1587 vmcb->control.exit_int_info,
1588 vmcb->control.exit_int_info_err);
1589
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001590 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, KM_USER0);
1591 if (!nested_vmcb)
1592 return 1;
1593
Alexander Grafcf74a782008-11-25 20:17:08 +01001594 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001595 disable_gif(svm);
1596
1597 nested_vmcb->save.es = vmcb->save.es;
1598 nested_vmcb->save.cs = vmcb->save.cs;
1599 nested_vmcb->save.ss = vmcb->save.ss;
1600 nested_vmcb->save.ds = vmcb->save.ds;
1601 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1602 nested_vmcb->save.idtr = vmcb->save.idtr;
1603 if (npt_enabled)
1604 nested_vmcb->save.cr3 = vmcb->save.cr3;
1605 nested_vmcb->save.cr2 = vmcb->save.cr2;
1606 nested_vmcb->save.rflags = vmcb->save.rflags;
1607 nested_vmcb->save.rip = vmcb->save.rip;
1608 nested_vmcb->save.rsp = vmcb->save.rsp;
1609 nested_vmcb->save.rax = vmcb->save.rax;
1610 nested_vmcb->save.dr7 = vmcb->save.dr7;
1611 nested_vmcb->save.dr6 = vmcb->save.dr6;
1612 nested_vmcb->save.cpl = vmcb->save.cpl;
1613
1614 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1615 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1616 nested_vmcb->control.int_state = vmcb->control.int_state;
1617 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1618 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1619 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1620 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1621 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1622 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Alexander Graf8d23c462009-10-09 16:08:25 +02001623
1624 /*
1625 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1626 * to make sure that we do not lose injected events. So check event_inj
1627 * here and copy it to exit_int_info if it is valid.
1628 * Exit_int_info and event_inj can't be both valid because the case
1629 * below only happens on a VMRUN instruction intercept which has
1630 * no valid exit_int_info set.
1631 */
1632 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1633 struct vmcb_control_area *nc = &nested_vmcb->control;
1634
1635 nc->exit_int_info = vmcb->control.event_inj;
1636 nc->exit_int_info_err = vmcb->control.event_inj_err;
1637 }
1638
Joerg Roedel33740e42009-08-07 11:49:29 +02001639 nested_vmcb->control.tlb_ctl = 0;
1640 nested_vmcb->control.event_inj = 0;
1641 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001642
1643 /* We always set V_INTR_MASKING and remember the old value in hflags */
1644 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1645 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1646
Alexander Grafcf74a782008-11-25 20:17:08 +01001647 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001648 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001649
Alexander Graf219b65d2009-06-15 15:21:25 +02001650 kvm_clear_exception_queue(&svm->vcpu);
1651 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01001652
1653 /* Restore selected save entries */
1654 svm->vmcb->save.es = hsave->save.es;
1655 svm->vmcb->save.cs = hsave->save.cs;
1656 svm->vmcb->save.ss = hsave->save.ss;
1657 svm->vmcb->save.ds = hsave->save.ds;
1658 svm->vmcb->save.gdtr = hsave->save.gdtr;
1659 svm->vmcb->save.idtr = hsave->save.idtr;
1660 svm->vmcb->save.rflags = hsave->save.rflags;
1661 svm_set_efer(&svm->vcpu, hsave->save.efer);
1662 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1663 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1664 if (npt_enabled) {
1665 svm->vmcb->save.cr3 = hsave->save.cr3;
1666 svm->vcpu.arch.cr3 = hsave->save.cr3;
1667 } else {
1668 kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
1669 }
1670 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1671 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1672 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1673 svm->vmcb->save.dr7 = 0;
1674 svm->vmcb->save.cpl = 0;
1675 svm->vmcb->control.exit_int_info = 0;
1676
Alexander Grafcf74a782008-11-25 20:17:08 +01001677 /* Exit nested SVM mode */
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001678 svm->nested.vmcb = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001679
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001680 nested_svm_unmap(nested_vmcb, KM_USER0);
Alexander Grafcf74a782008-11-25 20:17:08 +01001681
1682 kvm_mmu_reset_context(&svm->vcpu);
1683 kvm_mmu_load(&svm->vcpu);
1684
1685 return 0;
1686}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001687
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001688static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001689{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001690 u32 *nested_msrpm;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001691 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001692
1693 nested_msrpm = nested_svm_map(svm, svm->nested.vmcb_msrpm, KM_USER0);
1694 if (!nested_msrpm)
1695 return false;
1696
Alexander Graf3d6368e2008-11-25 20:17:07 +01001697 for (i=0; i< PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++)
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001698 svm->nested.msrpm[i] = svm->msrpm[i] | nested_msrpm[i];
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001699
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001700 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001701
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001702 nested_svm_unmap(nested_msrpm, KM_USER0);
1703
1704 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001705}
1706
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001707static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001708{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001709 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001710 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02001711 struct vmcb *vmcb = svm->vmcb;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001712
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001713 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, KM_USER0);
1714 if (!nested_vmcb)
1715 return false;
1716
Alexander Graf3d6368e2008-11-25 20:17:07 +01001717 /* nested_vmcb is our indicator if nested SVM is activated */
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001718 svm->nested.vmcb = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001719
Joerg Roedel0ac406d2009-10-09 16:08:27 +02001720 trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, svm->nested.vmcb,
1721 nested_vmcb->save.rip,
1722 nested_vmcb->control.int_ctl,
1723 nested_vmcb->control.event_inj,
1724 nested_vmcb->control.nested_ctl);
1725
Alexander Graf3d6368e2008-11-25 20:17:07 +01001726 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02001727 kvm_clear_exception_queue(&svm->vcpu);
1728 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001729
1730 /* Save the old vmcb, so we don't need to pick what we save, but
1731 can restore everything when a VMEXIT occurs */
Joerg Roedeldefbba52009-08-07 11:49:30 +02001732 hsave->save.es = vmcb->save.es;
1733 hsave->save.cs = vmcb->save.cs;
1734 hsave->save.ss = vmcb->save.ss;
1735 hsave->save.ds = vmcb->save.ds;
1736 hsave->save.gdtr = vmcb->save.gdtr;
1737 hsave->save.idtr = vmcb->save.idtr;
1738 hsave->save.efer = svm->vcpu.arch.shadow_efer;
1739 hsave->save.cr0 = svm->vcpu.arch.cr0;
1740 hsave->save.cr4 = svm->vcpu.arch.cr4;
1741 hsave->save.rflags = vmcb->save.rflags;
1742 hsave->save.rip = svm->next_rip;
1743 hsave->save.rsp = vmcb->save.rsp;
1744 hsave->save.rax = vmcb->save.rax;
1745 if (npt_enabled)
1746 hsave->save.cr3 = vmcb->save.cr3;
1747 else
1748 hsave->save.cr3 = svm->vcpu.arch.cr3;
1749
Joerg Roedel0460a972009-08-07 11:49:31 +02001750 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001751
1752 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
1753 svm->vcpu.arch.hflags |= HF_HIF_MASK;
1754 else
1755 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
1756
1757 /* Load the nested guest state */
1758 svm->vmcb->save.es = nested_vmcb->save.es;
1759 svm->vmcb->save.cs = nested_vmcb->save.cs;
1760 svm->vmcb->save.ss = nested_vmcb->save.ss;
1761 svm->vmcb->save.ds = nested_vmcb->save.ds;
1762 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
1763 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
1764 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
1765 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
1766 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
1767 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
1768 if (npt_enabled) {
1769 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
1770 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
1771 } else {
1772 kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
1773 kvm_mmu_reset_context(&svm->vcpu);
1774 }
Joerg Roedeldefbba52009-08-07 11:49:30 +02001775 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001776 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
1777 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
1778 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
1779 /* In case we don't even reach vcpu_run, the fields are not updated */
1780 svm->vmcb->save.rax = nested_vmcb->save.rax;
1781 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
1782 svm->vmcb->save.rip = nested_vmcb->save.rip;
1783 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
1784 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
1785 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
1786
1787 /* We don't want a nested guest to be more powerful than the guest,
1788 so all intercepts are ORed */
1789 svm->vmcb->control.intercept_cr_read |=
1790 nested_vmcb->control.intercept_cr_read;
1791 svm->vmcb->control.intercept_cr_write |=
1792 nested_vmcb->control.intercept_cr_write;
1793 svm->vmcb->control.intercept_dr_read |=
1794 nested_vmcb->control.intercept_dr_read;
1795 svm->vmcb->control.intercept_dr_write |=
1796 nested_vmcb->control.intercept_dr_write;
1797 svm->vmcb->control.intercept_exceptions |=
1798 nested_vmcb->control.intercept_exceptions;
1799
1800 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
1801
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001802 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001803
Joerg Roedelaad42c62009-08-07 11:49:34 +02001804 /* cache intercepts */
1805 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
1806 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
1807 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
1808 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
1809 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
1810 svm->nested.intercept = nested_vmcb->control.intercept;
1811
Alexander Graf3d6368e2008-11-25 20:17:07 +01001812 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001813 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001814 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
1815 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
1816 else
1817 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
1818
Alexander Graf3d6368e2008-11-25 20:17:07 +01001819 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
1820 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
1821 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001822 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
1823 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
1824
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001825 nested_svm_unmap(nested_vmcb, KM_USER0);
1826
Joerg Roedel2af91942009-08-07 11:49:28 +02001827 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001828
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001829 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001830}
1831
Joerg Roedel9966bf62009-08-07 11:49:40 +02001832static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01001833{
1834 to_vmcb->save.fs = from_vmcb->save.fs;
1835 to_vmcb->save.gs = from_vmcb->save.gs;
1836 to_vmcb->save.tr = from_vmcb->save.tr;
1837 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
1838 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
1839 to_vmcb->save.star = from_vmcb->save.star;
1840 to_vmcb->save.lstar = from_vmcb->save.lstar;
1841 to_vmcb->save.cstar = from_vmcb->save.cstar;
1842 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
1843 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
1844 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
1845 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01001846}
1847
Avi Kivity851ba692009-08-24 11:10:17 +03001848static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01001849{
Joerg Roedel9966bf62009-08-07 11:49:40 +02001850 struct vmcb *nested_vmcb;
1851
Alexander Graf55426752008-11-25 20:17:06 +01001852 if (nested_svm_check_permissions(svm))
1853 return 1;
1854
1855 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1856 skip_emulated_instruction(&svm->vcpu);
1857
Joerg Roedel9966bf62009-08-07 11:49:40 +02001858 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, KM_USER0);
1859 if (!nested_vmcb)
1860 return 1;
1861
1862 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
1863 nested_svm_unmap(nested_vmcb, KM_USER0);
Alexander Graf55426752008-11-25 20:17:06 +01001864
1865 return 1;
1866}
1867
Avi Kivity851ba692009-08-24 11:10:17 +03001868static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01001869{
Joerg Roedel9966bf62009-08-07 11:49:40 +02001870 struct vmcb *nested_vmcb;
1871
Alexander Graf55426752008-11-25 20:17:06 +01001872 if (nested_svm_check_permissions(svm))
1873 return 1;
1874
1875 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1876 skip_emulated_instruction(&svm->vcpu);
1877
Joerg Roedel9966bf62009-08-07 11:49:40 +02001878 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, KM_USER0);
1879 if (!nested_vmcb)
1880 return 1;
1881
1882 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
1883 nested_svm_unmap(nested_vmcb, KM_USER0);
Alexander Graf55426752008-11-25 20:17:06 +01001884
1885 return 1;
1886}
1887
Avi Kivity851ba692009-08-24 11:10:17 +03001888static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001889{
Alexander Graf3d6368e2008-11-25 20:17:07 +01001890 if (nested_svm_check_permissions(svm))
1891 return 1;
1892
1893 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1894 skip_emulated_instruction(&svm->vcpu);
1895
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001896 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01001897 return 1;
1898
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001899 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02001900 goto failed;
1901
1902 return 1;
1903
1904failed:
1905
1906 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
1907 svm->vmcb->control.exit_code_hi = 0;
1908 svm->vmcb->control.exit_info_1 = 0;
1909 svm->vmcb->control.exit_info_2 = 0;
1910
1911 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001912
1913 return 1;
1914}
1915
Avi Kivity851ba692009-08-24 11:10:17 +03001916static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01001917{
1918 if (nested_svm_check_permissions(svm))
1919 return 1;
1920
1921 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1922 skip_emulated_instruction(&svm->vcpu);
1923
Joerg Roedel2af91942009-08-07 11:49:28 +02001924 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01001925
1926 return 1;
1927}
1928
Avi Kivity851ba692009-08-24 11:10:17 +03001929static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01001930{
1931 if (nested_svm_check_permissions(svm))
1932 return 1;
1933
1934 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1935 skip_emulated_instruction(&svm->vcpu);
1936
Joerg Roedel2af91942009-08-07 11:49:28 +02001937 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01001938
1939 /* After a CLGI no interrupts should come */
1940 svm_clear_vintr(svm);
1941 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
1942
1943 return 1;
1944}
1945
Avi Kivity851ba692009-08-24 11:10:17 +03001946static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02001947{
1948 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02001949
Joerg Roedelec1ff792009-10-09 16:08:31 +02001950 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
1951 vcpu->arch.regs[VCPU_REGS_RAX]);
1952
Alexander Grafff092382009-06-15 15:21:24 +02001953 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
1954 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
1955
1956 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1957 skip_emulated_instruction(&svm->vcpu);
1958 return 1;
1959}
1960
Joerg Roedel532a46b2009-10-09 16:08:32 +02001961static int skinit_interception(struct vcpu_svm *svm)
1962{
1963 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
1964
1965 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1966 return 1;
1967}
1968
Avi Kivity851ba692009-08-24 11:10:17 +03001969static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001971 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972 return 1;
1973}
1974
Avi Kivity851ba692009-08-24 11:10:17 +03001975static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976{
Izik Eidus37817f22008-03-24 23:14:53 +02001977 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03001978 int reason;
1979 int int_type = svm->vmcb->control.exit_int_info &
1980 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03001981 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03001982 uint32_t type =
1983 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
1984 uint32_t idt_v =
1985 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Izik Eidus37817f22008-03-24 23:14:53 +02001986
1987 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03001988
Izik Eidus37817f22008-03-24 23:14:53 +02001989 if (svm->vmcb->control.exit_info_2 &
1990 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03001991 reason = TASK_SWITCH_IRET;
1992 else if (svm->vmcb->control.exit_info_2 &
1993 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
1994 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03001995 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03001996 reason = TASK_SWITCH_GATE;
1997 else
1998 reason = TASK_SWITCH_CALL;
1999
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002000 if (reason == TASK_SWITCH_GATE) {
2001 switch (type) {
2002 case SVM_EXITINTINFO_TYPE_NMI:
2003 svm->vcpu.arch.nmi_injected = false;
2004 break;
2005 case SVM_EXITINTINFO_TYPE_EXEPT:
2006 kvm_clear_exception_queue(&svm->vcpu);
2007 break;
2008 case SVM_EXITINTINFO_TYPE_INTR:
2009 kvm_clear_interrupt_queue(&svm->vcpu);
2010 break;
2011 default:
2012 break;
2013 }
2014 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002015
Gleb Natapov8317c292009-04-12 13:37:02 +03002016 if (reason != TASK_SWITCH_GATE ||
2017 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2018 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002019 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2020 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002021
2022 return kvm_task_switch(&svm->vcpu, tss_selector, reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023}
2024
Avi Kivity851ba692009-08-24 11:10:17 +03002025static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002026{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002027 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002028 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002029 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002030}
2031
Avi Kivity851ba692009-08-24 11:10:17 +03002032static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002033{
2034 ++svm->vcpu.stat.nmi_window_exits;
2035 svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002036 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002037 return 1;
2038}
2039
Avi Kivity851ba692009-08-24 11:10:17 +03002040static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002041{
Avi Kivity851ba692009-08-24 11:10:17 +03002042 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002043 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
2044 return 1;
2045}
2046
Avi Kivity851ba692009-08-24 11:10:17 +03002047static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048{
Avi Kivity851ba692009-08-24 11:10:17 +03002049 if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002050 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002051 return 1;
2052}
2053
Avi Kivity851ba692009-08-24 11:10:17 +03002054static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002055{
Avi Kivity851ba692009-08-24 11:10:17 +03002056 struct kvm_run *kvm_run = svm->vcpu.run;
2057
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002058 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2059 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002060 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002061 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2062 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002063 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002064 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002065 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2066 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002067 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2068 return 0;
2069}
2070
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2072{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002073 struct vcpu_svm *svm = to_svm(vcpu);
2074
Avi Kivity6aa8b732006-12-10 02:21:36 -08002075 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302076 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002077 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002078
Joerg Roedel20824f32009-09-16 15:24:18 +02002079 if (is_nested(svm))
2080 tsc_offset = svm->nested.hsave->control.tsc_offset;
2081 else
2082 tsc_offset = svm->vmcb->control.tsc_offset;
2083
2084 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085 break;
2086 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002087 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002088 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002089 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002090#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002092 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093 break;
2094 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002095 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096 break;
2097 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002098 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099 break;
2100 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002101 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102 break;
2103#endif
2104 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002105 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106 break;
2107 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002108 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109 break;
2110 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002111 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002113 /* Nobody will change the following 5 values in the VMCB so
2114 we can safely return them on rdmsr. They will always be 0
2115 until LBRV is implemented. */
2116 case MSR_IA32_DEBUGCTLMSR:
2117 *data = svm->vmcb->save.dbgctl;
2118 break;
2119 case MSR_IA32_LASTBRANCHFROMIP:
2120 *data = svm->vmcb->save.br_from;
2121 break;
2122 case MSR_IA32_LASTBRANCHTOIP:
2123 *data = svm->vmcb->save.br_to;
2124 break;
2125 case MSR_IA32_LASTINTFROMIP:
2126 *data = svm->vmcb->save.last_excp_from;
2127 break;
2128 case MSR_IA32_LASTINTTOIP:
2129 *data = svm->vmcb->save.last_excp_to;
2130 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002131 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002132 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002133 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002134 case MSR_VM_CR:
2135 *data = 0;
2136 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002137 case MSR_IA32_UCODE_REV:
2138 *data = 0x01000065;
2139 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002141 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142 }
2143 return 0;
2144}
2145
Avi Kivity851ba692009-08-24 11:10:17 +03002146static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002148 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149 u64 data;
2150
Rusty Russelle756fc62007-07-30 20:07:08 +10002151 if (svm_get_msr(&svm->vcpu, ecx, &data))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002152 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153 else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002154 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002155
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002156 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002157 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002158 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002159 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160 }
2161 return 1;
2162}
2163
2164static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2165{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002166 struct vcpu_svm *svm = to_svm(vcpu);
2167
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302169 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002170 u64 tsc_offset = data - native_read_tsc();
2171 u64 g_tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172
Joerg Roedel20824f32009-09-16 15:24:18 +02002173 if (is_nested(svm)) {
2174 g_tsc_offset = svm->vmcb->control.tsc_offset -
2175 svm->nested.hsave->control.tsc_offset;
2176 svm->nested.hsave->control.tsc_offset = tsc_offset;
2177 }
2178
2179 svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
2180
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181 break;
2182 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002183 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002184 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002186#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002188 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 break;
2190 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002191 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192 break;
2193 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002194 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195 break;
2196 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002197 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198 break;
2199#endif
2200 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002201 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002202 break;
2203 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002204 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002205 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206 break;
2207 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002208 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002209 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002211 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002212 if (!svm_has(SVM_FEATURE_LBRV)) {
2213 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002214 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002215 break;
2216 }
2217 if (data & DEBUGCTL_RESERVED_BITS)
2218 return 1;
2219
2220 svm->vmcb->save.dbgctl = data;
2221 if (data & (1ULL<<0))
2222 svm_enable_lbrv(svm);
2223 else
2224 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002225 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002226 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002227 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002228 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002229 case MSR_VM_CR:
2230 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002231 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2232 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002234 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002235 }
2236 return 0;
2237}
2238
Avi Kivity851ba692009-08-24 11:10:17 +03002239static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002241 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002242 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002243 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002244
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002245 trace_kvm_msr_write(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002246
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002247 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002248 if (svm_set_msr(&svm->vcpu, ecx, data))
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002249 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250 else
Rusty Russelle756fc62007-07-30 20:07:08 +10002251 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252 return 1;
2253}
2254
Avi Kivity851ba692009-08-24 11:10:17 +03002255static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256{
Rusty Russelle756fc62007-07-30 20:07:08 +10002257 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002258 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259 else
Avi Kivity851ba692009-08-24 11:10:17 +03002260 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261}
2262
Avi Kivity851ba692009-08-24 11:10:17 +03002263static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002264{
Avi Kivity851ba692009-08-24 11:10:17 +03002265 struct kvm_run *kvm_run = svm->vcpu.run;
2266
Alexander Graff0b85052008-11-25 20:17:01 +01002267 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002268 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002269 /*
2270 * If the user space waits to inject interrupts, exit as soon as
2271 * possible
2272 */
Gleb Natapov80618232009-04-21 17:44:56 +03002273 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2274 kvm_run->request_interrupt_window &&
2275 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002276 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002277 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2278 return 0;
2279 }
2280
2281 return 1;
2282}
2283
Avi Kivity851ba692009-08-24 11:10:17 +03002284static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2286 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2287 [SVM_EXIT_READ_CR4] = emulate_on_interception,
Avi Kivity80a81192007-12-06 19:50:00 +02002288 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289 /* for now: */
2290 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2291 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2292 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
Joerg Roedel1d075432007-12-06 21:02:25 +01002293 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294 [SVM_EXIT_READ_DR0] = emulate_on_interception,
2295 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2296 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2297 [SVM_EXIT_READ_DR3] = emulate_on_interception,
2298 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2299 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2300 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2301 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
2302 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
2303 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002304 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2305 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002306 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Anthony Liguori7807fa62007-04-23 09:17:21 -05002308 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
Joerg Roedel53371b52008-04-09 14:15:30 +02002309 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Joerg Roedela0698052008-04-30 17:56:01 +02002310 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002311 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312 [SVM_EXIT_SMI] = nop_on_interception,
2313 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002314 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
2316 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002317 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002318 [SVM_EXIT_INVD] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002320 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002321 [SVM_EXIT_INVLPGA] = invlpga_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322 [SVM_EXIT_IOIO] = io_interception,
2323 [SVM_EXIT_MSR] = msr_interception,
2324 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002325 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002326 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002327 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002328 [SVM_EXIT_VMLOAD] = vmload_interception,
2329 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002330 [SVM_EXIT_STGI] = stgi_interception,
2331 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002332 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002333 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002334 [SVM_EXIT_MONITOR] = invalid_op_interception,
2335 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002336 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337};
2338
Avi Kivity851ba692009-08-24 11:10:17 +03002339static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002341 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002342 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002343 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002345 trace_kvm_exit(exit_code, svm->vmcb->save.rip);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002346
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002347 if (unlikely(svm->nested.exit_required)) {
2348 nested_svm_vmexit(svm);
2349 svm->nested.exit_required = false;
2350
2351 return 1;
2352 }
2353
Alexander Grafcf74a782008-11-25 20:17:08 +01002354 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002355 int vmexit;
2356
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002357 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2358 svm->vmcb->control.exit_info_1,
2359 svm->vmcb->control.exit_info_2,
2360 svm->vmcb->control.exit_int_info,
2361 svm->vmcb->control.exit_int_info_err);
2362
Joerg Roedel410e4d52009-08-07 11:49:44 +02002363 vmexit = nested_svm_exit_special(svm);
2364
2365 if (vmexit == NESTED_EXIT_CONTINUE)
2366 vmexit = nested_svm_exit_handled(svm);
2367
2368 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002369 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002370 }
2371
Joerg Roedela5c38322009-08-07 11:49:32 +02002372 svm_complete_interrupts(svm);
2373
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002374 if (npt_enabled) {
2375 int mmu_reload = 0;
2376 if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
2377 svm_set_cr0(vcpu, svm->vmcb->save.cr0);
2378 mmu_reload = 1;
2379 }
2380 vcpu->arch.cr0 = svm->vmcb->save.cr0;
2381 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002382 if (mmu_reload) {
2383 kvm_mmu_reset_context(vcpu);
2384 kvm_mmu_load(vcpu);
2385 }
2386 }
2387
Avi Kivity04d2cc72007-09-10 18:10:54 +03002388
2389 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2390 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2391 kvm_run->fail_entry.hardware_entry_failure_reason
2392 = svm->vmcb->control.exit_code;
2393 return 0;
2394 }
2395
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002396 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002397 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002398 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2400 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002401 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402 exit_code);
2403
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002404 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002405 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002407 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408 return 0;
2409 }
2410
Avi Kivity851ba692009-08-24 11:10:17 +03002411 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002412}
2413
2414static void reload_tss(struct kvm_vcpu *vcpu)
2415{
2416 int cpu = raw_smp_processor_id();
2417
2418 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04002419 svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002420 load_TR_desc();
2421}
2422
Rusty Russelle756fc62007-07-30 20:07:08 +10002423static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002424{
2425 int cpu = raw_smp_processor_id();
2426
2427 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
2428
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002429 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002430 /* FIXME: handle wraparound of asid_generation */
2431 if (svm->asid_generation != svm_data->asid_generation)
Rusty Russelle756fc62007-07-30 20:07:08 +10002432 new_asid(svm, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433}
2434
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002435static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2436{
2437 struct vcpu_svm *svm = to_svm(vcpu);
2438
2439 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2440 vcpu->arch.hflags |= HF_NMI_MASK;
2441 svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
2442 ++vcpu->stat.nmi_injections;
2443}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444
Eddie Dong85f455f2007-07-06 12:20:49 +03002445static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446{
2447 struct vmcb_control_area *control;
2448
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002449 trace_kvm_inj_virq(irq);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002450
Avi Kivityfa89a812008-09-01 15:57:51 +03002451 ++svm->vcpu.stat.irq_injections;
Rusty Russelle756fc62007-07-30 20:07:08 +10002452 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002453 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002454 control->int_ctl &= ~V_INTR_PRIO_MASK;
2455 control->int_ctl |= V_IRQ_MASK |
2456 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2457}
2458
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002459static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002460{
2461 struct vcpu_svm *svm = to_svm(vcpu);
2462
Joerg Roedel2af91942009-08-07 11:49:28 +02002463 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01002464
Alexander Graf219b65d2009-06-15 15:21:25 +02002465 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2466 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002467}
2468
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002469static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2470{
2471 struct vcpu_svm *svm = to_svm(vcpu);
2472
2473 if (irr == -1)
2474 return;
2475
2476 if (tpr >= irr)
2477 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2478}
2479
2480static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002481{
2482 struct vcpu_svm *svm = to_svm(vcpu);
2483 struct vmcb *vmcb = svm->vmcb;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002484 return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2485 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002486}
2487
Gleb Natapov78646122009-03-23 12:12:11 +02002488static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
2489{
2490 struct vcpu_svm *svm = to_svm(vcpu);
2491 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02002492 int ret;
2493
2494 if (!gif_set(svm) ||
2495 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
2496 return 0;
2497
2498 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
2499
2500 if (is_nested(svm))
2501 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
2502
2503 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02002504}
2505
Gleb Natapov9222be12009-04-23 17:14:37 +03002506static void enable_irq_window(struct kvm_vcpu *vcpu)
2507{
Alexander Graf219b65d2009-06-15 15:21:25 +02002508 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02002509
2510 nested_svm_intr(svm);
2511
2512 /* In case GIF=0 we can't rely on the CPU to tell us when
2513 * GIF becomes 1, because that's a separate STGI/VMRUN intercept.
2514 * The next time we get that intercept, this function will be
2515 * called again though and we'll get the vintr intercept. */
Joerg Roedel2af91942009-08-07 11:49:28 +02002516 if (gif_set(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02002517 svm_set_vintr(svm);
2518 svm_inject_irq(svm, 0x0);
2519 }
Gleb Natapov9222be12009-04-23 17:14:37 +03002520}
2521
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002522static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002524 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002525
Gleb Natapov44c11432009-05-11 13:35:52 +03002526 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
2527 == HF_NMI_MASK)
2528 return; /* IRET will cause a vm exit */
2529
2530 /* Something prevents NMI from been injected. Single step over
2531 possible problem (IRET or exception injection or interrupt
2532 shadow) */
2533 vcpu->arch.singlestep = true;
2534 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
2535 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002536}
2537
Izik Eiduscbc94022007-10-25 00:29:55 +02002538static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
2539{
2540 return 0;
2541}
2542
Avi Kivityd9e368d2007-06-07 19:18:30 +03002543static void svm_flush_tlb(struct kvm_vcpu *vcpu)
2544{
2545 force_new_asid(vcpu);
2546}
2547
Avi Kivity04d2cc72007-09-10 18:10:54 +03002548static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
2549{
2550}
2551
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002552static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
2553{
2554 struct vcpu_svm *svm = to_svm(vcpu);
2555
2556 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
2557 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03002558 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002559 }
2560}
2561
Joerg Roedel649d6862008-04-16 16:51:15 +02002562static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
2563{
2564 struct vcpu_svm *svm = to_svm(vcpu);
2565 u64 cr8;
2566
Joerg Roedel649d6862008-04-16 16:51:15 +02002567 cr8 = kvm_get_cr8(vcpu);
2568 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
2569 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
2570}
2571
Gleb Natapov9222be12009-04-23 17:14:37 +03002572static void svm_complete_interrupts(struct vcpu_svm *svm)
2573{
2574 u8 vector;
2575 int type;
2576 u32 exitintinfo = svm->vmcb->control.exit_int_info;
2577
Gleb Natapov44c11432009-05-11 13:35:52 +03002578 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
2579 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
2580
Gleb Natapov9222be12009-04-23 17:14:37 +03002581 svm->vcpu.arch.nmi_injected = false;
2582 kvm_clear_exception_queue(&svm->vcpu);
2583 kvm_clear_interrupt_queue(&svm->vcpu);
2584
2585 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
2586 return;
2587
2588 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
2589 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
2590
2591 switch (type) {
2592 case SVM_EXITINTINFO_TYPE_NMI:
2593 svm->vcpu.arch.nmi_injected = true;
2594 break;
2595 case SVM_EXITINTINFO_TYPE_EXEPT:
2596 /* In case of software exception do not reinject an exception
2597 vector, but re-execute and instruction instead */
Alexander Graf219b65d2009-06-15 15:21:25 +02002598 if (is_nested(svm))
2599 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002600 if (kvm_exception_is_soft(vector))
Gleb Natapov9222be12009-04-23 17:14:37 +03002601 break;
2602 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
2603 u32 err = svm->vmcb->control.exit_int_info_err;
2604 kvm_queue_exception_e(&svm->vcpu, vector, err);
2605
2606 } else
2607 kvm_queue_exception(&svm->vcpu, vector);
2608 break;
2609 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002610 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03002611 break;
2612 default:
2613 break;
2614 }
2615}
2616
Avi Kivity80e31d42008-07-14 14:44:59 +03002617#ifdef CONFIG_X86_64
2618#define R "r"
2619#else
2620#define R "e"
2621#endif
2622
Avi Kivity851ba692009-08-24 11:10:17 +03002623static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002624{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002625 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626 u16 fs_selector;
2627 u16 gs_selector;
2628 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03002629
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002630 /*
2631 * A vmexit emulation is required before the vcpu can be executed
2632 * again.
2633 */
2634 if (unlikely(svm->nested.exit_required))
2635 return;
2636
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002637 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
2638 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2639 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
2640
Rusty Russelle756fc62007-07-30 20:07:08 +10002641 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642
Joerg Roedel649d6862008-04-16 16:51:15 +02002643 sync_lapic_to_cr8(vcpu);
2644
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645 save_host_msrs(vcpu);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002646 fs_selector = kvm_read_fs();
2647 gs_selector = kvm_read_gs();
2648 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02002649 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002650 /* required for live migration with NPT */
2651 if (npt_enabled)
2652 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653
Avi Kivity04d2cc72007-09-10 18:10:54 +03002654 clgi();
2655
2656 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08002657
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03002659 "push %%"R"bp; \n\t"
2660 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
2661 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
2662 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
2663 "mov %c[rsi](%[svm]), %%"R"si \n\t"
2664 "mov %c[rdi](%[svm]), %%"R"di \n\t"
2665 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002666#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002667 "mov %c[r8](%[svm]), %%r8 \n\t"
2668 "mov %c[r9](%[svm]), %%r9 \n\t"
2669 "mov %c[r10](%[svm]), %%r10 \n\t"
2670 "mov %c[r11](%[svm]), %%r11 \n\t"
2671 "mov %c[r12](%[svm]), %%r12 \n\t"
2672 "mov %c[r13](%[svm]), %%r13 \n\t"
2673 "mov %c[r14](%[svm]), %%r14 \n\t"
2674 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675#endif
2676
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03002678 "push %%"R"ax \n\t"
2679 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002680 __ex(SVM_VMLOAD) "\n\t"
2681 __ex(SVM_VMRUN) "\n\t"
2682 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03002683 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684
2685 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03002686 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
2687 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
2688 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
2689 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
2690 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
2691 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002692#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002693 "mov %%r8, %c[r8](%[svm]) \n\t"
2694 "mov %%r9, %c[r9](%[svm]) \n\t"
2695 "mov %%r10, %c[r10](%[svm]) \n\t"
2696 "mov %%r11, %c[r11](%[svm]) \n\t"
2697 "mov %%r12, %c[r12](%[svm]) \n\t"
2698 "mov %%r13, %c[r13](%[svm]) \n\t"
2699 "mov %%r14, %c[r14](%[svm]) \n\t"
2700 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03002702 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002704 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002705 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002706 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
2707 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
2708 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
2709 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
2710 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
2711 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002712#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002713 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
2714 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
2715 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
2716 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
2717 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
2718 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
2719 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
2720 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02002722 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03002723 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02002724#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02002725 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
2726#endif
2727 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002729 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002730 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
2731 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
2732 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002734 kvm_load_fs(fs_selector);
2735 kvm_load_gs(gs_selector);
2736 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 load_host_msrs(vcpu);
2738
2739 reload_tss(vcpu);
2740
Avi Kivity56ba47d2007-11-07 17:14:18 +02002741 local_irq_disable();
2742
2743 stgi();
2744
Joerg Roedeld7bf8222008-04-16 16:51:17 +02002745 sync_cr8_to_lapic(vcpu);
2746
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002747 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03002748
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002749 if (npt_enabled) {
2750 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
2751 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
2752 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753}
2754
Avi Kivity80e31d42008-07-14 14:44:59 +03002755#undef R
2756
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
2758{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002759 struct vcpu_svm *svm = to_svm(vcpu);
2760
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002761 if (npt_enabled) {
2762 svm->vmcb->control.nested_cr3 = root;
2763 force_new_asid(vcpu);
2764 return;
2765 }
2766
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002767 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768 force_new_asid(vcpu);
Anthony Liguori7807fa62007-04-23 09:17:21 -05002769
2770 if (vcpu->fpu_active) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002771 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
2772 svm->vmcb->save.cr0 |= X86_CR0_TS;
Anthony Liguori7807fa62007-04-23 09:17:21 -05002773 vcpu->fpu_active = 0;
2774 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775}
2776
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777static int is_disabled(void)
2778{
Joerg Roedel6031a612007-06-22 12:29:50 +03002779 u64 vm_cr;
2780
2781 rdmsrl(MSR_VM_CR, vm_cr);
2782 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
2783 return 1;
2784
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 return 0;
2786}
2787
Ingo Molnar102d8322007-02-19 14:37:47 +02002788static void
2789svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2790{
2791 /*
2792 * Patch in the VMMCALL instruction:
2793 */
2794 hypercall[0] = 0x0f;
2795 hypercall[1] = 0x01;
2796 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02002797}
2798
Yang, Sheng002c7f72007-07-31 14:23:01 +03002799static void svm_check_processor_compat(void *rtn)
2800{
2801 *(int *)rtn = 0;
2802}
2803
Avi Kivity774ead32007-12-26 13:57:04 +02002804static bool svm_cpu_has_accelerated_tpr(void)
2805{
2806 return false;
2807}
2808
Sheng Yang67253af2008-04-25 10:20:22 +08002809static int get_npt_level(void)
2810{
2811#ifdef CONFIG_X86_64
2812 return PT64_ROOT_LEVEL;
2813#else
2814 return PT32E_ROOT_LEVEL;
2815#endif
2816}
2817
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002818static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08002819{
2820 return 0;
2821}
2822
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002823static const struct trace_print_flags svm_exit_reasons_str[] = {
2824 { SVM_EXIT_READ_CR0, "read_cr0" },
2825 { SVM_EXIT_READ_CR3, "read_cr3" },
2826 { SVM_EXIT_READ_CR4, "read_cr4" },
2827 { SVM_EXIT_READ_CR8, "read_cr8" },
2828 { SVM_EXIT_WRITE_CR0, "write_cr0" },
2829 { SVM_EXIT_WRITE_CR3, "write_cr3" },
2830 { SVM_EXIT_WRITE_CR4, "write_cr4" },
2831 { SVM_EXIT_WRITE_CR8, "write_cr8" },
2832 { SVM_EXIT_READ_DR0, "read_dr0" },
2833 { SVM_EXIT_READ_DR1, "read_dr1" },
2834 { SVM_EXIT_READ_DR2, "read_dr2" },
2835 { SVM_EXIT_READ_DR3, "read_dr3" },
2836 { SVM_EXIT_WRITE_DR0, "write_dr0" },
2837 { SVM_EXIT_WRITE_DR1, "write_dr1" },
2838 { SVM_EXIT_WRITE_DR2, "write_dr2" },
2839 { SVM_EXIT_WRITE_DR3, "write_dr3" },
2840 { SVM_EXIT_WRITE_DR5, "write_dr5" },
2841 { SVM_EXIT_WRITE_DR7, "write_dr7" },
2842 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
2843 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
2844 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
2845 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
2846 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
2847 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
2848 { SVM_EXIT_INTR, "interrupt" },
2849 { SVM_EXIT_NMI, "nmi" },
2850 { SVM_EXIT_SMI, "smi" },
2851 { SVM_EXIT_INIT, "init" },
2852 { SVM_EXIT_VINTR, "vintr" },
2853 { SVM_EXIT_CPUID, "cpuid" },
2854 { SVM_EXIT_INVD, "invd" },
2855 { SVM_EXIT_HLT, "hlt" },
2856 { SVM_EXIT_INVLPG, "invlpg" },
2857 { SVM_EXIT_INVLPGA, "invlpga" },
2858 { SVM_EXIT_IOIO, "io" },
2859 { SVM_EXIT_MSR, "msr" },
2860 { SVM_EXIT_TASK_SWITCH, "task_switch" },
2861 { SVM_EXIT_SHUTDOWN, "shutdown" },
2862 { SVM_EXIT_VMRUN, "vmrun" },
2863 { SVM_EXIT_VMMCALL, "hypercall" },
2864 { SVM_EXIT_VMLOAD, "vmload" },
2865 { SVM_EXIT_VMSAVE, "vmsave" },
2866 { SVM_EXIT_STGI, "stgi" },
2867 { SVM_EXIT_CLGI, "clgi" },
2868 { SVM_EXIT_SKINIT, "skinit" },
2869 { SVM_EXIT_WBINVD, "wbinvd" },
2870 { SVM_EXIT_MONITOR, "monitor" },
2871 { SVM_EXIT_MWAIT, "mwait" },
2872 { SVM_EXIT_NPF, "npf" },
2873 { -1, NULL }
2874};
2875
Joerg Roedel344f4142009-07-27 16:30:48 +02002876static bool svm_gb_page_enable(void)
2877{
2878 return true;
2879}
2880
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002881static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 .cpu_has_kvm_support = has_svm,
2883 .disabled_by_bios = is_disabled,
2884 .hardware_setup = svm_hardware_setup,
2885 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03002886 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 .hardware_enable = svm_hardware_enable,
2888 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02002889 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002890
2891 .vcpu_create = svm_create_vcpu,
2892 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002893 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894
Avi Kivity04d2cc72007-09-10 18:10:54 +03002895 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 .vcpu_load = svm_vcpu_load,
2897 .vcpu_put = svm_vcpu_put,
2898
2899 .set_guest_debug = svm_guest_debug,
2900 .get_msr = svm_get_msr,
2901 .set_msr = svm_set_msr,
2902 .get_segment_base = svm_get_segment_base,
2903 .get_segment = svm_get_segment,
2904 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02002905 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10002906 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002907 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909 .set_cr3 = svm_set_cr3,
2910 .set_cr4 = svm_set_cr4,
2911 .set_efer = svm_set_efer,
2912 .get_idt = svm_get_idt,
2913 .set_idt = svm_set_idt,
2914 .get_gdt = svm_get_gdt,
2915 .set_gdt = svm_set_gdt,
2916 .get_dr = svm_get_dr,
2917 .set_dr = svm_set_dr,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002918 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002919 .get_rflags = svm_get_rflags,
2920 .set_rflags = svm_set_rflags,
2921
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002923
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002925 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04002927 .set_interrupt_shadow = svm_set_interrupt_shadow,
2928 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02002929 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03002930 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002931 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02002932 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02002933 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002934 .nmi_allowed = svm_nmi_allowed,
2935 .enable_nmi_window = enable_nmi_window,
2936 .enable_irq_window = enable_irq_window,
2937 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02002938
2939 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08002940 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08002941 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002942
2943 .exit_reasons_str = svm_exit_reasons_str,
Joerg Roedel344f4142009-07-27 16:30:48 +02002944 .gb_page_enable = svm_gb_page_enable,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945};
2946
2947static int __init svm_init(void)
2948{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002949 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Rusty Russellc16f8622007-07-30 21:12:19 +10002950 THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951}
2952
2953static void __exit svm_exit(void)
2954{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002955 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956}
2957
2958module_init(svm_init)
2959module_exit(svm_exit)