David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Kernel-based Virtual Machine driver for Linux |
| 4 | * |
| 5 | * derived from drivers/kvm/kvm_main.c |
| 6 | * |
| 7 | * Copyright (C) 2006 Qumranet, Inc. |
| 8 | * Copyright (C) 2008 Qumranet, Inc. |
| 9 | * Copyright IBM Corporation, 2008 |
| 10 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
| 11 | * |
| 12 | * Authors: |
| 13 | * Avi Kivity <avi@qumranet.com> |
| 14 | * Yaniv Kamay <yaniv@qumranet.com> |
| 15 | * Amit Shah <amit.shah@qumranet.com> |
| 16 | * Ben-Ami Yassour <benami@il.ibm.com> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/kvm_host.h> |
| 20 | #include "irq.h" |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 21 | #include "ioapic.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 22 | #include "mmu.h" |
| 23 | #include "i8254.h" |
| 24 | #include "tss.h" |
| 25 | #include "kvm_cache_regs.h" |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 26 | #include "kvm_emulate.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 27 | #include "x86.h" |
| 28 | #include "cpuid.h" |
| 29 | #include "pmu.h" |
| 30 | #include "hyperv.h" |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 31 | #include "lapic.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 32 | |
| 33 | #include <linux/clocksource.h> |
| 34 | #include <linux/interrupt.h> |
| 35 | #include <linux/kvm.h> |
| 36 | #include <linux/fs.h> |
| 37 | #include <linux/vmalloc.h> |
| 38 | #include <linux/export.h> |
| 39 | #include <linux/moduleparam.h> |
| 40 | #include <linux/mman.h> |
| 41 | #include <linux/highmem.h> |
| 42 | #include <linux/iommu.h> |
| 43 | #include <linux/intel-iommu.h> |
| 44 | #include <linux/cpufreq.h> |
| 45 | #include <linux/user-return-notifier.h> |
| 46 | #include <linux/srcu.h> |
| 47 | #include <linux/slab.h> |
| 48 | #include <linux/perf_event.h> |
| 49 | #include <linux/uaccess.h> |
| 50 | #include <linux/hash.h> |
| 51 | #include <linux/pci.h> |
| 52 | #include <linux/timekeeper_internal.h> |
| 53 | #include <linux/pvclock_gtod.h> |
| 54 | #include <linux/kvm_irqfd.h> |
| 55 | #include <linux/irqbypass.h> |
| 56 | #include <linux/sched/stat.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 57 | #include <linux/sched/isolation.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 58 | #include <linux/mem_encrypt.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 59 | #include <linux/entry-kvm.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 60 | |
| 61 | #include <trace/events/kvm.h> |
| 62 | |
| 63 | #include <asm/debugreg.h> |
| 64 | #include <asm/msr.h> |
| 65 | #include <asm/desc.h> |
| 66 | #include <asm/mce.h> |
| 67 | #include <linux/kernel_stat.h> |
| 68 | #include <asm/fpu/internal.h> /* Ugh! */ |
| 69 | #include <asm/pvclock.h> |
| 70 | #include <asm/div64.h> |
| 71 | #include <asm/irq_remapping.h> |
| 72 | #include <asm/mshyperv.h> |
| 73 | #include <asm/hypervisor.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 74 | #include <asm/tlbflush.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 75 | #include <asm/intel_pt.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 76 | #include <asm/emulate_prefix.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 77 | #include <clocksource/hyperv_timer.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 78 | |
| 79 | #define CREATE_TRACE_POINTS |
| 80 | #include "trace.h" |
| 81 | |
| 82 | #define MAX_IO_MSRS 256 |
| 83 | #define KVM_MAX_MCE_BANKS 32 |
| 84 | u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P; |
| 85 | EXPORT_SYMBOL_GPL(kvm_mce_cap_supported); |
| 86 | |
| 87 | #define emul_to_vcpu(ctxt) \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 88 | ((struct kvm_vcpu *)(ctxt)->vcpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 89 | |
| 90 | /* EFER defaults: |
| 91 | * - enable syscall per default because its emulated by KVM |
| 92 | * - enable LME and LMA per default on 64 bit KVM |
| 93 | */ |
| 94 | #ifdef CONFIG_X86_64 |
| 95 | static |
| 96 | u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA)); |
| 97 | #else |
| 98 | static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE); |
| 99 | #endif |
| 100 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 101 | static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS; |
| 102 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 103 | #define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \ |
| 104 | KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) |
| 105 | |
| 106 | static void update_cr8_intercept(struct kvm_vcpu *vcpu); |
| 107 | static void process_nmi(struct kvm_vcpu *vcpu); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 108 | static void process_smi(struct kvm_vcpu *vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 109 | static void enter_smm(struct kvm_vcpu *vcpu); |
| 110 | static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags); |
| 111 | static void store_regs(struct kvm_vcpu *vcpu); |
| 112 | static int sync_regs(struct kvm_vcpu *vcpu); |
| 113 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 114 | struct kvm_x86_ops kvm_x86_ops __read_mostly; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 115 | EXPORT_SYMBOL_GPL(kvm_x86_ops); |
| 116 | |
| 117 | static bool __read_mostly ignore_msrs = 0; |
| 118 | module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR); |
| 119 | |
| 120 | static bool __read_mostly report_ignored_msrs = true; |
| 121 | module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR); |
| 122 | |
| 123 | unsigned int min_timer_period_us = 200; |
| 124 | module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR); |
| 125 | |
| 126 | static bool __read_mostly kvmclock_periodic_sync = true; |
| 127 | module_param(kvmclock_periodic_sync, bool, S_IRUGO); |
| 128 | |
| 129 | bool __read_mostly kvm_has_tsc_control; |
| 130 | EXPORT_SYMBOL_GPL(kvm_has_tsc_control); |
| 131 | u32 __read_mostly kvm_max_guest_tsc_khz; |
| 132 | EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz); |
| 133 | u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits; |
| 134 | EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits); |
| 135 | u64 __read_mostly kvm_max_tsc_scaling_ratio; |
| 136 | EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio); |
| 137 | u64 __read_mostly kvm_default_tsc_scaling_ratio; |
| 138 | EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio); |
| 139 | |
| 140 | /* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */ |
| 141 | static u32 __read_mostly tsc_tolerance_ppm = 250; |
| 142 | module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR); |
| 143 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 144 | /* |
| 145 | * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables |
| 146 | * adaptive tuning starting from default advancment of 1000ns. '0' disables |
| 147 | * advancement entirely. Any other value is used as-is and disables adaptive |
| 148 | * tuning, i.e. allows priveleged userspace to set an exact advancement time. |
| 149 | */ |
| 150 | static int __read_mostly lapic_timer_advance_ns = -1; |
| 151 | module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 152 | |
| 153 | static bool __read_mostly vector_hashing = true; |
| 154 | module_param(vector_hashing, bool, S_IRUGO); |
| 155 | |
| 156 | bool __read_mostly enable_vmware_backdoor = false; |
| 157 | module_param(enable_vmware_backdoor, bool, S_IRUGO); |
| 158 | EXPORT_SYMBOL_GPL(enable_vmware_backdoor); |
| 159 | |
| 160 | static bool __read_mostly force_emulation_prefix = false; |
| 161 | module_param(force_emulation_prefix, bool, S_IRUGO); |
| 162 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 163 | int __read_mostly pi_inject_timer = -1; |
| 164 | module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR); |
| 165 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 166 | /* |
| 167 | * Restoring the host value for MSRs that are only consumed when running in |
| 168 | * usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU |
| 169 | * returns to userspace, i.e. the kernel can run with the guest's value. |
| 170 | */ |
| 171 | #define KVM_MAX_NR_USER_RETURN_MSRS 16 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 172 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 173 | struct kvm_user_return_msrs_global { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 174 | int nr; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 175 | u32 msrs[KVM_MAX_NR_USER_RETURN_MSRS]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 178 | struct kvm_user_return_msrs { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 179 | struct user_return_notifier urn; |
| 180 | bool registered; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 181 | struct kvm_user_return_msr_values { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 182 | u64 host; |
| 183 | u64 curr; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 184 | } values[KVM_MAX_NR_USER_RETURN_MSRS]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 187 | static struct kvm_user_return_msrs_global __read_mostly user_return_msrs_global; |
| 188 | static struct kvm_user_return_msrs __percpu *user_return_msrs; |
| 189 | |
| 190 | #define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \ |
| 191 | | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \ |
| 192 | | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ |
| 193 | | XFEATURE_MASK_PKRU) |
| 194 | |
| 195 | u64 __read_mostly host_efer; |
| 196 | EXPORT_SYMBOL_GPL(host_efer); |
| 197 | |
| 198 | bool __read_mostly allow_smaller_maxphyaddr = 0; |
| 199 | EXPORT_SYMBOL_GPL(allow_smaller_maxphyaddr); |
| 200 | |
| 201 | static u64 __read_mostly host_xss; |
| 202 | u64 __read_mostly supported_xss; |
| 203 | EXPORT_SYMBOL_GPL(supported_xss); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 204 | |
| 205 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 206 | VCPU_STAT("pf_fixed", pf_fixed), |
| 207 | VCPU_STAT("pf_guest", pf_guest), |
| 208 | VCPU_STAT("tlb_flush", tlb_flush), |
| 209 | VCPU_STAT("invlpg", invlpg), |
| 210 | VCPU_STAT("exits", exits), |
| 211 | VCPU_STAT("io_exits", io_exits), |
| 212 | VCPU_STAT("mmio_exits", mmio_exits), |
| 213 | VCPU_STAT("signal_exits", signal_exits), |
| 214 | VCPU_STAT("irq_window", irq_window_exits), |
| 215 | VCPU_STAT("nmi_window", nmi_window_exits), |
| 216 | VCPU_STAT("halt_exits", halt_exits), |
| 217 | VCPU_STAT("halt_successful_poll", halt_successful_poll), |
| 218 | VCPU_STAT("halt_attempted_poll", halt_attempted_poll), |
| 219 | VCPU_STAT("halt_poll_invalid", halt_poll_invalid), |
| 220 | VCPU_STAT("halt_wakeup", halt_wakeup), |
| 221 | VCPU_STAT("hypercalls", hypercalls), |
| 222 | VCPU_STAT("request_irq", request_irq_exits), |
| 223 | VCPU_STAT("irq_exits", irq_exits), |
| 224 | VCPU_STAT("host_state_reload", host_state_reload), |
| 225 | VCPU_STAT("fpu_reload", fpu_reload), |
| 226 | VCPU_STAT("insn_emulation", insn_emulation), |
| 227 | VCPU_STAT("insn_emulation_fail", insn_emulation_fail), |
| 228 | VCPU_STAT("irq_injections", irq_injections), |
| 229 | VCPU_STAT("nmi_injections", nmi_injections), |
| 230 | VCPU_STAT("req_event", req_event), |
| 231 | VCPU_STAT("l1d_flush", l1d_flush), |
| 232 | VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns), |
| 233 | VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns), |
| 234 | VM_STAT("mmu_shadow_zapped", mmu_shadow_zapped), |
| 235 | VM_STAT("mmu_pte_write", mmu_pte_write), |
| 236 | VM_STAT("mmu_pde_zapped", mmu_pde_zapped), |
| 237 | VM_STAT("mmu_flooded", mmu_flooded), |
| 238 | VM_STAT("mmu_recycled", mmu_recycled), |
| 239 | VM_STAT("mmu_cache_miss", mmu_cache_miss), |
| 240 | VM_STAT("mmu_unsync", mmu_unsync), |
| 241 | VM_STAT("remote_tlb_flush", remote_tlb_flush), |
| 242 | VM_STAT("largepages", lpages, .mode = 0444), |
| 243 | VM_STAT("nx_largepages_splitted", nx_lpage_splits, .mode = 0444), |
| 244 | VM_STAT("max_mmu_page_hash_collisions", max_mmu_page_hash_collisions), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 245 | { NULL } |
| 246 | }; |
| 247 | |
| 248 | u64 __read_mostly host_xcr0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 249 | u64 __read_mostly supported_xcr0; |
| 250 | EXPORT_SYMBOL_GPL(supported_xcr0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 251 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 252 | static struct kmem_cache *x86_fpu_cache; |
| 253 | |
| 254 | static struct kmem_cache *x86_emulator_cache; |
| 255 | |
| 256 | /* |
| 257 | * When called, it means the previous get/set msr reached an invalid msr. |
| 258 | * Return true if we want to ignore/silent this failed msr access. |
| 259 | */ |
| 260 | static bool kvm_msr_ignored_check(struct kvm_vcpu *vcpu, u32 msr, |
| 261 | u64 data, bool write) |
| 262 | { |
| 263 | const char *op = write ? "wrmsr" : "rdmsr"; |
| 264 | |
| 265 | if (ignore_msrs) { |
| 266 | if (report_ignored_msrs) |
| 267 | kvm_pr_unimpl("ignored %s: 0x%x data 0x%llx\n", |
| 268 | op, msr, data); |
| 269 | /* Mask the error */ |
| 270 | return true; |
| 271 | } else { |
| 272 | kvm_debug_ratelimited("unhandled %s: 0x%x data 0x%llx\n", |
| 273 | op, msr, data); |
| 274 | return false; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | static struct kmem_cache *kvm_alloc_emulator_cache(void) |
| 279 | { |
| 280 | unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src); |
| 281 | unsigned int size = sizeof(struct x86_emulate_ctxt); |
| 282 | |
| 283 | return kmem_cache_create_usercopy("x86_emulator", size, |
| 284 | __alignof__(struct x86_emulate_ctxt), |
| 285 | SLAB_ACCOUNT, useroffset, |
| 286 | size - useroffset, NULL); |
| 287 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 288 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 289 | static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt); |
| 290 | |
| 291 | static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu) |
| 292 | { |
| 293 | int i; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 294 | for (i = 0; i < ASYNC_PF_PER_VCPU; i++) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 295 | vcpu->arch.apf.gfns[i] = ~0; |
| 296 | } |
| 297 | |
| 298 | static void kvm_on_user_return(struct user_return_notifier *urn) |
| 299 | { |
| 300 | unsigned slot; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 301 | struct kvm_user_return_msrs *msrs |
| 302 | = container_of(urn, struct kvm_user_return_msrs, urn); |
| 303 | struct kvm_user_return_msr_values *values; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 304 | unsigned long flags; |
| 305 | |
| 306 | /* |
| 307 | * Disabling irqs at this point since the following code could be |
| 308 | * interrupted and executed through kvm_arch_hardware_disable() |
| 309 | */ |
| 310 | local_irq_save(flags); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 311 | if (msrs->registered) { |
| 312 | msrs->registered = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 313 | user_return_notifier_unregister(urn); |
| 314 | } |
| 315 | local_irq_restore(flags); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 316 | for (slot = 0; slot < user_return_msrs_global.nr; ++slot) { |
| 317 | values = &msrs->values[slot]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 318 | if (values->host != values->curr) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 319 | wrmsrl(user_return_msrs_global.msrs[slot], values->host); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 320 | values->curr = values->host; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 325 | int kvm_probe_user_return_msr(u32 msr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 326 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 327 | u64 val; |
| 328 | int ret; |
| 329 | |
| 330 | preempt_disable(); |
| 331 | ret = rdmsrl_safe(msr, &val); |
| 332 | if (ret) |
| 333 | goto out; |
| 334 | ret = wrmsrl_safe(msr, val); |
| 335 | out: |
| 336 | preempt_enable(); |
| 337 | return ret; |
| 338 | } |
| 339 | EXPORT_SYMBOL_GPL(kvm_probe_user_return_msr); |
| 340 | |
| 341 | void kvm_define_user_return_msr(unsigned slot, u32 msr) |
| 342 | { |
| 343 | BUG_ON(slot >= KVM_MAX_NR_USER_RETURN_MSRS); |
| 344 | user_return_msrs_global.msrs[slot] = msr; |
| 345 | if (slot >= user_return_msrs_global.nr) |
| 346 | user_return_msrs_global.nr = slot + 1; |
| 347 | } |
| 348 | EXPORT_SYMBOL_GPL(kvm_define_user_return_msr); |
| 349 | |
| 350 | static void kvm_user_return_msr_cpu_online(void) |
| 351 | { |
| 352 | unsigned int cpu = smp_processor_id(); |
| 353 | struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 354 | u64 value; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 355 | int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 356 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 357 | for (i = 0; i < user_return_msrs_global.nr; ++i) { |
| 358 | rdmsrl_safe(user_return_msrs_global.msrs[i], &value); |
| 359 | msrs->values[i].host = value; |
| 360 | msrs->values[i].curr = value; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 361 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 364 | int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 365 | { |
| 366 | unsigned int cpu = smp_processor_id(); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 367 | struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 368 | int err; |
| 369 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 370 | value = (value & mask) | (msrs->values[slot].host & ~mask); |
| 371 | if (value == msrs->values[slot].curr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 372 | return 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 373 | err = wrmsrl_safe(user_return_msrs_global.msrs[slot], value); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 374 | if (err) |
| 375 | return 1; |
| 376 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 377 | msrs->values[slot].curr = value; |
| 378 | if (!msrs->registered) { |
| 379 | msrs->urn.on_user_return = kvm_on_user_return; |
| 380 | user_return_notifier_register(&msrs->urn); |
| 381 | msrs->registered = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 382 | } |
| 383 | return 0; |
| 384 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 385 | EXPORT_SYMBOL_GPL(kvm_set_user_return_msr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 386 | |
| 387 | static void drop_user_return_notifiers(void) |
| 388 | { |
| 389 | unsigned int cpu = smp_processor_id(); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 390 | struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 391 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 392 | if (msrs->registered) |
| 393 | kvm_on_user_return(&msrs->urn); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
| 397 | { |
| 398 | return vcpu->arch.apic_base; |
| 399 | } |
| 400 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); |
| 401 | |
| 402 | enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu) |
| 403 | { |
| 404 | return kvm_apic_mode(kvm_get_apic_base(vcpu)); |
| 405 | } |
| 406 | EXPORT_SYMBOL_GPL(kvm_get_apic_mode); |
| 407 | |
| 408 | int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
| 409 | { |
| 410 | enum lapic_mode old_mode = kvm_get_apic_mode(vcpu); |
| 411 | enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); |
| 412 | u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff | |
| 413 | (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE); |
| 414 | |
| 415 | if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) |
| 416 | return 1; |
| 417 | if (!msr_info->host_initiated) { |
| 418 | if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC) |
| 419 | return 1; |
| 420 | if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC) |
| 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | kvm_lapic_set_base(vcpu, msr_info->data); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 425 | kvm_recalculate_apic_map(vcpu->kvm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 426 | return 0; |
| 427 | } |
| 428 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); |
| 429 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 430 | asmlinkage __visible noinstr void kvm_spurious_fault(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 431 | { |
| 432 | /* Fault while not rebooting. We want the trace. */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 433 | BUG_ON(!kvm_rebooting); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 434 | } |
| 435 | EXPORT_SYMBOL_GPL(kvm_spurious_fault); |
| 436 | |
| 437 | #define EXCPT_BENIGN 0 |
| 438 | #define EXCPT_CONTRIBUTORY 1 |
| 439 | #define EXCPT_PF 2 |
| 440 | |
| 441 | static int exception_class(int vector) |
| 442 | { |
| 443 | switch (vector) { |
| 444 | case PF_VECTOR: |
| 445 | return EXCPT_PF; |
| 446 | case DE_VECTOR: |
| 447 | case TS_VECTOR: |
| 448 | case NP_VECTOR: |
| 449 | case SS_VECTOR: |
| 450 | case GP_VECTOR: |
| 451 | return EXCPT_CONTRIBUTORY; |
| 452 | default: |
| 453 | break; |
| 454 | } |
| 455 | return EXCPT_BENIGN; |
| 456 | } |
| 457 | |
| 458 | #define EXCPT_FAULT 0 |
| 459 | #define EXCPT_TRAP 1 |
| 460 | #define EXCPT_ABORT 2 |
| 461 | #define EXCPT_INTERRUPT 3 |
| 462 | |
| 463 | static int exception_type(int vector) |
| 464 | { |
| 465 | unsigned int mask; |
| 466 | |
| 467 | if (WARN_ON(vector > 31 || vector == NMI_VECTOR)) |
| 468 | return EXCPT_INTERRUPT; |
| 469 | |
| 470 | mask = 1 << vector; |
| 471 | |
| 472 | /* #DB is trap, as instruction watchpoints are handled elsewhere */ |
| 473 | if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR))) |
| 474 | return EXCPT_TRAP; |
| 475 | |
| 476 | if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR))) |
| 477 | return EXCPT_ABORT; |
| 478 | |
| 479 | /* Reserved exceptions will result in fault */ |
| 480 | return EXCPT_FAULT; |
| 481 | } |
| 482 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 483 | void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu) |
| 484 | { |
| 485 | unsigned nr = vcpu->arch.exception.nr; |
| 486 | bool has_payload = vcpu->arch.exception.has_payload; |
| 487 | unsigned long payload = vcpu->arch.exception.payload; |
| 488 | |
| 489 | if (!has_payload) |
| 490 | return; |
| 491 | |
| 492 | switch (nr) { |
| 493 | case DB_VECTOR: |
| 494 | /* |
| 495 | * "Certain debug exceptions may clear bit 0-3. The |
| 496 | * remaining contents of the DR6 register are never |
| 497 | * cleared by the processor". |
| 498 | */ |
| 499 | vcpu->arch.dr6 &= ~DR_TRAP_BITS; |
| 500 | /* |
| 501 | * DR6.RTM is set by all #DB exceptions that don't clear it. |
| 502 | */ |
| 503 | vcpu->arch.dr6 |= DR6_RTM; |
| 504 | vcpu->arch.dr6 |= payload; |
| 505 | /* |
| 506 | * Bit 16 should be set in the payload whenever the #DB |
| 507 | * exception should clear DR6.RTM. This makes the payload |
| 508 | * compatible with the pending debug exceptions under VMX. |
| 509 | * Though not currently documented in the SDM, this also |
| 510 | * makes the payload compatible with the exit qualification |
| 511 | * for #DB exceptions under VMX. |
| 512 | */ |
| 513 | vcpu->arch.dr6 ^= payload & DR6_RTM; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 514 | |
| 515 | /* |
| 516 | * The #DB payload is defined as compatible with the 'pending |
| 517 | * debug exceptions' field under VMX, not DR6. While bit 12 is |
| 518 | * defined in the 'pending debug exceptions' field (enabled |
| 519 | * breakpoint), it is reserved and must be zero in DR6. |
| 520 | */ |
| 521 | vcpu->arch.dr6 &= ~BIT(12); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 522 | break; |
| 523 | case PF_VECTOR: |
| 524 | vcpu->arch.cr2 = payload; |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | vcpu->arch.exception.has_payload = false; |
| 529 | vcpu->arch.exception.payload = 0; |
| 530 | } |
| 531 | EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload); |
| 532 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 533 | static void kvm_multiple_exception(struct kvm_vcpu *vcpu, |
| 534 | unsigned nr, bool has_error, u32 error_code, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 535 | bool has_payload, unsigned long payload, bool reinject) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 536 | { |
| 537 | u32 prev_nr; |
| 538 | int class1, class2; |
| 539 | |
| 540 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 541 | |
| 542 | if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) { |
| 543 | queue: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 544 | if (reinject) { |
| 545 | /* |
| 546 | * On vmentry, vcpu->arch.exception.pending is only |
| 547 | * true if an event injection was blocked by |
| 548 | * nested_run_pending. In that case, however, |
| 549 | * vcpu_enter_guest requests an immediate exit, |
| 550 | * and the guest shouldn't proceed far enough to |
| 551 | * need reinjection. |
| 552 | */ |
| 553 | WARN_ON_ONCE(vcpu->arch.exception.pending); |
| 554 | vcpu->arch.exception.injected = true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 555 | if (WARN_ON_ONCE(has_payload)) { |
| 556 | /* |
| 557 | * A reinjected event has already |
| 558 | * delivered its payload. |
| 559 | */ |
| 560 | has_payload = false; |
| 561 | payload = 0; |
| 562 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 563 | } else { |
| 564 | vcpu->arch.exception.pending = true; |
| 565 | vcpu->arch.exception.injected = false; |
| 566 | } |
| 567 | vcpu->arch.exception.has_error_code = has_error; |
| 568 | vcpu->arch.exception.nr = nr; |
| 569 | vcpu->arch.exception.error_code = error_code; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 570 | vcpu->arch.exception.has_payload = has_payload; |
| 571 | vcpu->arch.exception.payload = payload; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 572 | if (!is_guest_mode(vcpu)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 573 | kvm_deliver_exception_payload(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
| 577 | /* to check exception */ |
| 578 | prev_nr = vcpu->arch.exception.nr; |
| 579 | if (prev_nr == DF_VECTOR) { |
| 580 | /* triple fault -> shutdown */ |
| 581 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 582 | return; |
| 583 | } |
| 584 | class1 = exception_class(prev_nr); |
| 585 | class2 = exception_class(nr); |
| 586 | if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) |
| 587 | || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { |
| 588 | /* |
| 589 | * Generate double fault per SDM Table 5-5. Set |
| 590 | * exception.pending = true so that the double fault |
| 591 | * can trigger a nested vmexit. |
| 592 | */ |
| 593 | vcpu->arch.exception.pending = true; |
| 594 | vcpu->arch.exception.injected = false; |
| 595 | vcpu->arch.exception.has_error_code = true; |
| 596 | vcpu->arch.exception.nr = DF_VECTOR; |
| 597 | vcpu->arch.exception.error_code = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 598 | vcpu->arch.exception.has_payload = false; |
| 599 | vcpu->arch.exception.payload = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 600 | } else |
| 601 | /* replace previous exception with a new one in a hope |
| 602 | that instruction re-execution will regenerate lost |
| 603 | exception */ |
| 604 | goto queue; |
| 605 | } |
| 606 | |
| 607 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
| 608 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 609 | kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 610 | } |
| 611 | EXPORT_SYMBOL_GPL(kvm_queue_exception); |
| 612 | |
| 613 | void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
| 614 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 615 | kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 616 | } |
| 617 | EXPORT_SYMBOL_GPL(kvm_requeue_exception); |
| 618 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 619 | void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, |
| 620 | unsigned long payload) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 621 | { |
| 622 | kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false); |
| 623 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 624 | EXPORT_SYMBOL_GPL(kvm_queue_exception_p); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 625 | |
| 626 | static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, |
| 627 | u32 error_code, unsigned long payload) |
| 628 | { |
| 629 | kvm_multiple_exception(vcpu, nr, true, error_code, |
| 630 | true, payload, false); |
| 631 | } |
| 632 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 633 | int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err) |
| 634 | { |
| 635 | if (err) |
| 636 | kvm_inject_gp(vcpu, 0); |
| 637 | else |
| 638 | return kvm_skip_emulated_instruction(vcpu); |
| 639 | |
| 640 | return 1; |
| 641 | } |
| 642 | EXPORT_SYMBOL_GPL(kvm_complete_insn_gp); |
| 643 | |
| 644 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault) |
| 645 | { |
| 646 | ++vcpu->stat.pf_guest; |
| 647 | vcpu->arch.exception.nested_apf = |
| 648 | is_guest_mode(vcpu) && fault->async_page_fault; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 649 | if (vcpu->arch.exception.nested_apf) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 650 | vcpu->arch.apf.nested_apf_token = fault->address; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 651 | kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code); |
| 652 | } else { |
| 653 | kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code, |
| 654 | fault->address); |
| 655 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 656 | } |
| 657 | EXPORT_SYMBOL_GPL(kvm_inject_page_fault); |
| 658 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 659 | bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu, |
| 660 | struct x86_exception *fault) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 661 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 662 | struct kvm_mmu *fault_mmu; |
| 663 | WARN_ON_ONCE(fault->vector != PF_VECTOR); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 664 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 665 | fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu : |
| 666 | vcpu->arch.walk_mmu; |
| 667 | |
| 668 | /* |
| 669 | * Invalidate the TLB entry for the faulting address, if it exists, |
| 670 | * else the access will fault indefinitely (and to emulate hardware). |
| 671 | */ |
| 672 | if ((fault->error_code & PFERR_PRESENT_MASK) && |
| 673 | !(fault->error_code & PFERR_RSVD_MASK)) |
| 674 | kvm_mmu_invalidate_gva(vcpu, fault_mmu, fault->address, |
| 675 | fault_mmu->root_hpa); |
| 676 | |
| 677 | fault_mmu->inject_page_fault(vcpu, fault); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 678 | return fault->nested_page_fault; |
| 679 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 680 | EXPORT_SYMBOL_GPL(kvm_inject_emulated_page_fault); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 681 | |
| 682 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
| 683 | { |
| 684 | atomic_inc(&vcpu->arch.nmi_queued); |
| 685 | kvm_make_request(KVM_REQ_NMI, vcpu); |
| 686 | } |
| 687 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); |
| 688 | |
| 689 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
| 690 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 691 | kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 692 | } |
| 693 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); |
| 694 | |
| 695 | void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
| 696 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 697 | kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 698 | } |
| 699 | EXPORT_SYMBOL_GPL(kvm_requeue_exception_e); |
| 700 | |
| 701 | /* |
| 702 | * Checks if cpl <= required_cpl; if true, return true. Otherwise queue |
| 703 | * a #GP and return false. |
| 704 | */ |
| 705 | bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl) |
| 706 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 707 | if (kvm_x86_ops.get_cpl(vcpu) <= required_cpl) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 708 | return true; |
| 709 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 710 | return false; |
| 711 | } |
| 712 | EXPORT_SYMBOL_GPL(kvm_require_cpl); |
| 713 | |
| 714 | bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr) |
| 715 | { |
| 716 | if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE)) |
| 717 | return true; |
| 718 | |
| 719 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 720 | return false; |
| 721 | } |
| 722 | EXPORT_SYMBOL_GPL(kvm_require_dr); |
| 723 | |
| 724 | /* |
| 725 | * This function will be used to read from the physical memory of the currently |
| 726 | * running guest. The difference to kvm_vcpu_read_guest_page is that this function |
| 727 | * can read from guest physical or from the guest's guest physical memory. |
| 728 | */ |
| 729 | int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, |
| 730 | gfn_t ngfn, void *data, int offset, int len, |
| 731 | u32 access) |
| 732 | { |
| 733 | struct x86_exception exception; |
| 734 | gfn_t real_gfn; |
| 735 | gpa_t ngpa; |
| 736 | |
| 737 | ngpa = gfn_to_gpa(ngfn); |
| 738 | real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception); |
| 739 | if (real_gfn == UNMAPPED_GVA) |
| 740 | return -EFAULT; |
| 741 | |
| 742 | real_gfn = gpa_to_gfn(real_gfn); |
| 743 | |
| 744 | return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len); |
| 745 | } |
| 746 | EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu); |
| 747 | |
| 748 | static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, |
| 749 | void *data, int offset, int len, u32 access) |
| 750 | { |
| 751 | return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn, |
| 752 | data, offset, len, access); |
| 753 | } |
| 754 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 755 | static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu) |
| 756 | { |
| 757 | return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) | |
| 758 | rsvd_bits(1, 2); |
| 759 | } |
| 760 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 761 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 762 | * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 763 | */ |
| 764 | int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3) |
| 765 | { |
| 766 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; |
| 767 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; |
| 768 | int i; |
| 769 | int ret; |
| 770 | u64 pdpte[ARRAY_SIZE(mmu->pdptrs)]; |
| 771 | |
| 772 | ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte, |
| 773 | offset * sizeof(u64), sizeof(pdpte), |
| 774 | PFERR_USER_MASK|PFERR_WRITE_MASK); |
| 775 | if (ret < 0) { |
| 776 | ret = 0; |
| 777 | goto out; |
| 778 | } |
| 779 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { |
| 780 | if ((pdpte[i] & PT_PRESENT_MASK) && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 781 | (pdpte[i] & pdptr_rsvd_bits(vcpu))) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 782 | ret = 0; |
| 783 | goto out; |
| 784 | } |
| 785 | } |
| 786 | ret = 1; |
| 787 | |
| 788 | memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 789 | kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); |
| 790 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 791 | out: |
| 792 | |
| 793 | return ret; |
| 794 | } |
| 795 | EXPORT_SYMBOL_GPL(load_pdptrs); |
| 796 | |
| 797 | bool pdptrs_changed(struct kvm_vcpu *vcpu) |
| 798 | { |
| 799 | u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 800 | int offset; |
| 801 | gfn_t gfn; |
| 802 | int r; |
| 803 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 804 | if (!is_pae_paging(vcpu)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 805 | return false; |
| 806 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 807 | if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 808 | return true; |
| 809 | |
| 810 | gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT; |
| 811 | offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1); |
| 812 | r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte), |
| 813 | PFERR_USER_MASK | PFERR_WRITE_MASK); |
| 814 | if (r < 0) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 815 | return true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 816 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 817 | return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 818 | } |
| 819 | EXPORT_SYMBOL_GPL(pdptrs_changed); |
| 820 | |
| 821 | int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 822 | { |
| 823 | unsigned long old_cr0 = kvm_read_cr0(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 824 | unsigned long pdptr_bits = X86_CR0_CD | X86_CR0_NW | X86_CR0_PG; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 825 | unsigned long update_bits = X86_CR0_PG | X86_CR0_WP; |
| 826 | |
| 827 | cr0 |= X86_CR0_ET; |
| 828 | |
| 829 | #ifdef CONFIG_X86_64 |
| 830 | if (cr0 & 0xffffffff00000000UL) |
| 831 | return 1; |
| 832 | #endif |
| 833 | |
| 834 | cr0 &= ~CR0_RESERVED_BITS; |
| 835 | |
| 836 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) |
| 837 | return 1; |
| 838 | |
| 839 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) |
| 840 | return 1; |
| 841 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 842 | #ifdef CONFIG_X86_64 |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 843 | if ((vcpu->arch.efer & EFER_LME) && !is_paging(vcpu) && |
| 844 | (cr0 & X86_CR0_PG)) { |
| 845 | int cs_db, cs_l; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 846 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 847 | if (!is_pae(vcpu)) |
| 848 | return 1; |
| 849 | kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
| 850 | if (cs_l) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 851 | return 1; |
| 852 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 853 | #endif |
| 854 | if (!(vcpu->arch.efer & EFER_LME) && (cr0 & X86_CR0_PG) && |
| 855 | is_pae(vcpu) && ((cr0 ^ old_cr0) & pdptr_bits) && |
| 856 | !load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu))) |
| 857 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 858 | |
| 859 | if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE)) |
| 860 | return 1; |
| 861 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 862 | kvm_x86_ops.set_cr0(vcpu, cr0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 863 | |
| 864 | if ((cr0 ^ old_cr0) & X86_CR0_PG) { |
| 865 | kvm_clear_async_pf_completion_queue(vcpu); |
| 866 | kvm_async_pf_hash_reset(vcpu); |
| 867 | } |
| 868 | |
| 869 | if ((cr0 ^ old_cr0) & update_bits) |
| 870 | kvm_mmu_reset_context(vcpu); |
| 871 | |
| 872 | if (((cr0 ^ old_cr0) & X86_CR0_CD) && |
| 873 | kvm_arch_has_noncoherent_dma(vcpu->kvm) && |
| 874 | !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
| 875 | kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL); |
| 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
| 880 | |
| 881 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
| 882 | { |
| 883 | (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f)); |
| 884 | } |
| 885 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
| 886 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 887 | void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 888 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 889 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) { |
| 890 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 891 | if (vcpu->arch.xcr0 != host_xcr0) |
| 892 | xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 893 | |
| 894 | if (vcpu->arch.xsaves_enabled && |
| 895 | vcpu->arch.ia32_xss != host_xss) |
| 896 | wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 897 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 898 | |
| 899 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 900 | (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || |
| 901 | (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU)) && |
| 902 | vcpu->arch.pkru != vcpu->arch.host_pkru) |
| 903 | __write_pkru(vcpu->arch.pkru); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 904 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 905 | EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 906 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 907 | void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 908 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 909 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 910 | (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || |
| 911 | (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU))) { |
| 912 | vcpu->arch.pkru = rdpkru(); |
| 913 | if (vcpu->arch.pkru != vcpu->arch.host_pkru) |
| 914 | __write_pkru(vcpu->arch.host_pkru); |
| 915 | } |
| 916 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 917 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) { |
| 918 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 919 | if (vcpu->arch.xcr0 != host_xcr0) |
| 920 | xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 921 | |
| 922 | if (vcpu->arch.xsaves_enabled && |
| 923 | vcpu->arch.ia32_xss != host_xss) |
| 924 | wrmsrl(MSR_IA32_XSS, host_xss); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 925 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 926 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 927 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 928 | EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 929 | |
| 930 | static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) |
| 931 | { |
| 932 | u64 xcr0 = xcr; |
| 933 | u64 old_xcr0 = vcpu->arch.xcr0; |
| 934 | u64 valid_bits; |
| 935 | |
| 936 | /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */ |
| 937 | if (index != XCR_XFEATURE_ENABLED_MASK) |
| 938 | return 1; |
| 939 | if (!(xcr0 & XFEATURE_MASK_FP)) |
| 940 | return 1; |
| 941 | if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE)) |
| 942 | return 1; |
| 943 | |
| 944 | /* |
| 945 | * Do not allow the guest to set bits that we do not support |
| 946 | * saving. However, xcr0 bit 0 is always set, even if the |
| 947 | * emulated CPU does not support XSAVE (see fx_init). |
| 948 | */ |
| 949 | valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP; |
| 950 | if (xcr0 & ~valid_bits) |
| 951 | return 1; |
| 952 | |
| 953 | if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) != |
| 954 | (!(xcr0 & XFEATURE_MASK_BNDCSR))) |
| 955 | return 1; |
| 956 | |
| 957 | if (xcr0 & XFEATURE_MASK_AVX512) { |
| 958 | if (!(xcr0 & XFEATURE_MASK_YMM)) |
| 959 | return 1; |
| 960 | if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512) |
| 961 | return 1; |
| 962 | } |
| 963 | vcpu->arch.xcr0 = xcr0; |
| 964 | |
| 965 | if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 966 | kvm_update_cpuid_runtime(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) |
| 971 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 972 | if (kvm_x86_ops.get_cpl(vcpu) != 0 || |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 973 | __kvm_set_xcr(vcpu, index, xcr)) { |
| 974 | kvm_inject_gp(vcpu, 0); |
| 975 | return 1; |
| 976 | } |
| 977 | return 0; |
| 978 | } |
| 979 | EXPORT_SYMBOL_GPL(kvm_set_xcr); |
| 980 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 981 | int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 982 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 983 | if (cr4 & cr4_reserved_bits) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 984 | return -EINVAL; |
| 985 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 986 | if (cr4 & vcpu->arch.cr4_guest_rsvd_bits) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 987 | return -EINVAL; |
| 988 | |
| 989 | return 0; |
| 990 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 991 | EXPORT_SYMBOL_GPL(kvm_valid_cr4); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 992 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 993 | int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 994 | { |
| 995 | unsigned long old_cr4 = kvm_read_cr4(vcpu); |
| 996 | unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 997 | X86_CR4_SMEP; |
| 998 | unsigned long mmu_role_bits = pdptr_bits | X86_CR4_SMAP | X86_CR4_PKE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 999 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1000 | if (kvm_valid_cr4(vcpu, cr4)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1001 | return 1; |
| 1002 | |
| 1003 | if (is_long_mode(vcpu)) { |
| 1004 | if (!(cr4 & X86_CR4_PAE)) |
| 1005 | return 1; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1006 | if ((cr4 ^ old_cr4) & X86_CR4_LA57) |
| 1007 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1008 | } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE) |
| 1009 | && ((cr4 ^ old_cr4) & pdptr_bits) |
| 1010 | && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, |
| 1011 | kvm_read_cr3(vcpu))) |
| 1012 | return 1; |
| 1013 | |
| 1014 | if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) { |
| 1015 | if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID)) |
| 1016 | return 1; |
| 1017 | |
| 1018 | /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */ |
| 1019 | if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu)) |
| 1020 | return 1; |
| 1021 | } |
| 1022 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1023 | if (kvm_x86_ops.set_cr4(vcpu, cr4)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1024 | return 1; |
| 1025 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1026 | if (((cr4 ^ old_cr4) & mmu_role_bits) || |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1027 | (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE))) |
| 1028 | kvm_mmu_reset_context(vcpu); |
| 1029 | |
| 1030 | if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1031 | kvm_update_cpuid_runtime(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1032 | |
| 1033 | return 0; |
| 1034 | } |
| 1035 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
| 1036 | |
| 1037 | int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 1038 | { |
| 1039 | bool skip_tlb_flush = false; |
| 1040 | #ifdef CONFIG_X86_64 |
| 1041 | bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
| 1042 | |
| 1043 | if (pcid_enabled) { |
| 1044 | skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH; |
| 1045 | cr3 &= ~X86_CR3_PCID_NOFLUSH; |
| 1046 | } |
| 1047 | #endif |
| 1048 | |
| 1049 | if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) { |
| 1050 | if (!skip_tlb_flush) { |
| 1051 | kvm_mmu_sync_roots(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1052 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1053 | } |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | if (is_long_mode(vcpu) && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1058 | (cr3 & vcpu->arch.cr3_lm_rsvd_bits)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1059 | return 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1060 | else if (is_pae_paging(vcpu) && |
| 1061 | !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1062 | return 1; |
| 1063 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1064 | kvm_mmu_new_pgd(vcpu, cr3, skip_tlb_flush, skip_tlb_flush); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1065 | vcpu->arch.cr3 = cr3; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1066 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
| 1071 | |
| 1072 | int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
| 1073 | { |
| 1074 | if (cr8 & CR8_RESERVED_BITS) |
| 1075 | return 1; |
| 1076 | if (lapic_in_kernel(vcpu)) |
| 1077 | kvm_lapic_set_tpr(vcpu, cr8); |
| 1078 | else |
| 1079 | vcpu->arch.cr8 = cr8; |
| 1080 | return 0; |
| 1081 | } |
| 1082 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
| 1083 | |
| 1084 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
| 1085 | { |
| 1086 | if (lapic_in_kernel(vcpu)) |
| 1087 | return kvm_lapic_get_cr8(vcpu); |
| 1088 | else |
| 1089 | return vcpu->arch.cr8; |
| 1090 | } |
| 1091 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
| 1092 | |
| 1093 | static void kvm_update_dr0123(struct kvm_vcpu *vcpu) |
| 1094 | { |
| 1095 | int i; |
| 1096 | |
| 1097 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { |
| 1098 | for (i = 0; i < KVM_NR_DB_REGS; i++) |
| 1099 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; |
| 1100 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD; |
| 1101 | } |
| 1102 | } |
| 1103 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1104 | void kvm_update_dr7(struct kvm_vcpu *vcpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1105 | { |
| 1106 | unsigned long dr7; |
| 1107 | |
| 1108 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 1109 | dr7 = vcpu->arch.guest_debug_dr7; |
| 1110 | else |
| 1111 | dr7 = vcpu->arch.dr7; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1112 | kvm_x86_ops.set_dr7(vcpu, dr7); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1113 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; |
| 1114 | if (dr7 & DR7_BP_EN_MASK) |
| 1115 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; |
| 1116 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1117 | EXPORT_SYMBOL_GPL(kvm_update_dr7); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1118 | |
| 1119 | static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu) |
| 1120 | { |
| 1121 | u64 fixed = DR6_FIXED_1; |
| 1122 | |
| 1123 | if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM)) |
| 1124 | fixed |= DR6_RTM; |
| 1125 | return fixed; |
| 1126 | } |
| 1127 | |
| 1128 | static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) |
| 1129 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1130 | size_t size = ARRAY_SIZE(vcpu->arch.db); |
| 1131 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1132 | switch (dr) { |
| 1133 | case 0 ... 3: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1134 | vcpu->arch.db[array_index_nospec(dr, size)] = val; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1135 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) |
| 1136 | vcpu->arch.eff_db[dr] = val; |
| 1137 | break; |
| 1138 | case 4: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1139 | case 6: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1140 | if (!kvm_dr6_valid(val)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1141 | return -1; /* #GP */ |
| 1142 | vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1143 | break; |
| 1144 | case 5: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1145 | default: /* 7 */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1146 | if (!kvm_dr7_valid(val)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1147 | return -1; /* #GP */ |
| 1148 | vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; |
| 1149 | kvm_update_dr7(vcpu); |
| 1150 | break; |
| 1151 | } |
| 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) |
| 1157 | { |
| 1158 | if (__kvm_set_dr(vcpu, dr, val)) { |
| 1159 | kvm_inject_gp(vcpu, 0); |
| 1160 | return 1; |
| 1161 | } |
| 1162 | return 0; |
| 1163 | } |
| 1164 | EXPORT_SYMBOL_GPL(kvm_set_dr); |
| 1165 | |
| 1166 | int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val) |
| 1167 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1168 | size_t size = ARRAY_SIZE(vcpu->arch.db); |
| 1169 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1170 | switch (dr) { |
| 1171 | case 0 ... 3: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1172 | *val = vcpu->arch.db[array_index_nospec(dr, size)]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1173 | break; |
| 1174 | case 4: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1175 | case 6: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1176 | *val = vcpu->arch.dr6; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1177 | break; |
| 1178 | case 5: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1179 | default: /* 7 */ |
| 1180 | *val = vcpu->arch.dr7; |
| 1181 | break; |
| 1182 | } |
| 1183 | return 0; |
| 1184 | } |
| 1185 | EXPORT_SYMBOL_GPL(kvm_get_dr); |
| 1186 | |
| 1187 | bool kvm_rdpmc(struct kvm_vcpu *vcpu) |
| 1188 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1189 | u32 ecx = kvm_rcx_read(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1190 | u64 data; |
| 1191 | int err; |
| 1192 | |
| 1193 | err = kvm_pmu_rdpmc(vcpu, ecx, &data); |
| 1194 | if (err) |
| 1195 | return err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1196 | kvm_rax_write(vcpu, (u32)data); |
| 1197 | kvm_rdx_write(vcpu, data >> 32); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1198 | return err; |
| 1199 | } |
| 1200 | EXPORT_SYMBOL_GPL(kvm_rdpmc); |
| 1201 | |
| 1202 | /* |
| 1203 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS |
| 1204 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. |
| 1205 | * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1206 | * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features) |
| 1207 | * extract the supported MSRs from the related const lists. |
| 1208 | * msrs_to_save is selected from the msrs_to_save_all to reflect the |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1209 | * capabilities of the host cpu. This capabilities test skips MSRs that are |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1210 | * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1211 | * may depend on host virtualization features rather than host cpu features. |
| 1212 | */ |
| 1213 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1214 | static const u32 msrs_to_save_all[] = { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1215 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
| 1216 | MSR_STAR, |
| 1217 | #ifdef CONFIG_X86_64 |
| 1218 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, |
| 1219 | #endif |
| 1220 | MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1221 | MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1222 | MSR_IA32_SPEC_CTRL, |
| 1223 | MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH, |
| 1224 | MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK, |
| 1225 | MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B, |
| 1226 | MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B, |
| 1227 | MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B, |
| 1228 | MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B, |
| 1229 | MSR_IA32_UMWAIT_CONTROL, |
| 1230 | |
| 1231 | MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1232 | MSR_ARCH_PERFMON_FIXED_CTR0 + 2, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1233 | MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS, |
| 1234 | MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL, |
| 1235 | MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1, |
| 1236 | MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3, |
| 1237 | MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5, |
| 1238 | MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7, |
| 1239 | MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9, |
| 1240 | MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11, |
| 1241 | MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13, |
| 1242 | MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15, |
| 1243 | MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17, |
| 1244 | MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1, |
| 1245 | MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3, |
| 1246 | MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5, |
| 1247 | MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7, |
| 1248 | MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9, |
| 1249 | MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11, |
| 1250 | MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13, |
| 1251 | MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15, |
| 1252 | MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1253 | |
| 1254 | MSR_K7_EVNTSEL0, MSR_K7_EVNTSEL1, MSR_K7_EVNTSEL2, MSR_K7_EVNTSEL3, |
| 1255 | MSR_K7_PERFCTR0, MSR_K7_PERFCTR1, MSR_K7_PERFCTR2, MSR_K7_PERFCTR3, |
| 1256 | MSR_F15H_PERF_CTL0, MSR_F15H_PERF_CTL1, MSR_F15H_PERF_CTL2, |
| 1257 | MSR_F15H_PERF_CTL3, MSR_F15H_PERF_CTL4, MSR_F15H_PERF_CTL5, |
| 1258 | MSR_F15H_PERF_CTR0, MSR_F15H_PERF_CTR1, MSR_F15H_PERF_CTR2, |
| 1259 | MSR_F15H_PERF_CTR3, MSR_F15H_PERF_CTR4, MSR_F15H_PERF_CTR5, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1260 | }; |
| 1261 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1262 | static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1263 | static unsigned num_msrs_to_save; |
| 1264 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1265 | static const u32 emulated_msrs_all[] = { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1266 | MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
| 1267 | MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW, |
| 1268 | HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL, |
| 1269 | HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC, |
| 1270 | HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY, |
| 1271 | HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2, |
| 1272 | HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL, |
| 1273 | HV_X64_MSR_RESET, |
| 1274 | HV_X64_MSR_VP_INDEX, |
| 1275 | HV_X64_MSR_VP_RUNTIME, |
| 1276 | HV_X64_MSR_SCONTROL, |
| 1277 | HV_X64_MSR_STIMER0_CONFIG, |
| 1278 | HV_X64_MSR_VP_ASSIST_PAGE, |
| 1279 | HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL, |
| 1280 | HV_X64_MSR_TSC_EMULATION_STATUS, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1281 | HV_X64_MSR_SYNDBG_OPTIONS, |
| 1282 | HV_X64_MSR_SYNDBG_CONTROL, HV_X64_MSR_SYNDBG_STATUS, |
| 1283 | HV_X64_MSR_SYNDBG_SEND_BUFFER, HV_X64_MSR_SYNDBG_RECV_BUFFER, |
| 1284 | HV_X64_MSR_SYNDBG_PENDING_BUFFER, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1285 | |
| 1286 | MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1287 | MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1288 | |
| 1289 | MSR_IA32_TSC_ADJUST, |
| 1290 | MSR_IA32_TSCDEADLINE, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1291 | MSR_IA32_ARCH_CAPABILITIES, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1292 | MSR_IA32_PERF_CAPABILITIES, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1293 | MSR_IA32_MISC_ENABLE, |
| 1294 | MSR_IA32_MCG_STATUS, |
| 1295 | MSR_IA32_MCG_CTL, |
| 1296 | MSR_IA32_MCG_EXT_CTL, |
| 1297 | MSR_IA32_SMBASE, |
| 1298 | MSR_SMI_COUNT, |
| 1299 | MSR_PLATFORM_INFO, |
| 1300 | MSR_MISC_FEATURES_ENABLES, |
| 1301 | MSR_AMD64_VIRT_SPEC_CTRL, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1302 | MSR_IA32_POWER_CTL, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1303 | MSR_IA32_UCODE_REV, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1304 | |
| 1305 | /* |
| 1306 | * The following list leaves out MSRs whose values are determined |
| 1307 | * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs. |
| 1308 | * We always support the "true" VMX control MSRs, even if the host |
| 1309 | * processor does not, so I am putting these registers here rather |
| 1310 | * than in msrs_to_save_all. |
| 1311 | */ |
| 1312 | MSR_IA32_VMX_BASIC, |
| 1313 | MSR_IA32_VMX_TRUE_PINBASED_CTLS, |
| 1314 | MSR_IA32_VMX_TRUE_PROCBASED_CTLS, |
| 1315 | MSR_IA32_VMX_TRUE_EXIT_CTLS, |
| 1316 | MSR_IA32_VMX_TRUE_ENTRY_CTLS, |
| 1317 | MSR_IA32_VMX_MISC, |
| 1318 | MSR_IA32_VMX_CR0_FIXED0, |
| 1319 | MSR_IA32_VMX_CR4_FIXED0, |
| 1320 | MSR_IA32_VMX_VMCS_ENUM, |
| 1321 | MSR_IA32_VMX_PROCBASED_CTLS2, |
| 1322 | MSR_IA32_VMX_EPT_VPID_CAP, |
| 1323 | MSR_IA32_VMX_VMFUNC, |
| 1324 | |
| 1325 | MSR_K7_HWCR, |
| 1326 | MSR_KVM_POLL_CONTROL, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1327 | }; |
| 1328 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1329 | static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1330 | static unsigned num_emulated_msrs; |
| 1331 | |
| 1332 | /* |
| 1333 | * List of msr numbers which are used to expose MSR-based features that |
| 1334 | * can be used by a hypervisor to validate requested CPU features. |
| 1335 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1336 | static const u32 msr_based_features_all[] = { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1337 | MSR_IA32_VMX_BASIC, |
| 1338 | MSR_IA32_VMX_TRUE_PINBASED_CTLS, |
| 1339 | MSR_IA32_VMX_PINBASED_CTLS, |
| 1340 | MSR_IA32_VMX_TRUE_PROCBASED_CTLS, |
| 1341 | MSR_IA32_VMX_PROCBASED_CTLS, |
| 1342 | MSR_IA32_VMX_TRUE_EXIT_CTLS, |
| 1343 | MSR_IA32_VMX_EXIT_CTLS, |
| 1344 | MSR_IA32_VMX_TRUE_ENTRY_CTLS, |
| 1345 | MSR_IA32_VMX_ENTRY_CTLS, |
| 1346 | MSR_IA32_VMX_MISC, |
| 1347 | MSR_IA32_VMX_CR0_FIXED0, |
| 1348 | MSR_IA32_VMX_CR0_FIXED1, |
| 1349 | MSR_IA32_VMX_CR4_FIXED0, |
| 1350 | MSR_IA32_VMX_CR4_FIXED1, |
| 1351 | MSR_IA32_VMX_VMCS_ENUM, |
| 1352 | MSR_IA32_VMX_PROCBASED_CTLS2, |
| 1353 | MSR_IA32_VMX_EPT_VPID_CAP, |
| 1354 | MSR_IA32_VMX_VMFUNC, |
| 1355 | |
| 1356 | MSR_F10H_DECFG, |
| 1357 | MSR_IA32_UCODE_REV, |
| 1358 | MSR_IA32_ARCH_CAPABILITIES, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1359 | MSR_IA32_PERF_CAPABILITIES, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1360 | }; |
| 1361 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1362 | static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1363 | static unsigned int num_msr_based_features; |
| 1364 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1365 | static u64 kvm_get_arch_capabilities(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1366 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1367 | u64 data = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1368 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1369 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
| 1370 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data); |
| 1371 | |
| 1372 | /* |
| 1373 | * If nx_huge_pages is enabled, KVM's shadow paging will ensure that |
| 1374 | * the nested hypervisor runs with NX huge pages. If it is not, |
| 1375 | * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other |
| 1376 | * L1 guests, so it need not worry about its own (L2) guests. |
| 1377 | */ |
| 1378 | data |= ARCH_CAP_PSCHANGE_MC_NO; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1379 | |
| 1380 | /* |
| 1381 | * If we're doing cache flushes (either "always" or "cond") |
| 1382 | * we will do one whenever the guest does a vmlaunch/vmresume. |
| 1383 | * If an outer hypervisor is doing the cache flush for us |
| 1384 | * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that |
| 1385 | * capability to the guest too, and if EPT is disabled we're not |
| 1386 | * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will |
| 1387 | * require a nested hypervisor to do a flush of its own. |
| 1388 | */ |
| 1389 | if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER) |
| 1390 | data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH; |
| 1391 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1392 | if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN)) |
| 1393 | data |= ARCH_CAP_RDCL_NO; |
| 1394 | if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) |
| 1395 | data |= ARCH_CAP_SSB_NO; |
| 1396 | if (!boot_cpu_has_bug(X86_BUG_MDS)) |
| 1397 | data |= ARCH_CAP_MDS_NO; |
| 1398 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1399 | if (!boot_cpu_has(X86_FEATURE_RTM)) { |
| 1400 | /* |
| 1401 | * If RTM=0 because the kernel has disabled TSX, the host might |
| 1402 | * have TAA_NO or TSX_CTRL. Clear TAA_NO (the guest sees RTM=0 |
| 1403 | * and therefore knows that there cannot be TAA) but keep |
| 1404 | * TSX_CTRL: some buggy userspaces leave it set on tsx=on hosts, |
| 1405 | * and we want to allow migrating those guests to tsx=off hosts. |
| 1406 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1407 | data &= ~ARCH_CAP_TAA_NO; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1408 | } else if (!boot_cpu_has_bug(X86_BUG_TAA)) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1409 | data |= ARCH_CAP_TAA_NO; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1410 | } else { |
| 1411 | /* |
| 1412 | * Nothing to do here; we emulate TSX_CTRL if present on the |
| 1413 | * host so the guest can choose between disabling TSX or |
| 1414 | * using VERW to clear CPU buffers. |
| 1415 | */ |
| 1416 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1417 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1418 | return data; |
| 1419 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1420 | |
| 1421 | static int kvm_get_msr_feature(struct kvm_msr_entry *msr) |
| 1422 | { |
| 1423 | switch (msr->index) { |
| 1424 | case MSR_IA32_ARCH_CAPABILITIES: |
| 1425 | msr->data = kvm_get_arch_capabilities(); |
| 1426 | break; |
| 1427 | case MSR_IA32_UCODE_REV: |
| 1428 | rdmsrl_safe(msr->index, &msr->data); |
| 1429 | break; |
| 1430 | default: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1431 | return kvm_x86_ops.get_msr_feature(msr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1432 | } |
| 1433 | return 0; |
| 1434 | } |
| 1435 | |
| 1436 | static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 1437 | { |
| 1438 | struct kvm_msr_entry msr; |
| 1439 | int r; |
| 1440 | |
| 1441 | msr.index = index; |
| 1442 | r = kvm_get_msr_feature(&msr); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1443 | |
| 1444 | if (r == KVM_MSR_RET_INVALID) { |
| 1445 | /* Unconditionally clear the output for simplicity */ |
| 1446 | *data = 0; |
| 1447 | if (kvm_msr_ignored_check(vcpu, index, 0, false)) |
| 1448 | r = 0; |
| 1449 | } |
| 1450 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1451 | if (r) |
| 1452 | return r; |
| 1453 | |
| 1454 | *data = msr.data; |
| 1455 | |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1459 | static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 1460 | { |
| 1461 | if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT)) |
| 1462 | return false; |
| 1463 | |
| 1464 | if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM)) |
| 1465 | return false; |
| 1466 | |
| 1467 | if (efer & (EFER_LME | EFER_LMA) && |
| 1468 | !guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
| 1469 | return false; |
| 1470 | |
| 1471 | if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX)) |
| 1472 | return false; |
| 1473 | |
| 1474 | return true; |
| 1475 | |
| 1476 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1477 | bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 1478 | { |
| 1479 | if (efer & efer_reserved_bits) |
| 1480 | return false; |
| 1481 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1482 | return __kvm_valid_efer(vcpu, efer); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1483 | } |
| 1484 | EXPORT_SYMBOL_GPL(kvm_valid_efer); |
| 1485 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1486 | static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1487 | { |
| 1488 | u64 old_efer = vcpu->arch.efer; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1489 | u64 efer = msr_info->data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1490 | int r; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1491 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1492 | if (efer & efer_reserved_bits) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1493 | return 1; |
| 1494 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1495 | if (!msr_info->host_initiated) { |
| 1496 | if (!__kvm_valid_efer(vcpu, efer)) |
| 1497 | return 1; |
| 1498 | |
| 1499 | if (is_paging(vcpu) && |
| 1500 | (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) |
| 1501 | return 1; |
| 1502 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1503 | |
| 1504 | efer &= ~EFER_LMA; |
| 1505 | efer |= vcpu->arch.efer & EFER_LMA; |
| 1506 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1507 | r = kvm_x86_ops.set_efer(vcpu, efer); |
| 1508 | if (r) { |
| 1509 | WARN_ON(r > 0); |
| 1510 | return r; |
| 1511 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1512 | |
| 1513 | /* Update reserved bits */ |
| 1514 | if ((efer ^ old_efer) & EFER_NX) |
| 1515 | kvm_mmu_reset_context(vcpu); |
| 1516 | |
| 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | void kvm_enable_efer_bits(u64 mask) |
| 1521 | { |
| 1522 | efer_reserved_bits &= ~mask; |
| 1523 | } |
| 1524 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); |
| 1525 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1526 | bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type) |
| 1527 | { |
| 1528 | struct kvm_x86_msr_filter *msr_filter; |
| 1529 | struct msr_bitmap_range *ranges; |
| 1530 | struct kvm *kvm = vcpu->kvm; |
| 1531 | bool allowed; |
| 1532 | int idx; |
| 1533 | u32 i; |
| 1534 | |
| 1535 | /* x2APIC MSRs do not support filtering. */ |
| 1536 | if (index >= 0x800 && index <= 0x8ff) |
| 1537 | return true; |
| 1538 | |
| 1539 | idx = srcu_read_lock(&kvm->srcu); |
| 1540 | |
| 1541 | msr_filter = srcu_dereference(kvm->arch.msr_filter, &kvm->srcu); |
| 1542 | if (!msr_filter) { |
| 1543 | allowed = true; |
| 1544 | goto out; |
| 1545 | } |
| 1546 | |
| 1547 | allowed = msr_filter->default_allow; |
| 1548 | ranges = msr_filter->ranges; |
| 1549 | |
| 1550 | for (i = 0; i < msr_filter->count; i++) { |
| 1551 | u32 start = ranges[i].base; |
| 1552 | u32 end = start + ranges[i].nmsrs; |
| 1553 | u32 flags = ranges[i].flags; |
| 1554 | unsigned long *bitmap = ranges[i].bitmap; |
| 1555 | |
| 1556 | if ((index >= start) && (index < end) && (flags & type)) { |
| 1557 | allowed = !!test_bit(index - start, bitmap); |
| 1558 | break; |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | out: |
| 1563 | srcu_read_unlock(&kvm->srcu, idx); |
| 1564 | |
| 1565 | return allowed; |
| 1566 | } |
| 1567 | EXPORT_SYMBOL_GPL(kvm_msr_allowed); |
| 1568 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1569 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1570 | * Write @data into the MSR specified by @index. Select MSR specific fault |
| 1571 | * checks are bypassed if @host_initiated is %true. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1572 | * Returns 0 on success, non-0 otherwise. |
| 1573 | * Assumes vcpu_load() was already called. |
| 1574 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1575 | static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data, |
| 1576 | bool host_initiated) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1577 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1578 | struct msr_data msr; |
| 1579 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1580 | if (!host_initiated && !kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_WRITE)) |
| 1581 | return KVM_MSR_RET_FILTERED; |
| 1582 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1583 | switch (index) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1584 | case MSR_FS_BASE: |
| 1585 | case MSR_GS_BASE: |
| 1586 | case MSR_KERNEL_GS_BASE: |
| 1587 | case MSR_CSTAR: |
| 1588 | case MSR_LSTAR: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1589 | if (is_noncanonical_address(data, vcpu)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1590 | return 1; |
| 1591 | break; |
| 1592 | case MSR_IA32_SYSENTER_EIP: |
| 1593 | case MSR_IA32_SYSENTER_ESP: |
| 1594 | /* |
| 1595 | * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if |
| 1596 | * non-canonical address is written on Intel but not on |
| 1597 | * AMD (which ignores the top 32-bits, because it does |
| 1598 | * not implement 64-bit SYSENTER). |
| 1599 | * |
| 1600 | * 64-bit code should hence be able to write a non-canonical |
| 1601 | * value on AMD. Making the address canonical ensures that |
| 1602 | * vmentry does not fail on Intel after writing a non-canonical |
| 1603 | * value, and that something deterministic happens if the guest |
| 1604 | * invokes 64-bit SYSENTER. |
| 1605 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1606 | data = get_canonical(data, vcpu_virt_addr_bits(vcpu)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1607 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1608 | |
| 1609 | msr.data = data; |
| 1610 | msr.index = index; |
| 1611 | msr.host_initiated = host_initiated; |
| 1612 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1613 | return kvm_x86_ops.set_msr(vcpu, &msr); |
| 1614 | } |
| 1615 | |
| 1616 | static int kvm_set_msr_ignored_check(struct kvm_vcpu *vcpu, |
| 1617 | u32 index, u64 data, bool host_initiated) |
| 1618 | { |
| 1619 | int ret = __kvm_set_msr(vcpu, index, data, host_initiated); |
| 1620 | |
| 1621 | if (ret == KVM_MSR_RET_INVALID) |
| 1622 | if (kvm_msr_ignored_check(vcpu, index, data, true)) |
| 1623 | ret = 0; |
| 1624 | |
| 1625 | return ret; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | /* |
| 1629 | * Read the MSR specified by @index into @data. Select MSR specific fault |
| 1630 | * checks are bypassed if @host_initiated is %true. |
| 1631 | * Returns 0 on success, non-0 otherwise. |
| 1632 | * Assumes vcpu_load() was already called. |
| 1633 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1634 | int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, |
| 1635 | bool host_initiated) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1636 | { |
| 1637 | struct msr_data msr; |
| 1638 | int ret; |
| 1639 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1640 | if (!host_initiated && !kvm_msr_allowed(vcpu, index, KVM_MSR_FILTER_READ)) |
| 1641 | return KVM_MSR_RET_FILTERED; |
| 1642 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1643 | msr.index = index; |
| 1644 | msr.host_initiated = host_initiated; |
| 1645 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1646 | ret = kvm_x86_ops.get_msr(vcpu, &msr); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1647 | if (!ret) |
| 1648 | *data = msr.data; |
| 1649 | return ret; |
| 1650 | } |
| 1651 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1652 | static int kvm_get_msr_ignored_check(struct kvm_vcpu *vcpu, |
| 1653 | u32 index, u64 *data, bool host_initiated) |
| 1654 | { |
| 1655 | int ret = __kvm_get_msr(vcpu, index, data, host_initiated); |
| 1656 | |
| 1657 | if (ret == KVM_MSR_RET_INVALID) { |
| 1658 | /* Unconditionally clear *data for simplicity */ |
| 1659 | *data = 0; |
| 1660 | if (kvm_msr_ignored_check(vcpu, index, 0, false)) |
| 1661 | ret = 0; |
| 1662 | } |
| 1663 | |
| 1664 | return ret; |
| 1665 | } |
| 1666 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1667 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data) |
| 1668 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1669 | return kvm_get_msr_ignored_check(vcpu, index, data, false); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1670 | } |
| 1671 | EXPORT_SYMBOL_GPL(kvm_get_msr); |
| 1672 | |
| 1673 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data) |
| 1674 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1675 | return kvm_set_msr_ignored_check(vcpu, index, data, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1676 | } |
| 1677 | EXPORT_SYMBOL_GPL(kvm_set_msr); |
| 1678 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1679 | static int complete_emulated_msr(struct kvm_vcpu *vcpu, bool is_read) |
| 1680 | { |
| 1681 | if (vcpu->run->msr.error) { |
| 1682 | kvm_inject_gp(vcpu, 0); |
| 1683 | return 1; |
| 1684 | } else if (is_read) { |
| 1685 | kvm_rax_write(vcpu, (u32)vcpu->run->msr.data); |
| 1686 | kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32); |
| 1687 | } |
| 1688 | |
| 1689 | return kvm_skip_emulated_instruction(vcpu); |
| 1690 | } |
| 1691 | |
| 1692 | static int complete_emulated_rdmsr(struct kvm_vcpu *vcpu) |
| 1693 | { |
| 1694 | return complete_emulated_msr(vcpu, true); |
| 1695 | } |
| 1696 | |
| 1697 | static int complete_emulated_wrmsr(struct kvm_vcpu *vcpu) |
| 1698 | { |
| 1699 | return complete_emulated_msr(vcpu, false); |
| 1700 | } |
| 1701 | |
| 1702 | static u64 kvm_msr_reason(int r) |
| 1703 | { |
| 1704 | switch (r) { |
| 1705 | case KVM_MSR_RET_INVALID: |
| 1706 | return KVM_MSR_EXIT_REASON_UNKNOWN; |
| 1707 | case KVM_MSR_RET_FILTERED: |
| 1708 | return KVM_MSR_EXIT_REASON_FILTER; |
| 1709 | default: |
| 1710 | return KVM_MSR_EXIT_REASON_INVAL; |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | static int kvm_msr_user_space(struct kvm_vcpu *vcpu, u32 index, |
| 1715 | u32 exit_reason, u64 data, |
| 1716 | int (*completion)(struct kvm_vcpu *vcpu), |
| 1717 | int r) |
| 1718 | { |
| 1719 | u64 msr_reason = kvm_msr_reason(r); |
| 1720 | |
| 1721 | /* Check if the user wanted to know about this MSR fault */ |
| 1722 | if (!(vcpu->kvm->arch.user_space_msr_mask & msr_reason)) |
| 1723 | return 0; |
| 1724 | |
| 1725 | vcpu->run->exit_reason = exit_reason; |
| 1726 | vcpu->run->msr.error = 0; |
| 1727 | memset(vcpu->run->msr.pad, 0, sizeof(vcpu->run->msr.pad)); |
| 1728 | vcpu->run->msr.reason = msr_reason; |
| 1729 | vcpu->run->msr.index = index; |
| 1730 | vcpu->run->msr.data = data; |
| 1731 | vcpu->arch.complete_userspace_io = completion; |
| 1732 | |
| 1733 | return 1; |
| 1734 | } |
| 1735 | |
| 1736 | static int kvm_get_msr_user_space(struct kvm_vcpu *vcpu, u32 index, int r) |
| 1737 | { |
| 1738 | return kvm_msr_user_space(vcpu, index, KVM_EXIT_X86_RDMSR, 0, |
| 1739 | complete_emulated_rdmsr, r); |
| 1740 | } |
| 1741 | |
| 1742 | static int kvm_set_msr_user_space(struct kvm_vcpu *vcpu, u32 index, u64 data, int r) |
| 1743 | { |
| 1744 | return kvm_msr_user_space(vcpu, index, KVM_EXIT_X86_WRMSR, data, |
| 1745 | complete_emulated_wrmsr, r); |
| 1746 | } |
| 1747 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1748 | int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu) |
| 1749 | { |
| 1750 | u32 ecx = kvm_rcx_read(vcpu); |
| 1751 | u64 data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1752 | int r; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1753 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1754 | r = kvm_get_msr(vcpu, ecx, &data); |
| 1755 | |
| 1756 | /* MSR read failed? See if we should ask user space */ |
| 1757 | if (r && kvm_get_msr_user_space(vcpu, ecx, r)) { |
| 1758 | /* Bounce to user space */ |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
| 1762 | /* MSR read failed? Inject a #GP */ |
| 1763 | if (r) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1764 | trace_kvm_msr_read_ex(ecx); |
| 1765 | kvm_inject_gp(vcpu, 0); |
| 1766 | return 1; |
| 1767 | } |
| 1768 | |
| 1769 | trace_kvm_msr_read(ecx, data); |
| 1770 | |
| 1771 | kvm_rax_write(vcpu, data & -1u); |
| 1772 | kvm_rdx_write(vcpu, (data >> 32) & -1u); |
| 1773 | return kvm_skip_emulated_instruction(vcpu); |
| 1774 | } |
| 1775 | EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr); |
| 1776 | |
| 1777 | int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu) |
| 1778 | { |
| 1779 | u32 ecx = kvm_rcx_read(vcpu); |
| 1780 | u64 data = kvm_read_edx_eax(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1781 | int r; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1782 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1783 | r = kvm_set_msr(vcpu, ecx, data); |
| 1784 | |
| 1785 | /* MSR write failed? See if we should ask user space */ |
| 1786 | if (r && kvm_set_msr_user_space(vcpu, ecx, data, r)) |
| 1787 | /* Bounce to user space */ |
| 1788 | return 0; |
| 1789 | |
| 1790 | /* Signal all other negative errors to userspace */ |
| 1791 | if (r < 0) |
| 1792 | return r; |
| 1793 | |
| 1794 | /* MSR write failed? Inject a #GP */ |
| 1795 | if (r > 0) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1796 | trace_kvm_msr_write_ex(ecx, data); |
| 1797 | kvm_inject_gp(vcpu, 0); |
| 1798 | return 1; |
| 1799 | } |
| 1800 | |
| 1801 | trace_kvm_msr_write(ecx, data); |
| 1802 | return kvm_skip_emulated_instruction(vcpu); |
| 1803 | } |
| 1804 | EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr); |
| 1805 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1806 | bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu) |
| 1807 | { |
| 1808 | return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) || |
| 1809 | xfer_to_guest_mode_work_pending(); |
| 1810 | } |
| 1811 | EXPORT_SYMBOL_GPL(kvm_vcpu_exit_request); |
| 1812 | |
| 1813 | /* |
| 1814 | * The fast path for frequent and performance sensitive wrmsr emulation, |
| 1815 | * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces |
| 1816 | * the latency of virtual IPI by avoiding the expensive bits of transitioning |
| 1817 | * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the |
| 1818 | * other cases which must be called after interrupts are enabled on the host. |
| 1819 | */ |
| 1820 | static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data) |
| 1821 | { |
| 1822 | if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic)) |
| 1823 | return 1; |
| 1824 | |
| 1825 | if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) && |
| 1826 | ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) && |
| 1827 | ((data & APIC_MODE_MASK) == APIC_DM_FIXED) && |
| 1828 | ((u32)(data >> 32) != X2APIC_BROADCAST)) { |
| 1829 | |
| 1830 | data &= ~(1 << 12); |
| 1831 | kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32)); |
| 1832 | kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32)); |
| 1833 | kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data); |
| 1834 | trace_kvm_apic_write(APIC_ICR, (u32)data); |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
| 1838 | return 1; |
| 1839 | } |
| 1840 | |
| 1841 | static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data) |
| 1842 | { |
| 1843 | if (!kvm_can_use_hv_timer(vcpu)) |
| 1844 | return 1; |
| 1845 | |
| 1846 | kvm_set_lapic_tscdeadline_msr(vcpu, data); |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
| 1850 | fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu) |
| 1851 | { |
| 1852 | u32 msr = kvm_rcx_read(vcpu); |
| 1853 | u64 data; |
| 1854 | fastpath_t ret = EXIT_FASTPATH_NONE; |
| 1855 | |
| 1856 | switch (msr) { |
| 1857 | case APIC_BASE_MSR + (APIC_ICR >> 4): |
| 1858 | data = kvm_read_edx_eax(vcpu); |
| 1859 | if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) { |
| 1860 | kvm_skip_emulated_instruction(vcpu); |
| 1861 | ret = EXIT_FASTPATH_EXIT_HANDLED; |
| 1862 | } |
| 1863 | break; |
| 1864 | case MSR_IA32_TSCDEADLINE: |
| 1865 | data = kvm_read_edx_eax(vcpu); |
| 1866 | if (!handle_fastpath_set_tscdeadline(vcpu, data)) { |
| 1867 | kvm_skip_emulated_instruction(vcpu); |
| 1868 | ret = EXIT_FASTPATH_REENTER_GUEST; |
| 1869 | } |
| 1870 | break; |
| 1871 | default: |
| 1872 | break; |
| 1873 | } |
| 1874 | |
| 1875 | if (ret != EXIT_FASTPATH_NONE) |
| 1876 | trace_kvm_msr_write(msr, data); |
| 1877 | |
| 1878 | return ret; |
| 1879 | } |
| 1880 | EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff); |
| 1881 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1882 | /* |
| 1883 | * Adapt set_msr() to msr_io()'s calling convention |
| 1884 | */ |
| 1885 | static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 1886 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1887 | return kvm_get_msr_ignored_check(vcpu, index, data, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
| 1891 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1892 | return kvm_set_msr_ignored_check(vcpu, index, *data, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | #ifdef CONFIG_X86_64 |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1896 | struct pvclock_clock { |
| 1897 | int vclock_mode; |
| 1898 | u64 cycle_last; |
| 1899 | u64 mask; |
| 1900 | u32 mult; |
| 1901 | u32 shift; |
| 1902 | u64 base_cycles; |
| 1903 | u64 offset; |
| 1904 | }; |
| 1905 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1906 | struct pvclock_gtod_data { |
| 1907 | seqcount_t seq; |
| 1908 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1909 | struct pvclock_clock clock; /* extract of a clocksource struct */ |
| 1910 | struct pvclock_clock raw_clock; /* extract of a clocksource struct */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1911 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1912 | ktime_t offs_boot; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1913 | u64 wall_time_sec; |
| 1914 | }; |
| 1915 | |
| 1916 | static struct pvclock_gtod_data pvclock_gtod_data; |
| 1917 | |
| 1918 | static void update_pvclock_gtod(struct timekeeper *tk) |
| 1919 | { |
| 1920 | struct pvclock_gtod_data *vdata = &pvclock_gtod_data; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1921 | |
| 1922 | write_seqcount_begin(&vdata->seq); |
| 1923 | |
| 1924 | /* copy pvclock gtod data */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1925 | vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1926 | vdata->clock.cycle_last = tk->tkr_mono.cycle_last; |
| 1927 | vdata->clock.mask = tk->tkr_mono.mask; |
| 1928 | vdata->clock.mult = tk->tkr_mono.mult; |
| 1929 | vdata->clock.shift = tk->tkr_mono.shift; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1930 | vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; |
| 1931 | vdata->clock.offset = tk->tkr_mono.base; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1932 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1933 | vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode; |
| 1934 | vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; |
| 1935 | vdata->raw_clock.mask = tk->tkr_raw.mask; |
| 1936 | vdata->raw_clock.mult = tk->tkr_raw.mult; |
| 1937 | vdata->raw_clock.shift = tk->tkr_raw.shift; |
| 1938 | vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; |
| 1939 | vdata->raw_clock.offset = tk->tkr_raw.base; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1940 | |
| 1941 | vdata->wall_time_sec = tk->xtime_sec; |
| 1942 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1943 | vdata->offs_boot = tk->offs_boot; |
| 1944 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1945 | write_seqcount_end(&vdata->seq); |
| 1946 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1947 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1948 | static s64 get_kvmclock_base_ns(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1949 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1950 | /* Count up from boot time, but with the frequency of the raw clock. */ |
| 1951 | return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1952 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1953 | #else |
| 1954 | static s64 get_kvmclock_base_ns(void) |
| 1955 | { |
| 1956 | /* Master clock not used, so we can just use CLOCK_BOOTTIME. */ |
| 1957 | return ktime_get_boottime_ns(); |
| 1958 | } |
| 1959 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1960 | |
| 1961 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) |
| 1962 | { |
| 1963 | int version; |
| 1964 | int r; |
| 1965 | struct pvclock_wall_clock wc; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1966 | u64 wall_nsec; |
| 1967 | |
| 1968 | kvm->arch.wall_clock = wall_clock; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1969 | |
| 1970 | if (!wall_clock) |
| 1971 | return; |
| 1972 | |
| 1973 | r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version)); |
| 1974 | if (r) |
| 1975 | return; |
| 1976 | |
| 1977 | if (version & 1) |
| 1978 | ++version; /* first time write, random junk */ |
| 1979 | |
| 1980 | ++version; |
| 1981 | |
| 1982 | if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version))) |
| 1983 | return; |
| 1984 | |
| 1985 | /* |
| 1986 | * The guest calculates current wall clock time by adding |
| 1987 | * system time (updated by kvm_guest_time_update below) to the |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1988 | * wall clock specified here. We do the reverse here. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1989 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1990 | wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1991 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1992 | wc.nsec = do_div(wall_nsec, 1000000000); |
| 1993 | wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1994 | wc.version = version; |
| 1995 | |
| 1996 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); |
| 1997 | |
| 1998 | version++; |
| 1999 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); |
| 2000 | } |
| 2001 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2002 | static void kvm_write_system_time(struct kvm_vcpu *vcpu, gpa_t system_time, |
| 2003 | bool old_msr, bool host_initiated) |
| 2004 | { |
| 2005 | struct kvm_arch *ka = &vcpu->kvm->arch; |
| 2006 | |
| 2007 | if (vcpu->vcpu_id == 0 && !host_initiated) { |
| 2008 | if (ka->boot_vcpu_runs_old_kvmclock != old_msr) |
| 2009 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
| 2010 | |
| 2011 | ka->boot_vcpu_runs_old_kvmclock = old_msr; |
| 2012 | } |
| 2013 | |
| 2014 | vcpu->arch.time = system_time; |
| 2015 | kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); |
| 2016 | |
| 2017 | /* we verify if the enable bit is set... */ |
| 2018 | vcpu->arch.pv_time_enabled = false; |
| 2019 | if (!(system_time & 1)) |
| 2020 | return; |
| 2021 | |
| 2022 | if (!kvm_gfn_to_hva_cache_init(vcpu->kvm, |
| 2023 | &vcpu->arch.pv_time, system_time & ~1ULL, |
| 2024 | sizeof(struct pvclock_vcpu_time_info))) |
| 2025 | vcpu->arch.pv_time_enabled = true; |
| 2026 | |
| 2027 | return; |
| 2028 | } |
| 2029 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2030 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
| 2031 | { |
| 2032 | do_shl32_div32(dividend, divisor); |
| 2033 | return dividend; |
| 2034 | } |
| 2035 | |
| 2036 | static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz, |
| 2037 | s8 *pshift, u32 *pmultiplier) |
| 2038 | { |
| 2039 | uint64_t scaled64; |
| 2040 | int32_t shift = 0; |
| 2041 | uint64_t tps64; |
| 2042 | uint32_t tps32; |
| 2043 | |
| 2044 | tps64 = base_hz; |
| 2045 | scaled64 = scaled_hz; |
| 2046 | while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) { |
| 2047 | tps64 >>= 1; |
| 2048 | shift--; |
| 2049 | } |
| 2050 | |
| 2051 | tps32 = (uint32_t)tps64; |
| 2052 | while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) { |
| 2053 | if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000) |
| 2054 | scaled64 >>= 1; |
| 2055 | else |
| 2056 | tps32 <<= 1; |
| 2057 | shift++; |
| 2058 | } |
| 2059 | |
| 2060 | *pshift = shift; |
| 2061 | *pmultiplier = div_frac(scaled64, tps32); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | #ifdef CONFIG_X86_64 |
| 2065 | static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0); |
| 2066 | #endif |
| 2067 | |
| 2068 | static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); |
| 2069 | static unsigned long max_tsc_khz; |
| 2070 | |
| 2071 | static u32 adjust_tsc_khz(u32 khz, s32 ppm) |
| 2072 | { |
| 2073 | u64 v = (u64)khz * (1000000 + ppm); |
| 2074 | do_div(v, 1000000); |
| 2075 | return v; |
| 2076 | } |
| 2077 | |
| 2078 | static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale) |
| 2079 | { |
| 2080 | u64 ratio; |
| 2081 | |
| 2082 | /* Guest TSC same frequency as host TSC? */ |
| 2083 | if (!scale) { |
| 2084 | vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; |
| 2085 | return 0; |
| 2086 | } |
| 2087 | |
| 2088 | /* TSC scaling supported? */ |
| 2089 | if (!kvm_has_tsc_control) { |
| 2090 | if (user_tsc_khz > tsc_khz) { |
| 2091 | vcpu->arch.tsc_catchup = 1; |
| 2092 | vcpu->arch.tsc_always_catchup = 1; |
| 2093 | return 0; |
| 2094 | } else { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2095 | pr_warn_ratelimited("user requested TSC rate below hardware speed\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2096 | return -1; |
| 2097 | } |
| 2098 | } |
| 2099 | |
| 2100 | /* TSC scaling required - calculate ratio */ |
| 2101 | ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits, |
| 2102 | user_tsc_khz, tsc_khz); |
| 2103 | |
| 2104 | if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2105 | pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", |
| 2106 | user_tsc_khz); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2107 | return -1; |
| 2108 | } |
| 2109 | |
| 2110 | vcpu->arch.tsc_scaling_ratio = ratio; |
| 2111 | return 0; |
| 2112 | } |
| 2113 | |
| 2114 | static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz) |
| 2115 | { |
| 2116 | u32 thresh_lo, thresh_hi; |
| 2117 | int use_scaling = 0; |
| 2118 | |
| 2119 | /* tsc_khz can be zero if TSC calibration fails */ |
| 2120 | if (user_tsc_khz == 0) { |
| 2121 | /* set tsc_scaling_ratio to a safe value */ |
| 2122 | vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; |
| 2123 | return -1; |
| 2124 | } |
| 2125 | |
| 2126 | /* Compute a scale to convert nanoseconds in TSC cycles */ |
| 2127 | kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC, |
| 2128 | &vcpu->arch.virtual_tsc_shift, |
| 2129 | &vcpu->arch.virtual_tsc_mult); |
| 2130 | vcpu->arch.virtual_tsc_khz = user_tsc_khz; |
| 2131 | |
| 2132 | /* |
| 2133 | * Compute the variation in TSC rate which is acceptable |
| 2134 | * within the range of tolerance and decide if the |
| 2135 | * rate being applied is within that bounds of the hardware |
| 2136 | * rate. If so, no scaling or compensation need be done. |
| 2137 | */ |
| 2138 | thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); |
| 2139 | thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm); |
| 2140 | if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) { |
| 2141 | pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", user_tsc_khz, thresh_lo, thresh_hi); |
| 2142 | use_scaling = 1; |
| 2143 | } |
| 2144 | return set_tsc_khz(vcpu, user_tsc_khz, use_scaling); |
| 2145 | } |
| 2146 | |
| 2147 | static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns) |
| 2148 | { |
| 2149 | u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, |
| 2150 | vcpu->arch.virtual_tsc_mult, |
| 2151 | vcpu->arch.virtual_tsc_shift); |
| 2152 | tsc += vcpu->arch.this_tsc_write; |
| 2153 | return tsc; |
| 2154 | } |
| 2155 | |
| 2156 | static inline int gtod_is_based_on_tsc(int mode) |
| 2157 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2158 | return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu) |
| 2162 | { |
| 2163 | #ifdef CONFIG_X86_64 |
| 2164 | bool vcpus_matched; |
| 2165 | struct kvm_arch *ka = &vcpu->kvm->arch; |
| 2166 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; |
| 2167 | |
| 2168 | vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == |
| 2169 | atomic_read(&vcpu->kvm->online_vcpus)); |
| 2170 | |
| 2171 | /* |
| 2172 | * Once the masterclock is enabled, always perform request in |
| 2173 | * order to update it. |
| 2174 | * |
| 2175 | * In order to enable masterclock, the host clocksource must be TSC |
| 2176 | * and the vcpus need to have matched TSCs. When that happens, |
| 2177 | * perform request to enable masterclock. |
| 2178 | */ |
| 2179 | if (ka->use_master_clock || |
| 2180 | (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched)) |
| 2181 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
| 2182 | |
| 2183 | trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, |
| 2184 | atomic_read(&vcpu->kvm->online_vcpus), |
| 2185 | ka->use_master_clock, gtod->clock.vclock_mode); |
| 2186 | #endif |
| 2187 | } |
| 2188 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2189 | /* |
| 2190 | * Multiply tsc by a fixed point number represented by ratio. |
| 2191 | * |
| 2192 | * The most significant 64-N bits (mult) of ratio represent the |
| 2193 | * integral part of the fixed point number; the remaining N bits |
| 2194 | * (frac) represent the fractional part, ie. ratio represents a fixed |
| 2195 | * point number (mult + frac * 2^(-N)). |
| 2196 | * |
| 2197 | * N equals to kvm_tsc_scaling_ratio_frac_bits. |
| 2198 | */ |
| 2199 | static inline u64 __scale_tsc(u64 ratio, u64 tsc) |
| 2200 | { |
| 2201 | return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits); |
| 2202 | } |
| 2203 | |
| 2204 | u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) |
| 2205 | { |
| 2206 | u64 _tsc = tsc; |
| 2207 | u64 ratio = vcpu->arch.tsc_scaling_ratio; |
| 2208 | |
| 2209 | if (ratio != kvm_default_tsc_scaling_ratio) |
| 2210 | _tsc = __scale_tsc(ratio, tsc); |
| 2211 | |
| 2212 | return _tsc; |
| 2213 | } |
| 2214 | EXPORT_SYMBOL_GPL(kvm_scale_tsc); |
| 2215 | |
| 2216 | static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
| 2217 | { |
| 2218 | u64 tsc; |
| 2219 | |
| 2220 | tsc = kvm_scale_tsc(vcpu, rdtsc()); |
| 2221 | |
| 2222 | return target_tsc - tsc; |
| 2223 | } |
| 2224 | |
| 2225 | u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) |
| 2226 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2227 | return vcpu->arch.l1_tsc_offset + kvm_scale_tsc(vcpu, host_tsc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2228 | } |
| 2229 | EXPORT_SYMBOL_GPL(kvm_read_l1_tsc); |
| 2230 | |
| 2231 | static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
| 2232 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2233 | vcpu->arch.l1_tsc_offset = offset; |
| 2234 | vcpu->arch.tsc_offset = kvm_x86_ops.write_l1_tsc_offset(vcpu, offset); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | static inline bool kvm_check_tsc_unstable(void) |
| 2238 | { |
| 2239 | #ifdef CONFIG_X86_64 |
| 2240 | /* |
| 2241 | * TSC is marked unstable when we're running on Hyper-V, |
| 2242 | * 'TSC page' clocksource is good. |
| 2243 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2244 | if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2245 | return false; |
| 2246 | #endif |
| 2247 | return check_tsc_unstable(); |
| 2248 | } |
| 2249 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2250 | static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2251 | { |
| 2252 | struct kvm *kvm = vcpu->kvm; |
| 2253 | u64 offset, ns, elapsed; |
| 2254 | unsigned long flags; |
| 2255 | bool matched; |
| 2256 | bool already_matched; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2257 | bool synchronizing = false; |
| 2258 | |
| 2259 | raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); |
| 2260 | offset = kvm_compute_tsc_offset(vcpu, data); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2261 | ns = get_kvmclock_base_ns(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2262 | elapsed = ns - kvm->arch.last_tsc_nsec; |
| 2263 | |
| 2264 | if (vcpu->arch.virtual_tsc_khz) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2265 | if (data == 0) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2266 | /* |
| 2267 | * detection of vcpu initialization -- need to sync |
| 2268 | * with other vCPUs. This particularly helps to keep |
| 2269 | * kvm_clock stable after CPU hotplug |
| 2270 | */ |
| 2271 | synchronizing = true; |
| 2272 | } else { |
| 2273 | u64 tsc_exp = kvm->arch.last_tsc_write + |
| 2274 | nsec_to_cycles(vcpu, elapsed); |
| 2275 | u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; |
| 2276 | /* |
| 2277 | * Special case: TSC write with a small delta (1 second) |
| 2278 | * of virtual cycle time against real time is |
| 2279 | * interpreted as an attempt to synchronize the CPU. |
| 2280 | */ |
| 2281 | synchronizing = data < tsc_exp + tsc_hz && |
| 2282 | data + tsc_hz > tsc_exp; |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | /* |
| 2287 | * For a reliable TSC, we can match TSC offsets, and for an unstable |
| 2288 | * TSC, we add elapsed time in this computation. We could let the |
| 2289 | * compensation code attempt to catch up if we fall behind, but |
| 2290 | * it's better to try to match offsets from the beginning. |
| 2291 | */ |
| 2292 | if (synchronizing && |
| 2293 | vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { |
| 2294 | if (!kvm_check_tsc_unstable()) { |
| 2295 | offset = kvm->arch.cur_tsc_offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2296 | } else { |
| 2297 | u64 delta = nsec_to_cycles(vcpu, elapsed); |
| 2298 | data += delta; |
| 2299 | offset = kvm_compute_tsc_offset(vcpu, data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2300 | } |
| 2301 | matched = true; |
| 2302 | already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation); |
| 2303 | } else { |
| 2304 | /* |
| 2305 | * We split periods of matched TSC writes into generations. |
| 2306 | * For each generation, we track the original measured |
| 2307 | * nanosecond time, offset, and write, so if TSCs are in |
| 2308 | * sync, we can match exact offset, and if not, we can match |
| 2309 | * exact software computation in compute_guest_tsc() |
| 2310 | * |
| 2311 | * These values are tracked in kvm->arch.cur_xxx variables. |
| 2312 | */ |
| 2313 | kvm->arch.cur_tsc_generation++; |
| 2314 | kvm->arch.cur_tsc_nsec = ns; |
| 2315 | kvm->arch.cur_tsc_write = data; |
| 2316 | kvm->arch.cur_tsc_offset = offset; |
| 2317 | matched = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | /* |
| 2321 | * We also track th most recent recorded KHZ, write and time to |
| 2322 | * allow the matching interval to be extended at each write. |
| 2323 | */ |
| 2324 | kvm->arch.last_tsc_nsec = ns; |
| 2325 | kvm->arch.last_tsc_write = data; |
| 2326 | kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; |
| 2327 | |
| 2328 | vcpu->arch.last_guest_tsc = data; |
| 2329 | |
| 2330 | /* Keep track of which generation this VCPU has synchronized to */ |
| 2331 | vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation; |
| 2332 | vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; |
| 2333 | vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; |
| 2334 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2335 | kvm_vcpu_write_tsc_offset(vcpu, offset); |
| 2336 | raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); |
| 2337 | |
| 2338 | spin_lock(&kvm->arch.pvclock_gtod_sync_lock); |
| 2339 | if (!matched) { |
| 2340 | kvm->arch.nr_vcpus_matched_tsc = 0; |
| 2341 | } else if (!already_matched) { |
| 2342 | kvm->arch.nr_vcpus_matched_tsc++; |
| 2343 | } |
| 2344 | |
| 2345 | kvm_track_tsc_matching(vcpu); |
| 2346 | spin_unlock(&kvm->arch.pvclock_gtod_sync_lock); |
| 2347 | } |
| 2348 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2349 | static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, |
| 2350 | s64 adjustment) |
| 2351 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2352 | u64 tsc_offset = vcpu->arch.l1_tsc_offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2353 | kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment); |
| 2354 | } |
| 2355 | |
| 2356 | static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment) |
| 2357 | { |
| 2358 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio) |
| 2359 | WARN_ON(adjustment < 0); |
| 2360 | adjustment = kvm_scale_tsc(vcpu, (u64) adjustment); |
| 2361 | adjust_tsc_offset_guest(vcpu, adjustment); |
| 2362 | } |
| 2363 | |
| 2364 | #ifdef CONFIG_X86_64 |
| 2365 | |
| 2366 | static u64 read_tsc(void) |
| 2367 | { |
| 2368 | u64 ret = (u64)rdtsc_ordered(); |
| 2369 | u64 last = pvclock_gtod_data.clock.cycle_last; |
| 2370 | |
| 2371 | if (likely(ret >= last)) |
| 2372 | return ret; |
| 2373 | |
| 2374 | /* |
| 2375 | * GCC likes to generate cmov here, but this branch is extremely |
| 2376 | * predictable (it's just a function of time and the likely is |
| 2377 | * very likely) and there's a data dependence, so force GCC |
| 2378 | * to generate a branch instead. I don't barrier() because |
| 2379 | * we don't actually need a barrier, and if this function |
| 2380 | * ever gets inlined it will generate worse code. |
| 2381 | */ |
| 2382 | asm volatile (""); |
| 2383 | return last; |
| 2384 | } |
| 2385 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2386 | static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp, |
| 2387 | int *mode) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2388 | { |
| 2389 | long v; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2390 | u64 tsc_pg_val; |
| 2391 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2392 | switch (clock->vclock_mode) { |
| 2393 | case VDSO_CLOCKMODE_HVCLOCK: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2394 | tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(), |
| 2395 | tsc_timestamp); |
| 2396 | if (tsc_pg_val != U64_MAX) { |
| 2397 | /* TSC page valid */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2398 | *mode = VDSO_CLOCKMODE_HVCLOCK; |
| 2399 | v = (tsc_pg_val - clock->cycle_last) & |
| 2400 | clock->mask; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2401 | } else { |
| 2402 | /* TSC page invalid */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2403 | *mode = VDSO_CLOCKMODE_NONE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2404 | } |
| 2405 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2406 | case VDSO_CLOCKMODE_TSC: |
| 2407 | *mode = VDSO_CLOCKMODE_TSC; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2408 | *tsc_timestamp = read_tsc(); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2409 | v = (*tsc_timestamp - clock->cycle_last) & |
| 2410 | clock->mask; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2411 | break; |
| 2412 | default: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2413 | *mode = VDSO_CLOCKMODE_NONE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2416 | if (*mode == VDSO_CLOCKMODE_NONE) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2417 | *tsc_timestamp = v = 0; |
| 2418 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2419 | return v * clock->mult; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2422 | static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2423 | { |
| 2424 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; |
| 2425 | unsigned long seq; |
| 2426 | int mode; |
| 2427 | u64 ns; |
| 2428 | |
| 2429 | do { |
| 2430 | seq = read_seqcount_begin(>od->seq); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2431 | ns = gtod->raw_clock.base_cycles; |
| 2432 | ns += vgettsc(>od->raw_clock, tsc_timestamp, &mode); |
| 2433 | ns >>= gtod->raw_clock.shift; |
| 2434 | ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2435 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); |
| 2436 | *t = ns; |
| 2437 | |
| 2438 | return mode; |
| 2439 | } |
| 2440 | |
| 2441 | static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp) |
| 2442 | { |
| 2443 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; |
| 2444 | unsigned long seq; |
| 2445 | int mode; |
| 2446 | u64 ns; |
| 2447 | |
| 2448 | do { |
| 2449 | seq = read_seqcount_begin(>od->seq); |
| 2450 | ts->tv_sec = gtod->wall_time_sec; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2451 | ns = gtod->clock.base_cycles; |
| 2452 | ns += vgettsc(>od->clock, tsc_timestamp, &mode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2453 | ns >>= gtod->clock.shift; |
| 2454 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); |
| 2455 | |
| 2456 | ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); |
| 2457 | ts->tv_nsec = ns; |
| 2458 | |
| 2459 | return mode; |
| 2460 | } |
| 2461 | |
| 2462 | /* returns true if host is using TSC based clocksource */ |
| 2463 | static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp) |
| 2464 | { |
| 2465 | /* checked again under seqlock below */ |
| 2466 | if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) |
| 2467 | return false; |
| 2468 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2469 | return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2470 | tsc_timestamp)); |
| 2471 | } |
| 2472 | |
| 2473 | /* returns true if host is using TSC based clocksource */ |
| 2474 | static bool kvm_get_walltime_and_clockread(struct timespec64 *ts, |
| 2475 | u64 *tsc_timestamp) |
| 2476 | { |
| 2477 | /* checked again under seqlock below */ |
| 2478 | if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) |
| 2479 | return false; |
| 2480 | |
| 2481 | return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp)); |
| 2482 | } |
| 2483 | #endif |
| 2484 | |
| 2485 | /* |
| 2486 | * |
| 2487 | * Assuming a stable TSC across physical CPUS, and a stable TSC |
| 2488 | * across virtual CPUs, the following condition is possible. |
| 2489 | * Each numbered line represents an event visible to both |
| 2490 | * CPUs at the next numbered event. |
| 2491 | * |
| 2492 | * "timespecX" represents host monotonic time. "tscX" represents |
| 2493 | * RDTSC value. |
| 2494 | * |
| 2495 | * VCPU0 on CPU0 | VCPU1 on CPU1 |
| 2496 | * |
| 2497 | * 1. read timespec0,tsc0 |
| 2498 | * 2. | timespec1 = timespec0 + N |
| 2499 | * | tsc1 = tsc0 + M |
| 2500 | * 3. transition to guest | transition to guest |
| 2501 | * 4. ret0 = timespec0 + (rdtsc - tsc0) | |
| 2502 | * 5. | ret1 = timespec1 + (rdtsc - tsc1) |
| 2503 | * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M)) |
| 2504 | * |
| 2505 | * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity: |
| 2506 | * |
| 2507 | * - ret0 < ret1 |
| 2508 | * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M)) |
| 2509 | * ... |
| 2510 | * - 0 < N - M => M < N |
| 2511 | * |
| 2512 | * That is, when timespec0 != timespec1, M < N. Unfortunately that is not |
| 2513 | * always the case (the difference between two distinct xtime instances |
| 2514 | * might be smaller then the difference between corresponding TSC reads, |
| 2515 | * when updating guest vcpus pvclock areas). |
| 2516 | * |
| 2517 | * To avoid that problem, do not allow visibility of distinct |
| 2518 | * system_timestamp/tsc_timestamp values simultaneously: use a master |
| 2519 | * copy of host monotonic time values. Update that master copy |
| 2520 | * in lockstep. |
| 2521 | * |
| 2522 | * Rely on synchronization of host TSCs and guest TSCs for monotonicity. |
| 2523 | * |
| 2524 | */ |
| 2525 | |
| 2526 | static void pvclock_update_vm_gtod_copy(struct kvm *kvm) |
| 2527 | { |
| 2528 | #ifdef CONFIG_X86_64 |
| 2529 | struct kvm_arch *ka = &kvm->arch; |
| 2530 | int vclock_mode; |
| 2531 | bool host_tsc_clocksource, vcpus_matched; |
| 2532 | |
| 2533 | vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == |
| 2534 | atomic_read(&kvm->online_vcpus)); |
| 2535 | |
| 2536 | /* |
| 2537 | * If the host uses TSC clock, then passthrough TSC as stable |
| 2538 | * to the guest. |
| 2539 | */ |
| 2540 | host_tsc_clocksource = kvm_get_time_and_clockread( |
| 2541 | &ka->master_kernel_ns, |
| 2542 | &ka->master_cycle_now); |
| 2543 | |
| 2544 | ka->use_master_clock = host_tsc_clocksource && vcpus_matched |
| 2545 | && !ka->backwards_tsc_observed |
| 2546 | && !ka->boot_vcpu_runs_old_kvmclock; |
| 2547 | |
| 2548 | if (ka->use_master_clock) |
| 2549 | atomic_set(&kvm_guest_has_master_clock, 1); |
| 2550 | |
| 2551 | vclock_mode = pvclock_gtod_data.clock.vclock_mode; |
| 2552 | trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, |
| 2553 | vcpus_matched); |
| 2554 | #endif |
| 2555 | } |
| 2556 | |
| 2557 | void kvm_make_mclock_inprogress_request(struct kvm *kvm) |
| 2558 | { |
| 2559 | kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS); |
| 2560 | } |
| 2561 | |
| 2562 | static void kvm_gen_update_masterclock(struct kvm *kvm) |
| 2563 | { |
| 2564 | #ifdef CONFIG_X86_64 |
| 2565 | int i; |
| 2566 | struct kvm_vcpu *vcpu; |
| 2567 | struct kvm_arch *ka = &kvm->arch; |
| 2568 | |
| 2569 | spin_lock(&ka->pvclock_gtod_sync_lock); |
| 2570 | kvm_make_mclock_inprogress_request(kvm); |
| 2571 | /* no guest entries from this point */ |
| 2572 | pvclock_update_vm_gtod_copy(kvm); |
| 2573 | |
| 2574 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 2575 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
| 2576 | |
| 2577 | /* guest entries allowed */ |
| 2578 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 2579 | kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu); |
| 2580 | |
| 2581 | spin_unlock(&ka->pvclock_gtod_sync_lock); |
| 2582 | #endif |
| 2583 | } |
| 2584 | |
| 2585 | u64 get_kvmclock_ns(struct kvm *kvm) |
| 2586 | { |
| 2587 | struct kvm_arch *ka = &kvm->arch; |
| 2588 | struct pvclock_vcpu_time_info hv_clock; |
| 2589 | u64 ret; |
| 2590 | |
| 2591 | spin_lock(&ka->pvclock_gtod_sync_lock); |
| 2592 | if (!ka->use_master_clock) { |
| 2593 | spin_unlock(&ka->pvclock_gtod_sync_lock); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2594 | return get_kvmclock_base_ns() + ka->kvmclock_offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | hv_clock.tsc_timestamp = ka->master_cycle_now; |
| 2598 | hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; |
| 2599 | spin_unlock(&ka->pvclock_gtod_sync_lock); |
| 2600 | |
| 2601 | /* both __this_cpu_read() and rdtsc() should be on the same cpu */ |
| 2602 | get_cpu(); |
| 2603 | |
| 2604 | if (__this_cpu_read(cpu_tsc_khz)) { |
| 2605 | kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, |
| 2606 | &hv_clock.tsc_shift, |
| 2607 | &hv_clock.tsc_to_system_mul); |
| 2608 | ret = __pvclock_read_cycles(&hv_clock, rdtsc()); |
| 2609 | } else |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2610 | ret = get_kvmclock_base_ns() + ka->kvmclock_offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2611 | |
| 2612 | put_cpu(); |
| 2613 | |
| 2614 | return ret; |
| 2615 | } |
| 2616 | |
| 2617 | static void kvm_setup_pvclock_page(struct kvm_vcpu *v) |
| 2618 | { |
| 2619 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 2620 | struct pvclock_vcpu_time_info guest_hv_clock; |
| 2621 | |
| 2622 | if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time, |
| 2623 | &guest_hv_clock, sizeof(guest_hv_clock)))) |
| 2624 | return; |
| 2625 | |
| 2626 | /* This VCPU is paused, but it's legal for a guest to read another |
| 2627 | * VCPU's kvmclock, so we really have to follow the specification where |
| 2628 | * it says that version is odd if data is being modified, and even after |
| 2629 | * it is consistent. |
| 2630 | * |
| 2631 | * Version field updates must be kept separate. This is because |
| 2632 | * kvm_write_guest_cached might use a "rep movs" instruction, and |
| 2633 | * writes within a string instruction are weakly ordered. So there |
| 2634 | * are three writes overall. |
| 2635 | * |
| 2636 | * As a small optimization, only write the version field in the first |
| 2637 | * and third write. The vcpu->pv_time cache is still valid, because the |
| 2638 | * version field is the first in the struct. |
| 2639 | */ |
| 2640 | BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0); |
| 2641 | |
| 2642 | if (guest_hv_clock.version & 1) |
| 2643 | ++guest_hv_clock.version; /* first time write, random junk */ |
| 2644 | |
| 2645 | vcpu->hv_clock.version = guest_hv_clock.version + 1; |
| 2646 | kvm_write_guest_cached(v->kvm, &vcpu->pv_time, |
| 2647 | &vcpu->hv_clock, |
| 2648 | sizeof(vcpu->hv_clock.version)); |
| 2649 | |
| 2650 | smp_wmb(); |
| 2651 | |
| 2652 | /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */ |
| 2653 | vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED); |
| 2654 | |
| 2655 | if (vcpu->pvclock_set_guest_stopped_request) { |
| 2656 | vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED; |
| 2657 | vcpu->pvclock_set_guest_stopped_request = false; |
| 2658 | } |
| 2659 | |
| 2660 | trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); |
| 2661 | |
| 2662 | kvm_write_guest_cached(v->kvm, &vcpu->pv_time, |
| 2663 | &vcpu->hv_clock, |
| 2664 | sizeof(vcpu->hv_clock)); |
| 2665 | |
| 2666 | smp_wmb(); |
| 2667 | |
| 2668 | vcpu->hv_clock.version++; |
| 2669 | kvm_write_guest_cached(v->kvm, &vcpu->pv_time, |
| 2670 | &vcpu->hv_clock, |
| 2671 | sizeof(vcpu->hv_clock.version)); |
| 2672 | } |
| 2673 | |
| 2674 | static int kvm_guest_time_update(struct kvm_vcpu *v) |
| 2675 | { |
| 2676 | unsigned long flags, tgt_tsc_khz; |
| 2677 | struct kvm_vcpu_arch *vcpu = &v->arch; |
| 2678 | struct kvm_arch *ka = &v->kvm->arch; |
| 2679 | s64 kernel_ns; |
| 2680 | u64 tsc_timestamp, host_tsc; |
| 2681 | u8 pvclock_flags; |
| 2682 | bool use_master_clock; |
| 2683 | |
| 2684 | kernel_ns = 0; |
| 2685 | host_tsc = 0; |
| 2686 | |
| 2687 | /* |
| 2688 | * If the host uses TSC clock, then passthrough TSC as stable |
| 2689 | * to the guest. |
| 2690 | */ |
| 2691 | spin_lock(&ka->pvclock_gtod_sync_lock); |
| 2692 | use_master_clock = ka->use_master_clock; |
| 2693 | if (use_master_clock) { |
| 2694 | host_tsc = ka->master_cycle_now; |
| 2695 | kernel_ns = ka->master_kernel_ns; |
| 2696 | } |
| 2697 | spin_unlock(&ka->pvclock_gtod_sync_lock); |
| 2698 | |
| 2699 | /* Keep irq disabled to prevent changes to the clock */ |
| 2700 | local_irq_save(flags); |
| 2701 | tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz); |
| 2702 | if (unlikely(tgt_tsc_khz == 0)) { |
| 2703 | local_irq_restore(flags); |
| 2704 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); |
| 2705 | return 1; |
| 2706 | } |
| 2707 | if (!use_master_clock) { |
| 2708 | host_tsc = rdtsc(); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2709 | kernel_ns = get_kvmclock_base_ns(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | tsc_timestamp = kvm_read_l1_tsc(v, host_tsc); |
| 2713 | |
| 2714 | /* |
| 2715 | * We may have to catch up the TSC to match elapsed wall clock |
| 2716 | * time for two reasons, even if kvmclock is used. |
| 2717 | * 1) CPU could have been running below the maximum TSC rate |
| 2718 | * 2) Broken TSC compensation resets the base at each VCPU |
| 2719 | * entry to avoid unknown leaps of TSC even when running |
| 2720 | * again on the same CPU. This may cause apparent elapsed |
| 2721 | * time to disappear, and the guest to stand still or run |
| 2722 | * very slowly. |
| 2723 | */ |
| 2724 | if (vcpu->tsc_catchup) { |
| 2725 | u64 tsc = compute_guest_tsc(v, kernel_ns); |
| 2726 | if (tsc > tsc_timestamp) { |
| 2727 | adjust_tsc_offset_guest(v, tsc - tsc_timestamp); |
| 2728 | tsc_timestamp = tsc; |
| 2729 | } |
| 2730 | } |
| 2731 | |
| 2732 | local_irq_restore(flags); |
| 2733 | |
| 2734 | /* With all the info we got, fill in the values */ |
| 2735 | |
| 2736 | if (kvm_has_tsc_control) |
| 2737 | tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz); |
| 2738 | |
| 2739 | if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { |
| 2740 | kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL, |
| 2741 | &vcpu->hv_clock.tsc_shift, |
| 2742 | &vcpu->hv_clock.tsc_to_system_mul); |
| 2743 | vcpu->hw_tsc_khz = tgt_tsc_khz; |
| 2744 | } |
| 2745 | |
| 2746 | vcpu->hv_clock.tsc_timestamp = tsc_timestamp; |
| 2747 | vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; |
| 2748 | vcpu->last_guest_tsc = tsc_timestamp; |
| 2749 | |
| 2750 | /* If the host uses TSC clocksource, then it is stable */ |
| 2751 | pvclock_flags = 0; |
| 2752 | if (use_master_clock) |
| 2753 | pvclock_flags |= PVCLOCK_TSC_STABLE_BIT; |
| 2754 | |
| 2755 | vcpu->hv_clock.flags = pvclock_flags; |
| 2756 | |
| 2757 | if (vcpu->pv_time_enabled) |
| 2758 | kvm_setup_pvclock_page(v); |
| 2759 | if (v == kvm_get_vcpu(v->kvm, 0)) |
| 2760 | kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); |
| 2761 | return 0; |
| 2762 | } |
| 2763 | |
| 2764 | /* |
| 2765 | * kvmclock updates which are isolated to a given vcpu, such as |
| 2766 | * vcpu->cpu migration, should not allow system_timestamp from |
| 2767 | * the rest of the vcpus to remain static. Otherwise ntp frequency |
| 2768 | * correction applies to one vcpu's system_timestamp but not |
| 2769 | * the others. |
| 2770 | * |
| 2771 | * So in those cases, request a kvmclock update for all vcpus. |
| 2772 | * We need to rate-limit these requests though, as they can |
| 2773 | * considerably slow guests that have a large number of vcpus. |
| 2774 | * The time for a remote vcpu to update its kvmclock is bound |
| 2775 | * by the delay we use to rate-limit the updates. |
| 2776 | */ |
| 2777 | |
| 2778 | #define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100) |
| 2779 | |
| 2780 | static void kvmclock_update_fn(struct work_struct *work) |
| 2781 | { |
| 2782 | int i; |
| 2783 | struct delayed_work *dwork = to_delayed_work(work); |
| 2784 | struct kvm_arch *ka = container_of(dwork, struct kvm_arch, |
| 2785 | kvmclock_update_work); |
| 2786 | struct kvm *kvm = container_of(ka, struct kvm, arch); |
| 2787 | struct kvm_vcpu *vcpu; |
| 2788 | |
| 2789 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 2790 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
| 2791 | kvm_vcpu_kick(vcpu); |
| 2792 | } |
| 2793 | } |
| 2794 | |
| 2795 | static void kvm_gen_kvmclock_update(struct kvm_vcpu *v) |
| 2796 | { |
| 2797 | struct kvm *kvm = v->kvm; |
| 2798 | |
| 2799 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); |
| 2800 | schedule_delayed_work(&kvm->arch.kvmclock_update_work, |
| 2801 | KVMCLOCK_UPDATE_DELAY); |
| 2802 | } |
| 2803 | |
| 2804 | #define KVMCLOCK_SYNC_PERIOD (300 * HZ) |
| 2805 | |
| 2806 | static void kvmclock_sync_fn(struct work_struct *work) |
| 2807 | { |
| 2808 | struct delayed_work *dwork = to_delayed_work(work); |
| 2809 | struct kvm_arch *ka = container_of(dwork, struct kvm_arch, |
| 2810 | kvmclock_sync_work); |
| 2811 | struct kvm *kvm = container_of(ka, struct kvm, arch); |
| 2812 | |
| 2813 | if (!kvmclock_periodic_sync) |
| 2814 | return; |
| 2815 | |
| 2816 | schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); |
| 2817 | schedule_delayed_work(&kvm->arch.kvmclock_sync_work, |
| 2818 | KVMCLOCK_SYNC_PERIOD); |
| 2819 | } |
| 2820 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2821 | /* |
| 2822 | * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP. |
| 2823 | */ |
| 2824 | static bool can_set_mci_status(struct kvm_vcpu *vcpu) |
| 2825 | { |
| 2826 | /* McStatusWrEn enabled? */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2827 | if (guest_cpuid_is_amd_or_hygon(vcpu)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2828 | return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); |
| 2829 | |
| 2830 | return false; |
| 2831 | } |
| 2832 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2833 | static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
| 2834 | { |
| 2835 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 2836 | unsigned bank_num = mcg_cap & 0xff; |
| 2837 | u32 msr = msr_info->index; |
| 2838 | u64 data = msr_info->data; |
| 2839 | |
| 2840 | switch (msr) { |
| 2841 | case MSR_IA32_MCG_STATUS: |
| 2842 | vcpu->arch.mcg_status = data; |
| 2843 | break; |
| 2844 | case MSR_IA32_MCG_CTL: |
| 2845 | if (!(mcg_cap & MCG_CTL_P) && |
| 2846 | (data || !msr_info->host_initiated)) |
| 2847 | return 1; |
| 2848 | if (data != 0 && data != ~(u64)0) |
| 2849 | return 1; |
| 2850 | vcpu->arch.mcg_ctl = data; |
| 2851 | break; |
| 2852 | default: |
| 2853 | if (msr >= MSR_IA32_MC0_CTL && |
| 2854 | msr < MSR_IA32_MCx_CTL(bank_num)) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2855 | u32 offset = array_index_nospec( |
| 2856 | msr - MSR_IA32_MC0_CTL, |
| 2857 | MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL); |
| 2858 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2859 | /* only 0 or all 1s can be written to IA32_MCi_CTL |
| 2860 | * some Linux kernels though clear bit 10 in bank 4 to |
| 2861 | * workaround a BIOS/GART TBL issue on AMD K8s, ignore |
| 2862 | * this to avoid an uncatched #GP in the guest |
| 2863 | */ |
| 2864 | if ((offset & 0x3) == 0 && |
| 2865 | data != 0 && (data | (1 << 10)) != ~(u64)0) |
| 2866 | return -1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2867 | |
| 2868 | /* MCi_STATUS */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2869 | if (!msr_info->host_initiated && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2870 | (offset & 0x3) == 1 && data != 0) { |
| 2871 | if (!can_set_mci_status(vcpu)) |
| 2872 | return -1; |
| 2873 | } |
| 2874 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2875 | vcpu->arch.mce_banks[offset] = data; |
| 2876 | break; |
| 2877 | } |
| 2878 | return 1; |
| 2879 | } |
| 2880 | return 0; |
| 2881 | } |
| 2882 | |
| 2883 | static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data) |
| 2884 | { |
| 2885 | struct kvm *kvm = vcpu->kvm; |
| 2886 | int lm = is_long_mode(vcpu); |
| 2887 | u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64 |
| 2888 | : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32; |
| 2889 | u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64 |
| 2890 | : kvm->arch.xen_hvm_config.blob_size_32; |
| 2891 | u32 page_num = data & ~PAGE_MASK; |
| 2892 | u64 page_addr = data & PAGE_MASK; |
| 2893 | u8 *page; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2894 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2895 | if (page_num >= blob_size) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2896 | return 1; |
| 2897 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2898 | page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2899 | if (IS_ERR(page)) |
| 2900 | return PTR_ERR(page); |
| 2901 | |
| 2902 | if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) { |
| 2903 | kfree(page); |
| 2904 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2905 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2906 | return 0; |
| 2907 | } |
| 2908 | |
| 2909 | static inline bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu) |
| 2910 | { |
| 2911 | u64 mask = KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT; |
| 2912 | |
| 2913 | return (vcpu->arch.apf.msr_en_val & mask) == mask; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data) |
| 2917 | { |
| 2918 | gpa_t gpa = data & ~0x3f; |
| 2919 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2920 | /* Bits 4:5 are reserved, Should be zero */ |
| 2921 | if (data & 0x30) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2922 | return 1; |
| 2923 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2924 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_VMEXIT) && |
| 2925 | (data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT)) |
| 2926 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2927 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2928 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT) && |
| 2929 | (data & KVM_ASYNC_PF_DELIVERY_AS_INT)) |
| 2930 | return 1; |
| 2931 | |
| 2932 | if (!lapic_in_kernel(vcpu)) |
| 2933 | return data ? 1 : 0; |
| 2934 | |
| 2935 | vcpu->arch.apf.msr_en_val = data; |
| 2936 | |
| 2937 | if (!kvm_pv_async_pf_enabled(vcpu)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2938 | kvm_clear_async_pf_completion_queue(vcpu); |
| 2939 | kvm_async_pf_hash_reset(vcpu); |
| 2940 | return 0; |
| 2941 | } |
| 2942 | |
| 2943 | if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2944 | sizeof(u64))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2945 | return 1; |
| 2946 | |
| 2947 | vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS); |
| 2948 | vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2949 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2950 | kvm_async_pf_wakeup_all(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2951 | |
| 2952 | return 0; |
| 2953 | } |
| 2954 | |
| 2955 | static int kvm_pv_enable_async_pf_int(struct kvm_vcpu *vcpu, u64 data) |
| 2956 | { |
| 2957 | /* Bits 8-63 are reserved */ |
| 2958 | if (data >> 8) |
| 2959 | return 1; |
| 2960 | |
| 2961 | if (!lapic_in_kernel(vcpu)) |
| 2962 | return 1; |
| 2963 | |
| 2964 | vcpu->arch.apf.msr_int_val = data; |
| 2965 | |
| 2966 | vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK; |
| 2967 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2968 | return 0; |
| 2969 | } |
| 2970 | |
| 2971 | static void kvmclock_reset(struct kvm_vcpu *vcpu) |
| 2972 | { |
| 2973 | vcpu->arch.pv_time_enabled = false; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2974 | vcpu->arch.time = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2977 | static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2978 | { |
| 2979 | ++vcpu->stat.tlb_flush; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2980 | kvm_x86_ops.tlb_flush_all(vcpu); |
| 2981 | } |
| 2982 | |
| 2983 | static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu) |
| 2984 | { |
| 2985 | ++vcpu->stat.tlb_flush; |
| 2986 | kvm_x86_ops.tlb_flush_guest(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | static void record_steal_time(struct kvm_vcpu *vcpu) |
| 2990 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2991 | struct kvm_host_map map; |
| 2992 | struct kvm_steal_time *st; |
| 2993 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2994 | if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) |
| 2995 | return; |
| 2996 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2997 | /* -EAGAIN is returned in atomic context so we can just return. */ |
| 2998 | if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, |
| 2999 | &map, &vcpu->arch.st.cache, false)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3000 | return; |
| 3001 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3002 | st = map.hva + |
| 3003 | offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS); |
| 3004 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3005 | /* |
| 3006 | * Doing a TLB flush here, on the guest's behalf, can avoid |
| 3007 | * expensive IPIs. |
| 3008 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3009 | if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) { |
| 3010 | trace_kvm_pv_tlb_flush(vcpu->vcpu_id, |
| 3011 | st->preempted & KVM_VCPU_FLUSH_TLB); |
| 3012 | if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB) |
| 3013 | kvm_vcpu_flush_tlb_guest(vcpu); |
| 3014 | } else { |
| 3015 | st->preempted = 0; |
| 3016 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3017 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3018 | vcpu->arch.st.preempted = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3019 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3020 | if (st->version & 1) |
| 3021 | st->version += 1; /* first time write, random junk */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3022 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3023 | st->version += 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3024 | |
| 3025 | smp_wmb(); |
| 3026 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3027 | st->steal += current->sched_info.run_delay - |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3028 | vcpu->arch.st.last_steal; |
| 3029 | vcpu->arch.st.last_steal = current->sched_info.run_delay; |
| 3030 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3031 | smp_wmb(); |
| 3032 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3033 | st->version += 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3034 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3035 | kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
| 3039 | { |
| 3040 | bool pr = false; |
| 3041 | u32 msr = msr_info->index; |
| 3042 | u64 data = msr_info->data; |
| 3043 | |
| 3044 | switch (msr) { |
| 3045 | case MSR_AMD64_NB_CFG: |
| 3046 | case MSR_IA32_UCODE_WRITE: |
| 3047 | case MSR_VM_HSAVE_PA: |
| 3048 | case MSR_AMD64_PATCH_LOADER: |
| 3049 | case MSR_AMD64_BU_CFG2: |
| 3050 | case MSR_AMD64_DC_CFG: |
| 3051 | case MSR_F15H_EX_CFG: |
| 3052 | break; |
| 3053 | |
| 3054 | case MSR_IA32_UCODE_REV: |
| 3055 | if (msr_info->host_initiated) |
| 3056 | vcpu->arch.microcode_version = data; |
| 3057 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3058 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3059 | if (!msr_info->host_initiated) |
| 3060 | return 1; |
| 3061 | vcpu->arch.arch_capabilities = data; |
| 3062 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3063 | case MSR_IA32_PERF_CAPABILITIES: { |
| 3064 | struct kvm_msr_entry msr_ent = {.index = msr, .data = 0}; |
| 3065 | |
| 3066 | if (!msr_info->host_initiated) |
| 3067 | return 1; |
| 3068 | if (kvm_get_msr_feature(&msr_ent)) |
| 3069 | return 1; |
| 3070 | if (data & ~msr_ent.data) |
| 3071 | return 1; |
| 3072 | |
| 3073 | vcpu->arch.perf_capabilities = data; |
| 3074 | |
| 3075 | return 0; |
| 3076 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3077 | case MSR_EFER: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3078 | return set_efer(vcpu, msr_info); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3079 | case MSR_K7_HWCR: |
| 3080 | data &= ~(u64)0x40; /* ignore flush filter disable */ |
| 3081 | data &= ~(u64)0x100; /* ignore ignne emulation enable */ |
| 3082 | data &= ~(u64)0x8; /* ignore TLB cache disable */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3083 | |
| 3084 | /* Handle McStatusWrEn */ |
| 3085 | if (data == BIT_ULL(18)) { |
| 3086 | vcpu->arch.msr_hwcr = data; |
| 3087 | } else if (data != 0) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3088 | vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n", |
| 3089 | data); |
| 3090 | return 1; |
| 3091 | } |
| 3092 | break; |
| 3093 | case MSR_FAM10H_MMIO_CONF_BASE: |
| 3094 | if (data != 0) { |
| 3095 | vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: " |
| 3096 | "0x%llx\n", data); |
| 3097 | return 1; |
| 3098 | } |
| 3099 | break; |
| 3100 | case MSR_IA32_DEBUGCTLMSR: |
| 3101 | if (!data) { |
| 3102 | /* We support the non-activated case already */ |
| 3103 | break; |
| 3104 | } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) { |
| 3105 | /* Values other than LBR and BTF are vendor-specific, |
| 3106 | thus reserved and should throw a #GP */ |
| 3107 | return 1; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3108 | } else if (report_ignored_msrs) |
| 3109 | vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n", |
| 3110 | __func__, data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3111 | break; |
| 3112 | case 0x200 ... 0x2ff: |
| 3113 | return kvm_mtrr_set_msr(vcpu, msr, data); |
| 3114 | case MSR_IA32_APICBASE: |
| 3115 | return kvm_set_apic_base(vcpu, msr_info); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3116 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3117 | return kvm_x2apic_msr_write(vcpu, msr, data); |
| 3118 | case MSR_IA32_TSCDEADLINE: |
| 3119 | kvm_set_lapic_tscdeadline_msr(vcpu, data); |
| 3120 | break; |
| 3121 | case MSR_IA32_TSC_ADJUST: |
| 3122 | if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) { |
| 3123 | if (!msr_info->host_initiated) { |
| 3124 | s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; |
| 3125 | adjust_tsc_offset_guest(vcpu, adj); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3126 | /* Before back to guest, tsc_timestamp must be adjusted |
| 3127 | * as well, otherwise guest's percpu pvclock time could jump. |
| 3128 | */ |
| 3129 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3130 | } |
| 3131 | vcpu->arch.ia32_tsc_adjust_msr = data; |
| 3132 | } |
| 3133 | break; |
| 3134 | case MSR_IA32_MISC_ENABLE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3135 | if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && |
| 3136 | ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) { |
| 3137 | if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3)) |
| 3138 | return 1; |
| 3139 | vcpu->arch.ia32_misc_enable_msr = data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3140 | kvm_update_cpuid_runtime(vcpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3141 | } else { |
| 3142 | vcpu->arch.ia32_misc_enable_msr = data; |
| 3143 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3144 | break; |
| 3145 | case MSR_IA32_SMBASE: |
| 3146 | if (!msr_info->host_initiated) |
| 3147 | return 1; |
| 3148 | vcpu->arch.smbase = data; |
| 3149 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3150 | case MSR_IA32_POWER_CTL: |
| 3151 | vcpu->arch.msr_ia32_power_ctl = data; |
| 3152 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3153 | case MSR_IA32_TSC: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3154 | if (msr_info->host_initiated) { |
| 3155 | kvm_synchronize_tsc(vcpu, data); |
| 3156 | } else { |
| 3157 | u64 adj = kvm_compute_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset; |
| 3158 | adjust_tsc_offset_guest(vcpu, adj); |
| 3159 | vcpu->arch.ia32_tsc_adjust_msr += adj; |
| 3160 | } |
| 3161 | break; |
| 3162 | case MSR_IA32_XSS: |
| 3163 | if (!msr_info->host_initiated && |
| 3164 | !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) |
| 3165 | return 1; |
| 3166 | /* |
| 3167 | * KVM supports exposing PT to the guest, but does not support |
| 3168 | * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than |
| 3169 | * XSAVES/XRSTORS to save/restore PT MSRs. |
| 3170 | */ |
| 3171 | if (data & ~supported_xss) |
| 3172 | return 1; |
| 3173 | vcpu->arch.ia32_xss = data; |
| 3174 | kvm_update_cpuid_runtime(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3175 | break; |
| 3176 | case MSR_SMI_COUNT: |
| 3177 | if (!msr_info->host_initiated) |
| 3178 | return 1; |
| 3179 | vcpu->arch.smi_count = data; |
| 3180 | break; |
| 3181 | case MSR_KVM_WALL_CLOCK_NEW: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3182 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) |
| 3183 | return 1; |
| 3184 | |
| 3185 | kvm_write_wall_clock(vcpu->kvm, data); |
| 3186 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3187 | case MSR_KVM_WALL_CLOCK: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3188 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) |
| 3189 | return 1; |
| 3190 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3191 | kvm_write_wall_clock(vcpu->kvm, data); |
| 3192 | break; |
| 3193 | case MSR_KVM_SYSTEM_TIME_NEW: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3194 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) |
| 3195 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3196 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3197 | kvm_write_system_time(vcpu, data, false, msr_info->host_initiated); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3198 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3199 | case MSR_KVM_SYSTEM_TIME: |
| 3200 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) |
| 3201 | return 1; |
| 3202 | |
| 3203 | kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); |
| 3204 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3205 | case MSR_KVM_ASYNC_PF_EN: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3206 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF)) |
| 3207 | return 1; |
| 3208 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3209 | if (kvm_pv_enable_async_pf(vcpu, data)) |
| 3210 | return 1; |
| 3211 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3212 | case MSR_KVM_ASYNC_PF_INT: |
| 3213 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT)) |
| 3214 | return 1; |
| 3215 | |
| 3216 | if (kvm_pv_enable_async_pf_int(vcpu, data)) |
| 3217 | return 1; |
| 3218 | break; |
| 3219 | case MSR_KVM_ASYNC_PF_ACK: |
| 3220 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT)) |
| 3221 | return 1; |
| 3222 | if (data & 0x1) { |
| 3223 | vcpu->arch.apf.pageready_pending = false; |
| 3224 | kvm_check_async_pf_completion(vcpu); |
| 3225 | } |
| 3226 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3227 | case MSR_KVM_STEAL_TIME: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3228 | if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME)) |
| 3229 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3230 | |
| 3231 | if (unlikely(!sched_info_on())) |
| 3232 | return 1; |
| 3233 | |
| 3234 | if (data & KVM_STEAL_RESERVED_MASK) |
| 3235 | return 1; |
| 3236 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3237 | vcpu->arch.st.msr_val = data; |
| 3238 | |
| 3239 | if (!(data & KVM_MSR_ENABLED)) |
| 3240 | break; |
| 3241 | |
| 3242 | kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); |
| 3243 | |
| 3244 | break; |
| 3245 | case MSR_KVM_PV_EOI_EN: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3246 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI)) |
| 3247 | return 1; |
| 3248 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3249 | if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3250 | return 1; |
| 3251 | break; |
| 3252 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3253 | case MSR_KVM_POLL_CONTROL: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3254 | if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL)) |
| 3255 | return 1; |
| 3256 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3257 | /* only enable bit supported */ |
| 3258 | if (data & (-1ULL << 1)) |
| 3259 | return 1; |
| 3260 | |
| 3261 | vcpu->arch.msr_kvm_poll_control = data; |
| 3262 | break; |
| 3263 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3264 | case MSR_IA32_MCG_CTL: |
| 3265 | case MSR_IA32_MCG_STATUS: |
| 3266 | case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: |
| 3267 | return set_msr_mce(vcpu, msr_info); |
| 3268 | |
| 3269 | case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: |
| 3270 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3271 | pr = true; |
| 3272 | fallthrough; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3273 | case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: |
| 3274 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: |
| 3275 | if (kvm_pmu_is_valid_msr(vcpu, msr)) |
| 3276 | return kvm_pmu_set_msr(vcpu, msr_info); |
| 3277 | |
| 3278 | if (pr || data != 0) |
| 3279 | vcpu_unimpl(vcpu, "disabled perfctr wrmsr: " |
| 3280 | "0x%x data 0x%llx\n", msr, data); |
| 3281 | break; |
| 3282 | case MSR_K7_CLK_CTL: |
| 3283 | /* |
| 3284 | * Ignore all writes to this no longer documented MSR. |
| 3285 | * Writes are only relevant for old K7 processors, |
| 3286 | * all pre-dating SVM, but a recommended workaround from |
| 3287 | * AMD for these chips. It is possible to specify the |
| 3288 | * affected processor models on the command line, hence |
| 3289 | * the need to ignore the workaround. |
| 3290 | */ |
| 3291 | break; |
| 3292 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3293 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
| 3294 | case HV_X64_MSR_SYNDBG_OPTIONS: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3295 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 3296 | case HV_X64_MSR_CRASH_CTL: |
| 3297 | case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT: |
| 3298 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
| 3299 | case HV_X64_MSR_TSC_EMULATION_CONTROL: |
| 3300 | case HV_X64_MSR_TSC_EMULATION_STATUS: |
| 3301 | return kvm_hv_set_msr_common(vcpu, msr, data, |
| 3302 | msr_info->host_initiated); |
| 3303 | case MSR_IA32_BBL_CR_CTL3: |
| 3304 | /* Drop writes to this legacy MSR -- see rdmsr |
| 3305 | * counterpart for further detail. |
| 3306 | */ |
| 3307 | if (report_ignored_msrs) |
| 3308 | vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n", |
| 3309 | msr, data); |
| 3310 | break; |
| 3311 | case MSR_AMD64_OSVW_ID_LENGTH: |
| 3312 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
| 3313 | return 1; |
| 3314 | vcpu->arch.osvw.length = data; |
| 3315 | break; |
| 3316 | case MSR_AMD64_OSVW_STATUS: |
| 3317 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
| 3318 | return 1; |
| 3319 | vcpu->arch.osvw.status = data; |
| 3320 | break; |
| 3321 | case MSR_PLATFORM_INFO: |
| 3322 | if (!msr_info->host_initiated || |
| 3323 | (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) && |
| 3324 | cpuid_fault_enabled(vcpu))) |
| 3325 | return 1; |
| 3326 | vcpu->arch.msr_platform_info = data; |
| 3327 | break; |
| 3328 | case MSR_MISC_FEATURES_ENABLES: |
| 3329 | if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT || |
| 3330 | (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT && |
| 3331 | !supports_cpuid_fault(vcpu))) |
| 3332 | return 1; |
| 3333 | vcpu->arch.msr_misc_features_enables = data; |
| 3334 | break; |
| 3335 | default: |
| 3336 | if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr)) |
| 3337 | return xen_hvm_config(vcpu, data); |
| 3338 | if (kvm_pmu_is_valid_msr(vcpu, msr)) |
| 3339 | return kvm_pmu_set_msr(vcpu, msr_info); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3340 | return KVM_MSR_RET_INVALID; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3341 | } |
| 3342 | return 0; |
| 3343 | } |
| 3344 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); |
| 3345 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3346 | static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) |
| 3347 | { |
| 3348 | u64 data; |
| 3349 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 3350 | unsigned bank_num = mcg_cap & 0xff; |
| 3351 | |
| 3352 | switch (msr) { |
| 3353 | case MSR_IA32_P5_MC_ADDR: |
| 3354 | case MSR_IA32_P5_MC_TYPE: |
| 3355 | data = 0; |
| 3356 | break; |
| 3357 | case MSR_IA32_MCG_CAP: |
| 3358 | data = vcpu->arch.mcg_cap; |
| 3359 | break; |
| 3360 | case MSR_IA32_MCG_CTL: |
| 3361 | if (!(mcg_cap & MCG_CTL_P) && !host) |
| 3362 | return 1; |
| 3363 | data = vcpu->arch.mcg_ctl; |
| 3364 | break; |
| 3365 | case MSR_IA32_MCG_STATUS: |
| 3366 | data = vcpu->arch.mcg_status; |
| 3367 | break; |
| 3368 | default: |
| 3369 | if (msr >= MSR_IA32_MC0_CTL && |
| 3370 | msr < MSR_IA32_MCx_CTL(bank_num)) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3371 | u32 offset = array_index_nospec( |
| 3372 | msr - MSR_IA32_MC0_CTL, |
| 3373 | MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL); |
| 3374 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3375 | data = vcpu->arch.mce_banks[offset]; |
| 3376 | break; |
| 3377 | } |
| 3378 | return 1; |
| 3379 | } |
| 3380 | *pdata = data; |
| 3381 | return 0; |
| 3382 | } |
| 3383 | |
| 3384 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
| 3385 | { |
| 3386 | switch (msr_info->index) { |
| 3387 | case MSR_IA32_PLATFORM_ID: |
| 3388 | case MSR_IA32_EBL_CR_POWERON: |
| 3389 | case MSR_IA32_DEBUGCTLMSR: |
| 3390 | case MSR_IA32_LASTBRANCHFROMIP: |
| 3391 | case MSR_IA32_LASTBRANCHTOIP: |
| 3392 | case MSR_IA32_LASTINTFROMIP: |
| 3393 | case MSR_IA32_LASTINTTOIP: |
| 3394 | case MSR_K8_SYSCFG: |
| 3395 | case MSR_K8_TSEG_ADDR: |
| 3396 | case MSR_K8_TSEG_MASK: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3397 | case MSR_VM_HSAVE_PA: |
| 3398 | case MSR_K8_INT_PENDING_MSG: |
| 3399 | case MSR_AMD64_NB_CFG: |
| 3400 | case MSR_FAM10H_MMIO_CONF_BASE: |
| 3401 | case MSR_AMD64_BU_CFG2: |
| 3402 | case MSR_IA32_PERF_CTL: |
| 3403 | case MSR_AMD64_DC_CFG: |
| 3404 | case MSR_F15H_EX_CFG: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3405 | /* |
| 3406 | * Intel Sandy Bridge CPUs must support the RAPL (running average power |
| 3407 | * limit) MSRs. Just return 0, as we do not want to expose the host |
| 3408 | * data here. Do not conditionalize this on CPUID, as KVM does not do |
| 3409 | * so for existing CPU-specific MSRs. |
| 3410 | */ |
| 3411 | case MSR_RAPL_POWER_UNIT: |
| 3412 | case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */ |
| 3413 | case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */ |
| 3414 | case MSR_PKG_ENERGY_STATUS: /* Total package */ |
| 3415 | case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3416 | msr_info->data = 0; |
| 3417 | break; |
| 3418 | case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: |
| 3419 | case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: |
| 3420 | case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: |
| 3421 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: |
| 3422 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: |
| 3423 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3424 | return kvm_pmu_get_msr(vcpu, msr_info); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3425 | msr_info->data = 0; |
| 3426 | break; |
| 3427 | case MSR_IA32_UCODE_REV: |
| 3428 | msr_info->data = vcpu->arch.microcode_version; |
| 3429 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3430 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3431 | if (!msr_info->host_initiated && |
| 3432 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) |
| 3433 | return 1; |
| 3434 | msr_info->data = vcpu->arch.arch_capabilities; |
| 3435 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3436 | case MSR_IA32_PERF_CAPABILITIES: |
| 3437 | if (!msr_info->host_initiated && |
| 3438 | !guest_cpuid_has(vcpu, X86_FEATURE_PDCM)) |
| 3439 | return 1; |
| 3440 | msr_info->data = vcpu->arch.perf_capabilities; |
| 3441 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3442 | case MSR_IA32_POWER_CTL: |
| 3443 | msr_info->data = vcpu->arch.msr_ia32_power_ctl; |
| 3444 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3445 | case MSR_IA32_TSC: { |
| 3446 | /* |
| 3447 | * Intel SDM states that MSR_IA32_TSC read adds the TSC offset |
| 3448 | * even when not intercepted. AMD manual doesn't explicitly |
| 3449 | * state this but appears to behave the same. |
| 3450 | * |
| 3451 | * On userspace reads and writes, however, we unconditionally |
| 3452 | * return L1's TSC value to ensure backwards-compatible |
| 3453 | * behavior for migration. |
| 3454 | */ |
| 3455 | u64 tsc_offset = msr_info->host_initiated ? vcpu->arch.l1_tsc_offset : |
| 3456 | vcpu->arch.tsc_offset; |
| 3457 | |
| 3458 | msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + tsc_offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3459 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3460 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3461 | case MSR_MTRRcap: |
| 3462 | case 0x200 ... 0x2ff: |
| 3463 | return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); |
| 3464 | case 0xcd: /* fsb frequency */ |
| 3465 | msr_info->data = 3; |
| 3466 | break; |
| 3467 | /* |
| 3468 | * MSR_EBC_FREQUENCY_ID |
| 3469 | * Conservative value valid for even the basic CPU models. |
| 3470 | * Models 0,1: 000 in bits 23:21 indicating a bus speed of |
| 3471 | * 100MHz, model 2 000 in bits 18:16 indicating 100MHz, |
| 3472 | * and 266MHz for model 3, or 4. Set Core Clock |
| 3473 | * Frequency to System Bus Frequency Ratio to 1 (bits |
| 3474 | * 31:24) even though these are only valid for CPU |
| 3475 | * models > 2, however guests may end up dividing or |
| 3476 | * multiplying by zero otherwise. |
| 3477 | */ |
| 3478 | case MSR_EBC_FREQUENCY_ID: |
| 3479 | msr_info->data = 1 << 24; |
| 3480 | break; |
| 3481 | case MSR_IA32_APICBASE: |
| 3482 | msr_info->data = kvm_get_apic_base(vcpu); |
| 3483 | break; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3484 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3485 | return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3486 | case MSR_IA32_TSCDEADLINE: |
| 3487 | msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); |
| 3488 | break; |
| 3489 | case MSR_IA32_TSC_ADJUST: |
| 3490 | msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; |
| 3491 | break; |
| 3492 | case MSR_IA32_MISC_ENABLE: |
| 3493 | msr_info->data = vcpu->arch.ia32_misc_enable_msr; |
| 3494 | break; |
| 3495 | case MSR_IA32_SMBASE: |
| 3496 | if (!msr_info->host_initiated) |
| 3497 | return 1; |
| 3498 | msr_info->data = vcpu->arch.smbase; |
| 3499 | break; |
| 3500 | case MSR_SMI_COUNT: |
| 3501 | msr_info->data = vcpu->arch.smi_count; |
| 3502 | break; |
| 3503 | case MSR_IA32_PERF_STATUS: |
| 3504 | /* TSC increment by tick */ |
| 3505 | msr_info->data = 1000ULL; |
| 3506 | /* CPU multiplier */ |
| 3507 | msr_info->data |= (((uint64_t)4ULL) << 40); |
| 3508 | break; |
| 3509 | case MSR_EFER: |
| 3510 | msr_info->data = vcpu->arch.efer; |
| 3511 | break; |
| 3512 | case MSR_KVM_WALL_CLOCK: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3513 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) |
| 3514 | return 1; |
| 3515 | |
| 3516 | msr_info->data = vcpu->kvm->arch.wall_clock; |
| 3517 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3518 | case MSR_KVM_WALL_CLOCK_NEW: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3519 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) |
| 3520 | return 1; |
| 3521 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3522 | msr_info->data = vcpu->kvm->arch.wall_clock; |
| 3523 | break; |
| 3524 | case MSR_KVM_SYSTEM_TIME: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3525 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) |
| 3526 | return 1; |
| 3527 | |
| 3528 | msr_info->data = vcpu->arch.time; |
| 3529 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3530 | case MSR_KVM_SYSTEM_TIME_NEW: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3531 | if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) |
| 3532 | return 1; |
| 3533 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3534 | msr_info->data = vcpu->arch.time; |
| 3535 | break; |
| 3536 | case MSR_KVM_ASYNC_PF_EN: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3537 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF)) |
| 3538 | return 1; |
| 3539 | |
| 3540 | msr_info->data = vcpu->arch.apf.msr_en_val; |
| 3541 | break; |
| 3542 | case MSR_KVM_ASYNC_PF_INT: |
| 3543 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT)) |
| 3544 | return 1; |
| 3545 | |
| 3546 | msr_info->data = vcpu->arch.apf.msr_int_val; |
| 3547 | break; |
| 3548 | case MSR_KVM_ASYNC_PF_ACK: |
| 3549 | if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT)) |
| 3550 | return 1; |
| 3551 | |
| 3552 | msr_info->data = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3553 | break; |
| 3554 | case MSR_KVM_STEAL_TIME: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3555 | if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME)) |
| 3556 | return 1; |
| 3557 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3558 | msr_info->data = vcpu->arch.st.msr_val; |
| 3559 | break; |
| 3560 | case MSR_KVM_PV_EOI_EN: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3561 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI)) |
| 3562 | return 1; |
| 3563 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3564 | msr_info->data = vcpu->arch.pv_eoi.msr_val; |
| 3565 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3566 | case MSR_KVM_POLL_CONTROL: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3567 | if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL)) |
| 3568 | return 1; |
| 3569 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3570 | msr_info->data = vcpu->arch.msr_kvm_poll_control; |
| 3571 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3572 | case MSR_IA32_P5_MC_ADDR: |
| 3573 | case MSR_IA32_P5_MC_TYPE: |
| 3574 | case MSR_IA32_MCG_CAP: |
| 3575 | case MSR_IA32_MCG_CTL: |
| 3576 | case MSR_IA32_MCG_STATUS: |
| 3577 | case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: |
| 3578 | return get_msr_mce(vcpu, msr_info->index, &msr_info->data, |
| 3579 | msr_info->host_initiated); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3580 | case MSR_IA32_XSS: |
| 3581 | if (!msr_info->host_initiated && |
| 3582 | !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) |
| 3583 | return 1; |
| 3584 | msr_info->data = vcpu->arch.ia32_xss; |
| 3585 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3586 | case MSR_K7_CLK_CTL: |
| 3587 | /* |
| 3588 | * Provide expected ramp-up count for K7. All other |
| 3589 | * are set to zero, indicating minimum divisors for |
| 3590 | * every field. |
| 3591 | * |
| 3592 | * This prevents guest kernels on AMD host with CPU |
| 3593 | * type 6, model 8 and higher from exploding due to |
| 3594 | * the rdmsr failing. |
| 3595 | */ |
| 3596 | msr_info->data = 0x20000000; |
| 3597 | break; |
| 3598 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3599 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
| 3600 | case HV_X64_MSR_SYNDBG_OPTIONS: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3601 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 3602 | case HV_X64_MSR_CRASH_CTL: |
| 3603 | case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT: |
| 3604 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
| 3605 | case HV_X64_MSR_TSC_EMULATION_CONTROL: |
| 3606 | case HV_X64_MSR_TSC_EMULATION_STATUS: |
| 3607 | return kvm_hv_get_msr_common(vcpu, |
| 3608 | msr_info->index, &msr_info->data, |
| 3609 | msr_info->host_initiated); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3610 | case MSR_IA32_BBL_CR_CTL3: |
| 3611 | /* This legacy MSR exists but isn't fully documented in current |
| 3612 | * silicon. It is however accessed by winxp in very narrow |
| 3613 | * scenarios where it sets bit #19, itself documented as |
| 3614 | * a "reserved" bit. Best effort attempt to source coherent |
| 3615 | * read data here should the balance of the register be |
| 3616 | * interpreted by the guest: |
| 3617 | * |
| 3618 | * L2 cache control register 3: 64GB range, 256KB size, |
| 3619 | * enabled, latency 0x1, configured |
| 3620 | */ |
| 3621 | msr_info->data = 0xbe702111; |
| 3622 | break; |
| 3623 | case MSR_AMD64_OSVW_ID_LENGTH: |
| 3624 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
| 3625 | return 1; |
| 3626 | msr_info->data = vcpu->arch.osvw.length; |
| 3627 | break; |
| 3628 | case MSR_AMD64_OSVW_STATUS: |
| 3629 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
| 3630 | return 1; |
| 3631 | msr_info->data = vcpu->arch.osvw.status; |
| 3632 | break; |
| 3633 | case MSR_PLATFORM_INFO: |
| 3634 | if (!msr_info->host_initiated && |
| 3635 | !vcpu->kvm->arch.guest_can_read_msr_platform_info) |
| 3636 | return 1; |
| 3637 | msr_info->data = vcpu->arch.msr_platform_info; |
| 3638 | break; |
| 3639 | case MSR_MISC_FEATURES_ENABLES: |
| 3640 | msr_info->data = vcpu->arch.msr_misc_features_enables; |
| 3641 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3642 | case MSR_K7_HWCR: |
| 3643 | msr_info->data = vcpu->arch.msr_hwcr; |
| 3644 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3645 | default: |
| 3646 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3647 | return kvm_pmu_get_msr(vcpu, msr_info); |
| 3648 | return KVM_MSR_RET_INVALID; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3649 | } |
| 3650 | return 0; |
| 3651 | } |
| 3652 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); |
| 3653 | |
| 3654 | /* |
| 3655 | * Read or write a bunch of msrs. All parameters are kernel addresses. |
| 3656 | * |
| 3657 | * @return number of msrs set successfully. |
| 3658 | */ |
| 3659 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, |
| 3660 | struct kvm_msr_entry *entries, |
| 3661 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 3662 | unsigned index, u64 *data)) |
| 3663 | { |
| 3664 | int i; |
| 3665 | |
| 3666 | for (i = 0; i < msrs->nmsrs; ++i) |
| 3667 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) |
| 3668 | break; |
| 3669 | |
| 3670 | return i; |
| 3671 | } |
| 3672 | |
| 3673 | /* |
| 3674 | * Read or write a bunch of msrs. Parameters are user addresses. |
| 3675 | * |
| 3676 | * @return number of msrs set successfully. |
| 3677 | */ |
| 3678 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, |
| 3679 | int (*do_msr)(struct kvm_vcpu *vcpu, |
| 3680 | unsigned index, u64 *data), |
| 3681 | int writeback) |
| 3682 | { |
| 3683 | struct kvm_msrs msrs; |
| 3684 | struct kvm_msr_entry *entries; |
| 3685 | int r, n; |
| 3686 | unsigned size; |
| 3687 | |
| 3688 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3689 | if (copy_from_user(&msrs, user_msrs, sizeof(msrs))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3690 | goto out; |
| 3691 | |
| 3692 | r = -E2BIG; |
| 3693 | if (msrs.nmsrs >= MAX_IO_MSRS) |
| 3694 | goto out; |
| 3695 | |
| 3696 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
| 3697 | entries = memdup_user(user_msrs->entries, size); |
| 3698 | if (IS_ERR(entries)) { |
| 3699 | r = PTR_ERR(entries); |
| 3700 | goto out; |
| 3701 | } |
| 3702 | |
| 3703 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); |
| 3704 | if (r < 0) |
| 3705 | goto out_free; |
| 3706 | |
| 3707 | r = -EFAULT; |
| 3708 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) |
| 3709 | goto out_free; |
| 3710 | |
| 3711 | r = n; |
| 3712 | |
| 3713 | out_free: |
| 3714 | kfree(entries); |
| 3715 | out: |
| 3716 | return r; |
| 3717 | } |
| 3718 | |
| 3719 | static inline bool kvm_can_mwait_in_guest(void) |
| 3720 | { |
| 3721 | return boot_cpu_has(X86_FEATURE_MWAIT) && |
| 3722 | !boot_cpu_has_bug(X86_BUG_MONITOR) && |
| 3723 | boot_cpu_has(X86_FEATURE_ARAT); |
| 3724 | } |
| 3725 | |
| 3726 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
| 3727 | { |
| 3728 | int r = 0; |
| 3729 | |
| 3730 | switch (ext) { |
| 3731 | case KVM_CAP_IRQCHIP: |
| 3732 | case KVM_CAP_HLT: |
| 3733 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
| 3734 | case KVM_CAP_SET_TSS_ADDR: |
| 3735 | case KVM_CAP_EXT_CPUID: |
| 3736 | case KVM_CAP_EXT_EMUL_CPUID: |
| 3737 | case KVM_CAP_CLOCKSOURCE: |
| 3738 | case KVM_CAP_PIT: |
| 3739 | case KVM_CAP_NOP_IO_DELAY: |
| 3740 | case KVM_CAP_MP_STATE: |
| 3741 | case KVM_CAP_SYNC_MMU: |
| 3742 | case KVM_CAP_USER_NMI: |
| 3743 | case KVM_CAP_REINJECT_CONTROL: |
| 3744 | case KVM_CAP_IRQ_INJECT_STATUS: |
| 3745 | case KVM_CAP_IOEVENTFD: |
| 3746 | case KVM_CAP_IOEVENTFD_NO_LENGTH: |
| 3747 | case KVM_CAP_PIT2: |
| 3748 | case KVM_CAP_PIT_STATE2: |
| 3749 | case KVM_CAP_SET_IDENTITY_MAP_ADDR: |
| 3750 | case KVM_CAP_XEN_HVM: |
| 3751 | case KVM_CAP_VCPU_EVENTS: |
| 3752 | case KVM_CAP_HYPERV: |
| 3753 | case KVM_CAP_HYPERV_VAPIC: |
| 3754 | case KVM_CAP_HYPERV_SPIN: |
| 3755 | case KVM_CAP_HYPERV_SYNIC: |
| 3756 | case KVM_CAP_HYPERV_SYNIC2: |
| 3757 | case KVM_CAP_HYPERV_VP_INDEX: |
| 3758 | case KVM_CAP_HYPERV_EVENTFD: |
| 3759 | case KVM_CAP_HYPERV_TLBFLUSH: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3760 | case KVM_CAP_HYPERV_SEND_IPI: |
| 3761 | case KVM_CAP_HYPERV_CPUID: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3762 | case KVM_CAP_PCI_SEGMENT: |
| 3763 | case KVM_CAP_DEBUGREGS: |
| 3764 | case KVM_CAP_X86_ROBUST_SINGLESTEP: |
| 3765 | case KVM_CAP_XSAVE: |
| 3766 | case KVM_CAP_ASYNC_PF: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3767 | case KVM_CAP_ASYNC_PF_INT: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3768 | case KVM_CAP_GET_TSC_KHZ: |
| 3769 | case KVM_CAP_KVMCLOCK_CTRL: |
| 3770 | case KVM_CAP_READONLY_MEM: |
| 3771 | case KVM_CAP_HYPERV_TIME: |
| 3772 | case KVM_CAP_IOAPIC_POLARITY_IGNORED: |
| 3773 | case KVM_CAP_TSC_DEADLINE_TIMER: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3774 | case KVM_CAP_DISABLE_QUIRKS: |
| 3775 | case KVM_CAP_SET_BOOT_CPU_ID: |
| 3776 | case KVM_CAP_SPLIT_IRQCHIP: |
| 3777 | case KVM_CAP_IMMEDIATE_EXIT: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3778 | case KVM_CAP_PMU_EVENT_FILTER: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3779 | case KVM_CAP_GET_MSR_FEATURES: |
| 3780 | case KVM_CAP_MSR_PLATFORM_INFO: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3781 | case KVM_CAP_EXCEPTION_PAYLOAD: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3782 | case KVM_CAP_SET_GUEST_DEBUG: |
| 3783 | case KVM_CAP_LAST_CPU: |
| 3784 | case KVM_CAP_X86_USER_SPACE_MSR: |
| 3785 | case KVM_CAP_X86_MSR_FILTER: |
| 3786 | case KVM_CAP_ENFORCE_PV_FEATURE_CPUID: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3787 | r = 1; |
| 3788 | break; |
| 3789 | case KVM_CAP_SYNC_REGS: |
| 3790 | r = KVM_SYNC_X86_VALID_FIELDS; |
| 3791 | break; |
| 3792 | case KVM_CAP_ADJUST_CLOCK: |
| 3793 | r = KVM_CLOCK_TSC_STABLE; |
| 3794 | break; |
| 3795 | case KVM_CAP_X86_DISABLE_EXITS: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3796 | r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE | |
| 3797 | KVM_X86_DISABLE_EXITS_CSTATE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3798 | if(kvm_can_mwait_in_guest()) |
| 3799 | r |= KVM_X86_DISABLE_EXITS_MWAIT; |
| 3800 | break; |
| 3801 | case KVM_CAP_X86_SMM: |
| 3802 | /* SMBASE is usually relocated above 1M on modern chipsets, |
| 3803 | * and SMM handlers might indeed rely on 4G segment limits, |
| 3804 | * so do not report SMM to be available if real mode is |
| 3805 | * emulated via vm86 mode. Still, do not go to great lengths |
| 3806 | * to avoid userspace's usage of the feature, because it is a |
| 3807 | * fringe case that is not enabled except via specific settings |
| 3808 | * of the module parameters. |
| 3809 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3810 | r = kvm_x86_ops.has_emulated_msr(MSR_IA32_SMBASE); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3811 | break; |
| 3812 | case KVM_CAP_VAPIC: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3813 | r = !kvm_x86_ops.cpu_has_accelerated_tpr(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3814 | break; |
| 3815 | case KVM_CAP_NR_VCPUS: |
| 3816 | r = KVM_SOFT_MAX_VCPUS; |
| 3817 | break; |
| 3818 | case KVM_CAP_MAX_VCPUS: |
| 3819 | r = KVM_MAX_VCPUS; |
| 3820 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3821 | case KVM_CAP_MAX_VCPU_ID: |
| 3822 | r = KVM_MAX_VCPU_ID; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3823 | break; |
| 3824 | case KVM_CAP_PV_MMU: /* obsolete */ |
| 3825 | r = 0; |
| 3826 | break; |
| 3827 | case KVM_CAP_MCE: |
| 3828 | r = KVM_MAX_MCE_BANKS; |
| 3829 | break; |
| 3830 | case KVM_CAP_XCRS: |
| 3831 | r = boot_cpu_has(X86_FEATURE_XSAVE); |
| 3832 | break; |
| 3833 | case KVM_CAP_TSC_CONTROL: |
| 3834 | r = kvm_has_tsc_control; |
| 3835 | break; |
| 3836 | case KVM_CAP_X2APIC_API: |
| 3837 | r = KVM_X2APIC_API_VALID_FLAGS; |
| 3838 | break; |
| 3839 | case KVM_CAP_NESTED_STATE: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3840 | r = kvm_x86_ops.nested_ops->get_state ? |
| 3841 | kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3842 | break; |
| 3843 | case KVM_CAP_HYPERV_DIRECT_TLBFLUSH: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3844 | r = kvm_x86_ops.enable_direct_tlbflush != NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3845 | break; |
| 3846 | case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3847 | r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; |
| 3848 | break; |
| 3849 | case KVM_CAP_SMALLER_MAXPHYADDR: |
| 3850 | r = (int) allow_smaller_maxphyaddr; |
| 3851 | break; |
| 3852 | case KVM_CAP_STEAL_TIME: |
| 3853 | r = sched_info_on(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3854 | break; |
| 3855 | default: |
| 3856 | break; |
| 3857 | } |
| 3858 | return r; |
| 3859 | |
| 3860 | } |
| 3861 | |
| 3862 | long kvm_arch_dev_ioctl(struct file *filp, |
| 3863 | unsigned int ioctl, unsigned long arg) |
| 3864 | { |
| 3865 | void __user *argp = (void __user *)arg; |
| 3866 | long r; |
| 3867 | |
| 3868 | switch (ioctl) { |
| 3869 | case KVM_GET_MSR_INDEX_LIST: { |
| 3870 | struct kvm_msr_list __user *user_msr_list = argp; |
| 3871 | struct kvm_msr_list msr_list; |
| 3872 | unsigned n; |
| 3873 | |
| 3874 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3875 | if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3876 | goto out; |
| 3877 | n = msr_list.nmsrs; |
| 3878 | msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3879 | if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3880 | goto out; |
| 3881 | r = -E2BIG; |
| 3882 | if (n < msr_list.nmsrs) |
| 3883 | goto out; |
| 3884 | r = -EFAULT; |
| 3885 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, |
| 3886 | num_msrs_to_save * sizeof(u32))) |
| 3887 | goto out; |
| 3888 | if (copy_to_user(user_msr_list->indices + num_msrs_to_save, |
| 3889 | &emulated_msrs, |
| 3890 | num_emulated_msrs * sizeof(u32))) |
| 3891 | goto out; |
| 3892 | r = 0; |
| 3893 | break; |
| 3894 | } |
| 3895 | case KVM_GET_SUPPORTED_CPUID: |
| 3896 | case KVM_GET_EMULATED_CPUID: { |
| 3897 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 3898 | struct kvm_cpuid2 cpuid; |
| 3899 | |
| 3900 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3901 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3902 | goto out; |
| 3903 | |
| 3904 | r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, |
| 3905 | ioctl); |
| 3906 | if (r) |
| 3907 | goto out; |
| 3908 | |
| 3909 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3910 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3911 | goto out; |
| 3912 | r = 0; |
| 3913 | break; |
| 3914 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3915 | case KVM_X86_GET_MCE_CAP_SUPPORTED: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3916 | r = -EFAULT; |
| 3917 | if (copy_to_user(argp, &kvm_mce_cap_supported, |
| 3918 | sizeof(kvm_mce_cap_supported))) |
| 3919 | goto out; |
| 3920 | r = 0; |
| 3921 | break; |
| 3922 | case KVM_GET_MSR_FEATURE_INDEX_LIST: { |
| 3923 | struct kvm_msr_list __user *user_msr_list = argp; |
| 3924 | struct kvm_msr_list msr_list; |
| 3925 | unsigned int n; |
| 3926 | |
| 3927 | r = -EFAULT; |
| 3928 | if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) |
| 3929 | goto out; |
| 3930 | n = msr_list.nmsrs; |
| 3931 | msr_list.nmsrs = num_msr_based_features; |
| 3932 | if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) |
| 3933 | goto out; |
| 3934 | r = -E2BIG; |
| 3935 | if (n < msr_list.nmsrs) |
| 3936 | goto out; |
| 3937 | r = -EFAULT; |
| 3938 | if (copy_to_user(user_msr_list->indices, &msr_based_features, |
| 3939 | num_msr_based_features * sizeof(u32))) |
| 3940 | goto out; |
| 3941 | r = 0; |
| 3942 | break; |
| 3943 | } |
| 3944 | case KVM_GET_MSRS: |
| 3945 | r = msr_io(NULL, argp, do_get_msr_feature, 1); |
| 3946 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3947 | default: |
| 3948 | r = -EINVAL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3949 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3950 | } |
| 3951 | out: |
| 3952 | return r; |
| 3953 | } |
| 3954 | |
| 3955 | static void wbinvd_ipi(void *garbage) |
| 3956 | { |
| 3957 | wbinvd(); |
| 3958 | } |
| 3959 | |
| 3960 | static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu) |
| 3961 | { |
| 3962 | return kvm_arch_has_noncoherent_dma(vcpu->kvm); |
| 3963 | } |
| 3964 | |
| 3965 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 3966 | { |
| 3967 | /* Address WBINVD may be executed by guest */ |
| 3968 | if (need_emulate_wbinvd(vcpu)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3969 | if (kvm_x86_ops.has_wbinvd_exit()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3970 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); |
| 3971 | else if (vcpu->cpu != -1 && vcpu->cpu != cpu) |
| 3972 | smp_call_function_single(vcpu->cpu, |
| 3973 | wbinvd_ipi, NULL, 1); |
| 3974 | } |
| 3975 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3976 | kvm_x86_ops.vcpu_load(vcpu, cpu); |
| 3977 | |
| 3978 | /* Save host pkru register if supported */ |
| 3979 | vcpu->arch.host_pkru = read_pkru(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3980 | |
| 3981 | /* Apply any externally detected TSC adjustments (due to suspend) */ |
| 3982 | if (unlikely(vcpu->arch.tsc_offset_adjustment)) { |
| 3983 | adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); |
| 3984 | vcpu->arch.tsc_offset_adjustment = 0; |
| 3985 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
| 3986 | } |
| 3987 | |
| 3988 | if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { |
| 3989 | s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : |
| 3990 | rdtsc() - vcpu->arch.last_host_tsc; |
| 3991 | if (tsc_delta < 0) |
| 3992 | mark_tsc_unstable("KVM discovered backwards TSC"); |
| 3993 | |
| 3994 | if (kvm_check_tsc_unstable()) { |
| 3995 | u64 offset = kvm_compute_tsc_offset(vcpu, |
| 3996 | vcpu->arch.last_guest_tsc); |
| 3997 | kvm_vcpu_write_tsc_offset(vcpu, offset); |
| 3998 | vcpu->arch.tsc_catchup = 1; |
| 3999 | } |
| 4000 | |
| 4001 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 4002 | kvm_lapic_restart_hv_timer(vcpu); |
| 4003 | |
| 4004 | /* |
| 4005 | * On a host with synchronized TSC, there is no need to update |
| 4006 | * kvmclock on vcpu->cpu migration |
| 4007 | */ |
| 4008 | if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) |
| 4009 | kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); |
| 4010 | if (vcpu->cpu != cpu) |
| 4011 | kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu); |
| 4012 | vcpu->cpu = cpu; |
| 4013 | } |
| 4014 | |
| 4015 | kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); |
| 4016 | } |
| 4017 | |
| 4018 | static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) |
| 4019 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 4020 | struct kvm_host_map map; |
| 4021 | struct kvm_steal_time *st; |
| 4022 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4023 | if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) |
| 4024 | return; |
| 4025 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 4026 | if (vcpu->arch.st.preempted) |
| 4027 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4028 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 4029 | if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map, |
| 4030 | &vcpu->arch.st.cache, true)) |
| 4031 | return; |
| 4032 | |
| 4033 | st = map.hva + |
| 4034 | offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS); |
| 4035 | |
| 4036 | st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; |
| 4037 | |
| 4038 | kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
| 4041 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 4042 | { |
| 4043 | int idx; |
| 4044 | |
| 4045 | if (vcpu->preempted) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4046 | vcpu->arch.preempted_in_kernel = !kvm_x86_ops.get_cpl(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4047 | |
| 4048 | /* |
| 4049 | * Disable page faults because we're in atomic context here. |
| 4050 | * kvm_write_guest_offset_cached() would call might_fault() |
| 4051 | * that relies on pagefault_disable() to tell if there's a |
| 4052 | * bug. NOTE: the write to guest memory may not go through if |
| 4053 | * during postcopy live migration or if there's heavy guest |
| 4054 | * paging. |
| 4055 | */ |
| 4056 | pagefault_disable(); |
| 4057 | /* |
| 4058 | * kvm_memslots() will be called by |
| 4059 | * kvm_write_guest_offset_cached() so take the srcu lock. |
| 4060 | */ |
| 4061 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 4062 | kvm_steal_time_set_preempted(vcpu); |
| 4063 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 4064 | pagefault_enable(); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4065 | kvm_x86_ops.vcpu_put(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4066 | vcpu->arch.last_host_tsc = rdtsc(); |
| 4067 | /* |
| 4068 | * If userspace has set any breakpoints or watchpoints, dr6 is restored |
| 4069 | * on every vmexit, but if not, we might have a stale dr6 from the |
| 4070 | * guest. do_debug expects dr6 to be cleared after it runs, do the same. |
| 4071 | */ |
| 4072 | set_debugreg(0, 6); |
| 4073 | } |
| 4074 | |
| 4075 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
| 4076 | struct kvm_lapic_state *s) |
| 4077 | { |
| 4078 | if (vcpu->arch.apicv_active) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4079 | kvm_x86_ops.sync_pir_to_irr(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4080 | |
| 4081 | return kvm_apic_get_state(vcpu, s); |
| 4082 | } |
| 4083 | |
| 4084 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, |
| 4085 | struct kvm_lapic_state *s) |
| 4086 | { |
| 4087 | int r; |
| 4088 | |
| 4089 | r = kvm_apic_set_state(vcpu, s); |
| 4090 | if (r) |
| 4091 | return r; |
| 4092 | update_cr8_intercept(vcpu); |
| 4093 | |
| 4094 | return 0; |
| 4095 | } |
| 4096 | |
| 4097 | static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu) |
| 4098 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 4099 | /* |
| 4100 | * We can accept userspace's request for interrupt injection |
| 4101 | * as long as we have a place to store the interrupt number. |
| 4102 | * The actual injection will happen when the CPU is able to |
| 4103 | * deliver the interrupt. |
| 4104 | */ |
| 4105 | if (kvm_cpu_has_extint(vcpu)) |
| 4106 | return false; |
| 4107 | |
| 4108 | /* Acknowledging ExtINT does not happen if LINT0 is masked. */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4109 | return (!lapic_in_kernel(vcpu) || |
| 4110 | kvm_apic_accept_pic_intr(vcpu)); |
| 4111 | } |
| 4112 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4113 | static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu) |
| 4114 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 4115 | /* |
| 4116 | * Do not cause an interrupt window exit if an exception |
| 4117 | * is pending or an event needs reinjection; userspace |
| 4118 | * might want to inject the interrupt manually using KVM_SET_REGS |
| 4119 | * or KVM_SET_SREGS. For that to work, we must be at an |
| 4120 | * instruction boundary and with no events half-injected. |
| 4121 | */ |
| 4122 | return (kvm_arch_interrupt_allowed(vcpu) && |
| 4123 | kvm_cpu_accept_dm_intr(vcpu) && |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4124 | !kvm_event_needs_reinjection(vcpu) && |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 4125 | !vcpu->arch.exception.pending); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 4129 | struct kvm_interrupt *irq) |
| 4130 | { |
| 4131 | if (irq->irq >= KVM_NR_INTERRUPTS) |
| 4132 | return -EINVAL; |
| 4133 | |
| 4134 | if (!irqchip_in_kernel(vcpu->kvm)) { |
| 4135 | kvm_queue_interrupt(vcpu, irq->irq, false); |
| 4136 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4137 | return 0; |
| 4138 | } |
| 4139 | |
| 4140 | /* |
| 4141 | * With in-kernel LAPIC, we only use this to inject EXTINT, so |
| 4142 | * fail for in-kernel 8259. |
| 4143 | */ |
| 4144 | if (pic_in_kernel(vcpu->kvm)) |
| 4145 | return -ENXIO; |
| 4146 | |
| 4147 | if (vcpu->arch.pending_external_vector != -1) |
| 4148 | return -EEXIST; |
| 4149 | |
| 4150 | vcpu->arch.pending_external_vector = irq->irq; |
| 4151 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4152 | return 0; |
| 4153 | } |
| 4154 | |
| 4155 | static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu) |
| 4156 | { |
| 4157 | kvm_inject_nmi(vcpu); |
| 4158 | |
| 4159 | return 0; |
| 4160 | } |
| 4161 | |
| 4162 | static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu) |
| 4163 | { |
| 4164 | kvm_make_request(KVM_REQ_SMI, vcpu); |
| 4165 | |
| 4166 | return 0; |
| 4167 | } |
| 4168 | |
| 4169 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
| 4170 | struct kvm_tpr_access_ctl *tac) |
| 4171 | { |
| 4172 | if (tac->flags) |
| 4173 | return -EINVAL; |
| 4174 | vcpu->arch.tpr_access_reporting = !!tac->enabled; |
| 4175 | return 0; |
| 4176 | } |
| 4177 | |
| 4178 | static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu, |
| 4179 | u64 mcg_cap) |
| 4180 | { |
| 4181 | int r; |
| 4182 | unsigned bank_num = mcg_cap & 0xff, bank; |
| 4183 | |
| 4184 | r = -EINVAL; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 4185 | if (!bank_num || bank_num > KVM_MAX_MCE_BANKS) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4186 | goto out; |
| 4187 | if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000)) |
| 4188 | goto out; |
| 4189 | r = 0; |
| 4190 | vcpu->arch.mcg_cap = mcg_cap; |
| 4191 | /* Init IA32_MCG_CTL to all 1s */ |
| 4192 | if (mcg_cap & MCG_CTL_P) |
| 4193 | vcpu->arch.mcg_ctl = ~(u64)0; |
| 4194 | /* Init IA32_MCi_CTL to all 1s */ |
| 4195 | for (bank = 0; bank < bank_num; bank++) |
| 4196 | vcpu->arch.mce_banks[bank*4] = ~(u64)0; |
| 4197 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4198 | kvm_x86_ops.setup_mce(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4199 | out: |
| 4200 | return r; |
| 4201 | } |
| 4202 | |
| 4203 | static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu, |
| 4204 | struct kvm_x86_mce *mce) |
| 4205 | { |
| 4206 | u64 mcg_cap = vcpu->arch.mcg_cap; |
| 4207 | unsigned bank_num = mcg_cap & 0xff; |
| 4208 | u64 *banks = vcpu->arch.mce_banks; |
| 4209 | |
| 4210 | if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) |
| 4211 | return -EINVAL; |
| 4212 | /* |
| 4213 | * if IA32_MCG_CTL is not all 1s, the uncorrected error |
| 4214 | * reporting is disabled |
| 4215 | */ |
| 4216 | if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && |
| 4217 | vcpu->arch.mcg_ctl != ~(u64)0) |
| 4218 | return 0; |
| 4219 | banks += 4 * mce->bank; |
| 4220 | /* |
| 4221 | * if IA32_MCi_CTL is not all 1s, the uncorrected error |
| 4222 | * reporting is disabled for the bank |
| 4223 | */ |
| 4224 | if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) |
| 4225 | return 0; |
| 4226 | if (mce->status & MCI_STATUS_UC) { |
| 4227 | if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || |
| 4228 | !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) { |
| 4229 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 4230 | return 0; |
| 4231 | } |
| 4232 | if (banks[1] & MCI_STATUS_VAL) |
| 4233 | mce->status |= MCI_STATUS_OVER; |
| 4234 | banks[2] = mce->addr; |
| 4235 | banks[3] = mce->misc; |
| 4236 | vcpu->arch.mcg_status = mce->mcg_status; |
| 4237 | banks[1] = mce->status; |
| 4238 | kvm_queue_exception(vcpu, MC_VECTOR); |
| 4239 | } else if (!(banks[1] & MCI_STATUS_VAL) |
| 4240 | || !(banks[1] & MCI_STATUS_UC)) { |
| 4241 | if (banks[1] & MCI_STATUS_VAL) |
| 4242 | mce->status |= MCI_STATUS_OVER; |
| 4243 | banks[2] = mce->addr; |
| 4244 | banks[3] = mce->misc; |
| 4245 | banks[1] = mce->status; |
| 4246 | } else |
| 4247 | banks[1] |= MCI_STATUS_OVER; |
| 4248 | return 0; |
| 4249 | } |
| 4250 | |
| 4251 | static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, |
| 4252 | struct kvm_vcpu_events *events) |
| 4253 | { |
| 4254 | process_nmi(vcpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4255 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 4256 | if (kvm_check_request(KVM_REQ_SMI, vcpu)) |
| 4257 | process_smi(vcpu); |
| 4258 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4259 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4260 | * In guest mode, payload delivery should be deferred, |
| 4261 | * so that the L1 hypervisor can intercept #PF before |
| 4262 | * CR2 is modified (or intercept #DB before DR6 is |
| 4263 | * modified under nVMX). Unless the per-VM capability, |
| 4264 | * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of |
| 4265 | * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we |
| 4266 | * opportunistically defer the exception payload, deliver it if the |
| 4267 | * capability hasn't been requested before processing a |
| 4268 | * KVM_GET_VCPU_EVENTS. |
| 4269 | */ |
| 4270 | if (!vcpu->kvm->arch.exception_payload_enabled && |
| 4271 | vcpu->arch.exception.pending && vcpu->arch.exception.has_payload) |
| 4272 | kvm_deliver_exception_payload(vcpu); |
| 4273 | |
| 4274 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4275 | * The API doesn't provide the instruction length for software |
| 4276 | * exceptions, so don't report them. As long as the guest RIP |
| 4277 | * isn't advanced, we should expect to encounter the exception |
| 4278 | * again. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4279 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4280 | if (kvm_exception_is_soft(vcpu->arch.exception.nr)) { |
| 4281 | events->exception.injected = 0; |
| 4282 | events->exception.pending = 0; |
| 4283 | } else { |
| 4284 | events->exception.injected = vcpu->arch.exception.injected; |
| 4285 | events->exception.pending = vcpu->arch.exception.pending; |
| 4286 | /* |
| 4287 | * For ABI compatibility, deliberately conflate |
| 4288 | * pending and injected exceptions when |
| 4289 | * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled. |
| 4290 | */ |
| 4291 | if (!vcpu->kvm->arch.exception_payload_enabled) |
| 4292 | events->exception.injected |= |
| 4293 | vcpu->arch.exception.pending; |
| 4294 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4295 | events->exception.nr = vcpu->arch.exception.nr; |
| 4296 | events->exception.has_error_code = vcpu->arch.exception.has_error_code; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4297 | events->exception.error_code = vcpu->arch.exception.error_code; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4298 | events->exception_has_payload = vcpu->arch.exception.has_payload; |
| 4299 | events->exception_payload = vcpu->arch.exception.payload; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4300 | |
| 4301 | events->interrupt.injected = |
| 4302 | vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; |
| 4303 | events->interrupt.nr = vcpu->arch.interrupt.nr; |
| 4304 | events->interrupt.soft = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4305 | events->interrupt.shadow = kvm_x86_ops.get_interrupt_shadow(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4306 | |
| 4307 | events->nmi.injected = vcpu->arch.nmi_injected; |
| 4308 | events->nmi.pending = vcpu->arch.nmi_pending != 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4309 | events->nmi.masked = kvm_x86_ops.get_nmi_mask(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4310 | events->nmi.pad = 0; |
| 4311 | |
| 4312 | events->sipi_vector = 0; /* never valid when reporting to user space */ |
| 4313 | |
| 4314 | events->smi.smm = is_smm(vcpu); |
| 4315 | events->smi.pending = vcpu->arch.smi_pending; |
| 4316 | events->smi.smm_inside_nmi = |
| 4317 | !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); |
| 4318 | events->smi.latched_init = kvm_lapic_latched_init(vcpu); |
| 4319 | |
| 4320 | events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |
| 4321 | | KVM_VCPUEVENT_VALID_SHADOW |
| 4322 | | KVM_VCPUEVENT_VALID_SMM); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4323 | if (vcpu->kvm->arch.exception_payload_enabled) |
| 4324 | events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; |
| 4325 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4326 | memset(&events->reserved, 0, sizeof(events->reserved)); |
| 4327 | } |
| 4328 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4329 | static void kvm_smm_changed(struct kvm_vcpu *vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4330 | |
| 4331 | static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, |
| 4332 | struct kvm_vcpu_events *events) |
| 4333 | { |
| 4334 | if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING |
| 4335 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
| 4336 | | KVM_VCPUEVENT_VALID_SHADOW |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4337 | | KVM_VCPUEVENT_VALID_SMM |
| 4338 | | KVM_VCPUEVENT_VALID_PAYLOAD)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4339 | return -EINVAL; |
| 4340 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4341 | if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { |
| 4342 | if (!vcpu->kvm->arch.exception_payload_enabled) |
| 4343 | return -EINVAL; |
| 4344 | if (events->exception.pending) |
| 4345 | events->exception.injected = 0; |
| 4346 | else |
| 4347 | events->exception_has_payload = 0; |
| 4348 | } else { |
| 4349 | events->exception.pending = 0; |
| 4350 | events->exception_has_payload = 0; |
| 4351 | } |
| 4352 | |
| 4353 | if ((events->exception.injected || events->exception.pending) && |
| 4354 | (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4355 | return -EINVAL; |
| 4356 | |
| 4357 | /* INITs are latched while in SMM */ |
| 4358 | if (events->flags & KVM_VCPUEVENT_VALID_SMM && |
| 4359 | (events->smi.smm || events->smi.pending) && |
| 4360 | vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 4361 | return -EINVAL; |
| 4362 | |
| 4363 | process_nmi(vcpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4364 | vcpu->arch.exception.injected = events->exception.injected; |
| 4365 | vcpu->arch.exception.pending = events->exception.pending; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4366 | vcpu->arch.exception.nr = events->exception.nr; |
| 4367 | vcpu->arch.exception.has_error_code = events->exception.has_error_code; |
| 4368 | vcpu->arch.exception.error_code = events->exception.error_code; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4369 | vcpu->arch.exception.has_payload = events->exception_has_payload; |
| 4370 | vcpu->arch.exception.payload = events->exception_payload; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4371 | |
| 4372 | vcpu->arch.interrupt.injected = events->interrupt.injected; |
| 4373 | vcpu->arch.interrupt.nr = events->interrupt.nr; |
| 4374 | vcpu->arch.interrupt.soft = events->interrupt.soft; |
| 4375 | if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4376 | kvm_x86_ops.set_interrupt_shadow(vcpu, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4377 | events->interrupt.shadow); |
| 4378 | |
| 4379 | vcpu->arch.nmi_injected = events->nmi.injected; |
| 4380 | if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) |
| 4381 | vcpu->arch.nmi_pending = events->nmi.pending; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4382 | kvm_x86_ops.set_nmi_mask(vcpu, events->nmi.masked); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4383 | |
| 4384 | if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && |
| 4385 | lapic_in_kernel(vcpu)) |
| 4386 | vcpu->arch.apic->sipi_vector = events->sipi_vector; |
| 4387 | |
| 4388 | if (events->flags & KVM_VCPUEVENT_VALID_SMM) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4389 | if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { |
| 4390 | if (events->smi.smm) |
| 4391 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 4392 | else |
| 4393 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4394 | |
| 4395 | kvm_x86_ops.nested_ops->leave_nested(vcpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4396 | kvm_smm_changed(vcpu); |
| 4397 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4398 | |
| 4399 | vcpu->arch.smi_pending = events->smi.pending; |
| 4400 | |
| 4401 | if (events->smi.smm) { |
| 4402 | if (events->smi.smm_inside_nmi) |
| 4403 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; |
| 4404 | else |
| 4405 | vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4406 | } |
| 4407 | |
| 4408 | if (lapic_in_kernel(vcpu)) { |
| 4409 | if (events->smi.latched_init) |
| 4410 | set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); |
| 4411 | else |
| 4412 | clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4417 | |
| 4418 | return 0; |
| 4419 | } |
| 4420 | |
| 4421 | static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu, |
| 4422 | struct kvm_debugregs *dbgregs) |
| 4423 | { |
| 4424 | unsigned long val; |
| 4425 | |
| 4426 | memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); |
| 4427 | kvm_get_dr(vcpu, 6, &val); |
| 4428 | dbgregs->dr6 = val; |
| 4429 | dbgregs->dr7 = vcpu->arch.dr7; |
| 4430 | dbgregs->flags = 0; |
| 4431 | memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved)); |
| 4432 | } |
| 4433 | |
| 4434 | static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, |
| 4435 | struct kvm_debugregs *dbgregs) |
| 4436 | { |
| 4437 | if (dbgregs->flags) |
| 4438 | return -EINVAL; |
| 4439 | |
| 4440 | if (dbgregs->dr6 & ~0xffffffffull) |
| 4441 | return -EINVAL; |
| 4442 | if (dbgregs->dr7 & ~0xffffffffull) |
| 4443 | return -EINVAL; |
| 4444 | |
| 4445 | memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); |
| 4446 | kvm_update_dr0123(vcpu); |
| 4447 | vcpu->arch.dr6 = dbgregs->dr6; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4448 | vcpu->arch.dr7 = dbgregs->dr7; |
| 4449 | kvm_update_dr7(vcpu); |
| 4450 | |
| 4451 | return 0; |
| 4452 | } |
| 4453 | |
| 4454 | #define XSTATE_COMPACTION_ENABLED (1ULL << 63) |
| 4455 | |
| 4456 | static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu) |
| 4457 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4458 | struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4459 | u64 xstate_bv = xsave->header.xfeatures; |
| 4460 | u64 valid; |
| 4461 | |
| 4462 | /* |
| 4463 | * Copy legacy XSAVE area, to avoid complications with CPUID |
| 4464 | * leaves 0 and 1 in the loop below. |
| 4465 | */ |
| 4466 | memcpy(dest, xsave, XSAVE_HDR_OFFSET); |
| 4467 | |
| 4468 | /* Set XSTATE_BV */ |
| 4469 | xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE; |
| 4470 | *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv; |
| 4471 | |
| 4472 | /* |
| 4473 | * Copy each region from the possibly compacted offset to the |
| 4474 | * non-compacted offset. |
| 4475 | */ |
| 4476 | valid = xstate_bv & ~XFEATURE_MASK_FPSSE; |
| 4477 | while (valid) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4478 | u64 xfeature_mask = valid & -valid; |
| 4479 | int xfeature_nr = fls64(xfeature_mask) - 1; |
| 4480 | void *src = get_xsave_addr(xsave, xfeature_nr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4481 | |
| 4482 | if (src) { |
| 4483 | u32 size, offset, ecx, edx; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4484 | cpuid_count(XSTATE_CPUID, xfeature_nr, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4485 | &size, &offset, &ecx, &edx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4486 | if (xfeature_nr == XFEATURE_PKRU) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4487 | memcpy(dest + offset, &vcpu->arch.pkru, |
| 4488 | sizeof(vcpu->arch.pkru)); |
| 4489 | else |
| 4490 | memcpy(dest + offset, src, size); |
| 4491 | |
| 4492 | } |
| 4493 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4494 | valid -= xfeature_mask; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4495 | } |
| 4496 | } |
| 4497 | |
| 4498 | static void load_xsave(struct kvm_vcpu *vcpu, u8 *src) |
| 4499 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4500 | struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4501 | u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET); |
| 4502 | u64 valid; |
| 4503 | |
| 4504 | /* |
| 4505 | * Copy legacy XSAVE area, to avoid complications with CPUID |
| 4506 | * leaves 0 and 1 in the loop below. |
| 4507 | */ |
| 4508 | memcpy(xsave, src, XSAVE_HDR_OFFSET); |
| 4509 | |
| 4510 | /* Set XSTATE_BV and possibly XCOMP_BV. */ |
| 4511 | xsave->header.xfeatures = xstate_bv; |
| 4512 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
| 4513 | xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED; |
| 4514 | |
| 4515 | /* |
| 4516 | * Copy each region from the non-compacted offset to the |
| 4517 | * possibly compacted offset. |
| 4518 | */ |
| 4519 | valid = xstate_bv & ~XFEATURE_MASK_FPSSE; |
| 4520 | while (valid) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4521 | u64 xfeature_mask = valid & -valid; |
| 4522 | int xfeature_nr = fls64(xfeature_mask) - 1; |
| 4523 | void *dest = get_xsave_addr(xsave, xfeature_nr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4524 | |
| 4525 | if (dest) { |
| 4526 | u32 size, offset, ecx, edx; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4527 | cpuid_count(XSTATE_CPUID, xfeature_nr, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4528 | &size, &offset, &ecx, &edx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4529 | if (xfeature_nr == XFEATURE_PKRU) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4530 | memcpy(&vcpu->arch.pkru, src + offset, |
| 4531 | sizeof(vcpu->arch.pkru)); |
| 4532 | else |
| 4533 | memcpy(dest, src + offset, size); |
| 4534 | } |
| 4535 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4536 | valid -= xfeature_mask; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4537 | } |
| 4538 | } |
| 4539 | |
| 4540 | static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu, |
| 4541 | struct kvm_xsave *guest_xsave) |
| 4542 | { |
| 4543 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
| 4544 | memset(guest_xsave, 0, sizeof(struct kvm_xsave)); |
| 4545 | fill_xsave((u8 *) guest_xsave->region, vcpu); |
| 4546 | } else { |
| 4547 | memcpy(guest_xsave->region, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4548 | &vcpu->arch.guest_fpu->state.fxsave, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4549 | sizeof(struct fxregs_state)); |
| 4550 | *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] = |
| 4551 | XFEATURE_MASK_FPSSE; |
| 4552 | } |
| 4553 | } |
| 4554 | |
| 4555 | #define XSAVE_MXCSR_OFFSET 24 |
| 4556 | |
| 4557 | static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, |
| 4558 | struct kvm_xsave *guest_xsave) |
| 4559 | { |
| 4560 | u64 xstate_bv = |
| 4561 | *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)]; |
| 4562 | u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)]; |
| 4563 | |
| 4564 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
| 4565 | /* |
| 4566 | * Here we allow setting states that are not present in |
| 4567 | * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility |
| 4568 | * with old userspace. |
| 4569 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4570 | if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4571 | return -EINVAL; |
| 4572 | load_xsave(vcpu, (u8 *)guest_xsave->region); |
| 4573 | } else { |
| 4574 | if (xstate_bv & ~XFEATURE_MASK_FPSSE || |
| 4575 | mxcsr & ~mxcsr_feature_mask) |
| 4576 | return -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4577 | memcpy(&vcpu->arch.guest_fpu->state.fxsave, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4578 | guest_xsave->region, sizeof(struct fxregs_state)); |
| 4579 | } |
| 4580 | return 0; |
| 4581 | } |
| 4582 | |
| 4583 | static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu, |
| 4584 | struct kvm_xcrs *guest_xcrs) |
| 4585 | { |
| 4586 | if (!boot_cpu_has(X86_FEATURE_XSAVE)) { |
| 4587 | guest_xcrs->nr_xcrs = 0; |
| 4588 | return; |
| 4589 | } |
| 4590 | |
| 4591 | guest_xcrs->nr_xcrs = 1; |
| 4592 | guest_xcrs->flags = 0; |
| 4593 | guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; |
| 4594 | guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; |
| 4595 | } |
| 4596 | |
| 4597 | static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu, |
| 4598 | struct kvm_xcrs *guest_xcrs) |
| 4599 | { |
| 4600 | int i, r = 0; |
| 4601 | |
| 4602 | if (!boot_cpu_has(X86_FEATURE_XSAVE)) |
| 4603 | return -EINVAL; |
| 4604 | |
| 4605 | if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) |
| 4606 | return -EINVAL; |
| 4607 | |
| 4608 | for (i = 0; i < guest_xcrs->nr_xcrs; i++) |
| 4609 | /* Only support XCR0 currently */ |
| 4610 | if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { |
| 4611 | r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK, |
| 4612 | guest_xcrs->xcrs[i].value); |
| 4613 | break; |
| 4614 | } |
| 4615 | if (r) |
| 4616 | r = -EINVAL; |
| 4617 | return r; |
| 4618 | } |
| 4619 | |
| 4620 | /* |
| 4621 | * kvm_set_guest_paused() indicates to the guest kernel that it has been |
| 4622 | * stopped by the hypervisor. This function will be called from the host only. |
| 4623 | * EINVAL is returned when the host attempts to set the flag for a guest that |
| 4624 | * does not support pv clocks. |
| 4625 | */ |
| 4626 | static int kvm_set_guest_paused(struct kvm_vcpu *vcpu) |
| 4627 | { |
| 4628 | if (!vcpu->arch.pv_time_enabled) |
| 4629 | return -EINVAL; |
| 4630 | vcpu->arch.pvclock_set_guest_stopped_request = true; |
| 4631 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
| 4632 | return 0; |
| 4633 | } |
| 4634 | |
| 4635 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
| 4636 | struct kvm_enable_cap *cap) |
| 4637 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4638 | int r; |
| 4639 | uint16_t vmcs_version; |
| 4640 | void __user *user_ptr; |
| 4641 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4642 | if (cap->flags) |
| 4643 | return -EINVAL; |
| 4644 | |
| 4645 | switch (cap->cap) { |
| 4646 | case KVM_CAP_HYPERV_SYNIC2: |
| 4647 | if (cap->args[0]) |
| 4648 | return -EINVAL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4649 | fallthrough; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4650 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4651 | case KVM_CAP_HYPERV_SYNIC: |
| 4652 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 4653 | return -EINVAL; |
| 4654 | return kvm_hv_activate_synic(vcpu, cap->cap == |
| 4655 | KVM_CAP_HYPERV_SYNIC2); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4656 | case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4657 | if (!kvm_x86_ops.nested_ops->enable_evmcs) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4658 | return -ENOTTY; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4659 | r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4660 | if (!r) { |
| 4661 | user_ptr = (void __user *)(uintptr_t)cap->args[0]; |
| 4662 | if (copy_to_user(user_ptr, &vmcs_version, |
| 4663 | sizeof(vmcs_version))) |
| 4664 | r = -EFAULT; |
| 4665 | } |
| 4666 | return r; |
| 4667 | case KVM_CAP_HYPERV_DIRECT_TLBFLUSH: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4668 | if (!kvm_x86_ops.enable_direct_tlbflush) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4669 | return -ENOTTY; |
| 4670 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4671 | return kvm_x86_ops.enable_direct_tlbflush(vcpu); |
| 4672 | |
| 4673 | case KVM_CAP_ENFORCE_PV_FEATURE_CPUID: |
| 4674 | vcpu->arch.pv_cpuid.enforce = cap->args[0]; |
| 4675 | if (vcpu->arch.pv_cpuid.enforce) |
| 4676 | kvm_update_pv_runtime(vcpu); |
| 4677 | |
| 4678 | return 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4679 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4680 | default: |
| 4681 | return -EINVAL; |
| 4682 | } |
| 4683 | } |
| 4684 | |
| 4685 | long kvm_arch_vcpu_ioctl(struct file *filp, |
| 4686 | unsigned int ioctl, unsigned long arg) |
| 4687 | { |
| 4688 | struct kvm_vcpu *vcpu = filp->private_data; |
| 4689 | void __user *argp = (void __user *)arg; |
| 4690 | int r; |
| 4691 | union { |
| 4692 | struct kvm_lapic_state *lapic; |
| 4693 | struct kvm_xsave *xsave; |
| 4694 | struct kvm_xcrs *xcrs; |
| 4695 | void *buffer; |
| 4696 | } u; |
| 4697 | |
| 4698 | vcpu_load(vcpu); |
| 4699 | |
| 4700 | u.buffer = NULL; |
| 4701 | switch (ioctl) { |
| 4702 | case KVM_GET_LAPIC: { |
| 4703 | r = -EINVAL; |
| 4704 | if (!lapic_in_kernel(vcpu)) |
| 4705 | goto out; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4706 | u.lapic = kzalloc(sizeof(struct kvm_lapic_state), |
| 4707 | GFP_KERNEL_ACCOUNT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4708 | |
| 4709 | r = -ENOMEM; |
| 4710 | if (!u.lapic) |
| 4711 | goto out; |
| 4712 | r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic); |
| 4713 | if (r) |
| 4714 | goto out; |
| 4715 | r = -EFAULT; |
| 4716 | if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state))) |
| 4717 | goto out; |
| 4718 | r = 0; |
| 4719 | break; |
| 4720 | } |
| 4721 | case KVM_SET_LAPIC: { |
| 4722 | r = -EINVAL; |
| 4723 | if (!lapic_in_kernel(vcpu)) |
| 4724 | goto out; |
| 4725 | u.lapic = memdup_user(argp, sizeof(*u.lapic)); |
| 4726 | if (IS_ERR(u.lapic)) { |
| 4727 | r = PTR_ERR(u.lapic); |
| 4728 | goto out_nofree; |
| 4729 | } |
| 4730 | |
| 4731 | r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic); |
| 4732 | break; |
| 4733 | } |
| 4734 | case KVM_INTERRUPT: { |
| 4735 | struct kvm_interrupt irq; |
| 4736 | |
| 4737 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4738 | if (copy_from_user(&irq, argp, sizeof(irq))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4739 | goto out; |
| 4740 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
| 4741 | break; |
| 4742 | } |
| 4743 | case KVM_NMI: { |
| 4744 | r = kvm_vcpu_ioctl_nmi(vcpu); |
| 4745 | break; |
| 4746 | } |
| 4747 | case KVM_SMI: { |
| 4748 | r = kvm_vcpu_ioctl_smi(vcpu); |
| 4749 | break; |
| 4750 | } |
| 4751 | case KVM_SET_CPUID: { |
| 4752 | struct kvm_cpuid __user *cpuid_arg = argp; |
| 4753 | struct kvm_cpuid cpuid; |
| 4754 | |
| 4755 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4756 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4757 | goto out; |
| 4758 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); |
| 4759 | break; |
| 4760 | } |
| 4761 | case KVM_SET_CPUID2: { |
| 4762 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 4763 | struct kvm_cpuid2 cpuid; |
| 4764 | |
| 4765 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4766 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4767 | goto out; |
| 4768 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, |
| 4769 | cpuid_arg->entries); |
| 4770 | break; |
| 4771 | } |
| 4772 | case KVM_GET_CPUID2: { |
| 4773 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 4774 | struct kvm_cpuid2 cpuid; |
| 4775 | |
| 4776 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4777 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4778 | goto out; |
| 4779 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, |
| 4780 | cpuid_arg->entries); |
| 4781 | if (r) |
| 4782 | goto out; |
| 4783 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4784 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4785 | goto out; |
| 4786 | r = 0; |
| 4787 | break; |
| 4788 | } |
| 4789 | case KVM_GET_MSRS: { |
| 4790 | int idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 4791 | r = msr_io(vcpu, argp, do_get_msr, 1); |
| 4792 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 4793 | break; |
| 4794 | } |
| 4795 | case KVM_SET_MSRS: { |
| 4796 | int idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 4797 | r = msr_io(vcpu, argp, do_set_msr, 0); |
| 4798 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 4799 | break; |
| 4800 | } |
| 4801 | case KVM_TPR_ACCESS_REPORTING: { |
| 4802 | struct kvm_tpr_access_ctl tac; |
| 4803 | |
| 4804 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4805 | if (copy_from_user(&tac, argp, sizeof(tac))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4806 | goto out; |
| 4807 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); |
| 4808 | if (r) |
| 4809 | goto out; |
| 4810 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4811 | if (copy_to_user(argp, &tac, sizeof(tac))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4812 | goto out; |
| 4813 | r = 0; |
| 4814 | break; |
| 4815 | }; |
| 4816 | case KVM_SET_VAPIC_ADDR: { |
| 4817 | struct kvm_vapic_addr va; |
| 4818 | int idx; |
| 4819 | |
| 4820 | r = -EINVAL; |
| 4821 | if (!lapic_in_kernel(vcpu)) |
| 4822 | goto out; |
| 4823 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4824 | if (copy_from_user(&va, argp, sizeof(va))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4825 | goto out; |
| 4826 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 4827 | r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); |
| 4828 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 4829 | break; |
| 4830 | } |
| 4831 | case KVM_X86_SETUP_MCE: { |
| 4832 | u64 mcg_cap; |
| 4833 | |
| 4834 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4835 | if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4836 | goto out; |
| 4837 | r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap); |
| 4838 | break; |
| 4839 | } |
| 4840 | case KVM_X86_SET_MCE: { |
| 4841 | struct kvm_x86_mce mce; |
| 4842 | |
| 4843 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4844 | if (copy_from_user(&mce, argp, sizeof(mce))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4845 | goto out; |
| 4846 | r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce); |
| 4847 | break; |
| 4848 | } |
| 4849 | case KVM_GET_VCPU_EVENTS: { |
| 4850 | struct kvm_vcpu_events events; |
| 4851 | |
| 4852 | kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events); |
| 4853 | |
| 4854 | r = -EFAULT; |
| 4855 | if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events))) |
| 4856 | break; |
| 4857 | r = 0; |
| 4858 | break; |
| 4859 | } |
| 4860 | case KVM_SET_VCPU_EVENTS: { |
| 4861 | struct kvm_vcpu_events events; |
| 4862 | |
| 4863 | r = -EFAULT; |
| 4864 | if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events))) |
| 4865 | break; |
| 4866 | |
| 4867 | r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events); |
| 4868 | break; |
| 4869 | } |
| 4870 | case KVM_GET_DEBUGREGS: { |
| 4871 | struct kvm_debugregs dbgregs; |
| 4872 | |
| 4873 | kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs); |
| 4874 | |
| 4875 | r = -EFAULT; |
| 4876 | if (copy_to_user(argp, &dbgregs, |
| 4877 | sizeof(struct kvm_debugregs))) |
| 4878 | break; |
| 4879 | r = 0; |
| 4880 | break; |
| 4881 | } |
| 4882 | case KVM_SET_DEBUGREGS: { |
| 4883 | struct kvm_debugregs dbgregs; |
| 4884 | |
| 4885 | r = -EFAULT; |
| 4886 | if (copy_from_user(&dbgregs, argp, |
| 4887 | sizeof(struct kvm_debugregs))) |
| 4888 | break; |
| 4889 | |
| 4890 | r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs); |
| 4891 | break; |
| 4892 | } |
| 4893 | case KVM_GET_XSAVE: { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4894 | u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4895 | r = -ENOMEM; |
| 4896 | if (!u.xsave) |
| 4897 | break; |
| 4898 | |
| 4899 | kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave); |
| 4900 | |
| 4901 | r = -EFAULT; |
| 4902 | if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave))) |
| 4903 | break; |
| 4904 | r = 0; |
| 4905 | break; |
| 4906 | } |
| 4907 | case KVM_SET_XSAVE: { |
| 4908 | u.xsave = memdup_user(argp, sizeof(*u.xsave)); |
| 4909 | if (IS_ERR(u.xsave)) { |
| 4910 | r = PTR_ERR(u.xsave); |
| 4911 | goto out_nofree; |
| 4912 | } |
| 4913 | |
| 4914 | r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave); |
| 4915 | break; |
| 4916 | } |
| 4917 | case KVM_GET_XCRS: { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4918 | u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4919 | r = -ENOMEM; |
| 4920 | if (!u.xcrs) |
| 4921 | break; |
| 4922 | |
| 4923 | kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs); |
| 4924 | |
| 4925 | r = -EFAULT; |
| 4926 | if (copy_to_user(argp, u.xcrs, |
| 4927 | sizeof(struct kvm_xcrs))) |
| 4928 | break; |
| 4929 | r = 0; |
| 4930 | break; |
| 4931 | } |
| 4932 | case KVM_SET_XCRS: { |
| 4933 | u.xcrs = memdup_user(argp, sizeof(*u.xcrs)); |
| 4934 | if (IS_ERR(u.xcrs)) { |
| 4935 | r = PTR_ERR(u.xcrs); |
| 4936 | goto out_nofree; |
| 4937 | } |
| 4938 | |
| 4939 | r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs); |
| 4940 | break; |
| 4941 | } |
| 4942 | case KVM_SET_TSC_KHZ: { |
| 4943 | u32 user_tsc_khz; |
| 4944 | |
| 4945 | r = -EINVAL; |
| 4946 | user_tsc_khz = (u32)arg; |
| 4947 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4948 | if (kvm_has_tsc_control && |
| 4949 | user_tsc_khz >= kvm_max_guest_tsc_khz) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4950 | goto out; |
| 4951 | |
| 4952 | if (user_tsc_khz == 0) |
| 4953 | user_tsc_khz = tsc_khz; |
| 4954 | |
| 4955 | if (!kvm_set_tsc_khz(vcpu, user_tsc_khz)) |
| 4956 | r = 0; |
| 4957 | |
| 4958 | goto out; |
| 4959 | } |
| 4960 | case KVM_GET_TSC_KHZ: { |
| 4961 | r = vcpu->arch.virtual_tsc_khz; |
| 4962 | goto out; |
| 4963 | } |
| 4964 | case KVM_KVMCLOCK_CTRL: { |
| 4965 | r = kvm_set_guest_paused(vcpu); |
| 4966 | goto out; |
| 4967 | } |
| 4968 | case KVM_ENABLE_CAP: { |
| 4969 | struct kvm_enable_cap cap; |
| 4970 | |
| 4971 | r = -EFAULT; |
| 4972 | if (copy_from_user(&cap, argp, sizeof(cap))) |
| 4973 | goto out; |
| 4974 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); |
| 4975 | break; |
| 4976 | } |
| 4977 | case KVM_GET_NESTED_STATE: { |
| 4978 | struct kvm_nested_state __user *user_kvm_nested_state = argp; |
| 4979 | u32 user_data_size; |
| 4980 | |
| 4981 | r = -EINVAL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4982 | if (!kvm_x86_ops.nested_ops->get_state) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4983 | break; |
| 4984 | |
| 4985 | BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); |
| 4986 | r = -EFAULT; |
| 4987 | if (get_user(user_data_size, &user_kvm_nested_state->size)) |
| 4988 | break; |
| 4989 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4990 | r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, |
| 4991 | user_data_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4992 | if (r < 0) |
| 4993 | break; |
| 4994 | |
| 4995 | if (r > user_data_size) { |
| 4996 | if (put_user(r, &user_kvm_nested_state->size)) |
| 4997 | r = -EFAULT; |
| 4998 | else |
| 4999 | r = -E2BIG; |
| 5000 | break; |
| 5001 | } |
| 5002 | |
| 5003 | r = 0; |
| 5004 | break; |
| 5005 | } |
| 5006 | case KVM_SET_NESTED_STATE: { |
| 5007 | struct kvm_nested_state __user *user_kvm_nested_state = argp; |
| 5008 | struct kvm_nested_state kvm_state; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5009 | int idx; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5010 | |
| 5011 | r = -EINVAL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5012 | if (!kvm_x86_ops.nested_ops->set_state) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5013 | break; |
| 5014 | |
| 5015 | r = -EFAULT; |
| 5016 | if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state))) |
| 5017 | break; |
| 5018 | |
| 5019 | r = -EINVAL; |
| 5020 | if (kvm_state.size < sizeof(kvm_state)) |
| 5021 | break; |
| 5022 | |
| 5023 | if (kvm_state.flags & |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5024 | ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5025 | | KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING |
| 5026 | | KVM_STATE_NESTED_GIF_SET)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5027 | break; |
| 5028 | |
| 5029 | /* nested_run_pending implies guest_mode. */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5030 | if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING) |
| 5031 | && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5032 | break; |
| 5033 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5034 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5035 | r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5036 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5037 | break; |
| 5038 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5039 | case KVM_GET_SUPPORTED_HV_CPUID: { |
| 5040 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
| 5041 | struct kvm_cpuid2 cpuid; |
| 5042 | |
| 5043 | r = -EFAULT; |
| 5044 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
| 5045 | goto out; |
| 5046 | |
| 5047 | r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid, |
| 5048 | cpuid_arg->entries); |
| 5049 | if (r) |
| 5050 | goto out; |
| 5051 | |
| 5052 | r = -EFAULT; |
| 5053 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
| 5054 | goto out; |
| 5055 | r = 0; |
| 5056 | break; |
| 5057 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5058 | default: |
| 5059 | r = -EINVAL; |
| 5060 | } |
| 5061 | out: |
| 5062 | kfree(u.buffer); |
| 5063 | out_nofree: |
| 5064 | vcpu_put(vcpu); |
| 5065 | return r; |
| 5066 | } |
| 5067 | |
| 5068 | vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
| 5069 | { |
| 5070 | return VM_FAULT_SIGBUS; |
| 5071 | } |
| 5072 | |
| 5073 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
| 5074 | { |
| 5075 | int ret; |
| 5076 | |
| 5077 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) |
| 5078 | return -EINVAL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5079 | ret = kvm_x86_ops.set_tss_addr(kvm, addr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5080 | return ret; |
| 5081 | } |
| 5082 | |
| 5083 | static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm, |
| 5084 | u64 ident_addr) |
| 5085 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5086 | return kvm_x86_ops.set_identity_map_addr(kvm, ident_addr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5087 | } |
| 5088 | |
| 5089 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5090 | unsigned long kvm_nr_mmu_pages) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5091 | { |
| 5092 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) |
| 5093 | return -EINVAL; |
| 5094 | |
| 5095 | mutex_lock(&kvm->slots_lock); |
| 5096 | |
| 5097 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); |
| 5098 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
| 5099 | |
| 5100 | mutex_unlock(&kvm->slots_lock); |
| 5101 | return 0; |
| 5102 | } |
| 5103 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5104 | static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5105 | { |
| 5106 | return kvm->arch.n_max_mmu_pages; |
| 5107 | } |
| 5108 | |
| 5109 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 5110 | { |
| 5111 | struct kvm_pic *pic = kvm->arch.vpic; |
| 5112 | int r; |
| 5113 | |
| 5114 | r = 0; |
| 5115 | switch (chip->chip_id) { |
| 5116 | case KVM_IRQCHIP_PIC_MASTER: |
| 5117 | memcpy(&chip->chip.pic, &pic->pics[0], |
| 5118 | sizeof(struct kvm_pic_state)); |
| 5119 | break; |
| 5120 | case KVM_IRQCHIP_PIC_SLAVE: |
| 5121 | memcpy(&chip->chip.pic, &pic->pics[1], |
| 5122 | sizeof(struct kvm_pic_state)); |
| 5123 | break; |
| 5124 | case KVM_IRQCHIP_IOAPIC: |
| 5125 | kvm_get_ioapic(kvm, &chip->chip.ioapic); |
| 5126 | break; |
| 5127 | default: |
| 5128 | r = -EINVAL; |
| 5129 | break; |
| 5130 | } |
| 5131 | return r; |
| 5132 | } |
| 5133 | |
| 5134 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
| 5135 | { |
| 5136 | struct kvm_pic *pic = kvm->arch.vpic; |
| 5137 | int r; |
| 5138 | |
| 5139 | r = 0; |
| 5140 | switch (chip->chip_id) { |
| 5141 | case KVM_IRQCHIP_PIC_MASTER: |
| 5142 | spin_lock(&pic->lock); |
| 5143 | memcpy(&pic->pics[0], &chip->chip.pic, |
| 5144 | sizeof(struct kvm_pic_state)); |
| 5145 | spin_unlock(&pic->lock); |
| 5146 | break; |
| 5147 | case KVM_IRQCHIP_PIC_SLAVE: |
| 5148 | spin_lock(&pic->lock); |
| 5149 | memcpy(&pic->pics[1], &chip->chip.pic, |
| 5150 | sizeof(struct kvm_pic_state)); |
| 5151 | spin_unlock(&pic->lock); |
| 5152 | break; |
| 5153 | case KVM_IRQCHIP_IOAPIC: |
| 5154 | kvm_set_ioapic(kvm, &chip->chip.ioapic); |
| 5155 | break; |
| 5156 | default: |
| 5157 | r = -EINVAL; |
| 5158 | break; |
| 5159 | } |
| 5160 | kvm_pic_update_irq(pic); |
| 5161 | return r; |
| 5162 | } |
| 5163 | |
| 5164 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 5165 | { |
| 5166 | struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; |
| 5167 | |
| 5168 | BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); |
| 5169 | |
| 5170 | mutex_lock(&kps->lock); |
| 5171 | memcpy(ps, &kps->channels, sizeof(*ps)); |
| 5172 | mutex_unlock(&kps->lock); |
| 5173 | return 0; |
| 5174 | } |
| 5175 | |
| 5176 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
| 5177 | { |
| 5178 | int i; |
| 5179 | struct kvm_pit *pit = kvm->arch.vpit; |
| 5180 | |
| 5181 | mutex_lock(&pit->pit_state.lock); |
| 5182 | memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); |
| 5183 | for (i = 0; i < 3; i++) |
| 5184 | kvm_pit_load_count(pit, i, ps->channels[i].count, 0); |
| 5185 | mutex_unlock(&pit->pit_state.lock); |
| 5186 | return 0; |
| 5187 | } |
| 5188 | |
| 5189 | static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) |
| 5190 | { |
| 5191 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
| 5192 | memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, |
| 5193 | sizeof(ps->channels)); |
| 5194 | ps->flags = kvm->arch.vpit->pit_state.flags; |
| 5195 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); |
| 5196 | memset(&ps->reserved, 0, sizeof(ps->reserved)); |
| 5197 | return 0; |
| 5198 | } |
| 5199 | |
| 5200 | static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) |
| 5201 | { |
| 5202 | int start = 0; |
| 5203 | int i; |
| 5204 | u32 prev_legacy, cur_legacy; |
| 5205 | struct kvm_pit *pit = kvm->arch.vpit; |
| 5206 | |
| 5207 | mutex_lock(&pit->pit_state.lock); |
| 5208 | prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; |
| 5209 | cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; |
| 5210 | if (!prev_legacy && cur_legacy) |
| 5211 | start = 1; |
| 5212 | memcpy(&pit->pit_state.channels, &ps->channels, |
| 5213 | sizeof(pit->pit_state.channels)); |
| 5214 | pit->pit_state.flags = ps->flags; |
| 5215 | for (i = 0; i < 3; i++) |
| 5216 | kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, |
| 5217 | start && i == 0); |
| 5218 | mutex_unlock(&pit->pit_state.lock); |
| 5219 | return 0; |
| 5220 | } |
| 5221 | |
| 5222 | static int kvm_vm_ioctl_reinject(struct kvm *kvm, |
| 5223 | struct kvm_reinject_control *control) |
| 5224 | { |
| 5225 | struct kvm_pit *pit = kvm->arch.vpit; |
| 5226 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5227 | /* pit->pit_state.lock was overloaded to prevent userspace from getting |
| 5228 | * an inconsistent state after running multiple KVM_REINJECT_CONTROL |
| 5229 | * ioctls in parallel. Use a separate lock if that ioctl isn't rare. |
| 5230 | */ |
| 5231 | mutex_lock(&pit->pit_state.lock); |
| 5232 | kvm_pit_set_reinject(pit, control->pit_reinject); |
| 5233 | mutex_unlock(&pit->pit_state.lock); |
| 5234 | |
| 5235 | return 0; |
| 5236 | } |
| 5237 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5238 | void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5239 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5240 | /* |
| 5241 | * Flush potentially hardware-cached dirty pages to dirty_bitmap. |
| 5242 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5243 | if (kvm_x86_ops.flush_log_dirty) |
| 5244 | kvm_x86_ops.flush_log_dirty(kvm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, |
| 5248 | bool line_status) |
| 5249 | { |
| 5250 | if (!irqchip_in_kernel(kvm)) |
| 5251 | return -ENXIO; |
| 5252 | |
| 5253 | irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, |
| 5254 | irq_event->irq, irq_event->level, |
| 5255 | line_status); |
| 5256 | return 0; |
| 5257 | } |
| 5258 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5259 | int kvm_vm_ioctl_enable_cap(struct kvm *kvm, |
| 5260 | struct kvm_enable_cap *cap) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5261 | { |
| 5262 | int r; |
| 5263 | |
| 5264 | if (cap->flags) |
| 5265 | return -EINVAL; |
| 5266 | |
| 5267 | switch (cap->cap) { |
| 5268 | case KVM_CAP_DISABLE_QUIRKS: |
| 5269 | kvm->arch.disabled_quirks = cap->args[0]; |
| 5270 | r = 0; |
| 5271 | break; |
| 5272 | case KVM_CAP_SPLIT_IRQCHIP: { |
| 5273 | mutex_lock(&kvm->lock); |
| 5274 | r = -EINVAL; |
| 5275 | if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) |
| 5276 | goto split_irqchip_unlock; |
| 5277 | r = -EEXIST; |
| 5278 | if (irqchip_in_kernel(kvm)) |
| 5279 | goto split_irqchip_unlock; |
| 5280 | if (kvm->created_vcpus) |
| 5281 | goto split_irqchip_unlock; |
| 5282 | r = kvm_setup_empty_irq_routing(kvm); |
| 5283 | if (r) |
| 5284 | goto split_irqchip_unlock; |
| 5285 | /* Pairs with irqchip_in_kernel. */ |
| 5286 | smp_wmb(); |
| 5287 | kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; |
| 5288 | kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; |
| 5289 | r = 0; |
| 5290 | split_irqchip_unlock: |
| 5291 | mutex_unlock(&kvm->lock); |
| 5292 | break; |
| 5293 | } |
| 5294 | case KVM_CAP_X2APIC_API: |
| 5295 | r = -EINVAL; |
| 5296 | if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) |
| 5297 | break; |
| 5298 | |
| 5299 | if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) |
| 5300 | kvm->arch.x2apic_format = true; |
| 5301 | if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) |
| 5302 | kvm->arch.x2apic_broadcast_quirk_disabled = true; |
| 5303 | |
| 5304 | r = 0; |
| 5305 | break; |
| 5306 | case KVM_CAP_X86_DISABLE_EXITS: |
| 5307 | r = -EINVAL; |
| 5308 | if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS) |
| 5309 | break; |
| 5310 | |
| 5311 | if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) && |
| 5312 | kvm_can_mwait_in_guest()) |
| 5313 | kvm->arch.mwait_in_guest = true; |
| 5314 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) |
| 5315 | kvm->arch.hlt_in_guest = true; |
| 5316 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) |
| 5317 | kvm->arch.pause_in_guest = true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5318 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) |
| 5319 | kvm->arch.cstate_in_guest = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5320 | r = 0; |
| 5321 | break; |
| 5322 | case KVM_CAP_MSR_PLATFORM_INFO: |
| 5323 | kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; |
| 5324 | r = 0; |
| 5325 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5326 | case KVM_CAP_EXCEPTION_PAYLOAD: |
| 5327 | kvm->arch.exception_payload_enabled = cap->args[0]; |
| 5328 | r = 0; |
| 5329 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5330 | case KVM_CAP_X86_USER_SPACE_MSR: |
| 5331 | kvm->arch.user_space_msr_mask = cap->args[0]; |
| 5332 | r = 0; |
| 5333 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5334 | default: |
| 5335 | r = -EINVAL; |
| 5336 | break; |
| 5337 | } |
| 5338 | return r; |
| 5339 | } |
| 5340 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5341 | static struct kvm_x86_msr_filter *kvm_alloc_msr_filter(bool default_allow) |
| 5342 | { |
| 5343 | struct kvm_x86_msr_filter *msr_filter; |
| 5344 | |
| 5345 | msr_filter = kzalloc(sizeof(*msr_filter), GFP_KERNEL_ACCOUNT); |
| 5346 | if (!msr_filter) |
| 5347 | return NULL; |
| 5348 | |
| 5349 | msr_filter->default_allow = default_allow; |
| 5350 | return msr_filter; |
| 5351 | } |
| 5352 | |
| 5353 | static void kvm_free_msr_filter(struct kvm_x86_msr_filter *msr_filter) |
| 5354 | { |
| 5355 | u32 i; |
| 5356 | |
| 5357 | if (!msr_filter) |
| 5358 | return; |
| 5359 | |
| 5360 | for (i = 0; i < msr_filter->count; i++) |
| 5361 | kfree(msr_filter->ranges[i].bitmap); |
| 5362 | |
| 5363 | kfree(msr_filter); |
| 5364 | } |
| 5365 | |
| 5366 | static int kvm_add_msr_filter(struct kvm_x86_msr_filter *msr_filter, |
| 5367 | struct kvm_msr_filter_range *user_range) |
| 5368 | { |
| 5369 | struct msr_bitmap_range range; |
| 5370 | unsigned long *bitmap = NULL; |
| 5371 | size_t bitmap_size; |
| 5372 | int r; |
| 5373 | |
| 5374 | if (!user_range->nmsrs) |
| 5375 | return 0; |
| 5376 | |
| 5377 | bitmap_size = BITS_TO_LONGS(user_range->nmsrs) * sizeof(long); |
| 5378 | if (!bitmap_size || bitmap_size > KVM_MSR_FILTER_MAX_BITMAP_SIZE) |
| 5379 | return -EINVAL; |
| 5380 | |
| 5381 | bitmap = memdup_user((__user u8*)user_range->bitmap, bitmap_size); |
| 5382 | if (IS_ERR(bitmap)) |
| 5383 | return PTR_ERR(bitmap); |
| 5384 | |
| 5385 | range = (struct msr_bitmap_range) { |
| 5386 | .flags = user_range->flags, |
| 5387 | .base = user_range->base, |
| 5388 | .nmsrs = user_range->nmsrs, |
| 5389 | .bitmap = bitmap, |
| 5390 | }; |
| 5391 | |
| 5392 | if (range.flags & ~(KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE)) { |
| 5393 | r = -EINVAL; |
| 5394 | goto err; |
| 5395 | } |
| 5396 | |
| 5397 | if (!range.flags) { |
| 5398 | r = -EINVAL; |
| 5399 | goto err; |
| 5400 | } |
| 5401 | |
| 5402 | /* Everything ok, add this range identifier. */ |
| 5403 | msr_filter->ranges[msr_filter->count] = range; |
| 5404 | msr_filter->count++; |
| 5405 | |
| 5406 | return 0; |
| 5407 | err: |
| 5408 | kfree(bitmap); |
| 5409 | return r; |
| 5410 | } |
| 5411 | |
| 5412 | static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, void __user *argp) |
| 5413 | { |
| 5414 | struct kvm_msr_filter __user *user_msr_filter = argp; |
| 5415 | struct kvm_x86_msr_filter *new_filter, *old_filter; |
| 5416 | struct kvm_msr_filter filter; |
| 5417 | bool default_allow; |
| 5418 | bool empty = true; |
| 5419 | int r = 0; |
| 5420 | u32 i; |
| 5421 | |
| 5422 | if (copy_from_user(&filter, user_msr_filter, sizeof(filter))) |
| 5423 | return -EFAULT; |
| 5424 | |
| 5425 | for (i = 0; i < ARRAY_SIZE(filter.ranges); i++) |
| 5426 | empty &= !filter.ranges[i].nmsrs; |
| 5427 | |
| 5428 | default_allow = !(filter.flags & KVM_MSR_FILTER_DEFAULT_DENY); |
| 5429 | if (empty && !default_allow) |
| 5430 | return -EINVAL; |
| 5431 | |
| 5432 | new_filter = kvm_alloc_msr_filter(default_allow); |
| 5433 | if (!new_filter) |
| 5434 | return -ENOMEM; |
| 5435 | |
| 5436 | for (i = 0; i < ARRAY_SIZE(filter.ranges); i++) { |
| 5437 | r = kvm_add_msr_filter(new_filter, &filter.ranges[i]); |
| 5438 | if (r) { |
| 5439 | kvm_free_msr_filter(new_filter); |
| 5440 | return r; |
| 5441 | } |
| 5442 | } |
| 5443 | |
| 5444 | mutex_lock(&kvm->lock); |
| 5445 | |
| 5446 | /* The per-VM filter is protected by kvm->lock... */ |
| 5447 | old_filter = srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1); |
| 5448 | |
| 5449 | rcu_assign_pointer(kvm->arch.msr_filter, new_filter); |
| 5450 | synchronize_srcu(&kvm->srcu); |
| 5451 | |
| 5452 | kvm_free_msr_filter(old_filter); |
| 5453 | |
| 5454 | kvm_make_all_cpus_request(kvm, KVM_REQ_MSR_FILTER_CHANGED); |
| 5455 | mutex_unlock(&kvm->lock); |
| 5456 | |
| 5457 | return 0; |
| 5458 | } |
| 5459 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5460 | long kvm_arch_vm_ioctl(struct file *filp, |
| 5461 | unsigned int ioctl, unsigned long arg) |
| 5462 | { |
| 5463 | struct kvm *kvm = filp->private_data; |
| 5464 | void __user *argp = (void __user *)arg; |
| 5465 | int r = -ENOTTY; |
| 5466 | /* |
| 5467 | * This union makes it completely explicit to gcc-3.x |
| 5468 | * that these two variables' stack usage should be |
| 5469 | * combined, not added together. |
| 5470 | */ |
| 5471 | union { |
| 5472 | struct kvm_pit_state ps; |
| 5473 | struct kvm_pit_state2 ps2; |
| 5474 | struct kvm_pit_config pit_config; |
| 5475 | } u; |
| 5476 | |
| 5477 | switch (ioctl) { |
| 5478 | case KVM_SET_TSS_ADDR: |
| 5479 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); |
| 5480 | break; |
| 5481 | case KVM_SET_IDENTITY_MAP_ADDR: { |
| 5482 | u64 ident_addr; |
| 5483 | |
| 5484 | mutex_lock(&kvm->lock); |
| 5485 | r = -EINVAL; |
| 5486 | if (kvm->created_vcpus) |
| 5487 | goto set_identity_unlock; |
| 5488 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5489 | if (copy_from_user(&ident_addr, argp, sizeof(ident_addr))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5490 | goto set_identity_unlock; |
| 5491 | r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr); |
| 5492 | set_identity_unlock: |
| 5493 | mutex_unlock(&kvm->lock); |
| 5494 | break; |
| 5495 | } |
| 5496 | case KVM_SET_NR_MMU_PAGES: |
| 5497 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); |
| 5498 | break; |
| 5499 | case KVM_GET_NR_MMU_PAGES: |
| 5500 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); |
| 5501 | break; |
| 5502 | case KVM_CREATE_IRQCHIP: { |
| 5503 | mutex_lock(&kvm->lock); |
| 5504 | |
| 5505 | r = -EEXIST; |
| 5506 | if (irqchip_in_kernel(kvm)) |
| 5507 | goto create_irqchip_unlock; |
| 5508 | |
| 5509 | r = -EINVAL; |
| 5510 | if (kvm->created_vcpus) |
| 5511 | goto create_irqchip_unlock; |
| 5512 | |
| 5513 | r = kvm_pic_init(kvm); |
| 5514 | if (r) |
| 5515 | goto create_irqchip_unlock; |
| 5516 | |
| 5517 | r = kvm_ioapic_init(kvm); |
| 5518 | if (r) { |
| 5519 | kvm_pic_destroy(kvm); |
| 5520 | goto create_irqchip_unlock; |
| 5521 | } |
| 5522 | |
| 5523 | r = kvm_setup_default_irq_routing(kvm); |
| 5524 | if (r) { |
| 5525 | kvm_ioapic_destroy(kvm); |
| 5526 | kvm_pic_destroy(kvm); |
| 5527 | goto create_irqchip_unlock; |
| 5528 | } |
| 5529 | /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ |
| 5530 | smp_wmb(); |
| 5531 | kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; |
| 5532 | create_irqchip_unlock: |
| 5533 | mutex_unlock(&kvm->lock); |
| 5534 | break; |
| 5535 | } |
| 5536 | case KVM_CREATE_PIT: |
| 5537 | u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY; |
| 5538 | goto create_pit; |
| 5539 | case KVM_CREATE_PIT2: |
| 5540 | r = -EFAULT; |
| 5541 | if (copy_from_user(&u.pit_config, argp, |
| 5542 | sizeof(struct kvm_pit_config))) |
| 5543 | goto out; |
| 5544 | create_pit: |
| 5545 | mutex_lock(&kvm->lock); |
| 5546 | r = -EEXIST; |
| 5547 | if (kvm->arch.vpit) |
| 5548 | goto create_pit_unlock; |
| 5549 | r = -ENOMEM; |
| 5550 | kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); |
| 5551 | if (kvm->arch.vpit) |
| 5552 | r = 0; |
| 5553 | create_pit_unlock: |
| 5554 | mutex_unlock(&kvm->lock); |
| 5555 | break; |
| 5556 | case KVM_GET_IRQCHIP: { |
| 5557 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
| 5558 | struct kvm_irqchip *chip; |
| 5559 | |
| 5560 | chip = memdup_user(argp, sizeof(*chip)); |
| 5561 | if (IS_ERR(chip)) { |
| 5562 | r = PTR_ERR(chip); |
| 5563 | goto out; |
| 5564 | } |
| 5565 | |
| 5566 | r = -ENXIO; |
| 5567 | if (!irqchip_kernel(kvm)) |
| 5568 | goto get_irqchip_out; |
| 5569 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); |
| 5570 | if (r) |
| 5571 | goto get_irqchip_out; |
| 5572 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5573 | if (copy_to_user(argp, chip, sizeof(*chip))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5574 | goto get_irqchip_out; |
| 5575 | r = 0; |
| 5576 | get_irqchip_out: |
| 5577 | kfree(chip); |
| 5578 | break; |
| 5579 | } |
| 5580 | case KVM_SET_IRQCHIP: { |
| 5581 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ |
| 5582 | struct kvm_irqchip *chip; |
| 5583 | |
| 5584 | chip = memdup_user(argp, sizeof(*chip)); |
| 5585 | if (IS_ERR(chip)) { |
| 5586 | r = PTR_ERR(chip); |
| 5587 | goto out; |
| 5588 | } |
| 5589 | |
| 5590 | r = -ENXIO; |
| 5591 | if (!irqchip_kernel(kvm)) |
| 5592 | goto set_irqchip_out; |
| 5593 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5594 | set_irqchip_out: |
| 5595 | kfree(chip); |
| 5596 | break; |
| 5597 | } |
| 5598 | case KVM_GET_PIT: { |
| 5599 | r = -EFAULT; |
| 5600 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
| 5601 | goto out; |
| 5602 | r = -ENXIO; |
| 5603 | if (!kvm->arch.vpit) |
| 5604 | goto out; |
| 5605 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
| 5606 | if (r) |
| 5607 | goto out; |
| 5608 | r = -EFAULT; |
| 5609 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
| 5610 | goto out; |
| 5611 | r = 0; |
| 5612 | break; |
| 5613 | } |
| 5614 | case KVM_SET_PIT: { |
| 5615 | r = -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5616 | if (copy_from_user(&u.ps, argp, sizeof(u.ps))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5617 | goto out; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5618 | mutex_lock(&kvm->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5619 | r = -ENXIO; |
| 5620 | if (!kvm->arch.vpit) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5621 | goto set_pit_out; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5622 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5623 | set_pit_out: |
| 5624 | mutex_unlock(&kvm->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5625 | break; |
| 5626 | } |
| 5627 | case KVM_GET_PIT2: { |
| 5628 | r = -ENXIO; |
| 5629 | if (!kvm->arch.vpit) |
| 5630 | goto out; |
| 5631 | r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2); |
| 5632 | if (r) |
| 5633 | goto out; |
| 5634 | r = -EFAULT; |
| 5635 | if (copy_to_user(argp, &u.ps2, sizeof(u.ps2))) |
| 5636 | goto out; |
| 5637 | r = 0; |
| 5638 | break; |
| 5639 | } |
| 5640 | case KVM_SET_PIT2: { |
| 5641 | r = -EFAULT; |
| 5642 | if (copy_from_user(&u.ps2, argp, sizeof(u.ps2))) |
| 5643 | goto out; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5644 | mutex_lock(&kvm->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5645 | r = -ENXIO; |
| 5646 | if (!kvm->arch.vpit) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5647 | goto set_pit2_out; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5648 | r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5649 | set_pit2_out: |
| 5650 | mutex_unlock(&kvm->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5651 | break; |
| 5652 | } |
| 5653 | case KVM_REINJECT_CONTROL: { |
| 5654 | struct kvm_reinject_control control; |
| 5655 | r = -EFAULT; |
| 5656 | if (copy_from_user(&control, argp, sizeof(control))) |
| 5657 | goto out; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5658 | r = -ENXIO; |
| 5659 | if (!kvm->arch.vpit) |
| 5660 | goto out; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5661 | r = kvm_vm_ioctl_reinject(kvm, &control); |
| 5662 | break; |
| 5663 | } |
| 5664 | case KVM_SET_BOOT_CPU_ID: |
| 5665 | r = 0; |
| 5666 | mutex_lock(&kvm->lock); |
| 5667 | if (kvm->created_vcpus) |
| 5668 | r = -EBUSY; |
| 5669 | else |
| 5670 | kvm->arch.bsp_vcpu_id = arg; |
| 5671 | mutex_unlock(&kvm->lock); |
| 5672 | break; |
| 5673 | case KVM_XEN_HVM_CONFIG: { |
| 5674 | struct kvm_xen_hvm_config xhc; |
| 5675 | r = -EFAULT; |
| 5676 | if (copy_from_user(&xhc, argp, sizeof(xhc))) |
| 5677 | goto out; |
| 5678 | r = -EINVAL; |
| 5679 | if (xhc.flags) |
| 5680 | goto out; |
| 5681 | memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc)); |
| 5682 | r = 0; |
| 5683 | break; |
| 5684 | } |
| 5685 | case KVM_SET_CLOCK: { |
| 5686 | struct kvm_clock_data user_ns; |
| 5687 | u64 now_ns; |
| 5688 | |
| 5689 | r = -EFAULT; |
| 5690 | if (copy_from_user(&user_ns, argp, sizeof(user_ns))) |
| 5691 | goto out; |
| 5692 | |
| 5693 | r = -EINVAL; |
| 5694 | if (user_ns.flags) |
| 5695 | goto out; |
| 5696 | |
| 5697 | r = 0; |
| 5698 | /* |
| 5699 | * TODO: userspace has to take care of races with VCPU_RUN, so |
| 5700 | * kvm_gen_update_masterclock() can be cut down to locked |
| 5701 | * pvclock_update_vm_gtod_copy(). |
| 5702 | */ |
| 5703 | kvm_gen_update_masterclock(kvm); |
| 5704 | now_ns = get_kvmclock_ns(kvm); |
| 5705 | kvm->arch.kvmclock_offset += user_ns.clock - now_ns; |
| 5706 | kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE); |
| 5707 | break; |
| 5708 | } |
| 5709 | case KVM_GET_CLOCK: { |
| 5710 | struct kvm_clock_data user_ns; |
| 5711 | u64 now_ns; |
| 5712 | |
| 5713 | now_ns = get_kvmclock_ns(kvm); |
| 5714 | user_ns.clock = now_ns; |
| 5715 | user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0; |
| 5716 | memset(&user_ns.pad, 0, sizeof(user_ns.pad)); |
| 5717 | |
| 5718 | r = -EFAULT; |
| 5719 | if (copy_to_user(argp, &user_ns, sizeof(user_ns))) |
| 5720 | goto out; |
| 5721 | r = 0; |
| 5722 | break; |
| 5723 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5724 | case KVM_MEMORY_ENCRYPT_OP: { |
| 5725 | r = -ENOTTY; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5726 | if (kvm_x86_ops.mem_enc_op) |
| 5727 | r = kvm_x86_ops.mem_enc_op(kvm, argp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5728 | break; |
| 5729 | } |
| 5730 | case KVM_MEMORY_ENCRYPT_REG_REGION: { |
| 5731 | struct kvm_enc_region region; |
| 5732 | |
| 5733 | r = -EFAULT; |
| 5734 | if (copy_from_user(®ion, argp, sizeof(region))) |
| 5735 | goto out; |
| 5736 | |
| 5737 | r = -ENOTTY; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5738 | if (kvm_x86_ops.mem_enc_reg_region) |
| 5739 | r = kvm_x86_ops.mem_enc_reg_region(kvm, ®ion); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5740 | break; |
| 5741 | } |
| 5742 | case KVM_MEMORY_ENCRYPT_UNREG_REGION: { |
| 5743 | struct kvm_enc_region region; |
| 5744 | |
| 5745 | r = -EFAULT; |
| 5746 | if (copy_from_user(®ion, argp, sizeof(region))) |
| 5747 | goto out; |
| 5748 | |
| 5749 | r = -ENOTTY; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5750 | if (kvm_x86_ops.mem_enc_unreg_region) |
| 5751 | r = kvm_x86_ops.mem_enc_unreg_region(kvm, ®ion); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5752 | break; |
| 5753 | } |
| 5754 | case KVM_HYPERV_EVENTFD: { |
| 5755 | struct kvm_hyperv_eventfd hvevfd; |
| 5756 | |
| 5757 | r = -EFAULT; |
| 5758 | if (copy_from_user(&hvevfd, argp, sizeof(hvevfd))) |
| 5759 | goto out; |
| 5760 | r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd); |
| 5761 | break; |
| 5762 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5763 | case KVM_SET_PMU_EVENT_FILTER: |
| 5764 | r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp); |
| 5765 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5766 | case KVM_X86_SET_MSR_FILTER: |
| 5767 | r = kvm_vm_ioctl_set_msr_filter(kvm, argp); |
| 5768 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5769 | default: |
| 5770 | r = -ENOTTY; |
| 5771 | } |
| 5772 | out: |
| 5773 | return r; |
| 5774 | } |
| 5775 | |
| 5776 | static void kvm_init_msr_list(void) |
| 5777 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5778 | struct x86_pmu_capability x86_pmu; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5779 | u32 dummy[2]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5780 | unsigned i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5781 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5782 | BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4, |
| 5783 | "Please update the fixed PMCs in msrs_to_saved_all[]"); |
| 5784 | |
| 5785 | perf_get_x86_pmu_capability(&x86_pmu); |
| 5786 | |
| 5787 | num_msrs_to_save = 0; |
| 5788 | num_emulated_msrs = 0; |
| 5789 | num_msr_based_features = 0; |
| 5790 | |
| 5791 | for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) { |
| 5792 | if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5793 | continue; |
| 5794 | |
| 5795 | /* |
| 5796 | * Even MSRs that are valid in the host may not be exposed |
| 5797 | * to the guests in some cases. |
| 5798 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5799 | switch (msrs_to_save_all[i]) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5800 | case MSR_IA32_BNDCFGS: |
| 5801 | if (!kvm_mpx_supported()) |
| 5802 | continue; |
| 5803 | break; |
| 5804 | case MSR_TSC_AUX: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5805 | if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5806 | continue; |
| 5807 | break; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5808 | case MSR_IA32_UMWAIT_CONTROL: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5809 | if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5810 | continue; |
| 5811 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5812 | case MSR_IA32_RTIT_CTL: |
| 5813 | case MSR_IA32_RTIT_STATUS: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5814 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5815 | continue; |
| 5816 | break; |
| 5817 | case MSR_IA32_RTIT_CR3_MATCH: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5818 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5819 | !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering)) |
| 5820 | continue; |
| 5821 | break; |
| 5822 | case MSR_IA32_RTIT_OUTPUT_BASE: |
| 5823 | case MSR_IA32_RTIT_OUTPUT_MASK: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5824 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5825 | (!intel_pt_validate_hw_cap(PT_CAP_topa_output) && |
| 5826 | !intel_pt_validate_hw_cap(PT_CAP_single_range_output))) |
| 5827 | continue; |
| 5828 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5829 | case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: |
| 5830 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5831 | msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >= |
| 5832 | intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2) |
| 5833 | continue; |
| 5834 | break; |
| 5835 | case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17: |
| 5836 | if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >= |
| 5837 | min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) |
| 5838 | continue; |
| 5839 | break; |
| 5840 | case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17: |
| 5841 | if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >= |
| 5842 | min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) |
| 5843 | continue; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5844 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5845 | default: |
| 5846 | break; |
| 5847 | } |
| 5848 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5849 | msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5850 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5851 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5852 | for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5853 | if (!kvm_x86_ops.has_emulated_msr(emulated_msrs_all[i])) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5854 | continue; |
| 5855 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5856 | emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5857 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5858 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5859 | for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5860 | struct kvm_msr_entry msr; |
| 5861 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5862 | msr.index = msr_based_features_all[i]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5863 | if (kvm_get_msr_feature(&msr)) |
| 5864 | continue; |
| 5865 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5866 | msr_based_features[num_msr_based_features++] = msr_based_features_all[i]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5867 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5868 | } |
| 5869 | |
| 5870 | static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, |
| 5871 | const void *v) |
| 5872 | { |
| 5873 | int handled = 0; |
| 5874 | int n; |
| 5875 | |
| 5876 | do { |
| 5877 | n = min(len, 8); |
| 5878 | if (!(lapic_in_kernel(vcpu) && |
| 5879 | !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) |
| 5880 | && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v)) |
| 5881 | break; |
| 5882 | handled += n; |
| 5883 | addr += n; |
| 5884 | len -= n; |
| 5885 | v += n; |
| 5886 | } while (len); |
| 5887 | |
| 5888 | return handled; |
| 5889 | } |
| 5890 | |
| 5891 | static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v) |
| 5892 | { |
| 5893 | int handled = 0; |
| 5894 | int n; |
| 5895 | |
| 5896 | do { |
| 5897 | n = min(len, 8); |
| 5898 | if (!(lapic_in_kernel(vcpu) && |
| 5899 | !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, |
| 5900 | addr, n, v)) |
| 5901 | && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v)) |
| 5902 | break; |
| 5903 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v); |
| 5904 | handled += n; |
| 5905 | addr += n; |
| 5906 | len -= n; |
| 5907 | v += n; |
| 5908 | } while (len); |
| 5909 | |
| 5910 | return handled; |
| 5911 | } |
| 5912 | |
| 5913 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
| 5914 | struct kvm_segment *var, int seg) |
| 5915 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5916 | kvm_x86_ops.set_segment(vcpu, var, seg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5917 | } |
| 5918 | |
| 5919 | void kvm_get_segment(struct kvm_vcpu *vcpu, |
| 5920 | struct kvm_segment *var, int seg) |
| 5921 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5922 | kvm_x86_ops.get_segment(vcpu, var, seg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5923 | } |
| 5924 | |
| 5925 | gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access, |
| 5926 | struct x86_exception *exception) |
| 5927 | { |
| 5928 | gpa_t t_gpa; |
| 5929 | |
| 5930 | BUG_ON(!mmu_is_nested(vcpu)); |
| 5931 | |
| 5932 | /* NPT walks are always user-walks */ |
| 5933 | access |= PFERR_USER_MASK; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5934 | t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5935 | |
| 5936 | return t_gpa; |
| 5937 | } |
| 5938 | |
| 5939 | gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, |
| 5940 | struct x86_exception *exception) |
| 5941 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5942 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5943 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
| 5944 | } |
| 5945 | |
| 5946 | gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, |
| 5947 | struct x86_exception *exception) |
| 5948 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5949 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5950 | access |= PFERR_FETCH_MASK; |
| 5951 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
| 5952 | } |
| 5953 | |
| 5954 | gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, |
| 5955 | struct x86_exception *exception) |
| 5956 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5957 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5958 | access |= PFERR_WRITE_MASK; |
| 5959 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
| 5960 | } |
| 5961 | |
| 5962 | /* uses this to access any guest's mapped memory without checking CPL */ |
| 5963 | gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, |
| 5964 | struct x86_exception *exception) |
| 5965 | { |
| 5966 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception); |
| 5967 | } |
| 5968 | |
| 5969 | static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, |
| 5970 | struct kvm_vcpu *vcpu, u32 access, |
| 5971 | struct x86_exception *exception) |
| 5972 | { |
| 5973 | void *data = val; |
| 5974 | int r = X86EMUL_CONTINUE; |
| 5975 | |
| 5976 | while (bytes) { |
| 5977 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access, |
| 5978 | exception); |
| 5979 | unsigned offset = addr & (PAGE_SIZE-1); |
| 5980 | unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); |
| 5981 | int ret; |
| 5982 | |
| 5983 | if (gpa == UNMAPPED_GVA) |
| 5984 | return X86EMUL_PROPAGATE_FAULT; |
| 5985 | ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data, |
| 5986 | offset, toread); |
| 5987 | if (ret < 0) { |
| 5988 | r = X86EMUL_IO_NEEDED; |
| 5989 | goto out; |
| 5990 | } |
| 5991 | |
| 5992 | bytes -= toread; |
| 5993 | data += toread; |
| 5994 | addr += toread; |
| 5995 | } |
| 5996 | out: |
| 5997 | return r; |
| 5998 | } |
| 5999 | |
| 6000 | /* used for instruction fetching */ |
| 6001 | static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt, |
| 6002 | gva_t addr, void *val, unsigned int bytes, |
| 6003 | struct x86_exception *exception) |
| 6004 | { |
| 6005 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6006 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6007 | unsigned offset; |
| 6008 | int ret; |
| 6009 | |
| 6010 | /* Inline kvm_read_guest_virt_helper for speed. */ |
| 6011 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK, |
| 6012 | exception); |
| 6013 | if (unlikely(gpa == UNMAPPED_GVA)) |
| 6014 | return X86EMUL_PROPAGATE_FAULT; |
| 6015 | |
| 6016 | offset = addr & (PAGE_SIZE-1); |
| 6017 | if (WARN_ON(offset + bytes > PAGE_SIZE)) |
| 6018 | bytes = (unsigned)PAGE_SIZE - offset; |
| 6019 | ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val, |
| 6020 | offset, bytes); |
| 6021 | if (unlikely(ret < 0)) |
| 6022 | return X86EMUL_IO_NEEDED; |
| 6023 | |
| 6024 | return X86EMUL_CONTINUE; |
| 6025 | } |
| 6026 | |
| 6027 | int kvm_read_guest_virt(struct kvm_vcpu *vcpu, |
| 6028 | gva_t addr, void *val, unsigned int bytes, |
| 6029 | struct x86_exception *exception) |
| 6030 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6031 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6032 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6033 | /* |
| 6034 | * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED |
| 6035 | * is returned, but our callers are not ready for that and they blindly |
| 6036 | * call kvm_inject_page_fault. Ensure that they at least do not leak |
| 6037 | * uninitialized kernel stack memory into cr2 and error code. |
| 6038 | */ |
| 6039 | memset(exception, 0, sizeof(*exception)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6040 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, |
| 6041 | exception); |
| 6042 | } |
| 6043 | EXPORT_SYMBOL_GPL(kvm_read_guest_virt); |
| 6044 | |
| 6045 | static int emulator_read_std(struct x86_emulate_ctxt *ctxt, |
| 6046 | gva_t addr, void *val, unsigned int bytes, |
| 6047 | struct x86_exception *exception, bool system) |
| 6048 | { |
| 6049 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6050 | u32 access = 0; |
| 6051 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6052 | if (!system && kvm_x86_ops.get_cpl(vcpu) == 3) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6053 | access |= PFERR_USER_MASK; |
| 6054 | |
| 6055 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception); |
| 6056 | } |
| 6057 | |
| 6058 | static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt, |
| 6059 | unsigned long addr, void *val, unsigned int bytes) |
| 6060 | { |
| 6061 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6062 | int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes); |
| 6063 | |
| 6064 | return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE; |
| 6065 | } |
| 6066 | |
| 6067 | static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, |
| 6068 | struct kvm_vcpu *vcpu, u32 access, |
| 6069 | struct x86_exception *exception) |
| 6070 | { |
| 6071 | void *data = val; |
| 6072 | int r = X86EMUL_CONTINUE; |
| 6073 | |
| 6074 | while (bytes) { |
| 6075 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, |
| 6076 | access, |
| 6077 | exception); |
| 6078 | unsigned offset = addr & (PAGE_SIZE-1); |
| 6079 | unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); |
| 6080 | int ret; |
| 6081 | |
| 6082 | if (gpa == UNMAPPED_GVA) |
| 6083 | return X86EMUL_PROPAGATE_FAULT; |
| 6084 | ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite); |
| 6085 | if (ret < 0) { |
| 6086 | r = X86EMUL_IO_NEEDED; |
| 6087 | goto out; |
| 6088 | } |
| 6089 | |
| 6090 | bytes -= towrite; |
| 6091 | data += towrite; |
| 6092 | addr += towrite; |
| 6093 | } |
| 6094 | out: |
| 6095 | return r; |
| 6096 | } |
| 6097 | |
| 6098 | static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val, |
| 6099 | unsigned int bytes, struct x86_exception *exception, |
| 6100 | bool system) |
| 6101 | { |
| 6102 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6103 | u32 access = PFERR_WRITE_MASK; |
| 6104 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6105 | if (!system && kvm_x86_ops.get_cpl(vcpu) == 3) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6106 | access |= PFERR_USER_MASK; |
| 6107 | |
| 6108 | return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, |
| 6109 | access, exception); |
| 6110 | } |
| 6111 | |
| 6112 | int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val, |
| 6113 | unsigned int bytes, struct x86_exception *exception) |
| 6114 | { |
| 6115 | /* kvm_write_guest_virt_system can pull in tons of pages. */ |
| 6116 | vcpu->arch.l1tf_flush_l1d = true; |
| 6117 | |
| 6118 | return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, |
| 6119 | PFERR_WRITE_MASK, exception); |
| 6120 | } |
| 6121 | EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system); |
| 6122 | |
| 6123 | int handle_ud(struct kvm_vcpu *vcpu) |
| 6124 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6125 | static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6126 | int emul_type = EMULTYPE_TRAP_UD; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6127 | char sig[5]; /* ud2; .ascii "kvm" */ |
| 6128 | struct x86_exception e; |
| 6129 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6130 | if (unlikely(!kvm_x86_ops.can_emulate_instruction(vcpu, NULL, 0))) |
| 6131 | return 1; |
| 6132 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6133 | if (force_emulation_prefix && |
| 6134 | kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu), |
| 6135 | sig, sizeof(sig), &e) == 0 && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6136 | memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6137 | kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6138 | emul_type = EMULTYPE_TRAP_UD_FORCED; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6139 | } |
| 6140 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6141 | return kvm_emulate_instruction(vcpu, emul_type); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6142 | } |
| 6143 | EXPORT_SYMBOL_GPL(handle_ud); |
| 6144 | |
| 6145 | static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva, |
| 6146 | gpa_t gpa, bool write) |
| 6147 | { |
| 6148 | /* For APIC access vmexit */ |
| 6149 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 6150 | return 1; |
| 6151 | |
| 6152 | if (vcpu_match_mmio_gpa(vcpu, gpa)) { |
| 6153 | trace_vcpu_match_mmio(gva, gpa, write, true); |
| 6154 | return 1; |
| 6155 | } |
| 6156 | |
| 6157 | return 0; |
| 6158 | } |
| 6159 | |
| 6160 | static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, |
| 6161 | gpa_t *gpa, struct x86_exception *exception, |
| 6162 | bool write) |
| 6163 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6164 | u32 access = ((kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6165 | | (write ? PFERR_WRITE_MASK : 0); |
| 6166 | |
| 6167 | /* |
| 6168 | * currently PKRU is only applied to ept enabled guest so |
| 6169 | * there is no pkey in EPT page table for L1 guest or EPT |
| 6170 | * shadow page table for L2 guest. |
| 6171 | */ |
| 6172 | if (vcpu_match_mmio_gva(vcpu, gva) |
| 6173 | && !permission_fault(vcpu, vcpu->arch.walk_mmu, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6174 | vcpu->arch.mmio_access, 0, access)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6175 | *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | |
| 6176 | (gva & (PAGE_SIZE - 1)); |
| 6177 | trace_vcpu_match_mmio(gva, *gpa, write, false); |
| 6178 | return 1; |
| 6179 | } |
| 6180 | |
| 6181 | *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
| 6182 | |
| 6183 | if (*gpa == UNMAPPED_GVA) |
| 6184 | return -1; |
| 6185 | |
| 6186 | return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write); |
| 6187 | } |
| 6188 | |
| 6189 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 6190 | const void *val, int bytes) |
| 6191 | { |
| 6192 | int ret; |
| 6193 | |
| 6194 | ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes); |
| 6195 | if (ret < 0) |
| 6196 | return 0; |
| 6197 | kvm_page_track_write(vcpu, gpa, val, bytes); |
| 6198 | return 1; |
| 6199 | } |
| 6200 | |
| 6201 | struct read_write_emulator_ops { |
| 6202 | int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val, |
| 6203 | int bytes); |
| 6204 | int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 6205 | void *val, int bytes); |
| 6206 | int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 6207 | int bytes, void *val); |
| 6208 | int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 6209 | void *val, int bytes); |
| 6210 | bool write; |
| 6211 | }; |
| 6212 | |
| 6213 | static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes) |
| 6214 | { |
| 6215 | if (vcpu->mmio_read_completed) { |
| 6216 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, |
| 6217 | vcpu->mmio_fragments[0].gpa, val); |
| 6218 | vcpu->mmio_read_completed = 0; |
| 6219 | return 1; |
| 6220 | } |
| 6221 | |
| 6222 | return 0; |
| 6223 | } |
| 6224 | |
| 6225 | static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 6226 | void *val, int bytes) |
| 6227 | { |
| 6228 | return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes); |
| 6229 | } |
| 6230 | |
| 6231 | static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 6232 | void *val, int bytes) |
| 6233 | { |
| 6234 | return emulator_write_phys(vcpu, gpa, val, bytes); |
| 6235 | } |
| 6236 | |
| 6237 | static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val) |
| 6238 | { |
| 6239 | trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val); |
| 6240 | return vcpu_mmio_write(vcpu, gpa, bytes, val); |
| 6241 | } |
| 6242 | |
| 6243 | static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 6244 | void *val, int bytes) |
| 6245 | { |
| 6246 | trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL); |
| 6247 | return X86EMUL_IO_NEEDED; |
| 6248 | } |
| 6249 | |
| 6250 | static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 6251 | void *val, int bytes) |
| 6252 | { |
| 6253 | struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; |
| 6254 | |
| 6255 | memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); |
| 6256 | return X86EMUL_CONTINUE; |
| 6257 | } |
| 6258 | |
| 6259 | static const struct read_write_emulator_ops read_emultor = { |
| 6260 | .read_write_prepare = read_prepare, |
| 6261 | .read_write_emulate = read_emulate, |
| 6262 | .read_write_mmio = vcpu_mmio_read, |
| 6263 | .read_write_exit_mmio = read_exit_mmio, |
| 6264 | }; |
| 6265 | |
| 6266 | static const struct read_write_emulator_ops write_emultor = { |
| 6267 | .read_write_emulate = write_emulate, |
| 6268 | .read_write_mmio = write_mmio, |
| 6269 | .read_write_exit_mmio = write_exit_mmio, |
| 6270 | .write = true, |
| 6271 | }; |
| 6272 | |
| 6273 | static int emulator_read_write_onepage(unsigned long addr, void *val, |
| 6274 | unsigned int bytes, |
| 6275 | struct x86_exception *exception, |
| 6276 | struct kvm_vcpu *vcpu, |
| 6277 | const struct read_write_emulator_ops *ops) |
| 6278 | { |
| 6279 | gpa_t gpa; |
| 6280 | int handled, ret; |
| 6281 | bool write = ops->write; |
| 6282 | struct kvm_mmio_fragment *frag; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6283 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6284 | |
| 6285 | /* |
| 6286 | * If the exit was due to a NPF we may already have a GPA. |
| 6287 | * If the GPA is present, use it to avoid the GVA to GPA table walk. |
| 6288 | * Note, this cannot be used on string operations since string |
| 6289 | * operation using rep will only have the initial GPA from the NPF |
| 6290 | * occurred. |
| 6291 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6292 | if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && |
| 6293 | (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { |
| 6294 | gpa = ctxt->gpa_val; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6295 | ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write); |
| 6296 | } else { |
| 6297 | ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write); |
| 6298 | if (ret < 0) |
| 6299 | return X86EMUL_PROPAGATE_FAULT; |
| 6300 | } |
| 6301 | |
| 6302 | if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) |
| 6303 | return X86EMUL_CONTINUE; |
| 6304 | |
| 6305 | /* |
| 6306 | * Is this MMIO handled locally? |
| 6307 | */ |
| 6308 | handled = ops->read_write_mmio(vcpu, gpa, bytes, val); |
| 6309 | if (handled == bytes) |
| 6310 | return X86EMUL_CONTINUE; |
| 6311 | |
| 6312 | gpa += handled; |
| 6313 | bytes -= handled; |
| 6314 | val += handled; |
| 6315 | |
| 6316 | WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); |
| 6317 | frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; |
| 6318 | frag->gpa = gpa; |
| 6319 | frag->data = val; |
| 6320 | frag->len = bytes; |
| 6321 | return X86EMUL_CONTINUE; |
| 6322 | } |
| 6323 | |
| 6324 | static int emulator_read_write(struct x86_emulate_ctxt *ctxt, |
| 6325 | unsigned long addr, |
| 6326 | void *val, unsigned int bytes, |
| 6327 | struct x86_exception *exception, |
| 6328 | const struct read_write_emulator_ops *ops) |
| 6329 | { |
| 6330 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6331 | gpa_t gpa; |
| 6332 | int rc; |
| 6333 | |
| 6334 | if (ops->read_write_prepare && |
| 6335 | ops->read_write_prepare(vcpu, val, bytes)) |
| 6336 | return X86EMUL_CONTINUE; |
| 6337 | |
| 6338 | vcpu->mmio_nr_fragments = 0; |
| 6339 | |
| 6340 | /* Crossing a page boundary? */ |
| 6341 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { |
| 6342 | int now; |
| 6343 | |
| 6344 | now = -addr & ~PAGE_MASK; |
| 6345 | rc = emulator_read_write_onepage(addr, val, now, exception, |
| 6346 | vcpu, ops); |
| 6347 | |
| 6348 | if (rc != X86EMUL_CONTINUE) |
| 6349 | return rc; |
| 6350 | addr += now; |
| 6351 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
| 6352 | addr = (u32)addr; |
| 6353 | val += now; |
| 6354 | bytes -= now; |
| 6355 | } |
| 6356 | |
| 6357 | rc = emulator_read_write_onepage(addr, val, bytes, exception, |
| 6358 | vcpu, ops); |
| 6359 | if (rc != X86EMUL_CONTINUE) |
| 6360 | return rc; |
| 6361 | |
| 6362 | if (!vcpu->mmio_nr_fragments) |
| 6363 | return rc; |
| 6364 | |
| 6365 | gpa = vcpu->mmio_fragments[0].gpa; |
| 6366 | |
| 6367 | vcpu->mmio_needed = 1; |
| 6368 | vcpu->mmio_cur_fragment = 0; |
| 6369 | |
| 6370 | vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); |
| 6371 | vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; |
| 6372 | vcpu->run->exit_reason = KVM_EXIT_MMIO; |
| 6373 | vcpu->run->mmio.phys_addr = gpa; |
| 6374 | |
| 6375 | return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); |
| 6376 | } |
| 6377 | |
| 6378 | static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt, |
| 6379 | unsigned long addr, |
| 6380 | void *val, |
| 6381 | unsigned int bytes, |
| 6382 | struct x86_exception *exception) |
| 6383 | { |
| 6384 | return emulator_read_write(ctxt, addr, val, bytes, |
| 6385 | exception, &read_emultor); |
| 6386 | } |
| 6387 | |
| 6388 | static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt, |
| 6389 | unsigned long addr, |
| 6390 | const void *val, |
| 6391 | unsigned int bytes, |
| 6392 | struct x86_exception *exception) |
| 6393 | { |
| 6394 | return emulator_read_write(ctxt, addr, (void *)val, bytes, |
| 6395 | exception, &write_emultor); |
| 6396 | } |
| 6397 | |
| 6398 | #define CMPXCHG_TYPE(t, ptr, old, new) \ |
| 6399 | (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old)) |
| 6400 | |
| 6401 | #ifdef CONFIG_X86_64 |
| 6402 | # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new) |
| 6403 | #else |
| 6404 | # define CMPXCHG64(ptr, old, new) \ |
| 6405 | (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old)) |
| 6406 | #endif |
| 6407 | |
| 6408 | static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt, |
| 6409 | unsigned long addr, |
| 6410 | const void *old, |
| 6411 | const void *new, |
| 6412 | unsigned int bytes, |
| 6413 | struct x86_exception *exception) |
| 6414 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6415 | struct kvm_host_map map; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6416 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6417 | u64 page_line_mask; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6418 | gpa_t gpa; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6419 | char *kaddr; |
| 6420 | bool exchanged; |
| 6421 | |
| 6422 | /* guests cmpxchg8b have to be emulated atomically */ |
| 6423 | if (bytes > 8 || (bytes & (bytes - 1))) |
| 6424 | goto emul_write; |
| 6425 | |
| 6426 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL); |
| 6427 | |
| 6428 | if (gpa == UNMAPPED_GVA || |
| 6429 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) |
| 6430 | goto emul_write; |
| 6431 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6432 | /* |
| 6433 | * Emulate the atomic as a straight write to avoid #AC if SLD is |
| 6434 | * enabled in the host and the access splits a cache line. |
| 6435 | */ |
| 6436 | if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) |
| 6437 | page_line_mask = ~(cache_line_size() - 1); |
| 6438 | else |
| 6439 | page_line_mask = PAGE_MASK; |
| 6440 | |
| 6441 | if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6442 | goto emul_write; |
| 6443 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6444 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6445 | goto emul_write; |
| 6446 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6447 | kaddr = map.hva + offset_in_page(gpa); |
| 6448 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6449 | switch (bytes) { |
| 6450 | case 1: |
| 6451 | exchanged = CMPXCHG_TYPE(u8, kaddr, old, new); |
| 6452 | break; |
| 6453 | case 2: |
| 6454 | exchanged = CMPXCHG_TYPE(u16, kaddr, old, new); |
| 6455 | break; |
| 6456 | case 4: |
| 6457 | exchanged = CMPXCHG_TYPE(u32, kaddr, old, new); |
| 6458 | break; |
| 6459 | case 8: |
| 6460 | exchanged = CMPXCHG64(kaddr, old, new); |
| 6461 | break; |
| 6462 | default: |
| 6463 | BUG(); |
| 6464 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6465 | |
| 6466 | kvm_vcpu_unmap(vcpu, &map, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6467 | |
| 6468 | if (!exchanged) |
| 6469 | return X86EMUL_CMPXCHG_FAILED; |
| 6470 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6471 | kvm_page_track_write(vcpu, gpa, new, bytes); |
| 6472 | |
| 6473 | return X86EMUL_CONTINUE; |
| 6474 | |
| 6475 | emul_write: |
| 6476 | printk_once(KERN_WARNING "kvm: emulating exchange as write\n"); |
| 6477 | |
| 6478 | return emulator_write_emulated(ctxt, addr, new, bytes, exception); |
| 6479 | } |
| 6480 | |
| 6481 | static int kernel_pio(struct kvm_vcpu *vcpu, void *pd) |
| 6482 | { |
| 6483 | int r = 0, i; |
| 6484 | |
| 6485 | for (i = 0; i < vcpu->arch.pio.count; i++) { |
| 6486 | if (vcpu->arch.pio.in) |
| 6487 | r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port, |
| 6488 | vcpu->arch.pio.size, pd); |
| 6489 | else |
| 6490 | r = kvm_io_bus_write(vcpu, KVM_PIO_BUS, |
| 6491 | vcpu->arch.pio.port, vcpu->arch.pio.size, |
| 6492 | pd); |
| 6493 | if (r) |
| 6494 | break; |
| 6495 | pd += vcpu->arch.pio.size; |
| 6496 | } |
| 6497 | return r; |
| 6498 | } |
| 6499 | |
| 6500 | static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, |
| 6501 | unsigned short port, void *val, |
| 6502 | unsigned int count, bool in) |
| 6503 | { |
| 6504 | vcpu->arch.pio.port = port; |
| 6505 | vcpu->arch.pio.in = in; |
| 6506 | vcpu->arch.pio.count = count; |
| 6507 | vcpu->arch.pio.size = size; |
| 6508 | |
| 6509 | if (!kernel_pio(vcpu, vcpu->arch.pio_data)) { |
| 6510 | vcpu->arch.pio.count = 0; |
| 6511 | return 1; |
| 6512 | } |
| 6513 | |
| 6514 | vcpu->run->exit_reason = KVM_EXIT_IO; |
| 6515 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
| 6516 | vcpu->run->io.size = size; |
| 6517 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; |
| 6518 | vcpu->run->io.count = count; |
| 6519 | vcpu->run->io.port = port; |
| 6520 | |
| 6521 | return 0; |
| 6522 | } |
| 6523 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6524 | static int emulator_pio_in(struct kvm_vcpu *vcpu, int size, |
| 6525 | unsigned short port, void *val, unsigned int count) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6526 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6527 | int ret; |
| 6528 | |
| 6529 | if (vcpu->arch.pio.count) |
| 6530 | goto data_avail; |
| 6531 | |
| 6532 | memset(vcpu->arch.pio_data, 0, size * count); |
| 6533 | |
| 6534 | ret = emulator_pio_in_out(vcpu, size, port, val, count, true); |
| 6535 | if (ret) { |
| 6536 | data_avail: |
| 6537 | memcpy(val, vcpu->arch.pio_data, size * count); |
| 6538 | trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data); |
| 6539 | vcpu->arch.pio.count = 0; |
| 6540 | return 1; |
| 6541 | } |
| 6542 | |
| 6543 | return 0; |
| 6544 | } |
| 6545 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6546 | static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
| 6547 | int size, unsigned short port, void *val, |
| 6548 | unsigned int count) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6549 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6550 | return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6551 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6552 | } |
| 6553 | |
| 6554 | static int emulator_pio_out(struct kvm_vcpu *vcpu, int size, |
| 6555 | unsigned short port, const void *val, |
| 6556 | unsigned int count) |
| 6557 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6558 | memcpy(vcpu->arch.pio_data, val, size * count); |
| 6559 | trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data); |
| 6560 | return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false); |
| 6561 | } |
| 6562 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6563 | static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt, |
| 6564 | int size, unsigned short port, |
| 6565 | const void *val, unsigned int count) |
| 6566 | { |
| 6567 | return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count); |
| 6568 | } |
| 6569 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6570 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 6571 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6572 | return kvm_x86_ops.get_segment_base(vcpu, seg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6573 | } |
| 6574 | |
| 6575 | static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address) |
| 6576 | { |
| 6577 | kvm_mmu_invlpg(emul_to_vcpu(ctxt), address); |
| 6578 | } |
| 6579 | |
| 6580 | static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu) |
| 6581 | { |
| 6582 | if (!need_emulate_wbinvd(vcpu)) |
| 6583 | return X86EMUL_CONTINUE; |
| 6584 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6585 | if (kvm_x86_ops.has_wbinvd_exit()) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6586 | int cpu = get_cpu(); |
| 6587 | |
| 6588 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); |
| 6589 | smp_call_function_many(vcpu->arch.wbinvd_dirty_mask, |
| 6590 | wbinvd_ipi, NULL, 1); |
| 6591 | put_cpu(); |
| 6592 | cpumask_clear(vcpu->arch.wbinvd_dirty_mask); |
| 6593 | } else |
| 6594 | wbinvd(); |
| 6595 | return X86EMUL_CONTINUE; |
| 6596 | } |
| 6597 | |
| 6598 | int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu) |
| 6599 | { |
| 6600 | kvm_emulate_wbinvd_noskip(vcpu); |
| 6601 | return kvm_skip_emulated_instruction(vcpu); |
| 6602 | } |
| 6603 | EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd); |
| 6604 | |
| 6605 | |
| 6606 | |
| 6607 | static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt) |
| 6608 | { |
| 6609 | kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt)); |
| 6610 | } |
| 6611 | |
| 6612 | static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, |
| 6613 | unsigned long *dest) |
| 6614 | { |
| 6615 | return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest); |
| 6616 | } |
| 6617 | |
| 6618 | static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, |
| 6619 | unsigned long value) |
| 6620 | { |
| 6621 | |
| 6622 | return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value); |
| 6623 | } |
| 6624 | |
| 6625 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
| 6626 | { |
| 6627 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
| 6628 | } |
| 6629 | |
| 6630 | static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr) |
| 6631 | { |
| 6632 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6633 | unsigned long value; |
| 6634 | |
| 6635 | switch (cr) { |
| 6636 | case 0: |
| 6637 | value = kvm_read_cr0(vcpu); |
| 6638 | break; |
| 6639 | case 2: |
| 6640 | value = vcpu->arch.cr2; |
| 6641 | break; |
| 6642 | case 3: |
| 6643 | value = kvm_read_cr3(vcpu); |
| 6644 | break; |
| 6645 | case 4: |
| 6646 | value = kvm_read_cr4(vcpu); |
| 6647 | break; |
| 6648 | case 8: |
| 6649 | value = kvm_get_cr8(vcpu); |
| 6650 | break; |
| 6651 | default: |
| 6652 | kvm_err("%s: unexpected cr %u\n", __func__, cr); |
| 6653 | return 0; |
| 6654 | } |
| 6655 | |
| 6656 | return value; |
| 6657 | } |
| 6658 | |
| 6659 | static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val) |
| 6660 | { |
| 6661 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6662 | int res = 0; |
| 6663 | |
| 6664 | switch (cr) { |
| 6665 | case 0: |
| 6666 | res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val)); |
| 6667 | break; |
| 6668 | case 2: |
| 6669 | vcpu->arch.cr2 = val; |
| 6670 | break; |
| 6671 | case 3: |
| 6672 | res = kvm_set_cr3(vcpu, val); |
| 6673 | break; |
| 6674 | case 4: |
| 6675 | res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val)); |
| 6676 | break; |
| 6677 | case 8: |
| 6678 | res = kvm_set_cr8(vcpu, val); |
| 6679 | break; |
| 6680 | default: |
| 6681 | kvm_err("%s: unexpected cr %u\n", __func__, cr); |
| 6682 | res = -1; |
| 6683 | } |
| 6684 | |
| 6685 | return res; |
| 6686 | } |
| 6687 | |
| 6688 | static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt) |
| 6689 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6690 | return kvm_x86_ops.get_cpl(emul_to_vcpu(ctxt)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6691 | } |
| 6692 | |
| 6693 | static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
| 6694 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6695 | kvm_x86_ops.get_gdt(emul_to_vcpu(ctxt), dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6696 | } |
| 6697 | |
| 6698 | static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
| 6699 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6700 | kvm_x86_ops.get_idt(emul_to_vcpu(ctxt), dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6701 | } |
| 6702 | |
| 6703 | static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
| 6704 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6705 | kvm_x86_ops.set_gdt(emul_to_vcpu(ctxt), dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6706 | } |
| 6707 | |
| 6708 | static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
| 6709 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6710 | kvm_x86_ops.set_idt(emul_to_vcpu(ctxt), dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6711 | } |
| 6712 | |
| 6713 | static unsigned long emulator_get_cached_segment_base( |
| 6714 | struct x86_emulate_ctxt *ctxt, int seg) |
| 6715 | { |
| 6716 | return get_segment_base(emul_to_vcpu(ctxt), seg); |
| 6717 | } |
| 6718 | |
| 6719 | static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector, |
| 6720 | struct desc_struct *desc, u32 *base3, |
| 6721 | int seg) |
| 6722 | { |
| 6723 | struct kvm_segment var; |
| 6724 | |
| 6725 | kvm_get_segment(emul_to_vcpu(ctxt), &var, seg); |
| 6726 | *selector = var.selector; |
| 6727 | |
| 6728 | if (var.unusable) { |
| 6729 | memset(desc, 0, sizeof(*desc)); |
| 6730 | if (base3) |
| 6731 | *base3 = 0; |
| 6732 | return false; |
| 6733 | } |
| 6734 | |
| 6735 | if (var.g) |
| 6736 | var.limit >>= 12; |
| 6737 | set_desc_limit(desc, var.limit); |
| 6738 | set_desc_base(desc, (unsigned long)var.base); |
| 6739 | #ifdef CONFIG_X86_64 |
| 6740 | if (base3) |
| 6741 | *base3 = var.base >> 32; |
| 6742 | #endif |
| 6743 | desc->type = var.type; |
| 6744 | desc->s = var.s; |
| 6745 | desc->dpl = var.dpl; |
| 6746 | desc->p = var.present; |
| 6747 | desc->avl = var.avl; |
| 6748 | desc->l = var.l; |
| 6749 | desc->d = var.db; |
| 6750 | desc->g = var.g; |
| 6751 | |
| 6752 | return true; |
| 6753 | } |
| 6754 | |
| 6755 | static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector, |
| 6756 | struct desc_struct *desc, u32 base3, |
| 6757 | int seg) |
| 6758 | { |
| 6759 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6760 | struct kvm_segment var; |
| 6761 | |
| 6762 | var.selector = selector; |
| 6763 | var.base = get_desc_base(desc); |
| 6764 | #ifdef CONFIG_X86_64 |
| 6765 | var.base |= ((u64)base3) << 32; |
| 6766 | #endif |
| 6767 | var.limit = get_desc_limit(desc); |
| 6768 | if (desc->g) |
| 6769 | var.limit = (var.limit << 12) | 0xfff; |
| 6770 | var.type = desc->type; |
| 6771 | var.dpl = desc->dpl; |
| 6772 | var.db = desc->d; |
| 6773 | var.s = desc->s; |
| 6774 | var.l = desc->l; |
| 6775 | var.g = desc->g; |
| 6776 | var.avl = desc->avl; |
| 6777 | var.present = desc->p; |
| 6778 | var.unusable = !var.present; |
| 6779 | var.padding = 0; |
| 6780 | |
| 6781 | kvm_set_segment(vcpu, &var, seg); |
| 6782 | return; |
| 6783 | } |
| 6784 | |
| 6785 | static int emulator_get_msr(struct x86_emulate_ctxt *ctxt, |
| 6786 | u32 msr_index, u64 *pdata) |
| 6787 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6788 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6789 | int r; |
| 6790 | |
| 6791 | r = kvm_get_msr(vcpu, msr_index, pdata); |
| 6792 | |
| 6793 | if (r && kvm_get_msr_user_space(vcpu, msr_index, r)) { |
| 6794 | /* Bounce to user space */ |
| 6795 | return X86EMUL_IO_NEEDED; |
| 6796 | } |
| 6797 | |
| 6798 | return r; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6799 | } |
| 6800 | |
| 6801 | static int emulator_set_msr(struct x86_emulate_ctxt *ctxt, |
| 6802 | u32 msr_index, u64 data) |
| 6803 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6804 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6805 | int r; |
| 6806 | |
| 6807 | r = kvm_set_msr(vcpu, msr_index, data); |
| 6808 | |
| 6809 | if (r && kvm_set_msr_user_space(vcpu, msr_index, data, r)) { |
| 6810 | /* Bounce to user space */ |
| 6811 | return X86EMUL_IO_NEEDED; |
| 6812 | } |
| 6813 | |
| 6814 | return r; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6815 | } |
| 6816 | |
| 6817 | static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt) |
| 6818 | { |
| 6819 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6820 | |
| 6821 | return vcpu->arch.smbase; |
| 6822 | } |
| 6823 | |
| 6824 | static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase) |
| 6825 | { |
| 6826 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6827 | |
| 6828 | vcpu->arch.smbase = smbase; |
| 6829 | } |
| 6830 | |
| 6831 | static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt, |
| 6832 | u32 pmc) |
| 6833 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6834 | return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6835 | } |
| 6836 | |
| 6837 | static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt, |
| 6838 | u32 pmc, u64 *pdata) |
| 6839 | { |
| 6840 | return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata); |
| 6841 | } |
| 6842 | |
| 6843 | static void emulator_halt(struct x86_emulate_ctxt *ctxt) |
| 6844 | { |
| 6845 | emul_to_vcpu(ctxt)->arch.halt_request = 1; |
| 6846 | } |
| 6847 | |
| 6848 | static int emulator_intercept(struct x86_emulate_ctxt *ctxt, |
| 6849 | struct x86_instruction_info *info, |
| 6850 | enum x86_intercept_stage stage) |
| 6851 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6852 | return kvm_x86_ops.check_intercept(emul_to_vcpu(ctxt), info, stage, |
| 6853 | &ctxt->exception); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6854 | } |
| 6855 | |
| 6856 | static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6857 | u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, |
| 6858 | bool exact_only) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6859 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6860 | return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only); |
| 6861 | } |
| 6862 | |
| 6863 | static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt) |
| 6864 | { |
| 6865 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM); |
| 6866 | } |
| 6867 | |
| 6868 | static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt) |
| 6869 | { |
| 6870 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE); |
| 6871 | } |
| 6872 | |
| 6873 | static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt) |
| 6874 | { |
| 6875 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6876 | } |
| 6877 | |
| 6878 | static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg) |
| 6879 | { |
| 6880 | return kvm_register_read(emul_to_vcpu(ctxt), reg); |
| 6881 | } |
| 6882 | |
| 6883 | static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val) |
| 6884 | { |
| 6885 | kvm_register_write(emul_to_vcpu(ctxt), reg, val); |
| 6886 | } |
| 6887 | |
| 6888 | static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked) |
| 6889 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6890 | kvm_x86_ops.set_nmi_mask(emul_to_vcpu(ctxt), masked); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6891 | } |
| 6892 | |
| 6893 | static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt) |
| 6894 | { |
| 6895 | return emul_to_vcpu(ctxt)->arch.hflags; |
| 6896 | } |
| 6897 | |
| 6898 | static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags) |
| 6899 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 6900 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 6901 | |
| 6902 | vcpu->arch.hflags = emul_flags; |
| 6903 | kvm_mmu_reset_context(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6904 | } |
| 6905 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6906 | static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt, |
| 6907 | const char *smstate) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6908 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6909 | return kvm_x86_ops.pre_leave_smm(emul_to_vcpu(ctxt), smstate); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6910 | } |
| 6911 | |
| 6912 | static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt) |
| 6913 | { |
| 6914 | kvm_smm_changed(emul_to_vcpu(ctxt)); |
| 6915 | } |
| 6916 | |
| 6917 | static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr) |
| 6918 | { |
| 6919 | return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6920 | } |
| 6921 | |
| 6922 | static const struct x86_emulate_ops emulate_ops = { |
| 6923 | .read_gpr = emulator_read_gpr, |
| 6924 | .write_gpr = emulator_write_gpr, |
| 6925 | .read_std = emulator_read_std, |
| 6926 | .write_std = emulator_write_std, |
| 6927 | .read_phys = kvm_read_guest_phys_system, |
| 6928 | .fetch = kvm_fetch_guest_virt, |
| 6929 | .read_emulated = emulator_read_emulated, |
| 6930 | .write_emulated = emulator_write_emulated, |
| 6931 | .cmpxchg_emulated = emulator_cmpxchg_emulated, |
| 6932 | .invlpg = emulator_invlpg, |
| 6933 | .pio_in_emulated = emulator_pio_in_emulated, |
| 6934 | .pio_out_emulated = emulator_pio_out_emulated, |
| 6935 | .get_segment = emulator_get_segment, |
| 6936 | .set_segment = emulator_set_segment, |
| 6937 | .get_cached_segment_base = emulator_get_cached_segment_base, |
| 6938 | .get_gdt = emulator_get_gdt, |
| 6939 | .get_idt = emulator_get_idt, |
| 6940 | .set_gdt = emulator_set_gdt, |
| 6941 | .set_idt = emulator_set_idt, |
| 6942 | .get_cr = emulator_get_cr, |
| 6943 | .set_cr = emulator_set_cr, |
| 6944 | .cpl = emulator_get_cpl, |
| 6945 | .get_dr = emulator_get_dr, |
| 6946 | .set_dr = emulator_set_dr, |
| 6947 | .get_smbase = emulator_get_smbase, |
| 6948 | .set_smbase = emulator_set_smbase, |
| 6949 | .set_msr = emulator_set_msr, |
| 6950 | .get_msr = emulator_get_msr, |
| 6951 | .check_pmc = emulator_check_pmc, |
| 6952 | .read_pmc = emulator_read_pmc, |
| 6953 | .halt = emulator_halt, |
| 6954 | .wbinvd = emulator_wbinvd, |
| 6955 | .fix_hypercall = emulator_fix_hypercall, |
| 6956 | .intercept = emulator_intercept, |
| 6957 | .get_cpuid = emulator_get_cpuid, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6958 | .guest_has_long_mode = emulator_guest_has_long_mode, |
| 6959 | .guest_has_movbe = emulator_guest_has_movbe, |
| 6960 | .guest_has_fxsr = emulator_guest_has_fxsr, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6961 | .set_nmi_mask = emulator_set_nmi_mask, |
| 6962 | .get_hflags = emulator_get_hflags, |
| 6963 | .set_hflags = emulator_set_hflags, |
| 6964 | .pre_leave_smm = emulator_pre_leave_smm, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6965 | .post_leave_smm = emulator_post_leave_smm, |
| 6966 | .set_xcr = emulator_set_xcr, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6967 | }; |
| 6968 | |
| 6969 | static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask) |
| 6970 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6971 | u32 int_shadow = kvm_x86_ops.get_interrupt_shadow(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6972 | /* |
| 6973 | * an sti; sti; sequence only disable interrupts for the first |
| 6974 | * instruction. So, if the last instruction, be it emulated or |
| 6975 | * not, left the system with the INT_STI flag enabled, it |
| 6976 | * means that the last instruction is an sti. We should not |
| 6977 | * leave the flag on in this case. The same goes for mov ss |
| 6978 | */ |
| 6979 | if (int_shadow & mask) |
| 6980 | mask = 0; |
| 6981 | if (unlikely(int_shadow || mask)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6982 | kvm_x86_ops.set_interrupt_shadow(vcpu, mask); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6983 | if (!mask) |
| 6984 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 6985 | } |
| 6986 | } |
| 6987 | |
| 6988 | static bool inject_emulated_exception(struct kvm_vcpu *vcpu) |
| 6989 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6990 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6991 | if (ctxt->exception.vector == PF_VECTOR) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6992 | return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6993 | |
| 6994 | if (ctxt->exception.error_code_valid) |
| 6995 | kvm_queue_exception_e(vcpu, ctxt->exception.vector, |
| 6996 | ctxt->exception.error_code); |
| 6997 | else |
| 6998 | kvm_queue_exception(vcpu, ctxt->exception.vector); |
| 6999 | return false; |
| 7000 | } |
| 7001 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7002 | static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu) |
| 7003 | { |
| 7004 | struct x86_emulate_ctxt *ctxt; |
| 7005 | |
| 7006 | ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT); |
| 7007 | if (!ctxt) { |
| 7008 | pr_err("kvm: failed to allocate vcpu's emulator\n"); |
| 7009 | return NULL; |
| 7010 | } |
| 7011 | |
| 7012 | ctxt->vcpu = vcpu; |
| 7013 | ctxt->ops = &emulate_ops; |
| 7014 | vcpu->arch.emulate_ctxt = ctxt; |
| 7015 | |
| 7016 | return ctxt; |
| 7017 | } |
| 7018 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7019 | static void init_emulate_ctxt(struct kvm_vcpu *vcpu) |
| 7020 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7021 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7022 | int cs_db, cs_l; |
| 7023 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7024 | kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7025 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7026 | ctxt->gpa_available = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7027 | ctxt->eflags = kvm_get_rflags(vcpu); |
| 7028 | ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; |
| 7029 | |
| 7030 | ctxt->eip = kvm_rip_read(vcpu); |
| 7031 | ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : |
| 7032 | (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : |
| 7033 | (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 : |
| 7034 | cs_db ? X86EMUL_MODE_PROT32 : |
| 7035 | X86EMUL_MODE_PROT16; |
| 7036 | BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK); |
| 7037 | BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK); |
| 7038 | BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK); |
| 7039 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7040 | ctxt->interruptibility = 0; |
| 7041 | ctxt->have_exception = false; |
| 7042 | ctxt->exception.vector = -1; |
| 7043 | ctxt->perm_ok = false; |
| 7044 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7045 | init_decode_cache(ctxt); |
| 7046 | vcpu->arch.emulate_regs_need_sync_from_vcpu = false; |
| 7047 | } |
| 7048 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7049 | void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7050 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7051 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7052 | int ret; |
| 7053 | |
| 7054 | init_emulate_ctxt(vcpu); |
| 7055 | |
| 7056 | ctxt->op_bytes = 2; |
| 7057 | ctxt->ad_bytes = 2; |
| 7058 | ctxt->_eip = ctxt->eip + inc_eip; |
| 7059 | ret = emulate_int_real(ctxt, irq); |
| 7060 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7061 | if (ret != X86EMUL_CONTINUE) { |
| 7062 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 7063 | } else { |
| 7064 | ctxt->eip = ctxt->_eip; |
| 7065 | kvm_rip_write(vcpu, ctxt->eip); |
| 7066 | kvm_set_rflags(vcpu, ctxt->eflags); |
| 7067 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7068 | } |
| 7069 | EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt); |
| 7070 | |
| 7071 | static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type) |
| 7072 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7073 | ++vcpu->stat.insn_emulation_fail; |
| 7074 | trace_kvm_emulate_insn_failed(vcpu); |
| 7075 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7076 | if (emulation_type & EMULTYPE_VMWARE_GP) { |
| 7077 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 7078 | return 1; |
| 7079 | } |
| 7080 | |
| 7081 | if (emulation_type & EMULTYPE_SKIP) { |
| 7082 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7083 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7084 | vcpu->run->internal.ndata = 0; |
| 7085 | return 0; |
| 7086 | } |
| 7087 | |
| 7088 | kvm_queue_exception(vcpu, UD_VECTOR); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7089 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7090 | if (!is_guest_mode(vcpu) && kvm_x86_ops.get_cpl(vcpu) == 0) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7091 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7092 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7093 | vcpu->run->internal.ndata = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7094 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7095 | } |
| 7096 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7097 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7098 | } |
| 7099 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7100 | static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7101 | bool write_fault_to_shadow_pgtable, |
| 7102 | int emulation_type) |
| 7103 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7104 | gpa_t gpa = cr2_or_gpa; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7105 | kvm_pfn_t pfn; |
| 7106 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7107 | if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7108 | return false; |
| 7109 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7110 | if (WARN_ON_ONCE(is_guest_mode(vcpu)) || |
| 7111 | WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7112 | return false; |
| 7113 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7114 | if (!vcpu->arch.mmu->direct_map) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7115 | /* |
| 7116 | * Write permission should be allowed since only |
| 7117 | * write access need to be emulated. |
| 7118 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7119 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7120 | |
| 7121 | /* |
| 7122 | * If the mapping is invalid in guest, let cpu retry |
| 7123 | * it to generate fault. |
| 7124 | */ |
| 7125 | if (gpa == UNMAPPED_GVA) |
| 7126 | return true; |
| 7127 | } |
| 7128 | |
| 7129 | /* |
| 7130 | * Do not retry the unhandleable instruction if it faults on the |
| 7131 | * readonly host memory, otherwise it will goto a infinite loop: |
| 7132 | * retry instruction -> write #PF -> emulation fail -> retry |
| 7133 | * instruction -> ... |
| 7134 | */ |
| 7135 | pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); |
| 7136 | |
| 7137 | /* |
| 7138 | * If the instruction failed on the error pfn, it can not be fixed, |
| 7139 | * report the error to userspace. |
| 7140 | */ |
| 7141 | if (is_error_noslot_pfn(pfn)) |
| 7142 | return false; |
| 7143 | |
| 7144 | kvm_release_pfn_clean(pfn); |
| 7145 | |
| 7146 | /* The instructions are well-emulated on direct mmu. */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7147 | if (vcpu->arch.mmu->direct_map) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7148 | unsigned int indirect_shadow_pages; |
| 7149 | |
| 7150 | spin_lock(&vcpu->kvm->mmu_lock); |
| 7151 | indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages; |
| 7152 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 7153 | |
| 7154 | if (indirect_shadow_pages) |
| 7155 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); |
| 7156 | |
| 7157 | return true; |
| 7158 | } |
| 7159 | |
| 7160 | /* |
| 7161 | * if emulation was due to access to shadowed page table |
| 7162 | * and it failed try to unshadow page and re-enter the |
| 7163 | * guest to let CPU execute the instruction. |
| 7164 | */ |
| 7165 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); |
| 7166 | |
| 7167 | /* |
| 7168 | * If the access faults on its page table, it can not |
| 7169 | * be fixed by unprotecting shadow page and it should |
| 7170 | * be reported to userspace. |
| 7171 | */ |
| 7172 | return !write_fault_to_shadow_pgtable; |
| 7173 | } |
| 7174 | |
| 7175 | static bool retry_instruction(struct x86_emulate_ctxt *ctxt, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7176 | gpa_t cr2_or_gpa, int emulation_type) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7177 | { |
| 7178 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7179 | unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7180 | |
| 7181 | last_retry_eip = vcpu->arch.last_retry_eip; |
| 7182 | last_retry_addr = vcpu->arch.last_retry_addr; |
| 7183 | |
| 7184 | /* |
| 7185 | * If the emulation is caused by #PF and it is non-page_table |
| 7186 | * writing instruction, it means the VM-EXIT is caused by shadow |
| 7187 | * page protected, we can zap the shadow page and retry this |
| 7188 | * instruction directly. |
| 7189 | * |
| 7190 | * Note: if the guest uses a non-page-table modifying instruction |
| 7191 | * on the PDE that points to the instruction, then we will unmap |
| 7192 | * the instruction and go to an infinite loop. So, we cache the |
| 7193 | * last retried eip and the last fault address, if we meet the eip |
| 7194 | * and the address again, we can break out of the potential infinite |
| 7195 | * loop. |
| 7196 | */ |
| 7197 | vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0; |
| 7198 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7199 | if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7200 | return false; |
| 7201 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7202 | if (WARN_ON_ONCE(is_guest_mode(vcpu)) || |
| 7203 | WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7204 | return false; |
| 7205 | |
| 7206 | if (x86_page_table_writing_insn(ctxt)) |
| 7207 | return false; |
| 7208 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7209 | if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7210 | return false; |
| 7211 | |
| 7212 | vcpu->arch.last_retry_eip = ctxt->eip; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7213 | vcpu->arch.last_retry_addr = cr2_or_gpa; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7214 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7215 | if (!vcpu->arch.mmu->direct_map) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7216 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7217 | |
| 7218 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); |
| 7219 | |
| 7220 | return true; |
| 7221 | } |
| 7222 | |
| 7223 | static int complete_emulated_mmio(struct kvm_vcpu *vcpu); |
| 7224 | static int complete_emulated_pio(struct kvm_vcpu *vcpu); |
| 7225 | |
| 7226 | static void kvm_smm_changed(struct kvm_vcpu *vcpu) |
| 7227 | { |
| 7228 | if (!(vcpu->arch.hflags & HF_SMM_MASK)) { |
| 7229 | /* This is a good place to trace that we are exiting SMM. */ |
| 7230 | trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false); |
| 7231 | |
| 7232 | /* Process a latched INIT or SMI, if any. */ |
| 7233 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 7234 | } |
| 7235 | |
| 7236 | kvm_mmu_reset_context(vcpu); |
| 7237 | } |
| 7238 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7239 | static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7, |
| 7240 | unsigned long *db) |
| 7241 | { |
| 7242 | u32 dr6 = 0; |
| 7243 | int i; |
| 7244 | u32 enable, rwlen; |
| 7245 | |
| 7246 | enable = dr7; |
| 7247 | rwlen = dr7 >> 16; |
| 7248 | for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4) |
| 7249 | if ((enable & 3) && (rwlen & 15) == type && db[i] == addr) |
| 7250 | dr6 |= (1 << i); |
| 7251 | return dr6; |
| 7252 | } |
| 7253 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7254 | static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7255 | { |
| 7256 | struct kvm_run *kvm_run = vcpu->run; |
| 7257 | |
| 7258 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { |
| 7259 | kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7260 | kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7261 | kvm_run->debug.arch.exception = DB_VECTOR; |
| 7262 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7263 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7264 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7265 | kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS); |
| 7266 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7267 | } |
| 7268 | |
| 7269 | int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 7270 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7271 | unsigned long rflags = kvm_x86_ops.get_rflags(vcpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7272 | int r; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7273 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7274 | r = kvm_x86_ops.skip_emulated_instruction(vcpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7275 | if (unlikely(!r)) |
| 7276 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7277 | |
| 7278 | /* |
| 7279 | * rflags is the old, "raw" value of the flags. The new value has |
| 7280 | * not been saved yet. |
| 7281 | * |
| 7282 | * This is correct even for TF set by the guest, because "the |
| 7283 | * processor will not generate this exception after the instruction |
| 7284 | * that sets the TF flag". |
| 7285 | */ |
| 7286 | if (unlikely(rflags & X86_EFLAGS_TF)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7287 | r = kvm_vcpu_do_singlestep(vcpu); |
| 7288 | return r; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7289 | } |
| 7290 | EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction); |
| 7291 | |
| 7292 | static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r) |
| 7293 | { |
| 7294 | if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && |
| 7295 | (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { |
| 7296 | struct kvm_run *kvm_run = vcpu->run; |
| 7297 | unsigned long eip = kvm_get_linear_rip(vcpu); |
| 7298 | u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0, |
| 7299 | vcpu->arch.guest_debug_dr7, |
| 7300 | vcpu->arch.eff_db); |
| 7301 | |
| 7302 | if (dr6 != 0) { |
| 7303 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM; |
| 7304 | kvm_run->debug.arch.pc = eip; |
| 7305 | kvm_run->debug.arch.exception = DB_VECTOR; |
| 7306 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7307 | *r = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7308 | return true; |
| 7309 | } |
| 7310 | } |
| 7311 | |
| 7312 | if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && |
| 7313 | !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) { |
| 7314 | unsigned long eip = kvm_get_linear_rip(vcpu); |
| 7315 | u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0, |
| 7316 | vcpu->arch.dr7, |
| 7317 | vcpu->arch.db); |
| 7318 | |
| 7319 | if (dr6 != 0) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7320 | kvm_queue_exception_p(vcpu, DB_VECTOR, dr6); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7321 | *r = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7322 | return true; |
| 7323 | } |
| 7324 | } |
| 7325 | |
| 7326 | return false; |
| 7327 | } |
| 7328 | |
| 7329 | static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt) |
| 7330 | { |
| 7331 | switch (ctxt->opcode_len) { |
| 7332 | case 1: |
| 7333 | switch (ctxt->b) { |
| 7334 | case 0xe4: /* IN */ |
| 7335 | case 0xe5: |
| 7336 | case 0xec: |
| 7337 | case 0xed: |
| 7338 | case 0xe6: /* OUT */ |
| 7339 | case 0xe7: |
| 7340 | case 0xee: |
| 7341 | case 0xef: |
| 7342 | case 0x6c: /* INS */ |
| 7343 | case 0x6d: |
| 7344 | case 0x6e: /* OUTS */ |
| 7345 | case 0x6f: |
| 7346 | return true; |
| 7347 | } |
| 7348 | break; |
| 7349 | case 2: |
| 7350 | switch (ctxt->b) { |
| 7351 | case 0x33: /* RDPMC */ |
| 7352 | return true; |
| 7353 | } |
| 7354 | break; |
| 7355 | } |
| 7356 | |
| 7357 | return false; |
| 7358 | } |
| 7359 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7360 | /* |
| 7361 | * Decode to be emulated instruction. Return EMULATION_OK if success. |
| 7362 | */ |
| 7363 | int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, |
| 7364 | void *insn, int insn_len) |
| 7365 | { |
| 7366 | int r = EMULATION_OK; |
| 7367 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
| 7368 | |
| 7369 | init_emulate_ctxt(vcpu); |
| 7370 | |
| 7371 | /* |
| 7372 | * We will reenter on the same instruction since we do not set |
| 7373 | * complete_userspace_io. This does not handle watchpoints yet, |
| 7374 | * those would be handled in the emulate_ops. |
| 7375 | */ |
| 7376 | if (!(emulation_type & EMULTYPE_SKIP) && |
| 7377 | kvm_vcpu_check_breakpoint(vcpu, &r)) |
| 7378 | return r; |
| 7379 | |
| 7380 | ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; |
| 7381 | |
| 7382 | r = x86_decode_insn(ctxt, insn, insn_len); |
| 7383 | |
| 7384 | trace_kvm_emulate_insn_start(vcpu); |
| 7385 | ++vcpu->stat.insn_emulation; |
| 7386 | |
| 7387 | return r; |
| 7388 | } |
| 7389 | EXPORT_SYMBOL_GPL(x86_decode_emulated_instruction); |
| 7390 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7391 | int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
| 7392 | int emulation_type, void *insn, int insn_len) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7393 | { |
| 7394 | int r; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7395 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7396 | bool writeback = true; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7397 | bool write_fault_to_spt; |
| 7398 | |
| 7399 | if (unlikely(!kvm_x86_ops.can_emulate_instruction(vcpu, insn, insn_len))) |
| 7400 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7401 | |
| 7402 | vcpu->arch.l1tf_flush_l1d = true; |
| 7403 | |
| 7404 | /* |
| 7405 | * Clear write_fault_to_shadow_pgtable here to ensure it is |
| 7406 | * never reused. |
| 7407 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7408 | write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7409 | vcpu->arch.write_fault_to_shadow_pgtable = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7410 | |
| 7411 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7412 | kvm_clear_exception_queue(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7413 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7414 | r = x86_decode_emulated_instruction(vcpu, emulation_type, |
| 7415 | insn, insn_len); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7416 | if (r != EMULATION_OK) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7417 | if ((emulation_type & EMULTYPE_TRAP_UD) || |
| 7418 | (emulation_type & EMULTYPE_TRAP_UD_FORCED)) { |
| 7419 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7420 | return 1; |
| 7421 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7422 | if (reexecute_instruction(vcpu, cr2_or_gpa, |
| 7423 | write_fault_to_spt, |
| 7424 | emulation_type)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7425 | return 1; |
| 7426 | if (ctxt->have_exception) { |
| 7427 | /* |
| 7428 | * #UD should result in just EMULATION_FAILED, and trap-like |
| 7429 | * exception should not be encountered during decode. |
| 7430 | */ |
| 7431 | WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || |
| 7432 | exception_type(ctxt->exception.vector) == EXCPT_TRAP); |
| 7433 | inject_emulated_exception(vcpu); |
| 7434 | return 1; |
| 7435 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7436 | return handle_emulation_failure(vcpu, emulation_type); |
| 7437 | } |
| 7438 | } |
| 7439 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7440 | if ((emulation_type & EMULTYPE_VMWARE_GP) && |
| 7441 | !is_vmware_backdoor_opcode(ctxt)) { |
| 7442 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 7443 | return 1; |
| 7444 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7445 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7446 | /* |
| 7447 | * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks |
| 7448 | * for kvm_skip_emulated_instruction(). The caller is responsible for |
| 7449 | * updating interruptibility state and injecting single-step #DBs. |
| 7450 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7451 | if (emulation_type & EMULTYPE_SKIP) { |
| 7452 | kvm_rip_write(vcpu, ctxt->_eip); |
| 7453 | if (ctxt->eflags & X86_EFLAGS_RF) |
| 7454 | kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7455 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7456 | } |
| 7457 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7458 | if (retry_instruction(ctxt, cr2_or_gpa, emulation_type)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7459 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7460 | |
| 7461 | /* this is needed for vmware backdoor interface to work since it |
| 7462 | changes registers values during IO operation */ |
| 7463 | if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { |
| 7464 | vcpu->arch.emulate_regs_need_sync_from_vcpu = false; |
| 7465 | emulator_invalidate_register_cache(ctxt); |
| 7466 | } |
| 7467 | |
| 7468 | restart: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7469 | if (emulation_type & EMULTYPE_PF) { |
| 7470 | /* Save the faulting GPA (cr2) in the address field */ |
| 7471 | ctxt->exception.address = cr2_or_gpa; |
| 7472 | |
| 7473 | /* With shadow page tables, cr2 contains a GVA or nGPA. */ |
| 7474 | if (vcpu->arch.mmu->direct_map) { |
| 7475 | ctxt->gpa_available = true; |
| 7476 | ctxt->gpa_val = cr2_or_gpa; |
| 7477 | } |
| 7478 | } else { |
| 7479 | /* Sanitize the address out of an abundance of paranoia. */ |
| 7480 | ctxt->exception.address = 0; |
| 7481 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7482 | |
| 7483 | r = x86_emulate_insn(ctxt); |
| 7484 | |
| 7485 | if (r == EMULATION_INTERCEPTED) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7486 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7487 | |
| 7488 | if (r == EMULATION_FAILED) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7489 | if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7490 | emulation_type)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7491 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7492 | |
| 7493 | return handle_emulation_failure(vcpu, emulation_type); |
| 7494 | } |
| 7495 | |
| 7496 | if (ctxt->have_exception) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7497 | r = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7498 | if (inject_emulated_exception(vcpu)) |
| 7499 | return r; |
| 7500 | } else if (vcpu->arch.pio.count) { |
| 7501 | if (!vcpu->arch.pio.in) { |
| 7502 | /* FIXME: return into emulator if single-stepping. */ |
| 7503 | vcpu->arch.pio.count = 0; |
| 7504 | } else { |
| 7505 | writeback = false; |
| 7506 | vcpu->arch.complete_userspace_io = complete_emulated_pio; |
| 7507 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7508 | r = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7509 | } else if (vcpu->mmio_needed) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7510 | ++vcpu->stat.mmio_exits; |
| 7511 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7512 | if (!vcpu->mmio_is_write) |
| 7513 | writeback = false; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7514 | r = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7515 | vcpu->arch.complete_userspace_io = complete_emulated_mmio; |
| 7516 | } else if (r == EMULATION_RESTART) |
| 7517 | goto restart; |
| 7518 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7519 | r = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7520 | |
| 7521 | if (writeback) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7522 | unsigned long rflags = kvm_x86_ops.get_rflags(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7523 | toggle_interruptibility(vcpu, ctxt->interruptibility); |
| 7524 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7525 | if (!ctxt->have_exception || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7526 | exception_type(ctxt->exception.vector) == EXCPT_TRAP) { |
| 7527 | kvm_rip_write(vcpu, ctxt->eip); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7528 | if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7529 | r = kvm_vcpu_do_singlestep(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7530 | if (kvm_x86_ops.update_emulated_instruction) |
| 7531 | kvm_x86_ops.update_emulated_instruction(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7532 | __kvm_set_rflags(vcpu, ctxt->eflags); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7533 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7534 | |
| 7535 | /* |
| 7536 | * For STI, interrupts are shadowed; so KVM_REQ_EVENT will |
| 7537 | * do nothing, and it will be requested again as soon as |
| 7538 | * the shadow expires. But we still need to check here, |
| 7539 | * because POPF has no interrupt shadow. |
| 7540 | */ |
| 7541 | if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) |
| 7542 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 7543 | } else |
| 7544 | vcpu->arch.emulate_regs_need_sync_to_vcpu = true; |
| 7545 | |
| 7546 | return r; |
| 7547 | } |
| 7548 | |
| 7549 | int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type) |
| 7550 | { |
| 7551 | return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0); |
| 7552 | } |
| 7553 | EXPORT_SYMBOL_GPL(kvm_emulate_instruction); |
| 7554 | |
| 7555 | int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, |
| 7556 | void *insn, int insn_len) |
| 7557 | { |
| 7558 | return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len); |
| 7559 | } |
| 7560 | EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer); |
| 7561 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7562 | static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu) |
| 7563 | { |
| 7564 | vcpu->arch.pio.count = 0; |
| 7565 | return 1; |
| 7566 | } |
| 7567 | |
| 7568 | static int complete_fast_pio_out(struct kvm_vcpu *vcpu) |
| 7569 | { |
| 7570 | vcpu->arch.pio.count = 0; |
| 7571 | |
| 7572 | if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) |
| 7573 | return 1; |
| 7574 | |
| 7575 | return kvm_skip_emulated_instruction(vcpu); |
| 7576 | } |
| 7577 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7578 | static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, |
| 7579 | unsigned short port) |
| 7580 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7581 | unsigned long val = kvm_rax_read(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7582 | int ret = emulator_pio_out(vcpu, size, port, &val, 1); |
| 7583 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7584 | if (ret) |
| 7585 | return ret; |
| 7586 | |
| 7587 | /* |
| 7588 | * Workaround userspace that relies on old KVM behavior of %rip being |
| 7589 | * incremented prior to exiting to userspace to handle "OUT 0x7e". |
| 7590 | */ |
| 7591 | if (port == 0x7e && |
| 7592 | kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { |
| 7593 | vcpu->arch.complete_userspace_io = |
| 7594 | complete_fast_pio_out_port_0x7e; |
| 7595 | kvm_skip_emulated_instruction(vcpu); |
| 7596 | } else { |
| 7597 | vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); |
| 7598 | vcpu->arch.complete_userspace_io = complete_fast_pio_out; |
| 7599 | } |
| 7600 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7601 | } |
| 7602 | |
| 7603 | static int complete_fast_pio_in(struct kvm_vcpu *vcpu) |
| 7604 | { |
| 7605 | unsigned long val; |
| 7606 | |
| 7607 | /* We should only ever be called with arch.pio.count equal to 1 */ |
| 7608 | BUG_ON(vcpu->arch.pio.count != 1); |
| 7609 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7610 | if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { |
| 7611 | vcpu->arch.pio.count = 0; |
| 7612 | return 1; |
| 7613 | } |
| 7614 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7615 | /* For size less than 4 we merge, else we zero extend */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7616 | val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7617 | |
| 7618 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7619 | * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7620 | * the copy and tracing |
| 7621 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7622 | emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7623 | kvm_rax_write(vcpu, val); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7624 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7625 | return kvm_skip_emulated_instruction(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7626 | } |
| 7627 | |
| 7628 | static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size, |
| 7629 | unsigned short port) |
| 7630 | { |
| 7631 | unsigned long val; |
| 7632 | int ret; |
| 7633 | |
| 7634 | /* For size less than 4 we merge, else we zero extend */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7635 | val = (size < 4) ? kvm_rax_read(vcpu) : 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7636 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7637 | ret = emulator_pio_in(vcpu, size, port, &val, 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7638 | if (ret) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7639 | kvm_rax_write(vcpu, val); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7640 | return ret; |
| 7641 | } |
| 7642 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7643 | vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7644 | vcpu->arch.complete_userspace_io = complete_fast_pio_in; |
| 7645 | |
| 7646 | return 0; |
| 7647 | } |
| 7648 | |
| 7649 | int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in) |
| 7650 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7651 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7652 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7653 | if (in) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7654 | ret = kvm_fast_pio_in(vcpu, size, port); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7655 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7656 | ret = kvm_fast_pio_out(vcpu, size, port); |
| 7657 | return ret && kvm_skip_emulated_instruction(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7658 | } |
| 7659 | EXPORT_SYMBOL_GPL(kvm_fast_pio); |
| 7660 | |
| 7661 | static int kvmclock_cpu_down_prep(unsigned int cpu) |
| 7662 | { |
| 7663 | __this_cpu_write(cpu_tsc_khz, 0); |
| 7664 | return 0; |
| 7665 | } |
| 7666 | |
| 7667 | static void tsc_khz_changed(void *data) |
| 7668 | { |
| 7669 | struct cpufreq_freqs *freq = data; |
| 7670 | unsigned long khz = 0; |
| 7671 | |
| 7672 | if (data) |
| 7673 | khz = freq->new; |
| 7674 | else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
| 7675 | khz = cpufreq_quick_get(raw_smp_processor_id()); |
| 7676 | if (!khz) |
| 7677 | khz = tsc_khz; |
| 7678 | __this_cpu_write(cpu_tsc_khz, khz); |
| 7679 | } |
| 7680 | |
| 7681 | #ifdef CONFIG_X86_64 |
| 7682 | static void kvm_hyperv_tsc_notifier(void) |
| 7683 | { |
| 7684 | struct kvm *kvm; |
| 7685 | struct kvm_vcpu *vcpu; |
| 7686 | int cpu; |
| 7687 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7688 | mutex_lock(&kvm_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7689 | list_for_each_entry(kvm, &vm_list, vm_list) |
| 7690 | kvm_make_mclock_inprogress_request(kvm); |
| 7691 | |
| 7692 | hyperv_stop_tsc_emulation(); |
| 7693 | |
| 7694 | /* TSC frequency always matches when on Hyper-V */ |
| 7695 | for_each_present_cpu(cpu) |
| 7696 | per_cpu(cpu_tsc_khz, cpu) = tsc_khz; |
| 7697 | kvm_max_guest_tsc_khz = tsc_khz; |
| 7698 | |
| 7699 | list_for_each_entry(kvm, &vm_list, vm_list) { |
| 7700 | struct kvm_arch *ka = &kvm->arch; |
| 7701 | |
| 7702 | spin_lock(&ka->pvclock_gtod_sync_lock); |
| 7703 | |
| 7704 | pvclock_update_vm_gtod_copy(kvm); |
| 7705 | |
| 7706 | kvm_for_each_vcpu(cpu, vcpu, kvm) |
| 7707 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
| 7708 | |
| 7709 | kvm_for_each_vcpu(cpu, vcpu, kvm) |
| 7710 | kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu); |
| 7711 | |
| 7712 | spin_unlock(&ka->pvclock_gtod_sync_lock); |
| 7713 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7714 | mutex_unlock(&kvm_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7715 | } |
| 7716 | #endif |
| 7717 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7718 | static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7719 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7720 | struct kvm *kvm; |
| 7721 | struct kvm_vcpu *vcpu; |
| 7722 | int i, send_ipi = 0; |
| 7723 | |
| 7724 | /* |
| 7725 | * We allow guests to temporarily run on slowing clocks, |
| 7726 | * provided we notify them after, or to run on accelerating |
| 7727 | * clocks, provided we notify them before. Thus time never |
| 7728 | * goes backwards. |
| 7729 | * |
| 7730 | * However, we have a problem. We can't atomically update |
| 7731 | * the frequency of a given CPU from this function; it is |
| 7732 | * merely a notifier, which can be called from any CPU. |
| 7733 | * Changing the TSC frequency at arbitrary points in time |
| 7734 | * requires a recomputation of local variables related to |
| 7735 | * the TSC for each VCPU. We must flag these local variables |
| 7736 | * to be updated and be sure the update takes place with the |
| 7737 | * new frequency before any guests proceed. |
| 7738 | * |
| 7739 | * Unfortunately, the combination of hotplug CPU and frequency |
| 7740 | * change creates an intractable locking scenario; the order |
| 7741 | * of when these callouts happen is undefined with respect to |
| 7742 | * CPU hotplug, and they can race with each other. As such, |
| 7743 | * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is |
| 7744 | * undefined; you can actually have a CPU frequency change take |
| 7745 | * place in between the computation of X and the setting of the |
| 7746 | * variable. To protect against this problem, all updates of |
| 7747 | * the per_cpu tsc_khz variable are done in an interrupt |
| 7748 | * protected IPI, and all callers wishing to update the value |
| 7749 | * must wait for a synchronous IPI to complete (which is trivial |
| 7750 | * if the caller is on the CPU already). This establishes the |
| 7751 | * necessary total order on variable updates. |
| 7752 | * |
| 7753 | * Note that because a guest time update may take place |
| 7754 | * anytime after the setting of the VCPU's request bit, the |
| 7755 | * correct TSC value must be set before the request. However, |
| 7756 | * to ensure the update actually makes it to any guest which |
| 7757 | * starts running in hardware virtualization between the set |
| 7758 | * and the acquisition of the spinlock, we must also ping the |
| 7759 | * CPU after setting the request bit. |
| 7760 | * |
| 7761 | */ |
| 7762 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7763 | smp_call_function_single(cpu, tsc_khz_changed, freq, 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7764 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7765 | mutex_lock(&kvm_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7766 | list_for_each_entry(kvm, &vm_list, vm_list) { |
| 7767 | kvm_for_each_vcpu(i, vcpu, kvm) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7768 | if (vcpu->cpu != cpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7769 | continue; |
| 7770 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7771 | if (vcpu->cpu != raw_smp_processor_id()) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7772 | send_ipi = 1; |
| 7773 | } |
| 7774 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7775 | mutex_unlock(&kvm_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7776 | |
| 7777 | if (freq->old < freq->new && send_ipi) { |
| 7778 | /* |
| 7779 | * We upscale the frequency. Must make the guest |
| 7780 | * doesn't see old kvmclock values while running with |
| 7781 | * the new frequency, otherwise we risk the guest sees |
| 7782 | * time go backwards. |
| 7783 | * |
| 7784 | * In case we update the frequency for another cpu |
| 7785 | * (which might be in guest context) send an interrupt |
| 7786 | * to kick the cpu out of guest context. Next time |
| 7787 | * guest context is entered kvmclock will be updated, |
| 7788 | * so the guest will not see stale values. |
| 7789 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7790 | smp_call_function_single(cpu, tsc_khz_changed, freq, 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7791 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7792 | } |
| 7793 | |
| 7794 | static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, |
| 7795 | void *data) |
| 7796 | { |
| 7797 | struct cpufreq_freqs *freq = data; |
| 7798 | int cpu; |
| 7799 | |
| 7800 | if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) |
| 7801 | return 0; |
| 7802 | if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) |
| 7803 | return 0; |
| 7804 | |
| 7805 | for_each_cpu(cpu, freq->policy->cpus) |
| 7806 | __kvmclock_cpufreq_notifier(freq, cpu); |
| 7807 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7808 | return 0; |
| 7809 | } |
| 7810 | |
| 7811 | static struct notifier_block kvmclock_cpufreq_notifier_block = { |
| 7812 | .notifier_call = kvmclock_cpufreq_notifier |
| 7813 | }; |
| 7814 | |
| 7815 | static int kvmclock_cpu_online(unsigned int cpu) |
| 7816 | { |
| 7817 | tsc_khz_changed(NULL); |
| 7818 | return 0; |
| 7819 | } |
| 7820 | |
| 7821 | static void kvm_timer_init(void) |
| 7822 | { |
| 7823 | max_tsc_khz = tsc_khz; |
| 7824 | |
| 7825 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
| 7826 | #ifdef CONFIG_CPU_FREQ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7827 | struct cpufreq_policy *policy; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7828 | int cpu; |
| 7829 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7830 | cpu = get_cpu(); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7831 | policy = cpufreq_cpu_get(cpu); |
| 7832 | if (policy) { |
| 7833 | if (policy->cpuinfo.max_freq) |
| 7834 | max_tsc_khz = policy->cpuinfo.max_freq; |
| 7835 | cpufreq_cpu_put(policy); |
| 7836 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7837 | put_cpu(); |
| 7838 | #endif |
| 7839 | cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, |
| 7840 | CPUFREQ_TRANSITION_NOTIFIER); |
| 7841 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7842 | |
| 7843 | cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online", |
| 7844 | kvmclock_cpu_online, kvmclock_cpu_down_prep); |
| 7845 | } |
| 7846 | |
| 7847 | DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu); |
| 7848 | EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu); |
| 7849 | |
| 7850 | int kvm_is_in_guest(void) |
| 7851 | { |
| 7852 | return __this_cpu_read(current_vcpu) != NULL; |
| 7853 | } |
| 7854 | |
| 7855 | static int kvm_is_user_mode(void) |
| 7856 | { |
| 7857 | int user_mode = 3; |
| 7858 | |
| 7859 | if (__this_cpu_read(current_vcpu)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7860 | user_mode = kvm_x86_ops.get_cpl(__this_cpu_read(current_vcpu)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7861 | |
| 7862 | return user_mode != 0; |
| 7863 | } |
| 7864 | |
| 7865 | static unsigned long kvm_get_guest_ip(void) |
| 7866 | { |
| 7867 | unsigned long ip = 0; |
| 7868 | |
| 7869 | if (__this_cpu_read(current_vcpu)) |
| 7870 | ip = kvm_rip_read(__this_cpu_read(current_vcpu)); |
| 7871 | |
| 7872 | return ip; |
| 7873 | } |
| 7874 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7875 | static void kvm_handle_intel_pt_intr(void) |
| 7876 | { |
| 7877 | struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu); |
| 7878 | |
| 7879 | kvm_make_request(KVM_REQ_PMI, vcpu); |
| 7880 | __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT, |
| 7881 | (unsigned long *)&vcpu->arch.pmu.global_status); |
| 7882 | } |
| 7883 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7884 | static struct perf_guest_info_callbacks kvm_guest_cbs = { |
| 7885 | .is_in_guest = kvm_is_in_guest, |
| 7886 | .is_user_mode = kvm_is_user_mode, |
| 7887 | .get_guest_ip = kvm_get_guest_ip, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7888 | .handle_intel_pt_intr = NULL, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7889 | }; |
| 7890 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7891 | #ifdef CONFIG_X86_64 |
| 7892 | static void pvclock_gtod_update_fn(struct work_struct *work) |
| 7893 | { |
| 7894 | struct kvm *kvm; |
| 7895 | |
| 7896 | struct kvm_vcpu *vcpu; |
| 7897 | int i; |
| 7898 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7899 | mutex_lock(&kvm_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7900 | list_for_each_entry(kvm, &vm_list, vm_list) |
| 7901 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 7902 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
| 7903 | atomic_set(&kvm_guest_has_master_clock, 0); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7904 | mutex_unlock(&kvm_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7905 | } |
| 7906 | |
| 7907 | static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn); |
| 7908 | |
| 7909 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7910 | * Indirection to move queue_work() out of the tk_core.seq write held |
| 7911 | * region to prevent possible deadlocks against time accessors which |
| 7912 | * are invoked with work related locks held. |
| 7913 | */ |
| 7914 | static void pvclock_irq_work_fn(struct irq_work *w) |
| 7915 | { |
| 7916 | queue_work(system_long_wq, &pvclock_gtod_work); |
| 7917 | } |
| 7918 | |
| 7919 | static DEFINE_IRQ_WORK(pvclock_irq_work, pvclock_irq_work_fn); |
| 7920 | |
| 7921 | /* |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7922 | * Notification about pvclock gtod data update. |
| 7923 | */ |
| 7924 | static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, |
| 7925 | void *priv) |
| 7926 | { |
| 7927 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; |
| 7928 | struct timekeeper *tk = priv; |
| 7929 | |
| 7930 | update_pvclock_gtod(tk); |
| 7931 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7932 | /* |
| 7933 | * Disable master clock if host does not trust, or does not use, |
| 7934 | * TSC based clocksource. Delegate queue_work() to irq_work as |
| 7935 | * this is invoked with tk_core.seq write held. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7936 | */ |
| 7937 | if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && |
| 7938 | atomic_read(&kvm_guest_has_master_clock) != 0) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7939 | irq_work_queue(&pvclock_irq_work); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7940 | return 0; |
| 7941 | } |
| 7942 | |
| 7943 | static struct notifier_block pvclock_gtod_notifier = { |
| 7944 | .notifier_call = pvclock_gtod_notify, |
| 7945 | }; |
| 7946 | #endif |
| 7947 | |
| 7948 | int kvm_arch_init(void *opaque) |
| 7949 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7950 | struct kvm_x86_init_ops *ops = opaque; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7951 | int r; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7952 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7953 | if (kvm_x86_ops.hardware_enable) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7954 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
| 7955 | r = -EEXIST; |
| 7956 | goto out; |
| 7957 | } |
| 7958 | |
| 7959 | if (!ops->cpu_has_kvm_support()) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7960 | pr_err_ratelimited("kvm: no hardware support\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7961 | r = -EOPNOTSUPP; |
| 7962 | goto out; |
| 7963 | } |
| 7964 | if (ops->disabled_by_bios()) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7965 | pr_err_ratelimited("kvm: disabled by bios\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7966 | r = -EOPNOTSUPP; |
| 7967 | goto out; |
| 7968 | } |
| 7969 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7970 | /* |
| 7971 | * KVM explicitly assumes that the guest has an FPU and |
| 7972 | * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the |
| 7973 | * vCPU's FPU state as a fxregs_state struct. |
| 7974 | */ |
| 7975 | if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) { |
| 7976 | printk(KERN_ERR "kvm: inadequate fpu\n"); |
| 7977 | r = -EOPNOTSUPP; |
| 7978 | goto out; |
| 7979 | } |
| 7980 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7981 | r = -ENOMEM; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7982 | x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu), |
| 7983 | __alignof__(struct fpu), SLAB_ACCOUNT, |
| 7984 | NULL); |
| 7985 | if (!x86_fpu_cache) { |
| 7986 | printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n"); |
| 7987 | goto out; |
| 7988 | } |
| 7989 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7990 | x86_emulator_cache = kvm_alloc_emulator_cache(); |
| 7991 | if (!x86_emulator_cache) { |
| 7992 | pr_err("kvm: failed to allocate cache for x86 emulator\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7993 | goto out_free_x86_fpu_cache; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7994 | } |
| 7995 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7996 | user_return_msrs = alloc_percpu(struct kvm_user_return_msrs); |
| 7997 | if (!user_return_msrs) { |
| 7998 | printk(KERN_ERR "kvm: failed to allocate percpu kvm_user_return_msrs\n"); |
| 7999 | goto out_free_x86_emulator_cache; |
| 8000 | } |
| 8001 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8002 | r = kvm_mmu_module_init(); |
| 8003 | if (r) |
| 8004 | goto out_free_percpu; |
| 8005 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8006 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, |
| 8007 | PT_DIRTY_MASK, PT64_NX_MASK, 0, |
| 8008 | PT_PRESENT_MASK, 0, sme_me_mask); |
| 8009 | kvm_timer_init(); |
| 8010 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8011 | if (ops->intel_pt_intr_in_guest && ops->intel_pt_intr_in_guest()) |
| 8012 | kvm_guest_cbs.handle_intel_pt_intr = kvm_handle_intel_pt_intr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8013 | perf_register_guest_info_callbacks(&kvm_guest_cbs); |
| 8014 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8015 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8016 | host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8017 | supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0; |
| 8018 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8019 | |
| 8020 | kvm_lapic_init(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8021 | if (pi_inject_timer == -1) |
| 8022 | pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8023 | #ifdef CONFIG_X86_64 |
| 8024 | pvclock_gtod_register_notifier(&pvclock_gtod_notifier); |
| 8025 | |
| 8026 | if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) |
| 8027 | set_hv_tscchange_cb(kvm_hyperv_tsc_notifier); |
| 8028 | #endif |
| 8029 | |
| 8030 | return 0; |
| 8031 | |
| 8032 | out_free_percpu: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8033 | free_percpu(user_return_msrs); |
| 8034 | out_free_x86_emulator_cache: |
| 8035 | kmem_cache_destroy(x86_emulator_cache); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8036 | out_free_x86_fpu_cache: |
| 8037 | kmem_cache_destroy(x86_fpu_cache); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8038 | out: |
| 8039 | return r; |
| 8040 | } |
| 8041 | |
| 8042 | void kvm_arch_exit(void) |
| 8043 | { |
| 8044 | #ifdef CONFIG_X86_64 |
| 8045 | if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) |
| 8046 | clear_hv_tscchange_cb(); |
| 8047 | #endif |
| 8048 | kvm_lapic_exit(); |
| 8049 | perf_unregister_guest_info_callbacks(&kvm_guest_cbs); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8050 | kvm_guest_cbs.handle_intel_pt_intr = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8051 | |
| 8052 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
| 8053 | cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block, |
| 8054 | CPUFREQ_TRANSITION_NOTIFIER); |
| 8055 | cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE); |
| 8056 | #ifdef CONFIG_X86_64 |
| 8057 | pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8058 | irq_work_sync(&pvclock_irq_work); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8059 | cancel_work_sync(&pvclock_gtod_work); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8060 | #endif |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8061 | kvm_x86_ops.hardware_enable = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8062 | kvm_mmu_module_exit(); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8063 | free_percpu(user_return_msrs); |
| 8064 | kmem_cache_destroy(x86_emulator_cache); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8065 | kmem_cache_destroy(x86_fpu_cache); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8066 | } |
| 8067 | |
| 8068 | int kvm_vcpu_halt(struct kvm_vcpu *vcpu) |
| 8069 | { |
| 8070 | ++vcpu->stat.halt_exits; |
| 8071 | if (lapic_in_kernel(vcpu)) { |
| 8072 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
| 8073 | return 1; |
| 8074 | } else { |
| 8075 | vcpu->run->exit_reason = KVM_EXIT_HLT; |
| 8076 | return 0; |
| 8077 | } |
| 8078 | } |
| 8079 | EXPORT_SYMBOL_GPL(kvm_vcpu_halt); |
| 8080 | |
| 8081 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) |
| 8082 | { |
| 8083 | int ret = kvm_skip_emulated_instruction(vcpu); |
| 8084 | /* |
| 8085 | * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered |
| 8086 | * KVM_EXIT_DEBUG here. |
| 8087 | */ |
| 8088 | return kvm_vcpu_halt(vcpu) && ret; |
| 8089 | } |
| 8090 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
| 8091 | |
| 8092 | #ifdef CONFIG_X86_64 |
| 8093 | static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr, |
| 8094 | unsigned long clock_type) |
| 8095 | { |
| 8096 | struct kvm_clock_pairing clock_pairing; |
| 8097 | struct timespec64 ts; |
| 8098 | u64 cycle; |
| 8099 | int ret; |
| 8100 | |
| 8101 | if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK) |
| 8102 | return -KVM_EOPNOTSUPP; |
| 8103 | |
| 8104 | if (kvm_get_walltime_and_clockread(&ts, &cycle) == false) |
| 8105 | return -KVM_EOPNOTSUPP; |
| 8106 | |
| 8107 | clock_pairing.sec = ts.tv_sec; |
| 8108 | clock_pairing.nsec = ts.tv_nsec; |
| 8109 | clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle); |
| 8110 | clock_pairing.flags = 0; |
| 8111 | memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad)); |
| 8112 | |
| 8113 | ret = 0; |
| 8114 | if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, |
| 8115 | sizeof(struct kvm_clock_pairing))) |
| 8116 | ret = -KVM_EFAULT; |
| 8117 | |
| 8118 | return ret; |
| 8119 | } |
| 8120 | #endif |
| 8121 | |
| 8122 | /* |
| 8123 | * kvm_pv_kick_cpu_op: Kick a vcpu. |
| 8124 | * |
| 8125 | * @apicid - apicid of vcpu to be kicked. |
| 8126 | */ |
| 8127 | static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid) |
| 8128 | { |
| 8129 | struct kvm_lapic_irq lapic_irq; |
| 8130 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8131 | lapic_irq.shorthand = APIC_DEST_NOSHORT; |
| 8132 | lapic_irq.dest_mode = APIC_DEST_PHYSICAL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8133 | lapic_irq.level = 0; |
| 8134 | lapic_irq.dest_id = apicid; |
| 8135 | lapic_irq.msi_redir_hint = false; |
| 8136 | |
| 8137 | lapic_irq.delivery_mode = APIC_DM_REMRD; |
| 8138 | kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL); |
| 8139 | } |
| 8140 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8141 | bool kvm_apicv_activated(struct kvm *kvm) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8142 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8143 | return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8144 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8145 | EXPORT_SYMBOL_GPL(kvm_apicv_activated); |
| 8146 | |
| 8147 | void kvm_apicv_init(struct kvm *kvm, bool enable) |
| 8148 | { |
| 8149 | if (enable) |
| 8150 | clear_bit(APICV_INHIBIT_REASON_DISABLE, |
| 8151 | &kvm->arch.apicv_inhibit_reasons); |
| 8152 | else |
| 8153 | set_bit(APICV_INHIBIT_REASON_DISABLE, |
| 8154 | &kvm->arch.apicv_inhibit_reasons); |
| 8155 | } |
| 8156 | EXPORT_SYMBOL_GPL(kvm_apicv_init); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8157 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8158 | static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id) |
| 8159 | { |
| 8160 | struct kvm_vcpu *target = NULL; |
| 8161 | struct kvm_apic_map *map; |
| 8162 | |
| 8163 | rcu_read_lock(); |
| 8164 | map = rcu_dereference(kvm->arch.apic_map); |
| 8165 | |
| 8166 | if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) |
| 8167 | target = map->phys_map[dest_id]->vcpu; |
| 8168 | |
| 8169 | rcu_read_unlock(); |
| 8170 | |
| 8171 | if (target && READ_ONCE(target->ready)) |
| 8172 | kvm_vcpu_yield_to(target); |
| 8173 | } |
| 8174 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8175 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
| 8176 | { |
| 8177 | unsigned long nr, a0, a1, a2, a3, ret; |
| 8178 | int op_64_bit; |
| 8179 | |
| 8180 | if (kvm_hv_hypercall_enabled(vcpu->kvm)) |
| 8181 | return kvm_hv_hypercall(vcpu); |
| 8182 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8183 | nr = kvm_rax_read(vcpu); |
| 8184 | a0 = kvm_rbx_read(vcpu); |
| 8185 | a1 = kvm_rcx_read(vcpu); |
| 8186 | a2 = kvm_rdx_read(vcpu); |
| 8187 | a3 = kvm_rsi_read(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8188 | |
| 8189 | trace_kvm_hypercall(nr, a0, a1, a2, a3); |
| 8190 | |
| 8191 | op_64_bit = is_64_bit_mode(vcpu); |
| 8192 | if (!op_64_bit) { |
| 8193 | nr &= 0xFFFFFFFF; |
| 8194 | a0 &= 0xFFFFFFFF; |
| 8195 | a1 &= 0xFFFFFFFF; |
| 8196 | a2 &= 0xFFFFFFFF; |
| 8197 | a3 &= 0xFFFFFFFF; |
| 8198 | } |
| 8199 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8200 | if (kvm_x86_ops.get_cpl(vcpu) != 0) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8201 | ret = -KVM_EPERM; |
| 8202 | goto out; |
| 8203 | } |
| 8204 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8205 | ret = -KVM_ENOSYS; |
| 8206 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8207 | switch (nr) { |
| 8208 | case KVM_HC_VAPIC_POLL_IRQ: |
| 8209 | ret = 0; |
| 8210 | break; |
| 8211 | case KVM_HC_KICK_CPU: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8212 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_UNHALT)) |
| 8213 | break; |
| 8214 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8215 | kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8216 | kvm_sched_yield(vcpu->kvm, a1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8217 | ret = 0; |
| 8218 | break; |
| 8219 | #ifdef CONFIG_X86_64 |
| 8220 | case KVM_HC_CLOCK_PAIRING: |
| 8221 | ret = kvm_pv_clock_pairing(vcpu, a0, a1); |
| 8222 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8223 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8224 | case KVM_HC_SEND_IPI: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8225 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SEND_IPI)) |
| 8226 | break; |
| 8227 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8228 | ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); |
| 8229 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8230 | case KVM_HC_SCHED_YIELD: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8231 | if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SCHED_YIELD)) |
| 8232 | break; |
| 8233 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8234 | kvm_sched_yield(vcpu->kvm, a0); |
| 8235 | ret = 0; |
| 8236 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8237 | default: |
| 8238 | ret = -KVM_ENOSYS; |
| 8239 | break; |
| 8240 | } |
| 8241 | out: |
| 8242 | if (!op_64_bit) |
| 8243 | ret = (u32)ret; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8244 | kvm_rax_write(vcpu, ret); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8245 | |
| 8246 | ++vcpu->stat.hypercalls; |
| 8247 | return kvm_skip_emulated_instruction(vcpu); |
| 8248 | } |
| 8249 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); |
| 8250 | |
| 8251 | static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt) |
| 8252 | { |
| 8253 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
| 8254 | char instruction[3]; |
| 8255 | unsigned long rip = kvm_rip_read(vcpu); |
| 8256 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8257 | kvm_x86_ops.patch_hypercall(vcpu, instruction); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8258 | |
| 8259 | return emulator_write_emulated(ctxt, rip, instruction, 3, |
| 8260 | &ctxt->exception); |
| 8261 | } |
| 8262 | |
| 8263 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu) |
| 8264 | { |
| 8265 | return vcpu->run->request_interrupt_window && |
| 8266 | likely(!pic_in_kernel(vcpu->kvm)); |
| 8267 | } |
| 8268 | |
| 8269 | static void post_kvm_run_save(struct kvm_vcpu *vcpu) |
| 8270 | { |
| 8271 | struct kvm_run *kvm_run = vcpu->run; |
| 8272 | |
| 8273 | kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0; |
| 8274 | kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0; |
| 8275 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
| 8276 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
| 8277 | kvm_run->ready_for_interrupt_injection = |
| 8278 | pic_in_kernel(vcpu->kvm) || |
| 8279 | kvm_vcpu_ready_for_interrupt_injection(vcpu); |
| 8280 | } |
| 8281 | |
| 8282 | static void update_cr8_intercept(struct kvm_vcpu *vcpu) |
| 8283 | { |
| 8284 | int max_irr, tpr; |
| 8285 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8286 | if (!kvm_x86_ops.update_cr8_intercept) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8287 | return; |
| 8288 | |
| 8289 | if (!lapic_in_kernel(vcpu)) |
| 8290 | return; |
| 8291 | |
| 8292 | if (vcpu->arch.apicv_active) |
| 8293 | return; |
| 8294 | |
| 8295 | if (!vcpu->arch.apic->vapic_addr) |
| 8296 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 8297 | else |
| 8298 | max_irr = -1; |
| 8299 | |
| 8300 | if (max_irr != -1) |
| 8301 | max_irr >>= 4; |
| 8302 | |
| 8303 | tpr = kvm_lapic_get_cr8(vcpu); |
| 8304 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8305 | kvm_x86_ops.update_cr8_intercept(vcpu, tpr, max_irr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8306 | } |
| 8307 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8308 | static void kvm_inject_exception(struct kvm_vcpu *vcpu) |
| 8309 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8310 | if (vcpu->arch.exception.error_code && !is_protmode(vcpu)) |
| 8311 | vcpu->arch.exception.error_code = false; |
| 8312 | kvm_x86_ops.queue_exception(vcpu); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8313 | } |
| 8314 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8315 | static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8316 | { |
| 8317 | int r; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8318 | bool can_inject = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8319 | |
| 8320 | /* try to reinject previous events if any */ |
| 8321 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8322 | if (vcpu->arch.exception.injected) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8323 | kvm_inject_exception(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8324 | can_inject = false; |
| 8325 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8326 | /* |
| 8327 | * Do not inject an NMI or interrupt if there is a pending |
| 8328 | * exception. Exceptions and interrupts are recognized at |
| 8329 | * instruction boundaries, i.e. the start of an instruction. |
| 8330 | * Trap-like exceptions, e.g. #DB, have higher priority than |
| 8331 | * NMIs and interrupts, i.e. traps are recognized before an |
| 8332 | * NMI/interrupt that's pending on the same instruction. |
| 8333 | * Fault-like exceptions, e.g. #GP and #PF, are the lowest |
| 8334 | * priority, but are only generated (pended) during instruction |
| 8335 | * execution, i.e. a pending fault-like exception means the |
| 8336 | * fault occurred on the *previous* instruction and must be |
| 8337 | * serviced prior to recognizing any new events in order to |
| 8338 | * fully complete the previous instruction. |
| 8339 | */ |
| 8340 | else if (!vcpu->arch.exception.pending) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8341 | if (vcpu->arch.nmi_injected) { |
| 8342 | kvm_x86_ops.set_nmi(vcpu); |
| 8343 | can_inject = false; |
| 8344 | } else if (vcpu->arch.interrupt.injected) { |
| 8345 | kvm_x86_ops.set_irq(vcpu); |
| 8346 | can_inject = false; |
| 8347 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8348 | } |
| 8349 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8350 | WARN_ON_ONCE(vcpu->arch.exception.injected && |
| 8351 | vcpu->arch.exception.pending); |
| 8352 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8353 | /* |
| 8354 | * Call check_nested_events() even if we reinjected a previous event |
| 8355 | * in order for caller to determine if it should require immediate-exit |
| 8356 | * from L2 to L1 due to pending L1 events which require exit |
| 8357 | * from L2 to L1. |
| 8358 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8359 | if (is_guest_mode(vcpu)) { |
| 8360 | r = kvm_x86_ops.nested_ops->check_events(vcpu); |
| 8361 | if (r < 0) |
| 8362 | goto busy; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8363 | } |
| 8364 | |
| 8365 | /* try to inject new event if pending */ |
| 8366 | if (vcpu->arch.exception.pending) { |
| 8367 | trace_kvm_inj_exception(vcpu->arch.exception.nr, |
| 8368 | vcpu->arch.exception.has_error_code, |
| 8369 | vcpu->arch.exception.error_code); |
| 8370 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8371 | vcpu->arch.exception.pending = false; |
| 8372 | vcpu->arch.exception.injected = true; |
| 8373 | |
| 8374 | if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT) |
| 8375 | __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) | |
| 8376 | X86_EFLAGS_RF); |
| 8377 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8378 | if (vcpu->arch.exception.nr == DB_VECTOR) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8379 | kvm_deliver_exception_payload(vcpu); |
| 8380 | if (vcpu->arch.dr7 & DR7_GD) { |
| 8381 | vcpu->arch.dr7 &= ~DR7_GD; |
| 8382 | kvm_update_dr7(vcpu); |
| 8383 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8384 | } |
| 8385 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8386 | kvm_inject_exception(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8387 | can_inject = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8388 | } |
| 8389 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8390 | /* |
| 8391 | * Finally, inject interrupt events. If an event cannot be injected |
| 8392 | * due to architectural conditions (e.g. IF=0) a window-open exit |
| 8393 | * will re-request KVM_REQ_EVENT. Sometimes however an event is pending |
| 8394 | * and can architecturally be injected, but we cannot do it right now: |
| 8395 | * an interrupt could have arrived just now and we have to inject it |
| 8396 | * as a vmexit, or there could already an event in the queue, which is |
| 8397 | * indicated by can_inject. In that case we request an immediate exit |
| 8398 | * in order to make progress and get back here for another iteration. |
| 8399 | * The kvm_x86_ops hooks communicate this by returning -EBUSY. |
| 8400 | */ |
| 8401 | if (vcpu->arch.smi_pending) { |
| 8402 | r = can_inject ? kvm_x86_ops.smi_allowed(vcpu, true) : -EBUSY; |
| 8403 | if (r < 0) |
| 8404 | goto busy; |
| 8405 | if (r) { |
| 8406 | vcpu->arch.smi_pending = false; |
| 8407 | ++vcpu->arch.smi_count; |
| 8408 | enter_smm(vcpu); |
| 8409 | can_inject = false; |
| 8410 | } else |
| 8411 | kvm_x86_ops.enable_smi_window(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8412 | } |
| 8413 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8414 | if (vcpu->arch.nmi_pending) { |
| 8415 | r = can_inject ? kvm_x86_ops.nmi_allowed(vcpu, true) : -EBUSY; |
| 8416 | if (r < 0) |
| 8417 | goto busy; |
| 8418 | if (r) { |
| 8419 | --vcpu->arch.nmi_pending; |
| 8420 | vcpu->arch.nmi_injected = true; |
| 8421 | kvm_x86_ops.set_nmi(vcpu); |
| 8422 | can_inject = false; |
| 8423 | WARN_ON(kvm_x86_ops.nmi_allowed(vcpu, true) < 0); |
| 8424 | } |
| 8425 | if (vcpu->arch.nmi_pending) |
| 8426 | kvm_x86_ops.enable_nmi_window(vcpu); |
| 8427 | } |
| 8428 | |
| 8429 | if (kvm_cpu_has_injectable_intr(vcpu)) { |
| 8430 | r = can_inject ? kvm_x86_ops.interrupt_allowed(vcpu, true) : -EBUSY; |
| 8431 | if (r < 0) |
| 8432 | goto busy; |
| 8433 | if (r) { |
| 8434 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false); |
| 8435 | kvm_x86_ops.set_irq(vcpu); |
| 8436 | WARN_ON(kvm_x86_ops.interrupt_allowed(vcpu, true) < 0); |
| 8437 | } |
| 8438 | if (kvm_cpu_has_injectable_intr(vcpu)) |
| 8439 | kvm_x86_ops.enable_irq_window(vcpu); |
| 8440 | } |
| 8441 | |
| 8442 | if (is_guest_mode(vcpu) && |
| 8443 | kvm_x86_ops.nested_ops->hv_timer_pending && |
| 8444 | kvm_x86_ops.nested_ops->hv_timer_pending(vcpu)) |
| 8445 | *req_immediate_exit = true; |
| 8446 | |
| 8447 | WARN_ON(vcpu->arch.exception.pending); |
| 8448 | return; |
| 8449 | |
| 8450 | busy: |
| 8451 | *req_immediate_exit = true; |
| 8452 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8453 | } |
| 8454 | |
| 8455 | static void process_nmi(struct kvm_vcpu *vcpu) |
| 8456 | { |
| 8457 | unsigned limit = 2; |
| 8458 | |
| 8459 | /* |
| 8460 | * x86 is limited to one NMI running, and one NMI pending after it. |
| 8461 | * If an NMI is already in progress, limit further NMIs to just one. |
| 8462 | * Otherwise, allow two (and we'll inject the first one immediately). |
| 8463 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8464 | if (kvm_x86_ops.get_nmi_mask(vcpu) || vcpu->arch.nmi_injected) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8465 | limit = 1; |
| 8466 | |
| 8467 | vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); |
| 8468 | vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); |
| 8469 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 8470 | } |
| 8471 | |
| 8472 | static u32 enter_smm_get_segment_flags(struct kvm_segment *seg) |
| 8473 | { |
| 8474 | u32 flags = 0; |
| 8475 | flags |= seg->g << 23; |
| 8476 | flags |= seg->db << 22; |
| 8477 | flags |= seg->l << 21; |
| 8478 | flags |= seg->avl << 20; |
| 8479 | flags |= seg->present << 15; |
| 8480 | flags |= seg->dpl << 13; |
| 8481 | flags |= seg->s << 12; |
| 8482 | flags |= seg->type << 8; |
| 8483 | return flags; |
| 8484 | } |
| 8485 | |
| 8486 | static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n) |
| 8487 | { |
| 8488 | struct kvm_segment seg; |
| 8489 | int offset; |
| 8490 | |
| 8491 | kvm_get_segment(vcpu, &seg, n); |
| 8492 | put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector); |
| 8493 | |
| 8494 | if (n < 3) |
| 8495 | offset = 0x7f84 + n * 12; |
| 8496 | else |
| 8497 | offset = 0x7f2c + (n - 3) * 12; |
| 8498 | |
| 8499 | put_smstate(u32, buf, offset + 8, seg.base); |
| 8500 | put_smstate(u32, buf, offset + 4, seg.limit); |
| 8501 | put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg)); |
| 8502 | } |
| 8503 | |
| 8504 | #ifdef CONFIG_X86_64 |
| 8505 | static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n) |
| 8506 | { |
| 8507 | struct kvm_segment seg; |
| 8508 | int offset; |
| 8509 | u16 flags; |
| 8510 | |
| 8511 | kvm_get_segment(vcpu, &seg, n); |
| 8512 | offset = 0x7e00 + n * 16; |
| 8513 | |
| 8514 | flags = enter_smm_get_segment_flags(&seg) >> 8; |
| 8515 | put_smstate(u16, buf, offset, seg.selector); |
| 8516 | put_smstate(u16, buf, offset + 2, flags); |
| 8517 | put_smstate(u32, buf, offset + 4, seg.limit); |
| 8518 | put_smstate(u64, buf, offset + 8, seg.base); |
| 8519 | } |
| 8520 | #endif |
| 8521 | |
| 8522 | static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf) |
| 8523 | { |
| 8524 | struct desc_ptr dt; |
| 8525 | struct kvm_segment seg; |
| 8526 | unsigned long val; |
| 8527 | int i; |
| 8528 | |
| 8529 | put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu)); |
| 8530 | put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu)); |
| 8531 | put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu)); |
| 8532 | put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu)); |
| 8533 | |
| 8534 | for (i = 0; i < 8; i++) |
| 8535 | put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i)); |
| 8536 | |
| 8537 | kvm_get_dr(vcpu, 6, &val); |
| 8538 | put_smstate(u32, buf, 0x7fcc, (u32)val); |
| 8539 | kvm_get_dr(vcpu, 7, &val); |
| 8540 | put_smstate(u32, buf, 0x7fc8, (u32)val); |
| 8541 | |
| 8542 | kvm_get_segment(vcpu, &seg, VCPU_SREG_TR); |
| 8543 | put_smstate(u32, buf, 0x7fc4, seg.selector); |
| 8544 | put_smstate(u32, buf, 0x7f64, seg.base); |
| 8545 | put_smstate(u32, buf, 0x7f60, seg.limit); |
| 8546 | put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg)); |
| 8547 | |
| 8548 | kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR); |
| 8549 | put_smstate(u32, buf, 0x7fc0, seg.selector); |
| 8550 | put_smstate(u32, buf, 0x7f80, seg.base); |
| 8551 | put_smstate(u32, buf, 0x7f7c, seg.limit); |
| 8552 | put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg)); |
| 8553 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8554 | kvm_x86_ops.get_gdt(vcpu, &dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8555 | put_smstate(u32, buf, 0x7f74, dt.address); |
| 8556 | put_smstate(u32, buf, 0x7f70, dt.size); |
| 8557 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8558 | kvm_x86_ops.get_idt(vcpu, &dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8559 | put_smstate(u32, buf, 0x7f58, dt.address); |
| 8560 | put_smstate(u32, buf, 0x7f54, dt.size); |
| 8561 | |
| 8562 | for (i = 0; i < 6; i++) |
| 8563 | enter_smm_save_seg_32(vcpu, buf, i); |
| 8564 | |
| 8565 | put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu)); |
| 8566 | |
| 8567 | /* revision id */ |
| 8568 | put_smstate(u32, buf, 0x7efc, 0x00020000); |
| 8569 | put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase); |
| 8570 | } |
| 8571 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8572 | #ifdef CONFIG_X86_64 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8573 | static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf) |
| 8574 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8575 | struct desc_ptr dt; |
| 8576 | struct kvm_segment seg; |
| 8577 | unsigned long val; |
| 8578 | int i; |
| 8579 | |
| 8580 | for (i = 0; i < 16; i++) |
| 8581 | put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i)); |
| 8582 | |
| 8583 | put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu)); |
| 8584 | put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu)); |
| 8585 | |
| 8586 | kvm_get_dr(vcpu, 6, &val); |
| 8587 | put_smstate(u64, buf, 0x7f68, val); |
| 8588 | kvm_get_dr(vcpu, 7, &val); |
| 8589 | put_smstate(u64, buf, 0x7f60, val); |
| 8590 | |
| 8591 | put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu)); |
| 8592 | put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu)); |
| 8593 | put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu)); |
| 8594 | |
| 8595 | put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase); |
| 8596 | |
| 8597 | /* revision id */ |
| 8598 | put_smstate(u32, buf, 0x7efc, 0x00020064); |
| 8599 | |
| 8600 | put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer); |
| 8601 | |
| 8602 | kvm_get_segment(vcpu, &seg, VCPU_SREG_TR); |
| 8603 | put_smstate(u16, buf, 0x7e90, seg.selector); |
| 8604 | put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8); |
| 8605 | put_smstate(u32, buf, 0x7e94, seg.limit); |
| 8606 | put_smstate(u64, buf, 0x7e98, seg.base); |
| 8607 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8608 | kvm_x86_ops.get_idt(vcpu, &dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8609 | put_smstate(u32, buf, 0x7e84, dt.size); |
| 8610 | put_smstate(u64, buf, 0x7e88, dt.address); |
| 8611 | |
| 8612 | kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR); |
| 8613 | put_smstate(u16, buf, 0x7e70, seg.selector); |
| 8614 | put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8); |
| 8615 | put_smstate(u32, buf, 0x7e74, seg.limit); |
| 8616 | put_smstate(u64, buf, 0x7e78, seg.base); |
| 8617 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8618 | kvm_x86_ops.get_gdt(vcpu, &dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8619 | put_smstate(u32, buf, 0x7e64, dt.size); |
| 8620 | put_smstate(u64, buf, 0x7e68, dt.address); |
| 8621 | |
| 8622 | for (i = 0; i < 6; i++) |
| 8623 | enter_smm_save_seg_64(vcpu, buf, i); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8624 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8625 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8626 | |
| 8627 | static void enter_smm(struct kvm_vcpu *vcpu) |
| 8628 | { |
| 8629 | struct kvm_segment cs, ds; |
| 8630 | struct desc_ptr dt; |
| 8631 | char buf[512]; |
| 8632 | u32 cr0; |
| 8633 | |
| 8634 | trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true); |
| 8635 | memset(buf, 0, 512); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8636 | #ifdef CONFIG_X86_64 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8637 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
| 8638 | enter_smm_save_state_64(vcpu, buf); |
| 8639 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8640 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8641 | enter_smm_save_state_32(vcpu, buf); |
| 8642 | |
| 8643 | /* |
| 8644 | * Give pre_enter_smm() a chance to make ISA-specific changes to the |
| 8645 | * vCPU state (e.g. leave guest mode) after we've saved the state into |
| 8646 | * the SMM state-save area. |
| 8647 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8648 | kvm_x86_ops.pre_enter_smm(vcpu, buf); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8649 | |
| 8650 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 8651 | kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf)); |
| 8652 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8653 | if (kvm_x86_ops.get_nmi_mask(vcpu)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8654 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; |
| 8655 | else |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8656 | kvm_x86_ops.set_nmi_mask(vcpu, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8657 | |
| 8658 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
| 8659 | kvm_rip_write(vcpu, 0x8000); |
| 8660 | |
| 8661 | cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8662 | kvm_x86_ops.set_cr0(vcpu, cr0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8663 | vcpu->arch.cr0 = cr0; |
| 8664 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8665 | kvm_x86_ops.set_cr4(vcpu, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8666 | |
| 8667 | /* Undocumented: IDT limit is set to zero on entry to SMM. */ |
| 8668 | dt.address = dt.size = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8669 | kvm_x86_ops.set_idt(vcpu, &dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8670 | |
| 8671 | __kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
| 8672 | |
| 8673 | cs.selector = (vcpu->arch.smbase >> 4) & 0xffff; |
| 8674 | cs.base = vcpu->arch.smbase; |
| 8675 | |
| 8676 | ds.selector = 0; |
| 8677 | ds.base = 0; |
| 8678 | |
| 8679 | cs.limit = ds.limit = 0xffffffff; |
| 8680 | cs.type = ds.type = 0x3; |
| 8681 | cs.dpl = ds.dpl = 0; |
| 8682 | cs.db = ds.db = 0; |
| 8683 | cs.s = ds.s = 1; |
| 8684 | cs.l = ds.l = 0; |
| 8685 | cs.g = ds.g = 1; |
| 8686 | cs.avl = ds.avl = 0; |
| 8687 | cs.present = ds.present = 1; |
| 8688 | cs.unusable = ds.unusable = 0; |
| 8689 | cs.padding = ds.padding = 0; |
| 8690 | |
| 8691 | kvm_set_segment(vcpu, &cs, VCPU_SREG_CS); |
| 8692 | kvm_set_segment(vcpu, &ds, VCPU_SREG_DS); |
| 8693 | kvm_set_segment(vcpu, &ds, VCPU_SREG_ES); |
| 8694 | kvm_set_segment(vcpu, &ds, VCPU_SREG_FS); |
| 8695 | kvm_set_segment(vcpu, &ds, VCPU_SREG_GS); |
| 8696 | kvm_set_segment(vcpu, &ds, VCPU_SREG_SS); |
| 8697 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8698 | #ifdef CONFIG_X86_64 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8699 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8700 | kvm_x86_ops.set_efer(vcpu, 0); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8701 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8702 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8703 | kvm_update_cpuid_runtime(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8704 | kvm_mmu_reset_context(vcpu); |
| 8705 | } |
| 8706 | |
| 8707 | static void process_smi(struct kvm_vcpu *vcpu) |
| 8708 | { |
| 8709 | vcpu->arch.smi_pending = true; |
| 8710 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 8711 | } |
| 8712 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8713 | void kvm_make_scan_ioapic_request_mask(struct kvm *kvm, |
| 8714 | unsigned long *vcpu_bitmap) |
| 8715 | { |
| 8716 | cpumask_var_t cpus; |
| 8717 | |
| 8718 | zalloc_cpumask_var(&cpus, GFP_ATOMIC); |
| 8719 | |
| 8720 | kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC, |
| 8721 | NULL, vcpu_bitmap, cpus); |
| 8722 | |
| 8723 | free_cpumask_var(cpus); |
| 8724 | } |
| 8725 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8726 | void kvm_make_scan_ioapic_request(struct kvm *kvm) |
| 8727 | { |
| 8728 | kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC); |
| 8729 | } |
| 8730 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8731 | void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu) |
| 8732 | { |
| 8733 | if (!lapic_in_kernel(vcpu)) |
| 8734 | return; |
| 8735 | |
| 8736 | vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm); |
| 8737 | kvm_apic_update_apicv(vcpu); |
| 8738 | kvm_x86_ops.refresh_apicv_exec_ctrl(vcpu); |
| 8739 | } |
| 8740 | EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv); |
| 8741 | |
| 8742 | /* |
| 8743 | * NOTE: Do not hold any lock prior to calling this. |
| 8744 | * |
| 8745 | * In particular, kvm_request_apicv_update() expects kvm->srcu not to be |
| 8746 | * locked, because it calls __x86_set_memory_region() which does |
| 8747 | * synchronize_srcu(&kvm->srcu). |
| 8748 | */ |
| 8749 | void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit) |
| 8750 | { |
| 8751 | struct kvm_vcpu *except; |
| 8752 | unsigned long old, new, expected; |
| 8753 | |
| 8754 | if (!kvm_x86_ops.check_apicv_inhibit_reasons || |
| 8755 | !kvm_x86_ops.check_apicv_inhibit_reasons(bit)) |
| 8756 | return; |
| 8757 | |
| 8758 | old = READ_ONCE(kvm->arch.apicv_inhibit_reasons); |
| 8759 | do { |
| 8760 | expected = new = old; |
| 8761 | if (activate) |
| 8762 | __clear_bit(bit, &new); |
| 8763 | else |
| 8764 | __set_bit(bit, &new); |
| 8765 | if (new == old) |
| 8766 | break; |
| 8767 | old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new); |
| 8768 | } while (old != expected); |
| 8769 | |
| 8770 | if (!!old == !!new) |
| 8771 | return; |
| 8772 | |
| 8773 | trace_kvm_apicv_update_request(activate, bit); |
| 8774 | if (kvm_x86_ops.pre_update_apicv_exec_ctrl) |
| 8775 | kvm_x86_ops.pre_update_apicv_exec_ctrl(kvm, activate); |
| 8776 | |
| 8777 | /* |
| 8778 | * Sending request to update APICV for all other vcpus, |
| 8779 | * while update the calling vcpu immediately instead of |
| 8780 | * waiting for another #VMEXIT to handle the request. |
| 8781 | */ |
| 8782 | except = kvm_get_running_vcpu(); |
| 8783 | kvm_make_all_cpus_request_except(kvm, KVM_REQ_APICV_UPDATE, |
| 8784 | except); |
| 8785 | if (except) |
| 8786 | kvm_vcpu_update_apicv(except); |
| 8787 | } |
| 8788 | EXPORT_SYMBOL_GPL(kvm_request_apicv_update); |
| 8789 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8790 | static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu) |
| 8791 | { |
| 8792 | if (!kvm_apic_present(vcpu)) |
| 8793 | return; |
| 8794 | |
| 8795 | bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); |
| 8796 | |
| 8797 | if (irqchip_split(vcpu->kvm)) |
| 8798 | kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); |
| 8799 | else { |
| 8800 | if (vcpu->arch.apicv_active) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8801 | kvm_x86_ops.sync_pir_to_irr(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8802 | if (ioapic_in_kernel(vcpu->kvm)) |
| 8803 | kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); |
| 8804 | } |
| 8805 | |
| 8806 | if (is_guest_mode(vcpu)) |
| 8807 | vcpu->arch.load_eoi_exitmap_pending = true; |
| 8808 | else |
| 8809 | kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu); |
| 8810 | } |
| 8811 | |
| 8812 | static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu) |
| 8813 | { |
| 8814 | u64 eoi_exit_bitmap[4]; |
| 8815 | |
| 8816 | if (!kvm_apic_hw_enabled(vcpu->arch.apic)) |
| 8817 | return; |
| 8818 | |
| 8819 | bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors, |
| 8820 | vcpu_to_synic(vcpu)->vec_bitmap, 256); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8821 | kvm_x86_ops.load_eoi_exitmap(vcpu, eoi_exit_bitmap); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8822 | } |
| 8823 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8824 | void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, |
| 8825 | unsigned long start, unsigned long end) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8826 | { |
| 8827 | unsigned long apic_address; |
| 8828 | |
| 8829 | /* |
| 8830 | * The physical address of apic access page is stored in the VMCS. |
| 8831 | * Update it when it becomes invalid. |
| 8832 | */ |
| 8833 | apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
| 8834 | if (start <= apic_address && apic_address < end) |
| 8835 | kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8836 | } |
| 8837 | |
| 8838 | void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu) |
| 8839 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8840 | if (!lapic_in_kernel(vcpu)) |
| 8841 | return; |
| 8842 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8843 | if (!kvm_x86_ops.set_apic_access_page_addr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8844 | return; |
| 8845 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8846 | kvm_x86_ops.set_apic_access_page_addr(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8847 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8848 | |
| 8849 | void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu) |
| 8850 | { |
| 8851 | smp_send_reschedule(vcpu->cpu); |
| 8852 | } |
| 8853 | EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit); |
| 8854 | |
| 8855 | /* |
| 8856 | * Returns 1 to let vcpu_run() continue the guest execution loop without |
| 8857 | * exiting to the userspace. Otherwise, the value will be returned to the |
| 8858 | * userspace. |
| 8859 | */ |
| 8860 | static int vcpu_enter_guest(struct kvm_vcpu *vcpu) |
| 8861 | { |
| 8862 | int r; |
| 8863 | bool req_int_win = |
| 8864 | dm_request_for_irq_injection(vcpu) && |
| 8865 | kvm_cpu_accept_dm_intr(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8866 | fastpath_t exit_fastpath; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8867 | |
| 8868 | bool req_immediate_exit = false; |
| 8869 | |
| 8870 | if (kvm_request_pending(vcpu)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8871 | if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) { |
| 8872 | if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8873 | r = 0; |
| 8874 | goto out; |
| 8875 | } |
| 8876 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8877 | if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) |
| 8878 | kvm_mmu_unload(vcpu); |
| 8879 | if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu)) |
| 8880 | __kvm_migrate_timers(vcpu); |
| 8881 | if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu)) |
| 8882 | kvm_gen_update_masterclock(vcpu->kvm); |
| 8883 | if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu)) |
| 8884 | kvm_gen_kvmclock_update(vcpu); |
| 8885 | if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) { |
| 8886 | r = kvm_guest_time_update(vcpu); |
| 8887 | if (unlikely(r)) |
| 8888 | goto out; |
| 8889 | } |
| 8890 | if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu)) |
| 8891 | kvm_mmu_sync_roots(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8892 | if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu)) |
| 8893 | kvm_mmu_load_pgd(vcpu); |
| 8894 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { |
| 8895 | kvm_vcpu_flush_tlb_all(vcpu); |
| 8896 | |
| 8897 | /* Flushing all ASIDs flushes the current ASID... */ |
| 8898 | kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
| 8899 | } |
| 8900 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 8901 | kvm_vcpu_flush_tlb_current(vcpu); |
| 8902 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu)) |
| 8903 | kvm_vcpu_flush_tlb_guest(vcpu); |
| 8904 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8905 | if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) { |
| 8906 | vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; |
| 8907 | r = 0; |
| 8908 | goto out; |
| 8909 | } |
| 8910 | if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) { |
| 8911 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 8912 | vcpu->mmio_needed = 0; |
| 8913 | r = 0; |
| 8914 | goto out; |
| 8915 | } |
| 8916 | if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) { |
| 8917 | /* Page is swapped out. Do synthetic halt */ |
| 8918 | vcpu->arch.apf.halted = true; |
| 8919 | r = 1; |
| 8920 | goto out; |
| 8921 | } |
| 8922 | if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu)) |
| 8923 | record_steal_time(vcpu); |
| 8924 | if (kvm_check_request(KVM_REQ_SMI, vcpu)) |
| 8925 | process_smi(vcpu); |
| 8926 | if (kvm_check_request(KVM_REQ_NMI, vcpu)) |
| 8927 | process_nmi(vcpu); |
| 8928 | if (kvm_check_request(KVM_REQ_PMU, vcpu)) |
| 8929 | kvm_pmu_handle_event(vcpu); |
| 8930 | if (kvm_check_request(KVM_REQ_PMI, vcpu)) |
| 8931 | kvm_pmu_deliver_pmi(vcpu); |
| 8932 | if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) { |
| 8933 | BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); |
| 8934 | if (test_bit(vcpu->arch.pending_ioapic_eoi, |
| 8935 | vcpu->arch.ioapic_handled_vectors)) { |
| 8936 | vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; |
| 8937 | vcpu->run->eoi.vector = |
| 8938 | vcpu->arch.pending_ioapic_eoi; |
| 8939 | r = 0; |
| 8940 | goto out; |
| 8941 | } |
| 8942 | } |
| 8943 | if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu)) |
| 8944 | vcpu_scan_ioapic(vcpu); |
| 8945 | if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu)) |
| 8946 | vcpu_load_eoi_exitmap(vcpu); |
| 8947 | if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu)) |
| 8948 | kvm_vcpu_reload_apic_access_page(vcpu); |
| 8949 | if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) { |
| 8950 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; |
| 8951 | vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; |
| 8952 | r = 0; |
| 8953 | goto out; |
| 8954 | } |
| 8955 | if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) { |
| 8956 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; |
| 8957 | vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; |
| 8958 | r = 0; |
| 8959 | goto out; |
| 8960 | } |
| 8961 | if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) { |
| 8962 | vcpu->run->exit_reason = KVM_EXIT_HYPERV; |
| 8963 | vcpu->run->hyperv = vcpu->arch.hyperv.exit; |
| 8964 | r = 0; |
| 8965 | goto out; |
| 8966 | } |
| 8967 | |
| 8968 | /* |
| 8969 | * KVM_REQ_HV_STIMER has to be processed after |
| 8970 | * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers |
| 8971 | * depend on the guest clock being up-to-date |
| 8972 | */ |
| 8973 | if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu)) |
| 8974 | kvm_hv_process_stimers(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8975 | if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu)) |
| 8976 | kvm_vcpu_update_apicv(vcpu); |
| 8977 | if (kvm_check_request(KVM_REQ_APF_READY, vcpu)) |
| 8978 | kvm_check_async_pf_completion(vcpu); |
| 8979 | if (kvm_check_request(KVM_REQ_MSR_FILTER_CHANGED, vcpu)) |
| 8980 | kvm_x86_ops.msr_filter_changed(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8981 | } |
| 8982 | |
| 8983 | if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) { |
| 8984 | ++vcpu->stat.req_event; |
| 8985 | kvm_apic_accept_events(vcpu); |
| 8986 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { |
| 8987 | r = 1; |
| 8988 | goto out; |
| 8989 | } |
| 8990 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8991 | inject_pending_event(vcpu, &req_immediate_exit); |
| 8992 | if (req_int_win) |
| 8993 | kvm_x86_ops.enable_irq_window(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8994 | |
| 8995 | if (kvm_lapic_enabled(vcpu)) { |
| 8996 | update_cr8_intercept(vcpu); |
| 8997 | kvm_lapic_sync_to_vapic(vcpu); |
| 8998 | } |
| 8999 | } |
| 9000 | |
| 9001 | r = kvm_mmu_reload(vcpu); |
| 9002 | if (unlikely(r)) { |
| 9003 | goto cancel_injection; |
| 9004 | } |
| 9005 | |
| 9006 | preempt_disable(); |
| 9007 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9008 | kvm_x86_ops.prepare_guest_switch(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9009 | |
| 9010 | /* |
| 9011 | * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt |
| 9012 | * IPI are then delayed after guest entry, which ensures that they |
| 9013 | * result in virtual interrupt delivery. |
| 9014 | */ |
| 9015 | local_irq_disable(); |
| 9016 | vcpu->mode = IN_GUEST_MODE; |
| 9017 | |
| 9018 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
| 9019 | |
| 9020 | /* |
| 9021 | * 1) We should set ->mode before checking ->requests. Please see |
| 9022 | * the comment in kvm_vcpu_exiting_guest_mode(). |
| 9023 | * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9024 | * 2) For APICv, we should set ->mode before checking PID.ON. This |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9025 | * pairs with the memory barrier implicit in pi_test_and_set_on |
| 9026 | * (see vmx_deliver_posted_interrupt). |
| 9027 | * |
| 9028 | * 3) This also orders the write to mode from any reads to the page |
| 9029 | * tables done while the VCPU is running. Please see the comment |
| 9030 | * in kvm_flush_remote_tlbs. |
| 9031 | */ |
| 9032 | smp_mb__after_srcu_read_unlock(); |
| 9033 | |
| 9034 | /* |
| 9035 | * This handles the case where a posted interrupt was |
| 9036 | * notified with kvm_vcpu_kick. |
| 9037 | */ |
| 9038 | if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9039 | kvm_x86_ops.sync_pir_to_irr(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9040 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9041 | if (kvm_vcpu_exit_request(vcpu)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9042 | vcpu->mode = OUTSIDE_GUEST_MODE; |
| 9043 | smp_wmb(); |
| 9044 | local_irq_enable(); |
| 9045 | preempt_enable(); |
| 9046 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 9047 | r = 1; |
| 9048 | goto cancel_injection; |
| 9049 | } |
| 9050 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9051 | if (req_immediate_exit) { |
| 9052 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9053 | kvm_x86_ops.request_immediate_exit(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9054 | } |
| 9055 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9056 | trace_kvm_entry(vcpu); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9057 | |
| 9058 | fpregs_assert_state_consistent(); |
| 9059 | if (test_thread_flag(TIF_NEED_FPU_LOAD)) |
| 9060 | switch_fpu_return(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9061 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9062 | if (unlikely(vcpu->arch.switch_db_regs)) { |
| 9063 | set_debugreg(0, 7); |
| 9064 | set_debugreg(vcpu->arch.eff_db[0], 0); |
| 9065 | set_debugreg(vcpu->arch.eff_db[1], 1); |
| 9066 | set_debugreg(vcpu->arch.eff_db[2], 2); |
| 9067 | set_debugreg(vcpu->arch.eff_db[3], 3); |
| 9068 | set_debugreg(vcpu->arch.dr6, 6); |
| 9069 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9070 | } else if (unlikely(hw_breakpoint_active())) { |
| 9071 | set_debugreg(0, 7); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9072 | } |
| 9073 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9074 | exit_fastpath = kvm_x86_ops.run(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9075 | |
| 9076 | /* |
| 9077 | * Do this here before restoring debug registers on the host. And |
| 9078 | * since we do this before handling the vmexit, a DR access vmexit |
| 9079 | * can (a) read the correct value of the debug registers, (b) set |
| 9080 | * KVM_DEBUGREG_WONT_EXIT again. |
| 9081 | */ |
| 9082 | if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { |
| 9083 | WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9084 | kvm_x86_ops.sync_dirty_debug_regs(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9085 | kvm_update_dr0123(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9086 | kvm_update_dr7(vcpu); |
| 9087 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD; |
| 9088 | } |
| 9089 | |
| 9090 | /* |
| 9091 | * If the guest has used debug registers, at least dr7 |
| 9092 | * will be disabled while returning to the host. |
| 9093 | * If we don't have active breakpoints in the host, we don't |
| 9094 | * care about the messed up debug address registers. But if |
| 9095 | * we have some of them active, restore the old state. |
| 9096 | */ |
| 9097 | if (hw_breakpoint_active()) |
| 9098 | hw_breakpoint_restore(); |
| 9099 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9100 | vcpu->arch.last_vmentry_cpu = vcpu->cpu; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9101 | vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
| 9102 | |
| 9103 | vcpu->mode = OUTSIDE_GUEST_MODE; |
| 9104 | smp_wmb(); |
| 9105 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9106 | kvm_x86_ops.handle_exit_irqoff(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9107 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9108 | /* |
| 9109 | * Consume any pending interrupts, including the possible source of |
| 9110 | * VM-Exit on SVM and any ticks that occur between VM-Exit and now. |
| 9111 | * An instruction is required after local_irq_enable() to fully unblock |
| 9112 | * interrupts on processors that implement an interrupt shadow, the |
| 9113 | * stat.exits increment will do nicely. |
| 9114 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9115 | kvm_before_interrupt(vcpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9116 | local_irq_enable(); |
| 9117 | ++vcpu->stat.exits; |
| 9118 | local_irq_disable(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9119 | kvm_after_interrupt(vcpu); |
| 9120 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9121 | /* |
| 9122 | * Wait until after servicing IRQs to account guest time so that any |
| 9123 | * ticks that occurred while running the guest are properly accounted |
| 9124 | * to the guest. Waiting until IRQs are enabled degrades the accuracy |
| 9125 | * of accounting via context tracking, but the loss of accuracy is |
| 9126 | * acceptable for all known use cases. |
| 9127 | */ |
| 9128 | vtime_account_guest_exit(); |
| 9129 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9130 | if (lapic_in_kernel(vcpu)) { |
| 9131 | s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta; |
| 9132 | if (delta != S64_MIN) { |
| 9133 | trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta); |
| 9134 | vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN; |
| 9135 | } |
| 9136 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9137 | |
| 9138 | local_irq_enable(); |
| 9139 | preempt_enable(); |
| 9140 | |
| 9141 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 9142 | |
| 9143 | /* |
| 9144 | * Profile KVM exit RIPs: |
| 9145 | */ |
| 9146 | if (unlikely(prof_on == KVM_PROFILING)) { |
| 9147 | unsigned long rip = kvm_rip_read(vcpu); |
| 9148 | profile_hit(KVM_PROFILING, (void *)rip); |
| 9149 | } |
| 9150 | |
| 9151 | if (unlikely(vcpu->arch.tsc_always_catchup)) |
| 9152 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
| 9153 | |
| 9154 | if (vcpu->arch.apic_attention) |
| 9155 | kvm_lapic_sync_from_vapic(vcpu); |
| 9156 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9157 | r = kvm_x86_ops.handle_exit(vcpu, exit_fastpath); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9158 | return r; |
| 9159 | |
| 9160 | cancel_injection: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9161 | if (req_immediate_exit) |
| 9162 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9163 | kvm_x86_ops.cancel_injection(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9164 | if (unlikely(vcpu->arch.apic_attention)) |
| 9165 | kvm_lapic_sync_from_vapic(vcpu); |
| 9166 | out: |
| 9167 | return r; |
| 9168 | } |
| 9169 | |
| 9170 | static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu) |
| 9171 | { |
| 9172 | if (!kvm_arch_vcpu_runnable(vcpu) && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9173 | (!kvm_x86_ops.pre_block || kvm_x86_ops.pre_block(vcpu) == 0)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9174 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
| 9175 | kvm_vcpu_block(vcpu); |
| 9176 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
| 9177 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9178 | if (kvm_x86_ops.post_block) |
| 9179 | kvm_x86_ops.post_block(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9180 | |
| 9181 | if (!kvm_check_request(KVM_REQ_UNHALT, vcpu)) |
| 9182 | return 1; |
| 9183 | } |
| 9184 | |
| 9185 | kvm_apic_accept_events(vcpu); |
| 9186 | switch(vcpu->arch.mp_state) { |
| 9187 | case KVM_MP_STATE_HALTED: |
| 9188 | vcpu->arch.pv.pv_unhalted = false; |
| 9189 | vcpu->arch.mp_state = |
| 9190 | KVM_MP_STATE_RUNNABLE; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9191 | fallthrough; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9192 | case KVM_MP_STATE_RUNNABLE: |
| 9193 | vcpu->arch.apf.halted = false; |
| 9194 | break; |
| 9195 | case KVM_MP_STATE_INIT_RECEIVED: |
| 9196 | break; |
| 9197 | default: |
| 9198 | return -EINTR; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9199 | } |
| 9200 | return 1; |
| 9201 | } |
| 9202 | |
| 9203 | static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu) |
| 9204 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9205 | if (is_guest_mode(vcpu)) |
| 9206 | kvm_x86_ops.nested_ops->check_events(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9207 | |
| 9208 | return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && |
| 9209 | !vcpu->arch.apf.halted); |
| 9210 | } |
| 9211 | |
| 9212 | static int vcpu_run(struct kvm_vcpu *vcpu) |
| 9213 | { |
| 9214 | int r; |
| 9215 | struct kvm *kvm = vcpu->kvm; |
| 9216 | |
| 9217 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
| 9218 | vcpu->arch.l1tf_flush_l1d = true; |
| 9219 | |
| 9220 | for (;;) { |
| 9221 | if (kvm_vcpu_running(vcpu)) { |
| 9222 | r = vcpu_enter_guest(vcpu); |
| 9223 | } else { |
| 9224 | r = vcpu_block(kvm, vcpu); |
| 9225 | } |
| 9226 | |
| 9227 | if (r <= 0) |
| 9228 | break; |
| 9229 | |
| 9230 | kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu); |
| 9231 | if (kvm_cpu_has_pending_timer(vcpu)) |
| 9232 | kvm_inject_pending_timer_irqs(vcpu); |
| 9233 | |
| 9234 | if (dm_request_for_irq_injection(vcpu) && |
| 9235 | kvm_vcpu_ready_for_interrupt_injection(vcpu)) { |
| 9236 | r = 0; |
| 9237 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
| 9238 | ++vcpu->stat.request_irq_exits; |
| 9239 | break; |
| 9240 | } |
| 9241 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9242 | if (__xfer_to_guest_mode_work_pending()) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9243 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9244 | r = xfer_to_guest_mode_handle_work(vcpu); |
| 9245 | if (r) |
| 9246 | return r; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9247 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
| 9248 | } |
| 9249 | } |
| 9250 | |
| 9251 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
| 9252 | |
| 9253 | return r; |
| 9254 | } |
| 9255 | |
| 9256 | static inline int complete_emulated_io(struct kvm_vcpu *vcpu) |
| 9257 | { |
| 9258 | int r; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9259 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9260 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 9261 | r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE); |
| 9262 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9263 | return r; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9264 | } |
| 9265 | |
| 9266 | static int complete_emulated_pio(struct kvm_vcpu *vcpu) |
| 9267 | { |
| 9268 | BUG_ON(!vcpu->arch.pio.count); |
| 9269 | |
| 9270 | return complete_emulated_io(vcpu); |
| 9271 | } |
| 9272 | |
| 9273 | /* |
| 9274 | * Implements the following, as a state machine: |
| 9275 | * |
| 9276 | * read: |
| 9277 | * for each fragment |
| 9278 | * for each mmio piece in the fragment |
| 9279 | * write gpa, len |
| 9280 | * exit |
| 9281 | * copy data |
| 9282 | * execute insn |
| 9283 | * |
| 9284 | * write: |
| 9285 | * for each fragment |
| 9286 | * for each mmio piece in the fragment |
| 9287 | * write gpa, len |
| 9288 | * copy data |
| 9289 | * exit |
| 9290 | */ |
| 9291 | static int complete_emulated_mmio(struct kvm_vcpu *vcpu) |
| 9292 | { |
| 9293 | struct kvm_run *run = vcpu->run; |
| 9294 | struct kvm_mmio_fragment *frag; |
| 9295 | unsigned len; |
| 9296 | |
| 9297 | BUG_ON(!vcpu->mmio_needed); |
| 9298 | |
| 9299 | /* Complete previous fragment */ |
| 9300 | frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; |
| 9301 | len = min(8u, frag->len); |
| 9302 | if (!vcpu->mmio_is_write) |
| 9303 | memcpy(frag->data, run->mmio.data, len); |
| 9304 | |
| 9305 | if (frag->len <= 8) { |
| 9306 | /* Switch to the next fragment. */ |
| 9307 | frag++; |
| 9308 | vcpu->mmio_cur_fragment++; |
| 9309 | } else { |
| 9310 | /* Go forward to the next mmio piece. */ |
| 9311 | frag->data += len; |
| 9312 | frag->gpa += len; |
| 9313 | frag->len -= len; |
| 9314 | } |
| 9315 | |
| 9316 | if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { |
| 9317 | vcpu->mmio_needed = 0; |
| 9318 | |
| 9319 | /* FIXME: return into emulator if single-stepping. */ |
| 9320 | if (vcpu->mmio_is_write) |
| 9321 | return 1; |
| 9322 | vcpu->mmio_read_completed = 1; |
| 9323 | return complete_emulated_io(vcpu); |
| 9324 | } |
| 9325 | |
| 9326 | run->exit_reason = KVM_EXIT_MMIO; |
| 9327 | run->mmio.phys_addr = frag->gpa; |
| 9328 | if (vcpu->mmio_is_write) |
| 9329 | memcpy(run->mmio.data, frag->data, min(8u, frag->len)); |
| 9330 | run->mmio.len = min(8u, frag->len); |
| 9331 | run->mmio.is_write = vcpu->mmio_is_write; |
| 9332 | vcpu->arch.complete_userspace_io = complete_emulated_mmio; |
| 9333 | return 0; |
| 9334 | } |
| 9335 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9336 | static void kvm_save_current_fpu(struct fpu *fpu) |
| 9337 | { |
| 9338 | /* |
| 9339 | * If the target FPU state is not resident in the CPU registers, just |
| 9340 | * memcpy() from current, else save CPU state directly to the target. |
| 9341 | */ |
| 9342 | if (test_thread_flag(TIF_NEED_FPU_LOAD)) |
| 9343 | memcpy(&fpu->state, ¤t->thread.fpu.state, |
| 9344 | fpu_kernel_xstate_size); |
| 9345 | else |
| 9346 | copy_fpregs_to_fpstate(fpu); |
| 9347 | } |
| 9348 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9349 | /* Swap (qemu) user FPU context for the guest FPU context. */ |
| 9350 | static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) |
| 9351 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9352 | fpregs_lock(); |
| 9353 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9354 | kvm_save_current_fpu(vcpu->arch.user_fpu); |
| 9355 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9356 | /* PKRU is separately restored in kvm_x86_ops.run. */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9357 | __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9358 | ~XFEATURE_MASK_PKRU); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9359 | |
| 9360 | fpregs_mark_activate(); |
| 9361 | fpregs_unlock(); |
| 9362 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9363 | trace_kvm_fpu(1); |
| 9364 | } |
| 9365 | |
| 9366 | /* When vcpu_run ends, restore user space FPU context. */ |
| 9367 | static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) |
| 9368 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9369 | fpregs_lock(); |
| 9370 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9371 | kvm_save_current_fpu(vcpu->arch.guest_fpu); |
| 9372 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9373 | copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state); |
| 9374 | |
| 9375 | fpregs_mark_activate(); |
| 9376 | fpregs_unlock(); |
| 9377 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9378 | ++vcpu->stat.fpu_reload; |
| 9379 | trace_kvm_fpu(0); |
| 9380 | } |
| 9381 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9382 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9383 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9384 | struct kvm_run *kvm_run = vcpu->run; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9385 | int r; |
| 9386 | |
| 9387 | vcpu_load(vcpu); |
| 9388 | kvm_sigset_activate(vcpu); |
| 9389 | kvm_load_guest_fpu(vcpu); |
| 9390 | |
| 9391 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
| 9392 | if (kvm_run->immediate_exit) { |
| 9393 | r = -EINTR; |
| 9394 | goto out; |
| 9395 | } |
| 9396 | kvm_vcpu_block(vcpu); |
| 9397 | kvm_apic_accept_events(vcpu); |
| 9398 | kvm_clear_request(KVM_REQ_UNHALT, vcpu); |
| 9399 | r = -EAGAIN; |
| 9400 | if (signal_pending(current)) { |
| 9401 | r = -EINTR; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9402 | kvm_run->exit_reason = KVM_EXIT_INTR; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9403 | ++vcpu->stat.signal_exits; |
| 9404 | } |
| 9405 | goto out; |
| 9406 | } |
| 9407 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9408 | if (kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9409 | r = -EINVAL; |
| 9410 | goto out; |
| 9411 | } |
| 9412 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9413 | if (kvm_run->kvm_dirty_regs) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9414 | r = sync_regs(vcpu); |
| 9415 | if (r != 0) |
| 9416 | goto out; |
| 9417 | } |
| 9418 | |
| 9419 | /* re-sync apic's tpr */ |
| 9420 | if (!lapic_in_kernel(vcpu)) { |
| 9421 | if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { |
| 9422 | r = -EINVAL; |
| 9423 | goto out; |
| 9424 | } |
| 9425 | } |
| 9426 | |
| 9427 | if (unlikely(vcpu->arch.complete_userspace_io)) { |
| 9428 | int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; |
| 9429 | vcpu->arch.complete_userspace_io = NULL; |
| 9430 | r = cui(vcpu); |
| 9431 | if (r <= 0) |
| 9432 | goto out; |
| 9433 | } else |
| 9434 | WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed); |
| 9435 | |
| 9436 | if (kvm_run->immediate_exit) |
| 9437 | r = -EINTR; |
| 9438 | else |
| 9439 | r = vcpu_run(vcpu); |
| 9440 | |
| 9441 | out: |
| 9442 | kvm_put_guest_fpu(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9443 | if (kvm_run->kvm_valid_regs) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9444 | store_regs(vcpu); |
| 9445 | post_kvm_run_save(vcpu); |
| 9446 | kvm_sigset_deactivate(vcpu); |
| 9447 | |
| 9448 | vcpu_put(vcpu); |
| 9449 | return r; |
| 9450 | } |
| 9451 | |
| 9452 | static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 9453 | { |
| 9454 | if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { |
| 9455 | /* |
| 9456 | * We are here if userspace calls get_regs() in the middle of |
| 9457 | * instruction emulation. Registers state needs to be copied |
| 9458 | * back from emulation context to vcpu. Userspace shouldn't do |
| 9459 | * that usually, but some bad designed PV devices (vmware |
| 9460 | * backdoor interface) need this to work |
| 9461 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9462 | emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9463 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
| 9464 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9465 | regs->rax = kvm_rax_read(vcpu); |
| 9466 | regs->rbx = kvm_rbx_read(vcpu); |
| 9467 | regs->rcx = kvm_rcx_read(vcpu); |
| 9468 | regs->rdx = kvm_rdx_read(vcpu); |
| 9469 | regs->rsi = kvm_rsi_read(vcpu); |
| 9470 | regs->rdi = kvm_rdi_read(vcpu); |
| 9471 | regs->rsp = kvm_rsp_read(vcpu); |
| 9472 | regs->rbp = kvm_rbp_read(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9473 | #ifdef CONFIG_X86_64 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9474 | regs->r8 = kvm_r8_read(vcpu); |
| 9475 | regs->r9 = kvm_r9_read(vcpu); |
| 9476 | regs->r10 = kvm_r10_read(vcpu); |
| 9477 | regs->r11 = kvm_r11_read(vcpu); |
| 9478 | regs->r12 = kvm_r12_read(vcpu); |
| 9479 | regs->r13 = kvm_r13_read(vcpu); |
| 9480 | regs->r14 = kvm_r14_read(vcpu); |
| 9481 | regs->r15 = kvm_r15_read(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9482 | #endif |
| 9483 | |
| 9484 | regs->rip = kvm_rip_read(vcpu); |
| 9485 | regs->rflags = kvm_get_rflags(vcpu); |
| 9486 | } |
| 9487 | |
| 9488 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 9489 | { |
| 9490 | vcpu_load(vcpu); |
| 9491 | __get_regs(vcpu, regs); |
| 9492 | vcpu_put(vcpu); |
| 9493 | return 0; |
| 9494 | } |
| 9495 | |
| 9496 | static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 9497 | { |
| 9498 | vcpu->arch.emulate_regs_need_sync_from_vcpu = true; |
| 9499 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
| 9500 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9501 | kvm_rax_write(vcpu, regs->rax); |
| 9502 | kvm_rbx_write(vcpu, regs->rbx); |
| 9503 | kvm_rcx_write(vcpu, regs->rcx); |
| 9504 | kvm_rdx_write(vcpu, regs->rdx); |
| 9505 | kvm_rsi_write(vcpu, regs->rsi); |
| 9506 | kvm_rdi_write(vcpu, regs->rdi); |
| 9507 | kvm_rsp_write(vcpu, regs->rsp); |
| 9508 | kvm_rbp_write(vcpu, regs->rbp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9509 | #ifdef CONFIG_X86_64 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9510 | kvm_r8_write(vcpu, regs->r8); |
| 9511 | kvm_r9_write(vcpu, regs->r9); |
| 9512 | kvm_r10_write(vcpu, regs->r10); |
| 9513 | kvm_r11_write(vcpu, regs->r11); |
| 9514 | kvm_r12_write(vcpu, regs->r12); |
| 9515 | kvm_r13_write(vcpu, regs->r13); |
| 9516 | kvm_r14_write(vcpu, regs->r14); |
| 9517 | kvm_r15_write(vcpu, regs->r15); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9518 | #endif |
| 9519 | |
| 9520 | kvm_rip_write(vcpu, regs->rip); |
| 9521 | kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); |
| 9522 | |
| 9523 | vcpu->arch.exception.pending = false; |
| 9524 | |
| 9525 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9526 | } |
| 9527 | |
| 9528 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 9529 | { |
| 9530 | vcpu_load(vcpu); |
| 9531 | __set_regs(vcpu, regs); |
| 9532 | vcpu_put(vcpu); |
| 9533 | return 0; |
| 9534 | } |
| 9535 | |
| 9536 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 9537 | { |
| 9538 | struct kvm_segment cs; |
| 9539 | |
| 9540 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 9541 | *db = cs.db; |
| 9542 | *l = cs.l; |
| 9543 | } |
| 9544 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); |
| 9545 | |
| 9546 | static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
| 9547 | { |
| 9548 | struct desc_ptr dt; |
| 9549 | |
| 9550 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 9551 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 9552 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 9553 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 9554 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 9555 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
| 9556 | |
| 9557 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 9558 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
| 9559 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9560 | kvm_x86_ops.get_idt(vcpu, &dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9561 | sregs->idt.limit = dt.size; |
| 9562 | sregs->idt.base = dt.address; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9563 | kvm_x86_ops.get_gdt(vcpu, &dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9564 | sregs->gdt.limit = dt.size; |
| 9565 | sregs->gdt.base = dt.address; |
| 9566 | |
| 9567 | sregs->cr0 = kvm_read_cr0(vcpu); |
| 9568 | sregs->cr2 = vcpu->arch.cr2; |
| 9569 | sregs->cr3 = kvm_read_cr3(vcpu); |
| 9570 | sregs->cr4 = kvm_read_cr4(vcpu); |
| 9571 | sregs->cr8 = kvm_get_cr8(vcpu); |
| 9572 | sregs->efer = vcpu->arch.efer; |
| 9573 | sregs->apic_base = kvm_get_apic_base(vcpu); |
| 9574 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9575 | memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9576 | |
| 9577 | if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) |
| 9578 | set_bit(vcpu->arch.interrupt.nr, |
| 9579 | (unsigned long *)sregs->interrupt_bitmap); |
| 9580 | } |
| 9581 | |
| 9582 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 9583 | struct kvm_sregs *sregs) |
| 9584 | { |
| 9585 | vcpu_load(vcpu); |
| 9586 | __get_sregs(vcpu, sregs); |
| 9587 | vcpu_put(vcpu); |
| 9588 | return 0; |
| 9589 | } |
| 9590 | |
| 9591 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 9592 | struct kvm_mp_state *mp_state) |
| 9593 | { |
| 9594 | vcpu_load(vcpu); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9595 | if (kvm_mpx_supported()) |
| 9596 | kvm_load_guest_fpu(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9597 | |
| 9598 | kvm_apic_accept_events(vcpu); |
| 9599 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED && |
| 9600 | vcpu->arch.pv.pv_unhalted) |
| 9601 | mp_state->mp_state = KVM_MP_STATE_RUNNABLE; |
| 9602 | else |
| 9603 | mp_state->mp_state = vcpu->arch.mp_state; |
| 9604 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9605 | if (kvm_mpx_supported()) |
| 9606 | kvm_put_guest_fpu(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9607 | vcpu_put(vcpu); |
| 9608 | return 0; |
| 9609 | } |
| 9610 | |
| 9611 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 9612 | struct kvm_mp_state *mp_state) |
| 9613 | { |
| 9614 | int ret = -EINVAL; |
| 9615 | |
| 9616 | vcpu_load(vcpu); |
| 9617 | |
| 9618 | if (!lapic_in_kernel(vcpu) && |
| 9619 | mp_state->mp_state != KVM_MP_STATE_RUNNABLE) |
| 9620 | goto out; |
| 9621 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9622 | /* |
| 9623 | * KVM_MP_STATE_INIT_RECEIVED means the processor is in |
| 9624 | * INIT state; latched init should be reported using |
| 9625 | * KVM_SET_VCPU_EVENTS, so reject it here. |
| 9626 | */ |
| 9627 | if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) && |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9628 | (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || |
| 9629 | mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) |
| 9630 | goto out; |
| 9631 | |
| 9632 | if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { |
| 9633 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; |
| 9634 | set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); |
| 9635 | } else |
| 9636 | vcpu->arch.mp_state = mp_state->mp_state; |
| 9637 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9638 | |
| 9639 | ret = 0; |
| 9640 | out: |
| 9641 | vcpu_put(vcpu); |
| 9642 | return ret; |
| 9643 | } |
| 9644 | |
| 9645 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index, |
| 9646 | int reason, bool has_error_code, u32 error_code) |
| 9647 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9648 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9649 | int ret; |
| 9650 | |
| 9651 | init_emulate_ctxt(vcpu); |
| 9652 | |
| 9653 | ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason, |
| 9654 | has_error_code, error_code); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9655 | if (ret) { |
| 9656 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 9657 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 9658 | vcpu->run->internal.ndata = 0; |
| 9659 | return 0; |
| 9660 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9661 | |
| 9662 | kvm_rip_write(vcpu, ctxt->eip); |
| 9663 | kvm_set_rflags(vcpu, ctxt->eflags); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9664 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9665 | } |
| 9666 | EXPORT_SYMBOL_GPL(kvm_task_switch); |
| 9667 | |
| 9668 | static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
| 9669 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9670 | if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { |
| 9671 | /* |
| 9672 | * When EFER.LME and CR0.PG are set, the processor is in |
| 9673 | * 64-bit mode (though maybe in a 32-bit code segment). |
| 9674 | * CR4.PAE and EFER.LMA must be set. |
| 9675 | */ |
| 9676 | if (!(sregs->cr4 & X86_CR4_PAE) |
| 9677 | || !(sregs->efer & EFER_LMA)) |
| 9678 | return -EINVAL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9679 | if (sregs->cr3 & vcpu->arch.cr3_lm_rsvd_bits) |
| 9680 | return -EINVAL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9681 | } else { |
| 9682 | /* |
| 9683 | * Not in 64-bit mode: EFER.LMA is clear and the code |
| 9684 | * segment cannot be 64-bit. |
| 9685 | */ |
| 9686 | if (sregs->efer & EFER_LMA || sregs->cs.l) |
| 9687 | return -EINVAL; |
| 9688 | } |
| 9689 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9690 | return kvm_valid_cr4(vcpu, sregs->cr4); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9691 | } |
| 9692 | |
| 9693 | static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
| 9694 | { |
| 9695 | struct msr_data apic_base_msr; |
| 9696 | int mmu_reset_needed = 0; |
| 9697 | int cpuid_update_needed = 0; |
| 9698 | int pending_vec, max_bits, idx; |
| 9699 | struct desc_ptr dt; |
| 9700 | int ret = -EINVAL; |
| 9701 | |
| 9702 | if (kvm_valid_sregs(vcpu, sregs)) |
| 9703 | goto out; |
| 9704 | |
| 9705 | apic_base_msr.data = sregs->apic_base; |
| 9706 | apic_base_msr.host_initiated = true; |
| 9707 | if (kvm_set_apic_base(vcpu, &apic_base_msr)) |
| 9708 | goto out; |
| 9709 | |
| 9710 | dt.size = sregs->idt.limit; |
| 9711 | dt.address = sregs->idt.base; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9712 | kvm_x86_ops.set_idt(vcpu, &dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9713 | dt.size = sregs->gdt.limit; |
| 9714 | dt.address = sregs->gdt.base; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9715 | kvm_x86_ops.set_gdt(vcpu, &dt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9716 | |
| 9717 | vcpu->arch.cr2 = sregs->cr2; |
| 9718 | mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; |
| 9719 | vcpu->arch.cr3 = sregs->cr3; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9720 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9721 | |
| 9722 | kvm_set_cr8(vcpu, sregs->cr8); |
| 9723 | |
| 9724 | mmu_reset_needed |= vcpu->arch.efer != sregs->efer; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9725 | kvm_x86_ops.set_efer(vcpu, sregs->efer); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9726 | |
| 9727 | mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9728 | kvm_x86_ops.set_cr0(vcpu, sregs->cr0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9729 | vcpu->arch.cr0 = sregs->cr0; |
| 9730 | |
| 9731 | mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; |
| 9732 | cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) & |
| 9733 | (X86_CR4_OSXSAVE | X86_CR4_PKE)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9734 | kvm_x86_ops.set_cr4(vcpu, sregs->cr4); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9735 | if (cpuid_update_needed) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9736 | kvm_update_cpuid_runtime(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9737 | |
| 9738 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9739 | if (is_pae_paging(vcpu)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9740 | load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); |
| 9741 | mmu_reset_needed = 1; |
| 9742 | } |
| 9743 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 9744 | |
| 9745 | if (mmu_reset_needed) |
| 9746 | kvm_mmu_reset_context(vcpu); |
| 9747 | |
| 9748 | max_bits = KVM_NR_INTERRUPTS; |
| 9749 | pending_vec = find_first_bit( |
| 9750 | (const unsigned long *)sregs->interrupt_bitmap, max_bits); |
| 9751 | if (pending_vec < max_bits) { |
| 9752 | kvm_queue_interrupt(vcpu, pending_vec, false); |
| 9753 | pr_debug("Set back pending irq %d\n", pending_vec); |
| 9754 | } |
| 9755 | |
| 9756 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
| 9757 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); |
| 9758 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); |
| 9759 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); |
| 9760 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); |
| 9761 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); |
| 9762 | |
| 9763 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
| 9764 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); |
| 9765 | |
| 9766 | update_cr8_intercept(vcpu); |
| 9767 | |
| 9768 | /* Older userspace won't unhalt the vcpu on reset. */ |
| 9769 | if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 && |
| 9770 | sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && |
| 9771 | !is_protmode(vcpu)) |
| 9772 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 9773 | |
| 9774 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9775 | |
| 9776 | ret = 0; |
| 9777 | out: |
| 9778 | return ret; |
| 9779 | } |
| 9780 | |
| 9781 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 9782 | struct kvm_sregs *sregs) |
| 9783 | { |
| 9784 | int ret; |
| 9785 | |
| 9786 | vcpu_load(vcpu); |
| 9787 | ret = __set_sregs(vcpu, sregs); |
| 9788 | vcpu_put(vcpu); |
| 9789 | return ret; |
| 9790 | } |
| 9791 | |
| 9792 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 9793 | struct kvm_guest_debug *dbg) |
| 9794 | { |
| 9795 | unsigned long rflags; |
| 9796 | int i, r; |
| 9797 | |
| 9798 | vcpu_load(vcpu); |
| 9799 | |
| 9800 | if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { |
| 9801 | r = -EBUSY; |
| 9802 | if (vcpu->arch.exception.pending) |
| 9803 | goto out; |
| 9804 | if (dbg->control & KVM_GUESTDBG_INJECT_DB) |
| 9805 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 9806 | else |
| 9807 | kvm_queue_exception(vcpu, BP_VECTOR); |
| 9808 | } |
| 9809 | |
| 9810 | /* |
| 9811 | * Read rflags as long as potentially injected trace flags are still |
| 9812 | * filtered out. |
| 9813 | */ |
| 9814 | rflags = kvm_get_rflags(vcpu); |
| 9815 | |
| 9816 | vcpu->guest_debug = dbg->control; |
| 9817 | if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) |
| 9818 | vcpu->guest_debug = 0; |
| 9819 | |
| 9820 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
| 9821 | for (i = 0; i < KVM_NR_DB_REGS; ++i) |
| 9822 | vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; |
| 9823 | vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; |
| 9824 | } else { |
| 9825 | for (i = 0; i < KVM_NR_DB_REGS; i++) |
| 9826 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; |
| 9827 | } |
| 9828 | kvm_update_dr7(vcpu); |
| 9829 | |
| 9830 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 9831 | vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) + |
| 9832 | get_segment_base(vcpu, VCPU_SREG_CS); |
| 9833 | |
| 9834 | /* |
| 9835 | * Trigger an rflags update that will inject or remove the trace |
| 9836 | * flags. |
| 9837 | */ |
| 9838 | kvm_set_rflags(vcpu, rflags); |
| 9839 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9840 | kvm_x86_ops.update_exception_bitmap(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9841 | |
| 9842 | r = 0; |
| 9843 | |
| 9844 | out: |
| 9845 | vcpu_put(vcpu); |
| 9846 | return r; |
| 9847 | } |
| 9848 | |
| 9849 | /* |
| 9850 | * Translate a guest virtual address to a guest physical address. |
| 9851 | */ |
| 9852 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 9853 | struct kvm_translation *tr) |
| 9854 | { |
| 9855 | unsigned long vaddr = tr->linear_address; |
| 9856 | gpa_t gpa; |
| 9857 | int idx; |
| 9858 | |
| 9859 | vcpu_load(vcpu); |
| 9860 | |
| 9861 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 9862 | gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL); |
| 9863 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 9864 | tr->physical_address = gpa; |
| 9865 | tr->valid = gpa != UNMAPPED_GVA; |
| 9866 | tr->writeable = 1; |
| 9867 | tr->usermode = 0; |
| 9868 | |
| 9869 | vcpu_put(vcpu); |
| 9870 | return 0; |
| 9871 | } |
| 9872 | |
| 9873 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 9874 | { |
| 9875 | struct fxregs_state *fxsave; |
| 9876 | |
| 9877 | vcpu_load(vcpu); |
| 9878 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9879 | fxsave = &vcpu->arch.guest_fpu->state.fxsave; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9880 | memcpy(fpu->fpr, fxsave->st_space, 128); |
| 9881 | fpu->fcw = fxsave->cwd; |
| 9882 | fpu->fsw = fxsave->swd; |
| 9883 | fpu->ftwx = fxsave->twd; |
| 9884 | fpu->last_opcode = fxsave->fop; |
| 9885 | fpu->last_ip = fxsave->rip; |
| 9886 | fpu->last_dp = fxsave->rdp; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9887 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9888 | |
| 9889 | vcpu_put(vcpu); |
| 9890 | return 0; |
| 9891 | } |
| 9892 | |
| 9893 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 9894 | { |
| 9895 | struct fxregs_state *fxsave; |
| 9896 | |
| 9897 | vcpu_load(vcpu); |
| 9898 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9899 | fxsave = &vcpu->arch.guest_fpu->state.fxsave; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9900 | |
| 9901 | memcpy(fxsave->st_space, fpu->fpr, 128); |
| 9902 | fxsave->cwd = fpu->fcw; |
| 9903 | fxsave->swd = fpu->fsw; |
| 9904 | fxsave->twd = fpu->ftwx; |
| 9905 | fxsave->fop = fpu->last_opcode; |
| 9906 | fxsave->rip = fpu->last_ip; |
| 9907 | fxsave->rdp = fpu->last_dp; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9908 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9909 | |
| 9910 | vcpu_put(vcpu); |
| 9911 | return 0; |
| 9912 | } |
| 9913 | |
| 9914 | static void store_regs(struct kvm_vcpu *vcpu) |
| 9915 | { |
| 9916 | BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES); |
| 9917 | |
| 9918 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) |
| 9919 | __get_regs(vcpu, &vcpu->run->s.regs.regs); |
| 9920 | |
| 9921 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) |
| 9922 | __get_sregs(vcpu, &vcpu->run->s.regs.sregs); |
| 9923 | |
| 9924 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) |
| 9925 | kvm_vcpu_ioctl_x86_get_vcpu_events( |
| 9926 | vcpu, &vcpu->run->s.regs.events); |
| 9927 | } |
| 9928 | |
| 9929 | static int sync_regs(struct kvm_vcpu *vcpu) |
| 9930 | { |
| 9931 | if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS) |
| 9932 | return -EINVAL; |
| 9933 | |
| 9934 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { |
| 9935 | __set_regs(vcpu, &vcpu->run->s.regs.regs); |
| 9936 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; |
| 9937 | } |
| 9938 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { |
| 9939 | if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs)) |
| 9940 | return -EINVAL; |
| 9941 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; |
| 9942 | } |
| 9943 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { |
| 9944 | if (kvm_vcpu_ioctl_x86_set_vcpu_events( |
| 9945 | vcpu, &vcpu->run->s.regs.events)) |
| 9946 | return -EINVAL; |
| 9947 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; |
| 9948 | } |
| 9949 | |
| 9950 | return 0; |
| 9951 | } |
| 9952 | |
| 9953 | static void fx_init(struct kvm_vcpu *vcpu) |
| 9954 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9955 | fpstate_init(&vcpu->arch.guest_fpu->state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9956 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9957 | vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv = |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9958 | host_xcr0 | XSTATE_COMPACTION_ENABLED; |
| 9959 | |
| 9960 | /* |
| 9961 | * Ensure guest xcr0 is valid for loading |
| 9962 | */ |
| 9963 | vcpu->arch.xcr0 = XFEATURE_MASK_FP; |
| 9964 | |
| 9965 | vcpu->arch.cr0 |= X86_CR0_ET; |
| 9966 | } |
| 9967 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9968 | int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9969 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9970 | if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9971 | pr_warn_once("kvm: SMP vm created on host with unstable TSC; " |
| 9972 | "guest TSC will not be reliable\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9973 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9974 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9975 | } |
| 9976 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9977 | int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9978 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9979 | struct page *page; |
| 9980 | int r; |
| 9981 | |
| 9982 | if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) |
| 9983 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 9984 | else |
| 9985 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; |
| 9986 | |
| 9987 | kvm_set_tsc_khz(vcpu, max_tsc_khz); |
| 9988 | |
| 9989 | r = kvm_mmu_create(vcpu); |
| 9990 | if (r < 0) |
| 9991 | return r; |
| 9992 | |
| 9993 | if (irqchip_in_kernel(vcpu->kvm)) { |
| 9994 | r = kvm_create_lapic(vcpu, lapic_timer_advance_ns); |
| 9995 | if (r < 0) |
| 9996 | goto fail_mmu_destroy; |
| 9997 | if (kvm_apicv_activated(vcpu->kvm)) |
| 9998 | vcpu->arch.apicv_active = true; |
| 9999 | } else |
| 10000 | static_key_slow_inc(&kvm_no_apic_vcpu); |
| 10001 | |
| 10002 | r = -ENOMEM; |
| 10003 | |
| 10004 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 10005 | if (!page) |
| 10006 | goto fail_free_lapic; |
| 10007 | vcpu->arch.pio_data = page_address(page); |
| 10008 | |
| 10009 | vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4, |
| 10010 | GFP_KERNEL_ACCOUNT); |
| 10011 | if (!vcpu->arch.mce_banks) |
| 10012 | goto fail_free_pio_data; |
| 10013 | vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; |
| 10014 | |
| 10015 | if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, |
| 10016 | GFP_KERNEL_ACCOUNT)) |
| 10017 | goto fail_free_mce_banks; |
| 10018 | |
| 10019 | if (!alloc_emulate_ctxt(vcpu)) |
| 10020 | goto free_wbinvd_dirty_mask; |
| 10021 | |
| 10022 | vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache, |
| 10023 | GFP_KERNEL_ACCOUNT); |
| 10024 | if (!vcpu->arch.user_fpu) { |
| 10025 | pr_err("kvm: failed to allocate userspace's fpu\n"); |
| 10026 | goto free_emulate_ctxt; |
| 10027 | } |
| 10028 | |
| 10029 | vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache, |
| 10030 | GFP_KERNEL_ACCOUNT); |
| 10031 | if (!vcpu->arch.guest_fpu) { |
| 10032 | pr_err("kvm: failed to allocate vcpu's fpu\n"); |
| 10033 | goto free_user_fpu; |
| 10034 | } |
| 10035 | fx_init(vcpu); |
| 10036 | |
| 10037 | vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); |
| 10038 | vcpu->arch.cr3_lm_rsvd_bits = rsvd_bits(cpuid_maxphyaddr(vcpu), 63); |
| 10039 | |
| 10040 | vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; |
| 10041 | |
| 10042 | kvm_async_pf_hash_reset(vcpu); |
| 10043 | kvm_pmu_init(vcpu); |
| 10044 | |
| 10045 | vcpu->arch.pending_external_vector = -1; |
| 10046 | vcpu->arch.preempted_in_kernel = false; |
| 10047 | |
| 10048 | kvm_hv_vcpu_init(vcpu); |
| 10049 | |
| 10050 | r = kvm_x86_ops.vcpu_create(vcpu); |
| 10051 | if (r) |
| 10052 | goto free_guest_fpu; |
| 10053 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10054 | vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); |
| 10055 | vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10056 | kvm_vcpu_mtrr_init(vcpu); |
| 10057 | vcpu_load(vcpu); |
| 10058 | kvm_vcpu_reset(vcpu, false); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10059 | kvm_init_mmu(vcpu, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10060 | vcpu_put(vcpu); |
| 10061 | return 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10062 | |
| 10063 | free_guest_fpu: |
| 10064 | kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu); |
| 10065 | free_user_fpu: |
| 10066 | kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu); |
| 10067 | free_emulate_ctxt: |
| 10068 | kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); |
| 10069 | free_wbinvd_dirty_mask: |
| 10070 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); |
| 10071 | fail_free_mce_banks: |
| 10072 | kfree(vcpu->arch.mce_banks); |
| 10073 | fail_free_pio_data: |
| 10074 | free_page((unsigned long)vcpu->arch.pio_data); |
| 10075 | fail_free_lapic: |
| 10076 | kvm_free_lapic(vcpu); |
| 10077 | fail_mmu_destroy: |
| 10078 | kvm_mmu_destroy(vcpu); |
| 10079 | return r; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10080 | } |
| 10081 | |
| 10082 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
| 10083 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10084 | struct kvm *kvm = vcpu->kvm; |
| 10085 | |
| 10086 | kvm_hv_vcpu_postcreate(vcpu); |
| 10087 | |
| 10088 | if (mutex_lock_killable(&vcpu->mutex)) |
| 10089 | return; |
| 10090 | vcpu_load(vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10091 | kvm_synchronize_tsc(vcpu, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10092 | vcpu_put(vcpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10093 | |
| 10094 | /* poll control enabled by default */ |
| 10095 | vcpu->arch.msr_kvm_poll_control = 1; |
| 10096 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10097 | mutex_unlock(&vcpu->mutex); |
| 10098 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10099 | if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) |
| 10100 | schedule_delayed_work(&kvm->arch.kvmclock_sync_work, |
| 10101 | KVMCLOCK_SYNC_PERIOD); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10102 | } |
| 10103 | |
| 10104 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 10105 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10106 | struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache; |
| 10107 | int idx; |
| 10108 | |
| 10109 | kvm_release_pfn(cache->pfn, cache->dirty, cache); |
| 10110 | |
| 10111 | kvmclock_reset(vcpu); |
| 10112 | |
| 10113 | kvm_x86_ops.vcpu_free(vcpu); |
| 10114 | |
| 10115 | kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); |
| 10116 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); |
| 10117 | kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu); |
| 10118 | kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu); |
| 10119 | |
| 10120 | kvm_hv_vcpu_uninit(vcpu); |
| 10121 | kvm_pmu_destroy(vcpu); |
| 10122 | kfree(vcpu->arch.mce_banks); |
| 10123 | kvm_free_lapic(vcpu); |
| 10124 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 10125 | kvm_mmu_destroy(vcpu); |
| 10126 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 10127 | free_page((unsigned long)vcpu->arch.pio_data); |
| 10128 | kvfree(vcpu->arch.cpuid_entries); |
| 10129 | if (!lapic_in_kernel(vcpu)) |
| 10130 | static_key_slow_dec(&kvm_no_apic_vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10131 | } |
| 10132 | |
| 10133 | void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
| 10134 | { |
| 10135 | kvm_lapic_reset(vcpu, init_event); |
| 10136 | |
| 10137 | vcpu->arch.hflags = 0; |
| 10138 | |
| 10139 | vcpu->arch.smi_pending = 0; |
| 10140 | vcpu->arch.smi_count = 0; |
| 10141 | atomic_set(&vcpu->arch.nmi_queued, 0); |
| 10142 | vcpu->arch.nmi_pending = 0; |
| 10143 | vcpu->arch.nmi_injected = false; |
| 10144 | kvm_clear_interrupt_queue(vcpu); |
| 10145 | kvm_clear_exception_queue(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10146 | |
| 10147 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); |
| 10148 | kvm_update_dr0123(vcpu); |
| 10149 | vcpu->arch.dr6 = DR6_INIT; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10150 | vcpu->arch.dr7 = DR7_FIXED_1; |
| 10151 | kvm_update_dr7(vcpu); |
| 10152 | |
| 10153 | vcpu->arch.cr2 = 0; |
| 10154 | |
| 10155 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10156 | vcpu->arch.apf.msr_en_val = 0; |
| 10157 | vcpu->arch.apf.msr_int_val = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10158 | vcpu->arch.st.msr_val = 0; |
| 10159 | |
| 10160 | kvmclock_reset(vcpu); |
| 10161 | |
| 10162 | kvm_clear_async_pf_completion_queue(vcpu); |
| 10163 | kvm_async_pf_hash_reset(vcpu); |
| 10164 | vcpu->arch.apf.halted = false; |
| 10165 | |
| 10166 | if (kvm_mpx_supported()) { |
| 10167 | void *mpx_state_buffer; |
| 10168 | |
| 10169 | /* |
| 10170 | * To avoid have the INIT path from kvm_apic_has_events() that be |
| 10171 | * called with loaded FPU and does not let userspace fix the state. |
| 10172 | */ |
| 10173 | if (init_event) |
| 10174 | kvm_put_guest_fpu(vcpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10175 | mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave, |
| 10176 | XFEATURE_BNDREGS); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10177 | if (mpx_state_buffer) |
| 10178 | memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10179 | mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave, |
| 10180 | XFEATURE_BNDCSR); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10181 | if (mpx_state_buffer) |
| 10182 | memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr)); |
| 10183 | if (init_event) |
| 10184 | kvm_load_guest_fpu(vcpu); |
| 10185 | } |
| 10186 | |
| 10187 | if (!init_event) { |
| 10188 | kvm_pmu_reset(vcpu); |
| 10189 | vcpu->arch.smbase = 0x30000; |
| 10190 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10191 | vcpu->arch.msr_misc_features_enables = 0; |
| 10192 | |
| 10193 | vcpu->arch.xcr0 = XFEATURE_MASK_FP; |
| 10194 | } |
| 10195 | |
| 10196 | memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); |
| 10197 | vcpu->arch.regs_avail = ~0; |
| 10198 | vcpu->arch.regs_dirty = ~0; |
| 10199 | |
| 10200 | vcpu->arch.ia32_xss = 0; |
| 10201 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10202 | kvm_x86_ops.vcpu_reset(vcpu, init_event); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10203 | } |
| 10204 | |
| 10205 | void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) |
| 10206 | { |
| 10207 | struct kvm_segment cs; |
| 10208 | |
| 10209 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 10210 | cs.selector = vector << 8; |
| 10211 | cs.base = vector << 12; |
| 10212 | kvm_set_segment(vcpu, &cs, VCPU_SREG_CS); |
| 10213 | kvm_rip_write(vcpu, 0); |
| 10214 | } |
| 10215 | |
| 10216 | int kvm_arch_hardware_enable(void) |
| 10217 | { |
| 10218 | struct kvm *kvm; |
| 10219 | struct kvm_vcpu *vcpu; |
| 10220 | int i; |
| 10221 | int ret; |
| 10222 | u64 local_tsc; |
| 10223 | u64 max_tsc = 0; |
| 10224 | bool stable, backwards_tsc = false; |
| 10225 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10226 | kvm_user_return_msr_cpu_online(); |
| 10227 | ret = kvm_x86_ops.hardware_enable(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10228 | if (ret != 0) |
| 10229 | return ret; |
| 10230 | |
| 10231 | local_tsc = rdtsc(); |
| 10232 | stable = !kvm_check_tsc_unstable(); |
| 10233 | list_for_each_entry(kvm, &vm_list, vm_list) { |
| 10234 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 10235 | if (!stable && vcpu->cpu == smp_processor_id()) |
| 10236 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
| 10237 | if (stable && vcpu->arch.last_host_tsc > local_tsc) { |
| 10238 | backwards_tsc = true; |
| 10239 | if (vcpu->arch.last_host_tsc > max_tsc) |
| 10240 | max_tsc = vcpu->arch.last_host_tsc; |
| 10241 | } |
| 10242 | } |
| 10243 | } |
| 10244 | |
| 10245 | /* |
| 10246 | * Sometimes, even reliable TSCs go backwards. This happens on |
| 10247 | * platforms that reset TSC during suspend or hibernate actions, but |
| 10248 | * maintain synchronization. We must compensate. Fortunately, we can |
| 10249 | * detect that condition here, which happens early in CPU bringup, |
| 10250 | * before any KVM threads can be running. Unfortunately, we can't |
| 10251 | * bring the TSCs fully up to date with real time, as we aren't yet far |
| 10252 | * enough into CPU bringup that we know how much real time has actually |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10253 | * elapsed; our helper function, ktime_get_boottime_ns() will be using boot |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10254 | * variables that haven't been updated yet. |
| 10255 | * |
| 10256 | * So we simply find the maximum observed TSC above, then record the |
| 10257 | * adjustment to TSC in each VCPU. When the VCPU later gets loaded, |
| 10258 | * the adjustment will be applied. Note that we accumulate |
| 10259 | * adjustments, in case multiple suspend cycles happen before some VCPU |
| 10260 | * gets a chance to run again. In the event that no KVM threads get a |
| 10261 | * chance to run, we will miss the entire elapsed period, as we'll have |
| 10262 | * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may |
| 10263 | * loose cycle time. This isn't too big a deal, since the loss will be |
| 10264 | * uniform across all VCPUs (not to mention the scenario is extremely |
| 10265 | * unlikely). It is possible that a second hibernate recovery happens |
| 10266 | * much faster than a first, causing the observed TSC here to be |
| 10267 | * smaller; this would require additional padding adjustment, which is |
| 10268 | * why we set last_host_tsc to the local tsc observed here. |
| 10269 | * |
| 10270 | * N.B. - this code below runs only on platforms with reliable TSC, |
| 10271 | * as that is the only way backwards_tsc is set above. Also note |
| 10272 | * that this runs for ALL vcpus, which is not a bug; all VCPUs should |
| 10273 | * have the same delta_cyc adjustment applied if backwards_tsc |
| 10274 | * is detected. Note further, this adjustment is only done once, |
| 10275 | * as we reset last_host_tsc on all VCPUs to stop this from being |
| 10276 | * called multiple times (one for each physical CPU bringup). |
| 10277 | * |
| 10278 | * Platforms with unreliable TSCs don't have to deal with this, they |
| 10279 | * will be compensated by the logic in vcpu_load, which sets the TSC to |
| 10280 | * catchup mode. This will catchup all VCPUs to real time, but cannot |
| 10281 | * guarantee that they stay in perfect synchronization. |
| 10282 | */ |
| 10283 | if (backwards_tsc) { |
| 10284 | u64 delta_cyc = max_tsc - local_tsc; |
| 10285 | list_for_each_entry(kvm, &vm_list, vm_list) { |
| 10286 | kvm->arch.backwards_tsc_observed = true; |
| 10287 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 10288 | vcpu->arch.tsc_offset_adjustment += delta_cyc; |
| 10289 | vcpu->arch.last_host_tsc = local_tsc; |
| 10290 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
| 10291 | } |
| 10292 | |
| 10293 | /* |
| 10294 | * We have to disable TSC offset matching.. if you were |
| 10295 | * booting a VM while issuing an S4 host suspend.... |
| 10296 | * you may have some problem. Solving this issue is |
| 10297 | * left as an exercise to the reader. |
| 10298 | */ |
| 10299 | kvm->arch.last_tsc_nsec = 0; |
| 10300 | kvm->arch.last_tsc_write = 0; |
| 10301 | } |
| 10302 | |
| 10303 | } |
| 10304 | return 0; |
| 10305 | } |
| 10306 | |
| 10307 | void kvm_arch_hardware_disable(void) |
| 10308 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10309 | kvm_x86_ops.hardware_disable(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10310 | drop_user_return_notifiers(); |
| 10311 | } |
| 10312 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10313 | int kvm_arch_hardware_setup(void *opaque) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10314 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10315 | struct kvm_x86_init_ops *ops = opaque; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10316 | int r; |
| 10317 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10318 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 10319 | |
| 10320 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
| 10321 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 10322 | |
| 10323 | r = ops->hardware_setup(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10324 | if (r != 0) |
| 10325 | return r; |
| 10326 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10327 | memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); |
| 10328 | |
| 10329 | if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES)) |
| 10330 | supported_xss = 0; |
| 10331 | |
| 10332 | #define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f) |
| 10333 | cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_); |
| 10334 | #undef __kvm_cpu_cap_has |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10335 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10336 | if (kvm_has_tsc_control) { |
| 10337 | /* |
| 10338 | * Make sure the user can only configure tsc_khz values that |
| 10339 | * fit into a signed integer. |
| 10340 | * A min value is not calculated because it will always |
| 10341 | * be 1 on all machines. |
| 10342 | */ |
| 10343 | u64 max = min(0x7fffffffULL, |
| 10344 | __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz)); |
| 10345 | kvm_max_guest_tsc_khz = max; |
| 10346 | |
| 10347 | kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits; |
| 10348 | } |
| 10349 | |
| 10350 | kvm_init_msr_list(); |
| 10351 | return 0; |
| 10352 | } |
| 10353 | |
| 10354 | void kvm_arch_hardware_unsetup(void) |
| 10355 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10356 | kvm_x86_ops.hardware_unsetup(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10357 | } |
| 10358 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10359 | int kvm_arch_check_processor_compat(void *opaque) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10360 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10361 | struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); |
| 10362 | struct kvm_x86_init_ops *ops = opaque; |
| 10363 | |
| 10364 | WARN_ON(!irqs_disabled()); |
| 10365 | |
| 10366 | if (__cr4_reserved_bits(cpu_has, c) != |
| 10367 | __cr4_reserved_bits(cpu_has, &boot_cpu_data)) |
| 10368 | return -EIO; |
| 10369 | |
| 10370 | return ops->check_processor_compatibility(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10371 | } |
| 10372 | |
| 10373 | bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) |
| 10374 | { |
| 10375 | return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; |
| 10376 | } |
| 10377 | EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp); |
| 10378 | |
| 10379 | bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu) |
| 10380 | { |
| 10381 | return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; |
| 10382 | } |
| 10383 | |
| 10384 | struct static_key kvm_no_apic_vcpu __read_mostly; |
| 10385 | EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu); |
| 10386 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10387 | void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) |
| 10388 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10389 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 10390 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10391 | vcpu->arch.l1tf_flush_l1d = true; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10392 | if (pmu->version && unlikely(pmu->event_count)) { |
| 10393 | pmu->need_cleanup = true; |
| 10394 | kvm_make_request(KVM_REQ_PMU, vcpu); |
| 10395 | } |
| 10396 | kvm_x86_ops.sched_in(vcpu, cpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10397 | } |
| 10398 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10399 | void kvm_arch_free_vm(struct kvm *kvm) |
| 10400 | { |
| 10401 | kfree(kvm->arch.hyperv.hv_pa_pg); |
| 10402 | vfree(kvm); |
| 10403 | } |
| 10404 | |
| 10405 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10406 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
| 10407 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10408 | int ret; |
| 10409 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10410 | if (type) |
| 10411 | return -EINVAL; |
| 10412 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10413 | ret = kvm_page_track_init(kvm); |
| 10414 | if (ret) |
| 10415 | return ret; |
| 10416 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10417 | INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); |
| 10418 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
| 10419 | INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10420 | INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10421 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
| 10422 | atomic_set(&kvm->arch.noncoherent_dma_count, 0); |
| 10423 | |
| 10424 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
| 10425 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); |
| 10426 | /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ |
| 10427 | set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, |
| 10428 | &kvm->arch.irq_sources_bitmap); |
| 10429 | |
| 10430 | raw_spin_lock_init(&kvm->arch.tsc_write_lock); |
| 10431 | mutex_init(&kvm->arch.apic_map_lock); |
| 10432 | spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock); |
| 10433 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10434 | kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10435 | pvclock_update_vm_gtod_copy(kvm); |
| 10436 | |
| 10437 | kvm->arch.guest_can_read_msr_platform_info = true; |
| 10438 | |
| 10439 | INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); |
| 10440 | INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); |
| 10441 | |
| 10442 | kvm_hv_init_vm(kvm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10443 | kvm_mmu_init_vm(kvm); |
| 10444 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10445 | return kvm_x86_ops.vm_init(kvm); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10446 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10447 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10448 | int kvm_arch_post_init_vm(struct kvm *kvm) |
| 10449 | { |
| 10450 | return kvm_mmu_post_init_vm(kvm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10451 | } |
| 10452 | |
| 10453 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
| 10454 | { |
| 10455 | vcpu_load(vcpu); |
| 10456 | kvm_mmu_unload(vcpu); |
| 10457 | vcpu_put(vcpu); |
| 10458 | } |
| 10459 | |
| 10460 | static void kvm_free_vcpus(struct kvm *kvm) |
| 10461 | { |
| 10462 | unsigned int i; |
| 10463 | struct kvm_vcpu *vcpu; |
| 10464 | |
| 10465 | /* |
| 10466 | * Unpin any mmu pages first. |
| 10467 | */ |
| 10468 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 10469 | kvm_clear_async_pf_completion_queue(vcpu); |
| 10470 | kvm_unload_vcpu_mmu(vcpu); |
| 10471 | } |
| 10472 | kvm_for_each_vcpu(i, vcpu, kvm) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10473 | kvm_vcpu_destroy(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10474 | |
| 10475 | mutex_lock(&kvm->lock); |
| 10476 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 10477 | kvm->vcpus[i] = NULL; |
| 10478 | |
| 10479 | atomic_set(&kvm->online_vcpus, 0); |
| 10480 | mutex_unlock(&kvm->lock); |
| 10481 | } |
| 10482 | |
| 10483 | void kvm_arch_sync_events(struct kvm *kvm) |
| 10484 | { |
| 10485 | cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); |
| 10486 | cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); |
| 10487 | kvm_free_pit(kvm); |
| 10488 | } |
| 10489 | |
| 10490 | int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) |
| 10491 | { |
| 10492 | int i, r; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10493 | unsigned long hva, old_npages; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10494 | struct kvm_memslots *slots = kvm_memslots(kvm); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10495 | struct kvm_memory_slot *slot; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10496 | |
| 10497 | /* Called with kvm->slots_lock held. */ |
| 10498 | if (WARN_ON(id >= KVM_MEM_SLOTS_NUM)) |
| 10499 | return -EINVAL; |
| 10500 | |
| 10501 | slot = id_to_memslot(slots, id); |
| 10502 | if (size) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10503 | if (slot && slot->npages) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10504 | return -EEXIST; |
| 10505 | |
| 10506 | /* |
| 10507 | * MAP_SHARED to prevent internal slot pages from being moved |
| 10508 | * by fork()/COW. |
| 10509 | */ |
| 10510 | hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE, |
| 10511 | MAP_SHARED | MAP_ANONYMOUS, 0); |
| 10512 | if (IS_ERR((void *)hva)) |
| 10513 | return PTR_ERR((void *)hva); |
| 10514 | } else { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10515 | if (!slot || !slot->npages) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10516 | return 0; |
| 10517 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10518 | old_npages = slot->npages; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10519 | hva = 0; |
| 10520 | } |
| 10521 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10522 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
| 10523 | struct kvm_userspace_memory_region m; |
| 10524 | |
| 10525 | m.slot = id | (i << 16); |
| 10526 | m.flags = 0; |
| 10527 | m.guest_phys_addr = gpa; |
| 10528 | m.userspace_addr = hva; |
| 10529 | m.memory_size = size; |
| 10530 | r = __kvm_set_memory_region(kvm, &m); |
| 10531 | if (r < 0) |
| 10532 | return r; |
| 10533 | } |
| 10534 | |
| 10535 | if (!size) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10536 | vm_munmap(hva, old_npages * PAGE_SIZE); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10537 | |
| 10538 | return 0; |
| 10539 | } |
| 10540 | EXPORT_SYMBOL_GPL(__x86_set_memory_region); |
| 10541 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10542 | void kvm_arch_pre_destroy_vm(struct kvm *kvm) |
| 10543 | { |
| 10544 | kvm_mmu_pre_destroy_vm(kvm); |
| 10545 | } |
| 10546 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10547 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 10548 | { |
| 10549 | if (current->mm == kvm->mm) { |
| 10550 | /* |
| 10551 | * Free memory regions allocated on behalf of userspace, |
| 10552 | * unless the the memory map has changed due to process exit |
| 10553 | * or fd copying. |
| 10554 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10555 | mutex_lock(&kvm->slots_lock); |
| 10556 | __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 10557 | 0, 0); |
| 10558 | __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
| 10559 | 0, 0); |
| 10560 | __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0); |
| 10561 | mutex_unlock(&kvm->slots_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10562 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10563 | if (kvm_x86_ops.vm_destroy) |
| 10564 | kvm_x86_ops.vm_destroy(kvm); |
| 10565 | kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->srcu, 1)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10566 | kvm_pic_destroy(kvm); |
| 10567 | kvm_ioapic_destroy(kvm); |
| 10568 | kvm_free_vcpus(kvm); |
| 10569 | kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10570 | kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10571 | kvm_mmu_uninit_vm(kvm); |
| 10572 | kvm_page_track_cleanup(kvm); |
| 10573 | kvm_hv_destroy_vm(kvm); |
| 10574 | } |
| 10575 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10576 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10577 | { |
| 10578 | int i; |
| 10579 | |
| 10580 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10581 | kvfree(slot->arch.rmap[i]); |
| 10582 | slot->arch.rmap[i] = NULL; |
| 10583 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10584 | if (i == 0) |
| 10585 | continue; |
| 10586 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10587 | kvfree(slot->arch.lpage_info[i - 1]); |
| 10588 | slot->arch.lpage_info[i - 1] = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10589 | } |
| 10590 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10591 | kvm_page_track_free_memslot(slot); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10592 | } |
| 10593 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10594 | static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot, |
| 10595 | unsigned long npages) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10596 | { |
| 10597 | int i; |
| 10598 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10599 | /* |
| 10600 | * Clear out the previous array pointers for the KVM_MR_MOVE case. The |
| 10601 | * old arrays will be freed by __kvm_set_memory_region() if installing |
| 10602 | * the new memslot is successful. |
| 10603 | */ |
| 10604 | memset(&slot->arch, 0, sizeof(slot->arch)); |
| 10605 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10606 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
| 10607 | struct kvm_lpage_info *linfo; |
| 10608 | unsigned long ugfn; |
| 10609 | int lpages; |
| 10610 | int level = i + 1; |
| 10611 | |
| 10612 | lpages = gfn_to_index(slot->base_gfn + npages - 1, |
| 10613 | slot->base_gfn, level) + 1; |
| 10614 | |
| 10615 | slot->arch.rmap[i] = |
| 10616 | kvcalloc(lpages, sizeof(*slot->arch.rmap[i]), |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10617 | GFP_KERNEL_ACCOUNT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10618 | if (!slot->arch.rmap[i]) |
| 10619 | goto out_free; |
| 10620 | if (i == 0) |
| 10621 | continue; |
| 10622 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10623 | linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10624 | if (!linfo) |
| 10625 | goto out_free; |
| 10626 | |
| 10627 | slot->arch.lpage_info[i - 1] = linfo; |
| 10628 | |
| 10629 | if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) |
| 10630 | linfo[0].disallow_lpage = 1; |
| 10631 | if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) |
| 10632 | linfo[lpages - 1].disallow_lpage = 1; |
| 10633 | ugfn = slot->userspace_addr >> PAGE_SHIFT; |
| 10634 | /* |
| 10635 | * If the gfn and userspace address are not aligned wrt each |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10636 | * other, disable large page support for this slot. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10637 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10638 | if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10639 | unsigned long j; |
| 10640 | |
| 10641 | for (j = 0; j < lpages; ++j) |
| 10642 | linfo[j].disallow_lpage = 1; |
| 10643 | } |
| 10644 | } |
| 10645 | |
| 10646 | if (kvm_page_track_create_memslot(slot, npages)) |
| 10647 | goto out_free; |
| 10648 | |
| 10649 | return 0; |
| 10650 | |
| 10651 | out_free: |
| 10652 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
| 10653 | kvfree(slot->arch.rmap[i]); |
| 10654 | slot->arch.rmap[i] = NULL; |
| 10655 | if (i == 0) |
| 10656 | continue; |
| 10657 | |
| 10658 | kvfree(slot->arch.lpage_info[i - 1]); |
| 10659 | slot->arch.lpage_info[i - 1] = NULL; |
| 10660 | } |
| 10661 | return -ENOMEM; |
| 10662 | } |
| 10663 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10664 | void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10665 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10666 | struct kvm_vcpu *vcpu; |
| 10667 | int i; |
| 10668 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10669 | /* |
| 10670 | * memslots->generation has been incremented. |
| 10671 | * mmio generation may have reached its maximum value. |
| 10672 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10673 | kvm_mmu_invalidate_mmio_sptes(kvm, gen); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10674 | |
| 10675 | /* Force re-initialization of steal_time cache */ |
| 10676 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 10677 | kvm_vcpu_kick(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10678 | } |
| 10679 | |
| 10680 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
| 10681 | struct kvm_memory_slot *memslot, |
| 10682 | const struct kvm_userspace_memory_region *mem, |
| 10683 | enum kvm_mr_change change) |
| 10684 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10685 | if (change == KVM_MR_CREATE || change == KVM_MR_MOVE) |
| 10686 | return kvm_alloc_memslot_metadata(memslot, |
| 10687 | mem->memory_size >> PAGE_SHIFT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10688 | return 0; |
| 10689 | } |
| 10690 | |
| 10691 | static void kvm_mmu_slot_apply_flags(struct kvm *kvm, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10692 | struct kvm_memory_slot *old, |
| 10693 | struct kvm_memory_slot *new, |
| 10694 | enum kvm_mr_change change) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10695 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10696 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10697 | * Nothing to do for RO slots or CREATE/MOVE/DELETE of a slot. |
| 10698 | * See comments below. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10699 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10700 | if ((change != KVM_MR_FLAGS_ONLY) || (new->flags & KVM_MEM_READONLY)) |
| 10701 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10702 | |
| 10703 | /* |
| 10704 | * Dirty logging tracks sptes in 4k granularity, meaning that large |
| 10705 | * sptes have to be split. If live migration is successful, the guest |
| 10706 | * in the source machine will be destroyed and large sptes will be |
| 10707 | * created in the destination. However, if the guest continues to run |
| 10708 | * in the source machine (for example if live migration fails), small |
| 10709 | * sptes will remain around and cause bad performance. |
| 10710 | * |
| 10711 | * Scan sptes if dirty logging has been stopped, dropping those |
| 10712 | * which can be collapsed into a single large-page spte. Later |
| 10713 | * page faults will create the large-page sptes. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10714 | * |
| 10715 | * There is no need to do this in any of the following cases: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10716 | * CREATE: No dirty mappings will already exist. |
| 10717 | * MOVE/DELETE: The old mappings will already have been cleaned up by |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10718 | * kvm_arch_flush_shadow_memslot() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10719 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10720 | if ((old->flags & KVM_MEM_LOG_DIRTY_PAGES) && |
| 10721 | !(new->flags & KVM_MEM_LOG_DIRTY_PAGES)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10722 | kvm_mmu_zap_collapsible_sptes(kvm, new); |
| 10723 | |
| 10724 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10725 | * Enable or disable dirty logging for the slot. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10726 | * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10727 | * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of the old |
| 10728 | * slot have been zapped so no dirty logging updates are needed for |
| 10729 | * the old slot. |
| 10730 | * For KVM_MR_CREATE and KVM_MR_MOVE, once the new slot is visible |
| 10731 | * any mappings that might be created in it will consume the |
| 10732 | * properties of the new slot and do not need to be updated here. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10733 | * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10734 | * When PML is enabled, the kvm_x86_ops dirty logging hooks are |
| 10735 | * called to enable/disable dirty logging. |
| 10736 | * |
| 10737 | * When disabling dirty logging with PML enabled, the D-bit is set |
| 10738 | * for sptes in the slot in order to prevent unnecessary GPA |
| 10739 | * logging in the PML buffer (and potential PML buffer full VMEXIT). |
| 10740 | * This guarantees leaving PML enabled for the guest's lifetime |
| 10741 | * won't have any additional overhead from PML when the guest is |
| 10742 | * running with dirty logging disabled. |
| 10743 | * |
| 10744 | * When enabling dirty logging, large sptes are write-protected |
| 10745 | * so they can be split on first write. New large sptes cannot |
| 10746 | * be created for this slot until the end of the logging. |
| 10747 | * See the comments in fast_page_fault(). |
| 10748 | * For small sptes, nothing is done if the dirty log is in the |
| 10749 | * initial-all-set state. Otherwise, depending on whether pml |
| 10750 | * is enabled the D-bit or the W-bit will be cleared. |
| 10751 | */ |
| 10752 | if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) { |
| 10753 | if (kvm_x86_ops.slot_enable_log_dirty) { |
| 10754 | kvm_x86_ops.slot_enable_log_dirty(kvm, new); |
| 10755 | } else { |
| 10756 | int level = |
| 10757 | kvm_dirty_log_manual_protect_and_init_set(kvm) ? |
| 10758 | PG_LEVEL_2M : PG_LEVEL_4K; |
| 10759 | |
| 10760 | /* |
| 10761 | * If we're with initial-all-set, we don't need |
| 10762 | * to write protect any small page because |
| 10763 | * they're reported as dirty already. However |
| 10764 | * we still need to write-protect huge pages |
| 10765 | * so that the page split can happen lazily on |
| 10766 | * the first write to the huge page. |
| 10767 | */ |
| 10768 | kvm_mmu_slot_remove_write_access(kvm, new, level); |
| 10769 | } |
| 10770 | } else { |
| 10771 | if (kvm_x86_ops.slot_disable_log_dirty) |
| 10772 | kvm_x86_ops.slot_disable_log_dirty(kvm, new); |
| 10773 | } |
| 10774 | } |
| 10775 | |
| 10776 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
| 10777 | const struct kvm_userspace_memory_region *mem, |
| 10778 | struct kvm_memory_slot *old, |
| 10779 | const struct kvm_memory_slot *new, |
| 10780 | enum kvm_mr_change change) |
| 10781 | { |
| 10782 | if (!kvm->arch.n_requested_mmu_pages) |
| 10783 | kvm_mmu_change_mmu_pages(kvm, |
| 10784 | kvm_mmu_calculate_default_mmu_pages(kvm)); |
| 10785 | |
| 10786 | /* |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10787 | * FIXME: const-ify all uses of struct kvm_memory_slot. |
| 10788 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10789 | kvm_mmu_slot_apply_flags(kvm, old, (struct kvm_memory_slot *) new, change); |
| 10790 | |
| 10791 | /* Free the arrays associated with the old memslot. */ |
| 10792 | if (change == KVM_MR_MOVE) |
| 10793 | kvm_arch_free_memslot(kvm, old); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10794 | } |
| 10795 | |
| 10796 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
| 10797 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10798 | kvm_mmu_zap_all(kvm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10799 | } |
| 10800 | |
| 10801 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 10802 | struct kvm_memory_slot *slot) |
| 10803 | { |
| 10804 | kvm_page_track_flush_slot(kvm, slot); |
| 10805 | } |
| 10806 | |
| 10807 | static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu) |
| 10808 | { |
| 10809 | return (is_guest_mode(vcpu) && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10810 | kvm_x86_ops.guest_apic_has_interrupt && |
| 10811 | kvm_x86_ops.guest_apic_has_interrupt(vcpu)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10812 | } |
| 10813 | |
| 10814 | static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) |
| 10815 | { |
| 10816 | if (!list_empty_careful(&vcpu->async_pf.done)) |
| 10817 | return true; |
| 10818 | |
| 10819 | if (kvm_apic_has_events(vcpu)) |
| 10820 | return true; |
| 10821 | |
| 10822 | if (vcpu->arch.pv.pv_unhalted) |
| 10823 | return true; |
| 10824 | |
| 10825 | if (vcpu->arch.exception.pending) |
| 10826 | return true; |
| 10827 | |
| 10828 | if (kvm_test_request(KVM_REQ_NMI, vcpu) || |
| 10829 | (vcpu->arch.nmi_pending && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10830 | kvm_x86_ops.nmi_allowed(vcpu, false))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10831 | return true; |
| 10832 | |
| 10833 | if (kvm_test_request(KVM_REQ_SMI, vcpu) || |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10834 | (vcpu->arch.smi_pending && |
| 10835 | kvm_x86_ops.smi_allowed(vcpu, false))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10836 | return true; |
| 10837 | |
| 10838 | if (kvm_arch_interrupt_allowed(vcpu) && |
| 10839 | (kvm_cpu_has_interrupt(vcpu) || |
| 10840 | kvm_guest_apic_has_interrupt(vcpu))) |
| 10841 | return true; |
| 10842 | |
| 10843 | if (kvm_hv_has_stimer_pending(vcpu)) |
| 10844 | return true; |
| 10845 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10846 | if (is_guest_mode(vcpu) && |
| 10847 | kvm_x86_ops.nested_ops->hv_timer_pending && |
| 10848 | kvm_x86_ops.nested_ops->hv_timer_pending(vcpu)) |
| 10849 | return true; |
| 10850 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10851 | return false; |
| 10852 | } |
| 10853 | |
| 10854 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 10855 | { |
| 10856 | return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu); |
| 10857 | } |
| 10858 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10859 | bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) |
| 10860 | { |
| 10861 | if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) |
| 10862 | return true; |
| 10863 | |
| 10864 | if (kvm_test_request(KVM_REQ_NMI, vcpu) || |
| 10865 | kvm_test_request(KVM_REQ_SMI, vcpu) || |
| 10866 | kvm_test_request(KVM_REQ_EVENT, vcpu)) |
| 10867 | return true; |
| 10868 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10869 | if (vcpu->arch.apicv_active && kvm_x86_ops.dy_apicv_has_pending_interrupt(vcpu)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10870 | return true; |
| 10871 | |
| 10872 | return false; |
| 10873 | } |
| 10874 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10875 | bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) |
| 10876 | { |
| 10877 | return vcpu->arch.preempted_in_kernel; |
| 10878 | } |
| 10879 | |
| 10880 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
| 10881 | { |
| 10882 | return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; |
| 10883 | } |
| 10884 | |
| 10885 | int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 10886 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10887 | return kvm_x86_ops.interrupt_allowed(vcpu, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10888 | } |
| 10889 | |
| 10890 | unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu) |
| 10891 | { |
| 10892 | if (is_64_bit_mode(vcpu)) |
| 10893 | return kvm_rip_read(vcpu); |
| 10894 | return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) + |
| 10895 | kvm_rip_read(vcpu)); |
| 10896 | } |
| 10897 | EXPORT_SYMBOL_GPL(kvm_get_linear_rip); |
| 10898 | |
| 10899 | bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip) |
| 10900 | { |
| 10901 | return kvm_get_linear_rip(vcpu) == linear_rip; |
| 10902 | } |
| 10903 | EXPORT_SYMBOL_GPL(kvm_is_linear_rip); |
| 10904 | |
| 10905 | unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu) |
| 10906 | { |
| 10907 | unsigned long rflags; |
| 10908 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10909 | rflags = kvm_x86_ops.get_rflags(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10910 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 10911 | rflags &= ~X86_EFLAGS_TF; |
| 10912 | return rflags; |
| 10913 | } |
| 10914 | EXPORT_SYMBOL_GPL(kvm_get_rflags); |
| 10915 | |
| 10916 | static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 10917 | { |
| 10918 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && |
| 10919 | kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) |
| 10920 | rflags |= X86_EFLAGS_TF; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10921 | kvm_x86_ops.set_rflags(vcpu, rflags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10922 | } |
| 10923 | |
| 10924 | void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 10925 | { |
| 10926 | __kvm_set_rflags(vcpu, rflags); |
| 10927 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 10928 | } |
| 10929 | EXPORT_SYMBOL_GPL(kvm_set_rflags); |
| 10930 | |
| 10931 | void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) |
| 10932 | { |
| 10933 | int r; |
| 10934 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10935 | if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) || |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10936 | work->wakeup_all) |
| 10937 | return; |
| 10938 | |
| 10939 | r = kvm_mmu_reload(vcpu); |
| 10940 | if (unlikely(r)) |
| 10941 | return; |
| 10942 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10943 | if (!vcpu->arch.mmu->direct_map && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10944 | work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10945 | return; |
| 10946 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10947 | kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10948 | } |
| 10949 | |
| 10950 | static inline u32 kvm_async_pf_hash_fn(gfn_t gfn) |
| 10951 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10952 | BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU)); |
| 10953 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10954 | return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU)); |
| 10955 | } |
| 10956 | |
| 10957 | static inline u32 kvm_async_pf_next_probe(u32 key) |
| 10958 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10959 | return (key + 1) & (ASYNC_PF_PER_VCPU - 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10960 | } |
| 10961 | |
| 10962 | static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 10963 | { |
| 10964 | u32 key = kvm_async_pf_hash_fn(gfn); |
| 10965 | |
| 10966 | while (vcpu->arch.apf.gfns[key] != ~0) |
| 10967 | key = kvm_async_pf_next_probe(key); |
| 10968 | |
| 10969 | vcpu->arch.apf.gfns[key] = gfn; |
| 10970 | } |
| 10971 | |
| 10972 | static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 10973 | { |
| 10974 | int i; |
| 10975 | u32 key = kvm_async_pf_hash_fn(gfn); |
| 10976 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10977 | for (i = 0; i < ASYNC_PF_PER_VCPU && |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10978 | (vcpu->arch.apf.gfns[key] != gfn && |
| 10979 | vcpu->arch.apf.gfns[key] != ~0); i++) |
| 10980 | key = kvm_async_pf_next_probe(key); |
| 10981 | |
| 10982 | return key; |
| 10983 | } |
| 10984 | |
| 10985 | bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 10986 | { |
| 10987 | return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; |
| 10988 | } |
| 10989 | |
| 10990 | static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 10991 | { |
| 10992 | u32 i, j, k; |
| 10993 | |
| 10994 | i = j = kvm_async_pf_gfn_slot(vcpu, gfn); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10995 | |
| 10996 | if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) |
| 10997 | return; |
| 10998 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10999 | while (true) { |
| 11000 | vcpu->arch.apf.gfns[i] = ~0; |
| 11001 | do { |
| 11002 | j = kvm_async_pf_next_probe(j); |
| 11003 | if (vcpu->arch.apf.gfns[j] == ~0) |
| 11004 | return; |
| 11005 | k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); |
| 11006 | /* |
| 11007 | * k lies cyclically in ]i,j] |
| 11008 | * | i.k.j | |
| 11009 | * |....j i.k.| or |.k..j i...| |
| 11010 | */ |
| 11011 | } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j)); |
| 11012 | vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; |
| 11013 | i = j; |
| 11014 | } |
| 11015 | } |
| 11016 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11017 | static inline int apf_put_user_notpresent(struct kvm_vcpu *vcpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11018 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11019 | u32 reason = KVM_PV_REASON_PAGE_NOT_PRESENT; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11020 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11021 | return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, |
| 11022 | sizeof(reason)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11023 | } |
| 11024 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11025 | static inline int apf_put_user_ready(struct kvm_vcpu *vcpu, u32 token) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11026 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11027 | unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11028 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11029 | return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, |
| 11030 | &token, offset, sizeof(token)); |
| 11031 | } |
| 11032 | |
| 11033 | static inline bool apf_pageready_slot_free(struct kvm_vcpu *vcpu) |
| 11034 | { |
| 11035 | unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token); |
| 11036 | u32 val; |
| 11037 | |
| 11038 | if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, |
| 11039 | &val, offset, sizeof(val))) |
| 11040 | return false; |
| 11041 | |
| 11042 | return !val; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11043 | } |
| 11044 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11045 | static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu) |
| 11046 | { |
| 11047 | if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu)) |
| 11048 | return false; |
| 11049 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11050 | if (!kvm_pv_async_pf_enabled(vcpu) || |
| 11051 | (vcpu->arch.apf.send_user_only && kvm_x86_ops.get_cpl(vcpu) == 0)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11052 | return false; |
| 11053 | |
| 11054 | return true; |
| 11055 | } |
| 11056 | |
| 11057 | bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu) |
| 11058 | { |
| 11059 | if (unlikely(!lapic_in_kernel(vcpu) || |
| 11060 | kvm_event_needs_reinjection(vcpu) || |
| 11061 | vcpu->arch.exception.pending)) |
| 11062 | return false; |
| 11063 | |
| 11064 | if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) |
| 11065 | return false; |
| 11066 | |
| 11067 | /* |
| 11068 | * If interrupts are off we cannot even use an artificial |
| 11069 | * halt state. |
| 11070 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11071 | return kvm_arch_interrupt_allowed(vcpu); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11072 | } |
| 11073 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11074 | bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11075 | struct kvm_async_pf *work) |
| 11076 | { |
| 11077 | struct x86_exception fault; |
| 11078 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11079 | trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11080 | kvm_add_async_pf_gfn(vcpu, work->arch.gfn); |
| 11081 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11082 | if (kvm_can_deliver_async_pf(vcpu) && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11083 | !apf_put_user_notpresent(vcpu)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11084 | fault.vector = PF_VECTOR; |
| 11085 | fault.error_code_valid = true; |
| 11086 | fault.error_code = 0; |
| 11087 | fault.nested_page_fault = false; |
| 11088 | fault.address = work->arch.token; |
| 11089 | fault.async_page_fault = true; |
| 11090 | kvm_inject_page_fault(vcpu, &fault); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11091 | return true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11092 | } else { |
| 11093 | /* |
| 11094 | * It is not possible to deliver a paravirtualized asynchronous |
| 11095 | * page fault, but putting the guest in an artificial halt state |
| 11096 | * can be beneficial nevertheless: if an interrupt arrives, we |
| 11097 | * can deliver it timely and perhaps the guest will schedule |
| 11098 | * another process. When the instruction that triggered a page |
| 11099 | * fault is retried, hopefully the page will be ready in the host. |
| 11100 | */ |
| 11101 | kvm_make_request(KVM_REQ_APF_HALT, vcpu); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11102 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11103 | } |
| 11104 | } |
| 11105 | |
| 11106 | void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, |
| 11107 | struct kvm_async_pf *work) |
| 11108 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11109 | struct kvm_lapic_irq irq = { |
| 11110 | .delivery_mode = APIC_DM_FIXED, |
| 11111 | .vector = vcpu->arch.apf.vec |
| 11112 | }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11113 | |
| 11114 | if (work->wakeup_all) |
| 11115 | work->arch.token = ~0; /* broadcast wakeup */ |
| 11116 | else |
| 11117 | kvm_del_async_pf_gfn(vcpu, work->arch.gfn); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11118 | trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11119 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11120 | if ((work->wakeup_all || work->notpresent_injected) && |
| 11121 | kvm_pv_async_pf_enabled(vcpu) && |
| 11122 | !apf_put_user_ready(vcpu, work->arch.token)) { |
| 11123 | vcpu->arch.apf.pageready_pending = true; |
| 11124 | kvm_apic_set_irq(vcpu, &irq, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11125 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11126 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11127 | vcpu->arch.apf.halted = false; |
| 11128 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 11129 | } |
| 11130 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11131 | void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11132 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11133 | kvm_make_request(KVM_REQ_APF_READY, vcpu); |
| 11134 | if (!vcpu->arch.apf.pageready_pending) |
| 11135 | kvm_vcpu_kick(vcpu); |
| 11136 | } |
| 11137 | |
| 11138 | bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) |
| 11139 | { |
| 11140 | if (!kvm_pv_async_pf_enabled(vcpu)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11141 | return true; |
| 11142 | else |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11143 | return apf_pageready_slot_free(vcpu); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11144 | } |
| 11145 | |
| 11146 | void kvm_arch_start_assignment(struct kvm *kvm) |
| 11147 | { |
| 11148 | atomic_inc(&kvm->arch.assigned_device_count); |
| 11149 | } |
| 11150 | EXPORT_SYMBOL_GPL(kvm_arch_start_assignment); |
| 11151 | |
| 11152 | void kvm_arch_end_assignment(struct kvm *kvm) |
| 11153 | { |
| 11154 | atomic_dec(&kvm->arch.assigned_device_count); |
| 11155 | } |
| 11156 | EXPORT_SYMBOL_GPL(kvm_arch_end_assignment); |
| 11157 | |
| 11158 | bool kvm_arch_has_assigned_device(struct kvm *kvm) |
| 11159 | { |
| 11160 | return atomic_read(&kvm->arch.assigned_device_count); |
| 11161 | } |
| 11162 | EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device); |
| 11163 | |
| 11164 | void kvm_arch_register_noncoherent_dma(struct kvm *kvm) |
| 11165 | { |
| 11166 | atomic_inc(&kvm->arch.noncoherent_dma_count); |
| 11167 | } |
| 11168 | EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma); |
| 11169 | |
| 11170 | void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm) |
| 11171 | { |
| 11172 | atomic_dec(&kvm->arch.noncoherent_dma_count); |
| 11173 | } |
| 11174 | EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma); |
| 11175 | |
| 11176 | bool kvm_arch_has_noncoherent_dma(struct kvm *kvm) |
| 11177 | { |
| 11178 | return atomic_read(&kvm->arch.noncoherent_dma_count); |
| 11179 | } |
| 11180 | EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma); |
| 11181 | |
| 11182 | bool kvm_arch_has_irq_bypass(void) |
| 11183 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11184 | return true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11185 | } |
| 11186 | |
| 11187 | int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, |
| 11188 | struct irq_bypass_producer *prod) |
| 11189 | { |
| 11190 | struct kvm_kernel_irqfd *irqfd = |
| 11191 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11192 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11193 | |
| 11194 | irqfd->producer = prod; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11195 | kvm_arch_start_assignment(irqfd->kvm); |
| 11196 | ret = kvm_x86_ops.update_pi_irte(irqfd->kvm, |
| 11197 | prod->irq, irqfd->gsi, 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11198 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11199 | if (ret) |
| 11200 | kvm_arch_end_assignment(irqfd->kvm); |
| 11201 | |
| 11202 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11203 | } |
| 11204 | |
| 11205 | void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, |
| 11206 | struct irq_bypass_producer *prod) |
| 11207 | { |
| 11208 | int ret; |
| 11209 | struct kvm_kernel_irqfd *irqfd = |
| 11210 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 11211 | |
| 11212 | WARN_ON(irqfd->producer != prod); |
| 11213 | irqfd->producer = NULL; |
| 11214 | |
| 11215 | /* |
| 11216 | * When producer of consumer is unregistered, we change back to |
| 11217 | * remapped mode, so we can re-use the current implementation |
| 11218 | * when the irq is masked/disabled or the consumer side (KVM |
| 11219 | * int this case doesn't want to receive the interrupts. |
| 11220 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11221 | ret = kvm_x86_ops.update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11222 | if (ret) |
| 11223 | printk(KERN_INFO "irq bypass consumer (token %p) unregistration" |
| 11224 | " fails: %d\n", irqfd->consumer.token, ret); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11225 | |
| 11226 | kvm_arch_end_assignment(irqfd->kvm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11227 | } |
| 11228 | |
| 11229 | int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq, |
| 11230 | uint32_t guest_irq, bool set) |
| 11231 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11232 | return kvm_x86_ops.update_pi_irte(kvm, host_irq, guest_irq, set); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11233 | } |
| 11234 | |
| 11235 | bool kvm_vector_hashing_enabled(void) |
| 11236 | { |
| 11237 | return vector_hashing; |
| 11238 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11239 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11240 | bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) |
| 11241 | { |
| 11242 | return (vcpu->arch.msr_kvm_poll_control & 1) == 0; |
| 11243 | } |
| 11244 | EXPORT_SYMBOL_GPL(kvm_arch_no_poll); |
| 11245 | |
| 11246 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11247 | int kvm_spec_ctrl_test_value(u64 value) |
| 11248 | { |
| 11249 | /* |
| 11250 | * test that setting IA32_SPEC_CTRL to given value |
| 11251 | * is allowed by the host processor |
| 11252 | */ |
| 11253 | |
| 11254 | u64 saved_value; |
| 11255 | unsigned long flags; |
| 11256 | int ret = 0; |
| 11257 | |
| 11258 | local_irq_save(flags); |
| 11259 | |
| 11260 | if (rdmsrl_safe(MSR_IA32_SPEC_CTRL, &saved_value)) |
| 11261 | ret = 1; |
| 11262 | else if (wrmsrl_safe(MSR_IA32_SPEC_CTRL, value)) |
| 11263 | ret = 1; |
| 11264 | else |
| 11265 | wrmsrl(MSR_IA32_SPEC_CTRL, saved_value); |
| 11266 | |
| 11267 | local_irq_restore(flags); |
| 11268 | |
| 11269 | return ret; |
| 11270 | } |
| 11271 | EXPORT_SYMBOL_GPL(kvm_spec_ctrl_test_value); |
| 11272 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11273 | void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code) |
| 11274 | { |
| 11275 | struct x86_exception fault; |
| 11276 | u32 access = error_code & |
| 11277 | (PFERR_WRITE_MASK | PFERR_FETCH_MASK | PFERR_USER_MASK); |
| 11278 | |
| 11279 | if (!(error_code & PFERR_PRESENT_MASK) || |
| 11280 | vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, &fault) != UNMAPPED_GVA) { |
| 11281 | /* |
| 11282 | * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page |
| 11283 | * tables probably do not match the TLB. Just proceed |
| 11284 | * with the error code that the processor gave. |
| 11285 | */ |
| 11286 | fault.vector = PF_VECTOR; |
| 11287 | fault.error_code_valid = true; |
| 11288 | fault.error_code = error_code; |
| 11289 | fault.nested_page_fault = false; |
| 11290 | fault.address = gva; |
| 11291 | } |
| 11292 | vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault); |
| 11293 | } |
| 11294 | EXPORT_SYMBOL_GPL(kvm_fixup_and_inject_pf_error); |
| 11295 | |
| 11296 | /* |
| 11297 | * Handles kvm_read/write_guest_virt*() result and either injects #PF or returns |
| 11298 | * KVM_EXIT_INTERNAL_ERROR for cases not currently handled by KVM. Return value |
| 11299 | * indicates whether exit to userspace is needed. |
| 11300 | */ |
| 11301 | int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r, |
| 11302 | struct x86_exception *e) |
| 11303 | { |
| 11304 | if (r == X86EMUL_PROPAGATE_FAULT) { |
| 11305 | kvm_inject_emulated_page_fault(vcpu, e); |
| 11306 | return 1; |
| 11307 | } |
| 11308 | |
| 11309 | /* |
| 11310 | * In case kvm_read/write_guest_virt*() failed with X86EMUL_IO_NEEDED |
| 11311 | * while handling a VMX instruction KVM could've handled the request |
| 11312 | * correctly by exiting to userspace and performing I/O but there |
| 11313 | * doesn't seem to be a real use-case behind such requests, just return |
| 11314 | * KVM_EXIT_INTERNAL_ERROR for now. |
| 11315 | */ |
| 11316 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 11317 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 11318 | vcpu->run->internal.ndata = 0; |
| 11319 | |
| 11320 | return 0; |
| 11321 | } |
| 11322 | EXPORT_SYMBOL_GPL(kvm_handle_memory_failure); |
| 11323 | |
| 11324 | int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva) |
| 11325 | { |
| 11326 | bool pcid_enabled; |
| 11327 | struct x86_exception e; |
| 11328 | unsigned i; |
| 11329 | unsigned long roots_to_free = 0; |
| 11330 | struct { |
| 11331 | u64 pcid; |
| 11332 | u64 gla; |
| 11333 | } operand; |
| 11334 | int r; |
| 11335 | |
| 11336 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 11337 | if (r != X86EMUL_CONTINUE) |
| 11338 | return kvm_handle_memory_failure(vcpu, r, &e); |
| 11339 | |
| 11340 | if (operand.pcid >> 12 != 0) { |
| 11341 | kvm_inject_gp(vcpu, 0); |
| 11342 | return 1; |
| 11343 | } |
| 11344 | |
| 11345 | pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
| 11346 | |
| 11347 | switch (type) { |
| 11348 | case INVPCID_TYPE_INDIV_ADDR: |
| 11349 | if ((!pcid_enabled && (operand.pcid != 0)) || |
| 11350 | is_noncanonical_address(operand.gla, vcpu)) { |
| 11351 | kvm_inject_gp(vcpu, 0); |
| 11352 | return 1; |
| 11353 | } |
| 11354 | kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid); |
| 11355 | return kvm_skip_emulated_instruction(vcpu); |
| 11356 | |
| 11357 | case INVPCID_TYPE_SINGLE_CTXT: |
| 11358 | if (!pcid_enabled && (operand.pcid != 0)) { |
| 11359 | kvm_inject_gp(vcpu, 0); |
| 11360 | return 1; |
| 11361 | } |
| 11362 | |
| 11363 | if (kvm_get_active_pcid(vcpu) == operand.pcid) { |
| 11364 | kvm_mmu_sync_roots(vcpu); |
| 11365 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
| 11366 | } |
| 11367 | |
| 11368 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) |
| 11369 | if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].pgd) |
| 11370 | == operand.pcid) |
| 11371 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
| 11372 | |
| 11373 | kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free); |
| 11374 | /* |
| 11375 | * If neither the current cr3 nor any of the prev_roots use the |
| 11376 | * given PCID, then nothing needs to be done here because a |
| 11377 | * resync will happen anyway before switching to any other CR3. |
| 11378 | */ |
| 11379 | |
| 11380 | return kvm_skip_emulated_instruction(vcpu); |
| 11381 | |
| 11382 | case INVPCID_TYPE_ALL_NON_GLOBAL: |
| 11383 | /* |
| 11384 | * Currently, KVM doesn't mark global entries in the shadow |
| 11385 | * page tables, so a non-global flush just degenerates to a |
| 11386 | * global flush. If needed, we could optimize this later by |
| 11387 | * keeping track of global entries in shadow page tables. |
| 11388 | */ |
| 11389 | |
| 11390 | fallthrough; |
| 11391 | case INVPCID_TYPE_ALL_INCL_GLOBAL: |
| 11392 | kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); |
| 11393 | return kvm_skip_emulated_instruction(vcpu); |
| 11394 | |
| 11395 | default: |
| 11396 | BUG(); /* We have already checked above that type <= 3 */ |
| 11397 | } |
| 11398 | } |
| 11399 | EXPORT_SYMBOL_GPL(kvm_handle_invpcid); |
| 11400 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11401 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); |
| 11402 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio); |
| 11403 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); |
| 11404 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault); |
| 11405 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr); |
| 11406 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr); |
| 11407 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun); |
| 11408 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit); |
| 11409 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject); |
| 11410 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11411 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11412 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga); |
| 11413 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit); |
| 11414 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts); |
| 11415 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11416 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11417 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full); |
| 11418 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update); |
| 11419 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access); |
| 11420 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11421 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log); |
| 11422 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request); |