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