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