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