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. |
| 8 | * |
| 9 | * Authors: |
| 10 | * Avi Kivity <avi@qumranet.com> |
| 11 | * Yaniv Kamay <yaniv@qumranet.com> |
| 12 | * |
| 13 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 14 | * the COPYING file in the top-level directory. |
| 15 | * |
| 16 | */ |
| 17 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 18 | #include "irq.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 19 | #include "vmx.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 | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 29 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 30 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 31 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 32 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 33 | #include <asm/desc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 34 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 35 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
| 36 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 37 | MODULE_AUTHOR("Qumranet"); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 40 | static int bypass_guest_pf = 1; |
| 41 | module_param(bypass_guest_pf, bool, 0); |
| 42 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 43 | static int enable_vpid = 1; |
| 44 | module_param(enable_vpid, bool, 0); |
| 45 | |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 46 | static int flexpriority_enabled = 1; |
| 47 | module_param(flexpriority_enabled, bool, 0); |
| 48 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 49 | static int enable_ept = 1; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 50 | module_param(enable_ept, bool, 0); |
| 51 | |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 52 | static int emulate_invalid_guest_state = 0; |
| 53 | module_param(emulate_invalid_guest_state, bool, 0); |
| 54 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 55 | struct vmcs { |
| 56 | u32 revision_id; |
| 57 | u32 abort; |
| 58 | char data[0]; |
| 59 | }; |
| 60 | |
| 61 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 62 | struct kvm_vcpu vcpu; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 63 | struct list_head local_vcpus_link; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 64 | unsigned long host_rsp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 65 | int launched; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 66 | u8 fail; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 67 | u32 idt_vectoring_info; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 68 | struct kvm_msr_entry *guest_msrs; |
| 69 | struct kvm_msr_entry *host_msrs; |
| 70 | int nmsrs; |
| 71 | int save_nmsrs; |
| 72 | int msr_offset_efer; |
| 73 | #ifdef CONFIG_X86_64 |
| 74 | int msr_offset_kernel_gs_base; |
| 75 | #endif |
| 76 | struct vmcs *vmcs; |
| 77 | struct { |
| 78 | int loaded; |
| 79 | u16 fs_sel, gs_sel, ldt_sel; |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 80 | int gs_ldt_reload_needed; |
| 81 | int fs_reload_needed; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 82 | int guest_efer_loaded; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 83 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 84 | struct { |
| 85 | struct { |
| 86 | bool pending; |
| 87 | u8 vector; |
| 88 | unsigned rip; |
| 89 | } irq; |
| 90 | } rmode; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 91 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 92 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame^] | 93 | |
| 94 | /* Support for vnmi-less CPUs */ |
| 95 | int soft_vnmi_blocked; |
| 96 | ktime_t entry_time; |
| 97 | s64 vnmi_blocked_time; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 101 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 102 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 105 | static int init_rmode(struct kvm *kvm); |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 106 | static u64 construct_eptp(unsigned long root_hpa); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 107 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 108 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 109 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 110 | static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 111 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 112 | static struct page *vmx_io_bitmap_a; |
| 113 | static struct page *vmx_io_bitmap_b; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 114 | static struct page *vmx_msr_bitmap; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 115 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 116 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 117 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 118 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 119 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 120 | int size; |
| 121 | int order; |
| 122 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 123 | u32 pin_based_exec_ctrl; |
| 124 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 125 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 126 | u32 vmexit_ctrl; |
| 127 | u32 vmentry_ctrl; |
| 128 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 129 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 130 | struct vmx_capability { |
| 131 | u32 ept; |
| 132 | u32 vpid; |
| 133 | } vmx_capability; |
| 134 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 135 | #define VMX_SEGMENT_FIELD(seg) \ |
| 136 | [VCPU_SREG_##seg] = { \ |
| 137 | .selector = GUEST_##seg##_SELECTOR, \ |
| 138 | .base = GUEST_##seg##_BASE, \ |
| 139 | .limit = GUEST_##seg##_LIMIT, \ |
| 140 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 141 | } |
| 142 | |
| 143 | static struct kvm_vmx_segment_field { |
| 144 | unsigned selector; |
| 145 | unsigned base; |
| 146 | unsigned limit; |
| 147 | unsigned ar_bytes; |
| 148 | } kvm_vmx_segment_fields[] = { |
| 149 | VMX_SEGMENT_FIELD(CS), |
| 150 | VMX_SEGMENT_FIELD(DS), |
| 151 | VMX_SEGMENT_FIELD(ES), |
| 152 | VMX_SEGMENT_FIELD(FS), |
| 153 | VMX_SEGMENT_FIELD(GS), |
| 154 | VMX_SEGMENT_FIELD(SS), |
| 155 | VMX_SEGMENT_FIELD(TR), |
| 156 | VMX_SEGMENT_FIELD(LDTR), |
| 157 | }; |
| 158 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 159 | /* |
| 160 | * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it |
| 161 | * away by decrementing the array size. |
| 162 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 163 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 164 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 165 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE, |
| 166 | #endif |
| 167 | MSR_EFER, MSR_K6_STAR, |
| 168 | }; |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 169 | #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 170 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 171 | static void load_msrs(struct kvm_msr_entry *e, int n) |
| 172 | { |
| 173 | int i; |
| 174 | |
| 175 | for (i = 0; i < n; ++i) |
| 176 | wrmsrl(e[i].index, e[i].data); |
| 177 | } |
| 178 | |
| 179 | static void save_msrs(struct kvm_msr_entry *e, int n) |
| 180 | { |
| 181 | int i; |
| 182 | |
| 183 | for (i = 0; i < n; ++i) |
| 184 | rdmsrl(e[i].index, e[i].data); |
| 185 | } |
| 186 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 187 | static inline int is_page_fault(u32 intr_info) |
| 188 | { |
| 189 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 190 | INTR_INFO_VALID_MASK)) == |
| 191 | (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK); |
| 192 | } |
| 193 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 194 | static inline int is_no_device(u32 intr_info) |
| 195 | { |
| 196 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 197 | INTR_INFO_VALID_MASK)) == |
| 198 | (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK); |
| 199 | } |
| 200 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 201 | static inline int is_invalid_opcode(u32 intr_info) |
| 202 | { |
| 203 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 204 | INTR_INFO_VALID_MASK)) == |
| 205 | (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK); |
| 206 | } |
| 207 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 208 | static inline int is_external_interrupt(u32 intr_info) |
| 209 | { |
| 210 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 211 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 212 | } |
| 213 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 214 | static inline int cpu_has_vmx_msr_bitmap(void) |
| 215 | { |
| 216 | return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS); |
| 217 | } |
| 218 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 219 | static inline int cpu_has_vmx_tpr_shadow(void) |
| 220 | { |
| 221 | return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW); |
| 222 | } |
| 223 | |
| 224 | static inline int vm_need_tpr_shadow(struct kvm *kvm) |
| 225 | { |
| 226 | return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm))); |
| 227 | } |
| 228 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 229 | static inline int cpu_has_secondary_exec_ctrls(void) |
| 230 | { |
| 231 | return (vmcs_config.cpu_based_exec_ctrl & |
| 232 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS); |
| 233 | } |
| 234 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 235 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 236 | { |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 237 | return flexpriority_enabled |
| 238 | && (vmcs_config.cpu_based_2nd_exec_ctrl & |
| 239 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 240 | } |
| 241 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 242 | static inline int cpu_has_vmx_invept_individual_addr(void) |
| 243 | { |
| 244 | return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT)); |
| 245 | } |
| 246 | |
| 247 | static inline int cpu_has_vmx_invept_context(void) |
| 248 | { |
| 249 | return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT)); |
| 250 | } |
| 251 | |
| 252 | static inline int cpu_has_vmx_invept_global(void) |
| 253 | { |
| 254 | return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT)); |
| 255 | } |
| 256 | |
| 257 | static inline int cpu_has_vmx_ept(void) |
| 258 | { |
| 259 | return (vmcs_config.cpu_based_2nd_exec_ctrl & |
| 260 | SECONDARY_EXEC_ENABLE_EPT); |
| 261 | } |
| 262 | |
| 263 | static inline int vm_need_ept(void) |
| 264 | { |
| 265 | return (cpu_has_vmx_ept() && enable_ept); |
| 266 | } |
| 267 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 268 | static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm) |
| 269 | { |
| 270 | return ((cpu_has_vmx_virtualize_apic_accesses()) && |
| 271 | (irqchip_in_kernel(kvm))); |
| 272 | } |
| 273 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 274 | static inline int cpu_has_vmx_vpid(void) |
| 275 | { |
| 276 | return (vmcs_config.cpu_based_2nd_exec_ctrl & |
| 277 | SECONDARY_EXEC_ENABLE_VPID); |
| 278 | } |
| 279 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 280 | static inline int cpu_has_virtual_nmis(void) |
| 281 | { |
| 282 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 283 | } |
| 284 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 285 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 286 | { |
| 287 | int i; |
| 288 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 289 | for (i = 0; i < vmx->nmsrs; ++i) |
| 290 | if (vmx->guest_msrs[i].index == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 291 | return i; |
| 292 | return -1; |
| 293 | } |
| 294 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 295 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 296 | { |
| 297 | struct { |
| 298 | u64 vpid : 16; |
| 299 | u64 rsvd : 48; |
| 300 | u64 gva; |
| 301 | } operand = { vpid, 0, gva }; |
| 302 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 303 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 304 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 305 | "; ja 1f ; ud2 ; 1:" |
| 306 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 307 | } |
| 308 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 309 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 310 | { |
| 311 | struct { |
| 312 | u64 eptp, gpa; |
| 313 | } operand = {eptp, gpa}; |
| 314 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 315 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 316 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 317 | "; ja 1f ; ud2 ; 1:\n" |
| 318 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 319 | } |
| 320 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 321 | static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 322 | { |
| 323 | int i; |
| 324 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 325 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 326 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 327 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 328 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 331 | static void vmcs_clear(struct vmcs *vmcs) |
| 332 | { |
| 333 | u64 phys_addr = __pa(vmcs); |
| 334 | u8 error; |
| 335 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 336 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 337 | : "=g"(error) : "a"(&phys_addr), "m"(phys_addr) |
| 338 | : "cc", "memory"); |
| 339 | if (error) |
| 340 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 341 | vmcs, phys_addr); |
| 342 | } |
| 343 | |
| 344 | static void __vcpu_clear(void *arg) |
| 345 | { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 346 | struct vcpu_vmx *vmx = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 347 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 348 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 349 | if (vmx->vcpu.cpu == cpu) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 350 | vmcs_clear(vmx->vmcs); |
| 351 | if (per_cpu(current_vmcs, cpu) == vmx->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 352 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 353 | rdtscll(vmx->vcpu.arch.host_tsc); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 354 | list_del(&vmx->local_vcpus_link); |
| 355 | vmx->vcpu.cpu = -1; |
| 356 | vmx->launched = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 359 | static void vcpu_clear(struct vcpu_vmx *vmx) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 360 | { |
Avi Kivity | eae5ecb | 2007-09-30 10:50:12 +0200 | [diff] [blame] | 361 | if (vmx->vcpu.cpu == -1) |
| 362 | return; |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 363 | smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 366 | static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx) |
| 367 | { |
| 368 | if (vmx->vpid == 0) |
| 369 | return; |
| 370 | |
| 371 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0); |
| 372 | } |
| 373 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 374 | static inline void ept_sync_global(void) |
| 375 | { |
| 376 | if (cpu_has_vmx_invept_global()) |
| 377 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 378 | } |
| 379 | |
| 380 | static inline void ept_sync_context(u64 eptp) |
| 381 | { |
| 382 | if (vm_need_ept()) { |
| 383 | if (cpu_has_vmx_invept_context()) |
| 384 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 385 | else |
| 386 | ept_sync_global(); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa) |
| 391 | { |
| 392 | if (vm_need_ept()) { |
| 393 | if (cpu_has_vmx_invept_individual_addr()) |
| 394 | __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR, |
| 395 | eptp, gpa); |
| 396 | else |
| 397 | ept_sync_context(eptp); |
| 398 | } |
| 399 | } |
| 400 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 401 | static unsigned long vmcs_readl(unsigned long field) |
| 402 | { |
| 403 | unsigned long value; |
| 404 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 405 | asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 406 | : "=a"(value) : "d"(field) : "cc"); |
| 407 | return value; |
| 408 | } |
| 409 | |
| 410 | static u16 vmcs_read16(unsigned long field) |
| 411 | { |
| 412 | return vmcs_readl(field); |
| 413 | } |
| 414 | |
| 415 | static u32 vmcs_read32(unsigned long field) |
| 416 | { |
| 417 | return vmcs_readl(field); |
| 418 | } |
| 419 | |
| 420 | static u64 vmcs_read64(unsigned long field) |
| 421 | { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 422 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 423 | return vmcs_readl(field); |
| 424 | #else |
| 425 | return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32); |
| 426 | #endif |
| 427 | } |
| 428 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 429 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 430 | { |
| 431 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 432 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 433 | dump_stack(); |
| 434 | } |
| 435 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 436 | static void vmcs_writel(unsigned long field, unsigned long value) |
| 437 | { |
| 438 | u8 error; |
| 439 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 440 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 441 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 442 | if (unlikely(error)) |
| 443 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static void vmcs_write16(unsigned long field, u16 value) |
| 447 | { |
| 448 | vmcs_writel(field, value); |
| 449 | } |
| 450 | |
| 451 | static void vmcs_write32(unsigned long field, u32 value) |
| 452 | { |
| 453 | vmcs_writel(field, value); |
| 454 | } |
| 455 | |
| 456 | static void vmcs_write64(unsigned long field, u64 value) |
| 457 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 458 | vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 459 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 460 | asm volatile (""); |
| 461 | vmcs_writel(field+1, value >> 32); |
| 462 | #endif |
| 463 | } |
| 464 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 465 | static void vmcs_clear_bits(unsigned long field, u32 mask) |
| 466 | { |
| 467 | vmcs_writel(field, vmcs_readl(field) & ~mask); |
| 468 | } |
| 469 | |
| 470 | static void vmcs_set_bits(unsigned long field, u32 mask) |
| 471 | { |
| 472 | vmcs_writel(field, vmcs_readl(field) | mask); |
| 473 | } |
| 474 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 475 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 476 | { |
| 477 | u32 eb; |
| 478 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 479 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR); |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 480 | if (!vcpu->fpu_active) |
| 481 | eb |= 1u << NM_VECTOR; |
| 482 | if (vcpu->guest_debug.enabled) |
Jan Kiszka | 19bd8af | 2008-07-13 13:40:55 +0200 | [diff] [blame] | 483 | eb |= 1u << DB_VECTOR; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 484 | if (vcpu->arch.rmode.active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 485 | eb = ~0; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 486 | if (vm_need_ept()) |
| 487 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 488 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 489 | } |
| 490 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 491 | static void reload_tss(void) |
| 492 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 493 | /* |
| 494 | * VT restores TR but not its size. Useless. |
| 495 | */ |
| 496 | struct descriptor_table gdt; |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 497 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 498 | |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 499 | kvm_get_gdt(&gdt); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 500 | descs = (void *)gdt.base; |
| 501 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 502 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 503 | } |
| 504 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 505 | static void load_transition_efer(struct vcpu_vmx *vmx) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 506 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 507 | int efer_offset = vmx->msr_offset_efer; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 508 | u64 host_efer = vmx->host_msrs[efer_offset].data; |
| 509 | u64 guest_efer = vmx->guest_msrs[efer_offset].data; |
| 510 | u64 ignore_bits; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 511 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 512 | if (efer_offset < 0) |
| 513 | return; |
| 514 | /* |
| 515 | * NX is emulated; LMA and LME handled by hardware; SCE meaninless |
| 516 | * outside long mode |
| 517 | */ |
| 518 | ignore_bits = EFER_NX | EFER_SCE; |
| 519 | #ifdef CONFIG_X86_64 |
| 520 | ignore_bits |= EFER_LMA | EFER_LME; |
| 521 | /* SCE is meaningful only in long mode on Intel */ |
| 522 | if (guest_efer & EFER_LMA) |
| 523 | ignore_bits &= ~(u64)EFER_SCE; |
| 524 | #endif |
| 525 | if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits)) |
| 526 | return; |
| 527 | |
| 528 | vmx->host_state.guest_efer_loaded = 1; |
| 529 | guest_efer &= ~ignore_bits; |
| 530 | guest_efer |= host_efer & ignore_bits; |
| 531 | wrmsrl(MSR_EFER, guest_efer); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 532 | vmx->vcpu.stat.efer_reload++; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 533 | } |
| 534 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 535 | static void reload_host_efer(struct vcpu_vmx *vmx) |
| 536 | { |
| 537 | if (vmx->host_state.guest_efer_loaded) { |
| 538 | vmx->host_state.guest_efer_loaded = 0; |
| 539 | load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1); |
| 540 | } |
| 541 | } |
| 542 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 543 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 544 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 545 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 546 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 547 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 548 | return; |
| 549 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 550 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 551 | /* |
| 552 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 553 | * allow segment selectors with cpl > 0 or ti == 1. |
| 554 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 555 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 556 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 557 | vmx->host_state.fs_sel = kvm_read_fs(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 558 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 559 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 560 | vmx->host_state.fs_reload_needed = 0; |
| 561 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 562 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 563 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 564 | } |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 565 | vmx->host_state.gs_sel = kvm_read_gs(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 566 | if (!(vmx->host_state.gs_sel & 7)) |
| 567 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 568 | else { |
| 569 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 570 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | #ifdef CONFIG_X86_64 |
| 574 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 575 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 576 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 577 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 578 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 579 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 580 | |
| 581 | #ifdef CONFIG_X86_64 |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 582 | if (is_long_mode(&vmx->vcpu)) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 583 | save_msrs(vmx->host_msrs + |
| 584 | vmx->msr_offset_kernel_gs_base, 1); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 585 | |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 586 | #endif |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 587 | load_msrs(vmx->guest_msrs, vmx->save_nmsrs); |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 588 | load_transition_efer(vmx); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 589 | } |
| 590 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 591 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 592 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 593 | unsigned long flags; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 594 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 595 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 596 | return; |
| 597 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 598 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 599 | vmx->host_state.loaded = 0; |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 600 | if (vmx->host_state.fs_reload_needed) |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 601 | kvm_load_fs(vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 602 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 603 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 604 | /* |
| 605 | * If we have to reload gs, we must take care to |
| 606 | * preserve our gs base. |
| 607 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 608 | local_irq_save(flags); |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 609 | kvm_load_gs(vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 610 | #ifdef CONFIG_X86_64 |
| 611 | wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE)); |
| 612 | #endif |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 613 | local_irq_restore(flags); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 614 | } |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 615 | reload_tss(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 616 | save_msrs(vmx->guest_msrs, vmx->save_nmsrs); |
| 617 | load_msrs(vmx->host_msrs, vmx->save_nmsrs); |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 618 | reload_host_efer(vmx); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 619 | } |
| 620 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 621 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 622 | { |
| 623 | preempt_disable(); |
| 624 | __vmx_load_host_state(vmx); |
| 625 | preempt_enable(); |
| 626 | } |
| 627 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 628 | /* |
| 629 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 630 | * vcpu mutex is already taken. |
| 631 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 632 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 633 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 634 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 635 | u64 phys_addr = __pa(vmx->vmcs); |
Avi Kivity | 019960a | 2008-03-04 10:44:51 +0200 | [diff] [blame] | 636 | u64 tsc_this, delta, new_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 637 | |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 638 | if (vcpu->cpu != cpu) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 639 | vcpu_clear(vmx); |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 640 | kvm_migrate_timers(vcpu); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 641 | vpid_sync_vcpu_all(vmx); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 642 | local_irq_disable(); |
| 643 | list_add(&vmx->local_vcpus_link, |
| 644 | &per_cpu(vcpus_on_cpu, cpu)); |
| 645 | local_irq_enable(); |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 646 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 647 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 648 | if (per_cpu(current_vmcs, cpu) != vmx->vmcs) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 649 | u8 error; |
| 650 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 651 | per_cpu(current_vmcs, cpu) = vmx->vmcs; |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 652 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 653 | : "=g"(error) : "a"(&phys_addr), "m"(phys_addr) |
| 654 | : "cc"); |
| 655 | if (error) |
| 656 | printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n", |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 657 | vmx->vmcs, phys_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | if (vcpu->cpu != cpu) { |
| 661 | struct descriptor_table dt; |
| 662 | unsigned long sysenter_esp; |
| 663 | |
| 664 | vcpu->cpu = cpu; |
| 665 | /* |
| 666 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 667 | * processors. |
| 668 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 669 | vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */ |
| 670 | kvm_get_gdt(&dt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 671 | vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */ |
| 672 | |
| 673 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 674 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Avi Kivity | 7700270 | 2007-06-13 19:55:28 +0300 | [diff] [blame] | 675 | |
| 676 | /* |
| 677 | * Make sure the time stamp counter is monotonous. |
| 678 | */ |
| 679 | rdtscll(tsc_this); |
Avi Kivity | 019960a | 2008-03-04 10:44:51 +0200 | [diff] [blame] | 680 | if (tsc_this < vcpu->arch.host_tsc) { |
| 681 | delta = vcpu->arch.host_tsc - tsc_this; |
| 682 | new_offset = vmcs_read64(TSC_OFFSET) + delta; |
| 683 | vmcs_write64(TSC_OFFSET, new_offset); |
| 684 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 685 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 689 | { |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 690 | __vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 691 | } |
| 692 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 693 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 694 | { |
| 695 | if (vcpu->fpu_active) |
| 696 | return; |
| 697 | vcpu->fpu_active = 1; |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 698 | vmcs_clear_bits(GUEST_CR0, X86_CR0_TS); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 699 | if (vcpu->arch.cr0 & X86_CR0_TS) |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 700 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 701 | update_exception_bitmap(vcpu); |
| 702 | } |
| 703 | |
| 704 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 705 | { |
| 706 | if (!vcpu->fpu_active) |
| 707 | return; |
| 708 | vcpu->fpu_active = 0; |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 709 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 710 | update_exception_bitmap(vcpu); |
| 711 | } |
| 712 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 713 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 714 | { |
| 715 | return vmcs_readl(GUEST_RFLAGS); |
| 716 | } |
| 717 | |
| 718 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 719 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 720 | if (vcpu->arch.rmode.active) |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 721 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 722 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 723 | } |
| 724 | |
| 725 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 726 | { |
| 727 | unsigned long rip; |
| 728 | u32 interruptibility; |
| 729 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 730 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 731 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 732 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 733 | |
| 734 | /* |
| 735 | * We emulated an instruction, so temporary interrupt blocking |
| 736 | * should be removed, if set. |
| 737 | */ |
| 738 | interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 739 | if (interruptibility & 3) |
| 740 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 741 | interruptibility & ~3); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 742 | vcpu->arch.interrupt_window_open = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 743 | } |
| 744 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 745 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
| 746 | bool has_error_code, u32 error_code) |
| 747 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 748 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 749 | |
| 750 | if (has_error_code) |
| 751 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
| 752 | |
| 753 | if (vcpu->arch.rmode.active) { |
| 754 | vmx->rmode.irq.pending = true; |
| 755 | vmx->rmode.irq.vector = nr; |
| 756 | vmx->rmode.irq.rip = kvm_rip_read(vcpu); |
| 757 | if (nr == BP_VECTOR) |
| 758 | vmx->rmode.irq.rip++; |
| 759 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 760 | nr | INTR_TYPE_SOFT_INTR |
| 761 | | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0) |
| 762 | | INTR_INFO_VALID_MASK); |
| 763 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1); |
| 764 | kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1); |
| 765 | return; |
| 766 | } |
| 767 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 768 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 769 | nr | INTR_TYPE_EXCEPTION |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 770 | | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 771 | | INTR_INFO_VALID_MASK); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | static bool vmx_exception_injected(struct kvm_vcpu *vcpu) |
| 775 | { |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 776 | return false; |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 777 | } |
| 778 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 779 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 780 | * Swap MSR entry in host/guest MSR entry array. |
| 781 | */ |
Gabriel C | 54e11fa | 2007-08-01 16:23:10 +0200 | [diff] [blame] | 782 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 783 | 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] | 784 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 785 | struct kvm_msr_entry tmp; |
| 786 | |
| 787 | tmp = vmx->guest_msrs[to]; |
| 788 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 789 | vmx->guest_msrs[from] = tmp; |
| 790 | tmp = vmx->host_msrs[to]; |
| 791 | vmx->host_msrs[to] = vmx->host_msrs[from]; |
| 792 | vmx->host_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 793 | } |
Gabriel C | 54e11fa | 2007-08-01 16:23:10 +0200 | [diff] [blame] | 794 | #endif |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 795 | |
| 796 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 797 | * Set up the vmcs to automatically save and restore system |
| 798 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 799 | * mode, as fiddling with msrs is very expensive. |
| 800 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 801 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 802 | { |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 803 | int save_nmsrs; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 804 | |
Avi Kivity | 33f9c50 | 2008-02-27 16:06:57 +0200 | [diff] [blame] | 805 | vmx_load_host_state(vmx); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 806 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 807 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 808 | if (is_long_mode(&vmx->vcpu)) { |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 809 | int index; |
| 810 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 811 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 812 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 813 | move_msr_up(vmx, index, save_nmsrs++); |
| 814 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 815 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 816 | move_msr_up(vmx, index, save_nmsrs++); |
| 817 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 818 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 819 | move_msr_up(vmx, index, save_nmsrs++); |
| 820 | index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 821 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 822 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 823 | /* |
| 824 | * MSR_K6_STAR is only needed on long mode guests, and only |
| 825 | * if efer.sce is enabled. |
| 826 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 827 | index = __find_msr_index(vmx, MSR_K6_STAR); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 828 | if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 829 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 830 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 831 | #endif |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 832 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 833 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 834 | #ifdef CONFIG_X86_64 |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 835 | vmx->msr_offset_kernel_gs_base = |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 836 | __find_msr_index(vmx, MSR_KERNEL_GS_BASE); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 837 | #endif |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 838 | vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 842 | * reads and returns guest's timestamp counter "register" |
| 843 | * guest_tsc = host_tsc + tsc_offset -- 21.3 |
| 844 | */ |
| 845 | static u64 guest_read_tsc(void) |
| 846 | { |
| 847 | u64 host_tsc, tsc_offset; |
| 848 | |
| 849 | rdtscll(host_tsc); |
| 850 | tsc_offset = vmcs_read64(TSC_OFFSET); |
| 851 | return host_tsc + tsc_offset; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * writes 'guest_tsc' into guest's timestamp counter "register" |
| 856 | * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc |
| 857 | */ |
| 858 | static void guest_write_tsc(u64 guest_tsc) |
| 859 | { |
| 860 | u64 host_tsc; |
| 861 | |
| 862 | rdtscll(host_tsc); |
| 863 | vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc); |
| 864 | } |
| 865 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 866 | /* |
| 867 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 868 | * Returns 0 on success, non-0 otherwise. |
| 869 | * Assumes vcpu_load() was already called. |
| 870 | */ |
| 871 | static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 872 | { |
| 873 | u64 data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 874 | struct kvm_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 875 | |
| 876 | if (!pdata) { |
| 877 | printk(KERN_ERR "BUG: get_msr called with NULL pdata\n"); |
| 878 | return -EINVAL; |
| 879 | } |
| 880 | |
| 881 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 882 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 883 | case MSR_FS_BASE: |
| 884 | data = vmcs_readl(GUEST_FS_BASE); |
| 885 | break; |
| 886 | case MSR_GS_BASE: |
| 887 | data = vmcs_readl(GUEST_GS_BASE); |
| 888 | break; |
| 889 | case MSR_EFER: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 890 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 891 | #endif |
| 892 | case MSR_IA32_TIME_STAMP_COUNTER: |
| 893 | data = guest_read_tsc(); |
| 894 | break; |
| 895 | case MSR_IA32_SYSENTER_CS: |
| 896 | data = vmcs_read32(GUEST_SYSENTER_CS); |
| 897 | break; |
| 898 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 899 | data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 900 | break; |
| 901 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 902 | data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 903 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 904 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 905 | msr = find_msr_entry(to_vmx(vcpu), msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 906 | if (msr) { |
| 907 | data = msr->data; |
| 908 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 909 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 910 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | *pdata = data; |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | /* |
| 918 | * Writes msr value into into the appropriate "register". |
| 919 | * Returns 0 on success, non-0 otherwise. |
| 920 | * Assumes vcpu_load() was already called. |
| 921 | */ |
| 922 | static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 923 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 924 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 925 | struct kvm_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 926 | int ret = 0; |
| 927 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 928 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 929 | #ifdef CONFIG_X86_64 |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 930 | case MSR_EFER: |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 931 | vmx_load_host_state(vmx); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 932 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 933 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 934 | case MSR_FS_BASE: |
| 935 | vmcs_writel(GUEST_FS_BASE, data); |
| 936 | break; |
| 937 | case MSR_GS_BASE: |
| 938 | vmcs_writel(GUEST_GS_BASE, data); |
| 939 | break; |
| 940 | #endif |
| 941 | case MSR_IA32_SYSENTER_CS: |
| 942 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 943 | break; |
| 944 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 945 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 946 | break; |
| 947 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 948 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 949 | break; |
Avi Kivity | d27d4ac | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 950 | case MSR_IA32_TIME_STAMP_COUNTER: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 951 | guest_write_tsc(data); |
| 952 | break; |
Chris Lalancette | efa67e0 | 2008-06-20 09:51:30 +0200 | [diff] [blame] | 953 | case MSR_P6_PERFCTR0: |
| 954 | case MSR_P6_PERFCTR1: |
| 955 | case MSR_P6_EVNTSEL0: |
| 956 | case MSR_P6_EVNTSEL1: |
| 957 | /* |
| 958 | * Just discard all writes to the performance counters; this |
| 959 | * should keep both older linux and windows 64-bit guests |
| 960 | * happy |
| 961 | */ |
| 962 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data); |
| 963 | |
| 964 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 965 | default: |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 966 | vmx_load_host_state(vmx); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 967 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 968 | if (msr) { |
| 969 | msr->data = data; |
| 970 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 971 | } |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 972 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 973 | } |
| 974 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 975 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 976 | } |
| 977 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 978 | 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] | 979 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 980 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 981 | switch (reg) { |
| 982 | case VCPU_REGS_RSP: |
| 983 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 984 | break; |
| 985 | case VCPU_REGS_RIP: |
| 986 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 987 | break; |
| 988 | default: |
| 989 | break; |
| 990 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg) |
| 994 | { |
| 995 | unsigned long dr7 = 0x400; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 996 | int old_singlestep; |
| 997 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 998 | old_singlestep = vcpu->guest_debug.singlestep; |
| 999 | |
| 1000 | vcpu->guest_debug.enabled = dbg->enabled; |
| 1001 | if (vcpu->guest_debug.enabled) { |
| 1002 | int i; |
| 1003 | |
| 1004 | dr7 |= 0x200; /* exact */ |
| 1005 | for (i = 0; i < 4; ++i) { |
| 1006 | if (!dbg->breakpoints[i].enabled) |
| 1007 | continue; |
| 1008 | vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address; |
| 1009 | dr7 |= 2 << (i*2); /* global enable */ |
| 1010 | dr7 |= 0 << (i*4+16); /* execution breakpoint */ |
| 1011 | } |
| 1012 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1013 | vcpu->guest_debug.singlestep = dbg->singlestep; |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1014 | } else |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1015 | vcpu->guest_debug.singlestep = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1016 | |
| 1017 | if (old_singlestep && !vcpu->guest_debug.singlestep) { |
| 1018 | unsigned long flags; |
| 1019 | |
| 1020 | flags = vmcs_readl(GUEST_RFLAGS); |
| 1021 | flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 1022 | vmcs_writel(GUEST_RFLAGS, flags); |
| 1023 | } |
| 1024 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1025 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1026 | vmcs_writel(GUEST_DR7, dr7); |
| 1027 | |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 1031 | static int vmx_get_irq(struct kvm_vcpu *vcpu) |
| 1032 | { |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 1033 | if (!vcpu->arch.interrupt.pending) |
| 1034 | return -1; |
| 1035 | return vcpu->arch.interrupt.nr; |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 1036 | } |
| 1037 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1038 | static __init int cpu_has_kvm_support(void) |
| 1039 | { |
| 1040 | unsigned long ecx = cpuid_ecx(1); |
| 1041 | return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */ |
| 1042 | } |
| 1043 | |
| 1044 | static __init int vmx_disabled_by_bios(void) |
| 1045 | { |
| 1046 | u64 msr; |
| 1047 | |
| 1048 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Sheng Yang | 9ea542f | 2008-09-11 15:27:49 +0800 | [diff] [blame] | 1049 | return (msr & (FEATURE_CONTROL_LOCKED | |
| 1050 | FEATURE_CONTROL_VMXON_ENABLED)) |
| 1051 | == FEATURE_CONTROL_LOCKED; |
Yang, Sheng | 62b3ffb | 2007-07-25 12:17:06 +0300 | [diff] [blame] | 1052 | /* locked but not enabled */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1055 | static void hardware_enable(void *garbage) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1056 | { |
| 1057 | int cpu = raw_smp_processor_id(); |
| 1058 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
| 1059 | u64 old; |
| 1060 | |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 1061 | INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1062 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Sheng Yang | 9ea542f | 2008-09-11 15:27:49 +0800 | [diff] [blame] | 1063 | if ((old & (FEATURE_CONTROL_LOCKED | |
| 1064 | FEATURE_CONTROL_VMXON_ENABLED)) |
| 1065 | != (FEATURE_CONTROL_LOCKED | |
| 1066 | FEATURE_CONTROL_VMXON_ENABLED)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1067 | /* enable and lock */ |
Yang, Sheng | 62b3ffb | 2007-07-25 12:17:06 +0300 | [diff] [blame] | 1068 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | |
Sheng Yang | 9ea542f | 2008-09-11 15:27:49 +0800 | [diff] [blame] | 1069 | FEATURE_CONTROL_LOCKED | |
| 1070 | FEATURE_CONTROL_VMXON_ENABLED); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1071 | write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */ |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1072 | asm volatile (ASM_VMX_VMXON_RAX |
| 1073 | : : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1074 | : "memory", "cc"); |
| 1075 | } |
| 1076 | |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 1077 | static void vmclear_local_vcpus(void) |
| 1078 | { |
| 1079 | int cpu = raw_smp_processor_id(); |
| 1080 | struct vcpu_vmx *vmx, *n; |
| 1081 | |
| 1082 | list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu), |
| 1083 | local_vcpus_link) |
| 1084 | __vcpu_clear(vmx); |
| 1085 | } |
| 1086 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1087 | static void hardware_disable(void *garbage) |
| 1088 | { |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 1089 | vmclear_local_vcpus(); |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1090 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Eli Collins | e693d71 | 2008-06-01 20:24:40 -0700 | [diff] [blame] | 1091 | write_cr4(read_cr4() & ~X86_CR4_VMXE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1092 | } |
| 1093 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1094 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1095 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1096 | { |
| 1097 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1098 | u32 ctl = ctl_min | ctl_opt; |
| 1099 | |
| 1100 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 1101 | |
| 1102 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 1103 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 1104 | |
| 1105 | /* Ensure minimum (required) set of control bits are supported. */ |
| 1106 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1107 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1108 | |
| 1109 | *result = ctl; |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1113 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1114 | { |
| 1115 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1116 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1117 | u32 _pin_based_exec_control = 0; |
| 1118 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1119 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1120 | u32 _vmexit_control = 0; |
| 1121 | u32 _vmentry_control = 0; |
| 1122 | |
| 1123 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 1124 | opt = PIN_BASED_VIRTUAL_NMIS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1125 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 1126 | &_pin_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1127 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1128 | |
| 1129 | min = CPU_BASED_HLT_EXITING | |
| 1130 | #ifdef CONFIG_X86_64 |
| 1131 | CPU_BASED_CR8_LOAD_EXITING | |
| 1132 | CPU_BASED_CR8_STORE_EXITING | |
| 1133 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1134 | CPU_BASED_CR3_LOAD_EXITING | |
| 1135 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1136 | CPU_BASED_USE_IO_BITMAPS | |
| 1137 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 1138 | CPU_BASED_USE_TSC_OFFSETING | |
| 1139 | CPU_BASED_INVLPG_EXITING; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1140 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1141 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1142 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1143 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 1144 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1145 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1146 | #ifdef CONFIG_X86_64 |
| 1147 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 1148 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 1149 | ~CPU_BASED_CR8_STORE_EXITING; |
| 1150 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1151 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1152 | min2 = 0; |
| 1153 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1154 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1155 | SECONDARY_EXEC_ENABLE_VPID | |
| 1156 | SECONDARY_EXEC_ENABLE_EPT; |
| 1157 | if (adjust_vmx_controls(min2, opt2, |
| 1158 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1159 | &_cpu_based_2nd_exec_control) < 0) |
| 1160 | return -EIO; |
| 1161 | } |
| 1162 | #ifndef CONFIG_X86_64 |
| 1163 | if (!(_cpu_based_2nd_exec_control & |
| 1164 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 1165 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 1166 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1167 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 1168 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 1169 | enabled */ |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1170 | min &= ~(CPU_BASED_CR3_LOAD_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 1171 | CPU_BASED_CR3_STORE_EXITING | |
| 1172 | CPU_BASED_INVLPG_EXITING); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1173 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 1174 | &_cpu_based_exec_control) < 0) |
| 1175 | return -EIO; |
| 1176 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 1177 | vmx_capability.ept, vmx_capability.vpid); |
| 1178 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1179 | |
| 1180 | min = 0; |
| 1181 | #ifdef CONFIG_X86_64 |
| 1182 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 1183 | #endif |
| 1184 | opt = 0; |
| 1185 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 1186 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1187 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1188 | |
| 1189 | min = opt = 0; |
| 1190 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 1191 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1192 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1193 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 1194 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1195 | |
| 1196 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 1197 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1198 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1199 | |
| 1200 | #ifdef CONFIG_X86_64 |
| 1201 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 1202 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1203 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1204 | #endif |
| 1205 | |
| 1206 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 1207 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1208 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1209 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1210 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
| 1211 | vmcs_conf->order = get_order(vmcs_config.size); |
| 1212 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1213 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1214 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 1215 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1216 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1217 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 1218 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1219 | |
| 1220 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 1221 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1222 | |
| 1223 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 1224 | { |
| 1225 | int node = cpu_to_node(cpu); |
| 1226 | struct page *pages; |
| 1227 | struct vmcs *vmcs; |
| 1228 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1229 | pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1230 | if (!pages) |
| 1231 | return NULL; |
| 1232 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1233 | memset(vmcs, 0, vmcs_config.size); |
| 1234 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1235 | return vmcs; |
| 1236 | } |
| 1237 | |
| 1238 | static struct vmcs *alloc_vmcs(void) |
| 1239 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1240 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | static void free_vmcs(struct vmcs *vmcs) |
| 1244 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1245 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1246 | } |
| 1247 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 1248 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1249 | { |
| 1250 | int cpu; |
| 1251 | |
| 1252 | for_each_online_cpu(cpu) |
| 1253 | free_vmcs(per_cpu(vmxarea, cpu)); |
| 1254 | } |
| 1255 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1256 | static __init int alloc_kvm_area(void) |
| 1257 | { |
| 1258 | int cpu; |
| 1259 | |
| 1260 | for_each_online_cpu(cpu) { |
| 1261 | struct vmcs *vmcs; |
| 1262 | |
| 1263 | vmcs = alloc_vmcs_cpu(cpu); |
| 1264 | if (!vmcs) { |
| 1265 | free_kvm_area(); |
| 1266 | return -ENOMEM; |
| 1267 | } |
| 1268 | |
| 1269 | per_cpu(vmxarea, cpu) = vmcs; |
| 1270 | } |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | static __init int hardware_setup(void) |
| 1275 | { |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1276 | if (setup_vmcs_config(&vmcs_config) < 0) |
| 1277 | return -EIO; |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 1278 | |
| 1279 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 1280 | kvm_enable_efer_bits(EFER_NX); |
| 1281 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1282 | return alloc_kvm_area(); |
| 1283 | } |
| 1284 | |
| 1285 | static __exit void hardware_unsetup(void) |
| 1286 | { |
| 1287 | free_kvm_area(); |
| 1288 | } |
| 1289 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1290 | static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save) |
| 1291 | { |
| 1292 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1293 | |
Avi Kivity | 6af11b9 | 2007-03-19 13:18:10 +0200 | [diff] [blame] | 1294 | if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1295 | vmcs_write16(sf->selector, save->selector); |
| 1296 | vmcs_writel(sf->base, save->base); |
| 1297 | vmcs_write32(sf->limit, save->limit); |
| 1298 | vmcs_write32(sf->ar_bytes, save->ar); |
| 1299 | } else { |
| 1300 | u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK) |
| 1301 | << AR_DPL_SHIFT; |
| 1302 | vmcs_write32(sf->ar_bytes, 0x93 | dpl); |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 1307 | { |
| 1308 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1309 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1310 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1311 | vmx->emulation_required = 1; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1312 | vcpu->arch.rmode.active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1313 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1314 | vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base); |
| 1315 | vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit); |
| 1316 | vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1317 | |
| 1318 | flags = vmcs_readl(GUEST_RFLAGS); |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1319 | flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1320 | flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1321 | vmcs_writel(GUEST_RFLAGS, flags); |
| 1322 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1323 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 1324 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1325 | |
| 1326 | update_exception_bitmap(vcpu); |
| 1327 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1328 | if (emulate_invalid_guest_state) |
| 1329 | return; |
| 1330 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1331 | fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es); |
| 1332 | fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds); |
| 1333 | fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs); |
| 1334 | fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1335 | |
| 1336 | vmcs_write16(GUEST_SS_SELECTOR, 0); |
| 1337 | vmcs_write32(GUEST_SS_AR_BYTES, 0x93); |
| 1338 | |
| 1339 | vmcs_write16(GUEST_CS_SELECTOR, |
| 1340 | vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK); |
| 1341 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); |
| 1342 | } |
| 1343 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1344 | static gva_t rmode_tss_base(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1345 | { |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1346 | if (!kvm->arch.tss_addr) { |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 1347 | gfn_t base_gfn = kvm->memslots[0].base_gfn + |
| 1348 | kvm->memslots[0].npages - 3; |
| 1349 | return base_gfn << PAGE_SHIFT; |
| 1350 | } |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1351 | return kvm->arch.tss_addr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | static void fix_rmode_seg(int seg, struct kvm_save_segment *save) |
| 1355 | { |
| 1356 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1357 | |
| 1358 | save->selector = vmcs_read16(sf->selector); |
| 1359 | save->base = vmcs_readl(sf->base); |
| 1360 | save->limit = vmcs_read32(sf->limit); |
| 1361 | save->ar = vmcs_read32(sf->ar_bytes); |
Jan Kiszka | 15b00f3 | 2007-11-19 10:21:45 +0100 | [diff] [blame] | 1362 | vmcs_write16(sf->selector, save->base >> 4); |
| 1363 | vmcs_write32(sf->base, save->base & 0xfffff); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1364 | vmcs_write32(sf->limit, 0xffff); |
| 1365 | vmcs_write32(sf->ar_bytes, 0xf3); |
| 1366 | } |
| 1367 | |
| 1368 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 1369 | { |
| 1370 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1371 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1372 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1373 | vmx->emulation_required = 1; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1374 | vcpu->arch.rmode.active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1375 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1376 | vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1377 | vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm)); |
| 1378 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1379 | vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1380 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
| 1381 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1382 | vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1383 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 1384 | |
| 1385 | flags = vmcs_readl(GUEST_RFLAGS); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1386 | vcpu->arch.rmode.save_iopl |
| 1387 | = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1388 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1389 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1390 | |
| 1391 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1392 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1393 | update_exception_bitmap(vcpu); |
| 1394 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1395 | if (emulate_invalid_guest_state) |
| 1396 | goto continue_rmode; |
| 1397 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1398 | vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4); |
| 1399 | vmcs_write32(GUEST_SS_LIMIT, 0xffff); |
| 1400 | vmcs_write32(GUEST_SS_AR_BYTES, 0xf3); |
| 1401 | |
| 1402 | vmcs_write32(GUEST_CS_AR_BYTES, 0xf3); |
Michael Riepe | abacf8d | 2006-12-22 01:05:45 -0800 | [diff] [blame] | 1403 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); |
Avi Kivity | 8cb5b03 | 2007-03-20 18:40:40 +0200 | [diff] [blame] | 1404 | if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000) |
| 1405 | vmcs_writel(GUEST_CS_BASE, 0xf0000); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1406 | vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4); |
| 1407 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1408 | fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es); |
| 1409 | fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds); |
| 1410 | fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs); |
| 1411 | fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 1412 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 1413 | continue_rmode: |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 1414 | kvm_mmu_reset_context(vcpu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 1415 | init_rmode(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1416 | } |
| 1417 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1418 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1419 | |
| 1420 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 1421 | { |
| 1422 | u32 guest_tr_ar; |
| 1423 | |
| 1424 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
| 1425 | if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) { |
| 1426 | printk(KERN_DEBUG "%s: tss fixup for long mode. \n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1427 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1428 | vmcs_write32(GUEST_TR_AR_BYTES, |
| 1429 | (guest_tr_ar & ~AR_TYPE_MASK) |
| 1430 | | AR_TYPE_BUSY_64_TSS); |
| 1431 | } |
| 1432 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1433 | vcpu->arch.shadow_efer |= EFER_LMA; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1434 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1435 | find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1436 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1437 | vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1438 | | VM_ENTRY_IA32E_MODE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 1442 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1443 | vcpu->arch.shadow_efer &= ~EFER_LMA; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1444 | |
| 1445 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1446 | vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1447 | & ~VM_ENTRY_IA32E_MODE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | #endif |
| 1451 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1452 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 1453 | { |
| 1454 | vpid_sync_vcpu_all(to_vmx(vcpu)); |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 1455 | if (vm_need_ept()) |
| 1456 | ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa)); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1457 | } |
| 1458 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 1459 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1460 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1461 | vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK; |
| 1462 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1463 | } |
| 1464 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1465 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 1466 | { |
| 1467 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
| 1468 | if (!load_pdptrs(vcpu, vcpu->arch.cr3)) { |
| 1469 | printk(KERN_ERR "EPT: Fail to load pdptrs!\n"); |
| 1470 | return; |
| 1471 | } |
| 1472 | vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]); |
| 1473 | vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]); |
| 1474 | vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]); |
| 1475 | vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]); |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
| 1480 | |
| 1481 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 1482 | unsigned long cr0, |
| 1483 | struct kvm_vcpu *vcpu) |
| 1484 | { |
| 1485 | if (!(cr0 & X86_CR0_PG)) { |
| 1486 | /* From paging/starting to nonpaging */ |
| 1487 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 1488 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1489 | (CPU_BASED_CR3_LOAD_EXITING | |
| 1490 | CPU_BASED_CR3_STORE_EXITING)); |
| 1491 | vcpu->arch.cr0 = cr0; |
| 1492 | vmx_set_cr4(vcpu, vcpu->arch.cr4); |
| 1493 | *hw_cr0 |= X86_CR0_PE | X86_CR0_PG; |
| 1494 | *hw_cr0 &= ~X86_CR0_WP; |
| 1495 | } else if (!is_paging(vcpu)) { |
| 1496 | /* From nonpaging to paging */ |
| 1497 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 1498 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1499 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 1500 | CPU_BASED_CR3_STORE_EXITING)); |
| 1501 | vcpu->arch.cr0 = cr0; |
| 1502 | vmx_set_cr4(vcpu, vcpu->arch.cr4); |
| 1503 | if (!(vcpu->arch.cr0 & X86_CR0_WP)) |
| 1504 | *hw_cr0 &= ~X86_CR0_WP; |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | static void ept_update_paging_mode_cr4(unsigned long *hw_cr4, |
| 1509 | struct kvm_vcpu *vcpu) |
| 1510 | { |
| 1511 | if (!is_paging(vcpu)) { |
| 1512 | *hw_cr4 &= ~X86_CR4_PAE; |
| 1513 | *hw_cr4 |= X86_CR4_PSE; |
| 1514 | } else if (!(vcpu->arch.cr4 & X86_CR4_PAE)) |
| 1515 | *hw_cr4 &= ~X86_CR4_PAE; |
| 1516 | } |
| 1517 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1518 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 1519 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1520 | unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | |
| 1521 | KVM_VM_CR0_ALWAYS_ON; |
| 1522 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1523 | vmx_fpu_deactivate(vcpu); |
| 1524 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1525 | if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1526 | enter_pmode(vcpu); |
| 1527 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1528 | if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1529 | enter_rmode(vcpu); |
| 1530 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1531 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1532 | if (vcpu->arch.shadow_efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1533 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1534 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1535 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1536 | exit_lmode(vcpu); |
| 1537 | } |
| 1538 | #endif |
| 1539 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1540 | if (vm_need_ept()) |
| 1541 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 1542 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1543 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1544 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1545 | vcpu->arch.cr0 = cr0; |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1546 | |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1547 | if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE)) |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1548 | vmx_fpu_activate(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1549 | } |
| 1550 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1551 | static u64 construct_eptp(unsigned long root_hpa) |
| 1552 | { |
| 1553 | u64 eptp; |
| 1554 | |
| 1555 | /* TODO write the value reading from MSR */ |
| 1556 | eptp = VMX_EPT_DEFAULT_MT | |
| 1557 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
| 1558 | eptp |= (root_hpa & PAGE_MASK); |
| 1559 | |
| 1560 | return eptp; |
| 1561 | } |
| 1562 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1563 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 1564 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1565 | unsigned long guest_cr3; |
| 1566 | u64 eptp; |
| 1567 | |
| 1568 | guest_cr3 = cr3; |
| 1569 | if (vm_need_ept()) { |
| 1570 | eptp = construct_eptp(cr3); |
| 1571 | vmcs_write64(EPT_POINTER, eptp); |
| 1572 | ept_sync_context(eptp); |
| 1573 | ept_load_pdptrs(vcpu); |
| 1574 | guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 : |
| 1575 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 1576 | } |
| 1577 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1578 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1579 | vmcs_writel(GUEST_CR3, guest_cr3); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1580 | if (vcpu->arch.cr0 & X86_CR0_PE) |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1581 | vmx_fpu_deactivate(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 1585 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1586 | unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ? |
| 1587 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
| 1588 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1589 | vcpu->arch.cr4 = cr4; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1590 | if (vm_need_ept()) |
| 1591 | ept_update_paging_mode_cr4(&hw_cr4, vcpu); |
| 1592 | |
| 1593 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 1594 | vmcs_writel(GUEST_CR4, hw_cr4); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1595 | } |
| 1596 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1597 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 1598 | { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1599 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1600 | struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1601 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1602 | vcpu->arch.shadow_efer = efer; |
Joerg Roedel | 9f62e19 | 2008-01-31 14:57:39 +0100 | [diff] [blame] | 1603 | if (!msr) |
| 1604 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1605 | if (efer & EFER_LMA) { |
| 1606 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1607 | vmcs_read32(VM_ENTRY_CONTROLS) | |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1608 | VM_ENTRY_IA32E_MODE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1609 | msr->data = efer; |
| 1610 | |
| 1611 | } else { |
| 1612 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1613 | vmcs_read32(VM_ENTRY_CONTROLS) & |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1614 | ~VM_ENTRY_IA32E_MODE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1615 | |
| 1616 | msr->data = efer & ~EFER_LME; |
| 1617 | } |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1618 | setup_msrs(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1619 | } |
| 1620 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1621 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 1622 | { |
| 1623 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1624 | |
| 1625 | return vmcs_readl(sf->base); |
| 1626 | } |
| 1627 | |
| 1628 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 1629 | struct kvm_segment *var, int seg) |
| 1630 | { |
| 1631 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1632 | u32 ar; |
| 1633 | |
| 1634 | var->base = vmcs_readl(sf->base); |
| 1635 | var->limit = vmcs_read32(sf->limit); |
| 1636 | var->selector = vmcs_read16(sf->selector); |
| 1637 | ar = vmcs_read32(sf->ar_bytes); |
| 1638 | if (ar & AR_UNUSABLE_MASK) |
| 1639 | ar = 0; |
| 1640 | var->type = ar & 15; |
| 1641 | var->s = (ar >> 4) & 1; |
| 1642 | var->dpl = (ar >> 5) & 3; |
| 1643 | var->present = (ar >> 7) & 1; |
| 1644 | var->avl = (ar >> 12) & 1; |
| 1645 | var->l = (ar >> 13) & 1; |
| 1646 | var->db = (ar >> 14) & 1; |
| 1647 | var->g = (ar >> 15) & 1; |
| 1648 | var->unusable = (ar >> 16) & 1; |
| 1649 | } |
| 1650 | |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 1651 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
| 1652 | { |
| 1653 | struct kvm_segment kvm_seg; |
| 1654 | |
| 1655 | if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */ |
| 1656 | return 0; |
| 1657 | |
| 1658 | if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */ |
| 1659 | return 3; |
| 1660 | |
| 1661 | vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS); |
| 1662 | return kvm_seg.selector & 3; |
| 1663 | } |
| 1664 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1665 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1666 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1667 | u32 ar; |
| 1668 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1669 | if (var->unusable) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1670 | ar = 1 << 16; |
| 1671 | else { |
| 1672 | ar = var->type & 15; |
| 1673 | ar |= (var->s & 1) << 4; |
| 1674 | ar |= (var->dpl & 3) << 5; |
| 1675 | ar |= (var->present & 1) << 7; |
| 1676 | ar |= (var->avl & 1) << 12; |
| 1677 | ar |= (var->l & 1) << 13; |
| 1678 | ar |= (var->db & 1) << 14; |
| 1679 | ar |= (var->g & 1) << 15; |
| 1680 | } |
Uri Lublin | f7fbf1f | 2006-12-13 00:34:00 -0800 | [diff] [blame] | 1681 | if (ar == 0) /* a 0 value means unusable */ |
| 1682 | ar = AR_UNUSABLE_MASK; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1683 | |
| 1684 | return ar; |
| 1685 | } |
| 1686 | |
| 1687 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 1688 | struct kvm_segment *var, int seg) |
| 1689 | { |
| 1690 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1691 | u32 ar; |
| 1692 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1693 | if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) { |
| 1694 | vcpu->arch.rmode.tr.selector = var->selector; |
| 1695 | vcpu->arch.rmode.tr.base = var->base; |
| 1696 | vcpu->arch.rmode.tr.limit = var->limit; |
| 1697 | vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var); |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1698 | return; |
| 1699 | } |
| 1700 | vmcs_writel(sf->base, var->base); |
| 1701 | vmcs_write32(sf->limit, var->limit); |
| 1702 | vmcs_write16(sf->selector, var->selector); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1703 | if (vcpu->arch.rmode.active && var->s) { |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1704 | /* |
| 1705 | * Hack real-mode segments into vm86 compatibility. |
| 1706 | */ |
| 1707 | if (var->base == 0xffff0000 && var->selector == 0xf000) |
| 1708 | vmcs_writel(sf->base, 0xf0000); |
| 1709 | ar = 0xf3; |
| 1710 | } else |
| 1711 | ar = vmx_segment_access_rights(var); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1712 | vmcs_write32(sf->ar_bytes, ar); |
| 1713 | } |
| 1714 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1715 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 1716 | { |
| 1717 | u32 ar = vmcs_read32(GUEST_CS_AR_BYTES); |
| 1718 | |
| 1719 | *db = (ar >> 14) & 1; |
| 1720 | *l = (ar >> 13) & 1; |
| 1721 | } |
| 1722 | |
| 1723 | static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1724 | { |
| 1725 | dt->limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 1726 | dt->base = vmcs_readl(GUEST_IDTR_BASE); |
| 1727 | } |
| 1728 | |
| 1729 | static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1730 | { |
| 1731 | vmcs_write32(GUEST_IDTR_LIMIT, dt->limit); |
| 1732 | vmcs_writel(GUEST_IDTR_BASE, dt->base); |
| 1733 | } |
| 1734 | |
| 1735 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1736 | { |
| 1737 | dt->limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 1738 | dt->base = vmcs_readl(GUEST_GDTR_BASE); |
| 1739 | } |
| 1740 | |
| 1741 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1742 | { |
| 1743 | vmcs_write32(GUEST_GDTR_LIMIT, dt->limit); |
| 1744 | vmcs_writel(GUEST_GDTR_BASE, dt->base); |
| 1745 | } |
| 1746 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 1747 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 1748 | { |
| 1749 | struct kvm_segment var; |
| 1750 | u32 ar; |
| 1751 | |
| 1752 | vmx_get_segment(vcpu, &var, seg); |
| 1753 | ar = vmx_segment_access_rights(&var); |
| 1754 | |
| 1755 | if (var.base != (var.selector << 4)) |
| 1756 | return false; |
| 1757 | if (var.limit != 0xffff) |
| 1758 | return false; |
| 1759 | if (ar != 0xf3) |
| 1760 | return false; |
| 1761 | |
| 1762 | return true; |
| 1763 | } |
| 1764 | |
| 1765 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 1766 | { |
| 1767 | struct kvm_segment cs; |
| 1768 | unsigned int cs_rpl; |
| 1769 | |
| 1770 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 1771 | cs_rpl = cs.selector & SELECTOR_RPL_MASK; |
| 1772 | |
| 1773 | if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK)) |
| 1774 | return false; |
| 1775 | if (!cs.s) |
| 1776 | return false; |
| 1777 | if (!(~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK))) { |
| 1778 | if (cs.dpl > cs_rpl) |
| 1779 | return false; |
| 1780 | } else if (cs.type & AR_TYPE_CODE_MASK) { |
| 1781 | if (cs.dpl != cs_rpl) |
| 1782 | return false; |
| 1783 | } |
| 1784 | if (!cs.present) |
| 1785 | return false; |
| 1786 | |
| 1787 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 1788 | return true; |
| 1789 | } |
| 1790 | |
| 1791 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 1792 | { |
| 1793 | struct kvm_segment ss; |
| 1794 | unsigned int ss_rpl; |
| 1795 | |
| 1796 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 1797 | ss_rpl = ss.selector & SELECTOR_RPL_MASK; |
| 1798 | |
| 1799 | if ((ss.type != 3) || (ss.type != 7)) |
| 1800 | return false; |
| 1801 | if (!ss.s) |
| 1802 | return false; |
| 1803 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 1804 | return false; |
| 1805 | if (!ss.present) |
| 1806 | return false; |
| 1807 | |
| 1808 | return true; |
| 1809 | } |
| 1810 | |
| 1811 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 1812 | { |
| 1813 | struct kvm_segment var; |
| 1814 | unsigned int rpl; |
| 1815 | |
| 1816 | vmx_get_segment(vcpu, &var, seg); |
| 1817 | rpl = var.selector & SELECTOR_RPL_MASK; |
| 1818 | |
| 1819 | if (!var.s) |
| 1820 | return false; |
| 1821 | if (!var.present) |
| 1822 | return false; |
| 1823 | if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) { |
| 1824 | if (var.dpl < rpl) /* DPL < RPL */ |
| 1825 | return false; |
| 1826 | } |
| 1827 | |
| 1828 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 1829 | * rights flags |
| 1830 | */ |
| 1831 | return true; |
| 1832 | } |
| 1833 | |
| 1834 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 1835 | { |
| 1836 | struct kvm_segment tr; |
| 1837 | |
| 1838 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 1839 | |
| 1840 | if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 1841 | return false; |
| 1842 | if ((tr.type != 3) || (tr.type != 11)) /* TODO: Check if guest is in IA32e mode */ |
| 1843 | return false; |
| 1844 | if (!tr.present) |
| 1845 | return false; |
| 1846 | |
| 1847 | return true; |
| 1848 | } |
| 1849 | |
| 1850 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 1851 | { |
| 1852 | struct kvm_segment ldtr; |
| 1853 | |
| 1854 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 1855 | |
| 1856 | if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 1857 | return false; |
| 1858 | if (ldtr.type != 2) |
| 1859 | return false; |
| 1860 | if (!ldtr.present) |
| 1861 | return false; |
| 1862 | |
| 1863 | return true; |
| 1864 | } |
| 1865 | |
| 1866 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 1867 | { |
| 1868 | struct kvm_segment cs, ss; |
| 1869 | |
| 1870 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 1871 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 1872 | |
| 1873 | return ((cs.selector & SELECTOR_RPL_MASK) == |
| 1874 | (ss.selector & SELECTOR_RPL_MASK)); |
| 1875 | } |
| 1876 | |
| 1877 | /* |
| 1878 | * Check if guest state is valid. Returns true if valid, false if |
| 1879 | * not. |
| 1880 | * We assume that registers are always usable |
| 1881 | */ |
| 1882 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 1883 | { |
| 1884 | /* real mode guest state checks */ |
| 1885 | if (!(vcpu->arch.cr0 & X86_CR0_PE)) { |
| 1886 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 1887 | return false; |
| 1888 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 1889 | return false; |
| 1890 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 1891 | return false; |
| 1892 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 1893 | return false; |
| 1894 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 1895 | return false; |
| 1896 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 1897 | return false; |
| 1898 | } else { |
| 1899 | /* protected mode guest state checks */ |
| 1900 | if (!cs_ss_rpl_check(vcpu)) |
| 1901 | return false; |
| 1902 | if (!code_segment_valid(vcpu)) |
| 1903 | return false; |
| 1904 | if (!stack_segment_valid(vcpu)) |
| 1905 | return false; |
| 1906 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 1907 | return false; |
| 1908 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 1909 | return false; |
| 1910 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 1911 | return false; |
| 1912 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 1913 | return false; |
| 1914 | if (!tr_valid(vcpu)) |
| 1915 | return false; |
| 1916 | if (!ldtr_valid(vcpu)) |
| 1917 | return false; |
| 1918 | } |
| 1919 | /* TODO: |
| 1920 | * - Add checks on RIP |
| 1921 | * - Add checks on RFLAGS |
| 1922 | */ |
| 1923 | |
| 1924 | return true; |
| 1925 | } |
| 1926 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1927 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1928 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1929 | gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1930 | u16 data = 0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1931 | int ret = 0; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1932 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1933 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1934 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 1935 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1936 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1937 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 1938 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 1939 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1940 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1941 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1942 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 1943 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1944 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1945 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 1946 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1947 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1948 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1949 | r = kvm_write_guest_page(kvm, fn, &data, |
| 1950 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 1951 | sizeof(u8)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1952 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1953 | goto out; |
| 1954 | |
| 1955 | ret = 1; |
| 1956 | out: |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1957 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1958 | } |
| 1959 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 1960 | static int init_rmode_identity_map(struct kvm *kvm) |
| 1961 | { |
| 1962 | int i, r, ret; |
| 1963 | pfn_t identity_map_pfn; |
| 1964 | u32 tmp; |
| 1965 | |
| 1966 | if (!vm_need_ept()) |
| 1967 | return 1; |
| 1968 | if (unlikely(!kvm->arch.ept_identity_pagetable)) { |
| 1969 | printk(KERN_ERR "EPT: identity-mapping pagetable " |
| 1970 | "haven't been allocated!\n"); |
| 1971 | return 0; |
| 1972 | } |
| 1973 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
| 1974 | return 1; |
| 1975 | ret = 0; |
| 1976 | identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT; |
| 1977 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 1978 | if (r < 0) |
| 1979 | goto out; |
| 1980 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 1981 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 1982 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 1983 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 1984 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 1985 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 1986 | if (r < 0) |
| 1987 | goto out; |
| 1988 | } |
| 1989 | kvm->arch.ept_identity_pagetable_done = true; |
| 1990 | ret = 1; |
| 1991 | out: |
| 1992 | return ret; |
| 1993 | } |
| 1994 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1995 | static void seg_setup(int seg) |
| 1996 | { |
| 1997 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1998 | |
| 1999 | vmcs_write16(sf->selector, 0); |
| 2000 | vmcs_writel(sf->base, 0); |
| 2001 | vmcs_write32(sf->limit, 0xffff); |
Avi Kivity | a16b20d | 2008-08-20 15:48:27 +0300 | [diff] [blame] | 2002 | vmcs_write32(sf->ar_bytes, 0xf3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2003 | } |
| 2004 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2005 | static int alloc_apic_access_page(struct kvm *kvm) |
| 2006 | { |
| 2007 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 2008 | int r = 0; |
| 2009 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2010 | down_write(&kvm->slots_lock); |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2011 | if (kvm->arch.apic_access_page) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2012 | goto out; |
| 2013 | kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT; |
| 2014 | kvm_userspace_mem.flags = 0; |
| 2015 | kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL; |
| 2016 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 2017 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 2018 | if (r) |
| 2019 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2020 | |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2021 | kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2022 | out: |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2023 | up_write(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2024 | return r; |
| 2025 | } |
| 2026 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2027 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 2028 | { |
| 2029 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 2030 | int r = 0; |
| 2031 | |
| 2032 | down_write(&kvm->slots_lock); |
| 2033 | if (kvm->arch.ept_identity_pagetable) |
| 2034 | goto out; |
| 2035 | kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT; |
| 2036 | kvm_userspace_mem.flags = 0; |
| 2037 | kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 2038 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 2039 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 2040 | if (r) |
| 2041 | goto out; |
| 2042 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2043 | kvm->arch.ept_identity_pagetable = gfn_to_page(kvm, |
| 2044 | VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2045 | out: |
| 2046 | up_write(&kvm->slots_lock); |
| 2047 | return r; |
| 2048 | } |
| 2049 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2050 | static void allocate_vpid(struct vcpu_vmx *vmx) |
| 2051 | { |
| 2052 | int vpid; |
| 2053 | |
| 2054 | vmx->vpid = 0; |
| 2055 | if (!enable_vpid || !cpu_has_vmx_vpid()) |
| 2056 | return; |
| 2057 | spin_lock(&vmx_vpid_lock); |
| 2058 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 2059 | if (vpid < VMX_NR_VPIDS) { |
| 2060 | vmx->vpid = vpid; |
| 2061 | __set_bit(vpid, vmx_vpid_bitmap); |
| 2062 | } |
| 2063 | spin_unlock(&vmx_vpid_lock); |
| 2064 | } |
| 2065 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 2066 | static void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2067 | { |
| 2068 | void *va; |
| 2069 | |
| 2070 | if (!cpu_has_vmx_msr_bitmap()) |
| 2071 | return; |
| 2072 | |
| 2073 | /* |
| 2074 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 2075 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 2076 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 2077 | */ |
| 2078 | va = kmap(msr_bitmap); |
| 2079 | if (msr <= 0x1fff) { |
| 2080 | __clear_bit(msr, va + 0x000); /* read-low */ |
| 2081 | __clear_bit(msr, va + 0x800); /* write-low */ |
| 2082 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2083 | msr &= 0x1fff; |
| 2084 | __clear_bit(msr, va + 0x400); /* read-high */ |
| 2085 | __clear_bit(msr, va + 0xc00); /* write-high */ |
| 2086 | } |
| 2087 | kunmap(msr_bitmap); |
| 2088 | } |
| 2089 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2090 | /* |
| 2091 | * Sets up the vmcs for emulated real mode. |
| 2092 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2093 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2094 | { |
| 2095 | u32 host_sysenter_cs; |
| 2096 | u32 junk; |
| 2097 | unsigned long a; |
| 2098 | struct descriptor_table dt; |
| 2099 | int i; |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 2100 | unsigned long kvm_vmx_return; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2101 | u32 exec_control; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2102 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2103 | /* I/O */ |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2104 | vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a)); |
| 2105 | vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2106 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2107 | if (cpu_has_vmx_msr_bitmap()) |
| 2108 | vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap)); |
| 2109 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2110 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 2111 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2112 | /* Control */ |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2113 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, |
| 2114 | vmcs_config.pin_based_exec_ctrl); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2115 | |
| 2116 | exec_control = vmcs_config.cpu_based_exec_ctrl; |
| 2117 | if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) { |
| 2118 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2119 | #ifdef CONFIG_X86_64 |
| 2120 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 2121 | CPU_BASED_CR8_LOAD_EXITING; |
| 2122 | #endif |
| 2123 | } |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2124 | if (!vm_need_ept()) |
| 2125 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
Marcelo Tosatti | 83dbc83 | 2008-10-07 17:01:27 -0300 | [diff] [blame] | 2126 | CPU_BASED_CR3_LOAD_EXITING | |
| 2127 | CPU_BASED_INVLPG_EXITING; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2128 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2129 | |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 2130 | if (cpu_has_secondary_exec_ctrls()) { |
| 2131 | exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
| 2132 | if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 2133 | exec_control &= |
| 2134 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2135 | if (vmx->vpid == 0) |
| 2136 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2137 | if (!vm_need_ept()) |
| 2138 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 2139 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 2140 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2141 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2142 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf); |
| 2143 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2144 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 2145 | |
| 2146 | vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */ |
| 2147 | vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */ |
| 2148 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 2149 | |
| 2150 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
| 2151 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 2152 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2153 | vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */ |
| 2154 | vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2155 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2156 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2157 | rdmsrl(MSR_FS_BASE, a); |
| 2158 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 2159 | rdmsrl(MSR_GS_BASE, a); |
| 2160 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 2161 | #else |
| 2162 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 2163 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 2164 | #endif |
| 2165 | |
| 2166 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 2167 | |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2168 | kvm_get_idt(&dt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2169 | vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */ |
| 2170 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2171 | asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return)); |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 2172 | vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */ |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2173 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 2174 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 2175 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2176 | |
| 2177 | rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk); |
| 2178 | vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs); |
| 2179 | rdmsrl(MSR_IA32_SYSENTER_ESP, a); |
| 2180 | vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */ |
| 2181 | rdmsrl(MSR_IA32_SYSENTER_EIP, a); |
| 2182 | vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */ |
| 2183 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2184 | for (i = 0; i < NR_VMX_MSR; ++i) { |
| 2185 | u32 index = vmx_msr_index[i]; |
| 2186 | u32 data_low, data_high; |
| 2187 | u64 data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2188 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2189 | |
| 2190 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 2191 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 2192 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 2193 | continue; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2194 | data = data_low | ((u64)data_high << 32); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2195 | vmx->host_msrs[j].index = index; |
| 2196 | vmx->host_msrs[j].reserved = 0; |
| 2197 | vmx->host_msrs[j].data = data; |
| 2198 | vmx->guest_msrs[j] = vmx->host_msrs[j]; |
| 2199 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2200 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2201 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2202 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2203 | |
| 2204 | /* 22.2.1, 20.8.1 */ |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2205 | vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl); |
| 2206 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2207 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
| 2208 | vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK); |
| 2209 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2210 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2211 | return 0; |
| 2212 | } |
| 2213 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2214 | static int init_rmode(struct kvm *kvm) |
| 2215 | { |
| 2216 | if (!init_rmode_tss(kvm)) |
| 2217 | return 0; |
| 2218 | if (!init_rmode_identity_map(kvm)) |
| 2219 | return 0; |
| 2220 | return 1; |
| 2221 | } |
| 2222 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2223 | static int vmx_vcpu_reset(struct kvm_vcpu *vcpu) |
| 2224 | { |
| 2225 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2226 | u64 msr; |
| 2227 | int ret; |
| 2228 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2229 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2230 | down_read(&vcpu->kvm->slots_lock); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 2231 | if (!init_rmode(vmx->vcpu.kvm)) { |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2232 | ret = -ENOMEM; |
| 2233 | goto out; |
| 2234 | } |
| 2235 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2236 | vmx->vcpu.arch.rmode.active = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2237 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame^] | 2238 | vmx->soft_vnmi_blocked = 0; |
| 2239 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2240 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2241 | kvm_set_cr8(&vmx->vcpu, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2242 | msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
| 2243 | if (vmx->vcpu.vcpu_id == 0) |
| 2244 | msr |= MSR_IA32_APICBASE_BSP; |
| 2245 | kvm_set_apic_base(&vmx->vcpu, msr); |
| 2246 | |
| 2247 | fx_init(&vmx->vcpu); |
| 2248 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 2249 | seg_setup(VCPU_SREG_CS); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2250 | /* |
| 2251 | * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode |
| 2252 | * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh. |
| 2253 | */ |
| 2254 | if (vmx->vcpu.vcpu_id == 0) { |
| 2255 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
| 2256 | vmcs_writel(GUEST_CS_BASE, 0x000f0000); |
| 2257 | } else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2258 | vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8); |
| 2259 | vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2260 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2261 | |
| 2262 | seg_setup(VCPU_SREG_DS); |
| 2263 | seg_setup(VCPU_SREG_ES); |
| 2264 | seg_setup(VCPU_SREG_FS); |
| 2265 | seg_setup(VCPU_SREG_GS); |
| 2266 | seg_setup(VCPU_SREG_SS); |
| 2267 | |
| 2268 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 2269 | vmcs_writel(GUEST_TR_BASE, 0); |
| 2270 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 2271 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 2272 | |
| 2273 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 2274 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 2275 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 2276 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 2277 | |
| 2278 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 2279 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 2280 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 2281 | |
| 2282 | vmcs_writel(GUEST_RFLAGS, 0x02); |
| 2283 | if (vmx->vcpu.vcpu_id == 0) |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2284 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2285 | else |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2286 | kvm_rip_write(vcpu, 0); |
| 2287 | kvm_register_write(vcpu, VCPU_REGS_RSP, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 2288 | |
| 2289 | /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */ |
| 2290 | vmcs_writel(GUEST_DR7, 0x400); |
| 2291 | |
| 2292 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 2293 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 2294 | |
| 2295 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 2296 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 2297 | |
| 2298 | vmcs_write32(GUEST_ACTIVITY_STATE, 0); |
| 2299 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
| 2300 | vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
| 2301 | |
| 2302 | guest_write_tsc(0); |
| 2303 | |
| 2304 | /* Special registers */ |
| 2305 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 2306 | |
| 2307 | setup_msrs(vmx); |
| 2308 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2309 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 2310 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2311 | if (cpu_has_vmx_tpr_shadow()) { |
| 2312 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
| 2313 | if (vm_need_tpr_shadow(vmx->vcpu.kvm)) |
| 2314 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2315 | page_to_phys(vmx->vcpu.arch.apic->regs_page)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2316 | vmcs_write32(TPR_THRESHOLD, 0); |
| 2317 | } |
| 2318 | |
| 2319 | if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 2320 | vmcs_write64(APIC_ACCESS_ADDR, |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2321 | page_to_phys(vmx->vcpu.kvm->arch.apic_access_page)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2322 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2323 | if (vmx->vpid != 0) |
| 2324 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 2325 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2326 | vmx->vcpu.arch.cr0 = 0x60000010; |
| 2327 | vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2328 | vmx_set_cr4(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2329 | vmx_set_efer(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2330 | vmx_fpu_activate(&vmx->vcpu); |
| 2331 | update_exception_bitmap(&vmx->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2332 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2333 | vpid_sync_vcpu_all(vmx); |
| 2334 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2335 | ret = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2336 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2337 | /* HACK: Don't enable emulation on guest boot/reset */ |
| 2338 | vmx->emulation_required = 0; |
| 2339 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2340 | out: |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2341 | up_read(&vcpu->kvm->slots_lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2342 | return ret; |
| 2343 | } |
| 2344 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame^] | 2345 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
| 2346 | { |
| 2347 | u32 cpu_based_vm_exec_control; |
| 2348 | |
| 2349 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 2350 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 2351 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 2352 | } |
| 2353 | |
| 2354 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
| 2355 | { |
| 2356 | u32 cpu_based_vm_exec_control; |
| 2357 | |
| 2358 | if (!cpu_has_virtual_nmis()) { |
| 2359 | enable_irq_window(vcpu); |
| 2360 | return; |
| 2361 | } |
| 2362 | |
| 2363 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 2364 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING; |
| 2365 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 2366 | } |
| 2367 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2368 | static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq) |
| 2369 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 2370 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2371 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2372 | KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler); |
| 2373 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 2374 | ++vcpu->stat.irq_injections; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2375 | if (vcpu->arch.rmode.active) { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 2376 | vmx->rmode.irq.pending = true; |
| 2377 | vmx->rmode.irq.vector = irq; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2378 | vmx->rmode.irq.rip = kvm_rip_read(vcpu); |
Avi Kivity | 9c5623e | 2007-11-08 18:19:20 +0200 | [diff] [blame] | 2379 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2380 | irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK); |
| 2381 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2382 | kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2383 | return; |
| 2384 | } |
| 2385 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2386 | irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 2387 | } |
| 2388 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2389 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 2390 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 2391 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2392 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame^] | 2393 | if (!cpu_has_virtual_nmis()) { |
| 2394 | /* |
| 2395 | * Tracking the NMI-blocked state in software is built upon |
| 2396 | * finding the next open IRQ window. This, in turn, depends on |
| 2397 | * well-behaving guests: They have to keep IRQs disabled at |
| 2398 | * least as long as the NMI handler runs. Otherwise we may |
| 2399 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 2400 | * highly unlikely, we can live with the residual risk. |
| 2401 | */ |
| 2402 | vmx->soft_vnmi_blocked = 1; |
| 2403 | vmx->vnmi_blocked_time = 0; |
| 2404 | } |
| 2405 | |
Jan Kiszka | 487b391 | 2008-09-26 09:30:56 +0200 | [diff] [blame] | 2406 | ++vcpu->stat.nmi_injections; |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 2407 | if (vcpu->arch.rmode.active) { |
| 2408 | vmx->rmode.irq.pending = true; |
| 2409 | vmx->rmode.irq.vector = NMI_VECTOR; |
| 2410 | vmx->rmode.irq.rip = kvm_rip_read(vcpu); |
| 2411 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2412 | NMI_VECTOR | INTR_TYPE_SOFT_INTR | |
| 2413 | INTR_INFO_VALID_MASK); |
| 2414 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1); |
| 2415 | kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1); |
| 2416 | return; |
| 2417 | } |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2418 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2419 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2420 | } |
| 2421 | |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 2422 | static void vmx_update_window_states(struct kvm_vcpu *vcpu) |
| 2423 | { |
| 2424 | u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2425 | |
| 2426 | vcpu->arch.nmi_window_open = |
| 2427 | !(guest_intr & (GUEST_INTR_STATE_STI | |
| 2428 | GUEST_INTR_STATE_MOV_SS | |
| 2429 | GUEST_INTR_STATE_NMI)); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame^] | 2430 | if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked) |
| 2431 | vcpu->arch.nmi_window_open = 0; |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 2432 | |
| 2433 | vcpu->arch.interrupt_window_open = |
| 2434 | ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
| 2435 | !(guest_intr & (GUEST_INTR_STATE_STI | |
| 2436 | GUEST_INTR_STATE_MOV_SS))); |
| 2437 | } |
| 2438 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2439 | static void kvm_do_inject_irq(struct kvm_vcpu *vcpu) |
| 2440 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2441 | int word_index = __ffs(vcpu->arch.irq_summary); |
| 2442 | int bit_index = __ffs(vcpu->arch.irq_pending[word_index]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2443 | int irq = word_index * BITS_PER_LONG + bit_index; |
| 2444 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2445 | clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]); |
| 2446 | if (!vcpu->arch.irq_pending[word_index]) |
| 2447 | clear_bit(word_index, &vcpu->arch.irq_summary); |
Avi Kivity | ecfc79c | 2008-08-14 11:13:16 +0300 | [diff] [blame] | 2448 | kvm_queue_interrupt(vcpu, irq); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2449 | } |
| 2450 | |
Jan Kiszka | f460ee4 | 2008-09-26 09:30:50 +0200 | [diff] [blame] | 2451 | static void do_interrupt_requests(struct kvm_vcpu *vcpu, |
| 2452 | struct kvm_run *kvm_run) |
| 2453 | { |
| 2454 | vmx_update_window_states(vcpu); |
| 2455 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame^] | 2456 | if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) { |
| 2457 | if (vcpu->arch.nmi_window_open) { |
| 2458 | vcpu->arch.nmi_pending = false; |
| 2459 | vcpu->arch.nmi_injected = true; |
| 2460 | } else { |
| 2461 | enable_nmi_window(vcpu); |
Jan Kiszka | 487b391 | 2008-09-26 09:30:56 +0200 | [diff] [blame] | 2462 | return; |
| 2463 | } |
Jan Kiszka | 487b391 | 2008-09-26 09:30:56 +0200 | [diff] [blame] | 2464 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame^] | 2465 | if (vcpu->arch.nmi_injected) { |
| 2466 | vmx_inject_nmi(vcpu); |
| 2467 | if (vcpu->arch.nmi_pending || kvm_run->request_nmi_window) |
| 2468 | enable_nmi_window(vcpu); |
| 2469 | else if (vcpu->arch.irq_summary |
| 2470 | || kvm_run->request_interrupt_window) |
| 2471 | enable_irq_window(vcpu); |
| 2472 | return; |
| 2473 | } |
| 2474 | if (!vcpu->arch.nmi_window_open || kvm_run->request_nmi_window) |
| 2475 | enable_nmi_window(vcpu); |
Jan Kiszka | 487b391 | 2008-09-26 09:30:56 +0200 | [diff] [blame] | 2476 | |
Jan Kiszka | f460ee4 | 2008-09-26 09:30:50 +0200 | [diff] [blame] | 2477 | if (vcpu->arch.interrupt_window_open) { |
| 2478 | if (vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending) |
| 2479 | kvm_do_inject_irq(vcpu); |
| 2480 | |
| 2481 | if (vcpu->arch.interrupt.pending) |
| 2482 | vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr); |
| 2483 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2484 | if (!vcpu->arch.interrupt_window_open && |
| 2485 | (vcpu->arch.irq_summary || kvm_run->request_interrupt_window)) |
Jan Kiszka | f460ee4 | 2008-09-26 09:30:50 +0200 | [diff] [blame] | 2486 | enable_irq_window(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2487 | } |
| 2488 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2489 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 2490 | { |
| 2491 | int ret; |
| 2492 | struct kvm_userspace_memory_region tss_mem = { |
| 2493 | .slot = 8, |
| 2494 | .guest_phys_addr = addr, |
| 2495 | .memory_size = PAGE_SIZE * 3, |
| 2496 | .flags = 0, |
| 2497 | }; |
| 2498 | |
| 2499 | ret = kvm_set_memory_region(kvm, &tss_mem, 0); |
| 2500 | if (ret) |
| 2501 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2502 | kvm->arch.tss_addr = addr; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2503 | return 0; |
| 2504 | } |
| 2505 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2506 | static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu) |
| 2507 | { |
| 2508 | struct kvm_guest_debug *dbg = &vcpu->guest_debug; |
| 2509 | |
| 2510 | set_debugreg(dbg->bp[0], 0); |
| 2511 | set_debugreg(dbg->bp[1], 1); |
| 2512 | set_debugreg(dbg->bp[2], 2); |
| 2513 | set_debugreg(dbg->bp[3], 3); |
| 2514 | |
| 2515 | if (dbg->singlestep) { |
| 2516 | unsigned long flags; |
| 2517 | |
| 2518 | flags = vmcs_readl(GUEST_RFLAGS); |
| 2519 | flags |= X86_EFLAGS_TF | X86_EFLAGS_RF; |
| 2520 | vmcs_writel(GUEST_RFLAGS, flags); |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 2525 | int vec, u32 err_code) |
| 2526 | { |
Nitin A Kamble | b3f3770 | 2007-05-17 15:50:34 +0300 | [diff] [blame] | 2527 | /* |
| 2528 | * Instruction with address size override prefix opcode 0x67 |
| 2529 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 2530 | */ |
| 2531 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 2532 | if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2533 | return 1; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2534 | /* |
| 2535 | * Forward all other exceptions that are valid in real mode. |
| 2536 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 2537 | * the required debugging infrastructure rework. |
| 2538 | */ |
| 2539 | switch (vec) { |
| 2540 | case DE_VECTOR: |
| 2541 | case DB_VECTOR: |
| 2542 | case BP_VECTOR: |
| 2543 | case OF_VECTOR: |
| 2544 | case BR_VECTOR: |
| 2545 | case UD_VECTOR: |
| 2546 | case DF_VECTOR: |
| 2547 | case SS_VECTOR: |
| 2548 | case GP_VECTOR: |
| 2549 | case MF_VECTOR: |
| 2550 | kvm_queue_exception(vcpu, vec); |
| 2551 | return 1; |
| 2552 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2553 | return 0; |
| 2554 | } |
| 2555 | |
| 2556 | static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2557 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 2558 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2559 | u32 intr_info, error_code; |
| 2560 | unsigned long cr2, rip; |
| 2561 | u32 vect_info; |
| 2562 | enum emulation_result er; |
| 2563 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 2564 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2565 | intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 2566 | |
| 2567 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2568 | !is_page_fault(intr_info)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2569 | printk(KERN_ERR "%s: unexpected, vectoring info 0x%x " |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2570 | "intr info 0x%x\n", __func__, vect_info, intr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2571 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2572 | if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2573 | int irq = vect_info & VECTORING_INFO_VECTOR_MASK; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2574 | set_bit(irq, vcpu->arch.irq_pending); |
| 2575 | set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2576 | } |
| 2577 | |
Jan Kiszka | e4a4188 | 2008-09-26 09:30:46 +0200 | [diff] [blame] | 2578 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 2579 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2580 | |
| 2581 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2582 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2583 | return 1; |
| 2584 | } |
| 2585 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2586 | if (is_invalid_opcode(intr_info)) { |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 2587 | er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2588 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 2589 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2590 | return 1; |
| 2591 | } |
| 2592 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2593 | error_code = 0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2594 | rip = kvm_rip_read(vcpu); |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 2595 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2596 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 2597 | if (is_page_fault(intr_info)) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2598 | /* EPT won't cause page fault directly */ |
| 2599 | if (vm_need_ept()) |
| 2600 | BUG(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2601 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2602 | KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2, |
| 2603 | (u32)((u64)cr2 >> 32), handler); |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 2604 | if (vcpu->arch.interrupt.pending || vcpu->arch.exception.pending) |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 2605 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 2606 | return kvm_mmu_page_fault(vcpu, cr2, error_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2607 | } |
| 2608 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2609 | if (vcpu->arch.rmode.active && |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2610 | handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK, |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 2611 | error_code)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2612 | if (vcpu->arch.halt_request) { |
| 2613 | vcpu->arch.halt_request = 0; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 2614 | return kvm_emulate_halt(vcpu); |
| 2615 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2616 | return 1; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 2617 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2618 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2619 | if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == |
| 2620 | (INTR_TYPE_EXCEPTION | 1)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2621 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 2622 | return 0; |
| 2623 | } |
| 2624 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 2625 | kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK; |
| 2626 | kvm_run->ex.error_code = error_code; |
| 2627 | return 0; |
| 2628 | } |
| 2629 | |
| 2630 | static int handle_external_interrupt(struct kvm_vcpu *vcpu, |
| 2631 | struct kvm_run *kvm_run) |
| 2632 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2633 | ++vcpu->stat.irq_exits; |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2634 | KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2635 | return 1; |
| 2636 | } |
| 2637 | |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 2638 | static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2639 | { |
| 2640 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 2641 | return 0; |
| 2642 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2643 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2644 | static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2645 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2646 | unsigned long exit_qualification; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2647 | int size, down, in, string, rep; |
| 2648 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2649 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2650 | ++vcpu->stat.io_exits; |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2651 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2652 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2653 | |
| 2654 | if (string) { |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 2655 | if (emulate_instruction(vcpu, |
| 2656 | kvm_run, 0, 0, 0) == EMULATE_DO_MMIO) |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2657 | return 0; |
| 2658 | return 1; |
| 2659 | } |
| 2660 | |
| 2661 | size = (exit_qualification & 7) + 1; |
| 2662 | in = (exit_qualification & 8) != 0; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2663 | down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2664 | rep = (exit_qualification & 32) != 0; |
| 2665 | port = exit_qualification >> 16; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2666 | |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 2667 | return kvm_emulate_pio(vcpu, kvm_run, in, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2668 | } |
| 2669 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2670 | static void |
| 2671 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 2672 | { |
| 2673 | /* |
| 2674 | * Patch in the VMCALL instruction: |
| 2675 | */ |
| 2676 | hypercall[0] = 0x0f; |
| 2677 | hypercall[1] = 0x01; |
| 2678 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2679 | } |
| 2680 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2681 | static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2682 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2683 | unsigned long exit_qualification; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2684 | int cr; |
| 2685 | int reg; |
| 2686 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2687 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2688 | cr = exit_qualification & 15; |
| 2689 | reg = (exit_qualification >> 8) & 15; |
| 2690 | switch ((exit_qualification >> 4) & 3) { |
| 2691 | case 0: /* mov to cr */ |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2692 | KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, |
| 2693 | (u32)kvm_register_read(vcpu, reg), |
| 2694 | (u32)((u64)kvm_register_read(vcpu, reg) >> 32), |
| 2695 | handler); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2696 | switch (cr) { |
| 2697 | case 0: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2698 | kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2699 | skip_emulated_instruction(vcpu); |
| 2700 | return 1; |
| 2701 | case 3: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2702 | kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2703 | skip_emulated_instruction(vcpu); |
| 2704 | return 1; |
| 2705 | case 4: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2706 | kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2707 | skip_emulated_instruction(vcpu); |
| 2708 | return 1; |
| 2709 | case 8: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2710 | kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2711 | skip_emulated_instruction(vcpu); |
Avi Kivity | e531406 | 2007-12-06 16:32:45 +0200 | [diff] [blame] | 2712 | if (irqchip_in_kernel(vcpu->kvm)) |
| 2713 | return 1; |
Yang, Sheng | 253abde | 2007-08-16 13:01:00 +0300 | [diff] [blame] | 2714 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
| 2715 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2716 | }; |
| 2717 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2718 | case 2: /* clts */ |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2719 | vmx_fpu_deactivate(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2720 | vcpu->arch.cr0 &= ~X86_CR0_TS; |
| 2721 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2722 | vmx_fpu_activate(vcpu); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2723 | KVMTRACE_0D(CLTS, vcpu, handler); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2724 | skip_emulated_instruction(vcpu); |
| 2725 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2726 | case 1: /*mov from cr*/ |
| 2727 | switch (cr) { |
| 2728 | case 3: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2729 | kvm_register_write(vcpu, reg, vcpu->arch.cr3); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2730 | KVMTRACE_3D(CR_READ, vcpu, (u32)cr, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2731 | (u32)kvm_register_read(vcpu, reg), |
| 2732 | (u32)((u64)kvm_register_read(vcpu, reg) >> 32), |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2733 | handler); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2734 | skip_emulated_instruction(vcpu); |
| 2735 | return 1; |
| 2736 | case 8: |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2737 | kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu)); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2738 | KVMTRACE_2D(CR_READ, vcpu, (u32)cr, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2739 | (u32)kvm_register_read(vcpu, reg), handler); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2740 | skip_emulated_instruction(vcpu); |
| 2741 | return 1; |
| 2742 | } |
| 2743 | break; |
| 2744 | case 3: /* lmsw */ |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2745 | kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2746 | |
| 2747 | skip_emulated_instruction(vcpu); |
| 2748 | return 1; |
| 2749 | default: |
| 2750 | break; |
| 2751 | } |
| 2752 | kvm_run->exit_reason = 0; |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 2753 | pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2754 | (int)(exit_qualification >> 4) & 3, cr); |
| 2755 | return 0; |
| 2756 | } |
| 2757 | |
| 2758 | static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2759 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2760 | unsigned long exit_qualification; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2761 | unsigned long val; |
| 2762 | int dr, reg; |
| 2763 | |
| 2764 | /* |
| 2765 | * FIXME: this code assumes the host is debugging the guest. |
| 2766 | * need to deal with guest debugging itself too. |
| 2767 | */ |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2768 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2769 | dr = exit_qualification & 7; |
| 2770 | reg = (exit_qualification >> 8) & 15; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2771 | if (exit_qualification & 16) { |
| 2772 | /* mov from dr */ |
| 2773 | switch (dr) { |
| 2774 | case 6: |
| 2775 | val = 0xffff0ff0; |
| 2776 | break; |
| 2777 | case 7: |
| 2778 | val = 0x400; |
| 2779 | break; |
| 2780 | default: |
| 2781 | val = 0; |
| 2782 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2783 | kvm_register_write(vcpu, reg, val); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2784 | KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2785 | } else { |
| 2786 | /* mov to dr */ |
| 2787 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2788 | skip_emulated_instruction(vcpu); |
| 2789 | return 1; |
| 2790 | } |
| 2791 | |
| 2792 | static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2793 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 2794 | kvm_emulate_cpuid(vcpu); |
| 2795 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2799 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2800 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2801 | u64 data; |
| 2802 | |
| 2803 | if (vmx_get_msr(vcpu, ecx, &data)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2804 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2805 | return 1; |
| 2806 | } |
| 2807 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2808 | KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32), |
| 2809 | handler); |
| 2810 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2811 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2812 | vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u; |
| 2813 | vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2814 | skip_emulated_instruction(vcpu); |
| 2815 | return 1; |
| 2816 | } |
| 2817 | |
| 2818 | static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2819 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2820 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 2821 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 2822 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2823 | |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2824 | KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32), |
| 2825 | handler); |
| 2826 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2827 | if (vmx_set_msr(vcpu, ecx, data) != 0) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2828 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2829 | return 1; |
| 2830 | } |
| 2831 | |
| 2832 | skip_emulated_instruction(vcpu); |
| 2833 | return 1; |
| 2834 | } |
| 2835 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2836 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu, |
| 2837 | struct kvm_run *kvm_run) |
| 2838 | { |
| 2839 | return 1; |
| 2840 | } |
| 2841 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2842 | static int handle_interrupt_window(struct kvm_vcpu *vcpu, |
| 2843 | struct kvm_run *kvm_run) |
| 2844 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2845 | u32 cpu_based_vm_exec_control; |
| 2846 | |
| 2847 | /* clear pending irq */ |
| 2848 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 2849 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 2850 | 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] | 2851 | |
| 2852 | KVMTRACE_0D(PEND_INTR, vcpu, handler); |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 2853 | ++vcpu->stat.irq_window_exits; |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 2854 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2855 | /* |
| 2856 | * If the user space waits to inject interrupts, exit as soon as |
| 2857 | * possible |
| 2858 | */ |
| 2859 | if (kvm_run->request_interrupt_window && |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2860 | !vcpu->arch.irq_summary) { |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2861 | kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2862 | return 0; |
| 2863 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2864 | return 1; |
| 2865 | } |
| 2866 | |
| 2867 | static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2868 | { |
| 2869 | skip_emulated_instruction(vcpu); |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 2870 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2871 | } |
| 2872 | |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2873 | static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2874 | { |
Dor Laor | 510043d | 2007-02-19 18:25:43 +0200 | [diff] [blame] | 2875 | skip_emulated_instruction(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2876 | kvm_emulate_hypercall(vcpu); |
| 2877 | return 1; |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2878 | } |
| 2879 | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2880 | static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2881 | { |
| 2882 | u64 exit_qualification = vmcs_read64(EXIT_QUALIFICATION); |
| 2883 | |
| 2884 | kvm_mmu_invlpg(vcpu, exit_qualification); |
| 2885 | skip_emulated_instruction(vcpu); |
| 2886 | return 1; |
| 2887 | } |
| 2888 | |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 2889 | static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2890 | { |
| 2891 | skip_emulated_instruction(vcpu); |
| 2892 | /* TODO: Add support for VT-d/pass-through device */ |
| 2893 | return 1; |
| 2894 | } |
| 2895 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2896 | static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2897 | { |
| 2898 | u64 exit_qualification; |
| 2899 | enum emulation_result er; |
| 2900 | unsigned long offset; |
| 2901 | |
| 2902 | exit_qualification = vmcs_read64(EXIT_QUALIFICATION); |
| 2903 | offset = exit_qualification & 0xffful; |
| 2904 | |
| 2905 | er = emulate_instruction(vcpu, kvm_run, 0, 0, 0); |
| 2906 | |
| 2907 | if (er != EMULATE_DONE) { |
| 2908 | printk(KERN_ERR |
| 2909 | "Fail to handle apic access vmexit! Offset is 0x%lx\n", |
| 2910 | offset); |
| 2911 | return -ENOTSUPP; |
| 2912 | } |
| 2913 | return 1; |
| 2914 | } |
| 2915 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2916 | static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2917 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 2918 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2919 | unsigned long exit_qualification; |
| 2920 | u16 tss_selector; |
| 2921 | int reason; |
| 2922 | |
| 2923 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 2924 | |
| 2925 | reason = (u32)exit_qualification >> 30; |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 2926 | if (reason == TASK_SWITCH_GATE && vmx->vcpu.arch.nmi_injected && |
| 2927 | (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 2928 | (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK) |
| 2929 | == INTR_TYPE_NMI_INTR) { |
| 2930 | vcpu->arch.nmi_injected = false; |
| 2931 | if (cpu_has_virtual_nmis()) |
| 2932 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 2933 | GUEST_INTR_STATE_NMI); |
| 2934 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2935 | tss_selector = exit_qualification; |
| 2936 | |
| 2937 | return kvm_task_switch(vcpu, tss_selector, reason); |
| 2938 | } |
| 2939 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2940 | static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2941 | { |
| 2942 | u64 exit_qualification; |
| 2943 | enum emulation_result er; |
| 2944 | gpa_t gpa; |
| 2945 | unsigned long hva; |
| 2946 | int gla_validity; |
| 2947 | int r; |
| 2948 | |
| 2949 | exit_qualification = vmcs_read64(EXIT_QUALIFICATION); |
| 2950 | |
| 2951 | if (exit_qualification & (1 << 6)) { |
| 2952 | printk(KERN_ERR "EPT: GPA exceeds GAW!\n"); |
| 2953 | return -ENOTSUPP; |
| 2954 | } |
| 2955 | |
| 2956 | gla_validity = (exit_qualification >> 7) & 0x3; |
| 2957 | if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) { |
| 2958 | printk(KERN_ERR "EPT: Handling EPT violation failed!\n"); |
| 2959 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 2960 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
| 2961 | (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS)); |
| 2962 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 2963 | (long unsigned int)exit_qualification); |
| 2964 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
| 2965 | kvm_run->hw.hardware_exit_reason = 0; |
| 2966 | return -ENOTSUPP; |
| 2967 | } |
| 2968 | |
| 2969 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 2970 | hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT); |
| 2971 | if (!kvm_is_error_hva(hva)) { |
| 2972 | r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0); |
| 2973 | if (r < 0) { |
| 2974 | printk(KERN_ERR "EPT: Not enough memory!\n"); |
| 2975 | return -ENOMEM; |
| 2976 | } |
| 2977 | return 1; |
| 2978 | } else { |
| 2979 | /* must be MMIO */ |
| 2980 | er = emulate_instruction(vcpu, kvm_run, 0, 0, 0); |
| 2981 | |
| 2982 | if (er == EMULATE_FAIL) { |
| 2983 | printk(KERN_ERR |
| 2984 | "EPT: Fail to handle EPT violation vmexit!er is %d\n", |
| 2985 | er); |
| 2986 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 2987 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
| 2988 | (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS)); |
| 2989 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 2990 | (long unsigned int)exit_qualification); |
| 2991 | return -ENOTSUPP; |
| 2992 | } else if (er == EMULATE_DO_MMIO) |
| 2993 | return 0; |
| 2994 | } |
| 2995 | return 1; |
| 2996 | } |
| 2997 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2998 | static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2999 | { |
| 3000 | u32 cpu_based_vm_exec_control; |
| 3001 | |
| 3002 | /* clear pending NMI */ |
| 3003 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 3004 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 3005 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 3006 | ++vcpu->stat.nmi_window_exits; |
| 3007 | |
Jan Kiszka | 487b391 | 2008-09-26 09:30:56 +0200 | [diff] [blame] | 3008 | /* |
| 3009 | * If the user space waits to inject a NMI, exit as soon as possible |
| 3010 | */ |
| 3011 | if (kvm_run->request_nmi_window && !vcpu->arch.nmi_pending) { |
| 3012 | kvm_run->exit_reason = KVM_EXIT_NMI_WINDOW_OPEN; |
| 3013 | return 0; |
| 3014 | } |
| 3015 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3016 | return 1; |
| 3017 | } |
| 3018 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 3019 | static void handle_invalid_guest_state(struct kvm_vcpu *vcpu, |
| 3020 | struct kvm_run *kvm_run) |
| 3021 | { |
| 3022 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3023 | int err; |
| 3024 | |
| 3025 | preempt_enable(); |
| 3026 | local_irq_enable(); |
| 3027 | |
| 3028 | while (!guest_state_valid(vcpu)) { |
| 3029 | err = emulate_instruction(vcpu, kvm_run, 0, 0, 0); |
| 3030 | |
| 3031 | switch (err) { |
| 3032 | case EMULATE_DONE: |
| 3033 | break; |
| 3034 | case EMULATE_DO_MMIO: |
| 3035 | kvm_report_emulation_failure(vcpu, "mmio"); |
| 3036 | /* TODO: Handle MMIO */ |
| 3037 | return; |
| 3038 | default: |
| 3039 | kvm_report_emulation_failure(vcpu, "emulation failure"); |
| 3040 | return; |
| 3041 | } |
| 3042 | |
| 3043 | if (signal_pending(current)) |
| 3044 | break; |
| 3045 | if (need_resched()) |
| 3046 | schedule(); |
| 3047 | } |
| 3048 | |
| 3049 | local_irq_disable(); |
| 3050 | preempt_disable(); |
| 3051 | |
| 3052 | /* Guest state should be valid now, no more emulation should be needed */ |
| 3053 | vmx->emulation_required = 0; |
| 3054 | } |
| 3055 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3056 | /* |
| 3057 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 3058 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 3059 | * to be done to userspace and return 0. |
| 3060 | */ |
| 3061 | static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu, |
| 3062 | struct kvm_run *kvm_run) = { |
| 3063 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 3064 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 3065 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3066 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3067 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3068 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 3069 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 3070 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 3071 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 3072 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 3073 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 3074 | [EXIT_REASON_HLT] = handle_halt, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3075 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3076 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3077 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 3078 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 3079 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3080 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3081 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3082 | }; |
| 3083 | |
| 3084 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 3085 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3086 | |
| 3087 | /* |
| 3088 | * The guest has exited. See if we can fix it or if we need userspace |
| 3089 | * assistance. |
| 3090 | */ |
| 3091 | static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
| 3092 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3093 | u32 exit_reason = vmcs_read32(VM_EXIT_REASON); |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 3094 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 3095 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 3096 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3097 | KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu), |
| 3098 | (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3099 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3100 | /* Access CR3 don't cause VMExit in paging mode, so we need |
| 3101 | * to sync with guest real CR3. */ |
| 3102 | if (vm_need_ept() && is_paging(vcpu)) { |
| 3103 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 3104 | ept_load_pdptrs(vcpu); |
| 3105 | } |
| 3106 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 3107 | if (unlikely(vmx->fail)) { |
| 3108 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 3109 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 3110 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 3111 | return 0; |
| 3112 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3113 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3114 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3115 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 3116 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
| 3117 | exit_reason != EXIT_REASON_TASK_SWITCH)) |
| 3118 | printk(KERN_WARNING "%s: unexpected, valid vectoring info " |
| 3119 | "(0x%x) and exit reason is 0x%x\n", |
| 3120 | __func__, vectoring_info, exit_reason); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame^] | 3121 | |
| 3122 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) { |
| 3123 | if (vcpu->arch.interrupt_window_open) { |
| 3124 | vmx->soft_vnmi_blocked = 0; |
| 3125 | vcpu->arch.nmi_window_open = 1; |
| 3126 | } else if (vmx->vnmi_blocked_time > 1000000000LL && |
| 3127 | (kvm_run->request_nmi_window || vcpu->arch.nmi_pending)) { |
| 3128 | /* |
| 3129 | * This CPU don't support us in finding the end of an |
| 3130 | * NMI-blocked window if the guest runs with IRQs |
| 3131 | * disabled. So we pull the trigger after 1 s of |
| 3132 | * futile waiting, but inform the user about this. |
| 3133 | */ |
| 3134 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 3135 | "state on VCPU %d after 1 s timeout\n", |
| 3136 | __func__, vcpu->vcpu_id); |
| 3137 | vmx->soft_vnmi_blocked = 0; |
| 3138 | vmx->vcpu.arch.nmi_window_open = 1; |
| 3139 | } |
| 3140 | |
| 3141 | /* |
| 3142 | * If the user space waits to inject an NNI, exit ASAP |
| 3143 | */ |
| 3144 | if (vcpu->arch.nmi_window_open && kvm_run->request_nmi_window |
| 3145 | && !vcpu->arch.nmi_pending) { |
| 3146 | kvm_run->exit_reason = KVM_EXIT_NMI_WINDOW_OPEN; |
| 3147 | ++vcpu->stat.nmi_window_exits; |
| 3148 | return 0; |
| 3149 | } |
| 3150 | } |
| 3151 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3152 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 3153 | && kvm_vmx_exit_handlers[exit_reason]) |
| 3154 | return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run); |
| 3155 | else { |
| 3156 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
| 3157 | kvm_run->hw.hardware_exit_reason = exit_reason; |
| 3158 | } |
| 3159 | return 0; |
| 3160 | } |
| 3161 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3162 | static void update_tpr_threshold(struct kvm_vcpu *vcpu) |
| 3163 | { |
| 3164 | int max_irr, tpr; |
| 3165 | |
| 3166 | if (!vm_need_tpr_shadow(vcpu->kvm)) |
| 3167 | return; |
| 3168 | |
| 3169 | if (!kvm_lapic_enabled(vcpu) || |
| 3170 | ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) { |
| 3171 | vmcs_write32(TPR_THRESHOLD, 0); |
| 3172 | return; |
| 3173 | } |
| 3174 | |
| 3175 | tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4; |
| 3176 | vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4); |
| 3177 | } |
| 3178 | |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3179 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 3180 | { |
| 3181 | u32 exit_intr_info; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 3182 | u32 idt_vectoring_info; |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3183 | bool unblock_nmi; |
| 3184 | u8 vector; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 3185 | int type; |
| 3186 | bool idtv_info_valid; |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 3187 | u32 error; |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3188 | |
| 3189 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 3190 | if (cpu_has_virtual_nmis()) { |
| 3191 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 3192 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 3193 | /* |
| 3194 | * SDM 3: 25.7.1.2 |
| 3195 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 3196 | * a guest IRET fault. |
| 3197 | */ |
| 3198 | if (unblock_nmi && vector != DF_VECTOR) |
| 3199 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 3200 | GUEST_INTR_STATE_NMI); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame^] | 3201 | } else if (unlikely(vmx->soft_vnmi_blocked)) |
| 3202 | vmx->vnmi_blocked_time += |
| 3203 | ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time)); |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 3204 | |
| 3205 | idt_vectoring_info = vmx->idt_vectoring_info; |
| 3206 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
| 3207 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 3208 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
| 3209 | if (vmx->vcpu.arch.nmi_injected) { |
| 3210 | /* |
| 3211 | * SDM 3: 25.7.1.2 |
| 3212 | * Clear bit "block by NMI" before VM entry if a NMI delivery |
| 3213 | * faulted. |
| 3214 | */ |
| 3215 | if (idtv_info_valid && type == INTR_TYPE_NMI_INTR) |
| 3216 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 3217 | GUEST_INTR_STATE_NMI); |
| 3218 | else |
| 3219 | vmx->vcpu.arch.nmi_injected = false; |
| 3220 | } |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 3221 | kvm_clear_exception_queue(&vmx->vcpu); |
| 3222 | if (idtv_info_valid && type == INTR_TYPE_EXCEPTION) { |
| 3223 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
| 3224 | error = vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 3225 | kvm_queue_exception_e(&vmx->vcpu, vector, error); |
| 3226 | } else |
| 3227 | kvm_queue_exception(&vmx->vcpu, vector); |
| 3228 | vmx->idt_vectoring_info = 0; |
| 3229 | } |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 3230 | kvm_clear_interrupt_queue(&vmx->vcpu); |
| 3231 | if (idtv_info_valid && type == INTR_TYPE_EXT_INTR) { |
| 3232 | kvm_queue_interrupt(&vmx->vcpu, vector); |
| 3233 | vmx->idt_vectoring_info = 0; |
| 3234 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3235 | } |
| 3236 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3237 | static void vmx_intr_assist(struct kvm_vcpu *vcpu) |
| 3238 | { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3239 | update_tpr_threshold(vcpu); |
| 3240 | |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 3241 | vmx_update_window_states(vcpu); |
| 3242 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame^] | 3243 | if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) { |
| 3244 | if (vcpu->arch.interrupt.pending) { |
| 3245 | enable_nmi_window(vcpu); |
| 3246 | } else if (vcpu->arch.nmi_window_open) { |
| 3247 | vcpu->arch.nmi_pending = false; |
| 3248 | vcpu->arch.nmi_injected = true; |
| 3249 | } else { |
| 3250 | enable_nmi_window(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3251 | return; |
| 3252 | } |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3253 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame^] | 3254 | if (vcpu->arch.nmi_injected) { |
| 3255 | vmx_inject_nmi(vcpu); |
| 3256 | if (vcpu->arch.nmi_pending) |
| 3257 | enable_nmi_window(vcpu); |
| 3258 | else if (kvm_cpu_has_interrupt(vcpu)) |
| 3259 | enable_irq_window(vcpu); |
| 3260 | return; |
| 3261 | } |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 3262 | if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) { |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 3263 | if (vcpu->arch.interrupt_window_open) |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 3264 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu)); |
| 3265 | else |
| 3266 | enable_irq_window(vcpu); |
| 3267 | } |
| 3268 | if (vcpu->arch.interrupt.pending) { |
| 3269 | vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr); |
| 3270 | kvm_timer_intr_post(vcpu, vcpu->arch.interrupt.nr); |
| 3271 | } |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3272 | } |
| 3273 | |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3274 | /* |
| 3275 | * Failure to inject an interrupt should give us the information |
| 3276 | * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs |
| 3277 | * when fetching the interrupt redirection bitmap in the real-mode |
| 3278 | * tss, this doesn't happen. So we do it ourselves. |
| 3279 | */ |
| 3280 | static void fixup_rmode_irq(struct vcpu_vmx *vmx) |
| 3281 | { |
| 3282 | vmx->rmode.irq.pending = 0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3283 | if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip) |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3284 | return; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3285 | kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip); |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3286 | if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) { |
| 3287 | vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK; |
| 3288 | vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR; |
| 3289 | return; |
| 3290 | } |
| 3291 | vmx->idt_vectoring_info = |
| 3292 | VECTORING_INFO_VALID_MASK |
| 3293 | | INTR_TYPE_EXT_INTR |
| 3294 | | vmx->rmode.irq.vector; |
| 3295 | } |
| 3296 | |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3297 | #ifdef CONFIG_X86_64 |
| 3298 | #define R "r" |
| 3299 | #define Q "q" |
| 3300 | #else |
| 3301 | #define R "e" |
| 3302 | #define Q "l" |
| 3303 | #endif |
| 3304 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3305 | static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3306 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3307 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 3308 | u32 intr_info; |
Avi Kivity | e6adf28 | 2007-04-30 16:07:54 +0300 | [diff] [blame] | 3309 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame^] | 3310 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
| 3311 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) |
| 3312 | vmx->entry_time = ktime_get(); |
| 3313 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3314 | /* Handle invalid guest state instead of entering VMX */ |
| 3315 | if (vmx->emulation_required && emulate_invalid_guest_state) { |
| 3316 | handle_invalid_guest_state(vcpu, kvm_run); |
| 3317 | return; |
| 3318 | } |
| 3319 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3320 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 3321 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 3322 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 3323 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 3324 | |
Avi Kivity | e6adf28 | 2007-04-30 16:07:54 +0300 | [diff] [blame] | 3325 | /* |
| 3326 | * Loading guest fpu may have cleared host cr0.ts |
| 3327 | */ |
| 3328 | vmcs_writel(HOST_CR0, read_cr0()); |
| 3329 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3330 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3331 | /* Store host registers */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3332 | "push %%"R"dx; push %%"R"bp;" |
| 3333 | "push %%"R"cx \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 3334 | "cmp %%"R"sp, %c[host_rsp](%0) \n\t" |
| 3335 | "je 1f \n\t" |
| 3336 | "mov %%"R"sp, %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 3337 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 3338 | "1: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3339 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 3340 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3341 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3342 | "mov %c[cr2](%0), %%"R"ax \n\t" |
| 3343 | "mov %%"R"ax, %%cr2 \n\t" |
| 3344 | "mov %c[rax](%0), %%"R"ax \n\t" |
| 3345 | "mov %c[rbx](%0), %%"R"bx \n\t" |
| 3346 | "mov %c[rdx](%0), %%"R"dx \n\t" |
| 3347 | "mov %c[rsi](%0), %%"R"si \n\t" |
| 3348 | "mov %c[rdi](%0), %%"R"di \n\t" |
| 3349 | "mov %c[rbp](%0), %%"R"bp \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3350 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 3351 | "mov %c[r8](%0), %%r8 \n\t" |
| 3352 | "mov %c[r9](%0), %%r9 \n\t" |
| 3353 | "mov %c[r10](%0), %%r10 \n\t" |
| 3354 | "mov %c[r11](%0), %%r11 \n\t" |
| 3355 | "mov %c[r12](%0), %%r12 \n\t" |
| 3356 | "mov %c[r13](%0), %%r13 \n\t" |
| 3357 | "mov %c[r14](%0), %%r14 \n\t" |
| 3358 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3359 | #endif |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3360 | "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */ |
| 3361 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3362 | /* Enter guest mode */ |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 3363 | "jne .Llaunched \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 3364 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 3365 | "jmp .Lkvm_vmx_return \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 3366 | ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 3367 | ".Lkvm_vmx_return: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3368 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3369 | "xchg %0, (%%"R"sp) \n\t" |
| 3370 | "mov %%"R"ax, %c[rax](%0) \n\t" |
| 3371 | "mov %%"R"bx, %c[rbx](%0) \n\t" |
| 3372 | "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t" |
| 3373 | "mov %%"R"dx, %c[rdx](%0) \n\t" |
| 3374 | "mov %%"R"si, %c[rsi](%0) \n\t" |
| 3375 | "mov %%"R"di, %c[rdi](%0) \n\t" |
| 3376 | "mov %%"R"bp, %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3377 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 3378 | "mov %%r8, %c[r8](%0) \n\t" |
| 3379 | "mov %%r9, %c[r9](%0) \n\t" |
| 3380 | "mov %%r10, %c[r10](%0) \n\t" |
| 3381 | "mov %%r11, %c[r11](%0) \n\t" |
| 3382 | "mov %%r12, %c[r12](%0) \n\t" |
| 3383 | "mov %%r13, %c[r13](%0) \n\t" |
| 3384 | "mov %%r14, %c[r14](%0) \n\t" |
| 3385 | "mov %%r15, %c[r15](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3386 | #endif |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3387 | "mov %%cr2, %%"R"ax \n\t" |
| 3388 | "mov %%"R"ax, %c[cr2](%0) \n\t" |
| 3389 | |
| 3390 | "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 3391 | "setbe %c[fail](%0) \n\t" |
| 3392 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
| 3393 | [launched]"i"(offsetof(struct vcpu_vmx, launched)), |
| 3394 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 3395 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3396 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 3397 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 3398 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 3399 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 3400 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 3401 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 3402 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3403 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3404 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 3405 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 3406 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 3407 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 3408 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 3409 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 3410 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 3411 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3412 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3413 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 3414 | : "cc", "memory" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3415 | , R"bx", R"di", R"si" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 3416 | #ifdef CONFIG_X86_64 |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 3417 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
| 3418 | #endif |
| 3419 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3420 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3421 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)); |
| 3422 | vcpu->arch.regs_dirty = 0; |
| 3423 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 3424 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3425 | if (vmx->rmode.irq.pending) |
| 3426 | fixup_rmode_irq(vmx); |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 3427 | |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 3428 | vmx_update_window_states(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3429 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3430 | asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS)); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 3431 | vmx->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 3432 | |
| 3433 | intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 3434 | |
| 3435 | /* We need to handle NMIs before interrupts are enabled */ |
Jan Kiszka | e4a4188 | 2008-09-26 09:30:46 +0200 | [diff] [blame] | 3436 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR && |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3437 | (intr_info & INTR_INFO_VALID_MASK)) { |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3438 | KVMTRACE_0D(NMI, vcpu, handler); |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 3439 | asm("int $2"); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3440 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 3441 | |
| 3442 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3443 | } |
| 3444 | |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 3445 | #undef R |
| 3446 | #undef Q |
| 3447 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3448 | static void vmx_free_vmcs(struct kvm_vcpu *vcpu) |
| 3449 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3450 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3451 | |
| 3452 | if (vmx->vmcs) { |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3453 | vcpu_clear(vmx); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3454 | free_vmcs(vmx->vmcs); |
| 3455 | vmx->vmcs = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3456 | } |
| 3457 | } |
| 3458 | |
| 3459 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 3460 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3461 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3462 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3463 | spin_lock(&vmx_vpid_lock); |
| 3464 | if (vmx->vpid != 0) |
| 3465 | __clear_bit(vmx->vpid, vmx_vpid_bitmap); |
| 3466 | spin_unlock(&vmx_vpid_lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3467 | vmx_free_vmcs(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3468 | kfree(vmx->host_msrs); |
| 3469 | kfree(vmx->guest_msrs); |
| 3470 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 3471 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3472 | } |
| 3473 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3474 | 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] | 3475 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3476 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 3477 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 3478 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3479 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3480 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3481 | return ERR_PTR(-ENOMEM); |
| 3482 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3483 | allocate_vpid(vmx); |
| 3484 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3485 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 3486 | if (err) |
| 3487 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3488 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3489 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3490 | if (!vmx->guest_msrs) { |
| 3491 | err = -ENOMEM; |
| 3492 | goto uninit_vcpu; |
| 3493 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3494 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3495 | vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3496 | if (!vmx->host_msrs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3497 | goto free_guest_msrs; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3498 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3499 | vmx->vmcs = alloc_vmcs(); |
| 3500 | if (!vmx->vmcs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3501 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3502 | |
| 3503 | vmcs_clear(vmx->vmcs); |
| 3504 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 3505 | cpu = get_cpu(); |
| 3506 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3507 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3508 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 3509 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3510 | if (err) |
| 3511 | goto free_vmcs; |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 3512 | if (vm_need_virtualize_apic_accesses(kvm)) |
| 3513 | if (alloc_apic_access_page(kvm) != 0) |
| 3514 | goto free_vmcs; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3515 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3516 | if (vm_need_ept()) |
| 3517 | if (alloc_identity_pagetable(kvm) != 0) |
| 3518 | goto free_vmcs; |
| 3519 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3520 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3521 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3522 | free_vmcs: |
| 3523 | free_vmcs(vmx->vmcs); |
| 3524 | free_msrs: |
| 3525 | kfree(vmx->host_msrs); |
| 3526 | free_guest_msrs: |
| 3527 | kfree(vmx->guest_msrs); |
| 3528 | uninit_vcpu: |
| 3529 | kvm_vcpu_uninit(&vmx->vcpu); |
| 3530 | free_vcpu: |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 3531 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 3532 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3533 | } |
| 3534 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3535 | static void __init vmx_check_processor_compat(void *rtn) |
| 3536 | { |
| 3537 | struct vmcs_config vmcs_conf; |
| 3538 | |
| 3539 | *(int *)rtn = 0; |
| 3540 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 3541 | *(int *)rtn = -EIO; |
| 3542 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 3543 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 3544 | smp_processor_id()); |
| 3545 | *(int *)rtn = -EIO; |
| 3546 | } |
| 3547 | } |
| 3548 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 3549 | static int get_ept_level(void) |
| 3550 | { |
| 3551 | return VMX_EPT_DEFAULT_GAW + 1; |
| 3552 | } |
| 3553 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 3554 | static struct kvm_x86_ops vmx_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3555 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 3556 | .disabled_by_bios = vmx_disabled_by_bios, |
| 3557 | .hardware_setup = hardware_setup, |
| 3558 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3559 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3560 | .hardware_enable = hardware_enable, |
| 3561 | .hardware_disable = hardware_disable, |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 3562 | .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3563 | |
| 3564 | .vcpu_create = vmx_create_vcpu, |
| 3565 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3566 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3567 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3568 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3569 | .vcpu_load = vmx_vcpu_load, |
| 3570 | .vcpu_put = vmx_vcpu_put, |
| 3571 | |
| 3572 | .set_guest_debug = set_guest_debug, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3573 | .guest_debug_pre = kvm_guest_debug_pre, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3574 | .get_msr = vmx_get_msr, |
| 3575 | .set_msr = vmx_set_msr, |
| 3576 | .get_segment_base = vmx_get_segment_base, |
| 3577 | .get_segment = vmx_get_segment, |
| 3578 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3579 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3580 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 3581 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3582 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3583 | .set_cr3 = vmx_set_cr3, |
| 3584 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3585 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3586 | .get_idt = vmx_get_idt, |
| 3587 | .set_idt = vmx_set_idt, |
| 3588 | .get_gdt = vmx_get_gdt, |
| 3589 | .set_gdt = vmx_set_gdt, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3590 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3591 | .get_rflags = vmx_get_rflags, |
| 3592 | .set_rflags = vmx_set_rflags, |
| 3593 | |
| 3594 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3595 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3596 | .run = vmx_vcpu_run, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3597 | .handle_exit = kvm_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3598 | .skip_emulated_instruction = skip_emulated_instruction, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 3599 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 3600 | .get_irq = vmx_get_irq, |
| 3601 | .set_irq = vmx_inject_irq, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3602 | .queue_exception = vmx_queue_exception, |
| 3603 | .exception_injected = vmx_exception_injected, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 3604 | .inject_pending_irq = vmx_intr_assist, |
| 3605 | .inject_pending_vectors = do_interrupt_requests, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 3606 | |
| 3607 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 3608 | .get_tdp_level = get_ept_level, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3609 | }; |
| 3610 | |
| 3611 | static int __init vmx_init(void) |
| 3612 | { |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3613 | void *va; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3614 | int r; |
| 3615 | |
| 3616 | vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 3617 | if (!vmx_io_bitmap_a) |
| 3618 | return -ENOMEM; |
| 3619 | |
| 3620 | vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 3621 | if (!vmx_io_bitmap_b) { |
| 3622 | r = -ENOMEM; |
| 3623 | goto out; |
| 3624 | } |
| 3625 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3626 | vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 3627 | if (!vmx_msr_bitmap) { |
| 3628 | r = -ENOMEM; |
| 3629 | goto out1; |
| 3630 | } |
| 3631 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3632 | /* |
| 3633 | * Allow direct access to the PC debug port (it is often used for I/O |
| 3634 | * delays, but the vmexits simply slow things down). |
| 3635 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3636 | va = kmap(vmx_io_bitmap_a); |
| 3637 | memset(va, 0xff, PAGE_SIZE); |
| 3638 | clear_bit(0x80, va); |
Avi Kivity | cd0536d | 2007-05-08 11:34:07 +0300 | [diff] [blame] | 3639 | kunmap(vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3640 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3641 | va = kmap(vmx_io_bitmap_b); |
| 3642 | memset(va, 0xff, PAGE_SIZE); |
Avi Kivity | cd0536d | 2007-05-08 11:34:07 +0300 | [diff] [blame] | 3643 | kunmap(vmx_io_bitmap_b); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3644 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3645 | va = kmap(vmx_msr_bitmap); |
| 3646 | memset(va, 0xff, PAGE_SIZE); |
| 3647 | kunmap(vmx_msr_bitmap); |
| 3648 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3649 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 3650 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 3651 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3652 | if (r) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3653 | goto out2; |
| 3654 | |
| 3655 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE); |
| 3656 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE); |
| 3657 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS); |
| 3658 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP); |
| 3659 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3660 | |
Sheng Yang | 5fdbcb9 | 2008-07-16 09:25:40 +0800 | [diff] [blame] | 3661 | if (vm_need_ept()) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3662 | bypass_guest_pf = 0; |
Sheng Yang | 5fdbcb9 | 2008-07-16 09:25:40 +0800 | [diff] [blame] | 3663 | kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK | |
| 3664 | VMX_EPT_WRITABLE_MASK | |
Sheng Yang | 928d4bf | 2008-11-06 14:55:45 +0800 | [diff] [blame] | 3665 | VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT | |
| 3666 | VMX_EPT_IGMT_BIT); |
Sheng Yang | 534e38b | 2008-09-08 15:12:30 +0800 | [diff] [blame] | 3667 | kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull, |
Sheng Yang | 5fdbcb9 | 2008-07-16 09:25:40 +0800 | [diff] [blame] | 3668 | VMX_EPT_EXECUTABLE_MASK); |
| 3669 | kvm_enable_tdp(); |
| 3670 | } else |
| 3671 | kvm_disable_tdp(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3672 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 3673 | if (bypass_guest_pf) |
| 3674 | kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull); |
| 3675 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3676 | ept_sync_global(); |
| 3677 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3678 | return 0; |
| 3679 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3680 | out2: |
| 3681 | __free_page(vmx_msr_bitmap); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3682 | out1: |
| 3683 | __free_page(vmx_io_bitmap_b); |
| 3684 | out: |
| 3685 | __free_page(vmx_io_bitmap_a); |
| 3686 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | static void __exit vmx_exit(void) |
| 3690 | { |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3691 | __free_page(vmx_msr_bitmap); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 3692 | __free_page(vmx_io_bitmap_b); |
| 3693 | __free_page(vmx_io_bitmap_a); |
| 3694 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 3695 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | module_init(vmx_init) |
| 3699 | module_exit(vmx_exit) |