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