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