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