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