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