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