Thomas Gleixner | 20c8ccb | 2019-06-04 10:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Kernel-based Virtual Machine driver for Linux |
| 4 | * |
| 5 | * AMD SVM support |
| 6 | * |
| 7 | * Copyright (C) 2006 Qumranet, Inc. |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 8 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9 | * |
| 10 | * Authors: |
| 11 | * Yaniv Kamay <yaniv@qumranet.com> |
| 12 | * Avi Kivity <avi@qumranet.com> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13 | */ |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 14 | |
| 15 | #define pr_fmt(fmt) "SVM: " fmt |
| 16 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 17 | #include <linux/kvm_host.h> |
| 18 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 19 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 20 | #include "mmu.h" |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 21 | #include "kvm_cache_regs.h" |
Gleb Natapov | fe4c7b1 | 2009-03-23 11:23:18 +0200 | [diff] [blame] | 22 | #include "x86.h" |
Julian Stecklina | 66f7b72 | 2012-12-05 15:26:19 +0100 | [diff] [blame] | 23 | #include "cpuid.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 24 | #include "pmu.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 25 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 26 | #include <linux/module.h> |
Josh Triplett | ae75954 | 2012-03-28 11:32:28 -0700 | [diff] [blame] | 27 | #include <linux/mod_devicetable.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 28 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 29 | #include <linux/vmalloc.h> |
| 30 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 31 | #include <linux/sched.h> |
Steven Rostedt (Red Hat) | af658dc | 2015-04-29 14:36:05 -0400 | [diff] [blame] | 32 | #include <linux/trace_events.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 34 | #include <linux/amd-iommu.h> |
| 35 | #include <linux/hashtable.h> |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 36 | #include <linux/frame.h> |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 37 | #include <linux/psp-sev.h> |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 38 | #include <linux/file.h> |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 39 | #include <linux/pagemap.h> |
| 40 | #include <linux/swap.h> |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 41 | #include <linux/rwsem.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 42 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 43 | #include <asm/apic.h> |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 44 | #include <asm/perf_event.h> |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 45 | #include <asm/tlbflush.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 46 | #include <asm/desc.h> |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 47 | #include <asm/debugreg.h> |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 48 | #include <asm/kvm_para.h> |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 49 | #include <asm/irq_remapping.h> |
Thomas Gleixner | 28a2775 | 2018-04-29 15:01:37 +0200 | [diff] [blame] | 50 | #include <asm/spec-ctrl.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 51 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 52 | #include <asm/virtext.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 53 | #include "trace.h" |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 54 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 55 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
| 56 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 57 | MODULE_AUTHOR("Qumranet"); |
| 58 | MODULE_LICENSE("GPL"); |
| 59 | |
Josh Triplett | ae75954 | 2012-03-28 11:32:28 -0700 | [diff] [blame] | 60 | static const struct x86_cpu_id svm_cpu_id[] = { |
| 61 | X86_FEATURE_MATCH(X86_FEATURE_SVM), |
| 62 | {} |
| 63 | }; |
| 64 | MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id); |
| 65 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 66 | #define IOPM_ALLOC_ORDER 2 |
| 67 | #define MSRPM_ALLOC_ORDER 1 |
| 68 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 69 | #define SEG_TYPE_LDT 2 |
| 70 | #define SEG_TYPE_BUSY_TSS16 3 |
| 71 | |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 72 | #define SVM_FEATURE_LBRV (1 << 1) |
| 73 | #define SVM_FEATURE_SVML (1 << 2) |
Andre Przywara | ddce97a | 2010-12-21 11:12:03 +0100 | [diff] [blame] | 74 | #define SVM_FEATURE_TSC_RATE (1 << 4) |
| 75 | #define SVM_FEATURE_VMCB_CLEAN (1 << 5) |
| 76 | #define SVM_FEATURE_FLUSH_ASID (1 << 6) |
| 77 | #define SVM_FEATURE_DECODE_ASSIST (1 << 7) |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 78 | #define SVM_FEATURE_PAUSE_FILTER (1 << 10) |
Joerg Roedel | 80b7706 | 2007-03-30 17:02:14 +0300 | [diff] [blame] | 79 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 80 | #define SVM_AVIC_DOORBELL 0xc001011b |
| 81 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 82 | #define NESTED_EXIT_HOST 0 /* Exit handled on host level */ |
| 83 | #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */ |
| 84 | #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */ |
| 85 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 86 | #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) |
| 87 | |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 88 | #define TSC_RATIO_RSVD 0xffffff0000000000ULL |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 89 | #define TSC_RATIO_MIN 0x0000000000000001ULL |
| 90 | #define TSC_RATIO_MAX 0x000000ffffffffffULL |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 91 | |
Dan Carpenter | 5446a97 | 2016-05-23 13:20:10 +0300 | [diff] [blame] | 92 | #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * 0xff is broadcast, so the max index allowed for physical APIC ID |
| 96 | * table is 0xfe. APIC IDs above 0xff are reserved. |
| 97 | */ |
| 98 | #define AVIC_MAX_PHYSICAL_ID_COUNT 255 |
| 99 | |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 100 | #define AVIC_UNACCEL_ACCESS_WRITE_MASK 1 |
| 101 | #define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0 |
| 102 | #define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF |
| 103 | |
Suravee Suthikulpanit | 5ea11f2 | 2016-08-23 13:52:41 -0500 | [diff] [blame] | 104 | /* AVIC GATAG is encoded using VM and VCPU IDs */ |
| 105 | #define AVIC_VCPU_ID_BITS 8 |
| 106 | #define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1) |
| 107 | |
| 108 | #define AVIC_VM_ID_BITS 24 |
| 109 | #define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS) |
| 110 | #define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1) |
| 111 | |
| 112 | #define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \ |
| 113 | (y & AVIC_VCPU_ID_MASK)) |
| 114 | #define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK) |
| 115 | #define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK) |
| 116 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 117 | static bool erratum_383_found __read_mostly; |
| 118 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 119 | static const u32 host_save_user_msrs[] = { |
| 120 | #ifdef CONFIG_X86_64 |
| 121 | MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE, |
| 122 | MSR_FS_BASE, |
| 123 | #endif |
| 124 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 125 | MSR_TSC_AUX, |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) |
| 129 | |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 130 | struct kvm_sev_info { |
| 131 | bool active; /* SEV enabled guest */ |
| 132 | unsigned int asid; /* ASID used for this guest */ |
| 133 | unsigned int handle; /* SEV firmware handle */ |
| 134 | int fd; /* SEV device fd */ |
| 135 | unsigned long pages_locked; /* Number of pages locked */ |
| 136 | struct list_head regions_list; /* List of registered regions */ |
| 137 | }; |
| 138 | |
| 139 | struct kvm_svm { |
| 140 | struct kvm kvm; |
| 141 | |
| 142 | /* Struct members for AVIC */ |
| 143 | u32 avic_vm_id; |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 144 | struct page *avic_logical_id_table_page; |
| 145 | struct page *avic_physical_id_table_page; |
| 146 | struct hlist_node hnode; |
| 147 | |
| 148 | struct kvm_sev_info sev_info; |
| 149 | }; |
| 150 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 151 | struct kvm_vcpu; |
| 152 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 153 | struct nested_state { |
| 154 | struct vmcb *hsave; |
| 155 | u64 hsave_msr; |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 156 | u64 vm_cr_msr; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 157 | u64 vmcb; |
| 158 | |
| 159 | /* These are the merged vectors */ |
| 160 | u32 *msrpm; |
| 161 | |
| 162 | /* gpa pointers to the real vectors */ |
| 163 | u64 vmcb_msrpm; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 164 | u64 vmcb_iopm; |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 165 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 166 | /* A VMEXIT is required but not yet emulated */ |
| 167 | bool exit_required; |
| 168 | |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 169 | /* cache for intercepts of the guest */ |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 170 | u32 intercept_cr; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 171 | u32 intercept_dr; |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 172 | u32 intercept_exceptions; |
| 173 | u64 intercept; |
| 174 | |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 175 | /* Nested Paging related state */ |
| 176 | u64 nested_cr3; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 177 | }; |
| 178 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 179 | #define MSRPM_OFFSETS 16 |
| 180 | static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; |
| 181 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 182 | /* |
| 183 | * Set osvw_len to higher value when updated Revision Guides |
| 184 | * are published and we know what the new status bits are |
| 185 | */ |
| 186 | static uint64_t osvw_len = 4, osvw_status; |
| 187 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 188 | struct vcpu_svm { |
| 189 | struct kvm_vcpu vcpu; |
| 190 | struct vmcb *vmcb; |
| 191 | unsigned long vmcb_pa; |
| 192 | struct svm_cpu_data *svm_data; |
| 193 | uint64_t asid_generation; |
| 194 | uint64_t sysenter_esp; |
| 195 | uint64_t sysenter_eip; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 196 | uint64_t tsc_aux; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 197 | |
Tom Lendacky | d1d93fa | 2018-02-24 00:18:20 +0100 | [diff] [blame] | 198 | u64 msr_decfg; |
| 199 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 200 | u64 next_rip; |
| 201 | |
| 202 | u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS]; |
Avi Kivity | afe9e66 | 2010-10-21 12:20:32 +0200 | [diff] [blame] | 203 | struct { |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 204 | u16 fs; |
| 205 | u16 gs; |
| 206 | u16 ldt; |
Avi Kivity | afe9e66 | 2010-10-21 12:20:32 +0200 | [diff] [blame] | 207 | u64 gs_base; |
| 208 | } host; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 209 | |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 210 | u64 spec_ctrl; |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 211 | /* |
| 212 | * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be |
| 213 | * translated into the appropriate L2_CFG bits on the host to |
| 214 | * perform speculative control. |
| 215 | */ |
| 216 | u64 virt_spec_ctrl; |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 217 | |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 218 | u32 *msrpm; |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 219 | |
Avi Kivity | bd3d1ec | 2011-02-03 15:29:52 +0200 | [diff] [blame] | 220 | ulong nmi_iret_rip; |
| 221 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 222 | struct nested_state nested; |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 223 | |
| 224 | bool nmi_singlestep; |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 225 | u64 nmi_singlestep_guest_rflags; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 226 | |
| 227 | unsigned int3_injected; |
| 228 | unsigned long int3_rip; |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 229 | |
Joerg Roedel | 6092d3d | 2015-10-14 15:10:54 +0200 | [diff] [blame] | 230 | /* cached guest cpuid flags for faster access */ |
| 231 | bool nrips_enabled : 1; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 232 | |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 233 | u32 ldr_reg; |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 234 | u32 dfr_reg; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 235 | struct page *avic_backing_page; |
| 236 | u64 *avic_physical_id_cache; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 237 | bool avic_is_running; |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 238 | |
| 239 | /* |
| 240 | * Per-vcpu list of struct amd_svm_iommu_ir: |
| 241 | * This is used mainly to store interrupt remapping information used |
| 242 | * when update the vcpu affinity. This avoids the need to scan for |
| 243 | * IRTE and try to match ga_tag in the IOMMU driver. |
| 244 | */ |
| 245 | struct list_head ir_list; |
| 246 | spinlock_t ir_list_lock; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 247 | |
| 248 | /* which host CPU was used for running this vcpu */ |
| 249 | unsigned int last_cpu; |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | /* |
| 253 | * This is a wrapper of struct amd_iommu_ir_data. |
| 254 | */ |
| 255 | struct amd_svm_iommu_ir { |
| 256 | struct list_head node; /* Used by SVM for per-vcpu ir_list */ |
| 257 | void *data; /* Storing pointer to struct amd_ir_data */ |
Avi Kivity | 6c8166a | 2009-05-31 18:15:37 +0300 | [diff] [blame] | 258 | }; |
| 259 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 260 | #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF) |
Suthikulpanit, Suravee | e44e3ea | 2019-03-26 03:57:37 +0000 | [diff] [blame] | 261 | #define AVIC_LOGICAL_ID_ENTRY_VALID_BIT 31 |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 262 | #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31) |
| 263 | |
| 264 | #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL) |
| 265 | #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12) |
| 266 | #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62) |
| 267 | #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63) |
| 268 | |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 269 | static DEFINE_PER_CPU(u64, current_tsc_ratio); |
| 270 | #define TSC_RATIO_DEFAULT 0x0100000000ULL |
| 271 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 272 | #define MSR_INVALID 0xffffffffU |
| 273 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 274 | static const struct svm_direct_access_msrs { |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 275 | u32 index; /* Index of the MSR */ |
| 276 | bool always; /* True if intercept is always on */ |
| 277 | } direct_access_msrs[] = { |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 278 | { .index = MSR_STAR, .always = true }, |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 279 | { .index = MSR_IA32_SYSENTER_CS, .always = true }, |
| 280 | #ifdef CONFIG_X86_64 |
| 281 | { .index = MSR_GS_BASE, .always = true }, |
| 282 | { .index = MSR_FS_BASE, .always = true }, |
| 283 | { .index = MSR_KERNEL_GS_BASE, .always = true }, |
| 284 | { .index = MSR_LSTAR, .always = true }, |
| 285 | { .index = MSR_CSTAR, .always = true }, |
| 286 | { .index = MSR_SYSCALL_MASK, .always = true }, |
| 287 | #endif |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 288 | { .index = MSR_IA32_SPEC_CTRL, .always = false }, |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 289 | { .index = MSR_IA32_PRED_CMD, .always = false }, |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 290 | { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false }, |
| 291 | { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, |
| 292 | { .index = MSR_IA32_LASTINTFROMIP, .always = false }, |
| 293 | { .index = MSR_IA32_LASTINTTOIP, .always = false }, |
| 294 | { .index = MSR_INVALID, .always = false }, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | /* enable NPT for AMD64 and X86 with PAE */ |
| 298 | #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) |
| 299 | static bool npt_enabled = true; |
| 300 | #else |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 301 | static bool npt_enabled; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 302 | #endif |
| 303 | |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 304 | /* |
| 305 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 306 | * pause_filter_count: On processors that support Pause filtering(indicated |
| 307 | * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter |
| 308 | * count value. On VMRUN this value is loaded into an internal counter. |
| 309 | * Each time a pause instruction is executed, this counter is decremented |
| 310 | * until it reaches zero at which time a #VMEXIT is generated if pause |
| 311 | * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause |
| 312 | * Intercept Filtering for more details. |
| 313 | * This also indicate if ple logic enabled. |
| 314 | * |
| 315 | * pause_filter_thresh: In addition, some processor families support advanced |
| 316 | * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on |
| 317 | * the amount of time a guest is allowed to execute in a pause loop. |
| 318 | * In this mode, a 16-bit pause filter threshold field is added in the |
| 319 | * VMCB. The threshold value is a cycle count that is used to reset the |
| 320 | * pause counter. As with simple pause filtering, VMRUN loads the pause |
| 321 | * count value from VMCB into an internal counter. Then, on each pause |
| 322 | * instruction the hardware checks the elapsed number of cycles since |
| 323 | * the most recent pause instruction against the pause filter threshold. |
| 324 | * If the elapsed cycle count is greater than the pause filter threshold, |
| 325 | * then the internal pause count is reloaded from the VMCB and execution |
| 326 | * continues. If the elapsed cycle count is less than the pause filter |
| 327 | * threshold, then the internal pause count is decremented. If the count |
| 328 | * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is |
| 329 | * triggered. If advanced pause filtering is supported and pause filter |
| 330 | * threshold field is set to zero, the filter will operate in the simpler, |
| 331 | * count only mode. |
| 332 | */ |
| 333 | |
| 334 | static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP; |
| 335 | module_param(pause_filter_thresh, ushort, 0444); |
| 336 | |
| 337 | static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW; |
| 338 | module_param(pause_filter_count, ushort, 0444); |
| 339 | |
| 340 | /* Default doubles per-vcpu window every exit. */ |
| 341 | static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW; |
| 342 | module_param(pause_filter_count_grow, ushort, 0444); |
| 343 | |
| 344 | /* Default resets per-vcpu window every exit to pause_filter_count. */ |
| 345 | static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; |
| 346 | module_param(pause_filter_count_shrink, ushort, 0444); |
| 347 | |
| 348 | /* Default is to compute the maximum so we can never overflow. */ |
| 349 | static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX; |
| 350 | module_param(pause_filter_count_max, ushort, 0444); |
| 351 | |
Davidlohr Bueso | e235885 | 2012-01-17 14:09:50 +0100 | [diff] [blame] | 352 | /* allow nested paging (virtualized MMU) for all guests */ |
| 353 | static int npt = true; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 354 | module_param(npt, int, S_IRUGO); |
| 355 | |
Davidlohr Bueso | e235885 | 2012-01-17 14:09:50 +0100 | [diff] [blame] | 356 | /* allow nested virtualization in KVM/SVM */ |
| 357 | static int nested = true; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 358 | module_param(nested, int, S_IRUGO); |
| 359 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 360 | /* enable / disable AVIC */ |
| 361 | static int avic; |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 362 | #ifdef CONFIG_X86_LOCAL_APIC |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 363 | module_param(avic, int, S_IRUGO); |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 364 | #endif |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 365 | |
Paolo Bonzini | d647eb6 | 2019-06-20 14:13:33 +0200 | [diff] [blame] | 366 | /* enable/disable Next RIP Save */ |
| 367 | static int nrips = true; |
| 368 | module_param(nrips, int, 0444); |
| 369 | |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 370 | /* enable/disable Virtual VMLOAD VMSAVE */ |
| 371 | static int vls = true; |
| 372 | module_param(vls, int, 0444); |
| 373 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 374 | /* enable/disable Virtual GIF */ |
| 375 | static int vgif = true; |
| 376 | module_param(vgif, int, 0444); |
Suravee Suthikulpanit | 5ea11f2 | 2016-08-23 13:52:41 -0500 | [diff] [blame] | 377 | |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 378 | /* enable/disable SEV support */ |
| 379 | static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT); |
| 380 | module_param(sev, int, 0444); |
| 381 | |
Paolo Bonzini | 6f2f845 | 2019-05-20 15:34:35 +0200 | [diff] [blame] | 382 | static bool __read_mostly dump_invalid_vmcb = 0; |
| 383 | module_param(dump_invalid_vmcb, bool, 0644); |
| 384 | |
Brijesh Singh | 7607b71 | 2018-02-19 10:14:44 -0600 | [diff] [blame] | 385 | static u8 rsm_ins_bytes[] = "\x0f\xaa"; |
| 386 | |
Paolo Bonzini | 79a8059 | 2015-09-21 07:46:55 +0200 | [diff] [blame] | 387 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0); |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 388 | static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa); |
Joerg Roedel | a5c3832 | 2009-08-07 11:49:32 +0200 | [diff] [blame] | 389 | static void svm_complete_interrupts(struct vcpu_svm *svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 390 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 391 | static int nested_svm_exit_handled(struct vcpu_svm *svm); |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 392 | static int nested_svm_intercept(struct vcpu_svm *svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 393 | static int nested_svm_vmexit(struct vcpu_svm *svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 394 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 395 | bool has_error_code, u32 error_code); |
| 396 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 397 | enum { |
Joerg Roedel | 116a0a2 | 2010-12-03 11:45:49 +0100 | [diff] [blame] | 398 | VMCB_INTERCEPTS, /* Intercept vectors, TSC offset, |
| 399 | pause filter count */ |
Joerg Roedel | f56838e | 2010-12-03 11:45:50 +0100 | [diff] [blame] | 400 | VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */ |
Joerg Roedel | d48086d | 2010-12-03 11:45:51 +0100 | [diff] [blame] | 401 | VMCB_ASID, /* ASID */ |
Joerg Roedel | decdbf6 | 2010-12-03 11:45:52 +0100 | [diff] [blame] | 402 | VMCB_INTR, /* int_ctl, int_vector */ |
Joerg Roedel | b274716 | 2010-12-03 11:45:53 +0100 | [diff] [blame] | 403 | VMCB_NPT, /* npt_en, nCR3, gPAT */ |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 404 | VMCB_CR, /* CR0, CR3, CR4, EFER */ |
Joerg Roedel | 72214b9 | 2010-12-03 11:45:55 +0100 | [diff] [blame] | 405 | VMCB_DR, /* DR6, DR7 */ |
Joerg Roedel | 17a703c | 2010-12-03 11:45:56 +0100 | [diff] [blame] | 406 | VMCB_DT, /* GDT, IDT */ |
Joerg Roedel | 060d0c9 | 2010-12-03 11:45:57 +0100 | [diff] [blame] | 407 | VMCB_SEG, /* CS, DS, SS, ES, CPL */ |
Joerg Roedel | 0574dec | 2010-12-03 11:45:58 +0100 | [diff] [blame] | 408 | VMCB_CR2, /* CR2 only */ |
Joerg Roedel | b53ba3f | 2010-12-03 11:45:59 +0100 | [diff] [blame] | 409 | 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] | 410 | VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE, |
| 411 | * AVIC PHYSICAL_TABLE pointer, |
| 412 | * AVIC LOGICAL_TABLE pointer |
| 413 | */ |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 414 | VMCB_DIRTY_MAX, |
| 415 | }; |
| 416 | |
Joerg Roedel | 0574dec | 2010-12-03 11:45:58 +0100 | [diff] [blame] | 417 | /* TPR and CR2 are always written before VMRUN */ |
| 418 | #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 419 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 420 | #define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL |
| 421 | |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 422 | static int sev_flush_asids(void); |
| 423 | static DECLARE_RWSEM(sev_deactivate_lock); |
Tom Lendacky | e3b9a9e | 2019-10-03 21:17:43 +0000 | [diff] [blame] | 424 | static DEFINE_MUTEX(sev_bitmap_lock); |
Brijesh Singh | ed3cd23 | 2017-12-04 10:57:32 -0600 | [diff] [blame] | 425 | static unsigned int max_sev_asid; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 426 | static unsigned int min_sev_asid; |
| 427 | static unsigned long *sev_asid_bitmap; |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 428 | static unsigned long *sev_reclaim_asid_bitmap; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 429 | #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] | 430 | |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 431 | struct enc_region { |
| 432 | struct list_head list; |
| 433 | unsigned long npages; |
| 434 | struct page **pages; |
| 435 | unsigned long uaddr; |
| 436 | unsigned long size; |
| 437 | }; |
| 438 | |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 439 | |
| 440 | static inline struct kvm_svm *to_kvm_svm(struct kvm *kvm) |
| 441 | { |
| 442 | return container_of(kvm, struct kvm_svm, kvm); |
| 443 | } |
| 444 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 445 | static inline bool svm_sev_enabled(void) |
| 446 | { |
Paolo Bonzini | 853c110 | 2018-10-09 18:35:29 +0200 | [diff] [blame] | 447 | return IS_ENABLED(CONFIG_KVM_AMD_SEV) ? max_sev_asid : 0; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static inline bool sev_guest(struct kvm *kvm) |
| 451 | { |
Paolo Bonzini | 853c110 | 2018-10-09 18:35:29 +0200 | [diff] [blame] | 452 | #ifdef CONFIG_KVM_AMD_SEV |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 453 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 454 | |
| 455 | return sev->active; |
Paolo Bonzini | 853c110 | 2018-10-09 18:35:29 +0200 | [diff] [blame] | 456 | #else |
| 457 | return false; |
| 458 | #endif |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 459 | } |
Brijesh Singh | ed3cd23 | 2017-12-04 10:57:32 -0600 | [diff] [blame] | 460 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 461 | static inline int sev_get_asid(struct kvm *kvm) |
| 462 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 463 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 464 | |
| 465 | return sev->asid; |
| 466 | } |
| 467 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 468 | static inline void mark_all_dirty(struct vmcb *vmcb) |
| 469 | { |
| 470 | vmcb->control.clean = 0; |
| 471 | } |
| 472 | |
| 473 | static inline void mark_all_clean(struct vmcb *vmcb) |
| 474 | { |
| 475 | vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1) |
| 476 | & ~VMCB_ALWAYS_DIRTY_MASK; |
| 477 | } |
| 478 | |
| 479 | static inline void mark_dirty(struct vmcb *vmcb, int bit) |
| 480 | { |
| 481 | vmcb->control.clean &= ~(1 << bit); |
| 482 | } |
| 483 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 484 | static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) |
| 485 | { |
| 486 | return container_of(vcpu, struct vcpu_svm, vcpu); |
| 487 | } |
| 488 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 489 | static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data) |
| 490 | { |
| 491 | svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK; |
| 492 | mark_dirty(svm->vmcb, VMCB_AVIC); |
| 493 | } |
| 494 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 495 | static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu) |
| 496 | { |
| 497 | struct vcpu_svm *svm = to_svm(vcpu); |
| 498 | u64 *entry = svm->avic_physical_id_cache; |
| 499 | |
| 500 | if (!entry) |
| 501 | return false; |
| 502 | |
| 503 | return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); |
| 504 | } |
| 505 | |
Joerg Roedel | 384c636 | 2010-11-30 18:03:56 +0100 | [diff] [blame] | 506 | static void recalc_intercepts(struct vcpu_svm *svm) |
| 507 | { |
| 508 | struct vmcb_control_area *c, *h; |
| 509 | struct nested_state *g; |
| 510 | |
Joerg Roedel | 116a0a2 | 2010-12-03 11:45:49 +0100 | [diff] [blame] | 511 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
| 512 | |
Joerg Roedel | 384c636 | 2010-11-30 18:03:56 +0100 | [diff] [blame] | 513 | if (!is_guest_mode(&svm->vcpu)) |
| 514 | return; |
| 515 | |
| 516 | c = &svm->vmcb->control; |
| 517 | h = &svm->nested.hsave->control; |
| 518 | g = &svm->nested; |
| 519 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 520 | c->intercept_cr = h->intercept_cr | g->intercept_cr; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 521 | c->intercept_dr = h->intercept_dr | g->intercept_dr; |
Paolo Bonzini | bd89525 | 2018-01-11 16:55:24 +0100 | [diff] [blame] | 522 | c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions; |
Joerg Roedel | 384c636 | 2010-11-30 18:03:56 +0100 | [diff] [blame] | 523 | c->intercept = h->intercept | g->intercept; |
| 524 | } |
| 525 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 526 | static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm) |
| 527 | { |
| 528 | if (is_guest_mode(&svm->vcpu)) |
| 529 | return svm->nested.hsave; |
| 530 | else |
| 531 | return svm->vmcb; |
| 532 | } |
| 533 | |
| 534 | static inline void set_cr_intercept(struct vcpu_svm *svm, int bit) |
| 535 | { |
| 536 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 537 | |
| 538 | vmcb->control.intercept_cr |= (1U << bit); |
| 539 | |
| 540 | recalc_intercepts(svm); |
| 541 | } |
| 542 | |
| 543 | static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit) |
| 544 | { |
| 545 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 546 | |
| 547 | vmcb->control.intercept_cr &= ~(1U << bit); |
| 548 | |
| 549 | recalc_intercepts(svm); |
| 550 | } |
| 551 | |
| 552 | static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit) |
| 553 | { |
| 554 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 555 | |
| 556 | return vmcb->control.intercept_cr & (1U << bit); |
| 557 | } |
| 558 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 559 | static inline void set_dr_intercepts(struct vcpu_svm *svm) |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 560 | { |
| 561 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 562 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 563 | vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ) |
| 564 | | (1 << INTERCEPT_DR1_READ) |
| 565 | | (1 << INTERCEPT_DR2_READ) |
| 566 | | (1 << INTERCEPT_DR3_READ) |
| 567 | | (1 << INTERCEPT_DR4_READ) |
| 568 | | (1 << INTERCEPT_DR5_READ) |
| 569 | | (1 << INTERCEPT_DR6_READ) |
| 570 | | (1 << INTERCEPT_DR7_READ) |
| 571 | | (1 << INTERCEPT_DR0_WRITE) |
| 572 | | (1 << INTERCEPT_DR1_WRITE) |
| 573 | | (1 << INTERCEPT_DR2_WRITE) |
| 574 | | (1 << INTERCEPT_DR3_WRITE) |
| 575 | | (1 << INTERCEPT_DR4_WRITE) |
| 576 | | (1 << INTERCEPT_DR5_WRITE) |
| 577 | | (1 << INTERCEPT_DR6_WRITE) |
| 578 | | (1 << INTERCEPT_DR7_WRITE); |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 579 | |
| 580 | recalc_intercepts(svm); |
| 581 | } |
| 582 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 583 | static inline void clr_dr_intercepts(struct vcpu_svm *svm) |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 584 | { |
| 585 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 586 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 587 | vmcb->control.intercept_dr = 0; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 588 | |
| 589 | recalc_intercepts(svm); |
| 590 | } |
| 591 | |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 592 | static inline void set_exception_intercept(struct vcpu_svm *svm, int bit) |
| 593 | { |
| 594 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 595 | |
| 596 | vmcb->control.intercept_exceptions |= (1U << bit); |
| 597 | |
| 598 | recalc_intercepts(svm); |
| 599 | } |
| 600 | |
| 601 | static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit) |
| 602 | { |
| 603 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 604 | |
| 605 | vmcb->control.intercept_exceptions &= ~(1U << bit); |
| 606 | |
| 607 | recalc_intercepts(svm); |
| 608 | } |
| 609 | |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 610 | static inline void set_intercept(struct vcpu_svm *svm, int bit) |
| 611 | { |
| 612 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 613 | |
| 614 | vmcb->control.intercept |= (1ULL << bit); |
| 615 | |
| 616 | recalc_intercepts(svm); |
| 617 | } |
| 618 | |
| 619 | static inline void clr_intercept(struct vcpu_svm *svm, int bit) |
| 620 | { |
| 621 | struct vmcb *vmcb = get_host_vmcb(svm); |
| 622 | |
| 623 | vmcb->control.intercept &= ~(1ULL << bit); |
| 624 | |
| 625 | recalc_intercepts(svm); |
| 626 | } |
| 627 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 628 | static inline bool vgif_enabled(struct vcpu_svm *svm) |
| 629 | { |
| 630 | return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK); |
| 631 | } |
| 632 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 633 | static inline void enable_gif(struct vcpu_svm *svm) |
| 634 | { |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 635 | if (vgif_enabled(svm)) |
| 636 | svm->vmcb->control.int_ctl |= V_GIF_MASK; |
| 637 | else |
| 638 | svm->vcpu.arch.hflags |= HF_GIF_MASK; |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | static inline void disable_gif(struct vcpu_svm *svm) |
| 642 | { |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 643 | if (vgif_enabled(svm)) |
| 644 | svm->vmcb->control.int_ctl &= ~V_GIF_MASK; |
| 645 | else |
| 646 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | static inline bool gif_set(struct vcpu_svm *svm) |
| 650 | { |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 651 | if (vgif_enabled(svm)) |
| 652 | return !!(svm->vmcb->control.int_ctl & V_GIF_MASK); |
| 653 | else |
| 654 | return !!(svm->vcpu.arch.hflags & HF_GIF_MASK); |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 655 | } |
| 656 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 657 | static unsigned long iopm_base; |
| 658 | |
| 659 | struct kvm_ldttss_desc { |
| 660 | u16 limit0; |
| 661 | u16 base0; |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 662 | unsigned base1:8, type:5, dpl:2, p:1; |
| 663 | unsigned limit1:4, zero0:3, g:1, base2:8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 664 | u32 base3; |
| 665 | u32 zero1; |
| 666 | } __attribute__((packed)); |
| 667 | |
| 668 | struct svm_cpu_data { |
| 669 | int cpu; |
| 670 | |
Avi Kivity | 5008fdf | 2007-04-02 13:05:50 +0300 | [diff] [blame] | 671 | u64 asid_generation; |
| 672 | u32 max_asid; |
| 673 | u32 next_asid; |
Brijesh Singh | 4faefff | 2017-12-04 10:57:25 -0600 | [diff] [blame] | 674 | u32 min_asid; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 675 | struct kvm_ldttss_desc *tss_desc; |
| 676 | |
| 677 | struct page *save_area; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 678 | struct vmcb *current_vmcb; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 679 | |
| 680 | /* index = sev_asid, value = vmcb pointer */ |
| 681 | struct vmcb **sev_vmcbs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 682 | }; |
| 683 | |
| 684 | static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); |
| 685 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 686 | static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 687 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 688 | #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 689 | #define MSRS_RANGE_SIZE 2048 |
| 690 | #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) |
| 691 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 692 | static u32 svm_msrpm_offset(u32 msr) |
| 693 | { |
| 694 | u32 offset; |
| 695 | int i; |
| 696 | |
| 697 | for (i = 0; i < NUM_MSR_MAPS; i++) { |
| 698 | if (msr < msrpm_ranges[i] || |
| 699 | msr >= msrpm_ranges[i] + MSRS_IN_RANGE) |
| 700 | continue; |
| 701 | |
| 702 | offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */ |
| 703 | offset += (i * MSRS_RANGE_SIZE); /* add range offset */ |
| 704 | |
| 705 | /* Now we have the u8 offset - but need the u32 offset */ |
| 706 | return offset / 4; |
| 707 | } |
| 708 | |
| 709 | /* MSR not in any range */ |
| 710 | return MSR_INVALID; |
| 711 | } |
| 712 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 713 | #define MAX_INST_SIZE 15 |
| 714 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 715 | static inline void clgi(void) |
| 716 | { |
Uros Bizjak | ac5ffda2 | 2018-11-26 17:00:08 +0100 | [diff] [blame] | 717 | asm volatile (__ex("clgi")); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | static inline void stgi(void) |
| 721 | { |
Uros Bizjak | ac5ffda2 | 2018-11-26 17:00:08 +0100 | [diff] [blame] | 722 | asm volatile (__ex("stgi")); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static inline void invlpga(unsigned long addr, u32 asid) |
| 726 | { |
Uros Bizjak | ac5ffda2 | 2018-11-26 17:00:08 +0100 | [diff] [blame] | 727 | asm volatile (__ex("invlpga %1, %0") : : "c"(asid), "a"(addr)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 730 | static int get_npt_level(struct kvm_vcpu *vcpu) |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 731 | { |
| 732 | #ifdef CONFIG_X86_64 |
Yu Zhang | 2a7266a | 2017-08-24 20:27:54 +0800 | [diff] [blame] | 733 | return PT64_ROOT_4LEVEL; |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 734 | #else |
| 735 | return PT32E_ROOT_LEVEL; |
| 736 | #endif |
| 737 | } |
| 738 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 739 | static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 740 | { |
Zachary Amsden | 6dc696d | 2010-05-26 15:09:43 -1000 | [diff] [blame] | 741 | vcpu->arch.efer = efer; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 742 | if (!npt_enabled && !(efer & EFER_LMA)) |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 743 | efer &= ~EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 744 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 745 | to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME; |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 746 | mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 747 | } |
| 748 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 749 | static int is_external_interrupt(u32 info) |
| 750 | { |
| 751 | info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; |
| 752 | return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); |
| 753 | } |
| 754 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 755 | static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 756 | { |
| 757 | struct vcpu_svm *svm = to_svm(vcpu); |
| 758 | u32 ret = 0; |
| 759 | |
| 760 | if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 761 | ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS; |
| 762 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 766 | { |
| 767 | struct vcpu_svm *svm = to_svm(vcpu); |
| 768 | |
| 769 | if (mask == 0) |
| 770 | svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; |
| 771 | else |
| 772 | svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK; |
| 773 | |
| 774 | } |
| 775 | |
Vitaly Kuznetsov | f8ea7c6 | 2019-08-13 15:53:30 +0200 | [diff] [blame] | 776 | static int skip_emulated_instruction(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 777 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 778 | struct vcpu_svm *svm = to_svm(vcpu); |
| 779 | |
Paolo Bonzini | d647eb6 | 2019-06-20 14:13:33 +0200 | [diff] [blame] | 780 | if (nrips && svm->vmcb->control.next_rip != 0) { |
Dirk Müller | d292242 | 2015-10-01 13:43:42 +0200 | [diff] [blame] | 781 | WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS)); |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 782 | svm->next_rip = svm->vmcb->control.next_rip; |
Bandan Das | f104765 | 2015-06-11 02:05:33 -0400 | [diff] [blame] | 783 | } |
Andre Przywara | 6bc31bd | 2010-04-11 23:07:28 +0200 | [diff] [blame] | 784 | |
Sean Christopherson | 1957aa6 | 2019-08-27 14:40:39 -0700 | [diff] [blame] | 785 | if (!svm->next_rip) { |
| 786 | if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP)) |
| 787 | return 0; |
| 788 | } else { |
| 789 | if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE) |
| 790 | pr_err("%s: ip 0x%lx next 0x%llx\n", |
| 791 | __func__, kvm_rip_read(vcpu), svm->next_rip); |
| 792 | kvm_rip_write(vcpu, svm->next_rip); |
| 793 | } |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 794 | svm_set_interrupt_shadow(vcpu, 0); |
Vitaly Kuznetsov | f8ea7c6 | 2019-08-13 15:53:30 +0200 | [diff] [blame] | 795 | |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 796 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 797 | } |
| 798 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 799 | static void svm_queue_exception(struct kvm_vcpu *vcpu) |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 800 | { |
| 801 | struct vcpu_svm *svm = to_svm(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 802 | unsigned nr = vcpu->arch.exception.nr; |
| 803 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 804 | bool reinject = vcpu->arch.exception.injected; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 805 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 806 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 807 | /* |
| 808 | * If we are within a nested VM we'd better #VMEXIT and let the guest |
| 809 | * handle the exception |
| 810 | */ |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 811 | if (!reinject && |
| 812 | nested_svm_check_exception(svm, nr, has_error_code, error_code)) |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 813 | return; |
| 814 | |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 815 | kvm_deliver_exception_payload(&svm->vcpu); |
| 816 | |
Paolo Bonzini | d647eb6 | 2019-06-20 14:13:33 +0200 | [diff] [blame] | 817 | if (nr == BP_VECTOR && !nrips) { |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 818 | unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu); |
| 819 | |
| 820 | /* |
| 821 | * For guest debugging where we have to reinject #BP if some |
| 822 | * INT3 is guest-owned: |
| 823 | * Emulate nRIP by moving RIP forward. Will fail if injection |
| 824 | * raises a fault that is not intercepted. Still better than |
| 825 | * failing in all cases. |
| 826 | */ |
Vitaly Kuznetsov | f8ea7c6 | 2019-08-13 15:53:30 +0200 | [diff] [blame] | 827 | (void)skip_emulated_instruction(&svm->vcpu); |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 828 | rip = kvm_rip_read(&svm->vcpu); |
| 829 | svm->int3_rip = rip + svm->vmcb->save.cs.base; |
| 830 | svm->int3_injected = rip - old_rip; |
| 831 | } |
| 832 | |
Jan Kiszka | 116a475 | 2010-02-23 17:47:54 +0100 | [diff] [blame] | 833 | svm->vmcb->control.event_inj = nr |
| 834 | | SVM_EVTINJ_VALID |
| 835 | | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) |
| 836 | | SVM_EVTINJ_TYPE_EXEPT; |
| 837 | svm->vmcb->control.event_inj_err = error_code; |
| 838 | } |
| 839 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 840 | static void svm_init_erratum_383(void) |
| 841 | { |
| 842 | u32 low, high; |
| 843 | int err; |
| 844 | u64 val; |
| 845 | |
Borislav Petkov | e6ee94d | 2013-03-20 15:07:27 +0100 | [diff] [blame] | 846 | if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH)) |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 847 | return; |
| 848 | |
| 849 | /* Use _safe variants to not break nested virtualization */ |
| 850 | val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err); |
| 851 | if (err) |
| 852 | return; |
| 853 | |
| 854 | val |= (1ULL << 47); |
| 855 | |
| 856 | low = lower_32_bits(val); |
| 857 | high = upper_32_bits(val); |
| 858 | |
| 859 | native_write_msr_safe(MSR_AMD64_DC_CFG, low, high); |
| 860 | |
| 861 | erratum_383_found = true; |
| 862 | } |
| 863 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 864 | static void svm_init_osvw(struct kvm_vcpu *vcpu) |
| 865 | { |
| 866 | /* |
| 867 | * Guests should see errata 400 and 415 as fixed (assuming that |
| 868 | * HLT and IO instructions are intercepted). |
| 869 | */ |
| 870 | vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3; |
| 871 | vcpu->arch.osvw.status = osvw_status & ~(6ULL); |
| 872 | |
| 873 | /* |
| 874 | * By increasing VCPU's osvw.length to 3 we are telling the guest that |
| 875 | * all osvw.status bits inside that length, including bit 0 (which is |
| 876 | * reserved for erratum 298), are valid. However, if host processor's |
| 877 | * osvw_len is 0 then osvw_status[0] carries no information. We need to |
| 878 | * be conservative here and therefore we tell the guest that erratum 298 |
| 879 | * is present (because we really don't know). |
| 880 | */ |
| 881 | if (osvw_len == 0 && boot_cpu_data.x86 == 0x10) |
| 882 | vcpu->arch.osvw.status |= 1; |
| 883 | } |
| 884 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 885 | static int has_svm(void) |
| 886 | { |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 887 | const char *msg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 888 | |
Eduardo Habkost | 63d1142 | 2008-11-17 19:03:20 -0200 | [diff] [blame] | 889 | if (!cpu_has_svm(&msg)) { |
Joe Perches | ff81ff1 | 2009-01-08 11:05:17 -0800 | [diff] [blame] | 890 | printk(KERN_INFO "has_svm: %s\n", msg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 891 | return 0; |
| 892 | } |
| 893 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 894 | return 1; |
| 895 | } |
| 896 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 897 | static void svm_hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 898 | { |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 899 | /* Make sure we clean up behind us */ |
| 900 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) |
| 901 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); |
| 902 | |
Eduardo Habkost | 2c8dcee | 2008-11-17 19:03:21 -0200 | [diff] [blame] | 903 | cpu_svm_disable(); |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 904 | |
| 905 | amd_pmu_disable_virt(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 906 | } |
| 907 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 908 | static int svm_hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 909 | { |
| 910 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 911 | struct svm_cpu_data *sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 912 | uint64_t efer; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 913 | struct desc_struct *gdt; |
| 914 | int me = raw_smp_processor_id(); |
| 915 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 916 | rdmsrl(MSR_EFER, efer); |
| 917 | if (efer & EFER_SVME) |
| 918 | return -EBUSY; |
| 919 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 920 | if (!has_svm()) { |
Borislav Petkov | 1f5b77f | 2012-10-20 20:20:04 +0200 | [diff] [blame] | 921 | pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 922 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 923 | } |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 924 | sd = per_cpu(svm_data, me); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 925 | if (!sd) { |
Borislav Petkov | 1f5b77f | 2012-10-20 20:20:04 +0200 | [diff] [blame] | 926 | pr_err("%s: svm_data is NULL on %d\n", __func__, me); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 927 | return -EINVAL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 928 | } |
| 929 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 930 | sd->asid_generation = 1; |
| 931 | sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; |
| 932 | sd->next_asid = sd->max_asid + 1; |
Brijesh Singh | ed3cd23 | 2017-12-04 10:57:32 -0600 | [diff] [blame] | 933 | sd->min_asid = max_sev_asid + 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 934 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 935 | gdt = get_current_gdt_rw(); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 936 | sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 937 | |
Alexander Graf | 9962d03 | 2008-11-25 20:17:02 +0100 | [diff] [blame] | 938 | wrmsrl(MSR_EFER, efer | EFER_SVME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 939 | |
Linus Torvalds | d031655 | 2009-12-14 09:58:24 -0800 | [diff] [blame] | 940 | 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] | 941 | |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 942 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
| 943 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 944 | __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT); |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 945 | } |
| 946 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 947 | |
| 948 | /* |
| 949 | * Get OSVW bits. |
| 950 | * |
| 951 | * Note that it is possible to have a system with mixed processor |
| 952 | * revisions and therefore different OSVW bits. If bits are not the same |
| 953 | * on different processors then choose the worst case (i.e. if erratum |
| 954 | * is present on one processor and not on another then assume that the |
| 955 | * erratum is present everywhere). |
| 956 | */ |
| 957 | if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) { |
| 958 | uint64_t len, status = 0; |
| 959 | int err; |
| 960 | |
| 961 | len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err); |
| 962 | if (!err) |
| 963 | status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS, |
| 964 | &err); |
| 965 | |
| 966 | if (err) |
| 967 | osvw_status = osvw_len = 0; |
| 968 | else { |
| 969 | if (len < osvw_len) |
| 970 | osvw_len = len; |
| 971 | osvw_status |= status; |
| 972 | osvw_status &= (1ULL << osvw_len) - 1; |
| 973 | } |
| 974 | } else |
| 975 | osvw_status = osvw_len = 0; |
| 976 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 977 | svm_init_erratum_383(); |
| 978 | |
Joerg Roedel | 1018faa | 2012-02-29 14:57:32 +0100 | [diff] [blame] | 979 | amd_pmu_enable_virt(); |
| 980 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 981 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 982 | } |
| 983 | |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 984 | static void svm_cpu_uninit(int cpu) |
| 985 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 986 | 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] | 987 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 988 | if (!sd) |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 989 | return; |
| 990 | |
| 991 | per_cpu(svm_data, raw_smp_processor_id()) = NULL; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 992 | kfree(sd->sev_vmcbs); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 993 | __free_page(sd->save_area); |
| 994 | kfree(sd); |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 995 | } |
| 996 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 997 | static int svm_cpu_init(int cpu) |
| 998 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 999 | struct svm_cpu_data *sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1000 | int r; |
| 1001 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1002 | sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); |
| 1003 | if (!sd) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1004 | return -ENOMEM; |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1005 | sd->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1006 | r = -ENOMEM; |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1007 | sd->save_area = alloc_page(GFP_KERNEL); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1008 | if (!sd->save_area) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1009 | goto err_1; |
| 1010 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1011 | if (svm_sev_enabled()) { |
| 1012 | r = -ENOMEM; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 1013 | sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1, |
| 1014 | sizeof(void *), |
| 1015 | GFP_KERNEL); |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1016 | if (!sd->sev_vmcbs) |
| 1017 | goto err_1; |
| 1018 | } |
| 1019 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1020 | per_cpu(svm_data, cpu) = sd; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1021 | |
| 1022 | return 0; |
| 1023 | |
| 1024 | err_1: |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1025 | kfree(sd); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1026 | return r; |
| 1027 | |
| 1028 | } |
| 1029 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 1030 | static bool valid_msr_intercept(u32 index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1031 | { |
| 1032 | int i; |
| 1033 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 1034 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) |
| 1035 | if (direct_access_msrs[i].index == index) |
| 1036 | return true; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1037 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 1038 | return false; |
| 1039 | } |
| 1040 | |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 1041 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr) |
| 1042 | { |
| 1043 | u8 bit_write; |
| 1044 | unsigned long tmp; |
| 1045 | u32 offset; |
| 1046 | u32 *msrpm; |
| 1047 | |
| 1048 | msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm: |
| 1049 | to_svm(vcpu)->msrpm; |
| 1050 | |
| 1051 | offset = svm_msrpm_offset(msr); |
| 1052 | bit_write = 2 * (msr & 0x0f) + 1; |
| 1053 | tmp = msrpm[offset]; |
| 1054 | |
| 1055 | BUG_ON(offset == MSR_INVALID); |
| 1056 | |
| 1057 | return !!test_bit(bit_write, &tmp); |
| 1058 | } |
| 1059 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1060 | static void set_msr_interception(u32 *msrpm, unsigned msr, |
| 1061 | int read, int write) |
| 1062 | { |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 1063 | u8 bit_read, bit_write; |
| 1064 | unsigned long tmp; |
| 1065 | u32 offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1066 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 1067 | /* |
| 1068 | * If this warning triggers extend the direct_access_msrs list at the |
| 1069 | * beginning of the file |
| 1070 | */ |
| 1071 | WARN_ON(!valid_msr_intercept(msr)); |
| 1072 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 1073 | offset = svm_msrpm_offset(msr); |
| 1074 | bit_read = 2 * (msr & 0x0f); |
| 1075 | bit_write = 2 * (msr & 0x0f) + 1; |
| 1076 | tmp = msrpm[offset]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1077 | |
Joerg Roedel | 455716f | 2010-03-01 15:34:35 +0100 | [diff] [blame] | 1078 | BUG_ON(offset == MSR_INVALID); |
| 1079 | |
| 1080 | read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp); |
| 1081 | write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp); |
| 1082 | |
| 1083 | msrpm[offset] = tmp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1084 | } |
| 1085 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1086 | static void svm_vcpu_init_msrpm(u32 *msrpm) |
| 1087 | { |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 1088 | int i; |
| 1089 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1090 | memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER)); |
| 1091 | |
Joerg Roedel | ac72a9b | 2010-03-01 15:34:36 +0100 | [diff] [blame] | 1092 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 1093 | if (!direct_access_msrs[i].always) |
| 1094 | continue; |
| 1095 | |
| 1096 | set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1); |
| 1097 | } |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1098 | } |
| 1099 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 1100 | static void add_msr_offset(u32 offset) |
| 1101 | { |
| 1102 | int i; |
| 1103 | |
| 1104 | for (i = 0; i < MSRPM_OFFSETS; ++i) { |
| 1105 | |
| 1106 | /* Offset already in list? */ |
| 1107 | if (msrpm_offsets[i] == offset) |
| 1108 | return; |
| 1109 | |
| 1110 | /* Slot used by another offset? */ |
| 1111 | if (msrpm_offsets[i] != MSR_INVALID) |
| 1112 | continue; |
| 1113 | |
| 1114 | /* Add offset to list */ |
| 1115 | msrpm_offsets[i] = offset; |
| 1116 | |
| 1117 | return; |
| 1118 | } |
| 1119 | |
| 1120 | /* |
| 1121 | * If this BUG triggers the msrpm_offsets table has an overflow. Just |
| 1122 | * increase MSRPM_OFFSETS in this case. |
| 1123 | */ |
| 1124 | BUG(); |
| 1125 | } |
| 1126 | |
| 1127 | static void init_msrpm_offsets(void) |
| 1128 | { |
| 1129 | int i; |
| 1130 | |
| 1131 | memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets)); |
| 1132 | |
| 1133 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
| 1134 | u32 offset; |
| 1135 | |
| 1136 | offset = svm_msrpm_offset(direct_access_msrs[i].index); |
| 1137 | BUG_ON(offset == MSR_INVALID); |
| 1138 | |
| 1139 | add_msr_offset(offset); |
| 1140 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 1143 | static void svm_enable_lbrv(struct vcpu_svm *svm) |
| 1144 | { |
| 1145 | u32 *msrpm = svm->msrpm; |
| 1146 | |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 1147 | svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK; |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 1148 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); |
| 1149 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); |
| 1150 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); |
| 1151 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1); |
| 1152 | } |
| 1153 | |
| 1154 | static void svm_disable_lbrv(struct vcpu_svm *svm) |
| 1155 | { |
| 1156 | u32 *msrpm = svm->msrpm; |
| 1157 | |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 1158 | svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK; |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 1159 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0); |
| 1160 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0); |
| 1161 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0); |
| 1162 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0); |
| 1163 | } |
| 1164 | |
Ladi Prosek | 4aebd0e | 2017-06-21 09:06:57 +0200 | [diff] [blame] | 1165 | static void disable_nmi_singlestep(struct vcpu_svm *svm) |
| 1166 | { |
| 1167 | svm->nmi_singlestep = false; |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 1168 | |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 1169 | if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) { |
| 1170 | /* Clear our flags if they were not set by the guest */ |
| 1171 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) |
| 1172 | svm->vmcb->save.rflags &= ~X86_EFLAGS_TF; |
| 1173 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) |
| 1174 | svm->vmcb->save.rflags &= ~X86_EFLAGS_RF; |
| 1175 | } |
Ladi Prosek | 4aebd0e | 2017-06-21 09:06:57 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1178 | /* Note: |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1179 | * This hash table is used to map VM_ID to a struct kvm_svm, |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1180 | * when handling AMD IOMMU GALOG notification to schedule in |
| 1181 | * a particular vCPU. |
| 1182 | */ |
| 1183 | #define SVM_VM_DATA_HASH_BITS 8 |
David Hildenbrand | 681bcea | 2017-01-24 22:21:16 +0100 | [diff] [blame] | 1184 | static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS); |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1185 | static u32 next_vm_id = 0; |
| 1186 | static bool next_vm_id_wrapped = 0; |
David Hildenbrand | 681bcea | 2017-01-24 22:21:16 +0100 | [diff] [blame] | 1187 | static DEFINE_SPINLOCK(svm_vm_data_hash_lock); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1188 | |
| 1189 | /* Note: |
| 1190 | * This function is called from IOMMU driver to notify |
| 1191 | * SVM to schedule in a particular vCPU of a particular VM. |
| 1192 | */ |
| 1193 | static int avic_ga_log_notifier(u32 ga_tag) |
| 1194 | { |
| 1195 | unsigned long flags; |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1196 | struct kvm_svm *kvm_svm; |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1197 | struct kvm_vcpu *vcpu = NULL; |
| 1198 | u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag); |
| 1199 | u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag); |
| 1200 | |
| 1201 | pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id); |
| 1202 | |
| 1203 | spin_lock_irqsave(&svm_vm_data_hash_lock, flags); |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1204 | hash_for_each_possible(svm_vm_data_hash, kvm_svm, hnode, vm_id) { |
| 1205 | if (kvm_svm->avic_vm_id != vm_id) |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1206 | continue; |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1207 | vcpu = kvm_get_vcpu_by_id(&kvm_svm->kvm, vcpu_id); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1208 | break; |
| 1209 | } |
| 1210 | spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); |
| 1211 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1212 | /* Note: |
| 1213 | * At this point, the IOMMU should have already set the pending |
| 1214 | * bit in the vAPIC backing page. So, we just need to schedule |
| 1215 | * in the vcpu. |
| 1216 | */ |
Paolo Bonzini | 1cf5358 | 2017-10-10 12:51:56 +0200 | [diff] [blame] | 1217 | if (vcpu) |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1218 | kvm_vcpu_wake_up(vcpu); |
| 1219 | |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 1223 | static __init int sev_hardware_setup(void) |
| 1224 | { |
| 1225 | struct sev_user_data_status *status; |
| 1226 | int rc; |
| 1227 | |
| 1228 | /* Maximum number of encrypted guests supported simultaneously */ |
| 1229 | max_sev_asid = cpuid_ecx(0x8000001F); |
| 1230 | |
| 1231 | if (!max_sev_asid) |
| 1232 | return 1; |
| 1233 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1234 | /* Minimum ASID value that should be used for SEV guest */ |
| 1235 | min_sev_asid = cpuid_edx(0x8000001F); |
| 1236 | |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 1237 | /* Initialize SEV ASID bitmaps */ |
Andy Shevchenko | a101c9d63 | 2018-08-30 14:49:59 +0300 | [diff] [blame] | 1238 | sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL); |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1239 | if (!sev_asid_bitmap) |
| 1240 | return 1; |
| 1241 | |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 1242 | sev_reclaim_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL); |
| 1243 | if (!sev_reclaim_asid_bitmap) |
| 1244 | return 1; |
| 1245 | |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 1246 | status = kmalloc(sizeof(*status), GFP_KERNEL); |
| 1247 | if (!status) |
| 1248 | return 1; |
| 1249 | |
| 1250 | /* |
| 1251 | * Check SEV platform status. |
| 1252 | * |
| 1253 | * PLATFORM_STATUS can be called in any state, if we failed to query |
| 1254 | * the PLATFORM status then either PSP firmware does not support SEV |
| 1255 | * feature or SEV firmware is dead. |
| 1256 | */ |
| 1257 | rc = sev_platform_status(status, NULL); |
| 1258 | if (rc) |
| 1259 | goto err; |
| 1260 | |
| 1261 | pr_info("SEV supported\n"); |
| 1262 | |
| 1263 | err: |
| 1264 | kfree(status); |
| 1265 | return rc; |
| 1266 | } |
| 1267 | |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 1268 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 1269 | { |
| 1270 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1271 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 1272 | int old = control->pause_filter_count; |
| 1273 | |
| 1274 | control->pause_filter_count = __grow_ple_window(old, |
| 1275 | pause_filter_count, |
| 1276 | pause_filter_count_grow, |
| 1277 | pause_filter_count_max); |
| 1278 | |
Peter Xu | 4f75bcc | 2019-09-06 10:17:22 +0800 | [diff] [blame] | 1279 | if (control->pause_filter_count != old) { |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 1280 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
Peter Xu | 4f75bcc | 2019-09-06 10:17:22 +0800 | [diff] [blame] | 1281 | trace_kvm_ple_window_update(vcpu->vcpu_id, |
| 1282 | control->pause_filter_count, old); |
| 1283 | } |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 1287 | { |
| 1288 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1289 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 1290 | int old = control->pause_filter_count; |
| 1291 | |
| 1292 | control->pause_filter_count = |
| 1293 | __shrink_ple_window(old, |
| 1294 | pause_filter_count, |
| 1295 | pause_filter_count_shrink, |
| 1296 | pause_filter_count); |
Peter Xu | 4f75bcc | 2019-09-06 10:17:22 +0800 | [diff] [blame] | 1297 | if (control->pause_filter_count != old) { |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 1298 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
Peter Xu | 4f75bcc | 2019-09-06 10:17:22 +0800 | [diff] [blame] | 1299 | trace_kvm_ple_window_update(vcpu->vcpu_id, |
| 1300 | control->pause_filter_count, old); |
| 1301 | } |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 1302 | } |
| 1303 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1304 | static __init int svm_hardware_setup(void) |
| 1305 | { |
| 1306 | int cpu; |
| 1307 | struct page *iopm_pages; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1308 | void *iopm_va; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1309 | int r; |
| 1310 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1311 | iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER); |
| 1312 | |
| 1313 | if (!iopm_pages) |
| 1314 | return -ENOMEM; |
Anthony Liguori | c868133 | 2007-04-30 09:48:11 +0300 | [diff] [blame] | 1315 | |
| 1316 | iopm_va = page_address(iopm_pages); |
| 1317 | memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1318 | iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; |
| 1319 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 1320 | init_msrpm_offsets(); |
| 1321 | |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 1322 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 1323 | kvm_enable_efer_bits(EFER_NX); |
| 1324 | |
Alexander Graf | 1b2fd70 | 2009-02-02 16:23:51 +0100 | [diff] [blame] | 1325 | if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) |
| 1326 | kvm_enable_efer_bits(EFER_FFXSR); |
| 1327 | |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 1328 | if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 1329 | kvm_has_tsc_control = true; |
Haozhong Zhang | bc9b961 | 2015-10-20 15:39:01 +0800 | [diff] [blame] | 1330 | kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX; |
| 1331 | kvm_tsc_scaling_ratio_frac_bits = 32; |
Joerg Roedel | 92a1f12 | 2011-03-25 09:44:51 +0100 | [diff] [blame] | 1332 | } |
| 1333 | |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 1334 | /* Check for pause filtering support */ |
| 1335 | if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) { |
| 1336 | pause_filter_count = 0; |
| 1337 | pause_filter_thresh = 0; |
| 1338 | } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) { |
| 1339 | pause_filter_thresh = 0; |
| 1340 | } |
| 1341 | |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 1342 | if (nested) { |
| 1343 | printk(KERN_INFO "kvm: Nested Virtualization enabled\n"); |
Joerg Roedel | eec4b14 | 2010-05-05 16:04:44 +0200 | [diff] [blame] | 1344 | kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE); |
Alexander Graf | 236de05 | 2008-11-25 20:17:10 +0100 | [diff] [blame] | 1345 | } |
| 1346 | |
Brijesh Singh | e9df094 | 2017-12-04 10:57:33 -0600 | [diff] [blame] | 1347 | if (sev) { |
| 1348 | if (boot_cpu_has(X86_FEATURE_SEV) && |
| 1349 | IS_ENABLED(CONFIG_KVM_AMD_SEV)) { |
| 1350 | r = sev_hardware_setup(); |
| 1351 | if (r) |
| 1352 | sev = false; |
| 1353 | } else { |
| 1354 | sev = false; |
| 1355 | } |
| 1356 | } |
| 1357 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 1358 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1359 | r = svm_cpu_init(cpu); |
| 1360 | if (r) |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1361 | goto err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1362 | } |
Joerg Roedel | 33bd6a0 | 2008-02-07 13:47:38 +0100 | [diff] [blame] | 1363 | |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 1364 | if (!boot_cpu_has(X86_FEATURE_NPT)) |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 1365 | npt_enabled = false; |
| 1366 | |
Joerg Roedel | 6c7dac7 | 2008-02-07 13:47:40 +0100 | [diff] [blame] | 1367 | if (npt_enabled && !npt) { |
| 1368 | printk(KERN_INFO "kvm: Nested Paging disabled\n"); |
| 1369 | npt_enabled = false; |
| 1370 | } |
| 1371 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 1372 | if (npt_enabled) { |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 1373 | printk(KERN_INFO "kvm: Nested Paging enabled\n"); |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 1374 | kvm_enable_tdp(); |
Joerg Roedel | 5f4cb66 | 2008-07-14 20:36:36 +0200 | [diff] [blame] | 1375 | } else |
| 1376 | kvm_disable_tdp(); |
Joerg Roedel | e3da3ac | 2008-02-07 13:47:39 +0100 | [diff] [blame] | 1377 | |
Paolo Bonzini | d647eb6 | 2019-06-20 14:13:33 +0200 | [diff] [blame] | 1378 | if (nrips) { |
| 1379 | if (!boot_cpu_has(X86_FEATURE_NRIPS)) |
| 1380 | nrips = false; |
| 1381 | } |
| 1382 | |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1383 | if (avic) { |
| 1384 | if (!npt_enabled || |
| 1385 | !boot_cpu_has(X86_FEATURE_AVIC) || |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1386 | !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) { |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1387 | avic = false; |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1388 | } else { |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1389 | pr_info("AVIC enabled\n"); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1390 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1391 | amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); |
| 1392 | } |
Suravee Suthikulpanit | 5b8abf1 | 2016-06-15 17:24:36 -0500 | [diff] [blame] | 1393 | } |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1394 | |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 1395 | if (vls) { |
| 1396 | if (!npt_enabled || |
Borislav Petkov | 5442c26 | 2017-08-01 20:55:52 +0200 | [diff] [blame] | 1397 | !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) || |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 1398 | !IS_ENABLED(CONFIG_X86_64)) { |
| 1399 | vls = false; |
| 1400 | } else { |
| 1401 | pr_info("Virtual VMLOAD VMSAVE supported\n"); |
| 1402 | } |
| 1403 | } |
| 1404 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 1405 | if (vgif) { |
| 1406 | if (!boot_cpu_has(X86_FEATURE_VGIF)) |
| 1407 | vgif = false; |
| 1408 | else |
| 1409 | pr_info("Virtual GIF supported\n"); |
| 1410 | } |
| 1411 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1412 | return 0; |
| 1413 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1414 | err: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1415 | __free_pages(iopm_pages, IOPM_ALLOC_ORDER); |
| 1416 | iopm_base = 0; |
| 1417 | return r; |
| 1418 | } |
| 1419 | |
| 1420 | static __exit void svm_hardware_unsetup(void) |
| 1421 | { |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 1422 | int cpu; |
| 1423 | |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 1424 | if (svm_sev_enabled()) { |
Andy Shevchenko | a101c9d63 | 2018-08-30 14:49:59 +0300 | [diff] [blame] | 1425 | bitmap_free(sev_asid_bitmap); |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 1426 | bitmap_free(sev_reclaim_asid_bitmap); |
| 1427 | |
| 1428 | sev_flush_asids(); |
| 1429 | } |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1430 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 1431 | for_each_possible_cpu(cpu) |
Joerg Roedel | 0da1db75 | 2008-07-02 16:02:11 +0200 | [diff] [blame] | 1432 | svm_cpu_uninit(cpu); |
| 1433 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1434 | __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 1435 | iopm_base = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | static void init_seg(struct vmcb_seg *seg) |
| 1439 | { |
| 1440 | seg->selector = 0; |
| 1441 | seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1442 | SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1443 | seg->limit = 0xffff; |
| 1444 | seg->base = 0; |
| 1445 | } |
| 1446 | |
| 1447 | static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) |
| 1448 | { |
| 1449 | seg->selector = 0; |
| 1450 | seg->attrib = SVM_SELECTOR_P_MASK | type; |
| 1451 | seg->limit = 0xffff; |
| 1452 | seg->base = 0; |
| 1453 | } |
| 1454 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 1455 | static u64 svm_read_l1_tsc_offset(struct kvm_vcpu *vcpu) |
| 1456 | { |
| 1457 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1458 | |
| 1459 | if (is_guest_mode(vcpu)) |
| 1460 | return svm->nested.hsave->control.tsc_offset; |
| 1461 | |
| 1462 | return vcpu->arch.tsc_offset; |
| 1463 | } |
| 1464 | |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 1465 | static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1466 | { |
| 1467 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1468 | u64 g_tsc_offset = 0; |
| 1469 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 1470 | if (is_guest_mode(vcpu)) { |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 1471 | /* Write L1's TSC offset. */ |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1472 | g_tsc_offset = svm->vmcb->control.tsc_offset - |
| 1473 | svm->nested.hsave->control.tsc_offset; |
| 1474 | svm->nested.hsave->control.tsc_offset = offset; |
Paolo Bonzini | 45c3af9 | 2018-11-25 18:45:35 +0100 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 1478 | svm->vmcb->control.tsc_offset - g_tsc_offset, |
| 1479 | offset); |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1480 | |
| 1481 | svm->vmcb->control.tsc_offset = offset + g_tsc_offset; |
Joerg Roedel | 116a0a2 | 2010-12-03 11:45:49 +0100 | [diff] [blame] | 1482 | |
| 1483 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 1484 | return svm->vmcb->control.tsc_offset; |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1485 | } |
| 1486 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1487 | static void avic_init_vmcb(struct vcpu_svm *svm) |
| 1488 | { |
| 1489 | struct vmcb *vmcb = svm->vmcb; |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1490 | struct kvm_svm *kvm_svm = to_kvm_svm(svm->vcpu.kvm); |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1491 | phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page)); |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1492 | phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page)); |
| 1493 | phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page)); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1494 | |
| 1495 | vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK; |
| 1496 | vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK; |
| 1497 | vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK; |
| 1498 | vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT; |
| 1499 | vmcb->control.int_ctl |= AVIC_ENABLE_MASK; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1500 | } |
| 1501 | |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 1502 | static void init_vmcb(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1503 | { |
Joerg Roedel | e6101a9 | 2008-02-13 18:58:45 +0100 | [diff] [blame] | 1504 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 1505 | struct vmcb_save_area *save = &svm->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1506 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 1507 | svm->vcpu.arch.hflags = 0; |
Avi Kivity | bff7827 | 2010-01-07 13:16:08 +0200 | [diff] [blame] | 1508 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 1509 | set_cr_intercept(svm, INTERCEPT_CR0_READ); |
| 1510 | set_cr_intercept(svm, INTERCEPT_CR3_READ); |
| 1511 | set_cr_intercept(svm, INTERCEPT_CR4_READ); |
| 1512 | set_cr_intercept(svm, INTERCEPT_CR0_WRITE); |
| 1513 | set_cr_intercept(svm, INTERCEPT_CR3_WRITE); |
| 1514 | set_cr_intercept(svm, INTERCEPT_CR4_WRITE); |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 1515 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) |
| 1516 | set_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1517 | |
Paolo Bonzini | 5315c71 | 2014-03-03 13:08:29 +0100 | [diff] [blame] | 1518 | set_dr_intercepts(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1519 | |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 1520 | set_exception_intercept(svm, PF_VECTOR); |
| 1521 | set_exception_intercept(svm, UD_VECTOR); |
| 1522 | set_exception_intercept(svm, MC_VECTOR); |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 1523 | set_exception_intercept(svm, AC_VECTOR); |
Paolo Bonzini | cbdb967 | 2015-11-10 09:14:39 +0100 | [diff] [blame] | 1524 | set_exception_intercept(svm, DB_VECTOR); |
Liran Alon | 9718420 | 2018-03-12 13:12:52 +0200 | [diff] [blame] | 1525 | /* |
| 1526 | * Guest access to VMware backdoor ports could legitimately |
| 1527 | * trigger #GP because of TSS I/O permission bitmap. |
| 1528 | * We intercept those #GP and allow access to them anyway |
| 1529 | * as VMware does. |
| 1530 | */ |
| 1531 | if (enable_vmware_backdoor) |
| 1532 | set_exception_intercept(svm, GP_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1533 | |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1534 | set_intercept(svm, INTERCEPT_INTR); |
| 1535 | set_intercept(svm, INTERCEPT_NMI); |
| 1536 | set_intercept(svm, INTERCEPT_SMI); |
| 1537 | set_intercept(svm, INTERCEPT_SELECTIVE_CR0); |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 1538 | set_intercept(svm, INTERCEPT_RDPMC); |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1539 | set_intercept(svm, INTERCEPT_CPUID); |
| 1540 | set_intercept(svm, INTERCEPT_INVD); |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1541 | set_intercept(svm, INTERCEPT_INVLPG); |
| 1542 | set_intercept(svm, INTERCEPT_INVLPGA); |
| 1543 | set_intercept(svm, INTERCEPT_IOIO_PROT); |
| 1544 | set_intercept(svm, INTERCEPT_MSR_PROT); |
| 1545 | set_intercept(svm, INTERCEPT_TASK_SWITCH); |
| 1546 | set_intercept(svm, INTERCEPT_SHUTDOWN); |
| 1547 | set_intercept(svm, INTERCEPT_VMRUN); |
| 1548 | set_intercept(svm, INTERCEPT_VMMCALL); |
| 1549 | set_intercept(svm, INTERCEPT_VMLOAD); |
| 1550 | set_intercept(svm, INTERCEPT_VMSAVE); |
| 1551 | set_intercept(svm, INTERCEPT_STGI); |
| 1552 | set_intercept(svm, INTERCEPT_CLGI); |
| 1553 | set_intercept(svm, INTERCEPT_SKINIT); |
| 1554 | set_intercept(svm, INTERCEPT_WBINVD); |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 1555 | set_intercept(svm, INTERCEPT_XSETBV); |
Jim Mattson | 0cb8410 | 2019-09-19 15:59:17 -0700 | [diff] [blame] | 1556 | set_intercept(svm, INTERCEPT_RDPRU); |
Brijesh Singh | 7607b71 | 2018-02-19 10:14:44 -0600 | [diff] [blame] | 1557 | set_intercept(svm, INTERCEPT_RSM); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1558 | |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 1559 | if (!kvm_mwait_in_guest(svm->vcpu.kvm)) { |
Michael S. Tsirkin | 668fffa | 2017-04-21 12:27:17 +0200 | [diff] [blame] | 1560 | set_intercept(svm, INTERCEPT_MONITOR); |
| 1561 | set_intercept(svm, INTERCEPT_MWAIT); |
| 1562 | } |
| 1563 | |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 1564 | if (!kvm_hlt_in_guest(svm->vcpu.kvm)) |
| 1565 | set_intercept(svm, INTERCEPT_HLT); |
| 1566 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1567 | control->iopm_base_pa = __sme_set(iopm_base); |
| 1568 | control->msrpm_base_pa = __sme_set(__pa(svm->msrpm)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1569 | control->int_ctl = V_INTR_MASKING_MASK; |
| 1570 | |
| 1571 | init_seg(&save->es); |
| 1572 | init_seg(&save->ss); |
| 1573 | init_seg(&save->ds); |
| 1574 | init_seg(&save->fs); |
| 1575 | init_seg(&save->gs); |
| 1576 | |
| 1577 | save->cs.selector = 0xf000; |
Paolo Bonzini | 04b6683 | 2013-03-19 16:30:26 +0100 | [diff] [blame] | 1578 | save->cs.base = 0xffff0000; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1579 | /* Executable/Readable Code Segment */ |
| 1580 | save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | |
| 1581 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; |
| 1582 | save->cs.limit = 0xffff; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1583 | |
| 1584 | save->gdtr.limit = 0xffff; |
| 1585 | save->idtr.limit = 0xffff; |
| 1586 | |
| 1587 | init_sys_seg(&save->ldtr, SEG_TYPE_LDT); |
| 1588 | init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); |
| 1589 | |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 1590 | svm_set_efer(&svm->vcpu, 0); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1591 | save->dr6 = 0xffff0ff0; |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 1592 | kvm_set_rflags(&svm->vcpu, 2); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1593 | save->rip = 0x0000fff0; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1594 | svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1595 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 1596 | /* |
Eduardo Habkost | 18fa000 | 2009-10-24 02:49:59 -0200 | [diff] [blame] | 1597 | * 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] | 1598 | * It also updates the guest-visible cr0 value. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1599 | */ |
Paolo Bonzini | 79a8059 | 2015-09-21 07:46:55 +0200 | [diff] [blame] | 1600 | 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] | 1601 | kvm_mmu_reset_context(&svm->vcpu); |
Eduardo Habkost | 18fa000 | 2009-10-24 02:49:59 -0200 | [diff] [blame] | 1602 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1603 | save->cr4 = X86_CR4_PAE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1604 | /* rdx = ?? */ |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 1605 | |
| 1606 | if (npt_enabled) { |
| 1607 | /* Setup VMCB for Nested Paging */ |
Tom Lendacky | cea3a19 | 2017-12-04 10:57:24 -0600 | [diff] [blame] | 1608 | control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1609 | clr_intercept(svm, INTERCEPT_INVLPG); |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 1610 | clr_exception_intercept(svm, PF_VECTOR); |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 1611 | clr_cr_intercept(svm, INTERCEPT_CR3_READ); |
| 1612 | clr_cr_intercept(svm, INTERCEPT_CR3_WRITE); |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 1613 | save->g_pat = svm->vcpu.arch.pat; |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 1614 | save->cr3 = 0; |
| 1615 | save->cr4 = 0; |
| 1616 | } |
Joerg Roedel | f40f6a4 | 2010-12-03 15:25:15 +0100 | [diff] [blame] | 1617 | svm->asid_generation = 0; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 1618 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 1619 | svm->nested.vmcb = 0; |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 1620 | svm->vcpu.arch.hflags = 0; |
| 1621 | |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 1622 | if (pause_filter_count) { |
| 1623 | control->pause_filter_count = pause_filter_count; |
| 1624 | if (pause_filter_thresh) |
| 1625 | control->pause_filter_thresh = pause_filter_thresh; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 1626 | set_intercept(svm, INTERCEPT_PAUSE); |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 1627 | } else { |
| 1628 | clr_intercept(svm, INTERCEPT_PAUSE); |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 1629 | } |
| 1630 | |
Suravee Suthikulpanit | 67034bb | 2017-09-12 10:42:42 -0500 | [diff] [blame] | 1631 | if (kvm_vcpu_apicv_active(&svm->vcpu)) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1632 | avic_init_vmcb(svm); |
| 1633 | |
Janakarajan Natarajan | 89c8a49 | 2017-07-06 15:50:47 -0500 | [diff] [blame] | 1634 | /* |
| 1635 | * If hardware supports Virtual VMLOAD VMSAVE then enable it |
| 1636 | * in VMCB and clear intercepts to avoid #VMEXIT. |
| 1637 | */ |
| 1638 | if (vls) { |
| 1639 | clr_intercept(svm, INTERCEPT_VMLOAD); |
| 1640 | clr_intercept(svm, INTERCEPT_VMSAVE); |
| 1641 | svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK; |
| 1642 | } |
| 1643 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 1644 | if (vgif) { |
| 1645 | clr_intercept(svm, INTERCEPT_STGI); |
| 1646 | clr_intercept(svm, INTERCEPT_CLGI); |
| 1647 | svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK; |
| 1648 | } |
| 1649 | |
Brijesh Singh | 35c6f649 | 2017-12-04 10:57:39 -0600 | [diff] [blame] | 1650 | if (sev_guest(svm->vcpu.kvm)) { |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1651 | svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE; |
Brijesh Singh | 35c6f649 | 2017-12-04 10:57:39 -0600 | [diff] [blame] | 1652 | clr_exception_intercept(svm, UD_VECTOR); |
| 1653 | } |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1654 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 1655 | mark_all_dirty(svm->vmcb); |
| 1656 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 1657 | enable_gif(svm); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1658 | |
| 1659 | } |
| 1660 | |
Dan Carpenter | d3e7dec | 2017-05-18 10:38:53 +0300 | [diff] [blame] | 1661 | static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu, |
| 1662 | unsigned int index) |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1663 | { |
| 1664 | u64 *avic_physical_id_table; |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1665 | struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1666 | |
| 1667 | if (index >= AVIC_MAX_PHYSICAL_ID_COUNT) |
| 1668 | return NULL; |
| 1669 | |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1670 | avic_physical_id_table = page_address(kvm_svm->avic_physical_id_table_page); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1671 | |
| 1672 | return &avic_physical_id_table[index]; |
| 1673 | } |
| 1674 | |
| 1675 | /** |
| 1676 | * Note: |
| 1677 | * AVIC hardware walks the nested page table to check permissions, |
| 1678 | * but does not use the SPA address specified in the leaf page |
| 1679 | * table entry since it uses address in the AVIC_BACKING_PAGE pointer |
| 1680 | * field of the VMCB. Therefore, we set up the |
| 1681 | * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here. |
| 1682 | */ |
| 1683 | static int avic_init_access_page(struct kvm_vcpu *vcpu) |
| 1684 | { |
| 1685 | struct kvm *kvm = vcpu->kvm; |
Wei Wang | 3051038 | 2018-11-12 12:23:14 +0000 | [diff] [blame] | 1686 | int ret = 0; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1687 | |
Wei Wang | 3051038 | 2018-11-12 12:23:14 +0000 | [diff] [blame] | 1688 | mutex_lock(&kvm->slots_lock); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1689 | if (kvm->arch.apic_access_page_done) |
Wei Wang | 3051038 | 2018-11-12 12:23:14 +0000 | [diff] [blame] | 1690 | goto out; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1691 | |
Wei Wang | 3051038 | 2018-11-12 12:23:14 +0000 | [diff] [blame] | 1692 | ret = __x86_set_memory_region(kvm, |
| 1693 | APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 1694 | APIC_DEFAULT_PHYS_BASE, |
| 1695 | PAGE_SIZE); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1696 | if (ret) |
Wei Wang | 3051038 | 2018-11-12 12:23:14 +0000 | [diff] [blame] | 1697 | goto out; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1698 | |
| 1699 | kvm->arch.apic_access_page_done = true; |
Wei Wang | 3051038 | 2018-11-12 12:23:14 +0000 | [diff] [blame] | 1700 | out: |
| 1701 | mutex_unlock(&kvm->slots_lock); |
| 1702 | return ret; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | static int avic_init_backing_page(struct kvm_vcpu *vcpu) |
| 1706 | { |
| 1707 | int ret; |
| 1708 | u64 *entry, new_entry; |
| 1709 | int id = vcpu->vcpu_id; |
| 1710 | struct vcpu_svm *svm = to_svm(vcpu); |
| 1711 | |
| 1712 | ret = avic_init_access_page(vcpu); |
| 1713 | if (ret) |
| 1714 | return ret; |
| 1715 | |
| 1716 | if (id >= AVIC_MAX_PHYSICAL_ID_COUNT) |
| 1717 | return -EINVAL; |
| 1718 | |
| 1719 | if (!svm->vcpu.arch.apic->regs) |
| 1720 | return -EINVAL; |
| 1721 | |
| 1722 | svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs); |
| 1723 | |
| 1724 | /* Setting AVIC backing page address in the phy APIC ID table */ |
| 1725 | entry = avic_get_physical_id_entry(vcpu, id); |
| 1726 | if (!entry) |
| 1727 | return -EINVAL; |
| 1728 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 1729 | new_entry = __sme_set((page_to_phys(svm->avic_backing_page) & |
| 1730 | AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) | |
| 1731 | AVIC_PHYSICAL_ID_ENTRY_VALID_MASK); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1732 | WRITE_ONCE(*entry, new_entry); |
| 1733 | |
| 1734 | svm->avic_physical_id_cache = entry; |
| 1735 | |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
Tom Lendacky | e3b9a9e | 2019-10-03 21:17:43 +0000 | [diff] [blame] | 1739 | static void sev_asid_free(int asid) |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1740 | { |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1741 | struct svm_cpu_data *sd; |
| 1742 | int cpu, pos; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1743 | |
Tom Lendacky | e3b9a9e | 2019-10-03 21:17:43 +0000 | [diff] [blame] | 1744 | mutex_lock(&sev_bitmap_lock); |
| 1745 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1746 | pos = asid - 1; |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 1747 | __set_bit(pos, sev_reclaim_asid_bitmap); |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1748 | |
| 1749 | for_each_possible_cpu(cpu) { |
| 1750 | sd = per_cpu(svm_data, cpu); |
| 1751 | sd->sev_vmcbs[pos] = NULL; |
| 1752 | } |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1753 | |
Tom Lendacky | e3b9a9e | 2019-10-03 21:17:43 +0000 | [diff] [blame] | 1754 | mutex_unlock(&sev_bitmap_lock); |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1755 | } |
| 1756 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1757 | static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) |
| 1758 | { |
| 1759 | struct sev_data_decommission *decommission; |
| 1760 | struct sev_data_deactivate *data; |
| 1761 | |
| 1762 | if (!handle) |
| 1763 | return; |
| 1764 | |
| 1765 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 1766 | if (!data) |
| 1767 | return; |
| 1768 | |
| 1769 | /* deactivate handle */ |
| 1770 | data->handle = handle; |
Tom Lendacky | 83af5e6 | 2019-10-03 21:17:45 +0000 | [diff] [blame] | 1771 | |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 1772 | /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */ |
| 1773 | down_read(&sev_deactivate_lock); |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1774 | sev_guest_deactivate(data, NULL); |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 1775 | up_read(&sev_deactivate_lock); |
Tom Lendacky | 83af5e6 | 2019-10-03 21:17:45 +0000 | [diff] [blame] | 1776 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1777 | kfree(data); |
| 1778 | |
| 1779 | decommission = kzalloc(sizeof(*decommission), GFP_KERNEL); |
| 1780 | if (!decommission) |
| 1781 | return; |
| 1782 | |
| 1783 | /* decommission handle */ |
| 1784 | decommission->handle = handle; |
| 1785 | sev_guest_decommission(decommission, NULL); |
| 1786 | |
| 1787 | kfree(decommission); |
| 1788 | } |
| 1789 | |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1790 | static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr, |
| 1791 | unsigned long ulen, unsigned long *n, |
| 1792 | int write) |
| 1793 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1794 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1795 | unsigned long npages, npinned, size; |
| 1796 | unsigned long locked, lock_limit; |
| 1797 | struct page **pages; |
Dan Carpenter | 86bf20c | 2018-05-19 09:01:36 +0300 | [diff] [blame] | 1798 | unsigned long first, last; |
| 1799 | |
| 1800 | if (ulen == 0 || uaddr + ulen < uaddr) |
| 1801 | return NULL; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1802 | |
| 1803 | /* Calculate number of pages. */ |
| 1804 | first = (uaddr & PAGE_MASK) >> PAGE_SHIFT; |
| 1805 | last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT; |
| 1806 | npages = (last - first + 1); |
| 1807 | |
| 1808 | locked = sev->pages_locked + npages; |
| 1809 | lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 1810 | if (locked > lock_limit && !capable(CAP_IPC_LOCK)) { |
| 1811 | pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit); |
| 1812 | return NULL; |
| 1813 | } |
| 1814 | |
| 1815 | /* Avoid using vmalloc for smaller buffers. */ |
| 1816 | size = npages * sizeof(struct page *); |
| 1817 | if (size > PAGE_SIZE) |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 1818 | pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO, |
| 1819 | PAGE_KERNEL); |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1820 | else |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 1821 | pages = kmalloc(size, GFP_KERNEL_ACCOUNT); |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1822 | |
| 1823 | if (!pages) |
| 1824 | return NULL; |
| 1825 | |
| 1826 | /* Pin the user virtual address. */ |
Ira Weiny | 73b0140 | 2019-05-13 17:17:11 -0700 | [diff] [blame] | 1827 | npinned = get_user_pages_fast(uaddr, npages, FOLL_WRITE, pages); |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1828 | if (npinned != npages) { |
| 1829 | pr_err("SEV: Failure locking %lu pages.\n", npages); |
| 1830 | goto err; |
| 1831 | } |
| 1832 | |
| 1833 | *n = npages; |
| 1834 | sev->pages_locked = locked; |
| 1835 | |
| 1836 | return pages; |
| 1837 | |
| 1838 | err: |
| 1839 | if (npinned > 0) |
| 1840 | release_pages(pages, npinned); |
| 1841 | |
| 1842 | kvfree(pages); |
| 1843 | return NULL; |
| 1844 | } |
| 1845 | |
| 1846 | static void sev_unpin_memory(struct kvm *kvm, struct page **pages, |
| 1847 | unsigned long npages) |
| 1848 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1849 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1850 | |
| 1851 | release_pages(pages, npages); |
| 1852 | kvfree(pages); |
| 1853 | sev->pages_locked -= npages; |
| 1854 | } |
| 1855 | |
| 1856 | static void sev_clflush_pages(struct page *pages[], unsigned long npages) |
| 1857 | { |
| 1858 | uint8_t *page_virtual; |
| 1859 | unsigned long i; |
| 1860 | |
| 1861 | if (npages == 0 || pages == NULL) |
| 1862 | return; |
| 1863 | |
| 1864 | for (i = 0; i < npages; i++) { |
| 1865 | page_virtual = kmap_atomic(pages[i]); |
| 1866 | clflush_cache_range(page_virtual, PAGE_SIZE); |
| 1867 | kunmap_atomic(page_virtual); |
| 1868 | } |
| 1869 | } |
| 1870 | |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 1871 | static void __unregister_enc_region_locked(struct kvm *kvm, |
| 1872 | struct enc_region *region) |
| 1873 | { |
| 1874 | /* |
| 1875 | * The guest may change the memory encryption attribute from C=0 -> C=1 |
| 1876 | * or vice versa for this memory range. Lets make sure caches are |
| 1877 | * flushed to ensure that guest data gets written into memory with |
| 1878 | * correct C-bit. |
| 1879 | */ |
| 1880 | sev_clflush_pages(region->pages, region->npages); |
| 1881 | |
| 1882 | sev_unpin_memory(kvm, region->pages, region->npages); |
| 1883 | list_del(®ion->list); |
| 1884 | kfree(region); |
| 1885 | } |
| 1886 | |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 1887 | static struct kvm *svm_vm_alloc(void) |
| 1888 | { |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 1889 | struct kvm_svm *kvm_svm = __vmalloc(sizeof(struct kvm_svm), |
| 1890 | GFP_KERNEL_ACCOUNT | __GFP_ZERO, |
| 1891 | PAGE_KERNEL); |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1892 | return &kvm_svm->kvm; |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | static void svm_vm_free(struct kvm *kvm) |
| 1896 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 1897 | vfree(to_kvm_svm(kvm)); |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1900 | static void sev_vm_destroy(struct kvm *kvm) |
| 1901 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1902 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 1903 | struct list_head *head = &sev->regions_list; |
| 1904 | struct list_head *pos, *q; |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1905 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1906 | if (!sev_guest(kvm)) |
| 1907 | return; |
| 1908 | |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 1909 | mutex_lock(&kvm->lock); |
| 1910 | |
| 1911 | /* |
| 1912 | * if userspace was terminated before unregistering the memory regions |
| 1913 | * then lets unpin all the registered memory. |
| 1914 | */ |
| 1915 | if (!list_empty(head)) { |
| 1916 | list_for_each_safe(pos, q, head) { |
| 1917 | __unregister_enc_region_locked(kvm, |
| 1918 | list_entry(pos, struct enc_region, list)); |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | mutex_unlock(&kvm->lock); |
| 1923 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 1924 | sev_unbind_asid(kvm, sev->handle); |
Tom Lendacky | e3b9a9e | 2019-10-03 21:17:43 +0000 | [diff] [blame] | 1925 | sev_asid_free(sev->asid); |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1926 | } |
| 1927 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1928 | static void avic_vm_destroy(struct kvm *kvm) |
| 1929 | { |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1930 | unsigned long flags; |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1931 | struct kvm_svm *kvm_svm = to_kvm_svm(kvm); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1932 | |
Dmitry Vyukov | 3863dff | 2017-01-24 14:06:48 +0100 | [diff] [blame] | 1933 | if (!avic) |
| 1934 | return; |
| 1935 | |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1936 | if (kvm_svm->avic_logical_id_table_page) |
| 1937 | __free_page(kvm_svm->avic_logical_id_table_page); |
| 1938 | if (kvm_svm->avic_physical_id_table_page) |
| 1939 | __free_page(kvm_svm->avic_physical_id_table_page); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1940 | |
| 1941 | spin_lock_irqsave(&svm_vm_data_hash_lock, flags); |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1942 | hash_del(&kvm_svm->hnode); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1943 | spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1944 | } |
| 1945 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 1946 | static void svm_vm_destroy(struct kvm *kvm) |
| 1947 | { |
| 1948 | avic_vm_destroy(kvm); |
| 1949 | sev_vm_destroy(kvm); |
| 1950 | } |
| 1951 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1952 | static int avic_vm_init(struct kvm *kvm) |
| 1953 | { |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1954 | unsigned long flags; |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1955 | int err = -ENOMEM; |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1956 | struct kvm_svm *kvm_svm = to_kvm_svm(kvm); |
| 1957 | struct kvm_svm *k2; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1958 | struct page *p_page; |
| 1959 | struct page *l_page; |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1960 | u32 vm_id; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1961 | |
| 1962 | if (!avic) |
| 1963 | return 0; |
| 1964 | |
| 1965 | /* Allocating physical APIC ID table (4KB) */ |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 1966 | p_page = alloc_page(GFP_KERNEL_ACCOUNT); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1967 | if (!p_page) |
| 1968 | goto free_avic; |
| 1969 | |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1970 | kvm_svm->avic_physical_id_table_page = p_page; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1971 | clear_page(page_address(p_page)); |
| 1972 | |
| 1973 | /* Allocating logical APIC ID table (4KB) */ |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 1974 | l_page = alloc_page(GFP_KERNEL_ACCOUNT); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1975 | if (!l_page) |
| 1976 | goto free_avic; |
| 1977 | |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1978 | kvm_svm->avic_logical_id_table_page = l_page; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1979 | clear_page(page_address(l_page)); |
| 1980 | |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1981 | spin_lock_irqsave(&svm_vm_data_hash_lock, flags); |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1982 | again: |
| 1983 | vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK; |
| 1984 | if (vm_id == 0) { /* id is 1-based, zero is not okay */ |
| 1985 | next_vm_id_wrapped = 1; |
| 1986 | goto again; |
| 1987 | } |
| 1988 | /* Is it still in use? Only possible if wrapped at least once */ |
| 1989 | if (next_vm_id_wrapped) { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1990 | hash_for_each_possible(svm_vm_data_hash, k2, hnode, vm_id) { |
| 1991 | if (k2->avic_vm_id == vm_id) |
Denys Vlasenko | 3f0d4db | 2017-08-11 22:11:58 +0200 | [diff] [blame] | 1992 | goto again; |
| 1993 | } |
| 1994 | } |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 1995 | kvm_svm->avic_vm_id = vm_id; |
| 1996 | hash_add(svm_vm_data_hash, &kvm_svm->hnode, kvm_svm->avic_vm_id); |
Suravee Suthikulpanit | 5881f73 | 2016-08-23 13:52:42 -0500 | [diff] [blame] | 1997 | spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); |
| 1998 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 1999 | return 0; |
| 2000 | |
| 2001 | free_avic: |
| 2002 | avic_vm_destroy(kvm); |
| 2003 | return err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2004 | } |
| 2005 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 2006 | static inline int |
| 2007 | 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] | 2008 | { |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 2009 | int ret = 0; |
| 2010 | unsigned long flags; |
| 2011 | struct amd_svm_iommu_ir *ir; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2012 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2013 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 2014 | if (!kvm_arch_has_assigned_device(vcpu->kvm)) |
| 2015 | return 0; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2016 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 2017 | /* |
| 2018 | * Here, we go through the per-vcpu ir_list to update all existing |
| 2019 | * interrupt remapping table entry targeting this vcpu. |
| 2020 | */ |
| 2021 | spin_lock_irqsave(&svm->ir_list_lock, flags); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2022 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 2023 | if (list_empty(&svm->ir_list)) |
| 2024 | goto out; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2025 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 2026 | list_for_each_entry(ir, &svm->ir_list, node) { |
| 2027 | ret = amd_iommu_update_ga(cpu, r, ir->data); |
| 2028 | if (ret) |
| 2029 | break; |
| 2030 | } |
| 2031 | out: |
| 2032 | spin_unlock_irqrestore(&svm->ir_list_lock, flags); |
| 2033 | return ret; |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 2037 | { |
| 2038 | u64 entry; |
| 2039 | /* ID = 0xff (broadcast), ID > 0xff (reserved) */ |
Suravee Suthikulpanit | 7d669f5 | 2016-06-15 17:23:45 -0500 | [diff] [blame] | 2040 | int h_physical_id = kvm_cpu_get_apicid(cpu); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2041 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2042 | |
| 2043 | if (!kvm_vcpu_apicv_active(vcpu)) |
| 2044 | return; |
| 2045 | |
Suthikulpanit, Suravee | c9bcd3e | 2019-05-14 15:49:52 +0000 | [diff] [blame] | 2046 | /* |
| 2047 | * Since the host physical APIC id is 8 bits, |
| 2048 | * we can support host APIC ID upto 255. |
| 2049 | */ |
| 2050 | if (WARN_ON(h_physical_id > AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK)) |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2051 | return; |
| 2052 | |
| 2053 | entry = READ_ONCE(*(svm->avic_physical_id_cache)); |
| 2054 | WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); |
| 2055 | |
| 2056 | entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; |
| 2057 | entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); |
| 2058 | |
| 2059 | entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; |
| 2060 | if (svm->avic_is_running) |
| 2061 | entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; |
| 2062 | |
| 2063 | WRITE_ONCE(*(svm->avic_physical_id_cache), entry); |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 2064 | avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, |
| 2065 | svm->avic_is_running); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | static void avic_vcpu_put(struct kvm_vcpu *vcpu) |
| 2069 | { |
| 2070 | u64 entry; |
| 2071 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2072 | |
| 2073 | if (!kvm_vcpu_apicv_active(vcpu)) |
| 2074 | return; |
| 2075 | |
| 2076 | entry = READ_ONCE(*(svm->avic_physical_id_cache)); |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 2077 | if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) |
| 2078 | avic_update_iommu_vcpu_affinity(vcpu, -1, 0); |
| 2079 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2080 | entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; |
| 2081 | WRITE_ONCE(*(svm->avic_physical_id_cache), entry); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2082 | } |
| 2083 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 2084 | /** |
| 2085 | * This function is called during VCPU halt/unhalt. |
| 2086 | */ |
| 2087 | static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run) |
| 2088 | { |
| 2089 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2090 | |
| 2091 | svm->avic_is_running = is_run; |
| 2092 | if (is_run) |
| 2093 | avic_vcpu_load(vcpu, vcpu->cpu); |
| 2094 | else |
| 2095 | avic_vcpu_put(vcpu); |
| 2096 | } |
| 2097 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 2098 | static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2099 | { |
| 2100 | struct vcpu_svm *svm = to_svm(vcpu); |
Julian Stecklina | 66f7b72 | 2012-12-05 15:26:19 +0100 | [diff] [blame] | 2101 | u32 dummy; |
| 2102 | u32 eax = 1; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2103 | |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 2104 | vcpu->arch.microcode_version = 0x01000065; |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 2105 | svm->spec_ctrl = 0; |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 2106 | svm->virt_spec_ctrl = 0; |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 2107 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 2108 | if (!init_event) { |
| 2109 | svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE | |
| 2110 | MSR_IA32_APICBASE_ENABLE; |
| 2111 | if (kvm_vcpu_is_reset_bsp(&svm->vcpu)) |
| 2112 | svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP; |
| 2113 | } |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 2114 | init_vmcb(svm); |
Avi Kivity | 7043338 | 2007-11-07 12:57:23 +0200 | [diff] [blame] | 2115 | |
Yu Zhang | e911eb3 | 2017-08-24 20:27:52 +0800 | [diff] [blame] | 2116 | kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true); |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 2117 | kvm_rdx_write(vcpu, eax); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 2118 | |
| 2119 | if (kvm_vcpu_apicv_active(vcpu) && !init_event) |
| 2120 | avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2121 | } |
| 2122 | |
Suravee Suthikulpanit | dfa2009 | 2017-09-12 10:42:40 -0500 | [diff] [blame] | 2123 | static int avic_init_vcpu(struct vcpu_svm *svm) |
| 2124 | { |
| 2125 | int ret; |
| 2126 | |
Suravee Suthikulpanit | 67034bb | 2017-09-12 10:42:42 -0500 | [diff] [blame] | 2127 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) |
Suravee Suthikulpanit | dfa2009 | 2017-09-12 10:42:40 -0500 | [diff] [blame] | 2128 | return 0; |
| 2129 | |
| 2130 | ret = avic_init_backing_page(&svm->vcpu); |
| 2131 | if (ret) |
| 2132 | return ret; |
| 2133 | |
| 2134 | INIT_LIST_HEAD(&svm->ir_list); |
| 2135 | spin_lock_init(&svm->ir_list_lock); |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 2136 | svm->dfr_reg = APIC_DFR_FLAT; |
Suravee Suthikulpanit | dfa2009 | 2017-09-12 10:42:40 -0500 | [diff] [blame] | 2137 | |
| 2138 | return ret; |
| 2139 | } |
| 2140 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2141 | 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] | 2142 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2143 | struct vcpu_svm *svm; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2144 | struct page *page; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 2145 | struct page *msrpm_pages; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2146 | struct page *hsave_page; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2147 | struct page *nested_msrpm_pages; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2148 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2149 | |
Sean Christopherson | 12b58f4 | 2019-08-15 10:22:37 -0700 | [diff] [blame] | 2150 | BUILD_BUG_ON_MSG(offsetof(struct vcpu_svm, vcpu) != 0, |
| 2151 | "struct kvm_vcpu must be at offset 0 for arch usercopy region"); |
| 2152 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 2153 | svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2154 | if (!svm) { |
| 2155 | err = -ENOMEM; |
| 2156 | goto out; |
| 2157 | } |
| 2158 | |
Wanpeng Li | d9a710e | 2019-07-22 12:26:21 +0800 | [diff] [blame] | 2159 | svm->vcpu.arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache, |
| 2160 | GFP_KERNEL_ACCOUNT); |
| 2161 | if (!svm->vcpu.arch.user_fpu) { |
| 2162 | printk(KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n"); |
| 2163 | err = -ENOMEM; |
| 2164 | goto free_partial_svm; |
| 2165 | } |
| 2166 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 2167 | svm->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache, |
| 2168 | GFP_KERNEL_ACCOUNT); |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 2169 | if (!svm->vcpu.arch.guest_fpu) { |
| 2170 | printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n"); |
| 2171 | err = -ENOMEM; |
Wanpeng Li | d9a710e | 2019-07-22 12:26:21 +0800 | [diff] [blame] | 2172 | goto free_user_fpu; |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 2173 | } |
| 2174 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2175 | err = kvm_vcpu_init(&svm->vcpu, kvm, id); |
| 2176 | if (err) |
| 2177 | goto free_svm; |
| 2178 | |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 2179 | err = -ENOMEM; |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 2180 | page = alloc_page(GFP_KERNEL_ACCOUNT); |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 2181 | if (!page) |
| 2182 | goto uninit; |
| 2183 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 2184 | msrpm_pages = alloc_pages(GFP_KERNEL_ACCOUNT, MSRPM_ALLOC_ORDER); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 2185 | if (!msrpm_pages) |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 2186 | goto free_page1; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2187 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 2188 | nested_msrpm_pages = alloc_pages(GFP_KERNEL_ACCOUNT, MSRPM_ALLOC_ORDER); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2189 | if (!nested_msrpm_pages) |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 2190 | goto free_page2; |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 2191 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 2192 | hsave_page = alloc_page(GFP_KERNEL_ACCOUNT); |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2193 | if (!hsave_page) |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 2194 | goto free_page3; |
| 2195 | |
Suravee Suthikulpanit | dfa2009 | 2017-09-12 10:42:40 -0500 | [diff] [blame] | 2196 | err = avic_init_vcpu(svm); |
| 2197 | if (err) |
| 2198 | goto free_page4; |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 2199 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2200 | /* We initialize this flag to true to make sure that the is_running |
| 2201 | * bit would be set the first time the vcpu is loaded. |
| 2202 | */ |
| 2203 | svm->avic_is_running = true; |
| 2204 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 2205 | svm->nested.hsave = page_address(hsave_page); |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 2206 | |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 2207 | svm->msrpm = page_address(msrpm_pages); |
| 2208 | svm_vcpu_init_msrpm(svm->msrpm); |
| 2209 | |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 2210 | svm->nested.msrpm = page_address(nested_msrpm_pages); |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 2211 | svm_vcpu_init_msrpm(svm->nested.msrpm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 2212 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2213 | svm->vmcb = page_address(page); |
| 2214 | clear_page(svm->vmcb); |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 2215 | svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2216 | svm->asid_generation = 0; |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 2217 | init_vmcb(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2218 | |
Boris Ostrovsky | 2b036c6 | 2012-01-09 14:00:35 -0500 | [diff] [blame] | 2219 | svm_init_osvw(&svm->vcpu); |
| 2220 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2221 | return &svm->vcpu; |
Avi Kivity | 36241b8 | 2006-12-22 01:05:20 -0800 | [diff] [blame] | 2222 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 2223 | free_page4: |
| 2224 | __free_page(hsave_page); |
Takuya Yoshikawa | b7af404 | 2010-03-09 14:55:19 +0900 | [diff] [blame] | 2225 | free_page3: |
| 2226 | __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER); |
| 2227 | free_page2: |
| 2228 | __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER); |
| 2229 | free_page1: |
| 2230 | __free_page(page); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2231 | uninit: |
| 2232 | kvm_vcpu_uninit(&svm->vcpu); |
| 2233 | free_svm: |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 2234 | kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.guest_fpu); |
Wanpeng Li | d9a710e | 2019-07-22 12:26:21 +0800 | [diff] [blame] | 2235 | free_user_fpu: |
| 2236 | kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.user_fpu); |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 2237 | free_partial_svm: |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 2238 | kmem_cache_free(kvm_vcpu_cache, svm); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2239 | out: |
| 2240 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2241 | } |
| 2242 | |
Jim Mattson | fd65d31 | 2018-05-22 09:54:20 -0700 | [diff] [blame] | 2243 | static void svm_clear_current_vmcb(struct vmcb *vmcb) |
| 2244 | { |
| 2245 | int i; |
| 2246 | |
| 2247 | for_each_online_cpu(i) |
| 2248 | cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL); |
| 2249 | } |
| 2250 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2251 | static void svm_free_vcpu(struct kvm_vcpu *vcpu) |
| 2252 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2253 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2254 | |
Jim Mattson | fd65d31 | 2018-05-22 09:54:20 -0700 | [diff] [blame] | 2255 | /* |
| 2256 | * The vmcb page can be recycled, causing a false negative in |
| 2257 | * svm_vcpu_load(). So, ensure that no logical CPU has this |
| 2258 | * vmcb page recorded as its current vmcb. |
| 2259 | */ |
| 2260 | svm_clear_current_vmcb(svm->vmcb); |
| 2261 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 2262 | __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT)); |
Joerg Roedel | f65c229 | 2008-02-13 18:58:46 +0100 | [diff] [blame] | 2263 | __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER); |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 2264 | __free_page(virt_to_page(svm->nested.hsave)); |
| 2265 | __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2266 | kvm_vcpu_uninit(vcpu); |
Wanpeng Li | d9a710e | 2019-07-22 12:26:21 +0800 | [diff] [blame] | 2267 | kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.user_fpu); |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 2268 | kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.guest_fpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 2269 | kmem_cache_free(kvm_vcpu_cache, svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2270 | } |
| 2271 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2272 | static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2273 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2274 | struct vcpu_svm *svm = to_svm(vcpu); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2275 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2276 | int i; |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 2277 | |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 2278 | if (unlikely(cpu != vcpu->cpu)) { |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 2279 | svm->asid_generation = 0; |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 2280 | mark_all_dirty(svm->vmcb); |
Avi Kivity | 0cc5064 | 2007-03-25 12:07:27 +0200 | [diff] [blame] | 2281 | } |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 2282 | |
Avi Kivity | 82ca2d1 | 2010-10-21 12:20:34 +0200 | [diff] [blame] | 2283 | #ifdef CONFIG_X86_64 |
| 2284 | rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base); |
| 2285 | #endif |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 2286 | savesegment(fs, svm->host.fs); |
| 2287 | savesegment(gs, svm->host.gs); |
| 2288 | svm->host.ldt = kvm_read_ldt(); |
| 2289 | |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 2290 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2291 | rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 2292 | |
Haozhong Zhang | ad721883 | 2015-10-20 15:39:02 +0800 | [diff] [blame] | 2293 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
| 2294 | u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio; |
| 2295 | if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) { |
| 2296 | __this_cpu_write(current_tsc_ratio, tsc_ratio); |
| 2297 | wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio); |
| 2298 | } |
Joerg Roedel | fbc0db7 | 2011-03-25 09:44:46 +0100 | [diff] [blame] | 2299 | } |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 2300 | /* This assumes that the kernel never uses MSR_TSC_AUX */ |
| 2301 | if (static_cpu_has(X86_FEATURE_RDTSCP)) |
| 2302 | wrmsrl(MSR_TSC_AUX, svm->tsc_aux); |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2303 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2304 | if (sd->current_vmcb != svm->vmcb) { |
| 2305 | sd->current_vmcb = svm->vmcb; |
| 2306 | indirect_branch_prediction_barrier(); |
| 2307 | } |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2308 | avic_vcpu_load(vcpu, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2309 | } |
| 2310 | |
| 2311 | static void svm_vcpu_put(struct kvm_vcpu *vcpu) |
| 2312 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2313 | struct vcpu_svm *svm = to_svm(vcpu); |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 2314 | int i; |
| 2315 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2316 | avic_vcpu_put(vcpu); |
| 2317 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2318 | ++vcpu->stat.host_state_reload; |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 2319 | kvm_load_ldt(svm->host.ldt); |
| 2320 | #ifdef CONFIG_X86_64 |
| 2321 | loadsegment(fs, svm->host.fs); |
Andy Lutomirski | 296f781 | 2016-04-26 12:23:29 -0700 | [diff] [blame] | 2322 | wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase); |
Joerg Roedel | 893a5ab | 2011-01-14 16:45:01 +0100 | [diff] [blame] | 2323 | load_gs_index(svm->host.gs); |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 2324 | #else |
Avi Kivity | 831ca60 | 2011-03-08 16:09:51 +0200 | [diff] [blame] | 2325 | #ifdef CONFIG_X86_32_LAZY_GS |
Avi Kivity | dacccfd | 2010-10-21 12:20:33 +0200 | [diff] [blame] | 2326 | loadsegment(gs, svm->host.gs); |
| 2327 | #endif |
Avi Kivity | 831ca60 | 2011-03-08 16:09:51 +0200 | [diff] [blame] | 2328 | #endif |
Anthony Liguori | 94dfbdb | 2007-04-29 11:56:06 +0300 | [diff] [blame] | 2329 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2330 | wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2331 | } |
| 2332 | |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 2333 | static void svm_vcpu_blocking(struct kvm_vcpu *vcpu) |
| 2334 | { |
| 2335 | avic_set_running(vcpu, false); |
| 2336 | } |
| 2337 | |
| 2338 | static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu) |
| 2339 | { |
| 2340 | avic_set_running(vcpu, true); |
| 2341 | } |
| 2342 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2343 | static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) |
| 2344 | { |
Ladi Prosek | 9b61174 | 2017-06-21 09:06:59 +0200 | [diff] [blame] | 2345 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2346 | unsigned long rflags = svm->vmcb->save.rflags; |
| 2347 | |
| 2348 | if (svm->nmi_singlestep) { |
| 2349 | /* Hide our flags if they were not set by the guest */ |
| 2350 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF)) |
| 2351 | rflags &= ~X86_EFLAGS_TF; |
| 2352 | if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF)) |
| 2353 | rflags &= ~X86_EFLAGS_RF; |
| 2354 | } |
| 2355 | return rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2359 | { |
Ladi Prosek | 9b61174 | 2017-06-21 09:06:59 +0200 | [diff] [blame] | 2360 | if (to_svm(vcpu)->nmi_singlestep) |
| 2361 | rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 2362 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 2363 | /* |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 2364 | * Any change of EFLAGS.VM is accompanied by a reload of SS |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 2365 | * (caused by either a task switch or an inter-privilege IRET), |
| 2366 | * so we do not need to update the CPL here. |
| 2367 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2368 | to_svm(vcpu)->vmcb->save.rflags = rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2369 | } |
| 2370 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2371 | static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
| 2372 | { |
| 2373 | switch (reg) { |
| 2374 | case VCPU_EXREG_PDPTR: |
| 2375 | BUG_ON(!npt_enabled); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 2376 | load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2377 | break; |
| 2378 | default: |
Sean Christopherson | 34059c2 | 2019-09-27 14:45:23 -0700 | [diff] [blame] | 2379 | WARN_ON_ONCE(1); |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2380 | } |
| 2381 | } |
| 2382 | |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 2383 | static void svm_set_vintr(struct vcpu_svm *svm) |
| 2384 | { |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 2385 | set_intercept(svm, INTERCEPT_VINTR); |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | static void svm_clear_vintr(struct vcpu_svm *svm) |
| 2389 | { |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 2390 | clr_intercept(svm, INTERCEPT_VINTR); |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 2391 | } |
| 2392 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2393 | static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) |
| 2394 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2395 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2396 | |
| 2397 | switch (seg) { |
| 2398 | case VCPU_SREG_CS: return &save->cs; |
| 2399 | case VCPU_SREG_DS: return &save->ds; |
| 2400 | case VCPU_SREG_ES: return &save->es; |
| 2401 | case VCPU_SREG_FS: return &save->fs; |
| 2402 | case VCPU_SREG_GS: return &save->gs; |
| 2403 | case VCPU_SREG_SS: return &save->ss; |
| 2404 | case VCPU_SREG_TR: return &save->tr; |
| 2405 | case VCPU_SREG_LDTR: return &save->ldtr; |
| 2406 | } |
| 2407 | BUG(); |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 2408 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2409 | } |
| 2410 | |
| 2411 | static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 2412 | { |
| 2413 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 2414 | |
| 2415 | return s->base; |
| 2416 | } |
| 2417 | |
| 2418 | static void svm_get_segment(struct kvm_vcpu *vcpu, |
| 2419 | struct kvm_segment *var, int seg) |
| 2420 | { |
| 2421 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 2422 | |
| 2423 | var->base = s->base; |
| 2424 | var->limit = s->limit; |
| 2425 | var->selector = s->selector; |
| 2426 | var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; |
| 2427 | var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; |
| 2428 | var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; |
| 2429 | var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; |
| 2430 | var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; |
| 2431 | var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; |
| 2432 | var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; |
Jim Mattson | 80112c8 | 2014-07-08 09:47:41 +0530 | [diff] [blame] | 2433 | |
| 2434 | /* |
| 2435 | * AMD CPUs circa 2014 track the G bit for all segments except CS. |
| 2436 | * However, the SVM spec states that the G bit is not observed by the |
| 2437 | * CPU, and some VMware virtual CPUs drop the G bit for all segments. |
| 2438 | * So let's synthesize a legal G bit for all segments, this helps |
| 2439 | * running KVM nested. It also helps cross-vendor migration, because |
| 2440 | * Intel's vmentry has a check on the 'G' bit. |
| 2441 | */ |
| 2442 | var->g = s->limit > 0xfffff; |
Amit Shah | 25022ac | 2008-10-27 09:04:17 +0000 | [diff] [blame] | 2443 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2444 | /* |
| 2445 | * 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] | 2446 | * for cross vendor migration purposes by "not present" |
| 2447 | */ |
Gioh Kim | 8eae957 | 2017-05-30 15:24:45 +0200 | [diff] [blame] | 2448 | var->unusable = !var->present; |
Andre Przywara | 19bca6a | 2009-04-28 12:45:30 +0200 | [diff] [blame] | 2449 | |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 2450 | switch (seg) { |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 2451 | case VCPU_SREG_TR: |
| 2452 | /* |
| 2453 | * Work around a bug where the busy flag in the tr selector |
| 2454 | * isn't exposed |
| 2455 | */ |
Amit Shah | c0d0982 | 2008-10-27 09:04:18 +0000 | [diff] [blame] | 2456 | var->type |= 0x2; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 2457 | break; |
| 2458 | case VCPU_SREG_DS: |
| 2459 | case VCPU_SREG_ES: |
| 2460 | case VCPU_SREG_FS: |
| 2461 | case VCPU_SREG_GS: |
| 2462 | /* |
| 2463 | * The accessed bit must always be set in the segment |
| 2464 | * descriptor cache, although it can be cleared in the |
| 2465 | * descriptor, the cached bit always remains at 1. Since |
| 2466 | * Intel has a check on this, set it here to support |
| 2467 | * cross-vendor migration. |
| 2468 | */ |
| 2469 | if (!var->unusable) |
| 2470 | var->type |= 0x1; |
| 2471 | break; |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 2472 | case VCPU_SREG_SS: |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2473 | /* |
| 2474 | * On AMD CPUs sometimes the DB bit in the segment |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 2475 | * descriptor is left as 1, although the whole segment has |
| 2476 | * been made unusable. Clear it here to pass an Intel VMX |
| 2477 | * entry check when cross vendor migrating. |
| 2478 | */ |
| 2479 | if (var->unusable) |
| 2480 | var->db = 0; |
Roman Pen | d9c1b54 | 2017-06-01 10:55:03 +0200 | [diff] [blame] | 2481 | /* This is symmetric with svm_set_segment() */ |
Jan Kiszka | 33b458d | 2014-06-29 17:12:43 +0200 | [diff] [blame] | 2482 | var->dpl = to_svm(vcpu)->vmcb->save.cpl; |
Andre Przywara | b586eb0 | 2009-04-28 12:45:43 +0200 | [diff] [blame] | 2483 | break; |
Andre Przywara | 1fbdc7a | 2009-01-11 22:39:44 +0100 | [diff] [blame] | 2484 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2485 | } |
| 2486 | |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2487 | static int svm_get_cpl(struct kvm_vcpu *vcpu) |
| 2488 | { |
| 2489 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
| 2490 | |
| 2491 | return save->cpl; |
| 2492 | } |
| 2493 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2494 | 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] | 2495 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2496 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2497 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2498 | dt->size = svm->vmcb->save.idtr.limit; |
| 2499 | dt->address = svm->vmcb->save.idtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2500 | } |
| 2501 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2502 | 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] | 2503 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2504 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2505 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2506 | svm->vmcb->save.idtr.limit = dt->size; |
| 2507 | svm->vmcb->save.idtr.base = dt->address ; |
Joerg Roedel | 17a703c | 2010-12-03 11:45:56 +0100 | [diff] [blame] | 2508 | mark_dirty(svm->vmcb, VMCB_DT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2509 | } |
| 2510 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2511 | 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] | 2512 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2513 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2514 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2515 | dt->size = svm->vmcb->save.gdtr.limit; |
| 2516 | dt->address = svm->vmcb->save.gdtr.base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2517 | } |
| 2518 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2519 | 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] | 2520 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2521 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2522 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2523 | svm->vmcb->save.gdtr.limit = dt->size; |
| 2524 | svm->vmcb->save.gdtr.base = dt->address ; |
Joerg Roedel | 17a703c | 2010-12-03 11:45:56 +0100 | [diff] [blame] | 2525 | mark_dirty(svm->vmcb, VMCB_DT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2526 | } |
| 2527 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 2528 | static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 2529 | { |
| 2530 | } |
| 2531 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2532 | static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2533 | { |
| 2534 | } |
| 2535 | |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2536 | static void update_cr0_intercept(struct vcpu_svm *svm) |
| 2537 | { |
| 2538 | ulong gcr0 = svm->vcpu.arch.cr0; |
| 2539 | u64 *hcr0 = &svm->vmcb->save.cr0; |
| 2540 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2541 | *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK) |
| 2542 | | (gcr0 & SVM_CR0_SELECTIVE_MASK); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2543 | |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 2544 | mark_dirty(svm->vmcb, VMCB_CR); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2545 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2546 | if (gcr0 == *hcr0) { |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 2547 | clr_cr_intercept(svm, INTERCEPT_CR0_READ); |
| 2548 | clr_cr_intercept(svm, INTERCEPT_CR0_WRITE); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2549 | } else { |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 2550 | set_cr_intercept(svm, INTERCEPT_CR0_READ); |
| 2551 | set_cr_intercept(svm, INTERCEPT_CR0_WRITE); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2552 | } |
| 2553 | } |
| 2554 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2555 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 2556 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2557 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2558 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2559 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2560 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 2561 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2562 | vcpu->arch.efer |= EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 2563 | svm->vmcb->save.efer |= EFER_LMA | EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2564 | } |
| 2565 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2566 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2567 | vcpu->arch.efer &= ~EFER_LMA; |
Carlo Marcelo Arenas Belon | 2b5203e | 2007-12-01 06:17:11 -0600 | [diff] [blame] | 2568 | svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2569 | } |
| 2570 | } |
| 2571 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2572 | vcpu->arch.cr0 = cr0; |
Avi Kivity | 888f9f3 | 2010-01-10 12:14:04 +0200 | [diff] [blame] | 2573 | |
| 2574 | if (!npt_enabled) |
| 2575 | cr0 |= X86_CR0_PG | X86_CR0_WP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 2576 | |
Paolo Bonzini | bcf166a | 2015-10-01 13:19:55 +0200 | [diff] [blame] | 2577 | /* |
| 2578 | * re-enable caching here because the QEMU bios |
| 2579 | * does not do it - this results in some delay at |
| 2580 | * reboot |
| 2581 | */ |
| 2582 | if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
| 2583 | cr0 &= ~(X86_CR0_CD | X86_CR0_NW); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2584 | svm->vmcb->save.cr0 = cr0; |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 2585 | mark_dirty(svm->vmcb, VMCB_CR); |
Avi Kivity | d225157 | 2010-01-06 10:55:27 +0200 | [diff] [blame] | 2586 | update_cr0_intercept(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2587 | } |
| 2588 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2589 | static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2590 | { |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 2591 | unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE; |
Joerg Roedel | e5eab0c | 2008-09-09 19:11:51 +0200 | [diff] [blame] | 2592 | unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4; |
| 2593 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2594 | if (cr4 & X86_CR4_VMXE) |
| 2595 | return 1; |
| 2596 | |
Joerg Roedel | e5eab0c | 2008-09-09 19:11:51 +0200 | [diff] [blame] | 2597 | if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE)) |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 2598 | svm_flush_tlb(vcpu, true); |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 2599 | |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 2600 | vcpu->arch.cr4 = cr4; |
| 2601 | if (!npt_enabled) |
| 2602 | cr4 |= X86_CR4_PAE; |
Joerg Roedel | 6394b64 | 2008-04-09 14:15:29 +0200 | [diff] [blame] | 2603 | cr4 |= host_cr4_mce; |
Joerg Roedel | ec07726 | 2008-04-09 14:15:28 +0200 | [diff] [blame] | 2604 | to_svm(vcpu)->vmcb->save.cr4 = cr4; |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 2605 | mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2606 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | static void svm_set_segment(struct kvm_vcpu *vcpu, |
| 2610 | struct kvm_segment *var, int seg) |
| 2611 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2612 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2613 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
| 2614 | |
| 2615 | s->base = var->base; |
| 2616 | s->limit = var->limit; |
| 2617 | s->selector = var->selector; |
Roman Pen | d9c1b54 | 2017-06-01 10:55:03 +0200 | [diff] [blame] | 2618 | s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); |
| 2619 | s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; |
| 2620 | s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; |
| 2621 | s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT; |
| 2622 | s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; |
| 2623 | s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; |
| 2624 | s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; |
| 2625 | s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 2626 | |
| 2627 | /* |
| 2628 | * This is always accurate, except if SYSRET returned to a segment |
| 2629 | * with SS.DPL != 3. Intel does not have this quirk, and always |
| 2630 | * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it |
| 2631 | * would entail passing the CPL to userspace and back. |
| 2632 | */ |
| 2633 | if (seg == VCPU_SREG_SS) |
Roman Pen | d9c1b54 | 2017-06-01 10:55:03 +0200 | [diff] [blame] | 2634 | /* This is symmetric with svm_get_segment() */ |
| 2635 | svm->vmcb->save.cpl = (var->dpl & 3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2636 | |
Joerg Roedel | 060d0c9 | 2010-12-03 11:45:57 +0100 | [diff] [blame] | 2637 | mark_dirty(svm->vmcb, VMCB_SEG); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2638 | } |
| 2639 | |
Paolo Bonzini | cbdb967 | 2015-11-10 09:14:39 +0100 | [diff] [blame] | 2640 | static void update_bp_intercept(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2641 | { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2642 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2643 | |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 2644 | clr_exception_intercept(svm, BP_VECTOR); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2645 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2646 | if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2647 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Joerg Roedel | 18c918c | 2010-11-30 18:03:59 +0100 | [diff] [blame] | 2648 | set_exception_intercept(svm, BP_VECTOR); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2649 | } else |
| 2650 | vcpu->guest_debug = 0; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2651 | } |
| 2652 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 2653 | 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] | 2654 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 2655 | if (sd->next_asid > sd->max_asid) { |
| 2656 | ++sd->asid_generation; |
Brijesh Singh | 4faefff | 2017-12-04 10:57:25 -0600 | [diff] [blame] | 2657 | sd->next_asid = sd->min_asid; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2658 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2659 | } |
| 2660 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 2661 | svm->asid_generation = sd->asid_generation; |
| 2662 | svm->vmcb->control.asid = sd->next_asid++; |
Joerg Roedel | d48086d | 2010-12-03 11:45:51 +0100 | [diff] [blame] | 2663 | |
| 2664 | mark_dirty(svm->vmcb, VMCB_ASID); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2665 | } |
| 2666 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 2667 | static u64 svm_get_dr6(struct kvm_vcpu *vcpu) |
| 2668 | { |
| 2669 | return to_svm(vcpu)->vmcb->save.dr6; |
| 2670 | } |
| 2671 | |
| 2672 | static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value) |
| 2673 | { |
| 2674 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2675 | |
| 2676 | svm->vmcb->save.dr6 = value; |
| 2677 | mark_dirty(svm->vmcb, VMCB_DR); |
| 2678 | } |
| 2679 | |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 2680 | static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 2681 | { |
| 2682 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2683 | |
| 2684 | get_debugreg(vcpu->arch.db[0], 0); |
| 2685 | get_debugreg(vcpu->arch.db[1], 1); |
| 2686 | get_debugreg(vcpu->arch.db[2], 2); |
| 2687 | get_debugreg(vcpu->arch.db[3], 3); |
| 2688 | vcpu->arch.dr6 = svm_get_dr6(vcpu); |
| 2689 | vcpu->arch.dr7 = svm->vmcb->save.dr7; |
| 2690 | |
| 2691 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
| 2692 | set_dr_intercepts(svm); |
| 2693 | } |
| 2694 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 2695 | static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2696 | { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2697 | struct vcpu_svm *svm = to_svm(vcpu); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2698 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 2699 | svm->vmcb->save.dr7 = value; |
Joerg Roedel | 72214b9 | 2010-12-03 11:45:55 +0100 | [diff] [blame] | 2700 | mark_dirty(svm->vmcb, VMCB_DR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2701 | } |
| 2702 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2703 | static int pf_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2704 | { |
Brijesh Singh | 0ede79e | 2017-12-04 10:57:39 -0600 | [diff] [blame] | 2705 | u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 2706 | u64 error_code = svm->vmcb->control.exit_info_1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2707 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 2708 | return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address, |
Brijesh Singh | 00b10fe | 2017-12-04 10:57:40 -0600 | [diff] [blame] | 2709 | static_cpu_has(X86_FEATURE_DECODEASSISTS) ? |
| 2710 | svm->vmcb->control.insn_bytes : NULL, |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 2711 | svm->vmcb->control.insn_len); |
| 2712 | } |
| 2713 | |
| 2714 | static int npf_interception(struct vcpu_svm *svm) |
| 2715 | { |
Brijesh Singh | 0ede79e | 2017-12-04 10:57:39 -0600 | [diff] [blame] | 2716 | u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2); |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 2717 | u64 error_code = svm->vmcb->control.exit_info_1; |
| 2718 | |
| 2719 | trace_kvm_page_fault(fault_address, error_code); |
| 2720 | return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code, |
Brijesh Singh | 00b10fe | 2017-12-04 10:57:40 -0600 | [diff] [blame] | 2721 | static_cpu_has(X86_FEATURE_DECODEASSISTS) ? |
| 2722 | svm->vmcb->control.insn_bytes : NULL, |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 2723 | svm->vmcb->control.insn_len); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2724 | } |
| 2725 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2726 | static int db_interception(struct vcpu_svm *svm) |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2727 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2728 | struct kvm_run *kvm_run = svm->vcpu.run; |
Vitaly Kuznetsov | 99c2217 | 2019-04-03 16:06:42 +0200 | [diff] [blame] | 2729 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2730 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2731 | if (!(svm->vcpu.guest_debug & |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2732 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) && |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 2733 | !svm->nmi_singlestep) { |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2734 | kvm_queue_exception(&svm->vcpu, DB_VECTOR); |
| 2735 | return 1; |
| 2736 | } |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2737 | |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 2738 | if (svm->nmi_singlestep) { |
Ladi Prosek | 4aebd0e | 2017-06-21 09:06:57 +0200 | [diff] [blame] | 2739 | disable_nmi_singlestep(svm); |
Vitaly Kuznetsov | 99c2217 | 2019-04-03 16:06:42 +0200 | [diff] [blame] | 2740 | /* Make sure we check for pending NMIs upon entry */ |
| 2741 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | if (svm->vcpu.guest_debug & |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 2745 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) { |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 2746 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 2747 | kvm_run->debug.arch.pc = |
| 2748 | svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 2749 | kvm_run->debug.arch.exception = DB_VECTOR; |
| 2750 | return 0; |
| 2751 | } |
| 2752 | |
| 2753 | return 1; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2754 | } |
| 2755 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2756 | static int bp_interception(struct vcpu_svm *svm) |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2757 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2758 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2759 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2760 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 2761 | kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; |
| 2762 | kvm_run->debug.arch.exception = BP_VECTOR; |
| 2763 | return 0; |
| 2764 | } |
| 2765 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2766 | static int ud_interception(struct vcpu_svm *svm) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2767 | { |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 2768 | return handle_ud(&svm->vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2769 | } |
| 2770 | |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 2771 | static int ac_interception(struct vcpu_svm *svm) |
| 2772 | { |
| 2773 | kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0); |
| 2774 | return 1; |
| 2775 | } |
| 2776 | |
Liran Alon | 9718420 | 2018-03-12 13:12:52 +0200 | [diff] [blame] | 2777 | static int gp_interception(struct vcpu_svm *svm) |
| 2778 | { |
| 2779 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 2780 | u32 error_code = svm->vmcb->control.exit_info_1; |
Liran Alon | 9718420 | 2018-03-12 13:12:52 +0200 | [diff] [blame] | 2781 | |
| 2782 | WARN_ON_ONCE(!enable_vmware_backdoor); |
| 2783 | |
Sean Christopherson | a6c6ed1 | 2019-08-27 14:40:30 -0700 | [diff] [blame] | 2784 | /* |
| 2785 | * VMware backdoor emulation on #GP interception only handles IN{S}, |
| 2786 | * OUT{S}, and RDPMC, none of which generate a non-zero error code. |
| 2787 | */ |
| 2788 | if (error_code) { |
| 2789 | kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); |
| 2790 | return 1; |
| 2791 | } |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 2792 | return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP); |
Liran Alon | 9718420 | 2018-03-12 13:12:52 +0200 | [diff] [blame] | 2793 | } |
| 2794 | |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 2795 | static bool is_erratum_383(void) |
| 2796 | { |
| 2797 | int err, i; |
| 2798 | u64 value; |
| 2799 | |
| 2800 | if (!erratum_383_found) |
| 2801 | return false; |
| 2802 | |
| 2803 | value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err); |
| 2804 | if (err) |
| 2805 | return false; |
| 2806 | |
| 2807 | /* Bit 62 may or may not be set for this mce */ |
| 2808 | value &= ~(1ULL << 62); |
| 2809 | |
| 2810 | if (value != 0xb600000000010015ULL) |
| 2811 | return false; |
| 2812 | |
| 2813 | /* Clear MCi_STATUS registers */ |
| 2814 | for (i = 0; i < 6; ++i) |
| 2815 | native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0); |
| 2816 | |
| 2817 | value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err); |
| 2818 | if (!err) { |
| 2819 | u32 low, high; |
| 2820 | |
| 2821 | value &= ~(1ULL << 2); |
| 2822 | low = lower_32_bits(value); |
| 2823 | high = upper_32_bits(value); |
| 2824 | |
| 2825 | native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high); |
| 2826 | } |
| 2827 | |
| 2828 | /* Flush tlb to evict multi-match entries */ |
| 2829 | __flush_tlb_all(); |
| 2830 | |
| 2831 | return true; |
| 2832 | } |
| 2833 | |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 2834 | static void svm_handle_mce(struct vcpu_svm *svm) |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2835 | { |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 2836 | if (is_erratum_383()) { |
| 2837 | /* |
| 2838 | * Erratum 383 triggered. Guest state is corrupt so kill the |
| 2839 | * guest. |
| 2840 | */ |
| 2841 | pr_err("KVM: Guest triggered AMD Erratum 383\n"); |
| 2842 | |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 2843 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu); |
Joerg Roedel | 67ec660 | 2010-05-17 14:43:35 +0200 | [diff] [blame] | 2844 | |
| 2845 | return; |
| 2846 | } |
| 2847 | |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2848 | /* |
| 2849 | * On an #MC intercept the MCE handler is not called automatically in |
| 2850 | * the host. So do it by hand here. |
| 2851 | */ |
| 2852 | asm volatile ( |
| 2853 | "int $0x12\n"); |
| 2854 | /* not sure if we ever come back to this point */ |
| 2855 | |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 2856 | return; |
| 2857 | } |
| 2858 | |
| 2859 | static int mc_interception(struct vcpu_svm *svm) |
| 2860 | { |
Joerg Roedel | 53371b5 | 2008-04-09 14:15:30 +0200 | [diff] [blame] | 2861 | return 1; |
| 2862 | } |
| 2863 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2864 | static int shutdown_interception(struct vcpu_svm *svm) |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2865 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2866 | struct kvm_run *kvm_run = svm->vcpu.run; |
| 2867 | |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2868 | /* |
| 2869 | * VMCB is undefined after a SHUTDOWN intercept |
| 2870 | * so reinitialize it. |
| 2871 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2872 | clear_page(svm->vmcb); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 2873 | init_vmcb(svm); |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 2874 | |
| 2875 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 2876 | return 0; |
| 2877 | } |
| 2878 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2879 | static int io_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2880 | { |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2881 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2882 | u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 2883 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2884 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2885 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2886 | ++svm->vcpu.stat.io_exits; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2887 | string = (io_info & SVM_IOIO_STR_MASK) != 0; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2888 | in = (io_info & SVM_IOIO_TYPE_MASK) != 0; |
Tom Lendacky | 8370c3d | 2016-11-23 12:01:50 -0500 | [diff] [blame] | 2889 | if (string) |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 2890 | return kvm_emulate_instruction(vcpu, 0); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2891 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2892 | port = io_info >> 16; |
| 2893 | size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2894 | svm->next_rip = svm->vmcb->control.exit_info_2; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 2895 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 2896 | return kvm_fast_pio(&svm->vcpu, size, port, in); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2897 | } |
| 2898 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2899 | static int nmi_interception(struct vcpu_svm *svm) |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 2900 | { |
| 2901 | return 1; |
| 2902 | } |
| 2903 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2904 | static int intr_interception(struct vcpu_svm *svm) |
Joerg Roedel | a069805 | 2008-04-30 17:56:01 +0200 | [diff] [blame] | 2905 | { |
| 2906 | ++svm->vcpu.stat.irq_exits; |
| 2907 | return 1; |
| 2908 | } |
| 2909 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2910 | static int nop_on_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2911 | { |
| 2912 | return 1; |
| 2913 | } |
| 2914 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2915 | static int halt_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2916 | { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 2917 | return kvm_emulate_halt(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2918 | } |
| 2919 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 2920 | static int vmmcall_interception(struct vcpu_svm *svm) |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2921 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 2922 | return kvm_emulate_hypercall(&svm->vcpu); |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2923 | } |
| 2924 | |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2925 | static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) |
| 2926 | { |
| 2927 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2928 | |
| 2929 | return svm->nested.nested_cr3; |
| 2930 | } |
| 2931 | |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 2932 | static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) |
| 2933 | { |
| 2934 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2935 | u64 cr3 = svm->nested.nested_cr3; |
| 2936 | u64 pdpte; |
| 2937 | int ret; |
| 2938 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 2939 | 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] | 2940 | offset_in_page(cr3) + index * 8, 8); |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 2941 | if (ret) |
| 2942 | return 0; |
| 2943 | return pdpte; |
| 2944 | } |
| 2945 | |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2946 | static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu, |
| 2947 | unsigned long root) |
| 2948 | { |
| 2949 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2950 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 2951 | svm->vmcb->control.nested_cr3 = __sme_set(root); |
Joerg Roedel | b274716 | 2010-12-03 11:45:53 +0100 | [diff] [blame] | 2952 | mark_dirty(svm->vmcb, VMCB_NPT); |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2953 | } |
| 2954 | |
Avi Kivity | 6389ee9 | 2010-11-29 16:12:30 +0200 | [diff] [blame] | 2955 | static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, |
| 2956 | struct x86_exception *fault) |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2957 | { |
| 2958 | struct vcpu_svm *svm = to_svm(vcpu); |
| 2959 | |
Paolo Bonzini | 5e35251 | 2014-09-02 13:18:37 +0200 | [diff] [blame] | 2960 | if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) { |
| 2961 | /* |
| 2962 | * TODO: track the cause of the nested page fault, and |
| 2963 | * correctly fill in the high bits of exit_info_1. |
| 2964 | */ |
| 2965 | svm->vmcb->control.exit_code = SVM_EXIT_NPF; |
| 2966 | svm->vmcb->control.exit_code_hi = 0; |
| 2967 | svm->vmcb->control.exit_info_1 = (1ULL << 32); |
| 2968 | svm->vmcb->control.exit_info_2 = fault->address; |
| 2969 | } |
| 2970 | |
| 2971 | svm->vmcb->control.exit_info_1 &= ~0xffffffffULL; |
| 2972 | svm->vmcb->control.exit_info_1 |= fault->error_code; |
| 2973 | |
| 2974 | /* |
| 2975 | * The present bit is always zero for page structure faults on real |
| 2976 | * hardware. |
| 2977 | */ |
| 2978 | if (svm->vmcb->control.exit_info_1 & (2ULL << 32)) |
| 2979 | svm->vmcb->control.exit_info_1 &= ~1; |
Joerg Roedel | 5bd2edc | 2010-09-10 17:31:02 +0200 | [diff] [blame] | 2980 | |
| 2981 | nested_svm_vmexit(svm); |
| 2982 | } |
| 2983 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 2984 | static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2985 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 2986 | WARN_ON(mmu_is_nested(vcpu)); |
Vitaly Kuznetsov | 3cf85f9 | 2018-12-19 17:25:14 +0100 | [diff] [blame] | 2987 | |
| 2988 | vcpu->arch.mmu = &vcpu->arch.guest_mmu; |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 2989 | kvm_init_shadow_mmu(vcpu); |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 2990 | vcpu->arch.mmu->set_cr3 = nested_svm_set_tdp_cr3; |
| 2991 | vcpu->arch.mmu->get_cr3 = nested_svm_get_tdp_cr3; |
| 2992 | vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr; |
| 2993 | vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit; |
| 2994 | vcpu->arch.mmu->shadow_root_level = get_npt_level(vcpu); |
| 2995 | reset_shadow_zero_bits_mask(vcpu, vcpu->arch.mmu); |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2996 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 3000 | { |
Vitaly Kuznetsov | 3cf85f9 | 2018-12-19 17:25:14 +0100 | [diff] [blame] | 3001 | vcpu->arch.mmu = &vcpu->arch.root_mmu; |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 3002 | vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3003 | } |
| 3004 | |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 3005 | static int nested_svm_check_permissions(struct vcpu_svm *svm) |
| 3006 | { |
Dan Carpenter | e9196ce | 2017-05-18 10:39:53 +0300 | [diff] [blame] | 3007 | if (!(svm->vcpu.arch.efer & EFER_SVME) || |
| 3008 | !is_paging(&svm->vcpu)) { |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 3009 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 3010 | return 1; |
| 3011 | } |
| 3012 | |
| 3013 | if (svm->vmcb->save.cpl) { |
| 3014 | kvm_inject_gp(&svm->vcpu, 0); |
| 3015 | return 1; |
| 3016 | } |
| 3017 | |
Dan Carpenter | e9196ce | 2017-05-18 10:39:53 +0300 | [diff] [blame] | 3018 | return 0; |
Alexander Graf | c072542 | 2008-11-25 20:17:03 +0100 | [diff] [blame] | 3019 | } |
| 3020 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3021 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
| 3022 | bool has_error_code, u32 error_code) |
| 3023 | { |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 3024 | int vmexit; |
| 3025 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 3026 | if (!is_guest_mode(&svm->vcpu)) |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 3027 | return 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3028 | |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3029 | vmexit = nested_svm_intercept(svm); |
| 3030 | if (vmexit != NESTED_EXIT_DONE) |
| 3031 | return 0; |
| 3032 | |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 3033 | svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; |
| 3034 | svm->vmcb->control.exit_code_hi = 0; |
| 3035 | svm->vmcb->control.exit_info_1 = error_code; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3036 | |
| 3037 | /* |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 3038 | * EXITINFO2 is undefined for all exception intercepts other |
| 3039 | * than #PF. |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3040 | */ |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3041 | if (svm->vcpu.arch.exception.nested_apf) |
| 3042 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token; |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 3043 | else if (svm->vcpu.arch.exception.has_payload) |
| 3044 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload; |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3045 | else |
| 3046 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; |
Joerg Roedel | 0295ad7 | 2009-08-07 11:49:37 +0200 | [diff] [blame] | 3047 | |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3048 | svm->nested.exit_required = true; |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 3049 | return vmexit; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3050 | } |
| 3051 | |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 3052 | /* This function returns true if it is save to enable the irq window */ |
| 3053 | static inline bool nested_svm_intr(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3054 | { |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 3055 | if (!is_guest_mode(&svm->vcpu)) |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 3056 | return true; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3057 | |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 3058 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 3059 | return true; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3060 | |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 3061 | if (!(svm->vcpu.arch.hflags & HF_HIF_MASK)) |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 3062 | return false; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3063 | |
Gleb Natapov | a0a07cd | 2010-09-20 10:15:32 +0200 | [diff] [blame] | 3064 | /* |
| 3065 | * if vmexit was already requested (by intercepted exception |
| 3066 | * for instance) do not overwrite it with "external interrupt" |
| 3067 | * vmexit. |
| 3068 | */ |
| 3069 | if (svm->nested.exit_required) |
| 3070 | return false; |
| 3071 | |
Joerg Roedel | 197717d | 2010-02-24 18:59:19 +0100 | [diff] [blame] | 3072 | svm->vmcb->control.exit_code = SVM_EXIT_INTR; |
| 3073 | svm->vmcb->control.exit_info_1 = 0; |
| 3074 | svm->vmcb->control.exit_info_2 = 0; |
Joerg Roedel | 2666695 | 2009-08-07 11:49:46 +0200 | [diff] [blame] | 3075 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 3076 | if (svm->nested.intercept & 1ULL) { |
| 3077 | /* |
| 3078 | * The #vmexit can't be emulated here directly because this |
Guo Chao | c5ec2e5 | 2012-06-28 15:16:43 +0800 | [diff] [blame] | 3079 | * code path runs with irqs and preemption disabled. A |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 3080 | * #vmexit emulation might sleep. Only signal request for |
| 3081 | * the #vmexit here. |
| 3082 | */ |
| 3083 | svm->nested.exit_required = true; |
Joerg Roedel | 236649d | 2009-10-09 16:08:30 +0200 | [diff] [blame] | 3084 | trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 3085 | return false; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3086 | } |
| 3087 | |
Joerg Roedel | 8fe5465 | 2010-02-19 16:23:01 +0100 | [diff] [blame] | 3088 | return true; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3089 | } |
| 3090 | |
Joerg Roedel | 887f500 | 2010-02-24 18:59:12 +0100 | [diff] [blame] | 3091 | /* This function returns true if it is save to enable the nmi window */ |
| 3092 | static inline bool nested_svm_nmi(struct vcpu_svm *svm) |
| 3093 | { |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 3094 | if (!is_guest_mode(&svm->vcpu)) |
Joerg Roedel | 887f500 | 2010-02-24 18:59:12 +0100 | [diff] [blame] | 3095 | return true; |
| 3096 | |
| 3097 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI))) |
| 3098 | return true; |
| 3099 | |
| 3100 | svm->vmcb->control.exit_code = SVM_EXIT_NMI; |
| 3101 | svm->nested.exit_required = true; |
| 3102 | |
| 3103 | return false; |
| 3104 | } |
| 3105 | |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 3106 | static int nested_svm_intercept_ioio(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3107 | { |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 3108 | unsigned port, size, iopm_len; |
| 3109 | u16 val, mask; |
| 3110 | u8 start_bit; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 3111 | u64 gpa; |
| 3112 | |
| 3113 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT))) |
| 3114 | return NESTED_EXIT_HOST; |
| 3115 | |
| 3116 | port = svm->vmcb->control.exit_info_1 >> 16; |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 3117 | size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >> |
| 3118 | SVM_IOIO_SIZE_SHIFT; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 3119 | gpa = svm->nested.vmcb_iopm + (port / 8); |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 3120 | start_bit = port % 8; |
| 3121 | iopm_len = (start_bit + size > 8) ? 2 : 1; |
| 3122 | mask = (0xf >> (4 - size)) << start_bit; |
| 3123 | val = 0; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 3124 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 3125 | if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len)) |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 3126 | return NESTED_EXIT_DONE; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 3127 | |
Jan Kiszka | 9bf4183 | 2014-06-30 10:54:17 +0200 | [diff] [blame] | 3128 | return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 3129 | } |
| 3130 | |
Joerg Roedel | d247782 | 2010-03-01 15:34:34 +0100 | [diff] [blame] | 3131 | static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3132 | { |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 3133 | u32 offset, msr, value; |
| 3134 | int write, mask; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 3135 | |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 3136 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
Joerg Roedel | d247782 | 2010-03-01 15:34:34 +0100 | [diff] [blame] | 3137 | return NESTED_EXIT_HOST; |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 3138 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 3139 | msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
| 3140 | offset = svm_msrpm_offset(msr); |
| 3141 | write = svm->vmcb->control.exit_info_1 & 1; |
| 3142 | mask = 1 << ((2 * (msr & 0xf)) + write); |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 3143 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 3144 | if (offset == MSR_INVALID) |
| 3145 | return NESTED_EXIT_DONE; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 3146 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 3147 | /* Offset is in 32 bit units but need in 8 bit units */ |
| 3148 | offset *= 4; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 3149 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 3150 | 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] | 3151 | return NESTED_EXIT_DONE; |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 3152 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 3153 | return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 3154 | } |
| 3155 | |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 3156 | /* DB exceptions for our internal use must not cause vmexit */ |
| 3157 | static int nested_svm_intercept_db(struct vcpu_svm *svm) |
| 3158 | { |
| 3159 | unsigned long dr6; |
| 3160 | |
| 3161 | /* if we're not singlestepping, it's not ours */ |
| 3162 | if (!svm->nmi_singlestep) |
| 3163 | return NESTED_EXIT_DONE; |
| 3164 | |
| 3165 | /* if it's not a singlestep exception, it's not ours */ |
| 3166 | if (kvm_get_dr(&svm->vcpu, 6, &dr6)) |
| 3167 | return NESTED_EXIT_DONE; |
| 3168 | if (!(dr6 & DR6_BS)) |
| 3169 | return NESTED_EXIT_DONE; |
| 3170 | |
| 3171 | /* if the guest is singlestepping, it should get the vmexit */ |
| 3172 | if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) { |
| 3173 | disable_nmi_singlestep(svm); |
| 3174 | return NESTED_EXIT_DONE; |
| 3175 | } |
| 3176 | |
| 3177 | /* it's ours, the nested hypervisor must not see this one */ |
| 3178 | return NESTED_EXIT_HOST; |
| 3179 | } |
| 3180 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3181 | static int nested_svm_exit_special(struct vcpu_svm *svm) |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 3182 | { |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3183 | u32 exit_code = svm->vmcb->control.exit_code; |
Joerg Roedel | 4c2161a | 2009-08-07 11:49:35 +0200 | [diff] [blame] | 3184 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3185 | switch (exit_code) { |
| 3186 | case SVM_EXIT_INTR: |
| 3187 | case SVM_EXIT_NMI: |
Joerg Roedel | ff47a49 | 2010-04-22 12:33:14 +0200 | [diff] [blame] | 3188 | case SVM_EXIT_EXCP_BASE + MC_VECTOR: |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3189 | return NESTED_EXIT_HOST; |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3190 | case SVM_EXIT_NPF: |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3191 | /* For now we are always handling NPFs when using them */ |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3192 | if (npt_enabled) |
| 3193 | return NESTED_EXIT_HOST; |
| 3194 | break; |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3195 | case SVM_EXIT_EXCP_BASE + PF_VECTOR: |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 3196 | /* When we're shadowing, trap PFs, but not async PF */ |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 3197 | if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3198 | return NESTED_EXIT_HOST; |
| 3199 | break; |
| 3200 | default: |
| 3201 | break; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3202 | } |
| 3203 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3204 | return NESTED_EXIT_CONTINUE; |
| 3205 | } |
| 3206 | |
| 3207 | /* |
| 3208 | * If this function returns true, this #vmexit was already handled |
| 3209 | */ |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 3210 | static int nested_svm_intercept(struct vcpu_svm *svm) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3211 | { |
| 3212 | u32 exit_code = svm->vmcb->control.exit_code; |
| 3213 | int vmexit = NESTED_EXIT_HOST; |
| 3214 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3215 | switch (exit_code) { |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 3216 | case SVM_EXIT_MSR: |
Joerg Roedel | 3d62d9a | 2009-08-07 11:49:39 +0200 | [diff] [blame] | 3217 | vmexit = nested_svm_exit_handled_msr(svm); |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 3218 | break; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 3219 | case SVM_EXIT_IOIO: |
| 3220 | vmexit = nested_svm_intercept_ioio(svm); |
| 3221 | break; |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 3222 | case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { |
| 3223 | u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0); |
| 3224 | if (svm->nested.intercept_cr & bit) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3225 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3226 | break; |
| 3227 | } |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 3228 | case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { |
| 3229 | u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0); |
| 3230 | if (svm->nested.intercept_dr & bit) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3231 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3232 | break; |
| 3233 | } |
| 3234 | case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { |
| 3235 | u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 3236 | if (svm->nested.intercept_exceptions & excp_bits) { |
| 3237 | if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR) |
| 3238 | vmexit = nested_svm_intercept_db(svm); |
| 3239 | else |
| 3240 | vmexit = NESTED_EXIT_DONE; |
| 3241 | } |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 3242 | /* async page fault always cause vmexit */ |
| 3243 | else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) && |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3244 | svm->vcpu.arch.exception.nested_apf != 0) |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 3245 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3246 | break; |
| 3247 | } |
Joerg Roedel | 228070b | 2010-04-22 12:33:10 +0200 | [diff] [blame] | 3248 | case SVM_EXIT_ERR: { |
| 3249 | vmexit = NESTED_EXIT_DONE; |
| 3250 | break; |
| 3251 | } |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3252 | default: { |
| 3253 | u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 3254 | if (svm->nested.intercept & exit_bits) |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 3255 | vmexit = NESTED_EXIT_DONE; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3256 | } |
| 3257 | } |
| 3258 | |
Joerg Roedel | b8e88bc | 2010-02-19 16:23:02 +0100 | [diff] [blame] | 3259 | return vmexit; |
| 3260 | } |
| 3261 | |
| 3262 | static int nested_svm_exit_handled(struct vcpu_svm *svm) |
| 3263 | { |
| 3264 | int vmexit; |
| 3265 | |
| 3266 | vmexit = nested_svm_intercept(svm); |
| 3267 | |
| 3268 | if (vmexit == NESTED_EXIT_DONE) |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 3269 | nested_svm_vmexit(svm); |
Joerg Roedel | 9c4e40b9 | 2009-08-07 11:49:36 +0200 | [diff] [blame] | 3270 | |
| 3271 | return vmexit; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3272 | } |
| 3273 | |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 3274 | static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb) |
| 3275 | { |
| 3276 | struct vmcb_control_area *dst = &dst_vmcb->control; |
| 3277 | struct vmcb_control_area *from = &from_vmcb->control; |
| 3278 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 3279 | dst->intercept_cr = from->intercept_cr; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 3280 | dst->intercept_dr = from->intercept_dr; |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 3281 | dst->intercept_exceptions = from->intercept_exceptions; |
| 3282 | dst->intercept = from->intercept; |
| 3283 | dst->iopm_base_pa = from->iopm_base_pa; |
| 3284 | dst->msrpm_base_pa = from->msrpm_base_pa; |
| 3285 | dst->tsc_offset = from->tsc_offset; |
| 3286 | dst->asid = from->asid; |
| 3287 | dst->tlb_ctl = from->tlb_ctl; |
| 3288 | dst->int_ctl = from->int_ctl; |
| 3289 | dst->int_vector = from->int_vector; |
| 3290 | dst->int_state = from->int_state; |
| 3291 | dst->exit_code = from->exit_code; |
| 3292 | dst->exit_code_hi = from->exit_code_hi; |
| 3293 | dst->exit_info_1 = from->exit_info_1; |
| 3294 | dst->exit_info_2 = from->exit_info_2; |
| 3295 | dst->exit_int_info = from->exit_int_info; |
| 3296 | dst->exit_int_info_err = from->exit_int_info_err; |
| 3297 | dst->nested_ctl = from->nested_ctl; |
| 3298 | dst->event_inj = from->event_inj; |
| 3299 | dst->event_inj_err = from->event_inj_err; |
| 3300 | dst->nested_cr3 = from->nested_cr3; |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 3301 | dst->virt_ext = from->virt_ext; |
Tambe, William | e081354 | 2018-11-13 16:51:20 +0000 | [diff] [blame] | 3302 | dst->pause_filter_count = from->pause_filter_count; |
| 3303 | dst->pause_filter_thresh = from->pause_filter_thresh; |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 3304 | } |
| 3305 | |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 3306 | static int nested_svm_vmexit(struct vcpu_svm *svm) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3307 | { |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3308 | int rc; |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 3309 | struct vmcb *nested_vmcb; |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 3310 | struct vmcb *hsave = svm->nested.hsave; |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3311 | struct vmcb *vmcb = svm->vmcb; |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3312 | struct kvm_host_map map; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3313 | |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 3314 | trace_kvm_nested_vmexit_inject(vmcb->control.exit_code, |
| 3315 | vmcb->control.exit_info_1, |
| 3316 | vmcb->control.exit_info_2, |
| 3317 | vmcb->control.exit_int_info, |
Stefan Hajnoczi | e097e5f | 2011-07-22 12:46:52 +0100 | [diff] [blame] | 3318 | vmcb->control.exit_int_info_err, |
| 3319 | KVM_ISA_SVM); |
Joerg Roedel | 17897f3 | 2009-10-09 16:08:29 +0200 | [diff] [blame] | 3320 | |
Vitaly Kuznetsov | 8f38302 | 2019-06-04 18:09:39 +0200 | [diff] [blame] | 3321 | rc = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->nested.vmcb), &map); |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3322 | if (rc) { |
| 3323 | if (rc == -EINVAL) |
| 3324 | kvm_inject_gp(&svm->vcpu, 0); |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 3325 | return 1; |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | nested_vmcb = map.hva; |
Joerg Roedel | 34f80cf | 2009-08-07 11:49:38 +0200 | [diff] [blame] | 3329 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 3330 | /* Exit Guest-Mode */ |
| 3331 | leave_guest_mode(&svm->vcpu); |
Joerg Roedel | 06fc7772 | 2010-02-19 16:23:07 +0100 | [diff] [blame] | 3332 | svm->nested.vmcb = 0; |
| 3333 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3334 | /* Give the current vmcb to the guest */ |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3335 | disable_gif(svm); |
| 3336 | |
| 3337 | nested_vmcb->save.es = vmcb->save.es; |
| 3338 | nested_vmcb->save.cs = vmcb->save.cs; |
| 3339 | nested_vmcb->save.ss = vmcb->save.ss; |
| 3340 | nested_vmcb->save.ds = vmcb->save.ds; |
| 3341 | nested_vmcb->save.gdtr = vmcb->save.gdtr; |
| 3342 | nested_vmcb->save.idtr = vmcb->save.idtr; |
Joerg Roedel | 3f6a9d1 | 2010-07-27 18:14:20 +0200 | [diff] [blame] | 3343 | nested_vmcb->save.efer = svm->vcpu.arch.efer; |
Joerg Roedel | cdbbdc1 | 2010-02-19 16:23:03 +0100 | [diff] [blame] | 3344 | nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 3345 | nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu); |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3346 | nested_vmcb->save.cr2 = vmcb->save.cr2; |
Joerg Roedel | cdbbdc1 | 2010-02-19 16:23:03 +0100 | [diff] [blame] | 3347 | nested_vmcb->save.cr4 = svm->vcpu.arch.cr4; |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 3348 | nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu); |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3349 | nested_vmcb->save.rip = vmcb->save.rip; |
| 3350 | nested_vmcb->save.rsp = vmcb->save.rsp; |
| 3351 | nested_vmcb->save.rax = vmcb->save.rax; |
| 3352 | nested_vmcb->save.dr7 = vmcb->save.dr7; |
| 3353 | nested_vmcb->save.dr6 = vmcb->save.dr6; |
| 3354 | nested_vmcb->save.cpl = vmcb->save.cpl; |
| 3355 | |
| 3356 | nested_vmcb->control.int_ctl = vmcb->control.int_ctl; |
| 3357 | nested_vmcb->control.int_vector = vmcb->control.int_vector; |
| 3358 | nested_vmcb->control.int_state = vmcb->control.int_state; |
| 3359 | nested_vmcb->control.exit_code = vmcb->control.exit_code; |
| 3360 | nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi; |
| 3361 | nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1; |
| 3362 | nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2; |
| 3363 | nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info; |
| 3364 | 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] | 3365 | |
| 3366 | if (svm->nrips_enabled) |
| 3367 | nested_vmcb->control.next_rip = vmcb->control.next_rip; |
Alexander Graf | 8d23c46 | 2009-10-09 16:08:25 +0200 | [diff] [blame] | 3368 | |
| 3369 | /* |
| 3370 | * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have |
| 3371 | * to make sure that we do not lose injected events. So check event_inj |
| 3372 | * here and copy it to exit_int_info if it is valid. |
| 3373 | * Exit_int_info and event_inj can't be both valid because the case |
| 3374 | * below only happens on a VMRUN instruction intercept which has |
| 3375 | * no valid exit_int_info set. |
| 3376 | */ |
| 3377 | if (vmcb->control.event_inj & SVM_EVTINJ_VALID) { |
| 3378 | struct vmcb_control_area *nc = &nested_vmcb->control; |
| 3379 | |
| 3380 | nc->exit_int_info = vmcb->control.event_inj; |
| 3381 | nc->exit_int_info_err = vmcb->control.event_inj_err; |
| 3382 | } |
| 3383 | |
Joerg Roedel | 33740e4 | 2009-08-07 11:49:29 +0200 | [diff] [blame] | 3384 | nested_vmcb->control.tlb_ctl = 0; |
| 3385 | nested_vmcb->control.event_inj = 0; |
| 3386 | nested_vmcb->control.event_inj_err = 0; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3387 | |
Tambe, William | e081354 | 2018-11-13 16:51:20 +0000 | [diff] [blame] | 3388 | nested_vmcb->control.pause_filter_count = |
| 3389 | svm->vmcb->control.pause_filter_count; |
| 3390 | nested_vmcb->control.pause_filter_thresh = |
| 3391 | svm->vmcb->control.pause_filter_thresh; |
| 3392 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3393 | /* We always set V_INTR_MASKING and remember the old value in hflags */ |
| 3394 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
| 3395 | nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK; |
| 3396 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3397 | /* Restore the original control entries */ |
Joerg Roedel | 0460a97 | 2009-08-07 11:49:31 +0200 | [diff] [blame] | 3398 | copy_vmcb_control_area(vmcb, hsave); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3399 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3400 | svm->vcpu.arch.tsc_offset = svm->vmcb->control.tsc_offset; |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 3401 | kvm_clear_exception_queue(&svm->vcpu); |
| 3402 | kvm_clear_interrupt_queue(&svm->vcpu); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3403 | |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3404 | svm->nested.nested_cr3 = 0; |
| 3405 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3406 | /* Restore selected save entries */ |
| 3407 | svm->vmcb->save.es = hsave->save.es; |
| 3408 | svm->vmcb->save.cs = hsave->save.cs; |
| 3409 | svm->vmcb->save.ss = hsave->save.ss; |
| 3410 | svm->vmcb->save.ds = hsave->save.ds; |
| 3411 | svm->vmcb->save.gdtr = hsave->save.gdtr; |
| 3412 | svm->vmcb->save.idtr = hsave->save.idtr; |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 3413 | kvm_set_rflags(&svm->vcpu, hsave->save.rflags); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3414 | svm_set_efer(&svm->vcpu, hsave->save.efer); |
| 3415 | svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); |
| 3416 | svm_set_cr4(&svm->vcpu, hsave->save.cr4); |
| 3417 | if (npt_enabled) { |
| 3418 | svm->vmcb->save.cr3 = hsave->save.cr3; |
| 3419 | svm->vcpu.arch.cr3 = hsave->save.cr3; |
| 3420 | } else { |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 3421 | (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3422 | } |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 3423 | kvm_rax_write(&svm->vcpu, hsave->save.rax); |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 3424 | kvm_rsp_write(&svm->vcpu, hsave->save.rsp); |
| 3425 | kvm_rip_write(&svm->vcpu, hsave->save.rip); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3426 | svm->vmcb->save.dr7 = 0; |
| 3427 | svm->vmcb->save.cpl = 0; |
| 3428 | svm->vmcb->control.exit_int_info = 0; |
| 3429 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 3430 | mark_all_dirty(svm->vmcb); |
| 3431 | |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3432 | kvm_vcpu_unmap(&svm->vcpu, &map, true); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3433 | |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3434 | nested_svm_uninit_mmu_context(&svm->vcpu); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3435 | kvm_mmu_reset_context(&svm->vcpu); |
| 3436 | kvm_mmu_load(&svm->vcpu); |
| 3437 | |
Vitaly Kuznetsov | 619ad84 | 2019-01-07 19:44:51 +0100 | [diff] [blame] | 3438 | /* |
| 3439 | * Drop what we picked up for L2 via svm_complete_interrupts() so it |
| 3440 | * doesn't end up in L1. |
| 3441 | */ |
| 3442 | svm->vcpu.arch.nmi_injected = false; |
| 3443 | kvm_clear_exception_queue(&svm->vcpu); |
| 3444 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 3445 | |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 3446 | return 0; |
| 3447 | } |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3448 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3449 | static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3450 | { |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3451 | /* |
| 3452 | * This function merges the msr permission bitmaps of kvm and the |
Guo Chao | c5ec2e5 | 2012-06-28 15:16:43 +0800 | [diff] [blame] | 3453 | * 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] | 3454 | * the kvm msr permission bitmap may contain zero bits |
| 3455 | */ |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3456 | int i; |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3457 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3458 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
| 3459 | return true; |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3460 | |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3461 | for (i = 0; i < MSRPM_OFFSETS; i++) { |
| 3462 | u32 value, p; |
| 3463 | u64 offset; |
| 3464 | |
| 3465 | if (msrpm_offsets[i] == 0xffffffff) |
| 3466 | break; |
| 3467 | |
Joerg Roedel | 0d6b353 | 2010-03-01 15:34:38 +0100 | [diff] [blame] | 3468 | p = msrpm_offsets[i]; |
| 3469 | offset = svm->nested.vmcb_msrpm + (p * 4); |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3470 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 3471 | if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4)) |
Joerg Roedel | 323c3d8 | 2010-03-01 15:34:37 +0100 | [diff] [blame] | 3472 | return false; |
| 3473 | |
| 3474 | svm->nested.msrpm[p] = svm->msrpm[p] | value; |
| 3475 | } |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3476 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 3477 | svm->vmcb->control.msrpm_base_pa = __sme_set(__pa(svm->nested.msrpm)); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3478 | |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3479 | return true; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3480 | } |
| 3481 | |
Joerg Roedel | 52c65a30 | 2010-08-02 16:46:44 +0200 | [diff] [blame] | 3482 | static bool nested_vmcb_checks(struct vmcb *vmcb) |
| 3483 | { |
| 3484 | if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0) |
| 3485 | return false; |
| 3486 | |
Joerg Roedel | dbe7758 | 2010-08-02 16:46:45 +0200 | [diff] [blame] | 3487 | if (vmcb->control.asid == 0) |
| 3488 | return false; |
| 3489 | |
Tom Lendacky | cea3a19 | 2017-12-04 10:57:24 -0600 | [diff] [blame] | 3490 | if ((vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) && |
| 3491 | !npt_enabled) |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3492 | return false; |
| 3493 | |
Joerg Roedel | 52c65a30 | 2010-08-02 16:46:44 +0200 | [diff] [blame] | 3494 | return true; |
| 3495 | } |
| 3496 | |
Ladi Prosek | c263406 | 2017-10-11 16:54:44 +0200 | [diff] [blame] | 3497 | static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa, |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3498 | struct vmcb *nested_vmcb, struct kvm_host_map *map) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3499 | { |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 3500 | if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3501 | svm->vcpu.arch.hflags |= HF_HIF_MASK; |
| 3502 | else |
| 3503 | svm->vcpu.arch.hflags &= ~HF_HIF_MASK; |
| 3504 | |
Tom Lendacky | cea3a19 | 2017-12-04 10:57:24 -0600 | [diff] [blame] | 3505 | if (nested_vmcb->control.nested_ctl & SVM_NESTED_CTL_NP_ENABLE) { |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3506 | svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3; |
| 3507 | nested_svm_init_mmu_context(&svm->vcpu); |
| 3508 | } |
| 3509 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3510 | /* Load the nested guest state */ |
| 3511 | svm->vmcb->save.es = nested_vmcb->save.es; |
| 3512 | svm->vmcb->save.cs = nested_vmcb->save.cs; |
| 3513 | svm->vmcb->save.ss = nested_vmcb->save.ss; |
| 3514 | svm->vmcb->save.ds = nested_vmcb->save.ds; |
| 3515 | svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; |
| 3516 | svm->vmcb->save.idtr = nested_vmcb->save.idtr; |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 3517 | kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3518 | svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); |
| 3519 | svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); |
| 3520 | svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); |
| 3521 | if (npt_enabled) { |
| 3522 | svm->vmcb->save.cr3 = nested_vmcb->save.cr3; |
| 3523 | svm->vcpu.arch.cr3 = nested_vmcb->save.cr3; |
Joerg Roedel | 0e5cbe3 | 2010-02-24 18:59:11 +0100 | [diff] [blame] | 3524 | } else |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 3525 | (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3); |
Joerg Roedel | 0e5cbe3 | 2010-02-24 18:59:11 +0100 | [diff] [blame] | 3526 | |
| 3527 | /* Guest paging mode is active - reset mmu */ |
| 3528 | kvm_mmu_reset_context(&svm->vcpu); |
| 3529 | |
Joerg Roedel | defbba5 | 2009-08-07 11:49:30 +0200 | [diff] [blame] | 3530 | svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2; |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 3531 | kvm_rax_write(&svm->vcpu, nested_vmcb->save.rax); |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 3532 | kvm_rsp_write(&svm->vcpu, nested_vmcb->save.rsp); |
| 3533 | kvm_rip_write(&svm->vcpu, nested_vmcb->save.rip); |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 3534 | |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3535 | /* In case we don't even reach vcpu_run, the fields are not updated */ |
| 3536 | svm->vmcb->save.rax = nested_vmcb->save.rax; |
| 3537 | svm->vmcb->save.rsp = nested_vmcb->save.rsp; |
| 3538 | svm->vmcb->save.rip = nested_vmcb->save.rip; |
| 3539 | svm->vmcb->save.dr7 = nested_vmcb->save.dr7; |
| 3540 | svm->vmcb->save.dr6 = nested_vmcb->save.dr6; |
| 3541 | svm->vmcb->save.cpl = nested_vmcb->save.cpl; |
| 3542 | |
Joerg Roedel | f713853 | 2010-03-01 15:34:40 +0100 | [diff] [blame] | 3543 | svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL; |
Joerg Roedel | ce2ac08 | 2010-03-01 15:34:39 +0100 | [diff] [blame] | 3544 | svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3545 | |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 3546 | /* cache intercepts */ |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 3547 | svm->nested.intercept_cr = nested_vmcb->control.intercept_cr; |
Joerg Roedel | 3aed041 | 2010-11-30 18:03:58 +0100 | [diff] [blame] | 3548 | svm->nested.intercept_dr = nested_vmcb->control.intercept_dr; |
Joerg Roedel | aad42c6 | 2009-08-07 11:49:34 +0200 | [diff] [blame] | 3549 | svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions; |
| 3550 | svm->nested.intercept = nested_vmcb->control.intercept; |
| 3551 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 3552 | svm_flush_tlb(&svm->vcpu, true); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3553 | 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] | 3554 | if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK) |
| 3555 | svm->vcpu.arch.hflags |= HF_VINTR_MASK; |
| 3556 | else |
| 3557 | svm->vcpu.arch.hflags &= ~HF_VINTR_MASK; |
| 3558 | |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 3559 | if (svm->vcpu.arch.hflags & HF_VINTR_MASK) { |
| 3560 | /* We only want the cr8 intercept bits of the guest */ |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 3561 | clr_cr_intercept(svm, INTERCEPT_CR8_READ); |
| 3562 | clr_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 3563 | } |
| 3564 | |
Joerg Roedel | 0d945bd | 2010-05-05 16:04:45 +0200 | [diff] [blame] | 3565 | /* We don't want to see VMMCALLs from a nested guest */ |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 3566 | clr_intercept(svm, INTERCEPT_VMMCALL); |
Joerg Roedel | 0d945bd | 2010-05-05 16:04:45 +0200 | [diff] [blame] | 3567 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3568 | svm->vcpu.arch.tsc_offset += nested_vmcb->control.tsc_offset; |
| 3569 | svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset; |
| 3570 | |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 3571 | svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3572 | svm->vmcb->control.int_vector = nested_vmcb->control.int_vector; |
| 3573 | svm->vmcb->control.int_state = nested_vmcb->control.int_state; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3574 | svm->vmcb->control.event_inj = nested_vmcb->control.event_inj; |
| 3575 | svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err; |
| 3576 | |
Tambe, William | e081354 | 2018-11-13 16:51:20 +0000 | [diff] [blame] | 3577 | svm->vmcb->control.pause_filter_count = |
| 3578 | nested_vmcb->control.pause_filter_count; |
| 3579 | svm->vmcb->control.pause_filter_thresh = |
| 3580 | nested_vmcb->control.pause_filter_thresh; |
| 3581 | |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3582 | kvm_vcpu_unmap(&svm->vcpu, map, true); |
Joerg Roedel | 9738b2c | 2009-08-07 11:49:41 +0200 | [diff] [blame] | 3583 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 3584 | /* Enter Guest-Mode */ |
| 3585 | enter_guest_mode(&svm->vcpu); |
| 3586 | |
Joerg Roedel | 384c636 | 2010-11-30 18:03:56 +0100 | [diff] [blame] | 3587 | /* |
| 3588 | * Merge guest and host intercepts - must be called with vcpu in |
| 3589 | * guest-mode to take affect here |
| 3590 | */ |
| 3591 | recalc_intercepts(svm); |
| 3592 | |
Joerg Roedel | 06fc7772 | 2010-02-19 16:23:07 +0100 | [diff] [blame] | 3593 | svm->nested.vmcb = vmcb_gpa; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3594 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 3595 | enable_gif(svm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3596 | |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 3597 | mark_all_dirty(svm->vmcb); |
Ladi Prosek | c263406 | 2017-10-11 16:54:44 +0200 | [diff] [blame] | 3598 | } |
| 3599 | |
Vitaly Kuznetsov | e7134c1 | 2019-08-13 15:53:34 +0200 | [diff] [blame] | 3600 | static int nested_svm_vmrun(struct vcpu_svm *svm) |
Ladi Prosek | c263406 | 2017-10-11 16:54:44 +0200 | [diff] [blame] | 3601 | { |
Vitaly Kuznetsov | c8e16b7 | 2019-08-13 15:53:35 +0200 | [diff] [blame] | 3602 | int ret; |
Ladi Prosek | c263406 | 2017-10-11 16:54:44 +0200 | [diff] [blame] | 3603 | struct vmcb *nested_vmcb; |
| 3604 | struct vmcb *hsave = svm->nested.hsave; |
| 3605 | struct vmcb *vmcb = svm->vmcb; |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3606 | struct kvm_host_map map; |
Ladi Prosek | c263406 | 2017-10-11 16:54:44 +0200 | [diff] [blame] | 3607 | u64 vmcb_gpa; |
| 3608 | |
| 3609 | vmcb_gpa = svm->vmcb->save.rax; |
| 3610 | |
Vitaly Kuznetsov | c8e16b7 | 2019-08-13 15:53:35 +0200 | [diff] [blame] | 3611 | ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb_gpa), &map); |
Dan Carpenter | a061985 | 2019-08-27 12:38:52 +0300 | [diff] [blame] | 3612 | if (ret == -EINVAL) { |
Vitaly Kuznetsov | c8e16b7 | 2019-08-13 15:53:35 +0200 | [diff] [blame] | 3613 | kvm_inject_gp(&svm->vcpu, 0); |
Vitaly Kuznetsov | e7134c1 | 2019-08-13 15:53:34 +0200 | [diff] [blame] | 3614 | return 1; |
Vitaly Kuznetsov | c8e16b7 | 2019-08-13 15:53:35 +0200 | [diff] [blame] | 3615 | } else if (ret) { |
| 3616 | return kvm_skip_emulated_instruction(&svm->vcpu); |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3617 | } |
| 3618 | |
Vitaly Kuznetsov | c8e16b7 | 2019-08-13 15:53:35 +0200 | [diff] [blame] | 3619 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
| 3620 | |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3621 | nested_vmcb = map.hva; |
Ladi Prosek | c263406 | 2017-10-11 16:54:44 +0200 | [diff] [blame] | 3622 | |
| 3623 | if (!nested_vmcb_checks(nested_vmcb)) { |
| 3624 | nested_vmcb->control.exit_code = SVM_EXIT_ERR; |
| 3625 | nested_vmcb->control.exit_code_hi = 0; |
| 3626 | nested_vmcb->control.exit_info_1 = 0; |
| 3627 | nested_vmcb->control.exit_info_2 = 0; |
| 3628 | |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3629 | kvm_vcpu_unmap(&svm->vcpu, &map, true); |
Ladi Prosek | c263406 | 2017-10-11 16:54:44 +0200 | [diff] [blame] | 3630 | |
Vitaly Kuznetsov | c8e16b7 | 2019-08-13 15:53:35 +0200 | [diff] [blame] | 3631 | return ret; |
Ladi Prosek | c263406 | 2017-10-11 16:54:44 +0200 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa, |
| 3635 | nested_vmcb->save.rip, |
| 3636 | nested_vmcb->control.int_ctl, |
| 3637 | nested_vmcb->control.event_inj, |
| 3638 | nested_vmcb->control.nested_ctl); |
| 3639 | |
| 3640 | trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff, |
| 3641 | nested_vmcb->control.intercept_cr >> 16, |
| 3642 | nested_vmcb->control.intercept_exceptions, |
| 3643 | nested_vmcb->control.intercept); |
| 3644 | |
| 3645 | /* Clear internal status */ |
| 3646 | kvm_clear_exception_queue(&svm->vcpu); |
| 3647 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 3648 | |
| 3649 | /* |
| 3650 | * Save the old vmcb, so we don't need to pick what we save, but can |
| 3651 | * restore everything when a VMEXIT occurs |
| 3652 | */ |
| 3653 | hsave->save.es = vmcb->save.es; |
| 3654 | hsave->save.cs = vmcb->save.cs; |
| 3655 | hsave->save.ss = vmcb->save.ss; |
| 3656 | hsave->save.ds = vmcb->save.ds; |
| 3657 | hsave->save.gdtr = vmcb->save.gdtr; |
| 3658 | hsave->save.idtr = vmcb->save.idtr; |
| 3659 | hsave->save.efer = svm->vcpu.arch.efer; |
| 3660 | hsave->save.cr0 = kvm_read_cr0(&svm->vcpu); |
| 3661 | hsave->save.cr4 = svm->vcpu.arch.cr4; |
| 3662 | hsave->save.rflags = kvm_get_rflags(&svm->vcpu); |
| 3663 | hsave->save.rip = kvm_rip_read(&svm->vcpu); |
| 3664 | hsave->save.rsp = vmcb->save.rsp; |
| 3665 | hsave->save.rax = vmcb->save.rax; |
| 3666 | if (npt_enabled) |
| 3667 | hsave->save.cr3 = vmcb->save.cr3; |
| 3668 | else |
| 3669 | hsave->save.cr3 = kvm_read_cr3(&svm->vcpu); |
| 3670 | |
| 3671 | copy_vmcb_control_area(hsave, vmcb); |
| 3672 | |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3673 | enter_svm_guest_mode(svm, vmcb_gpa, nested_vmcb, &map); |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 3674 | |
Vitaly Kuznetsov | e7134c1 | 2019-08-13 15:53:34 +0200 | [diff] [blame] | 3675 | if (!nested_svm_vmrun_msrpm(svm)) { |
| 3676 | svm->vmcb->control.exit_code = SVM_EXIT_ERR; |
| 3677 | svm->vmcb->control.exit_code_hi = 0; |
| 3678 | svm->vmcb->control.exit_info_1 = 0; |
| 3679 | svm->vmcb->control.exit_info_2 = 0; |
| 3680 | |
| 3681 | nested_svm_vmexit(svm); |
| 3682 | } |
| 3683 | |
Vitaly Kuznetsov | c8e16b7 | 2019-08-13 15:53:35 +0200 | [diff] [blame] | 3684 | return ret; |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3685 | } |
| 3686 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3687 | 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] | 3688 | { |
| 3689 | to_vmcb->save.fs = from_vmcb->save.fs; |
| 3690 | to_vmcb->save.gs = from_vmcb->save.gs; |
| 3691 | to_vmcb->save.tr = from_vmcb->save.tr; |
| 3692 | to_vmcb->save.ldtr = from_vmcb->save.ldtr; |
| 3693 | to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; |
| 3694 | to_vmcb->save.star = from_vmcb->save.star; |
| 3695 | to_vmcb->save.lstar = from_vmcb->save.lstar; |
| 3696 | to_vmcb->save.cstar = from_vmcb->save.cstar; |
| 3697 | to_vmcb->save.sfmask = from_vmcb->save.sfmask; |
| 3698 | to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; |
| 3699 | to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; |
| 3700 | to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3701 | } |
| 3702 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3703 | static int vmload_interception(struct vcpu_svm *svm) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3704 | { |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3705 | struct vmcb *nested_vmcb; |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3706 | struct kvm_host_map map; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3707 | int ret; |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3708 | |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3709 | if (nested_svm_check_permissions(svm)) |
| 3710 | return 1; |
| 3711 | |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3712 | ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map); |
| 3713 | if (ret) { |
| 3714 | if (ret == -EINVAL) |
| 3715 | kvm_inject_gp(&svm->vcpu, 0); |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3716 | return 1; |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | nested_vmcb = map.hva; |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3720 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3721 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Joerg Roedel | e3e9ed3 | 2011-04-06 12:30:03 +0200 | [diff] [blame] | 3722 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3723 | nested_svm_vmloadsave(nested_vmcb, svm->vmcb); |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3724 | kvm_vcpu_unmap(&svm->vcpu, &map, true); |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3725 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3726 | return ret; |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3727 | } |
| 3728 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3729 | static int vmsave_interception(struct vcpu_svm *svm) |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3730 | { |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3731 | struct vmcb *nested_vmcb; |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3732 | struct kvm_host_map map; |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3733 | int ret; |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3734 | |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3735 | if (nested_svm_check_permissions(svm)) |
| 3736 | return 1; |
| 3737 | |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3738 | ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map); |
| 3739 | if (ret) { |
| 3740 | if (ret == -EINVAL) |
| 3741 | kvm_inject_gp(&svm->vcpu, 0); |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3742 | return 1; |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3743 | } |
| 3744 | |
| 3745 | nested_vmcb = map.hva; |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3746 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3747 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Joerg Roedel | e3e9ed3 | 2011-04-06 12:30:03 +0200 | [diff] [blame] | 3748 | |
Joerg Roedel | 9966bf6 | 2009-08-07 11:49:40 +0200 | [diff] [blame] | 3749 | nested_svm_vmloadsave(svm->vmcb, nested_vmcb); |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 3750 | kvm_vcpu_unmap(&svm->vcpu, &map, true); |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3751 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3752 | return ret; |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 3753 | } |
| 3754 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3755 | static int vmrun_interception(struct vcpu_svm *svm) |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3756 | { |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3757 | if (nested_svm_check_permissions(svm)) |
| 3758 | return 1; |
| 3759 | |
Vitaly Kuznetsov | e7134c1 | 2019-08-13 15:53:34 +0200 | [diff] [blame] | 3760 | return nested_svm_vmrun(svm); |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 3761 | } |
| 3762 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3763 | static int stgi_interception(struct vcpu_svm *svm) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3764 | { |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3765 | int ret; |
| 3766 | |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3767 | if (nested_svm_check_permissions(svm)) |
| 3768 | return 1; |
| 3769 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 3770 | /* |
| 3771 | * If VGIF is enabled, the STGI intercept is only added to |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 3772 | * detect the opening of the SMI/NMI window; remove it now. |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 3773 | */ |
| 3774 | if (vgif_enabled(svm)) |
| 3775 | clr_intercept(svm, INTERCEPT_STGI); |
| 3776 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3777 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 3778 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3779 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 3780 | enable_gif(svm); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3781 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3782 | return ret; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3783 | } |
| 3784 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3785 | static int clgi_interception(struct vcpu_svm *svm) |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3786 | { |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3787 | int ret; |
| 3788 | |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3789 | if (nested_svm_check_permissions(svm)) |
| 3790 | return 1; |
| 3791 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3792 | ret = kvm_skip_emulated_instruction(&svm->vcpu); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3793 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 3794 | disable_gif(svm); |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3795 | |
| 3796 | /* After a CLGI no interrupts should come */ |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 3797 | if (!kvm_vcpu_apicv_active(&svm->vcpu)) { |
| 3798 | svm_clear_vintr(svm); |
| 3799 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
| 3800 | mark_dirty(svm->vmcb, VMCB_INTR); |
| 3801 | } |
Joerg Roedel | decdbf6 | 2010-12-03 11:45:52 +0100 | [diff] [blame] | 3802 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3803 | return ret; |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 3804 | } |
| 3805 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3806 | static int invlpga_interception(struct vcpu_svm *svm) |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3807 | { |
| 3808 | struct kvm_vcpu *vcpu = &svm->vcpu; |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3809 | |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 3810 | trace_kvm_invlpga(svm->vmcb->save.rip, kvm_rcx_read(&svm->vcpu), |
| 3811 | kvm_rax_read(&svm->vcpu)); |
Joerg Roedel | ec1ff79 | 2009-10-09 16:08:31 +0200 | [diff] [blame] | 3812 | |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3813 | /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */ |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 3814 | kvm_mmu_invlpg(vcpu, kvm_rax_read(&svm->vcpu)); |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3815 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3816 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 3817 | } |
| 3818 | |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 3819 | static int skinit_interception(struct vcpu_svm *svm) |
| 3820 | { |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 3821 | trace_kvm_skinit(svm->vmcb->save.rip, kvm_rax_read(&svm->vcpu)); |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 3822 | |
| 3823 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 3824 | return 1; |
| 3825 | } |
| 3826 | |
David Kaplan | dab429a | 2015-03-02 13:43:37 -0600 | [diff] [blame] | 3827 | static int wbinvd_interception(struct vcpu_svm *svm) |
| 3828 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 3829 | return kvm_emulate_wbinvd(&svm->vcpu); |
David Kaplan | dab429a | 2015-03-02 13:43:37 -0600 | [diff] [blame] | 3830 | } |
| 3831 | |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 3832 | static int xsetbv_interception(struct vcpu_svm *svm) |
| 3833 | { |
| 3834 | u64 new_bv = kvm_read_edx_eax(&svm->vcpu); |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 3835 | u32 index = kvm_rcx_read(&svm->vcpu); |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 3836 | |
| 3837 | if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) { |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3838 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | return 1; |
| 3842 | } |
| 3843 | |
Jim Mattson | 0cb8410 | 2019-09-19 15:59:17 -0700 | [diff] [blame] | 3844 | static int rdpru_interception(struct vcpu_svm *svm) |
| 3845 | { |
| 3846 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 3847 | return 1; |
| 3848 | } |
| 3849 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3850 | static int task_switch_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3851 | { |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3852 | u16 tss_selector; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3853 | int reason; |
| 3854 | int int_type = svm->vmcb->control.exit_int_info & |
| 3855 | SVM_EXITINTINFO_TYPE_MASK; |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 3856 | 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] | 3857 | uint32_t type = |
| 3858 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK; |
| 3859 | uint32_t idt_v = |
| 3860 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3861 | bool has_error_code = false; |
| 3862 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3863 | |
| 3864 | tss_selector = (u16)svm->vmcb->control.exit_info_1; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3865 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 3866 | if (svm->vmcb->control.exit_info_2 & |
| 3867 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3868 | reason = TASK_SWITCH_IRET; |
| 3869 | else if (svm->vmcb->control.exit_info_2 & |
| 3870 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) |
| 3871 | reason = TASK_SWITCH_JMP; |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 3872 | else if (idt_v) |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3873 | reason = TASK_SWITCH_GATE; |
| 3874 | else |
| 3875 | reason = TASK_SWITCH_CALL; |
| 3876 | |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 3877 | if (reason == TASK_SWITCH_GATE) { |
| 3878 | switch (type) { |
| 3879 | case SVM_EXITINTINFO_TYPE_NMI: |
| 3880 | svm->vcpu.arch.nmi_injected = false; |
| 3881 | break; |
| 3882 | case SVM_EXITINTINFO_TYPE_EXEPT: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 3883 | if (svm->vmcb->control.exit_info_2 & |
| 3884 | (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) { |
| 3885 | has_error_code = true; |
| 3886 | error_code = |
| 3887 | (u32)svm->vmcb->control.exit_info_2; |
| 3888 | } |
Gleb Natapov | fe8e7f8 | 2009-04-23 17:03:48 +0300 | [diff] [blame] | 3889 | kvm_clear_exception_queue(&svm->vcpu); |
| 3890 | break; |
| 3891 | case SVM_EXITINTINFO_TYPE_INTR: |
| 3892 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 3893 | break; |
| 3894 | default: |
| 3895 | break; |
| 3896 | } |
| 3897 | } |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3898 | |
Gleb Natapov | 8317c29 | 2009-04-12 13:37:02 +0300 | [diff] [blame] | 3899 | if (reason != TASK_SWITCH_GATE || |
| 3900 | int_type == SVM_EXITINTINFO_TYPE_SOFT || |
| 3901 | (int_type == SVM_EXITINTINFO_TYPE_EXEPT && |
Vitaly Kuznetsov | f8ea7c6 | 2019-08-13 15:53:30 +0200 | [diff] [blame] | 3902 | (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) { |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 3903 | if (!skip_emulated_instruction(&svm->vcpu)) |
Sean Christopherson | 738fece | 2019-08-27 14:40:34 -0700 | [diff] [blame] | 3904 | return 0; |
Vitaly Kuznetsov | f8ea7c6 | 2019-08-13 15:53:30 +0200 | [diff] [blame] | 3905 | } |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 3906 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 3907 | if (int_type != SVM_EXITINTINFO_TYPE_SOFT) |
| 3908 | int_vec = -1; |
| 3909 | |
Sean Christopherson | 1051778 | 2019-08-27 14:40:35 -0700 | [diff] [blame] | 3910 | return kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason, |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 3911 | has_error_code, error_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3912 | } |
| 3913 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3914 | static int cpuid_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3915 | { |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 3916 | return kvm_emulate_cpuid(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3917 | } |
| 3918 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3919 | static int iret_interception(struct vcpu_svm *svm) |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3920 | { |
| 3921 | ++svm->vcpu.stat.nmi_window_exits; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 3922 | clr_intercept(svm, INTERCEPT_IRET); |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 3923 | svm->vcpu.arch.hflags |= HF_IRET_MASK; |
Avi Kivity | bd3d1ec | 2011-02-03 15:29:52 +0200 | [diff] [blame] | 3924 | svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu); |
Radim Krčmář | f303b4c | 2014-01-17 20:52:42 +0100 | [diff] [blame] | 3925 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 3926 | return 1; |
| 3927 | } |
| 3928 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3929 | static int invlpg_interception(struct vcpu_svm *svm) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3930 | { |
Andre Przywara | df4f3108 | 2010-12-21 11:12:06 +0100 | [diff] [blame] | 3931 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 3932 | return kvm_emulate_instruction(&svm->vcpu, 0); |
Andre Przywara | df4f3108 | 2010-12-21 11:12:06 +0100 | [diff] [blame] | 3933 | |
| 3934 | kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1); |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 3935 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3936 | } |
| 3937 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3938 | static int emulate_on_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3939 | { |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 3940 | return kvm_emulate_instruction(&svm->vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3941 | } |
| 3942 | |
Brijesh Singh | 7607b71 | 2018-02-19 10:14:44 -0600 | [diff] [blame] | 3943 | static int rsm_interception(struct vcpu_svm *svm) |
| 3944 | { |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 3945 | return kvm_emulate_instruction_from_buffer(&svm->vcpu, rsm_ins_bytes, 2); |
Brijesh Singh | 7607b71 | 2018-02-19 10:14:44 -0600 | [diff] [blame] | 3946 | } |
| 3947 | |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 3948 | static int rdpmc_interception(struct vcpu_svm *svm) |
| 3949 | { |
| 3950 | int err; |
| 3951 | |
Paolo Bonzini | d647eb6 | 2019-06-20 14:13:33 +0200 | [diff] [blame] | 3952 | if (!nrips) |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 3953 | return emulate_on_interception(svm); |
| 3954 | |
| 3955 | err = kvm_rdpmc(&svm->vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 3956 | return kvm_complete_insn_gp(&svm->vcpu, err); |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 3957 | } |
| 3958 | |
Xiubo Li | 52eb5a6 | 2015-03-13 17:39:45 +0800 | [diff] [blame] | 3959 | static bool check_selective_cr0_intercepted(struct vcpu_svm *svm, |
| 3960 | unsigned long val) |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 3961 | { |
| 3962 | unsigned long cr0 = svm->vcpu.arch.cr0; |
| 3963 | bool ret = false; |
| 3964 | u64 intercept; |
| 3965 | |
| 3966 | intercept = svm->nested.intercept; |
| 3967 | |
| 3968 | if (!is_guest_mode(&svm->vcpu) || |
| 3969 | (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))) |
| 3970 | return false; |
| 3971 | |
| 3972 | cr0 &= ~SVM_CR0_SELECTIVE_MASK; |
| 3973 | val &= ~SVM_CR0_SELECTIVE_MASK; |
| 3974 | |
| 3975 | if (cr0 ^ val) { |
| 3976 | svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE; |
| 3977 | ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE); |
| 3978 | } |
| 3979 | |
| 3980 | return ret; |
| 3981 | } |
| 3982 | |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 3983 | #define CR_VALID (1ULL << 63) |
| 3984 | |
| 3985 | static int cr_interception(struct vcpu_svm *svm) |
| 3986 | { |
| 3987 | int reg, cr; |
| 3988 | unsigned long val; |
| 3989 | int err; |
| 3990 | |
| 3991 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) |
| 3992 | return emulate_on_interception(svm); |
| 3993 | |
| 3994 | if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0)) |
| 3995 | return emulate_on_interception(svm); |
| 3996 | |
| 3997 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; |
David Kaplan | 5e57518 | 2015-03-06 14:44:35 -0600 | [diff] [blame] | 3998 | if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE) |
| 3999 | cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0; |
| 4000 | else |
| 4001 | cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0; |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4002 | |
| 4003 | err = 0; |
| 4004 | if (cr >= 16) { /* mov to cr */ |
| 4005 | cr -= 16; |
| 4006 | val = kvm_register_read(&svm->vcpu, reg); |
| 4007 | switch (cr) { |
| 4008 | case 0: |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 4009 | if (!check_selective_cr0_intercepted(svm, val)) |
| 4010 | err = kvm_set_cr0(&svm->vcpu, val); |
Joerg Roedel | 977b2d0 | 2011-04-18 11:42:52 +0200 | [diff] [blame] | 4011 | else |
| 4012 | return 1; |
| 4013 | |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4014 | break; |
| 4015 | case 3: |
| 4016 | err = kvm_set_cr3(&svm->vcpu, val); |
| 4017 | break; |
| 4018 | case 4: |
| 4019 | err = kvm_set_cr4(&svm->vcpu, val); |
| 4020 | break; |
| 4021 | case 8: |
| 4022 | err = kvm_set_cr8(&svm->vcpu, val); |
| 4023 | break; |
| 4024 | default: |
| 4025 | WARN(1, "unhandled write to CR%d", cr); |
| 4026 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 4027 | return 1; |
| 4028 | } |
| 4029 | } else { /* mov from cr */ |
| 4030 | switch (cr) { |
| 4031 | case 0: |
| 4032 | val = kvm_read_cr0(&svm->vcpu); |
| 4033 | break; |
| 4034 | case 2: |
| 4035 | val = svm->vcpu.arch.cr2; |
| 4036 | break; |
| 4037 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 4038 | val = kvm_read_cr3(&svm->vcpu); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4039 | break; |
| 4040 | case 4: |
| 4041 | val = kvm_read_cr4(&svm->vcpu); |
| 4042 | break; |
| 4043 | case 8: |
| 4044 | val = kvm_get_cr8(&svm->vcpu); |
| 4045 | break; |
| 4046 | default: |
| 4047 | WARN(1, "unhandled read from CR%d", cr); |
| 4048 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
| 4049 | return 1; |
| 4050 | } |
| 4051 | kvm_register_write(&svm->vcpu, reg, val); |
| 4052 | } |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4053 | return kvm_complete_insn_gp(&svm->vcpu, err); |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4054 | } |
| 4055 | |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 4056 | static int dr_interception(struct vcpu_svm *svm) |
| 4057 | { |
| 4058 | int reg, dr; |
| 4059 | unsigned long val; |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 4060 | |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 4061 | if (svm->vcpu.guest_debug == 0) { |
| 4062 | /* |
| 4063 | * No more DR vmexits; force a reload of the debug registers |
| 4064 | * and reenter on this instruction. The next vmexit will |
| 4065 | * retrieve the full state of the debug registers. |
| 4066 | */ |
| 4067 | clr_dr_intercepts(svm); |
| 4068 | svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 4069 | return 1; |
| 4070 | } |
| 4071 | |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 4072 | if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS)) |
| 4073 | return emulate_on_interception(svm); |
| 4074 | |
| 4075 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; |
| 4076 | dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0; |
| 4077 | |
| 4078 | if (dr >= 16) { /* mov to DRn */ |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 4079 | if (!kvm_require_dr(&svm->vcpu, dr - 16)) |
| 4080 | return 1; |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 4081 | val = kvm_register_read(&svm->vcpu, reg); |
| 4082 | kvm_set_dr(&svm->vcpu, dr - 16, val); |
| 4083 | } else { |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 4084 | if (!kvm_require_dr(&svm->vcpu, dr)) |
| 4085 | return 1; |
| 4086 | kvm_get_dr(&svm->vcpu, dr, &val); |
| 4087 | kvm_register_write(&svm->vcpu, reg, val); |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 4088 | } |
| 4089 | |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 4090 | return kvm_skip_emulated_instruction(&svm->vcpu); |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 4091 | } |
| 4092 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4093 | static int cr8_write_interception(struct vcpu_svm *svm) |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 4094 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4095 | struct kvm_run *kvm_run = svm->vcpu.run; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 4096 | int r; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4097 | |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4098 | u8 cr8_prev = kvm_get_cr8(&svm->vcpu); |
| 4099 | /* instruction emulation calls kvm_set_cr8() */ |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4100 | r = cr_interception(svm); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 4101 | if (lapic_in_kernel(&svm->vcpu)) |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4102 | return r; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4103 | if (cr8_prev <= kvm_get_cr8(&svm->vcpu)) |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4104 | return r; |
Joerg Roedel | 1d07543 | 2007-12-06 21:02:25 +0100 | [diff] [blame] | 4105 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
| 4106 | return 0; |
| 4107 | } |
| 4108 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 4109 | static int svm_get_msr_feature(struct kvm_msr_entry *msr) |
| 4110 | { |
Tom Lendacky | d1d93fa | 2018-02-24 00:18:20 +0100 | [diff] [blame] | 4111 | msr->data = 0; |
| 4112 | |
| 4113 | switch (msr->index) { |
| 4114 | case MSR_F10H_DECFG: |
| 4115 | if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) |
| 4116 | msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE; |
| 4117 | break; |
| 4118 | default: |
| 4119 | return 1; |
| 4120 | } |
| 4121 | |
| 4122 | return 0; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 4123 | } |
| 4124 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4125 | 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] | 4126 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4127 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4128 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4129 | switch (msr_info->index) { |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 4130 | case MSR_STAR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4131 | msr_info->data = svm->vmcb->save.star; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4132 | break; |
Avi Kivity | 0e859ca | 2006-12-22 01:05:08 -0800 | [diff] [blame] | 4133 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4134 | case MSR_LSTAR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4135 | msr_info->data = svm->vmcb->save.lstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4136 | break; |
| 4137 | case MSR_CSTAR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4138 | msr_info->data = svm->vmcb->save.cstar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4139 | break; |
| 4140 | case MSR_KERNEL_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4141 | msr_info->data = svm->vmcb->save.kernel_gs_base; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4142 | break; |
| 4143 | case MSR_SYSCALL_MASK: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4144 | msr_info->data = svm->vmcb->save.sfmask; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4145 | break; |
| 4146 | #endif |
| 4147 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4148 | msr_info->data = svm->vmcb->save.sysenter_cs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4149 | break; |
| 4150 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4151 | msr_info->data = svm->sysenter_eip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4152 | break; |
| 4153 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4154 | msr_info->data = svm->sysenter_esp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4155 | break; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 4156 | case MSR_TSC_AUX: |
| 4157 | if (!boot_cpu_has(X86_FEATURE_RDTSCP)) |
| 4158 | return 1; |
| 4159 | msr_info->data = svm->tsc_aux; |
| 4160 | break; |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4161 | /* |
| 4162 | * Nobody will change the following 5 values in the VMCB so we can |
| 4163 | * safely return them on rdmsr. They will always be 0 until LBRV is |
| 4164 | * implemented. |
| 4165 | */ |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 4166 | case MSR_IA32_DEBUGCTLMSR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4167 | msr_info->data = svm->vmcb->save.dbgctl; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 4168 | break; |
| 4169 | case MSR_IA32_LASTBRANCHFROMIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4170 | msr_info->data = svm->vmcb->save.br_from; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 4171 | break; |
| 4172 | case MSR_IA32_LASTBRANCHTOIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4173 | msr_info->data = svm->vmcb->save.br_to; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 4174 | break; |
| 4175 | case MSR_IA32_LASTINTFROMIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4176 | msr_info->data = svm->vmcb->save.last_excp_from; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 4177 | break; |
| 4178 | case MSR_IA32_LASTINTTOIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4179 | msr_info->data = svm->vmcb->save.last_excp_to; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 4180 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 4181 | case MSR_VM_HSAVE_PA: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4182 | msr_info->data = svm->nested.hsave_msr; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 4183 | break; |
Joerg Roedel | eb6f302 | 2008-11-25 20:17:09 +0100 | [diff] [blame] | 4184 | case MSR_VM_CR: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4185 | msr_info->data = svm->nested.vm_cr_msr; |
Joerg Roedel | eb6f302 | 2008-11-25 20:17:09 +0100 | [diff] [blame] | 4186 | break; |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 4187 | case MSR_IA32_SPEC_CTRL: |
| 4188 | if (!msr_info->host_initiated && |
Konrad Rzeszutek Wilk | 6ac2f49 | 2018-06-01 10:59:20 -0400 | [diff] [blame] | 4189 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) && |
| 4190 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD)) |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 4191 | return 1; |
| 4192 | |
| 4193 | msr_info->data = svm->spec_ctrl; |
| 4194 | break; |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 4195 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 4196 | if (!msr_info->host_initiated && |
| 4197 | !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD)) |
| 4198 | return 1; |
| 4199 | |
| 4200 | msr_info->data = svm->virt_spec_ctrl; |
| 4201 | break; |
Borislav Petkov | ae8b787 | 2015-11-23 11:12:23 +0100 | [diff] [blame] | 4202 | case MSR_F15H_IC_CFG: { |
| 4203 | |
| 4204 | int family, model; |
| 4205 | |
| 4206 | family = guest_cpuid_family(vcpu); |
| 4207 | model = guest_cpuid_model(vcpu); |
| 4208 | |
| 4209 | if (family < 0 || model < 0) |
| 4210 | return kvm_get_msr_common(vcpu, msr_info); |
| 4211 | |
| 4212 | msr_info->data = 0; |
| 4213 | |
| 4214 | if (family == 0x15 && |
| 4215 | (model >= 0x2 && model < 0x20)) |
| 4216 | msr_info->data = 0x1E; |
| 4217 | } |
| 4218 | break; |
Tom Lendacky | d1d93fa | 2018-02-24 00:18:20 +0100 | [diff] [blame] | 4219 | case MSR_F10H_DECFG: |
| 4220 | msr_info->data = svm->msr_decfg; |
| 4221 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4222 | default: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4223 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4224 | } |
| 4225 | return 0; |
| 4226 | } |
| 4227 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4228 | static int rdmsr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4229 | { |
Sean Christopherson | 1edce0a | 2019-09-05 14:22:55 -0700 | [diff] [blame] | 4230 | return kvm_emulate_rdmsr(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4231 | } |
| 4232 | |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 4233 | static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data) |
| 4234 | { |
| 4235 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4236 | int svm_dis, chg_mask; |
| 4237 | |
| 4238 | if (data & ~SVM_VM_CR_VALID_MASK) |
| 4239 | return 1; |
| 4240 | |
| 4241 | chg_mask = SVM_VM_CR_VALID_MASK; |
| 4242 | |
| 4243 | if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK) |
| 4244 | chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK); |
| 4245 | |
| 4246 | svm->nested.vm_cr_msr &= ~chg_mask; |
| 4247 | svm->nested.vm_cr_msr |= (data & chg_mask); |
| 4248 | |
| 4249 | svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK; |
| 4250 | |
| 4251 | /* check for svm_disable while efer.svme is set */ |
| 4252 | if (svm_dis && (vcpu->arch.efer & EFER_SVME)) |
| 4253 | return 1; |
| 4254 | |
| 4255 | return 0; |
| 4256 | } |
| 4257 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4258 | 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] | 4259 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4260 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4261 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4262 | u32 ecx = msr->index; |
| 4263 | u64 data = msr->data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4264 | switch (ecx) { |
Paolo Bonzini | 15038e1 | 2017-10-26 09:13:27 +0200 | [diff] [blame] | 4265 | case MSR_IA32_CR_PAT: |
| 4266 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 4267 | return 1; |
| 4268 | vcpu->arch.pat = data; |
| 4269 | svm->vmcb->save.g_pat = data; |
| 4270 | mark_dirty(svm->vmcb, VMCB_NPT); |
| 4271 | break; |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 4272 | case MSR_IA32_SPEC_CTRL: |
| 4273 | if (!msr->host_initiated && |
Konrad Rzeszutek Wilk | 6ac2f49 | 2018-06-01 10:59:20 -0400 | [diff] [blame] | 4274 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) && |
| 4275 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD)) |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 4276 | return 1; |
| 4277 | |
| 4278 | /* The STIBP bit doesn't fault even if it's not advertised */ |
Konrad Rzeszutek Wilk | 6ac2f49 | 2018-06-01 10:59:20 -0400 | [diff] [blame] | 4279 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 4280 | return 1; |
| 4281 | |
| 4282 | svm->spec_ctrl = data; |
| 4283 | |
| 4284 | if (!data) |
| 4285 | break; |
| 4286 | |
| 4287 | /* |
| 4288 | * For non-nested: |
| 4289 | * When it's written (to non-zero) for the first time, pass |
| 4290 | * it through. |
| 4291 | * |
| 4292 | * For nested: |
| 4293 | * The handling of the MSR bitmap for L2 guests is done in |
| 4294 | * nested_svm_vmrun_msrpm. |
| 4295 | * We update the L1 MSR bit as well since it will end up |
| 4296 | * touching the MSR anyway now. |
| 4297 | */ |
| 4298 | set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1); |
| 4299 | break; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 4300 | case MSR_IA32_PRED_CMD: |
| 4301 | if (!msr->host_initiated && |
Borislav Petkov | e7c587d | 2018-05-02 18:15:14 +0200 | [diff] [blame] | 4302 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB)) |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 4303 | return 1; |
| 4304 | |
| 4305 | if (data & ~PRED_CMD_IBPB) |
| 4306 | return 1; |
| 4307 | |
| 4308 | if (!data) |
| 4309 | break; |
| 4310 | |
| 4311 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 4312 | if (is_guest_mode(vcpu)) |
| 4313 | break; |
| 4314 | set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1); |
| 4315 | break; |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 4316 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 4317 | if (!msr->host_initiated && |
| 4318 | !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD)) |
| 4319 | return 1; |
| 4320 | |
| 4321 | if (data & ~SPEC_CTRL_SSBD) |
| 4322 | return 1; |
| 4323 | |
| 4324 | svm->virt_spec_ctrl = data; |
| 4325 | break; |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 4326 | case MSR_STAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4327 | svm->vmcb->save.star = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4328 | break; |
Robert P. J. Day | 49b14f2 | 2007-01-29 13:19:50 -0800 | [diff] [blame] | 4329 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4330 | case MSR_LSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4331 | svm->vmcb->save.lstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4332 | break; |
| 4333 | case MSR_CSTAR: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4334 | svm->vmcb->save.cstar = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4335 | break; |
| 4336 | case MSR_KERNEL_GS_BASE: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4337 | svm->vmcb->save.kernel_gs_base = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4338 | break; |
| 4339 | case MSR_SYSCALL_MASK: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4340 | svm->vmcb->save.sfmask = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4341 | break; |
| 4342 | #endif |
| 4343 | case MSR_IA32_SYSENTER_CS: |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4344 | svm->vmcb->save.sysenter_cs = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4345 | break; |
| 4346 | case MSR_IA32_SYSENTER_EIP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 4347 | svm->sysenter_eip = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4348 | svm->vmcb->save.sysenter_eip = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4349 | break; |
| 4350 | case MSR_IA32_SYSENTER_ESP: |
Andre Przywara | 017cb99 | 2009-05-28 11:56:31 +0200 | [diff] [blame] | 4351 | svm->sysenter_esp = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4352 | svm->vmcb->save.sysenter_esp = data; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4353 | break; |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 4354 | case MSR_TSC_AUX: |
| 4355 | if (!boot_cpu_has(X86_FEATURE_RDTSCP)) |
| 4356 | return 1; |
| 4357 | |
| 4358 | /* |
| 4359 | * This is rare, so we update the MSR here instead of using |
| 4360 | * direct_access_msrs. Doing that would require a rdmsr in |
| 4361 | * svm_vcpu_put. |
| 4362 | */ |
| 4363 | svm->tsc_aux = data; |
| 4364 | wrmsrl(MSR_TSC_AUX, svm->tsc_aux); |
| 4365 | break; |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 4366 | case MSR_IA32_DEBUGCTLMSR: |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 4367 | if (!boot_cpu_has(X86_FEATURE_LBRV)) { |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 4368 | vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", |
| 4369 | __func__, data); |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 4370 | break; |
| 4371 | } |
| 4372 | if (data & DEBUGCTL_RESERVED_BITS) |
| 4373 | return 1; |
| 4374 | |
| 4375 | svm->vmcb->save.dbgctl = data; |
Joerg Roedel | b53ba3f | 2010-12-03 11:45:59 +0100 | [diff] [blame] | 4376 | mark_dirty(svm->vmcb, VMCB_LBR); |
Joerg Roedel | 24e09cb | 2008-02-13 18:58:47 +0100 | [diff] [blame] | 4377 | if (data & (1ULL<<0)) |
| 4378 | svm_enable_lbrv(svm); |
| 4379 | else |
| 4380 | svm_disable_lbrv(svm); |
Joerg Roedel | a2938c8 | 2008-02-13 16:30:28 +0100 | [diff] [blame] | 4381 | break; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 4382 | case MSR_VM_HSAVE_PA: |
Joerg Roedel | e6aa9ab | 2009-08-07 11:49:33 +0200 | [diff] [blame] | 4383 | svm->nested.hsave_msr = data; |
Alexander Graf | b286d5d | 2008-11-25 20:17:05 +0100 | [diff] [blame] | 4384 | break; |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 4385 | case MSR_VM_CR: |
Joerg Roedel | 4a81018 | 2010-02-24 18:59:15 +0100 | [diff] [blame] | 4386 | return svm_set_vm_cr(vcpu, data); |
Alexander Graf | 3c5d0a4 | 2009-06-15 15:21:23 +0200 | [diff] [blame] | 4387 | case MSR_VM_IGNNE: |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 4388 | 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] | 4389 | break; |
Tom Lendacky | d1d93fa | 2018-02-24 00:18:20 +0100 | [diff] [blame] | 4390 | case MSR_F10H_DECFG: { |
| 4391 | struct kvm_msr_entry msr_entry; |
| 4392 | |
| 4393 | msr_entry.index = msr->index; |
| 4394 | if (svm_get_msr_feature(&msr_entry)) |
| 4395 | return 1; |
| 4396 | |
| 4397 | /* Check the supported bits */ |
| 4398 | if (data & ~msr_entry.data) |
| 4399 | return 1; |
| 4400 | |
| 4401 | /* Don't allow the guest to change a bit, #GP */ |
| 4402 | if (!msr->host_initiated && (data ^ msr_entry.data)) |
| 4403 | return 1; |
| 4404 | |
| 4405 | svm->msr_decfg = data; |
| 4406 | break; |
| 4407 | } |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4408 | case MSR_IA32_APICBASE: |
| 4409 | if (kvm_vcpu_apicv_active(vcpu)) |
| 4410 | avic_update_vapic_bar(to_svm(vcpu), data); |
Gustavo A. R. Silva | b2869f2 | 2019-01-25 12:23:17 -0600 | [diff] [blame] | 4411 | /* Fall through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4412 | default: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4413 | return kvm_set_msr_common(vcpu, msr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4414 | } |
| 4415 | return 0; |
| 4416 | } |
| 4417 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4418 | static int wrmsr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4419 | { |
Sean Christopherson | 1edce0a | 2019-09-05 14:22:55 -0700 | [diff] [blame] | 4420 | return kvm_emulate_wrmsr(&svm->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4421 | } |
| 4422 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4423 | static int msr_interception(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4424 | { |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 4425 | if (svm->vmcb->control.exit_info_1) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4426 | return wrmsr_interception(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4427 | else |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4428 | return rdmsr_interception(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4429 | } |
| 4430 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4431 | static int interrupt_window_interception(struct vcpu_svm *svm) |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4432 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4433 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
Alexander Graf | f0b8505 | 2008-11-25 20:17:01 +0100 | [diff] [blame] | 4434 | svm_clear_vintr(svm); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4435 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
Joerg Roedel | decdbf6 | 2010-12-03 11:45:52 +0100 | [diff] [blame] | 4436 | mark_dirty(svm->vmcb, VMCB_INTR); |
Jason Wang | 675acb7 | 2012-03-08 18:07:56 +0800 | [diff] [blame] | 4437 | ++svm->vcpu.stat.irq_window_exits; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4438 | return 1; |
| 4439 | } |
| 4440 | |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 4441 | static int pause_interception(struct vcpu_svm *svm) |
| 4442 | { |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 4443 | struct kvm_vcpu *vcpu = &svm->vcpu; |
| 4444 | bool in_kernel = (svm_get_cpl(vcpu) == 0); |
| 4445 | |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 4446 | if (pause_filter_thresh) |
| 4447 | grow_ple_window(vcpu); |
| 4448 | |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 4449 | kvm_vcpu_on_spin(vcpu, in_kernel); |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 4450 | return 1; |
| 4451 | } |
| 4452 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 4453 | static int nop_interception(struct vcpu_svm *svm) |
| 4454 | { |
Ladi Prosek | b742c1e | 2017-06-22 09:05:26 +0200 | [diff] [blame] | 4455 | return kvm_skip_emulated_instruction(&(svm->vcpu)); |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 4456 | } |
| 4457 | |
| 4458 | static int monitor_interception(struct vcpu_svm *svm) |
| 4459 | { |
| 4460 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 4461 | return nop_interception(svm); |
| 4462 | } |
| 4463 | |
| 4464 | static int mwait_interception(struct vcpu_svm *svm) |
| 4465 | { |
| 4466 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 4467 | return nop_interception(svm); |
| 4468 | } |
| 4469 | |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4470 | enum avic_ipi_failure_cause { |
| 4471 | AVIC_IPI_FAILURE_INVALID_INT_TYPE, |
| 4472 | AVIC_IPI_FAILURE_TARGET_NOT_RUNNING, |
| 4473 | AVIC_IPI_FAILURE_INVALID_TARGET, |
| 4474 | AVIC_IPI_FAILURE_INVALID_BACKING_PAGE, |
| 4475 | }; |
| 4476 | |
| 4477 | static int avic_incomplete_ipi_interception(struct vcpu_svm *svm) |
| 4478 | { |
| 4479 | u32 icrh = svm->vmcb->control.exit_info_1 >> 32; |
| 4480 | u32 icrl = svm->vmcb->control.exit_info_1; |
| 4481 | u32 id = svm->vmcb->control.exit_info_2 >> 32; |
Dan Carpenter | 5446a97 | 2016-05-23 13:20:10 +0300 | [diff] [blame] | 4482 | u32 index = svm->vmcb->control.exit_info_2 & 0xFF; |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4483 | struct kvm_lapic *apic = svm->vcpu.arch.apic; |
| 4484 | |
| 4485 | trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index); |
| 4486 | |
| 4487 | switch (id) { |
| 4488 | case AVIC_IPI_FAILURE_INVALID_INT_TYPE: |
| 4489 | /* |
| 4490 | * AVIC hardware handles the generation of |
| 4491 | * IPIs when the specified Message Type is Fixed |
| 4492 | * (also known as fixed delivery mode) and |
| 4493 | * the Trigger Mode is edge-triggered. The hardware |
| 4494 | * also supports self and broadcast delivery modes |
| 4495 | * specified via the Destination Shorthand(DSH) |
| 4496 | * field of the ICRL. Logical and physical APIC ID |
| 4497 | * formats are supported. All other IPI types cause |
| 4498 | * a #VMEXIT, which needs to emulated. |
| 4499 | */ |
| 4500 | kvm_lapic_reg_write(apic, APIC_ICR2, icrh); |
| 4501 | kvm_lapic_reg_write(apic, APIC_ICR, icrl); |
| 4502 | break; |
| 4503 | case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: { |
Suthikulpanit, Suravee | 4a58038 | 2019-03-20 08:12:28 +0000 | [diff] [blame] | 4504 | int i; |
| 4505 | struct kvm_vcpu *vcpu; |
| 4506 | struct kvm *kvm = svm->vcpu.kvm; |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4507 | struct kvm_lapic *apic = svm->vcpu.arch.apic; |
| 4508 | |
| 4509 | /* |
Suthikulpanit, Suravee | 4a58038 | 2019-03-20 08:12:28 +0000 | [diff] [blame] | 4510 | * At this point, we expect that the AVIC HW has already |
| 4511 | * set the appropriate IRR bits on the valid target |
| 4512 | * vcpus. So, we just need to kick the appropriate vcpu. |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4513 | */ |
Suthikulpanit, Suravee | 4a58038 | 2019-03-20 08:12:28 +0000 | [diff] [blame] | 4514 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 4515 | bool m = kvm_apic_match_dest(vcpu, apic, |
| 4516 | icrl & KVM_APIC_SHORT_MASK, |
| 4517 | GET_APIC_DEST_FIELD(icrh), |
| 4518 | icrl & KVM_APIC_DEST_MASK); |
| 4519 | |
| 4520 | if (m && !avic_vcpu_is_running(vcpu)) |
| 4521 | kvm_vcpu_wake_up(vcpu); |
| 4522 | } |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4523 | break; |
| 4524 | } |
| 4525 | case AVIC_IPI_FAILURE_INVALID_TARGET: |
Suravee Suthikulpanit | 37ef0c4 | 2019-01-22 10:24:19 +0000 | [diff] [blame] | 4526 | WARN_ONCE(1, "Invalid IPI target: index=%u, vcpu=%d, icr=%#0x:%#0x\n", |
| 4527 | index, svm->vcpu.vcpu_id, icrh, icrl); |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4528 | break; |
| 4529 | case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE: |
| 4530 | WARN_ONCE(1, "Invalid backing page\n"); |
| 4531 | break; |
| 4532 | default: |
| 4533 | pr_err("Unknown IPI interception\n"); |
| 4534 | } |
| 4535 | |
| 4536 | return 1; |
| 4537 | } |
| 4538 | |
| 4539 | static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat) |
| 4540 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 4541 | struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4542 | int index; |
| 4543 | u32 *logical_apic_id_table; |
| 4544 | int dlid = GET_APIC_LOGICAL_ID(ldr); |
| 4545 | |
| 4546 | if (!dlid) |
| 4547 | return NULL; |
| 4548 | |
| 4549 | if (flat) { /* flat */ |
| 4550 | index = ffs(dlid) - 1; |
| 4551 | if (index > 7) |
| 4552 | return NULL; |
| 4553 | } else { /* cluster */ |
| 4554 | int cluster = (dlid & 0xf0) >> 4; |
| 4555 | int apic = ffs(dlid & 0x0f) - 1; |
| 4556 | |
| 4557 | if ((apic < 0) || (apic > 7) || |
| 4558 | (cluster >= 0xf)) |
| 4559 | return NULL; |
| 4560 | index = (cluster << 2) + apic; |
| 4561 | } |
| 4562 | |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 4563 | logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page); |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4564 | |
| 4565 | return &logical_apic_id_table[index]; |
| 4566 | } |
| 4567 | |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 4568 | static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr) |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4569 | { |
| 4570 | bool flat; |
| 4571 | u32 *entry, new_entry; |
| 4572 | |
| 4573 | flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT; |
| 4574 | entry = avic_get_logical_id_entry(vcpu, ldr, flat); |
| 4575 | if (!entry) |
| 4576 | return -EINVAL; |
| 4577 | |
| 4578 | new_entry = READ_ONCE(*entry); |
| 4579 | new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK; |
| 4580 | new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK); |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 4581 | new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK; |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4582 | WRITE_ONCE(*entry, new_entry); |
| 4583 | |
| 4584 | return 0; |
| 4585 | } |
| 4586 | |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 4587 | static void avic_invalidate_logical_id_entry(struct kvm_vcpu *vcpu) |
| 4588 | { |
| 4589 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4590 | bool flat = svm->dfr_reg == APIC_DFR_FLAT; |
| 4591 | u32 *entry = avic_get_logical_id_entry(vcpu, svm->ldr_reg, flat); |
| 4592 | |
| 4593 | if (entry) |
Suthikulpanit, Suravee | e44e3ea | 2019-03-26 03:57:37 +0000 | [diff] [blame] | 4594 | clear_bit(AVIC_LOGICAL_ID_ENTRY_VALID_BIT, (unsigned long *)entry); |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4597 | static int avic_handle_ldr_update(struct kvm_vcpu *vcpu) |
| 4598 | { |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 4599 | int ret = 0; |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4600 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4601 | u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR); |
| 4602 | |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 4603 | if (ldr == svm->ldr_reg) |
| 4604 | return 0; |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4605 | |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 4606 | avic_invalidate_logical_id_entry(vcpu); |
| 4607 | |
| 4608 | if (ldr) |
| 4609 | ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr); |
| 4610 | |
| 4611 | if (!ret) |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4612 | svm->ldr_reg = ldr; |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 4613 | |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4614 | return ret; |
| 4615 | } |
| 4616 | |
| 4617 | static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu) |
| 4618 | { |
| 4619 | u64 *old, *new; |
| 4620 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4621 | u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID); |
| 4622 | u32 id = (apic_id_reg >> 24) & 0xff; |
| 4623 | |
| 4624 | if (vcpu->vcpu_id == id) |
| 4625 | return 0; |
| 4626 | |
| 4627 | old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id); |
| 4628 | new = avic_get_physical_id_entry(vcpu, id); |
| 4629 | if (!new || !old) |
| 4630 | return 1; |
| 4631 | |
| 4632 | /* We need to move physical_id_entry to new offset */ |
| 4633 | *new = *old; |
| 4634 | *old = 0ULL; |
| 4635 | to_svm(vcpu)->avic_physical_id_cache = new; |
| 4636 | |
| 4637 | /* |
| 4638 | * Also update the guest physical APIC ID in the logical |
| 4639 | * APIC ID table entry if already setup the LDR. |
| 4640 | */ |
| 4641 | if (svm->ldr_reg) |
| 4642 | avic_handle_ldr_update(vcpu); |
| 4643 | |
| 4644 | return 0; |
| 4645 | } |
| 4646 | |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 4647 | static void avic_handle_dfr_update(struct kvm_vcpu *vcpu) |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4648 | { |
| 4649 | struct vcpu_svm *svm = to_svm(vcpu); |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4650 | u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR); |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4651 | |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 4652 | if (svm->dfr_reg == dfr) |
| 4653 | return; |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4654 | |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 4655 | avic_invalidate_logical_id_entry(vcpu); |
| 4656 | svm->dfr_reg = dfr; |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | static int avic_unaccel_trap_write(struct vcpu_svm *svm) |
| 4660 | { |
| 4661 | struct kvm_lapic *apic = svm->vcpu.arch.apic; |
| 4662 | u32 offset = svm->vmcb->control.exit_info_1 & |
| 4663 | AVIC_UNACCEL_ACCESS_OFFSET_MASK; |
| 4664 | |
| 4665 | switch (offset) { |
| 4666 | case APIC_ID: |
| 4667 | if (avic_handle_apic_id_update(&svm->vcpu)) |
| 4668 | return 0; |
| 4669 | break; |
| 4670 | case APIC_LDR: |
| 4671 | if (avic_handle_ldr_update(&svm->vcpu)) |
| 4672 | return 0; |
| 4673 | break; |
| 4674 | case APIC_DFR: |
| 4675 | avic_handle_dfr_update(&svm->vcpu); |
| 4676 | break; |
| 4677 | default: |
| 4678 | break; |
| 4679 | } |
| 4680 | |
| 4681 | kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset)); |
| 4682 | |
| 4683 | return 1; |
| 4684 | } |
| 4685 | |
| 4686 | static bool is_avic_unaccelerated_access_trap(u32 offset) |
| 4687 | { |
| 4688 | bool ret = false; |
| 4689 | |
| 4690 | switch (offset) { |
| 4691 | case APIC_ID: |
| 4692 | case APIC_EOI: |
| 4693 | case APIC_RRR: |
| 4694 | case APIC_LDR: |
| 4695 | case APIC_DFR: |
| 4696 | case APIC_SPIV: |
| 4697 | case APIC_ESR: |
| 4698 | case APIC_ICR: |
| 4699 | case APIC_LVTT: |
| 4700 | case APIC_LVTTHMR: |
| 4701 | case APIC_LVTPC: |
| 4702 | case APIC_LVT0: |
| 4703 | case APIC_LVT1: |
| 4704 | case APIC_LVTERR: |
| 4705 | case APIC_TMICT: |
| 4706 | case APIC_TDCR: |
| 4707 | ret = true; |
| 4708 | break; |
| 4709 | default: |
| 4710 | break; |
| 4711 | } |
| 4712 | return ret; |
| 4713 | } |
| 4714 | |
| 4715 | static int avic_unaccelerated_access_interception(struct vcpu_svm *svm) |
| 4716 | { |
| 4717 | int ret = 0; |
| 4718 | u32 offset = svm->vmcb->control.exit_info_1 & |
| 4719 | AVIC_UNACCEL_ACCESS_OFFSET_MASK; |
| 4720 | u32 vector = svm->vmcb->control.exit_info_2 & |
| 4721 | AVIC_UNACCEL_ACCESS_VECTOR_MASK; |
| 4722 | bool write = (svm->vmcb->control.exit_info_1 >> 32) & |
| 4723 | AVIC_UNACCEL_ACCESS_WRITE_MASK; |
| 4724 | bool trap = is_avic_unaccelerated_access_trap(offset); |
| 4725 | |
| 4726 | trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset, |
| 4727 | trap, write, vector); |
| 4728 | if (trap) { |
| 4729 | /* Handling Trap */ |
| 4730 | WARN_ONCE(!write, "svm: Handling trap read.\n"); |
| 4731 | ret = avic_unaccel_trap_write(svm); |
| 4732 | } else { |
| 4733 | /* Handling Fault */ |
Sean Christopherson | 60fc3d0 | 2019-08-27 14:40:38 -0700 | [diff] [blame] | 4734 | ret = kvm_emulate_instruction(&svm->vcpu, 0); |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | return ret; |
| 4738 | } |
| 4739 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 4740 | static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4741 | [SVM_EXIT_READ_CR0] = cr_interception, |
| 4742 | [SVM_EXIT_READ_CR3] = cr_interception, |
| 4743 | [SVM_EXIT_READ_CR4] = cr_interception, |
| 4744 | [SVM_EXIT_READ_CR8] = cr_interception, |
David Kaplan | 5e57518 | 2015-03-06 14:44:35 -0600 | [diff] [blame] | 4745 | [SVM_EXIT_CR0_SEL_WRITE] = cr_interception, |
Joerg Roedel | 628afd2 | 2011-04-04 12:39:36 +0200 | [diff] [blame] | 4746 | [SVM_EXIT_WRITE_CR0] = cr_interception, |
Andre Przywara | 7ff76d5 | 2010-12-21 11:12:04 +0100 | [diff] [blame] | 4747 | [SVM_EXIT_WRITE_CR3] = cr_interception, |
| 4748 | [SVM_EXIT_WRITE_CR4] = cr_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4749 | [SVM_EXIT_WRITE_CR8] = cr8_write_interception, |
Andre Przywara | cae3797 | 2010-12-21 11:12:05 +0100 | [diff] [blame] | 4750 | [SVM_EXIT_READ_DR0] = dr_interception, |
| 4751 | [SVM_EXIT_READ_DR1] = dr_interception, |
| 4752 | [SVM_EXIT_READ_DR2] = dr_interception, |
| 4753 | [SVM_EXIT_READ_DR3] = dr_interception, |
| 4754 | [SVM_EXIT_READ_DR4] = dr_interception, |
| 4755 | [SVM_EXIT_READ_DR5] = dr_interception, |
| 4756 | [SVM_EXIT_READ_DR6] = dr_interception, |
| 4757 | [SVM_EXIT_READ_DR7] = dr_interception, |
| 4758 | [SVM_EXIT_WRITE_DR0] = dr_interception, |
| 4759 | [SVM_EXIT_WRITE_DR1] = dr_interception, |
| 4760 | [SVM_EXIT_WRITE_DR2] = dr_interception, |
| 4761 | [SVM_EXIT_WRITE_DR3] = dr_interception, |
| 4762 | [SVM_EXIT_WRITE_DR4] = dr_interception, |
| 4763 | [SVM_EXIT_WRITE_DR5] = dr_interception, |
| 4764 | [SVM_EXIT_WRITE_DR6] = dr_interception, |
| 4765 | [SVM_EXIT_WRITE_DR7] = dr_interception, |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4766 | [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, |
| 4767 | [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4768 | [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4769 | [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4770 | [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 4771 | [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception, |
Liran Alon | 9718420 | 2018-03-12 13:12:52 +0200 | [diff] [blame] | 4772 | [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4773 | [SVM_EXIT_INTR] = intr_interception, |
Joerg Roedel | c47f098 | 2008-04-30 17:56:00 +0200 | [diff] [blame] | 4774 | [SVM_EXIT_NMI] = nmi_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4775 | [SVM_EXIT_SMI] = nop_on_interception, |
| 4776 | [SVM_EXIT_INIT] = nop_on_interception, |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4777 | [SVM_EXIT_VINTR] = interrupt_window_interception, |
Avi Kivity | 332b56e | 2011-11-10 14:57:24 +0200 | [diff] [blame] | 4778 | [SVM_EXIT_RDPMC] = rdpmc_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4779 | [SVM_EXIT_CPUID] = cpuid_interception, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 4780 | [SVM_EXIT_IRET] = iret_interception, |
Avi Kivity | cf5a94d | 2007-10-28 16:11:58 +0200 | [diff] [blame] | 4781 | [SVM_EXIT_INVD] = emulate_on_interception, |
Mark Langsdorf | 565d099 | 2009-10-06 14:25:02 -0500 | [diff] [blame] | 4782 | [SVM_EXIT_PAUSE] = pause_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4783 | [SVM_EXIT_HLT] = halt_interception, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4784 | [SVM_EXIT_INVLPG] = invlpg_interception, |
Alexander Graf | ff09238 | 2009-06-15 15:21:24 +0200 | [diff] [blame] | 4785 | [SVM_EXIT_INVLPGA] = invlpga_interception, |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 4786 | [SVM_EXIT_IOIO] = io_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4787 | [SVM_EXIT_MSR] = msr_interception, |
| 4788 | [SVM_EXIT_TASK_SWITCH] = task_switch_interception, |
Joerg Roedel | 46fe4dd | 2007-01-26 00:56:42 -0800 | [diff] [blame] | 4789 | [SVM_EXIT_SHUTDOWN] = shutdown_interception, |
Alexander Graf | 3d6368e | 2008-11-25 20:17:07 +0100 | [diff] [blame] | 4790 | [SVM_EXIT_VMRUN] = vmrun_interception, |
Avi Kivity | 02e235b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4791 | [SVM_EXIT_VMMCALL] = vmmcall_interception, |
Alexander Graf | 5542675 | 2008-11-25 20:17:06 +0100 | [diff] [blame] | 4792 | [SVM_EXIT_VMLOAD] = vmload_interception, |
| 4793 | [SVM_EXIT_VMSAVE] = vmsave_interception, |
Alexander Graf | 1371d90 | 2008-11-25 20:17:04 +0100 | [diff] [blame] | 4794 | [SVM_EXIT_STGI] = stgi_interception, |
| 4795 | [SVM_EXIT_CLGI] = clgi_interception, |
Joerg Roedel | 532a46b | 2009-10-09 16:08:32 +0200 | [diff] [blame] | 4796 | [SVM_EXIT_SKINIT] = skinit_interception, |
David Kaplan | dab429a | 2015-03-02 13:43:37 -0600 | [diff] [blame] | 4797 | [SVM_EXIT_WBINVD] = wbinvd_interception, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 4798 | [SVM_EXIT_MONITOR] = monitor_interception, |
| 4799 | [SVM_EXIT_MWAIT] = mwait_interception, |
Joerg Roedel | 81dd35d | 2010-12-07 17:15:06 +0100 | [diff] [blame] | 4800 | [SVM_EXIT_XSETBV] = xsetbv_interception, |
Jim Mattson | 0cb8410 | 2019-09-19 15:59:17 -0700 | [diff] [blame] | 4801 | [SVM_EXIT_RDPRU] = rdpru_interception, |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 4802 | [SVM_EXIT_NPF] = npf_interception, |
Brijesh Singh | 7607b71 | 2018-02-19 10:14:44 -0600 | [diff] [blame] | 4803 | [SVM_EXIT_RSM] = rsm_interception, |
Suravee Suthikulpanit | 18f40c5 | 2016-05-04 14:09:48 -0500 | [diff] [blame] | 4804 | [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception, |
| 4805 | [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4806 | }; |
| 4807 | |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4808 | static void dump_vmcb(struct kvm_vcpu *vcpu) |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4809 | { |
| 4810 | struct vcpu_svm *svm = to_svm(vcpu); |
| 4811 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 4812 | struct vmcb_save_area *save = &svm->vmcb->save; |
| 4813 | |
Paolo Bonzini | 6f2f845 | 2019-05-20 15:34:35 +0200 | [diff] [blame] | 4814 | if (!dump_invalid_vmcb) { |
| 4815 | pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n"); |
| 4816 | return; |
| 4817 | } |
| 4818 | |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4819 | pr_err("VMCB Control Area:\n"); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4820 | pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff); |
| 4821 | pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16); |
| 4822 | pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff); |
| 4823 | pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16); |
| 4824 | pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions); |
| 4825 | pr_err("%-20s%016llx\n", "intercepts:", control->intercept); |
| 4826 | pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count); |
Babu Moger | 1d8fb44 | 2018-03-16 16:37:25 -0400 | [diff] [blame] | 4827 | pr_err("%-20s%d\n", "pause filter threshold:", |
| 4828 | control->pause_filter_thresh); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4829 | pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa); |
| 4830 | pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa); |
| 4831 | pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset); |
| 4832 | pr_err("%-20s%d\n", "asid:", control->asid); |
| 4833 | pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl); |
| 4834 | pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl); |
| 4835 | pr_err("%-20s%08x\n", "int_vector:", control->int_vector); |
| 4836 | pr_err("%-20s%08x\n", "int_state:", control->int_state); |
| 4837 | pr_err("%-20s%08x\n", "exit_code:", control->exit_code); |
| 4838 | pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1); |
| 4839 | pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2); |
| 4840 | pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info); |
| 4841 | pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err); |
| 4842 | pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl); |
| 4843 | pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4844 | pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4845 | pr_err("%-20s%08x\n", "event_inj:", control->event_inj); |
| 4846 | pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err); |
Janakarajan Natarajan | 0dc9211 | 2017-07-06 15:50:45 -0500 | [diff] [blame] | 4847 | pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4848 | pr_err("%-20s%016llx\n", "next_rip:", control->next_rip); |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 4849 | pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page); |
| 4850 | pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id); |
| 4851 | pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id); |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4852 | pr_err("VMCB State Save Area:\n"); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4853 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4854 | "es:", |
| 4855 | save->es.selector, save->es.attrib, |
| 4856 | save->es.limit, save->es.base); |
| 4857 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4858 | "cs:", |
| 4859 | save->cs.selector, save->cs.attrib, |
| 4860 | save->cs.limit, save->cs.base); |
| 4861 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4862 | "ss:", |
| 4863 | save->ss.selector, save->ss.attrib, |
| 4864 | save->ss.limit, save->ss.base); |
| 4865 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4866 | "ds:", |
| 4867 | save->ds.selector, save->ds.attrib, |
| 4868 | save->ds.limit, save->ds.base); |
| 4869 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4870 | "fs:", |
| 4871 | save->fs.selector, save->fs.attrib, |
| 4872 | save->fs.limit, save->fs.base); |
| 4873 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4874 | "gs:", |
| 4875 | save->gs.selector, save->gs.attrib, |
| 4876 | save->gs.limit, save->gs.base); |
| 4877 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4878 | "gdtr:", |
| 4879 | save->gdtr.selector, save->gdtr.attrib, |
| 4880 | save->gdtr.limit, save->gdtr.base); |
| 4881 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4882 | "ldtr:", |
| 4883 | save->ldtr.selector, save->ldtr.attrib, |
| 4884 | save->ldtr.limit, save->ldtr.base); |
| 4885 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4886 | "idtr:", |
| 4887 | save->idtr.selector, save->idtr.attrib, |
| 4888 | save->idtr.limit, save->idtr.base); |
| 4889 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
| 4890 | "tr:", |
| 4891 | save->tr.selector, save->tr.attrib, |
| 4892 | save->tr.limit, save->tr.base); |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4893 | pr_err("cpl: %d efer: %016llx\n", |
| 4894 | save->cpl, save->efer); |
Joe Perches | ae8cc05 | 2011-04-24 22:00:50 -0700 | [diff] [blame] | 4895 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4896 | "cr0:", save->cr0, "cr2:", save->cr2); |
| 4897 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4898 | "cr3:", save->cr3, "cr4:", save->cr4); |
| 4899 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4900 | "dr6:", save->dr6, "dr7:", save->dr7); |
| 4901 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4902 | "rip:", save->rip, "rflags:", save->rflags); |
| 4903 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4904 | "rsp:", save->rsp, "rax:", save->rax); |
| 4905 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4906 | "star:", save->star, "lstar:", save->lstar); |
| 4907 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4908 | "cstar:", save->cstar, "sfmask:", save->sfmask); |
| 4909 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4910 | "kernel_gs_base:", save->kernel_gs_base, |
| 4911 | "sysenter_cs:", save->sysenter_cs); |
| 4912 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4913 | "sysenter_esp:", save->sysenter_esp, |
| 4914 | "sysenter_eip:", save->sysenter_eip); |
| 4915 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4916 | "gpat:", save->g_pat, "dbgctl:", save->dbgctl); |
| 4917 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4918 | "br_from:", save->br_from, "br_to:", save->br_to); |
| 4919 | pr_err("%-15s %016llx %-13s %016llx\n", |
| 4920 | "excp_from:", save->last_excp_from, |
| 4921 | "excp_to:", save->last_excp_to); |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4922 | } |
| 4923 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 4924 | static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 4925 | { |
| 4926 | struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; |
| 4927 | |
| 4928 | *info1 = control->exit_info_1; |
| 4929 | *info2 = control->exit_info_2; |
| 4930 | } |
| 4931 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4932 | static int handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4933 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4934 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4935 | struct kvm_run *kvm_run = vcpu->run; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4936 | u32 exit_code = svm->vmcb->control.exit_code; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4937 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 4938 | trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM); |
| 4939 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 4940 | if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE)) |
Joerg Roedel | 2be4fc7 | 2010-04-22 12:33:09 +0200 | [diff] [blame] | 4941 | vcpu->arch.cr0 = svm->vmcb->save.cr0; |
| 4942 | if (npt_enabled) |
| 4943 | vcpu->arch.cr3 = svm->vmcb->save.cr3; |
Joerg Roedel | af9ca2d | 2008-04-30 17:56:03 +0200 | [diff] [blame] | 4944 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 4945 | if (unlikely(svm->nested.exit_required)) { |
| 4946 | nested_svm_vmexit(svm); |
| 4947 | svm->nested.exit_required = false; |
| 4948 | |
| 4949 | return 1; |
| 4950 | } |
| 4951 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 4952 | if (is_guest_mode(vcpu)) { |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 4953 | int vmexit; |
| 4954 | |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 4955 | trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code, |
| 4956 | svm->vmcb->control.exit_info_1, |
| 4957 | svm->vmcb->control.exit_info_2, |
| 4958 | svm->vmcb->control.exit_int_info, |
Stefan Hajnoczi | e097e5f | 2011-07-22 12:46:52 +0100 | [diff] [blame] | 4959 | svm->vmcb->control.exit_int_info_err, |
| 4960 | KVM_ISA_SVM); |
Joerg Roedel | d8cabdd | 2009-10-09 16:08:28 +0200 | [diff] [blame] | 4961 | |
Joerg Roedel | 410e4d5 | 2009-08-07 11:49:44 +0200 | [diff] [blame] | 4962 | vmexit = nested_svm_exit_special(svm); |
| 4963 | |
| 4964 | if (vmexit == NESTED_EXIT_CONTINUE) |
| 4965 | vmexit = nested_svm_exit_handled(svm); |
| 4966 | |
| 4967 | if (vmexit == NESTED_EXIT_DONE) |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 4968 | return 1; |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 4969 | } |
| 4970 | |
Joerg Roedel | a5c3832 | 2009-08-07 11:49:32 +0200 | [diff] [blame] | 4971 | svm_complete_interrupts(svm); |
| 4972 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4973 | if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { |
| 4974 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 4975 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 4976 | = svm->vmcb->control.exit_code; |
Joerg Roedel | 3f10c84 | 2010-05-05 16:04:42 +0200 | [diff] [blame] | 4977 | dump_vmcb(vcpu); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 4978 | return 0; |
| 4979 | } |
| 4980 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4981 | if (is_external_interrupt(svm->vmcb->control.exit_int_info) && |
Joerg Roedel | 709ddeb | 2008-02-07 13:47:45 +0100 | [diff] [blame] | 4982 | exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && |
Joerg Roedel | 55c5e46 | 2010-09-10 17:31:04 +0200 | [diff] [blame] | 4983 | exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH && |
| 4984 | exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI) |
Borislav Petkov | 6614c7d | 2013-04-26 00:22:01 +0200 | [diff] [blame] | 4985 | printk(KERN_ERR "%s: unexpected exit_int_info 0x%x " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4986 | "exit_code 0x%x\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 4987 | __func__, svm->vmcb->control.exit_int_info, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4988 | exit_code); |
| 4989 | |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 4990 | if (exit_code >= ARRAY_SIZE(svm_exit_handlers) |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 4991 | || !svm_exit_handlers[exit_code]) { |
Liran Alon | 7396d33 | 2019-08-26 13:16:43 +0300 | [diff] [blame] | 4992 | vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%x\n", exit_code); |
| 4993 | dump_vmcb(vcpu); |
| 4994 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4995 | vcpu->run->internal.suberror = |
| 4996 | KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON; |
| 4997 | vcpu->run->internal.ndata = 1; |
| 4998 | vcpu->run->internal.data[0] = exit_code; |
| 4999 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5000 | } |
| 5001 | |
Andrea Arcangeli | 3dcb2a3 | 2019-11-04 18:00:00 -0500 | [diff] [blame^] | 5002 | #ifdef CONFIG_RETPOLINE |
| 5003 | if (exit_code == SVM_EXIT_MSR) |
| 5004 | return msr_interception(svm); |
| 5005 | else if (exit_code == SVM_EXIT_VINTR) |
| 5006 | return interrupt_window_interception(svm); |
| 5007 | else if (exit_code == SVM_EXIT_INTR) |
| 5008 | return intr_interception(svm); |
| 5009 | else if (exit_code == SVM_EXIT_HLT) |
| 5010 | return halt_interception(svm); |
| 5011 | else if (exit_code == SVM_EXIT_NPF) |
| 5012 | return npf_interception(svm); |
| 5013 | #endif |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5014 | return svm_exit_handlers[exit_code](svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | static void reload_tss(struct kvm_vcpu *vcpu) |
| 5018 | { |
| 5019 | int cpu = raw_smp_processor_id(); |
| 5020 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 5021 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 5022 | sd->tss_desc->type = 9; /* available 32/64-bit TSS */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5023 | load_TR_desc(); |
| 5024 | } |
| 5025 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 5026 | static void pre_sev_run(struct vcpu_svm *svm, int cpu) |
| 5027 | { |
| 5028 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
| 5029 | int asid = sev_get_asid(svm->vcpu.kvm); |
| 5030 | |
| 5031 | /* Assign the asid allocated with this SEV guest */ |
| 5032 | svm->vmcb->control.asid = asid; |
| 5033 | |
| 5034 | /* |
| 5035 | * Flush guest TLB: |
| 5036 | * |
| 5037 | * 1) when different VMCB for the same ASID is to be run on the same host CPU. |
| 5038 | * 2) or this VMCB was executed on different host CPU in previous VMRUNs. |
| 5039 | */ |
| 5040 | if (sd->sev_vmcbs[asid] == svm->vmcb && |
| 5041 | svm->last_cpu == cpu) |
| 5042 | return; |
| 5043 | |
| 5044 | svm->last_cpu = cpu; |
| 5045 | sd->sev_vmcbs[asid] = svm->vmcb; |
| 5046 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; |
| 5047 | mark_dirty(svm->vmcb, VMCB_ASID); |
| 5048 | } |
| 5049 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 5050 | static void pre_svm_run(struct vcpu_svm *svm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5051 | { |
| 5052 | int cpu = raw_smp_processor_id(); |
| 5053 | |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 5054 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5055 | |
Brijesh Singh | 70cd94e | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 5056 | if (sev_guest(svm->vcpu.kvm)) |
| 5057 | return pre_sev_run(svm, cpu); |
| 5058 | |
Marcelo Tosatti | 4b656b1 | 2009-07-21 12:47:45 -0300 | [diff] [blame] | 5059 | /* FIXME: handle wraparound of asid_generation */ |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 5060 | if (svm->asid_generation != sd->asid_generation) |
| 5061 | new_asid(svm, sd); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5062 | } |
| 5063 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5064 | static void svm_inject_nmi(struct kvm_vcpu *vcpu) |
| 5065 | { |
| 5066 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5067 | |
| 5068 | svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; |
| 5069 | vcpu->arch.hflags |= HF_NMI_MASK; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 5070 | set_intercept(svm, INTERCEPT_IRET); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5071 | ++vcpu->stat.nmi_injections; |
| 5072 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5073 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5074 | static inline void svm_inject_irq(struct vcpu_svm *svm, int irq) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5075 | { |
| 5076 | struct vmcb_control_area *control; |
| 5077 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 5078 | /* The following fields are ignored when AVIC is enabled */ |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 5079 | control = &svm->vmcb->control; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5080 | control->int_vector = irq; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5081 | control->int_ctl &= ~V_INTR_PRIO_MASK; |
| 5082 | control->int_ctl |= V_IRQ_MASK | |
| 5083 | ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); |
Joerg Roedel | decdbf6 | 2010-12-03 11:45:52 +0100 | [diff] [blame] | 5084 | mark_dirty(svm->vmcb, VMCB_INTR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5085 | } |
| 5086 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5087 | static void svm_set_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 5088 | { |
| 5089 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5090 | |
Joerg Roedel | 2af9194 | 2009-08-07 11:49:28 +0200 | [diff] [blame] | 5091 | BUG_ON(!(gif_set(svm))); |
Alexander Graf | cf74a78 | 2008-11-25 20:17:08 +0100 | [diff] [blame] | 5092 | |
Gleb Natapov | 9fb2d2b | 2010-05-23 14:28:26 +0300 | [diff] [blame] | 5093 | trace_kvm_inj_virq(vcpu->arch.interrupt.nr); |
| 5094 | ++vcpu->stat.irq_injections; |
| 5095 | |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5096 | svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | |
| 5097 | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR; |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 5098 | } |
| 5099 | |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 5100 | static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu) |
| 5101 | { |
| 5102 | return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK); |
| 5103 | } |
| 5104 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5105 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
| 5106 | { |
| 5107 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5108 | |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 5109 | if (svm_nested_virtualize_tpr(vcpu) || |
| 5110 | kvm_vcpu_apicv_active(vcpu)) |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 5111 | return; |
| 5112 | |
Radim Krčmář | 596f314 | 2014-03-11 19:11:18 +0100 | [diff] [blame] | 5113 | clr_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
| 5114 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5115 | if (irr == -1) |
| 5116 | return; |
| 5117 | |
| 5118 | if (tpr >= irr) |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 5119 | set_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5120 | } |
| 5121 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 5122 | static void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5123 | { |
| 5124 | return; |
| 5125 | } |
| 5126 | |
Suthikulpanit, Suravee | 2cf9af0 | 2019-09-13 19:00:49 +0000 | [diff] [blame] | 5127 | static bool svm_get_enable_apicv(struct kvm *kvm) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5128 | { |
Suthikulpanit, Suravee | 2cf9af0 | 2019-09-13 19:00:49 +0000 | [diff] [blame] | 5129 | return avic && irqchip_split(kvm); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5130 | } |
| 5131 | |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 5132 | static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 5133 | { |
| 5134 | } |
| 5135 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 5136 | 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] | 5137 | { |
| 5138 | } |
| 5139 | |
| 5140 | /* Note: Currently only used by Hyper-V. */ |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5141 | static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 5142 | { |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 5143 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5144 | struct vmcb *vmcb = svm->vmcb; |
| 5145 | |
Suthikulpanit, Suravee | c57cd3c | 2019-01-29 08:09:46 +0000 | [diff] [blame] | 5146 | if (kvm_vcpu_apicv_active(vcpu)) |
| 5147 | vmcb->control.int_ctl |= AVIC_ENABLE_MASK; |
| 5148 | else |
| 5149 | vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK; |
| 5150 | mark_dirty(vmcb, VMCB_AVIC); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5151 | } |
| 5152 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 5153 | 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] | 5154 | { |
| 5155 | return; |
| 5156 | } |
| 5157 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 5158 | static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) |
| 5159 | { |
| 5160 | kvm_lapic_set_irr(vec, vcpu->arch.apic); |
| 5161 | smp_mb__after_atomic(); |
| 5162 | |
Suthikulpanit, Suravee | 0532dd5 | 2019-05-03 06:38:53 -0700 | [diff] [blame] | 5163 | if (avic_vcpu_is_running(vcpu)) { |
| 5164 | int cpuid = vcpu->cpu; |
| 5165 | |
| 5166 | if (cpuid != get_cpu()) |
| 5167 | wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpuid)); |
| 5168 | put_cpu(); |
| 5169 | } else |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 5170 | kvm_vcpu_wake_up(vcpu); |
| 5171 | } |
| 5172 | |
Wanpeng Li | 17e433b | 2019-08-05 10:03:19 +0800 | [diff] [blame] | 5173 | static bool svm_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu) |
| 5174 | { |
| 5175 | return false; |
| 5176 | } |
| 5177 | |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 5178 | static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) |
| 5179 | { |
| 5180 | unsigned long flags; |
| 5181 | struct amd_svm_iommu_ir *cur; |
| 5182 | |
| 5183 | spin_lock_irqsave(&svm->ir_list_lock, flags); |
| 5184 | list_for_each_entry(cur, &svm->ir_list, node) { |
| 5185 | if (cur->data != pi->ir_data) |
| 5186 | continue; |
| 5187 | list_del(&cur->node); |
| 5188 | kfree(cur); |
| 5189 | break; |
| 5190 | } |
| 5191 | spin_unlock_irqrestore(&svm->ir_list_lock, flags); |
| 5192 | } |
| 5193 | |
| 5194 | static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) |
| 5195 | { |
| 5196 | int ret = 0; |
| 5197 | unsigned long flags; |
| 5198 | struct amd_svm_iommu_ir *ir; |
| 5199 | |
| 5200 | /** |
| 5201 | * In some cases, the existing irte is updaed and re-set, |
| 5202 | * so we need to check here if it's already been * added |
| 5203 | * to the ir_list. |
| 5204 | */ |
| 5205 | if (pi->ir_data && (pi->prev_ga_tag != 0)) { |
| 5206 | struct kvm *kvm = svm->vcpu.kvm; |
| 5207 | u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag); |
| 5208 | struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); |
| 5209 | struct vcpu_svm *prev_svm; |
| 5210 | |
| 5211 | if (!prev_vcpu) { |
| 5212 | ret = -EINVAL; |
| 5213 | goto out; |
| 5214 | } |
| 5215 | |
| 5216 | prev_svm = to_svm(prev_vcpu); |
| 5217 | svm_ir_list_del(prev_svm, pi); |
| 5218 | } |
| 5219 | |
| 5220 | /** |
| 5221 | * Allocating new amd_iommu_pi_data, which will get |
| 5222 | * add to the per-vcpu ir_list. |
| 5223 | */ |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 5224 | ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL_ACCOUNT); |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 5225 | if (!ir) { |
| 5226 | ret = -ENOMEM; |
| 5227 | goto out; |
| 5228 | } |
| 5229 | ir->data = pi->ir_data; |
| 5230 | |
| 5231 | spin_lock_irqsave(&svm->ir_list_lock, flags); |
| 5232 | list_add(&ir->node, &svm->ir_list); |
| 5233 | spin_unlock_irqrestore(&svm->ir_list_lock, flags); |
| 5234 | out: |
| 5235 | return ret; |
| 5236 | } |
| 5237 | |
| 5238 | /** |
| 5239 | * Note: |
| 5240 | * The HW cannot support posting multicast/broadcast |
| 5241 | * interrupts to a vCPU. So, we still use legacy interrupt |
| 5242 | * remapping for these kind of interrupts. |
| 5243 | * |
| 5244 | * For lowest-priority interrupts, we only support |
| 5245 | * those with single CPU as the destination, e.g. user |
| 5246 | * configures the interrupts via /proc/irq or uses |
| 5247 | * irqbalance to make the interrupts single-CPU. |
| 5248 | */ |
| 5249 | static int |
| 5250 | get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e, |
| 5251 | struct vcpu_data *vcpu_info, struct vcpu_svm **svm) |
| 5252 | { |
| 5253 | struct kvm_lapic_irq irq; |
| 5254 | struct kvm_vcpu *vcpu = NULL; |
| 5255 | |
| 5256 | kvm_set_msi_irq(kvm, e, &irq); |
| 5257 | |
Alexander Graf | fdcf756 | 2019-09-05 14:58:18 +0200 | [diff] [blame] | 5258 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) || |
| 5259 | !kvm_irq_is_postable(&irq)) { |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 5260 | pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n", |
| 5261 | __func__, irq.vector); |
| 5262 | return -1; |
| 5263 | } |
| 5264 | |
| 5265 | pr_debug("SVM: %s: use GA mode for irq %u\n", __func__, |
| 5266 | irq.vector); |
| 5267 | *svm = to_svm(vcpu); |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 5268 | 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] | 5269 | vcpu_info->vector = irq.vector; |
| 5270 | |
| 5271 | return 0; |
| 5272 | } |
| 5273 | |
| 5274 | /* |
| 5275 | * svm_update_pi_irte - set IRTE for Posted-Interrupts |
| 5276 | * |
| 5277 | * @kvm: kvm |
| 5278 | * @host_irq: host irq of the interrupt |
| 5279 | * @guest_irq: gsi of the interrupt |
| 5280 | * @set: set or unset PI |
| 5281 | * returns 0 on success, < 0 on failure |
| 5282 | */ |
| 5283 | static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 5284 | uint32_t guest_irq, bool set) |
| 5285 | { |
| 5286 | struct kvm_kernel_irq_routing_entry *e; |
| 5287 | struct kvm_irq_routing_table *irq_rt; |
| 5288 | int idx, ret = -EINVAL; |
| 5289 | |
| 5290 | if (!kvm_arch_has_assigned_device(kvm) || |
| 5291 | !irq_remapping_cap(IRQ_POSTING_CAP)) |
| 5292 | return 0; |
| 5293 | |
| 5294 | pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n", |
| 5295 | __func__, host_irq, guest_irq, set); |
| 5296 | |
| 5297 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 5298 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
| 5299 | WARN_ON(guest_irq >= irq_rt->nr_rt_entries); |
| 5300 | |
| 5301 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 5302 | struct vcpu_data vcpu_info; |
| 5303 | struct vcpu_svm *svm = NULL; |
| 5304 | |
| 5305 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 5306 | continue; |
| 5307 | |
| 5308 | /** |
| 5309 | * Here, we setup with legacy mode in the following cases: |
| 5310 | * 1. When cannot target interrupt to a specific vcpu. |
| 5311 | * 2. Unsetting posted interrupt. |
| 5312 | * 3. APIC virtialization is disabled for the vcpu. |
Alexander Graf | fdcf756 | 2019-09-05 14:58:18 +0200 | [diff] [blame] | 5313 | * 4. IRQ has incompatible delivery mode (SMI, INIT, etc) |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 5314 | */ |
| 5315 | if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set && |
| 5316 | kvm_vcpu_apicv_active(&svm->vcpu)) { |
| 5317 | struct amd_iommu_pi_data pi; |
| 5318 | |
| 5319 | /* Try to enable guest_mode in IRTE */ |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 5320 | pi.base = __sme_set(page_to_phys(svm->avic_backing_page) & |
| 5321 | AVIC_HPA_MASK); |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 5322 | pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 5323 | svm->vcpu.vcpu_id); |
| 5324 | pi.is_guest_mode = true; |
| 5325 | pi.vcpu_data = &vcpu_info; |
| 5326 | ret = irq_set_vcpu_affinity(host_irq, &pi); |
| 5327 | |
| 5328 | /** |
| 5329 | * Here, we successfully setting up vcpu affinity in |
| 5330 | * IOMMU guest mode. Now, we need to store the posted |
| 5331 | * interrupt information in a per-vcpu ir_list so that |
| 5332 | * we can reference to them directly when we update vcpu |
| 5333 | * scheduling information in IOMMU irte. |
| 5334 | */ |
| 5335 | if (!ret && pi.is_guest_mode) |
| 5336 | svm_ir_list_add(svm, &pi); |
| 5337 | } else { |
| 5338 | /* Use legacy mode in IRTE */ |
| 5339 | struct amd_iommu_pi_data pi; |
| 5340 | |
| 5341 | /** |
| 5342 | * Here, pi is used to: |
| 5343 | * - Tell IOMMU to use legacy mode for this interrupt. |
| 5344 | * - Retrieve ga_tag of prior interrupt remapping data. |
| 5345 | */ |
| 5346 | pi.is_guest_mode = false; |
| 5347 | ret = irq_set_vcpu_affinity(host_irq, &pi); |
| 5348 | |
| 5349 | /** |
| 5350 | * Check if the posted interrupt was previously |
| 5351 | * setup with the guest_mode by checking if the ga_tag |
| 5352 | * was cached. If so, we need to clean up the per-vcpu |
| 5353 | * ir_list. |
| 5354 | */ |
| 5355 | if (!ret && pi.prev_ga_tag) { |
| 5356 | int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag); |
| 5357 | struct kvm_vcpu *vcpu; |
| 5358 | |
| 5359 | vcpu = kvm_get_vcpu_by_id(kvm, id); |
| 5360 | if (vcpu) |
| 5361 | svm_ir_list_del(to_svm(vcpu), &pi); |
| 5362 | } |
| 5363 | } |
| 5364 | |
| 5365 | if (!ret && svm) { |
hu huajun | 2698d82 | 2018-04-11 15:16:40 +0800 | [diff] [blame] | 5366 | trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id, |
| 5367 | e->gsi, vcpu_info.vector, |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 5368 | vcpu_info.pi_desc_addr, set); |
| 5369 | } |
| 5370 | |
| 5371 | if (ret < 0) { |
| 5372 | pr_err("%s: failed to update PI IRTE\n", __func__); |
| 5373 | goto out; |
| 5374 | } |
| 5375 | } |
| 5376 | |
| 5377 | ret = 0; |
| 5378 | out: |
| 5379 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 5380 | return ret; |
| 5381 | } |
| 5382 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5383 | static int svm_nmi_allowed(struct kvm_vcpu *vcpu) |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 5384 | { |
| 5385 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5386 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 924584c | 2010-04-22 12:33:07 +0200 | [diff] [blame] | 5387 | int ret; |
| 5388 | ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) && |
| 5389 | !(svm->vcpu.arch.hflags & HF_NMI_MASK); |
| 5390 | ret = ret && gif_set(svm) && nested_svm_nmi(svm); |
| 5391 | |
| 5392 | return ret; |
Joerg Roedel | aaacfc9 | 2008-04-16 16:51:18 +0200 | [diff] [blame] | 5393 | } |
| 5394 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5395 | static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 5396 | { |
| 5397 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5398 | |
| 5399 | return !!(svm->vcpu.arch.hflags & HF_NMI_MASK); |
| 5400 | } |
| 5401 | |
| 5402 | static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 5403 | { |
| 5404 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5405 | |
| 5406 | if (masked) { |
| 5407 | svm->vcpu.arch.hflags |= HF_NMI_MASK; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 5408 | set_intercept(svm, INTERCEPT_IRET); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5409 | } else { |
| 5410 | svm->vcpu.arch.hflags &= ~HF_NMI_MASK; |
Joerg Roedel | 8a05a1b8 | 2010-11-30 18:04:00 +0100 | [diff] [blame] | 5411 | clr_intercept(svm, INTERCEPT_IRET); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5412 | } |
| 5413 | } |
| 5414 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5415 | static int svm_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 5416 | { |
| 5417 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5418 | struct vmcb *vmcb = svm->vmcb; |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 5419 | int ret; |
| 5420 | |
| 5421 | if (!gif_set(svm) || |
| 5422 | (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)) |
| 5423 | return 0; |
| 5424 | |
Avi Kivity | f6e7847 | 2010-08-02 15:30:20 +0300 | [diff] [blame] | 5425 | ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF); |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 5426 | |
Joerg Roedel | 2030753 | 2010-11-29 17:51:48 +0100 | [diff] [blame] | 5427 | if (is_guest_mode(vcpu)) |
Joerg Roedel | 7fcdb51 | 2009-09-16 15:24:15 +0200 | [diff] [blame] | 5428 | return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK); |
| 5429 | |
| 5430 | return ret; |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5431 | } |
| 5432 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5433 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5434 | { |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5435 | struct vcpu_svm *svm = to_svm(vcpu); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5436 | |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 5437 | if (kvm_vcpu_apicv_active(vcpu)) |
| 5438 | return; |
| 5439 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 5440 | /* |
| 5441 | * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes |
| 5442 | * 1, because that's a separate STGI/VMRUN intercept. The next time we |
| 5443 | * get that intercept, this function will be called again though and |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 5444 | * we'll get the vintr intercept. However, if the vGIF feature is |
| 5445 | * enabled, the STGI interception will not occur. Enable the irq |
| 5446 | * window under the assumption that the hardware will set the GIF. |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 5447 | */ |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 5448 | if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) { |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5449 | svm_set_vintr(svm); |
| 5450 | svm_inject_irq(svm, 0x0); |
| 5451 | } |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5452 | } |
| 5453 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5454 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5455 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 5456 | struct vcpu_svm *svm = to_svm(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5457 | |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5458 | if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) |
| 5459 | == HF_NMI_MASK) |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5460 | return; /* IRET will cause a vm exit */ |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5461 | |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 5462 | if (!gif_set(svm)) { |
| 5463 | if (vgif_enabled(svm)) |
| 5464 | set_intercept(svm, INTERCEPT_STGI); |
Ladi Prosek | 1a5e185 | 2017-06-21 09:07:01 +0200 | [diff] [blame] | 5465 | return; /* STGI will cause a vm exit */ |
Janakarajan Natarajan | 640bd6e | 2017-08-23 09:57:19 -0500 | [diff] [blame] | 5466 | } |
Ladi Prosek | 1a5e185 | 2017-06-21 09:07:01 +0200 | [diff] [blame] | 5467 | |
| 5468 | if (svm->nested.exit_required) |
| 5469 | return; /* we're not going to run the guest yet */ |
| 5470 | |
Joerg Roedel | e023171 | 2010-02-24 18:59:10 +0100 | [diff] [blame] | 5471 | /* |
| 5472 | * Something prevents NMI from been injected. Single step over possible |
| 5473 | * problem (IRET or exception injection or interrupt shadow) |
| 5474 | */ |
Ladi Prosek | ab2f4d73 | 2017-06-21 09:06:58 +0200 | [diff] [blame] | 5475 | svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu); |
Jan Kiszka | 6be7d30 | 2009-10-18 13:24:54 +0200 | [diff] [blame] | 5476 | svm->nmi_singlestep = true; |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5477 | svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5478 | } |
| 5479 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5480 | static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 5481 | { |
| 5482 | return 0; |
| 5483 | } |
| 5484 | |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 5485 | static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) |
| 5486 | { |
| 5487 | return 0; |
| 5488 | } |
| 5489 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 5490 | static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 5491 | { |
Joerg Roedel | 38e5e92 | 2010-12-03 15:25:16 +0100 | [diff] [blame] | 5492 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5493 | |
| 5494 | if (static_cpu_has(X86_FEATURE_FLUSHBYASID)) |
| 5495 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; |
| 5496 | else |
| 5497 | svm->asid_generation--; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 5498 | } |
| 5499 | |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 5500 | static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva) |
| 5501 | { |
| 5502 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5503 | |
| 5504 | invlpga(gva, svm->vmcb->control.asid); |
| 5505 | } |
| 5506 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 5507 | static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) |
| 5508 | { |
| 5509 | } |
| 5510 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 5511 | static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) |
| 5512 | { |
| 5513 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5514 | |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 5515 | if (svm_nested_virtualize_tpr(vcpu)) |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 5516 | return; |
| 5517 | |
Roedel, Joerg | 4ee546b | 2010-12-03 10:50:51 +0100 | [diff] [blame] | 5518 | if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) { |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 5519 | int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; |
Gleb Natapov | 615d519 | 2009-04-21 17:45:05 +0300 | [diff] [blame] | 5520 | kvm_set_cr8(vcpu, cr8); |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 5521 | } |
| 5522 | } |
| 5523 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 5524 | static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) |
| 5525 | { |
| 5526 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5527 | u64 cr8; |
| 5528 | |
Suravee Suthikulpanit | 3bbf356 | 2016-05-04 14:09:51 -0500 | [diff] [blame] | 5529 | if (svm_nested_virtualize_tpr(vcpu) || |
| 5530 | kvm_vcpu_apicv_active(vcpu)) |
Joerg Roedel | 88ab24a | 2010-02-19 16:23:06 +0100 | [diff] [blame] | 5531 | return; |
| 5532 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 5533 | cr8 = kvm_get_cr8(vcpu); |
| 5534 | svm->vmcb->control.int_ctl &= ~V_TPR_MASK; |
| 5535 | svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; |
| 5536 | } |
| 5537 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5538 | static void svm_complete_interrupts(struct vcpu_svm *svm) |
| 5539 | { |
| 5540 | u8 vector; |
| 5541 | int type; |
| 5542 | u32 exitintinfo = svm->vmcb->control.exit_int_info; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 5543 | unsigned int3_injected = svm->int3_injected; |
| 5544 | |
| 5545 | svm->int3_injected = 0; |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5546 | |
Avi Kivity | bd3d1ec | 2011-02-03 15:29:52 +0200 | [diff] [blame] | 5547 | /* |
| 5548 | * If we've made progress since setting HF_IRET_MASK, we've |
| 5549 | * executed an IRET and can allow NMI injection. |
| 5550 | */ |
| 5551 | if ((svm->vcpu.arch.hflags & HF_IRET_MASK) |
| 5552 | && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) { |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5553 | svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5554 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
| 5555 | } |
Gleb Natapov | 44c1143 | 2009-05-11 13:35:52 +0300 | [diff] [blame] | 5556 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5557 | svm->vcpu.arch.nmi_injected = false; |
| 5558 | kvm_clear_exception_queue(&svm->vcpu); |
| 5559 | kvm_clear_interrupt_queue(&svm->vcpu); |
| 5560 | |
| 5561 | if (!(exitintinfo & SVM_EXITINTINFO_VALID)) |
| 5562 | return; |
| 5563 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5564 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
| 5565 | |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5566 | vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK; |
| 5567 | type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK; |
| 5568 | |
| 5569 | switch (type) { |
| 5570 | case SVM_EXITINTINFO_TYPE_NMI: |
| 5571 | svm->vcpu.arch.nmi_injected = true; |
| 5572 | break; |
| 5573 | case SVM_EXITINTINFO_TYPE_EXEPT: |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 5574 | /* |
| 5575 | * In case of software exceptions, do not reinject the vector, |
| 5576 | * but re-execute the instruction instead. Rewind RIP first |
| 5577 | * if we emulated INT3 before. |
| 5578 | */ |
| 5579 | if (kvm_exception_is_soft(vector)) { |
| 5580 | if (vector == BP_VECTOR && int3_injected && |
| 5581 | kvm_is_linear_rip(&svm->vcpu, svm->int3_rip)) |
| 5582 | kvm_rip_write(&svm->vcpu, |
| 5583 | kvm_rip_read(&svm->vcpu) - |
| 5584 | int3_injected); |
Alexander Graf | 219b65d | 2009-06-15 15:21:25 +0200 | [diff] [blame] | 5585 | break; |
Jan Kiszka | 66b7138 | 2010-02-23 17:47:56 +0100 | [diff] [blame] | 5586 | } |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5587 | if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) { |
| 5588 | u32 err = svm->vmcb->control.exit_int_info_err; |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 5589 | kvm_requeue_exception_e(&svm->vcpu, vector, err); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5590 | |
| 5591 | } else |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 5592 | kvm_requeue_exception(&svm->vcpu, vector); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5593 | break; |
| 5594 | case SVM_EXITINTINFO_TYPE_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5595 | kvm_queue_interrupt(&svm->vcpu, vector, false); |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5596 | break; |
| 5597 | default: |
| 5598 | break; |
| 5599 | } |
| 5600 | } |
| 5601 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 5602 | static void svm_cancel_injection(struct kvm_vcpu *vcpu) |
| 5603 | { |
| 5604 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5605 | struct vmcb_control_area *control = &svm->vmcb->control; |
| 5606 | |
| 5607 | control->exit_int_info = control->event_inj; |
| 5608 | control->exit_int_info_err = control->event_inj_err; |
| 5609 | control->event_inj = 0; |
| 5610 | svm_complete_interrupts(svm); |
| 5611 | } |
| 5612 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5613 | static void svm_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5614 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5615 | struct vcpu_svm *svm = to_svm(vcpu); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 5616 | |
Joerg Roedel | 2041a06 | 2010-04-22 12:33:08 +0200 | [diff] [blame] | 5617 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 5618 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 5619 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; |
| 5620 | |
Joerg Roedel | cd3ff65 | 2009-10-09 16:08:26 +0200 | [diff] [blame] | 5621 | /* |
| 5622 | * A vmexit emulation is required before the vcpu can be executed |
| 5623 | * again. |
| 5624 | */ |
| 5625 | if (unlikely(svm->nested.exit_required)) |
| 5626 | return; |
| 5627 | |
Ladi Prosek | a12713c | 2017-06-21 09:07:00 +0200 | [diff] [blame] | 5628 | /* |
| 5629 | * Disable singlestep if we're injecting an interrupt/exception. |
| 5630 | * We don't want our modified rflags to be pushed on the stack where |
| 5631 | * we might not be able to easily reset them if we disabled NMI |
| 5632 | * singlestep later. |
| 5633 | */ |
| 5634 | if (svm->nmi_singlestep && svm->vmcb->control.event_inj) { |
| 5635 | /* |
| 5636 | * Event injection happens before external interrupts cause a |
| 5637 | * vmexit and interrupts are disabled here, so smp_send_reschedule |
| 5638 | * is enough to force an immediate vmexit. |
| 5639 | */ |
| 5640 | disable_nmi_singlestep(svm); |
| 5641 | smp_send_reschedule(vcpu->cpu); |
| 5642 | } |
| 5643 | |
Rusty Russell | e756fc6 | 2007-07-30 20:07:08 +1000 | [diff] [blame] | 5644 | pre_svm_run(svm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5645 | |
Joerg Roedel | 649d686 | 2008-04-16 16:51:15 +0200 | [diff] [blame] | 5646 | sync_lapic_to_cr8(vcpu); |
| 5647 | |
Joerg Roedel | cda0ffd | 2009-08-07 11:49:45 +0200 | [diff] [blame] | 5648 | svm->vmcb->save.cr2 = vcpu->arch.cr2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5649 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 5650 | clgi(); |
Aaron Lewis | 139a12c | 2019-10-21 16:30:25 -0700 | [diff] [blame] | 5651 | kvm_load_guest_xsave_state(vcpu); |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 5652 | |
Wanpeng Li | b6c4bc6 | 2019-05-20 16:18:09 +0800 | [diff] [blame] | 5653 | if (lapic_in_kernel(vcpu) && |
| 5654 | vcpu->arch.apic->lapic_timer.timer_advance_ns) |
| 5655 | kvm_wait_lapic_expire(vcpu); |
| 5656 | |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 5657 | /* |
| 5658 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 5659 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 5660 | * is no need to worry about the conditional branch over the wrmsr |
| 5661 | * being speculatively taken. |
| 5662 | */ |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 5663 | x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl); |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 5664 | |
Thomas Gleixner | 024d83c | 2018-08-12 20:41:45 +0200 | [diff] [blame] | 5665 | local_irq_enable(); |
| 5666 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5667 | asm volatile ( |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5668 | "push %%" _ASM_BP "; \n\t" |
| 5669 | "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t" |
| 5670 | "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t" |
| 5671 | "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t" |
| 5672 | "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t" |
| 5673 | "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t" |
| 5674 | "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5675 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5676 | "mov %c[r8](%[svm]), %%r8 \n\t" |
| 5677 | "mov %c[r9](%[svm]), %%r9 \n\t" |
| 5678 | "mov %c[r10](%[svm]), %%r10 \n\t" |
| 5679 | "mov %c[r11](%[svm]), %%r11 \n\t" |
| 5680 | "mov %c[r12](%[svm]), %%r12 \n\t" |
| 5681 | "mov %c[r13](%[svm]), %%r13 \n\t" |
| 5682 | "mov %c[r14](%[svm]), %%r14 \n\t" |
| 5683 | "mov %c[r15](%[svm]), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5684 | #endif |
| 5685 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5686 | /* Enter guest mode */ |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5687 | "push %%" _ASM_AX " \n\t" |
| 5688 | "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t" |
Uros Bizjak | ac5ffda2 | 2018-11-26 17:00:08 +0100 | [diff] [blame] | 5689 | __ex("vmload %%" _ASM_AX) "\n\t" |
| 5690 | __ex("vmrun %%" _ASM_AX) "\n\t" |
| 5691 | __ex("vmsave %%" _ASM_AX) "\n\t" |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5692 | "pop %%" _ASM_AX " \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5693 | |
| 5694 | /* Save guest registers, load host registers */ |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5695 | "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t" |
| 5696 | "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t" |
| 5697 | "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t" |
| 5698 | "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t" |
| 5699 | "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t" |
| 5700 | "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5701 | #ifdef CONFIG_X86_64 |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5702 | "mov %%r8, %c[r8](%[svm]) \n\t" |
| 5703 | "mov %%r9, %c[r9](%[svm]) \n\t" |
| 5704 | "mov %%r10, %c[r10](%[svm]) \n\t" |
| 5705 | "mov %%r11, %c[r11](%[svm]) \n\t" |
| 5706 | "mov %%r12, %c[r12](%[svm]) \n\t" |
| 5707 | "mov %%r13, %c[r13](%[svm]) \n\t" |
| 5708 | "mov %%r14, %c[r14](%[svm]) \n\t" |
| 5709 | "mov %%r15, %c[r15](%[svm]) \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 5710 | /* |
| 5711 | * Clear host registers marked as clobbered to prevent |
| 5712 | * speculative use. |
| 5713 | */ |
Uros Bizjak | 43ce76c | 2018-10-17 16:46:57 +0200 | [diff] [blame] | 5714 | "xor %%r8d, %%r8d \n\t" |
| 5715 | "xor %%r9d, %%r9d \n\t" |
| 5716 | "xor %%r10d, %%r10d \n\t" |
| 5717 | "xor %%r11d, %%r11d \n\t" |
| 5718 | "xor %%r12d, %%r12d \n\t" |
| 5719 | "xor %%r13d, %%r13d \n\t" |
| 5720 | "xor %%r14d, %%r14d \n\t" |
| 5721 | "xor %%r15d, %%r15d \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 5722 | #endif |
Uros Bizjak | 43ce76c | 2018-10-17 16:46:57 +0200 | [diff] [blame] | 5723 | "xor %%ebx, %%ebx \n\t" |
| 5724 | "xor %%ecx, %%ecx \n\t" |
| 5725 | "xor %%edx, %%edx \n\t" |
| 5726 | "xor %%esi, %%esi \n\t" |
| 5727 | "xor %%edi, %%edi \n\t" |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5728 | "pop %%" _ASM_BP |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5729 | : |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5730 | : [svm]"a"(svm), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5731 | [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5732 | [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 5733 | [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 5734 | [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 5735 | [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 5736 | [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 5737 | [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP])) |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5738 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5739 | , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])), |
| 5740 | [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])), |
| 5741 | [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])), |
| 5742 | [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])), |
| 5743 | [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])), |
| 5744 | [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])), |
| 5745 | [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])), |
| 5746 | [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15])) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5747 | #endif |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 5748 | : "cc", "memory" |
| 5749 | #ifdef CONFIG_X86_64 |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5750 | , "rbx", "rcx", "rdx", "rsi", "rdi" |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 5751 | , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15" |
Avi Kivity | 7454766 | 2012-09-16 15:10:59 +0300 | [diff] [blame] | 5752 | #else |
| 5753 | , "ebx", "ecx", "edx", "esi", "edi" |
Laurent Vivier | 54a08c0 | 2007-10-25 14:18:53 +0200 | [diff] [blame] | 5754 | #endif |
| 5755 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5756 | |
Thomas Gleixner | 15e6c22 | 2018-05-11 15:21:01 +0200 | [diff] [blame] | 5757 | /* Eliminate branch target predictions from guest mode */ |
| 5758 | vmexit_fill_RSB(); |
| 5759 | |
| 5760 | #ifdef CONFIG_X86_64 |
| 5761 | wrmsrl(MSR_GS_BASE, svm->host.gs_base); |
| 5762 | #else |
| 5763 | loadsegment(fs, svm->host.fs); |
| 5764 | #ifndef CONFIG_X86_32_LAZY_GS |
| 5765 | loadsegment(gs, svm->host.gs); |
| 5766 | #endif |
| 5767 | #endif |
| 5768 | |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 5769 | /* |
| 5770 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 5771 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 5772 | * turn it off. This is much more efficient than blindly adding |
| 5773 | * it to the atomic save/restore list. Especially as the former |
| 5774 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 5775 | * |
| 5776 | * For non-nested case: |
| 5777 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 5778 | * save it. |
| 5779 | * |
| 5780 | * For nested case: |
| 5781 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 5782 | * save it. |
| 5783 | */ |
Paolo Bonzini | 946fbbc | 2018-02-22 16:43:18 +0100 | [diff] [blame] | 5784 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 5785 | svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
KarimAllah Ahmed | b2ac58f | 2018-02-03 15:56:23 +0100 | [diff] [blame] | 5786 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5787 | reload_tss(vcpu); |
| 5788 | |
Avi Kivity | 56ba47d | 2007-11-07 17:14:18 +0200 | [diff] [blame] | 5789 | local_irq_disable(); |
| 5790 | |
Thomas Gleixner | 024d83c | 2018-08-12 20:41:45 +0200 | [diff] [blame] | 5791 | x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl); |
| 5792 | |
Avi Kivity | 13c34e0 | 2010-10-21 12:20:31 +0200 | [diff] [blame] | 5793 | vcpu->arch.cr2 = svm->vmcb->save.cr2; |
| 5794 | vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; |
| 5795 | vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; |
| 5796 | vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; |
| 5797 | |
Joerg Roedel | 3781c01 | 2011-01-14 16:45:02 +0100 | [diff] [blame] | 5798 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 5799 | kvm_before_interrupt(&svm->vcpu); |
Joerg Roedel | 3781c01 | 2011-01-14 16:45:02 +0100 | [diff] [blame] | 5800 | |
Aaron Lewis | 139a12c | 2019-10-21 16:30:25 -0700 | [diff] [blame] | 5801 | kvm_load_host_xsave_state(vcpu); |
Joerg Roedel | 3781c01 | 2011-01-14 16:45:02 +0100 | [diff] [blame] | 5802 | stgi(); |
| 5803 | |
| 5804 | /* Any pending NMI will happen here */ |
| 5805 | |
| 5806 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 5807 | kvm_after_interrupt(&svm->vcpu); |
Joerg Roedel | 3781c01 | 2011-01-14 16:45:02 +0100 | [diff] [blame] | 5808 | |
Joerg Roedel | d7bf822 | 2008-04-16 16:51:17 +0200 | [diff] [blame] | 5809 | sync_cr8_to_lapic(vcpu); |
| 5810 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5811 | svm->next_rip = 0; |
Gleb Natapov | 9222be1 | 2009-04-23 17:14:37 +0300 | [diff] [blame] | 5812 | |
Joerg Roedel | 38e5e92 | 2010-12-03 15:25:16 +0100 | [diff] [blame] | 5813 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; |
| 5814 | |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 5815 | /* if exit due to PF check for async PF */ |
| 5816 | if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 5817 | 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] | 5818 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 5819 | if (npt_enabled) { |
| 5820 | vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR); |
| 5821 | vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR); |
| 5822 | } |
Joerg Roedel | fe5913e | 2010-05-17 14:43:34 +0200 | [diff] [blame] | 5823 | |
| 5824 | /* |
| 5825 | * We need to handle MC intercepts here before the vcpu has a chance to |
| 5826 | * change the physical cpu |
| 5827 | */ |
| 5828 | if (unlikely(svm->vmcb->control.exit_code == |
| 5829 | SVM_EXIT_EXCP_BASE + MC_VECTOR)) |
| 5830 | svm_handle_mce(svm); |
Roedel, Joerg | 8d28fec | 2010-12-03 13:15:21 +0100 | [diff] [blame] | 5831 | |
| 5832 | mark_all_clean(svm->vmcb); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5833 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 5834 | STACK_FRAME_NON_STANDARD(svm_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5835 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5836 | static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
| 5837 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5838 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5839 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 5840 | svm->vmcb->save.cr3 = __sme_set(root); |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 5841 | mark_dirty(svm->vmcb, VMCB_CR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5842 | } |
| 5843 | |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 5844 | static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
| 5845 | { |
| 5846 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5847 | |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 5848 | svm->vmcb->control.nested_cr3 = __sme_set(root); |
Joerg Roedel | b274716 | 2010-12-03 11:45:53 +0100 | [diff] [blame] | 5849 | mark_dirty(svm->vmcb, VMCB_NPT); |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 5850 | |
| 5851 | /* Also sync guest cr3 here in case we live migrate */ |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 5852 | svm->vmcb->save.cr3 = kvm_read_cr3(vcpu); |
Joerg Roedel | dcca1a6 | 2010-12-03 11:45:54 +0100 | [diff] [blame] | 5853 | mark_dirty(svm->vmcb, VMCB_CR); |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 5854 | } |
| 5855 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5856 | static int is_disabled(void) |
| 5857 | { |
Joerg Roedel | 6031a61 | 2007-06-22 12:29:50 +0300 | [diff] [blame] | 5858 | u64 vm_cr; |
| 5859 | |
| 5860 | rdmsrl(MSR_VM_CR, vm_cr); |
| 5861 | if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) |
| 5862 | return 1; |
| 5863 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5864 | return 0; |
| 5865 | } |
| 5866 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5867 | static void |
| 5868 | svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 5869 | { |
| 5870 | /* |
| 5871 | * Patch in the VMMCALL instruction: |
| 5872 | */ |
| 5873 | hypercall[0] = 0x0f; |
| 5874 | hypercall[1] = 0x01; |
| 5875 | hypercall[2] = 0xd9; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5876 | } |
| 5877 | |
Sean Christopherson | f257d6d | 2019-04-19 22:18:17 -0700 | [diff] [blame] | 5878 | static int __init svm_check_processor_compat(void) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 5879 | { |
Sean Christopherson | f257d6d | 2019-04-19 22:18:17 -0700 | [diff] [blame] | 5880 | return 0; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 5881 | } |
| 5882 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 5883 | static bool svm_cpu_has_accelerated_tpr(void) |
| 5884 | { |
| 5885 | return false; |
| 5886 | } |
| 5887 | |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 5888 | static bool svm_has_emulated_msr(int index) |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 5889 | { |
Vitaly Kuznetsov | e87555e | 2018-12-19 12:06:13 +0100 | [diff] [blame] | 5890 | switch (index) { |
| 5891 | case MSR_IA32_MCG_EXT_CTL: |
Paolo Bonzini | 95c5c7c | 2019-07-02 14:45:24 +0200 | [diff] [blame] | 5892 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
Vitaly Kuznetsov | e87555e | 2018-12-19 12:06:13 +0100 | [diff] [blame] | 5893 | return false; |
| 5894 | default: |
| 5895 | break; |
| 5896 | } |
| 5897 | |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 5898 | return true; |
| 5899 | } |
| 5900 | |
Paolo Bonzini | fc07e76 | 2015-10-01 13:20:22 +0200 | [diff] [blame] | 5901 | static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
| 5902 | { |
| 5903 | return 0; |
| 5904 | } |
| 5905 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 5906 | static void svm_cpuid_update(struct kvm_vcpu *vcpu) |
| 5907 | { |
Joerg Roedel | 6092d3d | 2015-10-14 15:10:54 +0200 | [diff] [blame] | 5908 | struct vcpu_svm *svm = to_svm(vcpu); |
| 5909 | |
Aaron Lewis | 7204160 | 2019-10-21 16:30:20 -0700 | [diff] [blame] | 5910 | vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && |
| 5911 | boot_cpu_has(X86_FEATURE_XSAVES); |
| 5912 | |
Joerg Roedel | 6092d3d | 2015-10-14 15:10:54 +0200 | [diff] [blame] | 5913 | /* Update nrips enabled cache */ |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 5914 | svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS); |
Suravee Suthikulpanit | 46781ea | 2016-05-04 14:09:50 -0500 | [diff] [blame] | 5915 | |
| 5916 | if (!kvm_vcpu_apicv_active(vcpu)) |
| 5917 | return; |
| 5918 | |
Radim Krčmář | 1b4d56b | 2017-08-05 00:12:50 +0200 | [diff] [blame] | 5919 | guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 5920 | } |
| 5921 | |
Paolo Bonzini | 50896de | 2019-08-15 09:17:31 +0200 | [diff] [blame] | 5922 | #define F(x) bit(X86_FEATURE_##x) |
| 5923 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 5924 | static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 5925 | { |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5926 | switch (func) { |
Suravee Suthikulpanit | 46781ea | 2016-05-04 14:09:50 -0500 | [diff] [blame] | 5927 | case 0x1: |
| 5928 | if (avic) |
| 5929 | entry->ecx &= ~bit(X86_FEATURE_X2APIC); |
| 5930 | break; |
Joerg Roedel | 4c62a2d | 2010-09-10 17:31:06 +0200 | [diff] [blame] | 5931 | case 0x80000001: |
| 5932 | if (nested) |
| 5933 | entry->ecx |= (1 << 2); /* Set SVM bit */ |
| 5934 | break; |
Paolo Bonzini | 50896de | 2019-08-15 09:17:31 +0200 | [diff] [blame] | 5935 | case 0x80000008: |
| 5936 | if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) || |
| 5937 | boot_cpu_has(X86_FEATURE_AMD_SSBD)) |
| 5938 | entry->ebx |= F(VIRT_SSBD); |
| 5939 | break; |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5940 | case 0x8000000A: |
| 5941 | entry->eax = 1; /* SVM revision 1 */ |
| 5942 | entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper |
| 5943 | ASID emulation to nested SVM */ |
| 5944 | entry->ecx = 0; /* Reserved */ |
Joerg Roedel | 7a19066 | 2010-07-27 18:14:21 +0200 | [diff] [blame] | 5945 | entry->edx = 0; /* Per default do not support any |
| 5946 | additional features */ |
| 5947 | |
| 5948 | /* Support next_rip if host supports it */ |
Avi Kivity | 2a6b20b | 2010-11-09 16:15:42 +0200 | [diff] [blame] | 5949 | if (boot_cpu_has(X86_FEATURE_NRIPS)) |
Paolo Bonzini | 50896de | 2019-08-15 09:17:31 +0200 | [diff] [blame] | 5950 | entry->edx |= F(NRIPS); |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5951 | |
Joerg Roedel | 3d4aeaa | 2010-09-10 17:31:05 +0200 | [diff] [blame] | 5952 | /* Support NPT for the guest if enabled */ |
| 5953 | if (npt_enabled) |
Paolo Bonzini | 50896de | 2019-08-15 09:17:31 +0200 | [diff] [blame] | 5954 | entry->edx |= F(NPT); |
Joerg Roedel | 3d4aeaa | 2010-09-10 17:31:05 +0200 | [diff] [blame] | 5955 | |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5956 | break; |
Brijesh Singh | 8765d75 | 2017-12-04 10:57:25 -0600 | [diff] [blame] | 5957 | case 0x8000001F: |
| 5958 | /* Support memory encryption cpuid if host supports it */ |
| 5959 | if (boot_cpu_has(X86_FEATURE_SEV)) |
| 5960 | cpuid(0x8000001f, &entry->eax, &entry->ebx, |
| 5961 | &entry->ecx, &entry->edx); |
| 5962 | |
Joerg Roedel | c2c63a4 | 2010-04-22 12:33:12 +0200 | [diff] [blame] | 5963 | } |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 5964 | } |
| 5965 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 5966 | static int svm_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 5967 | { |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 5968 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 5969 | } |
| 5970 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 5971 | static bool svm_rdtscp_supported(void) |
| 5972 | { |
Paolo Bonzini | 46896c7 | 2015-11-12 14:49:16 +0100 | [diff] [blame] | 5973 | return boot_cpu_has(X86_FEATURE_RDTSCP); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 5974 | } |
| 5975 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 5976 | static bool svm_invpcid_supported(void) |
| 5977 | { |
| 5978 | return false; |
| 5979 | } |
| 5980 | |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 5981 | static bool svm_mpx_supported(void) |
| 5982 | { |
| 5983 | return false; |
| 5984 | } |
| 5985 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 5986 | static bool svm_xsaves_supported(void) |
| 5987 | { |
Aaron Lewis | 5229743 | 2019-10-21 16:30:27 -0700 | [diff] [blame] | 5988 | return boot_cpu_has(X86_FEATURE_XSAVES); |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 5989 | } |
| 5990 | |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 5991 | static bool svm_umip_emulated(void) |
| 5992 | { |
| 5993 | return false; |
| 5994 | } |
| 5995 | |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 5996 | static bool svm_pt_supported(void) |
| 5997 | { |
| 5998 | return false; |
| 5999 | } |
| 6000 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 6001 | static bool svm_has_wbinvd_exit(void) |
| 6002 | { |
| 6003 | return true; |
| 6004 | } |
| 6005 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 6006 | #define PRE_EX(exit) { .exit_code = (exit), \ |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 6007 | .stage = X86_ICPT_PRE_EXCEPT, } |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 6008 | #define POST_EX(exit) { .exit_code = (exit), \ |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 6009 | .stage = X86_ICPT_POST_EXCEPT, } |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 6010 | #define POST_MEM(exit) { .exit_code = (exit), \ |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 6011 | .stage = X86_ICPT_POST_MEMACCESS, } |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 6012 | |
Mathias Krause | 09941fb | 2012-08-30 01:30:20 +0200 | [diff] [blame] | 6013 | static const struct __x86_intercept { |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 6014 | u32 exit_code; |
| 6015 | enum x86_intercept_stage stage; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 6016 | } x86_intercept_map[] = { |
| 6017 | [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0), |
| 6018 | [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 6019 | [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 6020 | [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0), |
| 6021 | [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0), |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 6022 | [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0), |
| 6023 | [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0), |
Joerg Roedel | dee6bb7 | 2011-04-04 12:39:30 +0200 | [diff] [blame] | 6024 | [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ), |
| 6025 | [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ), |
| 6026 | [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE), |
| 6027 | [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE), |
| 6028 | [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ), |
| 6029 | [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ), |
| 6030 | [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE), |
| 6031 | [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE), |
Joerg Roedel | 01de8b0 | 2011-04-04 12:39:31 +0200 | [diff] [blame] | 6032 | [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN), |
| 6033 | [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL), |
| 6034 | [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD), |
| 6035 | [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE), |
| 6036 | [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI), |
| 6037 | [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI), |
| 6038 | [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT), |
| 6039 | [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA), |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 6040 | [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP), |
| 6041 | [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR), |
| 6042 | [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT), |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 6043 | [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG), |
| 6044 | [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD), |
| 6045 | [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD), |
| 6046 | [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR), |
| 6047 | [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC), |
| 6048 | [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR), |
| 6049 | [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC), |
| 6050 | [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID), |
| 6051 | [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM), |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 6052 | [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE), |
| 6053 | [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF), |
| 6054 | [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF), |
| 6055 | [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT), |
| 6056 | [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET), |
| 6057 | [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP), |
| 6058 | [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT), |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 6059 | [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO), |
| 6060 | [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO), |
| 6061 | [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO), |
| 6062 | [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO), |
Vitaly Kuznetsov | 02d4160 | 2019-08-13 15:53:32 +0200 | [diff] [blame] | 6063 | [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV), |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 6064 | }; |
| 6065 | |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 6066 | #undef PRE_EX |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 6067 | #undef POST_EX |
Joerg Roedel | d7eb820 | 2011-04-04 12:39:32 +0200 | [diff] [blame] | 6068 | #undef POST_MEM |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 6069 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 6070 | static int svm_check_intercept(struct kvm_vcpu *vcpu, |
| 6071 | struct x86_instruction_info *info, |
| 6072 | enum x86_intercept_stage stage) |
| 6073 | { |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 6074 | struct vcpu_svm *svm = to_svm(vcpu); |
| 6075 | int vmexit, ret = X86EMUL_CONTINUE; |
| 6076 | struct __x86_intercept icpt_info; |
| 6077 | struct vmcb *vmcb = svm->vmcb; |
| 6078 | |
| 6079 | if (info->intercept >= ARRAY_SIZE(x86_intercept_map)) |
| 6080 | goto out; |
| 6081 | |
| 6082 | icpt_info = x86_intercept_map[info->intercept]; |
| 6083 | |
Avi Kivity | 40e19b5 | 2011-04-21 12:35:41 +0300 | [diff] [blame] | 6084 | if (stage != icpt_info.stage) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 6085 | goto out; |
| 6086 | |
| 6087 | switch (icpt_info.exit_code) { |
| 6088 | case SVM_EXIT_READ_CR0: |
| 6089 | if (info->intercept == x86_intercept_cr_read) |
| 6090 | icpt_info.exit_code += info->modrm_reg; |
| 6091 | break; |
| 6092 | case SVM_EXIT_WRITE_CR0: { |
| 6093 | unsigned long cr0, val; |
| 6094 | u64 intercept; |
| 6095 | |
| 6096 | if (info->intercept == x86_intercept_cr_write) |
| 6097 | icpt_info.exit_code += info->modrm_reg; |
| 6098 | |
Jan Kiszka | 62baf44 | 2014-06-29 21:55:53 +0200 | [diff] [blame] | 6099 | if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 || |
| 6100 | info->intercept == x86_intercept_clts) |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 6101 | break; |
| 6102 | |
| 6103 | intercept = svm->nested.intercept; |
| 6104 | |
| 6105 | if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))) |
| 6106 | break; |
| 6107 | |
| 6108 | cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK; |
| 6109 | val = info->src_val & ~SVM_CR0_SELECTIVE_MASK; |
| 6110 | |
| 6111 | if (info->intercept == x86_intercept_lmsw) { |
| 6112 | cr0 &= 0xfUL; |
| 6113 | val &= 0xfUL; |
| 6114 | /* lmsw can't clear PE - catch this here */ |
| 6115 | if (cr0 & X86_CR0_PE) |
| 6116 | val |= X86_CR0_PE; |
| 6117 | } |
| 6118 | |
| 6119 | if (cr0 ^ val) |
| 6120 | icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE; |
| 6121 | |
| 6122 | break; |
| 6123 | } |
Joerg Roedel | 3b88e41 | 2011-04-04 12:39:29 +0200 | [diff] [blame] | 6124 | case SVM_EXIT_READ_DR0: |
| 6125 | case SVM_EXIT_WRITE_DR0: |
| 6126 | icpt_info.exit_code += info->modrm_reg; |
| 6127 | break; |
Joerg Roedel | 8061252 | 2011-04-04 12:39:33 +0200 | [diff] [blame] | 6128 | case SVM_EXIT_MSR: |
| 6129 | if (info->intercept == x86_intercept_wrmsr) |
| 6130 | vmcb->control.exit_info_1 = 1; |
| 6131 | else |
| 6132 | vmcb->control.exit_info_1 = 0; |
| 6133 | break; |
Joerg Roedel | bf608f8 | 2011-04-04 12:39:34 +0200 | [diff] [blame] | 6134 | case SVM_EXIT_PAUSE: |
| 6135 | /* |
| 6136 | * We get this for NOP only, but pause |
| 6137 | * is rep not, check this here |
| 6138 | */ |
| 6139 | if (info->rep_prefix != REPE_PREFIX) |
| 6140 | goto out; |
Jan H. Schönherr | 49a8afc | 2017-09-05 23:58:44 +0200 | [diff] [blame] | 6141 | break; |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 6142 | case SVM_EXIT_IOIO: { |
| 6143 | u64 exit_info; |
| 6144 | u32 bytes; |
| 6145 | |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 6146 | if (info->intercept == x86_intercept_in || |
| 6147 | info->intercept == x86_intercept_ins) { |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 6148 | exit_info = ((info->src_val & 0xffff) << 16) | |
| 6149 | SVM_IOIO_TYPE_MASK; |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 6150 | bytes = info->dst_bytes; |
Jan Kiszka | 6493f15 | 2014-06-30 11:07:05 +0200 | [diff] [blame] | 6151 | } else { |
Jan Kiszka | 6cbc5f5 | 2014-06-30 12:52:55 +0200 | [diff] [blame] | 6152 | exit_info = (info->dst_val & 0xffff) << 16; |
Jan Kiszka | 6493f15 | 2014-06-30 11:07:05 +0200 | [diff] [blame] | 6153 | bytes = info->src_bytes; |
Joerg Roedel | f651193 | 2011-04-04 12:39:35 +0200 | [diff] [blame] | 6154 | } |
| 6155 | |
| 6156 | if (info->intercept == x86_intercept_outs || |
| 6157 | info->intercept == x86_intercept_ins) |
| 6158 | exit_info |= SVM_IOIO_STR_MASK; |
| 6159 | |
| 6160 | if (info->rep_prefix) |
| 6161 | exit_info |= SVM_IOIO_REP_MASK; |
| 6162 | |
| 6163 | bytes = min(bytes, 4u); |
| 6164 | |
| 6165 | exit_info |= bytes << SVM_IOIO_SIZE_SHIFT; |
| 6166 | |
| 6167 | exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1); |
| 6168 | |
| 6169 | vmcb->control.exit_info_1 = exit_info; |
| 6170 | vmcb->control.exit_info_2 = info->next_rip; |
| 6171 | |
| 6172 | break; |
| 6173 | } |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 6174 | default: |
| 6175 | break; |
| 6176 | } |
| 6177 | |
Bandan Das | f104765 | 2015-06-11 02:05:33 -0400 | [diff] [blame] | 6178 | /* TODO: Advertise NRIPS to guest hypervisor unconditionally */ |
| 6179 | if (static_cpu_has(X86_FEATURE_NRIPS)) |
| 6180 | vmcb->control.next_rip = info->next_rip; |
Joerg Roedel | cfec82c | 2011-04-04 12:39:28 +0200 | [diff] [blame] | 6181 | vmcb->control.exit_code = icpt_info.exit_code; |
| 6182 | vmexit = nested_svm_exit_handled(svm); |
| 6183 | |
| 6184 | ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED |
| 6185 | : X86EMUL_CONTINUE; |
| 6186 | |
| 6187 | out: |
| 6188 | return ret; |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 6189 | } |
| 6190 | |
Sean Christopherson | 95b5a48 | 2019-04-19 22:50:59 -0700 | [diff] [blame] | 6191 | static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu) |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6192 | { |
Sean Christopherson | d7a0888 | 2019-07-10 09:07:34 -0700 | [diff] [blame] | 6193 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6194 | } |
| 6195 | |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 6196 | static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu) |
| 6197 | { |
Babu Moger | 8566ac8 | 2018-03-16 16:37:26 -0400 | [diff] [blame] | 6198 | if (pause_filter_thresh) |
| 6199 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 6200 | } |
| 6201 | |
Suravee Suthikulpanit | be8ca17 | 2016-05-04 14:09:49 -0500 | [diff] [blame] | 6202 | static inline void avic_post_state_restore(struct kvm_vcpu *vcpu) |
| 6203 | { |
| 6204 | if (avic_handle_apic_id_update(vcpu) != 0) |
| 6205 | return; |
Suthikulpanit, Suravee | 98d9058 | 2019-01-29 08:08:42 +0000 | [diff] [blame] | 6206 | avic_handle_dfr_update(vcpu); |
Suravee Suthikulpanit | be8ca17 | 2016-05-04 14:09:49 -0500 | [diff] [blame] | 6207 | avic_handle_ldr_update(vcpu); |
| 6208 | } |
| 6209 | |
Borislav Petkov | 74f1690 | 2017-03-26 23:51:24 +0200 | [diff] [blame] | 6210 | static void svm_setup_mce(struct kvm_vcpu *vcpu) |
| 6211 | { |
| 6212 | /* [63:9] are reserved. */ |
| 6213 | vcpu->arch.mcg_cap &= 0x1ff; |
| 6214 | } |
| 6215 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 6216 | static int svm_smi_allowed(struct kvm_vcpu *vcpu) |
| 6217 | { |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 6218 | struct vcpu_svm *svm = to_svm(vcpu); |
| 6219 | |
| 6220 | /* Per APM Vol.2 15.22.2 "Response to SMI" */ |
| 6221 | if (!gif_set(svm)) |
| 6222 | return 0; |
| 6223 | |
| 6224 | if (is_guest_mode(&svm->vcpu) && |
| 6225 | svm->nested.intercept & (1ULL << INTERCEPT_SMI)) { |
| 6226 | /* TODO: Might need to set exit_info_1 and exit_info_2 here */ |
| 6227 | svm->vmcb->control.exit_code = SVM_EXIT_SMI; |
| 6228 | svm->nested.exit_required = true; |
| 6229 | return 0; |
| 6230 | } |
| 6231 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 6232 | return 1; |
| 6233 | } |
| 6234 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 6235 | static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 6236 | { |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 6237 | struct vcpu_svm *svm = to_svm(vcpu); |
| 6238 | int ret; |
| 6239 | |
| 6240 | if (is_guest_mode(vcpu)) { |
| 6241 | /* FED8h - SVM Guest */ |
| 6242 | put_smstate(u64, smstate, 0x7ed8, 1); |
| 6243 | /* FEE0h - SVM Guest VMCB Physical Address */ |
| 6244 | put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb); |
| 6245 | |
| 6246 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 6247 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; |
| 6248 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; |
| 6249 | |
| 6250 | ret = nested_svm_vmexit(svm); |
| 6251 | if (ret) |
| 6252 | return ret; |
| 6253 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 6254 | return 0; |
| 6255 | } |
| 6256 | |
Sean Christopherson | ed19321 | 2019-04-02 08:03:09 -0700 | [diff] [blame] | 6257 | static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate) |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 6258 | { |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 6259 | struct vcpu_svm *svm = to_svm(vcpu); |
| 6260 | struct vmcb *nested_vmcb; |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 6261 | struct kvm_host_map map; |
Sean Christopherson | ed19321 | 2019-04-02 08:03:09 -0700 | [diff] [blame] | 6262 | u64 guest; |
| 6263 | u64 vmcb; |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 6264 | |
Sean Christopherson | ed19321 | 2019-04-02 08:03:09 -0700 | [diff] [blame] | 6265 | guest = GET_SMSTATE(u64, smstate, 0x7ed8); |
| 6266 | vmcb = GET_SMSTATE(u64, smstate, 0x7ee0); |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 6267 | |
Sean Christopherson | ed19321 | 2019-04-02 08:03:09 -0700 | [diff] [blame] | 6268 | if (guest) { |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 6269 | if (kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb), &map) == -EINVAL) |
Sean Christopherson | 9ec1949 | 2019-04-02 08:03:11 -0700 | [diff] [blame] | 6270 | return 1; |
KarimAllah Ahmed | 8c5fbf1 | 2019-01-31 21:24:40 +0100 | [diff] [blame] | 6271 | nested_vmcb = map.hva; |
| 6272 | enter_svm_guest_mode(svm, vmcb, nested_vmcb, &map); |
Ladi Prosek | 05cade7 | 2017-10-11 16:54:45 +0200 | [diff] [blame] | 6273 | } |
Sean Christopherson | 9ec1949 | 2019-04-02 08:03:11 -0700 | [diff] [blame] | 6274 | return 0; |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 6275 | } |
| 6276 | |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 6277 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
| 6278 | { |
| 6279 | struct vcpu_svm *svm = to_svm(vcpu); |
| 6280 | |
| 6281 | if (!gif_set(svm)) { |
| 6282 | if (vgif_enabled(svm)) |
| 6283 | set_intercept(svm, INTERCEPT_STGI); |
| 6284 | /* STGI will cause a vm exit */ |
| 6285 | return 1; |
| 6286 | } |
| 6287 | return 0; |
| 6288 | } |
| 6289 | |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 6290 | static int sev_flush_asids(void) |
| 6291 | { |
| 6292 | int ret, error; |
| 6293 | |
| 6294 | /* |
| 6295 | * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail, |
| 6296 | * so it must be guarded. |
| 6297 | */ |
| 6298 | down_write(&sev_deactivate_lock); |
| 6299 | |
| 6300 | wbinvd_on_all_cpus(); |
| 6301 | ret = sev_guest_df_flush(&error); |
| 6302 | |
| 6303 | up_write(&sev_deactivate_lock); |
| 6304 | |
| 6305 | if (ret) |
| 6306 | pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error); |
| 6307 | |
| 6308 | return ret; |
| 6309 | } |
| 6310 | |
| 6311 | /* Must be called with the sev_bitmap_lock held */ |
| 6312 | static bool __sev_recycle_asids(void) |
| 6313 | { |
| 6314 | int pos; |
| 6315 | |
| 6316 | /* Check if there are any ASIDs to reclaim before performing a flush */ |
| 6317 | pos = find_next_bit(sev_reclaim_asid_bitmap, |
| 6318 | max_sev_asid, min_sev_asid - 1); |
| 6319 | if (pos >= max_sev_asid) |
| 6320 | return false; |
| 6321 | |
| 6322 | if (sev_flush_asids()) |
| 6323 | return false; |
| 6324 | |
| 6325 | bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap, |
| 6326 | max_sev_asid); |
| 6327 | bitmap_zero(sev_reclaim_asid_bitmap, max_sev_asid); |
| 6328 | |
| 6329 | return true; |
| 6330 | } |
| 6331 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6332 | static int sev_asid_new(void) |
| 6333 | { |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 6334 | bool retry = true; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6335 | int pos; |
| 6336 | |
Tom Lendacky | e3b9a9e | 2019-10-03 21:17:43 +0000 | [diff] [blame] | 6337 | mutex_lock(&sev_bitmap_lock); |
| 6338 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6339 | /* |
| 6340 | * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid. |
| 6341 | */ |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 6342 | again: |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6343 | pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1); |
Tom Lendacky | e3b9a9e | 2019-10-03 21:17:43 +0000 | [diff] [blame] | 6344 | if (pos >= max_sev_asid) { |
Tom Lendacky | 33af3a7 | 2019-10-03 21:17:48 +0000 | [diff] [blame] | 6345 | if (retry && __sev_recycle_asids()) { |
| 6346 | retry = false; |
| 6347 | goto again; |
| 6348 | } |
Tom Lendacky | e3b9a9e | 2019-10-03 21:17:43 +0000 | [diff] [blame] | 6349 | mutex_unlock(&sev_bitmap_lock); |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6350 | return -EBUSY; |
Tom Lendacky | e3b9a9e | 2019-10-03 21:17:43 +0000 | [diff] [blame] | 6351 | } |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6352 | |
Tom Lendacky | e3b9a9e | 2019-10-03 21:17:43 +0000 | [diff] [blame] | 6353 | __set_bit(pos, sev_asid_bitmap); |
| 6354 | |
| 6355 | mutex_unlock(&sev_bitmap_lock); |
| 6356 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6357 | return pos + 1; |
| 6358 | } |
| 6359 | |
| 6360 | static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6361 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 6362 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6363 | int asid, ret; |
| 6364 | |
| 6365 | ret = -EBUSY; |
David Rientjes | 3f14a89 | 2019-01-02 12:56:33 -0800 | [diff] [blame] | 6366 | if (unlikely(sev->active)) |
| 6367 | return ret; |
| 6368 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6369 | asid = sev_asid_new(); |
| 6370 | if (asid < 0) |
| 6371 | return ret; |
| 6372 | |
| 6373 | ret = sev_platform_init(&argp->error); |
| 6374 | if (ret) |
| 6375 | goto e_free; |
| 6376 | |
| 6377 | sev->active = true; |
| 6378 | sev->asid = asid; |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 6379 | INIT_LIST_HEAD(&sev->regions_list); |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6380 | |
| 6381 | return 0; |
| 6382 | |
| 6383 | e_free: |
Tom Lendacky | e3b9a9e | 2019-10-03 21:17:43 +0000 | [diff] [blame] | 6384 | sev_asid_free(asid); |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 6385 | return ret; |
| 6386 | } |
| 6387 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6388 | static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error) |
| 6389 | { |
| 6390 | struct sev_data_activate *data; |
| 6391 | int asid = sev_get_asid(kvm); |
| 6392 | int ret; |
| 6393 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 6394 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6395 | if (!data) |
| 6396 | return -ENOMEM; |
| 6397 | |
| 6398 | /* activate ASID on the given handle */ |
| 6399 | data->handle = handle; |
| 6400 | data->asid = asid; |
| 6401 | ret = sev_guest_activate(data, error); |
| 6402 | kfree(data); |
| 6403 | |
| 6404 | return ret; |
| 6405 | } |
| 6406 | |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6407 | 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] | 6408 | { |
| 6409 | struct fd f; |
| 6410 | int ret; |
| 6411 | |
| 6412 | f = fdget(fd); |
| 6413 | if (!f.file) |
| 6414 | return -EBADF; |
| 6415 | |
| 6416 | ret = sev_issue_cmd_external_user(f.file, id, data, error); |
| 6417 | |
| 6418 | fdput(f); |
| 6419 | return ret; |
| 6420 | } |
| 6421 | |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6422 | static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error) |
| 6423 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 6424 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6425 | |
| 6426 | return __sev_issue_cmd(sev->fd, id, data, error); |
| 6427 | } |
| 6428 | |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6429 | static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6430 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 6431 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6432 | struct sev_data_launch_start *start; |
| 6433 | struct kvm_sev_launch_start params; |
| 6434 | void *dh_blob, *session_blob; |
| 6435 | int *error = &argp->error; |
| 6436 | int ret; |
| 6437 | |
| 6438 | if (!sev_guest(kvm)) |
| 6439 | return -ENOTTY; |
| 6440 | |
| 6441 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 6442 | return -EFAULT; |
| 6443 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 6444 | start = kzalloc(sizeof(*start), GFP_KERNEL_ACCOUNT); |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6445 | if (!start) |
| 6446 | return -ENOMEM; |
| 6447 | |
| 6448 | dh_blob = NULL; |
| 6449 | if (params.dh_uaddr) { |
| 6450 | dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len); |
| 6451 | if (IS_ERR(dh_blob)) { |
| 6452 | ret = PTR_ERR(dh_blob); |
| 6453 | goto e_free; |
| 6454 | } |
| 6455 | |
| 6456 | start->dh_cert_address = __sme_set(__pa(dh_blob)); |
| 6457 | start->dh_cert_len = params.dh_len; |
| 6458 | } |
| 6459 | |
| 6460 | session_blob = NULL; |
| 6461 | if (params.session_uaddr) { |
| 6462 | session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len); |
| 6463 | if (IS_ERR(session_blob)) { |
| 6464 | ret = PTR_ERR(session_blob); |
| 6465 | goto e_free_dh; |
| 6466 | } |
| 6467 | |
| 6468 | start->session_address = __sme_set(__pa(session_blob)); |
| 6469 | start->session_len = params.session_len; |
| 6470 | } |
| 6471 | |
| 6472 | start->handle = params.handle; |
| 6473 | start->policy = params.policy; |
| 6474 | |
| 6475 | /* create memory encryption context */ |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6476 | 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] | 6477 | if (ret) |
| 6478 | goto e_free_session; |
| 6479 | |
| 6480 | /* Bind ASID to this guest */ |
| 6481 | ret = sev_bind_asid(kvm, start->handle, error); |
| 6482 | if (ret) |
| 6483 | goto e_free_session; |
| 6484 | |
| 6485 | /* return handle to userspace */ |
| 6486 | params.handle = start->handle; |
| 6487 | if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) { |
| 6488 | sev_unbind_asid(kvm, start->handle); |
| 6489 | ret = -EFAULT; |
| 6490 | goto e_free_session; |
| 6491 | } |
| 6492 | |
| 6493 | sev->handle = start->handle; |
| 6494 | sev->fd = argp->sev_fd; |
| 6495 | |
| 6496 | e_free_session: |
| 6497 | kfree(session_blob); |
| 6498 | e_free_dh: |
| 6499 | kfree(dh_blob); |
| 6500 | e_free: |
| 6501 | kfree(start); |
| 6502 | return ret; |
| 6503 | } |
| 6504 | |
David Rientjes | ede885e | 2019-03-19 15:19:56 -0700 | [diff] [blame] | 6505 | static unsigned long get_num_contig_pages(unsigned long idx, |
| 6506 | struct page **inpages, unsigned long npages) |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6507 | { |
| 6508 | unsigned long paddr, next_paddr; |
David Rientjes | ede885e | 2019-03-19 15:19:56 -0700 | [diff] [blame] | 6509 | unsigned long i = idx + 1, pages = 1; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6510 | |
| 6511 | /* find the number of contiguous pages starting from idx */ |
| 6512 | paddr = __sme_page_pa(inpages[idx]); |
| 6513 | while (i < npages) { |
| 6514 | next_paddr = __sme_page_pa(inpages[i++]); |
| 6515 | if ((paddr + PAGE_SIZE) == next_paddr) { |
| 6516 | pages++; |
| 6517 | paddr = next_paddr; |
| 6518 | continue; |
| 6519 | } |
| 6520 | break; |
| 6521 | } |
| 6522 | |
| 6523 | return pages; |
| 6524 | } |
| 6525 | |
| 6526 | static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6527 | { |
David Rientjes | ede885e | 2019-03-19 15:19:56 -0700 | [diff] [blame] | 6528 | unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i; |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 6529 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6530 | struct kvm_sev_launch_update_data params; |
| 6531 | struct sev_data_launch_update_data *data; |
| 6532 | struct page **inpages; |
David Rientjes | ede885e | 2019-03-19 15:19:56 -0700 | [diff] [blame] | 6533 | int ret; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6534 | |
| 6535 | if (!sev_guest(kvm)) |
| 6536 | return -ENOTTY; |
| 6537 | |
| 6538 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 6539 | return -EFAULT; |
| 6540 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 6541 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 6542 | if (!data) |
| 6543 | return -ENOMEM; |
| 6544 | |
| 6545 | vaddr = params.uaddr; |
| 6546 | size = params.len; |
| 6547 | vaddr_end = vaddr + size; |
| 6548 | |
| 6549 | /* Lock the user memory. */ |
| 6550 | inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1); |
| 6551 | if (!inpages) { |
| 6552 | ret = -ENOMEM; |
| 6553 | goto e_free; |
| 6554 | } |
| 6555 | |
| 6556 | /* |
| 6557 | * The LAUNCH_UPDATE command will perform in-place encryption of the |
| 6558 | * memory content (i.e it will write the same memory region with C=1). |
| 6559 | * It's possible that the cache may contain the data with C=0, i.e., |
| 6560 | * unencrypted so invalidate it first. |
| 6561 | */ |
| 6562 | sev_clflush_pages(inpages, npages); |
| 6563 | |
| 6564 | for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) { |
| 6565 | int offset, len; |
| 6566 | |
| 6567 | /* |
| 6568 | * If the user buffer is not page-aligned, calculate the offset |
| 6569 | * within the page. |
| 6570 | */ |
| 6571 | offset = vaddr & (PAGE_SIZE - 1); |
| 6572 | |
| 6573 | /* Calculate the number of pages that can be encrypted in one go. */ |
| 6574 | pages = get_num_contig_pages(i, inpages, npages); |
| 6575 | |
| 6576 | len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size); |
| 6577 | |
| 6578 | data->handle = sev->handle; |
| 6579 | data->len = len; |
| 6580 | data->address = __sme_page_pa(inpages[i]) + offset; |
| 6581 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error); |
| 6582 | if (ret) |
| 6583 | goto e_unpin; |
| 6584 | |
| 6585 | size -= len; |
| 6586 | next_vaddr = vaddr + len; |
| 6587 | } |
| 6588 | |
| 6589 | e_unpin: |
| 6590 | /* content of memory is updated, mark pages dirty */ |
| 6591 | for (i = 0; i < npages; i++) { |
| 6592 | set_page_dirty_lock(inpages[i]); |
| 6593 | mark_page_accessed(inpages[i]); |
| 6594 | } |
| 6595 | /* unlock the user pages */ |
| 6596 | sev_unpin_memory(kvm, inpages, npages); |
| 6597 | e_free: |
| 6598 | kfree(data); |
| 6599 | return ret; |
| 6600 | } |
| 6601 | |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6602 | static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6603 | { |
Brijesh Singh | 3e23338 | 2018-02-23 12:36:50 -0600 | [diff] [blame] | 6604 | void __user *measure = (void __user *)(uintptr_t)argp->data; |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 6605 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6606 | struct sev_data_launch_measure *data; |
| 6607 | struct kvm_sev_launch_measure params; |
Brijesh Singh | 3e23338 | 2018-02-23 12:36:50 -0600 | [diff] [blame] | 6608 | void __user *p = NULL; |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6609 | void *blob = NULL; |
| 6610 | int ret; |
| 6611 | |
| 6612 | if (!sev_guest(kvm)) |
| 6613 | return -ENOTTY; |
| 6614 | |
Brijesh Singh | 3e23338 | 2018-02-23 12:36:50 -0600 | [diff] [blame] | 6615 | if (copy_from_user(¶ms, measure, sizeof(params))) |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6616 | return -EFAULT; |
| 6617 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 6618 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6619 | if (!data) |
| 6620 | return -ENOMEM; |
| 6621 | |
| 6622 | /* User wants to query the blob length */ |
| 6623 | if (!params.len) |
| 6624 | goto cmd; |
| 6625 | |
Brijesh Singh | 3e23338 | 2018-02-23 12:36:50 -0600 | [diff] [blame] | 6626 | p = (void __user *)(uintptr_t)params.uaddr; |
| 6627 | if (p) { |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6628 | if (params.len > SEV_FW_BLOB_MAX_SIZE) { |
| 6629 | ret = -EINVAL; |
| 6630 | goto e_free; |
| 6631 | } |
| 6632 | |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6633 | ret = -ENOMEM; |
| 6634 | blob = kmalloc(params.len, GFP_KERNEL); |
| 6635 | if (!blob) |
| 6636 | goto e_free; |
| 6637 | |
| 6638 | data->address = __psp_pa(blob); |
| 6639 | data->len = params.len; |
| 6640 | } |
| 6641 | |
| 6642 | cmd: |
| 6643 | data->handle = sev->handle; |
| 6644 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error); |
| 6645 | |
| 6646 | /* |
| 6647 | * If we query the session length, FW responded with expected data. |
| 6648 | */ |
| 6649 | if (!params.len) |
| 6650 | goto done; |
| 6651 | |
| 6652 | if (ret) |
| 6653 | goto e_free_blob; |
| 6654 | |
| 6655 | if (blob) { |
Brijesh Singh | 3e23338 | 2018-02-23 12:36:50 -0600 | [diff] [blame] | 6656 | if (copy_to_user(p, blob, params.len)) |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6657 | ret = -EFAULT; |
| 6658 | } |
| 6659 | |
| 6660 | done: |
| 6661 | params.len = data->len; |
Brijesh Singh | 3e23338 | 2018-02-23 12:36:50 -0600 | [diff] [blame] | 6662 | if (copy_to_user(measure, ¶ms, sizeof(params))) |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6663 | ret = -EFAULT; |
| 6664 | e_free_blob: |
| 6665 | kfree(blob); |
| 6666 | e_free: |
| 6667 | kfree(data); |
| 6668 | return ret; |
| 6669 | } |
| 6670 | |
Brijesh Singh | 5bdb0e2 | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6671 | static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6672 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 6673 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 5bdb0e2 | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6674 | struct sev_data_launch_finish *data; |
| 6675 | int ret; |
| 6676 | |
| 6677 | if (!sev_guest(kvm)) |
| 6678 | return -ENOTTY; |
| 6679 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 6680 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
Brijesh Singh | 5bdb0e2 | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 6681 | if (!data) |
| 6682 | return -ENOMEM; |
| 6683 | |
| 6684 | data->handle = sev->handle; |
| 6685 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error); |
| 6686 | |
| 6687 | kfree(data); |
| 6688 | return ret; |
| 6689 | } |
| 6690 | |
Brijesh Singh | 255d9e7 | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6691 | static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6692 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 6693 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 255d9e7 | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6694 | struct kvm_sev_guest_status params; |
| 6695 | struct sev_data_guest_status *data; |
| 6696 | int ret; |
| 6697 | |
| 6698 | if (!sev_guest(kvm)) |
| 6699 | return -ENOTTY; |
| 6700 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 6701 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
Brijesh Singh | 255d9e7 | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6702 | if (!data) |
| 6703 | return -ENOMEM; |
| 6704 | |
| 6705 | data->handle = sev->handle; |
| 6706 | ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error); |
| 6707 | if (ret) |
| 6708 | goto e_free; |
| 6709 | |
| 6710 | params.policy = data->policy; |
| 6711 | params.state = data->state; |
| 6712 | params.handle = data->handle; |
| 6713 | |
| 6714 | if (copy_to_user((void __user *)(uintptr_t)argp->data, ¶ms, sizeof(params))) |
| 6715 | ret = -EFAULT; |
| 6716 | e_free: |
| 6717 | kfree(data); |
| 6718 | return ret; |
| 6719 | } |
| 6720 | |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6721 | static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src, |
| 6722 | unsigned long dst, int size, |
| 6723 | int *error, bool enc) |
| 6724 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 6725 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6726 | struct sev_data_dbg *data; |
| 6727 | int ret; |
| 6728 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 6729 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6730 | if (!data) |
| 6731 | return -ENOMEM; |
| 6732 | |
| 6733 | data->handle = sev->handle; |
| 6734 | data->dst_addr = dst; |
| 6735 | data->src_addr = src; |
| 6736 | data->len = size; |
| 6737 | |
| 6738 | ret = sev_issue_cmd(kvm, |
| 6739 | enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT, |
| 6740 | data, error); |
| 6741 | kfree(data); |
| 6742 | return ret; |
| 6743 | } |
| 6744 | |
| 6745 | static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr, |
| 6746 | unsigned long dst_paddr, int sz, int *err) |
| 6747 | { |
| 6748 | int offset; |
| 6749 | |
| 6750 | /* |
| 6751 | * Its safe to read more than we are asked, caller should ensure that |
| 6752 | * destination has enough space. |
| 6753 | */ |
| 6754 | src_paddr = round_down(src_paddr, 16); |
| 6755 | offset = src_paddr & 15; |
| 6756 | sz = round_up(sz + offset, 16); |
| 6757 | |
| 6758 | return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false); |
| 6759 | } |
| 6760 | |
| 6761 | static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr, |
| 6762 | unsigned long __user dst_uaddr, |
| 6763 | unsigned long dst_paddr, |
| 6764 | int size, int *err) |
| 6765 | { |
| 6766 | struct page *tpage = NULL; |
| 6767 | int ret, offset; |
| 6768 | |
| 6769 | /* if inputs are not 16-byte then use intermediate buffer */ |
| 6770 | if (!IS_ALIGNED(dst_paddr, 16) || |
| 6771 | !IS_ALIGNED(paddr, 16) || |
| 6772 | !IS_ALIGNED(size, 16)) { |
| 6773 | tpage = (void *)alloc_page(GFP_KERNEL); |
| 6774 | if (!tpage) |
| 6775 | return -ENOMEM; |
| 6776 | |
| 6777 | dst_paddr = __sme_page_pa(tpage); |
| 6778 | } |
| 6779 | |
| 6780 | ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err); |
| 6781 | if (ret) |
| 6782 | goto e_free; |
| 6783 | |
| 6784 | if (tpage) { |
| 6785 | offset = paddr & 15; |
| 6786 | if (copy_to_user((void __user *)(uintptr_t)dst_uaddr, |
| 6787 | page_address(tpage) + offset, size)) |
| 6788 | ret = -EFAULT; |
| 6789 | } |
| 6790 | |
| 6791 | e_free: |
| 6792 | if (tpage) |
| 6793 | __free_page(tpage); |
| 6794 | |
| 6795 | return ret; |
| 6796 | } |
| 6797 | |
Brijesh Singh | 7d1594f | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6798 | static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr, |
| 6799 | unsigned long __user vaddr, |
| 6800 | unsigned long dst_paddr, |
| 6801 | unsigned long __user dst_vaddr, |
| 6802 | int size, int *error) |
| 6803 | { |
| 6804 | struct page *src_tpage = NULL; |
| 6805 | struct page *dst_tpage = NULL; |
| 6806 | int ret, len = size; |
| 6807 | |
| 6808 | /* If source buffer is not aligned then use an intermediate buffer */ |
| 6809 | if (!IS_ALIGNED(vaddr, 16)) { |
| 6810 | src_tpage = alloc_page(GFP_KERNEL); |
| 6811 | if (!src_tpage) |
| 6812 | return -ENOMEM; |
| 6813 | |
| 6814 | if (copy_from_user(page_address(src_tpage), |
| 6815 | (void __user *)(uintptr_t)vaddr, size)) { |
| 6816 | __free_page(src_tpage); |
| 6817 | return -EFAULT; |
| 6818 | } |
| 6819 | |
| 6820 | paddr = __sme_page_pa(src_tpage); |
| 6821 | } |
| 6822 | |
| 6823 | /* |
| 6824 | * If destination buffer or length is not aligned then do read-modify-write: |
| 6825 | * - decrypt destination in an intermediate buffer |
| 6826 | * - copy the source buffer in an intermediate buffer |
| 6827 | * - use the intermediate buffer as source buffer |
| 6828 | */ |
| 6829 | if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) { |
| 6830 | int dst_offset; |
| 6831 | |
| 6832 | dst_tpage = alloc_page(GFP_KERNEL); |
| 6833 | if (!dst_tpage) { |
| 6834 | ret = -ENOMEM; |
| 6835 | goto e_free; |
| 6836 | } |
| 6837 | |
| 6838 | ret = __sev_dbg_decrypt(kvm, dst_paddr, |
| 6839 | __sme_page_pa(dst_tpage), size, error); |
| 6840 | if (ret) |
| 6841 | goto e_free; |
| 6842 | |
| 6843 | /* |
| 6844 | * If source is kernel buffer then use memcpy() otherwise |
| 6845 | * copy_from_user(). |
| 6846 | */ |
| 6847 | dst_offset = dst_paddr & 15; |
| 6848 | |
| 6849 | if (src_tpage) |
| 6850 | memcpy(page_address(dst_tpage) + dst_offset, |
| 6851 | page_address(src_tpage), size); |
| 6852 | else { |
| 6853 | if (copy_from_user(page_address(dst_tpage) + dst_offset, |
| 6854 | (void __user *)(uintptr_t)vaddr, size)) { |
| 6855 | ret = -EFAULT; |
| 6856 | goto e_free; |
| 6857 | } |
| 6858 | } |
| 6859 | |
| 6860 | paddr = __sme_page_pa(dst_tpage); |
| 6861 | dst_paddr = round_down(dst_paddr, 16); |
| 6862 | len = round_up(size, 16); |
| 6863 | } |
| 6864 | |
| 6865 | ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true); |
| 6866 | |
| 6867 | e_free: |
| 6868 | if (src_tpage) |
| 6869 | __free_page(src_tpage); |
| 6870 | if (dst_tpage) |
| 6871 | __free_page(dst_tpage); |
| 6872 | return ret; |
| 6873 | } |
| 6874 | |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6875 | static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) |
| 6876 | { |
| 6877 | unsigned long vaddr, vaddr_end, next_vaddr; |
Colin Ian King | 0186ec8 | 2018-08-28 16:22:28 +0100 | [diff] [blame] | 6878 | unsigned long dst_vaddr; |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6879 | struct page **src_p, **dst_p; |
| 6880 | struct kvm_sev_dbg debug; |
| 6881 | unsigned long n; |
David Rientjes | b86bc28 | 2019-03-25 11:47:31 -0700 | [diff] [blame] | 6882 | unsigned int size; |
| 6883 | int ret; |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6884 | |
| 6885 | if (!sev_guest(kvm)) |
| 6886 | return -ENOTTY; |
| 6887 | |
| 6888 | if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug))) |
| 6889 | return -EFAULT; |
| 6890 | |
David Rientjes | b86bc28 | 2019-03-25 11:47:31 -0700 | [diff] [blame] | 6891 | if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr) |
| 6892 | return -EINVAL; |
| 6893 | if (!debug.dst_uaddr) |
| 6894 | return -EINVAL; |
| 6895 | |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6896 | vaddr = debug.src_uaddr; |
| 6897 | size = debug.len; |
| 6898 | vaddr_end = vaddr + size; |
| 6899 | dst_vaddr = debug.dst_uaddr; |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6900 | |
| 6901 | for (; vaddr < vaddr_end; vaddr = next_vaddr) { |
| 6902 | int len, s_off, d_off; |
| 6903 | |
| 6904 | /* lock userspace source and destination page */ |
| 6905 | src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0); |
| 6906 | if (!src_p) |
| 6907 | return -EFAULT; |
| 6908 | |
| 6909 | dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1); |
| 6910 | if (!dst_p) { |
| 6911 | sev_unpin_memory(kvm, src_p, n); |
| 6912 | return -EFAULT; |
| 6913 | } |
| 6914 | |
| 6915 | /* |
| 6916 | * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the |
| 6917 | * memory content (i.e it will write the same memory region with C=1). |
| 6918 | * It's possible that the cache may contain the data with C=0, i.e., |
| 6919 | * unencrypted so invalidate it first. |
| 6920 | */ |
| 6921 | sev_clflush_pages(src_p, 1); |
| 6922 | sev_clflush_pages(dst_p, 1); |
| 6923 | |
| 6924 | /* |
| 6925 | * Since user buffer may not be page aligned, calculate the |
| 6926 | * offset within the page. |
| 6927 | */ |
| 6928 | s_off = vaddr & ~PAGE_MASK; |
| 6929 | d_off = dst_vaddr & ~PAGE_MASK; |
| 6930 | len = min_t(size_t, (PAGE_SIZE - s_off), size); |
| 6931 | |
Brijesh Singh | 7d1594f | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6932 | if (dec) |
| 6933 | ret = __sev_dbg_decrypt_user(kvm, |
| 6934 | __sme_page_pa(src_p[0]) + s_off, |
| 6935 | dst_vaddr, |
| 6936 | __sme_page_pa(dst_p[0]) + d_off, |
| 6937 | len, &argp->error); |
| 6938 | else |
| 6939 | ret = __sev_dbg_encrypt_user(kvm, |
| 6940 | __sme_page_pa(src_p[0]) + s_off, |
| 6941 | vaddr, |
| 6942 | __sme_page_pa(dst_p[0]) + d_off, |
| 6943 | dst_vaddr, |
| 6944 | len, &argp->error); |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6945 | |
David Rientjes | b86bc28 | 2019-03-25 11:47:31 -0700 | [diff] [blame] | 6946 | sev_unpin_memory(kvm, src_p, n); |
| 6947 | sev_unpin_memory(kvm, dst_p, n); |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 6948 | |
| 6949 | if (ret) |
| 6950 | goto err; |
| 6951 | |
| 6952 | next_vaddr = vaddr + len; |
| 6953 | dst_vaddr = dst_vaddr + len; |
| 6954 | size -= len; |
| 6955 | } |
| 6956 | err: |
| 6957 | return ret; |
| 6958 | } |
| 6959 | |
Brijesh Singh | 9f5b5b9 | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 6960 | static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) |
| 6961 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 6962 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 9f5b5b9 | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 6963 | struct sev_data_launch_secret *data; |
| 6964 | struct kvm_sev_launch_secret params; |
| 6965 | struct page **pages; |
| 6966 | void *blob, *hdr; |
| 6967 | unsigned long n; |
Brijesh Singh | 9c5e0af | 2018-02-19 10:13:25 -0600 | [diff] [blame] | 6968 | int ret, offset; |
Brijesh Singh | 9f5b5b9 | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 6969 | |
| 6970 | if (!sev_guest(kvm)) |
| 6971 | return -ENOTTY; |
| 6972 | |
| 6973 | if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) |
| 6974 | return -EFAULT; |
| 6975 | |
| 6976 | pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1); |
| 6977 | if (!pages) |
| 6978 | return -ENOMEM; |
| 6979 | |
| 6980 | /* |
| 6981 | * The secret must be copied into contiguous memory region, lets verify |
| 6982 | * that userspace memory pages are contiguous before we issue command. |
| 6983 | */ |
| 6984 | if (get_num_contig_pages(0, pages, n) != n) { |
| 6985 | ret = -EINVAL; |
| 6986 | goto e_unpin_memory; |
| 6987 | } |
| 6988 | |
| 6989 | ret = -ENOMEM; |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 6990 | data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); |
Brijesh Singh | 9f5b5b9 | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 6991 | if (!data) |
| 6992 | goto e_unpin_memory; |
| 6993 | |
Brijesh Singh | 9c5e0af | 2018-02-19 10:13:25 -0600 | [diff] [blame] | 6994 | offset = params.guest_uaddr & (PAGE_SIZE - 1); |
| 6995 | data->guest_address = __sme_page_pa(pages[0]) + offset; |
| 6996 | data->guest_len = params.guest_len; |
| 6997 | |
Brijesh Singh | 9f5b5b9 | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 6998 | blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len); |
| 6999 | if (IS_ERR(blob)) { |
| 7000 | ret = PTR_ERR(blob); |
| 7001 | goto e_free; |
| 7002 | } |
| 7003 | |
| 7004 | data->trans_address = __psp_pa(blob); |
| 7005 | data->trans_len = params.trans_len; |
| 7006 | |
| 7007 | hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len); |
| 7008 | if (IS_ERR(hdr)) { |
| 7009 | ret = PTR_ERR(hdr); |
| 7010 | goto e_free_blob; |
| 7011 | } |
Brijesh Singh | 9c5e0af | 2018-02-19 10:13:25 -0600 | [diff] [blame] | 7012 | data->hdr_address = __psp_pa(hdr); |
| 7013 | data->hdr_len = params.hdr_len; |
Brijesh Singh | 9f5b5b9 | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 7014 | |
| 7015 | data->handle = sev->handle; |
| 7016 | ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error); |
| 7017 | |
| 7018 | kfree(hdr); |
| 7019 | |
| 7020 | e_free_blob: |
| 7021 | kfree(blob); |
| 7022 | e_free: |
| 7023 | kfree(data); |
| 7024 | e_unpin_memory: |
| 7025 | sev_unpin_memory(kvm, pages, n); |
| 7026 | return ret; |
| 7027 | } |
| 7028 | |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 7029 | static int svm_mem_enc_op(struct kvm *kvm, void __user *argp) |
| 7030 | { |
| 7031 | struct kvm_sev_cmd sev_cmd; |
| 7032 | int r; |
| 7033 | |
| 7034 | if (!svm_sev_enabled()) |
| 7035 | return -ENOTTY; |
| 7036 | |
| 7037 | if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd))) |
| 7038 | return -EFAULT; |
| 7039 | |
| 7040 | mutex_lock(&kvm->lock); |
| 7041 | |
| 7042 | switch (sev_cmd.id) { |
| 7043 | case KVM_SEV_INIT: |
| 7044 | r = sev_guest_init(kvm, &sev_cmd); |
| 7045 | break; |
Brijesh Singh | 59414c9 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 7046 | case KVM_SEV_LAUNCH_START: |
| 7047 | r = sev_launch_start(kvm, &sev_cmd); |
| 7048 | break; |
Brijesh Singh | 89c5058 | 2017-12-04 10:57:35 -0600 | [diff] [blame] | 7049 | case KVM_SEV_LAUNCH_UPDATE_DATA: |
| 7050 | r = sev_launch_update_data(kvm, &sev_cmd); |
| 7051 | break; |
Brijesh Singh | 0d0736f | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 7052 | case KVM_SEV_LAUNCH_MEASURE: |
| 7053 | r = sev_launch_measure(kvm, &sev_cmd); |
| 7054 | break; |
Brijesh Singh | 5bdb0e2 | 2017-12-04 10:57:36 -0600 | [diff] [blame] | 7055 | case KVM_SEV_LAUNCH_FINISH: |
| 7056 | r = sev_launch_finish(kvm, &sev_cmd); |
| 7057 | break; |
Brijesh Singh | 255d9e7 | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 7058 | case KVM_SEV_GUEST_STATUS: |
| 7059 | r = sev_guest_status(kvm, &sev_cmd); |
| 7060 | break; |
Brijesh Singh | 24f41fb | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 7061 | case KVM_SEV_DBG_DECRYPT: |
| 7062 | r = sev_dbg_crypt(kvm, &sev_cmd, true); |
| 7063 | break; |
Brijesh Singh | 7d1594f | 2017-12-04 10:57:37 -0600 | [diff] [blame] | 7064 | case KVM_SEV_DBG_ENCRYPT: |
| 7065 | r = sev_dbg_crypt(kvm, &sev_cmd, false); |
| 7066 | break; |
Brijesh Singh | 9f5b5b9 | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 7067 | case KVM_SEV_LAUNCH_SECRET: |
| 7068 | r = sev_launch_secret(kvm, &sev_cmd); |
| 7069 | break; |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 7070 | default: |
| 7071 | r = -EINVAL; |
| 7072 | goto out; |
| 7073 | } |
| 7074 | |
| 7075 | if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd))) |
| 7076 | r = -EFAULT; |
| 7077 | |
| 7078 | out: |
| 7079 | mutex_unlock(&kvm->lock); |
| 7080 | return r; |
| 7081 | } |
| 7082 | |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 7083 | static int svm_register_enc_region(struct kvm *kvm, |
| 7084 | struct kvm_enc_region *range) |
| 7085 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 7086 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 7087 | struct enc_region *region; |
| 7088 | int ret = 0; |
| 7089 | |
| 7090 | if (!sev_guest(kvm)) |
| 7091 | return -ENOTTY; |
| 7092 | |
Dan Carpenter | 86bf20c | 2018-05-19 09:01:36 +0300 | [diff] [blame] | 7093 | if (range->addr > ULONG_MAX || range->size > ULONG_MAX) |
| 7094 | return -EINVAL; |
| 7095 | |
Ben Gardon | 1ec6964 | 2019-02-11 11:02:51 -0800 | [diff] [blame] | 7096 | region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT); |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 7097 | if (!region) |
| 7098 | return -ENOMEM; |
| 7099 | |
| 7100 | region->pages = sev_pin_memory(kvm, range->addr, range->size, ®ion->npages, 1); |
| 7101 | if (!region->pages) { |
| 7102 | ret = -ENOMEM; |
| 7103 | goto e_free; |
| 7104 | } |
| 7105 | |
| 7106 | /* |
| 7107 | * The guest may change the memory encryption attribute from C=0 -> C=1 |
| 7108 | * or vice versa for this memory range. Lets make sure caches are |
| 7109 | * flushed to ensure that guest data gets written into memory with |
| 7110 | * correct C-bit. |
| 7111 | */ |
| 7112 | sev_clflush_pages(region->pages, region->npages); |
| 7113 | |
| 7114 | region->uaddr = range->addr; |
| 7115 | region->size = range->size; |
| 7116 | |
| 7117 | mutex_lock(&kvm->lock); |
| 7118 | list_add_tail(®ion->list, &sev->regions_list); |
| 7119 | mutex_unlock(&kvm->lock); |
| 7120 | |
| 7121 | return ret; |
| 7122 | |
| 7123 | e_free: |
| 7124 | kfree(region); |
| 7125 | return ret; |
| 7126 | } |
| 7127 | |
| 7128 | static struct enc_region * |
| 7129 | find_enc_region(struct kvm *kvm, struct kvm_enc_region *range) |
| 7130 | { |
Sean Christopherson | 81811c1 | 2018-03-20 12:17:21 -0700 | [diff] [blame] | 7131 | struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 7132 | struct list_head *head = &sev->regions_list; |
| 7133 | struct enc_region *i; |
| 7134 | |
| 7135 | list_for_each_entry(i, head, list) { |
| 7136 | if (i->uaddr == range->addr && |
| 7137 | i->size == range->size) |
| 7138 | return i; |
| 7139 | } |
| 7140 | |
| 7141 | return NULL; |
| 7142 | } |
| 7143 | |
| 7144 | |
| 7145 | static int svm_unregister_enc_region(struct kvm *kvm, |
| 7146 | struct kvm_enc_region *range) |
| 7147 | { |
| 7148 | struct enc_region *region; |
| 7149 | int ret; |
| 7150 | |
| 7151 | mutex_lock(&kvm->lock); |
| 7152 | |
| 7153 | if (!sev_guest(kvm)) { |
| 7154 | ret = -ENOTTY; |
| 7155 | goto failed; |
| 7156 | } |
| 7157 | |
| 7158 | region = find_enc_region(kvm, range); |
| 7159 | if (!region) { |
| 7160 | ret = -EINVAL; |
| 7161 | goto failed; |
| 7162 | } |
| 7163 | |
| 7164 | __unregister_enc_region_locked(kvm, region); |
| 7165 | |
| 7166 | mutex_unlock(&kvm->lock); |
| 7167 | return 0; |
| 7168 | |
| 7169 | failed: |
| 7170 | mutex_unlock(&kvm->lock); |
| 7171 | return ret; |
| 7172 | } |
| 7173 | |
Singh, Brijesh | 05d5a48 | 2019-02-15 17:24:12 +0000 | [diff] [blame] | 7174 | static bool svm_need_emulation_on_page_fault(struct kvm_vcpu *vcpu) |
| 7175 | { |
Liran Alon | 118154b | 2019-07-17 02:56:58 +0300 | [diff] [blame] | 7176 | unsigned long cr4 = kvm_read_cr4(vcpu); |
| 7177 | bool smep = cr4 & X86_CR4_SMEP; |
| 7178 | bool smap = cr4 & X86_CR4_SMAP; |
| 7179 | bool is_user = svm_get_cpl(vcpu) == 3; |
Singh, Brijesh | 05d5a48 | 2019-02-15 17:24:12 +0000 | [diff] [blame] | 7180 | |
| 7181 | /* |
Liran Alon | 118154b | 2019-07-17 02:56:58 +0300 | [diff] [blame] | 7182 | * Detect and workaround Errata 1096 Fam_17h_00_0Fh. |
| 7183 | * |
| 7184 | * Errata: |
| 7185 | * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is |
| 7186 | * possible that CPU microcode implementing DecodeAssist will fail |
| 7187 | * to read bytes of instruction which caused #NPF. In this case, |
| 7188 | * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly |
| 7189 | * return 0 instead of the correct guest instruction bytes. |
| 7190 | * |
| 7191 | * This happens because CPU microcode reading instruction bytes |
| 7192 | * uses a special opcode which attempts to read data using CPL=0 |
| 7193 | * priviledges. The microcode reads CS:RIP and if it hits a SMAP |
| 7194 | * fault, it gives up and returns no instruction bytes. |
| 7195 | * |
| 7196 | * Detection: |
| 7197 | * We reach here in case CPU supports DecodeAssist, raised #NPF and |
| 7198 | * returned 0 in GuestIntrBytes field of the VMCB. |
| 7199 | * First, errata can only be triggered in case vCPU CR4.SMAP=1. |
| 7200 | * Second, if vCPU CR4.SMEP=1, errata could only be triggered |
| 7201 | * in case vCPU CPL==3 (Because otherwise guest would have triggered |
| 7202 | * a SMEP fault instead of #NPF). |
| 7203 | * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL. |
| 7204 | * As most guests enable SMAP if they have also enabled SMEP, use above |
| 7205 | * logic in order to attempt minimize false-positive of detecting errata |
| 7206 | * while still preserving all cases semantic correctness. |
| 7207 | * |
| 7208 | * Workaround: |
| 7209 | * To determine what instruction the guest was executing, the hypervisor |
| 7210 | * will have to decode the instruction at the instruction pointer. |
Singh, Brijesh | 05d5a48 | 2019-02-15 17:24:12 +0000 | [diff] [blame] | 7211 | * |
| 7212 | * In non SEV guest, hypervisor will be able to read the guest |
| 7213 | * memory to decode the instruction pointer when insn_len is zero |
| 7214 | * so we return true to indicate that decoding is possible. |
| 7215 | * |
| 7216 | * But in the SEV guest, the guest memory is encrypted with the |
| 7217 | * guest specific key and hypervisor will not be able to decode the |
| 7218 | * instruction pointer so we will not able to workaround it. Lets |
| 7219 | * print the error and request to kill the guest. |
| 7220 | */ |
Liran Alon | 118154b | 2019-07-17 02:56:58 +0300 | [diff] [blame] | 7221 | if (smap && (!smep || is_user)) { |
Singh, Brijesh | 05d5a48 | 2019-02-15 17:24:12 +0000 | [diff] [blame] | 7222 | if (!sev_guest(vcpu->kvm)) |
| 7223 | return true; |
| 7224 | |
Liran Alon | 118154b | 2019-07-17 02:56:58 +0300 | [diff] [blame] | 7225 | pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n"); |
Singh, Brijesh | 05d5a48 | 2019-02-15 17:24:12 +0000 | [diff] [blame] | 7226 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 7227 | } |
| 7228 | |
| 7229 | return false; |
| 7230 | } |
| 7231 | |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 7232 | static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu) |
| 7233 | { |
| 7234 | struct vcpu_svm *svm = to_svm(vcpu); |
| 7235 | |
| 7236 | /* |
| 7237 | * TODO: Last condition latch INIT signals on vCPU when |
| 7238 | * vCPU is in guest-mode and vmcb12 defines intercept on INIT. |
| 7239 | * To properly emulate the INIT intercept, SVM should implement |
| 7240 | * kvm_x86_ops->check_nested_events() and call nested_svm_vmexit() |
| 7241 | * there if an INIT signal is pending. |
| 7242 | */ |
| 7243 | return !gif_set(svm) || |
| 7244 | (svm->vmcb->control.intercept & (1ULL << INTERCEPT_INIT)); |
| 7245 | } |
| 7246 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 7247 | static struct kvm_x86_ops svm_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7248 | .cpu_has_kvm_support = has_svm, |
| 7249 | .disabled_by_bios = is_disabled, |
| 7250 | .hardware_setup = svm_hardware_setup, |
| 7251 | .hardware_unsetup = svm_hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 7252 | .check_processor_compatibility = svm_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7253 | .hardware_enable = svm_hardware_enable, |
| 7254 | .hardware_disable = svm_hardware_disable, |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 7255 | .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 7256 | .has_emulated_msr = svm_has_emulated_msr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7257 | |
| 7258 | .vcpu_create = svm_create_vcpu, |
| 7259 | .vcpu_free = svm_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 7260 | .vcpu_reset = svm_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7261 | |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 7262 | .vm_alloc = svm_vm_alloc, |
| 7263 | .vm_free = svm_vm_free, |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 7264 | .vm_init = avic_vm_init, |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 7265 | .vm_destroy = svm_vm_destroy, |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 7266 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 7267 | .prepare_guest_switch = svm_prepare_guest_switch, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7268 | .vcpu_load = svm_vcpu_load, |
| 7269 | .vcpu_put = svm_vcpu_put, |
Suravee Suthikulpanit | 8221c13 | 2016-05-04 14:09:52 -0500 | [diff] [blame] | 7270 | .vcpu_blocking = svm_vcpu_blocking, |
| 7271 | .vcpu_unblocking = svm_vcpu_unblocking, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7272 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 7273 | .update_bp_intercept = update_bp_intercept, |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 7274 | .get_msr_feature = svm_get_msr_feature, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7275 | .get_msr = svm_get_msr, |
| 7276 | .set_msr = svm_set_msr, |
| 7277 | .get_segment_base = svm_get_segment_base, |
| 7278 | .get_segment = svm_get_segment, |
| 7279 | .set_segment = svm_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 7280 | .get_cpl = svm_get_cpl, |
Rusty Russell | 1747fb7 | 2007-09-06 01:21:32 +1000 | [diff] [blame] | 7281 | .get_cs_db_l_bits = kvm_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 7282 | .decache_cr0_guest_bits = svm_decache_cr0_guest_bits, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 7283 | .decache_cr4_guest_bits = svm_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7284 | .set_cr0 = svm_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7285 | .set_cr3 = svm_set_cr3, |
| 7286 | .set_cr4 = svm_set_cr4, |
| 7287 | .set_efer = svm_set_efer, |
| 7288 | .get_idt = svm_get_idt, |
| 7289 | .set_idt = svm_set_idt, |
| 7290 | .get_gdt = svm_get_gdt, |
| 7291 | .set_gdt = svm_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 7292 | .get_dr6 = svm_get_dr6, |
| 7293 | .set_dr6 = svm_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7294 | .set_dr7 = svm_set_dr7, |
Paolo Bonzini | facb013 | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7295 | .sync_dirty_debug_regs = svm_sync_dirty_debug_regs, |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 7296 | .cache_reg = svm_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7297 | .get_rflags = svm_get_rflags, |
| 7298 | .set_rflags = svm_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 7299 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7300 | .tlb_flush = svm_flush_tlb, |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 7301 | .tlb_flush_gva = svm_flush_tlb_gva, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7302 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7303 | .run = svm_vcpu_run, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 7304 | .handle_exit = handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7305 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 7306 | .set_interrupt_shadow = svm_set_interrupt_shadow, |
| 7307 | .get_interrupt_shadow = svm_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7308 | .patch_hypercall = svm_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 7309 | .set_irq = svm_set_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7310 | .set_nmi = svm_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 7311 | .queue_exception = svm_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 7312 | .cancel_injection = svm_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 7313 | .interrupt_allowed = svm_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7314 | .nmi_allowed = svm_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 7315 | .get_nmi_mask = svm_get_nmi_mask, |
| 7316 | .set_nmi_mask = svm_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7317 | .enable_nmi_window = enable_nmi_window, |
| 7318 | .enable_irq_window = enable_irq_window, |
| 7319 | .update_cr8_intercept = update_cr8_intercept, |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 7320 | .set_virtual_apic_mode = svm_set_virtual_apic_mode, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 7321 | .get_enable_apicv = svm_get_enable_apicv, |
| 7322 | .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7323 | .load_eoi_exitmap = svm_load_eoi_exitmap, |
Suravee Suthikulpanit | 44a95da | 2016-05-04 14:09:46 -0500 | [diff] [blame] | 7324 | .hwapic_irr_update = svm_hwapic_irr_update, |
| 7325 | .hwapic_isr_update = svm_hwapic_isr_update, |
Liran Alon | fa59cc0 | 2017-12-24 18:12:53 +0200 | [diff] [blame] | 7326 | .sync_pir_to_irr = kvm_lapic_find_highest_irr, |
Suravee Suthikulpanit | be8ca17 | 2016-05-04 14:09:49 -0500 | [diff] [blame] | 7327 | .apicv_post_state_restore = avic_post_state_restore, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 7328 | |
| 7329 | .set_tss_addr = svm_set_tss_addr, |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 7330 | .set_identity_map_addr = svm_set_identity_map_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 7331 | .get_tdp_level = get_npt_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 7332 | .get_mt_mask = svm_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7333 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7334 | .get_exit_info = svm_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7335 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 7336 | .get_lpage_level = svm_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 7337 | |
| 7338 | .cpuid_update = svm_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 7339 | |
| 7340 | .rdtscp_supported = svm_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 7341 | .invpcid_supported = svm_invpcid_supported, |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 7342 | .mpx_supported = svm_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 7343 | .xsaves_supported = svm_xsaves_supported, |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 7344 | .umip_emulated = svm_umip_emulated, |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 7345 | .pt_supported = svm_pt_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 7346 | |
| 7347 | .set_supported_cpuid = svm_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 7348 | |
| 7349 | .has_wbinvd_exit = svm_has_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 7350 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 7351 | .read_l1_tsc_offset = svm_read_l1_tsc_offset, |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 7352 | .write_l1_tsc_offset = svm_write_l1_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 7353 | |
| 7354 | .set_tdp_cr3 = set_tdp_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 7355 | |
| 7356 | .check_intercept = svm_check_intercept, |
Sean Christopherson | 95b5a48 | 2019-04-19 22:50:59 -0700 | [diff] [blame] | 7357 | .handle_exit_irqoff = svm_handle_exit_irqoff, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 7358 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 7359 | .request_immediate_exit = __kvm_request_immediate_exit, |
| 7360 | |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 7361 | .sched_in = svm_sched_in, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 7362 | |
| 7363 | .pmu_ops = &amd_pmu_ops, |
Suravee Suthikulpanit | 340d3bc | 2016-05-04 14:09:47 -0500 | [diff] [blame] | 7364 | .deliver_posted_interrupt = svm_deliver_avic_intr, |
Wanpeng Li | 17e433b | 2019-08-05 10:03:19 +0800 | [diff] [blame] | 7365 | .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt, |
Suravee Suthikulpanit | 411b44b | 2016-08-23 13:52:43 -0500 | [diff] [blame] | 7366 | .update_pi_irte = svm_update_pi_irte, |
Borislav Petkov | 74f1690 | 2017-03-26 23:51:24 +0200 | [diff] [blame] | 7367 | .setup_mce = svm_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7368 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 7369 | .smi_allowed = svm_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7370 | .pre_enter_smm = svm_pre_enter_smm, |
| 7371 | .pre_leave_smm = svm_pre_leave_smm, |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 7372 | .enable_smi_window = enable_smi_window, |
Brijesh Singh | 1654efc | 2017-12-04 10:57:34 -0600 | [diff] [blame] | 7373 | |
| 7374 | .mem_enc_op = svm_mem_enc_op, |
Brijesh Singh | 1e80fdc | 2017-12-04 10:57:38 -0600 | [diff] [blame] | 7375 | .mem_enc_reg_region = svm_register_enc_region, |
| 7376 | .mem_enc_unreg_region = svm_unregister_enc_region, |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 7377 | |
Vitaly Kuznetsov | 956e255 | 2019-08-28 09:59:04 +0200 | [diff] [blame] | 7378 | .nested_enable_evmcs = NULL, |
Vitaly Kuznetsov | ea15298 | 2019-08-27 18:04:02 +0200 | [diff] [blame] | 7379 | .nested_get_evmcs_version = NULL, |
Singh, Brijesh | 05d5a48 | 2019-02-15 17:24:12 +0000 | [diff] [blame] | 7380 | |
| 7381 | .need_emulation_on_page_fault = svm_need_emulation_on_page_fault, |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 7382 | |
| 7383 | .apic_init_signal_blocked = svm_apic_init_signal_blocked, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7384 | }; |
| 7385 | |
| 7386 | static int __init svm_init(void) |
| 7387 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 7388 | return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm), |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 7389 | __alignof__(struct vcpu_svm), THIS_MODULE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7390 | } |
| 7391 | |
| 7392 | static void __exit svm_exit(void) |
| 7393 | { |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 7394 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7395 | } |
| 7396 | |
| 7397 | module_init(svm_init) |
| 7398 | module_exit(svm_exit) |