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