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