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