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> |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 37 | #include <linux/amd-iommu.h> |
| 38 | #include <linux/hashtable.h> |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 39 | #include <linux/frame.h> |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 40 | #include <linux/psp-sev.h> |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 41 | #include <linux/file.h> |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 42 | #include <linux/pagemap.h> |
| 43 | #include <linux/swap.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 44 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 45 | #include <asm/apic.h> |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 46 | #include <asm/perf_event.h> |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 47 | #include <asm/tlbflush.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 48 | #include <asm/desc.h> |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 49 | #include <asm/debugreg.h> |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 50 | #include <asm/kvm_para.h> |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 51 | #include <asm/irq_remapping.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 52 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 53 | #include <asm/virtext.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 54 | #include "trace.h" |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 55 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 56 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
| 57 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 58 | MODULE_AUTHOR("Qumranet"); |
| 59 | MODULE_LICENSE("GPL"); |
| 60 | |
Josh Triplett | ae75954 | 2012-03-28 11:32:28 -0700 | [diff] [blame] | 61 | static const struct x86_cpu_id svm_cpu_id[] = { |
| 62 | X86_FEATURE_MATCH(X86_FEATURE_SVM), |
| 63 | {} |
| 64 | }; |
| 65 | MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id); |
| 66 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 67 | #define IOPM_ALLOC_ORDER 2 |
| 68 | #define MSRPM_ALLOC_ORDER 1 |
| 69 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 70 | #define SEG_TYPE_LDT 2 |
| 71 | #define SEG_TYPE_BUSY_TSS16 3 |
| 72 | |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 73 | #define SVM_FEATURE_NPT (1 << 0) |
| 74 | #define SVM_FEATURE_LBRV (1 << 1) |
| 75 | #define SVM_FEATURE_SVML (1 << 2) |
| 76 | #define SVM_FEATURE_NRIP (1 << 3) |
Andre Przywara | ddce97a | 2010-12-21 11:12:03 +0100 | [diff] [blame] | 77 | #define SVM_FEATURE_TSC_RATE (1 << 4) |
| 78 | #define SVM_FEATURE_VMCB_CLEAN (1 << 5) |
| 79 | #define SVM_FEATURE_FLUSH_ASID (1 << 6) |
| 80 | #define SVM_FEATURE_DECODE_ASSIST (1 << 7) |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 81 | #define SVM_FEATURE_PAUSE_FILTER (1 << 10) |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 82 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 83 | #define SVM_AVIC_DOORBELL 0xc001011b |
| 84 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 85 | #define NESTED_EXIT_HOST 0 /* Exit handled on host level */ |
| 86 | #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */ |
| 87 | #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */ |
| 88 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 89 | #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) |
| 90 | |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 91 | #define TSC_RATIO_RSVD 0xffffff0000000000ULL |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 92 | #define TSC_RATIO_MIN 0x0000000000000001ULL |
| 93 | #define TSC_RATIO_MAX 0x000000ffffffffffULL |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 94 | |
Dan Carpenter | 5446a97 | 2016-05-23 13:20:10 +0300 | [diff] [blame] | 95 | #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * 0xff is broadcast, so the max index allowed for physical APIC ID |
| 99 | * table is 0xfe. APIC IDs above 0xff are reserved. |
| 100 | */ |
| 101 | #define AVIC_MAX_PHYSICAL_ID_COUNT 255 |
| 102 | |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 103 | #define AVIC_UNACCEL_ACCESS_WRITE_MASK 1 |
| 104 | #define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0 |
| 105 | #define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF |
| 106 | |
Suravee Suthikulpanit | 5ea11f2 | 2016-08-23 13:52:41 -0500 | [diff] [blame] | 107 | /* AVIC GATAG is encoded using VM and VCPU IDs */ |
| 108 | #define AVIC_VCPU_ID_BITS 8 |
| 109 | #define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1) |
| 110 | |
| 111 | #define AVIC_VM_ID_BITS 24 |
| 112 | #define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS) |
| 113 | #define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1) |
| 114 | |
| 115 | #define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \ |
| 116 | (y & AVIC_VCPU_ID_MASK)) |
| 117 | #define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK) |
| 118 | #define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK) |
| 119 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 120 | static bool erratum_383_found __read_mostly; |
| 121 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 122 | static const u32 host_save_user_msrs[] = { |
| 123 | #ifdef CONFIG_X86_64 |
| 124 | MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE, |
| 125 | MSR_FS_BASE, |
| 126 | #endif |
| 127 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 128 | MSR_TSC_AUX, |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) |
| 132 | |
| 133 | struct kvm_vcpu; |
| 134 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 135 | struct nested_state { |
| 136 | struct vmcb *hsave; |
| 137 | u64 hsave_msr; |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 138 | u64 vm_cr_msr; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 139 | u64 vmcb; |
| 140 | |
| 141 | /* These are the merged vectors */ |
| 142 | u32 *msrpm; |
| 143 | |
| 144 | /* gpa pointers to the real vectors */ |
| 145 | u64 vmcb_msrpm; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 146 | u64 vmcb_iopm; |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 147 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 148 | /* A VMEXIT is required but not yet emulated */ |
| 149 | bool exit_required; |
| 150 | |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 151 | /* cache for intercepts of the guest */ |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 152 | u32 intercept_cr; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 153 | u32 intercept_dr; |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 154 | u32 intercept_exceptions; |
| 155 | u64 intercept; |
| 156 | |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 157 | /* Nested Paging related state */ |
| 158 | u64 nested_cr3; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 159 | }; |
| 160 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 161 | #define MSRPM_OFFSETS 16 |
| 162 | static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; |
| 163 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 164 | /* |
| 165 | * Set osvw_len to higher value when updated Revision Guides |
| 166 | * are published and we know what the new status bits are |
| 167 | */ |
| 168 | static uint64_t osvw_len = 4, osvw_status; |
| 169 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 170 | struct vcpu_svm { |
| 171 | struct kvm_vcpu vcpu; |
| 172 | struct vmcb *vmcb; |
| 173 | unsigned long vmcb_pa; |
| 174 | struct svm_cpu_data *svm_data; |
| 175 | uint64_t asid_generation; |
| 176 | uint64_t sysenter_esp; |
| 177 | uint64_t sysenter_eip; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 178 | uint64_t tsc_aux; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 179 | |
| 180 | u64 next_rip; |
| 181 | |
| 182 | u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS]; |
Avi Kivity | afe9e66 | 2010-10-21 12:20:32 +0200 | [diff] [blame] | 183 | struct { |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 184 | u16 fs; |
| 185 | u16 gs; |
| 186 | u16 ldt; |
Avi Kivity | afe9e66 | 2010-10-21 12:20:32 +0200 | [diff] [blame] | 187 | u64 gs_base; |
| 188 | } host; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 189 | |
| 190 | u32 *msrpm; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 191 | |
Avi Kivity | bd3d1ec | 2011-02-03 15:29:52 +0200 | [diff] [blame] | 192 | ulong nmi_iret_rip; |
| 193 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 194 | struct nested_state nested; |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 195 | |
| 196 | bool nmi_singlestep; |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 197 | u64 nmi_singlestep_guest_rflags; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 198 | |
| 199 | unsigned int3_injected; |
| 200 | unsigned long int3_rip; |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 201 | |
Joerg Roedel | 6092d3d | 2015-10-14 15:10:54 +0200 | [diff] [blame] | 202 | /* cached guest cpuid flags for faster access */ |
| 203 | bool nrips_enabled : 1; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 204 | |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 205 | u32 ldr_reg; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 206 | struct page *avic_backing_page; |
| 207 | u64 *avic_physical_id_cache; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 208 | bool avic_is_running; |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * Per-vcpu list of struct amd_svm_iommu_ir: |
| 212 | * This is used mainly to store interrupt remapping information used |
| 213 | * when update the vcpu affinity. This avoids the need to scan for |
| 214 | * IRTE and try to match ga_tag in the IOMMU driver. |
| 215 | */ |
| 216 | struct list_head ir_list; |
| 217 | spinlock_t ir_list_lock; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 218 | |
| 219 | /* which host CPU was used for running this vcpu */ |
| 220 | unsigned int last_cpu; |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | /* |
| 224 | * This is a wrapper of struct amd_iommu_ir_data. |
| 225 | */ |
| 226 | struct amd_svm_iommu_ir { |
| 227 | struct list_head node; /* Used by SVM for per-vcpu ir_list */ |
| 228 | void *data; /* Storing pointer to struct amd_ir_data */ |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 229 | }; |
| 230 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 231 | #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF) |
| 232 | #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31) |
| 233 | |
| 234 | #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL) |
| 235 | #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12) |
| 236 | #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62) |
| 237 | #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63) |
| 238 | |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 239 | static DEFINE_PER_CPU(u64, current_tsc_ratio); |
| 240 | #define TSC_RATIO_DEFAULT 0x0100000000ULL |
| 241 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 242 | #define MSR_INVALID 0xffffffffU |
| 243 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 244 | static const struct svm_direct_access_msrs { |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 245 | u32 index; /* Index of the MSR */ |
| 246 | bool always; /* True if intercept is always on */ |
| 247 | } direct_access_msrs[] = { |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 248 | { .index = MSR_STAR, .always = true }, |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 249 | { .index = MSR_IA32_SYSENTER_CS, .always = true }, |
| 250 | #ifdef CONFIG_X86_64 |
| 251 | { .index = MSR_GS_BASE, .always = true }, |
| 252 | { .index = MSR_FS_BASE, .always = true }, |
| 253 | { .index = MSR_KERNEL_GS_BASE, .always = true }, |
| 254 | { .index = MSR_LSTAR, .always = true }, |
| 255 | { .index = MSR_CSTAR, .always = true }, |
| 256 | { .index = MSR_SYSCALL_MASK, .always = true }, |
| 257 | #endif |
| 258 | { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false }, |
| 259 | { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, |
| 260 | { .index = MSR_IA32_LASTINTFROMIP, .always = false }, |
| 261 | { .index = MSR_IA32_LASTINTTOIP, .always = false }, |
| 262 | { .index = MSR_INVALID, .always = false }, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | /* enable NPT for AMD64 and X86 with PAE */ |
| 266 | #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) |
| 267 | static bool npt_enabled = true; |
| 268 | #else |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 269 | static bool npt_enabled; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 270 | #endif |
| 271 | |
Davidlohr Bueso | e235885 | 2012-01-17 14:09:50 +0100 | [diff] [blame] | 272 | /* allow nested paging (virtualized MMU) for all guests */ |
| 273 | static int npt = true; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 274 | module_param(npt, int, S_IRUGO); |
| 275 | |
Davidlohr Bueso | e235885 | 2012-01-17 14:09:50 +0100 | [diff] [blame] | 276 | /* allow nested virtualization in KVM/SVM */ |
| 277 | static int nested = true; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 278 | module_param(nested, int, S_IRUGO); |
| 279 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 280 | /* enable / disable AVIC */ |
| 281 | static int avic; |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 282 | #ifdef CONFIG_X86_LOCAL_APIC |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 283 | module_param(avic, int, S_IRUGO); |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 284 | #endif |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 285 | |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 286 | /* enable/disable Virtual VMLOAD VMSAVE */ |
| 287 | static int vls = true; |
| 288 | module_param(vls, int, 0444); |
| 289 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 290 | /* enable/disable Virtual GIF */ |
| 291 | static int vgif = true; |
| 292 | module_param(vgif, int, 0444); |
Suravee Suthikulpanit | 5ea11f2 | 2016-08-23 13:52:41 -0500 | [diff] [blame] | 293 | |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 294 | /* enable/disable SEV support */ |
| 295 | static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT); |
| 296 | module_param(sev, int, 0444); |
| 297 | |
Paolo Bonzini | 79a8059 | 2015-09-21 07:46:55 +0200 | [diff] [blame] | 298 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 299 | static void svm_flush_tlb(struct kvm_vcpu *vcpu); |
Joerg Roedel | a5c3832 | 2009-08-07 11:49:32 +0200 | [diff] [blame] | 300 | static void svm_complete_interrupts(struct vcpu_svm *svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 301 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 302 | static int nested_svm_exit_handled(struct vcpu_svm *svm); |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 303 | static int nested_svm_intercept(struct vcpu_svm *svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 304 | static int nested_svm_vmexit(struct vcpu_svm *svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 305 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 306 | bool has_error_code, u32 error_code); |
| 307 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 308 | enum { |
Joerg Roedel | 116a0a2 | 2010-12-03 11:45:49 +0100 | [diff] [blame] | 309 | VMCB_INTERCEPTS, /* Intercept vectors, TSC offset, |
| 310 | pause filter count */ |
Joerg Roedel | f56838e | 2010-12-03 11:45:50 +0100 | [diff] [blame] | 311 | VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */ |
Joerg Roedel | d48086d | 2010-12-03 11:45:51 +0100 | [diff] [blame] | 312 | VMCB_ASID, /* ASID */ |
Joerg Roedel | decdbf6 | 2010-12-03 11:45:52 +0100 | [diff] [blame] | 313 | VMCB_INTR, /* int_ctl, int_vector */ |
Joerg Roedel | b274716 | 2010-12-03 11:45:53 +0100 | [diff] [blame] | 314 | VMCB_NPT, /* npt_en, nCR3, gPAT */ |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 315 | VMCB_CR, /* CR0, CR3, CR4, EFER */ |
Joerg Roedel | 72214b9 | 2010-12-03 11:45:55 +0100 | [diff] [blame] | 316 | VMCB_DR, /* DR6, DR7 */ |
Joerg Roedel | 17a703c | 2010-12-03 11:45:56 +0100 | [diff] [blame] | 317 | VMCB_DT, /* GDT, IDT */ |
Joerg Roedel | 060d0c9 | 2010-12-03 11:45:57 +0100 | [diff] [blame] | 318 | VMCB_SEG, /* CS, DS, SS, ES, CPL */ |
Joerg Roedel | 0574dec | 2010-12-03 11:45:58 +0100 | [diff] [blame] | 319 | VMCB_CR2, /* CR2 only */ |
Joerg Roedel | b53ba3f | 2010-12-03 11:45:59 +0100 | [diff] [blame] | 320 | 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] | 321 | VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE, |
| 322 | * AVIC PHYSICAL_TABLE pointer, |
| 323 | * AVIC LOGICAL_TABLE pointer |
| 324 | */ |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 325 | VMCB_DIRTY_MAX, |
| 326 | }; |
| 327 | |
Joerg Roedel | 0574dec | 2010-12-03 11:45:58 +0100 | [diff] [blame] | 328 | /* TPR and CR2 are always written before VMRUN */ |
| 329 | #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 330 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 331 | #define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL |
| 332 | |
Brijesh Singh | ed3cd23 | 2017-12-04 10:57:32 -0600 | [diff] [blame] | 333 | static unsigned int max_sev_asid; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 334 | static unsigned int min_sev_asid; |
| 335 | static unsigned long *sev_asid_bitmap; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 336 | #define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT) |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 337 | |
| 338 | static inline bool svm_sev_enabled(void) |
| 339 | { |
| 340 | return max_sev_asid; |
| 341 | } |
| 342 | |
| 343 | static inline bool sev_guest(struct kvm *kvm) |
| 344 | { |
| 345 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 346 | |
| 347 | return sev->active; |
| 348 | } |
Brijesh Singh | ed3cd23 | 2017-12-04 10:57:32 -0600 | [diff] [blame] | 349 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 350 | static inline int sev_get_asid(struct kvm *kvm) |
| 351 | { |
| 352 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 353 | |
| 354 | return sev->asid; |
| 355 | } |
| 356 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 357 | static inline void mark_all_dirty(struct vmcb *vmcb) |
| 358 | { |
| 359 | vmcb->control.clean = 0; |
| 360 | } |
| 361 | |
| 362 | static inline void mark_all_clean(struct vmcb *vmcb) |
| 363 | { |
| 364 | vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1) |
| 365 | & ~VMCB_ALWAYS_DIRTY_MASK; |
| 366 | } |
| 367 | |
| 368 | static inline void mark_dirty(struct vmcb *vmcb, int bit) |
| 369 | { |
| 370 | vmcb->control.clean &= ~(1 << bit); |
| 371 | } |
| 372 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 373 | static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) |
| 374 | { |
| 375 | return container_of(vcpu, struct vcpu_svm, vcpu); |
| 376 | } |
| 377 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 378 | static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data) |
| 379 | { |
| 380 | svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK; |
| 381 | mark_dirty(svm->vmcb, VMCB_AVIC); |
| 382 | } |
| 383 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 384 | static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu) |
| 385 | { |
| 386 | struct vcpu_svm *svm = to_svm(vcpu); |
| 387 | u64 *entry = svm->avic_physical_id_cache; |
| 388 | |
| 389 | if (!entry) |
| 390 | return false; |
| 391 | |
| 392 | return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); |
| 393 | } |
| 394 | |
Joerg Roedel | 384c636 | 2010-11-30 18:03:56 +0100 | [diff] [blame] | 395 | static void recalc_intercepts(struct vcpu_svm *svm) |
| 396 | { |
| 397 | struct vmcb_control_area *c, *h; |
| 398 | struct nested_state *g; |
| 399 | |
Joerg Roedel | 116a0a2 | 2010-12-03 11:45:49 +0100 | [diff] [blame] | 400 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
| 401 | |
Joerg Roedel | 384c636 | 2010-11-30 18:03:56 +0100 | [diff] [blame] | 402 | if (!is_guest_mode(&svm->vcpu)) |
| 403 | return; |
| 404 | |
| 405 | c = &svm->vmcb->control; |
| 406 | h = &svm->nested.hsave->control; |
| 407 | g = &svm->nested; |
| 408 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 409 | c->intercept_cr = h->intercept_cr | g->intercept_cr; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 410 | c->intercept_dr = h->intercept_dr | g->intercept_dr; |
Joerg Roedel | 384c636 | 2010-11-30 18:03:56 +0100 | [diff] [blame] | 411 | c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions; |
| 412 | c->intercept = h->intercept | g->intercept; |
| 413 | } |
| 414 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 415 | static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm) |
| 416 | { |
| 417 | if (is_guest_mode(&svm->vcpu)) |
| 418 | return svm->nested.hsave; |
| 419 | else |
| 420 | return svm->vmcb; |
| 421 | } |
| 422 | |
| 423 | static inline void set_cr_intercept(struct vcpu_svm *svm, int bit) |
| 424 | { |
| 425 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 426 | |
| 427 | vmcb->control.intercept_cr |= (1U << bit); |
| 428 | |
| 429 | recalc_intercepts(svm); |
| 430 | } |
| 431 | |
| 432 | static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit) |
| 433 | { |
| 434 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 435 | |
| 436 | vmcb->control.intercept_cr &= ~(1U << bit); |
| 437 | |
| 438 | recalc_intercepts(svm); |
| 439 | } |
| 440 | |
| 441 | static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit) |
| 442 | { |
| 443 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 444 | |
| 445 | return vmcb->control.intercept_cr & (1U << bit); |
| 446 | } |
| 447 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 448 | static inline void set_dr_intercepts(struct vcpu_svm *svm) |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 449 | { |
| 450 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 451 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 452 | vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ) |
| 453 | | (1 << INTERCEPT_DR1_READ) |
| 454 | | (1 << INTERCEPT_DR2_READ) |
| 455 | | (1 << INTERCEPT_DR3_READ) |
| 456 | | (1 << INTERCEPT_DR4_READ) |
| 457 | | (1 << INTERCEPT_DR5_READ) |
| 458 | | (1 << INTERCEPT_DR6_READ) |
| 459 | | (1 << INTERCEPT_DR7_READ) |
| 460 | | (1 << INTERCEPT_DR0_WRITE) |
| 461 | | (1 << INTERCEPT_DR1_WRITE) |
| 462 | | (1 << INTERCEPT_DR2_WRITE) |
| 463 | | (1 << INTERCEPT_DR3_WRITE) |
| 464 | | (1 << INTERCEPT_DR4_WRITE) |
| 465 | | (1 << INTERCEPT_DR5_WRITE) |
| 466 | | (1 << INTERCEPT_DR6_WRITE) |
| 467 | | (1 << INTERCEPT_DR7_WRITE); |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 468 | |
| 469 | recalc_intercepts(svm); |
| 470 | } |
| 471 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 472 | static inline void clr_dr_intercepts(struct vcpu_svm *svm) |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 473 | { |
| 474 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 475 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 476 | vmcb->control.intercept_dr = 0; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 477 | |
| 478 | recalc_intercepts(svm); |
| 479 | } |
| 480 | |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 481 | static inline void set_exception_intercept(struct vcpu_svm *svm, int bit) |
| 482 | { |
| 483 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 484 | |
| 485 | vmcb->control.intercept_exceptions |= (1U << bit); |
| 486 | |
| 487 | recalc_intercepts(svm); |
| 488 | } |
| 489 | |
| 490 | static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit) |
| 491 | { |
| 492 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 493 | |
| 494 | vmcb->control.intercept_exceptions &= ~(1U << bit); |
| 495 | |
| 496 | recalc_intercepts(svm); |
| 497 | } |
| 498 | |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 499 | static inline void set_intercept(struct vcpu_svm *svm, int bit) |
| 500 | { |
| 501 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 502 | |
| 503 | vmcb->control.intercept |= (1ULL << bit); |
| 504 | |
| 505 | recalc_intercepts(svm); |
| 506 | } |
| 507 | |
| 508 | static inline void clr_intercept(struct vcpu_svm *svm, int bit) |
| 509 | { |
| 510 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 511 | |
| 512 | vmcb->control.intercept &= ~(1ULL << bit); |
| 513 | |
| 514 | recalc_intercepts(svm); |
| 515 | } |
| 516 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 517 | static inline bool vgif_enabled(struct vcpu_svm *svm) |
| 518 | { |
| 519 | return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK); |
| 520 | } |
| 521 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 522 | static inline void enable_gif(struct vcpu_svm *svm) |
| 523 | { |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 524 | if (vgif_enabled(svm)) |
| 525 | svm->vmcb->control.int_ctl |= V_GIF_MASK; |
| 526 | else |
| 527 | svm->vcpu.arch.hflags |= HF_GIF_MASK; |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static inline void disable_gif(struct vcpu_svm *svm) |
| 531 | { |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 532 | if (vgif_enabled(svm)) |
| 533 | svm->vmcb->control.int_ctl &= ~V_GIF_MASK; |
| 534 | else |
| 535 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static inline bool gif_set(struct vcpu_svm *svm) |
| 539 | { |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 540 | if (vgif_enabled(svm)) |
| 541 | return !!(svm->vmcb->control.int_ctl & V_GIF_MASK); |
| 542 | else |
| 543 | return !!(svm->vcpu.arch.hflags & HF_GIF_MASK); |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 546 | static unsigned long iopm_base; |
| 547 | |
| 548 | struct kvm_ldttss_desc { |
| 549 | u16 limit0; |
| 550 | u16 base0; |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 551 | unsigned base1:8, type:5, dpl:2, p:1; |
| 552 | unsigned limit1:4, zero0:3, g:1, base2:8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 553 | u32 base3; |
| 554 | u32 zero1; |
| 555 | } __attribute__((packed)); |
| 556 | |
| 557 | struct svm_cpu_data { |
| 558 | int cpu; |
| 559 | |
Avi Kivity | 5008fdf | 2007-04-02 13:05:50 +0300 | [diff] [blame] | 560 | u64 asid_generation; |
| 561 | u32 max_asid; |
| 562 | u32 next_asid; |
Brijesh Singh | 4faefff | 2017-12-04 10:57:25 -0600 | [diff] [blame] | 563 | u32 min_asid; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 564 | struct kvm_ldttss_desc *tss_desc; |
| 565 | |
| 566 | struct page *save_area; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 567 | |
| 568 | /* index = sev_asid, value = vmcb pointer */ |
| 569 | struct vmcb **sev_vmcbs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 570 | }; |
| 571 | |
| 572 | static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); |
| 573 | |
| 574 | struct svm_init_data { |
| 575 | int cpu; |
| 576 | int r; |
| 577 | }; |
| 578 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 579 | static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 580 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 581 | #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 582 | #define MSRS_RANGE_SIZE 2048 |
| 583 | #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) |
| 584 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 585 | static u32 svm_msrpm_offset(u32 msr) |
| 586 | { |
| 587 | u32 offset; |
| 588 | int i; |
| 589 | |
| 590 | for (i = 0; i < NUM_MSR_MAPS; i++) { |
| 591 | if (msr < msrpm_ranges[i] || |
| 592 | msr >= msrpm_ranges[i] + MSRS_IN_RANGE) |
| 593 | continue; |
| 594 | |
| 595 | offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */ |
| 596 | offset += (i * MSRS_RANGE_SIZE); /* add range offset */ |
| 597 | |
| 598 | /* Now we have the u8 offset - but need the u32 offset */ |
| 599 | return offset / 4; |
| 600 | } |
| 601 | |
| 602 | /* MSR not in any range */ |
| 603 | return MSR_INVALID; |
| 604 | } |
| 605 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 606 | #define MAX_INST_SIZE 15 |
| 607 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 608 | static inline void clgi(void) |
| 609 | { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 610 | asm volatile (__ex(SVM_CLGI)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | static inline void stgi(void) |
| 614 | { |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 615 | asm volatile (__ex(SVM_STGI)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static inline void invlpga(unsigned long addr, u32 asid) |
| 619 | { |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 620 | asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 623 | static int get_npt_level(struct kvm_vcpu *vcpu) |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 624 | { |
| 625 | #ifdef CONFIG_X86_64 |
Yu Zhang | 2a7266a | 2017-08-24 20:27:54 +0800 | [diff] [blame] | 626 | return PT64_ROOT_4LEVEL; |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 627 | #else |
| 628 | return PT32E_ROOT_LEVEL; |
| 629 | #endif |
| 630 | } |
| 631 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 632 | static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 633 | { |
Zachary Amsden | 6dc696d | 2010-05-26 15:09:43 -1000 | [diff] [blame] | 634 | vcpu->arch.efer = efer; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 635 | if (!npt_enabled && !(efer & EFER_LMA)) |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 636 | efer &= ~EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 637 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 638 | to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME; |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 639 | mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 642 | static int is_external_interrupt(u32 info) |
| 643 | { |
| 644 | info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; |
| 645 | return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); |
| 646 | } |
| 647 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 648 | static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 649 | { |
| 650 | struct vcpu_svm *svm = to_svm(vcpu); |
| 651 | u32 ret = 0; |
| 652 | |
| 653 | if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 654 | ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS; |
| 655 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 659 | { |
| 660 | struct vcpu_svm *svm = to_svm(vcpu); |
| 661 | |
| 662 | if (mask == 0) |
| 663 | svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; |
| 664 | else |
| 665 | svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK; |
| 666 | |
| 667 | } |
| 668 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 669 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 670 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 671 | struct vcpu_svm *svm = to_svm(vcpu); |
| 672 | |
Bandan Das | f104765 | 2015-06-11 02:05:33 -0400 | [diff] [blame] | 673 | if (svm->vmcb->control.next_rip != 0) { |
Dirk Müller | d292242 | 2015-10-01 13:43:42 +0200 | [diff] [blame] | 674 | WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS)); |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 675 | svm->next_rip = svm->vmcb->control.next_rip; |
Bandan Das | f104765 | 2015-06-11 02:05:33 -0400 | [diff] [blame] | 676 | } |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 677 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 678 | if (!svm->next_rip) { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 679 | if (emulate_instruction(vcpu, EMULTYPE_SKIP) != |
Gleb Natapov | f629cf8 | 2009-05-11 13:35:49 +0300 | [diff] [blame] | 680 | EMULATE_DONE) |
| 681 | printk(KERN_DEBUG "%s: NOP\n", __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 682 | return; |
| 683 | } |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 684 | if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE) |
| 685 | printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n", |
| 686 | __func__, kvm_rip_read(vcpu), svm->next_rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 687 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 688 | kvm_rip_write(vcpu, svm->next_rip); |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 689 | svm_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 692 | static void svm_queue_exception(struct kvm_vcpu *vcpu) |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 693 | { |
| 694 | struct vcpu_svm *svm = to_svm(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 695 | unsigned nr = vcpu->arch.exception.nr; |
| 696 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 697 | bool reinject = vcpu->arch.exception.injected; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 698 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 699 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 700 | /* |
| 701 | * If we are within a nested VM we'd better #VMEXIT and let the guest |
| 702 | * handle the exception |
| 703 | */ |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 704 | if (!reinject && |
| 705 | nested_svm_check_exception(svm, nr, has_error_code, error_code)) |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 706 | return; |
| 707 | |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 708 | if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) { |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 709 | unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu); |
| 710 | |
| 711 | /* |
| 712 | * For guest debugging where we have to reinject #BP if some |
| 713 | * INT3 is guest-owned: |
| 714 | * Emulate nRIP by moving RIP forward. Will fail if injection |
| 715 | * raises a fault that is not intercepted. Still better than |
| 716 | * failing in all cases. |
| 717 | */ |
| 718 | skip_emulated_instruction(&svm->vcpu); |
| 719 | rip = kvm_rip_read(&svm->vcpu); |
| 720 | svm->int3_rip = rip + svm->vmcb->save.cs.base; |
| 721 | svm->int3_injected = rip - old_rip; |
| 722 | } |
| 723 | |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 724 | svm->vmcb->control.event_inj = nr |
| 725 | | SVM_EVTINJ_VALID |
| 726 | | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) |
| 727 | | SVM_EVTINJ_TYPE_EXEPT; |
| 728 | svm->vmcb->control.event_inj_err = error_code; |
| 729 | } |
| 730 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 731 | static void svm_init_erratum_383(void) |
| 732 | { |
| 733 | u32 low, high; |
| 734 | int err; |
| 735 | u64 val; |
| 736 | |
Borislav Petkov | e6ee94d | 2013-03-20 15:07:27 +0100 | [diff] [blame] | 737 | if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH)) |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 738 | return; |
| 739 | |
| 740 | /* Use _safe variants to not break nested virtualization */ |
| 741 | val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err); |
| 742 | if (err) |
| 743 | return; |
| 744 | |
| 745 | val |= (1ULL << 47); |
| 746 | |
| 747 | low = lower_32_bits(val); |
| 748 | high = upper_32_bits(val); |
| 749 | |
| 750 | native_write_msr_safe(MSR_AMD64_DC_CFG, low, high); |
| 751 | |
| 752 | erratum_383_found = true; |
| 753 | } |
| 754 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 755 | static void svm_init_osvw(struct kvm_vcpu *vcpu) |
| 756 | { |
| 757 | /* |
| 758 | * Guests should see errata 400 and 415 as fixed (assuming that |
| 759 | * HLT and IO instructions are intercepted). |
| 760 | */ |
| 761 | vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3; |
| 762 | vcpu->arch.osvw.status = osvw_status & ~(6ULL); |
| 763 | |
| 764 | /* |
| 765 | * By increasing VCPU's osvw.length to 3 we are telling the guest that |
| 766 | * all osvw.status bits inside that length, including bit 0 (which is |
| 767 | * reserved for erratum 298), are valid. However, if host processor's |
| 768 | * osvw_len is 0 then osvw_status[0] carries no information. We need to |
| 769 | * be conservative here and therefore we tell the guest that erratum 298 |
| 770 | * is present (because we really don't know). |
| 771 | */ |
| 772 | if (osvw_len == 0 && boot_cpu_data.x86 == 0x10) |
| 773 | vcpu->arch.osvw.status |= 1; |
| 774 | } |
| 775 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 776 | static int has_svm(void) |
| 777 | { |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 778 | const char *msg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 779 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 780 | if (!cpu_has_svm(&msg)) { |
Joe Perches | ff81ff1 | 2009-01-08 11:05:17 -0800 | [diff] [blame] | 781 | printk(KERN_INFO "has_svm: %s\n", msg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 782 | return 0; |
| 783 | } |
| 784 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 785 | return 1; |
| 786 | } |
| 787 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 788 | static void svm_hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 789 | { |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 790 | /* Make sure we clean up behind us */ |
| 791 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) |
| 792 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); |
| 793 | |
Eduardo Habkost | 2c8dcee | 2008-11-17 19:03:21 -0200 | [diff] [blame] | 794 | cpu_svm_disable(); |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 795 | |
| 796 | amd_pmu_disable_virt(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 797 | } |
| 798 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 799 | static int svm_hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 800 | { |
| 801 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 802 | struct svm_cpu_data *sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 803 | uint64_t efer; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 804 | struct desc_struct *gdt; |
| 805 | int me = raw_smp_processor_id(); |
| 806 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 807 | rdmsrl(MSR_EFER, efer); |
| 808 | if (efer & EFER_SVME) |
| 809 | return -EBUSY; |
| 810 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 811 | if (!has_svm()) { |
Borislav Petkov | 1f5b77f | 2012-10-20 20:20:04 +0200 | [diff] [blame] | 812 | pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 813 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 814 | } |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 815 | sd = per_cpu(svm_data, me); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 816 | if (!sd) { |
Borislav Petkov | 1f5b77f | 2012-10-20 20:20:04 +0200 | [diff] [blame] | 817 | pr_err("%s: svm_data is NULL on %d\n", __func__, me); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 818 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 819 | } |
| 820 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 821 | sd->asid_generation = 1; |
| 822 | sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; |
| 823 | sd->next_asid = sd->max_asid + 1; |
Brijesh Singh | ed3cd23 | 2017-12-04 10:57:32 -0600 | [diff] [blame] | 824 | sd->min_asid = max_sev_asid + 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 825 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 826 | gdt = get_current_gdt_rw(); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 827 | sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 828 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 829 | wrmsrl(MSR_EFER, efer | EFER_SVME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 830 | |
Linus Torvalds | d031655 | 2009-12-14 09:58:24 -0800 | [diff] [blame] | 831 | 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] | 832 | |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 833 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
| 834 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 835 | __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT); |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 836 | } |
| 837 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 838 | |
| 839 | /* |
| 840 | * Get OSVW bits. |
| 841 | * |
| 842 | * Note that it is possible to have a system with mixed processor |
| 843 | * revisions and therefore different OSVW bits. If bits are not the same |
| 844 | * on different processors then choose the worst case (i.e. if erratum |
| 845 | * is present on one processor and not on another then assume that the |
| 846 | * erratum is present everywhere). |
| 847 | */ |
| 848 | if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) { |
| 849 | uint64_t len, status = 0; |
| 850 | int err; |
| 851 | |
| 852 | len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err); |
| 853 | if (!err) |
| 854 | status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS, |
| 855 | &err); |
| 856 | |
| 857 | if (err) |
| 858 | osvw_status = osvw_len = 0; |
| 859 | else { |
| 860 | if (len < osvw_len) |
| 861 | osvw_len = len; |
| 862 | osvw_status |= status; |
| 863 | osvw_status &= (1ULL << osvw_len) - 1; |
| 864 | } |
| 865 | } else |
| 866 | osvw_status = osvw_len = 0; |
| 867 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 868 | svm_init_erratum_383(); |
| 869 | |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 870 | amd_pmu_enable_virt(); |
| 871 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 872 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 873 | } |
| 874 | |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 875 | static void svm_cpu_uninit(int cpu) |
| 876 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 877 | 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] | 878 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 879 | if (!sd) |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 880 | return; |
| 881 | |
| 882 | per_cpu(svm_data, raw_smp_processor_id()) = NULL; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 883 | kfree(sd->sev_vmcbs); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 884 | __free_page(sd->save_area); |
| 885 | kfree(sd); |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 886 | } |
| 887 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 888 | static int svm_cpu_init(int cpu) |
| 889 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 890 | struct svm_cpu_data *sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 891 | int r; |
| 892 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 893 | sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); |
| 894 | if (!sd) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 895 | return -ENOMEM; |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 896 | sd->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 897 | r = -ENOMEM; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 898 | sd->save_area = alloc_page(GFP_KERNEL); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 899 | if (!sd->save_area) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 900 | goto err_1; |
| 901 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 902 | if (svm_sev_enabled()) { |
| 903 | r = -ENOMEM; |
| 904 | sd->sev_vmcbs = kmalloc((max_sev_asid + 1) * sizeof(void *), GFP_KERNEL); |
| 905 | if (!sd->sev_vmcbs) |
| 906 | goto err_1; |
| 907 | } |
| 908 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 909 | per_cpu(svm_data, cpu) = sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 910 | |
| 911 | return 0; |
| 912 | |
| 913 | err_1: |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 914 | kfree(sd); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 915 | return r; |
| 916 | |
| 917 | } |
| 918 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 919 | static bool valid_msr_intercept(u32 index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 920 | { |
| 921 | int i; |
| 922 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 923 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) |
| 924 | if (direct_access_msrs[i].index == index) |
| 925 | return true; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 926 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 927 | return false; |
| 928 | } |
| 929 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 930 | static void set_msr_interception(u32 *msrpm, unsigned msr, |
| 931 | int read, int write) |
| 932 | { |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 933 | u8 bit_read, bit_write; |
| 934 | unsigned long tmp; |
| 935 | u32 offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 936 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 937 | /* |
| 938 | * If this warning triggers extend the direct_access_msrs list at the |
| 939 | * beginning of the file |
| 940 | */ |
| 941 | WARN_ON(!valid_msr_intercept(msr)); |
| 942 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 943 | offset = svm_msrpm_offset(msr); |
| 944 | bit_read = 2 * (msr & 0x0f); |
| 945 | bit_write = 2 * (msr & 0x0f) + 1; |
| 946 | tmp = msrpm[offset]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 947 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 948 | BUG_ON(offset == MSR_INVALID); |
| 949 | |
| 950 | read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp); |
| 951 | write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp); |
| 952 | |
| 953 | msrpm[offset] = tmp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 954 | } |
| 955 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 956 | static void svm_vcpu_init_msrpm(u32 *msrpm) |
| 957 | { |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 958 | int i; |
| 959 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 960 | memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER)); |
| 961 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 962 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 963 | if (!direct_access_msrs[i].always) |
| 964 | continue; |
| 965 | |
| 966 | set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1); |
| 967 | } |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 968 | } |
| 969 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 970 | static void add_msr_offset(u32 offset) |
| 971 | { |
| 972 | int i; |
| 973 | |
| 974 | for (i = 0; i < MSRPM_OFFSETS; ++i) { |
| 975 | |
| 976 | /* Offset already in list? */ |
| 977 | if (msrpm_offsets[i] == offset) |
| 978 | return; |
| 979 | |
| 980 | /* Slot used by another offset? */ |
| 981 | if (msrpm_offsets[i] != MSR_INVALID) |
| 982 | continue; |
| 983 | |
| 984 | /* Add offset to list */ |
| 985 | msrpm_offsets[i] = offset; |
| 986 | |
| 987 | return; |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * If this BUG triggers the msrpm_offsets table has an overflow. Just |
| 992 | * increase MSRPM_OFFSETS in this case. |
| 993 | */ |
| 994 | BUG(); |
| 995 | } |
| 996 | |
| 997 | static void init_msrpm_offsets(void) |
| 998 | { |
| 999 | int i; |
| 1000 | |
| 1001 | memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets)); |
| 1002 | |
| 1003 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 1004 | u32 offset; |
| 1005 | |
| 1006 | offset = svm_msrpm_offset(direct_access_msrs[i].index); |
| 1007 | BUG_ON(offset == MSR_INVALID); |
| 1008 | |
| 1009 | add_msr_offset(offset); |
| 1010 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1011 | } |
| 1012 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 1013 | static void svm_enable_lbrv(struct vcpu_svm *svm) |
| 1014 | { |
| 1015 | u32 *msrpm = svm->msrpm; |
| 1016 | |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 1017 | svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK; |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 1018 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); |
| 1019 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); |
| 1020 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); |
| 1021 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1); |
| 1022 | } |
| 1023 | |
| 1024 | static void svm_disable_lbrv(struct vcpu_svm *svm) |
| 1025 | { |
| 1026 | u32 *msrpm = svm->msrpm; |
| 1027 | |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 1028 | svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK; |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 1029 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0); |
| 1030 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0); |
| 1031 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0); |
| 1032 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0); |
| 1033 | } |
| 1034 | |
Ladi Prosek | 4aebd0e | 2017-06-21 09:06:57 +0200 | [diff] [blame] | 1035 | static void disable_nmi_singlestep(struct vcpu_svm *svm) |
| 1036 | { |
| 1037 | svm->nmi_singlestep = false; |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 1038 | |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 1039 | if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) { |
| 1040 | /* Clear our flags if they were not set by the guest */ |
| 1041 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) |
| 1042 | svm->vmcb->save.rflags &= ~X86_EFLAGS_TF; |
| 1043 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) |
| 1044 | svm->vmcb->save.rflags &= ~X86_EFLAGS_RF; |
| 1045 | } |
Ladi Prosek | 4aebd0e | 2017-06-21 09:06:57 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1048 | /* Note: |
| 1049 | * This hash table is used to map VM_ID to a struct kvm_arch, |
| 1050 | * when handling AMD IOMMU GALOG notification to schedule in |
| 1051 | * a particular vCPU. |
| 1052 | */ |
| 1053 | #define SVM_VM_DATA_HASH_BITS 8 |
David Hildenbrand | 681bcea | 2017-01-24 22:21:16 +0100 | [diff] [blame] | 1054 | static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS); |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1055 | static u32 next_vm_id = 0; |
| 1056 | static bool next_vm_id_wrapped = 0; |
David Hildenbrand | 681bcea | 2017-01-24 22:21:16 +0100 | [diff] [blame] | 1057 | static DEFINE_SPINLOCK(svm_vm_data_hash_lock); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1058 | |
| 1059 | /* Note: |
| 1060 | * This function is called from IOMMU driver to notify |
| 1061 | * SVM to schedule in a particular vCPU of a particular VM. |
| 1062 | */ |
| 1063 | static int avic_ga_log_notifier(u32 ga_tag) |
| 1064 | { |
| 1065 | unsigned long flags; |
| 1066 | struct kvm_arch *ka = NULL; |
| 1067 | struct kvm_vcpu *vcpu = NULL; |
| 1068 | u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag); |
| 1069 | u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag); |
| 1070 | |
| 1071 | pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id); |
| 1072 | |
| 1073 | spin_lock_irqsave(&svm_vm_data_hash_lock, flags); |
| 1074 | hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) { |
| 1075 | struct kvm *kvm = container_of(ka, struct kvm, arch); |
| 1076 | struct kvm_arch *vm_data = &kvm->arch; |
| 1077 | |
| 1078 | if (vm_data->avic_vm_id != vm_id) |
| 1079 | continue; |
| 1080 | vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); |
| 1081 | break; |
| 1082 | } |
| 1083 | spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); |
| 1084 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1085 | /* Note: |
| 1086 | * At this point, the IOMMU should have already set the pending |
| 1087 | * bit in the vAPIC backing page. So, we just need to schedule |
| 1088 | * in the vcpu. |
| 1089 | */ |
Paolo Bonzini | 1cf5358 | 2017-10-10 12:51:56 +0200 | [diff] [blame] | 1090 | if (vcpu) |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1091 | kvm_vcpu_wake_up(vcpu); |
| 1092 | |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 1096 | static __init int sev_hardware_setup(void) |
| 1097 | { |
| 1098 | struct sev_user_data_status *status; |
| 1099 | int rc; |
| 1100 | |
| 1101 | /* Maximum number of encrypted guests supported simultaneously */ |
| 1102 | max_sev_asid = cpuid_ecx(0x8000001F); |
| 1103 | |
| 1104 | if (!max_sev_asid) |
| 1105 | return 1; |
| 1106 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1107 | /* Minimum ASID value that should be used for SEV guest */ |
| 1108 | min_sev_asid = cpuid_edx(0x8000001F); |
| 1109 | |
| 1110 | /* Initialize SEV ASID bitmap */ |
| 1111 | sev_asid_bitmap = kcalloc(BITS_TO_LONGS(max_sev_asid), |
| 1112 | sizeof(unsigned long), GFP_KERNEL); |
| 1113 | if (!sev_asid_bitmap) |
| 1114 | return 1; |
| 1115 | |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 1116 | status = kmalloc(sizeof(*status), GFP_KERNEL); |
| 1117 | if (!status) |
| 1118 | return 1; |
| 1119 | |
| 1120 | /* |
| 1121 | * Check SEV platform status. |
| 1122 | * |
| 1123 | * PLATFORM_STATUS can be called in any state, if we failed to query |
| 1124 | * the PLATFORM status then either PSP firmware does not support SEV |
| 1125 | * feature or SEV firmware is dead. |
| 1126 | */ |
| 1127 | rc = sev_platform_status(status, NULL); |
| 1128 | if (rc) |
| 1129 | goto err; |
| 1130 | |
| 1131 | pr_info("SEV supported\n"); |
| 1132 | |
| 1133 | err: |
| 1134 | kfree(status); |
| 1135 | return rc; |
| 1136 | } |
| 1137 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1138 | static __init int svm_hardware_setup(void) |
| 1139 | { |
| 1140 | int cpu; |
| 1141 | struct page *iopm_pages; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1142 | void *iopm_va; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1143 | int r; |
| 1144 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1145 | iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER); |
| 1146 | |
| 1147 | if (!iopm_pages) |
| 1148 | return -ENOMEM; |
Anthony Liguori | c868133 | 2007-04-30 09:48:11 +0300 | [diff] [blame] | 1149 | |
| 1150 | iopm_va = page_address(iopm_pages); |
| 1151 | memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1152 | iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; |
| 1153 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 1154 | init_msrpm_offsets(); |
| 1155 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 1156 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 1157 | kvm_enable_efer_bits(EFER_NX); |
| 1158 | |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 1159 | if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) |
| 1160 | kvm_enable_efer_bits(EFER_FFXSR); |
| 1161 | |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 1162 | if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 1163 | kvm_has_tsc_control = true; |
Haozhong Zhang | bc9b961 | 2015-10-20 15:39:01 +0800 | [diff] [blame] | 1164 | kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX; |
| 1165 | kvm_tsc_scaling_ratio_frac_bits = 32; |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 1166 | } |
| 1167 | |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 1168 | if (nested) { |
| 1169 | printk(KERN_INFO "kvm: Nested Virtualization enabled\n"); |
Joerg Roedel | eec4b14 | 2010-05-05 16:04:44 +0200 | [diff] [blame] | 1170 | kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE); |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 1173 | if (sev) { |
| 1174 | if (boot_cpu_has(X86_FEATURE_SEV) && |
| 1175 | IS_ENABLED(CONFIG_KVM_AMD_SEV)) { |
| 1176 | r = sev_hardware_setup(); |
| 1177 | if (r) |
| 1178 | sev = false; |
| 1179 | } else { |
| 1180 | sev = false; |
| 1181 | } |
| 1182 | } |
| 1183 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 1184 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1185 | r = svm_cpu_init(cpu); |
| 1186 | if (r) |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1187 | goto err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1188 | } |
Joerg Roedel | 33bd6a0 | 2008-02-07 13:47:38 +0100 | [diff] [blame] | 1189 | |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 1190 | if (!boot_cpu_has(X86_FEATURE_NPT)) |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 1191 | npt_enabled = false; |
| 1192 | |
Joerg Roedel | 6c7dac7 | 2008-02-07 13:47:40 +0100 | [diff] [blame] | 1193 | if (npt_enabled && !npt) { |
| 1194 | printk(KERN_INFO "kvm: Nested Paging disabled\n"); |
| 1195 | npt_enabled = false; |
| 1196 | } |
| 1197 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 1198 | if (npt_enabled) { |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 1199 | printk(KERN_INFO "kvm: Nested Paging enabled\n"); |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 1200 | kvm_enable_tdp(); |
Joerg Roedel | 5f4cb66 | 2008-07-14 20:36:36 +0200 | [diff] [blame] | 1201 | } else |
| 1202 | kvm_disable_tdp(); |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 1203 | |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1204 | if (avic) { |
| 1205 | if (!npt_enabled || |
| 1206 | !boot_cpu_has(X86_FEATURE_AVIC) || |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1207 | !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) { |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1208 | avic = false; |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1209 | } else { |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1210 | pr_info("AVIC enabled\n"); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1211 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1212 | amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); |
| 1213 | } |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1214 | } |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1215 | |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 1216 | if (vls) { |
| 1217 | if (!npt_enabled || |
Borislav Petkov | 5442c26 | 2017-08-01 20:55:52 +0200 | [diff] [blame] | 1218 | !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) || |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 1219 | !IS_ENABLED(CONFIG_X86_64)) { |
| 1220 | vls = false; |
| 1221 | } else { |
| 1222 | pr_info("Virtual VMLOAD VMSAVE supported\n"); |
| 1223 | } |
| 1224 | } |
| 1225 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 1226 | if (vgif) { |
| 1227 | if (!boot_cpu_has(X86_FEATURE_VGIF)) |
| 1228 | vgif = false; |
| 1229 | else |
| 1230 | pr_info("Virtual GIF supported\n"); |
| 1231 | } |
| 1232 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1233 | return 0; |
| 1234 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1235 | err: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1236 | __free_pages(iopm_pages, IOPM_ALLOC_ORDER); |
| 1237 | iopm_base = 0; |
| 1238 | return r; |
| 1239 | } |
| 1240 | |
| 1241 | static __exit void svm_hardware_unsetup(void) |
| 1242 | { |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 1243 | int cpu; |
| 1244 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1245 | if (svm_sev_enabled()) |
| 1246 | kfree(sev_asid_bitmap); |
| 1247 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 1248 | for_each_possible_cpu(cpu) |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 1249 | svm_cpu_uninit(cpu); |
| 1250 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1251 | __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1252 | iopm_base = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | static void init_seg(struct vmcb_seg *seg) |
| 1256 | { |
| 1257 | seg->selector = 0; |
| 1258 | seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1259 | SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1260 | seg->limit = 0xffff; |
| 1261 | seg->base = 0; |
| 1262 | } |
| 1263 | |
| 1264 | static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) |
| 1265 | { |
| 1266 | seg->selector = 0; |
| 1267 | seg->attrib = SVM_SELECTOR_P_MASK | type; |
| 1268 | seg->limit = 0xffff; |
| 1269 | seg->base = 0; |
| 1270 | } |
| 1271 | |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1272 | static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
| 1273 | { |
| 1274 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1275 | u64 g_tsc_offset = 0; |
| 1276 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 1277 | if (is_guest_mode(vcpu)) { |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1278 | g_tsc_offset = svm->vmcb->control.tsc_offset - |
| 1279 | svm->nested.hsave->control.tsc_offset; |
| 1280 | svm->nested.hsave->control.tsc_offset = offset; |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 1281 | } else |
| 1282 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 1283 | svm->vmcb->control.tsc_offset, |
| 1284 | offset); |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1285 | |
| 1286 | svm->vmcb->control.tsc_offset = offset + g_tsc_offset; |
Joerg Roedel | 116a0a2 | 2010-12-03 11:45:49 +0100 | [diff] [blame] | 1287 | |
| 1288 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1289 | } |
| 1290 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1291 | static void avic_init_vmcb(struct vcpu_svm *svm) |
| 1292 | { |
| 1293 | struct vmcb *vmcb = svm->vmcb; |
| 1294 | struct kvm_arch *vm_data = &svm->vcpu.kvm->arch; |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1295 | phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page)); |
| 1296 | phys_addr_t lpa = __sme_set(page_to_phys(vm_data->avic_logical_id_table_page)); |
| 1297 | phys_addr_t ppa = __sme_set(page_to_phys(vm_data->avic_physical_id_table_page)); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1298 | |
| 1299 | vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK; |
| 1300 | vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK; |
| 1301 | vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK; |
| 1302 | vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT; |
| 1303 | vmcb->control.int_ctl |= AVIC_ENABLE_MASK; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1304 | } |
| 1305 | |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 1306 | static void init_vmcb(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1307 | { |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 1308 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 1309 | struct vmcb_save_area *save = &svm->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1310 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 1311 | svm->vcpu.arch.hflags = 0; |
Avi Kivity | bff7827 | 2010-01-07 13:16:08 +0200 | [diff] [blame] | 1312 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 1313 | set_cr_intercept(svm, INTERCEPT_CR0_READ); |
| 1314 | set_cr_intercept(svm, INTERCEPT_CR3_READ); |
| 1315 | set_cr_intercept(svm, INTERCEPT_CR4_READ); |
| 1316 | set_cr_intercept(svm, INTERCEPT_CR0_WRITE); |
| 1317 | set_cr_intercept(svm, INTERCEPT_CR3_WRITE); |
| 1318 | set_cr_intercept(svm, INTERCEPT_CR4_WRITE); |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 1319 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) |
| 1320 | set_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1321 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 1322 | set_dr_intercepts(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1323 | |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 1324 | set_exception_intercept(svm, PF_VECTOR); |
| 1325 | set_exception_intercept(svm, UD_VECTOR); |
| 1326 | set_exception_intercept(svm, MC_VECTOR); |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 1327 | set_exception_intercept(svm, AC_VECTOR); |
Paolo Bonzini | cbdb967 | 2015-11-10 09:14:39 +0100 | [diff] [blame] | 1328 | set_exception_intercept(svm, DB_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1329 | |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1330 | set_intercept(svm, INTERCEPT_INTR); |
| 1331 | set_intercept(svm, INTERCEPT_NMI); |
| 1332 | set_intercept(svm, INTERCEPT_SMI); |
| 1333 | set_intercept(svm, INTERCEPT_SELECTIVE_CR0); |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 1334 | set_intercept(svm, INTERCEPT_RDPMC); |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1335 | set_intercept(svm, INTERCEPT_CPUID); |
| 1336 | set_intercept(svm, INTERCEPT_INVD); |
| 1337 | set_intercept(svm, INTERCEPT_HLT); |
| 1338 | set_intercept(svm, INTERCEPT_INVLPG); |
| 1339 | set_intercept(svm, INTERCEPT_INVLPGA); |
| 1340 | set_intercept(svm, INTERCEPT_IOIO_PROT); |
| 1341 | set_intercept(svm, INTERCEPT_MSR_PROT); |
| 1342 | set_intercept(svm, INTERCEPT_TASK_SWITCH); |
| 1343 | set_intercept(svm, INTERCEPT_SHUTDOWN); |
| 1344 | set_intercept(svm, INTERCEPT_VMRUN); |
| 1345 | set_intercept(svm, INTERCEPT_VMMCALL); |
| 1346 | set_intercept(svm, INTERCEPT_VMLOAD); |
| 1347 | set_intercept(svm, INTERCEPT_VMSAVE); |
| 1348 | set_intercept(svm, INTERCEPT_STGI); |
| 1349 | set_intercept(svm, INTERCEPT_CLGI); |
| 1350 | set_intercept(svm, INTERCEPT_SKINIT); |
| 1351 | set_intercept(svm, INTERCEPT_WBINVD); |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 1352 | set_intercept(svm, INTERCEPT_XSETBV); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1353 | |
Michael S. Tsirkin | 668fffa | 2017-04-21 12:27:17 +0200 | [diff] [blame] | 1354 | if (!kvm_mwait_in_guest()) { |
| 1355 | set_intercept(svm, INTERCEPT_MONITOR); |
| 1356 | set_intercept(svm, INTERCEPT_MWAIT); |
| 1357 | } |
| 1358 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1359 | control->iopm_base_pa = __sme_set(iopm_base); |
| 1360 | control->msrpm_base_pa = __sme_set(__pa(svm->msrpm)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1361 | control->int_ctl = V_INTR_MASKING_MASK; |
| 1362 | |
| 1363 | init_seg(&save->es); |
| 1364 | init_seg(&save->ss); |
| 1365 | init_seg(&save->ds); |
| 1366 | init_seg(&save->fs); |
| 1367 | init_seg(&save->gs); |
| 1368 | |
| 1369 | save->cs.selector = 0xf000; |
Paolo Bonzini | 04b6683 | 2013-03-19 16:30:26 +0100 | [diff] [blame] | 1370 | save->cs.base = 0xffff0000; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1371 | /* Executable/Readable Code Segment */ |
| 1372 | save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | |
| 1373 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; |
| 1374 | save->cs.limit = 0xffff; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1375 | |
| 1376 | save->gdtr.limit = 0xffff; |
| 1377 | save->idtr.limit = 0xffff; |
| 1378 | |
| 1379 | init_sys_seg(&save->ldtr, SEG_TYPE_LDT); |
| 1380 | init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); |
| 1381 | |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 1382 | svm_set_efer(&svm->vcpu, 0); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1383 | save->dr6 = 0xffff0ff0; |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 1384 | kvm_set_rflags(&svm->vcpu, 2); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1385 | save->rip = 0x0000fff0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1386 | svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1387 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1388 | /* |
Eduardo Habkost | 18fa000 | 2009-10-24 02:49:59 -0200 | [diff] [blame] | 1389 | * 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] | 1390 | * It also updates the guest-visible cr0 value. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1391 | */ |
Paolo Bonzini | 79a8059 | 2015-09-21 07:46:55 +0200 | [diff] [blame] | 1392 | 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] | 1393 | kvm_mmu_reset_context(&svm->vcpu); |
Eduardo Habkost | 18fa000 | 2009-10-24 02:49:59 -0200 | [diff] [blame] | 1394 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1395 | save->cr4 = X86_CR4_PAE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1396 | /* rdx = ?? */ |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 1397 | |
| 1398 | if (npt_enabled) { |
| 1399 | /* Setup VMCB for Nested Paging */ |
Tom Lendacky | cea3a19 | 2017-12-04 10:57:24 -0600 | [diff] [blame] | 1400 | control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1401 | clr_intercept(svm, INTERCEPT_INVLPG); |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 1402 | clr_exception_intercept(svm, PF_VECTOR); |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 1403 | clr_cr_intercept(svm, INTERCEPT_CR3_READ); |
| 1404 | clr_cr_intercept(svm, INTERCEPT_CR3_WRITE); |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 1405 | save->g_pat = svm->vcpu.arch.pat; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 1406 | save->cr3 = 0; |
| 1407 | save->cr4 = 0; |
| 1408 | } |
Joerg Roedel | f40f6a4 | 2010-12-03 15:25:15 +0100 | [diff] [blame] | 1409 | svm->asid_generation = 0; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 1410 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1411 | svm->nested.vmcb = 0; |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 1412 | svm->vcpu.arch.hflags = 0; |
| 1413 | |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 1414 | if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) { |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 1415 | control->pause_filter_count = 3000; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1416 | set_intercept(svm, INTERCEPT_PAUSE); |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 1417 | } |
| 1418 | |
Suravee Suthikulpanit | 67034bb | 2017-09-12 10:42:42 -0500 | [diff] [blame] | 1419 | if (kvm_vcpu_apicv_active(&svm->vcpu)) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1420 | avic_init_vmcb(svm); |
| 1421 | |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 1422 | /* |
| 1423 | * If hardware supports Virtual VMLOAD VMSAVE then enable it |
| 1424 | * in VMCB and clear intercepts to avoid #VMEXIT. |
| 1425 | */ |
| 1426 | if (vls) { |
| 1427 | clr_intercept(svm, INTERCEPT_VMLOAD); |
| 1428 | clr_intercept(svm, INTERCEPT_VMSAVE); |
| 1429 | svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; |
| 1430 | } |
| 1431 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 1432 | if (vgif) { |
| 1433 | clr_intercept(svm, INTERCEPT_STGI); |
| 1434 | clr_intercept(svm, INTERCEPT_CLGI); |
| 1435 | svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK; |
| 1436 | } |
| 1437 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1438 | if (sev_guest(svm->vcpu.kvm)) |
| 1439 | svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE; |
| 1440 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 1441 | mark_all_dirty(svm->vmcb); |
| 1442 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 1443 | enable_gif(svm); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1444 | |
| 1445 | } |
| 1446 | |
Dan Carpenter | d3e7dec | 2017-05-18 10:38:53 +0300 | [diff] [blame] | 1447 | static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu, |
| 1448 | unsigned int index) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1449 | { |
| 1450 | u64 *avic_physical_id_table; |
| 1451 | struct kvm_arch *vm_data = &vcpu->kvm->arch; |
| 1452 | |
| 1453 | if (index >= AVIC_MAX_PHYSICAL_ID_COUNT) |
| 1454 | return NULL; |
| 1455 | |
| 1456 | avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page); |
| 1457 | |
| 1458 | return &avic_physical_id_table[index]; |
| 1459 | } |
| 1460 | |
| 1461 | /** |
| 1462 | * Note: |
| 1463 | * AVIC hardware walks the nested page table to check permissions, |
| 1464 | * but does not use the SPA address specified in the leaf page |
| 1465 | * table entry since it uses address in the AVIC_BACKING_PAGE pointer |
| 1466 | * field of the VMCB. Therefore, we set up the |
| 1467 | * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here. |
| 1468 | */ |
| 1469 | static int avic_init_access_page(struct kvm_vcpu *vcpu) |
| 1470 | { |
| 1471 | struct kvm *kvm = vcpu->kvm; |
| 1472 | int ret; |
| 1473 | |
| 1474 | if (kvm->arch.apic_access_page_done) |
| 1475 | return 0; |
| 1476 | |
| 1477 | ret = x86_set_memory_region(kvm, |
| 1478 | APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 1479 | APIC_DEFAULT_PHYS_BASE, |
| 1480 | PAGE_SIZE); |
| 1481 | if (ret) |
| 1482 | return ret; |
| 1483 | |
| 1484 | kvm->arch.apic_access_page_done = true; |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | static int avic_init_backing_page(struct kvm_vcpu *vcpu) |
| 1489 | { |
| 1490 | int ret; |
| 1491 | u64 *entry, new_entry; |
| 1492 | int id = vcpu->vcpu_id; |
| 1493 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1494 | |
| 1495 | ret = avic_init_access_page(vcpu); |
| 1496 | if (ret) |
| 1497 | return ret; |
| 1498 | |
| 1499 | if (id >= AVIC_MAX_PHYSICAL_ID_COUNT) |
| 1500 | return -EINVAL; |
| 1501 | |
| 1502 | if (!svm->vcpu.arch.apic->regs) |
| 1503 | return -EINVAL; |
| 1504 | |
| 1505 | svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs); |
| 1506 | |
| 1507 | /* Setting AVIC backing page address in the phy APIC ID table */ |
| 1508 | entry = avic_get_physical_id_entry(vcpu, id); |
| 1509 | if (!entry) |
| 1510 | return -EINVAL; |
| 1511 | |
| 1512 | new_entry = READ_ONCE(*entry); |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1513 | new_entry = __sme_set((page_to_phys(svm->avic_backing_page) & |
| 1514 | AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) | |
| 1515 | AVIC_PHYSICAL_ID_ENTRY_VALID_MASK); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1516 | WRITE_ONCE(*entry, new_entry); |
| 1517 | |
| 1518 | svm->avic_physical_id_cache = entry; |
| 1519 | |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1523 | static void __sev_asid_free(int asid) |
| 1524 | { |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1525 | struct svm_cpu_data *sd; |
| 1526 | int cpu, pos; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1527 | |
| 1528 | pos = asid - 1; |
| 1529 | clear_bit(pos, sev_asid_bitmap); |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1530 | |
| 1531 | for_each_possible_cpu(cpu) { |
| 1532 | sd = per_cpu(svm_data, cpu); |
| 1533 | sd->sev_vmcbs[pos] = NULL; |
| 1534 | } |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | static void sev_asid_free(struct kvm *kvm) |
| 1538 | { |
| 1539 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 1540 | |
| 1541 | __sev_asid_free(sev->asid); |
| 1542 | } |
| 1543 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1544 | static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) |
| 1545 | { |
| 1546 | struct sev_data_decommission *decommission; |
| 1547 | struct sev_data_deactivate *data; |
| 1548 | |
| 1549 | if (!handle) |
| 1550 | return; |
| 1551 | |
| 1552 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 1553 | if (!data) |
| 1554 | return; |
| 1555 | |
| 1556 | /* deactivate handle */ |
| 1557 | data->handle = handle; |
| 1558 | sev_guest_deactivate(data, NULL); |
| 1559 | |
| 1560 | wbinvd_on_all_cpus(); |
| 1561 | sev_guest_df_flush(NULL); |
| 1562 | kfree(data); |
| 1563 | |
| 1564 | decommission = kzalloc(sizeof(*decommission), GFP_KERNEL); |
| 1565 | if (!decommission) |
| 1566 | return; |
| 1567 | |
| 1568 | /* decommission handle */ |
| 1569 | decommission->handle = handle; |
| 1570 | sev_guest_decommission(decommission, NULL); |
| 1571 | |
| 1572 | kfree(decommission); |
| 1573 | } |
| 1574 | |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1575 | static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr, |
| 1576 | unsigned long ulen, unsigned long *n, |
| 1577 | int write) |
| 1578 | { |
| 1579 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 1580 | unsigned long npages, npinned, size; |
| 1581 | unsigned long locked, lock_limit; |
| 1582 | struct page **pages; |
| 1583 | int first, last; |
| 1584 | |
| 1585 | /* Calculate number of pages. */ |
| 1586 | first = (uaddr & PAGE_MASK) >> PAGE_SHIFT; |
| 1587 | last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT; |
| 1588 | npages = (last - first + 1); |
| 1589 | |
| 1590 | locked = sev->pages_locked + npages; |
| 1591 | lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 1592 | if (locked > lock_limit && !capable(CAP_IPC_LOCK)) { |
| 1593 | pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit); |
| 1594 | return NULL; |
| 1595 | } |
| 1596 | |
| 1597 | /* Avoid using vmalloc for smaller buffers. */ |
| 1598 | size = npages * sizeof(struct page *); |
| 1599 | if (size > PAGE_SIZE) |
| 1600 | pages = vmalloc(size); |
| 1601 | else |
| 1602 | pages = kmalloc(size, GFP_KERNEL); |
| 1603 | |
| 1604 | if (!pages) |
| 1605 | return NULL; |
| 1606 | |
| 1607 | /* Pin the user virtual address. */ |
| 1608 | npinned = get_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages); |
| 1609 | if (npinned != npages) { |
| 1610 | pr_err("SEV: Failure locking %lu pages.\n", npages); |
| 1611 | goto err; |
| 1612 | } |
| 1613 | |
| 1614 | *n = npages; |
| 1615 | sev->pages_locked = locked; |
| 1616 | |
| 1617 | return pages; |
| 1618 | |
| 1619 | err: |
| 1620 | if (npinned > 0) |
| 1621 | release_pages(pages, npinned); |
| 1622 | |
| 1623 | kvfree(pages); |
| 1624 | return NULL; |
| 1625 | } |
| 1626 | |
| 1627 | static void sev_unpin_memory(struct kvm *kvm, struct page **pages, |
| 1628 | unsigned long npages) |
| 1629 | { |
| 1630 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 1631 | |
| 1632 | release_pages(pages, npages); |
| 1633 | kvfree(pages); |
| 1634 | sev->pages_locked -= npages; |
| 1635 | } |
| 1636 | |
| 1637 | static void sev_clflush_pages(struct page *pages[], unsigned long npages) |
| 1638 | { |
| 1639 | uint8_t *page_virtual; |
| 1640 | unsigned long i; |
| 1641 | |
| 1642 | if (npages == 0 || pages == NULL) |
| 1643 | return; |
| 1644 | |
| 1645 | for (i = 0; i < npages; i++) { |
| 1646 | page_virtual = kmap_atomic(pages[i]); |
| 1647 | clflush_cache_range(page_virtual, PAGE_SIZE); |
| 1648 | kunmap_atomic(page_virtual); |
| 1649 | } |
| 1650 | } |
| 1651 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1652 | static void sev_vm_destroy(struct kvm *kvm) |
| 1653 | { |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1654 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 1655 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1656 | if (!sev_guest(kvm)) |
| 1657 | return; |
| 1658 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1659 | sev_unbind_asid(kvm, sev->handle); |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1660 | sev_asid_free(kvm); |
| 1661 | } |
| 1662 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1663 | static void avic_vm_destroy(struct kvm *kvm) |
| 1664 | { |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1665 | unsigned long flags; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1666 | struct kvm_arch *vm_data = &kvm->arch; |
| 1667 | |
Dmitry Vyukov | 3863dff | 2017-01-24 14:06:48 +0100 | [diff] [blame] | 1668 | if (!avic) |
| 1669 | return; |
| 1670 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1671 | if (vm_data->avic_logical_id_table_page) |
| 1672 | __free_page(vm_data->avic_logical_id_table_page); |
| 1673 | if (vm_data->avic_physical_id_table_page) |
| 1674 | __free_page(vm_data->avic_physical_id_table_page); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1675 | |
| 1676 | spin_lock_irqsave(&svm_vm_data_hash_lock, flags); |
| 1677 | hash_del(&vm_data->hnode); |
| 1678 | spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1679 | } |
| 1680 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1681 | static void svm_vm_destroy(struct kvm *kvm) |
| 1682 | { |
| 1683 | avic_vm_destroy(kvm); |
| 1684 | sev_vm_destroy(kvm); |
| 1685 | } |
| 1686 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1687 | static int avic_vm_init(struct kvm *kvm) |
| 1688 | { |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1689 | unsigned long flags; |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1690 | int err = -ENOMEM; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1691 | struct kvm_arch *vm_data = &kvm->arch; |
| 1692 | struct page *p_page; |
| 1693 | struct page *l_page; |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1694 | struct kvm_arch *ka; |
| 1695 | u32 vm_id; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1696 | |
| 1697 | if (!avic) |
| 1698 | return 0; |
| 1699 | |
| 1700 | /* Allocating physical APIC ID table (4KB) */ |
| 1701 | p_page = alloc_page(GFP_KERNEL); |
| 1702 | if (!p_page) |
| 1703 | goto free_avic; |
| 1704 | |
| 1705 | vm_data->avic_physical_id_table_page = p_page; |
| 1706 | clear_page(page_address(p_page)); |
| 1707 | |
| 1708 | /* Allocating logical APIC ID table (4KB) */ |
| 1709 | l_page = alloc_page(GFP_KERNEL); |
| 1710 | if (!l_page) |
| 1711 | goto free_avic; |
| 1712 | |
| 1713 | vm_data->avic_logical_id_table_page = l_page; |
| 1714 | clear_page(page_address(l_page)); |
| 1715 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1716 | spin_lock_irqsave(&svm_vm_data_hash_lock, flags); |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1717 | again: |
| 1718 | vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK; |
| 1719 | if (vm_id == 0) { /* id is 1-based, zero is not okay */ |
| 1720 | next_vm_id_wrapped = 1; |
| 1721 | goto again; |
| 1722 | } |
| 1723 | /* Is it still in use? Only possible if wrapped at least once */ |
| 1724 | if (next_vm_id_wrapped) { |
| 1725 | hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) { |
| 1726 | struct kvm *k2 = container_of(ka, struct kvm, arch); |
| 1727 | struct kvm_arch *vd2 = &k2->arch; |
| 1728 | if (vd2->avic_vm_id == vm_id) |
| 1729 | goto again; |
| 1730 | } |
| 1731 | } |
| 1732 | vm_data->avic_vm_id = vm_id; |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1733 | hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id); |
| 1734 | spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); |
| 1735 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1736 | return 0; |
| 1737 | |
| 1738 | free_avic: |
| 1739 | avic_vm_destroy(kvm); |
| 1740 | return err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1741 | } |
| 1742 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1743 | static inline int |
| 1744 | avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r) |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1745 | { |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1746 | int ret = 0; |
| 1747 | unsigned long flags; |
| 1748 | struct amd_svm_iommu_ir *ir; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1749 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1750 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1751 | if (!kvm_arch_has_assigned_device(vcpu->kvm)) |
| 1752 | return 0; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1753 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1754 | /* |
| 1755 | * Here, we go through the per-vcpu ir_list to update all existing |
| 1756 | * interrupt remapping table entry targeting this vcpu. |
| 1757 | */ |
| 1758 | spin_lock_irqsave(&svm->ir_list_lock, flags); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1759 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1760 | if (list_empty(&svm->ir_list)) |
| 1761 | goto out; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1762 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1763 | list_for_each_entry(ir, &svm->ir_list, node) { |
| 1764 | ret = amd_iommu_update_ga(cpu, r, ir->data); |
| 1765 | if (ret) |
| 1766 | break; |
| 1767 | } |
| 1768 | out: |
| 1769 | spin_unlock_irqrestore(&svm->ir_list_lock, flags); |
| 1770 | return ret; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1774 | { |
| 1775 | u64 entry; |
| 1776 | /* ID = 0xff (broadcast), ID > 0xff (reserved) */ |
Suravee Suthikulpanit | 7d669f5 | 2016-06-15 17:23:45 -0500 | [diff] [blame] | 1777 | int h_physical_id = kvm_cpu_get_apicid(cpu); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1778 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1779 | |
| 1780 | if (!kvm_vcpu_apicv_active(vcpu)) |
| 1781 | return; |
| 1782 | |
| 1783 | if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT)) |
| 1784 | return; |
| 1785 | |
| 1786 | entry = READ_ONCE(*(svm->avic_physical_id_cache)); |
| 1787 | WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); |
| 1788 | |
| 1789 | entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; |
| 1790 | entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); |
| 1791 | |
| 1792 | entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; |
| 1793 | if (svm->avic_is_running) |
| 1794 | entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; |
| 1795 | |
| 1796 | WRITE_ONCE(*(svm->avic_physical_id_cache), entry); |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1797 | avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, |
| 1798 | svm->avic_is_running); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | static void avic_vcpu_put(struct kvm_vcpu *vcpu) |
| 1802 | { |
| 1803 | u64 entry; |
| 1804 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1805 | |
| 1806 | if (!kvm_vcpu_apicv_active(vcpu)) |
| 1807 | return; |
| 1808 | |
| 1809 | entry = READ_ONCE(*(svm->avic_physical_id_cache)); |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1810 | if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) |
| 1811 | avic_update_iommu_vcpu_affinity(vcpu, -1, 0); |
| 1812 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1813 | entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; |
| 1814 | WRITE_ONCE(*(svm->avic_physical_id_cache), entry); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1815 | } |
| 1816 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 1817 | /** |
| 1818 | * This function is called during VCPU halt/unhalt. |
| 1819 | */ |
| 1820 | static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run) |
| 1821 | { |
| 1822 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1823 | |
| 1824 | svm->avic_is_running = is_run; |
| 1825 | if (is_run) |
| 1826 | avic_vcpu_load(vcpu, vcpu->cpu); |
| 1827 | else |
| 1828 | avic_vcpu_put(vcpu); |
| 1829 | } |
| 1830 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 1831 | static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1832 | { |
| 1833 | struct vcpu_svm *svm = to_svm(vcpu); |
Julian Stecklina | 66f7b72 | 2012-12-05 15:26:19 +0100 | [diff] [blame] | 1834 | u32 dummy; |
| 1835 | u32 eax = 1; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1836 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 1837 | if (!init_event) { |
| 1838 | svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE | |
| 1839 | MSR_IA32_APICBASE_ENABLE; |
| 1840 | if (kvm_vcpu_is_reset_bsp(&svm->vcpu)) |
| 1841 | svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP; |
| 1842 | } |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 1843 | init_vmcb(svm); |
Avi Kivity | 7043338 | 2007-11-07 12:57:23 +0200 | [diff] [blame] | 1844 | |
Yu Zhang | e911eb3 | 2017-08-24 20:27:52 +0800 | [diff] [blame] | 1845 | kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true); |
Julian Stecklina | 66f7b72 | 2012-12-05 15:26:19 +0100 | [diff] [blame] | 1846 | kvm_register_write(vcpu, VCPU_REGS_RDX, eax); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1847 | |
| 1848 | if (kvm_vcpu_apicv_active(vcpu) && !init_event) |
| 1849 | avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1850 | } |
| 1851 | |
Suravee Suthikulpanit | dfa2009 | 2017-09-12 10:42:40 -0500 | [diff] [blame] | 1852 | static int avic_init_vcpu(struct vcpu_svm *svm) |
| 1853 | { |
| 1854 | int ret; |
| 1855 | |
Suravee Suthikulpanit | 67034bb | 2017-09-12 10:42:42 -0500 | [diff] [blame] | 1856 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) |
Suravee Suthikulpanit | dfa2009 | 2017-09-12 10:42:40 -0500 | [diff] [blame] | 1857 | return 0; |
| 1858 | |
| 1859 | ret = avic_init_backing_page(&svm->vcpu); |
| 1860 | if (ret) |
| 1861 | return ret; |
| 1862 | |
| 1863 | INIT_LIST_HEAD(&svm->ir_list); |
| 1864 | spin_lock_init(&svm->ir_list_lock); |
| 1865 | |
| 1866 | return ret; |
| 1867 | } |
| 1868 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1869 | 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] | 1870 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1871 | struct vcpu_svm *svm; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1872 | struct page *page; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1873 | struct page *msrpm_pages; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 1874 | struct page *hsave_page; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1875 | struct page *nested_msrpm_pages; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1876 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1877 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1878 | svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1879 | if (!svm) { |
| 1880 | err = -ENOMEM; |
| 1881 | goto out; |
| 1882 | } |
| 1883 | |
| 1884 | err = kvm_vcpu_init(&svm->vcpu, kvm, id); |
| 1885 | if (err) |
| 1886 | goto free_svm; |
| 1887 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1888 | err = -ENOMEM; |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1889 | page = alloc_page(GFP_KERNEL); |
| 1890 | if (!page) |
| 1891 | goto uninit; |
| 1892 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1893 | msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 1894 | if (!msrpm_pages) |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1895 | goto free_page1; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1896 | |
| 1897 | nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
| 1898 | if (!nested_msrpm_pages) |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1899 | goto free_page2; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1900 | |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 1901 | hsave_page = alloc_page(GFP_KERNEL); |
| 1902 | if (!hsave_page) |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1903 | goto free_page3; |
| 1904 | |
Suravee Suthikulpanit | dfa2009 | 2017-09-12 10:42:40 -0500 | [diff] [blame] | 1905 | err = avic_init_vcpu(svm); |
| 1906 | if (err) |
| 1907 | goto free_page4; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1908 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1909 | /* We initialize this flag to true to make sure that the is_running |
| 1910 | * bit would be set the first time the vcpu is loaded. |
| 1911 | */ |
| 1912 | svm->avic_is_running = true; |
| 1913 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1914 | svm->nested.hsave = page_address(hsave_page); |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 1915 | |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1916 | svm->msrpm = page_address(msrpm_pages); |
| 1917 | svm_vcpu_init_msrpm(svm->msrpm); |
| 1918 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1919 | svm->nested.msrpm = page_address(nested_msrpm_pages); |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 1920 | svm_vcpu_init_msrpm(svm->nested.msrpm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 1921 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1922 | svm->vmcb = page_address(page); |
| 1923 | clear_page(svm->vmcb); |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1924 | svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1925 | svm->asid_generation = 0; |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 1926 | init_vmcb(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1927 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 1928 | svm_init_osvw(&svm->vcpu); |
| 1929 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1930 | return &svm->vcpu; |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 1931 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1932 | free_page4: |
| 1933 | __free_page(hsave_page); |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 1934 | free_page3: |
| 1935 | __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER); |
| 1936 | free_page2: |
| 1937 | __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER); |
| 1938 | free_page1: |
| 1939 | __free_page(page); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1940 | uninit: |
| 1941 | kvm_vcpu_uninit(&svm->vcpu); |
| 1942 | free_svm: |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 1943 | kmem_cache_free(kvm_vcpu_cache, svm); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1944 | out: |
| 1945 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | static void svm_free_vcpu(struct kvm_vcpu *vcpu) |
| 1949 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1950 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1951 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1952 | __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT)); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1953 | __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER); |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1954 | __free_page(virt_to_page(svm->nested.hsave)); |
| 1955 | __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1956 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 1957 | kmem_cache_free(kvm_vcpu_cache, svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1958 | } |
| 1959 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1960 | static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1961 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1962 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1963 | int i; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 1964 | |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 1965 | if (unlikely(cpu != vcpu->cpu)) { |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 1966 | svm->asid_generation = 0; |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 1967 | mark_all_dirty(svm->vmcb); |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 1968 | } |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1969 | |
Avi Kivity | 82ca2d1 | 2010-10-21 12:20:34 +0200 | [diff] [blame] | 1970 | #ifdef CONFIG_X86_64 |
| 1971 | rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base); |
| 1972 | #endif |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 1973 | savesegment(fs, svm->host.fs); |
| 1974 | savesegment(gs, svm->host.gs); |
| 1975 | svm->host.ldt = kvm_read_ldt(); |
| 1976 | |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1977 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1978 | rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 1979 | |
Haozhong Zhang | ad721883 | 2015-10-20 15:39:02 +0800 | [diff] [blame] | 1980 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
| 1981 | u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio; |
| 1982 | if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) { |
| 1983 | __this_cpu_write(current_tsc_ratio, tsc_ratio); |
| 1984 | wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio); |
| 1985 | } |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 1986 | } |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 1987 | /* This assumes that the kernel never uses MSR_TSC_AUX */ |
| 1988 | if (static_cpu_has(X86_FEATURE_RDTSCP)) |
| 1989 | wrmsrl(MSR_TSC_AUX, svm->tsc_aux); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1990 | |
| 1991 | avic_vcpu_load(vcpu, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | static void svm_vcpu_put(struct kvm_vcpu *vcpu) |
| 1995 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1996 | struct vcpu_svm *svm = to_svm(vcpu); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 1997 | int i; |
| 1998 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 1999 | avic_vcpu_put(vcpu); |
| 2000 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2001 | ++vcpu->stat.host_state_reload; |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 2002 | kvm_load_ldt(svm->host.ldt); |
| 2003 | #ifdef CONFIG_X86_64 |
| 2004 | loadsegment(fs, svm->host.fs); |
Andy Lutomirski | 296f781 | 2016-04-26 12:23:29 -0700 | [diff] [blame] | 2005 | wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase); |
Joerg Roedel | 893a5ab | 2011-01-14 16:45:01 +0100 | [diff] [blame] | 2006 | load_gs_index(svm->host.gs); |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 2007 | #else |
Avi Kivity | 831ca60 | 2011-03-08 16:09:51 +0200 | [diff] [blame] | 2008 | #ifdef CONFIG_X86_32_LAZY_GS |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 2009 | loadsegment(gs, svm->host.gs); |
| 2010 | #endif |
Avi Kivity | 831ca60 | 2011-03-08 16:09:51 +0200 | [diff] [blame] | 2011 | #endif |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 2012 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2013 | wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2014 | } |
| 2015 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2016 | static void svm_vcpu_blocking(struct kvm_vcpu *vcpu) |
| 2017 | { |
| 2018 | avic_set_running(vcpu, false); |
| 2019 | } |
| 2020 | |
| 2021 | static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu) |
| 2022 | { |
| 2023 | avic_set_running(vcpu, true); |
| 2024 | } |
| 2025 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2026 | static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) |
| 2027 | { |
Ladi Prosek | 9b61174 | 2017-06-21 09:06:59 +0200 | [diff] [blame] | 2028 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2029 | unsigned long rflags = svm->vmcb->save.rflags; |
| 2030 | |
| 2031 | if (svm->nmi_singlestep) { |
| 2032 | /* Hide our flags if they were not set by the guest */ |
| 2033 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) |
| 2034 | rflags &= ~X86_EFLAGS_TF; |
| 2035 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) |
| 2036 | rflags &= ~X86_EFLAGS_RF; |
| 2037 | } |
| 2038 | return rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2042 | { |
Ladi Prosek | 9b61174 | 2017-06-21 09:06:59 +0200 | [diff] [blame] | 2043 | if (to_svm(vcpu)->nmi_singlestep) |
| 2044 | rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 2045 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 2046 | /* |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 2047 | * Any change of EFLAGS.VM is accompanied by a reload of SS |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 2048 | * (caused by either a task switch or an inter-privilege IRET), |
| 2049 | * so we do not need to update the CPL here. |
| 2050 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2051 | to_svm(vcpu)->vmcb->save.rflags = rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2052 | } |
| 2053 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2054 | static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
| 2055 | { |
| 2056 | switch (reg) { |
| 2057 | case VCPU_EXREG_PDPTR: |
| 2058 | BUG_ON(!npt_enabled); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 2059 | load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2060 | break; |
| 2061 | default: |
| 2062 | BUG(); |
| 2063 | } |
| 2064 | } |
| 2065 | |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 2066 | static void svm_set_vintr(struct vcpu_svm *svm) |
| 2067 | { |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 2068 | set_intercept(svm, INTERCEPT_VINTR); |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | static void svm_clear_vintr(struct vcpu_svm *svm) |
| 2072 | { |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 2073 | clr_intercept(svm, INTERCEPT_VINTR); |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 2074 | } |
| 2075 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2076 | static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) |
| 2077 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2078 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2079 | |
| 2080 | switch (seg) { |
| 2081 | case VCPU_SREG_CS: return &save->cs; |
| 2082 | case VCPU_SREG_DS: return &save->ds; |
| 2083 | case VCPU_SREG_ES: return &save->es; |
| 2084 | case VCPU_SREG_FS: return &save->fs; |
| 2085 | case VCPU_SREG_GS: return &save->gs; |
| 2086 | case VCPU_SREG_SS: return &save->ss; |
| 2087 | case VCPU_SREG_TR: return &save->tr; |
| 2088 | case VCPU_SREG_LDTR: return &save->ldtr; |
| 2089 | } |
| 2090 | BUG(); |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 2091 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 2095 | { |
| 2096 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 2097 | |
| 2098 | return s->base; |
| 2099 | } |
| 2100 | |
| 2101 | static void svm_get_segment(struct kvm_vcpu *vcpu, |
| 2102 | struct kvm_segment *var, int seg) |
| 2103 | { |
| 2104 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 2105 | |
| 2106 | var->base = s->base; |
| 2107 | var->limit = s->limit; |
| 2108 | var->selector = s->selector; |
| 2109 | var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; |
| 2110 | var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; |
| 2111 | var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 2112 | var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; |
| 2113 | var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; |
| 2114 | var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; |
| 2115 | var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; |
Jim Mattson | 80112c8 | 2014-07-08 09:47:41 +0530 | [diff] [blame] | 2116 | |
| 2117 | /* |
| 2118 | * AMD CPUs circa 2014 track the G bit for all segments except CS. |
| 2119 | * However, the SVM spec states that the G bit is not observed by the |
| 2120 | * CPU, and some VMware virtual CPUs drop the G bit for all segments. |
| 2121 | * So let's synthesize a legal G bit for all segments, this helps |
| 2122 | * running KVM nested. It also helps cross-vendor migration, because |
| 2123 | * Intel's vmentry has a check on the 'G' bit. |
| 2124 | */ |
| 2125 | var->g = s->limit > 0xfffff; |
Amit Shah | 25022ac | 2008-10-27 09:04:17 +0000 | [diff] [blame] | 2126 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2127 | /* |
| 2128 | * 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] | 2129 | * for cross vendor migration purposes by "not present" |
| 2130 | */ |
Gioh Kim | 8eae957 | 2017-05-30 15:24:45 +0200 | [diff] [blame] | 2131 | var->unusable = !var->present; |
Andre Przywara | 19bca6a | 2009-04-28 12:45:30 +0200 | [diff] [blame] | 2132 | |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 2133 | switch (seg) { |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 2134 | case VCPU_SREG_TR: |
| 2135 | /* |
| 2136 | * Work around a bug where the busy flag in the tr selector |
| 2137 | * isn't exposed |
| 2138 | */ |
Amit Shah | c0d0982 | 2008-10-27 09:04:18 +0000 | [diff] [blame] | 2139 | var->type |= 0x2; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 2140 | break; |
| 2141 | case VCPU_SREG_DS: |
| 2142 | case VCPU_SREG_ES: |
| 2143 | case VCPU_SREG_FS: |
| 2144 | case VCPU_SREG_GS: |
| 2145 | /* |
| 2146 | * The accessed bit must always be set in the segment |
| 2147 | * descriptor cache, although it can be cleared in the |
| 2148 | * descriptor, the cached bit always remains at 1. Since |
| 2149 | * Intel has a check on this, set it here to support |
| 2150 | * cross-vendor migration. |
| 2151 | */ |
| 2152 | if (!var->unusable) |
| 2153 | var->type |= 0x1; |
| 2154 | break; |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 2155 | case VCPU_SREG_SS: |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2156 | /* |
| 2157 | * On AMD CPUs sometimes the DB bit in the segment |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 2158 | * descriptor is left as 1, although the whole segment has |
| 2159 | * been made unusable. Clear it here to pass an Intel VMX |
| 2160 | * entry check when cross vendor migrating. |
| 2161 | */ |
| 2162 | if (var->unusable) |
| 2163 | var->db = 0; |
Roman Pen | d9c1b54 | 2017-06-01 10:55:03 +0200 | [diff] [blame] | 2164 | /* This is symmetric with svm_set_segment() */ |
Jan Kiszka | 33b458d | 2014-06-29 17:12:43 +0200 | [diff] [blame] | 2165 | var->dpl = to_svm(vcpu)->vmcb->save.cpl; |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 2166 | break; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 2167 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2168 | } |
| 2169 | |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2170 | static int svm_get_cpl(struct kvm_vcpu *vcpu) |
| 2171 | { |
| 2172 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
| 2173 | |
| 2174 | return save->cpl; |
| 2175 | } |
| 2176 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2177 | 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] | 2178 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2179 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2180 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2181 | dt->size = svm->vmcb->save.idtr.limit; |
| 2182 | dt->address = svm->vmcb->save.idtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2183 | } |
| 2184 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2185 | 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] | 2186 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2187 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2188 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2189 | svm->vmcb->save.idtr.limit = dt->size; |
| 2190 | svm->vmcb->save.idtr.base = dt->address ; |
Joerg Roedel | 17a703c | 2010-12-03 11:45:56 +0100 | [diff] [blame] | 2191 | mark_dirty(svm->vmcb, VMCB_DT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2192 | } |
| 2193 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2194 | 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] | 2195 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2196 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2197 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2198 | dt->size = svm->vmcb->save.gdtr.limit; |
| 2199 | dt->address = svm->vmcb->save.gdtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2200 | } |
| 2201 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2202 | 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] | 2203 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2204 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2205 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2206 | svm->vmcb->save.gdtr.limit = dt->size; |
| 2207 | svm->vmcb->save.gdtr.base = dt->address ; |
Joerg Roedel | 17a703c | 2010-12-03 11:45:56 +0100 | [diff] [blame] | 2208 | mark_dirty(svm->vmcb, VMCB_DT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2209 | } |
| 2210 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 2211 | static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 2212 | { |
| 2213 | } |
| 2214 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 2215 | static void svm_decache_cr3(struct kvm_vcpu *vcpu) |
| 2216 | { |
| 2217 | } |
| 2218 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2219 | static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2220 | { |
| 2221 | } |
| 2222 | |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2223 | static void update_cr0_intercept(struct vcpu_svm *svm) |
| 2224 | { |
| 2225 | ulong gcr0 = svm->vcpu.arch.cr0; |
| 2226 | u64 *hcr0 = &svm->vmcb->save.cr0; |
| 2227 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2228 | *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK) |
| 2229 | | (gcr0 & SVM_CR0_SELECTIVE_MASK); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2230 | |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 2231 | mark_dirty(svm->vmcb, VMCB_CR); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2232 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2233 | if (gcr0 == *hcr0) { |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 2234 | clr_cr_intercept(svm, INTERCEPT_CR0_READ); |
| 2235 | clr_cr_intercept(svm, INTERCEPT_CR0_WRITE); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2236 | } else { |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 2237 | set_cr_intercept(svm, INTERCEPT_CR0_READ); |
| 2238 | set_cr_intercept(svm, INTERCEPT_CR0_WRITE); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2239 | } |
| 2240 | } |
| 2241 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2242 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 2243 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2244 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2245 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2246 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2247 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 2248 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2249 | vcpu->arch.efer |= EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 2250 | svm->vmcb->save.efer |= EFER_LMA | EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2251 | } |
| 2252 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2253 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2254 | vcpu->arch.efer &= ~EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 2255 | svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2256 | } |
| 2257 | } |
| 2258 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2259 | vcpu->arch.cr0 = cr0; |
Avi Kivity | 888f9f3 | 2010-01-10 12:14:04 +0200 | [diff] [blame] | 2260 | |
| 2261 | if (!npt_enabled) |
| 2262 | cr0 |= X86_CR0_PG | X86_CR0_WP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 2263 | |
Paolo Bonzini | bcf166a | 2015-10-01 13:19:55 +0200 | [diff] [blame] | 2264 | /* |
| 2265 | * re-enable caching here because the QEMU bios |
| 2266 | * does not do it - this results in some delay at |
| 2267 | * reboot |
| 2268 | */ |
| 2269 | if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
| 2270 | cr0 &= ~(X86_CR0_CD | X86_CR0_NW); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2271 | svm->vmcb->save.cr0 = cr0; |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 2272 | mark_dirty(svm->vmcb, VMCB_CR); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2273 | update_cr0_intercept(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2274 | } |
| 2275 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2276 | static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2277 | { |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 2278 | unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE; |
Joerg Roedel | e5eab0c | 2008-09-09 19:11:51 +0200 | [diff] [blame] | 2279 | unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4; |
| 2280 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2281 | if (cr4 & X86_CR4_VMXE) |
| 2282 | return 1; |
| 2283 | |
Joerg Roedel | e5eab0c | 2008-09-09 19:11:51 +0200 | [diff] [blame] | 2284 | if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE)) |
Joerg Roedel | f40f6a4 | 2010-12-03 15:25:15 +0100 | [diff] [blame] | 2285 | svm_flush_tlb(vcpu); |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 2286 | |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 2287 | vcpu->arch.cr4 = cr4; |
| 2288 | if (!npt_enabled) |
| 2289 | cr4 |= X86_CR4_PAE; |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 2290 | cr4 |= host_cr4_mce; |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 2291 | to_svm(vcpu)->vmcb->save.cr4 = cr4; |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 2292 | mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2293 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | static void svm_set_segment(struct kvm_vcpu *vcpu, |
| 2297 | struct kvm_segment *var, int seg) |
| 2298 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2299 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2300 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 2301 | |
| 2302 | s->base = var->base; |
| 2303 | s->limit = var->limit; |
| 2304 | s->selector = var->selector; |
Roman Pen | d9c1b54 | 2017-06-01 10:55:03 +0200 | [diff] [blame] | 2305 | s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); |
| 2306 | s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; |
| 2307 | s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; |
| 2308 | s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT; |
| 2309 | s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; |
| 2310 | s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; |
| 2311 | s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; |
| 2312 | s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 2313 | |
| 2314 | /* |
| 2315 | * This is always accurate, except if SYSRET returned to a segment |
| 2316 | * with SS.DPL != 3. Intel does not have this quirk, and always |
| 2317 | * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it |
| 2318 | * would entail passing the CPL to userspace and back. |
| 2319 | */ |
| 2320 | if (seg == VCPU_SREG_SS) |
Roman Pen | d9c1b54 | 2017-06-01 10:55:03 +0200 | [diff] [blame] | 2321 | /* This is symmetric with svm_get_segment() */ |
| 2322 | svm->vmcb->save.cpl = (var->dpl & 3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2323 | |
Joerg Roedel | 060d0c9 | 2010-12-03 11:45:57 +0100 | [diff] [blame] | 2324 | mark_dirty(svm->vmcb, VMCB_SEG); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2325 | } |
| 2326 | |
Paolo Bonzini | cbdb967 | 2015-11-10 09:14:39 +0100 | [diff] [blame] | 2327 | static void update_bp_intercept(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2328 | { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2329 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2330 | |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 2331 | clr_exception_intercept(svm, BP_VECTOR); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2332 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2333 | if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2334 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 2335 | set_exception_intercept(svm, BP_VECTOR); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2336 | } else |
| 2337 | vcpu->guest_debug = 0; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2338 | } |
| 2339 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 2340 | 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] | 2341 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 2342 | if (sd->next_asid > sd->max_asid) { |
| 2343 | ++sd->asid_generation; |
Brijesh Singh | 4faefff | 2017-12-04 10:57:25 -0600 | [diff] [blame] | 2344 | sd->next_asid = sd->min_asid; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2345 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2346 | } |
| 2347 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 2348 | svm->asid_generation = sd->asid_generation; |
| 2349 | svm->vmcb->control.asid = sd->next_asid++; |
Joerg Roedel | d48086d | 2010-12-03 11:45:51 +0100 | [diff] [blame] | 2350 | |
| 2351 | mark_dirty(svm->vmcb, VMCB_ASID); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2352 | } |
| 2353 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 2354 | static u64 svm_get_dr6(struct kvm_vcpu *vcpu) |
| 2355 | { |
| 2356 | return to_svm(vcpu)->vmcb->save.dr6; |
| 2357 | } |
| 2358 | |
| 2359 | static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value) |
| 2360 | { |
| 2361 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2362 | |
| 2363 | svm->vmcb->save.dr6 = value; |
| 2364 | mark_dirty(svm->vmcb, VMCB_DR); |
| 2365 | } |
| 2366 | |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 2367 | static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 2368 | { |
| 2369 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2370 | |
| 2371 | get_debugreg(vcpu->arch.db[0], 0); |
| 2372 | get_debugreg(vcpu->arch.db[1], 1); |
| 2373 | get_debugreg(vcpu->arch.db[2], 2); |
| 2374 | get_debugreg(vcpu->arch.db[3], 3); |
| 2375 | vcpu->arch.dr6 = svm_get_dr6(vcpu); |
| 2376 | vcpu->arch.dr7 = svm->vmcb->save.dr7; |
| 2377 | |
| 2378 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
| 2379 | set_dr_intercepts(svm); |
| 2380 | } |
| 2381 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 2382 | static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2383 | { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2384 | struct vcpu_svm *svm = to_svm(vcpu); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2385 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 2386 | svm->vmcb->save.dr7 = value; |
Joerg Roedel | 72214b9 | 2010-12-03 11:45:55 +0100 | [diff] [blame] | 2387 | mark_dirty(svm->vmcb, VMCB_DR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2388 | } |
| 2389 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2390 | static int pf_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2391 | { |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 2392 | u64 fault_address = svm->vmcb->control.exit_info_2; |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 2393 | u64 error_code = svm->vmcb->control.exit_info_1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2394 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 2395 | return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address, |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 2396 | svm->vmcb->control.insn_bytes, |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 2397 | svm->vmcb->control.insn_len); |
| 2398 | } |
| 2399 | |
| 2400 | static int npf_interception(struct vcpu_svm *svm) |
| 2401 | { |
| 2402 | u64 fault_address = svm->vmcb->control.exit_info_2; |
| 2403 | u64 error_code = svm->vmcb->control.exit_info_1; |
| 2404 | |
| 2405 | trace_kvm_page_fault(fault_address, error_code); |
| 2406 | return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code, |
| 2407 | svm->vmcb->control.insn_bytes, |
| 2408 | svm->vmcb->control.insn_len); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2409 | } |
| 2410 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2411 | static int db_interception(struct vcpu_svm *svm) |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2412 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2413 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2414 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2415 | if (!(svm->vcpu.guest_debug & |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2416 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) && |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 2417 | !svm->nmi_singlestep) { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2418 | kvm_queue_exception(&svm->vcpu, DB_VECTOR); |
| 2419 | return 1; |
| 2420 | } |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2421 | |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 2422 | if (svm->nmi_singlestep) { |
Ladi Prosek | 4aebd0e | 2017-06-21 09:06:57 +0200 | [diff] [blame] | 2423 | disable_nmi_singlestep(svm); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | if (svm->vcpu.guest_debug & |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2427 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) { |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2428 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 2429 | kvm_run->debug.arch.pc = |
| 2430 | svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 2431 | kvm_run->debug.arch.exception = DB_VECTOR; |
| 2432 | return 0; |
| 2433 | } |
| 2434 | |
| 2435 | return 1; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2436 | } |
| 2437 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2438 | static int bp_interception(struct vcpu_svm *svm) |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2439 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2440 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2441 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2442 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 2443 | kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 2444 | kvm_run->debug.arch.exception = BP_VECTOR; |
| 2445 | return 0; |
| 2446 | } |
| 2447 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2448 | static int ud_interception(struct vcpu_svm *svm) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2449 | { |
| 2450 | int er; |
| 2451 | |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 2452 | er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2453 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 2454 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2455 | return 1; |
| 2456 | } |
| 2457 | |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 2458 | static int ac_interception(struct vcpu_svm *svm) |
| 2459 | { |
| 2460 | kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0); |
| 2461 | return 1; |
| 2462 | } |
| 2463 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 2464 | static bool is_erratum_383(void) |
| 2465 | { |
| 2466 | int err, i; |
| 2467 | u64 value; |
| 2468 | |
| 2469 | if (!erratum_383_found) |
| 2470 | return false; |
| 2471 | |
| 2472 | value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err); |
| 2473 | if (err) |
| 2474 | return false; |
| 2475 | |
| 2476 | /* Bit 62 may or may not be set for this mce */ |
| 2477 | value &= ~(1ULL << 62); |
| 2478 | |
| 2479 | if (value != 0xb600000000010015ULL) |
| 2480 | return false; |
| 2481 | |
| 2482 | /* Clear MCi_STATUS registers */ |
| 2483 | for (i = 0; i < 6; ++i) |
| 2484 | native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0); |
| 2485 | |
| 2486 | value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err); |
| 2487 | if (!err) { |
| 2488 | u32 low, high; |
| 2489 | |
| 2490 | value &= ~(1ULL << 2); |
| 2491 | low = lower_32_bits(value); |
| 2492 | high = upper_32_bits(value); |
| 2493 | |
| 2494 | native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high); |
| 2495 | } |
| 2496 | |
| 2497 | /* Flush tlb to evict multi-match entries */ |
| 2498 | __flush_tlb_all(); |
| 2499 | |
| 2500 | return true; |
| 2501 | } |
| 2502 | |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 2503 | static void svm_handle_mce(struct vcpu_svm *svm) |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2504 | { |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 2505 | if (is_erratum_383()) { |
| 2506 | /* |
| 2507 | * Erratum 383 triggered. Guest state is corrupt so kill the |
| 2508 | * guest. |
| 2509 | */ |
| 2510 | pr_err("KVM: Guest triggered AMD Erratum 383\n"); |
| 2511 | |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 2512 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu); |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 2513 | |
| 2514 | return; |
| 2515 | } |
| 2516 | |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2517 | /* |
| 2518 | * On an #MC intercept the MCE handler is not called automatically in |
| 2519 | * the host. So do it by hand here. |
| 2520 | */ |
| 2521 | asm volatile ( |
| 2522 | "int $0x12\n"); |
| 2523 | /* not sure if we ever come back to this point */ |
| 2524 | |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 2525 | return; |
| 2526 | } |
| 2527 | |
| 2528 | static int mc_interception(struct vcpu_svm *svm) |
| 2529 | { |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2530 | return 1; |
| 2531 | } |
| 2532 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2533 | static int shutdown_interception(struct vcpu_svm *svm) |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2534 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2535 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2536 | |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2537 | /* |
| 2538 | * VMCB is undefined after a SHUTDOWN intercept |
| 2539 | * so reinitialize it. |
| 2540 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2541 | clear_page(svm->vmcb); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 2542 | init_vmcb(svm); |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2543 | |
| 2544 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 2545 | return 0; |
| 2546 | } |
| 2547 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2548 | static int io_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2549 | { |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2550 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2551 | u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 2552 | int size, in, string, ret; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2553 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2554 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2555 | ++svm->vcpu.stat.io_exits; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2556 | string = (io_info & SVM_IOIO_STR_MASK) != 0; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2557 | in = (io_info & SVM_IOIO_TYPE_MASK) != 0; |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 2558 | if (string) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 2559 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2560 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2561 | port = io_info >> 16; |
| 2562 | size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2563 | svm->next_rip = svm->vmcb->control.exit_info_2; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 2564 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2565 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 2566 | /* |
| 2567 | * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered |
| 2568 | * KVM_EXIT_DEBUG here. |
| 2569 | */ |
| 2570 | if (in) |
| 2571 | return kvm_fast_pio_in(vcpu, size, port) && ret; |
| 2572 | else |
| 2573 | return kvm_fast_pio_out(vcpu, size, port) && ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2574 | } |
| 2575 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2576 | static int nmi_interception(struct vcpu_svm *svm) |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 2577 | { |
| 2578 | return 1; |
| 2579 | } |
| 2580 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2581 | static int intr_interception(struct vcpu_svm *svm) |
Joerg Roedel | a069805 | 2008-04-30 17:56:01 +0200 | [diff] [blame] | 2582 | { |
| 2583 | ++svm->vcpu.stat.irq_exits; |
| 2584 | return 1; |
| 2585 | } |
| 2586 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2587 | static int nop_on_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2588 | { |
| 2589 | return 1; |
| 2590 | } |
| 2591 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2592 | static int halt_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2593 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2594 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 1; |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2595 | return kvm_emulate_halt(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2596 | } |
| 2597 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2598 | static int vmmcall_interception(struct vcpu_svm *svm) |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2599 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2600 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 2601 | return kvm_emulate_hypercall(&svm->vcpu); |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2602 | } |
| 2603 | |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2604 | static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) |
| 2605 | { |
| 2606 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2607 | |
| 2608 | return svm->nested.nested_cr3; |
| 2609 | } |
| 2610 | |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 2611 | static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) |
| 2612 | { |
| 2613 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2614 | u64 cr3 = svm->nested.nested_cr3; |
| 2615 | u64 pdpte; |
| 2616 | int ret; |
| 2617 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 2618 | ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(__sme_clr(cr3)), &pdpte, |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 2619 | offset_in_page(cr3) + index * 8, 8); |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 2620 | if (ret) |
| 2621 | return 0; |
| 2622 | return pdpte; |
| 2623 | } |
| 2624 | |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2625 | static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu, |
| 2626 | unsigned long root) |
| 2627 | { |
| 2628 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2629 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 2630 | svm->vmcb->control.nested_cr3 = __sme_set(root); |
Joerg Roedel | b274716 | 2010-12-03 11:45:53 +0100 | [diff] [blame] | 2631 | mark_dirty(svm->vmcb, VMCB_NPT); |
Joerg Roedel | f40f6a4 | 2010-12-03 15:25:15 +0100 | [diff] [blame] | 2632 | svm_flush_tlb(vcpu); |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2633 | } |
| 2634 | |
Avi Kivity | 6389ee9 | 2010-11-29 16:12:30 +0200 | [diff] [blame] | 2635 | static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, |
| 2636 | struct x86_exception *fault) |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2637 | { |
| 2638 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2639 | |
Paolo Bonzini | 5e35251 | 2014-09-02 13:18:37 +0200 | [diff] [blame] | 2640 | if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) { |
| 2641 | /* |
| 2642 | * TODO: track the cause of the nested page fault, and |
| 2643 | * correctly fill in the high bits of exit_info_1. |
| 2644 | */ |
| 2645 | svm->vmcb->control.exit_code = SVM_EXIT_NPF; |
| 2646 | svm->vmcb->control.exit_code_hi = 0; |
| 2647 | svm->vmcb->control.exit_info_1 = (1ULL << 32); |
| 2648 | svm->vmcb->control.exit_info_2 = fault->address; |
| 2649 | } |
| 2650 | |
| 2651 | svm->vmcb->control.exit_info_1 &= ~0xffffffffULL; |
| 2652 | svm->vmcb->control.exit_info_1 |= fault->error_code; |
| 2653 | |
| 2654 | /* |
| 2655 | * The present bit is always zero for page structure faults on real |
| 2656 | * hardware. |
| 2657 | */ |
| 2658 | if (svm->vmcb->control.exit_info_1 & (2ULL << 32)) |
| 2659 | svm->vmcb->control.exit_info_1 &= ~1; |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2660 | |
| 2661 | nested_svm_vmexit(svm); |
| 2662 | } |
| 2663 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 2664 | static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2665 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 2666 | WARN_ON(mmu_is_nested(vcpu)); |
| 2667 | kvm_init_shadow_mmu(vcpu); |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2668 | vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3; |
| 2669 | vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3; |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 2670 | vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr; |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2671 | vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit; |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 2672 | vcpu->arch.mmu.shadow_root_level = get_npt_level(vcpu); |
Xiao Guangrong | c258b62 | 2015-08-05 12:04:24 +0800 | [diff] [blame] | 2673 | reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu); |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2674 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 2678 | { |
| 2679 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 2680 | } |
| 2681 | |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 2682 | static int nested_svm_check_permissions(struct vcpu_svm *svm) |
| 2683 | { |
Dan Carpenter | e9196ce | 2017-05-18 10:39:53 +0300 | [diff] [blame] | 2684 | if (!(svm->vcpu.arch.efer & EFER_SVME) || |
| 2685 | !is_paging(&svm->vcpu)) { |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 2686 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 2687 | return 1; |
| 2688 | } |
| 2689 | |
| 2690 | if (svm->vmcb->save.cpl) { |
| 2691 | kvm_inject_gp(&svm->vcpu, 0); |
| 2692 | return 1; |
| 2693 | } |
| 2694 | |
Dan Carpenter | e9196ce | 2017-05-18 10:39:53 +0300 | [diff] [blame] | 2695 | return 0; |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 2696 | } |
| 2697 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2698 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 2699 | bool has_error_code, u32 error_code) |
| 2700 | { |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 2701 | int vmexit; |
| 2702 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 2703 | if (!is_guest_mode(&svm->vcpu)) |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 2704 | return 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2705 | |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2706 | vmexit = nested_svm_intercept(svm); |
| 2707 | if (vmexit != NESTED_EXIT_DONE) |
| 2708 | return 0; |
| 2709 | |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 2710 | svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; |
| 2711 | svm->vmcb->control.exit_code_hi = 0; |
| 2712 | svm->vmcb->control.exit_info_1 = error_code; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2713 | |
| 2714 | /* |
| 2715 | * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception. |
| 2716 | * The fix is to add the ancillary datum (CR2 or DR6) to structs |
| 2717 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be |
| 2718 | * written only when inject_pending_event runs (DR6 would written here |
| 2719 | * too). This should be conditional on a new capability---if the |
| 2720 | * capability is disabled, kvm_multiple_exception would write the |
| 2721 | * ancillary information to CR2 or DR6, for backwards ABI-compatibility. |
| 2722 | */ |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2723 | if (svm->vcpu.arch.exception.nested_apf) |
| 2724 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token; |
| 2725 | else |
| 2726 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 2727 | |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2728 | svm->nested.exit_required = true; |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 2729 | return vmexit; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2730 | } |
| 2731 | |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 2732 | /* This function returns true if it is save to enable the irq window */ |
| 2733 | static inline bool nested_svm_intr(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2734 | { |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 2735 | if (!is_guest_mode(&svm->vcpu)) |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 2736 | return true; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2737 | |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 2738 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 2739 | return true; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2740 | |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 2741 | if (!(svm->vcpu.arch.hflags & HF_HIF_MASK)) |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 2742 | return false; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2743 | |
Gleb Natapov | a0a07cd | 2010-09-20 10:15:32 +0200 | [diff] [blame] | 2744 | /* |
| 2745 | * if vmexit was already requested (by intercepted exception |
| 2746 | * for instance) do not overwrite it with "external interrupt" |
| 2747 | * vmexit. |
| 2748 | */ |
| 2749 | if (svm->nested.exit_required) |
| 2750 | return false; |
| 2751 | |
Joerg Roedel | 197717d | 2010-02-24 18:59:19 +0100 | [diff] [blame] | 2752 | svm->vmcb->control.exit_code = SVM_EXIT_INTR; |
| 2753 | svm->vmcb->control.exit_info_1 = 0; |
| 2754 | svm->vmcb->control.exit_info_2 = 0; |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 2755 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 2756 | if (svm->nested.intercept & 1ULL) { |
| 2757 | /* |
| 2758 | * The #vmexit can't be emulated here directly because this |
Guo Chao | c5ec2e5 | 2012-06-28 15:16:43 +0800 | [diff] [blame] | 2759 | * code path runs with irqs and preemption disabled. A |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 2760 | * #vmexit emulation might sleep. Only signal request for |
| 2761 | * the #vmexit here. |
| 2762 | */ |
| 2763 | svm->nested.exit_required = true; |
Joerg Roedel | 236649d | 2009-10-09 16:08:30 +0200 | [diff] [blame] | 2764 | trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 2765 | return false; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2766 | } |
| 2767 | |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 2768 | return true; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2769 | } |
| 2770 | |
Joerg Roedel | 887f500 | 2010-02-24 18:59:12 +0100 | [diff] [blame] | 2771 | /* This function returns true if it is save to enable the nmi window */ |
| 2772 | static inline bool nested_svm_nmi(struct vcpu_svm *svm) |
| 2773 | { |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 2774 | if (!is_guest_mode(&svm->vcpu)) |
Joerg Roedel | 887f500 | 2010-02-24 18:59:12 +0100 | [diff] [blame] | 2775 | return true; |
| 2776 | |
| 2777 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI))) |
| 2778 | return true; |
| 2779 | |
| 2780 | svm->vmcb->control.exit_code = SVM_EXIT_NMI; |
| 2781 | svm->nested.exit_required = true; |
| 2782 | |
| 2783 | return false; |
| 2784 | } |
| 2785 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2786 | 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] | 2787 | { |
| 2788 | struct page *page; |
| 2789 | |
Joerg Roedel | 6c3bd3d | 2010-02-19 16:23:04 +0100 | [diff] [blame] | 2790 | might_sleep(); |
| 2791 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 2792 | page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 2793 | if (is_error_page(page)) |
| 2794 | goto error; |
| 2795 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2796 | *_page = page; |
| 2797 | |
| 2798 | return kmap(page); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 2799 | |
| 2800 | error: |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 2801 | kvm_inject_gp(&svm->vcpu, 0); |
| 2802 | |
| 2803 | return NULL; |
| 2804 | } |
| 2805 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2806 | static void nested_svm_unmap(struct page *page) |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 2807 | { |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 2808 | kunmap(page); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 2809 | kvm_release_page_dirty(page); |
| 2810 | } |
| 2811 | |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2812 | static int nested_svm_intercept_ioio(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2813 | { |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 2814 | unsigned port, size, iopm_len; |
| 2815 | u16 val, mask; |
| 2816 | u8 start_bit; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2817 | u64 gpa; |
| 2818 | |
| 2819 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT))) |
| 2820 | return NESTED_EXIT_HOST; |
| 2821 | |
| 2822 | port = svm->vmcb->control.exit_info_1 >> 16; |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 2823 | size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >> |
| 2824 | SVM_IOIO_SIZE_SHIFT; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2825 | gpa = svm->nested.vmcb_iopm + (port / 8); |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 2826 | start_bit = port % 8; |
| 2827 | iopm_len = (start_bit + size > 8) ? 2 : 1; |
| 2828 | mask = (0xf >> (4 - size)) << start_bit; |
| 2829 | val = 0; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2830 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 2831 | if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len)) |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 2832 | return NESTED_EXIT_DONE; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2833 | |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 2834 | return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2835 | } |
| 2836 | |
Joerg Roedel | d247782 | 2010-03-01 15:34:34 +0100 | [diff] [blame] | 2837 | static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2838 | { |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 2839 | u32 offset, msr, value; |
| 2840 | int write, mask; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 2841 | |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 2842 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
Joerg Roedel | d247782 | 2010-03-01 15:34:34 +0100 | [diff] [blame] | 2843 | return NESTED_EXIT_HOST; |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 2844 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 2845 | msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
| 2846 | offset = svm_msrpm_offset(msr); |
| 2847 | write = svm->vmcb->control.exit_info_1 & 1; |
| 2848 | mask = 1 << ((2 * (msr & 0xf)) + write); |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 2849 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 2850 | if (offset == MSR_INVALID) |
| 2851 | return NESTED_EXIT_DONE; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 2852 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 2853 | /* Offset is in 32 bit units but need in 8 bit units */ |
| 2854 | offset *= 4; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 2855 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 2856 | 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] | 2857 | return NESTED_EXIT_DONE; |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 2858 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 2859 | return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 2860 | } |
| 2861 | |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 2862 | /* DB exceptions for our internal use must not cause vmexit */ |
| 2863 | static int nested_svm_intercept_db(struct vcpu_svm *svm) |
| 2864 | { |
| 2865 | unsigned long dr6; |
| 2866 | |
| 2867 | /* if we're not singlestepping, it's not ours */ |
| 2868 | if (!svm->nmi_singlestep) |
| 2869 | return NESTED_EXIT_DONE; |
| 2870 | |
| 2871 | /* if it's not a singlestep exception, it's not ours */ |
| 2872 | if (kvm_get_dr(&svm->vcpu, 6, &dr6)) |
| 2873 | return NESTED_EXIT_DONE; |
| 2874 | if (!(dr6 & DR6_BS)) |
| 2875 | return NESTED_EXIT_DONE; |
| 2876 | |
| 2877 | /* if the guest is singlestepping, it should get the vmexit */ |
| 2878 | if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) { |
| 2879 | disable_nmi_singlestep(svm); |
| 2880 | return NESTED_EXIT_DONE; |
| 2881 | } |
| 2882 | |
| 2883 | /* it's ours, the nested hypervisor must not see this one */ |
| 2884 | return NESTED_EXIT_HOST; |
| 2885 | } |
| 2886 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2887 | static int nested_svm_exit_special(struct vcpu_svm *svm) |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 2888 | { |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2889 | u32 exit_code = svm->vmcb->control.exit_code; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 2890 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2891 | switch (exit_code) { |
| 2892 | case SVM_EXIT_INTR: |
| 2893 | case SVM_EXIT_NMI: |
Joerg Roedel | ff47a49 | 2010-04-22 12:33:14 +0200 | [diff] [blame] | 2894 | case SVM_EXIT_EXCP_BASE + MC_VECTOR: |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2895 | return NESTED_EXIT_HOST; |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2896 | case SVM_EXIT_NPF: |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2897 | /* For now we are always handling NPFs when using them */ |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2898 | if (npt_enabled) |
| 2899 | return NESTED_EXIT_HOST; |
| 2900 | break; |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2901 | case SVM_EXIT_EXCP_BASE + PF_VECTOR: |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 2902 | /* When we're shadowing, trap PFs, but not async PF */ |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 2903 | if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2904 | return NESTED_EXIT_HOST; |
| 2905 | break; |
| 2906 | default: |
| 2907 | break; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2908 | } |
| 2909 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2910 | return NESTED_EXIT_CONTINUE; |
| 2911 | } |
| 2912 | |
| 2913 | /* |
| 2914 | * If this function returns true, this #vmexit was already handled |
| 2915 | */ |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 2916 | static int nested_svm_intercept(struct vcpu_svm *svm) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2917 | { |
| 2918 | u32 exit_code = svm->vmcb->control.exit_code; |
| 2919 | int vmexit = NESTED_EXIT_HOST; |
| 2920 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2921 | switch (exit_code) { |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 2922 | case SVM_EXIT_MSR: |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 2923 | vmexit = nested_svm_exit_handled_msr(svm); |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 2924 | break; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 2925 | case SVM_EXIT_IOIO: |
| 2926 | vmexit = nested_svm_intercept_ioio(svm); |
| 2927 | break; |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 2928 | case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { |
| 2929 | u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0); |
| 2930 | if (svm->nested.intercept_cr & bit) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2931 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2932 | break; |
| 2933 | } |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 2934 | case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { |
| 2935 | u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0); |
| 2936 | if (svm->nested.intercept_dr & bit) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2937 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2938 | break; |
| 2939 | } |
| 2940 | case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { |
| 2941 | u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 2942 | if (svm->nested.intercept_exceptions & excp_bits) { |
| 2943 | if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR) |
| 2944 | vmexit = nested_svm_intercept_db(svm); |
| 2945 | else |
| 2946 | vmexit = NESTED_EXIT_DONE; |
| 2947 | } |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 2948 | /* async page fault always cause vmexit */ |
| 2949 | else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) && |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2950 | svm->vcpu.arch.exception.nested_apf != 0) |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 2951 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2952 | break; |
| 2953 | } |
Joerg Roedel | 228070b | 2010-04-22 12:33:10 +0200 | [diff] [blame] | 2954 | case SVM_EXIT_ERR: { |
| 2955 | vmexit = NESTED_EXIT_DONE; |
| 2956 | break; |
| 2957 | } |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2958 | default: { |
| 2959 | u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 2960 | if (svm->nested.intercept & exit_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 2961 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2962 | } |
| 2963 | } |
| 2964 | |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 2965 | return vmexit; |
| 2966 | } |
| 2967 | |
| 2968 | static int nested_svm_exit_handled(struct vcpu_svm *svm) |
| 2969 | { |
| 2970 | int vmexit; |
| 2971 | |
| 2972 | vmexit = nested_svm_intercept(svm); |
| 2973 | |
| 2974 | if (vmexit == NESTED_EXIT_DONE) |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 2975 | nested_svm_vmexit(svm); |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 2976 | |
| 2977 | return vmexit; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 2978 | } |
| 2979 | |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 2980 | static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb) |
| 2981 | { |
| 2982 | struct vmcb_control_area *dst = &dst_vmcb->control; |
| 2983 | struct vmcb_control_area *from = &from_vmcb->control; |
| 2984 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 2985 | dst->intercept_cr = from->intercept_cr; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 2986 | dst->intercept_dr = from->intercept_dr; |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 2987 | dst->intercept_exceptions = from->intercept_exceptions; |
| 2988 | dst->intercept = from->intercept; |
| 2989 | dst->iopm_base_pa = from->iopm_base_pa; |
| 2990 | dst->msrpm_base_pa = from->msrpm_base_pa; |
| 2991 | dst->tsc_offset = from->tsc_offset; |
| 2992 | dst->asid = from->asid; |
| 2993 | dst->tlb_ctl = from->tlb_ctl; |
| 2994 | dst->int_ctl = from->int_ctl; |
| 2995 | dst->int_vector = from->int_vector; |
| 2996 | dst->int_state = from->int_state; |
| 2997 | dst->exit_code = from->exit_code; |
| 2998 | dst->exit_code_hi = from->exit_code_hi; |
| 2999 | dst->exit_info_1 = from->exit_info_1; |
| 3000 | dst->exit_info_2 = from->exit_info_2; |
| 3001 | dst->exit_int_info = from->exit_int_info; |
| 3002 | dst->exit_int_info_err = from->exit_int_info_err; |
| 3003 | dst->nested_ctl = from->nested_ctl; |
| 3004 | dst->event_inj = from->event_inj; |
| 3005 | dst->event_inj_err = from->event_inj_err; |
| 3006 | dst->nested_cr3 = from->nested_cr3; |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 3007 | dst->virt_ext = from->virt_ext; |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 3008 | } |
| 3009 | |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 3010 | static int nested_svm_vmexit(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3011 | { |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 3012 | struct vmcb *nested_vmcb; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 3013 | struct vmcb *hsave = svm->nested.hsave; |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3014 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3015 | struct page *page; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3016 | |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 3017 | trace_kvm_nested_vmexit_inject(vmcb->control.exit_code, |
| 3018 | vmcb->control.exit_info_1, |
| 3019 | vmcb->control.exit_info_2, |
| 3020 | vmcb->control.exit_int_info, |
Stefan Hajnoczi | e097e5f | 2011-07-22 12:46:52 +0100 | [diff] [blame] | 3021 | vmcb->control.exit_int_info_err, |
| 3022 | KVM_ISA_SVM); |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 3023 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3024 | nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 3025 | if (!nested_vmcb) |
| 3026 | return 1; |
| 3027 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 3028 | /* Exit Guest-Mode */ |
| 3029 | leave_guest_mode(&svm->vcpu); |
Joerg Roedel | 06fc7772 | 2010-02-19 16:23:07 +0100 | [diff] [blame] | 3030 | svm->nested.vmcb = 0; |
| 3031 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3032 | /* Give the current vmcb to the guest */ |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3033 | disable_gif(svm); |
| 3034 | |
| 3035 | nested_vmcb->save.es = vmcb->save.es; |
| 3036 | nested_vmcb->save.cs = vmcb->save.cs; |
| 3037 | nested_vmcb->save.ss = vmcb->save.ss; |
| 3038 | nested_vmcb->save.ds = vmcb->save.ds; |
| 3039 | nested_vmcb->save.gdtr = vmcb->save.gdtr; |
| 3040 | nested_vmcb->save.idtr = vmcb->save.idtr; |
Joerg Roedel | 3f6a9d1 | 2010-07-27 18:14:20 +0200 | [diff] [blame] | 3041 | nested_vmcb->save.efer = svm->vcpu.arch.efer; |
Joerg Roedel | cdbbdc1 | 2010-02-19 16:23:03 +0100 | [diff] [blame] | 3042 | nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 3043 | nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu); |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3044 | nested_vmcb->save.cr2 = vmcb->save.cr2; |
Joerg Roedel | cdbbdc1 | 2010-02-19 16:23:03 +0100 | [diff] [blame] | 3045 | nested_vmcb->save.cr4 = svm->vcpu.arch.cr4; |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 3046 | nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu); |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3047 | nested_vmcb->save.rip = vmcb->save.rip; |
| 3048 | nested_vmcb->save.rsp = vmcb->save.rsp; |
| 3049 | nested_vmcb->save.rax = vmcb->save.rax; |
| 3050 | nested_vmcb->save.dr7 = vmcb->save.dr7; |
| 3051 | nested_vmcb->save.dr6 = vmcb->save.dr6; |
| 3052 | nested_vmcb->save.cpl = vmcb->save.cpl; |
| 3053 | |
| 3054 | nested_vmcb->control.int_ctl = vmcb->control.int_ctl; |
| 3055 | nested_vmcb->control.int_vector = vmcb->control.int_vector; |
| 3056 | nested_vmcb->control.int_state = vmcb->control.int_state; |
| 3057 | nested_vmcb->control.exit_code = vmcb->control.exit_code; |
| 3058 | nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi; |
| 3059 | nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1; |
| 3060 | nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2; |
| 3061 | nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info; |
| 3062 | 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] | 3063 | |
| 3064 | if (svm->nrips_enabled) |
| 3065 | nested_vmcb->control.next_rip = vmcb->control.next_rip; |
Alexander Graf | 8d23c46 | 2009-10-09 16:08:25 +0200 | [diff] [blame] | 3066 | |
| 3067 | /* |
| 3068 | * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have |
| 3069 | * to make sure that we do not lose injected events. So check event_inj |
| 3070 | * here and copy it to exit_int_info if it is valid. |
| 3071 | * Exit_int_info and event_inj can't be both valid because the case |
| 3072 | * below only happens on a VMRUN instruction intercept which has |
| 3073 | * no valid exit_int_info set. |
| 3074 | */ |
| 3075 | if (vmcb->control.event_inj & SVM_EVTINJ_VALID) { |
| 3076 | struct vmcb_control_area *nc = &nested_vmcb->control; |
| 3077 | |
| 3078 | nc->exit_int_info = vmcb->control.event_inj; |
| 3079 | nc->exit_int_info_err = vmcb->control.event_inj_err; |
| 3080 | } |
| 3081 | |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3082 | nested_vmcb->control.tlb_ctl = 0; |
| 3083 | nested_vmcb->control.event_inj = 0; |
| 3084 | nested_vmcb->control.event_inj_err = 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3085 | |
| 3086 | /* We always set V_INTR_MASKING and remember the old value in hflags */ |
| 3087 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
| 3088 | nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK; |
| 3089 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3090 | /* Restore the original control entries */ |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 3091 | copy_vmcb_control_area(vmcb, hsave); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3092 | |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3093 | kvm_clear_exception_queue(&svm->vcpu); |
| 3094 | kvm_clear_interrupt_queue(&svm->vcpu); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3095 | |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3096 | svm->nested.nested_cr3 = 0; |
| 3097 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3098 | /* Restore selected save entries */ |
| 3099 | svm->vmcb->save.es = hsave->save.es; |
| 3100 | svm->vmcb->save.cs = hsave->save.cs; |
| 3101 | svm->vmcb->save.ss = hsave->save.ss; |
| 3102 | svm->vmcb->save.ds = hsave->save.ds; |
| 3103 | svm->vmcb->save.gdtr = hsave->save.gdtr; |
| 3104 | svm->vmcb->save.idtr = hsave->save.idtr; |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 3105 | kvm_set_rflags(&svm->vcpu, hsave->save.rflags); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3106 | svm_set_efer(&svm->vcpu, hsave->save.efer); |
| 3107 | svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); |
| 3108 | svm_set_cr4(&svm->vcpu, hsave->save.cr4); |
| 3109 | if (npt_enabled) { |
| 3110 | svm->vmcb->save.cr3 = hsave->save.cr3; |
| 3111 | svm->vcpu.arch.cr3 = hsave->save.cr3; |
| 3112 | } else { |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 3113 | (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3114 | } |
| 3115 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax); |
| 3116 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp); |
| 3117 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip); |
| 3118 | svm->vmcb->save.dr7 = 0; |
| 3119 | svm->vmcb->save.cpl = 0; |
| 3120 | svm->vmcb->control.exit_int_info = 0; |
| 3121 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 3122 | mark_all_dirty(svm->vmcb); |
| 3123 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3124 | nested_svm_unmap(page); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3125 | |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3126 | nested_svm_uninit_mmu_context(&svm->vcpu); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3127 | kvm_mmu_reset_context(&svm->vcpu); |
| 3128 | kvm_mmu_load(&svm->vcpu); |
| 3129 | |
| 3130 | return 0; |
| 3131 | } |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3132 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3133 | static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3134 | { |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3135 | /* |
| 3136 | * This function merges the msr permission bitmaps of kvm and the |
Guo Chao | c5ec2e5 | 2012-06-28 15:16:43 +0800 | [diff] [blame] | 3137 | * 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] | 3138 | * the kvm msr permission bitmap may contain zero bits |
| 3139 | */ |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3140 | int i; |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3141 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3142 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
| 3143 | return true; |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3144 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3145 | for (i = 0; i < MSRPM_OFFSETS; i++) { |
| 3146 | u32 value, p; |
| 3147 | u64 offset; |
| 3148 | |
| 3149 | if (msrpm_offsets[i] == 0xffffffff) |
| 3150 | break; |
| 3151 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 3152 | p = msrpm_offsets[i]; |
| 3153 | offset = svm->nested.vmcb_msrpm + (p * 4); |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3154 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 3155 | if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4)) |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3156 | return false; |
| 3157 | |
| 3158 | svm->nested.msrpm[p] = svm->msrpm[p] | value; |
| 3159 | } |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3160 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 3161 | svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm)); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3162 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3163 | return true; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3164 | } |
| 3165 | |
Joerg Roedel | 52c65a30 | 2010-08-02 16:46:44 +0200 | [diff] [blame] | 3166 | static bool nested_vmcb_checks(struct vmcb *vmcb) |
| 3167 | { |
| 3168 | if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0) |
| 3169 | return false; |
| 3170 | |
Joerg Roedel | dbe7758 | 2010-08-02 16:46:45 +0200 | [diff] [blame] | 3171 | if (vmcb->control.asid == 0) |
| 3172 | return false; |
| 3173 | |
Tom Lendacky | cea3a19 | 2017-12-04 10:57:24 -0600 | [diff] [blame] | 3174 | if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && |
| 3175 | !npt_enabled) |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3176 | return false; |
| 3177 | |
Joerg Roedel | 52c65a30 | 2010-08-02 16:46:44 +0200 | [diff] [blame] | 3178 | return true; |
| 3179 | } |
| 3180 | |
Ladi Prosek | c263406 | 2017-10-11 16:54:44 +0200 | [diff] [blame] | 3181 | static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa, |
| 3182 | struct vmcb *nested_vmcb, struct page *page) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3183 | { |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 3184 | if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3185 | svm->vcpu.arch.hflags |= HF_HIF_MASK; |
| 3186 | else |
| 3187 | svm->vcpu.arch.hflags &= ~HF_HIF_MASK; |
| 3188 | |
Tom Lendacky | cea3a19 | 2017-12-04 10:57:24 -0600 | [diff] [blame] | 3189 | if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) { |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3190 | kvm_mmu_unload(&svm->vcpu); |
| 3191 | svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3; |
| 3192 | nested_svm_init_mmu_context(&svm->vcpu); |
| 3193 | } |
| 3194 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3195 | /* Load the nested guest state */ |
| 3196 | svm->vmcb->save.es = nested_vmcb->save.es; |
| 3197 | svm->vmcb->save.cs = nested_vmcb->save.cs; |
| 3198 | svm->vmcb->save.ss = nested_vmcb->save.ss; |
| 3199 | svm->vmcb->save.ds = nested_vmcb->save.ds; |
| 3200 | svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; |
| 3201 | svm->vmcb->save.idtr = nested_vmcb->save.idtr; |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 3202 | kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3203 | svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); |
| 3204 | svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); |
| 3205 | svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); |
| 3206 | if (npt_enabled) { |
| 3207 | svm->vmcb->save.cr3 = nested_vmcb->save.cr3; |
| 3208 | svm->vcpu.arch.cr3 = nested_vmcb->save.cr3; |
Joerg Roedel | 0e5cbe3 | 2010-02-24 18:59:11 +0100 | [diff] [blame] | 3209 | } else |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 3210 | (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3); |
Joerg Roedel | 0e5cbe3 | 2010-02-24 18:59:11 +0100 | [diff] [blame] | 3211 | |
| 3212 | /* Guest paging mode is active - reset mmu */ |
| 3213 | kvm_mmu_reset_context(&svm->vcpu); |
| 3214 | |
Joerg Roedel | defbba5 | 2009-08-07 11:49:30 +0200 | [diff] [blame] | 3215 | svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3216 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax); |
| 3217 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp); |
| 3218 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip); |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3219 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3220 | /* In case we don't even reach vcpu_run, the fields are not updated */ |
| 3221 | svm->vmcb->save.rax = nested_vmcb->save.rax; |
| 3222 | svm->vmcb->save.rsp = nested_vmcb->save.rsp; |
| 3223 | svm->vmcb->save.rip = nested_vmcb->save.rip; |
| 3224 | svm->vmcb->save.dr7 = nested_vmcb->save.dr7; |
| 3225 | svm->vmcb->save.dr6 = nested_vmcb->save.dr6; |
| 3226 | svm->vmcb->save.cpl = nested_vmcb->save.cpl; |
| 3227 | |
Joerg Roedel | f713853 | 2010-03-01 15:34:40 +0100 | [diff] [blame] | 3228 | svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 3229 | svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3230 | |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 3231 | /* cache intercepts */ |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 3232 | svm->nested.intercept_cr = nested_vmcb->control.intercept_cr; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 3233 | svm->nested.intercept_dr = nested_vmcb->control.intercept_dr; |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 3234 | svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions; |
| 3235 | svm->nested.intercept = nested_vmcb->control.intercept; |
| 3236 | |
Joerg Roedel | f40f6a4 | 2010-12-03 15:25:15 +0100 | [diff] [blame] | 3237 | svm_flush_tlb(&svm->vcpu); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3238 | 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] | 3239 | if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK) |
| 3240 | svm->vcpu.arch.hflags |= HF_VINTR_MASK; |
| 3241 | else |
| 3242 | svm->vcpu.arch.hflags &= ~HF_VINTR_MASK; |
| 3243 | |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 3244 | if (svm->vcpu.arch.hflags & HF_VINTR_MASK) { |
| 3245 | /* We only want the cr8 intercept bits of the guest */ |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 3246 | clr_cr_intercept(svm, INTERCEPT_CR8_READ); |
| 3247 | clr_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 3248 | } |
| 3249 | |
Joerg Roedel | 0d945bd | 2010-05-05 16:04:45 +0200 | [diff] [blame] | 3250 | /* We don't want to see VMMCALLs from a nested guest */ |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 3251 | clr_intercept(svm, INTERCEPT_VMMCALL); |
Joerg Roedel | 0d945bd | 2010-05-05 16:04:45 +0200 | [diff] [blame] | 3252 | |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 3253 | svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3254 | svm->vmcb->control.int_vector = nested_vmcb->control.int_vector; |
| 3255 | svm->vmcb->control.int_state = nested_vmcb->control.int_state; |
| 3256 | svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3257 | svm->vmcb->control.event_inj = nested_vmcb->control.event_inj; |
| 3258 | svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err; |
| 3259 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3260 | nested_svm_unmap(page); |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3261 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 3262 | /* Enter Guest-Mode */ |
| 3263 | enter_guest_mode(&svm->vcpu); |
| 3264 | |
Joerg Roedel | 384c636 | 2010-11-30 18:03:56 +0100 | [diff] [blame] | 3265 | /* |
| 3266 | * Merge guest and host intercepts - must be called with vcpu in |
| 3267 | * guest-mode to take affect here |
| 3268 | */ |
| 3269 | recalc_intercepts(svm); |
| 3270 | |
Joerg Roedel | 06fc7772 | 2010-02-19 16:23:07 +0100 | [diff] [blame] | 3271 | svm->nested.vmcb = vmcb_gpa; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3272 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 3273 | enable_gif(svm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3274 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 3275 | mark_all_dirty(svm->vmcb); |
Ladi Prosek | c263406 | 2017-10-11 16:54:44 +0200 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | static bool nested_svm_vmrun(struct vcpu_svm *svm) |
| 3279 | { |
| 3280 | struct vmcb *nested_vmcb; |
| 3281 | struct vmcb *hsave = svm->nested.hsave; |
| 3282 | struct vmcb *vmcb = svm->vmcb; |
| 3283 | struct page *page; |
| 3284 | u64 vmcb_gpa; |
| 3285 | |
| 3286 | vmcb_gpa = svm->vmcb->save.rax; |
| 3287 | |
| 3288 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
| 3289 | if (!nested_vmcb) |
| 3290 | return false; |
| 3291 | |
| 3292 | if (!nested_vmcb_checks(nested_vmcb)) { |
| 3293 | nested_vmcb->control.exit_code = SVM_EXIT_ERR; |
| 3294 | nested_vmcb->control.exit_code_hi = 0; |
| 3295 | nested_vmcb->control.exit_info_1 = 0; |
| 3296 | nested_vmcb->control.exit_info_2 = 0; |
| 3297 | |
| 3298 | nested_svm_unmap(page); |
| 3299 | |
| 3300 | return false; |
| 3301 | } |
| 3302 | |
| 3303 | trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa, |
| 3304 | nested_vmcb->save.rip, |
| 3305 | nested_vmcb->control.int_ctl, |
| 3306 | nested_vmcb->control.event_inj, |
| 3307 | nested_vmcb->control.nested_ctl); |
| 3308 | |
| 3309 | trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff, |
| 3310 | nested_vmcb->control.intercept_cr >> 16, |
| 3311 | nested_vmcb->control.intercept_exceptions, |
| 3312 | nested_vmcb->control.intercept); |
| 3313 | |
| 3314 | /* Clear internal status */ |
| 3315 | kvm_clear_exception_queue(&svm->vcpu); |
| 3316 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 3317 | |
| 3318 | /* |
| 3319 | * Save the old vmcb, so we don't need to pick what we save, but can |
| 3320 | * restore everything when a VMEXIT occurs |
| 3321 | */ |
| 3322 | hsave->save.es = vmcb->save.es; |
| 3323 | hsave->save.cs = vmcb->save.cs; |
| 3324 | hsave->save.ss = vmcb->save.ss; |
| 3325 | hsave->save.ds = vmcb->save.ds; |
| 3326 | hsave->save.gdtr = vmcb->save.gdtr; |
| 3327 | hsave->save.idtr = vmcb->save.idtr; |
| 3328 | hsave->save.efer = svm->vcpu.arch.efer; |
| 3329 | hsave->save.cr0 = kvm_read_cr0(&svm->vcpu); |
| 3330 | hsave->save.cr4 = svm->vcpu.arch.cr4; |
| 3331 | hsave->save.rflags = kvm_get_rflags(&svm->vcpu); |
| 3332 | hsave->save.rip = kvm_rip_read(&svm->vcpu); |
| 3333 | hsave->save.rsp = vmcb->save.rsp; |
| 3334 | hsave->save.rax = vmcb->save.rax; |
| 3335 | if (npt_enabled) |
| 3336 | hsave->save.cr3 = vmcb->save.cr3; |
| 3337 | else |
| 3338 | hsave->save.cr3 = kvm_read_cr3(&svm->vcpu); |
| 3339 | |
| 3340 | copy_vmcb_control_area(hsave, vmcb); |
| 3341 | |
| 3342 | enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, page); |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 3343 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3344 | return true; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3345 | } |
| 3346 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3347 | 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] | 3348 | { |
| 3349 | to_vmcb->save.fs = from_vmcb->save.fs; |
| 3350 | to_vmcb->save.gs = from_vmcb->save.gs; |
| 3351 | to_vmcb->save.tr = from_vmcb->save.tr; |
| 3352 | to_vmcb->save.ldtr = from_vmcb->save.ldtr; |
| 3353 | to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; |
| 3354 | to_vmcb->save.star = from_vmcb->save.star; |
| 3355 | to_vmcb->save.lstar = from_vmcb->save.lstar; |
| 3356 | to_vmcb->save.cstar = from_vmcb->save.cstar; |
| 3357 | to_vmcb->save.sfmask = from_vmcb->save.sfmask; |
| 3358 | to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; |
| 3359 | to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; |
| 3360 | to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3361 | } |
| 3362 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3363 | static int vmload_interception(struct vcpu_svm *svm) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3364 | { |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3365 | struct vmcb *nested_vmcb; |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3366 | struct page *page; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3367 | int ret; |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3368 | |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3369 | if (nested_svm_check_permissions(svm)) |
| 3370 | return 1; |
| 3371 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3372 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3373 | if (!nested_vmcb) |
| 3374 | return 1; |
| 3375 | |
Joerg Roedel | e3e9ed3 | 2011-04-06 12:30:03 +0200 | [diff] [blame] | 3376 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3377 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Joerg Roedel | e3e9ed3 | 2011-04-06 12:30:03 +0200 | [diff] [blame] | 3378 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3379 | nested_svm_vmloadsave(nested_vmcb, svm->vmcb); |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3380 | nested_svm_unmap(page); |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3381 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3382 | return ret; |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3383 | } |
| 3384 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3385 | static int vmsave_interception(struct vcpu_svm *svm) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3386 | { |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3387 | struct vmcb *nested_vmcb; |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3388 | struct page *page; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3389 | int ret; |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3390 | |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3391 | if (nested_svm_check_permissions(svm)) |
| 3392 | return 1; |
| 3393 | |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3394 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3395 | if (!nested_vmcb) |
| 3396 | return 1; |
| 3397 | |
Joerg Roedel | e3e9ed3 | 2011-04-06 12:30:03 +0200 | [diff] [blame] | 3398 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3399 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Joerg Roedel | e3e9ed3 | 2011-04-06 12:30:03 +0200 | [diff] [blame] | 3400 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3401 | nested_svm_vmloadsave(svm->vmcb, nested_vmcb); |
Joerg Roedel | 7597f12 | 2010-02-19 16:23:00 +0100 | [diff] [blame] | 3402 | nested_svm_unmap(page); |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3403 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3404 | return ret; |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3405 | } |
| 3406 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3407 | static int vmrun_interception(struct vcpu_svm *svm) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3408 | { |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3409 | if (nested_svm_check_permissions(svm)) |
| 3410 | return 1; |
| 3411 | |
Roedel, Joerg | b75f4eb | 2010-09-03 14:21:40 +0200 | [diff] [blame] | 3412 | /* Save rip after vmrun instruction */ |
| 3413 | kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3414 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3415 | if (!nested_svm_vmrun(svm)) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3416 | return 1; |
| 3417 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3418 | if (!nested_svm_vmrun_msrpm(svm)) |
Joerg Roedel | 1f8da47 | 2009-08-07 11:49:43 +0200 | [diff] [blame] | 3419 | goto failed; |
| 3420 | |
| 3421 | return 1; |
| 3422 | |
| 3423 | failed: |
| 3424 | |
| 3425 | svm->vmcb->control.exit_code = SVM_EXIT_ERR; |
| 3426 | svm->vmcb->control.exit_code_hi = 0; |
| 3427 | svm->vmcb->control.exit_info_1 = 0; |
| 3428 | svm->vmcb->control.exit_info_2 = 0; |
| 3429 | |
| 3430 | nested_svm_vmexit(svm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3431 | |
| 3432 | return 1; |
| 3433 | } |
| 3434 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3435 | static int stgi_interception(struct vcpu_svm *svm) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3436 | { |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3437 | int ret; |
| 3438 | |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3439 | if (nested_svm_check_permissions(svm)) |
| 3440 | return 1; |
| 3441 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 3442 | /* |
| 3443 | * If VGIF is enabled, the STGI intercept is only added to |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 3444 | * detect the opening of the SMI/NMI window; remove it now. |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 3445 | */ |
| 3446 | if (vgif_enabled(svm)) |
| 3447 | clr_intercept(svm, INTERCEPT_STGI); |
| 3448 | |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3449 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3450 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 3451 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3452 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 3453 | enable_gif(svm); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3454 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3455 | return ret; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3456 | } |
| 3457 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3458 | static int clgi_interception(struct vcpu_svm *svm) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3459 | { |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3460 | int ret; |
| 3461 | |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3462 | if (nested_svm_check_permissions(svm)) |
| 3463 | return 1; |
| 3464 | |
| 3465 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3466 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3467 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 3468 | disable_gif(svm); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3469 | |
| 3470 | /* After a CLGI no interrupts should come */ |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 3471 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) { |
| 3472 | svm_clear_vintr(svm); |
| 3473 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
| 3474 | mark_dirty(svm->vmcb, VMCB_INTR); |
| 3475 | } |
Joerg Roedel | decdbf6 | 2010-12-03 11:45:52 +0100 | [diff] [blame] | 3476 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3477 | return ret; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3478 | } |
| 3479 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3480 | static int invlpga_interception(struct vcpu_svm *svm) |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3481 | { |
| 3482 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3483 | |
David Kaplan | 668f198 | 2015-02-20 16:02:10 -0600 | [diff] [blame] | 3484 | trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX), |
| 3485 | kvm_register_read(&svm->vcpu, VCPU_REGS_RAX)); |
Joerg Roedel | ec1ff79 | 2009-10-09 16:08:31 +0200 | [diff] [blame] | 3486 | |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3487 | /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */ |
David Kaplan | 668f198 | 2015-02-20 16:02:10 -0600 | [diff] [blame] | 3488 | kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX)); |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3489 | |
| 3490 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3491 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3492 | } |
| 3493 | |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 3494 | static int skinit_interception(struct vcpu_svm *svm) |
| 3495 | { |
David Kaplan | 668f198 | 2015-02-20 16:02:10 -0600 | [diff] [blame] | 3496 | 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] | 3497 | |
| 3498 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 3499 | return 1; |
| 3500 | } |
| 3501 | |
David Kaplan | dab429a | 2015-03-02 13:43:37 -0600 | [diff] [blame] | 3502 | static int wbinvd_interception(struct vcpu_svm *svm) |
| 3503 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 3504 | return kvm_emulate_wbinvd(&svm->vcpu); |
David Kaplan | dab429a | 2015-03-02 13:43:37 -0600 | [diff] [blame] | 3505 | } |
| 3506 | |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 3507 | static int xsetbv_interception(struct vcpu_svm *svm) |
| 3508 | { |
| 3509 | u64 new_bv = kvm_read_edx_eax(&svm->vcpu); |
| 3510 | u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); |
| 3511 | |
| 3512 | if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) { |
| 3513 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3514 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | return 1; |
| 3518 | } |
| 3519 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3520 | static int task_switch_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3521 | { |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3522 | u16 tss_selector; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3523 | int reason; |
| 3524 | int int_type = svm->vmcb->control.exit_int_info & |
| 3525 | SVM_EXITINTINFO_TYPE_MASK; |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 3526 | 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] | 3527 | uint32_t type = |
| 3528 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK; |
| 3529 | uint32_t idt_v = |
| 3530 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3531 | bool has_error_code = false; |
| 3532 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3533 | |
| 3534 | tss_selector = (u16)svm->vmcb->control.exit_info_1; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3535 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3536 | if (svm->vmcb->control.exit_info_2 & |
| 3537 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3538 | reason = TASK_SWITCH_IRET; |
| 3539 | else if (svm->vmcb->control.exit_info_2 & |
| 3540 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) |
| 3541 | reason = TASK_SWITCH_JMP; |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 3542 | else if (idt_v) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3543 | reason = TASK_SWITCH_GATE; |
| 3544 | else |
| 3545 | reason = TASK_SWITCH_CALL; |
| 3546 | |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 3547 | if (reason == TASK_SWITCH_GATE) { |
| 3548 | switch (type) { |
| 3549 | case SVM_EXITINTINFO_TYPE_NMI: |
| 3550 | svm->vcpu.arch.nmi_injected = false; |
| 3551 | break; |
| 3552 | case SVM_EXITINTINFO_TYPE_EXEPT: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3553 | if (svm->vmcb->control.exit_info_2 & |
| 3554 | (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) { |
| 3555 | has_error_code = true; |
| 3556 | error_code = |
| 3557 | (u32)svm->vmcb->control.exit_info_2; |
| 3558 | } |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 3559 | kvm_clear_exception_queue(&svm->vcpu); |
| 3560 | break; |
| 3561 | case SVM_EXITINTINFO_TYPE_INTR: |
| 3562 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 3563 | break; |
| 3564 | default: |
| 3565 | break; |
| 3566 | } |
| 3567 | } |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3568 | |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 3569 | if (reason != TASK_SWITCH_GATE || |
| 3570 | int_type == SVM_EXITINTINFO_TYPE_SOFT || |
| 3571 | (int_type == SVM_EXITINTINFO_TYPE_EXEPT && |
Gleb Natapov | f629cf8 | 2009-05-11 13:35:49 +0300 | [diff] [blame] | 3572 | (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) |
| 3573 | skip_emulated_instruction(&svm->vcpu); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3574 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 3575 | if (int_type != SVM_EXITINTINFO_TYPE_SOFT) |
| 3576 | int_vec = -1; |
| 3577 | |
| 3578 | if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason, |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 3579 | has_error_code, error_code) == EMULATE_FAIL) { |
| 3580 | svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3581 | svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 3582 | svm->vcpu.run->internal.ndata = 0; |
| 3583 | return 0; |
| 3584 | } |
| 3585 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3586 | } |
| 3587 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3588 | static int cpuid_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3589 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3590 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 3591 | return kvm_emulate_cpuid(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3592 | } |
| 3593 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3594 | static int iret_interception(struct vcpu_svm *svm) |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3595 | { |
| 3596 | ++svm->vcpu.stat.nmi_window_exits; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 3597 | clr_intercept(svm, INTERCEPT_IRET); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 3598 | svm->vcpu.arch.hflags |= HF_IRET_MASK; |
Avi Kivity | bd3d1ec | 2011-02-03 15:29:52 +0200 | [diff] [blame] | 3599 | svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu); |
Radim Krčmář | f303b4c | 2014-01-17 20:52:42 +0100 | [diff] [blame] | 3600 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3601 | return 1; |
| 3602 | } |
| 3603 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3604 | static int invlpg_interception(struct vcpu_svm *svm) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3605 | { |
Andre Przywara | df4f3108 | 2010-12-21 11:12:06 +0100 | [diff] [blame] | 3606 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) |
| 3607 | return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE; |
| 3608 | |
| 3609 | kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1); |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3610 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3611 | } |
| 3612 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3613 | static int emulate_on_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3614 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 3615 | return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3616 | } |
| 3617 | |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 3618 | static int rdpmc_interception(struct vcpu_svm *svm) |
| 3619 | { |
| 3620 | int err; |
| 3621 | |
| 3622 | if (!static_cpu_has(X86_FEATURE_NRIPS)) |
| 3623 | return emulate_on_interception(svm); |
| 3624 | |
| 3625 | err = kvm_rdpmc(&svm->vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 3626 | return kvm_complete_insn_gp(&svm->vcpu, err); |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 3627 | } |
| 3628 | |
Xiubo Li | 52eb5a6 | 2015-03-13 17:39:45 +0800 | [diff] [blame] | 3629 | static bool check_selective_cr0_intercepted(struct vcpu_svm *svm, |
| 3630 | unsigned long val) |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 3631 | { |
| 3632 | unsigned long cr0 = svm->vcpu.arch.cr0; |
| 3633 | bool ret = false; |
| 3634 | u64 intercept; |
| 3635 | |
| 3636 | intercept = svm->nested.intercept; |
| 3637 | |
| 3638 | if (!is_guest_mode(&svm->vcpu) || |
| 3639 | (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))) |
| 3640 | return false; |
| 3641 | |
| 3642 | cr0 &= ~SVM_CR0_SELECTIVE_MASK; |
| 3643 | val &= ~SVM_CR0_SELECTIVE_MASK; |
| 3644 | |
| 3645 | if (cr0 ^ val) { |
| 3646 | svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE; |
| 3647 | ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE); |
| 3648 | } |
| 3649 | |
| 3650 | return ret; |
| 3651 | } |
| 3652 | |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3653 | #define CR_VALID (1ULL << 63) |
| 3654 | |
| 3655 | static int cr_interception(struct vcpu_svm *svm) |
| 3656 | { |
| 3657 | int reg, cr; |
| 3658 | unsigned long val; |
| 3659 | int err; |
| 3660 | |
| 3661 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) |
| 3662 | return emulate_on_interception(svm); |
| 3663 | |
| 3664 | if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0)) |
| 3665 | return emulate_on_interception(svm); |
| 3666 | |
| 3667 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; |
David Kaplan | 5e57518 | 2015-03-06 14:44:35 -0600 | [diff] [blame] | 3668 | if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE) |
| 3669 | cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0; |
| 3670 | else |
| 3671 | cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0; |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3672 | |
| 3673 | err = 0; |
| 3674 | if (cr >= 16) { /* mov to cr */ |
| 3675 | cr -= 16; |
| 3676 | val = kvm_register_read(&svm->vcpu, reg); |
| 3677 | switch (cr) { |
| 3678 | case 0: |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 3679 | if (!check_selective_cr0_intercepted(svm, val)) |
| 3680 | err = kvm_set_cr0(&svm->vcpu, val); |
Joerg Roedel | 977b2d0 | 2011-04-18 11:42:52 +0200 | [diff] [blame] | 3681 | else |
| 3682 | return 1; |
| 3683 | |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3684 | break; |
| 3685 | case 3: |
| 3686 | err = kvm_set_cr3(&svm->vcpu, val); |
| 3687 | break; |
| 3688 | case 4: |
| 3689 | err = kvm_set_cr4(&svm->vcpu, val); |
| 3690 | break; |
| 3691 | case 8: |
| 3692 | err = kvm_set_cr8(&svm->vcpu, val); |
| 3693 | break; |
| 3694 | default: |
| 3695 | WARN(1, "unhandled write to CR%d", cr); |
| 3696 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 3697 | return 1; |
| 3698 | } |
| 3699 | } else { /* mov from cr */ |
| 3700 | switch (cr) { |
| 3701 | case 0: |
| 3702 | val = kvm_read_cr0(&svm->vcpu); |
| 3703 | break; |
| 3704 | case 2: |
| 3705 | val = svm->vcpu.arch.cr2; |
| 3706 | break; |
| 3707 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 3708 | val = kvm_read_cr3(&svm->vcpu); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3709 | break; |
| 3710 | case 4: |
| 3711 | val = kvm_read_cr4(&svm->vcpu); |
| 3712 | break; |
| 3713 | case 8: |
| 3714 | val = kvm_get_cr8(&svm->vcpu); |
| 3715 | break; |
| 3716 | default: |
| 3717 | WARN(1, "unhandled read from CR%d", cr); |
| 3718 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 3719 | return 1; |
| 3720 | } |
| 3721 | kvm_register_write(&svm->vcpu, reg, val); |
| 3722 | } |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 3723 | return kvm_complete_insn_gp(&svm->vcpu, err); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3724 | } |
| 3725 | |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3726 | static int dr_interception(struct vcpu_svm *svm) |
| 3727 | { |
| 3728 | int reg, dr; |
| 3729 | unsigned long val; |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3730 | |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 3731 | if (svm->vcpu.guest_debug == 0) { |
| 3732 | /* |
| 3733 | * No more DR vmexits; force a reload of the debug registers |
| 3734 | * and reenter on this instruction. The next vmexit will |
| 3735 | * retrieve the full state of the debug registers. |
| 3736 | */ |
| 3737 | clr_dr_intercepts(svm); |
| 3738 | svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 3739 | return 1; |
| 3740 | } |
| 3741 | |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3742 | if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS)) |
| 3743 | return emulate_on_interception(svm); |
| 3744 | |
| 3745 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; |
| 3746 | dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0; |
| 3747 | |
| 3748 | if (dr >= 16) { /* mov to DRn */ |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 3749 | if (!kvm_require_dr(&svm->vcpu, dr - 16)) |
| 3750 | return 1; |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3751 | val = kvm_register_read(&svm->vcpu, reg); |
| 3752 | kvm_set_dr(&svm->vcpu, dr - 16, val); |
| 3753 | } else { |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 3754 | if (!kvm_require_dr(&svm->vcpu, dr)) |
| 3755 | return 1; |
| 3756 | kvm_get_dr(&svm->vcpu, dr, &val); |
| 3757 | kvm_register_write(&svm->vcpu, reg, val); |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3758 | } |
| 3759 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3760 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 3761 | } |
| 3762 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3763 | static int cr8_write_interception(struct vcpu_svm *svm) |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 3764 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3765 | struct kvm_run *kvm_run = svm->vcpu.run; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 3766 | int r; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3767 | |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 3768 | u8 cr8_prev = kvm_get_cr8(&svm->vcpu); |
| 3769 | /* instruction emulation calls kvm_set_cr8() */ |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3770 | r = cr_interception(svm); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 3771 | if (lapic_in_kernel(&svm->vcpu)) |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3772 | return r; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 3773 | if (cr8_prev <= kvm_get_cr8(&svm->vcpu)) |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3774 | return r; |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 3775 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
| 3776 | return 0; |
| 3777 | } |
| 3778 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3779 | 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] | 3780 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3781 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3782 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3783 | switch (msr_info->index) { |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 3784 | case MSR_IA32_TSC: { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3785 | msr_info->data = svm->vmcb->control.tsc_offset + |
Haozhong Zhang | 35181e8 | 2015-10-20 15:39:03 +0800 | [diff] [blame] | 3786 | kvm_scale_tsc(vcpu, rdtsc()); |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 3787 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3788 | break; |
| 3789 | } |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3790 | case MSR_STAR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3791 | msr_info->data = svm->vmcb->save.star; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3792 | break; |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 3793 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3794 | case MSR_LSTAR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3795 | msr_info->data = svm->vmcb->save.lstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3796 | break; |
| 3797 | case MSR_CSTAR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3798 | msr_info->data = svm->vmcb->save.cstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3799 | break; |
| 3800 | case MSR_KERNEL_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3801 | msr_info->data = svm->vmcb->save.kernel_gs_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3802 | break; |
| 3803 | case MSR_SYSCALL_MASK: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3804 | msr_info->data = svm->vmcb->save.sfmask; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3805 | break; |
| 3806 | #endif |
| 3807 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3808 | msr_info->data = svm->vmcb->save.sysenter_cs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3809 | break; |
| 3810 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3811 | msr_info->data = svm->sysenter_eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3812 | break; |
| 3813 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3814 | msr_info->data = svm->sysenter_esp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3815 | break; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 3816 | case MSR_TSC_AUX: |
| 3817 | if (!boot_cpu_has(X86_FEATURE_RDTSCP)) |
| 3818 | return 1; |
| 3819 | msr_info->data = svm->tsc_aux; |
| 3820 | break; |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3821 | /* |
| 3822 | * Nobody will change the following 5 values in the VMCB so we can |
| 3823 | * safely return them on rdmsr. They will always be 0 until LBRV is |
| 3824 | * implemented. |
| 3825 | */ |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3826 | case MSR_IA32_DEBUGCTLMSR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3827 | msr_info->data = svm->vmcb->save.dbgctl; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3828 | break; |
| 3829 | case MSR_IA32_LASTBRANCHFROMIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3830 | msr_info->data = svm->vmcb->save.br_from; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3831 | break; |
| 3832 | case MSR_IA32_LASTBRANCHTOIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3833 | msr_info->data = svm->vmcb->save.br_to; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3834 | break; |
| 3835 | case MSR_IA32_LASTINTFROMIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3836 | msr_info->data = svm->vmcb->save.last_excp_from; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3837 | break; |
| 3838 | case MSR_IA32_LASTINTTOIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3839 | msr_info->data = svm->vmcb->save.last_excp_to; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3840 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 3841 | case MSR_VM_HSAVE_PA: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3842 | msr_info->data = svm->nested.hsave_msr; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 3843 | break; |
Joerg Roedel | eb6f302 | 2008-11-25 20:17:09 +0100 | [diff] [blame] | 3844 | case MSR_VM_CR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3845 | msr_info->data = svm->nested.vm_cr_msr; |
Joerg Roedel | eb6f302 | 2008-11-25 20:17:09 +0100 | [diff] [blame] | 3846 | break; |
Alexander Graf | c8a73f1 | 2009-01-05 16:02:47 +0100 | [diff] [blame] | 3847 | case MSR_IA32_UCODE_REV: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3848 | msr_info->data = 0x01000065; |
Alexander Graf | c8a73f1 | 2009-01-05 16:02:47 +0100 | [diff] [blame] | 3849 | break; |
Borislav Petkov | ae8b787 | 2015-11-23 11:12:23 +0100 | [diff] [blame] | 3850 | case MSR_F15H_IC_CFG: { |
| 3851 | |
| 3852 | int family, model; |
| 3853 | |
| 3854 | family = guest_cpuid_family(vcpu); |
| 3855 | model = guest_cpuid_model(vcpu); |
| 3856 | |
| 3857 | if (family < 0 || model < 0) |
| 3858 | return kvm_get_msr_common(vcpu, msr_info); |
| 3859 | |
| 3860 | msr_info->data = 0; |
| 3861 | |
| 3862 | if (family == 0x15 && |
| 3863 | (model >= 0x2 && model < 0x20)) |
| 3864 | msr_info->data = 0x1E; |
| 3865 | } |
| 3866 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3867 | default: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3868 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3869 | } |
| 3870 | return 0; |
| 3871 | } |
| 3872 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3873 | static int rdmsr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3874 | { |
David Kaplan | 668f198 | 2015-02-20 16:02:10 -0600 | [diff] [blame] | 3875 | u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3876 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3877 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3878 | msr_info.index = ecx; |
| 3879 | msr_info.host_initiated = false; |
| 3880 | if (svm_get_msr(&svm->vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 3881 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 3882 | kvm_inject_gp(&svm->vcpu, 0); |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3883 | return 1; |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 3884 | } else { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3885 | trace_kvm_msr_read(ecx, msr_info.data); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 3886 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3887 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, |
| 3888 | msr_info.data & 0xffffffff); |
| 3889 | kvm_register_write(&svm->vcpu, VCPU_REGS_RDX, |
| 3890 | msr_info.data >> 32); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3891 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3892 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3893 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3894 | } |
| 3895 | |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 3896 | static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data) |
| 3897 | { |
| 3898 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3899 | int svm_dis, chg_mask; |
| 3900 | |
| 3901 | if (data & ~SVM_VM_CR_VALID_MASK) |
| 3902 | return 1; |
| 3903 | |
| 3904 | chg_mask = SVM_VM_CR_VALID_MASK; |
| 3905 | |
| 3906 | if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK) |
| 3907 | chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK); |
| 3908 | |
| 3909 | svm->nested.vm_cr_msr &= ~chg_mask; |
| 3910 | svm->nested.vm_cr_msr |= (data & chg_mask); |
| 3911 | |
| 3912 | svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK; |
| 3913 | |
| 3914 | /* check for svm_disable while efer.svme is set */ |
| 3915 | if (svm_dis && (vcpu->arch.efer & EFER_SVME)) |
| 3916 | return 1; |
| 3917 | |
| 3918 | return 0; |
| 3919 | } |
| 3920 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3921 | 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] | 3922 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3923 | struct vcpu_svm *svm = to_svm(vcpu); |
| 3924 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3925 | u32 ecx = msr->index; |
| 3926 | u64 data = msr->data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3927 | switch (ecx) { |
Paolo Bonzini | 15038e1 | 2017-10-26 09:13:27 +0200 | [diff] [blame] | 3928 | case MSR_IA32_CR_PAT: |
| 3929 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 3930 | return 1; |
| 3931 | vcpu->arch.pat = data; |
| 3932 | svm->vmcb->save.g_pat = data; |
| 3933 | mark_dirty(svm->vmcb, VMCB_NPT); |
| 3934 | break; |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 3935 | case MSR_IA32_TSC: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3936 | kvm_write_tsc(vcpu, msr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3937 | break; |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3938 | case MSR_STAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3939 | svm->vmcb->save.star = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3940 | break; |
Robert P. J. Day | 49b14f2 | 2007-01-29 13:19:50 -0800 | [diff] [blame] | 3941 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3942 | case MSR_LSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3943 | svm->vmcb->save.lstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3944 | break; |
| 3945 | case MSR_CSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3946 | svm->vmcb->save.cstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3947 | break; |
| 3948 | case MSR_KERNEL_GS_BASE: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3949 | svm->vmcb->save.kernel_gs_base = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3950 | break; |
| 3951 | case MSR_SYSCALL_MASK: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3952 | svm->vmcb->save.sfmask = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3953 | break; |
| 3954 | #endif |
| 3955 | case MSR_IA32_SYSENTER_CS: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3956 | svm->vmcb->save.sysenter_cs = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3957 | break; |
| 3958 | case MSR_IA32_SYSENTER_EIP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 3959 | svm->sysenter_eip = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3960 | svm->vmcb->save.sysenter_eip = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3961 | break; |
| 3962 | case MSR_IA32_SYSENTER_ESP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 3963 | svm->sysenter_esp = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3964 | svm->vmcb->save.sysenter_esp = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3965 | break; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 3966 | case MSR_TSC_AUX: |
| 3967 | if (!boot_cpu_has(X86_FEATURE_RDTSCP)) |
| 3968 | return 1; |
| 3969 | |
| 3970 | /* |
| 3971 | * This is rare, so we update the MSR here instead of using |
| 3972 | * direct_access_msrs. Doing that would require a rdmsr in |
| 3973 | * svm_vcpu_put. |
| 3974 | */ |
| 3975 | svm->tsc_aux = data; |
| 3976 | wrmsrl(MSR_TSC_AUX, svm->tsc_aux); |
| 3977 | break; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3978 | case MSR_IA32_DEBUGCTLMSR: |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 3979 | if (!boot_cpu_has(X86_FEATURE_LBRV)) { |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 3980 | vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", |
| 3981 | __func__, data); |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 3982 | break; |
| 3983 | } |
| 3984 | if (data & DEBUGCTL_RESERVED_BITS) |
| 3985 | return 1; |
| 3986 | |
| 3987 | svm->vmcb->save.dbgctl = data; |
Joerg Roedel | b53ba3f | 2010-12-03 11:45:59 +0100 | [diff] [blame] | 3988 | mark_dirty(svm->vmcb, VMCB_LBR); |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 3989 | if (data & (1ULL<<0)) |
| 3990 | svm_enable_lbrv(svm); |
| 3991 | else |
| 3992 | svm_disable_lbrv(svm); |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 3993 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 3994 | case MSR_VM_HSAVE_PA: |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 3995 | svm->nested.hsave_msr = data; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 3996 | break; |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 3997 | case MSR_VM_CR: |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 3998 | return svm_set_vm_cr(vcpu, data); |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 3999 | case MSR_VM_IGNNE: |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 4000 | 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] | 4001 | break; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4002 | case MSR_IA32_APICBASE: |
| 4003 | if (kvm_vcpu_apicv_active(vcpu)) |
| 4004 | avic_update_vapic_bar(to_svm(vcpu), data); |
| 4005 | /* Follow through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4006 | default: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4007 | return kvm_set_msr_common(vcpu, msr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4008 | } |
| 4009 | return 0; |
| 4010 | } |
| 4011 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4012 | static int wrmsr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4013 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4014 | struct msr_data msr; |
David Kaplan | 668f198 | 2015-02-20 16:02:10 -0600 | [diff] [blame] | 4015 | u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); |
| 4016 | u64 data = kvm_read_edx_eax(&svm->vcpu); |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 4017 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4018 | msr.data = data; |
| 4019 | msr.index = ecx; |
| 4020 | msr.host_initiated = false; |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 4021 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4022 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 4023 | if (kvm_set_msr(&svm->vcpu, &msr)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4024 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4025 | kvm_inject_gp(&svm->vcpu, 0); |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 4026 | return 1; |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4027 | } else { |
| 4028 | trace_kvm_msr_write(ecx, data); |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 4029 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4030 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4031 | } |
| 4032 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4033 | static int msr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4034 | { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 4035 | if (svm->vmcb->control.exit_info_1) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4036 | return wrmsr_interception(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4037 | else |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4038 | return rdmsr_interception(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4039 | } |
| 4040 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4041 | static int interrupt_window_interception(struct vcpu_svm *svm) |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4042 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4043 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 4044 | svm_clear_vintr(svm); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4045 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
Joerg Roedel | decdbf6 | 2010-12-03 11:45:52 +0100 | [diff] [blame] | 4046 | mark_dirty(svm->vmcb, VMCB_INTR); |
Jason Wang | 675acb7 | 2012-03-08 18:07:56 +0800 | [diff] [blame] | 4047 | ++svm->vcpu.stat.irq_window_exits; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4048 | return 1; |
| 4049 | } |
| 4050 | |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 4051 | static int pause_interception(struct vcpu_svm *svm) |
| 4052 | { |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 4053 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 4054 | bool in_kernel = (svm_get_cpl(vcpu) == 0); |
| 4055 | |
| 4056 | kvm_vcpu_on_spin(vcpu, in_kernel); |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 4057 | return 1; |
| 4058 | } |
| 4059 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 4060 | static int nop_interception(struct vcpu_svm *svm) |
| 4061 | { |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 4062 | return kvm_skip_emulated_instruction(&(svm->vcpu)); |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | static int monitor_interception(struct vcpu_svm *svm) |
| 4066 | { |
| 4067 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 4068 | return nop_interception(svm); |
| 4069 | } |
| 4070 | |
| 4071 | static int mwait_interception(struct vcpu_svm *svm) |
| 4072 | { |
| 4073 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 4074 | return nop_interception(svm); |
| 4075 | } |
| 4076 | |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4077 | enum avic_ipi_failure_cause { |
| 4078 | AVIC_IPI_FAILURE_INVALID_INT_TYPE, |
| 4079 | AVIC_IPI_FAILURE_TARGET_NOT_RUNNING, |
| 4080 | AVIC_IPI_FAILURE_INVALID_TARGET, |
| 4081 | AVIC_IPI_FAILURE_INVALID_BACKING_PAGE, |
| 4082 | }; |
| 4083 | |
| 4084 | static int avic_incomplete_ipi_interception(struct vcpu_svm *svm) |
| 4085 | { |
| 4086 | u32 icrh = svm->vmcb->control.exit_info_1 >> 32; |
| 4087 | u32 icrl = svm->vmcb->control.exit_info_1; |
| 4088 | u32 id = svm->vmcb->control.exit_info_2 >> 32; |
Dan Carpenter | 5446a97 | 2016-05-23 13:20:10 +0300 | [diff] [blame] | 4089 | u32 index = svm->vmcb->control.exit_info_2 & 0xFF; |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4090 | struct kvm_lapic *apic = svm->vcpu.arch.apic; |
| 4091 | |
| 4092 | trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index); |
| 4093 | |
| 4094 | switch (id) { |
| 4095 | case AVIC_IPI_FAILURE_INVALID_INT_TYPE: |
| 4096 | /* |
| 4097 | * AVIC hardware handles the generation of |
| 4098 | * IPIs when the specified Message Type is Fixed |
| 4099 | * (also known as fixed delivery mode) and |
| 4100 | * the Trigger Mode is edge-triggered. The hardware |
| 4101 | * also supports self and broadcast delivery modes |
| 4102 | * specified via the Destination Shorthand(DSH) |
| 4103 | * field of the ICRL. Logical and physical APIC ID |
| 4104 | * formats are supported. All other IPI types cause |
| 4105 | * a #VMEXIT, which needs to emulated. |
| 4106 | */ |
| 4107 | kvm_lapic_reg_write(apic, APIC_ICR2, icrh); |
| 4108 | kvm_lapic_reg_write(apic, APIC_ICR, icrl); |
| 4109 | break; |
| 4110 | case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: { |
| 4111 | int i; |
| 4112 | struct kvm_vcpu *vcpu; |
| 4113 | struct kvm *kvm = svm->vcpu.kvm; |
| 4114 | struct kvm_lapic *apic = svm->vcpu.arch.apic; |
| 4115 | |
| 4116 | /* |
| 4117 | * At this point, we expect that the AVIC HW has already |
| 4118 | * set the appropriate IRR bits on the valid target |
| 4119 | * vcpus. So, we just need to kick the appropriate vcpu. |
| 4120 | */ |
| 4121 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 4122 | bool m = kvm_apic_match_dest(vcpu, apic, |
| 4123 | icrl & KVM_APIC_SHORT_MASK, |
| 4124 | GET_APIC_DEST_FIELD(icrh), |
| 4125 | icrl & KVM_APIC_DEST_MASK); |
| 4126 | |
| 4127 | if (m && !avic_vcpu_is_running(vcpu)) |
| 4128 | kvm_vcpu_wake_up(vcpu); |
| 4129 | } |
| 4130 | break; |
| 4131 | } |
| 4132 | case AVIC_IPI_FAILURE_INVALID_TARGET: |
| 4133 | break; |
| 4134 | case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE: |
| 4135 | WARN_ONCE(1, "Invalid backing page\n"); |
| 4136 | break; |
| 4137 | default: |
| 4138 | pr_err("Unknown IPI interception\n"); |
| 4139 | } |
| 4140 | |
| 4141 | return 1; |
| 4142 | } |
| 4143 | |
| 4144 | static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat) |
| 4145 | { |
| 4146 | struct kvm_arch *vm_data = &vcpu->kvm->arch; |
| 4147 | int index; |
| 4148 | u32 *logical_apic_id_table; |
| 4149 | int dlid = GET_APIC_LOGICAL_ID(ldr); |
| 4150 | |
| 4151 | if (!dlid) |
| 4152 | return NULL; |
| 4153 | |
| 4154 | if (flat) { /* flat */ |
| 4155 | index = ffs(dlid) - 1; |
| 4156 | if (index > 7) |
| 4157 | return NULL; |
| 4158 | } else { /* cluster */ |
| 4159 | int cluster = (dlid & 0xf0) >> 4; |
| 4160 | int apic = ffs(dlid & 0x0f) - 1; |
| 4161 | |
| 4162 | if ((apic < 0) || (apic > 7) || |
| 4163 | (cluster >= 0xf)) |
| 4164 | return NULL; |
| 4165 | index = (cluster << 2) + apic; |
| 4166 | } |
| 4167 | |
| 4168 | logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page); |
| 4169 | |
| 4170 | return &logical_apic_id_table[index]; |
| 4171 | } |
| 4172 | |
| 4173 | static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr, |
| 4174 | bool valid) |
| 4175 | { |
| 4176 | bool flat; |
| 4177 | u32 *entry, new_entry; |
| 4178 | |
| 4179 | flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT; |
| 4180 | entry = avic_get_logical_id_entry(vcpu, ldr, flat); |
| 4181 | if (!entry) |
| 4182 | return -EINVAL; |
| 4183 | |
| 4184 | new_entry = READ_ONCE(*entry); |
| 4185 | new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK; |
| 4186 | new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK); |
| 4187 | if (valid) |
| 4188 | new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK; |
| 4189 | else |
| 4190 | new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK; |
| 4191 | WRITE_ONCE(*entry, new_entry); |
| 4192 | |
| 4193 | return 0; |
| 4194 | } |
| 4195 | |
| 4196 | static int avic_handle_ldr_update(struct kvm_vcpu *vcpu) |
| 4197 | { |
| 4198 | int ret; |
| 4199 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4200 | u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR); |
| 4201 | |
| 4202 | if (!ldr) |
| 4203 | return 1; |
| 4204 | |
| 4205 | ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true); |
| 4206 | if (ret && svm->ldr_reg) { |
| 4207 | avic_ldr_write(vcpu, 0, svm->ldr_reg, false); |
| 4208 | svm->ldr_reg = 0; |
| 4209 | } else { |
| 4210 | svm->ldr_reg = ldr; |
| 4211 | } |
| 4212 | return ret; |
| 4213 | } |
| 4214 | |
| 4215 | static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu) |
| 4216 | { |
| 4217 | u64 *old, *new; |
| 4218 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4219 | u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID); |
| 4220 | u32 id = (apic_id_reg >> 24) & 0xff; |
| 4221 | |
| 4222 | if (vcpu->vcpu_id == id) |
| 4223 | return 0; |
| 4224 | |
| 4225 | old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id); |
| 4226 | new = avic_get_physical_id_entry(vcpu, id); |
| 4227 | if (!new || !old) |
| 4228 | return 1; |
| 4229 | |
| 4230 | /* We need to move physical_id_entry to new offset */ |
| 4231 | *new = *old; |
| 4232 | *old = 0ULL; |
| 4233 | to_svm(vcpu)->avic_physical_id_cache = new; |
| 4234 | |
| 4235 | /* |
| 4236 | * Also update the guest physical APIC ID in the logical |
| 4237 | * APIC ID table entry if already setup the LDR. |
| 4238 | */ |
| 4239 | if (svm->ldr_reg) |
| 4240 | avic_handle_ldr_update(vcpu); |
| 4241 | |
| 4242 | return 0; |
| 4243 | } |
| 4244 | |
| 4245 | static int avic_handle_dfr_update(struct kvm_vcpu *vcpu) |
| 4246 | { |
| 4247 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4248 | struct kvm_arch *vm_data = &vcpu->kvm->arch; |
| 4249 | u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR); |
| 4250 | u32 mod = (dfr >> 28) & 0xf; |
| 4251 | |
| 4252 | /* |
| 4253 | * We assume that all local APICs are using the same type. |
| 4254 | * If this changes, we need to flush the AVIC logical |
| 4255 | * APID id table. |
| 4256 | */ |
| 4257 | if (vm_data->ldr_mode == mod) |
| 4258 | return 0; |
| 4259 | |
| 4260 | clear_page(page_address(vm_data->avic_logical_id_table_page)); |
| 4261 | vm_data->ldr_mode = mod; |
| 4262 | |
| 4263 | if (svm->ldr_reg) |
| 4264 | avic_handle_ldr_update(vcpu); |
| 4265 | return 0; |
| 4266 | } |
| 4267 | |
| 4268 | static int avic_unaccel_trap_write(struct vcpu_svm *svm) |
| 4269 | { |
| 4270 | struct kvm_lapic *apic = svm->vcpu.arch.apic; |
| 4271 | u32 offset = svm->vmcb->control.exit_info_1 & |
| 4272 | AVIC_UNACCEL_ACCESS_OFFSET_MASK; |
| 4273 | |
| 4274 | switch (offset) { |
| 4275 | case APIC_ID: |
| 4276 | if (avic_handle_apic_id_update(&svm->vcpu)) |
| 4277 | return 0; |
| 4278 | break; |
| 4279 | case APIC_LDR: |
| 4280 | if (avic_handle_ldr_update(&svm->vcpu)) |
| 4281 | return 0; |
| 4282 | break; |
| 4283 | case APIC_DFR: |
| 4284 | avic_handle_dfr_update(&svm->vcpu); |
| 4285 | break; |
| 4286 | default: |
| 4287 | break; |
| 4288 | } |
| 4289 | |
| 4290 | kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset)); |
| 4291 | |
| 4292 | return 1; |
| 4293 | } |
| 4294 | |
| 4295 | static bool is_avic_unaccelerated_access_trap(u32 offset) |
| 4296 | { |
| 4297 | bool ret = false; |
| 4298 | |
| 4299 | switch (offset) { |
| 4300 | case APIC_ID: |
| 4301 | case APIC_EOI: |
| 4302 | case APIC_RRR: |
| 4303 | case APIC_LDR: |
| 4304 | case APIC_DFR: |
| 4305 | case APIC_SPIV: |
| 4306 | case APIC_ESR: |
| 4307 | case APIC_ICR: |
| 4308 | case APIC_LVTT: |
| 4309 | case APIC_LVTTHMR: |
| 4310 | case APIC_LVTPC: |
| 4311 | case APIC_LVT0: |
| 4312 | case APIC_LVT1: |
| 4313 | case APIC_LVTERR: |
| 4314 | case APIC_TMICT: |
| 4315 | case APIC_TDCR: |
| 4316 | ret = true; |
| 4317 | break; |
| 4318 | default: |
| 4319 | break; |
| 4320 | } |
| 4321 | return ret; |
| 4322 | } |
| 4323 | |
| 4324 | static int avic_unaccelerated_access_interception(struct vcpu_svm *svm) |
| 4325 | { |
| 4326 | int ret = 0; |
| 4327 | u32 offset = svm->vmcb->control.exit_info_1 & |
| 4328 | AVIC_UNACCEL_ACCESS_OFFSET_MASK; |
| 4329 | u32 vector = svm->vmcb->control.exit_info_2 & |
| 4330 | AVIC_UNACCEL_ACCESS_VECTOR_MASK; |
| 4331 | bool write = (svm->vmcb->control.exit_info_1 >> 32) & |
| 4332 | AVIC_UNACCEL_ACCESS_WRITE_MASK; |
| 4333 | bool trap = is_avic_unaccelerated_access_trap(offset); |
| 4334 | |
| 4335 | trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset, |
| 4336 | trap, write, vector); |
| 4337 | if (trap) { |
| 4338 | /* Handling Trap */ |
| 4339 | WARN_ONCE(!write, "svm: Handling trap read.\n"); |
| 4340 | ret = avic_unaccel_trap_write(svm); |
| 4341 | } else { |
| 4342 | /* Handling Fault */ |
| 4343 | ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE); |
| 4344 | } |
| 4345 | |
| 4346 | return ret; |
| 4347 | } |
| 4348 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 4349 | static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4350 | [SVM_EXIT_READ_CR0] = cr_interception, |
| 4351 | [SVM_EXIT_READ_CR3] = cr_interception, |
| 4352 | [SVM_EXIT_READ_CR4] = cr_interception, |
| 4353 | [SVM_EXIT_READ_CR8] = cr_interception, |
David Kaplan | 5e57518 | 2015-03-06 14:44:35 -0600 | [diff] [blame] | 4354 | [SVM_EXIT_CR0_SEL_WRITE] = cr_interception, |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 4355 | [SVM_EXIT_WRITE_CR0] = cr_interception, |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4356 | [SVM_EXIT_WRITE_CR3] = cr_interception, |
| 4357 | [SVM_EXIT_WRITE_CR4] = cr_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4358 | [SVM_EXIT_WRITE_CR8] = cr8_write_interception, |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 4359 | [SVM_EXIT_READ_DR0] = dr_interception, |
| 4360 | [SVM_EXIT_READ_DR1] = dr_interception, |
| 4361 | [SVM_EXIT_READ_DR2] = dr_interception, |
| 4362 | [SVM_EXIT_READ_DR3] = dr_interception, |
| 4363 | [SVM_EXIT_READ_DR4] = dr_interception, |
| 4364 | [SVM_EXIT_READ_DR5] = dr_interception, |
| 4365 | [SVM_EXIT_READ_DR6] = dr_interception, |
| 4366 | [SVM_EXIT_READ_DR7] = dr_interception, |
| 4367 | [SVM_EXIT_WRITE_DR0] = dr_interception, |
| 4368 | [SVM_EXIT_WRITE_DR1] = dr_interception, |
| 4369 | [SVM_EXIT_WRITE_DR2] = dr_interception, |
| 4370 | [SVM_EXIT_WRITE_DR3] = dr_interception, |
| 4371 | [SVM_EXIT_WRITE_DR4] = dr_interception, |
| 4372 | [SVM_EXIT_WRITE_DR5] = dr_interception, |
| 4373 | [SVM_EXIT_WRITE_DR6] = dr_interception, |
| 4374 | [SVM_EXIT_WRITE_DR7] = dr_interception, |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4375 | [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, |
| 4376 | [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4377 | [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4378 | [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4379 | [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 4380 | [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4381 | [SVM_EXIT_INTR] = intr_interception, |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 4382 | [SVM_EXIT_NMI] = nmi_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4383 | [SVM_EXIT_SMI] = nop_on_interception, |
| 4384 | [SVM_EXIT_INIT] = nop_on_interception, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4385 | [SVM_EXIT_VINTR] = interrupt_window_interception, |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 4386 | [SVM_EXIT_RDPMC] = rdpmc_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4387 | [SVM_EXIT_CPUID] = cpuid_interception, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4388 | [SVM_EXIT_IRET] = iret_interception, |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 4389 | [SVM_EXIT_INVD] = emulate_on_interception, |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 4390 | [SVM_EXIT_PAUSE] = pause_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4391 | [SVM_EXIT_HLT] = halt_interception, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4392 | [SVM_EXIT_INVLPG] = invlpg_interception, |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 4393 | [SVM_EXIT_INVLPGA] = invlpga_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4394 | [SVM_EXIT_IOIO] = io_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4395 | [SVM_EXIT_MSR] = msr_interception, |
| 4396 | [SVM_EXIT_TASK_SWITCH] = task_switch_interception, |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 4397 | [SVM_EXIT_SHUTDOWN] = shutdown_interception, |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 4398 | [SVM_EXIT_VMRUN] = vmrun_interception, |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4399 | [SVM_EXIT_VMMCALL] = vmmcall_interception, |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 4400 | [SVM_EXIT_VMLOAD] = vmload_interception, |
| 4401 | [SVM_EXIT_VMSAVE] = vmsave_interception, |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 4402 | [SVM_EXIT_STGI] = stgi_interception, |
| 4403 | [SVM_EXIT_CLGI] = clgi_interception, |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 4404 | [SVM_EXIT_SKINIT] = skinit_interception, |
David Kaplan | dab429a | 2015-03-02 13:43:37 -0600 | [diff] [blame] | 4405 | [SVM_EXIT_WBINVD] = wbinvd_interception, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 4406 | [SVM_EXIT_MONITOR] = monitor_interception, |
| 4407 | [SVM_EXIT_MWAIT] = mwait_interception, |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 4408 | [SVM_EXIT_XSETBV] = xsetbv_interception, |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 4409 | [SVM_EXIT_NPF] = npf_interception, |
Paolo Bonzini | 64d6067 | 2015-05-07 11:36:11 +0200 | [diff] [blame] | 4410 | [SVM_EXIT_RSM] = emulate_on_interception, |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4411 | [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception, |
| 4412 | [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4413 | }; |
| 4414 | |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4415 | static void dump_vmcb(struct kvm_vcpu *vcpu) |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4416 | { |
| 4417 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4418 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 4419 | struct vmcb_save_area *save = &svm->vmcb->save; |
| 4420 | |
| 4421 | pr_err("VMCB Control Area:\n"); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4422 | pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff); |
| 4423 | pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16); |
| 4424 | pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff); |
| 4425 | pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16); |
| 4426 | pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions); |
| 4427 | pr_err("%-20s%016llx\n", "intercepts:", control->intercept); |
| 4428 | pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count); |
| 4429 | pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa); |
| 4430 | pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa); |
| 4431 | pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset); |
| 4432 | pr_err("%-20s%d\n", "asid:", control->asid); |
| 4433 | pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl); |
| 4434 | pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl); |
| 4435 | pr_err("%-20s%08x\n", "int_vector:", control->int_vector); |
| 4436 | pr_err("%-20s%08x\n", "int_state:", control->int_state); |
| 4437 | pr_err("%-20s%08x\n", "exit_code:", control->exit_code); |
| 4438 | pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1); |
| 4439 | pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2); |
| 4440 | pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info); |
| 4441 | pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err); |
| 4442 | pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl); |
| 4443 | pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4444 | pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4445 | pr_err("%-20s%08x\n", "event_inj:", control->event_inj); |
| 4446 | pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err); |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 4447 | pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4448 | pr_err("%-20s%016llx\n", "next_rip:", control->next_rip); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4449 | pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page); |
| 4450 | pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id); |
| 4451 | pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id); |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4452 | pr_err("VMCB State Save Area:\n"); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4453 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4454 | "es:", |
| 4455 | save->es.selector, save->es.attrib, |
| 4456 | save->es.limit, save->es.base); |
| 4457 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4458 | "cs:", |
| 4459 | save->cs.selector, save->cs.attrib, |
| 4460 | save->cs.limit, save->cs.base); |
| 4461 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4462 | "ss:", |
| 4463 | save->ss.selector, save->ss.attrib, |
| 4464 | save->ss.limit, save->ss.base); |
| 4465 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4466 | "ds:", |
| 4467 | save->ds.selector, save->ds.attrib, |
| 4468 | save->ds.limit, save->ds.base); |
| 4469 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4470 | "fs:", |
| 4471 | save->fs.selector, save->fs.attrib, |
| 4472 | save->fs.limit, save->fs.base); |
| 4473 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4474 | "gs:", |
| 4475 | save->gs.selector, save->gs.attrib, |
| 4476 | save->gs.limit, save->gs.base); |
| 4477 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4478 | "gdtr:", |
| 4479 | save->gdtr.selector, save->gdtr.attrib, |
| 4480 | save->gdtr.limit, save->gdtr.base); |
| 4481 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4482 | "ldtr:", |
| 4483 | save->ldtr.selector, save->ldtr.attrib, |
| 4484 | save->ldtr.limit, save->ldtr.base); |
| 4485 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4486 | "idtr:", |
| 4487 | save->idtr.selector, save->idtr.attrib, |
| 4488 | save->idtr.limit, save->idtr.base); |
| 4489 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4490 | "tr:", |
| 4491 | save->tr.selector, save->tr.attrib, |
| 4492 | save->tr.limit, save->tr.base); |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4493 | pr_err("cpl: %d efer: %016llx\n", |
| 4494 | save->cpl, save->efer); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4495 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4496 | "cr0:", save->cr0, "cr2:", save->cr2); |
| 4497 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4498 | "cr3:", save->cr3, "cr4:", save->cr4); |
| 4499 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4500 | "dr6:", save->dr6, "dr7:", save->dr7); |
| 4501 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4502 | "rip:", save->rip, "rflags:", save->rflags); |
| 4503 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4504 | "rsp:", save->rsp, "rax:", save->rax); |
| 4505 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4506 | "star:", save->star, "lstar:", save->lstar); |
| 4507 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4508 | "cstar:", save->cstar, "sfmask:", save->sfmask); |
| 4509 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4510 | "kernel_gs_base:", save->kernel_gs_base, |
| 4511 | "sysenter_cs:", save->sysenter_cs); |
| 4512 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4513 | "sysenter_esp:", save->sysenter_esp, |
| 4514 | "sysenter_eip:", save->sysenter_eip); |
| 4515 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4516 | "gpat:", save->g_pat, "dbgctl:", save->dbgctl); |
| 4517 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4518 | "br_from:", save->br_from, "br_to:", save->br_to); |
| 4519 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4520 | "excp_from:", save->last_excp_from, |
| 4521 | "excp_to:", save->last_excp_to); |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4522 | } |
| 4523 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 4524 | static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 4525 | { |
| 4526 | struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; |
| 4527 | |
| 4528 | *info1 = control->exit_info_1; |
| 4529 | *info2 = control->exit_info_2; |
| 4530 | } |
| 4531 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4532 | static int handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4533 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4534 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4535 | struct kvm_run *kvm_run = vcpu->run; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4536 | u32 exit_code = svm->vmcb->control.exit_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4537 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 4538 | trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM); |
| 4539 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 4540 | if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE)) |
Joerg Roedel | 2be4fc7 | 2010-04-22 12:33:09 +0200 | [diff] [blame] | 4541 | vcpu->arch.cr0 = svm->vmcb->save.cr0; |
| 4542 | if (npt_enabled) |
| 4543 | vcpu->arch.cr3 = svm->vmcb->save.cr3; |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 4544 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 4545 | if (unlikely(svm->nested.exit_required)) { |
| 4546 | nested_svm_vmexit(svm); |
| 4547 | svm->nested.exit_required = false; |
| 4548 | |
| 4549 | return 1; |
| 4550 | } |
| 4551 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 4552 | if (is_guest_mode(vcpu)) { |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 4553 | int vmexit; |
| 4554 | |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 4555 | trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code, |
| 4556 | svm->vmcb->control.exit_info_1, |
| 4557 | svm->vmcb->control.exit_info_2, |
| 4558 | svm->vmcb->control.exit_int_info, |
Stefan Hajnoczi | e097e5f | 2011-07-22 12:46:52 +0100 | [diff] [blame] | 4559 | svm->vmcb->control.exit_int_info_err, |
| 4560 | KVM_ISA_SVM); |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 4561 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 4562 | vmexit = nested_svm_exit_special(svm); |
| 4563 | |
| 4564 | if (vmexit == NESTED_EXIT_CONTINUE) |
| 4565 | vmexit = nested_svm_exit_handled(svm); |
| 4566 | |
| 4567 | if (vmexit == NESTED_EXIT_DONE) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 4568 | return 1; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 4569 | } |
| 4570 | |
Joerg Roedel | a5c3832 | 2009-08-07 11:49:32 +0200 | [diff] [blame] | 4571 | svm_complete_interrupts(svm); |
| 4572 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4573 | if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { |
| 4574 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 4575 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 4576 | = svm->vmcb->control.exit_code; |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4577 | pr_err("KVM: FAILED VMRUN WITH VMCB:\n"); |
| 4578 | dump_vmcb(vcpu); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4579 | return 0; |
| 4580 | } |
| 4581 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4582 | if (is_external_interrupt(svm->vmcb->control.exit_int_info) && |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 4583 | exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && |
Joerg Roedel | 55c5e46 | 2010-09-10 17:31:04 +0200 | [diff] [blame] | 4584 | exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH && |
| 4585 | exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI) |
Borislav Petkov | 6614c7d | 2013-04-26 00:22:01 +0200 | [diff] [blame] | 4586 | printk(KERN_ERR "%s: unexpected exit_int_info 0x%x " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4587 | "exit_code 0x%x\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 4588 | __func__, svm->vmcb->control.exit_int_info, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4589 | exit_code); |
| 4590 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 4591 | if (exit_code >= ARRAY_SIZE(svm_exit_handlers) |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 4592 | || !svm_exit_handlers[exit_code]) { |
Bandan Das | faac245 | 2015-03-16 17:18:25 -0400 | [diff] [blame] | 4593 | 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] | 4594 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4595 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4596 | } |
| 4597 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4598 | return svm_exit_handlers[exit_code](svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | static void reload_tss(struct kvm_vcpu *vcpu) |
| 4602 | { |
| 4603 | int cpu = raw_smp_processor_id(); |
| 4604 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 4605 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 4606 | sd->tss_desc->type = 9; /* available 32/64-bit TSS */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4607 | load_TR_desc(); |
| 4608 | } |
| 4609 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 4610 | static void pre_sev_run(struct vcpu_svm *svm, int cpu) |
| 4611 | { |
| 4612 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 4613 | int asid = sev_get_asid(svm->vcpu.kvm); |
| 4614 | |
| 4615 | /* Assign the asid allocated with this SEV guest */ |
| 4616 | svm->vmcb->control.asid = asid; |
| 4617 | |
| 4618 | /* |
| 4619 | * Flush guest TLB: |
| 4620 | * |
| 4621 | * 1) when different VMCB for the same ASID is to be run on the same host CPU. |
| 4622 | * 2) or this VMCB was executed on different host CPU in previous VMRUNs. |
| 4623 | */ |
| 4624 | if (sd->sev_vmcbs[asid] == svm->vmcb && |
| 4625 | svm->last_cpu == cpu) |
| 4626 | return; |
| 4627 | |
| 4628 | svm->last_cpu = cpu; |
| 4629 | sd->sev_vmcbs[asid] = svm->vmcb; |
| 4630 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; |
| 4631 | mark_dirty(svm->vmcb, VMCB_ASID); |
| 4632 | } |
| 4633 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 4634 | static void pre_svm_run(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4635 | { |
| 4636 | int cpu = raw_smp_processor_id(); |
| 4637 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 4638 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4639 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 4640 | if (sev_guest(svm->vcpu.kvm)) |
| 4641 | return pre_sev_run(svm, cpu); |
| 4642 | |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 4643 | /* FIXME: handle wraparound of asid_generation */ |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 4644 | if (svm->asid_generation != sd->asid_generation) |
| 4645 | new_asid(svm, sd); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4646 | } |
| 4647 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4648 | static void svm_inject_nmi(struct kvm_vcpu *vcpu) |
| 4649 | { |
| 4650 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4651 | |
| 4652 | svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; |
| 4653 | vcpu->arch.hflags |= HF_NMI_MASK; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 4654 | set_intercept(svm, INTERCEPT_IRET); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4655 | ++vcpu->stat.nmi_injections; |
| 4656 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4657 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4658 | static inline void svm_inject_irq(struct vcpu_svm *svm, int irq) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4659 | { |
| 4660 | struct vmcb_control_area *control; |
| 4661 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 4662 | /* The following fields are ignored when AVIC is enabled */ |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 4663 | control = &svm->vmcb->control; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4664 | control->int_vector = irq; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4665 | control->int_ctl &= ~V_INTR_PRIO_MASK; |
| 4666 | control->int_ctl |= V_IRQ_MASK | |
| 4667 | ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); |
Joerg Roedel | decdbf6 | 2010-12-03 11:45:52 +0100 | [diff] [blame] | 4668 | mark_dirty(svm->vmcb, VMCB_INTR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4669 | } |
| 4670 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4671 | static void svm_set_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 4672 | { |
| 4673 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4674 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 4675 | BUG_ON(!(gif_set(svm))); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 4676 | |
Gleb Natapov | 9fb2d2b | 2010-05-23 14:28:26 +0300 | [diff] [blame] | 4677 | trace_kvm_inj_virq(vcpu->arch.interrupt.nr); |
| 4678 | ++vcpu->stat.irq_injections; |
| 4679 | |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 4680 | svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | |
| 4681 | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR; |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 4682 | } |
| 4683 | |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 4684 | static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu) |
| 4685 | { |
| 4686 | return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK); |
| 4687 | } |
| 4688 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4689 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
| 4690 | { |
| 4691 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4692 | |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 4693 | if (svm_nested_virtualize_tpr(vcpu) || |
| 4694 | kvm_vcpu_apicv_active(vcpu)) |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 4695 | return; |
| 4696 | |
Radim Krčmář | 596f314 | 2014-03-11 19:11:18 +0100 | [diff] [blame] | 4697 | clr_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
| 4698 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4699 | if (irr == -1) |
| 4700 | return; |
| 4701 | |
| 4702 | if (tpr >= irr) |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 4703 | set_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4704 | } |
| 4705 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4706 | static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
| 4707 | { |
| 4708 | return; |
| 4709 | } |
| 4710 | |
Suravee Suthikulpanit | b2a05fe | 2017-09-12 10:42:41 -0500 | [diff] [blame] | 4711 | static bool svm_get_enable_apicv(struct kvm_vcpu *vcpu) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4712 | { |
Suravee Suthikulpanit | 67034bb | 2017-09-12 10:42:42 -0500 | [diff] [blame] | 4713 | return avic && irqchip_split(vcpu->kvm); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4714 | } |
| 4715 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4716 | static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 4717 | { |
| 4718 | } |
| 4719 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 4720 | 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] | 4721 | { |
| 4722 | } |
| 4723 | |
| 4724 | /* Note: Currently only used by Hyper-V. */ |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4725 | static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 4726 | { |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4727 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4728 | struct vmcb *vmcb = svm->vmcb; |
| 4729 | |
Suravee Suthikulpanit | 67034bb | 2017-09-12 10:42:42 -0500 | [diff] [blame] | 4730 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4731 | return; |
| 4732 | |
| 4733 | vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK; |
| 4734 | mark_dirty(vmcb, VMCB_INTR); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4735 | } |
| 4736 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 4737 | 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] | 4738 | { |
| 4739 | return; |
| 4740 | } |
| 4741 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 4742 | static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) |
| 4743 | { |
| 4744 | kvm_lapic_set_irr(vec, vcpu->arch.apic); |
| 4745 | smp_mb__after_atomic(); |
| 4746 | |
| 4747 | if (avic_vcpu_is_running(vcpu)) |
| 4748 | wrmsrl(SVM_AVIC_DOORBELL, |
Suravee Suthikulpanit | 7d669f5 | 2016-06-15 17:23:45 -0500 | [diff] [blame] | 4749 | kvm_cpu_get_apicid(vcpu->cpu)); |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 4750 | else |
| 4751 | kvm_vcpu_wake_up(vcpu); |
| 4752 | } |
| 4753 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 4754 | static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) |
| 4755 | { |
| 4756 | unsigned long flags; |
| 4757 | struct amd_svm_iommu_ir *cur; |
| 4758 | |
| 4759 | spin_lock_irqsave(&svm->ir_list_lock, flags); |
| 4760 | list_for_each_entry(cur, &svm->ir_list, node) { |
| 4761 | if (cur->data != pi->ir_data) |
| 4762 | continue; |
| 4763 | list_del(&cur->node); |
| 4764 | kfree(cur); |
| 4765 | break; |
| 4766 | } |
| 4767 | spin_unlock_irqrestore(&svm->ir_list_lock, flags); |
| 4768 | } |
| 4769 | |
| 4770 | static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) |
| 4771 | { |
| 4772 | int ret = 0; |
| 4773 | unsigned long flags; |
| 4774 | struct amd_svm_iommu_ir *ir; |
| 4775 | |
| 4776 | /** |
| 4777 | * In some cases, the existing irte is updaed and re-set, |
| 4778 | * so we need to check here if it's already been * added |
| 4779 | * to the ir_list. |
| 4780 | */ |
| 4781 | if (pi->ir_data && (pi->prev_ga_tag != 0)) { |
| 4782 | struct kvm *kvm = svm->vcpu.kvm; |
| 4783 | u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag); |
| 4784 | struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); |
| 4785 | struct vcpu_svm *prev_svm; |
| 4786 | |
| 4787 | if (!prev_vcpu) { |
| 4788 | ret = -EINVAL; |
| 4789 | goto out; |
| 4790 | } |
| 4791 | |
| 4792 | prev_svm = to_svm(prev_vcpu); |
| 4793 | svm_ir_list_del(prev_svm, pi); |
| 4794 | } |
| 4795 | |
| 4796 | /** |
| 4797 | * Allocating new amd_iommu_pi_data, which will get |
| 4798 | * add to the per-vcpu ir_list. |
| 4799 | */ |
| 4800 | ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL); |
| 4801 | if (!ir) { |
| 4802 | ret = -ENOMEM; |
| 4803 | goto out; |
| 4804 | } |
| 4805 | ir->data = pi->ir_data; |
| 4806 | |
| 4807 | spin_lock_irqsave(&svm->ir_list_lock, flags); |
| 4808 | list_add(&ir->node, &svm->ir_list); |
| 4809 | spin_unlock_irqrestore(&svm->ir_list_lock, flags); |
| 4810 | out: |
| 4811 | return ret; |
| 4812 | } |
| 4813 | |
| 4814 | /** |
| 4815 | * Note: |
| 4816 | * The HW cannot support posting multicast/broadcast |
| 4817 | * interrupts to a vCPU. So, we still use legacy interrupt |
| 4818 | * remapping for these kind of interrupts. |
| 4819 | * |
| 4820 | * For lowest-priority interrupts, we only support |
| 4821 | * those with single CPU as the destination, e.g. user |
| 4822 | * configures the interrupts via /proc/irq or uses |
| 4823 | * irqbalance to make the interrupts single-CPU. |
| 4824 | */ |
| 4825 | static int |
| 4826 | get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e, |
| 4827 | struct vcpu_data *vcpu_info, struct vcpu_svm **svm) |
| 4828 | { |
| 4829 | struct kvm_lapic_irq irq; |
| 4830 | struct kvm_vcpu *vcpu = NULL; |
| 4831 | |
| 4832 | kvm_set_msi_irq(kvm, e, &irq); |
| 4833 | |
| 4834 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 4835 | pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n", |
| 4836 | __func__, irq.vector); |
| 4837 | return -1; |
| 4838 | } |
| 4839 | |
| 4840 | pr_debug("SVM: %s: use GA mode for irq %u\n", __func__, |
| 4841 | irq.vector); |
| 4842 | *svm = to_svm(vcpu); |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 4843 | vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page)); |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 4844 | vcpu_info->vector = irq.vector; |
| 4845 | |
| 4846 | return 0; |
| 4847 | } |
| 4848 | |
| 4849 | /* |
| 4850 | * svm_update_pi_irte - set IRTE for Posted-Interrupts |
| 4851 | * |
| 4852 | * @kvm: kvm |
| 4853 | * @host_irq: host irq of the interrupt |
| 4854 | * @guest_irq: gsi of the interrupt |
| 4855 | * @set: set or unset PI |
| 4856 | * returns 0 on success, < 0 on failure |
| 4857 | */ |
| 4858 | static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 4859 | uint32_t guest_irq, bool set) |
| 4860 | { |
| 4861 | struct kvm_kernel_irq_routing_entry *e; |
| 4862 | struct kvm_irq_routing_table *irq_rt; |
| 4863 | int idx, ret = -EINVAL; |
| 4864 | |
| 4865 | if (!kvm_arch_has_assigned_device(kvm) || |
| 4866 | !irq_remapping_cap(IRQ_POSTING_CAP)) |
| 4867 | return 0; |
| 4868 | |
| 4869 | pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n", |
| 4870 | __func__, host_irq, guest_irq, set); |
| 4871 | |
| 4872 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 4873 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
| 4874 | WARN_ON(guest_irq >= irq_rt->nr_rt_entries); |
| 4875 | |
| 4876 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 4877 | struct vcpu_data vcpu_info; |
| 4878 | struct vcpu_svm *svm = NULL; |
| 4879 | |
| 4880 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 4881 | continue; |
| 4882 | |
| 4883 | /** |
| 4884 | * Here, we setup with legacy mode in the following cases: |
| 4885 | * 1. When cannot target interrupt to a specific vcpu. |
| 4886 | * 2. Unsetting posted interrupt. |
| 4887 | * 3. APIC virtialization is disabled for the vcpu. |
| 4888 | */ |
| 4889 | if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set && |
| 4890 | kvm_vcpu_apicv_active(&svm->vcpu)) { |
| 4891 | struct amd_iommu_pi_data pi; |
| 4892 | |
| 4893 | /* Try to enable guest_mode in IRTE */ |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 4894 | pi.base = __sme_set(page_to_phys(svm->avic_backing_page) & |
| 4895 | AVIC_HPA_MASK); |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 4896 | pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id, |
| 4897 | svm->vcpu.vcpu_id); |
| 4898 | pi.is_guest_mode = true; |
| 4899 | pi.vcpu_data = &vcpu_info; |
| 4900 | ret = irq_set_vcpu_affinity(host_irq, &pi); |
| 4901 | |
| 4902 | /** |
| 4903 | * Here, we successfully setting up vcpu affinity in |
| 4904 | * IOMMU guest mode. Now, we need to store the posted |
| 4905 | * interrupt information in a per-vcpu ir_list so that |
| 4906 | * we can reference to them directly when we update vcpu |
| 4907 | * scheduling information in IOMMU irte. |
| 4908 | */ |
| 4909 | if (!ret && pi.is_guest_mode) |
| 4910 | svm_ir_list_add(svm, &pi); |
| 4911 | } else { |
| 4912 | /* Use legacy mode in IRTE */ |
| 4913 | struct amd_iommu_pi_data pi; |
| 4914 | |
| 4915 | /** |
| 4916 | * Here, pi is used to: |
| 4917 | * - Tell IOMMU to use legacy mode for this interrupt. |
| 4918 | * - Retrieve ga_tag of prior interrupt remapping data. |
| 4919 | */ |
| 4920 | pi.is_guest_mode = false; |
| 4921 | ret = irq_set_vcpu_affinity(host_irq, &pi); |
| 4922 | |
| 4923 | /** |
| 4924 | * Check if the posted interrupt was previously |
| 4925 | * setup with the guest_mode by checking if the ga_tag |
| 4926 | * was cached. If so, we need to clean up the per-vcpu |
| 4927 | * ir_list. |
| 4928 | */ |
| 4929 | if (!ret && pi.prev_ga_tag) { |
| 4930 | int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag); |
| 4931 | struct kvm_vcpu *vcpu; |
| 4932 | |
| 4933 | vcpu = kvm_get_vcpu_by_id(kvm, id); |
| 4934 | if (vcpu) |
| 4935 | svm_ir_list_del(to_svm(vcpu), &pi); |
| 4936 | } |
| 4937 | } |
| 4938 | |
| 4939 | if (!ret && svm) { |
| 4940 | trace_kvm_pi_irte_update(svm->vcpu.vcpu_id, |
| 4941 | host_irq, e->gsi, |
| 4942 | vcpu_info.vector, |
| 4943 | vcpu_info.pi_desc_addr, set); |
| 4944 | } |
| 4945 | |
| 4946 | if (ret < 0) { |
| 4947 | pr_err("%s: failed to update PI IRTE\n", __func__); |
| 4948 | goto out; |
| 4949 | } |
| 4950 | } |
| 4951 | |
| 4952 | ret = 0; |
| 4953 | out: |
| 4954 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 4955 | return ret; |
| 4956 | } |
| 4957 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4958 | static int svm_nmi_allowed(struct kvm_vcpu *vcpu) |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 4959 | { |
| 4960 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4961 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 924584c | 2010-04-22 12:33:07 +0200 | [diff] [blame] | 4962 | int ret; |
| 4963 | ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) && |
| 4964 | !(svm->vcpu.arch.hflags & HF_NMI_MASK); |
| 4965 | ret = ret && gif_set(svm) && nested_svm_nmi(svm); |
| 4966 | |
| 4967 | return ret; |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 4968 | } |
| 4969 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4970 | static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 4971 | { |
| 4972 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4973 | |
| 4974 | return !!(svm->vcpu.arch.hflags & HF_NMI_MASK); |
| 4975 | } |
| 4976 | |
| 4977 | static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 4978 | { |
| 4979 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4980 | |
| 4981 | if (masked) { |
| 4982 | svm->vcpu.arch.hflags |= HF_NMI_MASK; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 4983 | set_intercept(svm, INTERCEPT_IRET); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4984 | } else { |
| 4985 | svm->vcpu.arch.hflags &= ~HF_NMI_MASK; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 4986 | clr_intercept(svm, INTERCEPT_IRET); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4987 | } |
| 4988 | } |
| 4989 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4990 | static int svm_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 4991 | { |
| 4992 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4993 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 4994 | int ret; |
| 4995 | |
| 4996 | if (!gif_set(svm) || |
| 4997 | (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)) |
| 4998 | return 0; |
| 4999 | |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 5000 | ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF); |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 5001 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 5002 | if (is_guest_mode(vcpu)) |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 5003 | return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK); |
| 5004 | |
| 5005 | return ret; |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5006 | } |
| 5007 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5008 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5009 | { |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5010 | struct vcpu_svm *svm = to_svm(vcpu); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5011 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 5012 | if (kvm_vcpu_apicv_active(vcpu)) |
| 5013 | return; |
| 5014 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 5015 | /* |
| 5016 | * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes |
| 5017 | * 1, because that's a separate STGI/VMRUN intercept. The next time we |
| 5018 | * get that intercept, this function will be called again though and |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 5019 | * we'll get the vintr intercept. However, if the vGIF feature is |
| 5020 | * enabled, the STGI interception will not occur. Enable the irq |
| 5021 | * window under the assumption that the hardware will set the GIF. |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 5022 | */ |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 5023 | if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) { |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5024 | svm_set_vintr(svm); |
| 5025 | svm_inject_irq(svm, 0x0); |
| 5026 | } |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5027 | } |
| 5028 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5029 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5030 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 5031 | struct vcpu_svm *svm = to_svm(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5032 | |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5033 | if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) |
| 5034 | == HF_NMI_MASK) |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5035 | return; /* IRET will cause a vm exit */ |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5036 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 5037 | if (!gif_set(svm)) { |
| 5038 | if (vgif_enabled(svm)) |
| 5039 | set_intercept(svm, INTERCEPT_STGI); |
Ladi Prosek | 1a5e185 | 2017-06-21 09:07:01 +0200 | [diff] [blame] | 5040 | return; /* STGI will cause a vm exit */ |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 5041 | } |
Ladi Prosek | 1a5e185 | 2017-06-21 09:07:01 +0200 | [diff] [blame] | 5042 | |
| 5043 | if (svm->nested.exit_required) |
| 5044 | return; /* we're not going to run the guest yet */ |
| 5045 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 5046 | /* |
| 5047 | * Something prevents NMI from been injected. Single step over possible |
| 5048 | * problem (IRET or exception injection or interrupt shadow) |
| 5049 | */ |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 5050 | svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu); |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 5051 | svm->nmi_singlestep = true; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5052 | svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5053 | } |
| 5054 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5055 | static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 5056 | { |
| 5057 | return 0; |
| 5058 | } |
| 5059 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 5060 | static void svm_flush_tlb(struct kvm_vcpu *vcpu) |
| 5061 | { |
Joerg Roedel | 38e5e92 | 2010-12-03 15:25:16 +0100 | [diff] [blame] | 5062 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5063 | |
| 5064 | if (static_cpu_has(X86_FEATURE_FLUSHBYASID)) |
| 5065 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; |
| 5066 | else |
| 5067 | svm->asid_generation--; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 5068 | } |
| 5069 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 5070 | static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) |
| 5071 | { |
| 5072 | } |
| 5073 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 5074 | static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) |
| 5075 | { |
| 5076 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5077 | |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 5078 | if (svm_nested_virtualize_tpr(vcpu)) |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 5079 | return; |
| 5080 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 5081 | if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) { |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 5082 | int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; |
Gleb Natapov | 615d519 | 2009-04-21 17:45:05 +0300 | [diff] [blame] | 5083 | kvm_set_cr8(vcpu, cr8); |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 5084 | } |
| 5085 | } |
| 5086 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 5087 | static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) |
| 5088 | { |
| 5089 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5090 | u64 cr8; |
| 5091 | |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 5092 | if (svm_nested_virtualize_tpr(vcpu) || |
| 5093 | kvm_vcpu_apicv_active(vcpu)) |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 5094 | return; |
| 5095 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 5096 | cr8 = kvm_get_cr8(vcpu); |
| 5097 | svm->vmcb->control.int_ctl &= ~V_TPR_MASK; |
| 5098 | svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; |
| 5099 | } |
| 5100 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5101 | static void svm_complete_interrupts(struct vcpu_svm *svm) |
| 5102 | { |
| 5103 | u8 vector; |
| 5104 | int type; |
| 5105 | u32 exitintinfo = svm->vmcb->control.exit_int_info; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 5106 | unsigned int3_injected = svm->int3_injected; |
| 5107 | |
| 5108 | svm->int3_injected = 0; |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5109 | |
Avi Kivity | bd3d1ec | 2011-02-03 15:29:52 +0200 | [diff] [blame] | 5110 | /* |
| 5111 | * If we've made progress since setting HF_IRET_MASK, we've |
| 5112 | * executed an IRET and can allow NMI injection. |
| 5113 | */ |
| 5114 | if ((svm->vcpu.arch.hflags & HF_IRET_MASK) |
| 5115 | && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) { |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5116 | svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5117 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
| 5118 | } |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5119 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5120 | svm->vcpu.arch.nmi_injected = false; |
| 5121 | kvm_clear_exception_queue(&svm->vcpu); |
| 5122 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 5123 | |
| 5124 | if (!(exitintinfo & SVM_EXITINTINFO_VALID)) |
| 5125 | return; |
| 5126 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5127 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
| 5128 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5129 | vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK; |
| 5130 | type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK; |
| 5131 | |
| 5132 | switch (type) { |
| 5133 | case SVM_EXITINTINFO_TYPE_NMI: |
| 5134 | svm->vcpu.arch.nmi_injected = true; |
| 5135 | break; |
| 5136 | case SVM_EXITINTINFO_TYPE_EXEPT: |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 5137 | /* |
| 5138 | * In case of software exceptions, do not reinject the vector, |
| 5139 | * but re-execute the instruction instead. Rewind RIP first |
| 5140 | * if we emulated INT3 before. |
| 5141 | */ |
| 5142 | if (kvm_exception_is_soft(vector)) { |
| 5143 | if (vector == BP_VECTOR && int3_injected && |
| 5144 | kvm_is_linear_rip(&svm->vcpu, svm->int3_rip)) |
| 5145 | kvm_rip_write(&svm->vcpu, |
| 5146 | kvm_rip_read(&svm->vcpu) - |
| 5147 | int3_injected); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5148 | break; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 5149 | } |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5150 | if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) { |
| 5151 | u32 err = svm->vmcb->control.exit_int_info_err; |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 5152 | kvm_requeue_exception_e(&svm->vcpu, vector, err); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5153 | |
| 5154 | } else |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 5155 | kvm_requeue_exception(&svm->vcpu, vector); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5156 | break; |
| 5157 | case SVM_EXITINTINFO_TYPE_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5158 | kvm_queue_interrupt(&svm->vcpu, vector, false); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5159 | break; |
| 5160 | default: |
| 5161 | break; |
| 5162 | } |
| 5163 | } |
| 5164 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 5165 | static void svm_cancel_injection(struct kvm_vcpu *vcpu) |
| 5166 | { |
| 5167 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5168 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 5169 | |
| 5170 | control->exit_int_info = control->event_inj; |
| 5171 | control->exit_int_info_err = control->event_inj_err; |
| 5172 | control->event_inj = 0; |
| 5173 | svm_complete_interrupts(svm); |
| 5174 | } |
| 5175 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5176 | static void svm_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5177 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5178 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 5179 | |
Joerg Roedel | 2041a06 | 2010-04-22 12:33:08 +0200 | [diff] [blame] | 5180 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 5181 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 5182 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; |
| 5183 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 5184 | /* |
| 5185 | * A vmexit emulation is required before the vcpu can be executed |
| 5186 | * again. |
| 5187 | */ |
| 5188 | if (unlikely(svm->nested.exit_required)) |
| 5189 | return; |
| 5190 | |
Ladi Prosek | a12713c | 2017-06-21 09:07:00 +0200 | [diff] [blame] | 5191 | /* |
| 5192 | * Disable singlestep if we're injecting an interrupt/exception. |
| 5193 | * We don't want our modified rflags to be pushed on the stack where |
| 5194 | * we might not be able to easily reset them if we disabled NMI |
| 5195 | * singlestep later. |
| 5196 | */ |
| 5197 | if (svm->nmi_singlestep && svm->vmcb->control.event_inj) { |
| 5198 | /* |
| 5199 | * Event injection happens before external interrupts cause a |
| 5200 | * vmexit and interrupts are disabled here, so smp_send_reschedule |
| 5201 | * is enough to force an immediate vmexit. |
| 5202 | */ |
| 5203 | disable_nmi_singlestep(svm); |
| 5204 | smp_send_reschedule(vcpu->cpu); |
| 5205 | } |
| 5206 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 5207 | pre_svm_run(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5208 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 5209 | sync_lapic_to_cr8(vcpu); |
| 5210 | |
Joerg Roedel | cda0ffd | 2009-08-07 11:49:45 +0200 | [diff] [blame] | 5211 | svm->vmcb->save.cr2 = vcpu->arch.cr2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5212 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 5213 | clgi(); |
| 5214 | |
| 5215 | local_irq_enable(); |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 5216 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5217 | asm volatile ( |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5218 | "push %%" _ASM_BP "; \n\t" |
| 5219 | "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t" |
| 5220 | "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t" |
| 5221 | "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t" |
| 5222 | "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t" |
| 5223 | "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t" |
| 5224 | "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5225 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5226 | "mov %c[r8](%[svm]), %%r8 \n\t" |
| 5227 | "mov %c[r9](%[svm]), %%r9 \n\t" |
| 5228 | "mov %c[r10](%[svm]), %%r10 \n\t" |
| 5229 | "mov %c[r11](%[svm]), %%r11 \n\t" |
| 5230 | "mov %c[r12](%[svm]), %%r12 \n\t" |
| 5231 | "mov %c[r13](%[svm]), %%r13 \n\t" |
| 5232 | "mov %c[r14](%[svm]), %%r14 \n\t" |
| 5233 | "mov %c[r15](%[svm]), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5234 | #endif |
| 5235 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5236 | /* Enter guest mode */ |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5237 | "push %%" _ASM_AX " \n\t" |
| 5238 | "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 5239 | __ex(SVM_VMLOAD) "\n\t" |
| 5240 | __ex(SVM_VMRUN) "\n\t" |
| 5241 | __ex(SVM_VMSAVE) "\n\t" |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5242 | "pop %%" _ASM_AX " \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5243 | |
| 5244 | /* Save guest registers, load host registers */ |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5245 | "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t" |
| 5246 | "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t" |
| 5247 | "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t" |
| 5248 | "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t" |
| 5249 | "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t" |
| 5250 | "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5251 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5252 | "mov %%r8, %c[r8](%[svm]) \n\t" |
| 5253 | "mov %%r9, %c[r9](%[svm]) \n\t" |
| 5254 | "mov %%r10, %c[r10](%[svm]) \n\t" |
| 5255 | "mov %%r11, %c[r11](%[svm]) \n\t" |
| 5256 | "mov %%r12, %c[r12](%[svm]) \n\t" |
| 5257 | "mov %%r13, %c[r13](%[svm]) \n\t" |
| 5258 | "mov %%r14, %c[r14](%[svm]) \n\t" |
| 5259 | "mov %%r15, %c[r15](%[svm]) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5260 | #endif |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5261 | "pop %%" _ASM_BP |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5262 | : |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5263 | : [svm]"a"(svm), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5264 | [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5265 | [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 5266 | [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 5267 | [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 5268 | [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 5269 | [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 5270 | [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP])) |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5271 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5272 | , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])), |
| 5273 | [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])), |
| 5274 | [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])), |
| 5275 | [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])), |
| 5276 | [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])), |
| 5277 | [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])), |
| 5278 | [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])), |
| 5279 | [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15])) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5280 | #endif |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 5281 | : "cc", "memory" |
| 5282 | #ifdef CONFIG_X86_64 |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5283 | , "rbx", "rcx", "rdx", "rsi", "rdi" |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 5284 | , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15" |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5285 | #else |
| 5286 | , "ebx", "ecx", "edx", "esi", "edi" |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 5287 | #endif |
| 5288 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5289 | |
Avi Kivity | 82ca2d1 | 2010-10-21 12:20:34 +0200 | [diff] [blame] | 5290 | #ifdef CONFIG_X86_64 |
| 5291 | wrmsrl(MSR_GS_BASE, svm->host.gs_base); |
| 5292 | #else |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 5293 | loadsegment(fs, svm->host.fs); |
Avi Kivity | 831ca60 | 2011-03-08 16:09:51 +0200 | [diff] [blame] | 5294 | #ifndef CONFIG_X86_32_LAZY_GS |
| 5295 | loadsegment(gs, svm->host.gs); |
| 5296 | #endif |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 5297 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5298 | |
| 5299 | reload_tss(vcpu); |
| 5300 | |
Avi Kivity | 56ba47d | 2007-11-07 17:14:18 +0200 | [diff] [blame] | 5301 | local_irq_disable(); |
| 5302 | |
Avi Kivity | 13c34e0 | 2010-10-21 12:20:31 +0200 | [diff] [blame] | 5303 | vcpu->arch.cr2 = svm->vmcb->save.cr2; |
| 5304 | vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; |
| 5305 | vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; |
| 5306 | vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; |
| 5307 | |
Joerg Roedel | 3781c01 | 2011-01-14 16:45:02 +0100 | [diff] [blame] | 5308 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) |
| 5309 | kvm_before_handle_nmi(&svm->vcpu); |
| 5310 | |
| 5311 | stgi(); |
| 5312 | |
| 5313 | /* Any pending NMI will happen here */ |
| 5314 | |
| 5315 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) |
| 5316 | kvm_after_handle_nmi(&svm->vcpu); |
| 5317 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 5318 | sync_cr8_to_lapic(vcpu); |
| 5319 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5320 | svm->next_rip = 0; |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5321 | |
Joerg Roedel | 38e5e92 | 2010-12-03 15:25:16 +0100 | [diff] [blame] | 5322 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; |
| 5323 | |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 5324 | /* if exit due to PF check for async PF */ |
| 5325 | if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 5326 | svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 5327 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 5328 | if (npt_enabled) { |
| 5329 | vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR); |
| 5330 | vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR); |
| 5331 | } |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 5332 | |
| 5333 | /* |
| 5334 | * We need to handle MC intercepts here before the vcpu has a chance to |
| 5335 | * change the physical cpu |
| 5336 | */ |
| 5337 | if (unlikely(svm->vmcb->control.exit_code == |
| 5338 | SVM_EXIT_EXCP_BASE + MC_VECTOR)) |
| 5339 | svm_handle_mce(svm); |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 5340 | |
| 5341 | mark_all_clean(svm->vmcb); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5342 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 5343 | STACK_FRAME_NON_STANDARD(svm_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5344 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5345 | static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
| 5346 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5347 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5348 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 5349 | svm->vmcb->save.cr3 = __sme_set(root); |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 5350 | mark_dirty(svm->vmcb, VMCB_CR); |
Joerg Roedel | f40f6a4 | 2010-12-03 15:25:15 +0100 | [diff] [blame] | 5351 | svm_flush_tlb(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5352 | } |
| 5353 | |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 5354 | static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
| 5355 | { |
| 5356 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5357 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 5358 | svm->vmcb->control.nested_cr3 = __sme_set(root); |
Joerg Roedel | b274716 | 2010-12-03 11:45:53 +0100 | [diff] [blame] | 5359 | mark_dirty(svm->vmcb, VMCB_NPT); |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 5360 | |
| 5361 | /* Also sync guest cr3 here in case we live migrate */ |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 5362 | svm->vmcb->save.cr3 = kvm_read_cr3(vcpu); |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 5363 | mark_dirty(svm->vmcb, VMCB_CR); |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 5364 | |
Joerg Roedel | f40f6a4 | 2010-12-03 15:25:15 +0100 | [diff] [blame] | 5365 | svm_flush_tlb(vcpu); |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 5366 | } |
| 5367 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5368 | static int is_disabled(void) |
| 5369 | { |
Joerg Roedel | 6031a61 | 2007-06-22 12:29:50 +0300 | [diff] [blame] | 5370 | u64 vm_cr; |
| 5371 | |
| 5372 | rdmsrl(MSR_VM_CR, vm_cr); |
| 5373 | if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) |
| 5374 | return 1; |
| 5375 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5376 | return 0; |
| 5377 | } |
| 5378 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5379 | static void |
| 5380 | svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 5381 | { |
| 5382 | /* |
| 5383 | * Patch in the VMMCALL instruction: |
| 5384 | */ |
| 5385 | hypercall[0] = 0x0f; |
| 5386 | hypercall[1] = 0x01; |
| 5387 | hypercall[2] = 0xd9; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5388 | } |
| 5389 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 5390 | static void svm_check_processor_compat(void *rtn) |
| 5391 | { |
| 5392 | *(int *)rtn = 0; |
| 5393 | } |
| 5394 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 5395 | static bool svm_cpu_has_accelerated_tpr(void) |
| 5396 | { |
| 5397 | return false; |
| 5398 | } |
| 5399 | |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 5400 | static bool svm_has_high_real_mode_segbase(void) |
| 5401 | { |
| 5402 | return true; |
| 5403 | } |
| 5404 | |
Paolo Bonzini | fc07e76 | 2015-10-01 13:20:22 +0200 | [diff] [blame] | 5405 | static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
| 5406 | { |
| 5407 | return 0; |
| 5408 | } |
| 5409 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 5410 | static void svm_cpuid_update(struct kvm_vcpu *vcpu) |
| 5411 | { |
Joerg Roedel | 6092d3d | 2015-10-14 15:10:54 +0200 | [diff] [blame] | 5412 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5413 | |
| 5414 | /* Update nrips enabled cache */ |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 5415 | svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS); |
Suravee Suthikulpanit | 46781ea | 2016-05-04 14:09:50 -0500 | [diff] [blame] | 5416 | |
| 5417 | if (!kvm_vcpu_apicv_active(vcpu)) |
| 5418 | return; |
| 5419 | |
Radim Krčmář | 1b4d56b | 2017-08-05 00:12:50 +0200 | [diff] [blame] | 5420 | guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 5421 | } |
| 5422 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 5423 | static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 5424 | { |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5425 | switch (func) { |
Suravee Suthikulpanit | 46781ea | 2016-05-04 14:09:50 -0500 | [diff] [blame] | 5426 | case 0x1: |
| 5427 | if (avic) |
| 5428 | entry->ecx &= ~bit(X86_FEATURE_X2APIC); |
| 5429 | break; |
Joerg Roedel | 4c62a2d | 2010-09-10 17:31:06 +0200 | [diff] [blame] | 5430 | case 0x80000001: |
| 5431 | if (nested) |
| 5432 | entry->ecx |= (1 << 2); /* Set SVM bit */ |
| 5433 | break; |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5434 | case 0x8000000A: |
| 5435 | entry->eax = 1; /* SVM revision 1 */ |
| 5436 | entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper |
| 5437 | ASID emulation to nested SVM */ |
| 5438 | entry->ecx = 0; /* Reserved */ |
Joerg Roedel | 7a19066 | 2010-07-27 18:14:21 +0200 | [diff] [blame] | 5439 | entry->edx = 0; /* Per default do not support any |
| 5440 | additional features */ |
| 5441 | |
| 5442 | /* Support next_rip if host supports it */ |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 5443 | if (boot_cpu_has(X86_FEATURE_NRIPS)) |
Joerg Roedel | 7a19066 | 2010-07-27 18:14:21 +0200 | [diff] [blame] | 5444 | entry->edx |= SVM_FEATURE_NRIP; |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5445 | |
Joerg Roedel | 3d4aeaa | 2010-09-10 17:31:05 +0200 | [diff] [blame] | 5446 | /* Support NPT for the guest if enabled */ |
| 5447 | if (npt_enabled) |
| 5448 | entry->edx |= SVM_FEATURE_NPT; |
| 5449 | |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5450 | break; |
Brijesh Singh | 8765d75 | 2017-12-04 10:57:25 -0600 | [diff] [blame] | 5451 | case 0x8000001F: |
| 5452 | /* Support memory encryption cpuid if host supports it */ |
| 5453 | if (boot_cpu_has(X86_FEATURE_SEV)) |
| 5454 | cpuid(0x8000001f, &entry->eax, &entry->ebx, |
| 5455 | &entry->ecx, &entry->edx); |
| 5456 | |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5457 | } |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 5458 | } |
| 5459 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 5460 | static int svm_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 5461 | { |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 5462 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 5463 | } |
| 5464 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 5465 | static bool svm_rdtscp_supported(void) |
| 5466 | { |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 5467 | return boot_cpu_has(X86_FEATURE_RDTSCP); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 5468 | } |
| 5469 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 5470 | static bool svm_invpcid_supported(void) |
| 5471 | { |
| 5472 | return false; |
| 5473 | } |
| 5474 | |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 5475 | static bool svm_mpx_supported(void) |
| 5476 | { |
| 5477 | return false; |
| 5478 | } |
| 5479 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 5480 | static bool svm_xsaves_supported(void) |
| 5481 | { |
| 5482 | return false; |
| 5483 | } |
| 5484 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5485 | static bool svm_has_wbinvd_exit(void) |
| 5486 | { |
| 5487 | return true; |
| 5488 | } |
| 5489 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 5490 | #define PRE_EX(exit) { .exit_code = (exit), \ |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 5491 | .stage = X86_ICPT_PRE_EXCEPT, } |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5492 | #define POST_EX(exit) { .exit_code = (exit), \ |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 5493 | .stage = X86_ICPT_POST_EXCEPT, } |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 5494 | #define POST_MEM(exit) { .exit_code = (exit), \ |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 5495 | .stage = X86_ICPT_POST_MEMACCESS, } |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5496 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 5497 | static const struct __x86_intercept { |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5498 | u32 exit_code; |
| 5499 | enum x86_intercept_stage stage; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5500 | } x86_intercept_map[] = { |
| 5501 | [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0), |
| 5502 | [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 5503 | [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 5504 | [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 5505 | [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0), |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 5506 | [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0), |
| 5507 | [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 5508 | [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ), |
| 5509 | [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ), |
| 5510 | [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE), |
| 5511 | [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE), |
| 5512 | [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ), |
| 5513 | [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ), |
| 5514 | [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE), |
| 5515 | [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 5516 | [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN), |
| 5517 | [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL), |
| 5518 | [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD), |
| 5519 | [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE), |
| 5520 | [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI), |
| 5521 | [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI), |
| 5522 | [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT), |
| 5523 | [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 5524 | [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP), |
| 5525 | [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR), |
| 5526 | [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 5527 | [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG), |
| 5528 | [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD), |
| 5529 | [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD), |
| 5530 | [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR), |
| 5531 | [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC), |
| 5532 | [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR), |
| 5533 | [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC), |
| 5534 | [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID), |
| 5535 | [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM), |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 5536 | [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE), |
| 5537 | [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF), |
| 5538 | [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF), |
| 5539 | [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT), |
| 5540 | [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET), |
| 5541 | [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP), |
| 5542 | [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT), |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 5543 | [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO), |
| 5544 | [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO), |
| 5545 | [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO), |
| 5546 | [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO), |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5547 | }; |
| 5548 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 5549 | #undef PRE_EX |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5550 | #undef POST_EX |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 5551 | #undef POST_MEM |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5552 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 5553 | static int svm_check_intercept(struct kvm_vcpu *vcpu, |
| 5554 | struct x86_instruction_info *info, |
| 5555 | enum x86_intercept_stage stage) |
| 5556 | { |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5557 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5558 | int vmexit, ret = X86EMUL_CONTINUE; |
| 5559 | struct __x86_intercept icpt_info; |
| 5560 | struct vmcb *vmcb = svm->vmcb; |
| 5561 | |
| 5562 | if (info->intercept >= ARRAY_SIZE(x86_intercept_map)) |
| 5563 | goto out; |
| 5564 | |
| 5565 | icpt_info = x86_intercept_map[info->intercept]; |
| 5566 | |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 5567 | if (stage != icpt_info.stage) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5568 | goto out; |
| 5569 | |
| 5570 | switch (icpt_info.exit_code) { |
| 5571 | case SVM_EXIT_READ_CR0: |
| 5572 | if (info->intercept == x86_intercept_cr_read) |
| 5573 | icpt_info.exit_code += info->modrm_reg; |
| 5574 | break; |
| 5575 | case SVM_EXIT_WRITE_CR0: { |
| 5576 | unsigned long cr0, val; |
| 5577 | u64 intercept; |
| 5578 | |
| 5579 | if (info->intercept == x86_intercept_cr_write) |
| 5580 | icpt_info.exit_code += info->modrm_reg; |
| 5581 | |
Jan Kiszka | 62baf44 | 2014-06-29 21:55:53 +0200 | [diff] [blame] | 5582 | if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 || |
| 5583 | info->intercept == x86_intercept_clts) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5584 | break; |
| 5585 | |
| 5586 | intercept = svm->nested.intercept; |
| 5587 | |
| 5588 | if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))) |
| 5589 | break; |
| 5590 | |
| 5591 | cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK; |
| 5592 | val = info->src_val & ~SVM_CR0_SELECTIVE_MASK; |
| 5593 | |
| 5594 | if (info->intercept == x86_intercept_lmsw) { |
| 5595 | cr0 &= 0xfUL; |
| 5596 | val &= 0xfUL; |
| 5597 | /* lmsw can't clear PE - catch this here */ |
| 5598 | if (cr0 & X86_CR0_PE) |
| 5599 | val |= X86_CR0_PE; |
| 5600 | } |
| 5601 | |
| 5602 | if (cr0 ^ val) |
| 5603 | icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE; |
| 5604 | |
| 5605 | break; |
| 5606 | } |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 5607 | case SVM_EXIT_READ_DR0: |
| 5608 | case SVM_EXIT_WRITE_DR0: |
| 5609 | icpt_info.exit_code += info->modrm_reg; |
| 5610 | break; |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 5611 | case SVM_EXIT_MSR: |
| 5612 | if (info->intercept == x86_intercept_wrmsr) |
| 5613 | vmcb->control.exit_info_1 = 1; |
| 5614 | else |
| 5615 | vmcb->control.exit_info_1 = 0; |
| 5616 | break; |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 5617 | case SVM_EXIT_PAUSE: |
| 5618 | /* |
| 5619 | * We get this for NOP only, but pause |
| 5620 | * is rep not, check this here |
| 5621 | */ |
| 5622 | if (info->rep_prefix != REPE_PREFIX) |
| 5623 | goto out; |
Jan H. Schönherr | 49a8afc | 2017-09-05 23:58:44 +0200 | [diff] [blame] | 5624 | break; |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 5625 | case SVM_EXIT_IOIO: { |
| 5626 | u64 exit_info; |
| 5627 | u32 bytes; |
| 5628 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 5629 | if (info->intercept == x86_intercept_in || |
| 5630 | info->intercept == x86_intercept_ins) { |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 5631 | exit_info = ((info->src_val & 0xffff) << 16) | |
| 5632 | SVM_IOIO_TYPE_MASK; |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 5633 | bytes = info->dst_bytes; |
Jan Kiszka | 6493f15 | 2014-06-30 11:07:05 +0200 | [diff] [blame] | 5634 | } else { |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 5635 | exit_info = (info->dst_val & 0xffff) << 16; |
Jan Kiszka | 6493f15 | 2014-06-30 11:07:05 +0200 | [diff] [blame] | 5636 | bytes = info->src_bytes; |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 5637 | } |
| 5638 | |
| 5639 | if (info->intercept == x86_intercept_outs || |
| 5640 | info->intercept == x86_intercept_ins) |
| 5641 | exit_info |= SVM_IOIO_STR_MASK; |
| 5642 | |
| 5643 | if (info->rep_prefix) |
| 5644 | exit_info |= SVM_IOIO_REP_MASK; |
| 5645 | |
| 5646 | bytes = min(bytes, 4u); |
| 5647 | |
| 5648 | exit_info |= bytes << SVM_IOIO_SIZE_SHIFT; |
| 5649 | |
| 5650 | exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1); |
| 5651 | |
| 5652 | vmcb->control.exit_info_1 = exit_info; |
| 5653 | vmcb->control.exit_info_2 = info->next_rip; |
| 5654 | |
| 5655 | break; |
| 5656 | } |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5657 | default: |
| 5658 | break; |
| 5659 | } |
| 5660 | |
Bandan Das | f104765 | 2015-06-11 02:05:33 -0400 | [diff] [blame] | 5661 | /* TODO: Advertise NRIPS to guest hypervisor unconditionally */ |
| 5662 | if (static_cpu_has(X86_FEATURE_NRIPS)) |
| 5663 | vmcb->control.next_rip = info->next_rip; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 5664 | vmcb->control.exit_code = icpt_info.exit_code; |
| 5665 | vmexit = nested_svm_exit_handled(svm); |
| 5666 | |
| 5667 | ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED |
| 5668 | : X86EMUL_CONTINUE; |
| 5669 | |
| 5670 | out: |
| 5671 | return ret; |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 5672 | } |
| 5673 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5674 | static void svm_handle_external_intr(struct kvm_vcpu *vcpu) |
| 5675 | { |
| 5676 | local_irq_enable(); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 5677 | /* |
| 5678 | * We must have an instruction with interrupts enabled, so |
| 5679 | * the timer interrupt isn't delayed by the interrupt shadow. |
| 5680 | */ |
| 5681 | asm("nop"); |
| 5682 | local_irq_disable(); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5683 | } |
| 5684 | |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 5685 | static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu) |
| 5686 | { |
| 5687 | } |
| 5688 | |
Suravee Suthikulpanit | be8ca17 | 2016-05-04 14:09:49 -0500 | [diff] [blame] | 5689 | static inline void avic_post_state_restore(struct kvm_vcpu *vcpu) |
| 5690 | { |
| 5691 | if (avic_handle_apic_id_update(vcpu) != 0) |
| 5692 | return; |
| 5693 | if (avic_handle_dfr_update(vcpu) != 0) |
| 5694 | return; |
| 5695 | avic_handle_ldr_update(vcpu); |
| 5696 | } |
| 5697 | |
Borislav Petkov | 74f1690 | 2017-03-26 23:51:24 +0200 | [diff] [blame] | 5698 | static void svm_setup_mce(struct kvm_vcpu *vcpu) |
| 5699 | { |
| 5700 | /* [63:9] are reserved. */ |
| 5701 | vcpu->arch.mcg_cap &= 0x1ff; |
| 5702 | } |
| 5703 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 5704 | static int svm_smi_allowed(struct kvm_vcpu *vcpu) |
| 5705 | { |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 5706 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5707 | |
| 5708 | /* Per APM Vol.2 15.22.2 "Response to SMI" */ |
| 5709 | if (!gif_set(svm)) |
| 5710 | return 0; |
| 5711 | |
| 5712 | if (is_guest_mode(&svm->vcpu) && |
| 5713 | svm->nested.intercept & (1ULL << INTERCEPT_SMI)) { |
| 5714 | /* TODO: Might need to set exit_info_1 and exit_info_2 here */ |
| 5715 | svm->vmcb->control.exit_code = SVM_EXIT_SMI; |
| 5716 | svm->nested.exit_required = true; |
| 5717 | return 0; |
| 5718 | } |
| 5719 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 5720 | return 1; |
| 5721 | } |
| 5722 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 5723 | static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 5724 | { |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 5725 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5726 | int ret; |
| 5727 | |
| 5728 | if (is_guest_mode(vcpu)) { |
| 5729 | /* FED8h - SVM Guest */ |
| 5730 | put_smstate(u64, smstate, 0x7ed8, 1); |
| 5731 | /* FEE0h - SVM Guest VMCB Physical Address */ |
| 5732 | put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb); |
| 5733 | |
| 5734 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 5735 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 5736 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; |
| 5737 | |
| 5738 | ret = nested_svm_vmexit(svm); |
| 5739 | if (ret) |
| 5740 | return ret; |
| 5741 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 5742 | return 0; |
| 5743 | } |
| 5744 | |
| 5745 | static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) |
| 5746 | { |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 5747 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5748 | struct vmcb *nested_vmcb; |
| 5749 | struct page *page; |
| 5750 | struct { |
| 5751 | u64 guest; |
| 5752 | u64 vmcb; |
| 5753 | } svm_state_save; |
| 5754 | int ret; |
| 5755 | |
| 5756 | ret = kvm_vcpu_read_guest(vcpu, smbase + 0xfed8, &svm_state_save, |
| 5757 | sizeof(svm_state_save)); |
| 5758 | if (ret) |
| 5759 | return ret; |
| 5760 | |
| 5761 | if (svm_state_save.guest) { |
| 5762 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
| 5763 | nested_vmcb = nested_svm_map(svm, svm_state_save.vmcb, &page); |
| 5764 | if (nested_vmcb) |
| 5765 | enter_svm_guest_mode(svm, svm_state_save.vmcb, nested_vmcb, page); |
| 5766 | else |
| 5767 | ret = 1; |
| 5768 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 5769 | } |
| 5770 | return ret; |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 5771 | } |
| 5772 | |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 5773 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
| 5774 | { |
| 5775 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5776 | |
| 5777 | if (!gif_set(svm)) { |
| 5778 | if (vgif_enabled(svm)) |
| 5779 | set_intercept(svm, INTERCEPT_STGI); |
| 5780 | /* STGI will cause a vm exit */ |
| 5781 | return 1; |
| 5782 | } |
| 5783 | return 0; |
| 5784 | } |
| 5785 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 5786 | static int sev_asid_new(void) |
| 5787 | { |
| 5788 | int pos; |
| 5789 | |
| 5790 | /* |
| 5791 | * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid. |
| 5792 | */ |
| 5793 | pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1); |
| 5794 | if (pos >= max_sev_asid) |
| 5795 | return -EBUSY; |
| 5796 | |
| 5797 | set_bit(pos, sev_asid_bitmap); |
| 5798 | return pos + 1; |
| 5799 | } |
| 5800 | |
| 5801 | static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 5802 | { |
| 5803 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 5804 | int asid, ret; |
| 5805 | |
| 5806 | ret = -EBUSY; |
| 5807 | asid = sev_asid_new(); |
| 5808 | if (asid < 0) |
| 5809 | return ret; |
| 5810 | |
| 5811 | ret = sev_platform_init(&argp->error); |
| 5812 | if (ret) |
| 5813 | goto e_free; |
| 5814 | |
| 5815 | sev->active = true; |
| 5816 | sev->asid = asid; |
| 5817 | |
| 5818 | return 0; |
| 5819 | |
| 5820 | e_free: |
| 5821 | __sev_asid_free(asid); |
| 5822 | return ret; |
| 5823 | } |
| 5824 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 5825 | static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error) |
| 5826 | { |
| 5827 | struct sev_data_activate *data; |
| 5828 | int asid = sev_get_asid(kvm); |
| 5829 | int ret; |
| 5830 | |
| 5831 | wbinvd_on_all_cpus(); |
| 5832 | |
| 5833 | ret = sev_guest_df_flush(error); |
| 5834 | if (ret) |
| 5835 | return ret; |
| 5836 | |
| 5837 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 5838 | if (!data) |
| 5839 | return -ENOMEM; |
| 5840 | |
| 5841 | /* activate ASID on the given handle */ |
| 5842 | data->handle = handle; |
| 5843 | data->asid = asid; |
| 5844 | ret = sev_guest_activate(data, error); |
| 5845 | kfree(data); |
| 5846 | |
| 5847 | return ret; |
| 5848 | } |
| 5849 | |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 5850 | static int __sev_issue_cmd(int fd, int id, void *data, int *error) |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 5851 | { |
| 5852 | struct fd f; |
| 5853 | int ret; |
| 5854 | |
| 5855 | f = fdget(fd); |
| 5856 | if (!f.file) |
| 5857 | return -EBADF; |
| 5858 | |
| 5859 | ret = sev_issue_cmd_external_user(f.file, id, data, error); |
| 5860 | |
| 5861 | fdput(f); |
| 5862 | return ret; |
| 5863 | } |
| 5864 | |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 5865 | static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error) |
| 5866 | { |
| 5867 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 5868 | |
| 5869 | return __sev_issue_cmd(sev->fd, id, data, error); |
| 5870 | } |
| 5871 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 5872 | static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 5873 | { |
| 5874 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 5875 | struct sev_data_launch_start *start; |
| 5876 | struct kvm_sev_launch_start params; |
| 5877 | void *dh_blob, *session_blob; |
| 5878 | int *error = &argp->error; |
| 5879 | int ret; |
| 5880 | |
| 5881 | if (!sev_guest(kvm)) |
| 5882 | return -ENOTTY; |
| 5883 | |
| 5884 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 5885 | return -EFAULT; |
| 5886 | |
| 5887 | start = kzalloc(sizeof(*start), GFP_KERNEL); |
| 5888 | if (!start) |
| 5889 | return -ENOMEM; |
| 5890 | |
| 5891 | dh_blob = NULL; |
| 5892 | if (params.dh_uaddr) { |
| 5893 | dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len); |
| 5894 | if (IS_ERR(dh_blob)) { |
| 5895 | ret = PTR_ERR(dh_blob); |
| 5896 | goto e_free; |
| 5897 | } |
| 5898 | |
| 5899 | start->dh_cert_address = __sme_set(__pa(dh_blob)); |
| 5900 | start->dh_cert_len = params.dh_len; |
| 5901 | } |
| 5902 | |
| 5903 | session_blob = NULL; |
| 5904 | if (params.session_uaddr) { |
| 5905 | session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len); |
| 5906 | if (IS_ERR(session_blob)) { |
| 5907 | ret = PTR_ERR(session_blob); |
| 5908 | goto e_free_dh; |
| 5909 | } |
| 5910 | |
| 5911 | start->session_address = __sme_set(__pa(session_blob)); |
| 5912 | start->session_len = params.session_len; |
| 5913 | } |
| 5914 | |
| 5915 | start->handle = params.handle; |
| 5916 | start->policy = params.policy; |
| 5917 | |
| 5918 | /* create memory encryption context */ |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 5919 | ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error); |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 5920 | if (ret) |
| 5921 | goto e_free_session; |
| 5922 | |
| 5923 | /* Bind ASID to this guest */ |
| 5924 | ret = sev_bind_asid(kvm, start->handle, error); |
| 5925 | if (ret) |
| 5926 | goto e_free_session; |
| 5927 | |
| 5928 | /* return handle to userspace */ |
| 5929 | params.handle = start->handle; |
| 5930 | if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) { |
| 5931 | sev_unbind_asid(kvm, start->handle); |
| 5932 | ret = -EFAULT; |
| 5933 | goto e_free_session; |
| 5934 | } |
| 5935 | |
| 5936 | sev->handle = start->handle; |
| 5937 | sev->fd = argp->sev_fd; |
| 5938 | |
| 5939 | e_free_session: |
| 5940 | kfree(session_blob); |
| 5941 | e_free_dh: |
| 5942 | kfree(dh_blob); |
| 5943 | e_free: |
| 5944 | kfree(start); |
| 5945 | return ret; |
| 5946 | } |
| 5947 | |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 5948 | static int get_num_contig_pages(int idx, struct page **inpages, |
| 5949 | unsigned long npages) |
| 5950 | { |
| 5951 | unsigned long paddr, next_paddr; |
| 5952 | int i = idx + 1, pages = 1; |
| 5953 | |
| 5954 | /* find the number of contiguous pages starting from idx */ |
| 5955 | paddr = __sme_page_pa(inpages[idx]); |
| 5956 | while (i < npages) { |
| 5957 | next_paddr = __sme_page_pa(inpages[i++]); |
| 5958 | if ((paddr + PAGE_SIZE) == next_paddr) { |
| 5959 | pages++; |
| 5960 | paddr = next_paddr; |
| 5961 | continue; |
| 5962 | } |
| 5963 | break; |
| 5964 | } |
| 5965 | |
| 5966 | return pages; |
| 5967 | } |
| 5968 | |
| 5969 | static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 5970 | { |
| 5971 | unsigned long vaddr, vaddr_end, next_vaddr, npages, size; |
| 5972 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 5973 | struct kvm_sev_launch_update_data params; |
| 5974 | struct sev_data_launch_update_data *data; |
| 5975 | struct page **inpages; |
| 5976 | int i, ret, pages; |
| 5977 | |
| 5978 | if (!sev_guest(kvm)) |
| 5979 | return -ENOTTY; |
| 5980 | |
| 5981 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 5982 | return -EFAULT; |
| 5983 | |
| 5984 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 5985 | if (!data) |
| 5986 | return -ENOMEM; |
| 5987 | |
| 5988 | vaddr = params.uaddr; |
| 5989 | size = params.len; |
| 5990 | vaddr_end = vaddr + size; |
| 5991 | |
| 5992 | /* Lock the user memory. */ |
| 5993 | inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1); |
| 5994 | if (!inpages) { |
| 5995 | ret = -ENOMEM; |
| 5996 | goto e_free; |
| 5997 | } |
| 5998 | |
| 5999 | /* |
| 6000 | * The LAUNCH_UPDATE command will perform in-place encryption of the |
| 6001 | * memory content (i.e it will write the same memory region with C=1). |
| 6002 | * It's possible that the cache may contain the data with C=0, i.e., |
| 6003 | * unencrypted so invalidate it first. |
| 6004 | */ |
| 6005 | sev_clflush_pages(inpages, npages); |
| 6006 | |
| 6007 | for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) { |
| 6008 | int offset, len; |
| 6009 | |
| 6010 | /* |
| 6011 | * If the user buffer is not page-aligned, calculate the offset |
| 6012 | * within the page. |
| 6013 | */ |
| 6014 | offset = vaddr & (PAGE_SIZE - 1); |
| 6015 | |
| 6016 | /* Calculate the number of pages that can be encrypted in one go. */ |
| 6017 | pages = get_num_contig_pages(i, inpages, npages); |
| 6018 | |
| 6019 | len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size); |
| 6020 | |
| 6021 | data->handle = sev->handle; |
| 6022 | data->len = len; |
| 6023 | data->address = __sme_page_pa(inpages[i]) + offset; |
| 6024 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error); |
| 6025 | if (ret) |
| 6026 | goto e_unpin; |
| 6027 | |
| 6028 | size -= len; |
| 6029 | next_vaddr = vaddr + len; |
| 6030 | } |
| 6031 | |
| 6032 | e_unpin: |
| 6033 | /* content of memory is updated, mark pages dirty */ |
| 6034 | for (i = 0; i < npages; i++) { |
| 6035 | set_page_dirty_lock(inpages[i]); |
| 6036 | mark_page_accessed(inpages[i]); |
| 6037 | } |
| 6038 | /* unlock the user pages */ |
| 6039 | sev_unpin_memory(kvm, inpages, npages); |
| 6040 | e_free: |
| 6041 | kfree(data); |
| 6042 | return ret; |
| 6043 | } |
| 6044 | |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6045 | static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6046 | { |
| 6047 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6048 | struct sev_data_launch_measure *data; |
| 6049 | struct kvm_sev_launch_measure params; |
| 6050 | void *blob = NULL; |
| 6051 | int ret; |
| 6052 | |
| 6053 | if (!sev_guest(kvm)) |
| 6054 | return -ENOTTY; |
| 6055 | |
| 6056 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 6057 | return -EFAULT; |
| 6058 | |
| 6059 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 6060 | if (!data) |
| 6061 | return -ENOMEM; |
| 6062 | |
| 6063 | /* User wants to query the blob length */ |
| 6064 | if (!params.len) |
| 6065 | goto cmd; |
| 6066 | |
| 6067 | if (params.uaddr) { |
| 6068 | if (params.len > SEV_FW_BLOB_MAX_SIZE) { |
| 6069 | ret = -EINVAL; |
| 6070 | goto e_free; |
| 6071 | } |
| 6072 | |
| 6073 | if (!access_ok(VERIFY_WRITE, params.uaddr, params.len)) { |
| 6074 | ret = -EFAULT; |
| 6075 | goto e_free; |
| 6076 | } |
| 6077 | |
| 6078 | ret = -ENOMEM; |
| 6079 | blob = kmalloc(params.len, GFP_KERNEL); |
| 6080 | if (!blob) |
| 6081 | goto e_free; |
| 6082 | |
| 6083 | data->address = __psp_pa(blob); |
| 6084 | data->len = params.len; |
| 6085 | } |
| 6086 | |
| 6087 | cmd: |
| 6088 | data->handle = sev->handle; |
| 6089 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error); |
| 6090 | |
| 6091 | /* |
| 6092 | * If we query the session length, FW responded with expected data. |
| 6093 | */ |
| 6094 | if (!params.len) |
| 6095 | goto done; |
| 6096 | |
| 6097 | if (ret) |
| 6098 | goto e_free_blob; |
| 6099 | |
| 6100 | if (blob) { |
| 6101 | if (copy_to_user((void __user *)(uintptr_t)params.uaddr, blob, params.len)) |
| 6102 | ret = -EFAULT; |
| 6103 | } |
| 6104 | |
| 6105 | done: |
| 6106 | params.len = data->len; |
| 6107 | if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) |
| 6108 | ret = -EFAULT; |
| 6109 | e_free_blob: |
| 6110 | kfree(blob); |
| 6111 | e_free: |
| 6112 | kfree(data); |
| 6113 | return ret; |
| 6114 | } |
| 6115 | |
Brijesh Singh | 5bdb0e2 | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6116 | static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6117 | { |
| 6118 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6119 | struct sev_data_launch_finish *data; |
| 6120 | int ret; |
| 6121 | |
| 6122 | if (!sev_guest(kvm)) |
| 6123 | return -ENOTTY; |
| 6124 | |
| 6125 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 6126 | if (!data) |
| 6127 | return -ENOMEM; |
| 6128 | |
| 6129 | data->handle = sev->handle; |
| 6130 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error); |
| 6131 | |
| 6132 | kfree(data); |
| 6133 | return ret; |
| 6134 | } |
| 6135 | |
Brijesh Singh | 255d9e7 | 2017-12-04 10:57:37 -0600 | [diff] [blame^] | 6136 | static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6137 | { |
| 6138 | struct kvm_sev_info *sev = &kvm->arch.sev_info; |
| 6139 | struct kvm_sev_guest_status params; |
| 6140 | struct sev_data_guest_status *data; |
| 6141 | int ret; |
| 6142 | |
| 6143 | if (!sev_guest(kvm)) |
| 6144 | return -ENOTTY; |
| 6145 | |
| 6146 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 6147 | if (!data) |
| 6148 | return -ENOMEM; |
| 6149 | |
| 6150 | data->handle = sev->handle; |
| 6151 | ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error); |
| 6152 | if (ret) |
| 6153 | goto e_free; |
| 6154 | |
| 6155 | params.policy = data->policy; |
| 6156 | params.state = data->state; |
| 6157 | params.handle = data->handle; |
| 6158 | |
| 6159 | if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) |
| 6160 | ret = -EFAULT; |
| 6161 | e_free: |
| 6162 | kfree(data); |
| 6163 | return ret; |
| 6164 | } |
| 6165 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6166 | static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) |
| 6167 | { |
| 6168 | struct kvm_sev_cmd sev_cmd; |
| 6169 | int r; |
| 6170 | |
| 6171 | if (!svm_sev_enabled()) |
| 6172 | return -ENOTTY; |
| 6173 | |
| 6174 | if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd))) |
| 6175 | return -EFAULT; |
| 6176 | |
| 6177 | mutex_lock(&kvm->lock); |
| 6178 | |
| 6179 | switch (sev_cmd.id) { |
| 6180 | case KVM_SEV_INIT: |
| 6181 | r = sev_guest_init(kvm, &sev_cmd); |
| 6182 | break; |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6183 | case KVM_SEV_LAUNCH_START: |
| 6184 | r = sev_launch_start(kvm, &sev_cmd); |
| 6185 | break; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6186 | case KVM_SEV_LAUNCH_UPDATE_DATA: |
| 6187 | r = sev_launch_update_data(kvm, &sev_cmd); |
| 6188 | break; |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6189 | case KVM_SEV_LAUNCH_MEASURE: |
| 6190 | r = sev_launch_measure(kvm, &sev_cmd); |
| 6191 | break; |
Brijesh Singh | 5bdb0e2 | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6192 | case KVM_SEV_LAUNCH_FINISH: |
| 6193 | r = sev_launch_finish(kvm, &sev_cmd); |
| 6194 | break; |
Brijesh Singh | 255d9e7 | 2017-12-04 10:57:37 -0600 | [diff] [blame^] | 6195 | case KVM_SEV_GUEST_STATUS: |
| 6196 | r = sev_guest_status(kvm, &sev_cmd); |
| 6197 | break; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6198 | default: |
| 6199 | r = -EINVAL; |
| 6200 | goto out; |
| 6201 | } |
| 6202 | |
| 6203 | if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd))) |
| 6204 | r = -EFAULT; |
| 6205 | |
| 6206 | out: |
| 6207 | mutex_unlock(&kvm->lock); |
| 6208 | return r; |
| 6209 | } |
| 6210 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 6211 | static struct kvm_x86_ops svm_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6212 | .cpu_has_kvm_support = has_svm, |
| 6213 | .disabled_by_bios = is_disabled, |
| 6214 | .hardware_setup = svm_hardware_setup, |
| 6215 | .hardware_unsetup = svm_hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6216 | .check_processor_compatibility = svm_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6217 | .hardware_enable = svm_hardware_enable, |
| 6218 | .hardware_disable = svm_hardware_disable, |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 6219 | .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 6220 | .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6221 | |
| 6222 | .vcpu_create = svm_create_vcpu, |
| 6223 | .vcpu_free = svm_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 6224 | .vcpu_reset = svm_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6225 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 6226 | .vm_init = avic_vm_init, |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6227 | .vm_destroy = svm_vm_destroy, |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 6228 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 6229 | .prepare_guest_switch = svm_prepare_guest_switch, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6230 | .vcpu_load = svm_vcpu_load, |
| 6231 | .vcpu_put = svm_vcpu_put, |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 6232 | .vcpu_blocking = svm_vcpu_blocking, |
| 6233 | .vcpu_unblocking = svm_vcpu_unblocking, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6234 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 6235 | .update_bp_intercept = update_bp_intercept, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6236 | .get_msr = svm_get_msr, |
| 6237 | .set_msr = svm_set_msr, |
| 6238 | .get_segment_base = svm_get_segment_base, |
| 6239 | .get_segment = svm_get_segment, |
| 6240 | .set_segment = svm_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 6241 | .get_cpl = svm_get_cpl, |
Rusty Russell | 1747fb7 | 2007-09-06 01:21:32 +1000 | [diff] [blame] | 6242 | .get_cs_db_l_bits = kvm_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 6243 | .decache_cr0_guest_bits = svm_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6244 | .decache_cr3 = svm_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 6245 | .decache_cr4_guest_bits = svm_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6246 | .set_cr0 = svm_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6247 | .set_cr3 = svm_set_cr3, |
| 6248 | .set_cr4 = svm_set_cr4, |
| 6249 | .set_efer = svm_set_efer, |
| 6250 | .get_idt = svm_get_idt, |
| 6251 | .set_idt = svm_set_idt, |
| 6252 | .get_gdt = svm_get_gdt, |
| 6253 | .set_gdt = svm_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 6254 | .get_dr6 = svm_get_dr6, |
| 6255 | .set_dr6 = svm_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6256 | .set_dr7 = svm_set_dr7, |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6257 | .sync_dirty_debug_regs = svm_sync_dirty_debug_regs, |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 6258 | .cache_reg = svm_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6259 | .get_rflags = svm_get_rflags, |
| 6260 | .set_rflags = svm_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 6261 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6262 | .tlb_flush = svm_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6263 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6264 | .run = svm_vcpu_run, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 6265 | .handle_exit = handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6266 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 6267 | .set_interrupt_shadow = svm_set_interrupt_shadow, |
| 6268 | .get_interrupt_shadow = svm_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6269 | .patch_hypercall = svm_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 6270 | .set_irq = svm_set_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6271 | .set_nmi = svm_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 6272 | .queue_exception = svm_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 6273 | .cancel_injection = svm_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6274 | .interrupt_allowed = svm_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6275 | .nmi_allowed = svm_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6276 | .get_nmi_mask = svm_get_nmi_mask, |
| 6277 | .set_nmi_mask = svm_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6278 | .enable_nmi_window = enable_nmi_window, |
| 6279 | .enable_irq_window = enable_irq_window, |
| 6280 | .update_cr8_intercept = update_cr8_intercept, |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 6281 | .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6282 | .get_enable_apicv = svm_get_enable_apicv, |
| 6283 | .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6284 | .load_eoi_exitmap = svm_load_eoi_exitmap, |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 6285 | .hwapic_irr_update = svm_hwapic_irr_update, |
| 6286 | .hwapic_isr_update = svm_hwapic_isr_update, |
Suravee Suthikulpanit | be8ca17 | 2016-05-04 14:09:49 -0500 | [diff] [blame] | 6287 | .apicv_post_state_restore = avic_post_state_restore, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6288 | |
| 6289 | .set_tss_addr = svm_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 6290 | .get_tdp_level = get_npt_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6291 | .get_mt_mask = svm_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6292 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 6293 | .get_exit_info = svm_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 6294 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 6295 | .get_lpage_level = svm_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 6296 | |
| 6297 | .cpuid_update = svm_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 6298 | |
| 6299 | .rdtscp_supported = svm_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 6300 | .invpcid_supported = svm_invpcid_supported, |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 6301 | .mpx_supported = svm_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 6302 | .xsaves_supported = svm_xsaves_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 6303 | |
| 6304 | .set_supported_cpuid = svm_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 6305 | |
| 6306 | .has_wbinvd_exit = svm_has_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 6307 | |
| 6308 | .write_tsc_offset = svm_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 6309 | |
| 6310 | .set_tdp_cr3 = set_tdp_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 6311 | |
| 6312 | .check_intercept = svm_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6313 | .handle_external_intr = svm_handle_external_intr, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 6314 | |
| 6315 | .sched_in = svm_sched_in, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 6316 | |
| 6317 | .pmu_ops = &amd_pmu_ops, |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 6318 | .deliver_posted_interrupt = svm_deliver_avic_intr, |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 6319 | .update_pi_irte = svm_update_pi_irte, |
Borislav Petkov | 74f1690 | 2017-03-26 23:51:24 +0200 | [diff] [blame] | 6320 | .setup_mce = svm_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 6321 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 6322 | .smi_allowed = svm_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 6323 | .pre_enter_smm = svm_pre_enter_smm, |
| 6324 | .pre_leave_smm = svm_pre_leave_smm, |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 6325 | .enable_smi_window = enable_smi_window, |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6326 | |
| 6327 | .mem_enc_op = svm_mem_enc_op, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6328 | }; |
| 6329 | |
| 6330 | static int __init svm_init(void) |
| 6331 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 6332 | return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm), |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 6333 | __alignof__(struct vcpu_svm), THIS_MODULE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6334 | } |
| 6335 | |
| 6336 | static void __exit svm_exit(void) |
| 6337 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 6338 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6339 | } |
| 6340 | |
| 6341 | module_init(svm_init) |
| 6342 | module_exit(svm_exit) |