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