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