Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * AMD SVM support |
| 5 | * |
| 6 | * Copyright (C) 2006 Qumranet, Inc. |
| 7 | * |
| 8 | * Authors: |
| 9 | * Yaniv Kamay <yaniv@qumranet.com> |
| 10 | * Avi Kivity <avi@qumranet.com> |
| 11 | * |
| 12 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 13 | * the COPYING file in the top-level directory. |
| 14 | * |
| 15 | */ |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 16 | #include <linux/kvm_host.h> |
| 17 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 18 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 19 | #include "mmu.h" |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 20 | #include "kvm_cache_regs.h" |
Gleb Natapov | fe4c7b1 | 2009-03-23 11:23:18 +0200 | [diff] [blame] | 21 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 22 | |
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/vmalloc.h> |
| 26 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 27 | #include <linux/sched.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 28 | #include <linux/ftrace_event.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 29 | |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 30 | #include <asm/desc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 31 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 32 | #include <asm/virtext.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 33 | #include "trace.h" |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 34 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 35 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
| 36 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 37 | MODULE_AUTHOR("Qumranet"); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | |
| 40 | #define IOPM_ALLOC_ORDER 2 |
| 41 | #define MSRPM_ALLOC_ORDER 1 |
| 42 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 43 | #define SEG_TYPE_LDT 2 |
| 44 | #define SEG_TYPE_BUSY_TSS16 3 |
| 45 | |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 46 | #define SVM_FEATURE_NPT (1 << 0) |
| 47 | #define SVM_FEATURE_LBRV (1 << 1) |
Amit Shah | 94c935a1 | 2008-08-18 13:11:46 +0300 | [diff] [blame] | 48 | #define SVM_FEATURE_SVML (1 << 2) |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 49 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 50 | #define NESTED_EXIT_HOST 0 /* Exit handled on host level */ |
| 51 | #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */ |
| 52 | #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */ |
| 53 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 54 | #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) |
| 55 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 56 | static const u32 host_save_user_msrs[] = { |
| 57 | #ifdef CONFIG_X86_64 |
| 58 | MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE, |
| 59 | MSR_FS_BASE, |
| 60 | #endif |
| 61 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 62 | }; |
| 63 | |
| 64 | #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) |
| 65 | |
| 66 | struct kvm_vcpu; |
| 67 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 68 | struct nested_state { |
| 69 | struct vmcb *hsave; |
| 70 | u64 hsave_msr; |
| 71 | u64 vmcb; |
| 72 | |
| 73 | /* These are the merged vectors */ |
| 74 | u32 *msrpm; |
| 75 | |
| 76 | /* gpa pointers to the real vectors */ |
| 77 | u64 vmcb_msrpm; |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 78 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 79 | /* A VMEXIT is required but not yet emulated */ |
| 80 | bool exit_required; |
| 81 | |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 82 | /* cache for intercepts of the guest */ |
| 83 | u16 intercept_cr_read; |
| 84 | u16 intercept_cr_write; |
| 85 | u16 intercept_dr_read; |
| 86 | u16 intercept_dr_write; |
| 87 | u32 intercept_exceptions; |
| 88 | u64 intercept; |
| 89 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 92 | struct vcpu_svm { |
| 93 | struct kvm_vcpu vcpu; |
| 94 | struct vmcb *vmcb; |
| 95 | unsigned long vmcb_pa; |
| 96 | struct svm_cpu_data *svm_data; |
| 97 | uint64_t asid_generation; |
| 98 | uint64_t sysenter_esp; |
| 99 | uint64_t sysenter_eip; |
| 100 | |
| 101 | u64 next_rip; |
| 102 | |
| 103 | u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS]; |
| 104 | u64 host_gs_base; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 105 | |
| 106 | u32 *msrpm; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 107 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 108 | struct nested_state nested; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 109 | }; |
| 110 | |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 111 | /* enable NPT for AMD64 and X86 with PAE */ |
| 112 | #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) |
| 113 | static bool npt_enabled = true; |
| 114 | #else |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 115 | static bool npt_enabled = false; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 116 | #endif |
Joerg Roedel | 6c7dac7 | 2008-02-07 13:47:40 +0100 | [diff] [blame] | 117 | static int npt = 1; |
| 118 | |
| 119 | module_param(npt, int, S_IRUGO); |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 120 | |
Joerg Roedel | 4b6e4dc | 2009-08-07 11:49:48 +0200 | [diff] [blame] | 121 | static int nested = 1; |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 122 | module_param(nested, int, S_IRUGO); |
| 123 | |
Joerg Roedel | 44874f8 | 2008-08-27 14:18:43 +0200 | [diff] [blame] | 124 | static void svm_flush_tlb(struct kvm_vcpu *vcpu); |
Joerg Roedel | a5c3832 | 2009-08-07 11:49:32 +0200 | [diff] [blame] | 125 | static void svm_complete_interrupts(struct vcpu_svm *svm); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 126 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 127 | static int nested_svm_exit_handled(struct vcpu_svm *svm); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 128 | static int nested_svm_vmexit(struct vcpu_svm *svm); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 129 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 130 | bool has_error_code, u32 error_code); |
| 131 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 132 | static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) |
| 133 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 134 | return container_of(vcpu, struct vcpu_svm, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 137 | static inline bool is_nested(struct vcpu_svm *svm) |
| 138 | { |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 139 | return svm->nested.vmcb; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 142 | static inline void enable_gif(struct vcpu_svm *svm) |
| 143 | { |
| 144 | svm->vcpu.arch.hflags |= HF_GIF_MASK; |
| 145 | } |
| 146 | |
| 147 | static inline void disable_gif(struct vcpu_svm *svm) |
| 148 | { |
| 149 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; |
| 150 | } |
| 151 | |
| 152 | static inline bool gif_set(struct vcpu_svm *svm) |
| 153 | { |
| 154 | return !!(svm->vcpu.arch.hflags & HF_GIF_MASK); |
| 155 | } |
| 156 | |
Harvey Harrison | 4866d5e | 2008-02-19 10:32:02 -0800 | [diff] [blame] | 157 | static unsigned long iopm_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 158 | |
| 159 | struct kvm_ldttss_desc { |
| 160 | u16 limit0; |
| 161 | u16 base0; |
| 162 | unsigned base1 : 8, type : 5, dpl : 2, p : 1; |
| 163 | unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8; |
| 164 | u32 base3; |
| 165 | u32 zero1; |
| 166 | } __attribute__((packed)); |
| 167 | |
| 168 | struct svm_cpu_data { |
| 169 | int cpu; |
| 170 | |
Avi Kivity | 5008fdf | 2007-04-02 13:05:50 +0300 | [diff] [blame] | 171 | u64 asid_generation; |
| 172 | u32 max_asid; |
| 173 | u32 next_asid; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 174 | struct kvm_ldttss_desc *tss_desc; |
| 175 | |
| 176 | struct page *save_area; |
| 177 | }; |
| 178 | |
| 179 | static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 180 | static uint32_t svm_features; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 181 | |
| 182 | struct svm_init_data { |
| 183 | int cpu; |
| 184 | int r; |
| 185 | }; |
| 186 | |
| 187 | static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; |
| 188 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 189 | #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 190 | #define MSRS_RANGE_SIZE 2048 |
| 191 | #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) |
| 192 | |
| 193 | #define MAX_INST_SIZE 15 |
| 194 | |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 195 | static inline u32 svm_has(u32 feat) |
| 196 | { |
| 197 | return svm_features & feat; |
| 198 | } |
| 199 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 200 | static inline void clgi(void) |
| 201 | { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 202 | asm volatile (__ex(SVM_CLGI)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static inline void stgi(void) |
| 206 | { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 207 | asm volatile (__ex(SVM_STGI)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static inline void invlpga(unsigned long addr, u32 asid) |
| 211 | { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 212 | asm volatile (__ex(SVM_INVLPGA) :: "a"(addr), "c"(asid)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 215 | static inline void force_new_asid(struct kvm_vcpu *vcpu) |
| 216 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 217 | to_svm(vcpu)->asid_generation--; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static inline void flush_guest_tlb(struct kvm_vcpu *vcpu) |
| 221 | { |
| 222 | force_new_asid(vcpu); |
| 223 | } |
| 224 | |
| 225 | static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 226 | { |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 227 | if (!npt_enabled && !(efer & EFER_LMA)) |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 228 | efer &= ~EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 229 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 230 | to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 231 | vcpu->arch.shadow_efer = efer; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 234 | static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
| 235 | bool has_error_code, u32 error_code) |
| 236 | { |
| 237 | struct vcpu_svm *svm = to_svm(vcpu); |
| 238 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 239 | /* If we are within a nested VM we'd better #VMEXIT and let the |
| 240 | guest handle the exception */ |
| 241 | if (nested_svm_check_exception(svm, nr, has_error_code, error_code)) |
| 242 | return; |
| 243 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 244 | svm->vmcb->control.event_inj = nr |
| 245 | | SVM_EVTINJ_VALID |
| 246 | | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) |
| 247 | | SVM_EVTINJ_TYPE_EXEPT; |
| 248 | svm->vmcb->control.event_inj_err = error_code; |
| 249 | } |
| 250 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 251 | static int is_external_interrupt(u32 info) |
| 252 | { |
| 253 | info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; |
| 254 | return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); |
| 255 | } |
| 256 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 257 | static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 258 | { |
| 259 | struct vcpu_svm *svm = to_svm(vcpu); |
| 260 | u32 ret = 0; |
| 261 | |
| 262 | if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) |
| 263 | ret |= X86_SHADOW_INT_STI | X86_SHADOW_INT_MOV_SS; |
| 264 | return ret & mask; |
| 265 | } |
| 266 | |
| 267 | static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 268 | { |
| 269 | struct vcpu_svm *svm = to_svm(vcpu); |
| 270 | |
| 271 | if (mask == 0) |
| 272 | svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; |
| 273 | else |
| 274 | svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK; |
| 275 | |
| 276 | } |
| 277 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 278 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 279 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 280 | struct vcpu_svm *svm = to_svm(vcpu); |
| 281 | |
| 282 | if (!svm->next_rip) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 283 | if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) != |
Gleb Natapov | f629cf8 | 2009-05-11 13:35:49 +0300 | [diff] [blame] | 284 | EMULATE_DONE) |
| 285 | printk(KERN_DEBUG "%s: NOP\n", __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 286 | return; |
| 287 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 288 | if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE) |
| 289 | printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n", |
| 290 | __func__, kvm_rip_read(vcpu), svm->next_rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 291 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 292 | kvm_rip_write(vcpu, svm->next_rip); |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 293 | svm_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static int has_svm(void) |
| 297 | { |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 298 | const char *msg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 299 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 300 | if (!cpu_has_svm(&msg)) { |
Joe Perches | ff81ff1 | 2009-01-08 11:05:17 -0800 | [diff] [blame] | 301 | printk(KERN_INFO "has_svm: %s\n", msg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | static void svm_hardware_disable(void *garbage) |
| 309 | { |
Eduardo Habkost | 2c8dcee | 2008-11-17 19:03:21 -0200 | [diff] [blame] | 310 | cpu_svm_disable(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 313 | static int svm_hardware_enable(void *garbage) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 314 | { |
| 315 | |
| 316 | struct svm_cpu_data *svm_data; |
| 317 | uint64_t efer; |
Akinobu Mita | b792c34 | 2009-07-19 00:00:01 +0900 | [diff] [blame] | 318 | struct descriptor_table gdt_descr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 319 | struct desc_struct *gdt; |
| 320 | int me = raw_smp_processor_id(); |
| 321 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 322 | rdmsrl(MSR_EFER, efer); |
| 323 | if (efer & EFER_SVME) |
| 324 | return -EBUSY; |
| 325 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 326 | if (!has_svm()) { |
Zachary Amsden | e6732a5 | 2009-09-29 11:38:36 -1000 | [diff] [blame] | 327 | printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n", |
| 328 | me); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 329 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 330 | } |
| 331 | svm_data = per_cpu(svm_data, me); |
| 332 | |
| 333 | if (!svm_data) { |
Zachary Amsden | e6732a5 | 2009-09-29 11:38:36 -1000 | [diff] [blame] | 334 | printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 335 | me); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 336 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | svm_data->asid_generation = 1; |
| 340 | svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; |
| 341 | svm_data->next_asid = svm_data->max_asid + 1; |
| 342 | |
Akinobu Mita | b792c34 | 2009-07-19 00:00:01 +0900 | [diff] [blame] | 343 | kvm_get_gdt(&gdt_descr); |
| 344 | gdt = (struct desc_struct *)gdt_descr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 345 | svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); |
| 346 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 347 | wrmsrl(MSR_EFER, efer | EFER_SVME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 348 | |
| 349 | wrmsrl(MSR_VM_HSAVE_PA, |
| 350 | page_to_pfn(svm_data->save_area) << PAGE_SHIFT); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 351 | |
| 352 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 355 | static void svm_cpu_uninit(int cpu) |
| 356 | { |
| 357 | struct svm_cpu_data *svm_data |
| 358 | = per_cpu(svm_data, raw_smp_processor_id()); |
| 359 | |
| 360 | if (!svm_data) |
| 361 | return; |
| 362 | |
| 363 | per_cpu(svm_data, raw_smp_processor_id()) = NULL; |
| 364 | __free_page(svm_data->save_area); |
| 365 | kfree(svm_data); |
| 366 | } |
| 367 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 368 | static int svm_cpu_init(int cpu) |
| 369 | { |
| 370 | struct svm_cpu_data *svm_data; |
| 371 | int r; |
| 372 | |
| 373 | svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); |
| 374 | if (!svm_data) |
| 375 | return -ENOMEM; |
| 376 | svm_data->cpu = cpu; |
| 377 | svm_data->save_area = alloc_page(GFP_KERNEL); |
| 378 | r = -ENOMEM; |
| 379 | if (!svm_data->save_area) |
| 380 | goto err_1; |
| 381 | |
| 382 | per_cpu(svm_data, cpu) = svm_data; |
| 383 | |
| 384 | return 0; |
| 385 | |
| 386 | err_1: |
| 387 | kfree(svm_data); |
| 388 | return r; |
| 389 | |
| 390 | } |
| 391 | |
Rusty Russell | bfc733a | 2007-07-31 20:42:42 +1000 | [diff] [blame] | 392 | static void set_msr_interception(u32 *msrpm, unsigned msr, |
| 393 | int read, int write) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 394 | { |
| 395 | int i; |
| 396 | |
| 397 | for (i = 0; i < NUM_MSR_MAPS; i++) { |
| 398 | if (msr >= msrpm_ranges[i] && |
| 399 | msr < msrpm_ranges[i] + MSRS_IN_RANGE) { |
| 400 | u32 msr_offset = (i * MSRS_IN_RANGE + msr - |
| 401 | msrpm_ranges[i]) * 2; |
| 402 | |
| 403 | u32 *base = msrpm + (msr_offset / 32); |
| 404 | u32 msr_shift = msr_offset % 32; |
| 405 | u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1); |
| 406 | *base = (*base & ~(0x3 << msr_shift)) | |
| 407 | (mask << msr_shift); |
Rusty Russell | bfc733a | 2007-07-31 20:42:42 +1000 | [diff] [blame] | 408 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
Rusty Russell | bfc733a | 2007-07-31 20:42:42 +1000 | [diff] [blame] | 411 | BUG(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 414 | static void svm_vcpu_init_msrpm(u32 *msrpm) |
| 415 | { |
| 416 | memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER)); |
| 417 | |
| 418 | #ifdef CONFIG_X86_64 |
| 419 | set_msr_interception(msrpm, MSR_GS_BASE, 1, 1); |
| 420 | set_msr_interception(msrpm, MSR_FS_BASE, 1, 1); |
| 421 | set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1); |
| 422 | set_msr_interception(msrpm, MSR_LSTAR, 1, 1); |
| 423 | set_msr_interception(msrpm, MSR_CSTAR, 1, 1); |
| 424 | set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1); |
| 425 | #endif |
| 426 | set_msr_interception(msrpm, MSR_K6_STAR, 1, 1); |
| 427 | set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 430 | static void svm_enable_lbrv(struct vcpu_svm *svm) |
| 431 | { |
| 432 | u32 *msrpm = svm->msrpm; |
| 433 | |
| 434 | svm->vmcb->control.lbr_ctl = 1; |
| 435 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); |
| 436 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); |
| 437 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); |
| 438 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1); |
| 439 | } |
| 440 | |
| 441 | static void svm_disable_lbrv(struct vcpu_svm *svm) |
| 442 | { |
| 443 | u32 *msrpm = svm->msrpm; |
| 444 | |
| 445 | svm->vmcb->control.lbr_ctl = 0; |
| 446 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0); |
| 447 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0); |
| 448 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0); |
| 449 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0); |
| 450 | } |
| 451 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 452 | static __init int svm_hardware_setup(void) |
| 453 | { |
| 454 | int cpu; |
| 455 | struct page *iopm_pages; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 456 | void *iopm_va; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 457 | int r; |
| 458 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 459 | iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER); |
| 460 | |
| 461 | if (!iopm_pages) |
| 462 | return -ENOMEM; |
Anthony Liguori | c868133 | 2007-04-30 09:48:11 +0300 | [diff] [blame] | 463 | |
| 464 | iopm_va = page_address(iopm_pages); |
| 465 | memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 466 | iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; |
| 467 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 468 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 469 | kvm_enable_efer_bits(EFER_NX); |
| 470 | |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 471 | if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) |
| 472 | kvm_enable_efer_bits(EFER_FFXSR); |
| 473 | |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 474 | if (nested) { |
| 475 | printk(KERN_INFO "kvm: Nested Virtualization enabled\n"); |
| 476 | kvm_enable_efer_bits(EFER_SVME); |
| 477 | } |
| 478 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 479 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 480 | r = svm_cpu_init(cpu); |
| 481 | if (r) |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 482 | goto err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 483 | } |
Joerg Roedel | 33bd6a0 | 2008-02-07 13:47:38 +0100 | [diff] [blame] | 484 | |
| 485 | svm_features = cpuid_edx(SVM_CPUID_FUNC); |
| 486 | |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 487 | if (!svm_has(SVM_FEATURE_NPT)) |
| 488 | npt_enabled = false; |
| 489 | |
Joerg Roedel | 6c7dac7 | 2008-02-07 13:47:40 +0100 | [diff] [blame] | 490 | if (npt_enabled && !npt) { |
| 491 | printk(KERN_INFO "kvm: Nested Paging disabled\n"); |
| 492 | npt_enabled = false; |
| 493 | } |
| 494 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 495 | if (npt_enabled) { |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 496 | printk(KERN_INFO "kvm: Nested Paging enabled\n"); |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 497 | kvm_enable_tdp(); |
Joerg Roedel | 5f4cb66 | 2008-07-14 20:36:36 +0200 | [diff] [blame] | 498 | } else |
| 499 | kvm_disable_tdp(); |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 500 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 501 | return 0; |
| 502 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 503 | err: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 504 | __free_pages(iopm_pages, IOPM_ALLOC_ORDER); |
| 505 | iopm_base = 0; |
| 506 | return r; |
| 507 | } |
| 508 | |
| 509 | static __exit void svm_hardware_unsetup(void) |
| 510 | { |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 511 | int cpu; |
| 512 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 513 | for_each_possible_cpu(cpu) |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 514 | svm_cpu_uninit(cpu); |
| 515 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 516 | __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 517 | iopm_base = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static void init_seg(struct vmcb_seg *seg) |
| 521 | { |
| 522 | seg->selector = 0; |
| 523 | seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | |
| 524 | SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ |
| 525 | seg->limit = 0xffff; |
| 526 | seg->base = 0; |
| 527 | } |
| 528 | |
| 529 | static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) |
| 530 | { |
| 531 | seg->selector = 0; |
| 532 | seg->attrib = SVM_SELECTOR_P_MASK | type; |
| 533 | seg->limit = 0xffff; |
| 534 | seg->base = 0; |
| 535 | } |
| 536 | |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 537 | static void init_vmcb(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 538 | { |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 539 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 540 | struct vmcb_save_area *save = &svm->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 541 | |
| 542 | control->intercept_cr_read = INTERCEPT_CR0_MASK | |
| 543 | INTERCEPT_CR3_MASK | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 544 | INTERCEPT_CR4_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 545 | |
| 546 | control->intercept_cr_write = INTERCEPT_CR0_MASK | |
| 547 | INTERCEPT_CR3_MASK | |
Avi Kivity | 80a8119 | 2007-12-06 19:50:00 +0200 | [diff] [blame] | 548 | INTERCEPT_CR4_MASK | |
| 549 | INTERCEPT_CR8_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 550 | |
| 551 | control->intercept_dr_read = INTERCEPT_DR0_MASK | |
| 552 | INTERCEPT_DR1_MASK | |
| 553 | INTERCEPT_DR2_MASK | |
| 554 | INTERCEPT_DR3_MASK; |
| 555 | |
| 556 | control->intercept_dr_write = INTERCEPT_DR0_MASK | |
| 557 | INTERCEPT_DR1_MASK | |
| 558 | INTERCEPT_DR2_MASK | |
| 559 | INTERCEPT_DR3_MASK | |
| 560 | INTERCEPT_DR5_MASK | |
| 561 | INTERCEPT_DR7_MASK; |
| 562 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 563 | control->intercept_exceptions = (1 << PF_VECTOR) | |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 564 | (1 << UD_VECTOR) | |
| 565 | (1 << MC_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 566 | |
| 567 | |
| 568 | control->intercept = (1ULL << INTERCEPT_INTR) | |
| 569 | (1ULL << INTERCEPT_NMI) | |
Joerg Roedel | 0152527 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 570 | (1ULL << INTERCEPT_SMI) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 571 | (1ULL << INTERCEPT_CPUID) | |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 572 | (1ULL << INTERCEPT_INVD) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 573 | (1ULL << INTERCEPT_HLT) | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 574 | (1ULL << INTERCEPT_INVLPG) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 575 | (1ULL << INTERCEPT_INVLPGA) | |
| 576 | (1ULL << INTERCEPT_IOIO_PROT) | |
| 577 | (1ULL << INTERCEPT_MSR_PROT) | |
| 578 | (1ULL << INTERCEPT_TASK_SWITCH) | |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 579 | (1ULL << INTERCEPT_SHUTDOWN) | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 580 | (1ULL << INTERCEPT_VMRUN) | |
| 581 | (1ULL << INTERCEPT_VMMCALL) | |
| 582 | (1ULL << INTERCEPT_VMLOAD) | |
| 583 | (1ULL << INTERCEPT_VMSAVE) | |
| 584 | (1ULL << INTERCEPT_STGI) | |
| 585 | (1ULL << INTERCEPT_CLGI) | |
Joerg Roedel | 916ce23 | 2007-03-21 19:47:00 +0100 | [diff] [blame] | 586 | (1ULL << INTERCEPT_SKINIT) | |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 587 | (1ULL << INTERCEPT_WBINVD) | |
Joerg Roedel | 916ce23 | 2007-03-21 19:47:00 +0100 | [diff] [blame] | 588 | (1ULL << INTERCEPT_MONITOR) | |
| 589 | (1ULL << INTERCEPT_MWAIT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 590 | |
| 591 | control->iopm_base_pa = iopm_base; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 592 | control->msrpm_base_pa = __pa(svm->msrpm); |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 593 | control->tsc_offset = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 594 | control->int_ctl = V_INTR_MASKING_MASK; |
| 595 | |
| 596 | init_seg(&save->es); |
| 597 | init_seg(&save->ss); |
| 598 | init_seg(&save->ds); |
| 599 | init_seg(&save->fs); |
| 600 | init_seg(&save->gs); |
| 601 | |
| 602 | save->cs.selector = 0xf000; |
| 603 | /* Executable/Readable Code Segment */ |
| 604 | save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | |
| 605 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; |
| 606 | save->cs.limit = 0xffff; |
Avi Kivity | d92899a | 2007-02-12 00:54:38 -0800 | [diff] [blame] | 607 | /* |
| 608 | * cs.base should really be 0xffff0000, but vmx can't handle that, so |
| 609 | * be consistent with it. |
| 610 | * |
| 611 | * Replace when we have real mode working for vmx. |
| 612 | */ |
| 613 | save->cs.base = 0xf0000; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 614 | |
| 615 | save->gdtr.limit = 0xffff; |
| 616 | save->idtr.limit = 0xffff; |
| 617 | |
| 618 | init_sys_seg(&save->ldtr, SEG_TYPE_LDT); |
| 619 | init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); |
| 620 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 621 | save->efer = EFER_SVME; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 622 | save->dr6 = 0xffff0ff0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 623 | save->dr7 = 0x400; |
| 624 | save->rflags = 2; |
| 625 | save->rip = 0x0000fff0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 626 | svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 627 | |
| 628 | /* |
| 629 | * cr0 val on cpu init should be 0x60000010, we enable cpu |
| 630 | * cache by default. the orderly way is to enable cache in bios. |
| 631 | */ |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 632 | save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP; |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 633 | save->cr4 = X86_CR4_PAE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 634 | /* rdx = ?? */ |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 635 | |
| 636 | if (npt_enabled) { |
| 637 | /* Setup VMCB for Nested Paging */ |
| 638 | control->nested_ctl = 1; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 639 | control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) | |
| 640 | (1ULL << INTERCEPT_INVLPG)); |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 641 | control->intercept_exceptions &= ~(1 << PF_VECTOR); |
| 642 | control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK| |
| 643 | INTERCEPT_CR3_MASK); |
| 644 | control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK| |
| 645 | INTERCEPT_CR3_MASK); |
| 646 | save->g_pat = 0x0007040600070406ULL; |
| 647 | /* enable caching because the QEMU Bios doesn't enable it */ |
| 648 | save->cr0 = X86_CR0_ET; |
| 649 | save->cr3 = 0; |
| 650 | save->cr4 = 0; |
| 651 | } |
Avi Kivity | a79d2f1 | 2008-04-14 13:10:21 +0300 | [diff] [blame] | 652 | force_new_asid(&svm->vcpu); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 653 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 654 | svm->nested.vmcb = 0; |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 655 | svm->vcpu.arch.hflags = 0; |
| 656 | |
| 657 | enable_gif(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 660 | static int svm_vcpu_reset(struct kvm_vcpu *vcpu) |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 661 | { |
| 662 | struct vcpu_svm *svm = to_svm(vcpu); |
| 663 | |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 664 | init_vmcb(svm); |
Avi Kivity | 7043338 | 2007-11-07 12:57:23 +0200 | [diff] [blame] | 665 | |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 666 | if (!kvm_vcpu_is_bsp(vcpu)) { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 667 | kvm_rip_write(vcpu, 0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 668 | svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12; |
| 669 | svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8; |
Avi Kivity | 7043338 | 2007-11-07 12:57:23 +0200 | [diff] [blame] | 670 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 671 | vcpu->arch.regs_avail = ~0; |
| 672 | vcpu->arch.regs_dirty = ~0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 673 | |
| 674 | return 0; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 675 | } |
| 676 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 677 | static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 678 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 679 | struct vcpu_svm *svm; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 680 | struct page *page; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 681 | struct page *msrpm_pages; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 682 | struct page *hsave_page; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 683 | struct page *nested_msrpm_pages; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 684 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 685 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 686 | svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 687 | if (!svm) { |
| 688 | err = -ENOMEM; |
| 689 | goto out; |
| 690 | } |
| 691 | |
| 692 | err = kvm_vcpu_init(&svm->vcpu, kvm, id); |
| 693 | if (err) |
| 694 | goto free_svm; |
| 695 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 696 | page = alloc_page(GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 697 | if (!page) { |
| 698 | err = -ENOMEM; |
| 699 | goto uninit; |
| 700 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 701 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 702 | err = -ENOMEM; |
| 703 | msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 704 | if (!msrpm_pages) |
| 705 | goto uninit; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 706 | |
| 707 | nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 708 | if (!nested_msrpm_pages) |
| 709 | goto uninit; |
| 710 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 711 | svm->msrpm = page_address(msrpm_pages); |
| 712 | svm_vcpu_init_msrpm(svm->msrpm); |
| 713 | |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 714 | hsave_page = alloc_page(GFP_KERNEL); |
| 715 | if (!hsave_page) |
| 716 | goto uninit; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 717 | svm->nested.hsave = page_address(hsave_page); |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 718 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 719 | svm->nested.msrpm = page_address(nested_msrpm_pages); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 720 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 721 | svm->vmcb = page_address(page); |
| 722 | clear_page(svm->vmcb); |
| 723 | svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT; |
| 724 | svm->asid_generation = 0; |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 725 | init_vmcb(svm); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 726 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 727 | fx_init(&svm->vcpu); |
| 728 | svm->vcpu.fpu_active = 1; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 729 | svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 730 | if (kvm_vcpu_is_bsp(&svm->vcpu)) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 731 | svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 732 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 733 | return &svm->vcpu; |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 734 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 735 | uninit: |
| 736 | kvm_vcpu_uninit(&svm->vcpu); |
| 737 | free_svm: |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 738 | kmem_cache_free(kvm_vcpu_cache, svm); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 739 | out: |
| 740 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | static void svm_free_vcpu(struct kvm_vcpu *vcpu) |
| 744 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 745 | struct vcpu_svm *svm = to_svm(vcpu); |
| 746 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 747 | __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT)); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 748 | __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER); |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 749 | __free_page(virt_to_page(svm->nested.hsave)); |
| 750 | __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 751 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 752 | kmem_cache_free(kvm_vcpu_cache, svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 753 | } |
| 754 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 755 | static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 756 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 757 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 758 | int i; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 759 | |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 760 | if (unlikely(cpu != vcpu->cpu)) { |
Joerg Roedel | e935d48 | 2009-09-16 15:24:19 +0200 | [diff] [blame] | 761 | u64 delta; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 762 | |
| 763 | /* |
| 764 | * Make sure that the guest sees a monotonically |
| 765 | * increasing TSC. |
| 766 | */ |
Joerg Roedel | e935d48 | 2009-09-16 15:24:19 +0200 | [diff] [blame] | 767 | delta = vcpu->arch.host_tsc - native_read_tsc(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 768 | svm->vmcb->control.tsc_offset += delta; |
Joerg Roedel | 77b1ab1 | 2009-09-16 15:24:17 +0200 | [diff] [blame] | 769 | if (is_nested(svm)) |
| 770 | svm->nested.hsave->control.tsc_offset += delta; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 771 | vcpu->cpu = cpu; |
Marcelo Tosatti | 2f59971 | 2008-05-27 12:10:20 -0300 | [diff] [blame] | 772 | kvm_migrate_timers(vcpu); |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 773 | svm->asid_generation = 0; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 774 | } |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 775 | |
| 776 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 777 | rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static void svm_vcpu_put(struct kvm_vcpu *vcpu) |
| 781 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 782 | struct vcpu_svm *svm = to_svm(vcpu); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 783 | int i; |
| 784 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 785 | ++vcpu->stat.host_state_reload; |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 786 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 787 | wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 788 | |
Joerg Roedel | e935d48 | 2009-09-16 15:24:19 +0200 | [diff] [blame] | 789 | vcpu->arch.host_tsc = native_read_tsc(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 790 | } |
| 791 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 792 | static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) |
| 793 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 794 | return to_svm(vcpu)->vmcb->save.rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 798 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 799 | to_svm(vcpu)->vmcb->save.rflags = rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 800 | } |
| 801 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 802 | static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
| 803 | { |
| 804 | switch (reg) { |
| 805 | case VCPU_EXREG_PDPTR: |
| 806 | BUG_ON(!npt_enabled); |
| 807 | load_pdptrs(vcpu, vcpu->arch.cr3); |
| 808 | break; |
| 809 | default: |
| 810 | BUG(); |
| 811 | } |
| 812 | } |
| 813 | |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 814 | static void svm_set_vintr(struct vcpu_svm *svm) |
| 815 | { |
| 816 | svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR; |
| 817 | } |
| 818 | |
| 819 | static void svm_clear_vintr(struct vcpu_svm *svm) |
| 820 | { |
| 821 | svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR); |
| 822 | } |
| 823 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 824 | static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) |
| 825 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 826 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 827 | |
| 828 | switch (seg) { |
| 829 | case VCPU_SREG_CS: return &save->cs; |
| 830 | case VCPU_SREG_DS: return &save->ds; |
| 831 | case VCPU_SREG_ES: return &save->es; |
| 832 | case VCPU_SREG_FS: return &save->fs; |
| 833 | case VCPU_SREG_GS: return &save->gs; |
| 834 | case VCPU_SREG_SS: return &save->ss; |
| 835 | case VCPU_SREG_TR: return &save->tr; |
| 836 | case VCPU_SREG_LDTR: return &save->ldtr; |
| 837 | } |
| 838 | BUG(); |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 839 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 843 | { |
| 844 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 845 | |
| 846 | return s->base; |
| 847 | } |
| 848 | |
| 849 | static void svm_get_segment(struct kvm_vcpu *vcpu, |
| 850 | struct kvm_segment *var, int seg) |
| 851 | { |
| 852 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 853 | |
| 854 | var->base = s->base; |
| 855 | var->limit = s->limit; |
| 856 | var->selector = s->selector; |
| 857 | var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; |
| 858 | var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; |
| 859 | var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 860 | var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; |
| 861 | var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; |
| 862 | var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; |
| 863 | var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; |
| 864 | var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1; |
Amit Shah | 25022ac | 2008-10-27 09:04:17 +0000 | [diff] [blame] | 865 | |
Andre Przywara | 19bca6a | 2009-04-28 12:45:30 +0200 | [diff] [blame] | 866 | /* AMD's VMCB does not have an explicit unusable field, so emulate it |
| 867 | * for cross vendor migration purposes by "not present" |
| 868 | */ |
| 869 | var->unusable = !var->present || (var->type == 0); |
| 870 | |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 871 | switch (seg) { |
| 872 | case VCPU_SREG_CS: |
| 873 | /* |
| 874 | * SVM always stores 0 for the 'G' bit in the CS selector in |
| 875 | * the VMCB on a VMEXIT. This hurts cross-vendor migration: |
| 876 | * Intel's VMENTRY has a check on the 'G' bit. |
| 877 | */ |
Amit Shah | 25022ac | 2008-10-27 09:04:17 +0000 | [diff] [blame] | 878 | var->g = s->limit > 0xfffff; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 879 | break; |
| 880 | case VCPU_SREG_TR: |
| 881 | /* |
| 882 | * Work around a bug where the busy flag in the tr selector |
| 883 | * isn't exposed |
| 884 | */ |
Amit Shah | c0d0982 | 2008-10-27 09:04:18 +0000 | [diff] [blame] | 885 | var->type |= 0x2; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 886 | break; |
| 887 | case VCPU_SREG_DS: |
| 888 | case VCPU_SREG_ES: |
| 889 | case VCPU_SREG_FS: |
| 890 | case VCPU_SREG_GS: |
| 891 | /* |
| 892 | * The accessed bit must always be set in the segment |
| 893 | * descriptor cache, although it can be cleared in the |
| 894 | * descriptor, the cached bit always remains at 1. Since |
| 895 | * Intel has a check on this, set it here to support |
| 896 | * cross-vendor migration. |
| 897 | */ |
| 898 | if (!var->unusable) |
| 899 | var->type |= 0x1; |
| 900 | break; |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 901 | case VCPU_SREG_SS: |
| 902 | /* On AMD CPUs sometimes the DB bit in the segment |
| 903 | * descriptor is left as 1, although the whole segment has |
| 904 | * been made unusable. Clear it here to pass an Intel VMX |
| 905 | * entry check when cross vendor migrating. |
| 906 | */ |
| 907 | if (var->unusable) |
| 908 | var->db = 0; |
| 909 | break; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 910 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 911 | } |
| 912 | |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 913 | static int svm_get_cpl(struct kvm_vcpu *vcpu) |
| 914 | { |
| 915 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
| 916 | |
| 917 | return save->cpl; |
| 918 | } |
| 919 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 920 | static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 921 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 922 | struct vcpu_svm *svm = to_svm(vcpu); |
| 923 | |
| 924 | dt->limit = svm->vmcb->save.idtr.limit; |
| 925 | dt->base = svm->vmcb->save.idtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 929 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 930 | struct vcpu_svm *svm = to_svm(vcpu); |
| 931 | |
| 932 | svm->vmcb->save.idtr.limit = dt->limit; |
| 933 | svm->vmcb->save.idtr.base = dt->base ; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 937 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 938 | struct vcpu_svm *svm = to_svm(vcpu); |
| 939 | |
| 940 | dt->limit = svm->vmcb->save.gdtr.limit; |
| 941 | dt->base = svm->vmcb->save.gdtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 945 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 946 | struct vcpu_svm *svm = to_svm(vcpu); |
| 947 | |
| 948 | svm->vmcb->save.gdtr.limit = dt->limit; |
| 949 | svm->vmcb->save.gdtr.base = dt->base ; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 950 | } |
| 951 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 952 | static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 953 | { |
| 954 | } |
| 955 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 956 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 957 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 958 | struct vcpu_svm *svm = to_svm(vcpu); |
| 959 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 960 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 961 | if (vcpu->arch.shadow_efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 962 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 963 | vcpu->arch.shadow_efer |= EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 964 | svm->vmcb->save.efer |= EFER_LMA | EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 965 | } |
| 966 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 967 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 968 | vcpu->arch.shadow_efer &= ~EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 969 | svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | #endif |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 973 | if (npt_enabled) |
| 974 | goto set; |
| 975 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 976 | if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 977 | svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR); |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 978 | vcpu->fpu_active = 1; |
| 979 | } |
| 980 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 981 | vcpu->arch.cr0 = cr0; |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 982 | cr0 |= X86_CR0_PG | X86_CR0_WP; |
Joerg Roedel | 6b390b6 | 2008-01-29 13:01:27 +0100 | [diff] [blame] | 983 | if (!vcpu->fpu_active) { |
| 984 | svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR); |
Joerg Roedel | 334df50 | 2008-01-21 13:09:33 +0100 | [diff] [blame] | 985 | cr0 |= X86_CR0_TS; |
Joerg Roedel | 6b390b6 | 2008-01-29 13:01:27 +0100 | [diff] [blame] | 986 | } |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 987 | set: |
| 988 | /* |
| 989 | * re-enable caching here because the QEMU bios |
| 990 | * does not do it - this results in some delay at |
| 991 | * reboot |
| 992 | */ |
| 993 | cr0 &= ~(X86_CR0_CD | X86_CR0_NW); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 994 | svm->vmcb->save.cr0 = cr0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 998 | { |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 999 | unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE; |
Joerg Roedel | e5eab0c | 2008-09-09 19:11:51 +0200 | [diff] [blame] | 1000 | unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4; |
| 1001 | |
| 1002 | if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE)) |
| 1003 | force_new_asid(vcpu); |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 1004 | |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 1005 | vcpu->arch.cr4 = cr4; |
| 1006 | if (!npt_enabled) |
| 1007 | cr4 |= X86_CR4_PAE; |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 1008 | cr4 |= host_cr4_mce; |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 1009 | to_svm(vcpu)->vmcb->save.cr4 = cr4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | static void svm_set_segment(struct kvm_vcpu *vcpu, |
| 1013 | struct kvm_segment *var, int seg) |
| 1014 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1015 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1016 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 1017 | |
| 1018 | s->base = var->base; |
| 1019 | s->limit = var->limit; |
| 1020 | s->selector = var->selector; |
| 1021 | if (var->unusable) |
| 1022 | s->attrib = 0; |
| 1023 | else { |
| 1024 | s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); |
| 1025 | s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; |
| 1026 | s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; |
| 1027 | s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT; |
| 1028 | s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; |
| 1029 | s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; |
| 1030 | s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; |
| 1031 | s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; |
| 1032 | } |
| 1033 | if (seg == VCPU_SREG_CS) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1034 | svm->vmcb->save.cpl |
| 1035 | = (svm->vmcb->save.cs.attrib |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1036 | >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 1037 | |
| 1038 | } |
| 1039 | |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1040 | static void update_db_intercept(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1041 | { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1042 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1043 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1044 | svm->vmcb->control.intercept_exceptions &= |
| 1045 | ~((1 << DB_VECTOR) | (1 << BP_VECTOR)); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1046 | |
| 1047 | if (vcpu->arch.singlestep) |
| 1048 | svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR); |
| 1049 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1050 | if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) { |
| 1051 | if (vcpu->guest_debug & |
| 1052 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 1053 | svm->vmcb->control.intercept_exceptions |= |
| 1054 | 1 << DB_VECTOR; |
| 1055 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 1056 | svm->vmcb->control.intercept_exceptions |= |
| 1057 | 1 << BP_VECTOR; |
| 1058 | } else |
| 1059 | vcpu->guest_debug = 0; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1060 | } |
| 1061 | |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 1062 | static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg) |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1063 | { |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1064 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1065 | |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1066 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 1067 | svm->vmcb->save.dr7 = dbg->arch.debugreg[7]; |
| 1068 | else |
| 1069 | svm->vmcb->save.dr7 = vcpu->arch.dr7; |
| 1070 | |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 1071 | update_db_intercept(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | static void load_host_msrs(struct kvm_vcpu *vcpu) |
| 1075 | { |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1076 | #ifdef CONFIG_X86_64 |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1077 | wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1078 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static void save_host_msrs(struct kvm_vcpu *vcpu) |
| 1082 | { |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1083 | #ifdef CONFIG_X86_64 |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1084 | rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1085 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1086 | } |
| 1087 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1088 | static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1089 | { |
| 1090 | if (svm_data->next_asid > svm_data->max_asid) { |
| 1091 | ++svm_data->asid_generation; |
| 1092 | svm_data->next_asid = 1; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1093 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1096 | svm->asid_generation = svm_data->asid_generation; |
| 1097 | svm->vmcb->control.asid = svm_data->next_asid++; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1098 | } |
| 1099 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1100 | static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr) |
| 1101 | { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1102 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1103 | unsigned long val; |
| 1104 | |
| 1105 | switch (dr) { |
| 1106 | case 0 ... 3: |
| 1107 | val = vcpu->arch.db[dr]; |
| 1108 | break; |
| 1109 | case 6: |
| 1110 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 1111 | val = vcpu->arch.dr6; |
| 1112 | else |
| 1113 | val = svm->vmcb->save.dr6; |
| 1114 | break; |
| 1115 | case 7: |
| 1116 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 1117 | val = vcpu->arch.dr7; |
| 1118 | else |
| 1119 | val = svm->vmcb->save.dr7; |
| 1120 | break; |
| 1121 | default: |
| 1122 | val = 0; |
| 1123 | } |
| 1124 | |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 1125 | return val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value, |
| 1129 | int *exception) |
| 1130 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1131 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1132 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1133 | *exception = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1134 | |
| 1135 | switch (dr) { |
| 1136 | case 0 ... 3: |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1137 | vcpu->arch.db[dr] = value; |
| 1138 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) |
| 1139 | vcpu->arch.eff_db[dr] = value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1140 | return; |
| 1141 | case 4 ... 5: |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1142 | if (vcpu->arch.cr4 & X86_CR4_DE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1143 | *exception = UD_VECTOR; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1144 | return; |
| 1145 | case 6: |
| 1146 | if (value & 0xffffffff00000000ULL) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1147 | *exception = GP_VECTOR; |
| 1148 | return; |
| 1149 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1150 | vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1151 | return; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1152 | case 7: |
| 1153 | if (value & 0xffffffff00000000ULL) { |
| 1154 | *exception = GP_VECTOR; |
| 1155 | return; |
| 1156 | } |
| 1157 | vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1; |
| 1158 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { |
| 1159 | svm->vmcb->save.dr7 = vcpu->arch.dr7; |
| 1160 | vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK); |
| 1161 | } |
| 1162 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1163 | default: |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1164 | /* FIXME: Possible case? */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1165 | printk(KERN_DEBUG "%s: unexpected dr %u\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1166 | __func__, dr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1167 | *exception = UD_VECTOR; |
| 1168 | return; |
| 1169 | } |
| 1170 | } |
| 1171 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1172 | static int pf_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1173 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1174 | u64 fault_address; |
| 1175 | u32 error_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1176 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1177 | fault_address = svm->vmcb->control.exit_info_2; |
| 1178 | error_code = svm->vmcb->control.exit_info_1; |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 1179 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 1180 | trace_kvm_page_fault(fault_address, error_code); |
Avi Kivity | 52c7847 | 2009-08-10 15:42:41 +0300 | [diff] [blame] | 1181 | if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu)) |
| 1182 | kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 1183 | return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1184 | } |
| 1185 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1186 | static int db_interception(struct vcpu_svm *svm) |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1187 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1188 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 1189 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1190 | if (!(svm->vcpu.guest_debug & |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1191 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) && |
| 1192 | !svm->vcpu.arch.singlestep) { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1193 | kvm_queue_exception(&svm->vcpu, DB_VECTOR); |
| 1194 | return 1; |
| 1195 | } |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 1196 | |
| 1197 | if (svm->vcpu.arch.singlestep) { |
| 1198 | svm->vcpu.arch.singlestep = false; |
| 1199 | if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) |
| 1200 | svm->vmcb->save.rflags &= |
| 1201 | ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 1202 | update_db_intercept(&svm->vcpu); |
| 1203 | } |
| 1204 | |
| 1205 | if (svm->vcpu.guest_debug & |
| 1206 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)){ |
| 1207 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 1208 | kvm_run->debug.arch.pc = |
| 1209 | svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 1210 | kvm_run->debug.arch.exception = DB_VECTOR; |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
| 1214 | return 1; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1215 | } |
| 1216 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1217 | static int bp_interception(struct vcpu_svm *svm) |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1218 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1219 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 1220 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1221 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 1222 | kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 1223 | kvm_run->debug.arch.exception = BP_VECTOR; |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1227 | static int ud_interception(struct vcpu_svm *svm) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1228 | { |
| 1229 | int er; |
| 1230 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1231 | er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1232 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 1233 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1234 | return 1; |
| 1235 | } |
| 1236 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1237 | static int nm_interception(struct vcpu_svm *svm) |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 1238 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1239 | svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1240 | if (!(svm->vcpu.arch.cr0 & X86_CR0_TS)) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1241 | svm->vmcb->save.cr0 &= ~X86_CR0_TS; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1242 | svm->vcpu.fpu_active = 1; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1243 | |
| 1244 | return 1; |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 1245 | } |
| 1246 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1247 | static int mc_interception(struct vcpu_svm *svm) |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 1248 | { |
| 1249 | /* |
| 1250 | * On an #MC intercept the MCE handler is not called automatically in |
| 1251 | * the host. So do it by hand here. |
| 1252 | */ |
| 1253 | asm volatile ( |
| 1254 | "int $0x12\n"); |
| 1255 | /* not sure if we ever come back to this point */ |
| 1256 | |
| 1257 | return 1; |
| 1258 | } |
| 1259 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1260 | static int shutdown_interception(struct vcpu_svm *svm) |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 1261 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1262 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 1263 | |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 1264 | /* |
| 1265 | * VMCB is undefined after a SHUTDOWN intercept |
| 1266 | * so reinitialize it. |
| 1267 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1268 | clear_page(svm->vmcb); |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 1269 | init_vmcb(svm); |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 1270 | |
| 1271 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1275 | static int io_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1276 | { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1277 | u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 1278 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1279 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1280 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1281 | ++svm->vcpu.stat.io_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1282 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1283 | svm->next_rip = svm->vmcb->control.exit_info_2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1284 | |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1285 | string = (io_info & SVM_IOIO_STR_MASK) != 0; |
| 1286 | |
| 1287 | if (string) { |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1288 | if (emulate_instruction(&svm->vcpu, |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1289 | 0, 0, 0) == EMULATE_DO_MMIO) |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 1290 | return 0; |
| 1291 | return 1; |
| 1292 | } |
| 1293 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1294 | in = (io_info & SVM_IOIO_TYPE_MASK) != 0; |
| 1295 | port = io_info >> 16; |
| 1296 | size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1297 | |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 1298 | skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1299 | return kvm_emulate_pio(&svm->vcpu, in, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1302 | static int nmi_interception(struct vcpu_svm *svm) |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 1303 | { |
| 1304 | return 1; |
| 1305 | } |
| 1306 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1307 | static int intr_interception(struct vcpu_svm *svm) |
Joerg Roedel | a069805 | 2008-04-30 17:56:01 +0200 | [diff] [blame] | 1308 | { |
| 1309 | ++svm->vcpu.stat.irq_exits; |
| 1310 | return 1; |
| 1311 | } |
| 1312 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1313 | static int nop_on_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1314 | { |
| 1315 | return 1; |
| 1316 | } |
| 1317 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1318 | static int halt_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1319 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1320 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 1; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1321 | skip_emulated_instruction(&svm->vcpu); |
| 1322 | return kvm_emulate_halt(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1323 | } |
| 1324 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1325 | static int vmmcall_interception(struct vcpu_svm *svm) |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1326 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1327 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 1328 | skip_emulated_instruction(&svm->vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1329 | kvm_emulate_hypercall(&svm->vcpu); |
| 1330 | return 1; |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1331 | } |
| 1332 | |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 1333 | static int nested_svm_check_permissions(struct vcpu_svm *svm) |
| 1334 | { |
| 1335 | if (!(svm->vcpu.arch.shadow_efer & EFER_SVME) |
| 1336 | || !is_paging(&svm->vcpu)) { |
| 1337 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 1338 | return 1; |
| 1339 | } |
| 1340 | |
| 1341 | if (svm->vmcb->save.cpl) { |
| 1342 | kvm_inject_gp(&svm->vcpu, 0); |
| 1343 | return 1; |
| 1344 | } |
| 1345 | |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1349 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 1350 | bool has_error_code, u32 error_code) |
| 1351 | { |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 1352 | if (!is_nested(svm)) |
| 1353 | return 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1354 | |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 1355 | svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; |
| 1356 | svm->vmcb->control.exit_code_hi = 0; |
| 1357 | svm->vmcb->control.exit_info_1 = error_code; |
| 1358 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; |
| 1359 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1360 | return nested_svm_exit_handled(svm); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | static inline int nested_svm_intr(struct vcpu_svm *svm) |
| 1364 | { |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 1365 | if (!is_nested(svm)) |
| 1366 | return 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1367 | |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 1368 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
| 1369 | return 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1370 | |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 1371 | if (!(svm->vcpu.arch.hflags & HF_HIF_MASK)) |
| 1372 | return 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1373 | |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 1374 | svm->vmcb->control.exit_code = SVM_EXIT_INTR; |
| 1375 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 1376 | if (svm->nested.intercept & 1ULL) { |
| 1377 | /* |
| 1378 | * The #vmexit can't be emulated here directly because this |
| 1379 | * code path runs with irqs and preemtion disabled. A |
| 1380 | * #vmexit emulation might sleep. Only signal request for |
| 1381 | * the #vmexit here. |
| 1382 | */ |
| 1383 | svm->nested.exit_required = true; |
Joerg Roedel | 236649d | 2009-10-09 16:08:30 +0200 | [diff] [blame] | 1384 | trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 1385 | return 1; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1391 | static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, enum km_type idx) |
| 1392 | { |
| 1393 | struct page *page; |
| 1394 | |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1395 | page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1396 | if (is_error_page(page)) |
| 1397 | goto error; |
| 1398 | |
| 1399 | return kmap_atomic(page, idx); |
| 1400 | |
| 1401 | error: |
| 1402 | kvm_release_page_clean(page); |
| 1403 | kvm_inject_gp(&svm->vcpu, 0); |
| 1404 | |
| 1405 | return NULL; |
| 1406 | } |
| 1407 | |
| 1408 | static void nested_svm_unmap(void *addr, enum km_type idx) |
| 1409 | { |
| 1410 | struct page *page; |
| 1411 | |
| 1412 | if (!addr) |
| 1413 | return; |
| 1414 | |
| 1415 | page = kmap_atomic_to_page(addr); |
| 1416 | |
| 1417 | kunmap_atomic(addr, idx); |
| 1418 | kvm_release_page_dirty(page); |
| 1419 | } |
| 1420 | |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 1421 | static bool nested_svm_exit_handled_msr(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1422 | { |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1423 | u32 param = svm->vmcb->control.exit_info_1 & 1; |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 1424 | u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
| 1425 | bool ret = false; |
| 1426 | u32 t0, t1; |
| 1427 | u8 *msrpm; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1428 | |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 1429 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
| 1430 | return false; |
| 1431 | |
| 1432 | msrpm = nested_svm_map(svm, svm->nested.vmcb_msrpm, KM_USER0); |
| 1433 | |
| 1434 | if (!msrpm) |
| 1435 | goto out; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1436 | |
| 1437 | switch (msr) { |
| 1438 | case 0 ... 0x1fff: |
| 1439 | t0 = (msr * 2) % 8; |
| 1440 | t1 = msr / 8; |
| 1441 | break; |
| 1442 | case 0xc0000000 ... 0xc0001fff: |
| 1443 | t0 = (8192 + msr - 0xc0000000) * 2; |
| 1444 | t1 = (t0 / 8); |
| 1445 | t0 %= 8; |
| 1446 | break; |
| 1447 | case 0xc0010000 ... 0xc0011fff: |
| 1448 | t0 = (16384 + msr - 0xc0010000) * 2; |
| 1449 | t1 = (t0 / 8); |
| 1450 | t0 %= 8; |
| 1451 | break; |
| 1452 | default: |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 1453 | ret = true; |
| 1454 | goto out; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1455 | } |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1456 | |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 1457 | ret = msrpm[t1] & ((1 << param) << t0); |
| 1458 | |
| 1459 | out: |
| 1460 | nested_svm_unmap(msrpm, KM_USER0); |
| 1461 | |
| 1462 | return ret; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1465 | static int nested_svm_exit_special(struct vcpu_svm *svm) |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1466 | { |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1467 | u32 exit_code = svm->vmcb->control.exit_code; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 1468 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1469 | switch (exit_code) { |
| 1470 | case SVM_EXIT_INTR: |
| 1471 | case SVM_EXIT_NMI: |
| 1472 | return NESTED_EXIT_HOST; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1473 | /* For now we are always handling NPFs when using them */ |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1474 | case SVM_EXIT_NPF: |
| 1475 | if (npt_enabled) |
| 1476 | return NESTED_EXIT_HOST; |
| 1477 | break; |
| 1478 | /* When we're shadowing, trap PFs */ |
| 1479 | case SVM_EXIT_EXCP_BASE + PF_VECTOR: |
| 1480 | if (!npt_enabled) |
| 1481 | return NESTED_EXIT_HOST; |
| 1482 | break; |
| 1483 | default: |
| 1484 | break; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1485 | } |
| 1486 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1487 | return NESTED_EXIT_CONTINUE; |
| 1488 | } |
| 1489 | |
| 1490 | /* |
| 1491 | * If this function returns true, this #vmexit was already handled |
| 1492 | */ |
| 1493 | static int nested_svm_exit_handled(struct vcpu_svm *svm) |
| 1494 | { |
| 1495 | u32 exit_code = svm->vmcb->control.exit_code; |
| 1496 | int vmexit = NESTED_EXIT_HOST; |
| 1497 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1498 | switch (exit_code) { |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 1499 | case SVM_EXIT_MSR: |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 1500 | vmexit = nested_svm_exit_handled_msr(svm); |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 1501 | break; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1502 | case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: { |
| 1503 | u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1504 | if (svm->nested.intercept_cr_read & cr_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1505 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1506 | break; |
| 1507 | } |
| 1508 | case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: { |
| 1509 | u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1510 | if (svm->nested.intercept_cr_write & cr_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1511 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1512 | break; |
| 1513 | } |
| 1514 | case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: { |
| 1515 | u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1516 | if (svm->nested.intercept_dr_read & dr_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1517 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1518 | break; |
| 1519 | } |
| 1520 | case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: { |
| 1521 | u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1522 | if (svm->nested.intercept_dr_write & dr_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1523 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1524 | break; |
| 1525 | } |
| 1526 | case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { |
| 1527 | u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1528 | if (svm->nested.intercept_exceptions & excp_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1529 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1530 | break; |
| 1531 | } |
| 1532 | default: { |
| 1533 | u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1534 | if (svm->nested.intercept & exit_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1535 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 1539 | if (vmexit == NESTED_EXIT_DONE) { |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 1540 | nested_svm_vmexit(svm); |
| 1541 | } |
| 1542 | |
| 1543 | return vmexit; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1544 | } |
| 1545 | |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 1546 | static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb) |
| 1547 | { |
| 1548 | struct vmcb_control_area *dst = &dst_vmcb->control; |
| 1549 | struct vmcb_control_area *from = &from_vmcb->control; |
| 1550 | |
| 1551 | dst->intercept_cr_read = from->intercept_cr_read; |
| 1552 | dst->intercept_cr_write = from->intercept_cr_write; |
| 1553 | dst->intercept_dr_read = from->intercept_dr_read; |
| 1554 | dst->intercept_dr_write = from->intercept_dr_write; |
| 1555 | dst->intercept_exceptions = from->intercept_exceptions; |
| 1556 | dst->intercept = from->intercept; |
| 1557 | dst->iopm_base_pa = from->iopm_base_pa; |
| 1558 | dst->msrpm_base_pa = from->msrpm_base_pa; |
| 1559 | dst->tsc_offset = from->tsc_offset; |
| 1560 | dst->asid = from->asid; |
| 1561 | dst->tlb_ctl = from->tlb_ctl; |
| 1562 | dst->int_ctl = from->int_ctl; |
| 1563 | dst->int_vector = from->int_vector; |
| 1564 | dst->int_state = from->int_state; |
| 1565 | dst->exit_code = from->exit_code; |
| 1566 | dst->exit_code_hi = from->exit_code_hi; |
| 1567 | dst->exit_info_1 = from->exit_info_1; |
| 1568 | dst->exit_info_2 = from->exit_info_2; |
| 1569 | dst->exit_int_info = from->exit_int_info; |
| 1570 | dst->exit_int_info_err = from->exit_int_info_err; |
| 1571 | dst->nested_ctl = from->nested_ctl; |
| 1572 | dst->event_inj = from->event_inj; |
| 1573 | dst->event_inj_err = from->event_inj_err; |
| 1574 | dst->nested_cr3 = from->nested_cr3; |
| 1575 | dst->lbr_ctl = from->lbr_ctl; |
| 1576 | } |
| 1577 | |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1578 | static int nested_svm_vmexit(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1579 | { |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1580 | struct vmcb *nested_vmcb; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1581 | struct vmcb *hsave = svm->nested.hsave; |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 1582 | struct vmcb *vmcb = svm->vmcb; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1583 | |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 1584 | trace_kvm_nested_vmexit_inject(vmcb->control.exit_code, |
| 1585 | vmcb->control.exit_info_1, |
| 1586 | vmcb->control.exit_info_2, |
| 1587 | vmcb->control.exit_int_info, |
| 1588 | vmcb->control.exit_int_info_err); |
| 1589 | |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1590 | nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, KM_USER0); |
| 1591 | if (!nested_vmcb) |
| 1592 | return 1; |
| 1593 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1594 | /* Give the current vmcb to the guest */ |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 1595 | disable_gif(svm); |
| 1596 | |
| 1597 | nested_vmcb->save.es = vmcb->save.es; |
| 1598 | nested_vmcb->save.cs = vmcb->save.cs; |
| 1599 | nested_vmcb->save.ss = vmcb->save.ss; |
| 1600 | nested_vmcb->save.ds = vmcb->save.ds; |
| 1601 | nested_vmcb->save.gdtr = vmcb->save.gdtr; |
| 1602 | nested_vmcb->save.idtr = vmcb->save.idtr; |
| 1603 | if (npt_enabled) |
| 1604 | nested_vmcb->save.cr3 = vmcb->save.cr3; |
| 1605 | nested_vmcb->save.cr2 = vmcb->save.cr2; |
| 1606 | nested_vmcb->save.rflags = vmcb->save.rflags; |
| 1607 | nested_vmcb->save.rip = vmcb->save.rip; |
| 1608 | nested_vmcb->save.rsp = vmcb->save.rsp; |
| 1609 | nested_vmcb->save.rax = vmcb->save.rax; |
| 1610 | nested_vmcb->save.dr7 = vmcb->save.dr7; |
| 1611 | nested_vmcb->save.dr6 = vmcb->save.dr6; |
| 1612 | nested_vmcb->save.cpl = vmcb->save.cpl; |
| 1613 | |
| 1614 | nested_vmcb->control.int_ctl = vmcb->control.int_ctl; |
| 1615 | nested_vmcb->control.int_vector = vmcb->control.int_vector; |
| 1616 | nested_vmcb->control.int_state = vmcb->control.int_state; |
| 1617 | nested_vmcb->control.exit_code = vmcb->control.exit_code; |
| 1618 | nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi; |
| 1619 | nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1; |
| 1620 | nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2; |
| 1621 | nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info; |
| 1622 | nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err; |
Alexander Graf | 8d23c46 | 2009-10-09 16:08:25 +0200 | [diff] [blame] | 1623 | |
| 1624 | /* |
| 1625 | * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have |
| 1626 | * to make sure that we do not lose injected events. So check event_inj |
| 1627 | * here and copy it to exit_int_info if it is valid. |
| 1628 | * Exit_int_info and event_inj can't be both valid because the case |
| 1629 | * below only happens on a VMRUN instruction intercept which has |
| 1630 | * no valid exit_int_info set. |
| 1631 | */ |
| 1632 | if (vmcb->control.event_inj & SVM_EVTINJ_VALID) { |
| 1633 | struct vmcb_control_area *nc = &nested_vmcb->control; |
| 1634 | |
| 1635 | nc->exit_int_info = vmcb->control.event_inj; |
| 1636 | nc->exit_int_info_err = vmcb->control.event_inj_err; |
| 1637 | } |
| 1638 | |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 1639 | nested_vmcb->control.tlb_ctl = 0; |
| 1640 | nested_vmcb->control.event_inj = 0; |
| 1641 | nested_vmcb->control.event_inj_err = 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1642 | |
| 1643 | /* We always set V_INTR_MASKING and remember the old value in hflags */ |
| 1644 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
| 1645 | nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK; |
| 1646 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1647 | /* Restore the original control entries */ |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 1648 | copy_vmcb_control_area(vmcb, hsave); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1649 | |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 1650 | kvm_clear_exception_queue(&svm->vcpu); |
| 1651 | kvm_clear_interrupt_queue(&svm->vcpu); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1652 | |
| 1653 | /* Restore selected save entries */ |
| 1654 | svm->vmcb->save.es = hsave->save.es; |
| 1655 | svm->vmcb->save.cs = hsave->save.cs; |
| 1656 | svm->vmcb->save.ss = hsave->save.ss; |
| 1657 | svm->vmcb->save.ds = hsave->save.ds; |
| 1658 | svm->vmcb->save.gdtr = hsave->save.gdtr; |
| 1659 | svm->vmcb->save.idtr = hsave->save.idtr; |
| 1660 | svm->vmcb->save.rflags = hsave->save.rflags; |
| 1661 | svm_set_efer(&svm->vcpu, hsave->save.efer); |
| 1662 | svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); |
| 1663 | svm_set_cr4(&svm->vcpu, hsave->save.cr4); |
| 1664 | if (npt_enabled) { |
| 1665 | svm->vmcb->save.cr3 = hsave->save.cr3; |
| 1666 | svm->vcpu.arch.cr3 = hsave->save.cr3; |
| 1667 | } else { |
| 1668 | kvm_set_cr3(&svm->vcpu, hsave->save.cr3); |
| 1669 | } |
| 1670 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax); |
| 1671 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp); |
| 1672 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip); |
| 1673 | svm->vmcb->save.dr7 = 0; |
| 1674 | svm->vmcb->save.cpl = 0; |
| 1675 | svm->vmcb->control.exit_int_info = 0; |
| 1676 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1677 | /* Exit nested SVM mode */ |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1678 | svm->nested.vmcb = 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1679 | |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 1680 | nested_svm_unmap(nested_vmcb, KM_USER0); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 1681 | |
| 1682 | kvm_mmu_reset_context(&svm->vcpu); |
| 1683 | kvm_mmu_load(&svm->vcpu); |
| 1684 | |
| 1685 | return 0; |
| 1686 | } |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1687 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1688 | static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1689 | { |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1690 | u32 *nested_msrpm; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1691 | int i; |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1692 | |
| 1693 | nested_msrpm = nested_svm_map(svm, svm->nested.vmcb_msrpm, KM_USER0); |
| 1694 | if (!nested_msrpm) |
| 1695 | return false; |
| 1696 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1697 | for (i=0; i< PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++) |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1698 | svm->nested.msrpm[i] = svm->msrpm[i] | nested_msrpm[i]; |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1699 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1700 | svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1701 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1702 | nested_svm_unmap(nested_msrpm, KM_USER0); |
| 1703 | |
| 1704 | return true; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1705 | } |
| 1706 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1707 | static bool nested_svm_vmrun(struct vcpu_svm *svm) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1708 | { |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1709 | struct vmcb *nested_vmcb; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1710 | struct vmcb *hsave = svm->nested.hsave; |
Joerg Roedel | defbba5 | 2009-08-07 11:49:30 +0200 | [diff] [blame] | 1711 | struct vmcb *vmcb = svm->vmcb; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1712 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1713 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, KM_USER0); |
| 1714 | if (!nested_vmcb) |
| 1715 | return false; |
| 1716 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1717 | /* nested_vmcb is our indicator if nested SVM is activated */ |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1718 | svm->nested.vmcb = svm->vmcb->save.rax; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1719 | |
Joerg Roedel | 0ac406d | 2009-10-09 16:08:27 +0200 | [diff] [blame] | 1720 | trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, svm->nested.vmcb, |
| 1721 | nested_vmcb->save.rip, |
| 1722 | nested_vmcb->control.int_ctl, |
| 1723 | nested_vmcb->control.event_inj, |
| 1724 | nested_vmcb->control.nested_ctl); |
| 1725 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1726 | /* Clear internal status */ |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 1727 | kvm_clear_exception_queue(&svm->vcpu); |
| 1728 | kvm_clear_interrupt_queue(&svm->vcpu); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1729 | |
| 1730 | /* Save the old vmcb, so we don't need to pick what we save, but |
| 1731 | can restore everything when a VMEXIT occurs */ |
Joerg Roedel | defbba5 | 2009-08-07 11:49:30 +0200 | [diff] [blame] | 1732 | hsave->save.es = vmcb->save.es; |
| 1733 | hsave->save.cs = vmcb->save.cs; |
| 1734 | hsave->save.ss = vmcb->save.ss; |
| 1735 | hsave->save.ds = vmcb->save.ds; |
| 1736 | hsave->save.gdtr = vmcb->save.gdtr; |
| 1737 | hsave->save.idtr = vmcb->save.idtr; |
| 1738 | hsave->save.efer = svm->vcpu.arch.shadow_efer; |
| 1739 | hsave->save.cr0 = svm->vcpu.arch.cr0; |
| 1740 | hsave->save.cr4 = svm->vcpu.arch.cr4; |
| 1741 | hsave->save.rflags = vmcb->save.rflags; |
| 1742 | hsave->save.rip = svm->next_rip; |
| 1743 | hsave->save.rsp = vmcb->save.rsp; |
| 1744 | hsave->save.rax = vmcb->save.rax; |
| 1745 | if (npt_enabled) |
| 1746 | hsave->save.cr3 = vmcb->save.cr3; |
| 1747 | else |
| 1748 | hsave->save.cr3 = svm->vcpu.arch.cr3; |
| 1749 | |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 1750 | copy_vmcb_control_area(hsave, vmcb); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1751 | |
| 1752 | if (svm->vmcb->save.rflags & X86_EFLAGS_IF) |
| 1753 | svm->vcpu.arch.hflags |= HF_HIF_MASK; |
| 1754 | else |
| 1755 | svm->vcpu.arch.hflags &= ~HF_HIF_MASK; |
| 1756 | |
| 1757 | /* Load the nested guest state */ |
| 1758 | svm->vmcb->save.es = nested_vmcb->save.es; |
| 1759 | svm->vmcb->save.cs = nested_vmcb->save.cs; |
| 1760 | svm->vmcb->save.ss = nested_vmcb->save.ss; |
| 1761 | svm->vmcb->save.ds = nested_vmcb->save.ds; |
| 1762 | svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; |
| 1763 | svm->vmcb->save.idtr = nested_vmcb->save.idtr; |
| 1764 | svm->vmcb->save.rflags = nested_vmcb->save.rflags; |
| 1765 | svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); |
| 1766 | svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); |
| 1767 | svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); |
| 1768 | if (npt_enabled) { |
| 1769 | svm->vmcb->save.cr3 = nested_vmcb->save.cr3; |
| 1770 | svm->vcpu.arch.cr3 = nested_vmcb->save.cr3; |
| 1771 | } else { |
| 1772 | kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3); |
| 1773 | kvm_mmu_reset_context(&svm->vcpu); |
| 1774 | } |
Joerg Roedel | defbba5 | 2009-08-07 11:49:30 +0200 | [diff] [blame] | 1775 | svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1776 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax); |
| 1777 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp); |
| 1778 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip); |
| 1779 | /* In case we don't even reach vcpu_run, the fields are not updated */ |
| 1780 | svm->vmcb->save.rax = nested_vmcb->save.rax; |
| 1781 | svm->vmcb->save.rsp = nested_vmcb->save.rsp; |
| 1782 | svm->vmcb->save.rip = nested_vmcb->save.rip; |
| 1783 | svm->vmcb->save.dr7 = nested_vmcb->save.dr7; |
| 1784 | svm->vmcb->save.dr6 = nested_vmcb->save.dr6; |
| 1785 | svm->vmcb->save.cpl = nested_vmcb->save.cpl; |
| 1786 | |
| 1787 | /* We don't want a nested guest to be more powerful than the guest, |
| 1788 | so all intercepts are ORed */ |
| 1789 | svm->vmcb->control.intercept_cr_read |= |
| 1790 | nested_vmcb->control.intercept_cr_read; |
| 1791 | svm->vmcb->control.intercept_cr_write |= |
| 1792 | nested_vmcb->control.intercept_cr_write; |
| 1793 | svm->vmcb->control.intercept_dr_read |= |
| 1794 | nested_vmcb->control.intercept_dr_read; |
| 1795 | svm->vmcb->control.intercept_dr_write |= |
| 1796 | nested_vmcb->control.intercept_dr_write; |
| 1797 | svm->vmcb->control.intercept_exceptions |= |
| 1798 | nested_vmcb->control.intercept_exceptions; |
| 1799 | |
| 1800 | svm->vmcb->control.intercept |= nested_vmcb->control.intercept; |
| 1801 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1802 | svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1803 | |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 1804 | /* cache intercepts */ |
| 1805 | svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read; |
| 1806 | svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write; |
| 1807 | svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read; |
| 1808 | svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write; |
| 1809 | svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions; |
| 1810 | svm->nested.intercept = nested_vmcb->control.intercept; |
| 1811 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1812 | force_new_asid(&svm->vcpu); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1813 | svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1814 | if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK) |
| 1815 | svm->vcpu.arch.hflags |= HF_VINTR_MASK; |
| 1816 | else |
| 1817 | svm->vcpu.arch.hflags &= ~HF_VINTR_MASK; |
| 1818 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1819 | svm->vmcb->control.int_vector = nested_vmcb->control.int_vector; |
| 1820 | svm->vmcb->control.int_state = nested_vmcb->control.int_state; |
| 1821 | svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1822 | svm->vmcb->control.event_inj = nested_vmcb->control.event_inj; |
| 1823 | svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err; |
| 1824 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1825 | nested_svm_unmap(nested_vmcb, KM_USER0); |
| 1826 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 1827 | enable_gif(svm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1828 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1829 | return true; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1830 | } |
| 1831 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 1832 | static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 1833 | { |
| 1834 | to_vmcb->save.fs = from_vmcb->save.fs; |
| 1835 | to_vmcb->save.gs = from_vmcb->save.gs; |
| 1836 | to_vmcb->save.tr = from_vmcb->save.tr; |
| 1837 | to_vmcb->save.ldtr = from_vmcb->save.ldtr; |
| 1838 | to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; |
| 1839 | to_vmcb->save.star = from_vmcb->save.star; |
| 1840 | to_vmcb->save.lstar = from_vmcb->save.lstar; |
| 1841 | to_vmcb->save.cstar = from_vmcb->save.cstar; |
| 1842 | to_vmcb->save.sfmask = from_vmcb->save.sfmask; |
| 1843 | to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; |
| 1844 | to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; |
| 1845 | to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 1846 | } |
| 1847 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1848 | static int vmload_interception(struct vcpu_svm *svm) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 1849 | { |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 1850 | struct vmcb *nested_vmcb; |
| 1851 | |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 1852 | if (nested_svm_check_permissions(svm)) |
| 1853 | return 1; |
| 1854 | |
| 1855 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 1856 | skip_emulated_instruction(&svm->vcpu); |
| 1857 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 1858 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, KM_USER0); |
| 1859 | if (!nested_vmcb) |
| 1860 | return 1; |
| 1861 | |
| 1862 | nested_svm_vmloadsave(nested_vmcb, svm->vmcb); |
| 1863 | nested_svm_unmap(nested_vmcb, KM_USER0); |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 1864 | |
| 1865 | return 1; |
| 1866 | } |
| 1867 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1868 | static int vmsave_interception(struct vcpu_svm *svm) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 1869 | { |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 1870 | struct vmcb *nested_vmcb; |
| 1871 | |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 1872 | if (nested_svm_check_permissions(svm)) |
| 1873 | return 1; |
| 1874 | |
| 1875 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 1876 | skip_emulated_instruction(&svm->vcpu); |
| 1877 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 1878 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, KM_USER0); |
| 1879 | if (!nested_vmcb) |
| 1880 | return 1; |
| 1881 | |
| 1882 | nested_svm_vmloadsave(svm->vmcb, nested_vmcb); |
| 1883 | nested_svm_unmap(nested_vmcb, KM_USER0); |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 1884 | |
| 1885 | return 1; |
| 1886 | } |
| 1887 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1888 | static int vmrun_interception(struct vcpu_svm *svm) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1889 | { |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1890 | if (nested_svm_check_permissions(svm)) |
| 1891 | return 1; |
| 1892 | |
| 1893 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 1894 | skip_emulated_instruction(&svm->vcpu); |
| 1895 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1896 | if (!nested_svm_vmrun(svm)) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1897 | return 1; |
| 1898 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 1899 | if (!nested_svm_vmrun_msrpm(svm)) |
Joerg Roedel | 1f8da47 | 2009-08-07 11:49:43 +0200 | [diff] [blame] | 1900 | goto failed; |
| 1901 | |
| 1902 | return 1; |
| 1903 | |
| 1904 | failed: |
| 1905 | |
| 1906 | svm->vmcb->control.exit_code = SVM_EXIT_ERR; |
| 1907 | svm->vmcb->control.exit_code_hi = 0; |
| 1908 | svm->vmcb->control.exit_info_1 = 0; |
| 1909 | svm->vmcb->control.exit_info_2 = 0; |
| 1910 | |
| 1911 | nested_svm_vmexit(svm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1912 | |
| 1913 | return 1; |
| 1914 | } |
| 1915 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1916 | static int stgi_interception(struct vcpu_svm *svm) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 1917 | { |
| 1918 | if (nested_svm_check_permissions(svm)) |
| 1919 | return 1; |
| 1920 | |
| 1921 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 1922 | skip_emulated_instruction(&svm->vcpu); |
| 1923 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 1924 | enable_gif(svm); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 1925 | |
| 1926 | return 1; |
| 1927 | } |
| 1928 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1929 | static int clgi_interception(struct vcpu_svm *svm) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 1930 | { |
| 1931 | if (nested_svm_check_permissions(svm)) |
| 1932 | return 1; |
| 1933 | |
| 1934 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 1935 | skip_emulated_instruction(&svm->vcpu); |
| 1936 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 1937 | disable_gif(svm); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 1938 | |
| 1939 | /* After a CLGI no interrupts should come */ |
| 1940 | svm_clear_vintr(svm); |
| 1941 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
| 1942 | |
| 1943 | return 1; |
| 1944 | } |
| 1945 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1946 | static int invlpga_interception(struct vcpu_svm *svm) |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 1947 | { |
| 1948 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 1949 | |
Joerg Roedel | ec1ff79 | 2009-10-09 16:08:31 +0200 | [diff] [blame] | 1950 | trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX], |
| 1951 | vcpu->arch.regs[VCPU_REGS_RAX]); |
| 1952 | |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 1953 | /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */ |
| 1954 | kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]); |
| 1955 | |
| 1956 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
| 1957 | skip_emulated_instruction(&svm->vcpu); |
| 1958 | return 1; |
| 1959 | } |
| 1960 | |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 1961 | static int skinit_interception(struct vcpu_svm *svm) |
| 1962 | { |
| 1963 | trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]); |
| 1964 | |
| 1965 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 1966 | return 1; |
| 1967 | } |
| 1968 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1969 | static int invalid_op_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1970 | { |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 1971 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1972 | return 1; |
| 1973 | } |
| 1974 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 1975 | static int task_switch_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1976 | { |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 1977 | u16 tss_selector; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 1978 | int reason; |
| 1979 | int int_type = svm->vmcb->control.exit_int_info & |
| 1980 | SVM_EXITINTINFO_TYPE_MASK; |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 1981 | int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK; |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 1982 | uint32_t type = |
| 1983 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK; |
| 1984 | uint32_t idt_v = |
| 1985 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 1986 | |
| 1987 | tss_selector = (u16)svm->vmcb->control.exit_info_1; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 1988 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 1989 | if (svm->vmcb->control.exit_info_2 & |
| 1990 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 1991 | reason = TASK_SWITCH_IRET; |
| 1992 | else if (svm->vmcb->control.exit_info_2 & |
| 1993 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) |
| 1994 | reason = TASK_SWITCH_JMP; |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 1995 | else if (idt_v) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 1996 | reason = TASK_SWITCH_GATE; |
| 1997 | else |
| 1998 | reason = TASK_SWITCH_CALL; |
| 1999 | |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2000 | if (reason == TASK_SWITCH_GATE) { |
| 2001 | switch (type) { |
| 2002 | case SVM_EXITINTINFO_TYPE_NMI: |
| 2003 | svm->vcpu.arch.nmi_injected = false; |
| 2004 | break; |
| 2005 | case SVM_EXITINTINFO_TYPE_EXEPT: |
| 2006 | kvm_clear_exception_queue(&svm->vcpu); |
| 2007 | break; |
| 2008 | case SVM_EXITINTINFO_TYPE_INTR: |
| 2009 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 2010 | break; |
| 2011 | default: |
| 2012 | break; |
| 2013 | } |
| 2014 | } |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2015 | |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 2016 | if (reason != TASK_SWITCH_GATE || |
| 2017 | int_type == SVM_EXITINTINFO_TYPE_SOFT || |
| 2018 | (int_type == SVM_EXITINTINFO_TYPE_EXEPT && |
Gleb Natapov | f629cf8 | 2009-05-11 13:35:49 +0300 | [diff] [blame] | 2019 | (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) |
| 2020 | skip_emulated_instruction(&svm->vcpu); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 2021 | |
| 2022 | return kvm_task_switch(&svm->vcpu, tss_selector, reason); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2023 | } |
| 2024 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2025 | static int cpuid_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2026 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2027 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2028 | kvm_emulate_cpuid(&svm->vcpu); |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 2029 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2030 | } |
| 2031 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2032 | static int iret_interception(struct vcpu_svm *svm) |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2033 | { |
| 2034 | ++svm->vcpu.stat.nmi_window_exits; |
| 2035 | svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2036 | svm->vcpu.arch.hflags |= HF_IRET_MASK; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2037 | return 1; |
| 2038 | } |
| 2039 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2040 | static int invlpg_interception(struct vcpu_svm *svm) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2041 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2042 | if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2043 | pr_unimpl(&svm->vcpu, "%s: failed\n", __func__); |
| 2044 | return 1; |
| 2045 | } |
| 2046 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2047 | static int emulate_on_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2048 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2049 | if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE) |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2050 | pr_unimpl(&svm->vcpu, "%s: failed\n", __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2051 | return 1; |
| 2052 | } |
| 2053 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2054 | static int cr8_write_interception(struct vcpu_svm *svm) |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 2055 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2056 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2057 | |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 2058 | u8 cr8_prev = kvm_get_cr8(&svm->vcpu); |
| 2059 | /* instruction emulation calls kvm_set_cr8() */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2060 | emulate_instruction(&svm->vcpu, 0, 0, 0); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2061 | if (irqchip_in_kernel(svm->vcpu.kvm)) { |
| 2062 | svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK; |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 2063 | return 1; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2064 | } |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 2065 | if (cr8_prev <= kvm_get_cr8(&svm->vcpu)) |
| 2066 | return 1; |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 2067 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2071 | static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data) |
| 2072 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2073 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2074 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2075 | switch (ecx) { |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2076 | case MSR_IA32_TSC: { |
Joerg Roedel | 20824f3 | 2009-09-16 15:24:18 +0200 | [diff] [blame] | 2077 | u64 tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2078 | |
Joerg Roedel | 20824f3 | 2009-09-16 15:24:18 +0200 | [diff] [blame] | 2079 | if (is_nested(svm)) |
| 2080 | tsc_offset = svm->nested.hsave->control.tsc_offset; |
| 2081 | else |
| 2082 | tsc_offset = svm->vmcb->control.tsc_offset; |
| 2083 | |
| 2084 | *data = tsc_offset + native_read_tsc(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2085 | break; |
| 2086 | } |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 2087 | case MSR_K6_STAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2088 | *data = svm->vmcb->save.star; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2089 | break; |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 2090 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2091 | case MSR_LSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2092 | *data = svm->vmcb->save.lstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2093 | break; |
| 2094 | case MSR_CSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2095 | *data = svm->vmcb->save.cstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2096 | break; |
| 2097 | case MSR_KERNEL_GS_BASE: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2098 | *data = svm->vmcb->save.kernel_gs_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2099 | break; |
| 2100 | case MSR_SYSCALL_MASK: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2101 | *data = svm->vmcb->save.sfmask; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2102 | break; |
| 2103 | #endif |
| 2104 | case MSR_IA32_SYSENTER_CS: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2105 | *data = svm->vmcb->save.sysenter_cs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2106 | break; |
| 2107 | case MSR_IA32_SYSENTER_EIP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 2108 | *data = svm->sysenter_eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2109 | break; |
| 2110 | case MSR_IA32_SYSENTER_ESP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 2111 | *data = svm->sysenter_esp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2112 | break; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2113 | /* Nobody will change the following 5 values in the VMCB so |
| 2114 | we can safely return them on rdmsr. They will always be 0 |
| 2115 | until LBRV is implemented. */ |
| 2116 | case MSR_IA32_DEBUGCTLMSR: |
| 2117 | *data = svm->vmcb->save.dbgctl; |
| 2118 | break; |
| 2119 | case MSR_IA32_LASTBRANCHFROMIP: |
| 2120 | *data = svm->vmcb->save.br_from; |
| 2121 | break; |
| 2122 | case MSR_IA32_LASTBRANCHTOIP: |
| 2123 | *data = svm->vmcb->save.br_to; |
| 2124 | break; |
| 2125 | case MSR_IA32_LASTINTFROMIP: |
| 2126 | *data = svm->vmcb->save.last_excp_from; |
| 2127 | break; |
| 2128 | case MSR_IA32_LASTINTTOIP: |
| 2129 | *data = svm->vmcb->save.last_excp_to; |
| 2130 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2131 | case MSR_VM_HSAVE_PA: |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 2132 | *data = svm->nested.hsave_msr; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2133 | break; |
Joerg Roedel | eb6f302 | 2008-11-25 20:17:09 +0100 | [diff] [blame] | 2134 | case MSR_VM_CR: |
| 2135 | *data = 0; |
| 2136 | break; |
Alexander Graf | c8a73f1 | 2009-01-05 16:02:47 +0100 | [diff] [blame] | 2137 | case MSR_IA32_UCODE_REV: |
| 2138 | *data = 0x01000065; |
| 2139 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2140 | default: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2141 | return kvm_get_msr_common(vcpu, ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2142 | } |
| 2143 | return 0; |
| 2144 | } |
| 2145 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2146 | static int rdmsr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2147 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2148 | u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2149 | u64 data; |
| 2150 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2151 | if (svm_get_msr(&svm->vcpu, ecx, &data)) |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2152 | kvm_inject_gp(&svm->vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2153 | else { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 2154 | trace_kvm_msr_read(ecx, data); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2155 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2156 | svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2157 | svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2158 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2159 | skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2160 | } |
| 2161 | return 1; |
| 2162 | } |
| 2163 | |
| 2164 | static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data) |
| 2165 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2166 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2167 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2168 | switch (ecx) { |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2169 | case MSR_IA32_TSC: { |
Joerg Roedel | 20824f3 | 2009-09-16 15:24:18 +0200 | [diff] [blame] | 2170 | u64 tsc_offset = data - native_read_tsc(); |
| 2171 | u64 g_tsc_offset = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2172 | |
Joerg Roedel | 20824f3 | 2009-09-16 15:24:18 +0200 | [diff] [blame] | 2173 | if (is_nested(svm)) { |
| 2174 | g_tsc_offset = svm->vmcb->control.tsc_offset - |
| 2175 | svm->nested.hsave->control.tsc_offset; |
| 2176 | svm->nested.hsave->control.tsc_offset = tsc_offset; |
| 2177 | } |
| 2178 | |
| 2179 | svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset; |
| 2180 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2181 | break; |
| 2182 | } |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 2183 | case MSR_K6_STAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2184 | svm->vmcb->save.star = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2185 | break; |
Robert P. J. Day | 49b14f2 | 2007-01-29 13:19:50 -0800 | [diff] [blame] | 2186 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2187 | case MSR_LSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2188 | svm->vmcb->save.lstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2189 | break; |
| 2190 | case MSR_CSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2191 | svm->vmcb->save.cstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2192 | break; |
| 2193 | case MSR_KERNEL_GS_BASE: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2194 | svm->vmcb->save.kernel_gs_base = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2195 | break; |
| 2196 | case MSR_SYSCALL_MASK: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2197 | svm->vmcb->save.sfmask = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2198 | break; |
| 2199 | #endif |
| 2200 | case MSR_IA32_SYSENTER_CS: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2201 | svm->vmcb->save.sysenter_cs = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2202 | break; |
| 2203 | case MSR_IA32_SYSENTER_EIP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 2204 | svm->sysenter_eip = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2205 | svm->vmcb->save.sysenter_eip = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2206 | break; |
| 2207 | case MSR_IA32_SYSENTER_ESP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 2208 | svm->sysenter_esp = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2209 | svm->vmcb->save.sysenter_esp = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2210 | break; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2211 | case MSR_IA32_DEBUGCTLMSR: |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 2212 | if (!svm_has(SVM_FEATURE_LBRV)) { |
| 2213 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2214 | __func__, data); |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 2215 | break; |
| 2216 | } |
| 2217 | if (data & DEBUGCTL_RESERVED_BITS) |
| 2218 | return 1; |
| 2219 | |
| 2220 | svm->vmcb->save.dbgctl = data; |
| 2221 | if (data & (1ULL<<0)) |
| 2222 | svm_enable_lbrv(svm); |
| 2223 | else |
| 2224 | svm_disable_lbrv(svm); |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 2225 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2226 | case MSR_VM_HSAVE_PA: |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 2227 | svm->nested.hsave_msr = data; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2228 | break; |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 2229 | case MSR_VM_CR: |
| 2230 | case MSR_VM_IGNNE: |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 2231 | pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data); |
| 2232 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2233 | default: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2234 | return kvm_set_msr_common(vcpu, ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2235 | } |
| 2236 | return 0; |
| 2237 | } |
| 2238 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2239 | static int wrmsr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2240 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2241 | u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2242 | u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2243 | | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2244 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 2245 | trace_kvm_msr_write(ecx, data); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2246 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2247 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2248 | if (svm_set_msr(&svm->vcpu, ecx, data)) |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2249 | kvm_inject_gp(&svm->vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2250 | else |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2251 | skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2252 | return 1; |
| 2253 | } |
| 2254 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2255 | static int msr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2256 | { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2257 | if (svm->vmcb->control.exit_info_1) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2258 | return wrmsr_interception(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2259 | else |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2260 | return rdmsr_interception(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2261 | } |
| 2262 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2263 | static int interrupt_window_interception(struct vcpu_svm *svm) |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2264 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2265 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2266 | |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 2267 | svm_clear_vintr(svm); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2268 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2269 | /* |
| 2270 | * If the user space waits to inject interrupts, exit as soon as |
| 2271 | * possible |
| 2272 | */ |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 2273 | if (!irqchip_in_kernel(svm->vcpu.kvm) && |
| 2274 | kvm_run->request_interrupt_window && |
| 2275 | !kvm_cpu_has_interrupt(&svm->vcpu)) { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2276 | ++svm->vcpu.stat.irq_window_exits; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2277 | kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
| 2278 | return 0; |
| 2279 | } |
| 2280 | |
| 2281 | return 1; |
| 2282 | } |
| 2283 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2284 | static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2285 | [SVM_EXIT_READ_CR0] = emulate_on_interception, |
| 2286 | [SVM_EXIT_READ_CR3] = emulate_on_interception, |
| 2287 | [SVM_EXIT_READ_CR4] = emulate_on_interception, |
Avi Kivity | 80a8119 | 2007-12-06 19:50:00 +0200 | [diff] [blame] | 2288 | [SVM_EXIT_READ_CR8] = emulate_on_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2289 | /* for now: */ |
| 2290 | [SVM_EXIT_WRITE_CR0] = emulate_on_interception, |
| 2291 | [SVM_EXIT_WRITE_CR3] = emulate_on_interception, |
| 2292 | [SVM_EXIT_WRITE_CR4] = emulate_on_interception, |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 2293 | [SVM_EXIT_WRITE_CR8] = cr8_write_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2294 | [SVM_EXIT_READ_DR0] = emulate_on_interception, |
| 2295 | [SVM_EXIT_READ_DR1] = emulate_on_interception, |
| 2296 | [SVM_EXIT_READ_DR2] = emulate_on_interception, |
| 2297 | [SVM_EXIT_READ_DR3] = emulate_on_interception, |
| 2298 | [SVM_EXIT_WRITE_DR0] = emulate_on_interception, |
| 2299 | [SVM_EXIT_WRITE_DR1] = emulate_on_interception, |
| 2300 | [SVM_EXIT_WRITE_DR2] = emulate_on_interception, |
| 2301 | [SVM_EXIT_WRITE_DR3] = emulate_on_interception, |
| 2302 | [SVM_EXIT_WRITE_DR5] = emulate_on_interception, |
| 2303 | [SVM_EXIT_WRITE_DR7] = emulate_on_interception, |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2304 | [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, |
| 2305 | [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2306 | [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2307 | [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 2308 | [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception, |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2309 | [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, |
Joerg Roedel | a069805 | 2008-04-30 17:56:01 +0200 | [diff] [blame] | 2310 | [SVM_EXIT_INTR] = intr_interception, |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 2311 | [SVM_EXIT_NMI] = nmi_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2312 | [SVM_EXIT_SMI] = nop_on_interception, |
| 2313 | [SVM_EXIT_INIT] = nop_on_interception, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2314 | [SVM_EXIT_VINTR] = interrupt_window_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2315 | /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */ |
| 2316 | [SVM_EXIT_CPUID] = cpuid_interception, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2317 | [SVM_EXIT_IRET] = iret_interception, |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 2318 | [SVM_EXIT_INVD] = emulate_on_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2319 | [SVM_EXIT_HLT] = halt_interception, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2320 | [SVM_EXIT_INVLPG] = invlpg_interception, |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 2321 | [SVM_EXIT_INVLPGA] = invlpga_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2322 | [SVM_EXIT_IOIO] = io_interception, |
| 2323 | [SVM_EXIT_MSR] = msr_interception, |
| 2324 | [SVM_EXIT_TASK_SWITCH] = task_switch_interception, |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2325 | [SVM_EXIT_SHUTDOWN] = shutdown_interception, |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2326 | [SVM_EXIT_VMRUN] = vmrun_interception, |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2327 | [SVM_EXIT_VMMCALL] = vmmcall_interception, |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 2328 | [SVM_EXIT_VMLOAD] = vmload_interception, |
| 2329 | [SVM_EXIT_VMSAVE] = vmsave_interception, |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 2330 | [SVM_EXIT_STGI] = stgi_interception, |
| 2331 | [SVM_EXIT_CLGI] = clgi_interception, |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 2332 | [SVM_EXIT_SKINIT] = skinit_interception, |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 2333 | [SVM_EXIT_WBINVD] = emulate_on_interception, |
Joerg Roedel | 916ce23 | 2007-03-21 19:47:00 +0100 | [diff] [blame] | 2334 | [SVM_EXIT_MONITOR] = invalid_op_interception, |
| 2335 | [SVM_EXIT_MWAIT] = invalid_op_interception, |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2336 | [SVM_EXIT_NPF] = pf_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2337 | }; |
| 2338 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2339 | static int handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2340 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2341 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2342 | struct kvm_run *kvm_run = vcpu->run; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2343 | u32 exit_code = svm->vmcb->control.exit_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2344 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 2345 | trace_kvm_exit(exit_code, svm->vmcb->save.rip); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2346 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 2347 | if (unlikely(svm->nested.exit_required)) { |
| 2348 | nested_svm_vmexit(svm); |
| 2349 | svm->nested.exit_required = false; |
| 2350 | |
| 2351 | return 1; |
| 2352 | } |
| 2353 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2354 | if (is_nested(svm)) { |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2355 | int vmexit; |
| 2356 | |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 2357 | trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code, |
| 2358 | svm->vmcb->control.exit_info_1, |
| 2359 | svm->vmcb->control.exit_info_2, |
| 2360 | svm->vmcb->control.exit_int_info, |
| 2361 | svm->vmcb->control.exit_int_info_err); |
| 2362 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2363 | vmexit = nested_svm_exit_special(svm); |
| 2364 | |
| 2365 | if (vmexit == NESTED_EXIT_CONTINUE) |
| 2366 | vmexit = nested_svm_exit_handled(svm); |
| 2367 | |
| 2368 | if (vmexit == NESTED_EXIT_DONE) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2369 | return 1; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2370 | } |
| 2371 | |
Joerg Roedel | a5c3832 | 2009-08-07 11:49:32 +0200 | [diff] [blame] | 2372 | svm_complete_interrupts(svm); |
| 2373 | |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2374 | if (npt_enabled) { |
| 2375 | int mmu_reload = 0; |
| 2376 | if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) { |
| 2377 | svm_set_cr0(vcpu, svm->vmcb->save.cr0); |
| 2378 | mmu_reload = 1; |
| 2379 | } |
| 2380 | vcpu->arch.cr0 = svm->vmcb->save.cr0; |
| 2381 | vcpu->arch.cr3 = svm->vmcb->save.cr3; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2382 | if (mmu_reload) { |
| 2383 | kvm_mmu_reset_context(vcpu); |
| 2384 | kvm_mmu_load(vcpu); |
| 2385 | } |
| 2386 | } |
| 2387 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2388 | |
| 2389 | if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { |
| 2390 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 2391 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 2392 | = svm->vmcb->control.exit_code; |
| 2393 | return 0; |
| 2394 | } |
| 2395 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2396 | if (is_external_interrupt(svm->vmcb->control.exit_int_info) && |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2397 | exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 2398 | exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2399 | printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x " |
| 2400 | "exit_code 0x%x\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2401 | __func__, svm->vmcb->control.exit_int_info, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2402 | exit_code); |
| 2403 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 2404 | if (exit_code >= ARRAY_SIZE(svm_exit_handlers) |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 2405 | || !svm_exit_handlers[exit_code]) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2406 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
Avi Kivity | 364b625 | 2007-04-16 14:28:40 +0300 | [diff] [blame] | 2407 | kvm_run->hw.hardware_exit_reason = exit_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2408 | return 0; |
| 2409 | } |
| 2410 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2411 | return svm_exit_handlers[exit_code](svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | static void reload_tss(struct kvm_vcpu *vcpu) |
| 2415 | { |
| 2416 | int cpu = raw_smp_processor_id(); |
| 2417 | |
| 2418 | struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2419 | svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2420 | load_TR_desc(); |
| 2421 | } |
| 2422 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2423 | static void pre_svm_run(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2424 | { |
| 2425 | int cpu = raw_smp_processor_id(); |
| 2426 | |
| 2427 | struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu); |
| 2428 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2429 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 2430 | /* FIXME: handle wraparound of asid_generation */ |
| 2431 | if (svm->asid_generation != svm_data->asid_generation) |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2432 | new_asid(svm, svm_data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2433 | } |
| 2434 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2435 | static void svm_inject_nmi(struct kvm_vcpu *vcpu) |
| 2436 | { |
| 2437 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2438 | |
| 2439 | svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; |
| 2440 | vcpu->arch.hflags |= HF_NMI_MASK; |
| 2441 | svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET); |
| 2442 | ++vcpu->stat.nmi_injections; |
| 2443 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2444 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2445 | static inline void svm_inject_irq(struct vcpu_svm *svm, int irq) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2446 | { |
| 2447 | struct vmcb_control_area *control; |
| 2448 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 2449 | trace_kvm_inj_virq(irq); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 2450 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 2451 | ++svm->vcpu.stat.irq_injections; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2452 | control = &svm->vmcb->control; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2453 | control->int_vector = irq; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2454 | control->int_ctl &= ~V_INTR_PRIO_MASK; |
| 2455 | control->int_ctl |= V_IRQ_MASK | |
| 2456 | ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); |
| 2457 | } |
| 2458 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2459 | static void svm_set_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2460 | { |
| 2461 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2462 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 2463 | BUG_ON(!(gif_set(svm))); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2464 | |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 2465 | svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | |
| 2466 | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR; |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2467 | } |
| 2468 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2469 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
| 2470 | { |
| 2471 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2472 | |
| 2473 | if (irr == -1) |
| 2474 | return; |
| 2475 | |
| 2476 | if (tpr >= irr) |
| 2477 | svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK; |
| 2478 | } |
| 2479 | |
| 2480 | static int svm_nmi_allowed(struct kvm_vcpu *vcpu) |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 2481 | { |
| 2482 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2483 | struct vmcb *vmcb = svm->vmcb; |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2484 | return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) && |
| 2485 | !(svm->vcpu.arch.hflags & HF_NMI_MASK); |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 2486 | } |
| 2487 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 2488 | static int svm_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 2489 | { |
| 2490 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2491 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 2492 | int ret; |
| 2493 | |
| 2494 | if (!gif_set(svm) || |
| 2495 | (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)) |
| 2496 | return 0; |
| 2497 | |
| 2498 | ret = !!(vmcb->save.rflags & X86_EFLAGS_IF); |
| 2499 | |
| 2500 | if (is_nested(svm)) |
| 2501 | return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK); |
| 2502 | |
| 2503 | return ret; |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 2504 | } |
| 2505 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2506 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
| 2507 | { |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 2508 | struct vcpu_svm *svm = to_svm(vcpu); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 2509 | |
| 2510 | nested_svm_intr(svm); |
| 2511 | |
| 2512 | /* In case GIF=0 we can't rely on the CPU to tell us when |
| 2513 | * GIF becomes 1, because that's a separate STGI/VMRUN intercept. |
| 2514 | * The next time we get that intercept, this function will be |
| 2515 | * called again though and we'll get the vintr intercept. */ |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 2516 | if (gif_set(svm)) { |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 2517 | svm_set_vintr(svm); |
| 2518 | svm_inject_irq(svm, 0x0); |
| 2519 | } |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2520 | } |
| 2521 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2522 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2523 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2524 | struct vcpu_svm *svm = to_svm(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2525 | |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2526 | if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) |
| 2527 | == HF_NMI_MASK) |
| 2528 | return; /* IRET will cause a vm exit */ |
| 2529 | |
| 2530 | /* Something prevents NMI from been injected. Single step over |
| 2531 | possible problem (IRET or exception injection or interrupt |
| 2532 | shadow) */ |
| 2533 | vcpu->arch.singlestep = true; |
| 2534 | svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 2535 | update_db_intercept(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2536 | } |
| 2537 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2538 | static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 2539 | { |
| 2540 | return 0; |
| 2541 | } |
| 2542 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 2543 | static void svm_flush_tlb(struct kvm_vcpu *vcpu) |
| 2544 | { |
| 2545 | force_new_asid(vcpu); |
| 2546 | } |
| 2547 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2548 | static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) |
| 2549 | { |
| 2550 | } |
| 2551 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 2552 | static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) |
| 2553 | { |
| 2554 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2555 | |
| 2556 | if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) { |
| 2557 | int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; |
Gleb Natapov | 615d519 | 2009-04-21 17:45:05 +0300 | [diff] [blame] | 2558 | kvm_set_cr8(vcpu, cr8); |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 2559 | } |
| 2560 | } |
| 2561 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 2562 | static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) |
| 2563 | { |
| 2564 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2565 | u64 cr8; |
| 2566 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 2567 | cr8 = kvm_get_cr8(vcpu); |
| 2568 | svm->vmcb->control.int_ctl &= ~V_TPR_MASK; |
| 2569 | svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; |
| 2570 | } |
| 2571 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2572 | static void svm_complete_interrupts(struct vcpu_svm *svm) |
| 2573 | { |
| 2574 | u8 vector; |
| 2575 | int type; |
| 2576 | u32 exitintinfo = svm->vmcb->control.exit_int_info; |
| 2577 | |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2578 | if (svm->vcpu.arch.hflags & HF_IRET_MASK) |
| 2579 | svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK); |
| 2580 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2581 | svm->vcpu.arch.nmi_injected = false; |
| 2582 | kvm_clear_exception_queue(&svm->vcpu); |
| 2583 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 2584 | |
| 2585 | if (!(exitintinfo & SVM_EXITINTINFO_VALID)) |
| 2586 | return; |
| 2587 | |
| 2588 | vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK; |
| 2589 | type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK; |
| 2590 | |
| 2591 | switch (type) { |
| 2592 | case SVM_EXITINTINFO_TYPE_NMI: |
| 2593 | svm->vcpu.arch.nmi_injected = true; |
| 2594 | break; |
| 2595 | case SVM_EXITINTINFO_TYPE_EXEPT: |
| 2596 | /* In case of software exception do not reinject an exception |
| 2597 | vector, but re-execute and instruction instead */ |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 2598 | if (is_nested(svm)) |
| 2599 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2600 | if (kvm_exception_is_soft(vector)) |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2601 | break; |
| 2602 | if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) { |
| 2603 | u32 err = svm->vmcb->control.exit_int_info_err; |
| 2604 | kvm_queue_exception_e(&svm->vcpu, vector, err); |
| 2605 | |
| 2606 | } else |
| 2607 | kvm_queue_exception(&svm->vcpu, vector); |
| 2608 | break; |
| 2609 | case SVM_EXITINTINFO_TYPE_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2610 | kvm_queue_interrupt(&svm->vcpu, vector, false); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2611 | break; |
| 2612 | default: |
| 2613 | break; |
| 2614 | } |
| 2615 | } |
| 2616 | |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2617 | #ifdef CONFIG_X86_64 |
| 2618 | #define R "r" |
| 2619 | #else |
| 2620 | #define R "e" |
| 2621 | #endif |
| 2622 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2623 | static void svm_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2624 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2625 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2626 | u16 fs_selector; |
| 2627 | u16 gs_selector; |
| 2628 | u16 ldt_selector; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 2629 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 2630 | /* |
| 2631 | * A vmexit emulation is required before the vcpu can be executed |
| 2632 | * again. |
| 2633 | */ |
| 2634 | if (unlikely(svm->nested.exit_required)) |
| 2635 | return; |
| 2636 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2637 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 2638 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 2639 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; |
| 2640 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2641 | pre_svm_run(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2642 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 2643 | sync_lapic_to_cr8(vcpu); |
| 2644 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2645 | save_host_msrs(vcpu); |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2646 | fs_selector = kvm_read_fs(); |
| 2647 | gs_selector = kvm_read_gs(); |
| 2648 | ldt_selector = kvm_read_ldt(); |
Joerg Roedel | cda0ffd | 2009-08-07 11:49:45 +0200 | [diff] [blame] | 2649 | svm->vmcb->save.cr2 = vcpu->arch.cr2; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2650 | /* required for live migration with NPT */ |
| 2651 | if (npt_enabled) |
| 2652 | svm->vmcb->save.cr3 = vcpu->arch.cr3; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2653 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2654 | clgi(); |
| 2655 | |
| 2656 | local_irq_enable(); |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 2657 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2658 | asm volatile ( |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2659 | "push %%"R"bp; \n\t" |
| 2660 | "mov %c[rbx](%[svm]), %%"R"bx \n\t" |
| 2661 | "mov %c[rcx](%[svm]), %%"R"cx \n\t" |
| 2662 | "mov %c[rdx](%[svm]), %%"R"dx \n\t" |
| 2663 | "mov %c[rsi](%[svm]), %%"R"si \n\t" |
| 2664 | "mov %c[rdi](%[svm]), %%"R"di \n\t" |
| 2665 | "mov %c[rbp](%[svm]), %%"R"bp \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2666 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2667 | "mov %c[r8](%[svm]), %%r8 \n\t" |
| 2668 | "mov %c[r9](%[svm]), %%r9 \n\t" |
| 2669 | "mov %c[r10](%[svm]), %%r10 \n\t" |
| 2670 | "mov %c[r11](%[svm]), %%r11 \n\t" |
| 2671 | "mov %c[r12](%[svm]), %%r12 \n\t" |
| 2672 | "mov %c[r13](%[svm]), %%r13 \n\t" |
| 2673 | "mov %c[r14](%[svm]), %%r14 \n\t" |
| 2674 | "mov %c[r15](%[svm]), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2675 | #endif |
| 2676 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2677 | /* Enter guest mode */ |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2678 | "push %%"R"ax \n\t" |
| 2679 | "mov %c[vmcb](%[svm]), %%"R"ax \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 2680 | __ex(SVM_VMLOAD) "\n\t" |
| 2681 | __ex(SVM_VMRUN) "\n\t" |
| 2682 | __ex(SVM_VMSAVE) "\n\t" |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2683 | "pop %%"R"ax \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2684 | |
| 2685 | /* Save guest registers, load host registers */ |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2686 | "mov %%"R"bx, %c[rbx](%[svm]) \n\t" |
| 2687 | "mov %%"R"cx, %c[rcx](%[svm]) \n\t" |
| 2688 | "mov %%"R"dx, %c[rdx](%[svm]) \n\t" |
| 2689 | "mov %%"R"si, %c[rsi](%[svm]) \n\t" |
| 2690 | "mov %%"R"di, %c[rdi](%[svm]) \n\t" |
| 2691 | "mov %%"R"bp, %c[rbp](%[svm]) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2692 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2693 | "mov %%r8, %c[r8](%[svm]) \n\t" |
| 2694 | "mov %%r9, %c[r9](%[svm]) \n\t" |
| 2695 | "mov %%r10, %c[r10](%[svm]) \n\t" |
| 2696 | "mov %%r11, %c[r11](%[svm]) \n\t" |
| 2697 | "mov %%r12, %c[r12](%[svm]) \n\t" |
| 2698 | "mov %%r13, %c[r13](%[svm]) \n\t" |
| 2699 | "mov %%r14, %c[r14](%[svm]) \n\t" |
| 2700 | "mov %%r15, %c[r15](%[svm]) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2701 | #endif |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2702 | "pop %%"R"bp" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2703 | : |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2704 | : [svm]"a"(svm), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2705 | [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2706 | [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 2707 | [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 2708 | [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 2709 | [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 2710 | [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 2711 | [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP])) |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2712 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2713 | , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])), |
| 2714 | [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])), |
| 2715 | [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])), |
| 2716 | [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])), |
| 2717 | [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])), |
| 2718 | [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])), |
| 2719 | [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])), |
| 2720 | [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15])) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2721 | #endif |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 2722 | : "cc", "memory" |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2723 | , R"bx", R"cx", R"dx", R"si", R"di" |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 2724 | #ifdef CONFIG_X86_64 |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 2725 | , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15" |
| 2726 | #endif |
| 2727 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2728 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2729 | vcpu->arch.cr2 = svm->vmcb->save.cr2; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2730 | vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; |
| 2731 | vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; |
| 2732 | vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2733 | |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2734 | kvm_load_fs(fs_selector); |
| 2735 | kvm_load_gs(gs_selector); |
| 2736 | kvm_load_ldt(ldt_selector); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2737 | load_host_msrs(vcpu); |
| 2738 | |
| 2739 | reload_tss(vcpu); |
| 2740 | |
Avi Kivity | 56ba47d | 2007-11-07 17:14:18 +0200 | [diff] [blame] | 2741 | local_irq_disable(); |
| 2742 | |
| 2743 | stgi(); |
| 2744 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 2745 | sync_cr8_to_lapic(vcpu); |
| 2746 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2747 | svm->next_rip = 0; |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 2748 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2749 | if (npt_enabled) { |
| 2750 | vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR); |
| 2751 | vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR); |
| 2752 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2753 | } |
| 2754 | |
Avi Kivity | 80e31d4 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 2755 | #undef R |
| 2756 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2757 | static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
| 2758 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2759 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2760 | |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 2761 | if (npt_enabled) { |
| 2762 | svm->vmcb->control.nested_cr3 = root; |
| 2763 | force_new_asid(vcpu); |
| 2764 | return; |
| 2765 | } |
| 2766 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2767 | svm->vmcb->save.cr3 = root; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2768 | force_new_asid(vcpu); |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 2769 | |
| 2770 | if (vcpu->fpu_active) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2771 | svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR); |
| 2772 | svm->vmcb->save.cr0 |= X86_CR0_TS; |
Anthony Liguori | 7807fa6 | 2007-04-23 09:17:21 -0500 | [diff] [blame] | 2773 | vcpu->fpu_active = 0; |
| 2774 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2775 | } |
| 2776 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2777 | static int is_disabled(void) |
| 2778 | { |
Joerg Roedel | 6031a61 | 2007-06-22 12:29:50 +0300 | [diff] [blame] | 2779 | u64 vm_cr; |
| 2780 | |
| 2781 | rdmsrl(MSR_VM_CR, vm_cr); |
| 2782 | if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) |
| 2783 | return 1; |
| 2784 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2785 | return 0; |
| 2786 | } |
| 2787 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2788 | static void |
| 2789 | svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 2790 | { |
| 2791 | /* |
| 2792 | * Patch in the VMMCALL instruction: |
| 2793 | */ |
| 2794 | hypercall[0] = 0x0f; |
| 2795 | hypercall[1] = 0x01; |
| 2796 | hypercall[2] = 0xd9; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2797 | } |
| 2798 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2799 | static void svm_check_processor_compat(void *rtn) |
| 2800 | { |
| 2801 | *(int *)rtn = 0; |
| 2802 | } |
| 2803 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 2804 | static bool svm_cpu_has_accelerated_tpr(void) |
| 2805 | { |
| 2806 | return false; |
| 2807 | } |
| 2808 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 2809 | static int get_npt_level(void) |
| 2810 | { |
| 2811 | #ifdef CONFIG_X86_64 |
| 2812 | return PT64_ROOT_LEVEL; |
| 2813 | #else |
| 2814 | return PT32E_ROOT_LEVEL; |
| 2815 | #endif |
| 2816 | } |
| 2817 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 2818 | static u64 svm_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] | 2819 | { |
| 2820 | return 0; |
| 2821 | } |
| 2822 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 2823 | static const struct trace_print_flags svm_exit_reasons_str[] = { |
| 2824 | { SVM_EXIT_READ_CR0, "read_cr0" }, |
| 2825 | { SVM_EXIT_READ_CR3, "read_cr3" }, |
| 2826 | { SVM_EXIT_READ_CR4, "read_cr4" }, |
| 2827 | { SVM_EXIT_READ_CR8, "read_cr8" }, |
| 2828 | { SVM_EXIT_WRITE_CR0, "write_cr0" }, |
| 2829 | { SVM_EXIT_WRITE_CR3, "write_cr3" }, |
| 2830 | { SVM_EXIT_WRITE_CR4, "write_cr4" }, |
| 2831 | { SVM_EXIT_WRITE_CR8, "write_cr8" }, |
| 2832 | { SVM_EXIT_READ_DR0, "read_dr0" }, |
| 2833 | { SVM_EXIT_READ_DR1, "read_dr1" }, |
| 2834 | { SVM_EXIT_READ_DR2, "read_dr2" }, |
| 2835 | { SVM_EXIT_READ_DR3, "read_dr3" }, |
| 2836 | { SVM_EXIT_WRITE_DR0, "write_dr0" }, |
| 2837 | { SVM_EXIT_WRITE_DR1, "write_dr1" }, |
| 2838 | { SVM_EXIT_WRITE_DR2, "write_dr2" }, |
| 2839 | { SVM_EXIT_WRITE_DR3, "write_dr3" }, |
| 2840 | { SVM_EXIT_WRITE_DR5, "write_dr5" }, |
| 2841 | { SVM_EXIT_WRITE_DR7, "write_dr7" }, |
| 2842 | { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" }, |
| 2843 | { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" }, |
| 2844 | { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" }, |
| 2845 | { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" }, |
| 2846 | { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" }, |
| 2847 | { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" }, |
| 2848 | { SVM_EXIT_INTR, "interrupt" }, |
| 2849 | { SVM_EXIT_NMI, "nmi" }, |
| 2850 | { SVM_EXIT_SMI, "smi" }, |
| 2851 | { SVM_EXIT_INIT, "init" }, |
| 2852 | { SVM_EXIT_VINTR, "vintr" }, |
| 2853 | { SVM_EXIT_CPUID, "cpuid" }, |
| 2854 | { SVM_EXIT_INVD, "invd" }, |
| 2855 | { SVM_EXIT_HLT, "hlt" }, |
| 2856 | { SVM_EXIT_INVLPG, "invlpg" }, |
| 2857 | { SVM_EXIT_INVLPGA, "invlpga" }, |
| 2858 | { SVM_EXIT_IOIO, "io" }, |
| 2859 | { SVM_EXIT_MSR, "msr" }, |
| 2860 | { SVM_EXIT_TASK_SWITCH, "task_switch" }, |
| 2861 | { SVM_EXIT_SHUTDOWN, "shutdown" }, |
| 2862 | { SVM_EXIT_VMRUN, "vmrun" }, |
| 2863 | { SVM_EXIT_VMMCALL, "hypercall" }, |
| 2864 | { SVM_EXIT_VMLOAD, "vmload" }, |
| 2865 | { SVM_EXIT_VMSAVE, "vmsave" }, |
| 2866 | { SVM_EXIT_STGI, "stgi" }, |
| 2867 | { SVM_EXIT_CLGI, "clgi" }, |
| 2868 | { SVM_EXIT_SKINIT, "skinit" }, |
| 2869 | { SVM_EXIT_WBINVD, "wbinvd" }, |
| 2870 | { SVM_EXIT_MONITOR, "monitor" }, |
| 2871 | { SVM_EXIT_MWAIT, "mwait" }, |
| 2872 | { SVM_EXIT_NPF, "npf" }, |
| 2873 | { -1, NULL } |
| 2874 | }; |
| 2875 | |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 2876 | static bool svm_gb_page_enable(void) |
| 2877 | { |
| 2878 | return true; |
| 2879 | } |
| 2880 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2881 | static struct kvm_x86_ops svm_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2882 | .cpu_has_kvm_support = has_svm, |
| 2883 | .disabled_by_bios = is_disabled, |
| 2884 | .hardware_setup = svm_hardware_setup, |
| 2885 | .hardware_unsetup = svm_hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2886 | .check_processor_compatibility = svm_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2887 | .hardware_enable = svm_hardware_enable, |
| 2888 | .hardware_disable = svm_hardware_disable, |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 2889 | .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2890 | |
| 2891 | .vcpu_create = svm_create_vcpu, |
| 2892 | .vcpu_free = svm_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2893 | .vcpu_reset = svm_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2894 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2895 | .prepare_guest_switch = svm_prepare_guest_switch, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2896 | .vcpu_load = svm_vcpu_load, |
| 2897 | .vcpu_put = svm_vcpu_put, |
| 2898 | |
| 2899 | .set_guest_debug = svm_guest_debug, |
| 2900 | .get_msr = svm_get_msr, |
| 2901 | .set_msr = svm_set_msr, |
| 2902 | .get_segment_base = svm_get_segment_base, |
| 2903 | .get_segment = svm_get_segment, |
| 2904 | .set_segment = svm_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2905 | .get_cpl = svm_get_cpl, |
Rusty Russell | 1747fb7 | 2007-09-06 01:21:32 +1000 | [diff] [blame] | 2906 | .get_cs_db_l_bits = kvm_get_cs_db_l_bits, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2907 | .decache_cr4_guest_bits = svm_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2908 | .set_cr0 = svm_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2909 | .set_cr3 = svm_set_cr3, |
| 2910 | .set_cr4 = svm_set_cr4, |
| 2911 | .set_efer = svm_set_efer, |
| 2912 | .get_idt = svm_get_idt, |
| 2913 | .set_idt = svm_set_idt, |
| 2914 | .get_gdt = svm_get_gdt, |
| 2915 | .set_gdt = svm_set_gdt, |
| 2916 | .get_dr = svm_get_dr, |
| 2917 | .set_dr = svm_set_dr, |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2918 | .cache_reg = svm_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2919 | .get_rflags = svm_get_rflags, |
| 2920 | .set_rflags = svm_set_rflags, |
| 2921 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2922 | .tlb_flush = svm_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2923 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2924 | .run = svm_vcpu_run, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2925 | .handle_exit = handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2926 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2927 | .set_interrupt_shadow = svm_set_interrupt_shadow, |
| 2928 | .get_interrupt_shadow = svm_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2929 | .patch_hypercall = svm_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2930 | .set_irq = svm_set_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2931 | .set_nmi = svm_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2932 | .queue_exception = svm_queue_exception, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 2933 | .interrupt_allowed = svm_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2934 | .nmi_allowed = svm_nmi_allowed, |
| 2935 | .enable_nmi_window = enable_nmi_window, |
| 2936 | .enable_irq_window = enable_irq_window, |
| 2937 | .update_cr8_intercept = update_cr8_intercept, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2938 | |
| 2939 | .set_tss_addr = svm_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 2940 | .get_tdp_level = get_npt_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 2941 | .get_mt_mask = svm_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 2942 | |
| 2943 | .exit_reasons_str = svm_exit_reasons_str, |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 2944 | .gb_page_enable = svm_gb_page_enable, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2945 | }; |
| 2946 | |
| 2947 | static int __init svm_init(void) |
| 2948 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2949 | return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm), |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2950 | THIS_MODULE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | static void __exit svm_exit(void) |
| 2954 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2955 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | module_init(svm_init) |
| 2959 | module_exit(svm_exit) |