Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 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. |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 8 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9 | * |
| 10 | * Authors: |
| 11 | * Avi Kivity <avi@qumranet.com> |
| 12 | * Yaniv Kamay <yaniv@qumranet.com> |
| 13 | * |
| 14 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 15 | * the COPYING file in the top-level directory. |
| 16 | * |
| 17 | */ |
| 18 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 19 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 20 | #include "mmu.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 21 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 22 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 23 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 24 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/mm.h> |
| 26 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 27 | #include <linux/sched.h> |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 28 | #include <linux/moduleparam.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 29 | #include <linux/ftrace_event.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 31 | #include <linux/tboot.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 32 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 33 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 34 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 35 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 36 | #include <asm/desc.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 37 | #include <asm/vmx.h> |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 38 | #include <asm/virtext.h> |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 39 | #include <asm/mce.h> |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 40 | #include <asm/i387.h> |
| 41 | #include <asm/xcr.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 42 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 43 | #include "trace.h" |
| 44 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 45 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
| 46 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 47 | MODULE_AUTHOR("Qumranet"); |
| 48 | MODULE_LICENSE("GPL"); |
| 49 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 50 | static int __read_mostly bypass_guest_pf = 1; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 51 | module_param(bypass_guest_pf, bool, S_IRUGO); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 52 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 53 | static int __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 54 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 55 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 56 | static int __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 57 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 58 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 59 | static int __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 60 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 61 | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 62 | static int __read_mostly enable_unrestricted_guest = 1; |
| 63 | module_param_named(unrestricted_guest, |
| 64 | enable_unrestricted_guest, bool, S_IRUGO); |
| 65 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 66 | static int __read_mostly emulate_invalid_guest_state = 0; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 67 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 68 | |
Dongxiao Xu | b923e62 | 2010-05-11 18:29:45 +0800 | [diff] [blame] | 69 | static int __read_mostly vmm_exclusive = 1; |
| 70 | module_param(vmm_exclusive, bool, S_IRUGO); |
| 71 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 72 | static int __read_mostly yield_on_hlt = 1; |
| 73 | module_param(yield_on_hlt, bool, S_IRUGO); |
| 74 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 75 | #define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \ |
| 76 | (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD) |
| 77 | #define KVM_GUEST_CR0_MASK \ |
| 78 | (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE) |
| 79 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \ |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 80 | (X86_CR0_WP | X86_CR0_NE) |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 81 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 82 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 83 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 84 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
| 85 | | X86_CR4_OSXMMEXCPT) |
| 86 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 87 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 88 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 89 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 90 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 91 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 92 | /* |
| 93 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 94 | * ple_gap: upper bound on the amount of time between two successive |
| 95 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
| 96 | * According to test, this time is usually small than 41 cycles. |
| 97 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 98 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 99 | * less than 2^12 cycles |
| 100 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 101 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 102 | */ |
| 103 | #define KVM_VMX_DEFAULT_PLE_GAP 41 |
| 104 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 |
| 105 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
| 106 | module_param(ple_gap, int, S_IRUGO); |
| 107 | |
| 108 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 109 | module_param(ple_window, int, S_IRUGO); |
| 110 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 111 | #define NR_AUTOLOAD_MSRS 1 |
| 112 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 113 | struct vmcs { |
| 114 | u32 revision_id; |
| 115 | u32 abort; |
| 116 | char data[0]; |
| 117 | }; |
| 118 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 119 | struct shared_msr_entry { |
| 120 | unsigned index; |
| 121 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 122 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 123 | }; |
| 124 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 125 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 126 | struct kvm_vcpu vcpu; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 127 | struct list_head local_vcpus_link; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 128 | unsigned long host_rsp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 129 | int launched; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 130 | u8 fail; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 131 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 132 | u32 idt_vectoring_info; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 133 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 134 | int nmsrs; |
| 135 | int save_nmsrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 136 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 137 | u64 msr_host_kernel_gs_base; |
| 138 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 139 | #endif |
| 140 | struct vmcs *vmcs; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 141 | struct msr_autoload { |
| 142 | unsigned nr; |
| 143 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 144 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 145 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 146 | struct { |
| 147 | int loaded; |
| 148 | u16 fs_sel, gs_sel, ldt_sel; |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 149 | int gs_ldt_reload_needed; |
| 150 | int fs_reload_needed; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 151 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 152 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 153 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 154 | ulong save_rflags; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 155 | struct kvm_save_segment { |
| 156 | u16 selector; |
| 157 | unsigned long base; |
| 158 | u32 limit; |
| 159 | u32 ar; |
| 160 | } tr, es, ds, fs, gs; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 161 | } rmode; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 162 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 163 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 164 | |
| 165 | /* Support for vnmi-less CPUs */ |
| 166 | int soft_vnmi_blocked; |
| 167 | ktime_t entry_time; |
| 168 | s64 vnmi_blocked_time; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 169 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 170 | |
| 171 | bool rdtscp_enabled; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 175 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 176 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 179 | static int init_rmode(struct kvm *kvm); |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 180 | static u64 construct_eptp(unsigned long root_hpa); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 181 | static void kvm_cpu_vmxon(u64 addr); |
| 182 | static void kvm_cpu_vmxoff(void); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 183 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 184 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 185 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 186 | static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 187 | static DEFINE_PER_CPU(struct desc_ptr, host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 188 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 189 | static unsigned long *vmx_io_bitmap_a; |
| 190 | static unsigned long *vmx_io_bitmap_b; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 191 | static unsigned long *vmx_msr_bitmap_legacy; |
| 192 | static unsigned long *vmx_msr_bitmap_longmode; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 193 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 194 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 195 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 196 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 197 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 198 | int size; |
| 199 | int order; |
| 200 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 201 | u32 pin_based_exec_ctrl; |
| 202 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 203 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 204 | u32 vmexit_ctrl; |
| 205 | u32 vmentry_ctrl; |
| 206 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 207 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 208 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 209 | u32 ept; |
| 210 | u32 vpid; |
| 211 | } vmx_capability; |
| 212 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 213 | #define VMX_SEGMENT_FIELD(seg) \ |
| 214 | [VCPU_SREG_##seg] = { \ |
| 215 | .selector = GUEST_##seg##_SELECTOR, \ |
| 216 | .base = GUEST_##seg##_BASE, \ |
| 217 | .limit = GUEST_##seg##_LIMIT, \ |
| 218 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 219 | } |
| 220 | |
| 221 | static struct kvm_vmx_segment_field { |
| 222 | unsigned selector; |
| 223 | unsigned base; |
| 224 | unsigned limit; |
| 225 | unsigned ar_bytes; |
| 226 | } kvm_vmx_segment_fields[] = { |
| 227 | VMX_SEGMENT_FIELD(CS), |
| 228 | VMX_SEGMENT_FIELD(DS), |
| 229 | VMX_SEGMENT_FIELD(ES), |
| 230 | VMX_SEGMENT_FIELD(FS), |
| 231 | VMX_SEGMENT_FIELD(GS), |
| 232 | VMX_SEGMENT_FIELD(SS), |
| 233 | VMX_SEGMENT_FIELD(TR), |
| 234 | VMX_SEGMENT_FIELD(LDTR), |
| 235 | }; |
| 236 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 237 | static u64 host_efer; |
| 238 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 239 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 240 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 241 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 242 | * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 243 | * away by decrementing the array size. |
| 244 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 245 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 246 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 247 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 248 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 249 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 250 | }; |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 251 | #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 252 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 253 | static inline bool is_page_fault(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 254 | { |
| 255 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 256 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 257 | (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 260 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 261 | { |
| 262 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 263 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 264 | (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 265 | } |
| 266 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 267 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 268 | { |
| 269 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 270 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 271 | (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 272 | } |
| 273 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 274 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 275 | { |
| 276 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 277 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 278 | } |
| 279 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 280 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 281 | { |
| 282 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 283 | INTR_INFO_VALID_MASK)) == |
| 284 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 285 | } |
| 286 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 287 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 288 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 289 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 290 | } |
| 291 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 292 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 293 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 294 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 295 | } |
| 296 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 297 | static inline bool vm_need_tpr_shadow(struct kvm *kvm) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 298 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 299 | return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 300 | } |
| 301 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 302 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 303 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 304 | return vmcs_config.cpu_based_exec_ctrl & |
| 305 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 306 | } |
| 307 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 308 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 309 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 310 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 311 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 312 | } |
| 313 | |
| 314 | static inline bool cpu_has_vmx_flexpriority(void) |
| 315 | { |
| 316 | return cpu_has_vmx_tpr_shadow() && |
| 317 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 318 | } |
| 319 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 320 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 321 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 322 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static inline bool cpu_has_vmx_eptp_uncacheable(void) |
| 326 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 327 | return vmx_capability.ept & VMX_EPTP_UC_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static inline bool cpu_has_vmx_eptp_writeback(void) |
| 331 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 332 | return vmx_capability.ept & VMX_EPTP_WB_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 336 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 337 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 338 | } |
| 339 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 340 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 341 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 342 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 343 | } |
| 344 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 345 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 346 | { |
| 347 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 348 | } |
| 349 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 350 | static inline bool cpu_has_vmx_invept_individual_addr(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 351 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 352 | return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 353 | } |
| 354 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 355 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 356 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 357 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 358 | } |
| 359 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 360 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 361 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 362 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 363 | } |
| 364 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 365 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 366 | { |
| 367 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 368 | } |
| 369 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 370 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 371 | { |
| 372 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 373 | } |
| 374 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 375 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 376 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 377 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 378 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 379 | } |
| 380 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 381 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 382 | { |
| 383 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 384 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 385 | } |
| 386 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 387 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 388 | { |
| 389 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 390 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 391 | } |
| 392 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 393 | static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 394 | { |
Gui Jianfeng | 6d3e435 | 2010-01-29 15:36:59 +0800 | [diff] [blame] | 395 | return flexpriority_enabled && irqchip_in_kernel(kvm); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 396 | } |
| 397 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 398 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 399 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 400 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 401 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 402 | } |
| 403 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 404 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 405 | { |
| 406 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 407 | SECONDARY_EXEC_RDTSCP; |
| 408 | } |
| 409 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 410 | static inline bool cpu_has_virtual_nmis(void) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 411 | { |
| 412 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 413 | } |
| 414 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 415 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 416 | { |
| 417 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 418 | SECONDARY_EXEC_WBINVD_EXITING; |
| 419 | } |
| 420 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 421 | static inline bool report_flexpriority(void) |
| 422 | { |
| 423 | return flexpriority_enabled; |
| 424 | } |
| 425 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 426 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 427 | { |
| 428 | int i; |
| 429 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 430 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 431 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 432 | return i; |
| 433 | return -1; |
| 434 | } |
| 435 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 436 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 437 | { |
| 438 | struct { |
| 439 | u64 vpid : 16; |
| 440 | u64 rsvd : 48; |
| 441 | u64 gva; |
| 442 | } operand = { vpid, 0, gva }; |
| 443 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 444 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 445 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 446 | "; ja 1f ; ud2 ; 1:" |
| 447 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 448 | } |
| 449 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 450 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 451 | { |
| 452 | struct { |
| 453 | u64 eptp, gpa; |
| 454 | } operand = {eptp, gpa}; |
| 455 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 456 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 457 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 458 | "; ja 1f ; ud2 ; 1:\n" |
| 459 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 460 | } |
| 461 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 462 | static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 463 | { |
| 464 | int i; |
| 465 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 466 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 467 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 468 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 469 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 472 | static void vmcs_clear(struct vmcs *vmcs) |
| 473 | { |
| 474 | u64 phys_addr = __pa(vmcs); |
| 475 | u8 error; |
| 476 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 477 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 478 | : "=g"(error) : "a"(&phys_addr), "m"(phys_addr) |
| 479 | : "cc", "memory"); |
| 480 | if (error) |
| 481 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 482 | vmcs, phys_addr); |
| 483 | } |
| 484 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 485 | static void vmcs_load(struct vmcs *vmcs) |
| 486 | { |
| 487 | u64 phys_addr = __pa(vmcs); |
| 488 | u8 error; |
| 489 | |
| 490 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
| 491 | : "=g"(error) : "a"(&phys_addr), "m"(phys_addr) |
| 492 | : "cc", "memory"); |
| 493 | if (error) |
| 494 | printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n", |
| 495 | vmcs, phys_addr); |
| 496 | } |
| 497 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 498 | static void __vcpu_clear(void *arg) |
| 499 | { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 500 | struct vcpu_vmx *vmx = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 501 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 502 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 503 | if (vmx->vcpu.cpu == cpu) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 504 | vmcs_clear(vmx->vmcs); |
| 505 | if (per_cpu(current_vmcs, cpu) == vmx->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 506 | per_cpu(current_vmcs, cpu) = NULL; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 507 | list_del(&vmx->local_vcpus_link); |
| 508 | vmx->vcpu.cpu = -1; |
| 509 | vmx->launched = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 512 | static void vcpu_clear(struct vcpu_vmx *vmx) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 513 | { |
Avi Kivity | eae5ecb | 2007-09-30 10:50:12 +0200 | [diff] [blame] | 514 | if (vmx->vcpu.cpu == -1) |
| 515 | return; |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 516 | smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Gui Jianfeng | 1760dd4 | 2010-06-07 10:33:27 +0800 | [diff] [blame] | 519 | static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 520 | { |
| 521 | if (vmx->vpid == 0) |
| 522 | return; |
| 523 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 524 | if (cpu_has_vmx_invvpid_single()) |
| 525 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 526 | } |
| 527 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 528 | static inline void vpid_sync_vcpu_global(void) |
| 529 | { |
| 530 | if (cpu_has_vmx_invvpid_global()) |
| 531 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 532 | } |
| 533 | |
| 534 | static inline void vpid_sync_context(struct vcpu_vmx *vmx) |
| 535 | { |
| 536 | if (cpu_has_vmx_invvpid_single()) |
Gui Jianfeng | 1760dd4 | 2010-06-07 10:33:27 +0800 | [diff] [blame] | 537 | vpid_sync_vcpu_single(vmx); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 538 | else |
| 539 | vpid_sync_vcpu_global(); |
| 540 | } |
| 541 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 542 | static inline void ept_sync_global(void) |
| 543 | { |
| 544 | if (cpu_has_vmx_invept_global()) |
| 545 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 546 | } |
| 547 | |
| 548 | static inline void ept_sync_context(u64 eptp) |
| 549 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 550 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 551 | if (cpu_has_vmx_invept_context()) |
| 552 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 553 | else |
| 554 | ept_sync_global(); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa) |
| 559 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 560 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 561 | if (cpu_has_vmx_invept_individual_addr()) |
| 562 | __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR, |
| 563 | eptp, gpa); |
| 564 | else |
| 565 | ept_sync_context(eptp); |
| 566 | } |
| 567 | } |
| 568 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 569 | static unsigned long vmcs_readl(unsigned long field) |
| 570 | { |
Avi Kivity | a295673 | 2010-12-02 17:55:23 +0200 | [diff] [blame] | 571 | unsigned long value = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 572 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 573 | asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX) |
Avi Kivity | a295673 | 2010-12-02 17:55:23 +0200 | [diff] [blame] | 574 | : "+a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 575 | return value; |
| 576 | } |
| 577 | |
| 578 | static u16 vmcs_read16(unsigned long field) |
| 579 | { |
| 580 | return vmcs_readl(field); |
| 581 | } |
| 582 | |
| 583 | static u32 vmcs_read32(unsigned long field) |
| 584 | { |
| 585 | return vmcs_readl(field); |
| 586 | } |
| 587 | |
| 588 | static u64 vmcs_read64(unsigned long field) |
| 589 | { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 590 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 591 | return vmcs_readl(field); |
| 592 | #else |
| 593 | return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32); |
| 594 | #endif |
| 595 | } |
| 596 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 597 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 598 | { |
| 599 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 600 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 601 | dump_stack(); |
| 602 | } |
| 603 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 604 | static void vmcs_writel(unsigned long field, unsigned long value) |
| 605 | { |
| 606 | u8 error; |
| 607 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 608 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 609 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 610 | if (unlikely(error)) |
| 611 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static void vmcs_write16(unsigned long field, u16 value) |
| 615 | { |
| 616 | vmcs_writel(field, value); |
| 617 | } |
| 618 | |
| 619 | static void vmcs_write32(unsigned long field, u32 value) |
| 620 | { |
| 621 | vmcs_writel(field, value); |
| 622 | } |
| 623 | |
| 624 | static void vmcs_write64(unsigned long field, u64 value) |
| 625 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 626 | vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 627 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 628 | asm volatile (""); |
| 629 | vmcs_writel(field+1, value >> 32); |
| 630 | #endif |
| 631 | } |
| 632 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 633 | static void vmcs_clear_bits(unsigned long field, u32 mask) |
| 634 | { |
| 635 | vmcs_writel(field, vmcs_readl(field) & ~mask); |
| 636 | } |
| 637 | |
| 638 | static void vmcs_set_bits(unsigned long field, u32 mask) |
| 639 | { |
| 640 | vmcs_writel(field, vmcs_readl(field) | mask); |
| 641 | } |
| 642 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 643 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 644 | { |
| 645 | u32 eb; |
| 646 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 647 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
| 648 | (1u << NM_VECTOR) | (1u << DB_VECTOR); |
| 649 | if ((vcpu->guest_debug & |
| 650 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 651 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 652 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 653 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 654 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 655 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 656 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 657 | if (vcpu->fpu_active) |
| 658 | eb &= ~(1u << NM_VECTOR); |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 659 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 660 | } |
| 661 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 662 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 663 | { |
| 664 | unsigned i; |
| 665 | struct msr_autoload *m = &vmx->msr_autoload; |
| 666 | |
| 667 | for (i = 0; i < m->nr; ++i) |
| 668 | if (m->guest[i].index == msr) |
| 669 | break; |
| 670 | |
| 671 | if (i == m->nr) |
| 672 | return; |
| 673 | --m->nr; |
| 674 | m->guest[i] = m->guest[m->nr]; |
| 675 | m->host[i] = m->host[m->nr]; |
| 676 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 677 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 678 | } |
| 679 | |
| 680 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 681 | u64 guest_val, u64 host_val) |
| 682 | { |
| 683 | unsigned i; |
| 684 | struct msr_autoload *m = &vmx->msr_autoload; |
| 685 | |
| 686 | for (i = 0; i < m->nr; ++i) |
| 687 | if (m->guest[i].index == msr) |
| 688 | break; |
| 689 | |
| 690 | if (i == m->nr) { |
| 691 | ++m->nr; |
| 692 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 693 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 694 | } |
| 695 | |
| 696 | m->guest[i].index = msr; |
| 697 | m->guest[i].value = guest_val; |
| 698 | m->host[i].index = msr; |
| 699 | m->host[i].value = host_val; |
| 700 | } |
| 701 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 702 | static void reload_tss(void) |
| 703 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 704 | /* |
| 705 | * VT restores TR but not its size. Useless. |
| 706 | */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 707 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 708 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 709 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 710 | descs = (void *)gdt->address; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 711 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 712 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 713 | } |
| 714 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 715 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 716 | { |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 717 | u64 guest_efer; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 718 | u64 ignore_bits; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 719 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 720 | guest_efer = vmx->vcpu.arch.efer; |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 721 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 722 | /* |
| 723 | * NX is emulated; LMA and LME handled by hardware; SCE meaninless |
| 724 | * outside long mode |
| 725 | */ |
| 726 | ignore_bits = EFER_NX | EFER_SCE; |
| 727 | #ifdef CONFIG_X86_64 |
| 728 | ignore_bits |= EFER_LMA | EFER_LME; |
| 729 | /* SCE is meaningful only in long mode on Intel */ |
| 730 | if (guest_efer & EFER_LMA) |
| 731 | ignore_bits &= ~(u64)EFER_SCE; |
| 732 | #endif |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 733 | guest_efer &= ~ignore_bits; |
| 734 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 735 | vmx->guest_msrs[efer_offset].data = guest_efer; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 736 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 737 | |
| 738 | clear_atomic_switch_msr(vmx, MSR_EFER); |
| 739 | /* On ept, can't emulate nx, and must switch nx atomically */ |
| 740 | if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) { |
| 741 | guest_efer = vmx->vcpu.arch.efer; |
| 742 | if (!(guest_efer & EFER_LMA)) |
| 743 | guest_efer &= ~EFER_LME; |
| 744 | add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer); |
| 745 | return false; |
| 746 | } |
| 747 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 748 | return true; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 749 | } |
| 750 | |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 751 | static unsigned long segment_base(u16 selector) |
| 752 | { |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 753 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 754 | struct desc_struct *d; |
| 755 | unsigned long table_base; |
| 756 | unsigned long v; |
| 757 | |
| 758 | if (!(selector & ~3)) |
| 759 | return 0; |
| 760 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 761 | table_base = gdt->address; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 762 | |
| 763 | if (selector & 4) { /* from ldt */ |
| 764 | u16 ldt_selector = kvm_read_ldt(); |
| 765 | |
| 766 | if (!(ldt_selector & ~3)) |
| 767 | return 0; |
| 768 | |
| 769 | table_base = segment_base(ldt_selector); |
| 770 | } |
| 771 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
| 772 | v = get_desc_base(d); |
| 773 | #ifdef CONFIG_X86_64 |
| 774 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 775 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
| 776 | #endif |
| 777 | return v; |
| 778 | } |
| 779 | |
| 780 | static inline unsigned long kvm_read_tr_base(void) |
| 781 | { |
| 782 | u16 tr; |
| 783 | asm("str %0" : "=g"(tr)); |
| 784 | return segment_base(tr); |
| 785 | } |
| 786 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 787 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 788 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 789 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 790 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 791 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 792 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 793 | return; |
| 794 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 795 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 796 | /* |
| 797 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 798 | * allow segment selectors with cpl > 0 or ti == 1. |
| 799 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 800 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 801 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 802 | savesegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 803 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 804 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 805 | vmx->host_state.fs_reload_needed = 0; |
| 806 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 807 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 808 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 809 | } |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 810 | savesegment(gs, vmx->host_state.gs_sel); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 811 | if (!(vmx->host_state.gs_sel & 7)) |
| 812 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 813 | else { |
| 814 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 815 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | #ifdef CONFIG_X86_64 |
| 819 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 820 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 821 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 822 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 823 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 824 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 825 | |
| 826 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 827 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
| 828 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 829 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 830 | #endif |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 831 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 832 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 833 | vmx->guest_msrs[i].data, |
| 834 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 835 | } |
| 836 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 837 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 838 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 839 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 840 | return; |
| 841 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 842 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 843 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 844 | #ifdef CONFIG_X86_64 |
| 845 | if (is_long_mode(&vmx->vcpu)) |
| 846 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 847 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 848 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 849 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 850 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 851 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 852 | #else |
| 853 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 854 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 855 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 856 | if (vmx->host_state.fs_reload_needed) |
| 857 | loadsegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 858 | reload_tss(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 859 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 860 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 861 | #endif |
Avi Kivity | 1c11e71 | 2010-05-03 16:05:44 +0300 | [diff] [blame] | 862 | if (current_thread_info()->status & TS_USEDFPU) |
| 863 | clts(); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 864 | load_gdt(&__get_cpu_var(host_gdt)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 865 | } |
| 866 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 867 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 868 | { |
| 869 | preempt_disable(); |
| 870 | __vmx_load_host_state(vmx); |
| 871 | preempt_enable(); |
| 872 | } |
| 873 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 874 | /* |
| 875 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 876 | * vcpu mutex is already taken. |
| 877 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 878 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 879 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 880 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 881 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 882 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 883 | if (!vmm_exclusive) |
| 884 | kvm_cpu_vmxon(phys_addr); |
| 885 | else if (vcpu->cpu != cpu) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 886 | vcpu_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 887 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 888 | if (per_cpu(current_vmcs, cpu) != vmx->vmcs) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 889 | per_cpu(current_vmcs, cpu) = vmx->vmcs; |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 890 | vmcs_load(vmx->vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | if (vcpu->cpu != cpu) { |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 894 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 895 | unsigned long sysenter_esp; |
| 896 | |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 897 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 898 | local_irq_disable(); |
| 899 | list_add(&vmx->local_vcpus_link, |
| 900 | &per_cpu(vcpus_on_cpu, cpu)); |
| 901 | local_irq_enable(); |
| 902 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 903 | /* |
| 904 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 905 | * processors. |
| 906 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 907 | vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 908 | vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 909 | |
| 910 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 911 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
| 912 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 916 | { |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 917 | __vmx_load_host_state(to_vmx(vcpu)); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 918 | if (!vmm_exclusive) { |
Dongxiao Xu | b923e62 | 2010-05-11 18:29:45 +0800 | [diff] [blame] | 919 | __vcpu_clear(to_vmx(vcpu)); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 920 | kvm_cpu_vmxoff(); |
| 921 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 922 | } |
| 923 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 924 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 925 | { |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 926 | ulong cr0; |
| 927 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 928 | if (vcpu->fpu_active) |
| 929 | return; |
| 930 | vcpu->fpu_active = 1; |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 931 | cr0 = vmcs_readl(GUEST_CR0); |
| 932 | cr0 &= ~(X86_CR0_TS | X86_CR0_MP); |
| 933 | cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP); |
| 934 | vmcs_writel(GUEST_CR0, cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 935 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 936 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 937 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 938 | } |
| 939 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 940 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 941 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 942 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 943 | { |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 944 | vmx_decache_cr0_guest_bits(vcpu); |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 945 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 946 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 947 | vcpu->arch.cr0_guest_owned_bits = 0; |
| 948 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 949 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 950 | } |
| 951 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 952 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 953 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 954 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 955 | |
| 956 | rflags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 957 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 958 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 959 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 960 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 961 | } |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 962 | return rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 966 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 967 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 968 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 969 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 970 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 971 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 972 | } |
| 973 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 974 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 975 | { |
| 976 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 977 | int ret = 0; |
| 978 | |
| 979 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 980 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 981 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 982 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 983 | |
| 984 | return ret & mask; |
| 985 | } |
| 986 | |
| 987 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 988 | { |
| 989 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 990 | u32 interruptibility = interruptibility_old; |
| 991 | |
| 992 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 993 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 994 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 995 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 996 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 997 | interruptibility |= GUEST_INTR_STATE_STI; |
| 998 | |
| 999 | if ((interruptibility != interruptibility_old)) |
| 1000 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 1001 | } |
| 1002 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1003 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 1004 | { |
| 1005 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1006 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1007 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1008 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1009 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1010 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1011 | /* skipping an emulated instruction also counts */ |
| 1012 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 1015 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
| 1016 | { |
| 1017 | /* Ensure that we clear the HLT state in the VMCS. We don't need to |
| 1018 | * explicitly skip the instruction because if the HLT state is set, then |
| 1019 | * the instruction is already executing and RIP has already been |
| 1020 | * advanced. */ |
| 1021 | if (!yield_on_hlt && |
| 1022 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) |
| 1023 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
| 1024 | } |
| 1025 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1026 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 1027 | bool has_error_code, u32 error_code, |
| 1028 | bool reinject) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1029 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1030 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1031 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1032 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1033 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1034 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1035 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 1036 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1037 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1038 | if (vmx->rmode.vm86_active) { |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 1039 | if (kvm_inject_realmode_interrupt(vcpu, nr) != EMULATE_DONE) |
| 1040 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1041 | return; |
| 1042 | } |
| 1043 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 1044 | if (kvm_exception_is_soft(nr)) { |
| 1045 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 1046 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1047 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 1048 | } else |
| 1049 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 1050 | |
| 1051 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 1052 | vmx_clear_hlt(vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1053 | } |
| 1054 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1055 | static bool vmx_rdtscp_supported(void) |
| 1056 | { |
| 1057 | return cpu_has_vmx_rdtscp(); |
| 1058 | } |
| 1059 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1060 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1061 | * Swap MSR entry in host/guest MSR entry array. |
| 1062 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1063 | static void move_msr_up(struct vcpu_vmx *vmx, int from, int to) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1064 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1065 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1066 | |
| 1067 | tmp = vmx->guest_msrs[to]; |
| 1068 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 1069 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1073 | * Set up the vmcs to automatically save and restore system |
| 1074 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 1075 | * mode, as fiddling with msrs is very expensive. |
| 1076 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1077 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1078 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1079 | int save_nmsrs, index; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 1080 | unsigned long *msr_bitmap; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1081 | |
Avi Kivity | 33f9c50 | 2008-02-27 16:06:57 +0200 | [diff] [blame] | 1082 | vmx_load_host_state(vmx); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1083 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1084 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1085 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1086 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1087 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1088 | move_msr_up(vmx, index, save_nmsrs++); |
| 1089 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1090 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1091 | move_msr_up(vmx, index, save_nmsrs++); |
| 1092 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1093 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1094 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1095 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
| 1096 | if (index >= 0 && vmx->rdtscp_enabled) |
| 1097 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1098 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1099 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1100 | * if efer.sce is enabled. |
| 1101 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1102 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1103 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1104 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1105 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1106 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1107 | index = __find_msr_index(vmx, MSR_EFER); |
| 1108 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1109 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1110 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1111 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 1112 | |
| 1113 | if (cpu_has_vmx_msr_bitmap()) { |
| 1114 | if (is_long_mode(&vmx->vcpu)) |
| 1115 | msr_bitmap = vmx_msr_bitmap_longmode; |
| 1116 | else |
| 1117 | msr_bitmap = vmx_msr_bitmap_legacy; |
| 1118 | |
| 1119 | vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); |
| 1120 | } |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1124 | * reads and returns guest's timestamp counter "register" |
| 1125 | * guest_tsc = host_tsc + tsc_offset -- 21.3 |
| 1126 | */ |
| 1127 | static u64 guest_read_tsc(void) |
| 1128 | { |
| 1129 | u64 host_tsc, tsc_offset; |
| 1130 | |
| 1131 | rdtscll(host_tsc); |
| 1132 | tsc_offset = vmcs_read64(TSC_OFFSET); |
| 1133 | return host_tsc + tsc_offset; |
| 1134 | } |
| 1135 | |
| 1136 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1137 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1138 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1139 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1140 | { |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1141 | vmcs_write64(TSC_OFFSET, offset); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 1144 | static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment) |
| 1145 | { |
| 1146 | u64 offset = vmcs_read64(TSC_OFFSET); |
| 1147 | vmcs_write64(TSC_OFFSET, offset + adjustment); |
| 1148 | } |
| 1149 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1150 | /* |
| 1151 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 1152 | * Returns 0 on success, non-0 otherwise. |
| 1153 | * Assumes vcpu_load() was already called. |
| 1154 | */ |
| 1155 | static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 1156 | { |
| 1157 | u64 data; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1158 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1159 | |
| 1160 | if (!pdata) { |
| 1161 | printk(KERN_ERR "BUG: get_msr called with NULL pdata\n"); |
| 1162 | return -EINVAL; |
| 1163 | } |
| 1164 | |
| 1165 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1166 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1167 | case MSR_FS_BASE: |
| 1168 | data = vmcs_readl(GUEST_FS_BASE); |
| 1169 | break; |
| 1170 | case MSR_GS_BASE: |
| 1171 | data = vmcs_readl(GUEST_GS_BASE); |
| 1172 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1173 | case MSR_KERNEL_GS_BASE: |
| 1174 | vmx_load_host_state(to_vmx(vcpu)); |
| 1175 | data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
| 1176 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1177 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1178 | case MSR_EFER: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1179 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 1180 | case MSR_IA32_TSC: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1181 | data = guest_read_tsc(); |
| 1182 | break; |
| 1183 | case MSR_IA32_SYSENTER_CS: |
| 1184 | data = vmcs_read32(GUEST_SYSENTER_CS); |
| 1185 | break; |
| 1186 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 1187 | data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1188 | break; |
| 1189 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 1190 | data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1191 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1192 | case MSR_TSC_AUX: |
| 1193 | if (!to_vmx(vcpu)->rdtscp_enabled) |
| 1194 | return 1; |
| 1195 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1196 | default: |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1197 | vmx_load_host_state(to_vmx(vcpu)); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1198 | msr = find_msr_entry(to_vmx(vcpu), msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1199 | if (msr) { |
Gleb Natapov | 542423b | 2009-08-27 15:07:30 +0300 | [diff] [blame] | 1200 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1201 | data = msr->data; |
| 1202 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1203 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1204 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | *pdata = data; |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | /* |
| 1212 | * Writes msr value into into the appropriate "register". |
| 1213 | * Returns 0 on success, non-0 otherwise. |
| 1214 | * Assumes vcpu_load() was already called. |
| 1215 | */ |
| 1216 | static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 1217 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1218 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1219 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1220 | int ret = 0; |
| 1221 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1222 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1223 | case MSR_EFER: |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1224 | vmx_load_host_state(vmx); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1225 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1226 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 1227 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1228 | case MSR_FS_BASE: |
| 1229 | vmcs_writel(GUEST_FS_BASE, data); |
| 1230 | break; |
| 1231 | case MSR_GS_BASE: |
| 1232 | vmcs_writel(GUEST_GS_BASE, data); |
| 1233 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1234 | case MSR_KERNEL_GS_BASE: |
| 1235 | vmx_load_host_state(vmx); |
| 1236 | vmx->msr_guest_kernel_gs_base = data; |
| 1237 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1238 | #endif |
| 1239 | case MSR_IA32_SYSENTER_CS: |
| 1240 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 1241 | break; |
| 1242 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 1243 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1244 | break; |
| 1245 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 1246 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1247 | break; |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 1248 | case MSR_IA32_TSC: |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1249 | kvm_write_tsc(vcpu, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1250 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 1251 | case MSR_IA32_CR_PAT: |
| 1252 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 1253 | vmcs_write64(GUEST_IA32_PAT, data); |
| 1254 | vcpu->arch.pat = data; |
| 1255 | break; |
| 1256 | } |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1257 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
| 1258 | break; |
| 1259 | case MSR_TSC_AUX: |
| 1260 | if (!vmx->rdtscp_enabled) |
| 1261 | return 1; |
| 1262 | /* Check reserved bit, higher 32 bits should be zero */ |
| 1263 | if ((data >> 32) != 0) |
| 1264 | return 1; |
| 1265 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1266 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1267 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1268 | if (msr) { |
Gleb Natapov | 542423b | 2009-08-27 15:07:30 +0300 | [diff] [blame] | 1269 | vmx_load_host_state(vmx); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1270 | msr->data = data; |
| 1271 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1272 | } |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1273 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1274 | } |
| 1275 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1276 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1277 | } |
| 1278 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1279 | static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1280 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1281 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 1282 | switch (reg) { |
| 1283 | case VCPU_REGS_RSP: |
| 1284 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 1285 | break; |
| 1286 | case VCPU_REGS_RIP: |
| 1287 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 1288 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1289 | case VCPU_EXREG_PDPTR: |
| 1290 | if (enable_ept) |
| 1291 | ept_save_pdptrs(vcpu); |
| 1292 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1293 | default: |
| 1294 | break; |
| 1295 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1296 | } |
| 1297 | |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 1298 | static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1299 | { |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1300 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 1301 | vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]); |
| 1302 | else |
| 1303 | vmcs_writel(GUEST_DR7, vcpu->arch.dr7); |
| 1304 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1305 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | static __init int cpu_has_kvm_support(void) |
| 1309 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 1310 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | static __init int vmx_disabled_by_bios(void) |
| 1314 | { |
| 1315 | u64 msr; |
| 1316 | |
| 1317 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1318 | if (msr & FEATURE_CONTROL_LOCKED) { |
| 1319 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 1320 | && tboot_enabled()) |
| 1321 | return 1; |
| 1322 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 1323 | && !tboot_enabled()) { |
| 1324 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
| 1325 | " activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1326 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 1327 | } |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | return 0; |
Yang, Sheng | 62b3ffb | 2007-07-25 12:17:06 +0300 | [diff] [blame] | 1331 | /* locked but not enabled */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1332 | } |
| 1333 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1334 | static void kvm_cpu_vmxon(u64 addr) |
| 1335 | { |
| 1336 | asm volatile (ASM_VMX_VMXON_RAX |
| 1337 | : : "a"(&addr), "m"(addr) |
| 1338 | : "memory", "cc"); |
| 1339 | } |
| 1340 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 1341 | static int hardware_enable(void *garbage) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1342 | { |
| 1343 | int cpu = raw_smp_processor_id(); |
| 1344 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1345 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1346 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 1347 | if (read_cr4() & X86_CR4_VMXE) |
| 1348 | return -EBUSY; |
| 1349 | |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 1350 | INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1351 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1352 | |
| 1353 | test_bits = FEATURE_CONTROL_LOCKED; |
| 1354 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 1355 | if (tboot_enabled()) |
| 1356 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 1357 | |
| 1358 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1359 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1360 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 1361 | } |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1362 | write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */ |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 1363 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1364 | if (vmm_exclusive) { |
| 1365 | kvm_cpu_vmxon(phys_addr); |
| 1366 | ept_sync_global(); |
| 1367 | } |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 1368 | |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 1369 | store_gdt(&__get_cpu_var(host_gdt)); |
| 1370 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 1371 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1372 | } |
| 1373 | |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 1374 | static void vmclear_local_vcpus(void) |
| 1375 | { |
| 1376 | int cpu = raw_smp_processor_id(); |
| 1377 | struct vcpu_vmx *vmx, *n; |
| 1378 | |
| 1379 | list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu), |
| 1380 | local_vcpus_link) |
| 1381 | __vcpu_clear(vmx); |
| 1382 | } |
| 1383 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 1384 | |
| 1385 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 1386 | * tricks. |
| 1387 | */ |
| 1388 | static void kvm_cpu_vmxoff(void) |
| 1389 | { |
| 1390 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 1391 | } |
| 1392 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1393 | static void hardware_disable(void *garbage) |
| 1394 | { |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1395 | if (vmm_exclusive) { |
| 1396 | vmclear_local_vcpus(); |
| 1397 | kvm_cpu_vmxoff(); |
| 1398 | } |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1399 | write_cr4(read_cr4() & ~X86_CR4_VMXE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1402 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1403 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1404 | { |
| 1405 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1406 | u32 ctl = ctl_min | ctl_opt; |
| 1407 | |
| 1408 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 1409 | |
| 1410 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 1411 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 1412 | |
| 1413 | /* Ensure minimum (required) set of control bits are supported. */ |
| 1414 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1415 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1416 | |
| 1417 | *result = ctl; |
| 1418 | return 0; |
| 1419 | } |
| 1420 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1421 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1422 | { |
| 1423 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1424 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1425 | u32 _pin_based_exec_control = 0; |
| 1426 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1427 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1428 | u32 _vmexit_control = 0; |
| 1429 | u32 _vmentry_control = 0; |
| 1430 | |
| 1431 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 1432 | opt = PIN_BASED_VIRTUAL_NMIS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1433 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 1434 | &_pin_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1435 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1436 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 1437 | min = |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1438 | #ifdef CONFIG_X86_64 |
| 1439 | CPU_BASED_CR8_LOAD_EXITING | |
| 1440 | CPU_BASED_CR8_STORE_EXITING | |
| 1441 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1442 | CPU_BASED_CR3_LOAD_EXITING | |
| 1443 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1444 | CPU_BASED_USE_IO_BITMAPS | |
| 1445 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 1446 | CPU_BASED_USE_TSC_OFFSETING | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 1447 | CPU_BASED_MWAIT_EXITING | |
| 1448 | CPU_BASED_MONITOR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 1449 | CPU_BASED_INVLPG_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 1450 | |
| 1451 | if (yield_on_hlt) |
| 1452 | min |= CPU_BASED_HLT_EXITING; |
| 1453 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1454 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1455 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1456 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1457 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 1458 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1459 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1460 | #ifdef CONFIG_X86_64 |
| 1461 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 1462 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 1463 | ~CPU_BASED_CR8_STORE_EXITING; |
| 1464 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1465 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1466 | min2 = 0; |
| 1467 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1468 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1469 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1470 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1471 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1472 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
| 1473 | SECONDARY_EXEC_RDTSCP; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1474 | if (adjust_vmx_controls(min2, opt2, |
| 1475 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1476 | &_cpu_based_2nd_exec_control) < 0) |
| 1477 | return -EIO; |
| 1478 | } |
| 1479 | #ifndef CONFIG_X86_64 |
| 1480 | if (!(_cpu_based_2nd_exec_control & |
| 1481 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 1482 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 1483 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1484 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 1485 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 1486 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 1487 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 1488 | CPU_BASED_CR3_STORE_EXITING | |
| 1489 | CPU_BASED_INVLPG_EXITING); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1490 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 1491 | vmx_capability.ept, vmx_capability.vpid); |
| 1492 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1493 | |
| 1494 | min = 0; |
| 1495 | #ifdef CONFIG_X86_64 |
| 1496 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 1497 | #endif |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 1498 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1499 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 1500 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1501 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1502 | |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 1503 | min = 0; |
| 1504 | opt = VM_ENTRY_LOAD_IA32_PAT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1505 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 1506 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1507 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1508 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 1509 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1510 | |
| 1511 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 1512 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1513 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1514 | |
| 1515 | #ifdef CONFIG_X86_64 |
| 1516 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 1517 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1518 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1519 | #endif |
| 1520 | |
| 1521 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 1522 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1523 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1524 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1525 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
| 1526 | vmcs_conf->order = get_order(vmcs_config.size); |
| 1527 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1528 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1529 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 1530 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1531 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1532 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 1533 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1534 | |
| 1535 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 1536 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1537 | |
| 1538 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 1539 | { |
| 1540 | int node = cpu_to_node(cpu); |
| 1541 | struct page *pages; |
| 1542 | struct vmcs *vmcs; |
| 1543 | |
Mel Gorman | 6484eb3 | 2009-06-16 15:31:54 -0700 | [diff] [blame] | 1544 | pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1545 | if (!pages) |
| 1546 | return NULL; |
| 1547 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1548 | memset(vmcs, 0, vmcs_config.size); |
| 1549 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1550 | return vmcs; |
| 1551 | } |
| 1552 | |
| 1553 | static struct vmcs *alloc_vmcs(void) |
| 1554 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1555 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | static void free_vmcs(struct vmcs *vmcs) |
| 1559 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1560 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1561 | } |
| 1562 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 1563 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1564 | { |
| 1565 | int cpu; |
| 1566 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 1567 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1568 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 1569 | per_cpu(vmxarea, cpu) = NULL; |
| 1570 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1573 | static __init int alloc_kvm_area(void) |
| 1574 | { |
| 1575 | int cpu; |
| 1576 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 1577 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1578 | struct vmcs *vmcs; |
| 1579 | |
| 1580 | vmcs = alloc_vmcs_cpu(cpu); |
| 1581 | if (!vmcs) { |
| 1582 | free_kvm_area(); |
| 1583 | return -ENOMEM; |
| 1584 | } |
| 1585 | |
| 1586 | per_cpu(vmxarea, cpu) = vmcs; |
| 1587 | } |
| 1588 | return 0; |
| 1589 | } |
| 1590 | |
| 1591 | static __init int hardware_setup(void) |
| 1592 | { |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1593 | if (setup_vmcs_config(&vmcs_config) < 0) |
| 1594 | return -EIO; |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 1595 | |
| 1596 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 1597 | kvm_enable_efer_bits(EFER_NX); |
| 1598 | |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 1599 | if (!cpu_has_vmx_vpid()) |
| 1600 | enable_vpid = 0; |
| 1601 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1602 | if (!cpu_has_vmx_ept() || |
| 1603 | !cpu_has_vmx_ept_4levels()) { |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 1604 | enable_ept = 0; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1605 | enable_unrestricted_guest = 0; |
| 1606 | } |
| 1607 | |
| 1608 | if (!cpu_has_vmx_unrestricted_guest()) |
| 1609 | enable_unrestricted_guest = 0; |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 1610 | |
| 1611 | if (!cpu_has_vmx_flexpriority()) |
| 1612 | flexpriority_enabled = 0; |
| 1613 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 1614 | if (!cpu_has_vmx_tpr_shadow()) |
| 1615 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 1616 | |
Marcelo Tosatti | 54dee99 | 2009-06-11 12:07:44 -0300 | [diff] [blame] | 1617 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 1618 | kvm_disable_largepages(); |
| 1619 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1620 | if (!cpu_has_vmx_ple()) |
| 1621 | ple_gap = 0; |
| 1622 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1623 | return alloc_kvm_area(); |
| 1624 | } |
| 1625 | |
| 1626 | static __exit void hardware_unsetup(void) |
| 1627 | { |
| 1628 | free_kvm_area(); |
| 1629 | } |
| 1630 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1631 | static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save) |
| 1632 | { |
| 1633 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1634 | |
Avi Kivity | 6af11b9 | 2007-03-19 13:18:10 +0200 | [diff] [blame] | 1635 | if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1636 | vmcs_write16(sf->selector, save->selector); |
| 1637 | vmcs_writel(sf->base, save->base); |
| 1638 | vmcs_write32(sf->limit, save->limit); |
| 1639 | vmcs_write32(sf->ar_bytes, save->ar); |
| 1640 | } else { |
| 1641 | u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK) |
| 1642 | << AR_DPL_SHIFT; |
| 1643 | vmcs_write32(sf->ar_bytes, 0x93 | dpl); |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 1648 | { |
| 1649 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1650 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1651 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1652 | vmx->emulation_required = 1; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1653 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1654 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1655 | vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base); |
| 1656 | vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit); |
| 1657 | vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1658 | |
| 1659 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1660 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1661 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1662 | vmcs_writel(GUEST_RFLAGS, flags); |
| 1663 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1664 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 1665 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1666 | |
| 1667 | update_exception_bitmap(vcpu); |
| 1668 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1669 | if (emulate_invalid_guest_state) |
| 1670 | return; |
| 1671 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1672 | fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es); |
| 1673 | fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds); |
| 1674 | fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs); |
| 1675 | fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1676 | |
| 1677 | vmcs_write16(GUEST_SS_SELECTOR, 0); |
| 1678 | vmcs_write32(GUEST_SS_AR_BYTES, 0x93); |
| 1679 | |
| 1680 | vmcs_write16(GUEST_CS_SELECTOR, |
| 1681 | vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK); |
| 1682 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); |
| 1683 | } |
| 1684 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1685 | static gva_t rmode_tss_base(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1686 | { |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1687 | if (!kvm->arch.tss_addr) { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 1688 | struct kvm_memslots *slots; |
| 1689 | gfn_t base_gfn; |
| 1690 | |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 1691 | slots = kvm_memslots(kvm); |
Avi Kivity | f495c6e | 2010-06-10 17:21:29 +0300 | [diff] [blame] | 1692 | base_gfn = slots->memslots[0].base_gfn + |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 1693 | kvm->memslots->memslots[0].npages - 3; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 1694 | return base_gfn << PAGE_SHIFT; |
| 1695 | } |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1696 | return kvm->arch.tss_addr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | static void fix_rmode_seg(int seg, struct kvm_save_segment *save) |
| 1700 | { |
| 1701 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1702 | |
| 1703 | save->selector = vmcs_read16(sf->selector); |
| 1704 | save->base = vmcs_readl(sf->base); |
| 1705 | save->limit = vmcs_read32(sf->limit); |
| 1706 | save->ar = vmcs_read32(sf->ar_bytes); |
Jan Kiszka | 15b00f3 | 2007-11-19 10:21:45 +0100 | [diff] [blame] | 1707 | vmcs_write16(sf->selector, save->base >> 4); |
| 1708 | vmcs_write32(sf->base, save->base & 0xfffff); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1709 | vmcs_write32(sf->limit, 0xffff); |
| 1710 | vmcs_write32(sf->ar_bytes, 0xf3); |
| 1711 | } |
| 1712 | |
| 1713 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 1714 | { |
| 1715 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1716 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1717 | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1718 | if (enable_unrestricted_guest) |
| 1719 | return; |
| 1720 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1721 | vmx->emulation_required = 1; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1722 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1723 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1724 | vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1725 | vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm)); |
| 1726 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1727 | vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1728 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
| 1729 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1730 | vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1731 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 1732 | |
| 1733 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1734 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1735 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1736 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1737 | |
| 1738 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1739 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1740 | update_exception_bitmap(vcpu); |
| 1741 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1742 | if (emulate_invalid_guest_state) |
| 1743 | goto continue_rmode; |
| 1744 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1745 | vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4); |
| 1746 | vmcs_write32(GUEST_SS_LIMIT, 0xffff); |
| 1747 | vmcs_write32(GUEST_SS_AR_BYTES, 0xf3); |
| 1748 | |
| 1749 | vmcs_write32(GUEST_CS_AR_BYTES, 0xf3); |
Michael Riepe | abacf8d | 2006-12-22 01:05:45 -0800 | [diff] [blame] | 1750 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); |
Avi Kivity | 8cb5b03 | 2007-03-20 18:40:40 +0200 | [diff] [blame] | 1751 | if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000) |
| 1752 | vmcs_writel(GUEST_CS_BASE, 0xf0000); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1753 | vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4); |
| 1754 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1755 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es); |
| 1756 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds); |
| 1757 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs); |
| 1758 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 1759 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1760 | continue_rmode: |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 1761 | kvm_mmu_reset_context(vcpu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 1762 | init_rmode(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1763 | } |
| 1764 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 1765 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 1766 | { |
| 1767 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1768 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 1769 | |
| 1770 | if (!msr) |
| 1771 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 1772 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1773 | /* |
| 1774 | * Force kernel_gs_base reloading before EFER changes, as control |
| 1775 | * of this msr depends on is_long_mode(). |
| 1776 | */ |
| 1777 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1778 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 1779 | if (efer & EFER_LMA) { |
| 1780 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1781 | vmcs_read32(VM_ENTRY_CONTROLS) | |
| 1782 | VM_ENTRY_IA32E_MODE); |
| 1783 | msr->data = efer; |
| 1784 | } else { |
| 1785 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1786 | vmcs_read32(VM_ENTRY_CONTROLS) & |
| 1787 | ~VM_ENTRY_IA32E_MODE); |
| 1788 | |
| 1789 | msr->data = efer & ~EFER_LME; |
| 1790 | } |
| 1791 | setup_msrs(vmx); |
| 1792 | } |
| 1793 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1794 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1795 | |
| 1796 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 1797 | { |
| 1798 | u32 guest_tr_ar; |
| 1799 | |
| 1800 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
| 1801 | if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) { |
| 1802 | printk(KERN_DEBUG "%s: tss fixup for long mode. \n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1803 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1804 | vmcs_write32(GUEST_TR_AR_BYTES, |
| 1805 | (guest_tr_ar & ~AR_TYPE_MASK) |
| 1806 | | AR_TYPE_BUSY_64_TSS); |
| 1807 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 1808 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 1812 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1813 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1814 | vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1815 | & ~VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 1816 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | #endif |
| 1820 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1821 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 1822 | { |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1823 | vpid_sync_context(to_vmx(vcpu)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 1824 | if (enable_ept) { |
| 1825 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 1826 | return; |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 1827 | ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 1828 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1829 | } |
| 1830 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 1831 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 1832 | { |
| 1833 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 1834 | |
| 1835 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 1836 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 1837 | } |
| 1838 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 1839 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1840 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 1841 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 1842 | |
| 1843 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 1844 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1845 | } |
| 1846 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1847 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 1848 | { |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1849 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 1850 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 1851 | return; |
| 1852 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1853 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 1854 | vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]); |
| 1855 | vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]); |
| 1856 | vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]); |
| 1857 | vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 1860 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 1861 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 1862 | { |
| 1863 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 1864 | vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 1865 | vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 1866 | vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 1867 | vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 1868 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1869 | |
| 1870 | __set_bit(VCPU_EXREG_PDPTR, |
| 1871 | (unsigned long *)&vcpu->arch.regs_avail); |
| 1872 | __set_bit(VCPU_EXREG_PDPTR, |
| 1873 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 1874 | } |
| 1875 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1876 | static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
| 1877 | |
| 1878 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 1879 | unsigned long cr0, |
| 1880 | struct kvm_vcpu *vcpu) |
| 1881 | { |
| 1882 | if (!(cr0 & X86_CR0_PG)) { |
| 1883 | /* From paging/starting to nonpaging */ |
| 1884 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 1885 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1886 | (CPU_BASED_CR3_LOAD_EXITING | |
| 1887 | CPU_BASED_CR3_STORE_EXITING)); |
| 1888 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 1889 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1890 | } else if (!is_paging(vcpu)) { |
| 1891 | /* From nonpaging to paging */ |
| 1892 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 1893 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1894 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 1895 | CPU_BASED_CR3_STORE_EXITING)); |
| 1896 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 1897 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1898 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 1899 | |
| 1900 | if (!(cr0 & X86_CR0_WP)) |
| 1901 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1902 | } |
| 1903 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1904 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 1905 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1906 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1907 | unsigned long hw_cr0; |
| 1908 | |
| 1909 | if (enable_unrestricted_guest) |
| 1910 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST) |
| 1911 | | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 1912 | else |
| 1913 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1914 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1915 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1916 | enter_pmode(vcpu); |
| 1917 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1918 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1919 | enter_rmode(vcpu); |
| 1920 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1921 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1922 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1923 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1924 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1925 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1926 | exit_lmode(vcpu); |
| 1927 | } |
| 1928 | #endif |
| 1929 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1930 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1931 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 1932 | |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1933 | if (!vcpu->fpu_active) |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1934 | hw_cr0 |= X86_CR0_TS | X86_CR0_MP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1935 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1936 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1937 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1938 | vcpu->arch.cr0 = cr0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1939 | } |
| 1940 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1941 | static u64 construct_eptp(unsigned long root_hpa) |
| 1942 | { |
| 1943 | u64 eptp; |
| 1944 | |
| 1945 | /* TODO write the value reading from MSR */ |
| 1946 | eptp = VMX_EPT_DEFAULT_MT | |
| 1947 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
| 1948 | eptp |= (root_hpa & PAGE_MASK); |
| 1949 | |
| 1950 | return eptp; |
| 1951 | } |
| 1952 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1953 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 1954 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1955 | unsigned long guest_cr3; |
| 1956 | u64 eptp; |
| 1957 | |
| 1958 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1959 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1960 | eptp = construct_eptp(cr3); |
| 1961 | vmcs_write64(EPT_POINTER, eptp); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1962 | guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 : |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 1963 | vcpu->kvm->arch.ept_identity_map_addr; |
Marcelo Tosatti | 7c93be4 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 1964 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1965 | } |
| 1966 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1967 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1968 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 1972 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1973 | unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ? |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1974 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
| 1975 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1976 | vcpu->arch.cr4 = cr4; |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 1977 | if (enable_ept) { |
| 1978 | if (!is_paging(vcpu)) { |
| 1979 | hw_cr4 &= ~X86_CR4_PAE; |
| 1980 | hw_cr4 |= X86_CR4_PSE; |
| 1981 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 1982 | hw_cr4 &= ~X86_CR4_PAE; |
| 1983 | } |
| 1984 | } |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1985 | |
| 1986 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 1987 | vmcs_writel(GUEST_CR4, hw_cr4); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1988 | } |
| 1989 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1990 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 1991 | { |
| 1992 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1993 | |
| 1994 | return vmcs_readl(sf->base); |
| 1995 | } |
| 1996 | |
| 1997 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 1998 | struct kvm_segment *var, int seg) |
| 1999 | { |
| 2000 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 2001 | u32 ar; |
| 2002 | |
| 2003 | var->base = vmcs_readl(sf->base); |
| 2004 | var->limit = vmcs_read32(sf->limit); |
| 2005 | var->selector = vmcs_read16(sf->selector); |
| 2006 | ar = vmcs_read32(sf->ar_bytes); |
Avi Kivity | 9fd4a3b | 2009-01-04 23:43:42 +0200 | [diff] [blame] | 2007 | if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2008 | ar = 0; |
| 2009 | var->type = ar & 15; |
| 2010 | var->s = (ar >> 4) & 1; |
| 2011 | var->dpl = (ar >> 5) & 3; |
| 2012 | var->present = (ar >> 7) & 1; |
| 2013 | var->avl = (ar >> 12) & 1; |
| 2014 | var->l = (ar >> 13) & 1; |
| 2015 | var->db = (ar >> 14) & 1; |
| 2016 | var->g = (ar >> 15) & 1; |
| 2017 | var->unusable = (ar >> 16) & 1; |
| 2018 | } |
| 2019 | |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2020 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
| 2021 | { |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 2022 | if (!is_protmode(vcpu)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2023 | return 0; |
| 2024 | |
| 2025 | if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */ |
| 2026 | return 3; |
| 2027 | |
Avi Kivity | eab4b8a | 2009-08-04 15:02:54 +0300 | [diff] [blame] | 2028 | return vmcs_read16(GUEST_CS_SELECTOR) & 3; |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2029 | } |
| 2030 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2031 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2032 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2033 | u32 ar; |
| 2034 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2035 | if (var->unusable) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2036 | ar = 1 << 16; |
| 2037 | else { |
| 2038 | ar = var->type & 15; |
| 2039 | ar |= (var->s & 1) << 4; |
| 2040 | ar |= (var->dpl & 3) << 5; |
| 2041 | ar |= (var->present & 1) << 7; |
| 2042 | ar |= (var->avl & 1) << 12; |
| 2043 | ar |= (var->l & 1) << 13; |
| 2044 | ar |= (var->db & 1) << 14; |
| 2045 | ar |= (var->g & 1) << 15; |
| 2046 | } |
Uri Lublin | f7fbf1f | 2006-12-13 00:34:00 -0800 | [diff] [blame] | 2047 | if (ar == 0) /* a 0 value means unusable */ |
| 2048 | ar = AR_UNUSABLE_MASK; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2049 | |
| 2050 | return ar; |
| 2051 | } |
| 2052 | |
| 2053 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 2054 | struct kvm_segment *var, int seg) |
| 2055 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2056 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2057 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 2058 | u32 ar; |
| 2059 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2060 | if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) { |
| 2061 | vmx->rmode.tr.selector = var->selector; |
| 2062 | vmx->rmode.tr.base = var->base; |
| 2063 | vmx->rmode.tr.limit = var->limit; |
| 2064 | vmx->rmode.tr.ar = vmx_segment_access_rights(var); |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2065 | return; |
| 2066 | } |
| 2067 | vmcs_writel(sf->base, var->base); |
| 2068 | vmcs_write32(sf->limit, var->limit); |
| 2069 | vmcs_write16(sf->selector, var->selector); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2070 | if (vmx->rmode.vm86_active && var->s) { |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2071 | /* |
| 2072 | * Hack real-mode segments into vm86 compatibility. |
| 2073 | */ |
| 2074 | if (var->base == 0xffff0000 && var->selector == 0xf000) |
| 2075 | vmcs_writel(sf->base, 0xf0000); |
| 2076 | ar = 0xf3; |
| 2077 | } else |
| 2078 | ar = vmx_segment_access_rights(var); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2079 | |
| 2080 | /* |
| 2081 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 2082 | * qemu binaries. |
| 2083 | * IA32 arch specifies that at the time of processor reset the |
| 2084 | * "Accessed" bit in the AR field of segment registers is 1. And qemu |
| 2085 | * is setting it to 0 in the usedland code. This causes invalid guest |
| 2086 | * state vmexit when "unrestricted guest" mode is turned on. |
| 2087 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 2088 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 2089 | * kvm hack. |
| 2090 | */ |
| 2091 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
| 2092 | ar |= 0x1; /* Accessed */ |
| 2093 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2094 | vmcs_write32(sf->ar_bytes, ar); |
| 2095 | } |
| 2096 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2097 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 2098 | { |
| 2099 | u32 ar = vmcs_read32(GUEST_CS_AR_BYTES); |
| 2100 | |
| 2101 | *db = (ar >> 14) & 1; |
| 2102 | *l = (ar >> 13) & 1; |
| 2103 | } |
| 2104 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2105 | static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2106 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2107 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 2108 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2109 | } |
| 2110 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2111 | static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2112 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2113 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 2114 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2115 | } |
| 2116 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2117 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2118 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2119 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 2120 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2121 | } |
| 2122 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2123 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2124 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2125 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 2126 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2127 | } |
| 2128 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2129 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 2130 | { |
| 2131 | struct kvm_segment var; |
| 2132 | u32 ar; |
| 2133 | |
| 2134 | vmx_get_segment(vcpu, &var, seg); |
| 2135 | ar = vmx_segment_access_rights(&var); |
| 2136 | |
| 2137 | if (var.base != (var.selector << 4)) |
| 2138 | return false; |
| 2139 | if (var.limit != 0xffff) |
| 2140 | return false; |
| 2141 | if (ar != 0xf3) |
| 2142 | return false; |
| 2143 | |
| 2144 | return true; |
| 2145 | } |
| 2146 | |
| 2147 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 2148 | { |
| 2149 | struct kvm_segment cs; |
| 2150 | unsigned int cs_rpl; |
| 2151 | |
| 2152 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 2153 | cs_rpl = cs.selector & SELECTOR_RPL_MASK; |
| 2154 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2155 | if (cs.unusable) |
| 2156 | return false; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2157 | if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK)) |
| 2158 | return false; |
| 2159 | if (!cs.s) |
| 2160 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2161 | if (cs.type & AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2162 | if (cs.dpl > cs_rpl) |
| 2163 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2164 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2165 | if (cs.dpl != cs_rpl) |
| 2166 | return false; |
| 2167 | } |
| 2168 | if (!cs.present) |
| 2169 | return false; |
| 2170 | |
| 2171 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 2172 | return true; |
| 2173 | } |
| 2174 | |
| 2175 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 2176 | { |
| 2177 | struct kvm_segment ss; |
| 2178 | unsigned int ss_rpl; |
| 2179 | |
| 2180 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 2181 | ss_rpl = ss.selector & SELECTOR_RPL_MASK; |
| 2182 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2183 | if (ss.unusable) |
| 2184 | return true; |
| 2185 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2186 | return false; |
| 2187 | if (!ss.s) |
| 2188 | return false; |
| 2189 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 2190 | return false; |
| 2191 | if (!ss.present) |
| 2192 | return false; |
| 2193 | |
| 2194 | return true; |
| 2195 | } |
| 2196 | |
| 2197 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 2198 | { |
| 2199 | struct kvm_segment var; |
| 2200 | unsigned int rpl; |
| 2201 | |
| 2202 | vmx_get_segment(vcpu, &var, seg); |
| 2203 | rpl = var.selector & SELECTOR_RPL_MASK; |
| 2204 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2205 | if (var.unusable) |
| 2206 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2207 | if (!var.s) |
| 2208 | return false; |
| 2209 | if (!var.present) |
| 2210 | return false; |
| 2211 | if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) { |
| 2212 | if (var.dpl < rpl) /* DPL < RPL */ |
| 2213 | return false; |
| 2214 | } |
| 2215 | |
| 2216 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 2217 | * rights flags |
| 2218 | */ |
| 2219 | return true; |
| 2220 | } |
| 2221 | |
| 2222 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 2223 | { |
| 2224 | struct kvm_segment tr; |
| 2225 | |
| 2226 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 2227 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2228 | if (tr.unusable) |
| 2229 | return false; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2230 | if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 2231 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2232 | if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2233 | return false; |
| 2234 | if (!tr.present) |
| 2235 | return false; |
| 2236 | |
| 2237 | return true; |
| 2238 | } |
| 2239 | |
| 2240 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 2241 | { |
| 2242 | struct kvm_segment ldtr; |
| 2243 | |
| 2244 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 2245 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2246 | if (ldtr.unusable) |
| 2247 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2248 | if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 2249 | return false; |
| 2250 | if (ldtr.type != 2) |
| 2251 | return false; |
| 2252 | if (!ldtr.present) |
| 2253 | return false; |
| 2254 | |
| 2255 | return true; |
| 2256 | } |
| 2257 | |
| 2258 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 2259 | { |
| 2260 | struct kvm_segment cs, ss; |
| 2261 | |
| 2262 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 2263 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 2264 | |
| 2265 | return ((cs.selector & SELECTOR_RPL_MASK) == |
| 2266 | (ss.selector & SELECTOR_RPL_MASK)); |
| 2267 | } |
| 2268 | |
| 2269 | /* |
| 2270 | * Check if guest state is valid. Returns true if valid, false if |
| 2271 | * not. |
| 2272 | * We assume that registers are always usable |
| 2273 | */ |
| 2274 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 2275 | { |
| 2276 | /* real mode guest state checks */ |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 2277 | if (!is_protmode(vcpu)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2278 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 2279 | return false; |
| 2280 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 2281 | return false; |
| 2282 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 2283 | return false; |
| 2284 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 2285 | return false; |
| 2286 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 2287 | return false; |
| 2288 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 2289 | return false; |
| 2290 | } else { |
| 2291 | /* protected mode guest state checks */ |
| 2292 | if (!cs_ss_rpl_check(vcpu)) |
| 2293 | return false; |
| 2294 | if (!code_segment_valid(vcpu)) |
| 2295 | return false; |
| 2296 | if (!stack_segment_valid(vcpu)) |
| 2297 | return false; |
| 2298 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 2299 | return false; |
| 2300 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 2301 | return false; |
| 2302 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 2303 | return false; |
| 2304 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 2305 | return false; |
| 2306 | if (!tr_valid(vcpu)) |
| 2307 | return false; |
| 2308 | if (!ldtr_valid(vcpu)) |
| 2309 | return false; |
| 2310 | } |
| 2311 | /* TODO: |
| 2312 | * - Add checks on RIP |
| 2313 | * - Add checks on RFLAGS |
| 2314 | */ |
| 2315 | |
| 2316 | return true; |
| 2317 | } |
| 2318 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2319 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2320 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2321 | gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2322 | u16 data = 0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2323 | int ret = 0; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2324 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2325 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2326 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 2327 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2328 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2329 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 2330 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 2331 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2332 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2333 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2334 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 2335 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2336 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2337 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 2338 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2339 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2340 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2341 | r = kvm_write_guest_page(kvm, fn, &data, |
| 2342 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 2343 | sizeof(u8)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 2344 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2345 | goto out; |
| 2346 | |
| 2347 | ret = 1; |
| 2348 | out: |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2349 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2350 | } |
| 2351 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2352 | static int init_rmode_identity_map(struct kvm *kvm) |
| 2353 | { |
| 2354 | int i, r, ret; |
| 2355 | pfn_t identity_map_pfn; |
| 2356 | u32 tmp; |
| 2357 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2358 | if (!enable_ept) |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2359 | return 1; |
| 2360 | if (unlikely(!kvm->arch.ept_identity_pagetable)) { |
| 2361 | printk(KERN_ERR "EPT: identity-mapping pagetable " |
| 2362 | "haven't been allocated!\n"); |
| 2363 | return 0; |
| 2364 | } |
| 2365 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
| 2366 | return 1; |
| 2367 | ret = 0; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 2368 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2369 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 2370 | if (r < 0) |
| 2371 | goto out; |
| 2372 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 2373 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 2374 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 2375 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 2376 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 2377 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 2378 | if (r < 0) |
| 2379 | goto out; |
| 2380 | } |
| 2381 | kvm->arch.ept_identity_pagetable_done = true; |
| 2382 | ret = 1; |
| 2383 | out: |
| 2384 | return ret; |
| 2385 | } |
| 2386 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2387 | static void seg_setup(int seg) |
| 2388 | { |
| 2389 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2390 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2391 | |
| 2392 | vmcs_write16(sf->selector, 0); |
| 2393 | vmcs_writel(sf->base, 0); |
| 2394 | vmcs_write32(sf->limit, 0xffff); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2395 | if (enable_unrestricted_guest) { |
| 2396 | ar = 0x93; |
| 2397 | if (seg == VCPU_SREG_CS) |
| 2398 | ar |= 0x08; /* code segment */ |
| 2399 | } else |
| 2400 | ar = 0xf3; |
| 2401 | |
| 2402 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2403 | } |
| 2404 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2405 | static int alloc_apic_access_page(struct kvm *kvm) |
| 2406 | { |
| 2407 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 2408 | int r = 0; |
| 2409 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2410 | mutex_lock(&kvm->slots_lock); |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2411 | if (kvm->arch.apic_access_page) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2412 | goto out; |
| 2413 | kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT; |
| 2414 | kvm_userspace_mem.flags = 0; |
| 2415 | kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL; |
| 2416 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 2417 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 2418 | if (r) |
| 2419 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2420 | |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2421 | kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2422 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2423 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2424 | return r; |
| 2425 | } |
| 2426 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2427 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 2428 | { |
| 2429 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 2430 | int r = 0; |
| 2431 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2432 | mutex_lock(&kvm->slots_lock); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2433 | if (kvm->arch.ept_identity_pagetable) |
| 2434 | goto out; |
| 2435 | kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT; |
| 2436 | kvm_userspace_mem.flags = 0; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 2437 | kvm_userspace_mem.guest_phys_addr = |
| 2438 | kvm->arch.ept_identity_map_addr; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2439 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 2440 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 2441 | if (r) |
| 2442 | goto out; |
| 2443 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2444 | kvm->arch.ept_identity_pagetable = gfn_to_page(kvm, |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 2445 | kvm->arch.ept_identity_map_addr >> PAGE_SHIFT); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2446 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 2447 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2448 | return r; |
| 2449 | } |
| 2450 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2451 | static void allocate_vpid(struct vcpu_vmx *vmx) |
| 2452 | { |
| 2453 | int vpid; |
| 2454 | |
| 2455 | vmx->vpid = 0; |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 2456 | if (!enable_vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2457 | return; |
| 2458 | spin_lock(&vmx_vpid_lock); |
| 2459 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 2460 | if (vpid < VMX_NR_VPIDS) { |
| 2461 | vmx->vpid = vpid; |
| 2462 | __set_bit(vpid, vmx_vpid_bitmap); |
| 2463 | } |
| 2464 | spin_unlock(&vmx_vpid_lock); |
| 2465 | } |
| 2466 | |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 2467 | static void free_vpid(struct vcpu_vmx *vmx) |
| 2468 | { |
| 2469 | if (!enable_vpid) |
| 2470 | return; |
| 2471 | spin_lock(&vmx_vpid_lock); |
| 2472 | if (vmx->vpid != 0) |
| 2473 | __clear_bit(vmx->vpid, vmx_vpid_bitmap); |
| 2474 | spin_unlock(&vmx_vpid_lock); |
| 2475 | } |
| 2476 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 2477 | static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2478 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 2479 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2480 | |
| 2481 | if (!cpu_has_vmx_msr_bitmap()) |
| 2482 | return; |
| 2483 | |
| 2484 | /* |
| 2485 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 2486 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 2487 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 2488 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2489 | if (msr <= 0x1fff) { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 2490 | __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */ |
| 2491 | __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2492 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2493 | msr &= 0x1fff; |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 2494 | __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */ |
| 2495 | __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2496 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2497 | } |
| 2498 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 2499 | static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) |
| 2500 | { |
| 2501 | if (!longmode_only) |
| 2502 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr); |
| 2503 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr); |
| 2504 | } |
| 2505 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2506 | /* |
| 2507 | * Sets up the vmcs for emulated real mode. |
| 2508 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2509 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2510 | { |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2511 | u32 host_sysenter_cs, msr_low, msr_high; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2512 | u32 junk; |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 2513 | u64 host_pat; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2514 | unsigned long a; |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2515 | struct desc_ptr dt; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2516 | int i; |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 2517 | unsigned long kvm_vmx_return; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2518 | u32 exec_control; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2519 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2520 | /* I/O */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 2521 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
| 2522 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2523 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2524 | if (cpu_has_vmx_msr_bitmap()) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 2525 | vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2526 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2527 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 2528 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2529 | /* Control */ |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2530 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, |
| 2531 | vmcs_config.pin_based_exec_ctrl); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2532 | |
| 2533 | exec_control = vmcs_config.cpu_based_exec_ctrl; |
| 2534 | if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) { |
| 2535 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2536 | #ifdef CONFIG_X86_64 |
| 2537 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 2538 | CPU_BASED_CR8_LOAD_EXITING; |
| 2539 | #endif |
| 2540 | } |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2541 | if (!enable_ept) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2542 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
Marcelo Tosatti | 83dbc83 | 2008-10-07 17:01:27 -0300 | [diff] [blame] | 2543 | CPU_BASED_CR3_LOAD_EXITING | |
| 2544 | CPU_BASED_INVLPG_EXITING; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2545 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2546 | |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 2547 | if (cpu_has_secondary_exec_ctrls()) { |
| 2548 | exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
| 2549 | if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 2550 | exec_control &= |
| 2551 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2552 | if (vmx->vpid == 0) |
| 2553 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 046d871 | 2009-11-27 16:46:26 +0800 | [diff] [blame] | 2554 | if (!enable_ept) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2555 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | 046d871 | 2009-11-27 16:46:26 +0800 | [diff] [blame] | 2556 | enable_unrestricted_guest = 0; |
| 2557 | } |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2558 | if (!enable_unrestricted_guest) |
| 2559 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2560 | if (!ple_gap) |
| 2561 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 2562 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 2563 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2564 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2565 | if (ple_gap) { |
| 2566 | vmcs_write32(PLE_GAP, ple_gap); |
| 2567 | vmcs_write32(PLE_WINDOW, ple_window); |
| 2568 | } |
| 2569 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2570 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf); |
| 2571 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2572 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 2573 | |
Avi Kivity | 1c11e71 | 2010-05-03 16:05:44 +0300 | [diff] [blame] | 2574 | vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2575 | vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */ |
| 2576 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 2577 | |
| 2578 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
| 2579 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 2580 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2581 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 2582 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2583 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2584 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2585 | rdmsrl(MSR_FS_BASE, a); |
| 2586 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 2587 | rdmsrl(MSR_GS_BASE, a); |
| 2588 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 2589 | #else |
| 2590 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 2591 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 2592 | #endif |
| 2593 | |
| 2594 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 2595 | |
Wei Yongjun | ec68798 | 2010-03-05 12:11:48 +0800 | [diff] [blame] | 2596 | native_store_idt(&dt); |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2597 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2598 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2599 | asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return)); |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 2600 | vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */ |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2601 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 2602 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2603 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2604 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2605 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2606 | |
| 2607 | rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk); |
| 2608 | vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs); |
| 2609 | rdmsrl(MSR_IA32_SYSENTER_ESP, a); |
| 2610 | vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */ |
| 2611 | rdmsrl(MSR_IA32_SYSENTER_EIP, a); |
| 2612 | vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */ |
| 2613 | |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2614 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 2615 | rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high); |
| 2616 | host_pat = msr_low | ((u64) msr_high << 32); |
| 2617 | vmcs_write64(HOST_IA32_PAT, host_pat); |
| 2618 | } |
| 2619 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2620 | rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high); |
| 2621 | host_pat = msr_low | ((u64) msr_high << 32); |
| 2622 | /* Write the default value follow host pat */ |
| 2623 | vmcs_write64(GUEST_IA32_PAT, host_pat); |
| 2624 | /* Keep arch.pat sync with GUEST_IA32_PAT */ |
| 2625 | vmx->vcpu.arch.pat = host_pat; |
| 2626 | } |
| 2627 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2628 | for (i = 0; i < NR_VMX_MSR; ++i) { |
| 2629 | u32 index = vmx_msr_index[i]; |
| 2630 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2631 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2632 | |
| 2633 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 2634 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 2635 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 2636 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2637 | vmx->guest_msrs[j].index = i; |
| 2638 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 2639 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2640 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2641 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2642 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2643 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2644 | |
| 2645 | /* 22.2.1, 20.8.1 */ |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2646 | vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl); |
| 2647 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2648 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 2649 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
Avi Kivity | ce03e4f | 2009-12-07 12:29:14 +0200 | [diff] [blame] | 2650 | if (enable_ept) |
| 2651 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 2652 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2653 | |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2654 | kvm_write_tsc(&vmx->vcpu, 0); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2655 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2656 | return 0; |
| 2657 | } |
| 2658 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2659 | static int init_rmode(struct kvm *kvm) |
| 2660 | { |
Xiao Guangrong | 4b9d3a0 | 2010-06-08 10:15:51 +0800 | [diff] [blame] | 2661 | int idx, ret = 0; |
| 2662 | |
| 2663 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2664 | if (!init_rmode_tss(kvm)) |
Xiao Guangrong | 4b9d3a0 | 2010-06-08 10:15:51 +0800 | [diff] [blame] | 2665 | goto exit; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2666 | if (!init_rmode_identity_map(kvm)) |
Xiao Guangrong | 4b9d3a0 | 2010-06-08 10:15:51 +0800 | [diff] [blame] | 2667 | goto exit; |
| 2668 | |
| 2669 | ret = 1; |
| 2670 | exit: |
| 2671 | srcu_read_unlock(&kvm->srcu, idx); |
| 2672 | return ret; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2673 | } |
| 2674 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2675 | static int vmx_vcpu_reset(struct kvm_vcpu *vcpu) |
| 2676 | { |
| 2677 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2678 | u64 msr; |
Xiao Guangrong | 4b9d3a0 | 2010-06-08 10:15:51 +0800 | [diff] [blame] | 2679 | int ret; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2680 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2681 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2682 | if (!init_rmode(vmx->vcpu.kvm)) { |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2683 | ret = -ENOMEM; |
| 2684 | goto out; |
| 2685 | } |
| 2686 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2687 | vmx->rmode.vm86_active = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2688 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 2689 | vmx->soft_vnmi_blocked = 0; |
| 2690 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2691 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2692 | kvm_set_cr8(&vmx->vcpu, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2693 | msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 2694 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2695 | msr |= MSR_IA32_APICBASE_BSP; |
| 2696 | kvm_set_apic_base(&vmx->vcpu, msr); |
| 2697 | |
Jan Kiszka | 10ab25c | 2010-05-25 16:01:50 +0200 | [diff] [blame] | 2698 | ret = fx_init(&vmx->vcpu); |
| 2699 | if (ret != 0) |
| 2700 | goto out; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2701 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 2702 | seg_setup(VCPU_SREG_CS); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2703 | /* |
| 2704 | * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode |
| 2705 | * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh. |
| 2706 | */ |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 2707 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) { |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2708 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
| 2709 | vmcs_writel(GUEST_CS_BASE, 0x000f0000); |
| 2710 | } else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2711 | vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8); |
| 2712 | vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2713 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2714 | |
| 2715 | seg_setup(VCPU_SREG_DS); |
| 2716 | seg_setup(VCPU_SREG_ES); |
| 2717 | seg_setup(VCPU_SREG_FS); |
| 2718 | seg_setup(VCPU_SREG_GS); |
| 2719 | seg_setup(VCPU_SREG_SS); |
| 2720 | |
| 2721 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 2722 | vmcs_writel(GUEST_TR_BASE, 0); |
| 2723 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 2724 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 2725 | |
| 2726 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 2727 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 2728 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 2729 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 2730 | |
| 2731 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 2732 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 2733 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 2734 | |
| 2735 | vmcs_writel(GUEST_RFLAGS, 0x02); |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 2736 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2737 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2738 | else |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2739 | kvm_rip_write(vcpu, 0); |
| 2740 | kvm_register_write(vcpu, VCPU_REGS_RSP, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2741 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2742 | vmcs_writel(GUEST_DR7, 0x400); |
| 2743 | |
| 2744 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 2745 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 2746 | |
| 2747 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 2748 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 2749 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 2750 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2751 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
| 2752 | vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
| 2753 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2754 | /* Special registers */ |
| 2755 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 2756 | |
| 2757 | setup_msrs(vmx); |
| 2758 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2759 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 2760 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2761 | if (cpu_has_vmx_tpr_shadow()) { |
| 2762 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
| 2763 | if (vm_need_tpr_shadow(vmx->vcpu.kvm)) |
| 2764 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2765 | page_to_phys(vmx->vcpu.arch.apic->regs_page)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2766 | vmcs_write32(TPR_THRESHOLD, 0); |
| 2767 | } |
| 2768 | |
| 2769 | if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 2770 | vmcs_write64(APIC_ACCESS_ADDR, |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2771 | page_to_phys(vmx->vcpu.kvm->arch.apic_access_page)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2772 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2773 | if (vmx->vpid != 0) |
| 2774 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 2775 | |
Eduardo Habkost | fa40052 | 2009-10-24 02:49:58 -0200 | [diff] [blame] | 2776 | vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 2777 | vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2778 | vmx_set_cr4(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2779 | vmx_set_efer(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2780 | vmx_fpu_activate(&vmx->vcpu); |
| 2781 | update_exception_bitmap(&vmx->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2782 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2783 | vpid_sync_context(vmx); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2784 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2785 | ret = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2786 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2787 | /* HACK: Don't enable emulation on guest boot/reset */ |
| 2788 | vmx->emulation_required = 0; |
| 2789 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2790 | out: |
| 2791 | return ret; |
| 2792 | } |
| 2793 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 2794 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
| 2795 | { |
| 2796 | u32 cpu_based_vm_exec_control; |
| 2797 | |
| 2798 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 2799 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 2800 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 2801 | } |
| 2802 | |
| 2803 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
| 2804 | { |
| 2805 | u32 cpu_based_vm_exec_control; |
| 2806 | |
| 2807 | if (!cpu_has_virtual_nmis()) { |
| 2808 | enable_irq_window(vcpu); |
| 2809 | return; |
| 2810 | } |
| 2811 | |
Avi Kivity | 30bd0c4 | 2010-11-01 23:20:48 +0200 | [diff] [blame] | 2812 | if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
| 2813 | enable_irq_window(vcpu); |
| 2814 | return; |
| 2815 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 2816 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 2817 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING; |
| 2818 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 2819 | } |
| 2820 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2821 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2822 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 2823 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2824 | uint32_t intr; |
| 2825 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 2826 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 2827 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2828 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 2829 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2830 | if (vmx->rmode.vm86_active) { |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 2831 | if (kvm_inject_realmode_interrupt(vcpu, irq) != EMULATE_DONE) |
| 2832 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2833 | return; |
| 2834 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2835 | intr = irq | INTR_INFO_VALID_MASK; |
| 2836 | if (vcpu->arch.interrupt.soft) { |
| 2837 | intr |= INTR_TYPE_SOFT_INTR; |
| 2838 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2839 | vmx->vcpu.arch.event_exit_inst_len); |
| 2840 | } else |
| 2841 | intr |= INTR_TYPE_EXT_INTR; |
| 2842 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 2843 | vmx_clear_hlt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2844 | } |
| 2845 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2846 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 2847 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 2848 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2849 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 2850 | if (!cpu_has_virtual_nmis()) { |
| 2851 | /* |
| 2852 | * Tracking the NMI-blocked state in software is built upon |
| 2853 | * finding the next open IRQ window. This, in turn, depends on |
| 2854 | * well-behaving guests: They have to keep IRQs disabled at |
| 2855 | * least as long as the NMI handler runs. Otherwise we may |
| 2856 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 2857 | * highly unlikely, we can live with the residual risk. |
| 2858 | */ |
| 2859 | vmx->soft_vnmi_blocked = 1; |
| 2860 | vmx->vnmi_blocked_time = 0; |
| 2861 | } |
| 2862 | |
Jan Kiszka | 487b391 | 2008-09-26 09:30:56 +0200 | [diff] [blame] | 2863 | ++vcpu->stat.nmi_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2864 | if (vmx->rmode.vm86_active) { |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 2865 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR) != EMULATE_DONE) |
| 2866 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 2867 | return; |
| 2868 | } |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2869 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2870 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 2871 | vmx_clear_hlt(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2872 | } |
| 2873 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 2874 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 2875 | { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 2876 | if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked) |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 2877 | return 0; |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 2878 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 2879 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
Avi Kivity | 30bd0c4 | 2010-11-01 23:20:48 +0200 | [diff] [blame] | 2880 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 2881 | | GUEST_INTR_STATE_NMI)); |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 2882 | } |
| 2883 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2884 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 2885 | { |
| 2886 | if (!cpu_has_virtual_nmis()) |
| 2887 | return to_vmx(vcpu)->soft_vnmi_blocked; |
Avi Kivity | c332c83 | 2010-05-04 12:24:12 +0300 | [diff] [blame] | 2888 | return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 2892 | { |
| 2893 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2894 | |
| 2895 | if (!cpu_has_virtual_nmis()) { |
| 2896 | if (vmx->soft_vnmi_blocked != masked) { |
| 2897 | vmx->soft_vnmi_blocked = masked; |
| 2898 | vmx->vnmi_blocked_time = 0; |
| 2899 | } |
| 2900 | } else { |
| 2901 | if (masked) |
| 2902 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 2903 | GUEST_INTR_STATE_NMI); |
| 2904 | else |
| 2905 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 2906 | GUEST_INTR_STATE_NMI); |
| 2907 | } |
| 2908 | } |
| 2909 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 2910 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 2911 | { |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 2912 | return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
| 2913 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 2914 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 2915 | } |
| 2916 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2917 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 2918 | { |
| 2919 | int ret; |
| 2920 | struct kvm_userspace_memory_region tss_mem = { |
Sheng Yang | 6fe6397 | 2008-10-16 17:30:58 +0800 | [diff] [blame] | 2921 | .slot = TSS_PRIVATE_MEMSLOT, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2922 | .guest_phys_addr = addr, |
| 2923 | .memory_size = PAGE_SIZE * 3, |
| 2924 | .flags = 0, |
| 2925 | }; |
| 2926 | |
| 2927 | ret = kvm_set_memory_region(kvm, &tss_mem, 0); |
| 2928 | if (ret) |
| 2929 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2930 | kvm->arch.tss_addr = addr; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2931 | return 0; |
| 2932 | } |
| 2933 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2934 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 2935 | int vec, u32 err_code) |
| 2936 | { |
Nitin A Kamble | b3f3770 | 2007-05-17 15:50:34 +0300 | [diff] [blame] | 2937 | /* |
| 2938 | * Instruction with address size override prefix opcode 0x67 |
| 2939 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 2940 | */ |
| 2941 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2942 | if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2943 | return 1; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2944 | /* |
| 2945 | * Forward all other exceptions that are valid in real mode. |
| 2946 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 2947 | * the required debugging infrastructure rework. |
| 2948 | */ |
| 2949 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2950 | case DB_VECTOR: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2951 | if (vcpu->guest_debug & |
| 2952 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 2953 | return 0; |
| 2954 | kvm_queue_exception(vcpu, vec); |
| 2955 | return 1; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2956 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 2957 | /* |
| 2958 | * Update instruction length as we may reinject the exception |
| 2959 | * from user space while in guest debugging mode. |
| 2960 | */ |
| 2961 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 2962 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2963 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 2964 | return 0; |
| 2965 | /* fall through */ |
| 2966 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2967 | case OF_VECTOR: |
| 2968 | case BR_VECTOR: |
| 2969 | case UD_VECTOR: |
| 2970 | case DF_VECTOR: |
| 2971 | case SS_VECTOR: |
| 2972 | case GP_VECTOR: |
| 2973 | case MF_VECTOR: |
| 2974 | kvm_queue_exception(vcpu, vec); |
| 2975 | return 1; |
| 2976 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2977 | return 0; |
| 2978 | } |
| 2979 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 2980 | /* |
| 2981 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 2982 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 2983 | * We pass a fake environment to the machine check handler because we want |
| 2984 | * the guest to be always treated like user space, no matter what context |
| 2985 | * it used internally. |
| 2986 | */ |
| 2987 | static void kvm_machine_check(void) |
| 2988 | { |
| 2989 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 2990 | struct pt_regs regs = { |
| 2991 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 2992 | .flags = X86_EFLAGS_IF, |
| 2993 | }; |
| 2994 | |
| 2995 | do_machine_check(®s, 0); |
| 2996 | #endif |
| 2997 | } |
| 2998 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2999 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 3000 | { |
| 3001 | /* already handled by vcpu_run */ |
| 3002 | return 1; |
| 3003 | } |
| 3004 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3005 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3006 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 3007 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3008 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3009 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3010 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3011 | u32 vect_info; |
| 3012 | enum emulation_result er; |
| 3013 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 3014 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3015 | intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 3016 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 3017 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3018 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 3019 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3020 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
Avi Kivity | 65ac726 | 2009-11-04 11:59:01 +0200 | [diff] [blame] | 3021 | !is_page_fault(intr_info)) { |
| 3022 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3023 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
| 3024 | vcpu->run->internal.ndata = 2; |
| 3025 | vcpu->run->internal.data[0] = vect_info; |
| 3026 | vcpu->run->internal.data[1] = intr_info; |
| 3027 | return 0; |
| 3028 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3029 | |
Jan Kiszka | e4a4188 | 2008-09-26 09:30:46 +0200 | [diff] [blame] | 3030 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 3031 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 3032 | |
| 3033 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 3034 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 3035 | return 1; |
| 3036 | } |
| 3037 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3038 | if (is_invalid_opcode(intr_info)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3039 | er = emulate_instruction(vcpu, 0, 0, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3040 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 3041 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3042 | return 1; |
| 3043 | } |
| 3044 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3045 | error_code = 0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3046 | rip = kvm_rip_read(vcpu); |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 3047 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3048 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 3049 | if (is_page_fault(intr_info)) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3050 | /* EPT won't cause page fault directly */ |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3051 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3052 | BUG(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3053 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3054 | trace_kvm_page_fault(cr2, error_code); |
| 3055 | |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 3056 | if (kvm_event_needs_reinjection(vcpu)) |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 3057 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3058 | return kvm_mmu_page_fault(vcpu, cr2, error_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3059 | } |
| 3060 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3061 | if (vmx->rmode.vm86_active && |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3062 | handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK, |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 3063 | error_code)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3064 | if (vcpu->arch.halt_request) { |
| 3065 | vcpu->arch.halt_request = 0; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 3066 | return kvm_emulate_halt(vcpu); |
| 3067 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3068 | return 1; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 3069 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3070 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3071 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3072 | switch (ex_no) { |
| 3073 | case DB_VECTOR: |
| 3074 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 3075 | if (!(vcpu->guest_debug & |
| 3076 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
| 3077 | vcpu->arch.dr6 = dr6 | DR6_FIXED_1; |
| 3078 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 3079 | return 1; |
| 3080 | } |
| 3081 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 3082 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 3083 | /* fall through */ |
| 3084 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 3085 | /* |
| 3086 | * Update instruction length as we may reinject #BP from |
| 3087 | * user space while in guest debugging mode. Reading it for |
| 3088 | * #DB as well causes no harm, it is not used in that case. |
| 3089 | */ |
| 3090 | vmx->vcpu.arch.event_exit_inst_len = |
| 3091 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3092 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3093 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 3094 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3095 | break; |
| 3096 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3097 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 3098 | kvm_run->ex.exception = ex_no; |
| 3099 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3100 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3101 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3102 | return 0; |
| 3103 | } |
| 3104 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3105 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3106 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 3107 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3108 | return 1; |
| 3109 | } |
| 3110 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3111 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 3112 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3113 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 3114 | return 0; |
| 3115 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3116 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3117 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3118 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 3119 | unsigned long exit_qualification; |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 3120 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 3121 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3122 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 3123 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 3124 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 3125 | in = (exit_qualification & 8) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 3126 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3127 | ++vcpu->stat.io_exits; |
| 3128 | |
| 3129 | if (string || in) |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 3130 | return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3131 | |
| 3132 | port = exit_qualification >> 16; |
| 3133 | size = (exit_qualification & 7) + 1; |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 3134 | skip_emulated_instruction(vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 3135 | |
| 3136 | return kvm_fast_pio_out(vcpu, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3137 | } |
| 3138 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3139 | static void |
| 3140 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 3141 | { |
| 3142 | /* |
| 3143 | * Patch in the VMCALL instruction: |
| 3144 | */ |
| 3145 | hypercall[0] = 0x0f; |
| 3146 | hypercall[1] = 0x01; |
| 3147 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3148 | } |
| 3149 | |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 3150 | static void complete_insn_gp(struct kvm_vcpu *vcpu, int err) |
| 3151 | { |
| 3152 | if (err) |
| 3153 | kvm_inject_gp(vcpu, 0); |
| 3154 | else |
| 3155 | skip_emulated_instruction(vcpu); |
| 3156 | } |
| 3157 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3158 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3159 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3160 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3161 | int cr; |
| 3162 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 3163 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3164 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 3165 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3166 | cr = exit_qualification & 15; |
| 3167 | reg = (exit_qualification >> 8) & 15; |
| 3168 | switch ((exit_qualification >> 4) & 3) { |
| 3169 | case 0: /* mov to cr */ |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3170 | val = kvm_register_read(vcpu, reg); |
| 3171 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3172 | switch (cr) { |
| 3173 | case 0: |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 3174 | err = kvm_set_cr0(vcpu, val); |
| 3175 | complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3176 | return 1; |
| 3177 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 3178 | err = kvm_set_cr3(vcpu, val); |
| 3179 | complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3180 | return 1; |
| 3181 | case 4: |
Avi Kivity | a83b29c | 2010-06-10 17:02:15 +0300 | [diff] [blame] | 3182 | err = kvm_set_cr4(vcpu, val); |
| 3183 | complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3184 | return 1; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 3185 | case 8: { |
| 3186 | u8 cr8_prev = kvm_get_cr8(vcpu); |
| 3187 | u8 cr8 = kvm_register_read(vcpu, reg); |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame^] | 3188 | err = kvm_set_cr8(vcpu, cr8); |
| 3189 | complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 3190 | if (irqchip_in_kernel(vcpu->kvm)) |
| 3191 | return 1; |
| 3192 | if (cr8_prev <= cr8) |
| 3193 | return 1; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3194 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 3195 | return 0; |
| 3196 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3197 | }; |
| 3198 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 3199 | case 2: /* clts */ |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 3200 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 3201 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 3202 | skip_emulated_instruction(vcpu); |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 3203 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 3204 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3205 | case 1: /*mov from cr*/ |
| 3206 | switch (cr) { |
| 3207 | case 3: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3208 | kvm_register_write(vcpu, reg, vcpu->arch.cr3); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3209 | trace_kvm_cr_read(cr, vcpu->arch.cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3210 | skip_emulated_instruction(vcpu); |
| 3211 | return 1; |
| 3212 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3213 | val = kvm_get_cr8(vcpu); |
| 3214 | kvm_register_write(vcpu, reg, val); |
| 3215 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3216 | skip_emulated_instruction(vcpu); |
| 3217 | return 1; |
| 3218 | } |
| 3219 | break; |
| 3220 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 3221 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 3222 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 3223 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3224 | |
| 3225 | skip_emulated_instruction(vcpu); |
| 3226 | return 1; |
| 3227 | default: |
| 3228 | break; |
| 3229 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3230 | vcpu->run->exit_reason = 0; |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 3231 | pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3232 | (int)(exit_qualification >> 4) & 3, cr); |
| 3233 | return 0; |
| 3234 | } |
| 3235 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3236 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3237 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 3238 | unsigned long exit_qualification; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3239 | int dr, reg; |
| 3240 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 3241 | /* Do not handle if the CPL > 0, will trigger GP on re-entry */ |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 3242 | if (!kvm_require_cpl(vcpu, 0)) |
| 3243 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3244 | dr = vmcs_readl(GUEST_DR7); |
| 3245 | if (dr & DR7_GD) { |
| 3246 | /* |
| 3247 | * As the vm-exit takes precedence over the debug trap, we |
| 3248 | * need to emulate the latter, either for the host or the |
| 3249 | * guest debugging itself. |
| 3250 | */ |
| 3251 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3252 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
| 3253 | vcpu->run->debug.arch.dr7 = dr; |
| 3254 | vcpu->run->debug.arch.pc = |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3255 | vmcs_readl(GUEST_CS_BASE) + |
| 3256 | vmcs_readl(GUEST_RIP); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3257 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 3258 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3259 | return 0; |
| 3260 | } else { |
| 3261 | vcpu->arch.dr7 &= ~DR7_GD; |
| 3262 | vcpu->arch.dr6 |= DR6_BD; |
| 3263 | vmcs_writel(GUEST_DR7, vcpu->arch.dr7); |
| 3264 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 3265 | return 1; |
| 3266 | } |
| 3267 | } |
| 3268 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 3269 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3270 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 3271 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 3272 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 3273 | unsigned long val; |
| 3274 | if (!kvm_get_dr(vcpu, dr, &val)) |
| 3275 | kvm_register_write(vcpu, reg, val); |
| 3276 | } else |
| 3277 | kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3278 | skip_emulated_instruction(vcpu); |
| 3279 | return 1; |
| 3280 | } |
| 3281 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 3282 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 3283 | { |
| 3284 | vmcs_writel(GUEST_DR7, val); |
| 3285 | } |
| 3286 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3287 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3288 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 3289 | kvm_emulate_cpuid(vcpu); |
| 3290 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3291 | } |
| 3292 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3293 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3294 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3295 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3296 | u64 data; |
| 3297 | |
| 3298 | if (vmx_get_msr(vcpu, ecx, &data)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 3299 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 3300 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3301 | return 1; |
| 3302 | } |
| 3303 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3304 | trace_kvm_msr_read(ecx, data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3305 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3306 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3307 | vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u; |
| 3308 | vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3309 | skip_emulated_instruction(vcpu); |
| 3310 | return 1; |
| 3311 | } |
| 3312 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3313 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3314 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3315 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 3316 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 3317 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3318 | |
| 3319 | if (vmx_set_msr(vcpu, ecx, data) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 3320 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 3321 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3322 | return 1; |
| 3323 | } |
| 3324 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 3325 | trace_kvm_msr_write(ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3326 | skip_emulated_instruction(vcpu); |
| 3327 | return 1; |
| 3328 | } |
| 3329 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3330 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3331 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 3332 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3333 | return 1; |
| 3334 | } |
| 3335 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3336 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3337 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3338 | u32 cpu_based_vm_exec_control; |
| 3339 | |
| 3340 | /* clear pending irq */ |
| 3341 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 3342 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 3343 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3344 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 3345 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3346 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 3347 | ++vcpu->stat.irq_window_exits; |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3348 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 3349 | /* |
| 3350 | * If the user space waits to inject interrupts, exit as soon as |
| 3351 | * possible |
| 3352 | */ |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 3353 | if (!irqchip_in_kernel(vcpu->kvm) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3354 | vcpu->run->request_interrupt_window && |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 3355 | !kvm_cpu_has_interrupt(vcpu)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3356 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 3357 | return 0; |
| 3358 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3359 | return 1; |
| 3360 | } |
| 3361 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3362 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3363 | { |
| 3364 | skip_emulated_instruction(vcpu); |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 3365 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3366 | } |
| 3367 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3368 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3369 | { |
Dor Laor | 510043d | 2007-02-19 18:25:43 +0200 | [diff] [blame] | 3370 | skip_emulated_instruction(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3371 | kvm_emulate_hypercall(vcpu); |
| 3372 | return 1; |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3373 | } |
| 3374 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3375 | static int handle_vmx_insn(struct kvm_vcpu *vcpu) |
Avi Kivity | e3c7cb6 | 2009-06-16 14:19:52 +0300 | [diff] [blame] | 3376 | { |
| 3377 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 3378 | return 1; |
| 3379 | } |
| 3380 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 3381 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 3382 | { |
| 3383 | return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE; |
| 3384 | } |
| 3385 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3386 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3387 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 3388 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3389 | |
| 3390 | kvm_mmu_invlpg(vcpu, exit_qualification); |
| 3391 | skip_emulated_instruction(vcpu); |
| 3392 | return 1; |
| 3393 | } |
| 3394 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3395 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 3396 | { |
| 3397 | skip_emulated_instruction(vcpu); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 3398 | kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 3399 | return 1; |
| 3400 | } |
| 3401 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 3402 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 3403 | { |
| 3404 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 3405 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 3406 | |
| 3407 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
| 3408 | skip_emulated_instruction(vcpu); |
| 3409 | return 1; |
| 3410 | } |
| 3411 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3412 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3413 | { |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 3414 | return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3415 | } |
| 3416 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3417 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3418 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 3419 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3420 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3421 | bool has_error_code = false; |
| 3422 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3423 | u16 tss_selector; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3424 | int reason, type, idt_v; |
| 3425 | |
| 3426 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
| 3427 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3428 | |
| 3429 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 3430 | |
| 3431 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3432 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 3433 | switch (type) { |
| 3434 | case INTR_TYPE_NMI_INTR: |
| 3435 | vcpu->arch.nmi_injected = false; |
| 3436 | if (cpu_has_virtual_nmis()) |
| 3437 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 3438 | GUEST_INTR_STATE_NMI); |
| 3439 | break; |
| 3440 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3441 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3442 | kvm_clear_interrupt_queue(vcpu); |
| 3443 | break; |
| 3444 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3445 | if (vmx->idt_vectoring_info & |
| 3446 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 3447 | has_error_code = true; |
| 3448 | error_code = |
| 3449 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 3450 | } |
| 3451 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3452 | case INTR_TYPE_SOFT_EXCEPTION: |
| 3453 | kvm_clear_exception_queue(vcpu); |
| 3454 | break; |
| 3455 | default: |
| 3456 | break; |
| 3457 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 3458 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3459 | tss_selector = exit_qualification; |
| 3460 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3461 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 3462 | type != INTR_TYPE_EXT_INTR && |
| 3463 | type != INTR_TYPE_NMI_INTR)) |
| 3464 | skip_emulated_instruction(vcpu); |
| 3465 | |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 3466 | if (kvm_task_switch(vcpu, tss_selector, reason, |
| 3467 | has_error_code, error_code) == EMULATE_FAIL) { |
| 3468 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3469 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 3470 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3471 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 3472 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3473 | |
| 3474 | /* clear all local breakpoint enable flags */ |
| 3475 | vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55); |
| 3476 | |
| 3477 | /* |
| 3478 | * TODO: What about debug traps on tss switch? |
| 3479 | * Are we supposed to inject them and update dr6? |
| 3480 | */ |
| 3481 | |
| 3482 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3483 | } |
| 3484 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3485 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3486 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 3487 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3488 | gpa_t gpa; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3489 | int gla_validity; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3490 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 3491 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3492 | |
| 3493 | if (exit_qualification & (1 << 6)) { |
| 3494 | printk(KERN_ERR "EPT: GPA exceeds GAW!\n"); |
Jan Kiszka | 7f582ab | 2009-07-22 23:53:01 +0200 | [diff] [blame] | 3495 | return -EINVAL; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | gla_validity = (exit_qualification >> 7) & 0x3; |
| 3499 | if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) { |
| 3500 | printk(KERN_ERR "EPT: Handling EPT violation failed!\n"); |
| 3501 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 3502 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 3503 | vmcs_readl(GUEST_LINEAR_ADDRESS)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3504 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 3505 | (long unsigned int)exit_qualification); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3506 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 3507 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Avi Kivity | 596ae89 | 2009-06-03 14:12:10 +0300 | [diff] [blame] | 3508 | return 0; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3512 | trace_kvm_page_fault(gpa, exit_qualification); |
Marcelo Tosatti | ff1fcb9 | 2010-10-22 14:18:15 -0200 | [diff] [blame] | 3513 | return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3514 | } |
| 3515 | |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 3516 | static u64 ept_rsvd_mask(u64 spte, int level) |
| 3517 | { |
| 3518 | int i; |
| 3519 | u64 mask = 0; |
| 3520 | |
| 3521 | for (i = 51; i > boot_cpu_data.x86_phys_bits; i--) |
| 3522 | mask |= (1ULL << i); |
| 3523 | |
| 3524 | if (level > 2) |
| 3525 | /* bits 7:3 reserved */ |
| 3526 | mask |= 0xf8; |
| 3527 | else if (level == 2) { |
| 3528 | if (spte & (1ULL << 7)) |
| 3529 | /* 2MB ref, bits 20:12 reserved */ |
| 3530 | mask |= 0x1ff000; |
| 3531 | else |
| 3532 | /* bits 6:3 reserved */ |
| 3533 | mask |= 0x78; |
| 3534 | } |
| 3535 | |
| 3536 | return mask; |
| 3537 | } |
| 3538 | |
| 3539 | static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte, |
| 3540 | int level) |
| 3541 | { |
| 3542 | printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level); |
| 3543 | |
| 3544 | /* 010b (write-only) */ |
| 3545 | WARN_ON((spte & 0x7) == 0x2); |
| 3546 | |
| 3547 | /* 110b (write/execute) */ |
| 3548 | WARN_ON((spte & 0x7) == 0x6); |
| 3549 | |
| 3550 | /* 100b (execute-only) and value not supported by logical processor */ |
| 3551 | if (!cpu_has_vmx_ept_execute_only()) |
| 3552 | WARN_ON((spte & 0x7) == 0x4); |
| 3553 | |
| 3554 | /* not 000b */ |
| 3555 | if ((spte & 0x7)) { |
| 3556 | u64 rsvd_bits = spte & ept_rsvd_mask(spte, level); |
| 3557 | |
| 3558 | if (rsvd_bits != 0) { |
| 3559 | printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n", |
| 3560 | __func__, rsvd_bits); |
| 3561 | WARN_ON(1); |
| 3562 | } |
| 3563 | |
| 3564 | if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) { |
| 3565 | u64 ept_mem_type = (spte & 0x38) >> 3; |
| 3566 | |
| 3567 | if (ept_mem_type == 2 || ept_mem_type == 3 || |
| 3568 | ept_mem_type == 7) { |
| 3569 | printk(KERN_ERR "%s: ept_mem_type=0x%llx\n", |
| 3570 | __func__, ept_mem_type); |
| 3571 | WARN_ON(1); |
| 3572 | } |
| 3573 | } |
| 3574 | } |
| 3575 | } |
| 3576 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3577 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 3578 | { |
| 3579 | u64 sptes[4]; |
| 3580 | int nr_sptes, i; |
| 3581 | gpa_t gpa; |
| 3582 | |
| 3583 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 3584 | |
| 3585 | printk(KERN_ERR "EPT: Misconfiguration.\n"); |
| 3586 | printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa); |
| 3587 | |
| 3588 | nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes); |
| 3589 | |
| 3590 | for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i) |
| 3591 | ept_misconfig_inspect_spte(vcpu, sptes[i-1], i); |
| 3592 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3593 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 3594 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 3595 | |
| 3596 | return 0; |
| 3597 | } |
| 3598 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3599 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3600 | { |
| 3601 | u32 cpu_based_vm_exec_control; |
| 3602 | |
| 3603 | /* clear pending NMI */ |
| 3604 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 3605 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 3606 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 3607 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 3608 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3609 | |
| 3610 | return 1; |
| 3611 | } |
| 3612 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 3613 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 3614 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 3615 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3616 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 3617 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 3618 | u32 cpu_exec_ctrl; |
| 3619 | bool intr_window_requested; |
| 3620 | |
| 3621 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 3622 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 3623 | |
| 3624 | while (!guest_state_valid(vcpu)) { |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 3625 | if (intr_window_requested |
| 3626 | && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF)) |
| 3627 | return handle_interrupt_window(&vmx->vcpu); |
| 3628 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3629 | err = emulate_instruction(vcpu, 0, 0, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 3630 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 3631 | if (err == EMULATE_DO_MMIO) { |
| 3632 | ret = 0; |
| 3633 | goto out; |
| 3634 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 3635 | |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 3636 | if (err != EMULATE_DONE) |
| 3637 | return 0; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 3638 | |
| 3639 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 3640 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 3641 | if (need_resched()) |
| 3642 | schedule(); |
| 3643 | } |
| 3644 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 3645 | vmx->emulation_required = 0; |
| 3646 | out: |
| 3647 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 3648 | } |
| 3649 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3650 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3651 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 3652 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 3653 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 3654 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3655 | { |
| 3656 | skip_emulated_instruction(vcpu); |
| 3657 | kvm_vcpu_on_spin(vcpu); |
| 3658 | |
| 3659 | return 1; |
| 3660 | } |
| 3661 | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 3662 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 3663 | { |
| 3664 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 3665 | return 1; |
| 3666 | } |
| 3667 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3668 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3669 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 3670 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 3671 | * to be done to userspace and return 0. |
| 3672 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3673 | static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3674 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 3675 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 3676 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3677 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3678 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3679 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 3680 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 3681 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 3682 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 3683 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 3684 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 3685 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 3686 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3687 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3688 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Avi Kivity | e3c7cb6 | 2009-06-16 14:19:52 +0300 | [diff] [blame] | 3689 | [EXIT_REASON_VMCLEAR] = handle_vmx_insn, |
| 3690 | [EXIT_REASON_VMLAUNCH] = handle_vmx_insn, |
| 3691 | [EXIT_REASON_VMPTRLD] = handle_vmx_insn, |
| 3692 | [EXIT_REASON_VMPTRST] = handle_vmx_insn, |
| 3693 | [EXIT_REASON_VMREAD] = handle_vmx_insn, |
| 3694 | [EXIT_REASON_VMRESUME] = handle_vmx_insn, |
| 3695 | [EXIT_REASON_VMWRITE] = handle_vmx_insn, |
| 3696 | [EXIT_REASON_VMOFF] = handle_vmx_insn, |
| 3697 | [EXIT_REASON_VMON] = handle_vmx_insn, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3698 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 3699 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 3700 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 3701 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3702 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 3703 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 3704 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 3705 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3706 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 3707 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op, |
| 3708 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3709 | }; |
| 3710 | |
| 3711 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 3712 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3713 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 3714 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 3715 | { |
| 3716 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 3717 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 3718 | } |
| 3719 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3720 | /* |
| 3721 | * The guest has exited. See if we can fix it or if we need userspace |
| 3722 | * assistance. |
| 3723 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3724 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3725 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 3726 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 3727 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 3728 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 3729 | |
Avi Kivity | aa17911 | 2010-11-17 18:44:19 +0200 | [diff] [blame] | 3730 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3731 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 3732 | /* If guest state is invalid, start emulating */ |
| 3733 | if (vmx->emulation_required && emulate_invalid_guest_state) |
| 3734 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 3735 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3736 | /* Access CR3 don't cause VMExit in paging mode, so we need |
| 3737 | * to sync with guest real CR3. */ |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3738 | if (enable_ept && is_paging(vcpu)) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3739 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3740 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 3741 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
| 3742 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 3743 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 3744 | = exit_reason; |
| 3745 | return 0; |
| 3746 | } |
| 3747 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 3748 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3749 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 3750 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 3751 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 3752 | return 0; |
| 3753 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3754 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3755 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3756 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 3757 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
| 3758 | exit_reason != EXIT_REASON_TASK_SWITCH)) |
| 3759 | printk(KERN_WARNING "%s: unexpected, valid vectoring info " |
| 3760 | "(0x%x) and exit reason is 0x%x\n", |
| 3761 | __func__, vectoring_info, exit_reason); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3762 | |
| 3763 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) { |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 3764 | if (vmx_interrupt_allowed(vcpu)) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3765 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3766 | } else if (vmx->vnmi_blocked_time > 1000000000LL && |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 3767 | vcpu->arch.nmi_pending) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3768 | /* |
| 3769 | * This CPU don't support us in finding the end of an |
| 3770 | * NMI-blocked window if the guest runs with IRQs |
| 3771 | * disabled. So we pull the trigger after 1 s of |
| 3772 | * futile waiting, but inform the user about this. |
| 3773 | */ |
| 3774 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 3775 | "state on VCPU %d after 1 s timeout\n", |
| 3776 | __func__, vcpu->vcpu_id); |
| 3777 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3778 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3779 | } |
| 3780 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3781 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 3782 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3783 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3784 | else { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3785 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 3786 | vcpu->run->hw.hardware_exit_reason = exit_reason; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3787 | } |
| 3788 | return 0; |
| 3789 | } |
| 3790 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3791 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3792 | { |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3793 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3794 | vmcs_write32(TPR_THRESHOLD, 0); |
| 3795 | return; |
| 3796 | } |
| 3797 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3798 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3799 | } |
| 3800 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 3801 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3802 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 3803 | u32 exit_intr_info = vmx->exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 3804 | |
| 3805 | /* Handle machine checks before interrupts are enabled */ |
| 3806 | if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY) |
| 3807 | || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI |
| 3808 | && is_machine_check(exit_intr_info))) |
| 3809 | kvm_machine_check(); |
| 3810 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 3811 | /* We need to handle NMIs before interrupts are enabled */ |
| 3812 | if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR && |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 3813 | (exit_intr_info & INTR_INFO_VALID_MASK)) { |
| 3814 | kvm_before_handle_nmi(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 3815 | asm("int $2"); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 3816 | kvm_after_handle_nmi(&vmx->vcpu); |
| 3817 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 3818 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 3819 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 3820 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 3821 | { |
| 3822 | u32 exit_intr_info = vmx->exit_intr_info; |
| 3823 | bool unblock_nmi; |
| 3824 | u8 vector; |
| 3825 | bool idtv_info_valid; |
| 3826 | |
| 3827 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 3828 | |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3829 | if (cpu_has_virtual_nmis()) { |
| 3830 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 3831 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 3832 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 3833 | * SDM 3: 27.7.1.2 (September 2008) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3834 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 3835 | * a guest IRET fault. |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 3836 | * SDM 3: 23.2.2 (September 2008) |
| 3837 | * Bit 12 is undefined in any of the following cases: |
| 3838 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 3839 | * information field. |
| 3840 | * If the VM exit is due to a double fault. |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3841 | */ |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 3842 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 3843 | vector != DF_VECTOR && !idtv_info_valid) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3844 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 3845 | GUEST_INTR_STATE_NMI); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3846 | } else if (unlikely(vmx->soft_vnmi_blocked)) |
| 3847 | vmx->vnmi_blocked_time += |
| 3848 | ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 3849 | } |
| 3850 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 3851 | static void __vmx_complete_interrupts(struct vcpu_vmx *vmx, |
| 3852 | u32 idt_vectoring_info, |
| 3853 | int instr_len_field, |
| 3854 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 3855 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 3856 | u8 vector; |
| 3857 | int type; |
| 3858 | bool idtv_info_valid; |
| 3859 | |
| 3860 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 3861 | |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 3862 | vmx->vcpu.arch.nmi_injected = false; |
| 3863 | kvm_clear_exception_queue(&vmx->vcpu); |
| 3864 | kvm_clear_interrupt_queue(&vmx->vcpu); |
| 3865 | |
| 3866 | if (!idtv_info_valid) |
| 3867 | return; |
| 3868 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 3869 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 3870 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 3871 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 3872 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 3873 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3874 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 3875 | case INTR_TYPE_NMI_INTR: |
| 3876 | vmx->vcpu.arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 3877 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 3878 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 3879 | * Clear bit "block by NMI" before VM entry if a NMI |
| 3880 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 3881 | */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 3882 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 3883 | GUEST_INTR_STATE_NMI); |
| 3884 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 3885 | case INTR_TYPE_SOFT_EXCEPTION: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3886 | vmx->vcpu.arch.event_exit_inst_len = |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 3887 | vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3888 | /* fall through */ |
| 3889 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 3890 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 3891 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 3892 | kvm_queue_exception_e(&vmx->vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 3893 | } else |
| 3894 | kvm_queue_exception(&vmx->vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 3895 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3896 | case INTR_TYPE_SOFT_INTR: |
| 3897 | vmx->vcpu.arch.event_exit_inst_len = |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 3898 | vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3899 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 3900 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3901 | kvm_queue_interrupt(&vmx->vcpu, vector, |
| 3902 | type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 3903 | break; |
| 3904 | default: |
| 3905 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 3906 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3907 | } |
| 3908 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 3909 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 3910 | { |
| 3911 | __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info, |
| 3912 | VM_EXIT_INSTRUCTION_LEN, |
| 3913 | IDT_VECTORING_ERROR_CODE); |
| 3914 | } |
| 3915 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 3916 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 3917 | { |
| 3918 | __vmx_complete_interrupts(to_vmx(vcpu), |
| 3919 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 3920 | VM_ENTRY_INSTRUCTION_LEN, |
| 3921 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 3922 | |
| 3923 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 3924 | } |
| 3925 | |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3926 | #ifdef CONFIG_X86_64 |
| 3927 | #define R "r" |
| 3928 | #define Q "q" |
| 3929 | #else |
| 3930 | #define R "e" |
| 3931 | #define Q "l" |
| 3932 | #endif |
| 3933 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 3934 | static void vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3935 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3936 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 3937 | |
| 3938 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
| 3939 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) |
| 3940 | vmx->entry_time = ktime_get(); |
| 3941 | |
| 3942 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 3943 | start emulation until we arrive back to a valid state */ |
| 3944 | if (vmx->emulation_required && emulate_invalid_guest_state) |
| 3945 | return; |
| 3946 | |
| 3947 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 3948 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 3949 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 3950 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 3951 | |
| 3952 | /* When single-stepping over STI and MOV SS, we must clear the |
| 3953 | * corresponding interruptibility bits in the guest state. Otherwise |
| 3954 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 3955 | * exceptions being set, but that's not correct for the guest debugging |
| 3956 | * case. */ |
| 3957 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 3958 | vmx_set_interrupt_shadow(vcpu, 0); |
| 3959 | |
| 3960 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3961 | /* Store host registers */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3962 | "push %%"R"dx; push %%"R"bp;" |
| 3963 | "push %%"R"cx \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 3964 | "cmp %%"R"sp, %c[host_rsp](%0) \n\t" |
| 3965 | "je 1f \n\t" |
| 3966 | "mov %%"R"sp, %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 3967 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 3968 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 3969 | /* Reload cr2 if changed */ |
| 3970 | "mov %c[cr2](%0), %%"R"ax \n\t" |
| 3971 | "mov %%cr2, %%"R"dx \n\t" |
| 3972 | "cmp %%"R"ax, %%"R"dx \n\t" |
| 3973 | "je 2f \n\t" |
| 3974 | "mov %%"R"ax, %%cr2 \n\t" |
| 3975 | "2: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3976 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 3977 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3978 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3979 | "mov %c[rax](%0), %%"R"ax \n\t" |
| 3980 | "mov %c[rbx](%0), %%"R"bx \n\t" |
| 3981 | "mov %c[rdx](%0), %%"R"dx \n\t" |
| 3982 | "mov %c[rsi](%0), %%"R"si \n\t" |
| 3983 | "mov %c[rdi](%0), %%"R"di \n\t" |
| 3984 | "mov %c[rbp](%0), %%"R"bp \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3985 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 3986 | "mov %c[r8](%0), %%r8 \n\t" |
| 3987 | "mov %c[r9](%0), %%r9 \n\t" |
| 3988 | "mov %c[r10](%0), %%r10 \n\t" |
| 3989 | "mov %c[r11](%0), %%r11 \n\t" |
| 3990 | "mov %c[r12](%0), %%r12 \n\t" |
| 3991 | "mov %c[r13](%0), %%r13 \n\t" |
| 3992 | "mov %c[r14](%0), %%r14 \n\t" |
| 3993 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3994 | #endif |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3995 | "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */ |
| 3996 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3997 | /* Enter guest mode */ |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 3998 | "jne .Llaunched \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 3999 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 4000 | "jmp .Lkvm_vmx_return \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 4001 | ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 4002 | ".Lkvm_vmx_return: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4003 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 4004 | "xchg %0, (%%"R"sp) \n\t" |
| 4005 | "mov %%"R"ax, %c[rax](%0) \n\t" |
| 4006 | "mov %%"R"bx, %c[rbx](%0) \n\t" |
| 4007 | "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t" |
| 4008 | "mov %%"R"dx, %c[rdx](%0) \n\t" |
| 4009 | "mov %%"R"si, %c[rsi](%0) \n\t" |
| 4010 | "mov %%"R"di, %c[rdi](%0) \n\t" |
| 4011 | "mov %%"R"bp, %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4012 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 4013 | "mov %%r8, %c[r8](%0) \n\t" |
| 4014 | "mov %%r9, %c[r9](%0) \n\t" |
| 4015 | "mov %%r10, %c[r10](%0) \n\t" |
| 4016 | "mov %%r11, %c[r11](%0) \n\t" |
| 4017 | "mov %%r12, %c[r12](%0) \n\t" |
| 4018 | "mov %%r13, %c[r13](%0) \n\t" |
| 4019 | "mov %%r14, %c[r14](%0) \n\t" |
| 4020 | "mov %%r15, %c[r15](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4021 | #endif |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 4022 | "mov %%cr2, %%"R"ax \n\t" |
| 4023 | "mov %%"R"ax, %c[cr2](%0) \n\t" |
| 4024 | |
| 4025 | "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 4026 | "setbe %c[fail](%0) \n\t" |
| 4027 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
| 4028 | [launched]"i"(offsetof(struct vcpu_vmx, launched)), |
| 4029 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 4030 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4031 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 4032 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 4033 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 4034 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 4035 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 4036 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 4037 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4038 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4039 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 4040 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 4041 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 4042 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 4043 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 4044 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 4045 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 4046 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4047 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4048 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 4049 | : "cc", "memory" |
Jan Kiszka | 07d6f55 | 2010-09-28 16:37:42 +0200 | [diff] [blame] | 4050 | , R"ax", R"bx", R"di", R"si" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 4051 | #ifdef CONFIG_X86_64 |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 4052 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
| 4053 | #endif |
| 4054 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4055 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4056 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
| 4057 | | (1 << VCPU_EXREG_PDPTR)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4058 | vcpu->arch.regs_dirty = 0; |
| 4059 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4060 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 4061 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4062 | asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS)); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 4063 | vmx->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 4064 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 4065 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
| 4066 | vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 4067 | |
| 4068 | vmx_complete_atomic_exit(vmx); |
| 4069 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 4070 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4071 | } |
| 4072 | |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 4073 | #undef R |
| 4074 | #undef Q |
| 4075 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4076 | static void vmx_free_vmcs(struct kvm_vcpu *vcpu) |
| 4077 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4078 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4079 | |
| 4080 | if (vmx->vmcs) { |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4081 | vcpu_clear(vmx); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4082 | free_vmcs(vmx->vmcs); |
| 4083 | vmx->vmcs = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4084 | } |
| 4085 | } |
| 4086 | |
| 4087 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 4088 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4089 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4090 | |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4091 | free_vpid(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4092 | vmx_free_vmcs(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4093 | kfree(vmx->guest_msrs); |
| 4094 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 4095 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4096 | } |
| 4097 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 4098 | static inline void vmcs_init(struct vmcs *vmcs) |
| 4099 | { |
| 4100 | u64 phys_addr = __pa(per_cpu(vmxarea, raw_smp_processor_id())); |
| 4101 | |
| 4102 | if (!vmm_exclusive) |
| 4103 | kvm_cpu_vmxon(phys_addr); |
| 4104 | |
| 4105 | vmcs_clear(vmcs); |
| 4106 | |
| 4107 | if (!vmm_exclusive) |
| 4108 | kvm_cpu_vmxoff(); |
| 4109 | } |
| 4110 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4111 | static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4112 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4113 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 4114 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 4115 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4116 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4117 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4118 | return ERR_PTR(-ENOMEM); |
| 4119 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4120 | allocate_vpid(vmx); |
| 4121 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4122 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 4123 | if (err) |
| 4124 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 4125 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4126 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4127 | if (!vmx->guest_msrs) { |
| 4128 | err = -ENOMEM; |
| 4129 | goto uninit_vcpu; |
| 4130 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 4131 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4132 | vmx->vmcs = alloc_vmcs(); |
| 4133 | if (!vmx->vmcs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4134 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4135 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 4136 | vmcs_init(vmx->vmcs); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4137 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 4138 | cpu = get_cpu(); |
| 4139 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 4140 | vmx->vcpu.cpu = cpu; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4141 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4142 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 4143 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4144 | if (err) |
| 4145 | goto free_vmcs; |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 4146 | if (vm_need_virtualize_apic_accesses(kvm)) |
| 4147 | if (alloc_apic_access_page(kvm) != 0) |
| 4148 | goto free_vmcs; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 4149 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4150 | if (enable_ept) { |
| 4151 | if (!kvm->arch.ept_identity_map_addr) |
| 4152 | kvm->arch.ept_identity_map_addr = |
| 4153 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4154 | if (alloc_identity_pagetable(kvm) != 0) |
| 4155 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4156 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4157 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4158 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 4159 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4160 | free_vmcs: |
| 4161 | free_vmcs(vmx->vmcs); |
| 4162 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4163 | kfree(vmx->guest_msrs); |
| 4164 | uninit_vcpu: |
| 4165 | kvm_vcpu_uninit(&vmx->vcpu); |
| 4166 | free_vcpu: |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4167 | free_vpid(vmx); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 4168 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 4169 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4170 | } |
| 4171 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4172 | static void __init vmx_check_processor_compat(void *rtn) |
| 4173 | { |
| 4174 | struct vmcs_config vmcs_conf; |
| 4175 | |
| 4176 | *(int *)rtn = 0; |
| 4177 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 4178 | *(int *)rtn = -EIO; |
| 4179 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 4180 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 4181 | smp_processor_id()); |
| 4182 | *(int *)rtn = -EIO; |
| 4183 | } |
| 4184 | } |
| 4185 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 4186 | static int get_ept_level(void) |
| 4187 | { |
| 4188 | return VMX_EPT_DEFAULT_GAW + 1; |
| 4189 | } |
| 4190 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 4191 | static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 4192 | { |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 4193 | u64 ret; |
| 4194 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 4195 | /* For VT-d and EPT combination |
| 4196 | * 1. MMIO: always map as UC |
| 4197 | * 2. EPT with VT-d: |
| 4198 | * a. VT-d without snooping control feature: can't guarantee the |
| 4199 | * result, try to trust guest. |
| 4200 | * b. VT-d with snooping control feature: snooping control feature of |
| 4201 | * VT-d engine can guarantee the cache correctness. Just set it |
| 4202 | * to WB to keep consistent with host. So the same as item 3. |
Sheng Yang | a19a6d1 | 2010-02-09 16:41:53 +0800 | [diff] [blame] | 4203 | * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 4204 | * consistent with host MTRR |
| 4205 | */ |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 4206 | if (is_mmio) |
| 4207 | ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT; |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 4208 | else if (vcpu->kvm->arch.iommu_domain && |
| 4209 | !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)) |
| 4210 | ret = kvm_get_guest_memory_type(vcpu, gfn) << |
| 4211 | VMX_EPT_MT_EPTE_SHIFT; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 4212 | else |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 4213 | ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) |
Sheng Yang | a19a6d1 | 2010-02-09 16:41:53 +0800 | [diff] [blame] | 4214 | | VMX_EPT_IPAT_BIT; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 4215 | |
| 4216 | return ret; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 4217 | } |
| 4218 | |
Avi Kivity | f4c9e87 | 2009-12-28 16:06:35 +0200 | [diff] [blame] | 4219 | #define _ER(x) { EXIT_REASON_##x, #x } |
| 4220 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4221 | static const struct trace_print_flags vmx_exit_reasons_str[] = { |
Avi Kivity | f4c9e87 | 2009-12-28 16:06:35 +0200 | [diff] [blame] | 4222 | _ER(EXCEPTION_NMI), |
| 4223 | _ER(EXTERNAL_INTERRUPT), |
| 4224 | _ER(TRIPLE_FAULT), |
| 4225 | _ER(PENDING_INTERRUPT), |
| 4226 | _ER(NMI_WINDOW), |
| 4227 | _ER(TASK_SWITCH), |
| 4228 | _ER(CPUID), |
| 4229 | _ER(HLT), |
| 4230 | _ER(INVLPG), |
| 4231 | _ER(RDPMC), |
| 4232 | _ER(RDTSC), |
| 4233 | _ER(VMCALL), |
| 4234 | _ER(VMCLEAR), |
| 4235 | _ER(VMLAUNCH), |
| 4236 | _ER(VMPTRLD), |
| 4237 | _ER(VMPTRST), |
| 4238 | _ER(VMREAD), |
| 4239 | _ER(VMRESUME), |
| 4240 | _ER(VMWRITE), |
| 4241 | _ER(VMOFF), |
| 4242 | _ER(VMON), |
| 4243 | _ER(CR_ACCESS), |
| 4244 | _ER(DR_ACCESS), |
| 4245 | _ER(IO_INSTRUCTION), |
| 4246 | _ER(MSR_READ), |
| 4247 | _ER(MSR_WRITE), |
| 4248 | _ER(MWAIT_INSTRUCTION), |
| 4249 | _ER(MONITOR_INSTRUCTION), |
| 4250 | _ER(PAUSE_INSTRUCTION), |
| 4251 | _ER(MCE_DURING_VMENTRY), |
| 4252 | _ER(TPR_BELOW_THRESHOLD), |
| 4253 | _ER(APIC_ACCESS), |
| 4254 | _ER(EPT_VIOLATION), |
| 4255 | _ER(EPT_MISCONFIG), |
| 4256 | _ER(WBINVD), |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4257 | { -1, NULL } |
| 4258 | }; |
| 4259 | |
Avi Kivity | f4c9e87 | 2009-12-28 16:06:35 +0200 | [diff] [blame] | 4260 | #undef _ER |
| 4261 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 4262 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 4263 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 4264 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 4265 | return PT_DIRECTORY_LEVEL; |
| 4266 | else |
| 4267 | /* For shadow and EPT supported 1GB page */ |
| 4268 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 4269 | } |
| 4270 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 4271 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 4272 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4273 | struct kvm_cpuid_entry2 *best; |
| 4274 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4275 | u32 exec_control; |
| 4276 | |
| 4277 | vmx->rdtscp_enabled = false; |
| 4278 | if (vmx_rdtscp_supported()) { |
| 4279 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 4280 | if (exec_control & SECONDARY_EXEC_RDTSCP) { |
| 4281 | best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 4282 | if (best && (best->edx & bit(X86_FEATURE_RDTSCP))) |
| 4283 | vmx->rdtscp_enabled = true; |
| 4284 | else { |
| 4285 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 4286 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 4287 | exec_control); |
| 4288 | } |
| 4289 | } |
| 4290 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 4291 | } |
| 4292 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 4293 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 4294 | { |
| 4295 | } |
| 4296 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 4297 | static struct kvm_x86_ops vmx_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4298 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 4299 | .disabled_by_bios = vmx_disabled_by_bios, |
| 4300 | .hardware_setup = hardware_setup, |
| 4301 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4302 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4303 | .hardware_enable = hardware_enable, |
| 4304 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 4305 | .cpu_has_accelerated_tpr = report_flexpriority, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4306 | |
| 4307 | .vcpu_create = vmx_create_vcpu, |
| 4308 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4309 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4310 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4311 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4312 | .vcpu_load = vmx_vcpu_load, |
| 4313 | .vcpu_put = vmx_vcpu_put, |
| 4314 | |
| 4315 | .set_guest_debug = set_guest_debug, |
| 4316 | .get_msr = vmx_get_msr, |
| 4317 | .set_msr = vmx_set_msr, |
| 4318 | .get_segment_base = vmx_get_segment_base, |
| 4319 | .get_segment = vmx_get_segment, |
| 4320 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4321 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4322 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 4323 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4324 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4325 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4326 | .set_cr3 = vmx_set_cr3, |
| 4327 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4328 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4329 | .get_idt = vmx_get_idt, |
| 4330 | .set_idt = vmx_set_idt, |
| 4331 | .get_gdt = vmx_get_gdt, |
| 4332 | .set_gdt = vmx_set_gdt, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4333 | .set_dr7 = vmx_set_dr7, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4334 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4335 | .get_rflags = vmx_get_rflags, |
| 4336 | .set_rflags = vmx_set_rflags, |
Avi Kivity | ebcbab4 | 2010-02-07 11:56:52 +0200 | [diff] [blame] | 4337 | .fpu_activate = vmx_fpu_activate, |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 4338 | .fpu_deactivate = vmx_fpu_deactivate, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4339 | |
| 4340 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4341 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4342 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 4343 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4344 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 4345 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 4346 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4347 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 4348 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4349 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 4350 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 4351 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4352 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4353 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4354 | .get_nmi_mask = vmx_get_nmi_mask, |
| 4355 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4356 | .enable_nmi_window = enable_nmi_window, |
| 4357 | .enable_irq_window = enable_irq_window, |
| 4358 | .update_cr8_intercept = update_cr8_intercept, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4359 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4360 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 4361 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 4362 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4363 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 4364 | .get_exit_info = vmx_get_exit_info, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4365 | .exit_reasons_str = vmx_exit_reasons_str, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 4366 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 4367 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 4368 | |
| 4369 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4370 | |
| 4371 | .rdtscp_supported = vmx_rdtscp_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 4372 | |
| 4373 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4374 | |
| 4375 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 4376 | |
| 4377 | .write_tsc_offset = vmx_write_tsc_offset, |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 4378 | .adjust_tsc_offset = vmx_adjust_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 4379 | |
| 4380 | .set_tdp_cr3 = vmx_set_cr3, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4381 | }; |
| 4382 | |
| 4383 | static int __init vmx_init(void) |
| 4384 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4385 | int r, i; |
| 4386 | |
| 4387 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 4388 | |
| 4389 | for (i = 0; i < NR_VMX_MSR; ++i) |
| 4390 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4391 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4392 | vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4393 | if (!vmx_io_bitmap_a) |
| 4394 | return -ENOMEM; |
| 4395 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4396 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4397 | if (!vmx_io_bitmap_b) { |
| 4398 | r = -ENOMEM; |
| 4399 | goto out; |
| 4400 | } |
| 4401 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4402 | vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 4403 | if (!vmx_msr_bitmap_legacy) { |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4404 | r = -ENOMEM; |
| 4405 | goto out1; |
| 4406 | } |
| 4407 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4408 | vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 4409 | if (!vmx_msr_bitmap_longmode) { |
| 4410 | r = -ENOMEM; |
| 4411 | goto out2; |
| 4412 | } |
| 4413 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4414 | /* |
| 4415 | * Allow direct access to the PC debug port (it is often used for I/O |
| 4416 | * delays, but the vmexits simply slow things down). |
| 4417 | */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4418 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
| 4419 | clear_bit(0x80, vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4420 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4421 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4422 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4423 | memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE); |
| 4424 | memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4425 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4426 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 4427 | |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 4428 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
| 4429 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4430 | if (r) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4431 | goto out3; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4432 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4433 | vmx_disable_intercept_for_msr(MSR_FS_BASE, false); |
| 4434 | vmx_disable_intercept_for_msr(MSR_GS_BASE, false); |
| 4435 | vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true); |
| 4436 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false); |
| 4437 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false); |
| 4438 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4439 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4440 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4441 | bypass_guest_pf = 0; |
Sheng Yang | 534e38b | 2008-09-08 15:12:30 +0800 | [diff] [blame] | 4442 | kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 4443 | VMX_EPT_EXECUTABLE_MASK); |
Sheng Yang | 5fdbcb9 | 2008-07-16 09:25:40 +0800 | [diff] [blame] | 4444 | kvm_enable_tdp(); |
| 4445 | } else |
| 4446 | kvm_disable_tdp(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4447 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 4448 | if (bypass_guest_pf) |
| 4449 | kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull); |
| 4450 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4451 | return 0; |
| 4452 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4453 | out3: |
| 4454 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4455 | out2: |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4456 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4457 | out1: |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4458 | free_page((unsigned long)vmx_io_bitmap_b); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4459 | out: |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4460 | free_page((unsigned long)vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4461 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4462 | } |
| 4463 | |
| 4464 | static void __exit vmx_exit(void) |
| 4465 | { |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4466 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
| 4467 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4468 | free_page((unsigned long)vmx_io_bitmap_b); |
| 4469 | free_page((unsigned long)vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 4470 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 4471 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4472 | } |
| 4473 | |
| 4474 | module_init(vmx_init) |
| 4475 | module_exit(vmx_exit) |