blob: 0989776ee7b050dd3ec000056552d782e17226f5 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020023#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/mm.h>
25#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020027#include <linux/moduleparam.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030028#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030029#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040030
Avi Kivity6aa8b732006-12-10 02:21:36 -080031#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080032#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020033#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020034#include <asm/virtext.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Avi Kivity4ecac3f2008-05-13 13:23:38 +030036#define __ex(x) __kvm_handle_fault_on_reboot(x)
37
Avi Kivity6aa8b732006-12-10 02:21:36 -080038MODULE_AUTHOR("Qumranet");
39MODULE_LICENSE("GPL");
40
Avi Kivityc7addb92007-09-16 18:58:32 +020041static int bypass_guest_pf = 1;
42module_param(bypass_guest_pf, bool, 0);
43
Sheng Yang2384d2b2008-01-17 15:14:33 +080044static int enable_vpid = 1;
45module_param(enable_vpid, bool, 0);
46
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020047static int flexpriority_enabled = 1;
48module_param(flexpriority_enabled, bool, 0);
49
Sheng Yang14394422008-04-28 12:24:45 +080050static int enable_ept = 1;
Sheng Yangd56f5462008-04-25 10:13:16 +080051module_param(enable_ept, bool, 0);
52
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030053static int emulate_invalid_guest_state = 0;
54module_param(emulate_invalid_guest_state, bool, 0);
55
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040056struct vmcs {
57 u32 revision_id;
58 u32 abort;
59 char data[0];
60};
61
62struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100063 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030064 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030065 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040066 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030067 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020068 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040069 struct kvm_msr_entry *guest_msrs;
70 struct kvm_msr_entry *host_msrs;
71 int nmsrs;
72 int save_nmsrs;
73 int msr_offset_efer;
74#ifdef CONFIG_X86_64
75 int msr_offset_kernel_gs_base;
76#endif
77 struct vmcs *vmcs;
78 struct {
79 int loaded;
80 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020081 int gs_ldt_reload_needed;
82 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030083 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040084 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020085 struct {
86 struct {
87 bool pending;
88 u8 vector;
89 unsigned rip;
90 } irq;
91 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080092 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030093 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +020094
95 /* Support for vnmi-less CPUs */
96 int soft_vnmi_blocked;
97 ktime_t entry_time;
98 s64 vnmi_blocked_time;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040099};
100
101static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
102{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000103 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400104}
105
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800106static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800107static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300108
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109static DEFINE_PER_CPU(struct vmcs *, vmxarea);
110static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300111static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112
He, Qingfdef3ad2007-04-30 09:45:24 +0300113static struct page *vmx_io_bitmap_a;
114static struct page *vmx_io_bitmap_b;
Sheng Yang25c5f222008-03-28 13:18:56 +0800115static struct page *vmx_msr_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300116
Sheng Yang2384d2b2008-01-17 15:14:33 +0800117static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
118static DEFINE_SPINLOCK(vmx_vpid_lock);
119
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300120static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121 int size;
122 int order;
123 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300124 u32 pin_based_exec_ctrl;
125 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800126 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300127 u32 vmexit_ctrl;
128 u32 vmentry_ctrl;
129} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800130
Hannes Ederefff9e52008-11-28 17:02:06 +0100131static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800132 u32 ept;
133 u32 vpid;
134} vmx_capability;
135
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136#define VMX_SEGMENT_FIELD(seg) \
137 [VCPU_SREG_##seg] = { \
138 .selector = GUEST_##seg##_SELECTOR, \
139 .base = GUEST_##seg##_BASE, \
140 .limit = GUEST_##seg##_LIMIT, \
141 .ar_bytes = GUEST_##seg##_AR_BYTES, \
142 }
143
144static struct kvm_vmx_segment_field {
145 unsigned selector;
146 unsigned base;
147 unsigned limit;
148 unsigned ar_bytes;
149} kvm_vmx_segment_fields[] = {
150 VMX_SEGMENT_FIELD(CS),
151 VMX_SEGMENT_FIELD(DS),
152 VMX_SEGMENT_FIELD(ES),
153 VMX_SEGMENT_FIELD(FS),
154 VMX_SEGMENT_FIELD(GS),
155 VMX_SEGMENT_FIELD(SS),
156 VMX_SEGMENT_FIELD(TR),
157 VMX_SEGMENT_FIELD(LDTR),
158};
159
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300160/*
161 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
162 * away by decrementing the array size.
163 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800165#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
167#endif
168 MSR_EFER, MSR_K6_STAR,
169};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200170#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400172static void load_msrs(struct kvm_msr_entry *e, int n)
173{
174 int i;
175
176 for (i = 0; i < n; ++i)
177 wrmsrl(e[i].index, e[i].data);
178}
179
180static void save_msrs(struct kvm_msr_entry *e, int n)
181{
182 int i;
183
184 for (i = 0; i < n; ++i)
185 rdmsrl(e[i].index, e[i].data);
186}
187
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188static inline int is_page_fault(u32 intr_info)
189{
190 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
191 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100192 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193}
194
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300195static inline int is_no_device(u32 intr_info)
196{
197 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
198 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100199 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300200}
201
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500202static inline int is_invalid_opcode(u32 intr_info)
203{
204 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
205 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100206 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500207}
208
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209static inline int is_external_interrupt(u32 intr_info)
210{
211 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
212 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
213}
214
Sheng Yang25c5f222008-03-28 13:18:56 +0800215static inline int cpu_has_vmx_msr_bitmap(void)
216{
217 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
218}
219
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800220static inline int cpu_has_vmx_tpr_shadow(void)
221{
222 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
223}
224
225static inline int vm_need_tpr_shadow(struct kvm *kvm)
226{
227 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
228}
229
Sheng Yangf78e0e22007-10-29 09:40:42 +0800230static inline int cpu_has_secondary_exec_ctrls(void)
231{
232 return (vmcs_config.cpu_based_exec_ctrl &
233 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
234}
235
Avi Kivity774ead32007-12-26 13:57:04 +0200236static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800237{
Avi Kivity4c9fc8e2008-03-24 18:15:14 +0200238 return flexpriority_enabled
239 && (vmcs_config.cpu_based_2nd_exec_ctrl &
240 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800241}
242
Sheng Yangd56f5462008-04-25 10:13:16 +0800243static inline int cpu_has_vmx_invept_individual_addr(void)
244{
245 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT));
246}
247
248static inline int cpu_has_vmx_invept_context(void)
249{
250 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT));
251}
252
253static inline int cpu_has_vmx_invept_global(void)
254{
255 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT));
256}
257
258static inline int cpu_has_vmx_ept(void)
259{
260 return (vmcs_config.cpu_based_2nd_exec_ctrl &
261 SECONDARY_EXEC_ENABLE_EPT);
262}
263
264static inline int vm_need_ept(void)
265{
266 return (cpu_has_vmx_ept() && enable_ept);
267}
268
Sheng Yangf78e0e22007-10-29 09:40:42 +0800269static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
270{
271 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
272 (irqchip_in_kernel(kvm)));
273}
274
Sheng Yang2384d2b2008-01-17 15:14:33 +0800275static inline int cpu_has_vmx_vpid(void)
276{
277 return (vmcs_config.cpu_based_2nd_exec_ctrl &
278 SECONDARY_EXEC_ENABLE_VPID);
279}
280
Sheng Yangf08864b2008-05-15 18:23:25 +0800281static inline int cpu_has_virtual_nmis(void)
282{
283 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
284}
285
Rusty Russell8b9cf982007-07-30 16:31:43 +1000286static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800287{
288 int i;
289
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400290 for (i = 0; i < vmx->nmsrs; ++i)
291 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300292 return i;
293 return -1;
294}
295
Sheng Yang2384d2b2008-01-17 15:14:33 +0800296static inline void __invvpid(int ext, u16 vpid, gva_t gva)
297{
298 struct {
299 u64 vpid : 16;
300 u64 rsvd : 48;
301 u64 gva;
302 } operand = { vpid, 0, gva };
303
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300304 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800305 /* CF==1 or ZF==1 --> rc = -1 */
306 "; ja 1f ; ud2 ; 1:"
307 : : "a"(&operand), "c"(ext) : "cc", "memory");
308}
309
Sheng Yang14394422008-04-28 12:24:45 +0800310static inline void __invept(int ext, u64 eptp, gpa_t gpa)
311{
312 struct {
313 u64 eptp, gpa;
314 } operand = {eptp, gpa};
315
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300316 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800317 /* CF==1 or ZF==1 --> rc = -1 */
318 "; ja 1f ; ud2 ; 1:\n"
319 : : "a" (&operand), "c" (ext) : "cc", "memory");
320}
321
Rusty Russell8b9cf982007-07-30 16:31:43 +1000322static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300323{
324 int i;
325
Rusty Russell8b9cf982007-07-30 16:31:43 +1000326 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300327 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400328 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000329 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800330}
331
Avi Kivity6aa8b732006-12-10 02:21:36 -0800332static void vmcs_clear(struct vmcs *vmcs)
333{
334 u64 phys_addr = __pa(vmcs);
335 u8 error;
336
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300337 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
339 : "cc", "memory");
340 if (error)
341 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
342 vmcs, phys_addr);
343}
344
345static void __vcpu_clear(void *arg)
346{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000347 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800348 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800349
Rusty Russell8b9cf982007-07-30 16:31:43 +1000350 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400351 vmcs_clear(vmx->vmcs);
352 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800354 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300355 list_del(&vmx->local_vcpus_link);
356 vmx->vcpu.cpu = -1;
357 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358}
359
Rusty Russell8b9cf982007-07-30 16:31:43 +1000360static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800361{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200362 if (vmx->vcpu.cpu == -1)
363 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200364 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800365}
366
Sheng Yang2384d2b2008-01-17 15:14:33 +0800367static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
368{
369 if (vmx->vpid == 0)
370 return;
371
372 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
373}
374
Sheng Yang14394422008-04-28 12:24:45 +0800375static inline void ept_sync_global(void)
376{
377 if (cpu_has_vmx_invept_global())
378 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
379}
380
381static inline void ept_sync_context(u64 eptp)
382{
383 if (vm_need_ept()) {
384 if (cpu_has_vmx_invept_context())
385 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
386 else
387 ept_sync_global();
388 }
389}
390
391static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
392{
393 if (vm_need_ept()) {
394 if (cpu_has_vmx_invept_individual_addr())
395 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
396 eptp, gpa);
397 else
398 ept_sync_context(eptp);
399 }
400}
401
Avi Kivity6aa8b732006-12-10 02:21:36 -0800402static unsigned long vmcs_readl(unsigned long field)
403{
404 unsigned long value;
405
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300406 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800407 : "=a"(value) : "d"(field) : "cc");
408 return value;
409}
410
411static u16 vmcs_read16(unsigned long field)
412{
413 return vmcs_readl(field);
414}
415
416static u32 vmcs_read32(unsigned long field)
417{
418 return vmcs_readl(field);
419}
420
421static u64 vmcs_read64(unsigned long field)
422{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800423#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424 return vmcs_readl(field);
425#else
426 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
427#endif
428}
429
Avi Kivitye52de1b2007-01-05 16:36:56 -0800430static noinline void vmwrite_error(unsigned long field, unsigned long value)
431{
432 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
433 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
434 dump_stack();
435}
436
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437static void vmcs_writel(unsigned long field, unsigned long value)
438{
439 u8 error;
440
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300441 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400442 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800443 if (unlikely(error))
444 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800445}
446
447static void vmcs_write16(unsigned long field, u16 value)
448{
449 vmcs_writel(field, value);
450}
451
452static void vmcs_write32(unsigned long field, u32 value)
453{
454 vmcs_writel(field, value);
455}
456
457static void vmcs_write64(unsigned long field, u64 value)
458{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300460#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461 asm volatile ("");
462 vmcs_writel(field+1, value >> 32);
463#endif
464}
465
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300466static void vmcs_clear_bits(unsigned long field, u32 mask)
467{
468 vmcs_writel(field, vmcs_readl(field) & ~mask);
469}
470
471static void vmcs_set_bits(unsigned long field, u32 mask)
472{
473 vmcs_writel(field, vmcs_readl(field) | mask);
474}
475
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300476static void update_exception_bitmap(struct kvm_vcpu *vcpu)
477{
478 u32 eb;
479
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500480 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300481 if (!vcpu->fpu_active)
482 eb |= 1u << NM_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100483 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
484 if (vcpu->guest_debug &
485 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
486 eb |= 1u << DB_VECTOR;
487 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
488 eb |= 1u << BP_VECTOR;
489 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800490 if (vcpu->arch.rmode.active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300491 eb = ~0;
Sheng Yang14394422008-04-28 12:24:45 +0800492 if (vm_need_ept())
493 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300494 vmcs_write32(EXCEPTION_BITMAP, eb);
495}
496
Avi Kivity33ed6322007-05-02 16:54:03 +0300497static void reload_tss(void)
498{
Avi Kivity33ed6322007-05-02 16:54:03 +0300499 /*
500 * VT restores TR but not its size. Useless.
501 */
502 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200503 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300504
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300505 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300506 descs = (void *)gdt.base;
507 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
508 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300509}
510
Rusty Russell8b9cf982007-07-30 16:31:43 +1000511static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300512{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400513 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300514 u64 host_efer = vmx->host_msrs[efer_offset].data;
515 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
516 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300517
Avi Kivity51c6cf62007-08-29 03:48:05 +0300518 if (efer_offset < 0)
519 return;
520 /*
521 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
522 * outside long mode
523 */
524 ignore_bits = EFER_NX | EFER_SCE;
525#ifdef CONFIG_X86_64
526 ignore_bits |= EFER_LMA | EFER_LME;
527 /* SCE is meaningful only in long mode on Intel */
528 if (guest_efer & EFER_LMA)
529 ignore_bits &= ~(u64)EFER_SCE;
530#endif
531 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
532 return;
533
534 vmx->host_state.guest_efer_loaded = 1;
535 guest_efer &= ~ignore_bits;
536 guest_efer |= host_efer & ignore_bits;
537 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000538 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300539}
540
Avi Kivity51c6cf62007-08-29 03:48:05 +0300541static void reload_host_efer(struct vcpu_vmx *vmx)
542{
543 if (vmx->host_state.guest_efer_loaded) {
544 vmx->host_state.guest_efer_loaded = 0;
545 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
546 }
547}
548
Avi Kivity04d2cc72007-09-10 18:10:54 +0300549static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300550{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300551 struct vcpu_vmx *vmx = to_vmx(vcpu);
552
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400553 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300554 return;
555
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400556 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300557 /*
558 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
559 * allow segment selectors with cpl > 0 or ti == 1.
560 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300561 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200562 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300563 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200564 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400565 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200566 vmx->host_state.fs_reload_needed = 0;
567 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300568 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200569 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300570 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300571 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400572 if (!(vmx->host_state.gs_sel & 7))
573 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300574 else {
575 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200576 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300577 }
578
579#ifdef CONFIG_X86_64
580 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
581 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
582#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400583 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
584 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300585#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300586
587#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400588 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400589 save_msrs(vmx->host_msrs +
590 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400591
Avi Kivity707c0872007-05-02 17:33:43 +0300592#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400593 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300594 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300595}
596
Avi Kivitya9b21b62008-06-24 11:48:49 +0300597static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300598{
Avi Kivity15ad7142007-07-11 18:17:21 +0300599 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300600
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400601 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300602 return;
603
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200604 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400605 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200606 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300607 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200608 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300609 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300610 /*
611 * If we have to reload gs, we must take care to
612 * preserve our gs base.
613 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300614 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300615 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300616#ifdef CONFIG_X86_64
617 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
618#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300619 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300620 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200621 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400622 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
623 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300624 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300625}
626
Avi Kivitya9b21b62008-06-24 11:48:49 +0300627static void vmx_load_host_state(struct vcpu_vmx *vmx)
628{
629 preempt_disable();
630 __vmx_load_host_state(vmx);
631 preempt_enable();
632}
633
Avi Kivity6aa8b732006-12-10 02:21:36 -0800634/*
635 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
636 * vcpu mutex is already taken.
637 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300638static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400640 struct vcpu_vmx *vmx = to_vmx(vcpu);
641 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200642 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643
Eddie Donga3d7f852007-09-03 16:15:12 +0300644 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000645 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300646 kvm_migrate_timers(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800647 vpid_sync_vcpu_all(vmx);
Avi Kivity543e4242008-05-13 16:22:47 +0300648 local_irq_disable();
649 list_add(&vmx->local_vcpus_link,
650 &per_cpu(vcpus_on_cpu, cpu));
651 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300652 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400654 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800655 u8 error;
656
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400657 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300658 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
660 : "cc");
661 if (error)
662 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400663 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 }
665
666 if (vcpu->cpu != cpu) {
667 struct descriptor_table dt;
668 unsigned long sysenter_esp;
669
670 vcpu->cpu = cpu;
671 /*
672 * Linux uses per-cpu TSS and GDT, so set these when switching
673 * processors.
674 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300675 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
676 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
678
679 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
680 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300681
682 /*
683 * Make sure the time stamp counter is monotonous.
684 */
685 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200686 if (tsc_this < vcpu->arch.host_tsc) {
687 delta = vcpu->arch.host_tsc - tsc_this;
688 new_offset = vmcs_read64(TSC_OFFSET) + delta;
689 vmcs_write64(TSC_OFFSET, new_offset);
690 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692}
693
694static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
695{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300696 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800697}
698
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300699static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
700{
701 if (vcpu->fpu_active)
702 return;
703 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000704 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800705 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92f2007-07-17 23:19:08 +1000706 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300707 update_exception_bitmap(vcpu);
708}
709
710static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
711{
712 if (!vcpu->fpu_active)
713 return;
714 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000715 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300716 update_exception_bitmap(vcpu);
717}
718
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
720{
721 return vmcs_readl(GUEST_RFLAGS);
722}
723
724static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
725{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800726 if (vcpu->arch.rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100727 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 vmcs_writel(GUEST_RFLAGS, rflags);
729}
730
731static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
732{
733 unsigned long rip;
734 u32 interruptibility;
735
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300736 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300738 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739
740 /*
741 * We emulated an instruction, so temporary interrupt blocking
742 * should be removed, if set.
743 */
744 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
745 if (interruptibility & 3)
746 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
747 interruptibility & ~3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800748 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749}
750
Avi Kivity298101d2007-11-25 13:41:11 +0200751static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
752 bool has_error_code, u32 error_code)
753{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200754 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100755 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200756
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100757 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200758 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100759 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
760 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200761
762 if (vcpu->arch.rmode.active) {
763 vmx->rmode.irq.pending = true;
764 vmx->rmode.irq.vector = nr;
765 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100766 if (nr == BP_VECTOR || nr == OF_VECTOR)
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200767 vmx->rmode.irq.rip++;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100768 intr_info |= INTR_TYPE_SOFT_INTR;
769 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200770 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
771 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
772 return;
773 }
774
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100775 if (nr == BP_VECTOR || nr == OF_VECTOR) {
776 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
777 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
778 } else
779 intr_info |= INTR_TYPE_HARD_EXCEPTION;
780
781 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200782}
783
784static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
785{
Avi Kivity35920a32008-07-03 14:50:12 +0300786 return false;
Avi Kivity298101d2007-11-25 13:41:11 +0200787}
788
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789/*
Eddie Donga75beee2007-05-17 18:55:15 +0300790 * Swap MSR entry in host/guest MSR entry array.
791 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200792#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000793static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300794{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400795 struct kvm_msr_entry tmp;
796
797 tmp = vmx->guest_msrs[to];
798 vmx->guest_msrs[to] = vmx->guest_msrs[from];
799 vmx->guest_msrs[from] = tmp;
800 tmp = vmx->host_msrs[to];
801 vmx->host_msrs[to] = vmx->host_msrs[from];
802 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300803}
Gabriel C54e11fa2007-08-01 16:23:10 +0200804#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300805
806/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300807 * Set up the vmcs to automatically save and restore system
808 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
809 * mode, as fiddling with msrs is very expensive.
810 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000811static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300812{
Eddie Dong2cc51562007-05-21 07:28:09 +0300813 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300814
Avi Kivity33f9c502008-02-27 16:06:57 +0200815 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300816 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300817#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000818 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300819 int index;
820
Rusty Russell8b9cf982007-07-30 16:31:43 +1000821 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300822 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000823 move_msr_up(vmx, index, save_nmsrs++);
824 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300825 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000826 move_msr_up(vmx, index, save_nmsrs++);
827 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300828 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000829 move_msr_up(vmx, index, save_nmsrs++);
830 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300831 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000832 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300833 /*
834 * MSR_K6_STAR is only needed on long mode guests, and only
835 * if efer.sce is enabled.
836 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000837 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800838 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000839 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300840 }
Eddie Donga75beee2007-05-17 18:55:15 +0300841#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400842 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300843
Eddie Donga75beee2007-05-17 18:55:15 +0300844#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400845 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000846 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300847#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000848 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300849}
850
851/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 * reads and returns guest's timestamp counter "register"
853 * guest_tsc = host_tsc + tsc_offset -- 21.3
854 */
855static u64 guest_read_tsc(void)
856{
857 u64 host_tsc, tsc_offset;
858
859 rdtscll(host_tsc);
860 tsc_offset = vmcs_read64(TSC_OFFSET);
861 return host_tsc + tsc_offset;
862}
863
864/*
865 * writes 'guest_tsc' into guest's timestamp counter "register"
866 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
867 */
868static void guest_write_tsc(u64 guest_tsc)
869{
870 u64 host_tsc;
871
872 rdtscll(host_tsc);
873 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
874}
875
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876/*
877 * Reads an msr value (of 'msr_index') into 'pdata'.
878 * Returns 0 on success, non-0 otherwise.
879 * Assumes vcpu_load() was already called.
880 */
881static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
882{
883 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400884 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800885
886 if (!pdata) {
887 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
888 return -EINVAL;
889 }
890
891 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800892#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 case MSR_FS_BASE:
894 data = vmcs_readl(GUEST_FS_BASE);
895 break;
896 case MSR_GS_BASE:
897 data = vmcs_readl(GUEST_GS_BASE);
898 break;
899 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800900 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800901#endif
902 case MSR_IA32_TIME_STAMP_COUNTER:
903 data = guest_read_tsc();
904 break;
905 case MSR_IA32_SYSENTER_CS:
906 data = vmcs_read32(GUEST_SYSENTER_CS);
907 break;
908 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200909 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 break;
911 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200912 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 default:
Avi Kivity516a1a72009-02-15 02:32:07 +0200915 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +1000916 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800917 if (msr) {
918 data = msr->data;
919 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800921 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922 }
923
924 *pdata = data;
925 return 0;
926}
927
928/*
929 * Writes msr value into into the appropriate "register".
930 * Returns 0 on success, non-0 otherwise.
931 * Assumes vcpu_load() was already called.
932 */
933static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
934{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400935 struct vcpu_vmx *vmx = to_vmx(vcpu);
936 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300937 int ret = 0;
938
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800940#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800941 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300942 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +0300943 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +0300944 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800945 case MSR_FS_BASE:
946 vmcs_writel(GUEST_FS_BASE, data);
947 break;
948 case MSR_GS_BASE:
949 vmcs_writel(GUEST_GS_BASE, data);
950 break;
951#endif
952 case MSR_IA32_SYSENTER_CS:
953 vmcs_write32(GUEST_SYSENTER_CS, data);
954 break;
955 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200956 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 break;
958 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200959 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200961 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 guest_write_tsc(data);
963 break;
Chris Lalancetteefa67e02008-06-20 09:51:30 +0200964 case MSR_P6_PERFCTR0:
965 case MSR_P6_PERFCTR1:
966 case MSR_P6_EVNTSEL0:
967 case MSR_P6_EVNTSEL1:
968 /*
969 * Just discard all writes to the performance counters; this
970 * should keep both older linux and windows 64-bit guests
971 * happy
972 */
973 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data);
974
975 break;
Sheng Yang468d4722008-10-09 16:01:55 +0800976 case MSR_IA32_CR_PAT:
977 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
978 vmcs_write64(GUEST_IA32_PAT, data);
979 vcpu->arch.pat = data;
980 break;
981 }
982 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983 default:
Avi Kivitya9b21b62008-06-24 11:48:49 +0300984 vmx_load_host_state(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000985 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800986 if (msr) {
987 msr->data = data;
988 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300990 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 }
992
Eddie Dong2cc51562007-05-21 07:28:09 +0300993 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994}
995
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300996static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300998 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
999 switch (reg) {
1000 case VCPU_REGS_RSP:
1001 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1002 break;
1003 case VCPU_REGS_RIP:
1004 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1005 break;
1006 default:
1007 break;
1008 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009}
1010
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001011static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001013 int old_debug = vcpu->guest_debug;
1014 unsigned long flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001016 vcpu->guest_debug = dbg->control;
1017 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
1018 vcpu->guest_debug = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001020 flags = vmcs_readl(GUEST_RFLAGS);
1021 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1022 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1023 else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001025 vmcs_writel(GUEST_RFLAGS, flags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001027 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028
1029 return 0;
1030}
1031
Eddie Dong2a8067f2007-08-06 16:29:07 +03001032static int vmx_get_irq(struct kvm_vcpu *vcpu)
1033{
Avi Kivityf7d92382008-07-03 16:14:28 +03001034 if (!vcpu->arch.interrupt.pending)
1035 return -1;
1036 return vcpu->arch.interrupt.nr;
Eddie Dong2a8067f2007-08-06 16:29:07 +03001037}
1038
Avi Kivity6aa8b732006-12-10 02:21:36 -08001039static __init int cpu_has_kvm_support(void)
1040{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001041 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042}
1043
1044static __init int vmx_disabled_by_bios(void)
1045{
1046 u64 msr;
1047
1048 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001049 return (msr & (FEATURE_CONTROL_LOCKED |
1050 FEATURE_CONTROL_VMXON_ENABLED))
1051 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001052 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053}
1054
Avi Kivity774c47f2007-02-12 00:54:47 -08001055static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056{
1057 int cpu = raw_smp_processor_id();
1058 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1059 u64 old;
1060
Avi Kivity543e4242008-05-13 16:22:47 +03001061 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001062 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001063 if ((old & (FEATURE_CONTROL_LOCKED |
1064 FEATURE_CONTROL_VMXON_ENABLED))
1065 != (FEATURE_CONTROL_LOCKED |
1066 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001068 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001069 FEATURE_CONTROL_LOCKED |
1070 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001071 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001072 asm volatile (ASM_VMX_VMXON_RAX
1073 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 : "memory", "cc");
1075}
1076
Avi Kivity543e4242008-05-13 16:22:47 +03001077static void vmclear_local_vcpus(void)
1078{
1079 int cpu = raw_smp_processor_id();
1080 struct vcpu_vmx *vmx, *n;
1081
1082 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1083 local_vcpus_link)
1084 __vcpu_clear(vmx);
1085}
1086
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001087
1088/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1089 * tricks.
1090 */
1091static void kvm_cpu_vmxoff(void)
1092{
1093 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1094 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1095}
1096
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097static void hardware_disable(void *garbage)
1098{
Avi Kivity543e4242008-05-13 16:22:47 +03001099 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001100 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101}
1102
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001103static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001104 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105{
1106 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001107 u32 ctl = ctl_min | ctl_opt;
1108
1109 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1110
1111 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1112 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1113
1114 /* Ensure minimum (required) set of control bits are supported. */
1115 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001116 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001117
1118 *result = ctl;
1119 return 0;
1120}
1121
Yang, Sheng002c7f72007-07-31 14:23:01 +03001122static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001123{
1124 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001125 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001126 u32 _pin_based_exec_control = 0;
1127 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001128 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001129 u32 _vmexit_control = 0;
1130 u32 _vmentry_control = 0;
1131
1132 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001133 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001134 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1135 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001136 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001137
1138 min = CPU_BASED_HLT_EXITING |
1139#ifdef CONFIG_X86_64
1140 CPU_BASED_CR8_LOAD_EXITING |
1141 CPU_BASED_CR8_STORE_EXITING |
1142#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001143 CPU_BASED_CR3_LOAD_EXITING |
1144 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001145 CPU_BASED_USE_IO_BITMAPS |
1146 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001147 CPU_BASED_USE_TSC_OFFSETING |
1148 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001149 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001150 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001151 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001152 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1153 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001154 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001155#ifdef CONFIG_X86_64
1156 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1157 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1158 ~CPU_BASED_CR8_STORE_EXITING;
1159#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001160 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001161 min2 = 0;
1162 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001163 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001164 SECONDARY_EXEC_ENABLE_VPID |
1165 SECONDARY_EXEC_ENABLE_EPT;
1166 if (adjust_vmx_controls(min2, opt2,
1167 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001168 &_cpu_based_2nd_exec_control) < 0)
1169 return -EIO;
1170 }
1171#ifndef CONFIG_X86_64
1172 if (!(_cpu_based_2nd_exec_control &
1173 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1174 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1175#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001176 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001177 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1178 enabled */
Sheng Yangd56f5462008-04-25 10:13:16 +08001179 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001180 CPU_BASED_CR3_STORE_EXITING |
1181 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001182 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1183 &_cpu_based_exec_control) < 0)
1184 return -EIO;
1185 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1186 vmx_capability.ept, vmx_capability.vpid);
1187 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001188
1189 min = 0;
1190#ifdef CONFIG_X86_64
1191 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1192#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001193 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001194 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1195 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001196 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001197
Sheng Yang468d4722008-10-09 16:01:55 +08001198 min = 0;
1199 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001200 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1201 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001202 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001204 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001205
1206 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1207 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001208 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001209
1210#ifdef CONFIG_X86_64
1211 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1212 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001213 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001214#endif
1215
1216 /* Require Write-Back (WB) memory type for VMCS accesses. */
1217 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001218 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001219
Yang, Sheng002c7f72007-07-31 14:23:01 +03001220 vmcs_conf->size = vmx_msr_high & 0x1fff;
1221 vmcs_conf->order = get_order(vmcs_config.size);
1222 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001223
Yang, Sheng002c7f72007-07-31 14:23:01 +03001224 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1225 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001226 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001227 vmcs_conf->vmexit_ctrl = _vmexit_control;
1228 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001229
1230 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001231}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232
1233static struct vmcs *alloc_vmcs_cpu(int cpu)
1234{
1235 int node = cpu_to_node(cpu);
1236 struct page *pages;
1237 struct vmcs *vmcs;
1238
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001239 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240 if (!pages)
1241 return NULL;
1242 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001243 memset(vmcs, 0, vmcs_config.size);
1244 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245 return vmcs;
1246}
1247
1248static struct vmcs *alloc_vmcs(void)
1249{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001250 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251}
1252
1253static void free_vmcs(struct vmcs *vmcs)
1254{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001255 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256}
1257
Sam Ravnborg39959582007-06-01 00:47:13 -07001258static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259{
1260 int cpu;
1261
1262 for_each_online_cpu(cpu)
1263 free_vmcs(per_cpu(vmxarea, cpu));
1264}
1265
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266static __init int alloc_kvm_area(void)
1267{
1268 int cpu;
1269
1270 for_each_online_cpu(cpu) {
1271 struct vmcs *vmcs;
1272
1273 vmcs = alloc_vmcs_cpu(cpu);
1274 if (!vmcs) {
1275 free_kvm_area();
1276 return -ENOMEM;
1277 }
1278
1279 per_cpu(vmxarea, cpu) = vmcs;
1280 }
1281 return 0;
1282}
1283
1284static __init int hardware_setup(void)
1285{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001286 if (setup_vmcs_config(&vmcs_config) < 0)
1287 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001288
1289 if (boot_cpu_has(X86_FEATURE_NX))
1290 kvm_enable_efer_bits(EFER_NX);
1291
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292 return alloc_kvm_area();
1293}
1294
1295static __exit void hardware_unsetup(void)
1296{
1297 free_kvm_area();
1298}
1299
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1301{
1302 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1303
Avi Kivity6af11b92007-03-19 13:18:10 +02001304 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305 vmcs_write16(sf->selector, save->selector);
1306 vmcs_writel(sf->base, save->base);
1307 vmcs_write32(sf->limit, save->limit);
1308 vmcs_write32(sf->ar_bytes, save->ar);
1309 } else {
1310 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1311 << AR_DPL_SHIFT;
1312 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1313 }
1314}
1315
1316static void enter_pmode(struct kvm_vcpu *vcpu)
1317{
1318 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001319 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001321 vmx->emulation_required = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001322 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001324 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1325 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1326 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327
1328 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001329 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001330 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331 vmcs_writel(GUEST_RFLAGS, flags);
1332
Rusty Russell66aee912007-07-17 23:34:16 +10001333 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1334 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335
1336 update_exception_bitmap(vcpu);
1337
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001338 if (emulate_invalid_guest_state)
1339 return;
1340
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001341 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1342 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1343 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1344 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345
1346 vmcs_write16(GUEST_SS_SELECTOR, 0);
1347 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1348
1349 vmcs_write16(GUEST_CS_SELECTOR,
1350 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1351 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1352}
1353
Mike Dayd77c26f2007-10-08 09:02:08 -04001354static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001356 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001357 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1358 kvm->memslots[0].npages - 3;
1359 return base_gfn << PAGE_SHIFT;
1360 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001361 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362}
1363
1364static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1365{
1366 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1367
1368 save->selector = vmcs_read16(sf->selector);
1369 save->base = vmcs_readl(sf->base);
1370 save->limit = vmcs_read32(sf->limit);
1371 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001372 vmcs_write16(sf->selector, save->base >> 4);
1373 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374 vmcs_write32(sf->limit, 0xffff);
1375 vmcs_write32(sf->ar_bytes, 0xf3);
1376}
1377
1378static void enter_rmode(struct kvm_vcpu *vcpu)
1379{
1380 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001381 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001383 vmx->emulation_required = 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001384 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001386 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1388
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001389 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1391
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001392 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1394
1395 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001396 vcpu->arch.rmode.save_iopl
1397 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001399 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400
1401 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001402 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403 update_exception_bitmap(vcpu);
1404
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001405 if (emulate_invalid_guest_state)
1406 goto continue_rmode;
1407
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1409 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1410 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1411
1412 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001413 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001414 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1415 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1417
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001418 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1419 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1420 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1421 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001422
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001423continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001424 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001425 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426}
1427
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001428#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429
1430static void enter_lmode(struct kvm_vcpu *vcpu)
1431{
1432 u32 guest_tr_ar;
1433
1434 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1435 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1436 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001437 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438 vmcs_write32(GUEST_TR_AR_BYTES,
1439 (guest_tr_ar & ~AR_TYPE_MASK)
1440 | AR_TYPE_BUSY_64_TSS);
1441 }
1442
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001443 vcpu->arch.shadow_efer |= EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444
Rusty Russell8b9cf982007-07-30 16:31:43 +10001445 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446 vmcs_write32(VM_ENTRY_CONTROLS,
1447 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001448 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449}
1450
1451static void exit_lmode(struct kvm_vcpu *vcpu)
1452{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001453 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454
1455 vmcs_write32(VM_ENTRY_CONTROLS,
1456 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001457 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458}
1459
1460#endif
1461
Sheng Yang2384d2b2008-01-17 15:14:33 +08001462static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1463{
1464 vpid_sync_vcpu_all(to_vmx(vcpu));
Sheng Yang4e1096d2008-07-06 19:16:51 +08001465 if (vm_need_ept())
1466 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001467}
1468
Anthony Liguori25c4c272007-04-27 09:29:21 +03001469static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001470{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001471 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1472 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001473}
1474
Sheng Yang14394422008-04-28 12:24:45 +08001475static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1476{
1477 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1478 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1479 printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
1480 return;
1481 }
1482 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1483 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1484 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1485 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1486 }
1487}
1488
1489static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1490
1491static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1492 unsigned long cr0,
1493 struct kvm_vcpu *vcpu)
1494{
1495 if (!(cr0 & X86_CR0_PG)) {
1496 /* From paging/starting to nonpaging */
1497 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001498 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001499 (CPU_BASED_CR3_LOAD_EXITING |
1500 CPU_BASED_CR3_STORE_EXITING));
1501 vcpu->arch.cr0 = cr0;
1502 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1503 *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
1504 *hw_cr0 &= ~X86_CR0_WP;
1505 } else if (!is_paging(vcpu)) {
1506 /* From nonpaging to paging */
1507 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001508 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001509 ~(CPU_BASED_CR3_LOAD_EXITING |
1510 CPU_BASED_CR3_STORE_EXITING));
1511 vcpu->arch.cr0 = cr0;
1512 vmx_set_cr4(vcpu, vcpu->arch.cr4);
1513 if (!(vcpu->arch.cr0 & X86_CR0_WP))
1514 *hw_cr0 &= ~X86_CR0_WP;
1515 }
1516}
1517
1518static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1519 struct kvm_vcpu *vcpu)
1520{
1521 if (!is_paging(vcpu)) {
1522 *hw_cr4 &= ~X86_CR4_PAE;
1523 *hw_cr4 |= X86_CR4_PSE;
1524 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1525 *hw_cr4 &= ~X86_CR4_PAE;
1526}
1527
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1529{
Sheng Yang14394422008-04-28 12:24:45 +08001530 unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
1531 KVM_VM_CR0_ALWAYS_ON;
1532
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001533 vmx_fpu_deactivate(vcpu);
1534
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001535 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001536 enter_pmode(vcpu);
1537
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001538 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001539 enter_rmode(vcpu);
1540
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001541#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001542 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001543 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001544 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001545 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001546 exit_lmode(vcpu);
1547 }
1548#endif
1549
Sheng Yang14394422008-04-28 12:24:45 +08001550 if (vm_need_ept())
1551 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1552
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001554 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001555 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001556
Rusty Russell707d92f2007-07-17 23:19:08 +10001557 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001558 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559}
1560
Sheng Yang14394422008-04-28 12:24:45 +08001561static u64 construct_eptp(unsigned long root_hpa)
1562{
1563 u64 eptp;
1564
1565 /* TODO write the value reading from MSR */
1566 eptp = VMX_EPT_DEFAULT_MT |
1567 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1568 eptp |= (root_hpa & PAGE_MASK);
1569
1570 return eptp;
1571}
1572
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1574{
Sheng Yang14394422008-04-28 12:24:45 +08001575 unsigned long guest_cr3;
1576 u64 eptp;
1577
1578 guest_cr3 = cr3;
1579 if (vm_need_ept()) {
1580 eptp = construct_eptp(cr3);
1581 vmcs_write64(EPT_POINTER, eptp);
1582 ept_sync_context(eptp);
1583 ept_load_pdptrs(vcpu);
1584 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
1585 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
1586 }
1587
Sheng Yang2384d2b2008-01-17 15:14:33 +08001588 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001589 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001590 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001591 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592}
1593
1594static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1595{
Sheng Yang14394422008-04-28 12:24:45 +08001596 unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
1597 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1598
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001599 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08001600 if (vm_need_ept())
1601 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1602
1603 vmcs_writel(CR4_READ_SHADOW, cr4);
1604 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605}
1606
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1608{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001609 struct vcpu_vmx *vmx = to_vmx(vcpu);
1610 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001612 vcpu->arch.shadow_efer = efer;
Joerg Roedel9f62e192008-01-31 14:57:39 +01001613 if (!msr)
1614 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615 if (efer & EFER_LMA) {
1616 vmcs_write32(VM_ENTRY_CONTROLS,
1617 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001618 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619 msr->data = efer;
1620
1621 } else {
1622 vmcs_write32(VM_ENTRY_CONTROLS,
1623 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001624 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625
1626 msr->data = efer & ~EFER_LME;
1627 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001628 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629}
1630
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1632{
1633 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1634
1635 return vmcs_readl(sf->base);
1636}
1637
1638static void vmx_get_segment(struct kvm_vcpu *vcpu,
1639 struct kvm_segment *var, int seg)
1640{
1641 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1642 u32 ar;
1643
1644 var->base = vmcs_readl(sf->base);
1645 var->limit = vmcs_read32(sf->limit);
1646 var->selector = vmcs_read16(sf->selector);
1647 ar = vmcs_read32(sf->ar_bytes);
1648 if (ar & AR_UNUSABLE_MASK)
1649 ar = 0;
1650 var->type = ar & 15;
1651 var->s = (ar >> 4) & 1;
1652 var->dpl = (ar >> 5) & 3;
1653 var->present = (ar >> 7) & 1;
1654 var->avl = (ar >> 12) & 1;
1655 var->l = (ar >> 13) & 1;
1656 var->db = (ar >> 14) & 1;
1657 var->g = (ar >> 15) & 1;
1658 var->unusable = (ar >> 16) & 1;
1659}
1660
Izik Eidus2e4d2652008-03-24 19:38:34 +02001661static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1662{
1663 struct kvm_segment kvm_seg;
1664
1665 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1666 return 0;
1667
1668 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1669 return 3;
1670
1671 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1672 return kvm_seg.selector & 3;
1673}
1674
Avi Kivity653e3102007-05-07 10:55:37 +03001675static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001676{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677 u32 ar;
1678
Avi Kivity653e3102007-05-07 10:55:37 +03001679 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680 ar = 1 << 16;
1681 else {
1682 ar = var->type & 15;
1683 ar |= (var->s & 1) << 4;
1684 ar |= (var->dpl & 3) << 5;
1685 ar |= (var->present & 1) << 7;
1686 ar |= (var->avl & 1) << 12;
1687 ar |= (var->l & 1) << 13;
1688 ar |= (var->db & 1) << 14;
1689 ar |= (var->g & 1) << 15;
1690 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001691 if (ar == 0) /* a 0 value means unusable */
1692 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001693
1694 return ar;
1695}
1696
1697static void vmx_set_segment(struct kvm_vcpu *vcpu,
1698 struct kvm_segment *var, int seg)
1699{
1700 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1701 u32 ar;
1702
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001703 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1704 vcpu->arch.rmode.tr.selector = var->selector;
1705 vcpu->arch.rmode.tr.base = var->base;
1706 vcpu->arch.rmode.tr.limit = var->limit;
1707 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001708 return;
1709 }
1710 vmcs_writel(sf->base, var->base);
1711 vmcs_write32(sf->limit, var->limit);
1712 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001713 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001714 /*
1715 * Hack real-mode segments into vm86 compatibility.
1716 */
1717 if (var->base == 0xffff0000 && var->selector == 0xf000)
1718 vmcs_writel(sf->base, 0xf0000);
1719 ar = 0xf3;
1720 } else
1721 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722 vmcs_write32(sf->ar_bytes, ar);
1723}
1724
Avi Kivity6aa8b732006-12-10 02:21:36 -08001725static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1726{
1727 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1728
1729 *db = (ar >> 14) & 1;
1730 *l = (ar >> 13) & 1;
1731}
1732
1733static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1734{
1735 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1736 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1737}
1738
1739static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1740{
1741 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1742 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1743}
1744
1745static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1746{
1747 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1748 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1749}
1750
1751static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1752{
1753 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1754 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1755}
1756
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001757static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1758{
1759 struct kvm_segment var;
1760 u32 ar;
1761
1762 vmx_get_segment(vcpu, &var, seg);
1763 ar = vmx_segment_access_rights(&var);
1764
1765 if (var.base != (var.selector << 4))
1766 return false;
1767 if (var.limit != 0xffff)
1768 return false;
1769 if (ar != 0xf3)
1770 return false;
1771
1772 return true;
1773}
1774
1775static bool code_segment_valid(struct kvm_vcpu *vcpu)
1776{
1777 struct kvm_segment cs;
1778 unsigned int cs_rpl;
1779
1780 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1781 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1782
1783 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1784 return false;
1785 if (!cs.s)
1786 return false;
1787 if (!(~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK))) {
1788 if (cs.dpl > cs_rpl)
1789 return false;
1790 } else if (cs.type & AR_TYPE_CODE_MASK) {
1791 if (cs.dpl != cs_rpl)
1792 return false;
1793 }
1794 if (!cs.present)
1795 return false;
1796
1797 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1798 return true;
1799}
1800
1801static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1802{
1803 struct kvm_segment ss;
1804 unsigned int ss_rpl;
1805
1806 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1807 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1808
1809 if ((ss.type != 3) || (ss.type != 7))
1810 return false;
1811 if (!ss.s)
1812 return false;
1813 if (ss.dpl != ss_rpl) /* DPL != RPL */
1814 return false;
1815 if (!ss.present)
1816 return false;
1817
1818 return true;
1819}
1820
1821static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1822{
1823 struct kvm_segment var;
1824 unsigned int rpl;
1825
1826 vmx_get_segment(vcpu, &var, seg);
1827 rpl = var.selector & SELECTOR_RPL_MASK;
1828
1829 if (!var.s)
1830 return false;
1831 if (!var.present)
1832 return false;
1833 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1834 if (var.dpl < rpl) /* DPL < RPL */
1835 return false;
1836 }
1837
1838 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1839 * rights flags
1840 */
1841 return true;
1842}
1843
1844static bool tr_valid(struct kvm_vcpu *vcpu)
1845{
1846 struct kvm_segment tr;
1847
1848 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1849
1850 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1851 return false;
1852 if ((tr.type != 3) || (tr.type != 11)) /* TODO: Check if guest is in IA32e mode */
1853 return false;
1854 if (!tr.present)
1855 return false;
1856
1857 return true;
1858}
1859
1860static bool ldtr_valid(struct kvm_vcpu *vcpu)
1861{
1862 struct kvm_segment ldtr;
1863
1864 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
1865
1866 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
1867 return false;
1868 if (ldtr.type != 2)
1869 return false;
1870 if (!ldtr.present)
1871 return false;
1872
1873 return true;
1874}
1875
1876static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
1877{
1878 struct kvm_segment cs, ss;
1879
1880 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1881 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1882
1883 return ((cs.selector & SELECTOR_RPL_MASK) ==
1884 (ss.selector & SELECTOR_RPL_MASK));
1885}
1886
1887/*
1888 * Check if guest state is valid. Returns true if valid, false if
1889 * not.
1890 * We assume that registers are always usable
1891 */
1892static bool guest_state_valid(struct kvm_vcpu *vcpu)
1893{
1894 /* real mode guest state checks */
1895 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
1896 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
1897 return false;
1898 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
1899 return false;
1900 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
1901 return false;
1902 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
1903 return false;
1904 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
1905 return false;
1906 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
1907 return false;
1908 } else {
1909 /* protected mode guest state checks */
1910 if (!cs_ss_rpl_check(vcpu))
1911 return false;
1912 if (!code_segment_valid(vcpu))
1913 return false;
1914 if (!stack_segment_valid(vcpu))
1915 return false;
1916 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
1917 return false;
1918 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
1919 return false;
1920 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
1921 return false;
1922 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
1923 return false;
1924 if (!tr_valid(vcpu))
1925 return false;
1926 if (!ldtr_valid(vcpu))
1927 return false;
1928 }
1929 /* TODO:
1930 * - Add checks on RIP
1931 * - Add checks on RFLAGS
1932 */
1933
1934 return true;
1935}
1936
Mike Dayd77c26f2007-10-08 09:02:08 -04001937static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001940 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001941 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001942 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943
Izik Eidus195aefd2007-10-01 22:14:18 +02001944 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1945 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001946 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001947 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08001948 r = kvm_write_guest_page(kvm, fn++, &data,
1949 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02001950 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001951 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001952 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1953 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001954 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001955 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1956 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001957 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001958 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001959 r = kvm_write_guest_page(kvm, fn, &data,
1960 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1961 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001962 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001963 goto out;
1964
1965 ret = 1;
1966out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001967 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001968}
1969
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001970static int init_rmode_identity_map(struct kvm *kvm)
1971{
1972 int i, r, ret;
1973 pfn_t identity_map_pfn;
1974 u32 tmp;
1975
1976 if (!vm_need_ept())
1977 return 1;
1978 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
1979 printk(KERN_ERR "EPT: identity-mapping pagetable "
1980 "haven't been allocated!\n");
1981 return 0;
1982 }
1983 if (likely(kvm->arch.ept_identity_pagetable_done))
1984 return 1;
1985 ret = 0;
1986 identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
1987 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
1988 if (r < 0)
1989 goto out;
1990 /* Set up identity-mapping pagetable for EPT in real mode */
1991 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
1992 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
1993 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
1994 r = kvm_write_guest_page(kvm, identity_map_pfn,
1995 &tmp, i * sizeof(tmp), sizeof(tmp));
1996 if (r < 0)
1997 goto out;
1998 }
1999 kvm->arch.ept_identity_pagetable_done = true;
2000 ret = 1;
2001out:
2002 return ret;
2003}
2004
Avi Kivity6aa8b732006-12-10 02:21:36 -08002005static void seg_setup(int seg)
2006{
2007 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2008
2009 vmcs_write16(sf->selector, 0);
2010 vmcs_writel(sf->base, 0);
2011 vmcs_write32(sf->limit, 0xffff);
Avi Kivitya16b20d2008-08-20 15:48:27 +03002012 vmcs_write32(sf->ar_bytes, 0xf3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002013}
2014
Sheng Yangf78e0e22007-10-29 09:40:42 +08002015static int alloc_apic_access_page(struct kvm *kvm)
2016{
2017 struct kvm_userspace_memory_region kvm_userspace_mem;
2018 int r = 0;
2019
Izik Eidus72dc67a2008-02-10 18:04:15 +02002020 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002021 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002022 goto out;
2023 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2024 kvm_userspace_mem.flags = 0;
2025 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2026 kvm_userspace_mem.memory_size = PAGE_SIZE;
2027 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2028 if (r)
2029 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002030
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002031 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002032out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002033 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002034 return r;
2035}
2036
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002037static int alloc_identity_pagetable(struct kvm *kvm)
2038{
2039 struct kvm_userspace_memory_region kvm_userspace_mem;
2040 int r = 0;
2041
2042 down_write(&kvm->slots_lock);
2043 if (kvm->arch.ept_identity_pagetable)
2044 goto out;
2045 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2046 kvm_userspace_mem.flags = 0;
2047 kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
2048 kvm_userspace_mem.memory_size = PAGE_SIZE;
2049 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2050 if (r)
2051 goto out;
2052
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002053 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
2054 VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002055out:
2056 up_write(&kvm->slots_lock);
2057 return r;
2058}
2059
Sheng Yang2384d2b2008-01-17 15:14:33 +08002060static void allocate_vpid(struct vcpu_vmx *vmx)
2061{
2062 int vpid;
2063
2064 vmx->vpid = 0;
2065 if (!enable_vpid || !cpu_has_vmx_vpid())
2066 return;
2067 spin_lock(&vmx_vpid_lock);
2068 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2069 if (vpid < VMX_NR_VPIDS) {
2070 vmx->vpid = vpid;
2071 __set_bit(vpid, vmx_vpid_bitmap);
2072 }
2073 spin_unlock(&vmx_vpid_lock);
2074}
2075
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002076static void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002077{
2078 void *va;
2079
2080 if (!cpu_has_vmx_msr_bitmap())
2081 return;
2082
2083 /*
2084 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2085 * have the write-low and read-high bitmap offsets the wrong way round.
2086 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2087 */
2088 va = kmap(msr_bitmap);
2089 if (msr <= 0x1fff) {
2090 __clear_bit(msr, va + 0x000); /* read-low */
2091 __clear_bit(msr, va + 0x800); /* write-low */
2092 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2093 msr &= 0x1fff;
2094 __clear_bit(msr, va + 0x400); /* read-high */
2095 __clear_bit(msr, va + 0xc00); /* write-high */
2096 }
2097 kunmap(msr_bitmap);
2098}
2099
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100/*
2101 * Sets up the vmcs for emulated real mode.
2102 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002103static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104{
Sheng Yang468d4722008-10-09 16:01:55 +08002105 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106 u32 junk;
Sheng Yang468d4722008-10-09 16:01:55 +08002107 u64 host_pat;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108 unsigned long a;
2109 struct descriptor_table dt;
2110 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002111 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002112 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03002115 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
2116 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117
Sheng Yang25c5f222008-03-28 13:18:56 +08002118 if (cpu_has_vmx_msr_bitmap())
2119 vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
2120
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2122
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002124 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2125 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002126
2127 exec_control = vmcs_config.cpu_based_exec_ctrl;
2128 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2129 exec_control &= ~CPU_BASED_TPR_SHADOW;
2130#ifdef CONFIG_X86_64
2131 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2132 CPU_BASED_CR8_LOAD_EXITING;
2133#endif
2134 }
Sheng Yangd56f5462008-04-25 10:13:16 +08002135 if (!vm_need_ept())
2136 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002137 CPU_BASED_CR3_LOAD_EXITING |
2138 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002139 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140
Sheng Yang83ff3b92007-11-21 14:33:25 +08002141 if (cpu_has_secondary_exec_ctrls()) {
2142 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2143 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2144 exec_control &=
2145 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002146 if (vmx->vpid == 0)
2147 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yangd56f5462008-04-25 10:13:16 +08002148 if (!vm_need_ept())
2149 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002150 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2151 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002152
Avi Kivityc7addb92007-09-16 18:58:32 +02002153 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2154 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2156
2157 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2158 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2159 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2160
2161 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2162 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2163 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002164 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2165 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002167#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168 rdmsrl(MSR_FS_BASE, a);
2169 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2170 rdmsrl(MSR_GS_BASE, a);
2171 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2172#else
2173 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2174 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2175#endif
2176
2177 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2178
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002179 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2181
Mike Dayd77c26f2007-10-08 09:02:08 -04002182 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002183 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002184 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2185 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2186 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187
2188 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2189 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2190 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2191 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2192 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2193 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2194
Sheng Yang468d4722008-10-09 16:01:55 +08002195 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2196 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2197 host_pat = msr_low | ((u64) msr_high << 32);
2198 vmcs_write64(HOST_IA32_PAT, host_pat);
2199 }
2200 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2201 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2202 host_pat = msr_low | ((u64) msr_high << 32);
2203 /* Write the default value follow host pat */
2204 vmcs_write64(GUEST_IA32_PAT, host_pat);
2205 /* Keep arch.pat sync with GUEST_IA32_PAT */
2206 vmx->vcpu.arch.pat = host_pat;
2207 }
2208
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209 for (i = 0; i < NR_VMX_MSR; ++i) {
2210 u32 index = vmx_msr_index[i];
2211 u32 data_low, data_high;
2212 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002213 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214
2215 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2216 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002217 if (wrmsr_safe(index, data_low, data_high) < 0)
2218 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002219 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002220 vmx->host_msrs[j].index = index;
2221 vmx->host_msrs[j].reserved = 0;
2222 vmx->host_msrs[j].data = data;
2223 vmx->guest_msrs[j] = vmx->host_msrs[j];
2224 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002227 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228
2229 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002230 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2231
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002232 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2233 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2234
Sheng Yangf78e0e22007-10-29 09:40:42 +08002235
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002236 return 0;
2237}
2238
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002239static int init_rmode(struct kvm *kvm)
2240{
2241 if (!init_rmode_tss(kvm))
2242 return 0;
2243 if (!init_rmode_identity_map(kvm))
2244 return 0;
2245 return 1;
2246}
2247
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002248static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2249{
2250 struct vcpu_vmx *vmx = to_vmx(vcpu);
2251 u64 msr;
2252 int ret;
2253
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002254 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002255 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002256 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002257 ret = -ENOMEM;
2258 goto out;
2259 }
2260
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002261 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002262
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002263 vmx->soft_vnmi_blocked = 0;
2264
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002265 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002266 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002267 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
2268 if (vmx->vcpu.vcpu_id == 0)
2269 msr |= MSR_IA32_APICBASE_BSP;
2270 kvm_set_apic_base(&vmx->vcpu, msr);
2271
2272 fx_init(&vmx->vcpu);
2273
Avi Kivity5706be02008-08-20 15:07:31 +03002274 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002275 /*
2276 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2277 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2278 */
2279 if (vmx->vcpu.vcpu_id == 0) {
2280 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2281 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2282 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002283 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2284 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002285 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002286
2287 seg_setup(VCPU_SREG_DS);
2288 seg_setup(VCPU_SREG_ES);
2289 seg_setup(VCPU_SREG_FS);
2290 seg_setup(VCPU_SREG_GS);
2291 seg_setup(VCPU_SREG_SS);
2292
2293 vmcs_write16(GUEST_TR_SELECTOR, 0);
2294 vmcs_writel(GUEST_TR_BASE, 0);
2295 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2296 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2297
2298 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2299 vmcs_writel(GUEST_LDTR_BASE, 0);
2300 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2301 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2302
2303 vmcs_write32(GUEST_SYSENTER_CS, 0);
2304 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2305 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2306
2307 vmcs_writel(GUEST_RFLAGS, 0x02);
2308 if (vmx->vcpu.vcpu_id == 0)
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002309 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002310 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002311 kvm_rip_write(vcpu, 0);
2312 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002313
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002314 vmcs_writel(GUEST_DR7, 0x400);
2315
2316 vmcs_writel(GUEST_GDTR_BASE, 0);
2317 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2318
2319 vmcs_writel(GUEST_IDTR_BASE, 0);
2320 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2321
2322 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2323 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2324 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2325
2326 guest_write_tsc(0);
2327
2328 /* Special registers */
2329 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2330
2331 setup_msrs(vmx);
2332
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2334
Sheng Yangf78e0e22007-10-29 09:40:42 +08002335 if (cpu_has_vmx_tpr_shadow()) {
2336 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2337 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2338 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002339 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002340 vmcs_write32(TPR_THRESHOLD, 0);
2341 }
2342
2343 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2344 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002345 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002346
Sheng Yang2384d2b2008-01-17 15:14:33 +08002347 if (vmx->vpid != 0)
2348 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2349
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002350 vmx->vcpu.arch.cr0 = 0x60000010;
2351 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002352 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002353 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002354 vmx_fpu_activate(&vmx->vcpu);
2355 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356
Sheng Yang2384d2b2008-01-17 15:14:33 +08002357 vpid_sync_vcpu_all(vmx);
2358
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002359 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002361 /* HACK: Don't enable emulation on guest boot/reset */
2362 vmx->emulation_required = 0;
2363
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002365 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366 return ret;
2367}
2368
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002369static void enable_irq_window(struct kvm_vcpu *vcpu)
2370{
2371 u32 cpu_based_vm_exec_control;
2372
2373 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2374 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2375 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2376}
2377
2378static void enable_nmi_window(struct kvm_vcpu *vcpu)
2379{
2380 u32 cpu_based_vm_exec_control;
2381
2382 if (!cpu_has_virtual_nmis()) {
2383 enable_irq_window(vcpu);
2384 return;
2385 }
2386
2387 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2388 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2389 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2390}
2391
Eddie Dong85f455f2007-07-06 12:20:49 +03002392static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
2393{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002394 struct vcpu_vmx *vmx = to_vmx(vcpu);
2395
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002396 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
2397
Avi Kivityfa89a812008-09-01 15:57:51 +03002398 ++vcpu->stat.irq_injections;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002399 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002400 vmx->rmode.irq.pending = true;
2401 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002402 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Avi Kivity9c5623e2007-11-08 18:19:20 +02002403 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2404 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2405 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002406 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002407 return;
2408 }
2409 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2410 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
2411}
2412
Sheng Yangf08864b2008-05-15 18:23:25 +08002413static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2414{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002415 struct vcpu_vmx *vmx = to_vmx(vcpu);
2416
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002417 if (!cpu_has_virtual_nmis()) {
2418 /*
2419 * Tracking the NMI-blocked state in software is built upon
2420 * finding the next open IRQ window. This, in turn, depends on
2421 * well-behaving guests: They have to keep IRQs disabled at
2422 * least as long as the NMI handler runs. Otherwise we may
2423 * cause NMI nesting, maybe breaking the guest. But as this is
2424 * highly unlikely, we can live with the residual risk.
2425 */
2426 vmx->soft_vnmi_blocked = 1;
2427 vmx->vnmi_blocked_time = 0;
2428 }
2429
Jan Kiszka487b3912008-09-26 09:30:56 +02002430 ++vcpu->stat.nmi_injections;
Jan Kiszka66a5a342008-09-26 09:30:51 +02002431 if (vcpu->arch.rmode.active) {
2432 vmx->rmode.irq.pending = true;
2433 vmx->rmode.irq.vector = NMI_VECTOR;
2434 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2435 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2436 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2437 INTR_INFO_VALID_MASK);
2438 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2439 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2440 return;
2441 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002442 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2443 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002444}
2445
Jan Kiszka33f089c2008-09-26 09:30:49 +02002446static void vmx_update_window_states(struct kvm_vcpu *vcpu)
2447{
2448 u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2449
2450 vcpu->arch.nmi_window_open =
2451 !(guest_intr & (GUEST_INTR_STATE_STI |
2452 GUEST_INTR_STATE_MOV_SS |
2453 GUEST_INTR_STATE_NMI));
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002454 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
2455 vcpu->arch.nmi_window_open = 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002456
2457 vcpu->arch.interrupt_window_open =
2458 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2459 !(guest_intr & (GUEST_INTR_STATE_STI |
2460 GUEST_INTR_STATE_MOV_SS)));
2461}
2462
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
2464{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002465 int word_index = __ffs(vcpu->arch.irq_summary);
2466 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002467 int irq = word_index * BITS_PER_LONG + bit_index;
2468
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002469 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
2470 if (!vcpu->arch.irq_pending[word_index])
2471 clear_bit(word_index, &vcpu->arch.irq_summary);
Avi Kivityecfc79c2008-08-14 11:13:16 +03002472 kvm_queue_interrupt(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473}
2474
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002475static void do_interrupt_requests(struct kvm_vcpu *vcpu,
2476 struct kvm_run *kvm_run)
2477{
2478 vmx_update_window_states(vcpu);
2479
Jan Kiszka55934c02008-12-15 13:52:10 +01002480 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
2481 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
2482 GUEST_INTR_STATE_STI |
2483 GUEST_INTR_STATE_MOV_SS);
2484
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002485 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
Jan Kiszka264ff012008-11-24 12:26:19 +01002486 if (vcpu->arch.interrupt.pending) {
2487 enable_nmi_window(vcpu);
2488 } else if (vcpu->arch.nmi_window_open) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002489 vcpu->arch.nmi_pending = false;
2490 vcpu->arch.nmi_injected = true;
2491 } else {
2492 enable_nmi_window(vcpu);
Jan Kiszka487b3912008-09-26 09:30:56 +02002493 return;
2494 }
Jan Kiszka487b3912008-09-26 09:30:56 +02002495 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002496 if (vcpu->arch.nmi_injected) {
2497 vmx_inject_nmi(vcpu);
Jan Kiszka45312202008-12-11 16:54:54 +01002498 if (vcpu->arch.nmi_pending)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002499 enable_nmi_window(vcpu);
2500 else if (vcpu->arch.irq_summary
2501 || kvm_run->request_interrupt_window)
2502 enable_irq_window(vcpu);
2503 return;
2504 }
Jan Kiszka487b3912008-09-26 09:30:56 +02002505
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002506 if (vcpu->arch.interrupt_window_open) {
2507 if (vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending)
2508 kvm_do_inject_irq(vcpu);
2509
2510 if (vcpu->arch.interrupt.pending)
2511 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
2512 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002513 if (!vcpu->arch.interrupt_window_open &&
2514 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Jan Kiszkaf460ee42008-09-26 09:30:50 +02002515 enable_irq_window(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516}
2517
Izik Eiduscbc94022007-10-25 00:29:55 +02002518static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2519{
2520 int ret;
2521 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002522 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002523 .guest_phys_addr = addr,
2524 .memory_size = PAGE_SIZE * 3,
2525 .flags = 0,
2526 };
2527
2528 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2529 if (ret)
2530 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002531 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002532 return 0;
2533}
2534
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2536 int vec, u32 err_code)
2537{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002538 /*
2539 * Instruction with address size override prefix opcode 0x67
2540 * Cause the #SS fault with 0 error code in VM86 mode.
2541 */
2542 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002543 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002545 /*
2546 * Forward all other exceptions that are valid in real mode.
2547 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2548 * the required debugging infrastructure rework.
2549 */
2550 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002551 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002552 if (vcpu->guest_debug &
2553 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2554 return 0;
2555 kvm_queue_exception(vcpu, vec);
2556 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002557 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002558 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2559 return 0;
2560 /* fall through */
2561 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002562 case OF_VECTOR:
2563 case BR_VECTOR:
2564 case UD_VECTOR:
2565 case DF_VECTOR:
2566 case SS_VECTOR:
2567 case GP_VECTOR:
2568 case MF_VECTOR:
2569 kvm_queue_exception(vcpu, vec);
2570 return 1;
2571 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572 return 0;
2573}
2574
2575static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2576{
Avi Kivity1155f762007-11-22 11:30:47 +02002577 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002578 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002579 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580 u32 vect_info;
2581 enum emulation_result er;
2582
Avi Kivity1155f762007-11-22 11:30:47 +02002583 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2585
2586 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002587 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002589 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590
Eddie Dong85f455f2007-07-06 12:20:49 +03002591 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002592 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002593 set_bit(irq, vcpu->arch.irq_pending);
2594 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002595 }
2596
Jan Kiszkae4a41882008-09-26 09:30:46 +02002597 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002598 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002599
2600 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002601 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002602 return 1;
2603 }
2604
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002605 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002606 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002607 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002608 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002609 return 1;
2610 }
2611
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002613 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002614 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2616 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002617 /* EPT won't cause page fault directly */
2618 if (vm_need_ept())
2619 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002621 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2622 (u32)((u64)cr2 >> 32), handler);
Avi Kivityf7d92382008-07-03 16:14:28 +03002623 if (vcpu->arch.interrupt.pending || vcpu->arch.exception.pending)
Avi Kivity577bdc42008-07-19 08:57:05 +03002624 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002625 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626 }
2627
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002628 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002629 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002630 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002631 if (vcpu->arch.halt_request) {
2632 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002633 return kvm_emulate_halt(vcpu);
2634 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002636 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002638 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002639 switch (ex_no) {
2640 case DB_VECTOR:
2641 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2642 if (!(vcpu->guest_debug &
2643 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2644 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2645 kvm_queue_exception(vcpu, DB_VECTOR);
2646 return 1;
2647 }
2648 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2649 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2650 /* fall through */
2651 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002653 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2654 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002655 break;
2656 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002657 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2658 kvm_run->ex.exception = ex_no;
2659 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002660 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 return 0;
2663}
2664
2665static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2666 struct kvm_run *kvm_run)
2667{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002668 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002669 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002670 return 1;
2671}
2672
Avi Kivity988ad742007-02-12 00:54:36 -08002673static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2674{
2675 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2676 return 0;
2677}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678
Avi Kivity6aa8b732006-12-10 02:21:36 -08002679static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2680{
He, Qingbfdaab02007-09-12 14:18:28 +08002681 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02002682 int size, down, in, string, rep;
2683 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002684
Avi Kivity1165f5f2007-04-19 17:27:43 +03002685 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002686 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002687 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002688
2689 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002690 if (emulate_instruction(vcpu,
2691 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002692 return 0;
2693 return 1;
2694 }
2695
2696 size = (exit_qualification & 7) + 1;
2697 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002698 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002699 rep = (exit_qualification & 32) != 0;
2700 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002701
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002702 skip_emulated_instruction(vcpu);
Laurent Vivier3090dd72007-08-05 10:43:32 +03002703 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704}
2705
Ingo Molnar102d8322007-02-19 14:37:47 +02002706static void
2707vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2708{
2709 /*
2710 * Patch in the VMCALL instruction:
2711 */
2712 hypercall[0] = 0x0f;
2713 hypercall[1] = 0x01;
2714 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002715}
2716
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2718{
He, Qingbfdaab02007-09-12 14:18:28 +08002719 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720 int cr;
2721 int reg;
2722
He, Qingbfdaab02007-09-12 14:18:28 +08002723 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 cr = exit_qualification & 15;
2725 reg = (exit_qualification >> 8) & 15;
2726 switch ((exit_qualification >> 4) & 3) {
2727 case 0: /* mov to cr */
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002728 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
2729 (u32)kvm_register_read(vcpu, reg),
2730 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
2731 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 switch (cr) {
2733 case 0:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002734 kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 skip_emulated_instruction(vcpu);
2736 return 1;
2737 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002738 kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 skip_emulated_instruction(vcpu);
2740 return 1;
2741 case 4:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002742 kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 skip_emulated_instruction(vcpu);
2744 return 1;
2745 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002746 kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002748 if (irqchip_in_kernel(vcpu->kvm))
2749 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002750 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2751 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752 };
2753 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002754 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002755 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002756 vcpu->arch.cr0 &= ~X86_CR0_TS;
2757 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002758 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002759 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002760 skip_emulated_instruction(vcpu);
2761 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 case 1: /*mov from cr*/
2763 switch (cr) {
2764 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002765 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002766 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002767 (u32)kvm_register_read(vcpu, reg),
2768 (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002769 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 skip_emulated_instruction(vcpu);
2771 return 1;
2772 case 8:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002773 kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002774 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002775 (u32)kvm_register_read(vcpu, reg), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776 skip_emulated_instruction(vcpu);
2777 return 1;
2778 }
2779 break;
2780 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002781 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782
2783 skip_emulated_instruction(vcpu);
2784 return 1;
2785 default:
2786 break;
2787 }
2788 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002789 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 (int)(exit_qualification >> 4) & 3, cr);
2791 return 0;
2792}
2793
2794static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2795{
He, Qingbfdaab02007-09-12 14:18:28 +08002796 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 unsigned long val;
2798 int dr, reg;
2799
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002800 dr = vmcs_readl(GUEST_DR7);
2801 if (dr & DR7_GD) {
2802 /*
2803 * As the vm-exit takes precedence over the debug trap, we
2804 * need to emulate the latter, either for the host or the
2805 * guest debugging itself.
2806 */
2807 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
2808 kvm_run->debug.arch.dr6 = vcpu->arch.dr6;
2809 kvm_run->debug.arch.dr7 = dr;
2810 kvm_run->debug.arch.pc =
2811 vmcs_readl(GUEST_CS_BASE) +
2812 vmcs_readl(GUEST_RIP);
2813 kvm_run->debug.arch.exception = DB_VECTOR;
2814 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2815 return 0;
2816 } else {
2817 vcpu->arch.dr7 &= ~DR7_GD;
2818 vcpu->arch.dr6 |= DR6_BD;
2819 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2820 kvm_queue_exception(vcpu, DB_VECTOR);
2821 return 1;
2822 }
2823 }
2824
He, Qingbfdaab02007-09-12 14:18:28 +08002825 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002826 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2827 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2828 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002830 case 0 ... 3:
2831 val = vcpu->arch.db[dr];
2832 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002834 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 break;
2836 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002837 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838 break;
2839 default:
2840 val = 0;
2841 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002842 kvm_register_write(vcpu, reg, val);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002843 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002845 val = vcpu->arch.regs[reg];
2846 switch (dr) {
2847 case 0 ... 3:
2848 vcpu->arch.db[dr] = val;
2849 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
2850 vcpu->arch.eff_db[dr] = val;
2851 break;
2852 case 4 ... 5:
2853 if (vcpu->arch.cr4 & X86_CR4_DE)
2854 kvm_queue_exception(vcpu, UD_VECTOR);
2855 break;
2856 case 6:
2857 if (val & 0xffffffff00000000ULL) {
2858 kvm_queue_exception(vcpu, GP_VECTOR);
2859 break;
2860 }
2861 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
2862 break;
2863 case 7:
2864 if (val & 0xffffffff00000000ULL) {
2865 kvm_queue_exception(vcpu, GP_VECTOR);
2866 break;
2867 }
2868 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
2869 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
2870 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2871 vcpu->arch.switch_db_regs =
2872 (val & DR7_BP_EN_MASK);
2873 }
2874 break;
2875 }
2876 KVMTRACE_2D(DR_WRITE, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 skip_emulated_instruction(vcpu);
2879 return 1;
2880}
2881
2882static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2883{
Avi Kivity06465c52007-02-28 20:46:53 +02002884 kvm_emulate_cpuid(vcpu);
2885 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886}
2887
2888static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2889{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002890 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891 u64 data;
2892
2893 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002894 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895 return 1;
2896 }
2897
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002898 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2899 handler);
2900
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002902 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2903 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904 skip_emulated_instruction(vcpu);
2905 return 1;
2906}
2907
2908static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2909{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002910 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2911 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2912 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002914 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2915 handler);
2916
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002918 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002919 return 1;
2920 }
2921
2922 skip_emulated_instruction(vcpu);
2923 return 1;
2924}
2925
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002926static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2927 struct kvm_run *kvm_run)
2928{
2929 return 1;
2930}
2931
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2933 struct kvm_run *kvm_run)
2934{
Eddie Dong85f455f2007-07-06 12:20:49 +03002935 u32 cpu_based_vm_exec_control;
2936
2937 /* clear pending irq */
2938 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2939 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2940 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002941
2942 KVMTRACE_0D(PEND_INTR, vcpu, handler);
Jan Kiszkaa26bf122008-09-26 09:30:45 +02002943 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002944
Dor Laorc1150d82007-01-05 16:36:24 -08002945 /*
2946 * If the user space waits to inject interrupts, exit as soon as
2947 * possible
2948 */
2949 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002950 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002951 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08002952 return 0;
2953 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 return 1;
2955}
2956
2957static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2958{
2959 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002960 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961}
2962
Ingo Molnarc21415e2007-02-19 14:37:47 +02002963static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2964{
Dor Laor510043d2007-02-19 18:25:43 +02002965 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002966 kvm_emulate_hypercall(vcpu);
2967 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002968}
2969
Marcelo Tosattia7052892008-09-23 13:18:35 -03002970static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2971{
2972 u64 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2973
2974 kvm_mmu_invlpg(vcpu, exit_qualification);
2975 skip_emulated_instruction(vcpu);
2976 return 1;
2977}
2978
Eddie Donge5edaa02007-11-11 12:28:35 +02002979static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2980{
2981 skip_emulated_instruction(vcpu);
2982 /* TODO: Add support for VT-d/pass-through device */
2983 return 1;
2984}
2985
Sheng Yangf78e0e22007-10-29 09:40:42 +08002986static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2987{
2988 u64 exit_qualification;
2989 enum emulation_result er;
2990 unsigned long offset;
2991
2992 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2993 offset = exit_qualification & 0xffful;
2994
2995 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2996
2997 if (er != EMULATE_DONE) {
2998 printk(KERN_ERR
2999 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3000 offset);
3001 return -ENOTSUPP;
3002 }
3003 return 1;
3004}
3005
Izik Eidus37817f22008-03-24 23:14:53 +02003006static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3007{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003008 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003009 unsigned long exit_qualification;
3010 u16 tss_selector;
3011 int reason;
3012
3013 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3014
3015 reason = (u32)exit_qualification >> 30;
Jan Kiszka60637aa2008-09-26 09:30:47 +02003016 if (reason == TASK_SWITCH_GATE && vmx->vcpu.arch.nmi_injected &&
3017 (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
3018 (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK)
3019 == INTR_TYPE_NMI_INTR) {
3020 vcpu->arch.nmi_injected = false;
3021 if (cpu_has_virtual_nmis())
3022 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3023 GUEST_INTR_STATE_NMI);
3024 }
Izik Eidus37817f22008-03-24 23:14:53 +02003025 tss_selector = exit_qualification;
3026
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003027 if (!kvm_task_switch(vcpu, tss_selector, reason))
3028 return 0;
3029
3030 /* clear all local breakpoint enable flags */
3031 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3032
3033 /*
3034 * TODO: What about debug traps on tss switch?
3035 * Are we supposed to inject them and update dr6?
3036 */
3037
3038 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003039}
3040
Sheng Yang14394422008-04-28 12:24:45 +08003041static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3042{
3043 u64 exit_qualification;
3044 enum emulation_result er;
3045 gpa_t gpa;
3046 unsigned long hva;
3047 int gla_validity;
3048 int r;
3049
3050 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
3051
3052 if (exit_qualification & (1 << 6)) {
3053 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
3054 return -ENOTSUPP;
3055 }
3056
3057 gla_validity = (exit_qualification >> 7) & 0x3;
3058 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3059 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3060 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3061 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
3062 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
3063 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3064 (long unsigned int)exit_qualification);
3065 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3066 kvm_run->hw.hardware_exit_reason = 0;
3067 return -ENOTSUPP;
3068 }
3069
3070 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3071 hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
3072 if (!kvm_is_error_hva(hva)) {
3073 r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
3074 if (r < 0) {
3075 printk(KERN_ERR "EPT: Not enough memory!\n");
3076 return -ENOMEM;
3077 }
3078 return 1;
3079 } else {
3080 /* must be MMIO */
3081 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3082
3083 if (er == EMULATE_FAIL) {
3084 printk(KERN_ERR
3085 "EPT: Fail to handle EPT violation vmexit!er is %d\n",
3086 er);
3087 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3088 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
3089 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
3090 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3091 (long unsigned int)exit_qualification);
3092 return -ENOTSUPP;
3093 } else if (er == EMULATE_DO_MMIO)
3094 return 0;
3095 }
3096 return 1;
3097}
3098
Sheng Yangf08864b2008-05-15 18:23:25 +08003099static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3100{
3101 u32 cpu_based_vm_exec_control;
3102
3103 /* clear pending NMI */
3104 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3105 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3106 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3107 ++vcpu->stat.nmi_window_exits;
3108
3109 return 1;
3110}
3111
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003112static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
3113 struct kvm_run *kvm_run)
3114{
3115 struct vcpu_vmx *vmx = to_vmx(vcpu);
3116 int err;
3117
3118 preempt_enable();
3119 local_irq_enable();
3120
3121 while (!guest_state_valid(vcpu)) {
3122 err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
3123
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003124 if (err == EMULATE_DO_MMIO)
3125 break;
3126
3127 if (err != EMULATE_DONE) {
3128 kvm_report_emulation_failure(vcpu, "emulation failure");
3129 return;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003130 }
3131
3132 if (signal_pending(current))
3133 break;
3134 if (need_resched())
3135 schedule();
3136 }
3137
3138 local_irq_disable();
3139 preempt_disable();
3140
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003141 /* Guest state should be valid now except if we need to
3142 * emulate an MMIO */
3143 if (guest_state_valid(vcpu))
3144 vmx->emulation_required = 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003145}
3146
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147/*
3148 * The exit handlers return 1 if the exit was handled fully and guest execution
3149 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3150 * to be done to userspace and return 0.
3151 */
3152static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
3153 struct kvm_run *kvm_run) = {
3154 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3155 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003156 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003157 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159 [EXIT_REASON_CR_ACCESS] = handle_cr,
3160 [EXIT_REASON_DR_ACCESS] = handle_dr,
3161 [EXIT_REASON_CPUID] = handle_cpuid,
3162 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3163 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3164 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3165 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003166 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003167 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003168 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3169 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003170 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003171 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Sheng Yang14394422008-04-28 12:24:45 +08003172 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173};
3174
3175static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003176 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177
3178/*
3179 * The guest has exited. See if we can fix it or if we need userspace
3180 * assistance.
3181 */
3182static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
3183{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003184 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03003185 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02003186 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003187
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003188 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
3189 (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003190
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003191 /* If we need to emulate an MMIO from handle_invalid_guest_state
3192 * we just return 0 */
3193 if (vmx->emulation_required && emulate_invalid_guest_state)
3194 return 0;
3195
Sheng Yang14394422008-04-28 12:24:45 +08003196 /* Access CR3 don't cause VMExit in paging mode, so we need
3197 * to sync with guest real CR3. */
3198 if (vm_need_ept() && is_paging(vcpu)) {
3199 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3200 ept_load_pdptrs(vcpu);
3201 }
3202
Avi Kivity29bd8a72007-09-10 17:27:03 +03003203 if (unlikely(vmx->fail)) {
3204 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3205 kvm_run->fail_entry.hardware_entry_failure_reason
3206 = vmcs_read32(VM_INSTRUCTION_ERROR);
3207 return 0;
3208 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209
Mike Dayd77c26f2007-10-08 09:02:08 -04003210 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003211 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003212 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3213 exit_reason != EXIT_REASON_TASK_SWITCH))
3214 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3215 "(0x%x) and exit reason is 0x%x\n",
3216 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003217
3218 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
3219 if (vcpu->arch.interrupt_window_open) {
3220 vmx->soft_vnmi_blocked = 0;
3221 vcpu->arch.nmi_window_open = 1;
3222 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003223 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003224 /*
3225 * This CPU don't support us in finding the end of an
3226 * NMI-blocked window if the guest runs with IRQs
3227 * disabled. So we pull the trigger after 1 s of
3228 * futile waiting, but inform the user about this.
3229 */
3230 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3231 "state on VCPU %d after 1 s timeout\n",
3232 __func__, vcpu->vcpu_id);
3233 vmx->soft_vnmi_blocked = 0;
3234 vmx->vcpu.arch.nmi_window_open = 1;
3235 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003236 }
3237
Avi Kivity6aa8b732006-12-10 02:21:36 -08003238 if (exit_reason < kvm_vmx_max_exit_handlers
3239 && kvm_vmx_exit_handlers[exit_reason])
3240 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
3241 else {
3242 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3243 kvm_run->hw.hardware_exit_reason = exit_reason;
3244 }
3245 return 0;
3246}
3247
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003248static void update_tpr_threshold(struct kvm_vcpu *vcpu)
3249{
3250 int max_irr, tpr;
3251
3252 if (!vm_need_tpr_shadow(vcpu->kvm))
3253 return;
3254
3255 if (!kvm_lapic_enabled(vcpu) ||
3256 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
3257 vmcs_write32(TPR_THRESHOLD, 0);
3258 return;
3259 }
3260
3261 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
3262 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
3263}
3264
Avi Kivitycf393f72008-07-01 16:20:21 +03003265static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3266{
3267 u32 exit_intr_info;
Avi Kivity668f6122008-07-02 09:28:55 +03003268 u32 idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003269 bool unblock_nmi;
3270 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003271 int type;
3272 bool idtv_info_valid;
Avi Kivity35920a32008-07-03 14:50:12 +03003273 u32 error;
Avi Kivitycf393f72008-07-01 16:20:21 +03003274
3275 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3276 if (cpu_has_virtual_nmis()) {
3277 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3278 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3279 /*
3280 * SDM 3: 25.7.1.2
3281 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3282 * a guest IRET fault.
3283 */
3284 if (unblock_nmi && vector != DF_VECTOR)
3285 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3286 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003287 } else if (unlikely(vmx->soft_vnmi_blocked))
3288 vmx->vnmi_blocked_time +=
3289 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003290
3291 idt_vectoring_info = vmx->idt_vectoring_info;
3292 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3293 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3294 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
3295 if (vmx->vcpu.arch.nmi_injected) {
3296 /*
3297 * SDM 3: 25.7.1.2
3298 * Clear bit "block by NMI" before VM entry if a NMI delivery
3299 * faulted.
3300 */
3301 if (idtv_info_valid && type == INTR_TYPE_NMI_INTR)
3302 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3303 GUEST_INTR_STATE_NMI);
3304 else
3305 vmx->vcpu.arch.nmi_injected = false;
3306 }
Avi Kivity35920a32008-07-03 14:50:12 +03003307 kvm_clear_exception_queue(&vmx->vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003308 if (idtv_info_valid && (type == INTR_TYPE_HARD_EXCEPTION ||
3309 type == INTR_TYPE_SOFT_EXCEPTION)) {
Avi Kivity35920a32008-07-03 14:50:12 +03003310 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
3311 error = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3312 kvm_queue_exception_e(&vmx->vcpu, vector, error);
3313 } else
3314 kvm_queue_exception(&vmx->vcpu, vector);
3315 vmx->idt_vectoring_info = 0;
3316 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003317 kvm_clear_interrupt_queue(&vmx->vcpu);
3318 if (idtv_info_valid && type == INTR_TYPE_EXT_INTR) {
3319 kvm_queue_interrupt(&vmx->vcpu, vector);
3320 vmx->idt_vectoring_info = 0;
3321 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003322}
3323
Eddie Dong85f455f2007-07-06 12:20:49 +03003324static void vmx_intr_assist(struct kvm_vcpu *vcpu)
3325{
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003326 update_tpr_threshold(vcpu);
3327
Jan Kiszka33f089c2008-09-26 09:30:49 +02003328 vmx_update_window_states(vcpu);
3329
Jan Kiszka55934c02008-12-15 13:52:10 +01003330 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3331 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3332 GUEST_INTR_STATE_STI |
3333 GUEST_INTR_STATE_MOV_SS);
3334
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003335 if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
3336 if (vcpu->arch.interrupt.pending) {
3337 enable_nmi_window(vcpu);
3338 } else if (vcpu->arch.nmi_window_open) {
3339 vcpu->arch.nmi_pending = false;
3340 vcpu->arch.nmi_injected = true;
3341 } else {
3342 enable_nmi_window(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003343 return;
3344 }
Sheng Yangf08864b2008-05-15 18:23:25 +08003345 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003346 if (vcpu->arch.nmi_injected) {
3347 vmx_inject_nmi(vcpu);
3348 if (vcpu->arch.nmi_pending)
3349 enable_nmi_window(vcpu);
3350 else if (kvm_cpu_has_interrupt(vcpu))
3351 enable_irq_window(vcpu);
3352 return;
3353 }
Avi Kivityf7d92382008-07-03 16:14:28 +03003354 if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
Jan Kiszka33f089c2008-09-26 09:30:49 +02003355 if (vcpu->arch.interrupt_window_open)
Avi Kivityf7d92382008-07-03 16:14:28 +03003356 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
3357 else
3358 enable_irq_window(vcpu);
3359 }
3360 if (vcpu->arch.interrupt.pending) {
3361 vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
Avi Kivitydf203ec2008-11-23 18:08:57 +02003362 if (kvm_cpu_has_interrupt(vcpu))
3363 enable_irq_window(vcpu);
Avi Kivityf7d92382008-07-03 16:14:28 +03003364 }
Eddie Dong85f455f2007-07-06 12:20:49 +03003365}
3366
Avi Kivity9c8cba32007-11-22 11:42:59 +02003367/*
3368 * Failure to inject an interrupt should give us the information
3369 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3370 * when fetching the interrupt redirection bitmap in the real-mode
3371 * tss, this doesn't happen. So we do it ourselves.
3372 */
3373static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3374{
3375 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003376 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003377 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003378 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003379 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3380 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3381 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3382 return;
3383 }
3384 vmx->idt_vectoring_info =
3385 VECTORING_INFO_VALID_MASK
3386 | INTR_TYPE_EXT_INTR
3387 | vmx->rmode.irq.vector;
3388}
3389
Avi Kivityc8019492008-07-14 14:44:59 +03003390#ifdef CONFIG_X86_64
3391#define R "r"
3392#define Q "q"
3393#else
3394#define R "e"
3395#define Q "l"
3396#endif
3397
Avi Kivity04d2cc72007-09-10 18:10:54 +03003398static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003399{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003400 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003401 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03003402
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003403 /* Record the guest's net vcpu time for enforced NMI injections. */
3404 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3405 vmx->entry_time = ktime_get();
3406
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003407 /* Handle invalid guest state instead of entering VMX */
3408 if (vmx->emulation_required && emulate_invalid_guest_state) {
3409 handle_invalid_guest_state(vcpu, kvm_run);
3410 return;
3411 }
3412
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003413 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3414 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3415 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3416 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3417
Avi Kivitye6adf282007-04-30 16:07:54 +03003418 /*
3419 * Loading guest fpu may have cleared host cr0.ts
3420 */
3421 vmcs_writel(HOST_CR0, read_cr0());
3422
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003423 set_debugreg(vcpu->arch.dr6, 6);
3424
Mike Dayd77c26f2007-10-08 09:02:08 -04003425 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003427 "push %%"R"dx; push %%"R"bp;"
3428 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003429 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3430 "je 1f \n\t"
3431 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003432 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003433 "1: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003434 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003435 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003437 "mov %c[cr2](%0), %%"R"ax \n\t"
3438 "mov %%"R"ax, %%cr2 \n\t"
3439 "mov %c[rax](%0), %%"R"ax \n\t"
3440 "mov %c[rbx](%0), %%"R"bx \n\t"
3441 "mov %c[rdx](%0), %%"R"dx \n\t"
3442 "mov %c[rsi](%0), %%"R"si \n\t"
3443 "mov %c[rdi](%0), %%"R"di \n\t"
3444 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003445#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003446 "mov %c[r8](%0), %%r8 \n\t"
3447 "mov %c[r9](%0), %%r9 \n\t"
3448 "mov %c[r10](%0), %%r10 \n\t"
3449 "mov %c[r11](%0), %%r11 \n\t"
3450 "mov %c[r12](%0), %%r12 \n\t"
3451 "mov %c[r13](%0), %%r13 \n\t"
3452 "mov %c[r14](%0), %%r14 \n\t"
3453 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003455 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3456
Avi Kivity6aa8b732006-12-10 02:21:36 -08003457 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003458 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003459 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003460 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003461 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003462 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003463 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003464 "xchg %0, (%%"R"sp) \n\t"
3465 "mov %%"R"ax, %c[rax](%0) \n\t"
3466 "mov %%"R"bx, %c[rbx](%0) \n\t"
3467 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3468 "mov %%"R"dx, %c[rdx](%0) \n\t"
3469 "mov %%"R"si, %c[rsi](%0) \n\t"
3470 "mov %%"R"di, %c[rdi](%0) \n\t"
3471 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003472#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003473 "mov %%r8, %c[r8](%0) \n\t"
3474 "mov %%r9, %c[r9](%0) \n\t"
3475 "mov %%r10, %c[r10](%0) \n\t"
3476 "mov %%r11, %c[r11](%0) \n\t"
3477 "mov %%r12, %c[r12](%0) \n\t"
3478 "mov %%r13, %c[r13](%0) \n\t"
3479 "mov %%r14, %c[r14](%0) \n\t"
3480 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003482 "mov %%cr2, %%"R"ax \n\t"
3483 "mov %%"R"ax, %c[cr2](%0) \n\t"
3484
3485 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003486 "setbe %c[fail](%0) \n\t"
3487 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3488 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3489 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003490 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003491 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3492 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3493 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3494 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3495 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3496 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3497 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003498#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003499 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3500 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3501 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3502 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3503 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3504 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3505 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3506 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003507#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003508 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003509 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003510 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003511#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003512 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3513#endif
3514 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003515
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003516 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
3517 vcpu->arch.regs_dirty = 0;
3518
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003519 get_debugreg(vcpu->arch.dr6, 6);
3520
Avi Kivity1155f762007-11-22 11:30:47 +02003521 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003522 if (vmx->rmode.irq.pending)
3523 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003524
Jan Kiszka33f089c2008-09-26 09:30:49 +02003525 vmx_update_window_states(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526
Mike Dayd77c26f2007-10-08 09:02:08 -04003527 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003528 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003529
3530 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
3531
3532 /* We need to handle NMIs before interrupts are enabled */
Jan Kiszkae4a41882008-09-26 09:30:46 +02003533 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Sheng Yangf08864b2008-05-15 18:23:25 +08003534 (intr_info & INTR_INFO_VALID_MASK)) {
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003535 KVMTRACE_0D(NMI, vcpu, handler);
Avi Kivity1b6269d2007-10-09 12:12:19 +02003536 asm("int $2");
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003537 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003538
3539 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540}
3541
Avi Kivityc8019492008-07-14 14:44:59 +03003542#undef R
3543#undef Q
3544
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3546{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003547 struct vcpu_vmx *vmx = to_vmx(vcpu);
3548
3549 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003550 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003551 free_vmcs(vmx->vmcs);
3552 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553 }
3554}
3555
3556static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3557{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003558 struct vcpu_vmx *vmx = to_vmx(vcpu);
3559
Sheng Yang2384d2b2008-01-17 15:14:33 +08003560 spin_lock(&vmx_vpid_lock);
3561 if (vmx->vpid != 0)
3562 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3563 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003564 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003565 kfree(vmx->host_msrs);
3566 kfree(vmx->guest_msrs);
3567 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003568 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003569}
3570
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003571static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003572{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003573 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003574 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003575 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003576
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003577 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003578 return ERR_PTR(-ENOMEM);
3579
Sheng Yang2384d2b2008-01-17 15:14:33 +08003580 allocate_vpid(vmx);
3581
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003582 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3583 if (err)
3584 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003585
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003586 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003587 if (!vmx->guest_msrs) {
3588 err = -ENOMEM;
3589 goto uninit_vcpu;
3590 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003591
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003592 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3593 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003594 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003595
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003596 vmx->vmcs = alloc_vmcs();
3597 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003598 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003599
3600 vmcs_clear(vmx->vmcs);
3601
Avi Kivity15ad7142007-07-11 18:17:21 +03003602 cpu = get_cpu();
3603 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003604 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003605 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003606 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003607 if (err)
3608 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003609 if (vm_need_virtualize_apic_accesses(kvm))
3610 if (alloc_apic_access_page(kvm) != 0)
3611 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003612
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003613 if (vm_need_ept())
3614 if (alloc_identity_pagetable(kvm) != 0)
3615 goto free_vmcs;
3616
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003617 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003618
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003619free_vmcs:
3620 free_vmcs(vmx->vmcs);
3621free_msrs:
3622 kfree(vmx->host_msrs);
3623free_guest_msrs:
3624 kfree(vmx->guest_msrs);
3625uninit_vcpu:
3626 kvm_vcpu_uninit(&vmx->vcpu);
3627free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003628 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003629 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003630}
3631
Yang, Sheng002c7f72007-07-31 14:23:01 +03003632static void __init vmx_check_processor_compat(void *rtn)
3633{
3634 struct vmcs_config vmcs_conf;
3635
3636 *(int *)rtn = 0;
3637 if (setup_vmcs_config(&vmcs_conf) < 0)
3638 *(int *)rtn = -EIO;
3639 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3640 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3641 smp_processor_id());
3642 *(int *)rtn = -EIO;
3643 }
3644}
3645
Sheng Yang67253af2008-04-25 10:20:22 +08003646static int get_ept_level(void)
3647{
3648 return VMX_EPT_DEFAULT_GAW + 1;
3649}
3650
Sheng Yang64d4d522008-10-09 16:01:57 +08003651static int vmx_get_mt_mask_shift(void)
3652{
3653 return VMX_EPT_MT_EPTE_SHIFT;
3654}
3655
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003656static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003657 .cpu_has_kvm_support = cpu_has_kvm_support,
3658 .disabled_by_bios = vmx_disabled_by_bios,
3659 .hardware_setup = hardware_setup,
3660 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003661 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003662 .hardware_enable = hardware_enable,
3663 .hardware_disable = hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003664 .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003665
3666 .vcpu_create = vmx_create_vcpu,
3667 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003668 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003669
Avi Kivity04d2cc72007-09-10 18:10:54 +03003670 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003671 .vcpu_load = vmx_vcpu_load,
3672 .vcpu_put = vmx_vcpu_put,
3673
3674 .set_guest_debug = set_guest_debug,
3675 .get_msr = vmx_get_msr,
3676 .set_msr = vmx_set_msr,
3677 .get_segment_base = vmx_get_segment_base,
3678 .get_segment = vmx_get_segment,
3679 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003680 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003681 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003682 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003683 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003684 .set_cr3 = vmx_set_cr3,
3685 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003686 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003687 .get_idt = vmx_get_idt,
3688 .set_idt = vmx_set_idt,
3689 .get_gdt = vmx_get_gdt,
3690 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003691 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003692 .get_rflags = vmx_get_rflags,
3693 .set_rflags = vmx_set_rflags,
3694
3695 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003696
Avi Kivity6aa8b732006-12-10 02:21:36 -08003697 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003698 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003699 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02003700 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003701 .get_irq = vmx_get_irq,
3702 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02003703 .queue_exception = vmx_queue_exception,
3704 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003705 .inject_pending_irq = vmx_intr_assist,
3706 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02003707
3708 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003709 .get_tdp_level = get_ept_level,
Sheng Yang64d4d522008-10-09 16:01:57 +08003710 .get_mt_mask_shift = vmx_get_mt_mask_shift,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003711};
3712
3713static int __init vmx_init(void)
3714{
Sheng Yang25c5f222008-03-28 13:18:56 +08003715 void *va;
He, Qingfdef3ad2007-04-30 09:45:24 +03003716 int r;
3717
3718 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3719 if (!vmx_io_bitmap_a)
3720 return -ENOMEM;
3721
3722 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3723 if (!vmx_io_bitmap_b) {
3724 r = -ENOMEM;
3725 goto out;
3726 }
3727
Sheng Yang25c5f222008-03-28 13:18:56 +08003728 vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
3729 if (!vmx_msr_bitmap) {
3730 r = -ENOMEM;
3731 goto out1;
3732 }
3733
He, Qingfdef3ad2007-04-30 09:45:24 +03003734 /*
3735 * Allow direct access to the PC debug port (it is often used for I/O
3736 * delays, but the vmexits simply slow things down).
3737 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003738 va = kmap(vmx_io_bitmap_a);
3739 memset(va, 0xff, PAGE_SIZE);
3740 clear_bit(0x80, va);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003741 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03003742
Sheng Yang25c5f222008-03-28 13:18:56 +08003743 va = kmap(vmx_io_bitmap_b);
3744 memset(va, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03003745 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03003746
Sheng Yang25c5f222008-03-28 13:18:56 +08003747 va = kmap(vmx_msr_bitmap);
3748 memset(va, 0xff, PAGE_SIZE);
3749 kunmap(vmx_msr_bitmap);
3750
Sheng Yang2384d2b2008-01-17 15:14:33 +08003751 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
3752
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003753 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03003754 if (r)
Sheng Yang25c5f222008-03-28 13:18:56 +08003755 goto out2;
3756
3757 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
3758 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
3759 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
3760 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
3761 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
He, Qingfdef3ad2007-04-30 09:45:24 +03003762
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003763 if (vm_need_ept()) {
Sheng Yang14394422008-04-28 12:24:45 +08003764 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003765 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08003766 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08003767 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang64d4d522008-10-09 16:01:57 +08003768 VMX_EPT_EXECUTABLE_MASK,
3769 VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08003770 kvm_enable_tdp();
3771 } else
3772 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08003773
Avi Kivityc7addb92007-09-16 18:58:32 +02003774 if (bypass_guest_pf)
3775 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
3776
Sheng Yang14394422008-04-28 12:24:45 +08003777 ept_sync_global();
3778
He, Qingfdef3ad2007-04-30 09:45:24 +03003779 return 0;
3780
Sheng Yang25c5f222008-03-28 13:18:56 +08003781out2:
3782 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003783out1:
3784 __free_page(vmx_io_bitmap_b);
3785out:
3786 __free_page(vmx_io_bitmap_a);
3787 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003788}
3789
3790static void __exit vmx_exit(void)
3791{
Sheng Yang25c5f222008-03-28 13:18:56 +08003792 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03003793 __free_page(vmx_io_bitmap_b);
3794 __free_page(vmx_io_bitmap_a);
3795
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003796 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003797}
3798
3799module_init(vmx_init)
3800module_exit(vmx_exit)