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